zig 0.12.0: upgrade universal_lambda

This commit is contained in:
Emil Lerch 2024-05-06 13:24:13 -07:00
parent 30a4e24656
commit 6242641fa4
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -34,7 +34,7 @@ fn runStandaloneServerParent(allocator: ?std.mem.Allocator, event_handler: inter
var arena = std.heap.ArenaAllocator.init(alloc); var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit(); defer arena.deinit();
var aa = arena.allocator(); const aa = arena.allocator();
var al = std.ArrayList([]const u8).init(aa); var al = std.ArrayList([]const u8).init(aa);
defer al.deinit(); defer al.deinit();
var argi = std.process.args(); var argi = std.process.args();
@ -67,6 +67,7 @@ fn runStandaloneServerParent(allocator: ?std.mem.Allocator, event_handler: inter
/// Will create a web server and marshall all requests back to our event handler /// Will create a web server and marshall all requests back to our event handler
/// To keep things simple, we'll have this on a single thread, at least for now /// To keep things simple, we'll have this on a single thread, at least for now
fn runStandaloneServer(allocator: ?std.mem.Allocator, event_handler: interface.HandlerFn) !u8 { fn runStandaloneServer(allocator: ?std.mem.Allocator, event_handler: interface.HandlerFn) !u8 {
// TODO: Get this under test. It doesn't work in zig 0.12.0
const alloc = allocator orelse std.heap.page_allocator; const alloc = allocator orelse std.heap.page_allocator;
var arena = std.heap.ArenaAllocator.init(alloc); var arena = std.heap.ArenaAllocator.init(alloc);
@ -123,41 +124,37 @@ fn runStandaloneServer(allocator: ?std.mem.Allocator, event_handler: interface.H
return 0; return 0;
} }
fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler: interface.HandlerFn) !void { fn processRequest(aa: std.mem.Allocator, server: *std.net.Server, event_handler: interface.HandlerFn) !void {
var res = try server.accept(.{ .allocator = aa }); var connection = try server.accept();
defer { defer connection.stream.close();
_ = res.reset();
if (res.headers.owned and res.headers.list.items.len > 0) res.headers.deinit();
res.deinit();
}
try res.wait(); // wait for client to send a complete request head
const errstr = "Internal Server Error\n"; var read_buffer: [1024 * 16]u8 = undefined; // TODO: Fix this
var errbuf: [errstr.len]u8 = undefined; var server_connection = std.http.Server.init(connection, &read_buffer);
@memcpy(&errbuf, errstr); var req = try server_connection.receiveHead();
var response_bytes: []const u8 = errbuf[0..];
var body = if (res.request.content_length) |l| const request_body = try (try req.reader()).readAllAlloc(aa, @as(usize, std.math.maxInt(usize)));
try res.reader().readAllAlloc(aa, @as(usize, @intCast(l))) var request_headers = std.ArrayList(std.http.Header).init(aa);
else defer request_headers.deinit();
try aa.dupe(u8, ""); var hi = req.iterateHeaders();
// no need to free - will be handled by arena while (hi.next()) |h| try request_headers.append(h);
var response = interface.Response.init(aa); var response = interface.Response.init(aa);
defer response.deinit(); defer response.deinit();
response.request.headers = res.request.headers; response.request.headers = request_headers.items;
response.request.headers_owned = false; response.request.headers_owned = false;
response.request.target = res.request.target; response.request.target = req.head.target;
response.request.method = res.request.method; response.request.method = req.head.method;
response.headers = res.headers; response.headers = &.{};
response.headers_owned = false; response.headers_owned = false;
response_bytes = event_handler(aa, body, &response) catch |e| brk: { var respond_options = std.http.Server.Request.RespondOptions{};
res.status = response.status; const response_bytes = event_handler(aa, request_body, &response) catch |e| brk: {
res.reason = response.reason; respond_options.status = response.status;
if (res.status.class() == .success) { respond_options.reason = response.reason;
res.status = .internal_server_error; if (respond_options.status.class() == .success) {
res.reason = null; respond_options.status = .internal_server_error;
respond_options.reason = null;
response.body.items = "";
} }
// TODO: stream body to client? or keep internal? // TODO: stream body to client? or keep internal?
// TODO: more about this particular request // TODO: more about this particular request
@ -167,22 +164,12 @@ fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler
} }
break :brk "Unexpected error generating request to lambda"; break :brk "Unexpected error generating request to lambda";
}; };
res.transfer_encoding = .{ .content_length = response_bytes.len };
try res.do(); const final_response = try std.mem.concat(aa, u8, &[_][]const u8{ response.body.items, response_bytes });
_ = try res.writer().writeAll(response.body.items); try req.respond(final_response, respond_options);
_ = try res.writer().writeAll(response_bytes);
try res.finish();
} }
test { test {
std.testing.refAllDecls(@This()); std.testing.refAllDecls(@This());
// if (builtin.os.tag == .wasi) return error.SkipZigTest;
if (@import("builtin").os.tag != .wasi) {
// these use http
std.testing.refAllDecls(@import("lambda.zig"));
std.testing.refAllDecls(@import("cloudflaredeploy.zig"));
std.testing.refAllDecls(@import("CloudflareDeployStep.zig"));
}
std.testing.refAllDecls(@import("console.zig")); std.testing.refAllDecls(@import("console.zig"));
// By importing flexilib.zig, this breaks downstream any time someone // By importing flexilib.zig, this breaks downstream any time someone
// tries to build flexilib, because flexilib.zig becomes the root module, // tries to build flexilib, because flexilib.zig becomes the root module,
@ -207,24 +194,21 @@ fn testRequest(request_bytes: []const u8, event_handler: interface.HandlerFn) !v
var arena = std.heap.ArenaAllocator.init(allocator); var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit(); defer arena.deinit();
var server = std.http.Server.init(allocator, .{ .reuse_address = true });
defer server.deinit();
const address = try std.net.Address.parseIp("127.0.0.1", 0); const address = try std.net.Address.parseIp("127.0.0.1", 0);
try server.listen(address); var http_server = try address.listen(.{ .reuse_address = true });
const server_port = server.socket.listen_address.in.getPort(); const server_port = http_server.listen_address.in.getPort();
var al = std.ArrayList(u8).init(allocator); var al = std.ArrayList(u8).init(allocator);
defer al.deinit(); defer al.deinit();
var writer = al.writer(); const writer = al.writer();
_ = writer; _ = writer;
var aa = arena.allocator(); const aa = arena.allocator();
var bytes_allocated: usize = 0; const bytes_allocated: usize = 0;
// pre-warm // pre-warm
const server_thread = try std.Thread.spawn( const server_thread = try std.Thread.spawn(
.{}, .{},
processRequest, processRequest,
.{ aa, &server, event_handler }, .{ aa, &http_server, event_handler },
); );
const stream = try std.net.tcpConnectToHost(allocator, "127.0.0.1", server_port); const stream = try std.net.tcpConnectToHost(allocator, "127.0.0.1", server_port);