Skip to content

Commit 5c2884b

Browse files
committed
http: simplify parser
1 parent c03b8e0 commit 5c2884b

File tree

1 file changed

+6
-69
lines changed

1 file changed

+6
-69
lines changed

src/node_http_parser.cc

+6-69
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,7 @@ class Parser : public AsyncWrap, public StreamListener {
248248
binding_data_(binding_data) {
249249
}
250250

251-
252-
void MemoryInfo(MemoryTracker* tracker) const override {
253-
tracker->TrackField("current_buffer", current_buffer_);
254-
}
255-
251+
SET_NO_MEMORY_INFO()
256252
SET_MEMORY_INFO_NAME(Parser)
257253
SET_SELF_SIZE(Parser)
258254

@@ -462,19 +458,14 @@ class Parser : public AsyncWrap, public StreamListener {
462458
if (!cb->IsFunction())
463459
return 0;
464460

465-
// We came from consumed stream
466-
if (current_buffer_.IsEmpty()) {
467-
// Make sure Buffer will be in parent HandleScope
468-
current_buffer_ = scope.Escape(Buffer::Copy(
469-
env()->isolate(),
470-
current_buffer_data_,
471-
current_buffer_len_).ToLocalChecked());
472-
}
461+
// Make sure Buffer will be in parent HandleScope
462+
Local<Object> current_buffer = scope.Escape(
463+
Buffer::Copy(env()->isolate(), at, length).ToLocalChecked());
473464

474465
Local<Value> argv[3] = {
475-
current_buffer_,
466+
current_buffer,
476467
Integer::NewFromUnsigned(
477-
env()->isolate(), static_cast<uint32_t>(at - current_buffer_data_)),
468+
env()->isolate(), 0),
478469
Integer::NewFromUnsigned(env()->isolate(), length)};
479470

480471
MaybeLocal<Value> r = MakeCallback(cb.As<Function>(),
@@ -594,45 +585,8 @@ class Parser : public AsyncWrap, public StreamListener {
594585
Parser* parser;
595586
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
596587

597-
// If parser.Execute is invoked within one of the callbacks,
598-
// like kOnHeadersComplete, it is scheduled before the buffer is
599-
// emptied and thus all assertions fails. For this reason we
600-
// postpone the actual execution.
601-
if (!parser->current_buffer_.IsEmpty()) {
602-
ArrayBufferViewContents<char> buffer(args[0]);
603-
604-
Environment::GetCurrent(args)->SetImmediate(
605-
[parser, args, buffer](Environment* env) {
606-
CHECK(parser->current_buffer_.IsEmpty());
607-
CHECK_EQ(parser->current_buffer_len_, 0);
608-
CHECK_NULL(parser->current_buffer_data_);
609-
610-
// This is a hack to get the current_buffer to the callbacks
611-
// with the least amount of overhead. Nothing else will run
612-
// while http_parser_execute() runs, therefore this pointer
613-
// can be set and used for the execution.
614-
parser->current_buffer_ = args[0].As<Object>();
615-
616-
Local<Value> ret = parser->Execute(buffer.data(), buffer.length());
617-
618-
if (!ret.IsEmpty())
619-
args.GetReturnValue().Set(ret);
620-
});
621-
622-
return;
623-
}
624-
625-
CHECK(parser->current_buffer_.IsEmpty());
626-
CHECK_EQ(parser->current_buffer_len_, 0);
627-
CHECK_NULL(parser->current_buffer_data_);
628-
629588
ArrayBufferViewContents<char> buffer(args[0]);
630589

631-
// This is a hack to get the current_buffer to the callbacks with the least
632-
// amount of overhead. Nothing else will run while http_parser_execute()
633-
// runs, therefore this pointer can be set and used for the execution.
634-
parser->current_buffer_ = args[0].As<Object>();
635-
636590
Local<Value> ret = parser->Execute(buffer.data(), buffer.length());
637591

638592
if (!ret.IsEmpty())
@@ -644,7 +598,6 @@ class Parser : public AsyncWrap, public StreamListener {
644598
Parser* parser;
645599
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
646600

647-
CHECK(parser->current_buffer_.IsEmpty());
648601
Local<Value> ret = parser->Execute(nullptr, 0);
649602

650603
if (!ret.IsEmpty())
@@ -724,11 +677,6 @@ class Parser : public AsyncWrap, public StreamListener {
724677
// Should always be called from the same context.
725678
CHECK_EQ(env, parser->env());
726679

727-
if (parser->execute_depth_) {
728-
parser->pending_pause_ = should_pause;
729-
return;
730-
}
731-
732680
if (should_pause) {
733681
llhttp_pause(&parser->parser_);
734682
} else {
@@ -830,7 +778,6 @@ class Parser : public AsyncWrap, public StreamListener {
830778
if (nread == 0)
831779
return;
832780

833-
current_buffer_.Clear();
834781
Local<Value> ret = Execute(buf.base, nread);
835782

836783
// Exception
@@ -863,17 +810,12 @@ class Parser : public AsyncWrap, public StreamListener {
863810

864811
llhttp_errno_t err;
865812

866-
// Do not allow re-entering `http_parser_execute()`
867-
CHECK_EQ(execute_depth_, 0);
868-
869-
execute_depth_++;
870813
if (data == nullptr) {
871814
err = llhttp_finish(&parser_);
872815
} else {
873816
err = llhttp_execute(&parser_, data, len);
874817
Save();
875818
}
876-
execute_depth_--;
877819

878820
// Calculate bytes read and resume after Upgrade/CONNECT pause
879821
size_t nread = len;
@@ -893,8 +835,6 @@ class Parser : public AsyncWrap, public StreamListener {
893835
llhttp_pause(&parser_);
894836
}
895837

896-
// Unassign the 'buffer_' variable
897-
current_buffer_.Clear();
898838
current_buffer_len_ = 0;
899839
current_buffer_data_ = nullptr;
900840

@@ -1018,8 +958,6 @@ class Parser : public AsyncWrap, public StreamListener {
1018958

1019959

1020960
int MaybePause() {
1021-
CHECK_NE(execute_depth_, 0);
1022-
1023961
if (!pending_pause_) {
1024962
return 0;
1025963
}
@@ -1047,7 +985,6 @@ class Parser : public AsyncWrap, public StreamListener {
1047985
size_t num_values_;
1048986
bool have_flushed_;
1049987
bool got_exception_;
1050-
Local<Object> current_buffer_;
1051988
size_t current_buffer_len_;
1052989
const char* current_buffer_data_;
1053990
unsigned int execute_depth_ = 0;

0 commit comments

Comments
 (0)