summaryrefslogtreecommitdiffstats
path: root/htgen.sh
blob: ff401c83d9049150ddf371b5a6d4d27e3980a006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! /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|vix: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