From bcb3d9a56df086aa97c05f2835e670d0b3989fb1 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Tue, 28 May 2024 12:54:42 +0200 Subject: [PATCH] serve a http request in a thread --- backend/cmd/httpd/main.ha | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/backend/cmd/httpd/main.ha b/backend/cmd/httpd/main.ha index 819a2f5..7babba4 100644 --- a/backend/cmd/httpd/main.ha +++ b/backend/cmd/httpd/main.ha @@ -9,6 +9,8 @@ use io; use fmt; use bufio; use strings; +use thread; +use time; const usage: [_]getopt::help = [ "HTTP server", @@ -50,21 +52,29 @@ export fn main() void = { yield this; case net::error => abort("failure while serving"); }; - defer http::serve_finish(serv_req); - let buf = memio::dynamic(); - defer io::close(&buf)!; - handlereq(&buf, &serv_req.request); - - http::response_write( - serv_req.socket, - http::STATUS_OK, - &buf, - ("Content-Type", "text/plain") - )!; + let tid = thread::spawn(&handle_req, serv_req)!; + thread::detach(tid)!; }; }; +fn handle_req(arg: nullable *opaque) void = { + let serv_req = arg: *http::server_request; + defer http::serve_finish(serv_req); + fmt::printfln("got a request. serving now.")!; + + let buf = memio::dynamic(); + defer io::close(&buf)!; + handlereq(&buf, &serv_req.request); + + http::response_write( + serv_req.socket, + http::STATUS_OK, + &buf, + ("Content-Type", "text/plain") + )!; +}; + export fn handlereq(buf: *io::stream, request: *http::request) void = { fmt::fprintfln(buf, "Method: {}", request.method)!; fmt::fprintfln(buf, "Path: {}", request.target.path)!; @@ -74,6 +84,8 @@ export fn handlereq(buf: *io::stream, request: *http::request) void = { http::write_header(buf, &request.header)!; fmt::fprintfln(buf, "EOF")!; + time::sleep(5 * time::SECOND); + match (request.body) { case void => void; case let body: io::handle =>