diff --git a/build.zig b/build.zig index d93b476..e4a3f33 100644 --- a/build.zig +++ b/build.zig @@ -26,6 +26,7 @@ pub fn build(b: *std.build.Builder) !void { src: []const u8, }{ .{ .name = "hello", .src = "examples/hello/hello.zig" }, + .{ .name = "https", .src = "examples/https/https.zig" }, .{ .name = "hello2", .src = "examples/hello2/hello2.zig" }, .{ .name = "routes", .src = "examples/routes/routes.zig" }, .{ .name = "serve", .src = "examples/serve/serve.zig" }, @@ -47,6 +48,7 @@ pub fn build(b: *std.build.Builder) !void { }) |excfg| { const ex_name = excfg.name; const ex_src = excfg.src; + const ex_build_desc = try std.fmt.allocPrint( b.allocator, "build the {s} example", diff --git a/examples/https/https.zig b/examples/https/https.zig new file mode 100644 index 0000000..8dc16aa --- /dev/null +++ b/examples/https/https.zig @@ -0,0 +1,80 @@ +const std = @import("std"); +const zap = @import("zap"); + +fn on_request_verbose(r: zap.SimpleRequest) void { + if (r.path) |the_path| { + std.debug.print("PATH: {s}\n", .{the_path}); + } + + if (r.query) |the_query| { + std.debug.print("QUERY: {s}\n", .{the_query}); + } + r.sendBody("

Hello from ZAP!!!

") catch return; +} + +fn on_request_minimal(r: zap.SimpleRequest) void { + r.sendBody("

Hello from ZAP!!!

") catch return; +} + +fn help_and_exit(filename: []const u8, err: anyerror) void { + std.debug.print( + \\ Error: File `{s}` : {any} + \\ + \\ To generate both the certificate file and the key file, use the following command: + \\ + \\ ********************************************************************************************** + \\ openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout mykey.pem -out mycert.pem + \\ ********************************************************************************************** + \\ + \\ After that, run this example again + , + .{ filename, err }, + ); + std.os.exit(1); +} +pub fn main() !void { + const CERT_FILE = "mycert.pem"; + const KEY_FILE = "mykey.pem"; + + std.fs.cwd().access(CERT_FILE, .{}) catch |err| { + help_and_exit(CERT_FILE, err); + }; + + std.fs.cwd().access(KEY_FILE, .{}) catch |err| { + help_and_exit(KEY_FILE, err); + }; + + const tls = zap.fio_tls_new( + "localhost:4443", + CERT_FILE, + KEY_FILE, + null, // key file is not password-protected + ); + + var listener = zap.SimpleHttpListener.init(.{ + .port = 4443, + .on_request = on_request_verbose, + .log = true, + .max_clients = 100000, + .tls = tls, + }); + try listener.listen(); + + std.debug.print("Listening on 0.0.0.0:4443\n", .{}); + std.debug.print("", .{}); + std.debug.print( + \\ + \\ *********************************************** + \\ *** Try me with: curl -k -v localhost:4443/ *** + \\ *********************************************** + \\ + \\Your browser may lie to you, indicate a non-secure connection because of the self-created certificate, and make you believe that HTTPS / TLS "does not work". + \\ + , .{}); + + // start worker threads + zap.start(.{ + .threads = 2, + .workers = 1, + }); +}