From 295c2f68dd0c28b754df14b1eb9f370193fb9740 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Tue, 9 Jan 2024 11:07:07 +0100 Subject: [PATCH] don't pollute global namespace with fio --- examples/bindataformpost/bindataformpost.zig | 2 +- examples/http_params/http_params.zig | 2 +- src/tests/test_auth.zig | 6 +- src/tests/test_http_params.zig | 2 +- src/tests/test_sendfile.zig | 2 +- src/zap.zig | 143 ++++++++++--------- 6 files changed, 81 insertions(+), 76 deletions(-) diff --git a/examples/bindataformpost/bindataformpost.zig b/examples/bindataformpost/bindataformpost.zig index 1265c1c..2578b21 100644 --- a/examples/bindataformpost/bindataformpost.zig +++ b/examples/bindataformpost/bindataformpost.zig @@ -73,7 +73,7 @@ const Handler = struct { defer s.deinit(); std.log.info("?terminate={s}\n", .{s.str}); if (std.mem.eql(u8, s.str, "true")) { - zap.fio_stop(); + zap.stop(); } } } else |err| { diff --git a/examples/http_params/http_params.zig b/examples/http_params/http_params.zig index 5235a8e..68e4040 100644 --- a/examples/http_params/http_params.zig +++ b/examples/http_params/http_params.zig @@ -86,7 +86,7 @@ pub fn main() !void { if (maybe_str) |*s| { defer s.deinit(); if (std.mem.eql(u8, s.str, "true")) { - zap.fio_stop(); + zap.stop(); } } } else |err| { diff --git a/src/tests/test_auth.zig b/src/tests/test_auth.zig index e84beab..d90b1fe 100644 --- a/src/tests/test_auth.zig +++ b/src/tests/test_auth.zig @@ -111,7 +111,7 @@ fn endpoint_http_get(e: *Endpoints.Endpoint, r: zap.Request) void { r.sendBody(HTTP_RESPONSE) catch return; received_response = HTTP_RESPONSE; std.time.sleep(1 * std.time.ns_per_s); - zap.fio_stop(); + zap.stop(); } fn endpoint_http_unauthorized(e: *Endpoints.Endpoint, r: zap.Request) void { @@ -120,7 +120,7 @@ fn endpoint_http_unauthorized(e: *Endpoints.Endpoint, r: zap.Request) void { r.sendBody("UNAUTHORIZED ACCESS") catch return; received_response = "UNAUTHORIZED"; std.time.sleep(1 * std.time.ns_per_s); - zap.fio_stop(); + zap.stop(); } // @@ -165,7 +165,7 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeader std.debug.print("RESPONSE:\n{s}\n", .{buffer[0..len]}); } - zap.fio_stop(); + zap.stop(); } fn makeRequestThread(a: std.mem.Allocator, url: []const u8, auth: ?ClientAuthReqHeaderFields) !std.Thread { diff --git a/src/tests/test_http_params.zig b/src/tests/test_http_params.zig index d3ebdea..c39343f 100644 --- a/src/tests/test_http_params.zig +++ b/src/tests/test_http_params.zig @@ -15,7 +15,7 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8) !void { try req.start(); try req.wait(); - zap.fio_stop(); + zap.stop(); } fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread { diff --git a/src/tests/test_sendfile.zig b/src/tests/test_sendfile.zig index 2725f9d..51064cf 100644 --- a/src/tests/test_sendfile.zig +++ b/src/tests/test_sendfile.zig @@ -22,7 +22,7 @@ fn makeRequest(a: std.mem.Allocator, url: []const u8) !void { try req.wait(); read_len = try req.readAll(&buffer); - zap.fio_stop(); + zap.stop(); } fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread { diff --git a/src/zap.zig b/src/zap.zig index 3b07494..cd825ef 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -2,12 +2,14 @@ // or maybe let's just make it zap directly... const std = @import("std"); -const fio = @import("fio.zig"); + +/// The facilio C API. No need to use this. +pub const fio = @import("fio.zig"); /// Server-Side TLS function wrapper pub const Tls = @import("tls.zig"); -pub usingnamespace @import("fio.zig"); +// pub usingnamespace @import("fio.zig"); pub usingnamespace @import("endpoint.zig"); pub usingnamespace @import("util.zig"); pub usingnamespace @import("http.zig"); @@ -975,75 +977,78 @@ pub const HttpListener = struct { } }; -/// lower level listening, if you don't want to use a listener but rather use -/// the listen() function. -pub const ListenSettings = struct { - on_request: ?FioHttpRequestFn = null, - on_upgrade: ?FioHttpRequestFn = null, - on_response: ?FioHttpRequestFn = null, - on_finish: ?FioHttpRequestFn = null, - public_folder: ?[]const u8 = null, - max_header_size: usize = 32 * 1024, - max_body_size: usize = 50 * 1024 * 1024, - max_clients: isize = 100, - keepalive_timeout_s: u8 = 5, - log: bool = false, - - const Self = @This(); +/// Low-level API +pub const LowLevel = struct { + /// lower level listening, if you don't want to use a listener but rather use + /// the listen() function. + pub const ListenSettings = struct { + on_request: ?FioHttpRequestFn = null, + on_upgrade: ?FioHttpRequestFn = null, + on_response: ?FioHttpRequestFn = null, + on_finish: ?FioHttpRequestFn = null, + public_folder: ?[]const u8 = null, + max_header_size: usize = 32 * 1024, + max_body_size: usize = 50 * 1024 * 1024, + max_clients: isize = 100, + keepalive_timeout_s: u8 = 5, + log: bool = false, + + const Self = @This(); + + /// Create settings with defaults + pub fn init() Self { + return .{}; + } + }; - /// Create settings with defaults - pub fn init() Self { - return .{}; - } -}; + /// Low level listen function + pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSettings) ListenError!void { + var pfolder: [*c]const u8 = null; + var pfolder_len: usize = 0; -/// Low level listen function -pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSettings) ListenError!void { - var pfolder: [*c]const u8 = null; - var pfolder_len: usize = 0; + if (settings.public_folder) |pf| { + pfolder_len = pf.len; + pfolder = pf.ptr; + } + const x: fio.http_settings_s = .{ + .on_request = settings.on_request, + .on_upgrade = settings.on_upgrade, + .on_response = settings.on_response, + .on_finish = settings.on_finish, + .udata = null, + .public_folder = pfolder, + .public_folder_length = pfolder_len, + .max_header_size = settings.max_header_size, + .max_body_size = settings.max_body_size, + .max_clients = settings.max_clients, + .tls = null, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + .ws_max_msg_size = settings.ws_max_msg_size, + .timeout = settings.keepalive_timeout_s, + .ws_timeout = 0, + .log = if (settings.log) 1 else 0, + .is_client = 0, + }; + // TODO: BUG: without this print/sleep statement, -Drelease* loop forever + // in debug2 and debug3 of hello example + // std.debug.print("X\n", .{}); + // TODO: still happening? + std.time.sleep(500 * std.time.ns_per_ms); - if (settings.public_folder) |pf| { - pfolder_len = pf.len; - pfolder = pf.ptr; - } - var x: fio.http_settings_s = .{ - .on_request = settings.on_request, - .on_upgrade = settings.on_upgrade, - .on_response = settings.on_response, - .on_finish = settings.on_finish, - .udata = null, - .public_folder = pfolder, - .public_folder_length = pfolder_len, - .max_header_size = settings.max_header_size, - .max_body_size = settings.max_body_size, - .max_clients = settings.max_clients, - .tls = null, - .reserved1 = 0, - .reserved2 = 0, - .reserved3 = 0, - .ws_max_msg_size = settings.ws_max_msg_size, - .timeout = settings.keepalive_timeout_s, - .ws_timeout = 0, - .log = if (settings.log) 1 else 0, - .is_client = 0, - }; - // TODO: BUG: without this print/sleep statement, -Drelease* loop forever - // in debug2 and debug3 of hello example - // std.debug.print("X\n", .{}); - // TODO: still happening? - std.time.sleep(500 * std.time.ns_per_ms); - - if (fio.http_listen(port, interface, x) == -1) { - return error.ListenError; + if (fio.http_listen(port, interface, x) == -1) { + return error.ListenError; + } } -} -/// lower level sendBody -pub fn sendBody(request: [*c]fio.http_s, body: []const u8) HttpError!void { - const ret = fio.http_send_body(request, @as( - *anyopaque, - @ptrFromInt(@intFromPtr(body.ptr)), - ), body.len); - debug("sendBody(): ret = {}\n", .{ret}); - if (ret != -1) return error.HttpSendBody; -} + /// lower level sendBody + pub fn sendBody(request: [*c]fio.http_s, body: []const u8) HttpError!void { + const ret = fio.http_send_body(request, @as( + *anyopaque, + @ptrFromInt(@intFromPtr(body.ptr)), + ), body.len); + debug("sendBody(): ret = {}\n", .{ret}); + if (ret != -1) return error.HttpSendBody; + } +};