From 590f25952d4a17864e5c039cc102349c52efa1df Mon Sep 17 00:00:00 2001 From: Andreas Stocker Date: Thu, 29 Feb 2024 15:51:35 +0100 Subject: [PATCH 1/4] WebSockets: fix write function by replacing fio.str2fio with util.str2fio --- src/websockets.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 0e51d32b3464b0b27cbdbc90aa83970d3dfd5429 Mon Sep 17 00:00:00 2001 From: Andreas Stocker Date: Sat, 23 Mar 2024 19:56:11 +0100 Subject: [PATCH 2/4] replace std.os.exit with std.process.exit --- build.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 85e6b1aa..687dc4fd 100644 --- a/build.zig +++ b/build.zig @@ -5,7 +5,7 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); if (target.result.os.tag == .windows) { std.log.err("\x1b[31mPlatform Not Supported\x1b[0m\nCurrently, Facil.io and Zap are not compatible with Windows. Consider using Linux or Windows Subsystem for Linux (WSL) instead.\nFor more information, please see:\n- https://github.com/zigzap/zap#most-faq\n- https://facil.io/#forking-contributing-and-all-that-jazz\n", .{}); - std.os.exit(1); + std.process.exit(1); } // Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. From 0f8903c06ebf6129d23571814e2f3884aa75c1d7 Mon Sep 17 00:00:00 2001 From: Andreas Stocker Date: Sat, 23 Mar 2024 20:05:59 +0100 Subject: [PATCH 3/4] build: remove openssl env var check --- build.zig | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/build.zig b/build.zig index 687dc4fd..55e87c6d 100644 --- a/build.zig +++ b/build.zig @@ -11,13 +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.os.getenv("ZAP_USE_OPENSSL")) |val| { - if (std.mem.eql(u8, val, "true")) break :blk true; - } - break :blk false; - }; + const use_openssl = b.option(bool, "openssl", "Use system-installed openssl for TLS support in zap") orelse false; // create a module to be used internally. const zap_module = b.addModule("zap", .{ From 44af23827e41bba674dcfc117f08188c75ceeb42 Mon Sep 17 00:00:00 2001 From: Andreas Stocker Date: Sun, 14 Apr 2024 18:19:18 +0200 Subject: [PATCH 4/4] fix breaking changes as of 14-4-24 --- examples/cookies/cookies.zig | 14 +++--- examples/endpoint/userweb.zig | 8 ++-- examples/http_params/http_params.zig | 10 ++-- examples/middleware/middleware.zig | 6 +-- .../middleware_with_endpoint.zig | 6 +-- src/endpoint.zig | 12 ++--- src/middleware.zig | 2 +- tools/announceybot.zig | 22 +++++---- wrk/zigstd/main.zig | 46 ++++++++----------- 9 files changed, 60 insertions(+), 66 deletions(-) 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/endpoint/userweb.zig b/examples/endpoint/userweb.zig index be6c06b2..495a5867 100644 --- a/examples/endpoint/userweb.zig +++ b/examples/endpoint/userweb.zig @@ -54,7 +54,7 @@ fn userIdFromPath(self: *Self, path: []const u8) ?usize { } fn getUser(e: *zap.Endpoint, r: zap.Request) void { - const self = @fieldParentPtr(Self, "ep", e); + const self: *Self = @fieldParentPtr("ep", e); if (r.path) |path| { // /users if (path.len == e.settings.path.len) { @@ -81,7 +81,7 @@ fn listUsers(self: *Self, r: zap.Request) void { } fn postUser(e: *zap.Endpoint, r: zap.Request) void { - const self = @fieldParentPtr(Self, "ep", e); + const self: *Self = @fieldParentPtr("ep", e); if (r.body) |body| { const maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null; if (maybe_user) |u| { @@ -100,7 +100,7 @@ fn postUser(e: *zap.Endpoint, r: zap.Request) void { } fn putUser(e: *zap.Endpoint, r: zap.Request) void { - const self = @fieldParentPtr(Self, "ep", e); + const self: *Self = @fieldParentPtr("ep", e); if (r.path) |path| { if (self.userIdFromPath(path)) |id| { if (self._users.get(id)) |_| { @@ -126,7 +126,7 @@ fn putUser(e: *zap.Endpoint, r: zap.Request) void { } fn deleteUser(e: *zap.Endpoint, r: zap.Request) void { - const self = @fieldParentPtr(Self, "ep", e); + const self: *Self = @fieldParentPtr("ep", e); if (r.path) |path| { if (self.userIdFromPath(path)) |id| { var jsonbuf: [128]u8 = undefined; 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/examples/middleware/middleware.zig b/examples/middleware/middleware.zig index 189e6f27..776a930d 100644 --- a/examples/middleware/middleware.zig +++ b/examples/middleware/middleware.zig @@ -72,7 +72,7 @@ const UserMiddleWare = struct { pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { // this is how we would get our self pointer - const self = @fieldParentPtr(Self, "handler", handler); + const self: *Self = @fieldParentPtr("handler", handler); _ = self; // do our work: fill in the user field of the context @@ -115,7 +115,7 @@ const SessionMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { // this is how we would get our self pointer - const self = @fieldParentPtr(Self, "handler", handler); + const self: *Self = @fieldParentPtr("handler", handler); _ = self; context.session = Session{ @@ -151,7 +151,7 @@ const HtmlMiddleWare = struct { pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { // this is how we would get our self pointer - const self = @fieldParentPtr(Self, "handler", handler); + const self: *Self = @fieldParentPtr("handler", handler); _ = self; std.debug.print("\n\nHtmlMiddleware: handling request with context: {any}\n\n", .{context}); diff --git a/examples/middleware_with_endpoint/middleware_with_endpoint.zig b/examples/middleware_with_endpoint/middleware_with_endpoint.zig index 60b48e52..543600c7 100644 --- a/examples/middleware_with_endpoint/middleware_with_endpoint.zig +++ b/examples/middleware_with_endpoint/middleware_with_endpoint.zig @@ -60,7 +60,7 @@ const UserMiddleWare = struct { pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { // this is how we would get our self pointer - const self = @fieldParentPtr(Self, "handler", handler); + const self: *Self = @fieldParentPtr("handler", handler); _ = self; // do our work: fill in the user field of the context @@ -105,7 +105,7 @@ const SessionMiddleWare = struct { // note that the first parameter is of type *Handler, not *Self !!! pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool { // this is how we would get our self pointer - const self = @fieldParentPtr(Self, "handler", handler); + const self: *Self = @fieldParentPtr("handler", handler); _ = self; context.session = Session{ @@ -155,7 +155,7 @@ const HtmlEndpoint = struct { } pub fn get(ep: *zap.Endpoint, r: zap.Request) void { - const self = @fieldParentPtr(Self, "ep", ep); + const self: *Self = @fieldParentPtr("ep", ep); _ = self; var buf: [1024]u8 = undefined; diff --git a/src/endpoint.zig b/src/endpoint.zig index c4ba86bf..07cecc95 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -112,7 +112,7 @@ pub fn Authenticating(comptime Authenticator: type) type { /// GET: here, the auth_endpoint will be passed in as endpoint. /// Authenticates GET requests using the Authenticator. pub fn get(e: *Endpoint, r: zap.Request) void { - const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + const authEp: *Self = @fieldParentPtr("auth_endpoint", e); switch (authEp.authenticator.authenticateRequest(&r)) { .AuthFailed => { if (e.settings.unauthorized) |unauthorized| { @@ -132,7 +132,7 @@ pub fn Authenticating(comptime Authenticator: type) type { /// POST: here, the auth_endpoint will be passed in as endpoint. /// Authenticates POST requests using the Authenticator. pub fn post(e: *Endpoint, r: zap.Request) void { - const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + const authEp: *Self = @fieldParentPtr("auth_endpoint", e); switch (authEp.authenticator.authenticateRequest(&r)) { .AuthFailed => { if (e.settings.unauthorized) |unauthorized| { @@ -152,7 +152,7 @@ pub fn Authenticating(comptime Authenticator: type) type { /// PUT: here, the auth_endpoint will be passed in as endpoint. /// Authenticates PUT requests using the Authenticator. pub fn put(e: *Endpoint, r: zap.Request) void { - const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + const authEp: *Self = @fieldParentPtr("auth_endpoint", e); switch (authEp.authenticator.authenticateRequest(&r)) { .AuthFailed => { if (e.settings.unauthorized) |unauthorized| { @@ -172,7 +172,7 @@ pub fn Authenticating(comptime Authenticator: type) type { /// DELETE: here, the auth_endpoint will be passed in as endpoint. /// Authenticates DELETE requests using the Authenticator. pub fn delete(e: *Endpoint, r: zap.Request) void { - const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + const authEp: *Self = @fieldParentPtr("auth_endpoint", e); switch (authEp.authenticator.authenticateRequest(&r)) { .AuthFailed => { if (e.settings.unauthorized) |unauthorized| { @@ -192,7 +192,7 @@ pub fn Authenticating(comptime Authenticator: type) type { /// PATCH: here, the auth_endpoint will be passed in as endpoint. /// Authenticates PATCH requests using the Authenticator. pub fn patch(e: *Endpoint, r: zap.Request) void { - const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + const authEp: *Self = @fieldParentPtr("auth_endpoint", e); switch (authEp.authenticator.authenticateRequest(&r)) { .AuthFailed => { if (e.settings.unauthorized) |unauthorized| { @@ -212,7 +212,7 @@ pub fn Authenticating(comptime Authenticator: type) type { /// OPTIONS: here, the auth_endpoint will be passed in as endpoint. /// Authenticates OPTIONS requests using the Authenticator. pub fn options(e: *Endpoint, r: zap.Request) void { - const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + const authEp: *Self = @fieldParentPtr("auth_endpoint", e); switch (authEp.authenticator.authenticateRequest(&r)) { .AuthFailed => { if (e.settings.unauthorized) |unauthorized| { diff --git a/src/middleware.zig b/src/middleware.zig index 8862c1ce..919876d0 100644 --- a/src/middleware.zig +++ b/src/middleware.zig @@ -82,7 +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 { - var self = @fieldParentPtr(Self, "handler", handler); + var self: *Self = @fieldParentPtr("handler", handler); r.setUserContext(context); self.endpoint.onRequest(r); diff --git a/tools/announceybot.zig b/tools/announceybot.zig index 071085ee..08f517ca 100644 --- a/tools/announceybot.zig +++ b/tools/announceybot.zig @@ -28,7 +28,7 @@ fn usage() void { \\ instructions ; std.debug.print("{s}", .{message}); - std.os.exit(1); + std.posix.exit(1); } var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; @@ -150,24 +150,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); @@ -336,7 +338,7 @@ fn command_announce(allocator: std.mem.Allocator, tag: []const u8) !void { defer allocator.free(url); sendToDiscord(allocator, url, announcement) catch |err| { std.debug.print("HTTP ERROR: {any}\n", .{err}); - std.os.exit(1); + std.posix.exit(1); }; } 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" }, + }, + }); } }