1
Fork 0

different error for wrong json

This commit is contained in:
Jan-Erik Rediger 2024-06-03 14:05:51 +02:00
parent b40508e224
commit 87aa4ed23d

View file

@ -20,6 +20,8 @@ use thread;
use time; use time;
type notfound = !void; type notfound = !void;
type badrequest = !void;
type http_err = !(notfound | badrequest);
const usage: [_]getopt::help = [ const usage: [_]getopt::help = [
"HTTP server", "HTTP server",
@ -84,11 +86,12 @@ fn handle_req(arg: nullable *opaque) void = {
match (route_request(&buf, serv_req)) { match (route_request(&buf, serv_req)) {
case void => void; case void => void;
case badrequest => handle_badrequest(&buf, serv_req);
case notfound => handle_notfound(&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; let request = &serv_req.request;
switch (request.method) { switch (request.method) {
case "GET" => 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 = { fn handle_notfound(buf: *io::stream, request: *http::server_request) void = {
fmt::fprintfln(buf, "not found")!;
http::response_write( http::response_write(
request.socket, request.socket,
http::STATUS_NOT_FOUND, 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; let request = serv_req.request;
if (request.target.path == "/v1/exec") { 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 request = serv_req.request;
let payload = match (request.body) { 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) { let payload = match (payload) {
case let obj: json::object => yield obj; case let obj: json::object => yield obj;
case => return notfound; case => return badrequest;
}; };
let files = match (json::get(&payload, "files")) { let files = match (json::get(&payload, "files")) {
case void => return notfound; case void => return badrequest;
case let j: *json::value => yield j; case let j: *json::value => yield j;
}; };
let files = match (*files) { let files = match (*files) {
case let o: json::object => yield o; case let o: json::object => yield o;
case => return notfound; case => return badrequest;
}; };
let code = match (json::get(&files, "")) { let code = match (json::get(&files, "")) {
case void => return notfound; case void => return badrequest;
case let j: *json::value => yield j; case let j: *json::value => yield j;
}; };
let code = match (*code) { let code = match (*code) {
case let c: str => yield c; case let c: str => yield c;
case => return notfound; case => return badrequest;
}; };
let (stdout, stderr) = run_code(code); let (stdout, stderr) = run_code(code);