summaryrefslogtreecommitdiffstats
path: root/htgen.sh
blob: c76283aa2a68d7571384e7ac64ef26eccc1de9f8 (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
#! /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.