-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.h
86 lines (82 loc) · 1.57 KB
/
url.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <NTPClient.h>
#include <string>
class URL
{
private:
std::string scheme_;
std::string host_;
int port_ = 0;
std::string path_;
public:
URL(const std::string &url)
{
char const *str = url.c_str();
char const *left;
char const *right;
left = str;
right = strstr(left, "://");
if (right)
{
scheme_.assign(str, right - str);
left = right + 3;
}
right = strchr(left, '/');
if (right)
{
char const *p = strchr(left, ':');
if (p && left < p && p < right)
{
int n = 0;
char const *q = p + 1;
while (q < right)
{
if (isdigit(*q & 0xff))
{
n = n * 10 + (*q - '0');
}
else
{
n = -1;
break;
}
q++;
}
host_.assign(left, p - left);
if (n > 0 && n < 65536)
{
port_ = n;
}
}
else
{
host_.assign(left, right - left);
}
path_ = right;
}
if (port_ == 0)
{
if (scheme_ == "http")
{
port_ = 80;
}
else if (scheme_ == "https")
{
port_ = 443;
}
}
}
std::string const &scheme() const { return scheme_; }
std::string const &host() const { return host_; }
int port() const { return port_; }
std::string const &path() const { return path_; }
bool isssl() const
{
if (scheme() == "https")
return true;
if (scheme() == "http")
return false;
if (port() == 443)
return true;
return false;
}
};