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 
19 //----------------------------------------------------------------------
20 // UriParser::Parse
21 //----------------------------------------------------------------------
22 bool
23 UriParser::Parse(const char* uri,
24     std::string& scheme,
25     std::string& hostname,
26     int& port,
27     std::string& path
28     )
29 {
30     char scheme_buf[100] = {0};
31     char hostname_buf[256] = {0};
32     char port_buf[11] = {0}; // 10==strlen(2^32)
33     char path_buf[2049] = {'/', 0};
34 
35     bool ok = false;
36          if (4==sscanf(uri, "%99[^:/]://%255[^/:]:%[^/]/%2047s", scheme_buf, hostname_buf, port_buf, path_buf+1)) { ok = true; }
37     else if (3==sscanf(uri, "%99[^:/]://%255[^/:]:%[^/]", scheme_buf, hostname_buf, port_buf)) { ok = true; }
38     else if (3==sscanf(uri, "%99[^:/]://%255[^/]/%2047s", scheme_buf, hostname_buf, path_buf+1)) { ok = true; }
39     else if (2==sscanf(uri, "%99[^:/]://%255[^/]", scheme_buf, hostname_buf)) { ok = true; }
40 
41     char* end = port_buf;
42     int port_tmp = strtoul(port_buf, &end, 10);
43     if (*end != 0)
44     {
45         // there are invalid characters in port_buf
46         return false;
47     }
48 
49     if (ok)
50     {
51         scheme.assign(scheme_buf);
52         hostname.assign(hostname_buf);
53         port = port_tmp;
54         path.assign(path_buf);
55     }
56     return ok;
57 }
58 
59