Last active
April 24, 2023 15:35
-
-
Save zigster64/955298295f43c6d50975d48cfd389614 to your computer and use it in GitHub Desktop.
Demonstrate proof of keep alive bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn main() !void { | |
var server = Server.init(std.heap.page_allocator, .{ .reuse_address = true }); | |
defer server.deinit(); | |
try server.listen(try net.Address.parseIp("127.0.0.1", 8080)); | |
while (true) { | |
var buf: [8192]u8 = undefined; | |
std.log.info("Waiting for a new connection ...", .{}); | |
const res = try server.accept(.{ .static = &buf }); // use a buffer for headers | |
const thread = try std.Thread.spawn(.{}, handler, .{res}); | |
thread.detach(); | |
} | |
} | |
fn handler(res: *Server.Response) !void { | |
std.log.info("New thread started ...", .{}); // <- this log shows that we are getting a new thread per req | |
try res.headers.append("server", "zwerve"); | |
while (true) { | |
defer res.reset(); | |
try res.wait(); | |
// Add this manual hackery to coerce the closing to FALSE if req is keepalive | |
// If you do this, then the before-patch code will correctly keepalive | |
// If you remove this, then the after-patch code will correctly keepalive | |
const req_connection = res.request.headers.getFirstValue("connection"); | |
const req_keepalive = req_connection != null and !std.ascii.eqlIgnoreCase("close", req_connection.?); | |
if (req_keepalive) { | |
res.connection.conn.closing = false; | |
} | |
res.transfer_encoding = .{ .content_length = 3 }; | |
try res.do(); | |
try res.writer().writeAll("ZIG"); | |
try res.finish(); | |
if (res.connection.conn.closing) break; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment