2024-06-01 14:33:00 +00:00
|
|
|
use bufio;
|
|
|
|
use bytes;
|
|
|
|
use encoding::json;
|
|
|
|
use fmt;
|
2024-05-28 10:23:52 +00:00
|
|
|
use getopt;
|
2024-06-01 14:33:00 +00:00
|
|
|
use io;
|
|
|
|
use log;
|
|
|
|
use log::logfmt;
|
|
|
|
use memio;
|
2024-05-28 10:23:52 +00:00
|
|
|
use net;
|
|
|
|
use net::dial;
|
2024-06-01 14:33:00 +00:00
|
|
|
use net::http;
|
|
|
|
use net::ip;
|
2024-05-28 18:28:52 +00:00
|
|
|
use net::tcp::{reuseaddr};
|
2024-06-01 14:33:00 +00:00
|
|
|
use net::uri;
|
2024-05-28 10:23:52 +00:00
|
|
|
use os;
|
2024-06-01 14:33:00 +00:00
|
|
|
use path;
|
2024-05-28 10:23:52 +00:00
|
|
|
use strings;
|
2024-05-28 10:54:42 +00:00
|
|
|
use thread;
|
|
|
|
use time;
|
2024-05-28 10:23:52 +00:00
|
|
|
|
|
|
|
const usage: [_]getopt::help = [
|
|
|
|
"HTTP server",
|
|
|
|
('a', "address", "listened address (ex: 127.0.0.1:8080)")
|
|
|
|
];
|
|
|
|
|
|
|
|
export fn main() void = {
|
2024-05-28 18:28:52 +00:00
|
|
|
let l = logfmt::new(os::stdout);
|
|
|
|
log::setlogger(&l);
|
|
|
|
|
2024-05-28 10:23:52 +00:00
|
|
|
const cmd = getopt::parse(os::args, usage...);
|
|
|
|
defer getopt::finish(&cmd);
|
|
|
|
|
|
|
|
let port: u16 = 8080;
|
|
|
|
let ip_addr: ip::addr4 = [127, 0, 0, 1];
|
|
|
|
|
|
|
|
for (let i = 0z; i < len(cmd.opts); i += 1) {
|
|
|
|
const opt = cmd.opts[i];
|
|
|
|
switch (opt.0) {
|
|
|
|
case 'a' =>
|
|
|
|
match (dial::splitaddr(opt.1, "")) {
|
|
|
|
case let value: (str, u16) =>
|
|
|
|
ip_addr = ip::parsev4(value.0)!;
|
|
|
|
port = value.1;
|
|
|
|
case dial::invalid_address =>
|
|
|
|
abort("Invalid address");
|
|
|
|
};
|
|
|
|
case => abort(); // unreachable
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-28 18:28:52 +00:00
|
|
|
const server = match (http::listen(ip_addr, port, reuseaddr)) {
|
2024-05-28 10:23:52 +00:00
|
|
|
case let this: *http::server =>
|
|
|
|
yield this;
|
|
|
|
case net::error => abort("failure while listening");
|
|
|
|
};
|
|
|
|
defer http::server_finish(server);
|
|
|
|
|
|
|
|
for (true) {
|
|
|
|
const serv_req = match (http::serve(server)) {
|
|
|
|
case let this: *http::server_request =>
|
|
|
|
yield this;
|
|
|
|
case net::error => abort("failure while serving");
|
|
|
|
};
|
|
|
|
|
2024-05-28 18:28:52 +00:00
|
|
|
log::println("method", serv_req.request.method, "uri", uri::string(serv_req.request.target));
|
2024-05-28 10:54:42 +00:00
|
|
|
let tid = thread::spawn(&handle_req, serv_req)!;
|
|
|
|
thread::detach(tid)!;
|
2024-05-28 10:23:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-28 10:54:42 +00:00
|
|
|
fn handle_req(arg: nullable *opaque) void = {
|
|
|
|
let serv_req = arg: *http::server_request;
|
|
|
|
defer http::serve_finish(serv_req);
|
|
|
|
|
|
|
|
let buf = memio::dynamic();
|
|
|
|
defer io::close(&buf)!;
|
2024-05-28 18:28:52 +00:00
|
|
|
|
|
|
|
let request = &serv_req.request;
|
|
|
|
switch (request.method) {
|
|
|
|
case "GET" =>
|
|
|
|
switch (request.target.path) {
|
2024-05-28 18:50:45 +00:00
|
|
|
case "/" => handle_index(&buf, serv_req);
|
2024-05-28 18:28:52 +00:00
|
|
|
case =>
|
2024-06-01 14:33:00 +00:00
|
|
|
handle_file(&buf, serv_req);
|
2024-05-28 18:28:52 +00:00
|
|
|
return;
|
|
|
|
};
|
2024-05-28 18:47:57 +00:00
|
|
|
case "POST" =>
|
|
|
|
switch (request.target.path) {
|
|
|
|
case "/v1/exec" => handle_exec(&buf, serv_req);
|
|
|
|
case =>
|
|
|
|
handle_notfound(&buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
2024-05-28 18:28:52 +00:00
|
|
|
case "OPTIONS" =>
|
|
|
|
handle_cors(&buf, serv_req);
|
|
|
|
return;
|
|
|
|
case =>
|
|
|
|
handle_notfound(&buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
2024-05-28 10:54:42 +00:00
|
|
|
};
|
|
|
|
|
2024-06-01 14:39:49 +00:00
|
|
|
fn handle_index(buf: *io::stream, serv_req: *http::server_request) void = {
|
|
|
|
let request = serv_req.request;
|
|
|
|
|
|
|
|
let fp = os::open("index.html")!;
|
|
|
|
defer io::close(fp)!;
|
|
|
|
let filecontent = io::drain(fp)!;
|
|
|
|
defer free(filecontent);
|
|
|
|
|
|
|
|
io::writeall(buf, filecontent)!;
|
|
|
|
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_OK,
|
|
|
|
buf,
|
|
|
|
("Content-Type", "text/html; charset=utf-8")
|
|
|
|
)!;
|
|
|
|
};
|
|
|
|
|
|
|
|
fn handle_file(buf: *memio::stream, serv_req: *http::server_request) void = {
|
2024-06-01 14:33:00 +00:00
|
|
|
let request = &serv_req.request;
|
|
|
|
|
|
|
|
let pathbuf = strings::toutf8(request.target.path);
|
|
|
|
if (bytes::contains(pathbuf[1..], '/')) {
|
|
|
|
fmt::printfln("additional slash. not handling that.")!;
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let p = path::init(request.target.path)!;
|
|
|
|
let ext = path::peek_ext(&p);
|
|
|
|
|
|
|
|
if (ext is void) {
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let content_type = switch (ext: str) {
|
|
|
|
case "html" => yield "text/html; charset=utf-8";
|
|
|
|
case "css" => yield "text/css";
|
|
|
|
case "js" => yield "application/javascript; charset=utf-8";
|
|
|
|
case =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let filename = path::basename(request.target.path);
|
2024-06-01 14:39:49 +00:00
|
|
|
let fp = match (os::open(filename)) {
|
|
|
|
case let fp: io::file => yield fp;
|
|
|
|
case =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
2024-06-01 14:33:00 +00:00
|
|
|
defer io::close(fp)!;
|
|
|
|
let filecontent = io::drain(fp)!;
|
|
|
|
defer free(filecontent);
|
|
|
|
|
|
|
|
io::writeall(buf, filecontent)!;
|
|
|
|
|
|
|
|
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_OK,
|
|
|
|
buf,
|
|
|
|
("Content-Type", content_type)
|
|
|
|
)!;
|
|
|
|
};
|
|
|
|
|
2024-05-28 18:28:52 +00:00
|
|
|
export fn handle_notfound(buf: *io::stream, request: *http::server_request) void = {
|
|
|
|
fmt::fprintfln(buf, "not found")!;
|
|
|
|
http::response_write(
|
|
|
|
request.socket,
|
|
|
|
http::STATUS_NOT_FOUND,
|
|
|
|
buf,
|
|
|
|
("Content-Type", "text/plain")
|
|
|
|
)!;
|
|
|
|
};
|
|
|
|
|
|
|
|
export fn handle_cors(buf: *io::stream, serv_req: *http::server_request) void = {
|
|
|
|
let request = serv_req.request;
|
|
|
|
|
|
|
|
if (request.target.path == "/v1/exec") {
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_OK,
|
|
|
|
void,
|
|
|
|
("Content-Length", "0"),
|
|
|
|
("access-control-allow-origin", "*"),
|
2024-05-28 18:47:57 +00:00
|
|
|
("access-control-allow-methods", "options, post"),
|
|
|
|
("access-control-allow-headers", "authorization, content-type")
|
2024-05-28 18:28:52 +00:00
|
|
|
)!;
|
|
|
|
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_METHOD_NOT_ALLOWED,
|
|
|
|
void,
|
|
|
|
("Content-Length", "0")
|
|
|
|
)!;
|
|
|
|
};
|
|
|
|
|
2024-05-28 18:47:57 +00:00
|
|
|
export fn handle_exec(buf: *io::stream, serv_req: *http::server_request) void = {
|
|
|
|
let request = serv_req.request;
|
|
|
|
|
|
|
|
let payload = match (request.body) {
|
|
|
|
case void =>
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_BAD_REQUEST,
|
|
|
|
void,
|
|
|
|
("Content-Length", "0")
|
|
|
|
)!;
|
|
|
|
return;
|
|
|
|
case let body: io::handle =>
|
|
|
|
let payload = json::load(body)!;
|
2024-06-01 13:36:43 +00:00
|
|
|
// defer json::finish(payload);
|
2024-05-28 18:47:57 +00:00
|
|
|
yield payload;
|
|
|
|
};
|
|
|
|
|
2024-06-01 13:36:43 +00:00
|
|
|
defer json::finish(payload);
|
|
|
|
|
|
|
|
let payload = match (payload) {
|
|
|
|
case let obj: json::object => yield obj;
|
|
|
|
case =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let files = match (json::get(&payload, "files")) {
|
|
|
|
case void =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
case let j: *json::value => yield j;
|
|
|
|
};
|
|
|
|
|
|
|
|
let files = match (*files) {
|
|
|
|
case let o: json::object => yield o;
|
|
|
|
case =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let code = match (json::get(&files, "")) {
|
|
|
|
case void =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
case let j: *json::value => yield j;
|
|
|
|
};
|
|
|
|
|
|
|
|
let code = match (*code) {
|
|
|
|
case let c: str => yield c;
|
|
|
|
case =>
|
|
|
|
handle_notfound(buf, serv_req);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let (stdout, stderr) = run_code(code);
|
|
|
|
defer free(stderr);
|
|
|
|
defer free(stdout);
|
|
|
|
|
|
|
|
let obj = json::newobject();
|
|
|
|
defer json::finish(obj);
|
|
|
|
json::set(&obj, "stdout", stdout);
|
|
|
|
json::set(&obj, "stderr", stderr);
|
|
|
|
json::set(&obj, "id", "123");
|
|
|
|
json::set(&obj, "ok", true);
|
|
|
|
json::set(&obj, "duration", 0.0);
|
|
|
|
|
|
|
|
json::dump(buf, obj)!;
|
2024-05-28 18:47:57 +00:00
|
|
|
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_OK,
|
|
|
|
buf,
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
("access-control-allow-origin", "*"),
|
|
|
|
("access-control-allow-methods", "options, post"),
|
|
|
|
("access-control-allow-headers", "authorization, content-type")
|
|
|
|
)!;
|
|
|
|
};
|