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,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 =>
|
||||
|
|
Loading…
Reference in a new issue