1
Fork 0

handle request in function

This commit is contained in:
Jan-Erik Rediger 2024-05-28 20:50:45 +02:00
parent cf699ad928
commit fc2f0d0dd2

View file

@ -79,7 +79,7 @@ fn handle_req(arg: nullable *opaque) void = {
switch (request.method) { switch (request.method) {
case "GET" => case "GET" =>
switch (request.target.path) { switch (request.target.path) {
case "/" => handle_index(&buf, &serv_req.request); case "/" => handle_index(&buf, serv_req);
case => case =>
handle_notfound(&buf, serv_req); handle_notfound(&buf, serv_req);
return; return;
@ -98,13 +98,6 @@ fn handle_req(arg: nullable *opaque) void = {
handle_notfound(&buf, serv_req); handle_notfound(&buf, serv_req);
return; return;
}; };
http::response_write(
serv_req.socket,
http::STATUS_OK,
&buf,
("Content-Type", "text/plain")
)!;
}; };
export fn handle_notfound(buf: *io::stream, request: *http::server_request) void = { export fn handle_notfound(buf: *io::stream, request: *http::server_request) void = {
@ -172,7 +165,9 @@ export fn handle_exec(buf: *io::stream, serv_req: *http::server_request) void =
)!; )!;
}; };
export fn handle_index(buf: *io::stream, request: *http::request) void = { export fn handle_index(buf: *io::stream, serv_req: *http::server_request) void = {
let request = serv_req.request;
fmt::fprintfln(buf, "Method: {}", request.method)!; fmt::fprintfln(buf, "Method: {}", request.method)!;
fmt::fprintfln(buf, "Path: {}", request.target.path)!; fmt::fprintfln(buf, "Path: {}", request.target.path)!;
fmt::fprintfln(buf, "Fragment: {}", request.target.fragment)!; fmt::fprintfln(buf, "Fragment: {}", request.target.fragment)!;
@ -196,4 +191,11 @@ export fn handle_index(buf: *io::stream, request: *http::request) void = {
}; };
fmt::fprintfln(buf, "EOF")!; fmt::fprintfln(buf, "EOF")!;
}; };
http::response_write(
serv_req.socket,
http::STATUS_OK,
buf,
("Content-Type", "text/plain")
)!;
}; };