1 |
|
|
#ifndef SRC_NODE_URL_H_ |
2 |
|
|
#define SRC_NODE_URL_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "node.h" |
7 |
|
|
#include "env.h" |
8 |
|
|
|
9 |
|
|
#include <string> |
10 |
|
|
|
11 |
|
|
namespace node { |
12 |
|
|
namespace url { |
13 |
|
|
|
14 |
|
|
#define PARSESTATES(XX) \ |
15 |
|
|
XX(kSchemeStart) \ |
16 |
|
|
XX(kScheme) \ |
17 |
|
|
XX(kNoScheme) \ |
18 |
|
|
XX(kSpecialRelativeOrAuthority) \ |
19 |
|
|
XX(kPathOrAuthority) \ |
20 |
|
|
XX(kRelative) \ |
21 |
|
|
XX(kRelativeSlash) \ |
22 |
|
|
XX(kSpecialAuthoritySlashes) \ |
23 |
|
|
XX(kSpecialAuthorityIgnoreSlashes) \ |
24 |
|
|
XX(kAuthority) \ |
25 |
|
|
XX(kHost) \ |
26 |
|
|
XX(kHostname) \ |
27 |
|
|
XX(kPort) \ |
28 |
|
|
XX(kFile) \ |
29 |
|
|
XX(kFileSlash) \ |
30 |
|
|
XX(kFileHost) \ |
31 |
|
|
XX(kPathStart) \ |
32 |
|
|
XX(kPath) \ |
33 |
|
|
XX(kCannotBeBase) \ |
34 |
|
|
XX(kQuery) \ |
35 |
|
|
XX(kFragment) |
36 |
|
|
|
37 |
|
|
#define FLAGS(XX) \ |
38 |
|
|
XX(URL_FLAGS_NONE, 0) \ |
39 |
|
|
XX(URL_FLAGS_FAILED, 0x01) \ |
40 |
|
|
XX(URL_FLAGS_CANNOT_BE_BASE, 0x02) \ |
41 |
|
|
XX(URL_FLAGS_INVALID_PARSE_STATE, 0x04) \ |
42 |
|
|
XX(URL_FLAGS_TERMINATED, 0x08) \ |
43 |
|
|
XX(URL_FLAGS_SPECIAL, 0x10) \ |
44 |
|
|
XX(URL_FLAGS_HAS_USERNAME, 0x20) \ |
45 |
|
|
XX(URL_FLAGS_HAS_PASSWORD, 0x40) \ |
46 |
|
|
XX(URL_FLAGS_HAS_HOST, 0x80) \ |
47 |
|
|
XX(URL_FLAGS_HAS_PATH, 0x100) \ |
48 |
|
|
XX(URL_FLAGS_HAS_QUERY, 0x200) \ |
49 |
|
|
XX(URL_FLAGS_HAS_FRAGMENT, 0x400) \ |
50 |
|
|
XX(URL_FLAGS_IS_DEFAULT_SCHEME_PORT, 0x800) \ |
51 |
|
|
|
52 |
|
|
enum url_parse_state { |
53 |
|
|
kUnknownState = -1, |
54 |
|
|
#define XX(name) name, |
55 |
|
|
PARSESTATES(XX) |
56 |
|
|
#undef XX |
57 |
|
|
}; |
58 |
|
|
|
59 |
|
|
enum url_flags { |
60 |
|
|
#define XX(name, val) name = val, |
61 |
|
|
FLAGS(XX) |
62 |
|
|
#undef XX |
63 |
|
|
}; |
64 |
|
|
|
65 |
|
297773 |
struct url_data { |
66 |
|
|
int32_t flags = URL_FLAGS_NONE; |
67 |
|
|
int port = -1; |
68 |
|
|
std::string scheme; |
69 |
|
|
std::string username; |
70 |
|
|
std::string password; |
71 |
|
|
std::string host; |
72 |
|
|
std::string query; |
73 |
|
|
std::string fragment; |
74 |
|
|
std::vector<std::string> path; |
75 |
|
|
}; |
76 |
|
|
|
77 |
|
46013 |
class URL { |
78 |
|
|
public: |
79 |
|
|
static void Parse(const char* input, |
80 |
|
|
size_t len, |
81 |
|
|
enum url_parse_state state_override, |
82 |
|
|
struct url_data* url, |
83 |
|
|
bool has_url, |
84 |
|
|
const struct url_data* base, |
85 |
|
|
bool has_base); |
86 |
|
|
|
87 |
|
39954 |
URL(const char* input, const size_t len) { |
88 |
|
39954 |
Parse(input, len, kUnknownState, &context_, false, nullptr, false); |
89 |
|
39954 |
} |
90 |
|
|
|
91 |
|
2159 |
URL(const char* input, const size_t len, const URL* base) { |
92 |
✓✗ |
2159 |
if (base != nullptr) |
93 |
|
|
Parse(input, len, kUnknownState, |
94 |
|
|
&context_, false, |
95 |
|
2159 |
&(base->context_), true); |
96 |
|
|
else |
97 |
|
|
Parse(input, len, kUnknownState, &context_, false, nullptr, false); |
98 |
|
2159 |
} |
99 |
|
|
|
100 |
|
|
URL(const char* input, const size_t len, |
101 |
|
|
const char* base, const size_t baselen) { |
102 |
|
|
if (base != nullptr && baselen > 0) { |
103 |
|
|
URL _base(base, baselen); |
104 |
|
|
Parse(input, len, kUnknownState, |
105 |
|
|
&context_, false, |
106 |
|
|
&(_base.context_), true); |
107 |
|
|
} else { |
108 |
|
|
Parse(input, len, kUnknownState, &context_, false, nullptr, false); |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
39530 |
explicit URL(const std::string& input) : |
113 |
|
39530 |
URL(input.c_str(), input.length()) {} |
114 |
|
|
|
115 |
|
297 |
URL(const std::string& input, const URL* base) : |
116 |
|
297 |
URL(input.c_str(), input.length(), base) {} |
117 |
|
|
|
118 |
|
1862 |
URL(const std::string& input, const URL& base) : |
119 |
|
1862 |
URL(input.c_str(), input.length(), &base) {} |
120 |
|
|
|
121 |
|
|
URL(const std::string& input, const std::string& base) : |
122 |
|
|
URL(input.c_str(), input.length(), base.c_str(), base.length()) {} |
123 |
|
|
|
124 |
|
462 |
int32_t flags() { |
125 |
|
462 |
return context_.flags; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
int port() { |
129 |
|
|
return context_.port; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
39204 |
const std::string& protocol() const { |
133 |
|
39204 |
return context_.scheme; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
const std::string& username() const { |
137 |
|
|
return context_.username; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
const std::string& password() const { |
141 |
|
|
return context_.password; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
const std::string& host() const { |
145 |
|
|
return context_.host; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
const std::string& query() const { |
149 |
|
|
return context_.query; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
const std::string& fragment() const { |
153 |
|
|
return context_.fragment; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
44185 |
std::string path() const { |
157 |
|
44185 |
std::string ret; |
158 |
✓✓ |
533893 |
for (const std::string& element : context_.path) { |
159 |
|
489708 |
ret += '/' + element; |
160 |
|
|
} |
161 |
|
44185 |
return ret; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
// Get the path of the file: URL in a format consumable by native file system |
165 |
|
|
// APIs. Returns an empty string if something went wrong. |
166 |
|
|
std::string ToFilePath() const; |
167 |
|
|
// Get the file URL from native file system path. |
168 |
|
|
static URL FromFilePath(const std::string& file_path); |
169 |
|
|
|
170 |
|
|
v8::MaybeLocal<v8::Value> ToObject(Environment* env) const; |
171 |
|
|
|
172 |
|
2025 |
URL(const URL&) = default; |
173 |
|
|
URL& operator=(const URL&) = default; |
174 |
|
16 |
URL(URL&&) = default; |
175 |
|
|
URL& operator=(URL&&) = default; |
176 |
|
|
|
177 |
|
288 |
URL() : URL("") {} |
178 |
|
|
|
179 |
|
|
private: |
180 |
|
|
struct url_data context_; |
181 |
|
|
}; |
182 |
|
|
|
183 |
|
|
} // namespace url |
184 |
|
|
|
185 |
|
|
} // namespace node |
186 |
|
|
|
187 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
188 |
|
|
|
189 |
|
|
#endif // SRC_NODE_URL_H_ |