slitaz-base-files view rootfs/usr/lib/slitaz/httphelper @ rev 122

httphelper: fix for apache
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Apr 12 16:47:54 2012 +0200 (2012-04-12)
parents 31f8714202b6
children 333e86ef0a8f
line source
1 #!/bin/sh
2 #
3 # SliTaz Helper for SHell CGI scripts - See httphelper function
4 #
5 # This helper is used in TazPanel, Pizza and other SliTaz CGI tools and
6 # services. The goal is to maintain it according to the needs of the
7 # project and Busybox httpd server applet since it is used for URL
8 # [en|de]coding.
9 #
10 # Copyright (C) SliTaz 2012 - GNU gpl v2
11 #
13 alias urlencode='busybox httpd -e'
14 alias urldecode='busybox httpd -d'
16 # Help and usage.
17 httphelper() {
18 cat << EOT
20 Include this helper in a script:
21 . /usr/lib/slitaz/httphelper
23 Functions:
24 GET [var [index]]
25 POST [var [index]]
26 COOKIE [var [index]]
27 FILE [var {name|tmpname|size|type}]
28 header [strings]
29 http_urlencode string
30 urlencode string
31 urldecode string
32 htmlentities string
33 md5crypt string
34 sha512crypt string
35 httpinfo
36 httphelper
38 Example:
39 header "Content-type: text/html" "Set-Cookie: name=value; HttpOnly"
41 if [ "\$(GET name)" ]; then
42 echo "Input name has a value from HTML form"
43 fi
45 EOT
46 }
48 # Send headers.
49 header() {
50 local i
51 [ -z "$1" ] && set -- "Content-type: text/html"
52 for i in "$@" ""
53 do
54 echo -e "$i\r"
55 done
56 }
58 http_urlencode() {
59 sed -e "s|%|%25|g;s|!|%21|g;s|\*|%2A|g;s|'|%27|g;s|(|%28|g" \
60 -e "s|)|%29|g;s|;|%3B|g;s|:|%3A|g;s|@|%40|g;s|&|%26|g" \
61 -e "s|=|%3D|g;s|+|%2B|g;s|\$|%24|g;s|,|%2C|g;s|/|%2F|g" \
62 -e "s|\?|%3F|g;s|#|%25|g;s|\[|%5B|g;s|\]|%5D|g;s| |+|g"
63 }
65 htmlentities() {
66 echo $1 | sed 's|&|\&amp;|g;s|<|\&lt;|g;s|>|\&gt;|g;s|"|\&quot;|g'
67 }
69 # MD5 crypt a string such as password (httpd -m dont give same result ?)
70 md5crypt() {
71 echo -n "$1" | md5sum | awk '{print $1}'
72 }
74 # MD5 crypt a string. Stronger crypto than MD5
75 sha512crypt() {
76 echo -n "$1" | sha512sum | awk '{print $1}'
77 }
79 _ARRAY()
80 {
81 [ -z "$2" ] && eval echo \$${1}__NAMES ||
82 [ -n "$(eval echo \$${1}__NAMES)" ] && eval echo \$${1}_${2}_${3:-1}
83 }
85 GET() { _ARRAY GET "$1" $2; }
86 POST() { _ARRAY POST "$1" $2; }
87 FILE() { _ARRAY FILE "$1" $2; }
88 COOKIE() { _ARRAY COOKIE "$1" $2; }
90 httpinfo()
91 {
92 local i
93 local j
94 local x
95 for i in SERVER_PROTOCOL SERVER_SOFTWARE SERVER_NAME SERVER_PORT AUTH_TYPE \
96 GATEWAY_INTERFACE REMOTE_HOST REMOTE_ADDR REMOTE_PORT \
97 HTTP_REFERER HTTP_HOST HTTP_USER_AGENT HTTP_ACCEPT \
98 HTTP_ACCEPT_LANGUAGE HTTP_COOKIE AUTH_TYPE REMOTE_USER REMOTE_IDENT \
99 REQUEST_METHOD REQUEST_URI QUERY_STRING CONTENT_LENGTH CONTENT_TYPE \
100 SCRIPT_NAME SCRIPT_FILENAME PATH_INFO PATH_TRANSLATED \
101 USER HOME LOGNAME SHELL PWD ; do
102 eval x=\$$i
103 [ -n "$x" ] && echo "$i='$x'"
104 done
105 for x in GET POST COOKIE ; do
106 for i in $($x) ; do
107 if [ $($x $i count) -gt 1 ]; then
108 for j in $(seq 1 $($x $i count)); do
109 echo "$x($i,$j)='$($x $i $j)'"
110 done
111 else
112 echo "$x($i)='$($x $i)'"
113 fi
114 done
115 done
116 for i in $(FILE) ; do
117 for j in name size type tmpname ; do
118 echo "FILE($i,$j)='$(FILE $i $j)'"
119 done
120 done
121 }
123 read_query_string()
124 {
125 local i
126 local names
127 local cnt
128 names=""
129 IFS="&"
130 for i in $2 ; do
131 var=${i%%[^A-Za-z_0-9]*}
132 case " $names " in
133 *\ $var\ *) eval cnt=\$${1}_${var}_count ;;
134 *) cnt=0
135 names="$names $var" ;;
136 esac
137 eval ${1}_${var}_count=$((++cnt))
138 eval ${1}_${var}_$cnt=\'$(busybox httpd -d "${i#*=}" | sed "s/'/\'\\\\\'\'/g")\'
139 done
140 unset IFS
141 eval ${1}__NAMES=\'${names# }\'
142 }
144 [ -z "$GET__NAMES" ] && read_query_string GET "$QUERY_STRING"
145 [ -z "$COOKIE_NAMES" ] &&
146 read_query_string COOKIE "$(echo "$HTTP_COOKIE" | sed 's/; /\&/g')"
148 ddcut()
149 {
150 page=4096
151 skip=$1
152 count=$(($2 - $1 -2))
153 tmp=$(($skip / $page))
154 [ $tmp -ne 0 ] && dd bs=$page skip=$tmp count=0
155 skip=$(($skip - ($tmp * $page) ))
156 dd bs=1 skip=$skip count=0
157 tmp=$(( ($page - $skip) % $page ))
158 if [ $tmp -ne 0 -a $tmp -le $count ]; then
159 dd bs=1 count=$tmp
160 count=$(($count - $tmp))
161 fi
162 tmp=$(($count / $page))
163 [ $tmp -ne 0 ] && dd bs=$page count=$tmp
164 dd bs=1 count=$(($count - ($tmp * $page) ))
165 }
167 if [ "$REQUEST_METHOD$POST__NAMES" == "POST" ]; then
168 prefix=/tmp/httpd_post
169 mkdir $prefix$$
170 now=$(stat -c %Y $prefix$$)
171 for i in $prefix* ; do
172 [ $(stat -c %Y $i) -lt $(($now - 3600)) ] && rm -rf $i
173 done
174 post=$prefix$$/post
175 n=1
176 cat > ${post}0
177 read args < ${post}0
178 delim="${args%?}"
179 case "$delim" in
180 -*) awk "/$delim/ { o+=index(\$0,\"$delim\")-1; print o }
181 { o+=1+length() }" < ${post}0 | while read offset; do
182 if [ $offset -ne 0 ]; then
183 ddcut $last $offset < ${post}0 > $post$n 2> /dev/null
184 n=$(($n+1))
185 fi
186 last=$offset
187 done
188 rm -f ${post}0
189 CR=`printf '\r'`
190 for i in $post* ; do
191 head -n 2 $i | grep -q filename= || echo '' >> $i
192 filename=
193 while read line; do
194 case "$line" in
195 *Content-Disposition*)
196 name=$(echo $line | sed 's/.* name="\([^"]*\)".*$/\1/')
197 name=${name%%[^A-Za-z_0-9]*}
198 case "$line" in
199 *filename=*) filename=$(echo $line | sed 's/.* filename="\([^"]*\)".*$/\1/') ;;
200 esac ;;
201 *Content-Type*)
202 type=$(echo $line | sed 's/.*-Type: \(.*\).$/\1/') ;;
203 $CR)
204 if [ -n "$filename" ]; then
205 tmp=$(mktemp $prefix$$/uploadXXXXXX)
206 cat > $tmp
207 FILE__NAMES="$FILE__NAMES $name"
208 FILE__NAMES="${FILE__NAMES# }"
209 eval FILE_${name}_tmpname=$tmp
210 eval FILE_${name}_name=$filename
211 eval FILE_${name}_size=$(stat -c %s $tmp)
212 eval FILE_${name}_type=$type
213 elif [ -n "$name" ]; then
214 eval cnt=\$POST_${name}_count
215 cnt=$(($cnt + 1))
216 eval var=\$POST_${name}_$cnt
217 while read line; do
218 [ -n "$var" ] && var="$var
219 "
220 var="$var$line"
221 done
222 eval POST_${name}_$cnt="\$var"
223 eval POST_${name}_count=$cnt
224 case " $POST__NAMES " in
225 *\ $name\ *) ;;
226 *) POST__NAMES="$POST__NAMES $name"
227 POST__NAMES="${POST__NAMES# }" ;;
228 esac
229 fi
230 break ;;
231 *)
232 esac
233 done < $i
234 rm -f $i
235 done
236 rmdir $(dirname $post) ;;
237 *) rm -rf $(dirname $post)
238 read_query_string POST "$args" ;;
239 esac
240 fi