-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_stream_server.zig
70 lines (57 loc) · 1.94 KB
/
test_stream_server.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
const std = @import("std");
const log = @import("log.zig");
const server = @import("server.zig");
pub fn main() !void {
try do(std.heap.page_allocator);
}
fn do(allocator: std.mem.Allocator) !void {
log.info("started.", .{});
// key and certificates need to be der-formatted.
// if you want to use RSAPrivateKey, please change '.ec' to '.rsa'.
// The procedure to generate test certificate is described in test/gen_cert.sh
var tls_server = try server.TLSServerTCP.init("./test/key.pem", "./test/cert.der", null, "localhost", allocator);
defer tls_server.deinit();
// Enable KEYLOG output.
tls_server.print_keys = true;
try tls_server.listen(8443);
var buf: [32768]u8 = undefined;
while (true) {
var con = try tls_server.accept();
defer {
con.close();
log.info("connection closed", .{});
con.deinit();
}
con.handshake() catch |err| {
switch (err) {
error.EndOfStream => continue,
else => return err,
}
};
while (true) {
const msg_len = con.tlsReader().readInt(u64, .big) catch |err| {
switch (err) {
error.ConnectionResetByPeer => return,
error.EndOfStream => return,
else => return err,
}
};
try con.tlsWriter().writeInt(u64, msg_len, .big);
var cur_idx: u64 = 0;
while (cur_idx < msg_len) {
var end_idx = cur_idx + buf.len;
if (end_idx > msg_len) {
end_idx = msg_len;
}
try con.tlsReader().readNoEof(buf[0 .. end_idx - cur_idx]);
try con.tlsWriter().writeAll(buf[0 .. end_idx - cur_idx]);
cur_idx = end_idx;
}
}
return;
}
return;
}
test "stream" {
try do(std.testing.allocator);
}