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 "lldb/Utility/UriParser.h"
17 using namespace lldb_private;
19 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
23 llvm::StringRef &hostname, int &port,
24 llvm::StringRef &path) {
25 llvm::StringRef tmp_scheme, tmp_hostname, tmp_port, tmp_path;
27 const llvm::StringRef kSchemeSep("://");
28 auto pos = uri.find(kSchemeSep);
29 if (pos == std::string::npos)
33 tmp_scheme = uri.substr(0, pos);
34 auto host_pos = pos + kSchemeSep.size();
35 auto path_pos = uri.find('/', host_pos);
36 if (path_pos != std::string::npos)
37 tmp_path = uri.substr(path_pos);
41 auto host_port = uri.substr(
43 ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos);
46 if (host_port[0] == '[') {
47 // hostname is enclosed with square brackets.
48 pos = host_port.find(']');
49 if (pos == std::string::npos)
52 tmp_hostname = host_port.substr(1, pos - 1);
53 host_port = host_port.drop_front(pos + 1);
54 if (!host_port.empty() && !host_port.consume_front(":"))
57 std::tie(tmp_hostname, host_port) = host_port.split(':');
61 if (!host_port.empty()) {
62 uint16_t port_value = 0;
63 if (host_port.getAsInteger(0, port_value))
70 hostname = tmp_hostname;