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