1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const std = @import("std");
const zttp = @import("zttp");
const zio = @import("zio");
const Io = std.Io;
const Observation = struct { status: ?u16 = null, body_base64: []const u8 = "", failure: ?[]const u8 = null };
const Result = struct { first: Observation, followup: ?Observation = null };
fn once(client: *zttp.Client, allocator: std.mem.Allocator, url: []const u8, method: std.http.Method) Observation {
var result = client.fetch(.{ .url = url, .method = method, .deadline_ns = std.time.ns_per_s }) catch |err| return .{ .failure = @errorName(err) };
defer result.deinit(allocator);
const encoded = allocator.alloc(u8, std.base64.standard.Encoder.calcSize(result.body.len)) catch return .{ .failure = "OutOfMemory" };
_ = std.base64.standard.Encoder.encode(encoded, result.body);
return .{ .status = @backingInt(result.status), .body_base64 = encoded };
}
fn exchange(io: Io, allocator: std.mem.Allocator, url: []const u8, method: std.http.Method, followup: bool, ca_file: []const u8) Result {
var client = zttp.Client.init(io, allocator);
defer client.deinit();
if (!std.mem.eql(u8, ca_file, "-")) {
const now = Io.Clock.real.now(io);
client.http_client.ca_bundle.addCertsFromFilePathAbsolute(allocator, io, now, ca_file) catch |err| return .{ .first = .{ .failure = @errorName(err) } };
client.http_client.now = now;
}
client.head_deadline_ns = 300 * std.time.ns_per_ms;
client.dead_connection_attempts = 1;
client.max_response_size = 128 * 1024;
client.stall = .{ .idle_ns = 300 * std.time.ns_per_ms, .min_bytes_per_s = 0 };
const first = once(&client, allocator, url, method);
return .{ .first = first, .followup = if (followup) once(&client, allocator, url, .GET) else null };
}
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
const args = try init.minimal.args.toSlice(init.arena.allocator());
if (args.len != 6) return error.ExpectedBackendUrlMethodFollowup;
const method = std.meta.stringToEnum(std.http.Method, args[3]) orelse return error.InvalidMethod;
const followup = std.mem.eql(u8, args[4], "followup");
const observation = if (std.mem.eql(u8, args[1], "threaded")) result: {
var threaded: Io.Threaded = .init(allocator, .{ .async_limit = .nothing });
defer threaded.deinit();
break :result exchange(threaded.io(), allocator, args[2], method, followup, args[5]);
} else result: {
if (!std.mem.eql(u8, args[1], "zio") and !std.mem.eql(u8, args[1], "zio4") and !std.mem.eql(u8, args[1], "zio-native")) return error.InvalidBackend;
const executors: u8 = if (std.mem.eql(u8, args[1], "zio4")) 4 else 1;
const runtime = try zio.Runtime.init(allocator, .{ .executors = .exact(executors), .enable_main_executor = false });
defer runtime.deinit();
if (std.mem.eql(u8, args[1], "zio-native")) break :result exchange(runtime.io(), allocator, args[2], method, followup, args[5]);
var task = try runtime.spawn(exchange, .{ runtime.io(), allocator, args[2], method, followup, args[5] });
break :result task.join();
};
defer {
if (observation.first.failure == null) allocator.free(observation.first.body_base64);
if (observation.followup) |next| if (next.failure == null) {
allocator.free(next.body_base64);
};
}
var buffer: [4096]u8 = undefined;
var output = Io.File.stdout().writer(init.io, &buffer);
try std.json.Stringify.value(observation, .{}, &output.interface);
try output.interface.writeByte('\n');
try output.interface.flush();
}