web: unnecessary refactor before compilation

This commit is contained in:
2026-03-12 02:47:54 +01:00
parent 9a4932e629
commit fe4a585b6b
19 changed files with 754 additions and 414 deletions

View File

@@ -7,10 +7,11 @@
//!
//! During a single ingestion, the parser can return one of the following:
//!
//! - route of type `Route`, i.e. HTTP method (aka verb) with pathname
//! - method of type `Method`, i.e. HTTP method (aka verb)
//! - pathname of type `[]const u8`
//! - header of type `Header`, i.e. a field name with a value
//! - end_of_headers of type `void`, i.e. a marker which informs the user of
//! this parser that there will be no more headers; this moment can be used by
//! this parser that there will be no more headers; this result can be used by
//! the user to make decisions about further processing of the request based
//! on the full knowledge of all the headers
//! - body of type `[]const u8`, i.e. a slice to the request body (or
@@ -48,7 +49,6 @@ const Parser = @This();
const FieldName = @import("FieldName.zig").FieldName;
const Header = @import("Header.zig");
const Method = @import("Method.zig").Method;
const Route = @import("Route.zig");
pub const Error = error{
MethodNotSupported,
@@ -57,13 +57,18 @@ pub const Error = error{
};
pub const Result = union(enum) {
route: Route,
method: Method,
pathname: []const u8,
header: Header,
end_of_headers: void,
body: []const u8,
pub fn initRoute(route: Route) Result {
return .{ .route = route };
pub fn initMethod(method: Method) Result {
return .{ .method = method };
}
pub fn initPathname(pathname: []const u8) Result {
return .{ .pathname = pathname };
}
pub fn initHeader(header: Header) Result {
@@ -115,8 +120,8 @@ pub const State = union(enum) {
method_optio: void,
method_connec: void,
method_option: void,
method_complete: Method,
pathname: Route,
method_complete: void,
pathname: []const u8,
pathname_complete: void,
version_h: void,
version_ht: void,
@@ -135,12 +140,8 @@ pub const State = union(enum) {
body: []const u8,
done: void,
pub fn initMethodComplete(method: Method) State {
return .{ .method_complete = method };
}
pub fn initPathname(route: Route) State {
return .{ .pathname = route };
pub fn initPathname(pathname: []const u8) State {
return .{ .pathname = pathname };
}
pub fn initHeaderName(name: []const u8) State {
@@ -280,7 +281,10 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_ge => switch (char) {
'T' => self.state = .initMethodComplete(.GET),
'T' => {
self.state = .method_complete;
return .initMethod(.GET);
},
else => return error.MethodNotSupported,
},
.method_he => switch (char) {
@@ -300,7 +304,10 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_pu => switch (char) {
'T' => self.state = .initMethodComplete(.PUT),
'T' => {
self.state = .method_complete;
return .initMethod(.PUT);
},
else => return error.MethodNotSupported,
},
.method_tr => switch (char) {
@@ -316,7 +323,10 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_hea => switch (char) {
'D' => self.state = .initHeaderName(.HEAD),
'D' => {
self.state = .method_complete;
return .initMethod(.HEAD);
},
else => return error.MethodNotSupported,
},
.method_opt => switch (char) {
@@ -328,7 +338,10 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_pos => switch (char) {
'T' => self.state = .initHeaderName(.POST),
'T' => {
self.state = .method_complete;
return .initMethod(.POST);
},
else => return error.MethodNotSupported,
},
.method_tra => switch (char) {
@@ -348,11 +361,17 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_patc => switch (char) {
'H' => self.state = .initMethodComplete(.PATCH),
'H' => {
self.state = .method_complete;
return .initMethod(.PATCH);
},
else => return error.MethodNotSupported,
},
.method_trac => switch (char) {
'E' => self.state = .initMethodComplete(.TRACE),
'E' => {
self.state = .method_complete;
return .initMethod(.TRACE);
},
else => return error.MethodNotSupported,
},
.method_conne => switch (char) {
@@ -360,7 +379,10 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_delet => switch (char) {
'E' => self.state = .initMethodComplete(.DELETE),
'E' => {
self.state = .method_complete;
return .initMethod(.DELETE);
},
else => return error.MethodNotSupported,
},
.method_optio => switch (char) {
@@ -368,23 +390,29 @@ fn consumeChar(self: *Parser, char_ptr: *const u8) Error!?Result {
else => return error.MethodNotSupported,
},
.method_connec => switch (char) {
'T' => self.state = .initMethodComplete(.CONNECT),
'T' => {
self.state = .method_complete;
return .initMethod(.CONNECT);
},
else => return error.MethodNotSupported,
},
.method_option => switch (char) {
'S' => self.state = .initMethodComplete(.OPTIONS),
'S' => {
self.state = .method_complete;
return .initMethod(.OPTIONS);
},
else => return error.MethodNotSupported,
},
.method_complete => |method| switch (char) {
' ' => self.state = .initPathname(.init(method, next_char_slice)),
.method_complete => switch (char) {
' ' => self.state = .initPathname(next_char_slice),
else => return error.MethodNotSupported,
},
.pathname => |route| switch (char) {
.pathname => |pathname| switch (char) {
' ' => {
self.state = .pathname_complete;
return .initRoute(route);
return .initPathname(pathname);
},
else => self.state = .initPathname(extendRoute(route)),
else => self.state = .initPathname(extendSlice(pathname)),
},
.pathname_complete => switch (char) {
'H' => self.state = .version_h,
@@ -503,13 +531,6 @@ fn extendSliceBy(slice: []const u8, n: usize) []const u8 {
return slice.ptr[0 .. slice.len + n];
}
fn extendRoute(route: Route) Route {
return .{
.method = route.method,
.pathname = extendSlice(route.pathname),
};
}
fn extendHeader(header: Header) Header {
return .{
.name = header.name,
@@ -597,10 +618,10 @@ pub fn consumeVec(self: *Parser, vec_ptr: *const [vec_len]u8) Error!ConsumeResul
inline for (@typeInfo(patterns.methods).@"struct".decls) |decl| {
const pattern: Pattern = @field(patterns.methods, decl.name);
if (pattern.check(vec)) {
self.state = .methodComplete(@field(Method, decl.name));
self.state = .method_complete;
return .{
.consumed = pattern.len,
.done = false,
.result = .initMethod(@field(Method, decl.name)),
};
}
}
@@ -612,14 +633,14 @@ pub fn consumeVec(self: *Parser, vec_ptr: *const [vec_len]u8) Error!ConsumeResul
// Delegate to `consumeChar`.
return .{
.consumed = 0,
.done = false,
.result = null,
};
}
self.state = .pathname(s.method, s.pathname.ptr[0 .. s.pathname.len + vec_len]);
return .{
.consumed = vec_len,
.done = false,
.result = null,
};
},
.pathname_complete => {
@@ -627,7 +648,7 @@ pub fn consumeVec(self: *Parser, vec_ptr: *const [vec_len]u8) Error!ConsumeResul
self.state = .header_name_start;
return .{
.consumed = patterns.@"version_http/1.1".len,
.done = false,
.result = null,
};
} else {
return error.HttpVersionNotSupported;
@@ -638,21 +659,21 @@ pub fn consumeVec(self: *Parser, vec_ptr: *const [vec_len]u8) Error!ConsumeResul
// Delegate to `consumeChar`.
return .{
.consumed = 0,
.done = false,
.result = null,
};
}
self.state = .headerValue(s.name, s.value.ptr[0 .. s.value.len + vec_len]);
return .{
.consumed = vec_len,
.done = false,
.result = null,
};
},
else => {
// Delegate to `consumeChar`.
return .{
.consumed = 0,
.done = false,
.result = null,
};
},
}