GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_http_parser.cc Lines: 435 462 94.2 %
Date: 2021-09-20 04:12:42 Branches: 188 246 76.4 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "node.h"
23
#include "node_buffer.h"
24
#include "util.h"
25
26
#include "async_wrap-inl.h"
27
#include "env-inl.h"
28
#include "memory_tracker-inl.h"
29
#include "stream_base-inl.h"
30
#include "v8.h"
31
#include "llhttp.h"
32
33
#include <cstdlib>  // free()
34
#include <cstring>  // strdup(), strchr()
35
36
37
// This is a binding to llhttp (https://github.com/nodejs/llhttp)
38
// The goal is to decouple sockets from parsing for more javascript-level
39
// agility. A Buffer is read from a socket and passed to parser.execute().
40
// The parser then issues callbacks with slices of the data
41
//     parser.onMessageBegin
42
//     parser.onPath
43
//     parser.onBody
44
//     ...
45
// No copying is performed when slicing the buffer, only small reference
46
// allocations.
47
48
49
namespace node {
50
namespace {  // NOLINT(build/namespaces)
51
52
using v8::Array;
53
using v8::Boolean;
54
using v8::Context;
55
using v8::EscapableHandleScope;
56
using v8::Exception;
57
using v8::Function;
58
using v8::FunctionCallbackInfo;
59
using v8::FunctionTemplate;
60
using v8::HandleScope;
61
using v8::Int32;
62
using v8::Integer;
63
using v8::Local;
64
using v8::MaybeLocal;
65
using v8::Number;
66
using v8::Object;
67
using v8::String;
68
using v8::Uint32;
69
using v8::Undefined;
70
using v8::Value;
71
72
const uint32_t kOnMessageBegin = 0;
73
const uint32_t kOnHeaders = 1;
74
const uint32_t kOnHeadersComplete = 2;
75
const uint32_t kOnBody = 3;
76
const uint32_t kOnMessageComplete = 4;
77
const uint32_t kOnExecute = 5;
78
const uint32_t kOnTimeout = 6;
79
// Any more fields than this will be flushed into JS
80
const size_t kMaxHeaderFieldsCount = 32;
81
82
const uint32_t kLenientNone = 0;
83
const uint32_t kLenientHeaders = 1 << 0;
84
const uint32_t kLenientChunkedLength = 1 << 1;
85
const uint32_t kLenientKeepAlive = 1 << 2;
86
const uint32_t kLenientAll = kLenientHeaders | kLenientChunkedLength |
87
  kLenientKeepAlive;
88
89
16770
inline bool IsOWS(char c) {
90

16770
  return c == ' ' || c == '\t';
91
}
92
93
class BindingData : public BaseObject {
94
 public:
95
798
  BindingData(Environment* env, Local<Object> obj)
96
798
      : BaseObject(env, obj) {}
97
98
  static constexpr FastStringKey type_name { "http_parser" };
99
100
  std::vector<char> parser_buffer;
101
  bool parser_buffer_in_use = false;
102
103
3
  void MemoryInfo(MemoryTracker* tracker) const override {
104
3
    tracker->TrackField("parser_buffer", parser_buffer);
105
3
  }
106
3
  SET_SELF_SIZE(BindingData)
107
3
  SET_MEMORY_INFO_NAME(BindingData)
108
};
109
110
// TODO(addaleax): Remove once we're on C++17.
111
constexpr FastStringKey BindingData::type_name;
112
113
// helper class for the Parser
114
struct StringPtr {
115
239118
  StringPtr() {
116
239118
    on_heap_ = false;
117
239118
    Reset();
118
239118
  }
119
120
121
472164
  ~StringPtr() {
122
236082
    Reset();
123
236082
  }
124
125
126
  // If str_ does not point to a heap string yet, this function makes it do
127
  // so. This is called at the end of each http_parser_execute() so as not
128
  // to leak references. See issue #2438 and test-http-parser-bad-ref.js.
129
14656
  void Save() {
130

14656
    if (!on_heap_ && size_ > 0) {
131
6029
      char* s = new char[size_];
132
6029
      memcpy(s, str_, size_);
133
6029
      str_ = s;
134
6029
      on_heap_ = true;
135
    }
136
14656
  }
137
138
139
555218
  void Reset() {
140
555218
    if (on_heap_) {
141
5982
      delete[] str_;
142
5982
      on_heap_ = false;
143
    }
144
145
555218
    str_ = nullptr;
146
555218
    size_ = 0;
147
555218
  }
148
149
150
50733
  void Update(const char* str, size_t size) {
151
50733
    if (str_ == nullptr) {
152
50667
      str_ = str;
153

66
    } else if (on_heap_ || str_ + size_ != str) {
154
      // Non-consecutive input, make a copy on the heap.
155
      // TODO(bnoordhuis) Use slab allocation, O(n) allocs is bad.
156
66
      char* s = new char[size_ + size];
157
66
      memcpy(s, str_, size_);
158
66
      memcpy(s + size_, str, size);
159
160
66
      if (on_heap_)
161
65
        delete[] str_;
162
      else
163
1
        on_heap_ = true;
164
165
66
      str_ = s;
166
    }
167
50733
    size_ += size;
168
50733
  }
169
170
171
50572
  Local<String> ToString(Environment* env) const {
172
50572
    if (size_ != 0)
173
50508
      return OneByteString(env->isolate(), str_, size_);
174
    else
175
128
      return String::Empty(env->isolate());
176
  }
177
178
179
  // Strip trailing OWS (SPC or HTAB) from string.
180
16772
  Local<String> ToTrimmedString(Environment* env) {
181

16772
    while (size_ > 0 && IsOWS(str_[size_ - 1])) {
182
11
      size_--;
183
    }
184
16761
    return ToString(env);
185
  }
186
187
188
  const char* str_;
189
  bool on_heap_;
190
  size_t size_;
191
};
192
193
class Parser : public AsyncWrap, public StreamListener {
194
 public:
195
3623
  Parser(BindingData* binding_data, Local<Object> wrap)
196
3623
      : AsyncWrap(binding_data->env(), wrap),
197
        current_buffer_len_(0),
198
        current_buffer_data_(nullptr),
199

235495
        binding_data_(binding_data) {
200
3623
  }
201
202
203
  void MemoryInfo(MemoryTracker* tracker) const override {
204
    tracker->TrackField("current_buffer", current_buffer_);
205
  }
206
207
  SET_MEMORY_INFO_NAME(Parser)
208
  SET_SELF_SIZE(Parser)
209
210
17040
  int on_message_begin() {
211
17040
    num_fields_ = num_values_ = 0;
212
17040
    url_.Reset();
213
17040
    status_message_.Reset();
214
17040
    header_parsing_start_time_ = uv_hrtime();
215
216
34080
    Local<Value> cb = object()->Get(env()->context(), kOnMessageBegin)
217
17040
                              .ToLocalChecked();
218
17040
    if (cb->IsFunction()) {
219
      InternalCallbackScope callback_scope(
220
27534
        this, InternalCallbackScope::kSkipTaskQueues);
221
222
13767
      MaybeLocal<Value> r = cb.As<Function>()->Call(
223
27534
        env()->context(), object(), 0, nullptr);
224
225
13767
      if (r.IsEmpty()) callback_scope.MarkAsFailed();
226
    }
227
228
17040
    return 0;
229
  }
230
231
232
13931
  int on_url(const char* at, size_t length) {
233
13931
    int rv = TrackHeader(length);
234
13931
    if (rv != 0) {
235
      return rv;
236
    }
237
238
13931
    url_.Update(at, length);
239
13931
    return 0;
240
  }
241
242
243
3101
  int on_status(const char* at, size_t length) {
244
3101
    int rv = TrackHeader(length);
245
3101
    if (rv != 0) {
246
      return rv;
247
    }
248
249
3101
    status_message_.Update(at, length);
250
3101
    return 0;
251
  }
252
253
254
16863
  int on_header_field(const char* at, size_t length) {
255
16863
    int rv = TrackHeader(length);
256
16863
    if (rv != 0) {
257
      return rv;
258
    }
259
260
16863
    if (num_fields_ == num_values_) {
261
      // start of new field name
262
16834
      num_fields_++;
263
16834
      if (num_fields_ == kMaxHeaderFieldsCount) {
264
        // ran out of space - flush to javascript land
265
47
        Flush();
266
47
        num_fields_ = 1;
267
47
        num_values_ = 0;
268
      }
269
16834
      fields_[num_fields_ - 1].Reset();
270
    }
271
272
16863
    CHECK_LT(num_fields_, kMaxHeaderFieldsCount);
273
16863
    CHECK_EQ(num_fields_, num_values_ + 1);
274
275
16863
    fields_[num_fields_ - 1].Update(at, length);
276
277
16863
    return 0;
278
  }
279
280
281
16855
  int on_header_value(const char* at, size_t length) {
282
16855
    int rv = TrackHeader(length);
283
16855
    if (rv != 0) {
284
17
      return rv;
285
    }
286
287
16838
    if (num_values_ != num_fields_) {
288
      // start of new header value
289
16809
      num_values_++;
290
16809
      values_[num_values_ - 1].Reset();
291
    }
292
293
16838
    CHECK_LT(num_values_, arraysize(values_));
294
16838
    CHECK_EQ(num_values_, num_fields_);
295
296
16838
    values_[num_values_ - 1].Update(at, length);
297
298
16838
    return 0;
299
  }
300
301
302
16991
  int on_headers_complete() {
303
16991
    header_nread_ = 0;
304
16991
    header_parsing_start_time_ = 0;
305
306
    // Arguments for the on-headers-complete javascript callback. This
307
    // list needs to be kept in sync with the actual argument list for
308
    // `parserOnHeadersComplete` in lib/_http_common.js.
309
    enum on_headers_complete_arg_index {
310
      A_VERSION_MAJOR = 0,
311
      A_VERSION_MINOR,
312
      A_HEADERS,
313
      A_METHOD,
314
      A_URL,
315
      A_STATUS_CODE,
316
      A_STATUS_MESSAGE,
317
      A_UPGRADE,
318
      A_SHOULD_KEEP_ALIVE,
319
      A_MAX
320
    };
321
322
169910
    Local<Value> argv[A_MAX];
323
16991
    Local<Object> obj = object();
324
16991
    Local<Value> cb = obj->Get(env()->context(),
325
33982
                               kOnHeadersComplete).ToLocalChecked();
326
327
16991
    if (!cb->IsFunction())
328
      return 0;
329
330
16991
    Local<Value> undefined = Undefined(env()->isolate());
331
169910
    for (size_t i = 0; i < arraysize(argv); i++)
332
152919
      argv[i] = undefined;
333
334
16991
    if (have_flushed_) {
335
      // Slow case, flush remaining headers.
336
16
      Flush();
337
    } else {
338
      // Fast case, pass headers and URL to JS land.
339
16975
      argv[A_HEADERS] = CreateHeaders();
340
16975
      if (parser_.type == HTTP_REQUEST)
341
27782
        argv[A_URL] = url_.ToString(env());
342
    }
343
344
16991
    num_fields_ = 0;
345
16991
    num_values_ = 0;
346
347
    // METHOD
348
16991
    if (parser_.type == HTTP_REQUEST) {
349
27798
      argv[A_METHOD] =
350
13899
          Uint32::NewFromUnsigned(env()->isolate(), parser_.method);
351
    }
352
353
    // STATUS
354
16991
    if (parser_.type == HTTP_RESPONSE) {
355
3092
      argv[A_STATUS_CODE] =
356
3092
          Integer::New(env()->isolate(), parser_.status_code);
357
6184
      argv[A_STATUS_MESSAGE] = status_message_.ToString(env());
358
    }
359
360
    // VERSION
361
16991
    argv[A_VERSION_MAJOR] = Integer::New(env()->isolate(), parser_.http_major);
362
16991
    argv[A_VERSION_MINOR] = Integer::New(env()->isolate(), parser_.http_minor);
363
364
    bool should_keep_alive;
365
16991
    should_keep_alive = llhttp_should_keep_alive(&parser_);
366
367
16991
    argv[A_SHOULD_KEEP_ALIVE] =
368
        Boolean::New(env()->isolate(), should_keep_alive);
369
370
33982
    argv[A_UPGRADE] = Boolean::New(env()->isolate(), parser_.upgrade);
371
372
    MaybeLocal<Value> head_response;
373
    {
374
      InternalCallbackScope callback_scope(
375
33980
          this, InternalCallbackScope::kSkipTaskQueues);
376
16991
      head_response = cb.As<Function>()->Call(
377
33982
          env()->context(), object(), arraysize(argv), argv);
378
16989
      if (head_response.IsEmpty()) callback_scope.MarkAsFailed();
379
    }
380
381
    int64_t val;
382
383
50951
    if (head_response.IsEmpty() || !head_response.ToLocalChecked()
384
33970
                                        ->IntegerValue(env()->context())
385
16981
                                        .To(&val)) {
386
8
      got_exception_ = true;
387
8
      return -1;
388
    }
389
390
16981
    return static_cast<int>(val);
391
  }
392
393
394
11096
  int on_body(const char* at, size_t length) {
395
11096
    EscapableHandleScope scope(env()->isolate());
396
397
11096
    Local<Object> obj = object();
398
22192
    Local<Value> cb = obj->Get(env()->context(), kOnBody).ToLocalChecked();
399
400
11096
    if (!cb->IsFunction())
401
      return 0;
402
403
    // We came from consumed stream
404
11096
    if (current_buffer_.IsEmpty()) {
405
      // Make sure Buffer will be in parent HandleScope
406
522
      current_buffer_ = scope.Escape(Buffer::Copy(
407
          env()->isolate(),
408
          current_buffer_data_,
409
1044
          current_buffer_len_).ToLocalChecked());
410
    }
411
412
    Local<Value> argv[3] = {
413
        current_buffer_,
414
        Integer::NewFromUnsigned(
415
11096
            env()->isolate(), static_cast<uint32_t>(at - current_buffer_data_)),
416
22192
        Integer::NewFromUnsigned(env()->isolate(), length)};
417
418
    MaybeLocal<Value> r = MakeCallback(cb.As<Function>(),
419
11096
                                       arraysize(argv),
420
22192
                                       argv);
421
422
11096
    if (r.IsEmpty()) {
423
      got_exception_ = true;
424
      llhttp_set_error_reason(&parser_, "HPE_JS_EXCEPTION:JS Exception");
425
      return HPE_USER;
426
    }
427
428
11096
    return 0;
429
  }
430
431
432
15784
  int on_message_complete() {
433
31568
    HandleScope scope(env()->isolate());
434
435
15784
    if (num_fields_)
436
4
      Flush();  // Flush trailing HTTP headers.
437
438
15784
    Local<Object> obj = object();
439
15784
    Local<Value> cb = obj->Get(env()->context(),
440
31568
                               kOnMessageComplete).ToLocalChecked();
441
442
15784
    if (!cb->IsFunction())
443
2
      return 0;
444
445
    MaybeLocal<Value> r;
446
    {
447
      InternalCallbackScope callback_scope(
448
31564
          this, InternalCallbackScope::kSkipTaskQueues);
449
31564
      r = cb.As<Function>()->Call(env()->context(), object(), 0, nullptr);
450
15782
      if (r.IsEmpty()) callback_scope.MarkAsFailed();
451
    }
452
453
15782
    if (r.IsEmpty()) {
454
      got_exception_ = true;
455
      return -1;
456
    }
457
458
15782
    return 0;
459
  }
460
461
  // Reset nread for the next chunk
462
10939
  int on_chunk_header() {
463
10939
    header_nread_ = 0;
464
10939
    return 0;
465
  }
466
467
468
  // Reset nread for the next chunk
469
10785
  int on_chunk_complete() {
470
10785
    header_nread_ = 0;
471
10785
    return 0;
472
  }
473
474
3623
  static void New(const FunctionCallbackInfo<Value>& args) {
475
3623
    BindingData* binding_data = Environment::GetBindingData<BindingData>(args);
476
3623
    new Parser(binding_data, args.This());
477
3623
  }
478
479
480
3
  static void Close(const FunctionCallbackInfo<Value>& args) {
481
    Parser* parser;
482
3
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
483
484
3
    delete parser;
485
  }
486
487
488
5917
  static void Free(const FunctionCallbackInfo<Value>& args) {
489
    Parser* parser;
490
5917
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
491
492
    // Since the Parser destructor isn't going to run the destroy() callbacks
493
    // it needs to be triggered manually.
494
5917
    parser->EmitTraceEventDestroy();
495
5917
    parser->EmitDestroy();
496
  }
497
498
499
7147
  void Save() {
500
7147
    url_.Save();
501
7147
    status_message_.Save();
502
503
7359
    for (size_t i = 0; i < num_fields_; i++) {
504
212
      fields_[i].Save();
505
    }
506
507
7297
    for (size_t i = 0; i < num_values_; i++) {
508
150
      values_[i].Save();
509
    }
510
7147
  }
511
512
513
  // var bytesParsed = parser->execute(buffer);
514
3168
  static void Execute(const FunctionCallbackInfo<Value>& args) {
515
    Parser* parser;
516
3168
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
517
3168
    CHECK(parser->current_buffer_.IsEmpty());
518
3168
    CHECK_EQ(parser->current_buffer_len_, 0);
519
3168
    CHECK_NULL(parser->current_buffer_data_);
520
521
3168
    ArrayBufferViewContents<char> buffer(args[0]);
522
523
    // This is a hack to get the current_buffer to the callbacks with the least
524
    // amount of overhead. Nothing else will run while http_parser_execute()
525
    // runs, therefore this pointer can be set and used for the execution.
526
3168
    parser->current_buffer_ = args[0].As<Object>();
527
528
3168
    Local<Value> ret = parser->Execute(buffer.data(), buffer.length());
529
530
3167
    if (!ret.IsEmpty())
531
6328
      args.GetReturnValue().Set(ret);
532
  }
533
534
535
988
  static void Finish(const FunctionCallbackInfo<Value>& args) {
536
    Parser* parser;
537
988
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
538
539
988
    CHECK(parser->current_buffer_.IsEmpty());
540
988
    Local<Value> ret = parser->Execute(nullptr, 0);
541
542
988
    if (!ret.IsEmpty())
543
66
      args.GetReturnValue().Set(ret);
544
  }
545
546
547
6114
  static void Initialize(const FunctionCallbackInfo<Value>& args) {
548
6114
    Environment* env = Environment::GetCurrent(args);
549
550
6114
    uint64_t max_http_header_size = 0;
551
6114
    uint64_t headers_timeout = 0;
552
6114
    uint32_t lenient_flags = kLenientNone;
553
554
6114
    CHECK(args[0]->IsInt32());
555
6114
    CHECK(args[1]->IsObject());
556
557
6114
    if (args.Length() > 2) {
558
4944
      CHECK(args[2]->IsNumber());
559
4944
      max_http_header_size =
560
9888
          static_cast<uint64_t>(args[2].As<Number>()->Value());
561
    }
562
6114
    if (max_http_header_size == 0) {
563
6110
      max_http_header_size = env->options()->max_http_header_size;
564
    }
565
566
6114
    if (args.Length() > 3) {
567
4944
      CHECK(args[3]->IsInt32());
568
9888
      lenient_flags = args[3].As<Int32>()->Value();
569
    }
570
571
6114
    if (args.Length() > 4) {
572
4944
      CHECK(args[4]->IsInt32());
573
9888
      headers_timeout = args[4].As<Int32>()->Value();
574
    }
575
576
    llhttp_type_t type =
577
12228
        static_cast<llhttp_type_t>(args[0].As<Int32>()->Value());
578
579

6114
    CHECK(type == HTTP_REQUEST || type == HTTP_RESPONSE);
580
    Parser* parser;
581
6114
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
582
    // Should always be called from the same context.
583
6114
    CHECK_EQ(env, parser->env());
584
585
6114
    AsyncWrap::ProviderType provider =
586
6114
        (type == HTTP_REQUEST ?
587
            AsyncWrap::PROVIDER_HTTPINCOMINGMESSAGE
588
            : AsyncWrap::PROVIDER_HTTPCLIENTREQUEST);
589
590
6114
    parser->set_provider_type(provider);
591
18342
    parser->AsyncReset(args[1].As<Object>());
592
6114
    parser->Init(type, max_http_header_size, lenient_flags, headers_timeout);
593
  }
594
595
  template <bool should_pause>
596
22
  static void Pause(const FunctionCallbackInfo<Value>& args) {
597
22
    Environment* env = Environment::GetCurrent(args);
598
    Parser* parser;
599
30
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
600
    // Should always be called from the same context.
601
22
    CHECK_EQ(env, parser->env());
602
603
22
    if (parser->execute_depth_) {
604
8
      parser->pending_pause_ = should_pause;
605
8
      return;
606
    }
607
608
    if (should_pause) {
609
8
      llhttp_pause(&parser->parser_);
610
    } else {
611
6
      llhttp_resume(&parser->parser_);
612
    }
613
  }
614
615
616
3248
  static void Consume(const FunctionCallbackInfo<Value>& args) {
617
    Parser* parser;
618
3248
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
619
3248
    CHECK(args[0]->IsObject());
620
6496
    StreamBase* stream = StreamBase::FromObject(args[0].As<Object>());
621
3248
    CHECK_NOT_NULL(stream);
622
3248
    stream->PushStreamListener(parser);
623
  }
624
625
626
3238
  static void Unconsume(const FunctionCallbackInfo<Value>& args) {
627
    Parser* parser;
628
3238
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
629
630
    // Already unconsumed
631
3238
    if (parser->stream_ == nullptr)
632
      return;
633
634
3238
    parser->stream_->RemoveStreamListener(parser);
635
  }
636
637
638
58
  static void GetCurrentBuffer(const FunctionCallbackInfo<Value>& args) {
639
    Parser* parser;
640
58
    ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
641
642
58
    Local<Object> ret = Buffer::Copy(
643
        parser->env(),
644
        parser->current_buffer_data_,
645
116
        parser->current_buffer_len_).ToLocalChecked();
646
647
116
    args.GetReturnValue().Set(ret);
648
  }
649
650
 protected:
651
  static const size_t kAllocBufferSize = 64 * 1024;
652
653
4216
  uv_buf_t OnStreamAlloc(size_t suggested_size) override {
654
    // For most types of streams, OnStreamRead will be immediately after
655
    // OnStreamAlloc, and will consume all data, so using a static buffer for
656
    // reading is more efficient. For other streams, just use Malloc() directly.
657
4216
    if (binding_data_->parser_buffer_in_use)
658
      return uv_buf_init(Malloc(suggested_size), suggested_size);
659
4216
    binding_data_->parser_buffer_in_use = true;
660
661
4216
    if (binding_data_->parser_buffer.empty())
662
341
      binding_data_->parser_buffer.resize(kAllocBufferSize);
663
664
4216
    return uv_buf_init(binding_data_->parser_buffer.data(), kAllocBufferSize);
665
  }
666
667
668
4243
  void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override {
669
4243
    HandleScope scope(env()->isolate());
670
    // Once we’re done here, either indicate that the HTTP parser buffer
671
    // is free for re-use, or free() the data if it didn’t come from there
672
    // in the first place.
673
4239
    auto on_scope_leave = OnScopeLeave([&]() {
674
4239
      if (buf.base == binding_data_->parser_buffer.data())
675
4214
        binding_data_->parser_buffer_in_use = false;
676
      else
677
25
        free(buf.base);
678
4243
    });
679
680
4243
    if (nread < 0) {
681
262
      PassReadErrorToPreviousListener(nread);
682
262
      return;
683
    }
684
685
    // Ignore, empty reads have special meaning in http parser
686
3981
    if (nread == 0)
687
      return;
688
689
3981
    current_buffer_.Clear();
690
3981
    Local<Value> ret = Execute(buf.base, nread);
691
692
    // Exception
693
3980
    if (ret.IsEmpty())
694
5
      return;
695
696
    // check header parsing time
697

3975
    if (header_parsing_start_time_ != 0 && headers_timeout_ != 0) {
698
52
      uint64_t now = uv_hrtime();
699
52
      uint64_t parsing_time = (now - header_parsing_start_time_) / 1000000;
700
701
52
      if (parsing_time > headers_timeout_) {
702
        Local<Value> cb =
703
18
            object()->Get(env()->context(), kOnTimeout).ToLocalChecked();
704
705
6
        if (!cb->IsFunction())
706
          return;
707
708
12
        MakeCallback(cb.As<Function>(), 0, nullptr);
709
710
6
        return;
711
      }
712
    }
713
714
    Local<Value> cb =
715
11907
        object()->Get(env()->context(), kOnExecute).ToLocalChecked();
716
717
3969
    if (!cb->IsFunction())
718
1003
      return;
719
720
    // Hooks for GetCurrentBuffer
721
2966
    current_buffer_len_ = nread;
722
2966
    current_buffer_data_ = buf.base;
723
724
5932
    MakeCallback(cb.As<Function>(), 1, &ret);
725
726
2963
    current_buffer_len_ = 0;
727
2963
    current_buffer_data_ = nullptr;
728
  }
729
730
731
8137
  Local<Value> Execute(const char* data, size_t len) {
732
8137
    EscapableHandleScope scope(env()->isolate());
733
734
8137
    current_buffer_len_ = len;
735
8137
    current_buffer_data_ = data;
736
8137
    got_exception_ = false;
737
738
    llhttp_errno_t err;
739
740
    // Do not allow re-entering `http_parser_execute()`
741
8137
    CHECK_EQ(execute_depth_, 0);
742
743
8137
    execute_depth_++;
744
8137
    if (data == nullptr) {
745
988
      err = llhttp_finish(&parser_);
746
    } else {
747
7149
      err = llhttp_execute(&parser_, data, len);
748
7147
      Save();
749
    }
750
8135
    execute_depth_--;
751
752
    // Calculate bytes read and resume after Upgrade/CONNECT pause
753
8135
    size_t nread = len;
754
8135
    if (err != HPE_OK) {
755
428
      nread = llhttp_get_error_pos(&parser_) - data;
756
757
      // This isn't a real pause, just a way to stop parsing early.
758
428
      if (err == HPE_PAUSED_UPGRADE) {
759
97
        err = HPE_OK;
760
97
        llhttp_resume_after_upgrade(&parser_);
761
      }
762
    }
763
764
    // Apply pending pause
765
8135
    if (pending_pause_) {
766
      pending_pause_ = false;
767
      llhttp_pause(&parser_);
768
    }
769
770
    // Unassign the 'buffer_' variable
771
8135
    current_buffer_.Clear();
772
8135
    current_buffer_len_ = 0;
773
8135
    current_buffer_data_ = nullptr;
774
775
    // If there was an exception in one of the callbacks
776
8135
    if (got_exception_)
777
8
      return scope.Escape(Local<Value>());
778
779
8127
    Local<Integer> nread_obj = Integer::New(env()->isolate(), nread);
780
781
    // If there was a parse error in one of the callbacks
782
    // TODO(bnoordhuis) What if there is an error on EOF?
783

8127
    if (!parser_.upgrade && err != HPE_OK) {
784
226
      Local<Value> e = Exception::Error(env()->parse_error_string());
785
226
      Local<Object> obj = e->ToObject(env()->isolate()->GetCurrentContext())
786
226
        .ToLocalChecked();
787
226
      obj->Set(env()->context(),
788
               env()->bytes_parsed_string(),
789
452
               nread_obj).Check();
790
226
      const char* errno_reason = llhttp_get_error_reason(&parser_);
791
792
      Local<String> code;
793
      Local<String> reason;
794
226
      if (err == HPE_USER) {
795
19
        const char* colon = strchr(errno_reason, ':');
796
19
        CHECK_NOT_NULL(colon);
797
        code = OneByteString(env()->isolate(),
798
                             errno_reason,
799
19
                             static_cast<int>(colon - errno_reason));
800
19
        reason = OneByteString(env()->isolate(), colon + 1);
801
      } else {
802
207
        code = OneByteString(env()->isolate(), llhttp_errno_name(err));
803
207
        reason = OneByteString(env()->isolate(), errno_reason);
804
      }
805
806
678
      obj->Set(env()->context(), env()->code_string(), code).Check();
807
678
      obj->Set(env()->context(), env()->reason_string(), reason).Check();
808
226
      return scope.Escape(e);
809
    }
810
811
    // No return value is needed for `Finish()`
812
7901
    if (data == nullptr) {
813
955
      return scope.Escape(Local<Value>());
814
    }
815
6946
    return scope.Escape(nread_obj);
816
  }
817
818
17042
  Local<Array> CreateHeaders() {
819
    // There could be extra entries but the max size should be fixed
820
1107730
    Local<Value> headers_v[kMaxHeaderFieldsCount * 2];
821
822
33803
    for (size_t i = 0; i < num_values_; ++i) {
823
16761
      headers_v[i * 2] = fields_[i].ToString(env());
824
33522
      headers_v[i * 2 + 1] = values_[i].ToTrimmedString(env());
825
    }
826
827
17042
    return Array::New(env()->isolate(), headers_v, num_values_ * 2);
828
  }
829
830
831
  // spill headers and request path to JS land
832
67
  void Flush() {
833
67
    HandleScope scope(env()->isolate());
834
835
67
    Local<Object> obj = object();
836
134
    Local<Value> cb = obj->Get(env()->context(), kOnHeaders).ToLocalChecked();
837
838
67
    if (!cb->IsFunction())
839
      return;
840
841
    Local<Value> argv[2] = {
842
      CreateHeaders(),
843
      url_.ToString(env())
844
134
    };
845
846
    MaybeLocal<Value> r = MakeCallback(cb.As<Function>(),
847
67
                                       arraysize(argv),
848
134
                                       argv);
849
850
67
    if (r.IsEmpty())
851
      got_exception_ = true;
852
853
67
    url_.Reset();
854
67
    have_flushed_ = true;
855
  }
856
857
858
6114
  void Init(llhttp_type_t type, uint64_t max_http_header_size,
859
            uint32_t lenient_flags, uint64_t headers_timeout) {
860
6114
    llhttp_init(&parser_, type, &settings);
861
862
6114
    if (lenient_flags & kLenientHeaders) {
863
5
      llhttp_set_lenient_headers(&parser_, 1);
864
    }
865
6114
    if (lenient_flags & kLenientChunkedLength) {
866
5
      llhttp_set_lenient_chunked_length(&parser_, 1);
867
    }
868
6114
    if (lenient_flags & kLenientKeepAlive) {
869
5
      llhttp_set_lenient_keep_alive(&parser_, 1);
870
    }
871
872
6114
    header_nread_ = 0;
873
6114
    url_.Reset();
874
6114
    status_message_.Reset();
875
6114
    num_fields_ = 0;
876
6114
    num_values_ = 0;
877
6114
    have_flushed_ = false;
878
6114
    got_exception_ = false;
879
6114
    max_http_header_size_ = max_http_header_size;
880
6114
    header_parsing_start_time_ = 0;
881
6114
    headers_timeout_ = headers_timeout;
882
6114
  }
883
884
885
50750
  int TrackHeader(size_t len) {
886
50750
    header_nread_ += len;
887
50750
    if (header_nread_ >= max_http_header_size_) {
888
17
      llhttp_set_error_reason(&parser_, "HPE_HEADER_OVERFLOW:Header overflow");
889
17
      return HPE_USER;
890
    }
891
50733
    return 0;
892
  }
893
894
895
133252
  int MaybePause() {
896
133252
    CHECK_NE(execute_depth_, 0);
897
898
133252
    if (!pending_pause_) {
899
133252
      return 0;
900
    }
901
902
    pending_pause_ = false;
903
    llhttp_set_error_reason(&parser_, "Paused in callback");
904
    return HPE_PAUSED;
905
  }
906
907
908
  bool IsNotIndicativeOfMemoryLeakAtExit() const override {
909
    // HTTP parsers are able to emit events without any GC root referring
910
    // to them, because they receive events directly from the underlying
911
    // libuv resource.
912
    return true;
913
  }
914
915
916
  llhttp_t parser_;
917
  StringPtr fields_[kMaxHeaderFieldsCount];  // header fields
918
  StringPtr values_[kMaxHeaderFieldsCount];  // header values
919
  StringPtr url_;
920
  StringPtr status_message_;
921
  size_t num_fields_;
922
  size_t num_values_;
923
  bool have_flushed_;
924
  bool got_exception_;
925
  Local<Object> current_buffer_;
926
  size_t current_buffer_len_;
927
  const char* current_buffer_data_;
928
  unsigned int execute_depth_ = 0;
929
  bool pending_pause_ = false;
930
  uint64_t header_nread_ = 0;
931
  uint64_t max_http_header_size_;
932
  uint64_t headers_timeout_;
933
  uint64_t header_parsing_start_time_ = 0;
934
935
  BaseObjectPtr<BindingData> binding_data_;
936
937
  // These are helper functions for filling `http_parser_settings`, which turn
938
  // a member function of Parser into a C-style HTTP parser callback.
939
  template <typename Parser, Parser> struct Proxy;
940
  template <typename Parser, typename ...Args, int (Parser::*Member)(Args...)>
941
  struct Proxy<int (Parser::*)(Args...), Member> {
942
266770
    static int Raw(llhttp_t* p, Args ... args) {
943
266770
      Parser* parser = ContainerOf(&Parser::parser_, p);
944
266770
      int rv = (parser->*Member)(std::forward<Args>(args)...);
945
266766
      if (rv == 0) {
946
266504
        rv = parser->MaybePause();
947
      }
948
266766
      return rv;
949
    }
950
  };
951
952
  typedef int (Parser::*Call)();
953
  typedef int (Parser::*DataCall)(const char* at, size_t length);
954
955
  static const llhttp_settings_t settings;
956
};
957
958
const llhttp_settings_t Parser::settings = {
959
  Proxy<Call, &Parser::on_message_begin>::Raw,
960
  Proxy<DataCall, &Parser::on_url>::Raw,
961
  Proxy<DataCall, &Parser::on_status>::Raw,
962
  Proxy<DataCall, &Parser::on_header_field>::Raw,
963
  Proxy<DataCall, &Parser::on_header_value>::Raw,
964
  Proxy<Call, &Parser::on_headers_complete>::Raw,
965
  Proxy<DataCall, &Parser::on_body>::Raw,
966
  Proxy<Call, &Parser::on_message_complete>::Raw,
967
  Proxy<Call, &Parser::on_chunk_header>::Raw,
968
  Proxy<Call, &Parser::on_chunk_complete>::Raw,
969
970
  // on_url_complete
971
  nullptr,
972
  // on_status_complete
973
  nullptr,
974
  // on_header_field_complete
975
  nullptr,
976
  // on_header_value_complete
977
  nullptr,
978
};
979
980
981
798
void InitializeHttpParser(Local<Object> target,
982
                          Local<Value> unused,
983
                          Local<Context> context,
984
                          void* priv) {
985
798
  Environment* env = Environment::GetCurrent(context);
986
  BindingData* const binding_data =
987
798
      env->AddBindingData<BindingData>(context, target);
988
798
  if (binding_data == nullptr) return;
989
990
798
  Local<FunctionTemplate> t = env->NewFunctionTemplate(Parser::New);
991
1596
  t->InstanceTemplate()->SetInternalFieldCount(Parser::kInternalFieldCount);
992
993
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "REQUEST"),
994
         Integer::New(env->isolate(), HTTP_REQUEST));
995
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "RESPONSE"),
996
         Integer::New(env->isolate(), HTTP_RESPONSE));
997
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnMessageBegin"),
998
         Integer::NewFromUnsigned(env->isolate(), kOnMessageBegin));
999
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnHeaders"),
1000
         Integer::NewFromUnsigned(env->isolate(), kOnHeaders));
1001
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnHeadersComplete"),
1002
         Integer::NewFromUnsigned(env->isolate(), kOnHeadersComplete));
1003
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnBody"),
1004
         Integer::NewFromUnsigned(env->isolate(), kOnBody));
1005
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnMessageComplete"),
1006
         Integer::NewFromUnsigned(env->isolate(), kOnMessageComplete));
1007
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnExecute"),
1008
         Integer::NewFromUnsigned(env->isolate(), kOnExecute));
1009
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
1010
         Integer::NewFromUnsigned(env->isolate(), kOnTimeout));
1011
1012
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kLenientNone"),
1013
         Integer::NewFromUnsigned(env->isolate(), kLenientNone));
1014
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kLenientHeaders"),
1015
         Integer::NewFromUnsigned(env->isolate(), kLenientHeaders));
1016
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kLenientChunkedLength"),
1017
         Integer::NewFromUnsigned(env->isolate(), kLenientChunkedLength));
1018
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kLenientKeepAlive"),
1019
         Integer::NewFromUnsigned(env->isolate(), kLenientKeepAlive));
1020
2394
  t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kLenientAll"),
1021
         Integer::NewFromUnsigned(env->isolate(), kLenientAll));
1022
1023
798
  Local<Array> methods = Array::New(env->isolate());
1024
#define V(num, name, string)                                                  \
1025
    methods->Set(env->context(),                                              \
1026
        num, FIXED_ONE_BYTE_STRING(env->isolate(), #string)).Check();
1027
55062
  HTTP_METHOD_MAP(V)
1028
#undef V
1029
798
  target->Set(env->context(),
1030
              FIXED_ONE_BYTE_STRING(env->isolate(), "methods"),
1031
2394
              methods).Check();
1032
1033
798
  t->Inherit(AsyncWrap::GetConstructorTemplate(env));
1034
798
  env->SetProtoMethod(t, "close", Parser::Close);
1035
798
  env->SetProtoMethod(t, "free", Parser::Free);
1036
798
  env->SetProtoMethod(t, "execute", Parser::Execute);
1037
798
  env->SetProtoMethod(t, "finish", Parser::Finish);
1038
798
  env->SetProtoMethod(t, "initialize", Parser::Initialize);
1039
798
  env->SetProtoMethod(t, "pause", Parser::Pause<true>);
1040
798
  env->SetProtoMethod(t, "resume", Parser::Pause<false>);
1041
798
  env->SetProtoMethod(t, "consume", Parser::Consume);
1042
798
  env->SetProtoMethod(t, "unconsume", Parser::Unconsume);
1043
798
  env->SetProtoMethod(t, "getCurrentBuffer", Parser::GetCurrentBuffer);
1044
1045
798
  env->SetConstructorFunction(target, "HTTPParser", t);
1046
}
1047
1048
}  // anonymous namespace
1049
}  // namespace node
1050
1051
4926
NODE_MODULE_CONTEXT_AWARE_INTERNAL(http_parser, node::InitializeHttpParser)