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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include "coding/url.hpp"
#include <string_view>
#include "coding/hex.hpp"
#include "base/assert.hpp"
namespace url
{
Url::Url(std::string_view url)
{
if (!Parse(url))
ASSERT(m_scheme.empty() && m_host.empty() && m_path.empty() && !IsValid(), ());
}
Url Url::FromString(std::string_view url)
{
if (url.starts_with("http://") || url.starts_with("https://"))
return Url(url);
return Url("https://" + std::string(url));
}
bool Url::Parse(std::string_view url)
{
static constexpr size_t kNotFound = std::string_view::npos;
// Get url scheme.
size_t start = url.find(':');
if (start == kNotFound || start == 0)
return false;
m_scheme = url.substr(0, start);
// Skip slashes.
start = url.find_first_not_of('/', start + 1);
if (start == kNotFound)
return true;
// Get host.
size_t end = url.find_first_of("/?#", start);
if (end == kNotFound)
{
m_host = url.substr(start);
return true;
}
else
m_host = url.substr(start, end - start);
// Get path.
if (url[end] == '/')
{
// Skip slashes.
start = url.find_first_not_of('/', end);
if (start == kNotFound)
return true;
end = url.find_first_of("?#", start);
if (end == kNotFound)
{
m_path = url.substr(start);
return true;
}
else
m_path = url.substr(start, end - start);
}
// Parse query/fragment for keys and values.
for (start = end + 1; start < url.size();)
{
end = url.find_first_of("&#", start);
if (end == kNotFound)
end = url.size();
// Skip empty keys.
if (end != start)
{
size_t const eq = url.find('=', start);
std::string key, value;
if (eq != kNotFound && eq < end)
{
key = UrlDecode(url.substr(start, eq - start));
value = UrlDecode(url.substr(eq + 1, end - eq - 1));
}
else
{
key = UrlDecode(url.substr(start, end - start));
}
m_params.emplace_back(key, value);
}
start = end + 1;
}
return true;
}
std::string Join(std::string const & lhs, std::string const & rhs)
{
if (lhs.empty())
return rhs;
if (rhs.empty())
return lhs;
if (lhs.back() == '/' && rhs.front() == '/')
return lhs + rhs.substr(1);
if (lhs.back() != '/' && rhs.front() != '/')
return lhs + '/' + rhs;
return lhs + rhs;
}
std::string UrlEncode(std::string_view component)
{
size_t const count = component.size();
std::string result;
result.reserve(count);
for (auto const c : component)
if (c < '-' || c == '/' || (c > '9' && c < 'A') || (c > 'Z' && c < '_') || c == '`' || (c > 'z' && c < '~') ||
c > '~')
{
result += '%';
result += NumToHex(c);
}
else
result += c;
return result;
}
std::string UrlDecode(std::string_view encodedUrl)
{
size_t const count = encodedUrl.size();
std::string result;
result.reserve(count);
for (size_t i = 0; i < count; ++i)
{
if (encodedUrl[i] == '%')
{
result += FromHex(encodedUrl.substr(i + 1, 2));
i += 2;
}
else if (encodedUrl[i] == '+')
{
result += ' ';
}
else
{
result += encodedUrl[i];
}
}
return result;
}
} // namespace url
|