authorgravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-04-28 09:55:23-05:00
committergravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-05-06 21:35:15-05:00
log533049fdd80a4c7bc3098512b4033a60daea745e
treeb9cfaedc88cf6931cfb6e19297370f98cbed7505
parent6513eb4696551a91e322fe2d9879335cd73c92db
signature Commit is signed but in an unrecognized format.

std.http.Server: use enum for reset state instead of bool


2 files changed, 12 insertions(+), 6 deletions(-)

lib/std/http/Server.zig+11-5
......@@ -355,17 +355,19 @@ pub const Response = struct {
355355 }
356356 }
357357
358 pub const ResetState = enum { reset, closing };
359
358360 /// Reset this response to its initial state. This must be called before handling a second request on the same connection.
359 pub fn reset(res: *Response) bool {
361 pub fn reset(res: *Response) ResetState {
360362 if (res.state == .first) {
361363 res.state = .start;
362 return true;
364 return .reset;
363365 }
364366
365367 if (!res.request.parser.done) {
366368 // If the response wasn't fully read, then we need to close the connection.
367369 res.connection.conn.closing = true;
368 return false;
370 return .closing;
369371 }
370372
371373 // A connection is only keep-alive if the Connection header is present and it's value is not "close".
......@@ -408,7 +410,11 @@ pub const Response = struct {
408410 .parser = res.request.parser,
409411 };
410412
411 return !res.connection.conn.closing;
413 if (res.connection.conn.closing) {
414 return .closing;
415 } else {
416 return .reset;
417 }
412418 }
413419
414420 pub const DoError = BufferedConnection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
......@@ -699,7 +705,7 @@ pub const HeaderStrategy = union(enum) {
699705 static: []u8,
700706};
701707
702/// Accept a new connection and allocate a Response for it.
708/// Accept a new connection.
703709pub fn accept(server: *Server, options: HeaderStrategy) AcceptError!Response {
704710 const in = try server.socket.accept();
705711
test/standalone/http.zig+1-1
......@@ -122,7 +122,7 @@ fn runServer(srv: *Server) !void {
122122 var res = try srv.accept(.{ .dynamic = max_header_size });
123123 defer res.deinit();
124124
125 while (res.reset()) {
125 while (res.reset() != .closing) {
126126 res.wait() catch |err| switch (err) {
127127 error.HttpHeadersInvalid => continue :outer,
128128 error.EndOfStream => continue,