Skip to content

Commit 95bd7b2

Browse files
committed
http: fix error return in Finish()
`http_parser_execute(..., nullptr, 0)` returns either `0` or `1`. The expectation is that no error must be returned if it is `0`, and if it is `1` - a `Error` object must be returned back to user. The introduction of `llhttp` and the refactor that happened during it accidentally removed the error-returning code. This commit reverts it back to its original state. Fix: #24585
1 parent 7b8058a commit 95bd7b2

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/node_http_parser.cc

+28-4
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,10 @@ class Parser : public AsyncWrap, public StreamListener {
491491
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());
492492

493493
CHECK(parser->current_buffer_.IsEmpty());
494-
parser->Execute(nullptr, 0);
494+
Local<Value> ret = parser->Execute(nullptr, 0);
495+
496+
if (!ret.IsEmpty())
497+
args.GetReturnValue().Set(ret);
495498
}
496499

497500

@@ -684,11 +687,28 @@ class Parser : public AsyncWrap, public StreamListener {
684687
}
685688
#else /* !NODE_EXPERIMENTAL_HTTP */
686689
size_t nread = http_parser_execute(&parser_, &settings, data, len);
687-
if (data != nullptr) {
690+
err = HTTP_PARSER_ERRNO(&parser_);
691+
692+
// Finish()
693+
if (data == nullptr) {
694+
// `http_parser_execute()` returns either `0` or `1` when `len` is 0
695+
// (part of the finishing sequence).
696+
CHECK_EQ(len, 0);
697+
switch (nread) {
698+
case 0:
699+
err = HPE_OK;
700+
break;
701+
case 1:
702+
nread = 0;
703+
break;
704+
default:
705+
UNREACHABLE();
706+
}
707+
708+
// Regular Execute()
709+
} else {
688710
Save();
689711
}
690-
691-
err = HTTP_PARSER_ERRNO(&parser_);
692712
#endif /* NODE_EXPERIMENTAL_HTTP */
693713

694714
// Unassign the 'buffer_' variable
@@ -738,6 +758,10 @@ class Parser : public AsyncWrap, public StreamListener {
738758
return scope.Escape(e);
739759
}
740760

761+
// No return value is needed for `Finish()`
762+
if (data == nullptr) {
763+
return scope.Escape(Local<Value>());
764+
}
741765
return scope.Escape(nread_obj);
742766
}
743767

0 commit comments

Comments
 (0)