deal with headers
This commit is contained in:
parent
ce5fc4e99f
commit
271c53f650
|
@ -85,10 +85,12 @@ pub fn build(b: *std.Build) void {
|
||||||
const run_main_tests = b.addRunArtifact(main_tests);
|
const run_main_tests = b.addRunArtifact(main_tests);
|
||||||
const run_lib_tests = b.addRunArtifact(lib_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,
|
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||||
// and can be selected like this: `zig build test`
|
// and can be selected like this: `zig build test`
|
||||||
// This will evaluate the `test` step rather than the default, which is "install".
|
// This will evaluate the `test` step rather than the default, which is "install".
|
||||||
const test_step = b.step("test", "Run unit tests");
|
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_lib_tests.step);
|
||||||
|
test_step.dependOn(&run_main_tests.step);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,20 +30,21 @@ export fn handle_request() ?*interface.Response {
|
||||||
}
|
}
|
||||||
return null;
|
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});
|
||||||
log.err("Unexpected error processing request: {any}", .{e});
|
// Marshall data back for handling by server
|
||||||
if (@errorReturnTrace()) |trace| {
|
|
||||||
std.debug.dumpStackTrace(trace.*);
|
var rc = allocator.create(interface.Response) catch @panic("OOM");
|
||||||
}
|
rc.ptr = response.items.ptr;
|
||||||
return null;
|
rc.len = response.items.len;
|
||||||
},
|
rc.headers = interface.toHeaders(allocator, headers) catch |e| {
|
||||||
.headers_len = headers.count(),
|
log.err("Unexpected error processing request: {any}", .{e});
|
||||||
|
if (@errorReturnTrace()) |trace| {
|
||||||
|
std.debug.dumpStackTrace(trace.*);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
rc.headers_len = headers.count();
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +65,7 @@ fn handleRequest(response: Response) !void {
|
||||||
// real work
|
// real work
|
||||||
response_writer.print(" 2.", .{}) catch unreachable;
|
response_writer.print(" 2.", .{}) catch unreachable;
|
||||||
try response.headers.put("X-custom-foo", "bar");
|
try response.headers.put("X-custom-foo", "bar");
|
||||||
|
log.info("handlerequest header count {d}", .{response.headers.count()});
|
||||||
}
|
}
|
||||||
|
|
||||||
test "handle_request" {
|
test "handle_request" {
|
||||||
|
|
37
src/main.zig
37
src/main.zig
|
@ -68,23 +68,27 @@ fn serve(allocator: std.mem.Allocator, response: *std.http.Server.Response) !*Fu
|
||||||
errdefer executor.in_request_lock = false;
|
errdefer executor.in_request_lock = false;
|
||||||
// Call external library
|
// Call external library
|
||||||
var serve_result = executor.serveFn.?().?; // ok for this pointer deref to fail
|
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
|
// Deal with results
|
||||||
// var content_type_added = false;
|
var content_type_added = false;
|
||||||
// for (0..serve_result.headers_len) |inx| {
|
for (0..serve_result.headers_len) |inx| {
|
||||||
// const head = serve_result.headers[inx];
|
const head = serve_result.headers[inx];
|
||||||
// try response.headers.append(
|
// head.name_ptr[0..head.name_len],
|
||||||
// head.name_ptr[0..head.name_len],
|
try response.headers.append(
|
||||||
// head.value_ptr[0..head.value_len],
|
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");
|
// 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");
|
if (!content_type_added)
|
||||||
_ = response;
|
try response.headers.append("content-type", "text/plain");
|
||||||
var slice: []u8 = serve_result.ptr[0..serve_result.len];
|
// target is path
|
||||||
var rc = try allocator.create(FullReturn);
|
var rc = try allocator.create(FullReturn);
|
||||||
rc.executor = executor;
|
rc.executor = executor;
|
||||||
rc.response = slice;
|
rc.response = slice;
|
||||||
|
@ -350,7 +354,8 @@ test {
|
||||||
var test_resp_buf: [1024]u8 = undefined;
|
var test_resp_buf: [1024]u8 = undefined;
|
||||||
var test_resp_buf_len: usize = undefined;
|
var test_resp_buf_len: usize = undefined;
|
||||||
test "root path get" {
|
test "root path get" {
|
||||||
// std.testing.log_level = .debug;
|
std.testing.log_level = .debug;
|
||||||
|
log.debug("", .{});
|
||||||
try testGet("/");
|
try testGet("/");
|
||||||
try std.testing.expectEqual(@as(usize, 3), test_resp_buf_len);
|
try std.testing.expectEqual(@as(usize, 3), test_resp_buf_len);
|
||||||
try std.testing.expectEqualStrings(" 2.", test_resp_buf[0..test_resp_buf_len]);
|
try std.testing.expectEqualStrings(" 2.", test_resp_buf[0..test_resp_buf_len]);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user