summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-03-16 20:58:17 +0100
committertv <tv@krebsco.de>2017-03-16 21:48:11 +0100
commit86c8027ed9073bcb249fb2cfbb49a813db5e9022 (patch)
tree77238dd4609821a907fa8e4e83a26b34520f0198
parent2b40d680fa9aabeabb6746be0caa56935d4ecb7f (diff)
move paste code into examples/
-rw-r--r--examples/paste60
-rwxr-xr-xhtgen63
2 files changed, 60 insertions, 63 deletions
diff --git a/examples/paste b/examples/paste
new file mode 100644
index 0000000..79d48aa
--- /dev/null
+++ b/examples/paste
@@ -0,0 +1,60 @@
+find_item() {
+ if test ${#1} -ge 7; then
+ set -- "$(find "$STATEDIR/items" -mindepth 1 -maxdepth 1 \
+ -regex "$STATEDIR/items/$1[0-9A-Za-z]*$")"
+ if test -n "$1" && test $(echo "$1" | wc -l) = 1; then
+ echo "$1"
+ return 0
+ fi
+ fi
+ return 1
+}
+
+case "$Method $Request_URI" in
+ "GET /"[0-9a-z]*)
+ 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"
+ 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
+
+ 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"
+ printf 'Connection: close\r\n'
+ printf 'Content-Length: %d\r\n' $(expr ${#ref} + 1)
+ printf '\r\n'
+ printf '%s\n' "$ref"
+
+ exit
+ ;;
+esac
diff --git a/htgen b/htgen
index 20cbcf6..2a45b10 100755
--- a/htgen
+++ b/htgen
@@ -93,68 +93,5 @@ 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]*)
- 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"
- 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
-
- 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"
- 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