Handle basic HTTP errors
This commit is contained in:
parent
5e796d3b3d
commit
f5af54e5d1
1 changed files with 16 additions and 1 deletions
|
@ -54,7 +54,7 @@ fn handleConnection(conn: Connection) void {
|
|||
var request_buf: [request_buf_size]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&request_buf);
|
||||
|
||||
const request = try parseRequest(fba.allocator(), conn.stream.reader());
|
||||
const request = parseRequest(fba.allocator(), conn.stream.reader()) catch |err| handleError(conn.stream.writer(), err);
|
||||
_ = request;
|
||||
}
|
||||
|
||||
|
@ -195,6 +195,21 @@ fn parseEncoding(encoding: ?[]const u8) !Encoding {
|
|||
return error.UnsupportedMediaType;
|
||||
}
|
||||
|
||||
fn handleError(writer: anytype, err: anyerror) !void {
|
||||
const status: http.Status = switch (err) {
|
||||
error.BadRequest => .bad_request,
|
||||
error.MethodNotImplemented => .method_not_implemented,
|
||||
error.UnknownProtocol => .unknown_protocol,
|
||||
error.UnsupportedMediaType => .unsupported_media_type,
|
||||
error.RequestEntityTooLarge => .request_entity_too_large,
|
||||
error.HttpVersionNotSupported => .http_version_not_supported,
|
||||
|
||||
else => err,
|
||||
};
|
||||
|
||||
try writer.print("HTTP/1.1 {} {}\r\n\r\n", .{ @enumToInt(status), status.phrase() });
|
||||
}
|
||||
|
||||
const _test = struct {
|
||||
const expectEqual = std.testing.expectEqual;
|
||||
const expectEqualStrings = std.testing.expectEqualStrings;
|
||||
|
|
Loading…
Reference in a new issue