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 |
|
649903 |
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 |
|
|
}; |
75 |
|
|
|
76 |
|
31185 |
class URL { |
77 |
|
|
public: |
78 |
|
|
static void Parse(const char* input, |
79 |
|
|
size_t len, |
80 |
|
|
enum url_parse_state state_override, |
81 |
|
|
struct url_data* url, |
82 |
|
|
bool has_url, |
83 |
|
|
const struct url_data* base, |
84 |
|
|
bool has_base); |
85 |
|
|
|
86 |
|
31179 |
URL(const char* input, const size_t len) { |
87 |
|
31179 |
Parse(input, len, kUnknownState, &context_, false, nullptr, false); |
88 |
|
31179 |
} |
89 |
|
|
|
90 |
|
1 |
URL(const char* input, const size_t len, const URL* base) { |
91 |
✓✗ |
1 |
if (base != nullptr) |
92 |
|
1 |
Parse(input, len, kUnknownState, |
93 |
|
|
&context_, false, |
94 |
|
1 |
&(base->context_), true); |
95 |
|
|
else |
96 |
|
|
Parse(input, len, kUnknownState, &context_, false, nullptr, false); |
97 |
|
1 |
} |
98 |
|
|
|
99 |
|
2 |
URL(const char* input, const size_t len, |
100 |
|
2 |
const char* base, const size_t baselen) { |
101 |
✓✗✓✗
|
2 |
if (base != nullptr && baselen > 0) { |
102 |
|
4 |
URL _base(base, baselen); |
103 |
|
2 |
Parse(input, len, kUnknownState, |
104 |
|
|
&context_, false, |
105 |
|
4 |
&(_base.context_), true); |
106 |
|
|
} else { |
107 |
|
|
Parse(input, len, kUnknownState, &context_, false, nullptr, false); |
108 |
|
|
} |
109 |
|
2 |
} |
110 |
|
|
|
111 |
|
31174 |
explicit URL(const std::string& input) : |
112 |
|
31174 |
URL(input.c_str(), input.length()) {} |
113 |
|
|
|
114 |
|
1 |
URL(const std::string& input, const URL* base) : |
115 |
|
1 |
URL(input.c_str(), input.length(), base) {} |
116 |
|
|
|
117 |
|
|
URL(const std::string& input, const URL& base) : |
118 |
|
|
URL(input.c_str(), input.length(), &base) {} |
119 |
|
|
|
120 |
|
1 |
URL(const std::string& input, const std::string& base) : |
121 |
|
1 |
URL(input.c_str(), input.length(), base.c_str(), base.length()) {} |
122 |
|
|
|
123 |
|
9 |
int32_t flags() const { |
124 |
|
9 |
return context_.flags; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
2 |
int port() const { |
128 |
|
2 |
return context_.port; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
31169 |
const std::string& protocol() const { |
132 |
|
31169 |
return context_.scheme; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
const std::string& username() const { |
136 |
|
|
return context_.username; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
const std::string& password() const { |
140 |
|
|
return context_.password; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
7 |
const std::string& host() const { |
144 |
|
7 |
return context_.host; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
2 |
const std::string& query() const { |
148 |
|
2 |
return context_.query; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
2 |
const std::string& fragment() const { |
152 |
|
2 |
return context_.fragment; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
31169 |
std::string path() const { |
156 |
|
31169 |
std::string ret; |
157 |
✓✓ |
420923 |
for (const std::string& element : context_.path) { |
158 |
|
389744 |
ret += '/' + element; |
159 |
|
|
} |
160 |
|
31169 |
return ret; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
// Get the path of the file: URL in a format consumable by native file system |
164 |
|
|
// APIs. Returns an empty string if something went wrong. |
165 |
|
|
std::string ToFilePath() const; |
166 |
|
|
// Get the file URL from native file system path. |
167 |
|
|
static URL FromFilePath(const std::string& file_path); |
168 |
|
|
|
169 |
|
|
v8::MaybeLocal<v8::Value> ToObject(Environment* env) const; |
170 |
|
|
|
171 |
|
|
URL(const URL&) = default; |
172 |
|
|
URL& operator=(const URL&) = default; |
173 |
|
|
URL(URL&&) = default; |
174 |
|
|
URL& operator=(URL&&) = default; |
175 |
|
|
|
176 |
|
1 |
URL() : URL("") {} |
177 |
|
|
|
178 |
|
|
private: |
179 |
|
|
struct url_data context_; |
180 |
|
|
}; |
181 |
|
|
|
182 |
|
|
} // namespace url |
183 |
|
|
|
184 |
|
|
} // namespace node |
185 |
|
|
|
186 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
187 |
|
|
|
188 |
|
|
#endif // SRC_NODE_URL_H_ |