1 //===-- UriParser.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/UriParser.h"
10 #include "llvm/Support/raw_ostream.h"
17 using namespace lldb_private
;
19 llvm::raw_ostream
&lldb_private::operator<<(llvm::raw_ostream
&OS
,
21 OS
<< U
.scheme
<< "://[" << U
.hostname
<< ']';
23 OS
<< ':' << U
.port
.getValue();
27 llvm::Optional
<URI
> URI::Parse(llvm::StringRef uri
) {
30 const llvm::StringRef
kSchemeSep("://");
31 auto pos
= uri
.find(kSchemeSep
);
32 if (pos
== std::string::npos
)
36 ret
.scheme
= uri
.substr(0, pos
);
37 auto host_pos
= pos
+ kSchemeSep
.size();
38 auto path_pos
= uri
.find('/', host_pos
);
39 if (path_pos
!= std::string::npos
)
40 ret
.path
= uri
.substr(path_pos
);
44 auto host_port
= uri
.substr(
46 ((path_pos
!= std::string::npos
) ? path_pos
: uri
.size()) - host_pos
);
49 if (!host_port
.empty() && host_port
[0] == '[') {
50 // hostname is enclosed with square brackets.
51 pos
= host_port
.rfind(']');
52 if (pos
== std::string::npos
)
55 ret
.hostname
= host_port
.substr(1, pos
- 1);
56 host_port
= host_port
.drop_front(pos
+ 1);
57 if (!host_port
.empty() && !host_port
.consume_front(":"))
60 std::tie(ret
.hostname
, host_port
) = host_port
.split(':');
64 if (!host_port
.empty()) {
65 uint16_t port_value
= 0;
66 if (host_port
.getAsInteger(0, port_value
))
68 ret
.port
= port_value
;
70 ret
.port
= llvm::None
;