Repository navigation
Expand file tree
/
Copy pathCanonicalize.cpp
More file actions
105 lines (95 loc) · 3.36 KB
/
Copy pathCanonicalize.cpp
File metadata and controls
105 lines (95 loc) · 3.36 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
#include <algorithm>
#include <string>
#include <vector>
#include "ns/http/ModuleFetch.h"
// The query can be part of an HTTP module's identity (auth, content
// versioning, routing), so it is normalized only for the endpoints the client
// names. Which params are cache busters and which paths are dev endpoints is
// knowledge only the client has; guessing would silently collapse two distinct
// modules onto one registry key.
//
// `preserveQueryPrefixes` is checked before the dev-endpoint test so it also
// covers endpoints nested under a dev prefix: a framework that refetches
// recompiled component metadata under a timestamped query would otherwise
// collapse every refetch onto the boot-time key and replay stale metadata.
namespace nativescript::common::http {
std::string CanonicalizeNormalizedHttpUrlKey(
const std::string& normalizedUrl,
const CanonicalizationConfig* vocabulary) {
const size_t hashPos = normalizedUrl.find('#');
const std::string noHash = hashPos == std::string::npos
? normalizedUrl
: normalizedUrl.substr(0, hashPos);
const size_t schemePos = noHash.find("://");
if (schemePos == std::string::npos) {
const size_t q = noHash.find('?');
return q == std::string::npos ? noHash : noHash.substr(0, q);
}
const size_t pathStart = noHash.find('/', schemePos + 3);
if (pathStart == std::string::npos) {
return noHash;
}
const size_t qPos = noHash.find('?', pathStart);
const std::string originAndPath =
qPos == std::string::npos ? noHash : noHash.substr(0, qPos);
const std::string query =
qPos == std::string::npos ? std::string() : noHash.substr(qPos + 1);
if (vocabulary == nullptr) {
return noHash;
}
const std::string pathOnly = originAndPath.substr(pathStart);
for (const std::string& p : vocabulary->preserveQueryPrefixes) {
if (!p.empty() && pathOnly.find(p) != std::string::npos) {
return noHash;
}
}
bool isDevEndpoint = false;
for (const std::string& p : vocabulary->devPathPrefixes) {
if (!p.empty() && pathOnly.compare(0, p.size(), p) == 0) {
isDevEndpoint = true;
break;
}
}
if (!isDevEndpoint) {
return noHash;
}
if (query.empty()) {
return originAndPath;
}
std::vector<std::string> kept;
size_t start = 0;
while (start <= query.size()) {
const size_t amp = query.find('&', start);
const std::string pair = amp == std::string::npos
? query.substr(start)
: query.substr(start, amp - start);
if (!pair.empty()) {
const size_t eq = pair.find('=');
const std::string name =
eq == std::string::npos ? pair : pair.substr(0, eq);
const bool drop = std::find(vocabulary->stripParams.begin(),
vocabulary->stripParams.end(),
name) != vocabulary->stripParams.end();
if (!drop) {
kept.push_back(pair);
}
}
if (amp == std::string::npos) {
break;
}
start = amp + 1;
}
if (kept.empty()) {
return originAndPath;
}
std::sort(kept.begin(), kept.end());
std::string rebuilt = originAndPath + "?";
for (size_t i = 0; i < kept.size(); i++) {
if (i > 0) {
rebuilt += "&";
}
rebuilt += kept[i];
}
return rebuilt;
}
} // namespace nativescript::common::http