180814287SRaphael Isemann //===-- UriParser.cpp -----------------------------------------------------===// 23218c0fbSVince Harron // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63218c0fbSVince Harron // 73218c0fbSVince Harron //===----------------------------------------------------------------------===// 83218c0fbSVince Harron 95f7e583bSPavel Labath #include "lldb/Utility/UriParser.h" 100e5a4147SMichał Górny #include "llvm/Support/raw_ostream.h" 114479ac15SZachary Turner 124479ac15SZachary Turner #include <string> 134479ac15SZachary Turner 1476e47d48SRaphael Isemann #include <cstdint> 154479ac15SZachary Turner #include <tuple> 16e6c5dcf5SVince Harron 17e6c5dcf5SVince Harron using namespace lldb_private; 183218c0fbSVince Harron operator <<(llvm::raw_ostream & OS,const URI & U)190e5a4147SMichał Górnyllvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &OS, 200e5a4147SMichał Górny const URI &U) { 210e5a4147SMichał Górny OS << U.scheme << "://[" << U.hostname << ']'; 220e5a4147SMichał Górny if (U.port) 23*ed8fceaaSKazu Hirata OS << ':' << *U.port; 240e5a4147SMichał Górny return OS << U.path; 250e5a4147SMichał Górny } 260e5a4147SMichał Górny Parse(llvm::StringRef uri)270e5a4147SMichał Górnyllvm::Optional<URI> URI::Parse(llvm::StringRef uri) { 280e5a4147SMichał Górny URI ret; 293218c0fbSVince Harron 30245f7fdcSZachary Turner const llvm::StringRef kSchemeSep("://"); 3154971856SOleksiy Vyalov auto pos = uri.find(kSchemeSep); 3254971856SOleksiy Vyalov if (pos == std::string::npos) 330e5a4147SMichał Górny return llvm::None; 343218c0fbSVince Harron 3554971856SOleksiy Vyalov // Extract path. 360e5a4147SMichał Górny ret.scheme = uri.substr(0, pos); 37245f7fdcSZachary Turner auto host_pos = pos + kSchemeSep.size(); 38e8433cc1SBruce Mitchener auto path_pos = uri.find('/', host_pos); 3954971856SOleksiy Vyalov if (path_pos != std::string::npos) 400e5a4147SMichał Górny ret.path = uri.substr(path_pos); 4154971856SOleksiy Vyalov else 420e5a4147SMichał Górny ret.path = "/"; 4354971856SOleksiy Vyalov 4454971856SOleksiy Vyalov auto host_port = uri.substr( 45b9c1b51eSKate Stone host_pos, 46b9c1b51eSKate Stone ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos); 4754971856SOleksiy Vyalov 4854971856SOleksiy Vyalov // Extract hostname 492434d45bSGreg Clayton if (!host_port.empty() && host_port[0] == '[') { 5054971856SOleksiy Vyalov // hostname is enclosed with square brackets. 5157be22faSEmre Kultursay pos = host_port.rfind(']'); 5254971856SOleksiy Vyalov if (pos == std::string::npos) 530e5a4147SMichał Górny return llvm::None; 5454971856SOleksiy Vyalov 550e5a4147SMichał Górny ret.hostname = host_port.substr(1, pos - 1); 56245f7fdcSZachary Turner host_port = host_port.drop_front(pos + 1); 57245f7fdcSZachary Turner if (!host_port.empty() && !host_port.consume_front(":")) 580e5a4147SMichał Górny return llvm::None; 59b9c1b51eSKate Stone } else { 600e5a4147SMichał Górny std::tie(ret.hostname, host_port) = host_port.split(':'); 6154971856SOleksiy Vyalov } 6254971856SOleksiy Vyalov 6354971856SOleksiy Vyalov // Extract port 64245f7fdcSZachary Turner if (!host_port.empty()) { 65245f7fdcSZachary Turner uint16_t port_value = 0; 66245f7fdcSZachary Turner if (host_port.getAsInteger(0, port_value)) 670e5a4147SMichał Górny return llvm::None; 680e5a4147SMichał Górny ret.port = port_value; 69b9c1b51eSKate Stone } else 700e5a4147SMichał Górny ret.port = llvm::None; 7154971856SOleksiy Vyalov 720e5a4147SMichał Górny return ret; 733218c0fbSVince Harron } 74