[ UP ] cgi
[archserver.git] / cgi / proccgi.sh
blob47ce187f86c7e2e4bad820d6f313fff337ff8585
1 #!/bin/sh
3 # Process input to a CGI script. Written and Copyright 1995 Frank Pilhofer
4 # You may freely use and distribute this code free of charge provided that
5 # this copyright notice remains. fp@informatik.uni-frankfurt.de
7 # All variables in here are prefixed by _F_, so you shouldn't have
8 # any conflicts with your own var names
10 # get query string. if $REQUEST_METHOD is "POST", then it must be read
11 # from stdin, else it's in $QUERY_STRING
14 if [ ${DEBUG:-0} -eq 1 ] ; then
15 echo --Program Starts-- 1>&2
18 if [ "$REQUEST_METHOD" = "POST" ] ; then
19 _F_QUERY_STRING=`dd count=$CONTENT_LENGTH bs=1 2> /dev/null`"&"
20 if [ "$QUERY_STRING" != "" ] ; then
21 _F_QUERY_STRING="$_F_QUERY_STRING""$QUERY_STRING""&"
23 if [ ${DEBUG:-0} -eq 1 ] ; then
24 echo --Posted String-- 1>&2
26 else
27 _F_QUERY_STRING="$QUERY_STRING""&"
28 if [ ${DEBUG:-0} -eq 1 ] ; then
29 echo --Query String-- 1>&2
32 if [ ${DEBUG:-0} -eq 1 ] ; then
33 ( echo " " $_F_QUERY_STRING
34 echo --Adding Arguments-- ) 1>&2
37 # if there are arguments, use them as well.
39 for _F_PAR in $* ; do
40 _F_QUERY_STRING="$_F_QUERY_STRING""$_F_PAR""&"
41 if [ ${DEBUG:-0} -eq 1 ] ; then
42 echo " " arg $_F_PAR 1>&2
44 done
45 if [ ${DEBUG:-0} -eq 1 ] ; then
46 ( echo --With Added Arguments--
47 echo " " $_F_QUERY_STRING ) 1>&2
50 # if $PATH_INFO is not empty and contains definitions '=', append it as well.
51 # but replace slashes by ampersands
53 if echo $PATH_INFO | grep = > /dev/null ; then
54 _F_PATH_INFO="$PATH_INFO""//"
55 if [ ${DEBUG:-0} -eq 1 ] ; then
56 ( echo --Adding Path Info--
57 echo " " $_F_PATH_INFO ) 1>&2
60 while [ "$_F_PATH_INFO" != "" -a "$_F_PATH_INFO" != "/" ] ; do
61 _F_QUERY_STRING="$_F_QUERY_STRING""`echo $_F_PATH_INFO | cut -d / -f 1`""&"
62 _F_PATH_INFO=`echo $_F_PATH_INFO | cut -s -d / -f 2-`
63 done
66 # append another '&' to fool some braindead cut implementations. Test yours:
67 # echo 'i am braindead!' | cut -d '!' -f 2
69 _F_QUERY_STRING="$_F_QUERY_STRING""&"
71 if [ ${DEBUG:-0} -eq 1 ] ; then
72 ( echo --Final Query String--
73 echo " " $_F_QUERY_STRING ) 1>&2
76 while [ "$_F_QUERY_STRING" != "" -a "$_F_QUERY_STRING" != "&" ] ; do
77 _F_VARDEF=`echo $_F_QUERY_STRING | cut -d \& -f 1`
78 # _F_QUERY_STRING=`echo $_F_QUERY_STRING | cut -d \& -f 2-`
79 _F_VAR=`echo $_F_VARDEF | cut -d = -f 1`
80 _F_VAL=`echo "$_F_VARDEF""=" | cut -d = -f 2`
83 # Workaround for more braindead cut implementations that strip delimiters
84 # at the end of the line (i.e. HP-UX 10)
87 if echo $_F_QUERY_STRING | grep -c \& > /dev/null ; then
88 _F_QUERY_STRING=`echo $_F_QUERY_STRING | cut -d \& -f 2-`
89 else
90 _F_QUERY_STRING=""
93 if [ ${DEBUG:-0} -eq 1 ] ; then
94 ( echo --Got Variable--
95 echo " " var=$_F_VAR
96 echo " " val=$_F_VAL
97 echo " " rem=$_F_QUERY_STRING ) 1>&2
99 if [ "$_F_VAR" = "" ] ; then
100 continue
104 # replace '+' by spaces
107 _F_VAL="$_F_VAL""++"
108 _F_TMP=
110 while [ "$_F_VAL" != "" -a "$_F_VAL" != "+" -a "$_F_VAL" != "++" ] ; do
111 _F_TMP="$_F_TMP""`echo $_F_VAL | cut -d + -f 1`"
112 _F_VAL=`echo $_F_VAL | cut -s -d + -f 2-`
114 if [ "$_F_VAL" != "" -a "$_F_VAL" != "+" ] ; then
115 _F_TMP="$_F_TMP"" "
117 done
119 if [ ${DEBUG:-0} -eq 1 ] ; then
120 echo " " vrs=$_F_TMP 1>&2
124 # replace '%XX' by ascii character. the hex sequence MUST BE uppercase
127 _F_TMP="$_F_TMP""%%"
128 _F_VAL=
130 while [ "$_F_TMP" != "" -a "$_F_TMP" != "%" ] ; do
131 _F_VAL="$_F_VAL""`echo $_F_TMP | cut -d % -f 1`"
132 _F_TMP=`echo $_F_TMP | cut -s -d % -f 2-`
134 if [ "$_F_TMP" != "" -a "$_F_TMP" != "%" ] ; then
135 if [ ${DEBUG:-0} -eq 1 ] ; then
136 echo " " got hex "%" $_F_TMP 1>&2
138 _F_HEX=`echo $_F_TMP | cut -c 1-2 | tr "abcdef" "ABCDEF"`
139 _F_TMP=`echo $_F_TMP | cut -c 3-`
141 # can't handle newlines anyway. replace by space
143 # if [ "$_F_HEX" = "0A" ] ; then
144 # _F_HEX="20"
145 # fi
147 _F_VAL="$_F_VAL""`/bin/echo '\0'\`echo "16i8o"$_F_HEX"p" | dc\``"
149 done
152 # replace forward quotes to backward quotes, since we have trouble handling
153 # the former ones.
156 _F_VAL=`echo $_F_VAL | tr "'" '\`'`
159 # if debug, send variables to stderr
162 if [ ${DEBUG:-0} -eq 1 ] ; then
163 ( echo --Final Assignment--
164 echo "F1_$_F_VAR"=\'$_F_VAL\' ) 1>&2
167 # /bin/echo "FORM_$_F_VAR"=\'$_F_VAL\'
168 # /bin/echo "F2_$_F_VAR"="'"$_F_VAL"'"
169 export "F_"$_F_VAR=`echo -e "$_F_VAL"`
171 done
173 if [ ${DEBUG:-0} -eq 1 ] ; then
174 echo done. 1>&2
177 # done.
179 #exit 0