web: fix compiler errors

This commit is contained in:
2026-03-12 03:07:05 +01:00
parent fe4a585b6b
commit 712e214f61
8 changed files with 48 additions and 36 deletions

View File

@@ -41,8 +41,8 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Worker {
try header_hash_map.ensureTotalCapacity(allocator, options.max_header_fields);
errdefer header_hash_map.deinit(allocator);
const header_list_buffer = try allocator.alloc(Request.HeaderList, options.max_header_fields);
errdefer allocator.free(header_list_buffer);
const header_value_buffer = try allocator.alloc(Request.HeaderValue, options.max_header_fields);
errdefer allocator.free(header_value_buffer);
return .{
.worker_id = options.worker_id,
@@ -56,7 +56,7 @@ pub fn init(allocator: std.mem.Allocator, options: Options) !Worker {
.body_write_buffer = options.body_write_buffer,
.header_hash_map = header_hash_map,
.header_value_buffer = header_list_buffer,
.header_value_buffer = header_value_buffer,
};
}
@@ -126,7 +126,7 @@ fn handleRequest(
.headers = &self.header_hash_map,
.body = undefined,
};
var response: Response = .init(connection, self.write_buffer);
var response: Response = .init(connection, self.header_write_buffer, self.body_write_buffer);
var parser: http.Parser = .init();
var next_header_index: usize = 0;
@@ -152,15 +152,9 @@ fn handleRequest(
const res = parser.consume(chunk) catch |err| {
switch (err) {
error.MethodNotSupported => {
try response.sendClose(.{ .status_text = http.status.method_not_allowed });
},
error.HttpVersionNotSupported => {
try response.sendClose(.{ .status_text = http.status.http_version_not_supported });
},
error.SyntaxError => {
try response.sendClose(.{ .status_text = http.status.bad_request });
},
error.MethodNotSupported => try closeWith(&response, http.status.method_not_allowed),
error.HttpVersionNotSupported => try closeWith(&response, http.status.http_version_not_supported),
error.SyntaxError => try closeWith(&response, http.status.bad_request),
}
return false;
};
@@ -169,9 +163,9 @@ fn handleRequest(
if (self.read_tail - self.read_head >= self.read_buffer_size and !done) {
if (parser.state == .body) {
try response.sendClose(.{ .status_text = http.status.content_too_large });
try closeWith(&response, http.status.content_too_large);
} else {
try response.sendClose(.{ .status_text = http.status.request_header_fields_too_large });
try closeWith(&response, http.status.request_header_fields_too_large);
}
return false;
}
@@ -180,14 +174,19 @@ fn handleRequest(
switch (result) {
.method => |method| request.method = method,
.pathname => |pathname| request.pathname = pathname,
.header => |header| {
.header => |header| blk: {
if (ignore) {
break;
break :blk;
}
if (next_header_index >= self.header_value_buffer.len or self.header_hash_map.available == 0) {
try response.send(.{ .status_text = http.status.request_header_fields_too_large });
ignore = true;
// TODO Here, we could ignore, but make sure this does
// not clash with the other "request too long" checks
// (i.e. be careful not to double respond).
_ = &ignore;
try closeWith(&response, http.status.request_header_fields_too_large);
return false;
} else {
const entry = self.header_hash_map.getOrPutAssumeCapacity(header.name);
const header_value = &self.header_value_buffer[next_header_index];
@@ -267,3 +266,15 @@ fn handleRequest(
leftover_bytes = bytes_read - res.consumed;
}
}
fn closeWith(response: *Response, status_line: []const u8) !void {
// This function is meant to be called before a request handler gets to do
// anything.
std.debug.assert(response.header_writer.end == 0);
std.debug.assert(response.body_writer.end == 0);
std.debug.assert(response.state == .init);
try response.header_writer.writeAll(status_line);
try response.header_writer.writeAll("Connection: close\r\n");
try response.header_writer.writeAll("\r\n");
}