serve a http request in a thread
This commit is contained in:
parent
6d95fd5fb8
commit
bcb3d9a56d
|
@ -9,6 +9,8 @@ use io;
|
|||
use fmt;
|
||||
use bufio;
|
||||
use strings;
|
||||
use thread;
|
||||
use time;
|
||||
|
||||
const usage: [_]getopt::help = [
|
||||
"HTTP server",
|
||||
|
@ -50,7 +52,16 @@ export fn main() void = {
|
|||
yield this;
|
||||
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);
|
||||
fmt::printfln("got a request. serving now.")!;
|
||||
|
||||
let buf = memio::dynamic();
|
||||
defer io::close(&buf)!;
|
||||
|
@ -62,7 +73,6 @@ export fn main() void = {
|
|||
&buf,
|
||||
("Content-Type", "text/plain")
|
||||
)!;
|
||||
};
|
||||
};
|
||||
|
||||
export fn handlereq(buf: *io::stream, request: *http::request) void = {
|
||||
|
@ -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 =>
|
||||
|
|
Loading…
Reference in a new issue