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