diff --git a/build.zig b/build.zig index be6f5c93..44fd73e2 100644 --- a/build.zig +++ b/build.zig @@ -11,6 +11,7 @@ pub fn build(b: *std.Build) !void { // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const optimize = b.standardOptimizeOption(.{}); + const use_openssl = b.option(bool, "openssl", "Use system-installed openssl for TLS support in zap") orelse blk: { // Alternatively, use an os env var to determine whether to build openssl support if (std.posix.getenv("ZAP_USE_OPENSSL")) |val| { diff --git a/examples/cookies/cookies.zig b/examples/cookies/cookies.zig index ed7268be..087731fe 100644 --- a/examples/cookies/cookies.zig +++ b/examples/cookies/cookies.zig @@ -5,17 +5,19 @@ const zap = @import("zap"); fn makeRequest(a: std.mem.Allocator, url: []const u8) !void { const uri = try std.Uri.parse(url); - var h = std.http.Headers{ .allocator = a }; - defer h.deinit(); - var http_client: std.http.Client = .{ .allocator = a }; defer http_client.deinit(); - var req = try http_client.open(.GET, uri, h, .{}); + var server_header_buffer: [2048]u8 = undefined; + var req = try http_client.open(.GET, uri, .{ + .server_header_buffer = &server_header_buffer, + .extra_headers = &.{ + .{ .name = "cookie", .value = "ZIG_ZAP=awesome" }, + }, + }); defer req.deinit(); - try req.headers.append("cookie", "ZIG_ZAP=awesome"); - try req.send(.{}); + try req.send(); try req.wait(); } diff --git a/examples/http_params/http_params.zig b/examples/http_params/http_params.zig index aa522d18..e387e9b0 100644 --- a/examples/http_params/http_params.zig +++ b/examples/http_params/http_params.zig @@ -5,16 +5,16 @@ const zap = @import("zap"); fn makeRequest(a: std.mem.Allocator, url: []const u8) !void { const uri = try std.Uri.parse(url); - var h = std.http.Headers{ .allocator = a }; - defer h.deinit(); - var http_client: std.http.Client = .{ .allocator = a }; defer http_client.deinit(); - var req = try http_client.open(.GET, uri, h, .{}); + var server_header_buffer: [2048]u8 = undefined; + var req = try http_client.open(.GET, uri, .{ + .server_header_buffer = &server_header_buffer, + }); defer req.deinit(); - try req.send(.{}); + try req.send(); try req.wait(); } diff --git a/src/middleware.zig b/src/middleware.zig index 91b3da36..e9b121e0 100644 --- a/src/middleware.zig +++ b/src/middleware.zig @@ -82,6 +82,7 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt /// If `breakOnFinish` is `true`, the handler will stop handing requests down the chain if /// the endpoint processed the request. pub fn onRequest(handler: *HandlerType, r: zap.Request, context: *ContextType) bool { + const self: *Self = @fieldParentPtr("handler", handler); r.setUserContext(context); self.endpoint.onRequest(r); diff --git a/src/websockets.zig b/src/websockets.zig index c92ba370..772a2893 100644 --- a/src/websockets.zig +++ b/src/websockets.zig @@ -119,7 +119,7 @@ pub fn Handler(comptime ContextType: type) type { pub inline fn write(handle: WsHandle, message: []const u8, is_text: bool) WebSocketError!void { if (fio.websocket_write( handle, - fio.str2fio(message), + util.str2fio(message), if (is_text) 1 else 0, ) != 0) { return error.WriteError; diff --git a/tools/announceybot.zig b/tools/announceybot.zig index 2b7e835b..3a88deb7 100644 --- a/tools/announceybot.zig +++ b/tools/announceybot.zig @@ -28,6 +28,7 @@ fn usage() void { \\ instructions ; std.debug.print("{s}", .{message}); + std.process.exit(1); } @@ -150,24 +151,26 @@ fn sendToDiscordPart(allocator: std.mem.Allocator, url: []const u8, message_json // url const uri = try std.Uri.parse(url); - // http headers - var h = std.http.Headers{ .allocator = allocator }; - defer h.deinit(); - try h.append("accept", "*/*"); - try h.append("Content-Type", "application/json"); - // client var http_client: std.http.Client = .{ .allocator = allocator }; defer http_client.deinit(); + var server_header_buffer: [2048]u8 = undefined; + // request - var req = try http_client.open(.POST, uri, h, .{}); + var req = try http_client.open(.POST, uri, .{ + .server_header_buffer = &server_header_buffer, + .extra_headers = &.{ + .{ .name = "accept", .value = "*/*" }, + .{ .name = "Content-Type", .value = "application/json" }, + }, + }); defer req.deinit(); req.transfer_encoding = .chunked; // connect, send request - try req.send(.{}); + try req.send(); // send POST payload try req.writer().writeAll(message_json); diff --git a/wrk/zigstd/main.zig b/wrk/zigstd/main.zig index 9d97a2cc..45f9273e 100644 --- a/wrk/zigstd/main.zig +++ b/wrk/zigstd/main.zig @@ -1,43 +1,33 @@ const std = @import("std"); pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{ - .thread_safe = true, - }){}; - const allocator = gpa.allocator(); + // var gpa = std.heap.GeneralPurposeAllocator(.{ + // .thread_safe = true, + // }){}; + // const allocator = gpa.allocator(); - var server = std.http.Server.init(.{ + const address = try std.net.Address.parseIp("127.0.0.1", 3000); + var http_server = try address.listen(.{ .reuse_address = true, }); - defer server.deinit(); - const address = try std.net.Address.parseIp("127.0.0.1", 3000); - try server.listen(address); + var read_buffer: [2048]u8 = undefined; - const max_header_size = 8192; + // const max_header_size = 8192; while (true) { - var res = try server.accept(.{ - .allocator = allocator, - .header_strategy = .{ .dynamic = max_header_size }, - }); - // const start_time = std.time.nanoTimestamp(); - defer res.deinit(); - defer _ = res.reset(); - try res.wait(); + const connection = try http_server.accept(); + defer connection.stream.close(); + var server = std.http.Server.init(connection, &read_buffer); + var request = try server.receiveHead(); const server_body: []const u8 = "HI FROM ZIG STD!\n"; - res.transfer_encoding = .{ .content_length = server_body.len }; - try res.headers.append("content-type", "text/plain"); - try res.headers.append("connection", "close"); - try res.send(); - var buf: [128]u8 = undefined; - _ = try res.readAll(&buf); - _ = try res.writer().writeAll(server_body); - try res.finish(); - // const end_time = std.time.nanoTimestamp(); - // const diff = end_time - start_time; - // std.debug.print("{d}\n", .{diff}); + try request.respond(server_body, .{ + .extra_headers = &.{ + .{ .name = "content_type", .value = "text/plain" }, + .{ .name = "connection", .value = "close" }, + }, + }); } }