diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-15 01:16:39 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-15 01:16:39 +0800 |
commit | cb2517bee240c592c07e77c1507877cf47ace553 (patch) | |
tree | ea316415dfb1f29684c80e108762e4a66c937fb4 | |
parent | Add horrible patch needed to make net::uri work (diff) | |
download | forge-cb2517bee240c592c07e77c1507877cf47ace553.tar.gz forge-cb2517bee240c592c07e77c1507877cf47ace553.tar.zst forge-cb2517bee240c592c07e77c1507877cf47ace553.zip |
Separate paths into segments
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | main.ha | 7 | ||||
-rw-r--r-- | templates/index.htmpl | 8 | ||||
-rw-r--r-- | url.ha | 15 |
4 files changed, 28 insertions, 4 deletions
@@ -1,4 +1,4 @@ -forge: main.ha templates.ha +forge: templates.ha *.ha hare build -o $@ . templates.ha: templates/*.htmpl @@ -10,6 +10,7 @@ use net::dial; use net::http; use net::ip; use net::tcp; +use net::uri; use os; use memio; use io; @@ -58,8 +59,10 @@ export fn main() void = { }; }; -export fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem) = { +export fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem | net::uri::invalid) = { htmpl::write(conn, "HTTP/1.1 200 OK\r\n")?; htmpl::write(conn, "Content-Type: text/html\r\n\r\n")?; - tp_index(conn)?; + let segments = segments_from_path(request.target.raw_path)?; + defer free_segments(segments); + tp_index(conn, segments)?; }; diff --git a/templates/index.htmpl b/templates/index.htmpl index e67cc09..1b4dd8b 100644 --- a/templates/index.htmpl +++ b/templates/index.htmpl @@ -1,4 +1,4 @@ -{{ define tp_index(handle: io::handle) (void | io::error | nomem) }} +{{ define tp_index(handle: io::handle, segments: []str) (void | io::error | nomem) }} <!DOCTYPE html> <html lang="en"> <head> @@ -7,6 +7,12 @@ </head> <body> {{ render _tp_header(handle, "test", "test") }} +<h2>Path segments</h2> +<ul> + {{ for let s .. segments }} + <li>{{ s }}</li> + {{ end }} +</ul> <div class="padding-wrapper"> <table class="wide rounded"> <thead> @@ -0,0 +1,15 @@ +use strings; +use net::uri; + +fn segments_from_path(s: str) ([]str | nomem | net::uri::invalid) = { + let sp: []str = strings::split(s, "/")?; + for (let i = 1z; i < len(sp); i += 1) + sp[i - 1] = net::uri::percent_decode(sp[i])?; + return sp[.. len(sp) - 1]; +}; + +fn free_segments(ss: []str) void = { + for (let s .. ss) { + free(s); + }; +}; |