Compare commits
4 Commits
5376b6a725
...
95b5394aa8
Author | SHA1 | Date | |
---|---|---|---|
95b5394aa8 | |||
e84f6e7efe | |||
878f62ef0a | |||
02df7a1c75 |
|
@ -55,6 +55,19 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
|
||||||
|
|
||||||
var client = std.http.Client{ .allocator = b.allocator };
|
var client = std.http.Client{ .allocator = b.allocator };
|
||||||
defer client.deinit();
|
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
|
const script = self.options.primary_file_data orelse
|
||||||
try std.fs.cwd().readFileAlloc(b.allocator, self.primary_javascript_file.path, std.math.maxInt(usize));
|
try std.fs.cwd().readFileAlloc(b.allocator, self.primary_javascript_file.path, std.math.maxInt(usize));
|
||||||
|
|
|
@ -13,13 +13,19 @@ pub fn main() !u8 {
|
||||||
const allocator = arena.allocator();
|
const allocator = arena.allocator();
|
||||||
var client = std.http.Client{ .allocator = allocator };
|
var client = std.http.Client{ .allocator = allocator };
|
||||||
defer client.deinit();
|
defer client.deinit();
|
||||||
// .allocator = allocator,
|
var proxy_text = std.os.getenv("https_proxy") orelse std.os.getenv("HTTPS_PROXY");
|
||||||
// .proxy = .{
|
if (proxy_text) |p| {
|
||||||
// .protocol = .plain,
|
client.deinit();
|
||||||
// .host = "localhost",
|
const proxy = try std.Uri.parse(p);
|
||||||
// .port = 8080,
|
client = std.http.Client{
|
||||||
// },
|
.allocator = allocator,
|
||||||
// };
|
.proxy = .{
|
||||||
|
.protocol = if (std.ascii.eqlIgnoreCase(proxy.scheme, "http")) .plain else .tls,
|
||||||
|
.host = proxy.host.?,
|
||||||
|
.port = proxy.port,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
const stdout_file = std.io.getStdOut().writer();
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var bw = std.io.bufferedWriter(stdout_file);
|
||||||
|
@ -367,5 +373,5 @@ fn addAuthHeaders(headers: *std.http.Headers) !void {
|
||||||
try headers.append("X-Auth-Email", email);
|
try headers.append("X-Auth-Email", email);
|
||||||
try headers.append("X-Auth-Key", x_auth_key.?);
|
try headers.append("X-Auth-Key", x_auth_key.?);
|
||||||
}
|
}
|
||||||
return error.NoCfAuthenticationEnvironmentVariablesSet;
|
return error.NoCloudflareAuthenticationEnvironmentVariablesSet;
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,8 +85,23 @@ fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler
|
||||||
try aa.dupe(u8, "");
|
try aa.dupe(u8, "");
|
||||||
// no need to free - will be handled by arena
|
// no need to free - will be handled by arena
|
||||||
|
|
||||||
response_bytes = event_handler(aa, body, .{ .web_request = &res }) catch |e| brk: {
|
var response = interface.Response.init(aa);
|
||||||
if (res.status.class() == .success) res.status = .internal_server_error;
|
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?
|
||||||
// TODO: more about this particular request
|
// TODO: more about this particular request
|
||||||
log.err("Unexpected error from executor processing request: {any}", .{e});
|
log.err("Unexpected error from executor processing request: {any}", .{e});
|
||||||
if (@errorReturnTrace()) |trace| {
|
if (@errorReturnTrace()) |trace| {
|
||||||
|
@ -97,6 +112,7 @@ fn processRequest(aa: std.mem.Allocator, server: *std.http.Server, event_handler
|
||||||
res.transfer_encoding = .{ .content_length = response_bytes.len };
|
res.transfer_encoding = .{ .content_length = response_bytes.len };
|
||||||
|
|
||||||
try res.do();
|
try res.do();
|
||||||
|
_ = try res.writer().writeAll(response.body.items);
|
||||||
_ = try res.writer().writeAll(response_bytes);
|
_ = try res.writer().writeAll(response_bytes);
|
||||||
try res.finish();
|
try res.finish();
|
||||||
}
|
}
|
||||||
|
@ -116,3 +132,58 @@ test {
|
||||||
|
|
||||||
// TODO: Do we want build files here too?
|
// 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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user