-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathuri.cpp
More file actions
377 lines (318 loc) · 12.1 KB
/
uri.cpp
File metadata and controls
377 lines (318 loc) · 12.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <cassert>
#include <cctype>
#include <ostream>
#include <uri>
#include <utility>
#include <vector>
#include <array>
#include <http_parser.h>
namespace uri {
static inline std::vector<char> decoded_vector(util::csview input)
{
auto res = decode(input);
return {res.begin(), res.end()};
}
///////////////////////////////////////////////////////////////////////////////
static inline bool icase_equal(util::csview lhs, util::csview rhs) noexcept {
return (lhs.size() == rhs.size())
and
std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin(), [](const char a, const char b) {
return std::tolower(a) == std::tolower(b);
});
}
///////////////////////////////////////////////////////////////////////////////
static inline uint16_t bind_port(util::csview scheme, const uint16_t port_from_uri) noexcept {
static const std::vector<std::pair<util::csview, uint16_t>> port_table
{
{"ftp", 21U},
{"http", 80U},
{"https", 443U},
{"irc", 6667U},
{"ldap", 389U},
{"nntp", 119U},
{"rtsp", 554U},
{"sip", 5060U},
{"sips", 5061U},
{"smtp", 25U},
{"ssh", 22U},
{"telnet", 23U},
{"ws", 80U},
{"wss", 443U},
{"xmpp", 5222U},
};
if (port_from_uri not_eq 0) return port_from_uri;
const auto it = std::find_if(port_table.cbegin(), port_table.cend(), [scheme](const auto& _) {
return icase_equal(_.first, scheme);
});
return (it not_eq port_table.cend()) ? it->second : 0xFFFFU;
}
///////////////////////////////////////////////////////////////////////////////
// copy helper
///////////////////////////////////////////////////////////////////////////////
static inline util::sview updated_copy(const std::vector<char>& to_copy,
util::csview view,
const std::vector<char>& from_copy)
{
// sometimes the source is empty, but we need a valid empty string
if (view.data() == nullptr) return {&to_copy.back(), 0};
return {&to_copy.data()[view.data() - from_copy.data()], view.size()};
}
///////////////////////////////////////////////////////////////////////////////
URI::URI(const char* uri, const bool parse)
: uri_str_{decoded_vector(uri)}
{
if (parse) this->parse();
}
///////////////////////////////////////////////////////////////////////////////
URI::URI(const char* uri, const size_t count, const bool parse)
: uri_str_{decoded_vector(util::csview{uri, count})}
{
if (parse) this->parse();
}
///////////////////////////////////////////////////////////////////////////////
URI::URI(const std::string& uri, const bool parse)
: uri_str_{decoded_vector(util::csview{uri.data(), uri.length()})}
{
if (parse) this->parse();
}
///////////////////////////////////////////////////////////////////////////////
URI::URI(util::csview uri, const bool parse)
: uri_str_{decoded_vector(uri)}
{
if (parse) this->parse();
}
///////////////////////////////////////////////////////////////////////////////
URI::URI(const URI& u)
: uri_str_ {u.uri_str_}
, port_ {u.port_}
, scheme_ {updated_copy(uri_str_, u.scheme_, u.uri_str_)}
, userinfo_ {updated_copy(uri_str_, u.userinfo_, u.uri_str_)}
, host_ {updated_copy(uri_str_, u.host_, u.uri_str_)}
, path_ {updated_copy(uri_str_, u.path_, u.uri_str_)}
, query_ {updated_copy(uri_str_, u.query_, u.uri_str_)}
, fragment_ {updated_copy(uri_str_, u.fragment_, u.uri_str_)}
, query_map_{}
{
for(const auto& ent : u.query_map_)
{
query_map_.emplace(updated_copy(uri_str_, ent.first, u.uri_str_),
updated_copy(uri_str_, ent.second, u.uri_str_));
}
}
///////////////////////////////////////////////////////////////////////////////
URI::URI(URI&& u) noexcept
: uri_str_{std::move(u.uri_str_)}
, port_ {u.port_}
, scheme_ {u.scheme_}
, userinfo_ {u.userinfo_}
, host_ {u.host_}
, path_ {u.path_}
, query_ {u.query_}
, fragment_ {u.fragment_}
, query_map_{std::move(u.query_map_)}
{
}
///////////////////////////////////////////////////////////////////////////////
URI& URI::operator=(const URI& u) {
uri_str_ = u.uri_str_;
port_ = u.port_;
scheme_ = updated_copy(uri_str_, u.scheme_, u.uri_str_);
userinfo_ = updated_copy(uri_str_, u.userinfo_, u.uri_str_);
host_ = updated_copy(uri_str_, u.host_, u.uri_str_);
path_ = updated_copy(uri_str_, u.path_, u.uri_str_);
query_ = updated_copy(uri_str_, u.query_, u.uri_str_);
fragment_ = updated_copy(uri_str_, u.fragment_, u.uri_str_);
query_map_.clear();
for(const auto& ent : u.query_map_) {
query_map_.emplace(updated_copy(uri_str_, ent.first, u.uri_str_),
updated_copy(uri_str_, ent.second, u.uri_str_));
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
URI& URI::operator=(URI&& u) noexcept {
uri_str_ = std::move(u.uri_str_);
port_ = u.port_;
scheme_ = u.scheme_;
userinfo_ = u.userinfo_;
host_ = u.host_;
path_ = u.path_;
query_ = u.query_;
fragment_ = u.fragment_;
query_map_ = std::move(u.query_map_);
return *this;
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::scheme() const noexcept {
return scheme_;
}
bool URI::scheme_is_secure() const noexcept {
return scheme() == "https" or scheme() == "wss";
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::userinfo() const noexcept {
return userinfo_;
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::host() const noexcept {
return host_;
}
///////////////////////////////////////////////////////////////////////////////
bool URI::host_is_ip4() const noexcept {
return host_.empty() ? false : std::isdigit(host_.back());
}
///////////////////////////////////////////////////////////////////////////////
bool URI::host_is_ip6() const noexcept {
return host_.empty() ? false : (*(host_.data() + host_.length()) == ']');
}
///////////////////////////////////////////////////////////////////////////////
std::string URI::host_and_port() const {
return std::string{host_.data(), host_.length()} + ':' + std::to_string(port_);
}
///////////////////////////////////////////////////////////////////////////////
uint16_t URI::port() const noexcept {
return port_;
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::path() const noexcept {
return path_;
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::query() const noexcept {
return query_;
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::fragment() const noexcept {
return fragment_;
}
///////////////////////////////////////////////////////////////////////////////
util::sview URI::query(util::csview key) {
if (query_map_.empty() and (not query_.empty())) {
load_queries();
}
const auto target = query_map_.find(key);
return (target not_eq query_map_.cend()) ? target->second : util::sview{};
}
///////////////////////////////////////////////////////////////////////////////
bool URI::is_valid() const noexcept {
return (not host_.empty()) or (not path_.empty()) ;
}
///////////////////////////////////////////////////////////////////////////////
URI::operator bool() const noexcept {
return is_valid();
}
///////////////////////////////////////////////////////////////////////////////
std::string URI::to_string() const {
return {uri_str_.begin(), uri_str_.end()};
}
///////////////////////////////////////////////////////////////////////////////
URI::operator std::string () const {
return to_string();
}
///////////////////////////////////////////////////////////////////////////////
URI& URI::operator<<(const std::string& chunk) {
uri_str_.insert(uri_str_.end(), chunk.begin(), chunk.end());
parse();
return *this;
}
///////////////////////////////////////////////////////////////////////////////
URI& URI::parse() {
http_parser_url u;
http_parser_url_init(&u);
const auto p = uri_str_.data();
const auto result = http_parser_parse_url(p, uri_str_.size(), 0, &u);
#ifdef URI_THROW_ON_ERROR
if (result not_eq 0) {
std::string uri{uri_str_.begin(), uri_str_.end()};
throw URI_error{"Invalid uri: " + uri};
}
#endif //< URI_THROW_ON_ERROR
(void)result;
scheme_ = (u.field_set & (1 << UF_SCHEMA)) ? util::sview{p + u.field_data[UF_SCHEMA].off, u.field_data[UF_SCHEMA].len} : util::sview{};
userinfo_ = (u.field_set & (1 << UF_USERINFO)) ? util::sview{p + u.field_data[UF_USERINFO].off, u.field_data[UF_USERINFO].len} : util::sview{};
host_ = (u.field_set & (1 << UF_HOST)) ? util::sview{p + u.field_data[UF_HOST].off, u.field_data[UF_HOST].len} : util::sview{};
path_ = (u.field_set & (1 << UF_PATH)) ? util::sview{p + u.field_data[UF_PATH].off, u.field_data[UF_PATH].len} : util::sview{};
query_ = (u.field_set & (1 << UF_QUERY)) ? util::sview{p + u.field_data[UF_QUERY].off, u.field_data[UF_QUERY].len} : util::sview{};
fragment_ = (u.field_set & (1 << UF_FRAGMENT)) ? util::sview{p + u.field_data[UF_FRAGMENT].off, u.field_data[UF_FRAGMENT].len} : util::sview{};
auto port_str_ = (u.field_set & (1 << UF_PORT)) ?
util::sview{p + u.field_data[UF_PORT].off, u.field_data[UF_PORT].len} : util::sview{};
if(not port_str_.empty())
{
std::array<char, 32> buf;
std::copy(port_str_.begin(), port_str_.end(), buf.begin());
buf[port_str_.size()] = 0;
port_ = std::atoi(buf.data());
}
else
{
port_ = bind_port(scheme_, u.port);
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
URI& URI::reset() {
new (this) URI{};
return *this;
}
///////////////////////////////////////////////////////////////////////////////
void URI::load_queries() {
auto _ = query_;
util::sview name {};
util::sview value {};
util::sview::size_type base {0U};
util::sview::size_type break_point {};
while (true) {
if ((break_point = _.find('=')) not_eq util::sview::npos) {
name = _.substr(base, break_point);
//-----------------------------------
_.remove_prefix(name.length() + 1U);
}
else {
break;
}
if ((break_point = _.find('&')) not_eq util::sview::npos) {
value = _.substr(base, break_point);
query_map_.emplace(name, value);
_.remove_prefix(value.length() + 1U);
}
else {
query_map_.emplace(name, _);
}
}
}
///////////////////////////////////////////////////////////////////////////////
bool operator<(const URI& lhs, const URI& rhs) noexcept {
return lhs.to_string() < rhs.to_string();
}
///////////////////////////////////////////////////////////////////////////////
bool operator==(const URI& lhs, const URI& rhs) noexcept {
return icase_equal(lhs.scheme(), rhs.scheme())
and (lhs.userinfo() == rhs.userinfo())
and icase_equal(lhs.host(), rhs.host())
and lhs.port() == rhs.port()
and lhs.path() == rhs.path()
and lhs.query() == rhs.query()
and lhs.fragment() == rhs.fragment();
}
///////////////////////////////////////////////////////////////////////////////
std::ostream& operator<< (std::ostream& output_device, const URI& uri) {
return output_device << uri.to_string();
}
} //< namespace uri