1 //===-- UriParser.cpp -----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Utility/UriParser.h" 10 11 #include <string> 12 13 #include <cstdint> 14 #include <tuple> 15 16 using namespace lldb_private; 17 18 // UriParser::Parse 19 bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme, 20 llvm::StringRef &hostname, int &port, 21 llvm::StringRef &path) { 22 llvm::StringRef tmp_scheme, tmp_hostname, tmp_path; 23 24 const llvm::StringRef kSchemeSep("://"); 25 auto pos = uri.find(kSchemeSep); 26 if (pos == std::string::npos) 27 return false; 28 29 // Extract path. 30 tmp_scheme = uri.substr(0, pos); 31 auto host_pos = pos + kSchemeSep.size(); 32 auto path_pos = uri.find('/', host_pos); 33 if (path_pos != std::string::npos) 34 tmp_path = uri.substr(path_pos); 35 else 36 tmp_path = "/"; 37 38 auto host_port = uri.substr( 39 host_pos, 40 ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos); 41 42 // Extract hostname 43 if (!host_port.empty() && host_port[0] == '[') { 44 // hostname is enclosed with square brackets. 45 pos = host_port.rfind(']'); 46 if (pos == std::string::npos) 47 return false; 48 49 tmp_hostname = host_port.substr(1, pos - 1); 50 host_port = host_port.drop_front(pos + 1); 51 if (!host_port.empty() && !host_port.consume_front(":")) 52 return false; 53 } else { 54 std::tie(tmp_hostname, host_port) = host_port.split(':'); 55 } 56 57 // Extract port 58 if (!host_port.empty()) { 59 uint16_t port_value = 0; 60 if (host_port.getAsInteger(0, port_value)) 61 return false; 62 port = port_value; 63 } else 64 port = -1; 65 66 scheme = tmp_scheme; 67 hostname = tmp_hostname; 68 path = tmp_path; 69 return true; 70 } 71