1
Fork 0

serve a http request in a thread

This commit is contained in:
Jan-Erik Rediger 2024-05-28 12:54:42 +02:00
parent 6d95fd5fb8
commit bcb3d9a56d

View file

@ -9,6 +9,8 @@ use io;
use fmt; use fmt;
use bufio; use bufio;
use strings; use strings;
use thread;
use time;
const usage: [_]getopt::help = [ const usage: [_]getopt::help = [
"HTTP server", "HTTP server",
@ -50,7 +52,16 @@ export fn main() void = {
yield this; yield this;
case net::error => abort("failure while serving"); case net::error => abort("failure while serving");
}; };
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); defer http::serve_finish(serv_req);
fmt::printfln("got a request. serving now.")!;
let buf = memio::dynamic(); let buf = memio::dynamic();
defer io::close(&buf)!; defer io::close(&buf)!;
@ -63,7 +74,6 @@ export fn main() void = {
("Content-Type", "text/plain") ("Content-Type", "text/plain")
)!; )!;
}; };
};
export fn handlereq(buf: *io::stream, request: *http::request) void = { export fn handlereq(buf: *io::stream, request: *http::request) void = {
fmt::fprintfln(buf, "Method: {}", request.method)!; fmt::fprintfln(buf, "Method: {}", request.method)!;
@ -74,6 +84,8 @@ export fn handlereq(buf: *io::stream, request: *http::request) void = {
http::write_header(buf, &request.header)!; http::write_header(buf, &request.header)!;
fmt::fprintfln(buf, "EOF")!; fmt::fprintfln(buf, "EOF")!;
time::sleep(5 * time::SECOND);
match (request.body) { match (request.body) {
case void => void; case void => void;
case let body: io::handle => case let body: io::handle =>