1 //===-- UriParser.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Utility/UriParser.h"
17 // Other libraries and framework includes
19 #include "lldb/Host/StringConvert.h"
21 using namespace lldb_private;
23 //----------------------------------------------------------------------
25 //----------------------------------------------------------------------
27 UriParser::Parse(const std::string& uri,
29 std::string& hostname,
33 std::string tmp_scheme, tmp_hostname, tmp_port, tmp_path;
35 static const char* kSchemeSep = "://";
36 auto pos = uri.find(kSchemeSep);
37 if (pos == std::string::npos)
41 tmp_scheme = uri.substr(0, pos);
42 auto host_pos = pos + strlen(kSchemeSep);
43 auto path_pos = uri.find('/', host_pos);
44 if (path_pos != std::string::npos)
45 tmp_path = uri.substr(path_pos);
49 auto host_port = uri.substr(
50 host_pos, ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos);
53 if (host_port[0] == '[')
55 // hostname is enclosed with square brackets.
56 pos = host_port.find(']');
57 if (pos == std::string::npos)
60 tmp_hostname = host_port.substr(1, pos - 1);
61 host_port.erase(0, pos + 1);
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());
72 if (!tmp_port.empty())
74 if (tmp_port[0] != ':')
76 tmp_port = tmp_port.substr(1);
78 auto port_tmp = StringConvert::ToUInt32(tmp_port.c_str(), UINT32_MAX, 10, &success);
79 if (!success || port_tmp > 65535)
81 // there are invalid characters in port_buf
90 hostname = tmp_hostname;