1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2026-01-29 09:06:58 +00:00
zap/examples/https/https.zig
renerocksai a0f67ffc6e
Update to Zig 0.16.0-dev (master) compatibility
Adapt codebase to breaking API changes in Zig 0.16.0-dev:

std.Io introduction:
- std.io namespace renamed to std.Io (capitalization)
- std.http.Client now requires io field
- Create std.Io.Threaded at entry points, pass io to functions
- Use std.Io.Writer.fixed() for fixed buffer writers
- Use std.Io.Dir.cwd().access(io, ...) for file access checks

Sleep API:
- std.Thread.sleep / std.time.sleep removed
- Use std.posix.nanosleep(seconds, nanoseconds) instead

JSON API:
- std.json.stringify() -> std.json.Stringify.value()

Time API:
- std.time.nanoTimestamp() removed
- Use std.time.Instant.now() with .since() for durations

File API:
- readFileAlloc parameter order changed, uses std.Io.Limit

Debug API:
- std.debug.writeStackTrace signature changed (removed debugInfo)
- Capture traces by pointer with |*trace| pattern

Refactored Io pattern in tests/examples to create Threaded once
at entry points and pass io: std.Io to helper functions, following
Zig's new convention of treating Io like allocators.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-06 16:11:10 +01:00

93 lines
2.8 KiB
Zig

//!
//! Part of the Zap examples.
//!
//! Build me with `zig build -Dopenssl=true https`.
//! Run me with `zig build -Dopenssl=true run-https`.
//!
const std = @import("std");
const zap = @import("zap");
fn on_request_verbose(r: zap.Request) !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});
}
try r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>");
}
fn on_request_minimal(r: zap.Request) !void {
try r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>");
}
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.process.exit(1);
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var threaded = std.Io.Threaded.init(gpa.allocator());
defer threaded.deinit();
const io = threaded.io();
const CERT_FILE = "mycert.pem";
const KEY_FILE = "mykey.pem";
std.Io.Dir.cwd().access(io, CERT_FILE, .{}) catch |err| {
help_and_exit(CERT_FILE, err);
};
std.Io.Dir.cwd().access(io, KEY_FILE, .{}) catch |err| {
help_and_exit(KEY_FILE, err);
};
const tls = try zap.Tls.init(.{
.server_name = "localhost:4443",
.public_certificate_file = CERT_FILE,
.private_key_file = KEY_FILE,
});
defer tls.deinit();
var listener = zap.HttpListener.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 https://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,
});
}