]> git.plutz.net Git - cgilite/blob - cgilite.sh
fix builtin header parsing
[cgilite] / cgilite.sh
1 #!/bin/sh
2
3 # Copyright 2017 - 2018 Paul Hänsch
4 #
5 # This is CGIlite.
6 # A collection of posix shell functions for writing CGI scripts.
7
8 # CGIlite is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # CGIlite is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with CGIlite.  If not, see <http://www.gnu.org/licenses/>. 
20
21 # ksh and zsh workaround
22 # set -o posix # ksh, not portable
23 setopt -o OCTAL_ZEROES 2>&-
24
25 BR="$(printf '\n')"
26 CR="$(printf '\r')"
27
28 HEADER(){
29   # Read value of header line. Use this instead of
30   # referencing HTTP_* environment variables.
31   if [ -n "${cgilite_headers+x}" ]; then
32     printf %s "$cgilite_headers" \
33     | sed -rn 's;^'"${1}"': ([^\r]+)\r?$;\1;i; tX; d; :X;p;q;'
34   else
35     eval "printf %s \"\$HTTP_$(printf %s "${1}" |tr a-z A-Z |tr -c A-Z _)\""
36   fi
37 }
38
39 HEX_DECODE(){
40   printf "$(printf %s "$1" \
41   | sed -r '
42     s;\\;\\\\;g; :x; s;%([^0-9A-F]);\\045\1;g; tx;
43     # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
44     s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
45     s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
46     s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
47     s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
48     s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
49   ')"
50 }
51
52 if [ "$1" = '--inetd' -a -z "$REQUEST_METHOD" ]; then
53   REMOTE_ADDR="${TCPREMOTEIP:-$NCAT_REMOTE_ADDR}"
54   SERVER_NAME="${TCPLOCALIP:-$NCAT_LOCAL_ADDR}"
55   SERVER_PORT="${TCPLOCALPORT:-$NCAT_LOCAL_PORT}"
56
57   read REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL
58   PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}")"
59   QUERY_STRING="${REQUEST_URI#*\?}"
60   cgilite_headers="$(sed -u '/^\r\?$/q')"
61
62   HTTP_CONTENT_LENGTH="$(HEADER Content-Length |grep -xE '[0-9]+')"
63   HTTP_COOKIE="$(HEADER Cookie)"
64
65   export REMOTE_ADDR SERVER_NAME SERVER_PORT REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL \
66          PATH_INFO QUERY_STRING HTTP_CONTENT_LENGTH HTTP_COOKIE
67
68   . "$0" |sed '1{s;^Status: ;HTTP/1.0 ;; t; iHTTP/1.0 200 OK\r
69              }'
70   exit $?
71 fi
72
73 if [ "$REQUEST_METHOD" = POST -a "${HTTP_CONTENT_LENGTH:=${CONTENT_LENGTH:=0}}" -gt 0 ]; then
74   cgilite_post="$(head -c "$HTTP_CONTENT_LENGTH")"
75 fi
76
77 [ -n "${DEBUG+x}" ] && env
78
79 cgilite_count(){
80   case $1 in
81     GET)  printf %s "&${QUERY_STRING}";;
82     POST) printf %s "&${cgilite_post}";;
83     REF)  printf %s "&${HTTP_REFERER#*\?}";;
84   esac \
85   | grep -Eo '&'"$2"'=[^&]*' \
86   | wc -l
87 }
88
89 cgilite_value(){
90   HEX_DECODE "$(
91     case $1 in
92       GET)  printf %s "&${QUERY_STRING}";;
93       POST) printf %s "&${cgilite_post}";;
94       REF)  printf %s "&${HTTP_REFERER#*\?}";;
95     esac \
96     | grep -Eo '&'"$2"'=[^&]*' \
97     | sed -rn "${3:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
98   )"
99 }
100
101 GET(){ cgilite_value GET $@; }
102 GET_COUNT(){ cgilite_count GET $1; }
103
104 POST(){ cgilite_value POST $@; }
105 POST_COUNT(){ cgilite_count POST $1; }
106
107 REF(){ cgilite_value REF $@; }
108 REF_COUNT(){ cgilite_count REF $1; }
109
110 COOKIE(){
111   HEX_DECODE "$(
112     HEADER Cookie \
113     | grep -oE '(^|; ?)'"$1"'=[^;]*' \
114     | sed -rn "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
115   )"
116 }
117
118 HTML(){
119   # HTML Entity Coding
120   # Prints UTF-8 string as decimal Unicode Code Points
121   # Useful for escaping user input for use in HTML text and attributes
122   printf %s "$*" \
123   | hexdump -ve '/1 "%03o\n"' \
124   | while read n; do
125     case $n in
126       # bitbanging octal UTF-8 chains into singular 7 digit octal numbers
127       [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do
128       2??)    printf '%s' ${n#2};; # tail fragment, append 6 bit
129       3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start
130       34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start
131       35?) printf '01%s' ${n#35};; # 3 octet chain start, high
132       36?) printf '%s' ${n#36};;   # 4 octet (21 bit) chain start
133     esac
134   done \
135   | sed -r 's;.{7};&\n;g;' \
136   | while read n; do
137     printf '&#%d;' $((0$n))
138   done
139 }
140
141 URL(){
142   # Code every character in URL escape hex format
143   # except alphanumeric ascii
144
145   printf %s "$*" \
146   | hexdump -v -e '/1 ",%02X"' \
147   | tr , %
148 }
149
150 SET_COOKIE(){
151   case "$1" in
152     ''|0|session) expire='';;
153     [+-][0-9]*)   expire="$(date -R -d @$(($(date +%s) + $1)))";;
154     *)            expire="$(date -R -d "$1")";;
155   esac
156   cookie="$2"
157   
158   printf 'Set-Cookie: %s' "$cookie"
159   [ -n "$expire" ] && printf '; Expires=%s' "$expire" 
160   [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"
161   printf '\r\n'
162 }
163
164 REDIRECT(){
165   printf 'Status: 303 See Other\r\nLocation: %s\r\n\r\n' "$*"
166   exit 0
167 }
168