52 lines
1.3 KiB
Zig
52 lines
1.3 KiB
Zig
const std = @import("std");
|
|
const Connection = std.net.Server.Connection;
|
|
const Map = std.static_string_map.StaticStringMap;
|
|
|
|
pub const Method = enum {
|
|
GET,
|
|
|
|
pub fn init(method: [] const u8) !Method {
|
|
return MethodMap.get(method).?;
|
|
}
|
|
|
|
pub fn is_supported(method: [] const u8) bool {
|
|
const m = MethodMap.get(method);
|
|
|
|
if (m) |_| {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const MethodMap = Map(Method).initComptime(.{
|
|
.{"GET", Method.GET},
|
|
});
|
|
|
|
pub const Request = struct {
|
|
method: Method,
|
|
version: [] const u8,
|
|
uri: [] const u8,
|
|
|
|
pub fn init(method: Method, version: [] const u8, uri: [] const u8) Request {
|
|
return Request{.method = method, .version = version, .uri = uri};
|
|
}
|
|
};
|
|
|
|
pub fn read_request(connection: Connection, buffer: []u8) !void {
|
|
const reader = connection.stream.reader();
|
|
_ = try reader.read(buffer);
|
|
}
|
|
|
|
pub fn parse_request(text: [] u8) Request {
|
|
const line_index = std.mem.indexOfScalar(u8, text, '\n') orelse text.len;
|
|
var iterator = std.mem.splitScalar(u8, text[0..line_index], ' ');
|
|
const method = try Method.init(iterator.next().?);
|
|
const uri = iterator.next().?;
|
|
const version = iterator.next().?;
|
|
const request = Request.init(method, version, uri);
|
|
|
|
return request;
|
|
}
|