deal with headers

This commit is contained in:
Emil Lerch 2023-05-15 19:49:28 -07:00
parent ce5fc4e99f
commit 271c53f650
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
3 changed files with 38 additions and 29 deletions

View File

@ -85,10 +85,12 @@ pub fn build(b: *std.Build) void {
const run_main_tests = b.addRunArtifact(main_tests);
const run_lib_tests = b.addRunArtifact(lib_tests);
run_main_tests.step.dependOn(b.getInstallStep());
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_main_tests.step);
test_step.dependOn(&run_lib_tests.step);
test_step.dependOn(&run_main_tests.step);
}

View File

@ -30,20 +30,21 @@ export fn handle_request() ?*interface.Response {
}
return null;
};
// Marshall data back for handling by server
var rc = &interface.Response{
.ptr = response.items.ptr,
.len = response.items.len,
.headers = interface.toHeaders(allocator, headers) catch |e| {
log.debug("response ptr: {*}", .{response.items.ptr});
// Marshall data back for handling by server
var rc = allocator.create(interface.Response) catch @panic("OOM");
rc.ptr = response.items.ptr;
rc.len = response.items.len;
rc.headers = interface.toHeaders(allocator, headers) catch |e| {
log.err("Unexpected error processing request: {any}", .{e});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
return null;
},
.headers_len = headers.count(),
};
rc.headers_len = headers.count();
return rc;
}
@ -64,6 +65,7 @@ fn handleRequest(response: Response) !void {
// real work
response_writer.print(" 2.", .{}) catch unreachable;
try response.headers.put("X-custom-foo", "bar");
log.info("handlerequest header count {d}", .{response.headers.count()});
}
test "handle_request" {

View File

@ -68,23 +68,27 @@ fn serve(allocator: std.mem.Allocator, response: *std.http.Server.Response) !*Fu
errdefer executor.in_request_lock = false;
// Call external library
var serve_result = executor.serveFn.?().?; // ok for this pointer deref to fail
log.debug("target: {s}", .{response.request.target});
log.warn("response ptr: {*}", .{serve_result.ptr}); // BUG: This works in tests, but does not when compiled (even debug mode)
var slice: []u8 = serve_result.ptr[0..serve_result.len];
log.debug("response body: {s}", .{slice});
// Deal with results
// var content_type_added = false;
// for (0..serve_result.headers_len) |inx| {
// const head = serve_result.headers[inx];
// try response.headers.append(
var content_type_added = false;
for (0..serve_result.headers_len) |inx| {
const head = serve_result.headers[inx];
// head.name_ptr[0..head.name_len],
// head.value_ptr[0..head.value_len],
// );
//
// // TODO: are these headers case insensitive?
// content_type_added = std.mem.eql(u8, head.name_ptr[0..head.name_len], "content-type");
// }
// if (!content_type_added)
// try response.headers.append("content-type", "text/plain");
_ = response;
var slice: []u8 = serve_result.ptr[0..serve_result.len];
try response.headers.append(
head.name_ptr[0..head.name_len],
head.value_ptr[0..head.value_len],
);
// TODO: are these headers case insensitive?
content_type_added = std.mem.eql(u8, head.name_ptr[0..head.name_len], "content-type");
}
if (!content_type_added)
try response.headers.append("content-type", "text/plain");
// target is path
var rc = try allocator.create(FullReturn);
rc.executor = executor;
rc.response = slice;
@ -350,7 +354,8 @@ test {
var test_resp_buf: [1024]u8 = undefined;
var test_resp_buf_len: usize = undefined;
test "root path get" {
// std.testing.log_level = .debug;
std.testing.log_level = .debug;
log.debug("", .{});
try testGet("/");
try std.testing.expectEqual(@as(usize, 3), test_resp_buf_len);
try std.testing.expectEqualStrings(" 2.", test_resp_buf[0..test_resp_buf_len]);