LLDB  mainline
UriParser.cpp
Go to the documentation of this file.
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 
10 #include "llvm/Support/raw_ostream.h"
11 
12 #include <string>
13 
14 #include <cstdint>
15 #include <tuple>
16 
17 using namespace lldb_private;
18 
19 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &OS,
20  const URI &U) {
21  OS << U.scheme << "://[" << U.hostname << ']';
22  if (U.port)
23  OS << ':' << *U.port;
24  return OS << U.path;
25 }
26 
27 llvm::Optional<URI> URI::Parse(llvm::StringRef uri) {
28  URI ret;
29 
30  const llvm::StringRef kSchemeSep("://");
31  auto pos = uri.find(kSchemeSep);
32  if (pos == std::string::npos)
33  return llvm::None;
34 
35  // Extract path.
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);
41  else
42  ret.path = "/";
43 
44  auto host_port = uri.substr(
45  host_pos,
46  ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos);
47 
48  // Extract hostname
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)
53  return llvm::None;
54 
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(":"))
58  return llvm::None;
59  } else {
60  std::tie(ret.hostname, host_port) = host_port.split(':');
61  }
62 
63  // Extract port
64  if (!host_port.empty()) {
65  uint16_t port_value = 0;
66  if (host_port.getAsInteger(0, port_value))
67  return llvm::None;
68  ret.port = port_value;
69  } else
70  ret.port = llvm::None;
71 
72  return ret;
73 }
lldb_private::URI::port
llvm::Optional< uint16_t > port
Definition: UriParser.h:20
lldb_private::URI::Parse
static llvm::Optional< URI > Parse(llvm::StringRef uri)
Definition: UriParser.cpp:27
lldb_private::URI::scheme
llvm::StringRef scheme
Definition: UriParser.h:18
lldb_private::URI
Definition: UriParser.h:17
lldb_private::URI::path
llvm::StringRef path
Definition: UriParser.h:21
lldb_private::URI::hostname
llvm::StringRef hostname
Definition: UriParser.h:19
lldb_private::operator<<
Stream & operator<<(Stream &s, const SourceLocationSpec &loc)
Dump a SourceLocationSpec object to a stream.
Definition: SourceLocationSpec.cpp:40
uint16_t
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
UriParser.h
lldb_private::LLDBLog::OS
@ OS