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 =>