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 "Utility/UriParser.h"
11 
12 // C Includes
13 #include <stdlib.h>
14 
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Host/StringConvert.h"
19 
20 using namespace lldb_private;
21 
22 //----------------------------------------------------------------------
23 // UriParser::Parse
24 //----------------------------------------------------------------------
25 bool
26 UriParser::Parse(const char* uri,
27     std::string& scheme,
28     std::string& hostname,
29     int& port,
30     std::string& path
31     )
32 {
33     char scheme_buf[100] = {0};
34     char hostname_buf[256] = {0};
35     char port_buf[11] = {0}; // 10==strlen(2^32)
36     char path_buf[2049] = {'/', 0};
37 
38     bool ok = false;
39          if (4==sscanf(uri, "%99[^:/]://%255[^/:]:%10[^/]/%2047s", scheme_buf, hostname_buf, port_buf, path_buf+1)) { ok = true; }
40     else if (3==sscanf(uri, "%99[^:/]://%255[^/:]:%10[^/]", scheme_buf, hostname_buf, port_buf)) { ok = true; }
41     else if (3==sscanf(uri, "%99[^:/]://%255[^/]/%2047s", scheme_buf, hostname_buf, path_buf+1)) { ok = true; }
42     else if (2==sscanf(uri, "%99[^:/]://%255[^/]", scheme_buf, hostname_buf)) { ok = true; }
43 
44     bool success = false;
45     int port_tmp = -1;
46     if (port_buf[0])
47     {
48         port_tmp = StringConvert::ToUInt32(port_buf, UINT32_MAX, 10, &success);
49         if (!success || port_tmp > 65535)
50         {
51             // there are invalid characters in port_buf
52             return false;
53         }
54     }
55 
56     if (ok)
57     {
58         scheme.assign(scheme_buf);
59         hostname.assign(hostname_buf);
60         port = port_tmp;
61         path.assign(path_buf);
62     }
63     return ok;
64 }
65 
66