1
Fork 0

simply json unwrappign

This commit is contained in:
Jan-Erik Rediger 2024-06-03 14:09:37 +02:00
parent 87aa4ed23d
commit 7af826ea13

View file

@ -86,7 +86,7 @@ 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 badrequest => handle_badrequest(serv_req);
case notfound => handle_notfound(&buf, serv_req); case notfound => handle_notfound(&buf, serv_req);
}; };
}; };
@ -186,12 +186,12 @@ fn handle_notfound(buf: *io::stream, request: *http::server_request) void = {
)!; )!;
}; };
fn handle_badrequest(buf: *io::stream, request: *http::server_request) void = { fn handle_badrequest(request: *http::server_request) void = {
http::response_write( http::response_write(
request.socket, request.socket,
http::STATUS_BAD_REQUEST, http::STATUS_BAD_REQUEST,
buf, void,
("Content-Type", "text/plain") ("Content-Length", "0")
)!; )!;
}; };
@ -220,21 +220,20 @@ fn handle_cors(buf: *io::stream, serv_req: *http::server_request) void = {
)!; )!;
}; };
fn json_wrap(res: (*json::value | void)) (*json::value | badrequest) = {
return match (res) {
case let payload: *json::value => return payload;
case void => return badrequest;
};
};
fn handle_exec(buf: *io::stream, serv_req: *http::server_request) (void | badrequest) = { 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) {
case void => case void => return badrequest;
http::response_write(
serv_req.socket,
http::STATUS_BAD_REQUEST,
void,
("Content-Length", "0")
)!;
return;
case let body: io::handle => case let body: io::handle =>
let payload = json::load(body)!; let payload = json::load(body)!;
// defer json::finish(payload);
yield payload; yield payload;
}; };
@ -245,20 +244,14 @@ fn handle_exec(buf: *io::stream, serv_req: *http::server_request) (void | badreq
case => return badrequest; case => return badrequest;
}; };
let files = match (json::get(&payload, "files")) { let files = json_wrap(json::get(&payload, "files"))?;
case void => return badrequest;
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 badrequest; case => return badrequest;
}; };
let code = match (json::get(&files, "")) { let code = json_wrap(json::get(&files, ""))?;
case void => return badrequest;
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;