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