From 87aa4ed23d8c76d0ff2da56bbc5ac124eed2a4cb Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Mon, 3 Jun 2024 14:05:51 +0200 Subject: [PATCH] different error for wrong json --- cmd/httpd/main.ha | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/httpd/main.ha b/cmd/httpd/main.ha index fb18942..48f9ec3 100644 --- a/cmd/httpd/main.ha +++ b/cmd/httpd/main.ha @@ -20,6 +20,8 @@ use thread; use time; type notfound = !void; +type badrequest = !void; +type http_err = !(notfound | badrequest); const usage: [_]getopt::help = [ "HTTP server", @@ -84,11 +86,12 @@ fn handle_req(arg: nullable *opaque) void = { match (route_request(&buf, serv_req)) { case void => void; + case badrequest => handle_badrequest(&buf, serv_req); case notfound => handle_notfound(&buf, serv_req); }; }; -fn route_request(buf: *io::stream, serv_req: *http::server_request) (void | notfound) = { +fn route_request(buf: *io::stream, serv_req: *http::server_request) (void | http_err) = { let request = &serv_req.request; switch (request.method) { case "GET" => @@ -174,8 +177,7 @@ fn handle_file(buf: *io::stream, serv_req: *http::server_request) (void | notfou )!; }; -export fn handle_notfound(buf: *io::stream, request: *http::server_request) void = { - fmt::fprintfln(buf, "not found")!; +fn handle_notfound(buf: *io::stream, request: *http::server_request) void = { http::response_write( request.socket, http::STATUS_NOT_FOUND, @@ -184,7 +186,16 @@ export fn handle_notfound(buf: *io::stream, request: *http::server_request) void )!; }; -export fn handle_cors(buf: *io::stream, serv_req: *http::server_request) void = { +fn handle_badrequest(buf: *io::stream, request: *http::server_request) void = { + http::response_write( + request.socket, + http::STATUS_BAD_REQUEST, + buf, + ("Content-Type", "text/plain") + )!; +}; + +fn handle_cors(buf: *io::stream, serv_req: *http::server_request) void = { let request = serv_req.request; if (request.target.path == "/v1/exec") { @@ -209,7 +220,7 @@ export fn handle_cors(buf: *io::stream, serv_req: *http::server_request) void = )!; }; -fn handle_exec(buf: *io::stream, serv_req: *http::server_request) (void | notfound) = { +fn handle_exec(buf: *io::stream, serv_req: *http::server_request) (void | badrequest) = { let request = serv_req.request; let payload = match (request.body) { @@ -231,27 +242,27 @@ fn handle_exec(buf: *io::stream, serv_req: *http::server_request) (void | notfou let payload = match (payload) { case let obj: json::object => yield obj; - case => return notfound; + case => return badrequest; }; let files = match (json::get(&payload, "files")) { - case void => return notfound; + case void => return badrequest; case let j: *json::value => yield j; }; let files = match (*files) { case let o: json::object => yield o; - case => return notfound; + case => return badrequest; }; let code = match (json::get(&files, "")) { - case void => return notfound; + case void => return badrequest; case let j: *json::value => yield j; }; let code = match (*code) { case let c: str => yield c; - case => return notfound; + case => return badrequest; }; let (stdout, stderr) = run_code(code);