authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-04-22 16:34:33-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-04-23 15:33:23-07:00
log0488c3cb52a75ffa4fa73b385d3d6e79cc6da774
tree66f847c3dd9f89b5af9b518b2269feecfdbe47c4
parent1acb3162b7e24e6badad4aff37dbeac09f47f165

std.http: Always initialize `response.headers` in Client.request

Before this change, if a request errored before getting its `response.headers` initialized, then it would attempt to `deinit` `response.headers` which would still be `undefined`. Since all locations that set `response.headers` use the same code, it can just be done upfront in `request` instead. Closes #15380

1 files changed, 2 insertions(+), 3 deletions(-)

lib/std/http/Client.zig+2-3
...@@ -645,7 +645,6 @@ pub const Request = struct {...@@ -645,7 +645,6 @@ pub const Request = struct {
645 if (req.response.parser.state.isContent()) break;645 if (req.response.parser.state.isContent()) break;
646 }646 }
647647
648 req.response.headers = http.Headers{ .allocator = req.client.allocator, .owned = false };
649 try req.response.parse(req.response.parser.header_bytes.items);648 try req.response.parse(req.response.parser.header_bytes.items);
650649
651 if (req.response.status == .switching_protocols) {650 if (req.response.status == .switching_protocols) {
...@@ -765,7 +764,7 @@ pub const Request = struct {...@@ -765,7 +764,7 @@ pub const Request = struct {
765 }764 }
766765
767 if (has_trail) {766 if (has_trail) {
768 req.response.headers = http.Headers{ .allocator = req.client.allocator, .owned = false };767 req.response.headers.clearRetainingCapacity();
769768
770 // The response headers before the trailers are already guaranteed to be valid, so they will always be parsed again and cannot return an error.769 // The response headers before the trailers are already guaranteed to be valid, so they will always be parsed again and cannot return an error.
771 // This will *only* fail for a malformed trailer.770 // This will *only* fail for a malformed trailer.
...@@ -1019,7 +1018,7 @@ pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Hea...@@ -1019,7 +1018,7 @@ pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Hea
1019 .status = undefined,1018 .status = undefined,
1020 .reason = undefined,1019 .reason = undefined,
1021 .version = undefined,1020 .version = undefined,
1022 .headers = undefined,1021 .headers = http.Headers{ .allocator = client.allocator, .owned = false },
1023 .parser = switch (options.header_strategy) {1022 .parser = switch (options.header_strategy) {
1024 .dynamic => |max| proto.HeadersParser.initDynamic(max),1023 .dynamic => |max| proto.HeadersParser.initDynamic(max),
1025 .static => |buf| proto.HeadersParser.initStatic(buf),1024 .static => |buf| proto.HeadersParser.initStatic(buf),