Skip to content

Commit a8e46e0

Browse files
committed
http: simplify parser
1 parent c03b8e0 commit a8e46e0

File tree

1 file changed

+7
-68
lines changed

1 file changed

+7
-68
lines changed

src/node_http_parser.cc

Lines changed: 7 additions & 68 deletions
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,14 +458,11 @@ 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(Buffer::Copy(
463+
env()->isolate(),
464+
current_buffer_data_,
465+
current_buffer_len_).ToLocalChecked());
473466

474467
Local<Value> argv[3] = {
475468
current_buffer_,
@@ -594,45 +587,8 @@ class Parser : public AsyncWrap, public StreamListener {
594587
Parser* parser;
595588
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
596589

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-
629590
ArrayBufferViewContents<char> buffer(args[0]);
630591

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-
636592
Local<Value> ret = parser->Execute(buffer.data(), buffer.length());
637593

638594
if (!ret.IsEmpty())
@@ -644,7 +600,6 @@ class Parser : public AsyncWrap, public StreamListener {
644600
Parser* parser;
645601
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
646602

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

650605
if (!ret.IsEmpty())
@@ -724,11 +679,6 @@ class Parser : public AsyncWrap, public StreamListener {
724679
// Should always be called from the same context.
725680
CHECK_EQ(env, parser->env());
726681

727-
if (parser->execute_depth_) {
728-
parser->pending_pause_ = should_pause;
729-
return;
730-
}
731-
732682
if (should_pause) {
733683
llhttp_pause(&parser->parser_);
734684
} else {
@@ -830,7 +780,6 @@ class Parser : public AsyncWrap, public StreamListener {
830780
if (nread == 0)
831781
return;
832782

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

836785
// Exception
@@ -856,24 +805,19 @@ class Parser : public AsyncWrap, public StreamListener {
856805

857806
Local<Value> Execute(const char* data, size_t len) {
858807
EscapableHandleScope scope(env()->isolate());
859-
808+
860809
current_buffer_len_ = len;
861810
current_buffer_data_ = data;
862811
got_exception_ = false;
863812

864813
llhttp_errno_t err;
865814

866-
// Do not allow re-entering `http_parser_execute()`
867-
CHECK_EQ(execute_depth_, 0);
868-
869-
execute_depth_++;
870815
if (data == nullptr) {
871816
err = llhttp_finish(&parser_);
872817
} else {
873818
err = llhttp_execute(&parser_, data, len);
874819
Save();
875820
}
876-
execute_depth_--;
877821

878822
// Calculate bytes read and resume after Upgrade/CONNECT pause
879823
size_t nread = len;
@@ -893,8 +837,6 @@ class Parser : public AsyncWrap, public StreamListener {
893837
llhttp_pause(&parser_);
894838
}
895839

896-
// Unassign the 'buffer_' variable
897-
current_buffer_.Clear();
898840
current_buffer_len_ = 0;
899841
current_buffer_data_ = nullptr;
900842

@@ -1018,8 +960,6 @@ class Parser : public AsyncWrap, public StreamListener {
1018960

1019961

1020962
int MaybePause() {
1021-
CHECK_NE(execute_depth_, 0);
1022-
1023963
if (!pending_pause_) {
1024964
return 0;
1025965
}
@@ -1047,7 +987,6 @@ class Parser : public AsyncWrap, public StreamListener {
1047987
size_t num_values_;
1048988
bool have_flushed_;
1049989
bool got_exception_;
1050-
Local<Object> current_buffer_;
1051990
size_t current_buffer_len_;
1052991
const char* current_buffer_data_;
1053992
unsigned int execute_depth_ = 0;

0 commit comments

Comments
 (0)