summaryrefslogtreecommitdiffstats
path: root/htgen
blob: 23e3b2a21f0360f7f36984389eb430c66d70d1b4 (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
#! /bin/sh
# vim:set fdm=marker:
set -euf

Server=htgen

## parse arguments {{{
action=handle_request
verbose=${HTGEN_VERBOSE-false}
while test $# -gt 0; do
  case $1 in)
    --serve) action=serve; shift; continue;;
    --verbose) verbose=true; shift; continue;;
    *) echo "$0: error: bad argument: $1" >&2; exit 1;;
  esac
done
## }}}
## dispatch on $action {{
case $action in
  handle_request)
    : # fall through for handling the request
    ;;
  serve)
    HTGEN_HOST=${HTGEN_HOST-0.0.0.0}
    HTGEN_PORT=${HTGEN_PORT-42380}
    HTGEN_SCRIPT=${HTGEN_SCRIPT-:}
    HTGEN_VERBOSE=$verbose; export HTGEN_VERBOSE
    TCPSERVER_OPTS='-c 423 -t 2 -D'
    echo "#### $Server $HTGEN_HOST:$HTGEN_PORT" >&2
    exec tcpserver $TCPSERVER_OPTS "$HTGEN_HOST" "$HTGEN_PORT" "$0"
    ;;
  *)
    echo "$0: error: bad action: $action" >&2
    exit 2
    ;;
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 '+%Y-%m-%d %H:%M:%S')
cat 1>&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 1>&2 <<EOF
$Method $Request_URI HTTP/$HTTP_Version
EOF
## }}}
## Parse HTTP-Headers. {{{
# TODO support https://tools.ietf.org/html/rfc7230#section-3.2.4, obs-fold?
# TODO allow headers to appear multiple times
k=
v=
while read -r line; do
  line=${line%
}
  case $line in
    '')
      break
      ;;
    [A-Za-z][0-9A-Za-z-]*:*)
      k=${line%%:*}
      k=$(echo "$k" | tr [:upper:]- [:lower:]_)
      k=req_$k
      v=${line#*:}
      v=${v## }
      if test "$HTGEN_VERBOSE" = true; then
        printf '%s=%s\n' "$k" "$v" >&2
      fi
      export "$k=$v"
      ;;
  esac
done
unset k v

echo >&2
## }}}

eval "$HTGEN_SCRIPT"

reply_404