summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-03-15 12:48:31 +0100
committertv <tv@krebsco.de>2017-03-15 12:48:31 +0100
commitd7b9e60202642254132059bc5f1d88e9a73770fc (patch)
tree00e8090e3a8501ea2128283d46e59c76b544f687
parentc5165ad7774af1ead36f0b28446882c09bb0ec33 (diff)
simple paste server example
-rwxr-xr-xhtgen48
1 files changed, 48 insertions, 0 deletions
diff --git a/htgen b/htgen
index 2a45b10..314ed52 100755
--- a/htgen
+++ b/htgen
@@ -93,5 +93,53 @@ unset k v
echo >&2
## }}}
+# 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
+ printf 'HTTP/1.1 200 OK\r\n'
+ printf 'Content-Type: %s\r\n' "$(file -ib $item)"
+ printf 'Server: %s\r\n' "$Server"
+ printf 'Connection: close\r\n'
+ printf 'Content-Length: %d\r\n' $(wc -c < $item)
+ printf '\r\n'
+ cat $item
+ exit
+ fi
+ ;;
+ "POST /")
+ content=$(mktemp -t htgen.$$.content.XXXXXXXX)
+ trap "rm $content >&2" EXIT
+
+ case ${req_expect-} in 100-continue)
+ printf 'HTTP/1.1 100 Continue\r\n\r\n'
+ esac
+
+ head -c $req_content_length > $content
+
+ sha256=$(sha256sum -b $content | cut -d\ -f1)
+ base32=$(nix-hash --to-base32 --type sha256 $sha256)
+ item=$STATEDIR/items/$base32
+ ref=http://$req_host/$base32
+
+ if ! test -e $item; then
+ mkdir -v -p $STATEDIR/items >&2
+ cp -v $content $item >&2
+ 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"
+ printf 'Connection: close\r\n'
+ printf 'Content-Length: %d\r\n' $(expr ${#ref} + 1)
+ printf '\r\n'
+ printf '%s\n' "$ref"
+
+ exit
+ ;;
+esac
+
reply_404
exit