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: 1191 1243 95.8 %
Date: 2020-12-12 04:11:07 Branches: 1070 1211 88.4 %

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
114190
Local<String> Utf8String(Isolate* isolate, const std::string& str) {
35
228380
  return String::NewFromUtf8(isolate,
36
                             str.data(),
37
                             NewStringType::kNormal,
38
228380
                             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
3746
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
3746
  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
3746
    ~Value() {}
84
3746
    Value() : ipv4(0) {}
85
  };
86
87
  Value value_;
88
  HostType type_ = HostType::H_FAILED;
89
90
10587
  void Reset() {
91
    using string = std::string;
92
10587
    switch (type_) {
93
      case HostType::H_DOMAIN:
94
      case HostType::H_OPAQUE:
95
3382
        value_.domain_or_opaque.~string();
96
3382
        break;
97
      default:
98
7205
        break;
99
    }
100
10587
    type_ = HostType::H_FAILED;
101
10587
  }
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
3094
  void SetDomain(std::string&& string) {
118
3094
    Reset();
119
3094
    type_ = HostType::H_DOMAIN;
120
3094
    new(&value_.domain_or_opaque) std::string(std::move(string));
121
3094
  }
122
};
123
124
7492
URLHost::~URLHost() {
125
3746
  Reset();
126
3746
}
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

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

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

456323
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


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

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








84462
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


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


866
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
11054447
bool BitAt(const uint8_t a[], const uint8_t i) {
673
11054447
  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
11054434
void AppendOrEscape(std::string* str,
679
                           const unsigned char ch,
680
                           const uint8_t encode_set[]) {
681
11054434
  if (BitAt(encode_set, ch))
682
1043
    *str += hex[ch];
683
  else
684
11053409
    *str += ch;
685
11054481
}
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
3338
std::string PercentDecode(const char* input, size_t len) {
699
3338
  std::string dest;
700
3338
  if (len == 0)
701
2
    return dest;
702
3336
  dest.reserve(len);
703
3336
  const char* pointer = input;
704
3336
  const char* end = input + len;
705
706
170220
  while (pointer < end) {
707
83442
    const char ch = pointer[0];
708
83442
    size_t remaining = end - pointer - 1;
709


166673
    if (ch != '%' || remaining < 2 ||
710
220
        (ch == '%' &&
711
437
         (!IsASCIIHexDigit(pointer[1]) ||
712
217
          !IsASCIIHexDigit(pointer[2])))) {
713
83231
      dest += ch;
714
83231
      pointer++;
715
83231
      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
3336
  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
281305
bool IsSpecial(const std::string& scheme) {
736
#define V(_, __, name) if (scheme == name) return true;
737



281305
  SPECIALS(V);
738
#undef V
739
5235
  return false;
740
}
741
742
111621
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



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









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

2940
    IsWindowsDriveLetter(p[0], p[1]) &&
762
14
    (length == 2 ||
763
21
      p[2] == '/' ||
764
10
      p[2] == '\\' ||
765
5
      p[2] == '?' ||
766
2911
      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
3325
bool ToASCII(const std::string& input, std::string* output) {
779
6650
  MaybeStackBuffer<char> buf;
780
3325
  if (i18n::ToASCII(&buf, input.c_str(), input.length()) < 0)
781
77
    return false;
782
3248
  if (buf.length() == 0)
783
16
    return false;
784
3232
  output->assign(*buf, buf.length());
785
3232
  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
3302
int64_t ParseNumber(const char* start, const char* end) {
917
3302
  unsigned R = 10;
918

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

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

3831
    switch (R) {
933
      case 8:
934

173
        if (ch < '0' || ch > '7')
935
19
          return -1;
936
154
        break;
937
      case 10:
938
3536
        if (!IsASCIIDigit(ch))
939
3065
          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
3167
void URLHost::ParseIPv4Host(const char* input, size_t length, bool* is_ipv4) {
952
3167
  CHECK_EQ(type_, HostType::H_FAILED);
953
3167
  *is_ipv4 = false;
954
3167
  const char* pointer = input;
955
3167
  const char* mark = input;
956
3167
  const char* end = pointer + length;
957
3167
  int parts = 0;
958
3167
  uint32_t val = 0;
959
  uint64_t numbers[4];
960
3167
  int tooBigNumbers = 0;
961
3167
  if (length == 0)
962
3115
    return;
963
964
57581
  while (pointer <= end) {
965
30303
    const char ch = pointer < end ? pointer[0] : kEOL;
966
30303
    int remaining = end - pointer - 1;
967

30303
    if (ch == '.' || ch == kEOL) {
968
3310
      if (++parts > static_cast<int>(arraysize(numbers)))
969
2
        return;
970
3308
      if (pointer == mark)
971
6
        return;
972
3302
      int64_t n = ParseNumber(mark, pointer);
973
3302
      if (n < 0)
974
3086
        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
27207
    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
3746
void URLHost::ParseHost(const char* input,
1025
                        size_t length,
1026
                        bool is_special,
1027
                        bool unicode) {
1028
3746
  CHECK_EQ(type_, HostType::H_FAILED);
1029
3746
  const char* pointer = input;
1030
1031
3746
  if (length == 0)
1032
652
    return;
1033
1034
3746
  if (pointer[0] == '[') {
1035
98
    if (pointer[length - 1] != ']')
1036
6
      return;
1037
92
    return ParseIPv6Host(++pointer, length - 2);
1038
  }
1039
1040
3648
  if (!is_special)
1041
323
    return ParseOpaqueHost(input, length);
1042
1043
  // First, we have to percent decode
1044
6419
  std::string decoded = PercentDecode(input, length);
1045
1046
  // Then we have to punycode toASCII
1047
3325
  if (!ToASCII(decoded, &decoded))
1048
93
    return;
1049
1050
  // If any of the following characters are still present, we have to fail
1051
86143
  for (size_t n = 0; n < decoded.size(); n++) {
1052
82976
    const char ch = decoded[n];
1053
82976
    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
3167
  ParseIPv4Host(decoded.c_str(), decoded.length(), &is_ipv4);
1061
3167
  if (is_ipv4)
1062
73
    return;
1063
1064
  // If the unicode flag is set, run the result through punycode ToUnicode
1065

3094
  if (unicode && !ToUnicode(decoded, &decoded))
1066
    return;
1067
1068
  // It's not an IPv4 or IPv6 address, it must be a domain
1069
3094
  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
3459
std::string URLHost::ToStringMove() {
1104
3459
  std::string return_value;
1105
3459
  switch (type_) {
1106
    case HostType::H_DOMAIN:
1107
    case HostType::H_OPAQUE:
1108
3382
      return_value = std::move(value_.domain_or_opaque);
1109
3382
      break;
1110
    default:
1111
77
      return_value = ToString();
1112
77
      break;
1113
  }
1114
3459
  Reset();
1115
3459
  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
3355
bool ParseHost(const std::string& input,
1172
               std::string* output,
1173
               bool is_special,
1174
               bool unicode = false) {
1175
3355
  if (input.empty()) {
1176
45
    output->clear();
1177
45
    return true;
1178
  }
1179
6620
  URLHost host;
1180
3310
  host.ParseHost(input.c_str(), input.length(), is_special, unicode);
1181
3310
  if (host.ParsingFailed())
1182
263
    return false;
1183
3047
  *output = host.ToStringMove();
1184
3047
  return true;
1185
}
1186
1187
3984
std::vector<std::string> FromJSStringArray(Environment* env,
1188
                                           Local<Array> array) {
1189
3984
  std::vector<std::string> vec;
1190
3984
  if (array->Length() > 0)
1191
3976
    vec.reserve(array->Length());
1192
58748
  for (size_t n = 0; n < array->Length(); n++) {
1193
76170
    Local<Value> val = array->Get(env->context(), n).ToLocalChecked();
1194
50780
    if (val->IsString()) {
1195
50780
      Utf8Value value(env->isolate(), val.As<String>());
1196
25390
      vec.emplace_back(*value, value.length());
1197
    }
1198
  }
1199
3984
  return vec;
1200
}
1201
1202
3984
url_data HarvestBase(Environment* env, Local<Object> base_obj) {
1203
3984
  url_data base;
1204
3984
  Local<Context> context = env->context();
1205
1206
  Local<Value> flags =
1207
15936
      base_obj->Get(env->context(), env->flags_string()).ToLocalChecked();
1208
3984
  if (flags->IsInt32())
1209
7968
    base.flags = flags->Int32Value(context).FromJust();
1210
1211
  Local<Value> port =
1212
15936
      base_obj->Get(env->context(), env->port_string()).ToLocalChecked();
1213
3984
  if (port->IsInt32())
1214
8
    base.port = port->Int32Value(context).FromJust();
1215
1216
  Local<Value> scheme =
1217
15936
      base_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
1218
3984
  base.scheme = Utf8Value(env->isolate(), scheme).out();
1219
1220
  auto GetStr = [&](std::string url_data::*member,
1221
                    int flag,
1222
                    Local<String> name,
1223
19920
                    bool empty_as_present) {
1224
71161
    Local<Value> value = base_obj->Get(env->context(), name).ToLocalChecked();
1225
39840
    if (value->IsString()) {
1226
22802
      Utf8Value utf8value(env->isolate(), value.As<String>());
1227
14846
      (base.*member).assign(*utf8value, utf8value.length());
1228

27337
      if (empty_as_present || value.As<String>()->Length() != 0) {
1229
3445
        base.flags |= flag;
1230
      }
1231
    }
1232
23904
  };
1233
3984
  GetStr(&url_data::username,
1234
         URL_FLAGS_HAS_USERNAME,
1235
         env->username_string(),
1236
3984
         false);
1237
3984
  GetStr(&url_data::password,
1238
         URL_FLAGS_HAS_PASSWORD,
1239
         env->password_string(),
1240
3984
         false);
1241
3984
  GetStr(&url_data::host, URL_FLAGS_HAS_HOST, env->host_string(), true);
1242
3984
  GetStr(&url_data::query, URL_FLAGS_HAS_QUERY, env->query_string(), true);
1243
3984
  GetStr(&url_data::fragment,
1244
         URL_FLAGS_HAS_FRAGMENT,
1245
         env->fragment_string(),
1246
3984
         true);
1247
1248
  Local<Value>
1249
15936
      path = base_obj->Get(env->context(), env->path_string()).ToLocalChecked();
1250
3984
  if (path->IsArray()) {
1251
3984
    base.flags |= URL_FLAGS_HAS_PATH;
1252
3984
    base.path = FromJSStringArray(env, path.As<Array>());
1253
  }
1254
3984
  return base;
1255
}
1256
1257
36091
url_data HarvestContext(Environment* env, Local<Object> context_obj) {
1258
36091
  url_data context;
1259
  Local<Value> flags =
1260
144368
      context_obj->Get(env->context(), env->flags_string()).ToLocalChecked();
1261
36092
  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
72184
    context.flags |= flags.As<Int32>()->Value() & kCopyFlagsMask;
1269
  }
1270
  Local<Value> scheme =
1271
144368
      context_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
1272
72184
  if (scheme->IsString()) {
1273
72184
    Utf8Value value(env->isolate(), scheme);
1274
36092
    context.scheme.assign(*value, value.length());
1275
  }
1276
  Local<Value> port =
1277
144368
      context_obj->Get(env->context(), env->port_string()).ToLocalChecked();
1278
36092
  if (port->IsInt32())
1279
444
    context.port = port.As<Int32>()->Value();
1280
36092
  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
36092
  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
72184
      context_obj->Get(env->context(),
1298
180460
                       env->host_string()).ToLocalChecked();
1299
72184
  if (host->IsString()) {
1300
72150
    Utf8Value value(env->isolate(), host);
1301
36075
    context.host.assign(*value, value.length());
1302
  }
1303
36092
  return context;
1304
}
1305
1306
// Single dot segment can be ".", "%2e", or "%2E"
1307
2452476
bool IsSingleDotSegment(const std::string& str) {
1308
2452476
  switch (str.size()) {
1309
    case 1:
1310
2556
      return str == ".";
1311
    case 3:
1312
114816
      return str[0] == '%' &&
1313

114804
             str[1] == '2' &&
1314
114804
             ASCIILowercase(str[2]) == 'e';
1315
    default:
1316
2335143
      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
1228308
bool IsDoubleDotSegment(const std::string& str) {
1324

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

294637
      if (str[0] != '.' && str[0] != '%')
1329
294625
        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
60105
      return (str[0] == '%' &&
1340
14
              str[1] == '2' &&
1341
9
              ASCIILowercase(str[2]) == 'e' &&
1342
4
              str[3] == '%' &&
1343

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


4994
  if (url->path.size() == 1 && url->scheme == "file:" &&
1353
357
      IsNormalizedWindowsDriveLetter(url->path[0])) return;
1354
4637
  url->path.pop_back();
1355
}
1356
1357
}  // anonymous namespace
1358
1359
181577
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
181577
  const char* p = input;
1367
181577
  const char* end = input + len;
1368
1369
181577
  if (!has_url) {
1370
113928
    for (const char* ptr = p; ptr < end; ptr++) {
1371
113917
      if (IsC0ControlOrSpace(*ptr))
1372
28
        p++;
1373
      else
1374
113889
        break;
1375
    }
1376
113925
    for (const char* ptr = end - 1; ptr >= p; ptr--) {
1377
113914
      if (IsC0ControlOrSpace(*ptr))
1378
25
        end--;
1379
      else
1380
113889
        break;
1381
    }
1382
113900
    input = p;
1383
113900
    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
361189
  std::string whitespace_stripped;
1390
13253653
  for (const char* ptr = p; ptr < end; ptr++) {
1391
13072218
    if (!IsASCIITabOrNewline(*ptr))
1392
13072077
      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
181577
  bool atflag = false;  // Set when @ has been seen.
1413
181577
  bool square_bracket_flag = false;  // Set inside of [...]
1414
181577
  bool password_token_seen_flag = false;  // Set after a : after an username.
1415
1416
361189
  std::string buffer;
1417
1418
  // Set the initial parse state.
1419
181683
  const bool has_state_override = state_override != kUnknownState;
1420
181683
  enum url_parse_state state = has_state_override ? state_override :
1421
181683
                                                    kSchemeStart;
1422
1423

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

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

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





13495649
    switch (state) {
1435
      case kSchemeStart:
1436
113949
        if (IsASCIIAlpha(ch)) {
1437
110093
          buffer += ASCIILowercase(ch);
1438
109970
          state = kScheme;
1439
3858
        } else if (!has_state_override) {
1440
3851
          state = kNoScheme;
1441
3851
          continue;
1442
        } else {
1443
7
          url->flags |= URL_FLAGS_FAILED;
1444
7
          return;
1445
        }
1446
109970
        break;
1447
      case kScheme:
1448


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

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

109381
          if (has_state_override && buffer.size() == 0) {
1452
            url->flags |= URL_FLAGS_TERMINATED;
1453
            return;
1454
          }
1455
109381
          buffer += ':';
1456
1457
109381
          bool new_is_special = IsSpecial(buffer);
1458
1459
109381
          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
109365
          url->scheme = std::move(buffer);
1472
109365
          url->port = NormalizePort(url->scheme, url->port);
1473
109365
          if (new_is_special) {
1474
104539
            url->flags |= URL_FLAGS_SPECIAL;
1475
104539
            special = true;
1476
          } else {
1477
4826
            url->flags &= ~URL_FLAGS_SPECIAL;
1478
4826
            special = false;
1479
          }
1480
109365
          buffer.clear();
1481
109365
          if (has_state_override)
1482
20
            return;
1483
109345
          if (url->scheme == "file:") {
1484
101730
            state = kFile;
1485

10415
          } else if (special &&
1486

8132
                     has_base &&
1487
517
                     url->scheme == base->scheme) {
1488
178
            state = kSpecialRelativeOrAuthority;
1489
7437
          } else if (special) {
1490
2622
            state = kSpecialAuthoritySlashes;
1491

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

4555
        cannot_be_base = has_base && (base->flags & URL_FLAGS_CANNOT_BE_BASE);
1512

4555
        if (!has_base || (cannot_be_base && ch != '#')) {
1513
1390
          url->flags |= URL_FLAGS_FAILED;
1514
1390
          return;
1515

3165
        } else if (cannot_be_base && ch == '#') {
1516
14
          url->scheme = base->scheme;
1517
14
          if (IsSpecial(url->scheme)) {
1518
            url->flags |= URL_FLAGS_SPECIAL;
1519
            special = true;
1520
          } else {
1521
14
            url->flags &= ~URL_FLAGS_SPECIAL;
1522
14
            special = false;
1523
          }
1524
14
          if (base->flags & URL_FLAGS_HAS_PATH) {
1525
14
            url->flags |= URL_FLAGS_HAS_PATH;
1526
14
            url->path = base->path;
1527
          }
1528
14
          if (base->flags & URL_FLAGS_HAS_QUERY) {
1529
2
            url->flags |= URL_FLAGS_HAS_QUERY;
1530
2
            url->query = base->query;
1531
          }
1532
14
          if (base->flags & URL_FLAGS_HAS_FRAGMENT) {
1533
            url->flags |= URL_FLAGS_HAS_FRAGMENT;
1534
            url->fragment = base->fragment;
1535
          }
1536
14
          url->flags |= URL_FLAGS_CANNOT_BE_BASE;
1537
14
          state = kFragment;
1538

6302
        } else if (has_base &&
1539
3151
                   base->scheme != "file:") {
1540
227
          state = kRelative;
1541
227
          continue;
1542
        } else {
1543
2924
          url->scheme = "file:";
1544
2924
          url->flags |= URL_FLAGS_SPECIAL;
1545
2924
          special = true;
1546
2924
          state = kFile;
1547
2924
          continue;
1548
        }
1549
14
        break;
1550
      case kSpecialRelativeOrAuthority:
1551

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

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


41
        if (IsSpecial(url->scheme) && (ch == '/' || ch == '\\')) {
1676
8
          state = kSpecialAuthorityIgnoreSlashes;
1677
33
        } else if (ch == '/') {
1678
3
          state = kAuthority;
1679
        } else {
1680
30
          if (base->flags & URL_FLAGS_HAS_USERNAME) {
1681
4
            url->flags |= URL_FLAGS_HAS_USERNAME;
1682
4
            url->username = base->username;
1683
          }
1684
30
          if (base->flags & URL_FLAGS_HAS_PASSWORD) {
1685
2
            url->flags |= URL_FLAGS_HAS_PASSWORD;
1686
2
            url->password = base->password;
1687
          }
1688
30
          if (base->flags & URL_FLAGS_HAS_HOST) {
1689
26
            url->flags |= URL_FLAGS_HAS_HOST;
1690
26
            url->host = base->host;
1691
          }
1692
30
          url->port = base->port;
1693
30
          state = kPath;
1694
30
          continue;
1695
        }
1696
11
        break;
1697
      case kSpecialAuthoritySlashes:
1698
2622
        state = kSpecialAuthorityIgnoreSlashes;
1699

2622
        if (ch == '/' && p + 1 < end && p[1] == '/') {
1700
2537
          p++;
1701
        } else {
1702
85
          continue;
1703
        }
1704
2537
        break;
1705
      case kSpecialAuthorityIgnoreSlashes:
1706

2835
        if (ch != '/' && ch != '\\') {
1707
2792
          state = kAuthority;
1708
2792
          continue;
1709
        }
1710
43
        break;
1711
      case kAuthority:
1712
81927
        if (ch == '@') {
1713
413
          if (atflag) {
1714
13
            buffer.reserve(buffer.size() + 3);
1715
13
            buffer.insert(0, "%40");
1716
          }
1717
413
          atflag = true;
1718
413
          size_t blen = buffer.size();
1719

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

81514
        } else if (ch == kEOL ||
1739
78427
                   ch == '/' ||
1740
78409
                   ch == '?' ||
1741
78399
                   ch == '#' ||
1742
                   special_back_slash) {
1743

3123
          if (atflag && buffer.size() == 0) {
1744
39
            url->flags |= URL_FLAGS_FAILED;
1745
39
            return;
1746
          }
1747
3084
          p -= buffer.size() + 1;
1748
3084
          buffer.clear();
1749
3084
          state = kHost;
1750
        } else {
1751
78391
          buffer += ch;
1752
        }
1753
81888
        break;
1754
      case kHost:
1755
      case kHostname:
1756

75350
        if (has_state_override && url->scheme == "file:") {
1757
6
          state = kFileHost;
1758
6
          continue;
1759

75344
        } else if (ch == ':' && !square_bracket_flag) {
1760
864
          if (buffer.size() == 0) {
1761
19
            url->flags |= URL_FLAGS_FAILED;
1762
19
            return;
1763
          }
1764
845
          url->flags |= URL_FLAGS_HAS_HOST;
1765
845
          if (!ParseHost(buffer, &url->host, special)) {
1766
3
            url->flags |= URL_FLAGS_FAILED;
1767
3
            return;
1768
          }
1769
842
          buffer.clear();
1770
842
          state = kPort;
1771
1682
          if (state_override == kHostname) {
1772
2
            return;
1773
          }
1774

74480
        } else if (ch == kEOL ||
1775
72092
                   ch == '/' ||
1776
72070
                   ch == '?' ||
1777
72056
                   ch == '#' ||
1778
                   special_back_slash) {
1779
2434
          p--;
1780

2434
          if (special && buffer.size() == 0) {
1781
12
            url->flags |= URL_FLAGS_FAILED;
1782
12
            return;
1783
          }
1784

2613
          if (has_state_override &&
1785

2445
              buffer.size() == 0 &&
1786

53
              ((url->username.size() > 0 || url->password.size() > 0) ||
1787
17
               url->port != -1)) {
1788
4
            url->flags |= URL_FLAGS_TERMINATED;
1789
4
            return;
1790
          }
1791
2418
          url->flags |= URL_FLAGS_HAS_HOST;
1792
2418
          if (!ParseHost(buffer, &url->host, special)) {
1793
237
            url->flags |= URL_FLAGS_FAILED;
1794
237
            return;
1795
          }
1796
2181
          buffer.clear();
1797
2181
          state = kPathStart;
1798
4229
          if (has_state_override) {
1799
133
            return;
1800
          }
1801
        } else {
1802
72046
          if (ch == '[')
1803
94
            square_bracket_flag = true;
1804
72046
          if (ch == ']')
1805
91
            square_bracket_flag = false;
1806
72046
          buffer += ch;
1807
        }
1808
74934
        break;
1809
      case kPort:
1810
4765
        if (IsASCIIDigit(ch)) {
1811
3878
          buffer += ch;
1812

887
        } else if (has_state_override ||
1813
445
                   ch == kEOL ||
1814
27
                   ch == '/' ||
1815
27
                   ch == '?' ||
1816
27
                   ch == '#' ||
1817
                   special_back_slash) {
1818
860
          if (buffer.size() > 0) {
1819
851
            unsigned port = 0;
1820
            // the condition port <= 0xffff prevents integer overflow
1821

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

104654
        if (ch == '/' || ch == '\\') {
1859
101834
          state = kFileSlash;
1860

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

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

101834
        if (ch == '/' || ch == '\\') {
1927
101727
          state = kFileHost;
1928
        } else {
1929

107
          if (has_base && base->scheme == "file:") {
1930
103
            url->flags |= URL_FLAGS_HAS_HOST;
1931
103
            url->host = base->host;
1932

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

102292
        if (ch == kEOL ||
1944
564
            ch == '/' ||
1945
559
            ch == '\\' ||
1946
559
            ch == '?' ||
1947
            ch == '#') {
1948

305193
          if (!has_state_override &&
1949

101743
              buffer.size() == 2 &&
1950
10
              IsWindowsDriveLetter(buffer)) {
1951
4
            state = kPath;
1952
101729
          } else if (buffer.size() == 0) {
1953
101637
            url->flags |= URL_FLAGS_HAS_HOST;
1954
101637
            url->host.clear();
1955
101637
            if (has_state_override)
1956
2
              return;
1957
101635
            state = kPathStart;
1958
          } else {
1959
159
            std::string host;
1960
92
            if (!ParseHost(buffer, &host, special)) {
1961
23
              url->flags |= URL_FLAGS_FAILED;
1962
23
              return;
1963
            }
1964
69
            if (host == "localhost")
1965
22
              host.clear();
1966
69
            url->flags |= URL_FLAGS_HAS_HOST;
1967
69
            url->host = host;
1968
69
            if (has_state_override)
1969
2
              return;
1970
67
            buffer.clear();
1971
67
            state = kPathStart;
1972
          }
1973
101706
          continue;
1974
        } else {
1975
559
          buffer += ch;
1976
        }
1977
559
        break;
1978
      case kPathStart:
1979
171626
        if (IsSpecial(url->scheme)) {
1980
171296
          state = kPath;
1981

171296
          if (ch != '/' && ch != '\\') {
1982
67577
            continue;
1983
          }
1984

330
        } else if (!has_state_override && ch == '?') {
1985
3
          url->flags |= URL_FLAGS_HAS_QUERY;
1986
3
          url->query.clear();
1987
3
          state = kQuery;
1988

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

12238894
        if (ch == kEOL ||
2001
11011031
            ch == '/' ||
2002
11010999
            special_back_slash ||
2003

14774270
            (!has_state_override && (ch == '?' || ch == '#'))) {
2004
1228284
          if (IsDoubleDotSegment(buffer)) {
2005
1911
            ShortenUrlPath(url);
2006

1911
            if (ch != '/' && !special_back_slash) {
2007
171
              url->flags |= URL_FLAGS_HAS_PATH;
2008
171
              url->path.emplace_back("");
2009
            }
2010

2453871
          } else if (IsSingleDotSegment(buffer) &&
2011

1226707
                     ch != '/' && !special_back_slash) {
2012
310
            url->flags |= URL_FLAGS_HAS_PATH;
2013
310
            url->path.emplace_back("");
2014
1226086
          } else if (!IsSingleDotSegment(buffer)) {
2015

3672284
            if (url->scheme == "file:" &&
2016
1359158
                url->path.empty() &&
2017

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

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

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

63170
    if (!(url->flags & URL_FLAGS_HAS_HOST) &&
2109

31585
          url->path.size() > 1 &&
2110
          url->path[0].empty()) {
2111
      output += "/.";
2112
    }
2113
395263
    for (size_t i = 1; i < url->path.size(); i++) {
2114
363678
      output += "/" + url->path[i];
2115
    }
2116
  }
2117
31585
  if (url->flags & URL_FLAGS_HAS_QUERY) {
2118
    output = "?" + url->query;
2119
  }
2120

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

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


483357
  CHECK(args[2]->IsUndefined() ||  // base context
2214
        args[2]->IsNull() ||
2215
        args[2]->IsObject());
2216


535621
  CHECK(args[3]->IsUndefined() ||  // context
2217
        args[3]->IsNull() ||
2218
        args[3]->IsObject());
2219
236774
  CHECK(args[4]->IsFunction());  // complete callback
2220

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

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

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

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