zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 533049fdd80a4c7bc3098512b4033a60daea745e (tree)
parent 6513eb4696551a91e322fe2d9879335cd73c92db
Author: Nameless <truemedian@gmail.com>
Date:   Fri, 28 Apr 2023 09:55:23 -0500

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

Diffstat:
Mlib/std/http/Server.zig | 16+++++++++++-----
Mtest/standalone/http.zig | 2+-
2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/std/http/Server.zig b/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(); diff --git a/test/standalone/http.zig b/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,