diff --git a/build.zig b/build.zig index b6a21cc..3f1b3d2 100644 --- a/build.zig +++ b/build.zig @@ -1,26 +1,30 @@ const builtin = @import("builtin"); const std = @import("std"); -const pkgs = @import("deps.zig").pkgs; +const GitRepoStep = @import("GitRepoStep.zig"); +const CopyStep = @import("CopyStep.zig"); pub fn build(b: *std.build.Builder) !void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. - // We want the target to be aarch64-linux for deploys - const target = std.zig.CrossTarget{ - .cpu_arch = .aarch64, - .os_tag = .linux, - }; + const zfetch_repo = GitRepoStep.create(b, .{ + .url = "https://github.com/truemedian/zfetch", + // .branch = "0.1.10", // branch also takes tags. Tag 0.1.10 isn't quite new enough + .sha = "271cab5da4d12c8f08e67aa0cd5268da100e52f1", + }); + const copy_deps = CopyStep.create( + b, + "zfetch_deps.zig", + "libs/zfetch/deps.zig", + ); + copy_deps.step.dependOn(&zfetch_repo.step); // Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // const mode = b.standardReleaseOptions(); const exe = b.addExecutable("bootstrap", "src/main.zig"); - pkgs.addAllTo(exe); - exe.setTarget(target); + exe.step.dependOn(©_deps.step); + exe.addPackage(getZfetchPackage(b, "libs/zfetch") catch unreachable); + exe.setBuildMode(.ReleaseSafe); const debug = b.option(bool, "debug", "Debug mode (do not strip executable)") orelse false; exe.strip = !debug; @@ -28,7 +32,7 @@ pub fn build(b: *std.build.Builder) !void { // TODO: We can cross-compile of course, but stripping and zip commands // may vary - if (std.builtin.os.tag == .linux) { + if (builtin.os.tag == .linux) { // Package step const package_step = b.step("package", "Package the function"); package_step.dependOn(b.getInstallStep()); @@ -165,10 +169,46 @@ fn fileExists(file_name: []const u8) bool { defer file.close(); return true; } -fn addArgs(allocator: *std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 { +fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u8) ![]const u8 { var rc = original; for (args) |arg| { rc = try std.mem.concat(allocator, u8, &.{ rc, " ", arg }); } return rc; } + +fn getDependency(comptime lib_prefix: []const u8, comptime name: []const u8, comptime root: []const u8) !std.build.Pkg { + const path = lib_prefix ++ "/libs/" ++ name ++ "/" ++ root; + + // We don't actually care if the dependency has been checked out, as + // GitRepoStep will handle that for us + // Make sure that the dependency has been checked out. + // std.fs.cwd().access(path, .{}) catch |err| switch (err) { + // error.FileNotFound => { + // std.log.err("zfetch: dependency '{s}' not checked out", .{name}); + // + // return err; + // }, + // else => return err, + // }; + + return std.build.Pkg{ + .name = name, + .path = .{ .path = path }, + }; +} + +pub fn getZfetchPackage(b: *std.build.Builder, comptime lib_prefix: []const u8) !std.build.Pkg { + var dependencies = b.allocator.alloc(std.build.Pkg, 4) catch unreachable; + + dependencies[0] = try getDependency(lib_prefix, "iguanaTLS", "src/main.zig"); + dependencies[1] = try getDependency(lib_prefix, "network", "network.zig"); + dependencies[2] = try getDependency(lib_prefix, "uri", "uri.zig"); + dependencies[3] = try getDependency(lib_prefix, "hzzp", "src/main.zig"); + + return std.build.Pkg{ + .name = "zfetch", + .path = .{ .path = lib_prefix ++ "/src/main.zig" }, + .dependencies = dependencies, + }; +} diff --git a/gyro.lock b/gyro.lock deleted file mode 100644 index 063d4c4..0000000 --- a/gyro.lock +++ /dev/null @@ -1,5 +0,0 @@ -pkg default ducdetronquito http 0.1.3 -pkg default ducdetronquito h11 0.1.1 -github nektro iguanaTLS 953ad821fae6c920fb82399493663668cd91bde7 src/main.zig 953ad821fae6c920fb82399493663668cd91bde7 -github MasterQ32 zig-network 15b88658809cac9022ec7d59449b0cd3ebfd0361 network.zig 15b88658809cac9022ec7d59449b0cd3ebfd0361 -github elerch requestz 1fa8157641300805b9503f98cd201d0959d19631 src/main.zig 1fa8157641300805b9503f98cd201d0959d19631 diff --git a/gyro.zzz b/gyro.zzz deleted file mode 100644 index 6fba457..0000000 --- a/gyro.zzz +++ /dev/null @@ -1,7 +0,0 @@ -deps: - requestz: - src: - github: - user: elerch - repo: requestz - ref: 1fa8157641300805b9503f98cd201d0959d19631 diff --git a/src/lambda.zig b/src/lambda.zig index 355646d..a0edf67 100644 --- a/src/lambda.zig +++ b/src/lambda.zig @@ -1,29 +1,34 @@ const std = @import("std"); -const requestz = @import("requestz"); +const zfetch = @import("zfetch"); -pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u8) !void { // TODO: remove inferred error set? +pub fn run(event_handler: fn (std.mem.Allocator, []const u8) anyerror![]const u8) !void { // TODO: remove inferred error set? const prefix = "http://"; const postfix = "/2018-06-01/runtime/invocation"; const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API"); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); - const allocator = &gpa.allocator; + const allocator = gpa.allocator(); const url = try std.fmt.allocPrint(allocator, "{s}{s}{s}/next", .{ prefix, lambda_runtime_uri, postfix }); defer allocator.free(url); - std.log.notice("Bootstrap initializing with event url: {s}", .{url}); + try zfetch.init(); + defer zfetch.deinit(); + std.log.info("Bootstrap initializing with event url: {s}", .{url}); + var headers = zfetch.Headers.init(allocator); + defer headers.deinit(); while (true) { var req_alloc = std.heap.ArenaAllocator.init(allocator); defer req_alloc.deinit(); - const req_allocator = &req_alloc.allocator; - var client = try requestz.Client.init(req_allocator); - // defer client.deinit(); + const req_allocator = req_alloc.allocator(); + var req = try zfetch.Request.init(req_allocator, url, null); + defer req.deinit(); + // Lambda freezes the process at this line of code. During warm start, // the process will unfreeze and data will be sent in response to client.get - var response = client.get(url, .{}) catch |err| { + req.do(.GET, headers, null) catch |err| { std.log.err("Get fail: {}", .{err}); // Documentation says something about "exit immediately". The // Lambda infrastrucutre restarts, so it's unclear if that's necessary. @@ -31,12 +36,17 @@ pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u // std.os.exit(1); continue; }; - defer response.deinit(); var request_id: ?[]const u8 = null; - for (response.headers.items()) |h| { - if (std.mem.indexOf(u8, h.name.value, "Lambda-Runtime-Aws-Request-Id")) |_| + var content_length: ?usize = null; + for (req.headers.list.items) |h| { + if (std.ascii.eqlIgnoreCase(h.name, "Lambda-Runtime-Aws-Request-Id")) request_id = h.value; + if (std.ascii.eqlIgnoreCase(h.name, "Content-Length")) { + content_length = std.fmt.parseUnsigned(usize, h.value, 10) catch null; + if (content_length == null) + std.log.warn("Error parsing content length value: '{s}'", .{h.value}); + } // TODO: XRay uses an environment variable to do its magic. It's our // responsibility to set this, but no zig-native setenv(3)/putenv(3) // exists. I would kind of rather not link in libc for this, @@ -54,7 +64,25 @@ pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u } const req_id = request_id.?; - const event_response = event_handler(req_allocator, response.body) catch |err| { + const reader = req.reader(); + var buf: [65535]u8 = undefined; + + var resp_payload = std.ArrayList(u8).init(req_allocator); + if (content_length) |len| { + resp_payload.ensureTotalCapacity(len) catch { + std.log.err("Could not allocate memory for body of request id: {s}", .{request_id.?}); + continue; + }; + } + + defer resp_payload.deinit(); + + while (true) { + const read = try reader.read(&buf); + try resp_payload.appendSlice(buf[0..read]); + if (read == 0) break; + } + const event_response = event_handler(req_allocator, resp_payload.items) catch |err| { // Stack trace will return null if stripped const return_trace = @errorReturnTrace(); std.log.err("Caught error: {}. Return Trace: {}", .{ err, return_trace }); @@ -70,27 +98,37 @@ pub fn run(event_handler: fn (*std.mem.Allocator, []const u8) anyerror![]const u const content_fmt = try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), return_trace, "}" }); defer req_allocator.free(content_fmt); std.log.err("Posting to {s}: Data {s}", .{ err_url, content_fmt }); - var headers = .{.{ "Lambda-Runtime-Function-Error-Type", "HandlerReturned" }}; + + var err_req = try zfetch.Request.init(req_allocator, err_url, null); + defer err_req.deinit(); + var err_headers = zfetch.Headers.init(req_allocator); + defer err_headers.deinit(); + err_headers.append(.{ + .name = "Lambda-Runtime-Function-Error-Type", + .value = "HandlerReturned", + }) catch |append_err| { + std.log.err("Error appending error header to post response for request id {s}: {}", .{ req_id, append_err }); + std.os.exit(0); + continue; + }; // TODO: Determine why this post is not returning - var err_resp = client.post(err_url, .{ - .content = content_fmt, - .headers = headers, - }) catch |post_err| { // Well, at this point all we can do is shout at the void + err_req.do(.POST, err_headers, content_fmt) catch |post_err| { // Well, at this point all we can do is shout at the void std.log.err("Error posting response for request id {s}: {}", .{ req_id, post_err }); std.os.exit(0); continue; }; std.log.err("Post complete", .{}); - defer err_resp.deinit(); continue; }; + // We should catch these potential alloc errors too const response_url = try std.fmt.allocPrint(req_allocator, "{s}{s}{s}/{s}/response", .{ prefix, lambda_runtime_uri, postfix, req_id }); - // defer req_allocator.free(response_url); - var resp_resp = client.post(response_url, .{ .content = event_response }) catch |err| { + const response_content = try std.fmt.allocPrint(req_allocator, "{s} \"content\": \"{s}\" {s}", .{ "{", event_response, "}" }); + var resp_req = try zfetch.Request.init(req_allocator, response_url, null); + defer resp_req.deinit(); + resp_req.do(.POST, headers, response_content) catch |err| { // TODO: report error std.log.err("Error posting response for request id {s}: {}", .{ req_id, err }); continue; }; - defer resp_resp.deinit(); } } diff --git a/src/main.zig b/src/main.zig index e9e6610..5fec78e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,7 +5,7 @@ pub fn main() anyerror!void { try lambda.run(handler); } -fn handler(allocator: *std.mem.Allocator, event_data: []const u8) ![]const u8 { +fn handler(allocator: std.mem.Allocator, event_data: []const u8) ![]const u8 { _ = allocator; return event_data; }