Compare commits

..

No commits in common. "95b5394aa8aac15849d4e8496e076b966182cfce" and "5376b6a725d406b0b9581eb65505a852730d6a98" have entirely different histories.

3 changed files with 10 additions and 100 deletions

View File

@ -55,19 +55,6 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
var client = std.http.Client{ .allocator = b.allocator };
defer client.deinit();
var proxy_text = std.os.getenv("https_proxy") orelse std.os.getenv("HTTPS_PROXY");
if (proxy_text) |p| {
client.deinit();
const proxy = try std.Uri.parse(p);
client = std.http.Client{
.allocator = b.allocator,
.proxy = .{
.protocol = if (std.ascii.eqlIgnoreCase(proxy.scheme, "http")) .plain else .tls,
.host = proxy.host.?,
.port = proxy.port,
},
};
}
const script = self.options.primary_file_data orelse
try std.fs.cwd().readFileAlloc(b.allocator, self.primary_javascript_file.path, std.math.maxInt(usize));

View File

@ -13,19 +13,13 @@ pub fn main() !u8 {
const allocator = arena.allocator();
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
var proxy_text = std.os.getenv("https_proxy") orelse std.os.getenv("HTTPS_PROXY");
if (proxy_text) |p| {
client.deinit();
const proxy = try std.Uri.parse(p);
client = std.http.Client{
.allocator = allocator,
.proxy = .{
.protocol = if (std.ascii.eqlIgnoreCase(proxy.scheme, "http")) .plain else .tls,
.host = proxy.host.?,
.port = proxy.port,
},
};
}
// .allocator = allocator,
// .proxy = .{
// .protocol = .plain,
// .host = "localhost",
// .port = 8080,
// },
// };
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
@ -373,5 +367,5 @@ fn addAuthHeaders(headers: *std.http.Headers) !void {
try headers.append("X-Auth-Email", email);
try headers.append("X-Auth-Key", x_auth_key.?);
}
return error.NoCloudflareAuthenticationEnvironmentVariablesSet;
return error.NoCfAuthenticationEnvironmentVariablesSet;
}

View File

@ -85,23 +85,8 @@ fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler
try aa.dupe(u8, "");
// no need to free - will be handled by arena
var response = interface.Response.init(aa);
defer response.deinit();
response.request.headers = res.request.headers;
response.request.headers_owned = false;
response.request.target = res.request.target;
response.request.method = res.request.method;
response.headers = res.headers;
response.headers_owned = false;
response_bytes = event_handler(aa, body, &response) catch |e| brk: {
res.status = response.status;
res.reason = response.reason;
if (res.status.class() == .success) {
res.status = .internal_server_error;
res.reason = null;
}
// TODO: stream body to client? or keep internal?
response_bytes = event_handler(aa, body, .{ .web_request = &res }) catch |e| brk: {
if (res.status.class() == .success) res.status = .internal_server_error;
// TODO: more about this particular request
log.err("Unexpected error from executor processing request: {any}", .{e});
if (@errorReturnTrace()) |trace| {
@ -112,7 +97,6 @@ fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler
res.transfer_encoding = .{ .content_length = response_bytes.len };
try res.do();
_ = try res.writer().writeAll(response.body.items);
_ = try res.writer().writeAll(response_bytes);
try res.finish();
}
@ -132,58 +116,3 @@ test {
// TODO: Do we want build files here too?
}
fn testRequest(request_bytes: []const u8, event_handler: interface.HandlerFn) !void {
const allocator = std.testing.allocator;
var arena = std.heap.ArenaAllocator.init(allocator);
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);
try server.listen(address);
const server_port = server.socket.listen_address.in.getPort();
var al = std.ArrayList(u8).init(allocator);
defer al.deinit();
var writer = al.writer();
_ = writer;
var aa = arena.allocator();
var bytes_allocated: usize = 0;
// pre-warm
const server_thread = try std.Thread.spawn(
.{},
processRequest,
.{ aa, &server, event_handler },
);
const stream = try std.net.tcpConnectToHost(allocator, "127.0.0.1", server_port);
defer stream.close();
_ = try stream.writeAll(request_bytes[0..]);
server_thread.join();
log.debug("Bytes allocated during request: {d}", .{arena.queryCapacity() - bytes_allocated});
log.debug("Stdout: {s}", .{al.items});
}
fn testGet(comptime path: []const u8, event_handler: interface.HandlerFn) !void {
try testRequest("GET " ++ path ++ " HTTP/1.1\r\n" ++
"Accept: */*\r\n" ++
"\r\n", event_handler);
}
test "can make a request" {
if (@import("builtin").os.tag == .wasi) return error.SkipZigTest;
const HandlerClosure = struct {
var data_received: []const u8 = undefined;
var context_received: interface.Context = undefined;
const Self = @This();
pub fn handler(allocator: std.mem.Allocator, event_data: []const u8, context: interface.Context) ![]const u8 {
_ = allocator;
data_received = event_data;
context_received = context;
return "success";
}
};
try testGet("/", HandlerClosure.handler);
}