GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_url.cc Lines: 1195 1247 95.8 %
Date: 2021-02-11 04:11:15 Branches: 1083 1227 88.3 %

Line Branch Exec Source
1
#include "node_url.h"
2
#include "base_object-inl.h"
3
#include "node_errors.h"
4
#include "node_external_reference.h"
5
#include "node_i18n.h"
6
#include "util-inl.h"
7
8
#include <cmath>
9
#include <cstdio>
10
#include <string>
11
#include <vector>
12
13
namespace node {
14
15
using errors::TryCatchScope;
16
17
using v8::Array;
18
using v8::Context;
19
using v8::Function;
20
using v8::FunctionCallbackInfo;
21
using v8::HandleScope;
22
using v8::Int32;
23
using v8::Integer;
24
using v8::Isolate;
25
using v8::Local;
26
using v8::MaybeLocal;
27
using v8::NewStringType;
28
using v8::Null;
29
using v8::Object;
30
using v8::String;
31
using v8::Undefined;
32
using v8::Value;
33
34
114572
Local<String> Utf8String(Isolate* isolate, const std::string& str) {
35
229144
  return String::NewFromUtf8(isolate,
36
                             str.data(),
37
                             NewStringType::kNormal,
38
229144
                             str.length()).ToLocalChecked();
39
}
40
41
namespace url {
42
43
namespace {
44
45
// https://url.spec.whatwg.org/#eof-code-point
46
constexpr char kEOL = -1;
47
48
// Used in ToUSVString().
49
constexpr char16_t kUnicodeReplacementCharacter = 0xFFFD;
50
51
// https://url.spec.whatwg.org/#concept-host
52
3731
class URLHost {
53
 public:
54
  ~URLHost();
55
56
  void ParseIPv4Host(const char* input, size_t length, bool* is_ipv4);
57
  void ParseIPv6Host(const char* input, size_t length);
58
  void ParseOpaqueHost(const char* input, size_t length);
59
  void ParseHost(const char* input,
60
                 size_t length,
61
                 bool is_special,
62
                 bool unicode = false);
63
64
3731
  bool ParsingFailed() const { return type_ == HostType::H_FAILED; }
65
  std::string ToString() const;
66
  // Like ToString(), but avoids a copy in exchange for invalidating `*this`.
67
  std::string ToStringMove();
68
69
 private:
70
  enum class HostType {
71
    H_FAILED,
72
    H_DOMAIN,
73
    H_IPV4,
74
    H_IPV6,
75
    H_OPAQUE,
76
  };
77
78
  union Value {
79
    std::string domain_or_opaque;
80
    uint32_t ipv4;
81
    uint16_t ipv6[8];
82
83
3731
    ~Value() {}
84
3731
    Value() : ipv4(0) {}
85
  };
86
87
  Value value_;
88
  HostType type_ = HostType::H_FAILED;
89
90
10542
  void Reset() {
91
    using string = std::string;
92
10542
    switch (type_) {
93
      case HostType::H_DOMAIN:
94
      case HostType::H_OPAQUE:
95
3367
        value_.domain_or_opaque.~string();
96
3367
        break;
97
      default:
98
7175
        break;
99
    }
100
10542
    type_ = HostType::H_FAILED;
101
10542
  }
102
103
  // Setting the string members of the union with = is brittle because
104
  // it relies on them being initialized to a state that requires no
105
  // destruction of old data.
106
  // For a long time, that worked well enough because ParseIPv6Host() happens
107
  // to zero-fill `value_`, but that really is relying on standard library
108
  // internals too much.
109
  // These helpers are the easiest solution but we might want to consider
110
  // just not forcing strings into an union.
111
288
  void SetOpaque(std::string&& string) {
112
288
    Reset();
113
288
    type_ = HostType::H_OPAQUE;
114
288
    new(&value_.domain_or_opaque) std::string(std::move(string));
115
288
  }
116
117
3079
  void SetDomain(std::string&& string) {
118
3079
    Reset();
119
3079
    type_ = HostType::H_DOMAIN;
120
3079
    new(&value_.domain_or_opaque) std::string(std::move(string));
121
3079
  }
122
};
123
124
7462
URLHost::~URLHost() {
125
3731
  Reset();
126
3731
}
127
128
#define ARGS(XX)                                                              \
129
  XX(ARG_FLAGS)                                                               \
130
  XX(ARG_PROTOCOL)                                                            \
131
  XX(ARG_USERNAME)                                                            \
132
  XX(ARG_PASSWORD)                                                            \
133
  XX(ARG_HOST)                                                                \
134
  XX(ARG_PORT)                                                                \
135
  XX(ARG_PATH)                                                                \
136
  XX(ARG_QUERY)                                                               \
137
  XX(ARG_FRAGMENT)                                                            \
138
  XX(ARG_COUNT)  // This one has to be last.
139
140
#define ERR_ARGS(XX)                                                          \
141
  XX(ERR_ARG_FLAGS)                                                           \
142
  XX(ERR_ARG_INPUT)                                                           \
143
144
enum url_cb_args {
145
#define XX(name) name,
146
  ARGS(XX)
147
#undef XX
148
};
149
150
enum url_error_cb_args {
151
#define XX(name) name,
152
  ERR_ARGS(XX)
153
#undef XX
154
};
155
156
#define CHAR_TEST(bits, name, expr)                                           \
157
  template <typename T>                                                       \
158
  bool name(const T ch) {                                              \
159
    static_assert(sizeof(ch) >= (bits) / 8,                                   \
160
                  "Character must be wider than " #bits " bits");             \
161
    return (expr);                                                            \
162
  }
163
164
#define TWO_CHAR_STRING_TEST(bits, name, expr)                                \
165
  template <typename T>                                                       \
166
  bool name(const T ch1, const T ch2) {                                \
167
    static_assert(sizeof(ch1) >= (bits) / 8,                                  \
168
                  "Character must be wider than " #bits " bits");             \
169
    return (expr);                                                            \
170
  }                                                                           \
171
  template <typename T>                                                       \
172
  bool name(const std::basic_string<T>& str) {                         \
173
    static_assert(sizeof(str[0]) >= (bits) / 8,                               \
174
                  "Character must be wider than " #bits " bits");             \
175
    return str.length() >= 2 && name(str[0], str[1]);                         \
176
  }
177
178
// https://infra.spec.whatwg.org/#ascii-tab-or-newline
179

13099365
CHAR_TEST(8, IsASCIITabOrNewline, (ch == '\t' || ch == '\n' || ch == '\r'))
180
181
// https://infra.spec.whatwg.org/#c0-control-or-space
182

228438
CHAR_TEST(8, IsC0ControlOrSpace, (ch >= '\0' && ch <= ' '))
183
184
// https://infra.spec.whatwg.org/#ascii-digit
185

457253
CHAR_TEST(8, IsASCIIDigit, (ch >= '0' && ch <= '9'))
186
187
// https://infra.spec.whatwg.org/#ascii-hex-digit
188


976
CHAR_TEST(8, IsASCIIHexDigit, (IsASCIIDigit(ch) ||
189
                               (ch >= 'A' && ch <= 'F') ||
190
                               (ch >= 'a' && ch <= 'f')))
191
192
// https://infra.spec.whatwg.org/#ascii-alpha
193


1012753
CHAR_TEST(8, IsASCIIAlpha, ((ch >= 'A' && ch <= 'Z') ||
194
                            (ch >= 'a' && ch <= 'z')))
195
196
// https://infra.spec.whatwg.org/#ascii-alphanumeric
197

447649
CHAR_TEST(8, IsASCIIAlphanumeric, (IsASCIIDigit(ch) || IsASCIIAlpha(ch)))
198
199
// https://infra.spec.whatwg.org/#ascii-lowercase
200
template <typename T>
201
447691
T ASCIILowercase(T ch) {
202
447691
  return IsASCIIAlpha(ch) ? (ch | 0x20) : ch;
203
}
204
205
// https://url.spec.whatwg.org/#forbidden-host-code-point
206








84110
CHAR_TEST(8, IsForbiddenHostCodePoint,
207
          ch == '\0' || ch == '\t' || ch == '\n' || ch == '\r' ||
208
          ch == ' ' || ch == '#' || ch == '%' || ch == '/' ||
209
          ch == ':' || ch == '?' || ch == '@' || ch == '[' ||
210
          ch == '<' || ch == '>' || ch == '\\' || ch == ']' ||
211
          ch == '^')
212
213
// https://url.spec.whatwg.org/#windows-drive-letter
214


2841
TWO_CHAR_STRING_TEST(8, IsWindowsDriveLetter,
215
                     (IsASCIIAlpha(ch1) && (ch2 == ':' || ch2 == '|')))
216
217
// https://url.spec.whatwg.org/#normalized-windows-drive-letter
218


882
TWO_CHAR_STRING_TEST(8, IsNormalizedWindowsDriveLetter,
219
                     (IsASCIIAlpha(ch1) && ch2 == ':'))
220
221
// If a UTF-16 character is a low/trailing surrogate.
222
3
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)
223
224
// If a UTF-16 character is a surrogate.
225
31
CHAR_TEST(16, IsUnicodeSurrogate, (ch & 0xF800) == 0xD800)
226
227
// If a UTF-16 surrogate is a low/trailing one.
228
18
CHAR_TEST(16, IsUnicodeSurrogateTrail, (ch & 0x400) != 0)
229
230
#undef CHAR_TEST
231
#undef TWO_CHAR_STRING_TEST
232
233
const char* hex[256] = {
234
  "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
235
  "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
236
  "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
237
  "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F",
238
  "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
239
  "%28", "%29", "%2A", "%2B", "%2C", "%2D", "%2E", "%2F",
240
  "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
241
  "%38", "%39", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F",
242
  "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
243
  "%48", "%49", "%4A", "%4B", "%4C", "%4D", "%4E", "%4F",
244
  "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
245
  "%58", "%59", "%5A", "%5B", "%5C", "%5D", "%5E", "%5F",
246
  "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
247
  "%68", "%69", "%6A", "%6B", "%6C", "%6D", "%6E", "%6F",
248
  "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
249
  "%78", "%79", "%7A", "%7B", "%7C", "%7D", "%7E", "%7F",
250
  "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
251
  "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F",
252
  "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
253
  "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F",
254
  "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7",
255
  "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF",
256
  "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7",
257
  "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF",
258
  "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7",
259
  "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF",
260
  "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7",
261
  "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF",
262
  "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7",
263
  "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF",
264
  "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7",
265
  "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF"
266
};
267
268
const uint8_t C0_CONTROL_ENCODE_SET[32] = {
269
  // 00     01     02     03     04     05     06     07
270
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
271
  // 08     09     0A     0B     0C     0D     0E     0F
272
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
273
  // 10     11     12     13     14     15     16     17
274
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
275
  // 18     19     1A     1B     1C     1D     1E     1F
276
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
277
  // 20     21     22     23     24     25     26     27
278
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
279
  // 28     29     2A     2B     2C     2D     2E     2F
280
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
281
  // 30     31     32     33     34     35     36     37
282
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
283
  // 38     39     3A     3B     3C     3D     3E     3F
284
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
285
  // 40     41     42     43     44     45     46     47
286
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
287
  // 48     49     4A     4B     4C     4D     4E     4F
288
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
289
  // 50     51     52     53     54     55     56     57
290
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
291
  // 58     59     5A     5B     5C     5D     5E     5F
292
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
293
  // 60     61     62     63     64     65     66     67
294
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
295
  // 68     69     6A     6B     6C     6D     6E     6F
296
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
297
  // 70     71     72     73     74     75     76     77
298
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
299
  // 78     79     7A     7B     7C     7D     7E     7F
300
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x80,
301
  // 80     81     82     83     84     85     86     87
302
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
303
  // 88     89     8A     8B     8C     8D     8E     8F
304
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
305
  // 90     91     92     93     94     95     96     97
306
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
307
  // 98     99     9A     9B     9C     9D     9E     9F
308
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
309
  // A0     A1     A2     A3     A4     A5     A6     A7
310
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
311
  // A8     A9     AA     AB     AC     AD     AE     AF
312
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
313
  // B0     B1     B2     B3     B4     B5     B6     B7
314
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
315
  // B8     B9     BA     BB     BC     BD     BE     BF
316
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
317
  // C0     C1     C2     C3     C4     C5     C6     C7
318
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
319
  // C8     C9     CA     CB     CC     CD     CE     CF
320
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
321
  // D0     D1     D2     D3     D4     D5     D6     D7
322
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
323
  // D8     D9     DA     DB     DC     DD     DE     DF
324
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
325
  // E0     E1     E2     E3     E4     E5     E6     E7
326
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
327
  // E8     E9     EA     EB     EC     ED     EE     EF
328
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
329
  // F0     F1     F2     F3     F4     F5     F6     F7
330
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
331
  // F8     F9     FA     FB     FC     FD     FE     FF
332
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
333
};
334
335
const uint8_t FRAGMENT_ENCODE_SET[32] = {
336
  // 00     01     02     03     04     05     06     07
337
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
338
  // 08     09     0A     0B     0C     0D     0E     0F
339
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
340
  // 10     11     12     13     14     15     16     17
341
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
342
  // 18     19     1A     1B     1C     1D     1E     1F
343
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
344
  // 20     21     22     23     24     25     26     27
345
    0x01 | 0x00 | 0x04 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
346
  // 28     29     2A     2B     2C     2D     2E     2F
347
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
348
  // 30     31     32     33     34     35     36     37
349
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
350
  // 38     39     3A     3B     3C     3D     3E     3F
351
    0x00 | 0x00 | 0x00 | 0x00 | 0x10 | 0x00 | 0x40 | 0x00,
352
  // 40     41     42     43     44     45     46     47
353
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
354
  // 48     49     4A     4B     4C     4D     4E     4F
355
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
356
  // 50     51     52     53     54     55     56     57
357
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
358
  // 58     59     5A     5B     5C     5D     5E     5F
359
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
360
  // 60     61     62     63     64     65     66     67
361
    0x01 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
362
  // 68     69     6A     6B     6C     6D     6E     6F
363
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
364
  // 70     71     72     73     74     75     76     77
365
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
366
  // 78     79     7A     7B     7C     7D     7E     7F
367
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x80,
368
  // 80     81     82     83     84     85     86     87
369
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
370
  // 88     89     8A     8B     8C     8D     8E     8F
371
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
372
  // 90     91     92     93     94     95     96     97
373
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
374
  // 98     99     9A     9B     9C     9D     9E     9F
375
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
376
  // A0     A1     A2     A3     A4     A5     A6     A7
377
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
378
  // A8     A9     AA     AB     AC     AD     AE     AF
379
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
380
  // B0     B1     B2     B3     B4     B5     B6     B7
381
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
382
  // B8     B9     BA     BB     BC     BD     BE     BF
383
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
384
  // C0     C1     C2     C3     C4     C5     C6     C7
385
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
386
  // C8     C9     CA     CB     CC     CD     CE     CF
387
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
388
  // D0     D1     D2     D3     D4     D5     D6     D7
389
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
390
  // D8     D9     DA     DB     DC     DD     DE     DF
391
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
392
  // E0     E1     E2     E3     E4     E5     E6     E7
393
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
394
  // E8     E9     EA     EB     EC     ED     EE     EF
395
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
396
  // F0     F1     F2     F3     F4     F5     F6     F7
397
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
398
  // F8     F9     FA     FB     FC     FD     FE     FF
399
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
400
};
401
402
403
const uint8_t PATH_ENCODE_SET[32] = {
404
  // 00     01     02     03     04     05     06     07
405
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
406
  // 08     09     0A     0B     0C     0D     0E     0F
407
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
408
  // 10     11     12     13     14     15     16     17
409
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
410
  // 18     19     1A     1B     1C     1D     1E     1F
411
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
412
  // 20     21     22     23     24     25     26     27
413
    0x01 | 0x00 | 0x04 | 0x08 | 0x00 | 0x00 | 0x00 | 0x00,
414
  // 28     29     2A     2B     2C     2D     2E     2F
415
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
416
  // 30     31     32     33     34     35     36     37
417
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
418
  // 38     39     3A     3B     3C     3D     3E     3F
419
    0x00 | 0x00 | 0x00 | 0x00 | 0x10 | 0x00 | 0x40 | 0x80,
420
  // 40     41     42     43     44     45     46     47
421
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
422
  // 48     49     4A     4B     4C     4D     4E     4F
423
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
424
  // 50     51     52     53     54     55     56     57
425
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
426
  // 58     59     5A     5B     5C     5D     5E     5F
427
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
428
  // 60     61     62     63     64     65     66     67
429
    0x01 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
430
  // 68     69     6A     6B     6C     6D     6E     6F
431
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
432
  // 70     71     72     73     74     75     76     77
433
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
434
  // 78     79     7A     7B     7C     7D     7E     7F
435
    0x00 | 0x00 | 0x00 | 0x08 | 0x00 | 0x20 | 0x00 | 0x80,
436
  // 80     81     82     83     84     85     86     87
437
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
438
  // 88     89     8A     8B     8C     8D     8E     8F
439
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
440
  // 90     91     92     93     94     95     96     97
441
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
442
  // 98     99     9A     9B     9C     9D     9E     9F
443
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
444
  // A0     A1     A2     A3     A4     A5     A6     A7
445
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
446
  // A8     A9     AA     AB     AC     AD     AE     AF
447
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
448
  // B0     B1     B2     B3     B4     B5     B6     B7
449
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
450
  // B8     B9     BA     BB     BC     BD     BE     BF
451
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
452
  // C0     C1     C2     C3     C4     C5     C6     C7
453
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
454
  // C8     C9     CA     CB     CC     CD     CE     CF
455
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
456
  // D0     D1     D2     D3     D4     D5     D6     D7
457
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
458
  // D8     D9     DA     DB     DC     DD     DE     DF
459
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
460
  // E0     E1     E2     E3     E4     E5     E6     E7
461
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
462
  // E8     E9     EA     EB     EC     ED     EE     EF
463
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
464
  // F0     F1     F2     F3     F4     F5     F6     F7
465
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
466
  // F8     F9     FA     FB     FC     FD     FE     FF
467
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
468
};
469
470
const uint8_t USERINFO_ENCODE_SET[32] = {
471
  // 00     01     02     03     04     05     06     07
472
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
473
  // 08     09     0A     0B     0C     0D     0E     0F
474
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
475
  // 10     11     12     13     14     15     16     17
476
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
477
  // 18     19     1A     1B     1C     1D     1E     1F
478
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
479
  // 20     21     22     23     24     25     26     27
480
    0x01 | 0x00 | 0x04 | 0x08 | 0x00 | 0x00 | 0x00 | 0x00,
481
  // 28     29     2A     2B     2C     2D     2E     2F
482
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x80,
483
  // 30     31     32     33     34     35     36     37
484
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
485
  // 38     39     3A     3B     3C     3D     3E     3F
486
    0x00 | 0x00 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
487
  // 40     41     42     43     44     45     46     47
488
    0x01 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
489
  // 48     49     4A     4B     4C     4D     4E     4F
490
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
491
  // 50     51     52     53     54     55     56     57
492
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
493
  // 58     59     5A     5B     5C     5D     5E     5F
494
    0x00 | 0x00 | 0x00 | 0x08 | 0x10 | 0x20 | 0x40 | 0x00,
495
  // 60     61     62     63     64     65     66     67
496
    0x01 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
497
  // 68     69     6A     6B     6C     6D     6E     6F
498
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
499
  // 70     71     72     73     74     75     76     77
500
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
501
  // 78     79     7A     7B     7C     7D     7E     7F
502
    0x00 | 0x00 | 0x00 | 0x08 | 0x10 | 0x20 | 0x00 | 0x80,
503
  // 80     81     82     83     84     85     86     87
504
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
505
  // 88     89     8A     8B     8C     8D     8E     8F
506
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
507
  // 90     91     92     93     94     95     96     97
508
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
509
  // 98     99     9A     9B     9C     9D     9E     9F
510
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
511
  // A0     A1     A2     A3     A4     A5     A6     A7
512
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
513
  // A8     A9     AA     AB     AC     AD     AE     AF
514
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
515
  // B0     B1     B2     B3     B4     B5     B6     B7
516
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
517
  // B8     B9     BA     BB     BC     BD     BE     BF
518
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
519
  // C0     C1     C2     C3     C4     C5     C6     C7
520
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
521
  // C8     C9     CA     CB     CC     CD     CE     CF
522
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
523
  // D0     D1     D2     D3     D4     D5     D6     D7
524
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
525
  // D8     D9     DA     DB     DC     DD     DE     DF
526
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
527
  // E0     E1     E2     E3     E4     E5     E6     E7
528
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
529
  // E8     E9     EA     EB     EC     ED     EE     EF
530
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
531
  // F0     F1     F2     F3     F4     F5     F6     F7
532
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
533
  // F8     F9     FA     FB     FC     FD     FE     FF
534
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
535
};
536
537
const uint8_t QUERY_ENCODE_SET_NONSPECIAL[32] = {
538
  // 00     01     02     03     04     05     06     07
539
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
540
  // 08     09     0A     0B     0C     0D     0E     0F
541
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
542
  // 10     11     12     13     14     15     16     17
543
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
544
  // 18     19     1A     1B     1C     1D     1E     1F
545
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
546
  // 20     21     22     23     24     25     26     27
547
    0x01 | 0x00 | 0x04 | 0x08 | 0x00 | 0x00 | 0x00 | 0x00,
548
  // 28     29     2A     2B     2C     2D     2E     2F
549
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
550
  // 30     31     32     33     34     35     36     37
551
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
552
  // 38     39     3A     3B     3C     3D     3E     3F
553
    0x00 | 0x00 | 0x00 | 0x00 | 0x10 | 0x00 | 0x40 | 0x00,
554
  // 40     41     42     43     44     45     46     47
555
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
556
  // 48     49     4A     4B     4C     4D     4E     4F
557
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
558
  // 50     51     52     53     54     55     56     57
559
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
560
  // 58     59     5A     5B     5C     5D     5E     5F
561
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
562
  // 60     61     62     63     64     65     66     67
563
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
564
  // 68     69     6A     6B     6C     6D     6E     6F
565
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
566
  // 70     71     72     73     74     75     76     77
567
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
568
  // 78     79     7A     7B     7C     7D     7E     7F
569
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x80,
570
  // 80     81     82     83     84     85     86     87
571
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
572
  // 88     89     8A     8B     8C     8D     8E     8F
573
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
574
  // 90     91     92     93     94     95     96     97
575
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
576
  // 98     99     9A     9B     9C     9D     9E     9F
577
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
578
  // A0     A1     A2     A3     A4     A5     A6     A7
579
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
580
  // A8     A9     AA     AB     AC     AD     AE     AF
581
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
582
  // B0     B1     B2     B3     B4     B5     B6     B7
583
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
584
  // B8     B9     BA     BB     BC     BD     BE     BF
585
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
586
  // C0     C1     C2     C3     C4     C5     C6     C7
587
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
588
  // C8     C9     CA     CB     CC     CD     CE     CF
589
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
590
  // D0     D1     D2     D3     D4     D5     D6     D7
591
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
592
  // D8     D9     DA     DB     DC     DD     DE     DF
593
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
594
  // E0     E1     E2     E3     E4     E5     E6     E7
595
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
596
  // E8     E9     EA     EB     EC     ED     EE     EF
597
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
598
  // F0     F1     F2     F3     F4     F5     F6     F7
599
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
600
  // F8     F9     FA     FB     FC     FD     FE     FF
601
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
602
};
603
604
// Same as QUERY_ENCODE_SET_NONSPECIAL, but with 0x27 (') encoded.
605
const uint8_t QUERY_ENCODE_SET_SPECIAL[32] = {
606
  // 00     01     02     03     04     05     06     07
607
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
608
  // 08     09     0A     0B     0C     0D     0E     0F
609
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
610
  // 10     11     12     13     14     15     16     17
611
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
612
  // 18     19     1A     1B     1C     1D     1E     1F
613
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
614
  // 20     21     22     23     24     25     26     27
615
    0x01 | 0x00 | 0x04 | 0x08 | 0x00 | 0x00 | 0x00 | 0x80,
616
  // 28     29     2A     2B     2C     2D     2E     2F
617
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
618
  // 30     31     32     33     34     35     36     37
619
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
620
  // 38     39     3A     3B     3C     3D     3E     3F
621
    0x00 | 0x00 | 0x00 | 0x00 | 0x10 | 0x00 | 0x40 | 0x00,
622
  // 40     41     42     43     44     45     46     47
623
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
624
  // 48     49     4A     4B     4C     4D     4E     4F
625
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
626
  // 50     51     52     53     54     55     56     57
627
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
628
  // 58     59     5A     5B     5C     5D     5E     5F
629
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
630
  // 60     61     62     63     64     65     66     67
631
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
632
  // 68     69     6A     6B     6C     6D     6E     6F
633
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
634
  // 70     71     72     73     74     75     76     77
635
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00,
636
  // 78     79     7A     7B     7C     7D     7E     7F
637
    0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x80,
638
  // 80     81     82     83     84     85     86     87
639
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
640
  // 88     89     8A     8B     8C     8D     8E     8F
641
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
642
  // 90     91     92     93     94     95     96     97
643
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
644
  // 98     99     9A     9B     9C     9D     9E     9F
645
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
646
  // A0     A1     A2     A3     A4     A5     A6     A7
647
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
648
  // A8     A9     AA     AB     AC     AD     AE     AF
649
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
650
  // B0     B1     B2     B3     B4     B5     B6     B7
651
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
652
  // B8     B9     BA     BB     BC     BD     BE     BF
653
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
654
  // C0     C1     C2     C3     C4     C5     C6     C7
655
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
656
  // C8     C9     CA     CB     CC     CD     CE     CF
657
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
658
  // D0     D1     D2     D3     D4     D5     D6     D7
659
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
660
  // D8     D9     DA     DB     DC     DD     DE     DF
661
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
662
  // E0     E1     E2     E3     E4     E5     E6     E7
663
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
664
  // E8     E9     EA     EB     EC     ED     EE     EF
665
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
666
  // F0     F1     F2     F3     F4     F5     F6     F7
667
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80,
668
  // F8     F9     FA     FB     FC     FD     FE     FF
669
    0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80
670
};
671
672
11077321
bool BitAt(const uint8_t a[], const uint8_t i) {
673
11077321
  return !!(a[i >> 3] & (1 << (i & 7)));
674
}
675
676
// Appends ch to str. If ch position in encode_set is set, the ch will
677
// be percent-encoded then appended.
678
11077324
void AppendOrEscape(std::string* str,
679
                           const unsigned char ch,
680
                           const uint8_t encode_set[]) {
681
11077324
  if (BitAt(encode_set, ch))
682
1039
    *str += hex[ch];
683
  else
684
11076290
    *str += ch;
685
11077413
}
686
687
template <typename T>
688
642
unsigned hex2bin(const T ch) {
689

642
  if (ch >= '0' && ch <= '9')
690
468
    return ch - '0';
691

174
  if (ch >= 'A' && ch <= 'F')
692
74
    return 10 + (ch - 'A');
693

100
  if (ch >= 'a' && ch <= 'f')
694
100
    return 10 + (ch - 'a');
695
  return static_cast<unsigned>(-1);
696
}
697
698
3323
std::string PercentDecode(const char* input, size_t len) {
699
3323
  std::string dest;
700
3323
  if (len == 0)
701
2
    return dest;
702
3321
  dest.reserve(len);
703
3321
  const char* pointer = input;
704
3321
  const char* end = input + len;
705
706
169501
  while (pointer < end) {
707
83090
    const char ch = pointer[0];
708
83090
    size_t remaining = end - pointer - 1;
709


165969
    if (ch != '%' || remaining < 2 ||
710
220
        (ch == '%' &&
711
437
         (!IsASCIIHexDigit(pointer[1]) ||
712
217
          !IsASCIIHexDigit(pointer[2])))) {
713
82879
      dest += ch;
714
82879
      pointer++;
715
82879
      continue;
716
    } else {
717
211
      unsigned a = hex2bin(pointer[1]);
718
211
      unsigned b = hex2bin(pointer[2]);
719
211
      char c = static_cast<char>(a * 16 + b);
720
211
      dest += c;
721
211
      pointer += 3;
722
    }
723
  }
724
3321
  return dest;
725
}
726
727
#define SPECIALS(XX)                                                          \
728
  XX(ftp, 21, "ftp:")                                                         \
729
  XX(file, -1, "file:")                                                       \
730
  XX(http, 80, "http:")                                                       \
731
  XX(https, 443, "https:")                                                    \
732
  XX(ws, 80, "ws:")                                                           \
733
  XX(wss, 443, "wss:")
734
735
281894
bool IsSpecial(const std::string& scheme) {
736
#define V(_, __, name) if (scheme == name) return true;
737



281894
  SPECIALS(V);
738
#undef V
739
5235
  return false;
740
}
741
742
112005
Local<String> GetSpecial(Environment* env, const std::string& scheme) {
743
#define V(key, _, name) if (scheme == name)                                  \
744
    return env->url_special_##key##_string();
745



112005
  SPECIALS(V)
746
#undef V
747
  UNREACHABLE();
748
}
749
750
110427
int NormalizePort(const std::string& scheme, int p) {
751
#define V(_, port, name) if (scheme == name && p == port) return -1;
752









110427
  SPECIALS(V);
753
#undef V
754
8423
  return p;
755
}
756
757
// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
758
2973
bool StartsWithWindowsDriveLetter(const char* p, const char* end) {
759
2973
  size_t length = end - p;
760
2731
  return length >= 2 &&
761

3004
    IsWindowsDriveLetter(p[0], p[1]) &&
762
14
    (length == 2 ||
763
21
      p[2] == '/' ||
764
10
      p[2] == '\\' ||
765
5
      p[2] == '?' ||
766
2975
      p[2] == '#');
767
}
768
769
#if defined(NODE_HAVE_I18N_SUPPORT)
770
195
bool ToUnicode(const std::string& input, std::string* output) {
771
390
  MaybeStackBuffer<char> buf;
772
195
  if (i18n::ToUnicode(&buf, input.c_str(), input.length()) < 0)
773
    return false;
774
195
  output->assign(*buf, buf.length());
775
195
  return true;
776
}
777
778
3310
bool ToASCII(const std::string& input, std::string* output) {
779
6620
  MaybeStackBuffer<char> buf;
780
3310
  if (i18n::ToASCII(&buf, input.c_str(), input.length()) < 0)
781
77
    return false;
782
3233
  if (buf.length() == 0)
783
16
    return false;
784
3217
  output->assign(*buf, buf.length());
785
3217
  return true;
786
}
787
#else
788
// Intentional non-ops if ICU is not present.
789
bool ToUnicode(const std::string& input, std::string* output) {
790
  *output = input;
791
  return true;
792
}
793
794
bool ToASCII(const std::string& input, std::string* output) {
795
  *output = input;
796
  return true;
797
}
798
#endif
799
800
92
void URLHost::ParseIPv6Host(const char* input, size_t length) {
801
92
  CHECK_EQ(type_, HostType::H_FAILED);
802
92
  unsigned size = arraysize(value_.ipv6);
803
828
  for (unsigned n = 0; n < size; n++)
804
736
    value_.ipv6[n] = 0;
805
92
  uint16_t* piece_pointer = &value_.ipv6[0];
806
92
  uint16_t* const buffer_end = piece_pointer + size;
807
92
  uint16_t* compress_pointer = nullptr;
808
92
  const char* pointer = input;
809
92
  const char* end = pointer + length;
810
  unsigned value, len, numbers_seen;
811
92
  char ch = pointer < end ? pointer[0] : kEOL;
812
92
  if (ch == ':') {
813

33
    if (length < 2 || pointer[1] != ':')
814
3
      return;
815
30
    pointer += 2;
816
30
    ch = pointer < end ? pointer[0] : kEOL;
817
30
    piece_pointer++;
818
30
    compress_pointer = piece_pointer;
819
  }
820
413
  while (ch != kEOL) {
821
223
    if (piece_pointer >= buffer_end)
822
3
      return;
823
220
    if (ch == ':') {
824
18
      if (compress_pointer != nullptr)
825
3
        return;
826
15
      pointer++;
827
15
      ch = pointer < end ? pointer[0] : kEOL;
828
15
      piece_pointer++;
829
15
      compress_pointer = piece_pointer;
830
15
      continue;
831
    }
832
202
    value = 0;
833
202
    len = 0;
834

642
    while (len < 4 && IsASCIIHexDigit(ch)) {
835
220
      value = value * 0x10 + hex2bin(ch);
836
220
      pointer++;
837
220
      ch = pointer < end ? pointer[0] : kEOL;
838
220
      len++;
839
    }
840

202
    switch (ch) {
841
      case '.':
842
43
        if (len == 0)
843
3
          return;
844
40
        pointer -= len;
845
40
        ch = pointer < end ? pointer[0] : kEOL;
846
40
        if (piece_pointer > buffer_end - 2)
847
3
          return;
848
37
        numbers_seen = 0;
849
223
        while (ch != kEOL) {
850
123
          value = 0xffffffff;
851
123
          if (numbers_seen > 0) {
852

86
            if (ch == '.' && numbers_seen < 4) {
853
78
              pointer++;
854
78
              ch = pointer < end ? pointer[0] : kEOL;
855
            } else {
856
8
              return;
857
            }
858
          }
859
115
          if (!IsASCIIDigit(ch))
860
16
            return;
861
343
          while (IsASCIIDigit(ch)) {
862
128
            unsigned number = ch - '0';
863
128
            if (value == 0xffffffff) {
864
99
              value = number;
865
29
            } else if (value == 0) {
866
3
              return;
867
            } else {
868
26
              value = value * 10 + number;
869
            }
870
125
            if (value > 255)
871
3
              return;
872
122
            pointer++;
873
122
            ch = pointer < end ? pointer[0] : kEOL;
874
          }
875
93
          *piece_pointer = *piece_pointer * 0x100 + value;
876
93
          numbers_seen++;
877

93
          if (numbers_seen == 2 || numbers_seen == 4)
878
37
            piece_pointer++;
879
        }
880
7
        if (numbers_seen != 4)
881
3
          return;
882
4
        continue;
883
      case ':':
884
127
        pointer++;
885
127
        ch = pointer < end ? pointer[0] : kEOL;
886
127
        if (ch == kEOL)
887
3
          return;
888
124
        break;
889
      case kEOL:
890
19
        break;
891
      default:
892
13
        return;
893
    }
894
143
    *piece_pointer = value;
895
143
    piece_pointer++;
896
  }
897
898
28
  if (compress_pointer != nullptr) {
899
19
    unsigned swaps = piece_pointer - compress_pointer;
900
19
    piece_pointer = buffer_end - 1;
901

61
    while (piece_pointer != &value_.ipv6[0] && swaps > 0) {
902
21
      uint16_t temp = *piece_pointer;
903
21
      uint16_t* swap_piece = compress_pointer + swaps - 1;
904
21
      *piece_pointer = *swap_piece;
905
21
      *swap_piece = temp;
906
21
       piece_pointer--;
907
21
       swaps--;
908
    }
909

9
  } else if (compress_pointer == nullptr &&
910
             piece_pointer != buffer_end) {
911
3
    return;
912
  }
913
25
  type_ = HostType::H_IPV6;
914
}
915
916
3287
int64_t ParseNumber(const char* start, const char* end) {
917
3287
  unsigned R = 10;
918

3287
  if (end - start >= 2 && start[0] == '0' && (start[1] | 0x20) == 'x') {
919
26
    start += 2;
920
26
    R = 16;
921
  }
922
3287
  if (end - start == 0) {
923
4
    return 0;
924

3283
  } else if (R == 10 && end - start > 1 && start[0] == '0') {
925
32
    start++;
926
32
    R = 8;
927
  }
928
3283
  const char* p = start;
929
930
4773
  while (p < end) {
931
3816
    const char ch = p[0];
932

3816
    switch (R) {
933
      case 8:
934

173
        if (ch < '0' || ch > '7')
935
19
          return -1;
936
154
        break;
937
      case 10:
938
3521
        if (!IsASCIIDigit(ch))
939
3050
          return -1;
940
471
        break;
941
      case 16:
942
122
        if (!IsASCIIHexDigit(ch))
943
2
          return -1;
944
120
        break;
945
    }
946
745
    p++;
947
  }
948
212
  return strtoll(start, nullptr, R);
949
}
950
951
3152
void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
952
3152
  CHECK_EQ(type_, HostType::H_FAILED);
953
3152
  *is_ipv4 = false;
954
3152
  const char* pointer = input;
955
3152
  const char* mark = input;
956
3152
  const char* end = pointer + length;
957
3152
  int parts = 0;
958
3152
  uint32_t val = 0;
959
  uint64_t numbers[4];
960
3152
  int tooBigNumbers = 0;
961
3152
  if (length == 0)
962
3100
    return;
963
964
57270
  while (pointer <= end) {
965
30140
    const char ch = pointer < end ? pointer[0] : kEOL;
966
30140
    int remaining = end - pointer - 1;
967

30140
    if (ch == '.' || ch == kEOL) {
968
3295
      if (++parts > static_cast<int>(arraysize(numbers)))
969
2
        return;
970
3293
      if (pointer == mark)
971
6
        return;
972
3287
      int64_t n = ParseNumber(mark, pointer);
973
3287
      if (n < 0)
974
3071
        return;
975
976
216
      if (n > 255) {
977
69
        tooBigNumbers++;
978
      }
979
216
      numbers[parts - 1] = n;
980
216
      mark = pointer + 1;
981

216
      if (ch == '.' && remaining == 0)
982
2
        break;
983
    }
984
27059
    pointer++;
985
  }
986
73
  CHECK_GT(parts, 0);
987
73
  *is_ipv4 = true;
988
989
  // If any but the last item in numbers is greater than 255, return failure.
990
  // If the last item in numbers is greater than or equal to
991
  // 256^(5 - the number of items in numbers), return failure.
992

143
  if (tooBigNumbers > 1 ||
993

181
      (tooBigNumbers == 1 && numbers[parts - 1] <= 255) ||
994
67
      numbers[parts - 1] >= pow(256, static_cast<double>(5 - parts))) {
995
21
    return;
996
  }
997
998
52
  type_ = HostType::H_IPV4;
999
52
  val = numbers[parts - 1];
1000
137
  for (int n = 0; n < parts - 1; n++) {
1001
85
    double b = 3 - n;
1002
85
    val += numbers[n] * pow(256, b);
1003
  }
1004
1005
52
  value_.ipv4 = val;
1006
}
1007
1008
323
void URLHost::ParseOpaqueHost(const char* input, size_t length) {
1009
323
  CHECK_EQ(type_, HostType::H_FAILED);
1010
611
  std::string output;
1011
323
  output.reserve(length);
1012
1779
  for (size_t i = 0; i < length; i++) {
1013
1491
    const char ch = input[i];
1014

1491
    if (ch != '%' && IsForbiddenHostCodePoint(ch)) {
1015
35
      return;
1016
    } else {
1017
1456
      AppendOrEscape(&output, ch, C0_CONTROL_ENCODE_SET);
1018
    }
1019
  }
1020
1021
288
  SetOpaque(std::move(output));
1022
}
1023
1024
3731
void URLHost::ParseHost(const char* input,
1025
                        size_t length,
1026
                        bool is_special,
1027
                        bool unicode) {
1028
3731
  CHECK_EQ(type_, HostType::H_FAILED);
1029
3731
  const char* pointer = input;
1030
1031
3731
  if (length == 0)
1032
652
    return;
1033
1034
3731
  if (pointer[0] == '[') {
1035
98
    if (pointer[length - 1] != ']')
1036
6
      return;
1037
92
    return ParseIPv6Host(++pointer, length - 2);
1038
  }
1039
1040
3633
  if (!is_special)
1041
323
    return ParseOpaqueHost(input, length);
1042
1043
  // First, we have to percent decode
1044
6389
  std::string decoded = PercentDecode(input, length);
1045
1046
  // Then we have to punycode toASCII
1047
3310
  if (!ToASCII(decoded, &decoded))
1048
93
    return;
1049
1050
  // If any of the following characters are still present, we have to fail
1051
85776
  for (size_t n = 0; n < decoded.size(); n++) {
1052
82624
    const char ch = decoded[n];
1053
82624
    if (IsForbiddenHostCodePoint(ch)) {
1054
65
      return;
1055
    }
1056
  }
1057
1058
  // Check to see if it's an IPv4 IP address
1059
  bool is_ipv4;
1060
3152
  ParseIPv4Host(decoded.c_str(), decoded.length(), &is_ipv4);
1061
3152
  if (is_ipv4)
1062
73
    return;
1063
1064
  // If the unicode flag is set, run the result through punycode ToUnicode
1065

3079
  if (unicode && !ToUnicode(decoded, &decoded))
1066
    return;
1067
1068
  // It's not an IPv4 or IPv6 address, it must be a domain
1069
3079
  SetDomain(std::move(decoded));
1070
}
1071
1072
// Locates the longest sequence of 0 segments in an IPv6 address
1073
// in order to use the :: compression when serializing
1074
template <typename T>
1075
25
T* FindLongestZeroSequence(T* values, size_t len) {
1076
25
  T* start = values;
1077
25
  T* end = start + len;
1078
25
  T* result = nullptr;
1079
1080
25
  T* current = nullptr;
1081
25
  unsigned counter = 0, longest = 1;
1082
1083
425
  while (start < end) {
1084
200
    if (*start == 0) {
1085
149
      if (current == nullptr)
1086
32
        current = start;
1087
149
      counter++;
1088
    } else {
1089
51
      if (counter > longest) {
1090
21
        longest = counter;
1091
21
        result = current;
1092
      }
1093
51
      counter = 0;
1094
51
      current = nullptr;
1095
    }
1096
200
    start++;
1097
  }
1098
25
  if (counter > longest)
1099
3
    result = current;
1100
25
  return result;
1101
}
1102
1103
3444
std::string URLHost::ToStringMove() {
1104
3444
  std::string return_value;
1105
3444
  switch (type_) {
1106
    case HostType::H_DOMAIN:
1107
    case HostType::H_OPAQUE:
1108
3367
      return_value = std::move(value_.domain_or_opaque);
1109
3367
      break;
1110
    default:
1111
77
      return_value = ToString();
1112
77
      break;
1113
  }
1114
3444
  Reset();
1115
3444
  return return_value;
1116
}
1117
1118
77
std::string URLHost::ToString() const {
1119
154
  std::string dest;
1120

77
  switch (type_) {
1121
    case HostType::H_DOMAIN:
1122
    case HostType::H_OPAQUE:
1123
      return value_.domain_or_opaque;
1124
      break;
1125
    case HostType::H_IPV4: {
1126
52
      dest.reserve(15);
1127
52
      uint32_t value = value_.ipv4;
1128
260
      for (int n = 0; n < 4; n++) {
1129
        char buf[4];
1130
208
        snprintf(buf, sizeof(buf), "%d", value % 256);
1131
208
        dest.insert(0, buf);
1132
208
        if (n < 3)
1133
156
          dest.insert(0, 1, '.');
1134
208
        value /= 256;
1135
      }
1136
52
      break;
1137
    }
1138
    case HostType::H_IPV6: {
1139
25
      dest.reserve(41);
1140
25
      dest += '[';
1141
25
      const uint16_t* start = &value_.ipv6[0];
1142
      const uint16_t* compress_pointer =
1143
25
          FindLongestZeroSequence(start, 8);
1144
25
      bool ignore0 = false;
1145
225
      for (int n = 0; n <= 7; n++) {
1146
200
        const uint16_t* piece = &value_.ipv6[n];
1147

200
        if (ignore0 && *piece == 0)
1148
255
          continue;
1149
84
        else if (ignore0)
1150
20
          ignore0 = false;
1151
84
        if (compress_pointer == piece) {
1152
23
          dest += n == 0 ? "::" : ":";
1153
23
          ignore0 = true;
1154
23
          continue;
1155
        }
1156
        char buf[5];
1157
61
        snprintf(buf, sizeof(buf), "%x", *piece);
1158
61
        dest += buf;
1159
61
        if (n < 7)
1160
39
          dest += ':';
1161
      }
1162
25
      dest += ']';
1163
25
      break;
1164
    }
1165
    case HostType::H_FAILED:
1166
      break;
1167
  }
1168
77
  return dest;
1169
}
1170
1171
3340
bool ParseHost(const std::string& input,
1172
               std::string* output,
1173
               bool is_special,
1174
               bool unicode = false) {
1175
3340
  if (input.empty()) {
1176
45
    output->clear();
1177
45
    return true;
1178
  }
1179
6590
  URLHost host;
1180
3295
  host.ParseHost(input.c_str(), input.length(), is_special, unicode);
1181
3295
  if (host.ParsingFailed())
1182
263
    return false;
1183
3032
  *output = host.ToStringMove();
1184
3032
  return true;
1185
}
1186
1187
4046
std::vector<std::string> FromJSStringArray(Environment* env,
1188
                                           Local<Array> array) {
1189
4046
  std::vector<std::string> vec;
1190
4046
  if (array->Length() > 0)
1191
4038
    vec.reserve(array->Length());
1192
59864
  for (size_t n = 0; n < array->Length(); n++) {
1193
77658
    Local<Value> val = array->Get(env->context(), n).ToLocalChecked();
1194
51772
    if (val->IsString()) {
1195
51772
      Utf8Value value(env->isolate(), val.As<String>());
1196
25886
      vec.emplace_back(*value, value.length());
1197
    }
1198
  }
1199
4046
  return vec;
1200
}
1201
1202
4046
url_data HarvestBase(Environment* env, Local<Object> base_obj) {
1203
4046
  url_data base;
1204
4046
  Local<Context> context = env->context();
1205
1206
  Local<Value> flags =
1207
16184
      base_obj->Get(env->context(), env->flags_string()).ToLocalChecked();
1208
4046
  if (flags->IsInt32())
1209
8092
    base.flags = flags->Int32Value(context).FromJust();
1210
1211
  Local<Value> port =
1212
16184
      base_obj->Get(env->context(), env->port_string()).ToLocalChecked();
1213
4046
  if (port->IsInt32())
1214
8
    base.port = port->Int32Value(context).FromJust();
1215
1216
  Local<Value> scheme =
1217
16184
      base_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
1218
4046
  base.scheme = Utf8Value(env->isolate(), scheme).out();
1219
1220
  auto GetStr = [&](std::string url_data::*member,
1221
                    int flag,
1222
                    Local<String> name,
1223
20230
                    bool empty_as_present) {
1224
72277
    Local<Value> value = base_obj->Get(env->context(), name).ToLocalChecked();
1225
40460
    if (value->IsString()) {
1226
23174
      Utf8Value utf8value(env->isolate(), value.As<String>());
1227
15094
      (base.*member).assign(*utf8value, utf8value.length());
1228

27771
      if (empty_as_present || value.As<String>()->Length() != 0) {
1229
3507
        base.flags |= flag;
1230
      }
1231
    }
1232
24276
  };
1233
4046
  GetStr(&url_data::username,
1234
         URL_FLAGS_HAS_USERNAME,
1235
         env->username_string(),
1236
4046
         false);
1237
4046
  GetStr(&url_data::password,
1238
         URL_FLAGS_HAS_PASSWORD,
1239
         env->password_string(),
1240
4046
         false);
1241
4046
  GetStr(&url_data::host, URL_FLAGS_HAS_HOST, env->host_string(), true);
1242
4046
  GetStr(&url_data::query, URL_FLAGS_HAS_QUERY, env->query_string(), true);
1243
4046
  GetStr(&url_data::fragment,
1244
         URL_FLAGS_HAS_FRAGMENT,
1245
         env->fragment_string(),
1246
4046
         true);
1247
1248
  Local<Value>
1249
16184
      path = base_obj->Get(env->context(), env->path_string()).ToLocalChecked();
1250
4046
  if (path->IsArray()) {
1251
4046
    base.flags |= URL_FLAGS_HAS_PATH;
1252
4046
    base.path = FromJSStringArray(env, path.As<Array>());
1253
  }
1254
4046
  return base;
1255
}
1256
1257
36202
url_data HarvestContext(Environment* env, Local<Object> context_obj) {
1258
36202
  url_data context;
1259
  Local<Value> flags =
1260
144808
      context_obj->Get(env->context(), env->flags_string()).ToLocalChecked();
1261
36202
  if (flags->IsInt32()) {
1262
    static constexpr int32_t kCopyFlagsMask =
1263
        URL_FLAGS_SPECIAL |
1264
        URL_FLAGS_CANNOT_BE_BASE |
1265
        URL_FLAGS_HAS_USERNAME |
1266
        URL_FLAGS_HAS_PASSWORD |
1267
        URL_FLAGS_HAS_HOST;
1268
72404
    context.flags |= flags.As<Int32>()->Value() & kCopyFlagsMask;
1269
  }
1270
  Local<Value> scheme =
1271
144808
      context_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
1272
72404
  if (scheme->IsString()) {
1273
72404
    Utf8Value value(env->isolate(), scheme);
1274
36202
    context.scheme.assign(*value, value.length());
1275
  }
1276
  Local<Value> port =
1277
144808
      context_obj->Get(env->context(), env->port_string()).ToLocalChecked();
1278
36202
  if (port->IsInt32())
1279
444
    context.port = port.As<Int32>()->Value();
1280
36202
  if (context.flags & URL_FLAGS_HAS_USERNAME) {
1281
    Local<Value> username =
1282
428
        context_obj->Get(env->context(),
1283
1070
                         env->username_string()).ToLocalChecked();
1284
428
    CHECK(username->IsString());
1285
428
    Utf8Value value(env->isolate(), username);
1286
214
    context.username.assign(*value, value.length());
1287
  }
1288
36202
  if (context.flags & URL_FLAGS_HAS_PASSWORD) {
1289
    Local<Value> password =
1290
416
        context_obj->Get(env->context(),
1291
1040
                         env->password_string()).ToLocalChecked();
1292
416
    CHECK(password->IsString());
1293
416
    Utf8Value value(env->isolate(), password);
1294
208
    context.password.assign(*value, value.length());
1295
  }
1296
  Local<Value> host =
1297
72404
      context_obj->Get(env->context(),
1298
181010
                       env->host_string()).ToLocalChecked();
1299
72404
  if (host->IsString()) {
1300
72370
    Utf8Value value(env->isolate(), host);
1301
36185
    context.host.assign(*value, value.length());
1302
  }
1303
36202
  return context;
1304
}
1305
1306
// Single dot segment can be ".", "%2e", or "%2E"
1307
2457263
bool IsSingleDotSegment(const std::string& str) {
1308
2457263
  switch (str.size()) {
1309
    case 1:
1310
2590
      return str == ".";
1311
    case 3:
1312
114744
      return str[0] == '%' &&
1313

114732
             str[1] == '2' &&
1314
114732
             ASCIILowercase(str[2]) == 'e';
1315
    default:
1316
2339970
      return false;
1317
  }
1318
}
1319
1320
// Double dot segment can be:
1321
//   "..", ".%2e", ".%2E", "%2e.", "%2E.",
1322
//   "%2e%2e", "%2E%2E", "%2e%2E", or "%2E%2e"
1323
1230743
bool IsDoubleDotSegment(const std::string& str) {
1324

1230743
  switch (str.size()) {
1325
    case 2:
1326
2225
      return str == "..";
1327
    case 4:
1328

295139
      if (str[0] != '.' && str[0] != '%')
1329
295127
        return false;
1330
21
      return ((str[0] == '.' &&
1331
11
               str[1] == '%' &&
1332
4
               str[2] == '2' &&
1333

29
               ASCIILowercase(str[3]) == 'e') ||
1334
13
              (str[0] == '%' &&
1335
6
               str[1] == '2' &&
1336
6
               ASCIILowercase(str[2]) == 'e' &&
1337
15
               str[3] == '.'));
1338
    case 6:
1339
60211
      return (str[0] == '%' &&
1340
14
              str[1] == '2' &&
1341
9
              ASCIILowercase(str[2]) == 'e' &&
1342
4
              str[3] == '%' &&
1343

60208
              str[4] == '2' &&
1344
60206
              ASCIILowercase(str[5]) == 'e');
1345
    default:
1346
873182
      return false;
1347
  }
1348
}
1349
1350
4960
void ShortenUrlPath(struct url_data* url) {
1351
4960
  if (url->path.empty()) return;
1352


5101
  if (url->path.size() == 1 && url->scheme == "file:" &&
1353
365
      IsNormalizedWindowsDriveLetter(url->path[0])) return;
1354
4736
  url->path.pop_back();
1355
}
1356
1357
}  // anonymous namespace
1358
1359
182004
void URL::Parse(const char* input,
1360
                size_t len,
1361
                enum url_parse_state state_override,
1362
                struct url_data* url,
1363
                bool has_url,
1364
                const struct url_data* base,
1365
                bool has_base) {
1366
182004
  const char* p = input;
1367
182004
  const char* end = input + len;
1368
1369
182004
  if (!has_url) {
1370
114232
    for (const char* ptr = p; ptr < end; ptr++) {
1371
114221
      if (IsC0ControlOrSpace(*ptr))
1372
28
        p++;
1373
      else
1374
114193
        break;
1375
    }
1376
114229
    for (const char* ptr = end - 1; ptr >= p; ptr--) {
1377
114218
      if (IsC0ControlOrSpace(*ptr))
1378
25
        end--;
1379
      else
1380
114192
        break;
1381
    }
1382
114203
    input = p;
1383
114203
    len = end - p;
1384
  }
1385
1386
  // The spec says we should strip out any ASCII tabs or newlines.
1387
  // In those cases, we create another std::string instance with the filtered
1388
  // contents, but in the general case we avoid the overhead.
1389
362034
  std::string whitespace_stripped;
1390
13280693
  for (const char* ptr = p; ptr < end; ptr++) {
1391
13098817
    if (!IsASCIITabOrNewline(*ptr))
1392
13098690
      continue;
1393
    // Hit tab or newline. Allocate storage, copy what we have until now,
1394
    // and then iterate and filter all similar characters out.
1395
142
    whitespace_stripped.reserve(len - 1);
1396
142
    whitespace_stripped.assign(p, ptr - p);
1397
    // 'ptr + 1' skips the current char, which we know to be tab or newline.
1398
681
    for (ptr = ptr + 1; ptr < end; ptr++) {
1399
539
      if (!IsASCIITabOrNewline(*ptr))
1400
497
        whitespace_stripped += *ptr;
1401
    }
1402
1403
    // Update variables like they should have looked like if the string
1404
    // had been stripped of whitespace to begin with.
1405
142
    input = whitespace_stripped.c_str();
1406
142
    len = whitespace_stripped.size();
1407
142
    p = input;
1408
142
    end = input + len;
1409
142
    break;
1410
  }
1411
1412
182018
  bool atflag = false;  // Set when @ has been seen.
1413
182018
  bool square_bracket_flag = false;  // Set inside of [...]
1414
182018
  bool password_token_seen_flag = false;  // Set after a : after an username.
1415
1416
362049
  std::string buffer;
1417
1418
  // Set the initial parse state.
1419
182130
  const bool has_state_override = state_override != kUnknownState;
1420
182130
  enum url_parse_state state = has_state_override ? state_override :
1421
182130
                                                    kSchemeStart;
1422
1423

182130
  if (state < kSchemeStart || state > kFragment) {
1424
    url->flags |= URL_FLAGS_INVALID_PARSE_STATE;
1425
    return;
1426
  }
1427
1428

27223098
  while (p <= end) {
1429
13522583
    const char ch = p < end ? p[0] : kEOL;
1430
13522583
    bool special = (url->flags & URL_FLAGS_SPECIAL);
1431
    bool cannot_be_base;
1432

13522583
    bool special_back_slash = (special && ch == '\\');
1433
1434





13522583
    switch (state) {
1435
      case kSchemeStart:
1436
114253
        if (IsASCIIAlpha(ch)) {
1437
110335
          buffer += ASCIILowercase(ch);
1438
110173
          state = kScheme;
1439
3889
        } else if (!has_state_override) {
1440
3882
          state = kNoScheme;
1441
3882
          continue;
1442
        } else {
1443
7
          url->flags |= URL_FLAGS_FAILED;
1444
7
          return;
1445
        }
1446
110173
        break;
1447
      case kScheme:
1448


447649
        if (IsASCIIAlphanumeric(ch) || ch == '+' || ch == '-' || ch == '.') {
1449
337318
          buffer += ASCIILowercase(ch);
1450

110334
        } else if (ch == ':' || (has_state_override && ch == kEOL)) {
1451

109610
          if (has_state_override && buffer.size() == 0) {
1452
            url->flags |= URL_FLAGS_TERMINATED;
1453
            return;
1454
          }
1455
109610
          buffer += ':';
1456
1457
109610
          bool new_is_special = IsSpecial(buffer);
1458
1459
109609
          if (has_state_override) {
1460

62
            if ((special != new_is_special) ||
1461
29
                ((buffer == "file:") &&
1462
4
                 ((url->flags & URL_FLAGS_HAS_USERNAME) ||
1463
2
                  (url->flags & URL_FLAGS_HAS_PASSWORD) ||
1464

76
                  (url->port != -1))) ||
1465
26
                  (url->scheme == "file:" && url->host.empty())) {
1466
16
              url->flags |= URL_FLAGS_TERMINATED;
1467
16
              return;
1468
            }
1469
          }
1470
1471
109593
          url->scheme = std::move(buffer);
1472
109594
          url->port = NormalizePort(url->scheme, url->port);
1473
109595
          if (new_is_special) {
1474
104769
            url->flags |= URL_FLAGS_SPECIAL;
1475
104769
            special = true;
1476
          } else {
1477
4826
            url->flags &= ~URL_FLAGS_SPECIAL;
1478
4826
            special = false;
1479
          }
1480

109595
          special_back_slash = (special && ch == '\\');
1481
109595
          buffer.clear();
1482
109595
          if (has_state_override)
1483
20
            return;
1484
109575
          if (url->scheme == "file:") {
1485
101977
            state = kFile;
1486

10381
          } else if (special &&
1487

8115
                     has_base &&
1488
517
                     url->scheme == base->scheme) {
1489
178
            state = kSpecialRelativeOrAuthority;
1490
7420
          } else if (special) {
1491
2605
            state = kSpecialAuthoritySlashes;
1492

4815
          } else if (p + 1 < end && p[1] == '/') {
1493
409
            state = kPathOrAuthority;
1494
409
            p++;
1495
          } else {
1496
4406
            url->flags |= URL_FLAGS_CANNOT_BE_BASE;
1497
4406
            url->flags |= URL_FLAGS_HAS_PATH;
1498
4406
            url->path.emplace_back("");
1499
4406
            state = kCannotBeBase;
1500
109575
          }
1501
724
        } else if (!has_state_override) {
1502
718
          buffer.clear();
1503
718
          state = kNoScheme;
1504
718
          p = input;
1505
718
          continue;
1506
        } else {
1507
6
          url->flags |= URL_FLAGS_FAILED;
1508
6
          return;
1509
        }
1510
446892
        break;
1511
      case kNoScheme:
1512

4629
        cannot_be_base = has_base && (base->flags & URL_FLAGS_CANNOT_BE_BASE);
1513

4629
        if (!has_base || (cannot_be_base && ch != '#')) {
1514
1398
          url->flags |= URL_FLAGS_FAILED;
1515
1398
          return;
1516

3231
        } else if (cannot_be_base && ch == '#') {
1517
14
          url->scheme = base->scheme;
1518
14
          if (IsSpecial(url->scheme)) {
1519
            url->flags |= URL_FLAGS_SPECIAL;
1520
            special = true;
1521
          } else {
1522
14
            url->flags &= ~URL_FLAGS_SPECIAL;
1523
14
            special = false;
1524
          }
1525

14
          special_back_slash = (special && ch == '\\');
1526
14
          if (base->flags & URL_FLAGS_HAS_PATH) {
1527
14
            url->flags |= URL_FLAGS_HAS_PATH;
1528
14
            url->path = base->path;
1529
          }
1530
14
          if (base->flags & URL_FLAGS_HAS_QUERY) {
1531
2
            url->flags |= URL_FLAGS_HAS_QUERY;
1532
2
            url->query = base->query;
1533
          }
1534
14
          if (base->flags & URL_FLAGS_HAS_FRAGMENT) {
1535
            url->flags |= URL_FLAGS_HAS_FRAGMENT;
1536
            url->fragment = base->fragment;
1537
          }
1538
14
          url->flags |= URL_FLAGS_CANNOT_BE_BASE;
1539
14
          state = kFragment;
1540

6434
        } else if (has_base &&
1541
3217
                   base->scheme != "file:") {
1542
229
          state = kRelative;
1543
229
          continue;
1544
        } else {
1545
2988
          url->scheme = "file:";
1546
2988
          url->flags |= URL_FLAGS_SPECIAL;
1547
2988
          special = true;
1548
2988
          state = kFile;
1549

2988
          special_back_slash = (special && ch == '\\');
1550
2988
          continue;
1551
        }
1552
14
        break;
1553
      case kSpecialRelativeOrAuthority:
1554

178
        if (ch == '/' && p + 1 < end && p[1] == '/') {
1555
162
          state = kSpecialAuthorityIgnoreSlashes;
1556
162
          p++;
1557
        } else {
1558
16
          state = kRelative;
1559
16
          continue;
1560
        }
1561
162
        break;
1562
      case kPathOrAuthority:
1563
409
        if (ch == '/') {
1564
328
          state = kAuthority;
1565
        } else {
1566
81
          state = kPath;
1567
81
          continue;
1568
        }
1569
328
        break;
1570
      case kRelative:
1571
245
        url->scheme = base->scheme;
1572
245
        if (IsSpecial(url->scheme)) {
1573
195
          url->flags |= URL_FLAGS_SPECIAL;
1574
195
          special = true;
1575
        } else {
1576
50
          url->flags &= ~URL_FLAGS_SPECIAL;
1577
50
          special = false;
1578
        }
1579

245
        special_back_slash = (special && ch == '\\');
1580

245
        switch (ch) {
1581
          case kEOL:
1582
9
            if (base->flags & URL_FLAGS_HAS_USERNAME) {
1583
2
              url->flags |= URL_FLAGS_HAS_USERNAME;
1584
2
              url->username = base->username;
1585
            }
1586
9
            if (base->flags & URL_FLAGS_HAS_PASSWORD) {
1587
2
              url->flags |= URL_FLAGS_HAS_PASSWORD;
1588
2
              url->password = base->password;
1589
            }
1590
9
            if (base->flags & URL_FLAGS_HAS_HOST) {
1591
8
              url->flags |= URL_FLAGS_HAS_HOST;
1592
8
              url->host = base->host;
1593
            }
1594
9
            if (base->flags & URL_FLAGS_HAS_QUERY) {
1595
              url->flags |= URL_FLAGS_HAS_QUERY;
1596
              url->query = base->query;
1597
            }
1598
9
            if (base->flags & URL_FLAGS_HAS_PATH) {
1599
9
              url->flags |= URL_FLAGS_HAS_PATH;
1600
9
              url->path = base->path;
1601
            }
1602
9
            url->port = base->port;
1603
9
            break;
1604
          case '/':
1605
39
            state = kRelativeSlash;
1606
39
            break;
1607
          case '?':
1608
24
            if (base->flags & URL_FLAGS_HAS_USERNAME) {
1609
              url->flags |= URL_FLAGS_HAS_USERNAME;
1610
              url->username = base->username;
1611
            }
1612
24
            if (base->flags & URL_FLAGS_HAS_PASSWORD) {
1613
              url->flags |= URL_FLAGS_HAS_PASSWORD;
1614
              url->password = base->password;
1615
            }
1616
24
            if (base->flags & URL_FLAGS_HAS_HOST) {
1617
22
              url->flags |= URL_FLAGS_HAS_HOST;
1618
22
              url->host = base->host;
1619
            }
1620
24
            if (base->flags & URL_FLAGS_HAS_PATH) {
1621
24
              url->flags |= URL_FLAGS_HAS_PATH;
1622
24
              url->path = base->path;
1623
            }
1624
24
            url->port = base->port;
1625
24
            state = kQuery;
1626
24
            break;
1627
          case '#':
1628
19
            if (base->flags & URL_FLAGS_HAS_USERNAME) {
1629
              url->flags |= URL_FLAGS_HAS_USERNAME;
1630
              url->username = base->username;
1631
            }
1632
19
            if (base->flags & URL_FLAGS_HAS_PASSWORD) {
1633
              url->flags |= URL_FLAGS_HAS_PASSWORD;
1634
              url->password = base->password;
1635
            }
1636
19
            if (base->flags & URL_FLAGS_HAS_HOST) {
1637
17
              url->flags |= URL_FLAGS_HAS_HOST;
1638
17
              url->host = base->host;
1639
            }
1640
19
            if (base->flags & URL_FLAGS_HAS_QUERY) {
1641
              url->flags |= URL_FLAGS_HAS_QUERY;
1642
              url->query = base->query;
1643
            }
1644
19
            if (base->flags & URL_FLAGS_HAS_PATH) {
1645
19
              url->flags |= URL_FLAGS_HAS_PATH;
1646
19
              url->path = base->path;
1647
            }
1648
19
            url->port = base->port;
1649
19
            state = kFragment;
1650
19
            break;
1651
          default:
1652
154
            if (special_back_slash) {
1653
6
              state = kRelativeSlash;
1654
            } else {
1655
148
              if (base->flags & URL_FLAGS_HAS_USERNAME) {
1656
1
                url->flags |= URL_FLAGS_HAS_USERNAME;
1657
1
                url->username = base->username;
1658
              }
1659
148
              if (base->flags & URL_FLAGS_HAS_PASSWORD) {
1660
1
                url->flags |= URL_FLAGS_HAS_PASSWORD;
1661
1
                url->password = base->password;
1662
              }
1663
148
              if (base->flags & URL_FLAGS_HAS_HOST) {
1664
138
                url->flags |= URL_FLAGS_HAS_HOST;
1665
138
                url->host = base->host;
1666
              }
1667
148
              if (base->flags & URL_FLAGS_HAS_PATH) {
1668
148
                url->flags |= URL_FLAGS_HAS_PATH;
1669
148
                url->path = base->path;
1670
148
                ShortenUrlPath(url);
1671
              }
1672
148
              url->port = base->port;
1673
148
              state = kPath;
1674
148
              continue;
1675
            }
1676
        }
1677
97
        break;
1678
      case kRelativeSlash:
1679


45
        if (IsSpecial(url->scheme) && (ch == '/' || ch == '\\')) {
1680
10
          state = kSpecialAuthorityIgnoreSlashes;
1681
35
        } else if (ch == '/') {
1682
3
          state = kAuthority;
1683
        } else {
1684
32
          if (base->flags & URL_FLAGS_HAS_USERNAME) {
1685
4
            url->flags |= URL_FLAGS_HAS_USERNAME;
1686
4
            url->username = base->username;
1687
          }
1688
32
          if (base->flags & URL_FLAGS_HAS_PASSWORD) {
1689
2
            url->flags |= URL_FLAGS_HAS_PASSWORD;
1690
2
            url->password = base->password;
1691
          }
1692
32
          if (base->flags & URL_FLAGS_HAS_HOST) {
1693
28
            url->flags |= URL_FLAGS_HAS_HOST;
1694
28
            url->host = base->host;
1695
          }
1696
32
          url->port = base->port;
1697
32
          state = kPath;
1698
32
          continue;
1699
        }
1700
13
        break;
1701
      case kSpecialAuthoritySlashes:
1702
2605
        state = kSpecialAuthorityIgnoreSlashes;
1703

2605
        if (ch == '/' && p + 1 < end && p[1] == '/') {
1704
2520
          p++;
1705
        } else {
1706
85
          continue;
1707
        }
1708
2520
        break;
1709
      case kSpecialAuthorityIgnoreSlashes:
1710

2820
        if (ch != '/' && ch != '\\') {
1711
2777
          state = kAuthority;
1712
2777
          continue;
1713
        }
1714
43
        break;
1715
      case kAuthority:
1716
81566
        if (ch == '@') {
1717
413
          if (atflag) {
1718
13
            buffer.reserve(buffer.size() + 3);
1719
13
            buffer.insert(0, "%40");
1720
          }
1721
413
          atflag = true;
1722
413
          size_t blen = buffer.size();
1723

413
          if (blen > 0 && buffer[0] != ':') {
1724
355
            url->flags |= URL_FLAGS_HAS_USERNAME;
1725
          }
1726
5421
          for (size_t n = 0; n < blen; n++) {
1727
5008
            const char bch = buffer[n];
1728
5008
            if (bch == ':') {
1729
349
              url->flags |= URL_FLAGS_HAS_PASSWORD;
1730
349
              if (!password_token_seen_flag) {
1731
347
                password_token_seen_flag = true;
1732
347
                continue;
1733
              }
1734
            }
1735
4661
            if (password_token_seen_flag) {
1736
2281
              AppendOrEscape(&url->password, bch, USERINFO_ENCODE_SET);
1737
            } else {
1738
2380
              AppendOrEscape(&url->username, bch, USERINFO_ENCODE_SET);
1739
            }
1740
          }
1741
413
          buffer.clear();
1742

81153
        } else if (ch == kEOL ||
1743
78081
                   ch == '/' ||
1744
78063
                   ch == '?' ||
1745
78053
                   ch == '#' ||
1746
                   special_back_slash) {
1747

3108
          if (atflag && buffer.size() == 0) {
1748
39
            url->flags |= URL_FLAGS_FAILED;
1749
39
            return;
1750
          }
1751
3069
          p -= buffer.size() + 1;
1752
3069
          buffer.clear();
1753
3069
          state = kHost;
1754
        } else {
1755
78045
          buffer += ch;
1756
        }
1757
81527
        break;
1758
      case kHost:
1759
      case kHostname:
1760

74983
        if (has_state_override && url->scheme == "file:") {
1761
6
          state = kFileHost;
1762
6
          continue;
1763

74977
        } else if (ch == ':' && !square_bracket_flag) {
1764
865
          if (buffer.size() == 0) {
1765
19
            url->flags |= URL_FLAGS_FAILED;
1766
19
            return;
1767
          }
1768
846
          url->flags |= URL_FLAGS_HAS_HOST;
1769
846
          if (!ParseHost(buffer, &url->host, special)) {
1770
3
            url->flags |= URL_FLAGS_FAILED;
1771
3
            return;
1772
          }
1773
843
          buffer.clear();
1774
843
          state = kPort;
1775
1684
          if (state_override == kHostname) {
1776
2
            return;
1777
          }
1778

74112
        } else if (ch == kEOL ||
1779
71740
                   ch == '/' ||
1780
71718
                   ch == '?' ||
1781
71704
                   ch == '#' ||
1782
                   special_back_slash) {
1783
2418
          p--;
1784

2418
          if (special && buffer.size() == 0) {
1785
12
            url->flags |= URL_FLAGS_FAILED;
1786
12
            return;
1787
          }
1788

2597
          if (has_state_override &&
1789

2429
              buffer.size() == 0 &&
1790

53
              ((url->username.size() > 0 || url->password.size() > 0) ||
1791
17
               url->port != -1)) {
1792
4
            url->flags |= URL_FLAGS_TERMINATED;
1793
4
            return;
1794
          }
1795
2402
          url->flags |= URL_FLAGS_HAS_HOST;
1796
2402
          if (!ParseHost(buffer, &url->host, special)) {
1797
237
            url->flags |= URL_FLAGS_FAILED;
1798
237
            return;
1799
          }
1800
2165
          buffer.clear();
1801
2165
          state = kPathStart;
1802
4197
          if (has_state_override) {
1803
133
            return;
1804
          }
1805
        } else {
1806
71694
          if (ch == '[')
1807
94
            square_bracket_flag = true;
1808
71694
          if (ch == ']')
1809
91
            square_bracket_flag = false;
1810
71694
          buffer += ch;
1811
        }
1812
74567
        break;
1813
      case kPort:
1814
4771
        if (IsASCIIDigit(ch)) {
1815
3883
          buffer += ch;
1816

888
        } else if (has_state_override ||
1817
445
                   ch == kEOL ||
1818
27
                   ch == '/' ||
1819
27
                   ch == '?' ||
1820
27
                   ch == '#' ||
1821
                   special_back_slash) {
1822
861
          if (buffer.size() > 0) {
1823
852
            unsigned port = 0;
1824
            // the condition port <= 0xffff prevents integer overflow
1825

4573
            for (size_t i = 0; port <= 0xffff && i < buffer.size(); i++)
1826
3721
              port = port * 10 + buffer[i] - '0';
1827
852
            if (port > 0xffff) {
1828
              // TODO(TimothyGu): This hack is currently needed for the host
1829
              // setter since it needs access to hostname if it is valid, and
1830
              // if the FAILED flag is set the entire response to JS layer
1831
              // will be empty.
1832
18
              if (state_override == kHost)
1833
1
                url->port = -1;
1834
              else
1835
17
                url->flags |= URL_FLAGS_FAILED;
1836
18
              return;
1837
            }
1838
            // the port is valid
1839
834
            url->port = NormalizePort(url->scheme, static_cast<int>(port));
1840
834
            if (url->port == -1)
1841
28
              url->flags |= URL_FLAGS_IS_DEFAULT_SCHEME_PORT;
1842
834
            buffer.clear();
1843
9
          } else if (has_state_override) {
1844
            // TODO(TimothyGu): Similar case as above.
1845
5
            if (state_override == kHost)
1846
1
              url->port = -1;
1847
            else
1848
4
              url->flags |= URL_FLAGS_TERMINATED;
1849
5
            return;
1850
          }
1851
838
          state = kPathStart;
1852
838
          continue;
1853
        } else {
1854
27
          url->flags |= URL_FLAGS_FAILED;
1855
27
          return;
1856
        }
1857
3883
        break;
1858
      case kFile:
1859
104964
        url->scheme = "file:";
1860
104964
        url->host.clear();
1861
104964
        url->flags |= URL_FLAGS_HAS_HOST;
1862

104964
        if (ch == '/' || ch == '\\') {
1863
102080
          state = kFileSlash;
1864

2884
        } else if (has_base && base->scheme == "file:") {
1865

2876
          switch (ch) {
1866
            case kEOL:
1867
2
              if (base->flags & URL_FLAGS_HAS_HOST) {
1868
2
                url->host = base->host;
1869
              }
1870
2
              if (base->flags & URL_FLAGS_HAS_PATH) {
1871
2
                url->flags |= URL_FLAGS_HAS_PATH;
1872
2
                url->path = base->path;
1873
              }
1874
2
              if (base->flags & URL_FLAGS_HAS_QUERY) {
1875
2
                url->flags |= URL_FLAGS_HAS_QUERY;
1876
2
                url->query = base->query;
1877
              }
1878
2
              break;
1879
            case '?':
1880
2
              if (base->flags & URL_FLAGS_HAS_HOST) {
1881
2
                url->host = base->host;
1882
              }
1883
2
              if (base->flags & URL_FLAGS_HAS_PATH) {
1884
2
                url->flags |= URL_FLAGS_HAS_PATH;
1885
2
                url->path = base->path;
1886
              }
1887
2
              url->flags |= URL_FLAGS_HAS_QUERY;
1888
2
              url->query.clear();
1889
2
              state = kQuery;
1890
2
              break;
1891
            case '#':
1892
2
              if (base->flags & URL_FLAGS_HAS_HOST) {
1893
2
                url->host = base->host;
1894
              }
1895
2
              if (base->flags & URL_FLAGS_HAS_PATH) {
1896
2
                url->flags |= URL_FLAGS_HAS_PATH;
1897
2
                url->path = base->path;
1898
              }
1899
2
              if (base->flags & URL_FLAGS_HAS_QUERY) {
1900
2
                url->flags |= URL_FLAGS_HAS_QUERY;
1901
2
                url->query = base->query;
1902
              }
1903
2
              url->flags |= URL_FLAGS_HAS_FRAGMENT;
1904
2
              url->fragment.clear();
1905
2
              state = kFragment;
1906
2
              break;
1907
            default:
1908
2870
              url->query.clear();
1909
2870
              if (base->flags & URL_FLAGS_HAS_HOST) {
1910
2870
                url->host = base->host;
1911
              }
1912
2870
              if (base->flags & URL_FLAGS_HAS_PATH) {
1913
2870
                url->flags |= URL_FLAGS_HAS_PATH;
1914
2870
                url->path = base->path;
1915
              }
1916
2870
              if (!StartsWithWindowsDriveLetter(p, end)) {
1917
2860
                ShortenUrlPath(url);
1918
              } else {
1919
10
                url->path.clear();
1920
              }
1921
2870
              state = kPath;
1922
2870
              continue;
1923
          }
1924
        } else {
1925
8
          state = kPath;
1926
8
          continue;
1927
        }
1928
102086
        break;
1929
      case kFileSlash:
1930

102078
        if (ch == '/' || ch == '\\') {
1931
101971
          state = kFileHost;
1932
        } else {
1933

107
          if (has_base && base->scheme == "file:") {
1934
103
            url->flags |= URL_FLAGS_HAS_HOST;
1935
103
            url->host = base->host;
1936

201
            if (!StartsWithWindowsDriveLetter(p, end) &&
1937
98
                IsNormalizedWindowsDriveLetter(base->path[0])) {
1938
2
              url->flags |= URL_FLAGS_HAS_PATH;
1939
2
              url->path.push_back(base->path[0]);
1940
            }
1941
          }
1942
107
          state = kPath;
1943
107
          continue;
1944
        }
1945
101971
        break;
1946
      case kFileHost:
1947

102536
        if (ch == kEOL ||
1948
564
            ch == '/' ||
1949
559
            ch == '\\' ||
1950
559
            ch == '?' ||
1951
            ch == '#') {
1952

305925
          if (!has_state_override &&
1953

101987
              buffer.size() == 2 &&
1954
10
              IsWindowsDriveLetter(buffer)) {
1955
4
            state = kPath;
1956
101973
          } else if (buffer.size() == 0) {
1957
101881
            url->flags |= URL_FLAGS_HAS_HOST;
1958
101881
            url->host.clear();
1959
101881
            if (has_state_override)
1960
2
              return;
1961
101879
            state = kPathStart;
1962
          } else {
1963
159
            std::string host;
1964
92
            if (!ParseHost(buffer, &host, special)) {
1965
23
              url->flags |= URL_FLAGS_FAILED;
1966
23
              return;
1967
            }
1968
69
            if (host == "localhost")
1969
22
              host.clear();
1970
69
            url->flags |= URL_FLAGS_HAS_HOST;
1971
69
            url->host = host;
1972
69
            if (has_state_override)
1973
2
              return;
1974
67
            buffer.clear();
1975
67
            state = kPathStart;
1976
          }
1977
101950
          continue;
1978
        } else {
1979
559
          buffer += ch;
1980
        }
1981
559
        break;
1982
      case kPathStart:
1983
171980
        if (IsSpecial(url->scheme)) {
1984
171652
          state = kPath;
1985

171652
          if (ch != '/' && ch != '\\') {
1986
67705
            continue;
1987
          }
1988

330
        } else if (!has_state_override && ch == '?') {
1989
3
          url->flags |= URL_FLAGS_HAS_QUERY;
1990
3
          url->query.clear();
1991
3
          state = kQuery;
1992

327
        } else if (!has_state_override && ch == '#') {
1993
3
          url->flags |= URL_FLAGS_HAS_FRAGMENT;
1994
3
          url->fragment.clear();
1995
3
          state = kFragment;
1996
324
        } else if (ch != kEOL) {
1997
282
          state = kPath;
1998
282
          if (ch != '/') {
1999
26
            continue;
2000
          }
2001
        }
2002
104251
        break;
2003
      case kPath:
2004

12264117
        if (ch == kEOL ||
2005
11033902
            ch == '/' ||
2006
11033872
            special_back_slash ||
2007

14808984
            (!has_state_override && (ch == '?' || ch == '#'))) {
2008
1230634
          if (IsDoubleDotSegment(buffer)) {
2009
1952
            ShortenUrlPath(url);
2010

1952
            if (ch != '/' && !special_back_slash) {
2011
171
              url->flags |= URL_FLAGS_HAS_PATH;
2012
171
              url->path.emplace_back("");
2013
            }
2014

2458685
          } else if (IsSingleDotSegment(buffer) &&
2015

1229102
                     ch != '/' && !special_back_slash) {
2016
308
            url->flags |= URL_FLAGS_HAS_PATH;
2017
308
            url->path.emplace_back("");
2018
1228485
          } else if (!IsSingleDotSegment(buffer)) {
2019

3679437
            if (url->scheme == "file:" &&
2020
1361939
                url->path.empty() &&
2021

1365603
                buffer.size() == 2 &&
2022
45
                IsWindowsDriveLetter(buffer)) {
2023
44
              buffer[1] = ':';
2024
            }
2025
1227682
            url->flags |= URL_FLAGS_HAS_PATH;
2026
1227682
            url->path.emplace_back(std::move(buffer));
2027
          }
2028
1230735
          buffer.clear();
2029
2461472
          if (ch == '?') {
2030
377
            url->flags |= URL_FLAGS_HAS_QUERY;
2031
377
            url->query.clear();
2032
377
            state = kQuery;
2033
1230359
          } else if (ch == '#') {
2034
12
            url->flags |= URL_FLAGS_HAS_FRAGMENT;
2035
12
            url->fragment.clear();
2036
12
            state = kFragment;
2037
          }
2038
        } else {
2039
11033483
          AppendOrEscape(&buffer, ch, PATH_ENCODE_SET);
2040
        }
2041
12264177
        break;
2042
      case kCannotBeBase:
2043
34188
        switch (ch) {
2044
          case '?':
2045
2
            state = kQuery;
2046
2
            break;
2047
          case '#':
2048
5
            state = kFragment;
2049
5
            break;
2050
          default:
2051
34181
            if (url->path.empty())
2052
              url->path.emplace_back("");
2053
34181
            else if (ch != kEOL)
2054
29782
              AppendOrEscape(&url->path[0], ch, C0_CONTROL_ENCODE_SET);
2055
        }
2056
34188
        break;
2057
      case kQuery:
2058

4805
        if (ch == kEOL || (!has_state_override && ch == '#')) {
2059
553
          url->flags |= URL_FLAGS_HAS_QUERY;
2060
553
          url->query = std::move(buffer);
2061
553
          buffer.clear();
2062
1106
          if (ch == '#')
2063
318
            state = kFragment;
2064
        } else {
2065
4252
          AppendOrEscape(&buffer, ch, special ? QUERY_ENCODE_SET_SPECIAL :
2066
4252
                                                QUERY_ENCODE_SET_NONSPECIAL);
2067
        }
2068
4805
        break;
2069
      case kFragment:
2070
3762
        switch (ch) {
2071
          case kEOL:
2072
548
            url->flags |= URL_FLAGS_HAS_FRAGMENT;
2073
548
            url->fragment = std::move(buffer);
2074
548
            break;
2075
          default:
2076
3214
            AppendOrEscape(&buffer, ch, FRAGMENT_ENCODE_SET);
2077
        }
2078
3762
        break;
2079
      default:
2080
        url->flags |= URL_FLAGS_INVALID_PARSE_STATE;
2081
        return;
2082
    }
2083
2084
13336018
    p++;
2085
  }
2086
}  // NOLINT(readability/fn_size)
2087
2088
// https://url.spec.whatwg.org/#url-serializing
2089
31598
std::string URL::SerializeURL(const struct url_data* url,
2090
                              bool exclude = false) {
2091
31598
  std::string output = url->scheme;
2092
31598
  if (url->flags & URL_FLAGS_HAS_HOST) {
2093
31598
    output += "//";
2094

63198
    if (url->flags & URL_FLAGS_HAS_USERNAME ||
2095
31599
        url->flags & URL_FLAGS_HAS_PASSWORD) {
2096
      if (url->flags & URL_FLAGS_HAS_USERNAME) {
2097
        output += url->username;
2098
      }
2099
      if (url->flags & URL_FLAGS_HAS_PASSWORD) {
2100
        output += ":" + url->password;
2101
      }
2102
      output += "@";
2103
    }
2104
31599
    output += url->host;
2105
31599
    if (url->port != -1) {
2106
      output += ":" + std::to_string(url->port);
2107
    }
2108
  }
2109
31599
  if (url->flags & URL_FLAGS_CANNOT_BE_BASE) {
2110
    output += url->path[0];
2111
  } else {
2112

63198
    if (!(url->flags & URL_FLAGS_HAS_HOST) &&
2113

31599
          url->path.size() > 1 &&
2114
          url->path[0].empty()) {
2115
      output += "/.";
2116
    }
2117
395375
    for (size_t i = 1; i < url->path.size(); i++) {
2118
363777
      output += "/" + url->path[i];
2119
    }
2120
  }
2121
31599
  if (url->flags & URL_FLAGS_HAS_QUERY) {
2122
    output = "?" + url->query;
2123
  }
2124

31599
  if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) {
2125
    output = "#" + url->fragment;
2126
  }
2127
31599
  return output;
2128
}
2129
2130
namespace {
2131
116970
void SetArgs(Environment* env,
2132
             Local<Value> argv[ARG_COUNT],
2133
             const struct url_data& url) {
2134
116970
  Isolate* isolate = env->isolate();
2135
233939
  argv[ARG_FLAGS] = Integer::NewFromUnsigned(isolate, url.flags);
2136
116970
  argv[ARG_PROTOCOL] =
2137
116970
      url.flags & URL_FLAGS_SPECIAL ?
2138
112005
          GetSpecial(env, url.scheme) :
2139
462915
          OneByteString(isolate, url.scheme.c_str());
2140
116970
  if (url.flags & URL_FLAGS_HAS_USERNAME)
2141
1040
    argv[ARG_USERNAME] = Utf8String(isolate, url.username);
2142
116970
  if (url.flags & URL_FLAGS_HAS_PASSWORD)
2143
1028
    argv[ARG_PASSWORD] = Utf8String(isolate, url.password);
2144
116970
  if (url.flags & URL_FLAGS_HAS_HOST)
2145
224878
    argv[ARG_HOST] = Utf8String(isolate, url.host);
2146
116970
  if (url.flags & URL_FLAGS_HAS_QUERY)
2147
1110
    argv[ARG_QUERY] = Utf8String(isolate, url.query);
2148
116970
  if (url.flags & URL_FLAGS_HAS_FRAGMENT)
2149
1088
    argv[ARG_FRAGMENT] = Utf8String(isolate, url.fragment);
2150
116970
  if (url.port > -1)
2151
1934
    argv[ARG_PORT] = Integer::New(isolate, url.port);
2152
116970
  if (url.flags & URL_FLAGS_HAS_PATH)
2153
232882
    argv[ARG_PATH] = ToV8Value(env->context(), url.path).ToLocalChecked();
2154
116970
}
2155
2156
118778
void Parse(Environment* env,
2157
           Local<Value> recv,
2158
           const char* input,
2159
           size_t len,
2160
           enum url_parse_state state_override,
2161
           Local<Value> base_obj,
2162
           Local<Value> context_obj,
2163
           Local<Function> cb,
2164
           Local<Value> error_cb) {
2165
118778
  Isolate* isolate = env->isolate();
2166
118778
  Local<Context> context = env->context();
2167
237533
  HandleScope handle_scope(isolate);
2168
118755
  Context::Scope context_scope(context);
2169
2170
118779
  const bool has_context = context_obj->IsObject();
2171
118779
  const bool has_base = base_obj->IsObject();
2172
2173
237534
  url_data base;
2174
237533
  url_data url;
2175
118779
  if (has_context)
2176
36202
    url = HarvestContext(env, context_obj.As<Object>());
2177
118779
  if (has_base)
2178
4046
    base = HarvestBase(env, base_obj.As<Object>());
2179
2180
118779
  URL::Parse(input, len, state_override, &url, has_context, &base, has_base);
2181

118779
  if ((url.flags & URL_FLAGS_INVALID_PARSE_STATE) ||
2182
36202
      ((state_override != kUnknownState) &&
2183
36202
       (url.flags & URL_FLAGS_TERMINATED)))
2184
24
    return;
2185
2186
  // Define the return value placeholders
2187
  const Local<Value> undef = Undefined(isolate);
2188
  const Local<Value> null = Null(isolate);
2189
118755
  if (!(url.flags & URL_FLAGS_FAILED)) {
2190
    Local<Value> argv[] = {
2191
      undef,
2192
      undef,
2193
      undef,
2194
      undef,
2195
      null,  // host defaults to null
2196
      null,  // port defaults to null
2197
      undef,
2198
      null,  // query defaults to null
2199
      null,  // fragment defaults to null
2200
116970
    };
2201
116970
    SetArgs(env, argv, url);
2202
350910
    cb->Call(context, recv, arraysize(argv), argv).FromMaybe(Local<Value>());
2203
1785
  } else if (error_cb->IsFunction()) {
2204
1712
    Local<Value> argv[2] = { undef, undef };
2205
3424
    argv[ERR_ARG_FLAGS] = Integer::NewFromUnsigned(isolate, url.flags);
2206
1712
    argv[ERR_ARG_INPUT] =
2207
3424
      String::NewFromUtf8(env->isolate(), input).ToLocalChecked();
2208
6848
    error_cb.As<Function>()->Call(context, recv, arraysize(argv), argv)
2209
1712
        .FromMaybe(Local<Value>());
2210
  }
2211
}
2212
2213
118779
void Parse(const FunctionCallbackInfo<Value>& args) {
2214
118779
  Environment* env = Environment::GetCurrent(args);
2215
118779
  CHECK_GE(args.Length(), 5);
2216
356337
  CHECK(args[0]->IsString());  // input
2217


485173
  CHECK(args[2]->IsUndefined() ||  // base context
2218
        args[2]->IsNull() ||
2219
        args[2]->IsObject());
2220


537347
  CHECK(args[3]->IsUndefined() ||  // context
2221
        args[3]->IsNull() ||
2222
        args[3]->IsObject());
2223
237558
  CHECK(args[4]->IsFunction());  // complete callback
2224

521491
  CHECK(args[5]->IsUndefined() || args[5]->IsFunction());  // error callback
2225
2226
237558
  Utf8Value input(env->isolate(), args[0]);
2227
118778
  enum url_parse_state state_override = kUnknownState;
2228
237556
  if (args[1]->IsNumber()) {
2229
118778
    state_override = static_cast<enum url_parse_state>(
2230
593890
        args[1]->Uint32Value(env->context()).FromJust());
2231
  }
2232
2233
237558
  Parse(env, args.This(),
2234
118779
        *input, input.length(),
2235
        state_override,
2236
        args[2],
2237
        args[3],
2238
237558
        args[4].As<Function>(),
2239
118778
        args[5]);
2240
118779
}
2241
2242
82
void EncodeAuthSet(const FunctionCallbackInfo<Value>& args) {
2243
82
  Environment* env = Environment::GetCurrent(args);
2244
82
  CHECK_GE(args.Length(), 1);
2245
246
  CHECK(args[0]->IsString());
2246
164
  Utf8Value value(env->isolate(), args[0]);
2247
164
  std::string output;
2248
82
  size_t len = value.length();
2249
82
  output.reserve(len);
2250
593
  for (size_t n = 0; n < len; n++) {
2251
511
    const char ch = (*value)[n];
2252
511
    AppendOrEscape(&output, ch, USERINFO_ENCODE_SET);
2253
  }
2254
164
  args.GetReturnValue().Set(
2255
164
      String::NewFromUtf8(env->isolate(), output.c_str()).ToLocalChecked());
2256
82
}
2257
2258
16
void ToUSVString(const FunctionCallbackInfo<Value>& args) {
2259
16
  Environment* env = Environment::GetCurrent(args);
2260
16
  CHECK_GE(args.Length(), 2);
2261
48
  CHECK(args[0]->IsString());
2262
32
  CHECK(args[1]->IsNumber());
2263
2264
32
  TwoByteValue value(env->isolate(), args[0]);
2265
2266
64
  int64_t start = args[1]->IntegerValue(env->context()).FromJust();
2267
16
  CHECK_GE(start, 0);
2268
2269
47
  for (size_t i = start; i < value.length(); i++) {
2270
31
    char16_t c = value[i];
2271
31
    if (!IsUnicodeSurrogate(c)) {
2272
13
      continue;
2273

18
    } else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
2274
15
      value[i] = kUnicodeReplacementCharacter;
2275
    } else {
2276
3
      char16_t d = value[i + 1];
2277
3
      if (IsUnicodeTrail(d)) {
2278
        i++;
2279
      } else {
2280
3
        value[i] = kUnicodeReplacementCharacter;
2281
      }
2282
    }
2283
  }
2284
2285
32
  args.GetReturnValue().Set(
2286
32
      String::NewFromTwoByte(env->isolate(),
2287
16
                             *value,
2288
                             NewStringType::kNormal,
2289
32
                             value.length()).ToLocalChecked());
2290
16
}
2291
2292
229
void DomainToASCII(const FunctionCallbackInfo<Value>& args) {
2293
229
  Environment* env = Environment::GetCurrent(args);
2294
229
  CHECK_GE(args.Length(), 1);
2295
687
  CHECK(args[0]->IsString());
2296
446
  Utf8Value value(env->isolate(), args[0]);
2297
2298
446
  URLHost host;
2299
  // Assuming the host is used for a special scheme.
2300
229
  host.ParseHost(*value, value.length(), true);
2301
229
  if (host.ParsingFailed()) {
2302
36
    args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(env->isolate(), ""));
2303
12
    return;
2304
  }
2305
434
  std::string out = host.ToStringMove();
2306
434
  args.GetReturnValue().Set(
2307
434
      String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
2308
}
2309
2310
207
void DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
2311
207
  Environment* env = Environment::GetCurrent(args);
2312
207
  CHECK_GE(args.Length(), 1);
2313
621
  CHECK(args[0]->IsString());
2314
402
  Utf8Value value(env->isolate(), args[0]);
2315
2316
402
  URLHost host;
2317
  // Assuming the host is used for a special scheme.
2318
207
  host.ParseHost(*value, value.length(), true, true);
2319
207
  if (host.ParsingFailed()) {
2320
36
    args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(env->isolate(), ""));
2321
12
    return;
2322
  }
2323
390
  std::string out = host.ToStringMove();
2324
390
  args.GetReturnValue().Set(
2325
390
      String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
2326
}
2327
2328
444
void SetURLConstructor(const FunctionCallbackInfo<Value>& args) {
2329
444
  Environment* env = Environment::GetCurrent(args);
2330
444
  CHECK_EQ(args.Length(), 1);
2331
888
  CHECK(args[0]->IsFunction());
2332
888
  env->set_url_constructor_function(args[0].As<Function>());
2333
444
}
2334
2335
444
void Initialize(Local<Object> target,
2336
                Local<Value> unused,
2337
                Local<Context> context,
2338
                void* priv) {
2339
444
  Environment* env = Environment::GetCurrent(context);
2340
444
  env->SetMethod(target, "parse", Parse);
2341
444
  env->SetMethodNoSideEffect(target, "encodeAuth", EncodeAuthSet);
2342
444
  env->SetMethodNoSideEffect(target, "toUSVString", ToUSVString);
2343
444
  env->SetMethodNoSideEffect(target, "domainToASCII", DomainToASCII);
2344
444
  env->SetMethodNoSideEffect(target, "domainToUnicode", DomainToUnicode);
2345
444
  env->SetMethod(target, "setURLConstructor", SetURLConstructor);
2346
2347
#define XX(name, _) NODE_DEFINE_CONSTANT(target, name);
2348
13320
  FLAGS(XX)
2349
#undef XX
2350
2351
#define XX(name) NODE_DEFINE_CONSTANT(target, name);
2352
26640
  PARSESTATES(XX)
2353
1776
#undef XX
2354
1332
}
2355
1776
}  // namespace
2356
2664
2357
5953
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
2358
5953
  registry->Register(Parse);
2359
5509
  registry->Register(EncodeAuthSet);
2360
6397
  registry->Register(ToUSVString);
2361
5953
  registry->Register(DomainToASCII);
2362
5953
  registry->Register(DomainToUnicode);
2363
5953
  registry->Register(SetURLConstructor);
2364
6397
}
2365
2664
2366
2228
std::string URL::ToFilePath() const {
2367
1340
  if (context_.scheme != "file:") {
2368
889
    return "";
2369
1776
  }
2370
1776
2371
1332
#ifdef _WIN32
2372
1776
  const char* slash = "\\";
2373
2220
  auto is_slash = [] (char ch) {
2374
1776
    return ch == '/' || ch == '\\';
2375
1776
  };
2376
888
#else
2377
1339
  const char* slash = "/";
2378
1822
  auto is_slash = [] (char ch) {
2379
1776
    return ch == '/';
2380
934
  };
2381

902
  if ((context_.flags & URL_FLAGS_HAS_HOST) &&
2382
1339
      context_.host.length() > 0) {
2383
1333
    return "";
2384
  }
2385
#endif
2386
12
  std::string decoded_path;
2387
18
  for (const std::string& part : context_.path) {
2388
25
    std::string decoded = PercentDecode(part.c_str(), part.length());
2389
58
    for (char& ch : decoded) {
2390
46
      if (is_slash(ch)) {
2391
1
        return "";
2392
      }
2393
    }
2394
12
    decoded_path += slash + decoded;
2395
  }
2396
2397
#ifdef _WIN32
2398
  // TODO(TimothyGu): Use "\\?\" long paths on Windows.
2399
2400
  // If hostname is set, then we have a UNC path. Pass the hostname through
2401
  // ToUnicode just in case it is an IDN using punycode encoding. We do not
2402
  // need to worry about percent encoding because the URL parser will have
2403
  // already taken care of that for us. Note that this only causes IDNs with an
2404
  // appropriate `xn--` prefix to be decoded.
2405
  if ((context_.flags & URL_FLAGS_HAS_HOST) &&
2406
      context_.host.length() > 0) {
2407
    std::string unicode_host;
2408
    if (!ToUnicode(context_.host, &unicode_host)) {
2409
      return "";
2410
    }
2411
    return "\\\\" + unicode_host + decoded_path;
2412
  }
2413
  // Otherwise, it's a local path that requires a drive letter.
2414
  if (decoded_path.length() < 3) {
2415
    return "";
2416
  }
2417
  if (decoded_path[2] != ':' ||
2418
      !IsASCIIAlpha(decoded_path[1])) {
2419
    return "";
2420
  }
2421
  // Strip out the leading '\'.
2422
  return decoded_path.substr(1);
2423
#else
2424
5
  return decoded_path;
2425
#endif
2426
}
2427
2428
31599
URL URL::FromFilePath(const std::string& file_path) {
2429
31599
  URL url("file://");
2430
63198
  std::string escaped_file_path;
2431
3855260
  for (size_t i = 0; i < file_path.length(); ++i) {
2432
3823662
    escaped_file_path += file_path[i];
2433
3823653
    if (file_path[i] == '%')
2434
11
      escaped_file_path += "25";
2435
  }
2436
31599
  URL::Parse(escaped_file_path.c_str(), escaped_file_path.length(), kPathStart,
2437
31599
             &url.context_, true, nullptr, false);
2438
63198
  return url;
2439
}
2440
2441
// This function works by calling out to a JS function that creates and
2442
// returns the JS URL object. Be mindful of the JS<->Native boundary
2443
// crossing that is required.
2444
MaybeLocal<Value> URL::ToObject(Environment* env) const {
2445
  Isolate* isolate = env->isolate();
2446
  Local<Context> context = env->context();
2447
  Context::Scope context_scope(context);
2448
2449
  const Local<Value> undef = Undefined(isolate);
2450
  const Local<Value> null = Null(isolate);
2451
2452
  if (context_.flags & URL_FLAGS_FAILED)
2453
    return Local<Value>();
2454
2455
  Local<Value> argv[] = {
2456
    undef,
2457
    undef,
2458
    undef,
2459
    undef,
2460
    null,  // host defaults to null
2461
    null,  // port defaults to null
2462
    undef,
2463
    null,  // query defaults to null
2464
    null,  // fragment defaults to null
2465
  };
2466
  SetArgs(env, argv, context_);
2467
2468
  MaybeLocal<Value> ret;
2469
  {
2470
    TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
2471
2472
    // The SetURLConstructor method must have been called already to
2473
    // set the constructor function used below. SetURLConstructor is
2474
    // called automatically when the internal/url.js module is loaded
2475
    // during the internal/bootstrap/node.js processing.
2476
    ret = env->url_constructor_function()
2477
        ->Call(env->context(), undef, arraysize(argv), argv);
2478
  }
2479
2480
  return ret;
2481
}
2482
2483
}  // namespace url
2484
}  // namespace node
2485
2486
4691
NODE_MODULE_CONTEXT_AWARE_INTERNAL(url, node::url::Initialize)
2487

18718
NODE_MODULE_EXTERNAL_REFERENCE(url, node::url::RegisterExternalReferences)