From 44af23827e41bba674dcfc117f08188c75ceeb42 Mon Sep 17 00:00:00 2001 From: Andreas Stocker Date: Sun, 14 Apr 2024 18:19:18 +0200 Subject: [PATCH] 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 | 50 ++++++++----------- 9 files changed, 62 insertions(+), 68 deletions(-) diff --git a/examples/cookies/cookies.zig b/examples/cookies/cookies.zig index ed7268b..087731f 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 be6c06b..495a586 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 aa522d1..e387e9b 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 189e6f2..776a930 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 60b48e5..543600c 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 c4ba86b..07cecc9 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 8862c1c..919876d 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 071085e..08f517c 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 9d97a2c..45f9273 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 server = std.http.Server.init(.{ - .reuse_address = true, - }); - defer server.deinit(); + // var gpa = std.heap.GeneralPurposeAllocator(.{ + // .thread_safe = true, + // }){}; + // const allocator = gpa.allocator(); const address = try std.net.Address.parseIp("127.0.0.1", 3000); - try server.listen(address); + var http_server = try address.listen(.{ + .reuse_address = true, + }); - const max_header_size = 8192; + var read_buffer: [2048]u8 = undefined; + + // 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" }, + }, + }); } }