From 341d3951973db058df5a560871dfd93acd70bc1c Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Sat, 1 Jun 2024 16:33:00 +0200 Subject: [PATCH] handle files --- backend/cmd/httpd/main.ha | 81 ++++++++++++++++++++++++------ backend/debug.sh | 60 ++++++++++++++++++++++ backend/out.txt | 102 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 227 insertions(+), 16 deletions(-) create mode 100755 backend/debug.sh create mode 100644 backend/out.txt diff --git a/backend/cmd/httpd/main.ha b/backend/cmd/httpd/main.ha index 74216f5..68c879f 100644 --- a/backend/cmd/httpd/main.ha +++ b/backend/cmd/httpd/main.ha @@ -1,21 +1,23 @@ -use getopt; -use net; -use net::ip; -use net::uri; -use net::http; -use net::dial; -use net::tcp::{reuseaddr}; -use os; -use memio; -use io; -use fmt; use bufio; +use bytes; +use encoding::json; +use fmt; +use getopt; +use io; +use log; +use log::logfmt; +use memio; +use net; +use net::dial; +use net::http; +use net::ip; +use net::tcp::{reuseaddr}; +use net::uri; +use os; +use path; use strings; use thread; use time; -use log::logfmt; -use log; -use encoding::json; const usage: [_]getopt::help = [ "HTTP server", @@ -75,13 +77,12 @@ fn handle_req(arg: nullable *opaque) void = { defer io::close(&buf)!; let request = &serv_req.request; - let route = (request.method, request.target.path); switch (request.method) { case "GET" => switch (request.target.path) { case "/" => handle_index(&buf, serv_req); case => - handle_notfound(&buf, serv_req); + handle_file(&buf, serv_req); return; }; case "POST" => @@ -100,6 +101,54 @@ fn handle_req(arg: nullable *opaque) void = { }; }; +export fn handle_file(buf: *memio::stream, serv_req: *http::server_request) void = { + let request = &serv_req.request; + + fmt::printfln("handling file for path: {}", request.target.path)!; + 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) { + fmt::printfln("can't find ext. bye.")!; + 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 => + fmt::printfln("unknown ext: {}", ext)!; + handle_notfound(buf, serv_req); + return; + }; + + let filename = path::basename(request.target.path); + fmt::printfln("filename: {}", filename)!; + let fp = os::open(filename)!; + 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) + )!; +}; + export fn handle_notfound(buf: *io::stream, request: *http::server_request) void = { fmt::fprintfln(buf, "not found")!; http::response_write( diff --git a/backend/debug.sh b/backend/debug.sh new file mode 100755 index 0000000..a6f7ab5 --- /dev/null +++ b/backend/debug.sh @@ -0,0 +1,60 @@ +set -xeuo pipefail + +(exec bwrap \ +--ro-bind \ +/usr \ +/usr \ +--dir \ +/tmp \ +--dir \ +/var \ +--symlink \ +../tmp \ +/var/tmp \ +--proc \ +/proc \ +--dev \ +/dev \ +--ro-bind \ +/etc/resolv.conf \ +/etc/resolv.conf \ +--ro-bind \ +/nix \ +/nix \ +--symlink \ +usr/lib \ +/lib \ +--symlink \ +usr/lib64 /lib64 \ +--symlink \ +usr/bin \ +/bin \ +--symlink \ +usr/sbin \ +/sbin \ +--chdir \ +/app \ +--unshare-all \ +--die-with-parent \ +--clearenv \ +--bind \ +/run/user/1000/.tmpeQrbnA \ +/app \ +--dir \ +/run/user/1000 \ +--setenv \ +HOME \ +/run/user/1000/home \ +--setenv \ +XDG_RUNTIME_DIR \ +/run/user/1000 \ +--file \ +11 \ +/etc/passwd \ +--file \ +12 \ +/etc/group \ +/bin/bash \ +) \ + 11< <(getent passwd $UID 65534) \ + 12< <(getent group $(id -g) 65534) diff --git a/backend/out.txt b/backend/out.txt new file mode 100644 index 0000000..d7002d9 --- /dev/null +++ b/backend/out.txt @@ -0,0 +1,102 @@ +ok: true +duration: 463 +stdout: +Hello, world! +¡Hola Mundo! +Γειά σου Κόσμε! +Привіт, світ! +こんにちは世界! + +stderr: +harec bytes +harec errors +harec linux +harec types::c +harec math +qbe rt +qbe types +qbe encoding::utf8 +qbe sort::cmp +qbe format::elf +qbe endian +qbe types::c +as types +as sort::cmp +as format::elf +as encoding::utf8 +as types::c +as endian +qbe errors +harec linux::vdso +qbe linux +as errors +harec io +harec strings +qbe bytes +as linux +as bytes +qbe linux::vdso +as linux::vdso +harec time +harec sort +qbe math +harec path +harec ascii +qbe strings +harec bufio +harec memio +qbe io +qbe sort +as strings +qbe time +qbe ascii +harec fs +qbe path +as sort +as io +as time +as rt +as math +as ascii +harec strconv +qbe memio +qbe bufio +as memio +as path +as bufio +harec os +harec unix +qbe fs +as fs +qbe strconv +harec unix::signal +qbe unix +as unix +harec fmt +qbe os +qbe unix::signal +as unix::signal +as os +as strconv +harec os::exec +harec encoding::hex +harec /app/main.ha +qbe fmt +as fmt +qbe /app/main.ha +as /app/main.ha +qbe encoding::hex +as encoding::hex +harec debug::image +qbe os::exec +as os::exec +harec debug::dwarf +qbe debug::image +as debug::image +harec debug +qbe debug::dwarf +as debug::dwarf +qbe debug +as debug +ld /app/main.ha +