authorgravatar for nwtgck@nwtgck.orgRyo Ota <nwtgck@nwtgck.org> 2023-04-21 10:17:16+09:00
committergravatar for nwtgck@nwtgck.orgRyo Ota <nwtgck@nwtgck.org> 2023-04-21 10:17:16+09:00
logafebef2465a828132f87cd2aeff0c5873ca10de5
tree05fd7b6ea576ed19cc6a6686ae3ae622a7600eb3
parent06763c4c8c35d2e984ee2315e674a01d5506ded1

create std.http.Server.Response.deinit to handle keepalive connections


2 files changed, 5 insertions(+), 2 deletions(-)

lib/std/http/Server.zig+4-2
...@@ -336,6 +336,10 @@ pub const Response = struct {...@@ -336,6 +336,10 @@ pub const Response = struct {
336 headers: http.Headers,336 headers: http.Headers,
337 request: Request,337 request: Request,
338338
339 pub fn deinit(res: *Response) void {
340 res.server.allocator.destroy(res);
341 }
342
339 /// Reset this response to its initial state. This must be called before handling a second request on the same connection.343 /// Reset this response to its initial state. This must be called before handling a second request on the same connection.
340 pub fn reset(res: *Response) void {344 pub fn reset(res: *Response) void {
341 res.request.headers.deinit();345 res.request.headers.deinit();
...@@ -359,8 +363,6 @@ pub const Response = struct {...@@ -359,8 +363,6 @@ pub const Response = struct {
359 if (res.request.parser.header_bytes_owned) {363 if (res.request.parser.header_bytes_owned) {
360 res.request.parser.header_bytes.deinit(res.server.allocator);364 res.request.parser.header_bytes.deinit(res.server.allocator);
361 }365 }
362
363 res.server.allocator.destroy(res);
364 } else {366 } else {
365 res.request.parser.reset();367 res.request.parser.reset();
366 }368 }
lib/std/http/test.zig+1
...@@ -30,6 +30,7 @@ test "client requests server" {...@@ -30,6 +30,7 @@ test "client requests server" {
30 const server_thread = try std.Thread.spawn(.{}, (struct {30 const server_thread = try std.Thread.spawn(.{}, (struct {
31 fn apply(s: *std.http.Server) !void {31 fn apply(s: *std.http.Server) !void {
32 const res = try s.accept(.{ .dynamic = max_header_size });32 const res = try s.accept(.{ .dynamic = max_header_size });
33 defer res.deinit();
33 defer res.reset();34 defer res.reset();
34 try res.wait();35 try res.wait();
3536