]> git.plutz.net Git - cgilite/blob - cgilite.sh
send http status code when redirecting
[cgilite] / cgilite.sh
1 #!/bin/sh
2
3 # Copyright 2017 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 if [ "$REQUEST_METHOD" = POST -a "${HTTP_CONTENT_LENGTH:=${CONTENT_LENGTH:=0}}" -gt 0 ]; then
26   cgilite_post="$(head -c "$HTTP_CONTENT_LENGTH")"
27 fi
28
29 cgilite_count(){
30   case $1 in
31     GET)  printf %s "&${QUERY_STRING}";;
32     POST) printf %s "&${cgilite_post}";;
33     REF)  printf %s "&${HTTP_REFERER#*\?}";;
34   esac \
35   | grep -Eo '&'"$2"'=[^&]*' \
36   | wc -l
37 }
38
39 cgilite_value(){
40   printf "$(
41     case $1 in
42       GET)  printf %s "&${QUERY_STRING}";;
43       POST) printf %s "&${cgilite_post}";;
44       REF)  printf %s "&${HTTP_REFERER#*\?}";;
45     esac \
46     | grep -Eo '&'"$2"'=[^&]*' \
47     | sed -rn "${3:-1}"'{s;^[^=]+=;;; s;\+; ;g; s;\\;\\\\;g;
48       # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
49       s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
50       s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
51       s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
52       s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
53       s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
54       p}'
55   )"
56 }
57
58 GET(){ cgilite_value GET $@; }
59 GET_COUNT(){ cgilite_count GET $1; }
60
61 POST(){ cgilite_value POST $@; }
62 POST_COUNT(){ cgilite_count POST $1; }
63
64 REF(){ cgilite_value REF $@; }
65 REF_COUNT(){ cgilite_count REF $1; }
66
67 COOKIE(){
68   printf "$(
69     printf %s " ${HTTP_COOKIE}" \
70     | grep -Eo '[; ]'"$1"'=[^;]*' \
71     | sed -rn "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; s;\\;\\\\;g;
72       # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
73       s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
74       s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
75       s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
76       s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
77       s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
78       p}'
79   )"
80 }
81
82 HTML(){
83   # HTML Entity Coding
84   # Prints UTF-8 string as decimal Unicode Code Points
85   # Useful for escaping user input for use in HTML text and attributes
86   printf %s "$*" \
87   | hexdump -ve '/1 "%03o\n"' \
88   | while read n; do
89     case $n in
90       # bitbanging octal UTF-8 chains into singular 7 digit octal numbers
91       [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do
92       2??)    printf '%s' ${n#2};; # tail fragment, append 6 bit
93       3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start
94       34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start
95       35?) printf '01%s' ${n#35};; # 3 octet chain start, high
96       36?) printf '%s' ${n#36};;   # 4 octet (21 bit) chain start
97     esac
98   done \
99   | sed -r 's;.{7};&\n;g;' \
100   | while read n; do
101     printf '&#%d;' $((0$n))
102   done
103 }
104
105 URL(){
106   # Code every character in URL escape hex format
107   # except alphanumeric ascii
108
109   printf %s "$*" \
110   | hexdump -v -e '/1 ",%02X"' \
111   | tr , %
112 }
113
114 SET_COOKIE(){
115   case "$1" in
116     ''|0|session) expire='';;
117     [+-][0-9]*)   expire="$(date -R -d @$(($(date +%s) + $1)))";;
118     *)            expire="$(date -R -d "$1")";;
119   esac
120   cookie="$2"
121   
122   printf 'Set-Cookie: %s' "$cookie"
123   [ -n "$expire" ] && printf '; Expires=%s' "$expire" 
124   [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"
125   printf '\r\n'
126 }
127
128 REDIRECT(){
129   printf 'Status: 303 See Other\r\nLocation: %s\r\n\r\n' "$*"
130   exit 0
131 }