summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xhtgen.sh116
-rwxr-xr-xhtgend3
2 files changed, 69 insertions, 50 deletions
diff --git a/htgen.sh b/htgen.sh
index c76283a..262e81b 100755
--- a/htgen.sh
+++ b/htgen.sh
@@ -2,8 +2,12 @@
set -euf
+# TODO Cache-Control [RFC 2616, Section 14.9]
+# TODO Accept-Encoding: gzip,deflate
+# TODO Keep-Alive: 300
+# TODO Connection: keep-alive
-Server='httpd-single/0.2'
+Server='htgen'
Request="$(sed 's/\r$//;/^$/q')"
date="`date '+%Y-%m-%d %H:%M:%S'`"
@@ -33,27 +37,28 @@ cat>&2<<EOF
## $date, $TCPREMOTEIP:$TCPREMOTEPORT $Request_Line
$Request
EOF
- if test -z "$Request_Line" ; then
- echo "# FAIL"
- exit -23
- fi >&2
-
- # TODO Date: `date --utc`
+
+##
+## Handle request.
+##
+if test -z "$Request_Line" ; then
+ echo "# FAIL"
+ exit -23
+fi >&2
- ##
- ## Handle request.
- ##
- case "$Host" in
- src.mine.nu:42380|src.mine.nu|vix|vix:42380|localhost:42380)
- case "${Method-Invalid-Request}" in
- GET|HEAD)
- case "$Request_URI" in
- /style5.css)
- body_type=file
- body=~/public_html$Request_URI
- cat<<EOF
+# TODO Date: `date --utc`
+
+case "$Host" in
+ src.mine.nu:42380|src.mine.nu|vix|vix:42380|localhost:42380)
+ case "${Method-Invalid-Request}" in
+ GET|HEAD)
+ case "$Request_URI" in
+ /style5.css)
+ body_type=file
+ body=~/public_html$Request_URI
+ cat<<EOF
HTTP/1.0 200 OK
-Connection: Close
+Connection: close
Content-Length: `du -b $body | cut -f1`
Content-Type: text/css
Server: $Server
@@ -65,22 +70,27 @@ EOF
body=~/public_html$Request_URI
cat<<EOF
HTTP/1.0 200 OK
-Connection: Close
+Connection: close
Content-Length: `du -b $body | cut -f1`
Content-Type: image/png
Server: $Server
+Cache-Control: public
EOF
;;
/|/index.html)
+ # 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
cat<<EOF
HTTP/1.0 200 OK
Content-Type: text/html; charset=UTF-8
Server: $Server
-Connection: Close
+Connection: close
Content-Length: `du -b $body | cut -f1`
+Cache-Control: max-age=60
EOF
;;
@@ -91,47 +101,55 @@ EOF
HTTP/1.0 404 Not Found
Content-Type: text/plain; charset=UTF-8
Server: $Server
-Connection: Close
+Connection: close
Content-Length: ${#body}
EOF
;;
esac
-
- case "$Method" in
- GET)
- case "$body_type" in
- file) cat "$body" ;;
- string) echo "$body" ;;
- esac
- ;;
- esac
;;
*) # Method
+ body_type=string
+ body="501 Not Implemented: $Method"
+ 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"
+ 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" >&2
exit
-
-# tcpserver info:
-# $PROTO is the string TCP.
-#
-# $TCPLOCALIP is the IP address of the local host, in dotted-decimal form.
-#
-# $TCPLOCALPORT is the local TCP port number, in decimal.
-#
-# $TCPLOCALHOST is the name listed in DNS for the local host. If no name is available, $TCPLOCALHOST is not set. Beware that $TCPLOCALHOST can contain arbitrary characters.
-#
-# $TCPREMOTEIP is the IP address of the remote host, in dotted-decimal form.
-#
-# $TCPREMOTEPORT is the remote TCP port number, in decimal.
-#
-# $TCPREMOTEHOST is the name listed in DNS for the remote host. If no name is available, $TCPREMOTEHOST is not set. Beware that $TCPREMOTEHOST can contain arbitrary characters.
-#
-# $TCPREMOTEINFO is a connection-specific string supplied by the remote host via the 931/1413/IDENT/TAP protocol. If no information is available, $TCPREMOTEINFO is not set. Beware that $TCPREMOTEINFO can contain arbitrary characters.
diff --git a/htgend b/htgend
index 9f3ba6f..0e4c668 100755
--- a/htgend
+++ b/htgend
@@ -1,7 +1,8 @@
#! /bin/sh
set -euf
HTGEN_PROG="${HTGEN_SH-$(dirname "$(readlink -f "$0")")/htgen.sh}"
-HTGEN_HOST="${HTGEN_HOST-0}"
+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"