Repository navigation
HTTP client hangs trying to read a response body of 204 No Content #25181
Description
Activity
- addedbugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behavior
on Sep 7, 2025 After further looking into it, it seems that
std.http.MethodfunctionsrequestHasBodyandresponseHasBodyare both flawed and should be deleted.RFC-2616 section 4.3 says that:
The presence of a message-body in a request is signaled by the
inclusion of a Content-Length or Transfer-Encoding header field in
the request's message-headers. A message-body MUST NOT be included in
a request if the specification of the request method (section 5.1.1)
does not allow sending an entity-body in requests. A server SHOULD
read and forward a message-body on any request; if the request method
does not include defined semantics for an entity-body, then the
message-body SHOULD be ignored when handling the request.But no request method section actually disallows sending a message-body. Stack Overflow seems to agree that message-body is allowed in any request method (even if the semantics are not always well defined), so
requestHasBodyshould always returntrue.And as already mentioned in the issue,
responseHasBodycannot be answered purely based on the used method - this depends on the method AND on the response status code. Existence of this method is misleading.Actually the logic for skipping the response body already exists, it just does nothing -
r.response_content_length = head.content_length;just reassignsnulland ther.response_content_lengthis not read in the relevant path -head.content_lengthis.Adding
head.content_length = 0;just before that line (and changingresponsetovarto allow that change) does fix the issue.However, it would still be good to delete the
HasBodyfunctions - their existence implies that there might be more bugs in the client and/or the server.- addedstandard libraryThis issue involves writing Zig code for the standard library.This issue involves writing Zig code for the standard library.
on Sep 7, 2025 - added a commit that references this issue
on Sep 16, 2025
Zig Version
0.16.0-dev.195+ac42eaaad
Steps to Reproduce and Observed Behavior
The HTTP Client will try to read/discard response body even if no body is expected to appear, which will hang forever/until the server closes the connection. Which can take a while with
Connection: keep-alive.In this example, when
keep_aliveisfalse, everything will work as expected, because while Client will attempt to errorniously discard the body, the server will close the connection and no hang will occur.But with
keep_alivetrue, the connection will stay open, with no traffic. Because the response code is204 No Content, the response will not even containContent-Length: 0header. The Client will still assume that the response body is everything left in the stream (despite response headers containing HTTP 1.1 and Connection: keep-alive). It will then try to read the body forever and hang here.Expected Behavior
zig.http.Client(probably functionbodyReaderin particular) should respect HTTP 1.1 specification on when the response contains the body.