summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-03-15 00:47:35 +0100
committertv <tv@krebsco.de>2017-03-15 01:13:39 +0100
commitf6dce79e752e57909981cf455a82a8e48f732f62 (patch)
treebe336dbdeda49fb067876ea2fba63f838324dfaa
parent9cba8ab645c5b37d784c9ef54d64f2335a88a7ec (diff)
parent70cc941643ccd3824091443df008bfda7765a2f9 (diff)
import htgen-plain/1.0
-rwxr-xr-xhtgen-plain94
-rwxr-xr-xhtgen.sh178
-rwxr-xr-xhtgend8
3 files changed, 94 insertions, 186 deletions
diff --git a/htgen-plain b/htgen-plain
new file mode 100755
index 0000000..659576f
--- /dev/null
+++ b/htgen-plain
@@ -0,0 +1,94 @@
+#! /bin/sh
+# vim:set fdm=marker:
+set -euf
+
+Server='htgen-plain/1.0'
+
+## htgen --serve {{{
+case $# in
+ (1)
+ case "$1" in
+ (--serve)
+ HTGEN_HOST="${HTGEN_HOST-0.0.0.0}"
+ HTGEN_PORT="${HTGEN_PORT-42380}"
+ TCPSERVER_OPTS='-c 423 -t 2 -D'
+ echo "#### $Server $HTGEN_HOST:$HTGEN_PORT" >&2
+ exec tcpserver $TCPSERVER_OPTS "$HTGEN_HOST" "$HTGEN_PORT" "$0"
+ ## this should not happen...
+ exit 23
+ esac
+esac
+## }}}
+
+## reply_404 {{{
+reply_404() {
+ cat<<EOF
+HTTP/1.0 404 Not Found
+Content-Type: text/plain; charset=UTF-8
+Server: $Server
+Connection: close
+Content-Length: 0
+
+EOF
+}
+## }}}
+
+## date. {{{
+#date=`date --rfc-3339=ns`
+date="`date '+%Y-%m-%d %H:%M:%S'`"
+cat>&2<<EOF
+
+# ${TCPREMOTEHOST-}[$TCPREMOTEIP]:$TCPREMOTEPORT connected at $date over $PROTO
+EOF
+## }}}
+## Parse Request-Line. {{{
+read Request_Line
+eval "$(echo "$Request_Line" | sed -rn '
+ s^([a-zA-Z]+) ([a-zA-Z0-9?&+*/.-]+) HTTP/([0-9]+\.[0-9]+)\r$\
+ Request_Line="&"\
+ Method="\1"\
+ Request_URI="\2"\
+ HTTP_Version="\3"\
+ p;t;i\
+ Request_Line= # invalid request
+')"
+#echo "Request-Line: $Request_Line" >&2
+# cat>&2<<EOF
+# Request_Line='$Request_Line'
+# Method='$Method'
+# Request_URI='$Request_URI'
+# HTTP_Version='$HTTP_Version'
+# EOF
+
+## debug
+cat>&2<<EOF
+$Method $Request_URI HTTP/$HTTP_Version
+EOF
+## }}}
+## Parse HTTP-Headers. {{{
+HTTP_Headers="`sed -rn 's/ $//;/^$/q;p'`"
+
+## debug
+echo "$HTTP_Headers" | sed -r 's/.*/^&$/' >&2
+
+## Parse HTTP_Headers into Variables.
+eval "$(
+echo "$HTTP_Headers" | sed -r '
+ ## TODO concatenate lines
+ /^[^:]+-[^:]+:/s/^([a-zA-Z-]+):[[:space:]]*([^'\'']*)*$/\
+ x=`echo \1 | tr - _`; \
+ echo $x=\\'\'''\''\2'\''\\'\''\\;;\
+ echo HTTP_Header_Variables=\\\${HTTP_Header_Variables+\\\$HTTP_Header_Variables:}$x\\;;\
+ /;t
+ s/^([a-zA-Z]+):[[:space:]]*([^'\'']*)*$/\
+ echo \1=\\'\'''\''\2'\''\\'\''\\;;\
+ echo HTTP_Header_Variables=\\\${HTTP_Header_Variables+\\\$HTTP_Header_Variables:}\1\\;;\
+ /;t
+ s/^.*/# &/
+ '| sh)" >&2
+
+echo >&2
+## }}}
+
+reply_404
+exit
diff --git a/htgen.sh b/htgen.sh
deleted file mode 100755
index 3fa28d0..0000000
--- a/htgen.sh
+++ /dev/null
@@ -1,178 +0,0 @@
-#! /bin/sh
-set -euf
-
-
-# TODO Cache-Control [RFC 2616, Section 14.9]
-# TODO Accept-Encoding: gzip,deflate
-# TODO Keep-Alive: 300
-# TODO Connection: keep-alive
-# TODO /robots.txt
-
-Server='htgen'
-Request="$(sed 's/\r$//;/^$/q')"
-date="`date '+%Y-%m-%d %H:%M:%S'`"
-
-##
-## Parse request.
-##
-Request_Line=
-eval "$(echo "$Request" | sed -rn '
- 1{s^([a-zA-Z]+) ([a-zA-Z0-9?&+*/.-]+) HTTP/([0-9]+\.[0-9]+)$\
- Request_Line="&"\
- Method="\1"\
- Request_URI="\2"\
- HTTP_Version="\3"\
- p;t;i\
- Request_Line= # invalid request
- q
- }
- s/^Host: ([a-zA-Z0-9?+&/:.-]+)$/Host="\1"/p
-')"
-
-##
-## Log request.
-##
-# TODO real logging
-cat>&2<<EOF
-
-## $date, $TCPREMOTEIP:$TCPREMOTEPORT $Request_Line
-$Request
-EOF
-
-##
-## Handle request.
-##
-if test -z "$Request_Line" ; then
- echo "# FAIL"
- exit -23
-fi >&2
-
-# TODO Date: `date --utc`
-
-case "$Host" in
- src.mine.nu:42380|src.mine.nu|vix|$HOSTNAME:42380|localhost:42380)
- case "${Method-Invalid-Request}" in
- GET|HEAD)
- case "$Request_URI" in
- /dump.txt)
- body_type=file
- body=~/public_html$Request_URI
- status=200
- cat<<EOF
-HTTP/1.0 200 OK
-Connection: close
-Content-Length: `du -b $body | cut -f1`
-Content-Type: text/plain; charset=utf-8
-Server: $Server
-
-EOF
- ;;
- /style5.css)
- body_type=file
- body=~/public_html$Request_URI
- status=200
- cat<<EOF
-HTTP/1.0 200 OK
-Connection: close
-Content-Length: `du -b $body | cut -f1`
-Content-Type: text/css
-Server: $Server
-
-EOF
- ;;
- /favicon5.png|/favicon2.png)
- body_type=file
- body=~/public_html$Request_URI
- status=200
- cat<<EOF
-HTTP/1.0 200 OK
-Connection: close
-Content-Length: `du -b $body | cut -f1`
-Content-Type: image/png
-Server: $Server
-Cache-Control: public
-
-EOF
- ;;
- /|/index.html)
- make -C ~/public_html index.html >&2
- #### XXX this is a hack:
- ### make -C ~/public_html index.html >&2
- # TODO 304 Not Modified
- # TODO better max-age(?)
- # TODO may we use more than one Cache-Control?
- body_type=file
- body=~/public_html/index.html
- status=200
- cat<<EOF
-HTTP/1.0 200 OK
-Content-Type: text/html; charset=UTF-8
-Server: $Server
-Connection: close
-Content-Length: `du -b $body | cut -f1`
-Cache-Control: max-age=60
-
-EOF
- ;;
- *)
- body_type=string
- body="404 Not found: $Request_URI"
- status=404
- cat<<EOF
-HTTP/1.0 404 Not Found
-Content-Type: text/plain; charset=UTF-8
-Server: $Server
-Connection: close
-Content-Length: ${#body}
-
-EOF
- ;;
- esac
- ;;
- *) # Method
- body_type=string
- body="501 Not Implemented: $Method"
- status=501
- cat<<EOF
-HTTP/1.0 501 Not Implemented
-Content-Type: text/plain; charset=UTF-8
-Server: $Server
-Connection: close
-Content-Length: ${#body}
-Cache-Control: public
-
-EOF
- ;;
- esac
- ;;
- *) # Host
- body_type=string
- body="403 Forbidden"
- status=403
- cat<<EOF
-HTTP/1.1 403 Forbidden
-Content-Type: text/plain; charset=UTF-8
-Connection: close
-Content-Length: ${#body}
-Cache-Control: public
-
-EOF
- ;;
-esac
-
-##
-## Print message-body.
-##
-case "$Method" in
- GET)
- case "$body_type" in
- file) cat "$body" ;;
- string) echo -n "$body" ;;
- esac
- ;;
-esac
-
-
-# Success (mod transfer)
-echo "# OK: $status" >&2
-exit
diff --git a/htgend b/htgend
deleted file mode 100755
index 0e4c668..0000000
--- a/htgend
+++ /dev/null
@@ -1,8 +0,0 @@
-#! /bin/sh
-set -euf
-HTGEN_PROG="${HTGEN_SH-$(dirname "$(readlink -f "$0")")/htgen.sh}"
-HTGEN_HOST="${HTGEN_HOST-0.0.0.0}"
-HTGEN_PORT="${HTGEN_PORT-42380}"
-TCPSERVER_OPTS='-c 423 -t 2 -D'
-echo "#### htgend $HTGEN_HOST:$HTGEN_PORT" >&2
-exec tcpserver $TCPSERVER_OPTS "$HTGEN_HOST" "$HTGEN_PORT" "$HTGEN_PROG"