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