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: 645 1244 51.8 %
Date: 2021-02-19 04:08:54 Branches: 501 1227 40.8 %

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

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

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

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


6
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


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

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








16141
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


6
TWO_CHAR_STRING_TEST(8, IsWindowsDriveLetter,
215
                     (IsASCIIAlpha(ch1) && (ch2 == ':' || ch2 == '|')))
216
217
// https://url.spec.whatwg.org/#normalized-windows-drive-letter
218
TWO_CHAR_STRING_TEST(8, IsNormalizedWindowsDriveLetter,
219
                     (IsASCIIAlpha(ch1) && ch2 == ':'))
220
221
// If a UTF-16 character is a low/trailing surrogate.
222
CHAR_TEST(16, IsUnicodeTrail, (ch & 0xFC00) == 0xDC00)
223
224
// If a UTF-16 character is a surrogate.
225
CHAR_TEST(16, IsUnicodeSurrogate, (ch & 0xF800) == 0xD800)
226
227
// If a UTF-16 surrogate is a low/trailing one.
228
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
4427708
bool BitAt(const uint8_t a[], const uint8_t i) {
673
4427708
  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
4427708
void AppendOrEscape(std::string* str,
679
                           const unsigned char ch,
680
                           const uint8_t encode_set[]) {
681
4427708
  if (BitAt(encode_set, ch))
682
    *str += hex[ch];
683
  else
684
4427708
    *str += ch;
685
4427708
}
686
687
template <typename T>
688
6
unsigned hex2bin(const T ch) {
689

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

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


32375
    if (ch != '%' || remaining < 2 ||
710
3
        (ch == '%' &&
711
6
         (!IsASCIIHexDigit(pointer[1]) ||
712
3
          !IsASCIIHexDigit(pointer[2])))) {
713
16186
      dest += ch;
714
16186
      pointer++;
715
16186
      continue;
716
    } else {
717
3
      unsigned a = hex2bin(pointer[1]);
718
3
      unsigned b = hex2bin(pointer[2]);
719
3
      char c = static_cast<char>(a * 16 + b);
720
3
      dest += c;
721
3
      pointer += 3;
722
    }
723
  }
724
930
  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
104684
bool IsSpecial(const std::string& scheme) {
736
#define V(_, __, name) if (scheme == name) return true;
737



104684
  SPECIALS(V);
738
#undef V
739
1
  return false;
740
}
741
742
39538
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



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









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

12
    IsWindowsDriveLetter(p[0], p[1]) &&
762
    (length == 2 ||
763
      p[2] == '/' ||
764
      p[2] == '\\' ||
765
      p[2] == '?' ||
766
12
      p[2] == '#');
767
}
768
769
#if defined(NODE_HAVE_I18N_SUPPORT)
770
bool ToUnicode(const std::string& input, std::string* output) {
771
  MaybeStackBuffer<char> buf;
772
  if (i18n::ToUnicode(&buf, input.c_str(), input.length()) < 0)
773
    return false;
774
  output->assign(*buf, buf.length());
775
  return true;
776
}
777
778
919
bool ToASCII(const std::string& input, std::string* output) {
779
1838
  MaybeStackBuffer<char> buf;
780
919
  if (i18n::ToASCII(&buf, input.c_str(), input.length()) < 0)
781
    return false;
782
919
  if (buf.length() == 0)
783
    return false;
784
919
  output->assign(*buf, buf.length());
785
919
  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
void URLHost::ParseIPv6Host(const char* input, size_t length) {
801
  CHECK_EQ(type_, HostType::H_FAILED);
802
  unsigned size = arraysize(value_.ipv6);
803
  for (unsigned n = 0; n < size; n++)
804
    value_.ipv6[n] = 0;
805
  uint16_t* piece_pointer = &value_.ipv6[0];
806
  uint16_t* const buffer_end = piece_pointer + size;
807
  uint16_t* compress_pointer = nullptr;
808
  const char* pointer = input;
809
  const char* end = pointer + length;
810
  unsigned value, len, numbers_seen;
811
  char ch = pointer < end ? pointer[0] : kEOL;
812
  if (ch == ':') {
813
    if (length < 2 || pointer[1] != ':')
814
      return;
815
    pointer += 2;
816
    ch = pointer < end ? pointer[0] : kEOL;
817
    piece_pointer++;
818
    compress_pointer = piece_pointer;
819
  }
820
  while (ch != kEOL) {
821
    if (piece_pointer >= buffer_end)
822
      return;
823
    if (ch == ':') {
824
      if (compress_pointer != nullptr)
825
        return;
826
      pointer++;
827
      ch = pointer < end ? pointer[0] : kEOL;
828
      piece_pointer++;
829
      compress_pointer = piece_pointer;
830
      continue;
831
    }
832
    value = 0;
833
    len = 0;
834
    while (len < 4 && IsASCIIHexDigit(ch)) {
835
      value = value * 0x10 + hex2bin(ch);
836
      pointer++;
837
      ch = pointer < end ? pointer[0] : kEOL;
838
      len++;
839
    }
840
    switch (ch) {
841
      case '.':
842
        if (len == 0)
843
          return;
844
        pointer -= len;
845
        ch = pointer < end ? pointer[0] : kEOL;
846
        if (piece_pointer > buffer_end - 2)
847
          return;
848
        numbers_seen = 0;
849
        while (ch != kEOL) {
850
          value = 0xffffffff;
851
          if (numbers_seen > 0) {
852
            if (ch == '.' && numbers_seen < 4) {
853
              pointer++;
854
              ch = pointer < end ? pointer[0] : kEOL;
855
            } else {
856
              return;
857
            }
858
          }
859
          if (!IsASCIIDigit(ch))
860
            return;
861
          while (IsASCIIDigit(ch)) {
862
            unsigned number = ch - '0';
863
            if (value == 0xffffffff) {
864
              value = number;
865
            } else if (value == 0) {
866
              return;
867
            } else {
868
              value = value * 10 + number;
869
            }
870
            if (value > 255)
871
              return;
872
            pointer++;
873
            ch = pointer < end ? pointer[0] : kEOL;
874
          }
875
          *piece_pointer = *piece_pointer * 0x100 + value;
876
          numbers_seen++;
877
          if (numbers_seen == 2 || numbers_seen == 4)
878
            piece_pointer++;
879
        }
880
        if (numbers_seen != 4)
881
          return;
882
        continue;
883
      case ':':
884
        pointer++;
885
        ch = pointer < end ? pointer[0] : kEOL;
886
        if (ch == kEOL)
887
          return;
888
        break;
889
      case kEOL:
890
        break;
891
      default:
892
        return;
893
    }
894
    *piece_pointer = value;
895
    piece_pointer++;
896
  }
897
898
  if (compress_pointer != nullptr) {
899
    unsigned swaps = piece_pointer - compress_pointer;
900
    piece_pointer = buffer_end - 1;
901
    while (piece_pointer != &value_.ipv6[0] && swaps > 0) {
902
      uint16_t temp = *piece_pointer;
903
      uint16_t* swap_piece = compress_pointer + swaps - 1;
904
      *piece_pointer = *swap_piece;
905
      *swap_piece = temp;
906
       piece_pointer--;
907
       swaps--;
908
    }
909
  } else if (compress_pointer == nullptr &&
910
             piece_pointer != buffer_end) {
911
    return;
912
  }
913
  type_ = HostType::H_IPV6;
914
}
915
916
919
int64_t ParseNumber(const char* start, const char* end) {
917
919
  unsigned R = 10;
918

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

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

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

8444
    if (ch == '.' || ch == kEOL) {
968
919
      if (++parts > static_cast<int>(arraysize(numbers)))
969
        return;
970
919
      if (pointer == mark)
971
        return;
972
919
      int64_t n = ParseNumber(mark, pointer);
973
919
      if (n < 0)
974
919
        return;
975
976
      if (n > 255) {
977
        tooBigNumbers++;
978
      }
979
      numbers[parts - 1] = n;
980
      mark = pointer + 1;
981
      if (ch == '.' && remaining == 0)
982
        break;
983
    }
984
7525
    pointer++;
985
  }
986
  CHECK_GT(parts, 0);
987
  *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
  if (tooBigNumbers > 1 ||
993
      (tooBigNumbers == 1 && numbers[parts - 1] <= 255) ||
994
      numbers[parts - 1] >= pow(256, static_cast<double>(5 - parts))) {
995
    return;
996
  }
997
998
  type_ = HostType::H_IPV4;
999
  val = numbers[parts - 1];
1000
  for (int n = 0; n < parts - 1; n++) {
1001
    double b = 3 - n;
1002
    val += numbers[n] * pow(256, b);
1003
  }
1004
1005
  value_.ipv4 = val;
1006
}
1007
1008
void URLHost::ParseOpaqueHost(const char* input, size_t length) {
1009
  CHECK_EQ(type_, HostType::H_FAILED);
1010
  std::string output;
1011
  output.reserve(length);
1012
  for (size_t i = 0; i < length; i++) {
1013
    const char ch = input[i];
1014
    if (ch != '%' && IsForbiddenHostCodePoint(ch)) {
1015
      return;
1016
    } else {
1017
      AppendOrEscape(&output, ch, C0_CONTROL_ENCODE_SET);
1018
    }
1019
  }
1020
1021
  SetOpaque(std::move(output));
1022
}
1023
1024
919
void URLHost::ParseHost(const char* input,
1025
                        size_t length,
1026
                        bool is_special,
1027
                        bool unicode) {
1028
919
  CHECK_EQ(type_, HostType::H_FAILED);
1029
919
  const char* pointer = input;
1030
1031
919
  if (length == 0)
1032
    return;
1033
1034
919
  if (pointer[0] == '[') {
1035
    if (pointer[length - 1] != ']')
1036
      return;
1037
    return ParseIPv6Host(++pointer, length - 2);
1038
  }
1039
1040
919
  if (!is_special)
1041
    return ParseOpaqueHost(input, length);
1042
1043
  // First, we have to percent decode
1044
1838
  std::string decoded = PercentDecode(input, length);
1045
1046
  // Then we have to punycode toASCII
1047
919
  if (!ToASCII(decoded, &decoded))
1048
    return;
1049
1050
  // If any of the following characters are still present, we have to fail
1051
17060
  for (size_t n = 0; n < decoded.size(); n++) {
1052
16141
    const char ch = decoded[n];
1053
16141
    if (IsForbiddenHostCodePoint(ch)) {
1054
      return;
1055
    }
1056
  }
1057
1058
  // Check to see if it's an IPv4 IP address
1059
  bool is_ipv4;
1060
919
  ParseIPv4Host(decoded.c_str(), decoded.length(), &is_ipv4);
1061
919
  if (is_ipv4)
1062
    return;
1063
1064
  // If the unicode flag is set, run the result through punycode ToUnicode
1065

919
  if (unicode && !ToUnicode(decoded, &decoded))
1066
    return;
1067
1068
  // It's not an IPv4 or IPv6 address, it must be a domain
1069
919
  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
T* FindLongestZeroSequence(T* values, size_t len) {
1076
  T* start = values;
1077
  T* end = start + len;
1078
  T* result = nullptr;
1079
1080
  T* current = nullptr;
1081
  unsigned counter = 0, longest = 1;
1082
1083
  while (start < end) {
1084
    if (*start == 0) {
1085
      if (current == nullptr)
1086
        current = start;
1087
      counter++;
1088
    } else {
1089
      if (counter > longest) {
1090
        longest = counter;
1091
        result = current;
1092
      }
1093
      counter = 0;
1094
      current = nullptr;
1095
    }
1096
    start++;
1097
  }
1098
  if (counter > longest)
1099
    result = current;
1100
  return result;
1101
}
1102
1103
919
std::string URLHost::ToStringMove() {
1104
919
  std::string return_value;
1105
919
  switch (type_) {
1106
    case HostType::H_DOMAIN:
1107
    case HostType::H_OPAQUE:
1108
919
      return_value = std::move(value_.domain_or_opaque);
1109
919
      break;
1110
    default:
1111
      return_value = ToString();
1112
      break;
1113
  }
1114
919
  Reset();
1115
919
  return return_value;
1116
}
1117
1118
std::string URLHost::ToString() const {
1119
  std::string dest;
1120
  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
      dest.reserve(15);
1127
      uint32_t value = value_.ipv4;
1128
      for (int n = 0; n < 4; n++) {
1129
        char buf[4];
1130
        snprintf(buf, sizeof(buf), "%d", value % 256);
1131
        dest.insert(0, buf);
1132
        if (n < 3)
1133
          dest.insert(0, 1, '.');
1134
        value /= 256;
1135
      }
1136
      break;
1137
    }
1138
    case HostType::H_IPV6: {
1139
      dest.reserve(41);
1140
      dest += '[';
1141
      const uint16_t* start = &value_.ipv6[0];
1142
      const uint16_t* compress_pointer =
1143
          FindLongestZeroSequence(start, 8);
1144
      bool ignore0 = false;
1145
      for (int n = 0; n <= 7; n++) {
1146
        const uint16_t* piece = &value_.ipv6[n];
1147
        if (ignore0 && *piece == 0)
1148
          continue;
1149
        else if (ignore0)
1150
          ignore0 = false;
1151
        if (compress_pointer == piece) {
1152
          dest += n == 0 ? "::" : ":";
1153
          ignore0 = true;
1154
          continue;
1155
        }
1156
        char buf[5];
1157
        snprintf(buf, sizeof(buf), "%x", *piece);
1158
        dest += buf;
1159
        if (n < 7)
1160
          dest += ':';
1161
      }
1162
      dest += ']';
1163
      break;
1164
    }
1165
    case HostType::H_FAILED:
1166
      break;
1167
  }
1168
  return dest;
1169
}
1170
1171
919
bool ParseHost(const std::string& input,
1172
               std::string* output,
1173
               bool is_special,
1174
               bool unicode = false) {
1175
919
  if (input.empty()) {
1176
    output->clear();
1177
    return true;
1178
  }
1179
1838
  URLHost host;
1180
919
  host.ParseHost(input.c_str(), input.length(), is_special, unicode);
1181
919
  if (host.ParsingFailed())
1182
    return false;
1183
919
  *output = host.ToStringMove();
1184
919
  return true;
1185
}
1186
1187
76
std::vector<std::string> FromJSStringArray(Environment* env,
1188
                                           Local<Array> array) {
1189
76
  std::vector<std::string> vec;
1190
76
  if (array->Length() > 0)
1191
76
    vec.reserve(array->Length());
1192
520
  for (size_t n = 0; n < array->Length(); n++) {
1193
552
    Local<Value> val = array->Get(env->context(), n).ToLocalChecked();
1194
368
    if (val->IsString()) {
1195
368
      Utf8Value value(env->isolate(), val.As<String>());
1196
184
      vec.emplace_back(*value, value.length());
1197
    }
1198
  }
1199
76
  return vec;
1200
}
1201
1202
76
url_data HarvestBase(Environment* env, Local<Object> base_obj) {
1203
76
  url_data base;
1204
76
  Local<Context> context = env->context();
1205
1206
  Local<Value> flags =
1207
304
      base_obj->Get(env->context(), env->flags_string()).ToLocalChecked();
1208
76
  if (flags->IsInt32())
1209
152
    base.flags = flags->Int32Value(context).FromJust();
1210
1211
  Local<Value> port =
1212
304
      base_obj->Get(env->context(), env->port_string()).ToLocalChecked();
1213
76
  if (port->IsInt32())
1214
    base.port = port->Int32Value(context).FromJust();
1215
1216
  Local<Value> scheme =
1217
304
      base_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
1218
76
  base.scheme = Utf8Value(env->isolate(), scheme).out();
1219
1220
  auto GetStr = [&](std::string url_data::*member,
1221
                    int flag,
1222
                    Local<String> name,
1223
380
                    bool empty_as_present) {
1224
1368
    Local<Value> value = base_obj->Get(env->context(), name).ToLocalChecked();
1225
760
    if (value->IsString()) {
1226
456
      Utf8Value utf8value(env->isolate(), value.As<String>());
1227
304
      (base.*member).assign(*utf8value, utf8value.length());
1228

532
      if (empty_as_present || value.As<String>()->Length() != 0) {
1229
76
        base.flags |= flag;
1230
      }
1231
    }
1232
456
  };
1233
76
  GetStr(&url_data::username,
1234
         URL_FLAGS_HAS_USERNAME,
1235
         env->username_string(),
1236
76
         false);
1237
76
  GetStr(&url_data::password,
1238
         URL_FLAGS_HAS_PASSWORD,
1239
         env->password_string(),
1240
76
         false);
1241
76
  GetStr(&url_data::host, URL_FLAGS_HAS_HOST, env->host_string(), true);
1242
76
  GetStr(&url_data::query, URL_FLAGS_HAS_QUERY, env->query_string(), true);
1243
76
  GetStr(&url_data::fragment,
1244
         URL_FLAGS_HAS_FRAGMENT,
1245
         env->fragment_string(),
1246
76
         true);
1247
1248
  Local<Value>
1249
304
      path = base_obj->Get(env->context(), env->path_string()).ToLocalChecked();
1250
76
  if (path->IsArray()) {
1251
76
    base.flags |= URL_FLAGS_HAS_PATH;
1252
76
    base.path = FromJSStringArray(env, path.As<Array>());
1253
  }
1254
76
  return base;
1255
}
1256
1257
13079
url_data HarvestContext(Environment* env, Local<Object> context_obj) {
1258
13079
  url_data context;
1259
  Local<Value> flags =
1260
52316
      context_obj->Get(env->context(), env->flags_string()).ToLocalChecked();
1261
13079
  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
26158
    context.flags |= flags.As<Int32>()->Value() & kCopyFlagsMask;
1269
  }
1270
  Local<Value> scheme =
1271
52316
      context_obj->Get(env->context(), env->scheme_string()).ToLocalChecked();
1272
26158
  if (scheme->IsString()) {
1273
26158
    Utf8Value value(env->isolate(), scheme);
1274
13079
    context.scheme.assign(*value, value.length());
1275
  }
1276
  Local<Value> port =
1277
52316
      context_obj->Get(env->context(), env->port_string()).ToLocalChecked();
1278
13079
  if (port->IsInt32())
1279
    context.port = port.As<Int32>()->Value();
1280
13079
  if (context.flags & URL_FLAGS_HAS_USERNAME) {
1281
    Local<Value> username =
1282
        context_obj->Get(env->context(),
1283
                         env->username_string()).ToLocalChecked();
1284
    CHECK(username->IsString());
1285
    Utf8Value value(env->isolate(), username);
1286
    context.username.assign(*value, value.length());
1287
  }
1288
13079
  if (context.flags & URL_FLAGS_HAS_PASSWORD) {
1289
    Local<Value> password =
1290
        context_obj->Get(env->context(),
1291
                         env->password_string()).ToLocalChecked();
1292
    CHECK(password->IsString());
1293
    Utf8Value value(env->isolate(), password);
1294
    context.password.assign(*value, value.length());
1295
  }
1296
  Local<Value> host =
1297
26158
      context_obj->Get(env->context(),
1298
65395
                       env->host_string()).ToLocalChecked();
1299
26158
  if (host->IsString()) {
1300
26158
    Utf8Value value(env->isolate(), host);
1301
13079
    context.host.assign(*value, value.length());
1302
  }
1303
13079
  return context;
1304
}
1305
1306
// Single dot segment can be ".", "%2e", or "%2E"
1307
1021588
bool IsSingleDotSegment(const std::string& str) {
1308
1021588
  switch (str.size()) {
1309
    case 1:
1310
130
      return str == ".";
1311
    case 3:
1312
88846
      return str[0] == '%' &&
1313

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

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

116621
      if (str[0] != '.' && str[0] != '%')
1329
116621
        return false;
1330
      return ((str[0] == '.' &&
1331
               str[1] == '%' &&
1332
               str[2] == '2' &&
1333
               ASCIILowercase(str[3]) == 'e') ||
1334
              (str[0] == '%' &&
1335
               str[1] == '2' &&
1336
               ASCIILowercase(str[2]) == 'e' &&
1337
               str[3] == '.'));
1338
    case 6:
1339
19156
      return (str[0] == '%' &&
1340
              str[1] == '2' &&
1341
              ASCIILowercase(str[2]) == 'e' &&
1342
              str[3] == '%' &&
1343

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


82
  if (url->path.size() == 1 && url->scheme == "file:" &&
1353
      IsNormalizedWindowsDriveLetter(url->path[0])) return;
1354
82
  url->path.pop_back();
1355
}
1356
1357
}  // anonymous namespace
1358
1359
65470
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
65470
  const char* p = input;
1367
65470
  const char* end = input + len;
1368
1369
65470
  if (!has_url) {
1370
39441
    for (const char* ptr = p; ptr < end; ptr++) {
1371
39440
      if (IsC0ControlOrSpace(*ptr))
1372
        p++;
1373
      else
1374
39440
        break;
1375
    }
1376
39441
    for (const char* ptr = end - 1; ptr >= p; ptr--) {
1377
39440
      if (IsC0ControlOrSpace(*ptr))
1378
        end--;
1379
      else
1380
39440
        break;
1381
    }
1382
39441
    input = p;
1383
39441
    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
130933
  std::string whitespace_stripped;
1390
5270659
  for (const char* ptr = p; ptr < end; ptr++) {
1391
5205189
    if (!IsASCIITabOrNewline(*ptr))
1392
5205189
      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
    whitespace_stripped.reserve(len - 1);
1396
    whitespace_stripped.assign(p, ptr - p);
1397
    // 'ptr + 1' skips the current char, which we know to be tab or newline.
1398
    for (ptr = ptr + 1; ptr < end; ptr++) {
1399
      if (!IsASCIITabOrNewline(*ptr))
1400
        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
    input = whitespace_stripped.c_str();
1406
    len = whitespace_stripped.size();
1407
    p = input;
1408
    end = input + len;
1409
    break;
1410
  }
1411
1412
65470
  bool atflag = false;  // Set when @ has been seen.
1413
65470
  bool square_bracket_flag = false;  // Set inside of [...]
1414
65470
  bool password_token_seen_flag = false;  // Set after a : after an username.
1415
1416
130933
  std::string buffer;
1417
1418
  // Set the initial parse state.
1419
65470
  const bool has_state_override = state_override != kUnknownState;
1420
65470
  enum url_parse_state state = has_state_override ? state_override :
1421
65470
                                                    kSchemeStart;
1422
1423

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

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

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





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


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

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

39352
          if (has_state_override && buffer.size() == 0) {
1452
            url->flags |= URL_FLAGS_TERMINATED;
1453
            return;
1454
          }
1455
39352
          buffer += ':';
1456
1457
39352
          bool new_is_special = IsSpecial(buffer);
1458
1459
39352
          if (has_state_override) {
1460
            if ((special != new_is_special) ||
1461
                ((buffer == "file:") &&
1462
                 ((url->flags & URL_FLAGS_HAS_USERNAME) ||
1463
                  (url->flags & URL_FLAGS_HAS_PASSWORD) ||
1464
                  (url->port != -1))) ||
1465
                  (url->scheme == "file:" && url->host.empty())) {
1466
              url->flags |= URL_FLAGS_TERMINATED;
1467
              return;
1468
            }
1469
          }
1470
1471
39352
          url->scheme = std::move(buffer);
1472
39352
          url->port = NormalizePort(url->scheme, url->port);
1473
39352
          if (new_is_special) {
1474
39351
            url->flags |= URL_FLAGS_SPECIAL;
1475
39351
            special = true;
1476
          } else {
1477
1
            url->flags &= ~URL_FLAGS_SPECIAL;
1478
1
            special = false;
1479
          }
1480

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

1835
          } else if (special &&
1487

918
                     has_base &&
1488
                     url->scheme == base->scheme) {
1489
            state = kSpecialRelativeOrAuthority;
1490
918
          } else if (special) {
1491
917
            state = kSpecialAuthoritySlashes;
1492

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

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

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

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

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

12
          special_back_slash = (special && ch == '\\');
1550
12
          continue;
1551
        }
1552
        break;
1553
      case kSpecialRelativeOrAuthority:
1554
        if (ch == '/' && p + 1 < end && p[1] == '/') {
1555
          state = kSpecialAuthorityIgnoreSlashes;
1556
          p++;
1557
        } else {
1558
          state = kRelative;
1559
          continue;
1560
        }
1561
        break;
1562
      case kPathOrAuthority:
1563
        if (ch == '/') {
1564
          state = kAuthority;
1565
        } else {
1566
          state = kPath;
1567
          continue;
1568
        }
1569
        break;
1570
      case kRelative:
1571
71
        url->scheme = base->scheme;
1572
71
        if (IsSpecial(url->scheme)) {
1573
71
          url->flags |= URL_FLAGS_SPECIAL;
1574
71
          special = true;
1575
        } else {
1576
          url->flags &= ~URL_FLAGS_SPECIAL;
1577
          special = false;
1578
        }
1579

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

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


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

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

920
        if (ch != '/' && ch != '\\') {
1711
919
          state = kAuthority;
1712
919
          continue;
1713
        }
1714
1
        break;
1715
      case kAuthority:
1716
17062
        if (ch == '@') {
1717
          if (atflag) {
1718
            buffer.reserve(buffer.size() + 3);
1719
            buffer.insert(0, "%40");
1720
          }
1721
          atflag = true;
1722
          size_t blen = buffer.size();
1723
          if (blen > 0 && buffer[0] != ':') {
1724
            url->flags |= URL_FLAGS_HAS_USERNAME;
1725
          }
1726
          for (size_t n = 0; n < blen; n++) {
1727
            const char bch = buffer[n];
1728
            if (bch == ':') {
1729
              url->flags |= URL_FLAGS_HAS_PASSWORD;
1730
              if (!password_token_seen_flag) {
1731
                password_token_seen_flag = true;
1732
                continue;
1733
              }
1734
            }
1735
            if (password_token_seen_flag) {
1736
              AppendOrEscape(&url->password, bch, USERINFO_ENCODE_SET);
1737
            } else {
1738
              AppendOrEscape(&url->username, bch, USERINFO_ENCODE_SET);
1739
            }
1740
          }
1741
          buffer.clear();
1742

17062
        } else if (ch == kEOL ||
1743
16143
                   ch == '/' ||
1744
16143
                   ch == '?' ||
1745
16143
                   ch == '#' ||
1746
                   special_back_slash) {
1747

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

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

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

17054
        } else if (ch == kEOL ||
1779
16137
                   ch == '/' ||
1780
16137
                   ch == '?' ||
1781
16137
                   ch == '#' ||
1782
                   special_back_slash) {
1783
917
          p--;
1784

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

916
          if (has_state_override &&
1789

916
              buffer.size() == 0 &&
1790
              ((url->username.size() > 0 || url->password.size() > 0) ||
1791
               url->port != -1)) {
1792
            url->flags |= URL_FLAGS_TERMINATED;
1793
            return;
1794
          }
1795
916
          url->flags |= URL_FLAGS_HAS_HOST;
1796
916
          if (!ParseHost(buffer, &url->host, special)) {
1797
            url->flags |= URL_FLAGS_FAILED;
1798
            return;
1799
          }
1800
916
          buffer.clear();
1801
916
          state = kPathStart;
1802
1832
          if (has_state_override) {
1803
            return;
1804
          }
1805
        } else {
1806
16137
          if (ch == '[')
1807
            square_bracket_flag = true;
1808
16137
          if (ch == ']')
1809
            square_bracket_flag = false;
1810
16137
          buffer += ch;
1811
        }
1812
17055
        break;
1813
      case kPort:
1814
6
        if (IsASCIIDigit(ch)) {
1815
4
          buffer += ch;
1816

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

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

38446
        if (ch == '/' || ch == '\\') {
1863
38434
          state = kFileSlash;
1864

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

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

38434
        if (ch == '/' || ch == '\\') {
1931
38434
          state = kFileHost;
1932
        } else {
1933
          if (has_base && base->scheme == "file:") {
1934
            url->flags |= URL_FLAGS_HAS_HOST;
1935
            url->host = base->host;
1936
            if (!StartsWithWindowsDriveLetter(p, end) &&
1937
                IsNormalizedWindowsDriveLetter(base->path[0])) {
1938
              url->flags |= URL_FLAGS_HAS_PATH;
1939
              url->path.push_back(base->path[0]);
1940
            }
1941
          }
1942
          state = kPath;
1943
          continue;
1944
        }
1945
38434
        break;
1946
      case kFileHost:
1947

38438
        if (ch == kEOL ||
1948
4
            ch == '/' ||
1949
4
            ch == '\\' ||
1950
4
            ch == '?' ||
1951
            ch == '#') {
1952

115302
          if (!has_state_override &&
1953

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

65257
          if (ch != '/' && ch != '\\') {
1986
25907
            continue;
1987
          }
1988
        } else if (!has_state_override && ch == '?') {
1989
          url->flags |= URL_FLAGS_HAS_QUERY;
1990
          url->query.clear();
1991
          state = kQuery;
1992
        } else if (!has_state_override && ch == '#') {
1993
          url->flags |= URL_FLAGS_HAS_FRAGMENT;
1994
          url->fragment.clear();
1995
          state = kFragment;
1996
        } else if (ch != kEOL) {
1997
          state = kPath;
1998
          if (ch != '/') {
1999
            continue;
2000
          }
2001
        }
2002
39350
        break;
2003
      case kPath:
2004

4937965
        if (ch == kEOL ||
2005
4427140
            ch == '/' ||
2006
4427140
            special_back_slash ||
2007

5875397
            (!has_state_override && (ch == '?' || ch == '#'))) {
2008
510832
          if (IsDoubleDotSegment(buffer)) {
2009
3
            ShortenUrlPath(url);
2010

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

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

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

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

562142
                buffer.size() == 2 &&
2022
                IsWindowsDriveLetter(buffer)) {
2023
              buffer[1] = ':';
2024
            }
2025
510753
            url->flags |= URL_FLAGS_HAS_PATH;
2026
510753
            url->path.emplace_back(std::move(buffer));
2027
          }
2028
510832
          buffer.clear();
2029
1021664
          if (ch == '?') {
2030
7
            url->flags |= URL_FLAGS_HAS_QUERY;
2031
7
            url->query.clear();
2032
7
            state = kQuery;
2033
510825
          } else if (ch == '#') {
2034
            url->flags |= URL_FLAGS_HAS_FRAGMENT;
2035
            url->fragment.clear();
2036
            state = kFragment;
2037
          }
2038
        } else {
2039
4427133
          AppendOrEscape(&buffer, ch, PATH_ENCODE_SET);
2040
        }
2041
4937965
        break;
2042
      case kCannotBeBase:
2043
1
        switch (ch) {
2044
          case '?':
2045
            state = kQuery;
2046
            break;
2047
          case '#':
2048
            state = kFragment;
2049
            break;
2050
          default:
2051
1
            if (url->path.empty())
2052
              url->path.emplace_back("");
2053
1
            else if (ch != kEOL)
2054
              AppendOrEscape(&url->path[0], ch, C0_CONTROL_ENCODE_SET);
2055
        }
2056
1
        break;
2057
      case kQuery:
2058

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

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

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

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

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

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


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


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

171552
  CHECK(args[5]->IsUndefined() || args[5]->IsFunction());  // error callback
2225
2226
79084
  Utf8Value input(env->isolate(), args[0]);
2227
39542
  enum url_parse_state state_override = kUnknownState;
2228
79084
  if (args[1]->IsNumber()) {
2229
39542
    state_override = static_cast<enum url_parse_state>(
2230
197710
        args[1]->Uint32Value(env->context()).FromJust());
2231
  }
2232
2233
79084
  Parse(env, args.This(),
2234
39542
        *input, input.length(),
2235
        state_override,
2236
        args[2],
2237
        args[3],
2238
79084
        args[4].As<Function>(),
2239
39542
        args[5]);
2240
39542
}
2241
2242
void EncodeAuthSet(const FunctionCallbackInfo<Value>& args) {
2243
  Environment* env = Environment::GetCurrent(args);
2244
  CHECK_GE(args.Length(), 1);
2245
  CHECK(args[0]->IsString());
2246
  Utf8Value value(env->isolate(), args[0]);
2247
  std::string output;
2248
  size_t len = value.length();
2249
  output.reserve(len);
2250
  for (size_t n = 0; n < len; n++) {
2251
    const char ch = (*value)[n];
2252
    AppendOrEscape(&output, ch, USERINFO_ENCODE_SET);
2253
  }
2254
  args.GetReturnValue().Set(
2255
      String::NewFromUtf8(env->isolate(), output.c_str()).ToLocalChecked());
2256
}
2257
2258
void ToUSVString(const FunctionCallbackInfo<Value>& args) {
2259
  Environment* env = Environment::GetCurrent(args);
2260
  CHECK_GE(args.Length(), 2);
2261
  CHECK(args[0]->IsString());
2262
  CHECK(args[1]->IsNumber());
2263
2264
  TwoByteValue value(env->isolate(), args[0]);
2265
2266
  int64_t start = args[1]->IntegerValue(env->context()).FromJust();
2267
  CHECK_GE(start, 0);
2268
2269
  for (size_t i = start; i < value.length(); i++) {
2270
    char16_t c = value[i];
2271
    if (!IsUnicodeSurrogate(c)) {
2272
      continue;
2273
    } else if (IsUnicodeSurrogateTrail(c) || i == value.length() - 1) {
2274
      value[i] = kUnicodeReplacementCharacter;
2275
    } else {
2276
      char16_t d = value[i + 1];
2277
      if (IsUnicodeTrail(d)) {
2278
        i++;
2279
      } else {
2280
        value[i] = kUnicodeReplacementCharacter;
2281
      }
2282
    }
2283
  }
2284
2285
  args.GetReturnValue().Set(
2286
      String::NewFromTwoByte(env->isolate(),
2287
                             *value,
2288
                             NewStringType::kNormal,
2289
                             value.length()).ToLocalChecked());
2290
}
2291
2292
void DomainToASCII(const FunctionCallbackInfo<Value>& args) {
2293
  Environment* env = Environment::GetCurrent(args);
2294
  CHECK_GE(args.Length(), 1);
2295
  CHECK(args[0]->IsString());
2296
  Utf8Value value(env->isolate(), args[0]);
2297
2298
  URLHost host;
2299
  // Assuming the host is used for a special scheme.
2300
  host.ParseHost(*value, value.length(), true);
2301
  if (host.ParsingFailed()) {
2302
    args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(env->isolate(), ""));
2303
    return;
2304
  }
2305
  std::string out = host.ToStringMove();
2306
  args.GetReturnValue().Set(
2307
      String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
2308
}
2309
2310
void DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
2311
  Environment* env = Environment::GetCurrent(args);
2312
  CHECK_GE(args.Length(), 1);
2313
  CHECK(args[0]->IsString());
2314
  Utf8Value value(env->isolate(), args[0]);
2315
2316
  URLHost host;
2317
  // Assuming the host is used for a special scheme.
2318
  host.ParseHost(*value, value.length(), true, true);
2319
  if (host.ParsingFailed()) {
2320
    args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(env->isolate(), ""));
2321
    return;
2322
  }
2323
  std::string out = host.ToStringMove();
2324
  args.GetReturnValue().Set(
2325
      String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
2326
}
2327
2328
52
void SetURLConstructor(const FunctionCallbackInfo<Value>& args) {
2329
52
  Environment* env = Environment::GetCurrent(args);
2330
52
  CHECK_EQ(args.Length(), 1);
2331
104
  CHECK(args[0]->IsFunction());
2332
104
  env->set_url_constructor_function(args[0].As<Function>());
2333
52
}
2334
2335
52
void Initialize(Local<Object> target,
2336
                Local<Value> unused,
2337
                Local<Context> context,
2338
                void* priv) {
2339
52
  Environment* env = Environment::GetCurrent(context);
2340
52
  env->SetMethod(target, "parse", Parse);
2341
52
  env->SetMethodNoSideEffect(target, "encodeAuth", EncodeAuthSet);
2342
52
  env->SetMethodNoSideEffect(target, "toUSVString", ToUSVString);
2343
52
  env->SetMethodNoSideEffect(target, "domainToASCII", DomainToASCII);
2344
52
  env->SetMethodNoSideEffect(target, "domainToUnicode", DomainToUnicode);
2345
52
  env->SetMethod(target, "setURLConstructor", SetURLConstructor);
2346
2347
#define XX(name, _) NODE_DEFINE_CONSTANT(target, name);
2348
1560
  FLAGS(XX)
2349
#undef XX
2350
2351
#define XX(name) NODE_DEFINE_CONSTANT(target, name);
2352
3120
  PARSESTATES(XX)
2353
208
#undef XX
2354
156
}
2355
208
}  // namespace
2356
312
2357
263
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
2358
263
  registry->Register(Parse);
2359
211
  registry->Register(EncodeAuthSet);
2360
315
  registry->Register(ToUSVString);
2361
263
  registry->Register(DomainToASCII);
2362
263
  registry->Register(DomainToUnicode);
2363
263
  registry->Register(SetURLConstructor);
2364
315
}
2365
312
2366
268
std::string URL::ToFilePath() const {
2367
164
  if (context_.scheme != "file:") {
2368
105
    return "";
2369
208
  }
2370
208
2371
156
#ifdef _WIN32
2372
208
  const char* slash = "\\";
2373
260
  auto is_slash = [] (char ch) {
2374
208
    return ch == '/' || ch == '\\';
2375
208
  };
2376
104
#else
2377
163
  const char* slash = "/";
2378
254
  auto is_slash = [] (char ch) {
2379
208
    return ch == '/';
2380
150
  };
2381

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

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