1
Fork 0

handle files

This commit is contained in:
Jan-Erik Rediger 2024-06-01 16:33:00 +02:00
parent f4fae4b967
commit 341d395197
3 changed files with 227 additions and 16 deletions

View file

@ -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 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 strings;
use thread; use thread;
use time; use time;
use log::logfmt;
use log;
use encoding::json;
const usage: [_]getopt::help = [ const usage: [_]getopt::help = [
"HTTP server", "HTTP server",
@ -75,13 +77,12 @@ fn handle_req(arg: nullable *opaque) void = {
defer io::close(&buf)!; defer io::close(&buf)!;
let request = &serv_req.request; let request = &serv_req.request;
let route = (request.method, request.target.path);
switch (request.method) { switch (request.method) {
case "GET" => case "GET" =>
switch (request.target.path) { switch (request.target.path) {
case "/" => handle_index(&buf, serv_req); case "/" => handle_index(&buf, serv_req);
case => case =>
handle_notfound(&buf, serv_req); handle_file(&buf, serv_req);
return; return;
}; };
case "POST" => 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 = { export fn handle_notfound(buf: *io::stream, request: *http::server_request) void = {
fmt::fprintfln(buf, "not found")!; fmt::fprintfln(buf, "not found")!;
http::response_write( http::response_write(

60
backend/debug.sh Executable file
View file

@ -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)

102
backend/out.txt Normal file
View file

@ -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