summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@vix>2010-02-02 03:44:18 +0100
committertv <tv@vix>2010-02-02 03:44:18 +0100
commit7740117825f62a763be27a4656669d4a3debb6e0 (patch)
tree16208d832b0c08e0c134f6717614eba8d55cdc10
Initial commit.
-rwxr-xr-xhtgen.sh137
1 files changed, 137 insertions, 0 deletions
diff --git a/htgen.sh b/htgen.sh
new file mode 100755
index 0000000..c76283a
--- /dev/null
+++ b/htgen.sh
@@ -0,0 +1,137 @@
+#! /bin/sh
+set -euf
+
+
+
+Server='httpd-single/0.2'
+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
+ if test -z "$Request_Line" ; then
+ echo "# FAIL"
+ exit -23
+ fi >&2
+
+ # TODO Date: `date --utc`
+
+ ##
+ ## 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
+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
+ cat<<EOF
+HTTP/1.0 200 OK
+Connection: Close
+Content-Length: `du -b $body | cut -f1`
+Content-Type: image/png
+Server: $Server
+
+EOF
+ ;;
+ /|/index.html)
+ 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
+Content-Length: `du -b $body | cut -f1`
+
+EOF
+ ;;
+ *)
+ body_type=string
+ body="404 Not found: $Request_URI"
+ cat<<EOF
+HTTP/1.0 404 Not Found
+Content-Type: text/plain; charset=UTF-8
+Server: $Server
+Connection: Close
+Content-Length: ${#body}
+
+EOF
+ ;;
+ esac
+
+ case "$Method" in
+ GET)
+ case "$body_type" in
+ file) cat "$body" ;;
+ string) echo "$body" ;;
+ esac
+ ;;
+ esac
+ ;;
+ *) # Method
+ ;;
+ esac
+ ;;
+ *) # Host
+ ;;
+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.