Commit ddef683fcb

Nameless <truemedian@gmail.com>
2023-08-23 22:29:50
std.http.Server: responses to HEAD not allowed to have a payload
1 parent 3817c73
Changed files (2)
lib
std
test
standalone
lib/std/http/Server.zig
@@ -458,6 +458,10 @@ pub const Response = struct {
             try w.print("{}", .{res.headers});
         }
 
+        if (res.request.method == .HEAD) {
+            res.transfer_encoding = .none;
+        }
+
         try w.writeAll("\r\n");
 
         try buffered.flush();
@@ -520,10 +524,6 @@ pub const Response = struct {
             res.request.parser.done = true;
         }
 
-        if (res.request.method == .HEAD) {
-            res.request.parser.done = true;
-        }
-
         if (!res.request.parser.done) {
             if (res.request.transfer_compression) |tc| switch (tc) {
                 .compress => return error.CompressionNotSupported,
test/standalone/http.zig
@@ -55,6 +55,8 @@ fn handleRequest(res: *Server.Response) !void {
             try res.writeAll("Hello, ");
             try res.writeAll("World!\n");
             try res.finish();
+        } else {
+            try testing.expectEqual(res.writeAll("errors"), error.NotWriteable);
         }
     } else if (mem.startsWith(u8, res.request.target, "/large")) {
         res.transfer_encoding = .{ .content_length = 14 * 1024 + 14 * 10 };