summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-03-16 19:16:11 +0100
committertv <tv@krebsco.de>2017-03-16 19:32:36 +0100
commit2b40d680fa9aabeabb6746be0caa56935d4ecb7f (patch)
tree9f03c75dbbc26366d1337932248c261dcb511cc1
parentd7b9e60202642254132059bc5f1d88e9a73770fc (diff)
support short URIs
-rwxr-xr-xhtgen19
1 files changed, 17 insertions, 2 deletions
diff --git a/htgen b/htgen
index 314ed52..20cbcf6 100755
--- a/htgen
+++ b/htgen
@@ -93,12 +93,23 @@ unset k v
echo >&2
## }}}
+find_item() {
+ if test ${#1} -ge 7; then
+ set -- "$(find "$STATEDIR/items" -type f -mindepth 1 -maxdepth 1 \
+ -regex "$STATEDIR/items/$1[0-9A-Za-z]*$")"
+ if test $(printf '%s' "$1" | wc -l) = 1; then
+ echo "$1"
+ return 0
+ fi
+ fi
+ return 1
+}
+
# run server: STATEDIR=/tmp/htgen-state HTGEN_PORT=1080 ./htgen --serve
# paste: echo lol | curl http://localhost:1080 -d @-
case "$Method $Request_URI" in
"GET /"[0-9a-z]*)
- item=$STATEDIR/items$Request_URI
- if test -e $item; then
+ if item=$(find_item ${Request_URI:1}); then
printf 'HTTP/1.1 200 OK\r\n'
printf 'Content-Type: %s\r\n' "$(file -ib $item)"
printf 'Server: %s\r\n' "$Server"
@@ -129,6 +140,10 @@ case "$Method $Request_URI" in
cp -v $content $item >&2
fi
+ if item=$(find_item ${base32:0:7}); then
+ ref=$(echo "$ref"; echo "http://$req_host/${base32:0:7}")
+ fi
+
printf 'HTTP/1.1 200 OK\r\n'
printf 'Content-Type: text/plain; charset=UTF-8\r\n'
printf 'Server: %s\r\n' "$Server"