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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/http/URI.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/core/utils/memory/stl/AWSSet.h>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <iomanip>
using namespace Aws::Http;
using namespace Aws::Utils;
namespace Aws
{
namespace Http
{
const char* SEPARATOR = "://";
} // namespace Http
} // namespace Aws
URI::URI() : m_scheme(Scheme::HTTP), m_port(HTTP_DEFAULT_PORT)
{
}
URI::URI(const Aws::String& uri) : m_scheme(Scheme::HTTP), m_port(HTTP_DEFAULT_PORT)
{
ParseURIParts(uri);
}
URI::URI(const char* uri) : m_scheme(Scheme::HTTP), m_port(HTTP_DEFAULT_PORT)
{
ParseURIParts(uri);
}
URI& URI::operator =(const Aws::String& uri)
{
this->ParseURIParts(uri);
return *this;
}
URI& URI::operator =(const char* uri)
{
this->ParseURIParts(uri);
return *this;
}
bool URI::operator ==(const URI& other) const
{
return CompareURIParts(other);
}
bool URI::operator ==(const Aws::String& other) const
{
return CompareURIParts(other);
}
bool URI::operator ==(const char* other) const
{
return CompareURIParts(other);
}
bool URI::operator !=(const URI& other) const
{
return !(*this == other);
}
bool URI::operator !=(const Aws::String& other) const
{
return !(*this == other);
}
bool URI::operator !=(const char* other) const
{
return !(*this == other);
}
void URI::SetScheme(Scheme value)
{
assert(value == Scheme::HTTP || value == Scheme::HTTPS);
if (value == Scheme::HTTP)
{
m_port = m_port == HTTPS_DEFAULT_PORT || m_port == 0 ? HTTP_DEFAULT_PORT : m_port;
m_scheme = value;
}
else if (value == Scheme::HTTPS)
{
m_port = m_port == HTTP_DEFAULT_PORT || m_port == 0 ? HTTPS_DEFAULT_PORT : m_port;
m_scheme = value;
}
}
Aws::String URI::URLEncodePathRFC3986(const Aws::String& path)
{
if(path.empty())
{
return path;
}
const Aws::Vector<Aws::String> pathParts = StringUtils::Split(path, '/');
Aws::StringStream ss;
ss << std::hex << std::uppercase;
// escape characters appearing in a URL path according to RFC 3986
for (const auto& segment : pathParts)
{
ss << '/';
for(unsigned char c : segment) // alnum results in UB if the value of c is not unsigned char & is not EOF
{
// §2.3 unreserved characters
if (StringUtils::IsAlnum(c))
{
ss << c;
continue;
}
switch(c)
{
// §2.3 unreserved characters
case '-': case '_': case '.': case '~':
// The path section of the URL allow reserved characters to appear unescaped
// RFC 3986 §2.2 Reserved characters
// NOTE: this implementation does not accurately implement the RFC on purpose to accommodate for
// discrepancies in the implementations of URL encoding between AWS services for legacy reasons.
case '$': case '&': case ',':
case ':': case '=': case '@':
ss << c;
break;
default:
ss << '%' << std::setfill('0') << std::setw(2) << (int)((unsigned char)c) << std::setw(0);
}
}
}
//if the last character was also a slash, then add that back here.
if (path.back() == '/')
{
ss << '/';
}
return ss.str();
}
Aws::String URI::URLEncodePath(const Aws::String& path)
{
Aws::Vector<Aws::String> pathParts = StringUtils::Split(path, '/');
Aws::StringStream ss;
for (Aws::Vector<Aws::String>::iterator iter = pathParts.begin(); iter != pathParts.end(); ++iter)
{
ss << '/' << StringUtils::URLEncode(iter->c_str());
}
//if the last character was also a slash, then add that back here.
if (path.length() > 0 && path[path.length() - 1] == '/')
{
ss << '/';
}
if (path.length() > 0 && path[0] != '/')
{
return ss.str().substr(1);
}
else
{
return ss.str();
}
}
void URI::SetPath(const Aws::String& value)
{
const Aws::Vector<Aws::String> pathParts = StringUtils::Split(value, '/');
Aws::String path;
path.reserve(value.length() + 1/* in case we have to append slash before the path. */);
for (const auto& segment : pathParts)
{
path.push_back('/');
path.append(segment);
}
if (value.back() == '/')
{
path.push_back('/');
}
m_path = std::move(path);
}
//ugh, this isn't even part of the canonicalization spec. It is part of how our services have implemented their signers though....
//it doesn't really hurt anything to reorder it though, so go ahead and sort the values for parameters with the same key
void InsertValueOrderedParameter(QueryStringParameterCollection& queryParams, const Aws::String& key, const Aws::String& value)
{
auto entriesAtKey = queryParams.equal_range(key);
for (auto& entry = entriesAtKey.first; entry != entriesAtKey.second; ++entry)
{
if (entry->second > value)
{
queryParams.emplace_hint(entry, key, value);
return;
}
}
queryParams.emplace(key, value);
}
QueryStringParameterCollection URI::GetQueryStringParameters(bool decode) const
{
Aws::String queryString = GetQueryString();
QueryStringParameterCollection parameterCollection;
//if we actually have a query string
if (queryString.size() > 0)
{
size_t currentPos = 1, locationOfNextDelimiter = 1;
//while we have params left to parse
while (currentPos < queryString.size())
{
//find next key/value pair
locationOfNextDelimiter = queryString.find('&', currentPos);
Aws::String keyValuePair;
//if this isn't the last parameter
if (locationOfNextDelimiter != Aws::String::npos)
{
keyValuePair = queryString.substr(currentPos, locationOfNextDelimiter - currentPos);
}
//if it is the last parameter
else
{
keyValuePair = queryString.substr(currentPos);
}
//split on =
size_t locationOfEquals = keyValuePair.find('=');
Aws::String key = keyValuePair.substr(0, locationOfEquals);
Aws::String value = keyValuePair.substr(locationOfEquals + 1);
if(decode)
{
InsertValueOrderedParameter(parameterCollection, StringUtils::URLDecode(key.c_str()), StringUtils::URLDecode(value.c_str()));
}
else
{
InsertValueOrderedParameter(parameterCollection, key, value);
}
currentPos += keyValuePair.size() + 1;
}
}
return parameterCollection;
}
void URI::CanonicalizeQueryString()
{
QueryStringParameterCollection sortedParameters = GetQueryStringParameters(false);
Aws::StringStream queryStringStream;
bool first = true;
if(sortedParameters.size() > 0)
{
queryStringStream << "?";
}
if(m_queryString.find('=') != std::string::npos)
{
for (QueryStringParameterCollection::iterator iter = sortedParameters.begin();
iter != sortedParameters.end(); ++iter)
{
if (!first)
{
queryStringStream << "&";
}
first = false;
queryStringStream << iter->first.c_str() << "=" << iter->second.c_str();
}
m_queryString = queryStringStream.str();
}
}
void URI::AddQueryStringParameter(const char* key, const Aws::String& value)
{
if (m_queryString.size() <= 0)
{
m_queryString.append("?");
}
else
{
m_queryString.append("&");
}
m_queryString.append(StringUtils::URLEncode(key) + "=" + StringUtils::URLEncode(value.c_str()));
}
void URI::AddQueryStringParameter(const Aws::Map<Aws::String, Aws::String>& queryStringPairs)
{
for(const auto& entry: queryStringPairs)
{
AddQueryStringParameter(entry.first.c_str(), entry.second);
}
}
void URI::SetQueryString(const Aws::String& str)
{
m_queryString = "";
if (str.empty()) return;
if (str.front() != '?')
{
m_queryString.append("?").append(str);
}
else
{
m_queryString = str;
}
}
Aws::String URI::GetURIString(bool includeQueryString) const
{
assert(m_authority.size() > 0);
Aws::StringStream ss;
ss << SchemeMapper::ToString(m_scheme) << SEPARATOR << m_authority;
if (m_scheme == Scheme::HTTP && m_port != HTTP_DEFAULT_PORT)
{
ss << ":" << m_port;
}
else if (m_scheme == Scheme::HTTPS && m_port != HTTPS_DEFAULT_PORT)
{
ss << ":" << m_port;
}
if(m_path != "/")
{
ss << URLEncodePathRFC3986(m_path);
}
if(includeQueryString)
{
ss << m_queryString;
}
return ss.str();
}
void URI::ParseURIParts(const Aws::String& uri)
{
ExtractAndSetScheme(uri);
ExtractAndSetAuthority(uri);
ExtractAndSetPort(uri);
ExtractAndSetPath(uri);
ExtractAndSetQueryString(uri);
}
void URI::ExtractAndSetScheme(const Aws::String& uri)
{
size_t posOfSeparator = uri.find(SEPARATOR);
if (posOfSeparator != Aws::String::npos)
{
Aws::String schemePortion = uri.substr(0, posOfSeparator);
SetScheme(SchemeMapper::FromString(schemePortion.c_str()));
}
else
{
SetScheme(Scheme::HTTP);
}
}
void URI::ExtractAndSetAuthority(const Aws::String& uri)
{
size_t authorityStart = uri.find(SEPARATOR);
if (authorityStart == Aws::String::npos)
{
authorityStart = 0;
}
else
{
authorityStart += 3;
}
size_t posOfEndOfAuthorityPort = uri.find(':', authorityStart);
size_t posOfEndOfAuthoritySlash = uri.find('/', authorityStart);
size_t posOfEndOfAuthorityQuery = uri.find('?', authorityStart);
size_t posEndOfAuthority = (std::min)({posOfEndOfAuthorityPort, posOfEndOfAuthoritySlash, posOfEndOfAuthorityQuery});
if (posEndOfAuthority == Aws::String::npos)
{
posEndOfAuthority = uri.length();
}
SetAuthority(uri.substr(authorityStart, posEndOfAuthority - authorityStart));
}
void URI::ExtractAndSetPort(const Aws::String& uri)
{
size_t authorityStart = uri.find(SEPARATOR);
if(authorityStart == Aws::String::npos)
{
authorityStart = 0;
}
else
{
authorityStart += 3;
}
size_t positionOfPortDelimiter = uri.find(':', authorityStart);
bool hasPort = positionOfPortDelimiter != Aws::String::npos;
if ((uri.find('/', authorityStart) < positionOfPortDelimiter) || (uri.find('?', authorityStart) < positionOfPortDelimiter))
{
hasPort = false;
}
if (hasPort)
{
Aws::String strPort;
size_t i = positionOfPortDelimiter + 1;
char currentDigit = uri[i];
while (std::isdigit(currentDigit))
{
strPort += currentDigit;
currentDigit = uri[++i];
}
SetPort(static_cast<uint16_t>(atoi(strPort.c_str())));
}
}
void URI::ExtractAndSetPath(const Aws::String& uri)
{
size_t authorityStart = uri.find(SEPARATOR);
if (authorityStart == Aws::String::npos)
{
authorityStart = 0;
}
else
{
authorityStart += 3;
}
size_t pathEnd = uri.find('?');
if (pathEnd == Aws::String::npos)
{
pathEnd = uri.length();
}
Aws::String authorityAndPath = uri.substr(authorityStart, pathEnd - authorityStart);
size_t pathStart = authorityAndPath.find('/');
if (pathStart != Aws::String::npos)
{
SetPath(authorityAndPath.substr(pathStart, pathEnd - pathStart));
}
else
{
SetPath("/");
}
}
void URI::ExtractAndSetQueryString(const Aws::String& uri)
{
size_t queryStart = uri.find('?');
if (queryStart != Aws::String::npos)
{
m_queryString = uri.substr(queryStart);
}
}
Aws::String URI::GetFormParameters() const
{
if(m_queryString.length() == 0)
{
return "";
}
else
{
return m_queryString.substr(1);
}
}
bool URI::CompareURIParts(const URI& other) const
{
return m_scheme == other.m_scheme && m_authority == other.m_authority && m_path == other.m_path && m_queryString == other.m_queryString;
}
|