Commit 533049fdd8

Nameless <truemedian@gmail.com>
2023-04-28 16:55:23
std.http.Server: use enum for reset state instead of bool
1 parent 6513eb4
Changed files (2)
lib
std
test
standalone
lib/std/http/Server.zig
@@ -355,17 +355,19 @@ pub const Response = struct {
         }
     }
 
+    pub const ResetState = enum { reset, closing };
+
     /// Reset this response to its initial state. This must be called before handling a second request on the same connection.
-    pub fn reset(res: *Response) bool {
+    pub fn reset(res: *Response) ResetState {
         if (res.state == .first) {
             res.state = .start;
-            return true;
+            return .reset;
         }
 
         if (!res.request.parser.done) {
             // If the response wasn't fully read, then we need to close the connection.
             res.connection.conn.closing = true;
-            return false;
+            return .closing;
         }
 
         // 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 {
             .parser = res.request.parser,
         };
 
-        return !res.connection.conn.closing;
+        if (res.connection.conn.closing) {
+            return .closing;
+        } else {
+            return .reset;
+        }
     }
 
     pub const DoError = BufferedConnection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
@@ -699,7 +705,7 @@ pub const HeaderStrategy = union(enum) {
     static: []u8,
 };
 
-/// Accept a new connection and allocate a Response for it.
+/// Accept a new connection.
 pub fn accept(server: *Server, options: HeaderStrategy) AcceptError!Response {
     const in = try server.socket.accept();
 
test/standalone/http.zig
@@ -122,7 +122,7 @@ fn runServer(srv: *Server) !void {
         var res = try srv.accept(.{ .dynamic = max_header_size });
         defer res.deinit();
 
-        while (res.reset()) {
+        while (res.reset() != .closing) {
             res.wait() catch |err| switch (err) {
                 error.HttpHeadersInvalid => continue :outer,
                 error.EndOfStream => continue,