# HG changeset patch # User Kim Alvefur # Date 1771596483 -3600 # Node ID 79a6598cef0b60f0bf297e7290dab3dd01f46dcf # Parent dfbc60d47668151ceb3494eea08975f331ab8527 net.http.parser: Fix handling of chunked request How to determine whether a request/response has a body depends on different things for requests and responses, which was not reflected in the code. Test case based on curl http://localhost:5280/debug -H "Transfer-Encoding: chunked" --json '{"json":true}' Thanks isafashiondev diff -r dfbc60d47668 -r 79a6598cef0b net/http/parser.lua --- a/net/http/parser.lua Fri Feb 13 14:54:57 2026 +0100 +++ b/net/http/parser.lua Fri Feb 20 15:08:03 2026 +0100 @@ -93,9 +93,12 @@ end end if not first_line then error = true; return error_cb("invalid-status-line"); end - chunked = have_body and headers["transfer-encoding"] == "chunked"; len = tonumber(headers["content-length"]); -- TODO check for invalid len if client then + -- The presence of a message body in a response, as detailed in Section 6.3, depends on both the request method to which it is responding + -- and the response status code. + -- -- RFC9112 Section 6 + chunked = have_body and headers["transfer-encoding"] == "chunked"; -- FIXME handle '100 Continue' response (by skipping it) if not have_body then len = 0; end packet = { @@ -111,6 +114,11 @@ responseheaders = headers; }; else + -- The presence of a message body in a request is signaled by a Content-Length or Transfer-Encoding header field. Request message framing + -- is independent of method semantics. + -- -- RFC9112 Section 6 + chunked = headers["transfer-encoding"] == "chunked"; + have_body = chunked or len; local parsed_url; if path:byte() == 47 then -- starts with / local _path, _query = path:match("([^?]*).?(.*)"); diff -r dfbc60d47668 -r 79a6598cef0b spec/net_http_parser_spec.lua --- a/spec/net_http_parser_spec.lua Fri Feb 13 14:54:57 2026 +0100 +++ b/spec/net_http_parser_spec.lua Fri Feb 20 15:08:03 2026 +0100 @@ -120,6 +120,11 @@ body = "Hello", count = 3; } ); + + test_stream( + "POST /debug HTTP/1.1\r\nHost: localhost:5280\r\nUser-Agent: curl/8.14.1\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n" + .."Accept: application/json\r\n\r\nd\r\n{\"json\":true}\r\n0\r\n\r\n", + { count = 2; body = "{\"json\":true}" }) end); it("should handle a stream of responses", function ()