From 2b40d680fa9aabeabb6746be0caa56935d4ecb7f Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 16 Mar 2017 19:16:11 +0100 Subject: support short URIs --- htgen | 19 +++++++++++++++++-- 1 file 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" -- cgit v1.2.3