Skip to content

Commit

Permalink
added https example
Browse files Browse the repository at this point in the history
  • Loading branch information
renerocksai committed Dec 29, 2023
1 parent edf585c commit 5e789ee
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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",
Expand Down
80 changes: 80 additions & 0 deletions examples/https/https.zig
Original file line number Diff line number Diff line change
@@ -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("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
}

fn on_request_minimal(r: zap.SimpleRequest) void {
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") 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,
});
}

0 comments on commit 5e789ee

Please sign in to comment.