blob: 77e16b02aaedf975f2ca78edd6d0d33b4ad464ad [file] [log] [blame]
Vince Harron3218c0f2015-01-06 23:38:24 +00001//===-- UriParser.cpp -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Utility/UriParser.h"
11
12// C Includes
Vince Harron3218c0f2015-01-06 23:38:24 +000013
14// C++ Includes
Oleksiy Vyalov54971852015-08-20 23:09:34 +000015#include <cstring>
16
Vince Harron3218c0f2015-01-06 23:38:24 +000017// Other libraries and framework includes
18// Project includes
Vince Harrone6c5dcf2015-01-15 20:57:01 +000019#include "lldb/Host/StringConvert.h"
20
21using namespace lldb_private;
Vince Harron3218c0f2015-01-06 23:38:24 +000022
23//----------------------------------------------------------------------
24// UriParser::Parse
25//----------------------------------------------------------------------
26bool
Oleksiy Vyalov54971852015-08-20 23:09:34 +000027UriParser::Parse(const std::string& uri,
28 std::string& scheme,
29 std::string& hostname,
30 int& port,
31 std::string& path)
Vince Harron3218c0f2015-01-06 23:38:24 +000032{
Oleksiy Vyalov54971852015-08-20 23:09:34 +000033 std::string tmp_scheme, tmp_hostname, tmp_port, tmp_path;
Vince Harron3218c0f2015-01-06 23:38:24 +000034
Oleksiy Vyalov54971852015-08-20 23:09:34 +000035 static const char* kSchemeSep = "://";
36 auto pos = uri.find(kSchemeSep);
37 if (pos == std::string::npos)
38 return false;
39
40 // Extract path.
41 tmp_scheme = uri.substr(0, pos);
42 auto host_pos = pos + strlen(kSchemeSep);
Bruce Mitchenere8433cc2015-09-01 23:57:17 +000043 auto path_pos = uri.find('/', host_pos);
Oleksiy Vyalov54971852015-08-20 23:09:34 +000044 if (path_pos != std::string::npos)
45 tmp_path = uri.substr(path_pos);
46 else
47 tmp_path = "/";
48
49 auto host_port = uri.substr(
50 host_pos, ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos);
51
52 // Extract hostname
53 if (host_port[0] == '[')
Vince Harron3218c0f2015-01-06 23:38:24 +000054 {
Oleksiy Vyalov54971852015-08-20 23:09:34 +000055 // hostname is enclosed with square brackets.
56 pos = host_port.find(']');
57 if (pos == std::string::npos)
58 return false;
59
60 tmp_hostname = host_port.substr(1, pos - 1);
61 host_port.erase(0, pos + 1);
62 }
63 else
64 {
65 pos = host_port.find(':');
66 tmp_hostname = host_port.substr(0, (pos != std::string::npos) ? pos : host_port.size());
67 host_port.erase(0, (pos != std::string::npos) ? pos : host_port.size());
68 }
69
70 // Extract port
71 tmp_port = host_port;
72 if (!tmp_port.empty())
73 {
74 if (tmp_port[0] != ':')
75 return false;
76 tmp_port = tmp_port.substr(1);
77 bool success = false;
78 auto port_tmp = StringConvert::ToUInt32(tmp_port.c_str(), UINT32_MAX, 10, &success);
Vince Harrone6c5dcf2015-01-15 20:57:01 +000079 if (!success || port_tmp > 65535)
80 {
81 // there are invalid characters in port_buf
82 return false;
83 }
Vince Harron3218c0f2015-01-06 23:38:24 +000084 port = port_tmp;
Vince Harron3218c0f2015-01-06 23:38:24 +000085 }
Oleksiy Vyalov54971852015-08-20 23:09:34 +000086 else
87 port = -1;
88
89 scheme = tmp_scheme;
90 hostname = tmp_hostname;
91 path = tmp_path;
92 return true;
Vince Harron3218c0f2015-01-06 23:38:24 +000093}
94