1 //===-- 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 "lldb/Utility/UriParser.h" 11 #include <cstring> 12 13 using namespace lldb_private; 14 15 //---------------------------------------------------------------------- 16 // UriParser::Parse 17 //---------------------------------------------------------------------- 18 bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme, 19 llvm::StringRef &hostname, int &port, 20 llvm::StringRef &path) { 21 llvm::StringRef tmp_scheme, tmp_hostname, tmp_port, tmp_path; 22 23 const llvm::StringRef kSchemeSep("://"); 24 auto pos = uri.find(kSchemeSep); 25 if (pos == std::string::npos) 26 return false; 27 28 // Extract path. 29 tmp_scheme = uri.substr(0, pos); 30 auto host_pos = pos + kSchemeSep.size(); 31 auto path_pos = uri.find('/', host_pos); 32 if (path_pos != std::string::npos) 33 tmp_path = uri.substr(path_pos); 34 else 35 tmp_path = "/"; 36 37 auto host_port = uri.substr( 38 host_pos, 39 ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos); 40 41 // Extract hostname 42 if (host_port[0] == '[') { 43 // hostname is enclosed with square brackets. 44 pos = host_port.find(']'); 45 if (pos == std::string::npos) 46 return false; 47 48 tmp_hostname = host_port.substr(1, pos - 1); 49 host_port = host_port.drop_front(pos + 1); 50 if (!host_port.empty() && !host_port.consume_front(":")) 51 return false; 52 } else { 53 std::tie(tmp_hostname, host_port) = host_port.split(':'); 54 } 55 56 // Extract port 57 if (!host_port.empty()) { 58 uint16_t port_value = 0; 59 if (host_port.getAsInteger(0, port_value)) 60 return false; 61 port = port_value; 62 } else 63 port = -1; 64 65 scheme = tmp_scheme; 66 hostname = tmp_hostname; 67 path = tmp_path; 68 return true; 69 } 70