-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
https.zig
80 lines (70 loc) · 2.43 KB
/
https.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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});
}
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
}
fn on_request_minimal(r: zap.Request) 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.process.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 = 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 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,
});
}