Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Accept #100

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn build(b: *std.Build) !void {
.{ .name = "middleware_with_endpoint", .src = "examples/middleware_with_endpoint/middleware_with_endpoint.zig" },
.{ .name = "senderror", .src = "examples/senderror/senderror.zig" },
.{ .name = "bindataformpost", .src = "examples/bindataformpost/bindataformpost.zig" },
.{ .name = "accept", .src = "examples/accept/accept.zig" },
}) |excfg| {
const ex_name = excfg.name;
const ex_src = excfg.src;
Expand Down
72 changes: 72 additions & 0 deletions examples/accept/accept.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const std = @import("std");
const zap = @import("zap");

var gpa = std.heap.GeneralPurposeAllocator(.{
.thread_safe = true,
}){};

fn on_request_verbose(r: zap.Request) void {
const allocator = gpa.allocator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to use a FixedBufferAllocator here? Maybe with a 1kB buffer?

const content_type: zap.ContentType = content_type: {
var accept_list = std.ArrayList(zap.Request.AcceptItem).init(allocator);
defer accept_list.deinit();
r.parseAccept(&accept_list) catch break :content_type .HTML;
for (accept_list.items) |accept| {
break :content_type accept.toContentType() orelse continue;
}
break :content_type .HTML;
};

r.setContentType(content_type) catch return;
switch (content_type) {
.TEXT => {
r.sendBody("Hello from ZAP!!!") catch return;
},
.HTML => {
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
},
.XML => {
r.sendBody(
\\<?xml version="1.0" encoding="UTF-8"?>
\\<message>
\\ <warning>
\\ Hello from ZAP!!!
\\ </warning>
\\</message>
) catch return;
},
.JSON => {
var buffer: [128]u8 = undefined;
const json = zap.stringifyBuf(&buffer, .{ .message = "Hello from ZAP!!!" }, .{}) orelse return;
r.sendJson(json) catch return;
},
.XHTML => {
r.sendBody(
\\<?xml version="1.0" encoding="UTF-8"?>
\\<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
\\ <body>
\\ <h1>Hello from ZAP!!!</h1>
\\ </body>
\\</html>
) catch return;
},
}
}

pub fn main() !void {
var listener = zap.HttpListener.init(.{
.port = 3000,
.on_request = on_request_verbose,
.log = true,
.max_clients = 100000,
});
try listener.listen();

std.debug.print("Listening on 0.0.0.0:3000\n", .{});

// start worker threads
zap.start(.{
.threads = 2,
.workers = 2,
});
}
18 changes: 0 additions & 18 deletions src/http.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const fio = @import("fio.zig");

/// HTTP Status codes according to `rfc9110`
/// https://datatracker.ietf.org/doc/html/rfc9110#name-status-codes
Expand Down Expand Up @@ -149,20 +148,3 @@ pub fn methodToEnum(method: ?[]const u8) Method {
return Method.UNKNOWN;
}
}

/// Registers a new mimetype to be used for files ending with the given extension.
pub fn mimetypeRegister(file_extension: []const u8, mime_type_str: []const u8) void {
// NOTE: facil.io is expecting a non-const pointer to u8 values, but it does not
// not appear to actually modify the value. Here we do a const cast so
// that it is easy to pass static strings to http_mimetype_register without
// needing to allocate a buffer on the heap.
const extension = @constCast(file_extension);
const mimetype = fio.fiobj_str_new(mime_type_str.ptr, mime_type_str.len);

fio.http_mimetype_register(extension.ptr, extension.len, mimetype);
}

/// Clears the Mime-Type registry (it will be empty after this call).
pub fn mimetypeClear() void {
fio.http_mimetype_clear();
}
74 changes: 65 additions & 9 deletions src/request.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const fio = @import("fio.zig");
const util = @import("util.zig");
const zap = @import("zap.zig");

const ContentType = zap.ContentType;

pub const HttpError = error{
HttpSendBody,
HttpSetContentType,
Expand All @@ -16,15 +18,6 @@ pub const HttpError = error{
SendFile,
};

/// Http Content Type enum.
/// Needs some love.
pub const ContentType = enum {
TEXT,
HTML,
JSON,
// TODO: more content types
};

/// Key value pair of strings from HTTP parameters
pub const HttpParamStrKV = struct {
key: util.FreeOrNot,
Expand Down Expand Up @@ -584,6 +577,69 @@ pub fn parseCookies(self: *const Self, url_encoded: bool) void {
fio.http_parse_cookies(self.h, if (url_encoded) 1 else 0);
}

pub const AcceptItem = struct {
raw: []const u8,
type: Fragment,
subtype: Fragment,
q: f64,

const Fragment = union(enum) {
glob,
value: []const u8,
};

pub fn lessThan(_: void, lhs: AcceptItem, rhs: AcceptItem) bool {
return lhs.q < rhs.q;
}

pub fn toContentType(item: AcceptItem) ?ContentType {
if (ContentType.string_map.get(item.raw)) |common| {
return common;
}
return null;
}
};

/// Parses `Accept:` http header into `list`, ordered from highest q factor to lowest
pub fn parseAccept(self: *const Self, list: *std.ArrayList(AcceptItem)) !void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it makes sense to make a type alias for the ArrayList?
So that users can write zap.AcceptList.init() instead of std.ArrayList(zap.AcceptItem).init()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And taking it one step further: instead of passing the ArrayList, let the user just pass an allocator and do the init within parseAccept. I think that would look very zig-like, so I propose:

pub fn parseAccept(self: *const Self, allocator: std.mem.Allocator) !std.ArrayList(AcceptItem)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, regarding naming: What do you think of the more verbose parseAcceptHeaders()?

pub fn parseAcceptHeaders(self: *const Self, allocator: std.mem.Allocator) !std.ArrayList(AcceptItem)

const accept_str = self.getHeaderCommon(.accept) orelse return error.NoAccept;

var tok_iter = std.mem.tokenize(u8, accept_str, ", ");
while (tok_iter.next()) |tok| {
var split_iter = std.mem.split(u8, tok, ";");
const mimetype_str = split_iter.next().?;
const q_factor = q_factor: {
const q_factor_str = split_iter.next() orelse break :q_factor 1;
var eq_iter = std.mem.split(u8, q_factor_str, "=");
const q = eq_iter.next().?;
if (q[0] != 'q') break :q_factor 1;
const value = eq_iter.next() orelse break :q_factor 1;
const parsed = std.fmt.parseFloat(f64, value) catch break :q_factor 1;
break :q_factor parsed;
};

var type_split_iter = std.mem.split(u8, mimetype_str, "/");

const mimetype_type_str = type_split_iter.next() orelse continue;
const mimetype_subtype_str = type_split_iter.next() orelse continue;

const new_item: AcceptItem = .{
.raw = mimetype_str,
.type = if (std.mem.eql(u8, "*", mimetype_type_str)) .glob else .{ .value = mimetype_type_str },
.subtype = if (std.mem.eql(u8, "*", mimetype_subtype_str)) .glob else .{ .value = mimetype_subtype_str },
.q = q_factor,
};
for (list.items, 1..) |item, i| {
if (AcceptItem.lessThan({}, new_item, item)) {
try list.insert(i, new_item);
break;
}
} else {
try list.append(new_item);
}
}
}

/// Set a response cookie
pub fn setCookie(self: *const Self, args: CookieArgs) HttpError!void {
const c: fio.http_cookie_args_s = .{
Expand Down
27 changes: 27 additions & 0 deletions src/zap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ pub fn startWithLogging(args: fio.fio_start_args) void {
fio.fio_start(args);
}

/// Registers a new mimetype to be used for files ending with the given extension.
pub fn mimetypeRegister(file_extension: []const u8, mime_type_str: []const u8) void {
// NOTE: facil.io is expecting a non-const pointer to u8 values, but it does not
// not appear to actually modify the value. Here we do a const cast so
// that it is easy to pass static strings to http_mimetype_register without
// needing to allocate a buffer on the heap.
const extension = @constCast(file_extension);
const mimetype = fio.fiobj_str_new(mime_type_str.ptr, mime_type_str.len);

fio.http_mimetype_register(extension.ptr, extension.len, mimetype);
}

/// Clears the Mime-Type registry (it will be empty after this call).
pub fn mimetypeClear() void {
fio.http_mimetype_clear();
}

pub const ListenError = error{
AlreadyListening,
ListenError,
Expand All @@ -142,8 +159,18 @@ pub const HttpError = error{
pub const ContentType = enum {
TEXT,
HTML,
XML,
JSON,
XHTML,
// TODO: more content types

pub const string_map = std.ComptimeStringMap(ContentType, .{
.{ "text/plain", .TEXT },
.{ "text/html", .HTML },
.{ "application/xml", .XML },
.{ "application/json", .JSON },
.{ "application/xhtml+xml", .XHTML },
});
};

/// Used internally: facilio Http request callback function type
Expand Down
Loading