mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
std.http.Client: discard response body when reusing connection
When an error response was encountered, such as 404 not found, the body wasn't discarded, leading to the string "404 not found" being incorrectly interpreted as the next request's response. closes #24732
This commit is contained in:
parent
b734d03340
commit
89c9282d34
@ -787,6 +787,13 @@ pub const Request = struct {
|
|||||||
/// Standard headers that have default, but overridable, behavior.
|
/// Standard headers that have default, but overridable, behavior.
|
||||||
headers: Headers,
|
headers: Headers,
|
||||||
|
|
||||||
|
/// Populated in `receiveHead`; used in `deinit` to determine whether to
|
||||||
|
/// discard the body to reuse the connection.
|
||||||
|
response_content_length: ?u64 = null,
|
||||||
|
/// Populated in `receiveHead`; used in `deinit` to determine whether to
|
||||||
|
/// discard the body to reuse the connection.
|
||||||
|
response_transfer_encoding: http.TransferEncoding = .none,
|
||||||
|
|
||||||
/// These headers are kept including when following a redirect to a
|
/// These headers are kept including when following a redirect to a
|
||||||
/// different domain.
|
/// different domain.
|
||||||
/// Externally-owned; must outlive the Request.
|
/// Externally-owned; must outlive the Request.
|
||||||
@ -860,7 +867,15 @@ pub const Request = struct {
|
|||||||
if (r.connection) |connection| {
|
if (r.connection) |connection| {
|
||||||
connection.closing = connection.closing or switch (r.reader.state) {
|
connection.closing = connection.closing or switch (r.reader.state) {
|
||||||
.ready => false,
|
.ready => false,
|
||||||
.received_head => r.method.requestHasBody(),
|
.received_head => c: {
|
||||||
|
if (r.method.requestHasBody()) break :c true;
|
||||||
|
if (!r.method.responseHasBody()) break :c false;
|
||||||
|
const reader = r.reader.bodyReader(&.{}, r.response_transfer_encoding, r.response_content_length);
|
||||||
|
_ = reader.discardRemaining() catch |err| switch (err) {
|
||||||
|
error.ReadFailed => break :c true,
|
||||||
|
};
|
||||||
|
break :c r.reader.state != .ready;
|
||||||
|
},
|
||||||
else => true,
|
else => true,
|
||||||
};
|
};
|
||||||
r.client.connection_pool.release(connection);
|
r.client.connection_pool.release(connection);
|
||||||
@ -1102,6 +1117,8 @@ pub const Request = struct {
|
|||||||
|
|
||||||
if (head.status == .@"continue") {
|
if (head.status == .@"continue") {
|
||||||
if (r.handle_continue) continue;
|
if (r.handle_continue) continue;
|
||||||
|
r.response_transfer_encoding = head.transfer_encoding;
|
||||||
|
r.response_content_length = head.content_length;
|
||||||
return response; // we're not handling the 100-continue
|
return response; // we're not handling the 100-continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1113,6 +1130,8 @@ pub const Request = struct {
|
|||||||
if (r.method == .CONNECT and head.status.class() == .success) {
|
if (r.method == .CONNECT and head.status.class() == .success) {
|
||||||
// This connection is no longer doing HTTP.
|
// This connection is no longer doing HTTP.
|
||||||
connection.closing = false;
|
connection.closing = false;
|
||||||
|
r.response_transfer_encoding = head.transfer_encoding;
|
||||||
|
r.response_content_length = head.content_length;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1126,6 +1145,8 @@ pub const Request = struct {
|
|||||||
if (r.method == .HEAD or head.status.class() == .informational or
|
if (r.method == .HEAD or head.status.class() == .informational or
|
||||||
head.status == .no_content or head.status == .not_modified)
|
head.status == .no_content or head.status == .not_modified)
|
||||||
{
|
{
|
||||||
|
r.response_transfer_encoding = head.transfer_encoding;
|
||||||
|
r.response_content_length = head.content_length;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1146,6 +1167,8 @@ pub const Request = struct {
|
|||||||
if (!r.accept_encoding[@intFromEnum(head.content_encoding)])
|
if (!r.accept_encoding[@intFromEnum(head.content_encoding)])
|
||||||
return error.HttpContentEncodingUnsupported;
|
return error.HttpContentEncodingUnsupported;
|
||||||
|
|
||||||
|
r.response_transfer_encoding = head.transfer_encoding;
|
||||||
|
r.response_content_length = head.content_length;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user