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
signaturelock-open 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 {...@@ -355,17 +355,19 @@ pub const Response = struct {
355 }355 }
356 }356 }
357357
358 pub const ResetState = enum { reset, closing };
359
358 /// Reset this response to its initial state. This must be called before handling a second request on the same connection.360 /// 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 {
360 if (res.state == .first) {362 if (res.state == .first) {
361 res.state = .start;363 res.state = .start;
362 return true;364 return .reset;
363 }365 }
364366
365 if (!res.request.parser.done) {367 if (!res.request.parser.done) {
366 // If the response wasn't fully read, then we need to close the connection.368 // If the response wasn't fully read, then we need to close the connection.
367 res.connection.conn.closing = true;369 res.connection.conn.closing = true;
368 return false;370 return .closing;
369 }371 }
370372
371 // A connection is only keep-alive if the Connection header is present and it's value is not "close".373 // 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 {...@@ -408,7 +410,11 @@ pub const Response = struct {
408 .parser = res.request.parser,410 .parser = res.request.parser,
409 };411 };
410412
411 return !res.connection.conn.closing;413 if (res.connection.conn.closing) {
414 return .closing;
415 } else {
416 return .reset;
417 }
412 }418 }
413419
414 pub const DoError = BufferedConnection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };420 pub const DoError = BufferedConnection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
...@@ -699,7 +705,7 @@ pub const HeaderStrategy = union(enum) {...@@ -699,7 +705,7 @@ pub const HeaderStrategy = union(enum) {
699 static: []u8,705 static: []u8,
700};706};
701707
702/// Accept a new connection and allocate a Response for it.708/// Accept a new connection.
703pub fn accept(server: *Server, options: HeaderStrategy) AcceptError!Response {709pub fn accept(server: *Server, options: HeaderStrategy) AcceptError!Response {
704 const in = try server.socket.accept();710 const in = try server.socket.accept();
705711
test/standalone/http.zig+1-1
...@@ -122,7 +122,7 @@ fn runServer(srv: *Server) !void {...@@ -122,7 +122,7 @@ fn runServer(srv: *Server) !void {
122 var res = try srv.accept(.{ .dynamic = max_header_size });122 var res = try srv.accept(.{ .dynamic = max_header_size });
123 defer res.deinit();123 defer res.deinit();
124124
125 while (res.reset()) {125 while (res.reset() != .closing) {
126 res.wait() catch |err| switch (err) {126 res.wait() catch |err| switch (err) {
127 error.HttpHeadersInvalid => continue :outer,127 error.HttpHeadersInvalid => continue :outer,
128 error.EndOfStream => continue,128 error.EndOfStream => continue,