slitaz-base-files view rootfs/usr/lib/slitaz/httphelper.sh @ rev 340

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 08:22:10 2019 +0100 (2019-02-26)
parents d9e8cd5b9ad8
children
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 # Documentation: man httphelper or /usr/share/doc/slitaz/httphelper.txt
11 #
12 # Copyright (C) SliTaz 2014-2015 - GNU GPL v2
13 #
16 alias urlencode='busybox httpd -e'
17 alias urldecode='busybox httpd -d'
20 # Send headers.
22 header() {
23 local i
24 [ -z "$1" ] && set -- "Content-type: text/html; charset=UTF-8"
25 for i in "$@" ""; do
26 echo -e "$i\r"
27 done
28 }
31 http_urlencode() {
32 sed \
33 -e "s|%|%25|g; s|!|%21|g; s|\*|%2A|g; s|'|%27|g; s|(|%28|g" \
34 -e "s|)|%29|g; s|;|%3B|g; s|:|%3A|g; s|@|%40|g; s|&|%26|g" \
35 -e "s|=|%3D|g; s|+|%2B|g; s|\$|%24|g; s|,|%2C|g; s|/|%2F|g" \
36 -e "s|\?|%3F|g; s|#|%25|g; s|\[|%5B|g; s|\]|%5D|g; s| |+|g"
37 }
40 htmlentities() {
41 echo $1 | sed \
42 -e 's|&|\&amp;|g; s|<|\&lt;|g; s|>|\&gt;|g' \
43 -e 's|"|\&quot;|g; s|'"'"'|\&apos;|g; s|\t|\&#09;|g'
44 }
47 # MD5 crypt a string such as password (httpd -m give not the same result)
49 md5crypt() {
50 echo -n "$1" | md5sum | awk '{print $1}'
51 }
54 # MD5 crypt a string. Stronger crypto than MD5
56 sha512crypt() {
57 echo -n "$1" | sha512sum | awk '{print $1}'
58 }
61 _ARRAY() {
62 if [ -z "$2" ]; then
63 eval echo \$${1}__NAMES
64 elif [ "$(eval echo \$${1}__NAMES)" ]; then
65 [ "$4" ] && eval "echo \"\$${1}_${2}_${3}_${4}\"" ||
66 eval "echo \"\$${1}_${2}_${3:-1}\""
67 fi
68 }
71 GET() { _ARRAY GET "$1" $2; }
72 POST() { _ARRAY POST "$1" $2; }
73 FILE() { _ARRAY FILE "$1" $2 ${3:-1}; }
74 COOKIE() { _ARRAY COOKIE "$1" $2; }
77 httpinfo() {
78 local i
79 local j
80 local x
81 env | sort | sed "s/=/='/;s/$/'/"
82 for x in GET POST COOKIE ; do
83 for i in $($x) ; do
84 if [ $($x $i count) -gt 1 ]; then
85 for j in $(seq 1 $($x $i count)); do
86 echo "$x($i,$j)='$($x $i $j)'"
87 done
88 else
89 echo "$x($i)='$($x $i)'"
90 fi
91 done
92 done
93 for i in $(FILE) ; do
94 for j in name size type tmpname ; do
95 if [ $(FILE $i count) -gt 1 ]; then
96 for k in $(seq 1 $(FILE $i count)); do
97 echo "FILE($i,$j,$k)='$(FILE $i $j $k)'"
98 done
99 else
100 echo "FILE($i,$j)='$(FILE $i $j)'"
101 fi
102 done
103 done
104 }
107 read_query_string() {
108 local i
109 local names
110 local cnt
111 names=""
112 IFS="&"
113 for i in $2; do
114 var=${i%%=*}
115 case " $names " in
116 *\ $var\ *)
117 eval cnt=\$${1}_${var}_count ;;
118 *)
119 cnt=0
120 names="$names $var" ;;
121 esac
122 eval ${1}_${var}_count=$((++cnt))
123 eval "${1}_${var}_$cnt='$(busybox httpd -d "${i#*=}" | sed "s/'/\'\\\\\'\'/g; s|\r||g")'"
124 done
125 unset IFS
126 eval ${1}__NAMES=\'${names# }\'
127 }
131 [ -z "$GET__NAMES" ] && read_query_string GET "$QUERY_STRING"
132 [ -z "$COOKIE_NAMES" ] &&
133 read_query_string COOKIE "$(echo "$HTTP_COOKIE" | sed 's/; /\&/g')"
137 ddcut() {
138 page=4096
139 skip=$1
140 count=$(($2 - $1 -2))
142 tmp=$(($skip / $page))
143 [ $tmp -ne 0 ] && dd bs=$page skip=$tmp count=0
145 skip=$(($skip - ($tmp * $page) ))
146 dd bs=1 skip=$skip count=0
148 tmp=$(( ($page - $skip) % $page ))
149 if [ $tmp -ne 0 -a $tmp -le $count ]; then
150 dd bs=1 count=$tmp
151 count=$(($count - $tmp))
152 fi
154 tmp=$(($count / $page))
155 [ $tmp -ne 0 ] && dd bs=$page count=$tmp
156 dd bs=1 count=$(($count - ($tmp * $page) ))
157 }
160 [ "$1" = "--skip-post" ] ||
161 if [ "$REQUEST_METHOD$POST__NAMES" = "POST" ]; then
162 prefix=/tmp/httpd_post
163 mkdir $prefix$$
164 now=$(stat -c %Y $prefix$$)
165 for i in $prefix* ; do
166 [ $(stat -c %Y $i) -lt $(($now - 3600)) ] && rm -rf $i
167 done
168 post=$prefix$$/post
169 n=1
170 cat > ${post}0
171 read args < ${post}0
172 delim="${args%?}"
174 case "$delim" in
176 -*)
177 awk "/$delim/ { o+=index(\$0,\"$delim\")-1; print o }
178 { o+=1+length() }" < ${post}0 | \
179 while read offset; do
180 if [ $offset -ne 0 ]; then
181 ddcut $last $offset < ${post}0 > $post$n 2> /dev/null
182 n=$(($n+1))
183 fi
184 last=$offset
185 done
186 rm -f ${post}0
188 for i in $post* ; do
189 head -n2 $i | grep -q filename= || echo '' >> $i
190 filename=
191 while read line; do
192 case "$line" in
194 *Content-Disposition*)
195 name=$(echo $line | sed 's/.* name="\([^"]*\)".*$/\1/')
196 name=${name%%[^A-Za-z_0-9]*}
197 case "$line" in
198 *filename=*)
199 filename=$(echo $line | sed 's/.* filename="\([^"]*\)".*$/\1/') ;;
200 esac ;;
202 *Content-Type*)
203 type=$(echo $line | sed 's/.*-Type: \(.*\).$/\1/') ;;
205 $'\r')
206 if [ -n "$filename" ]; then
207 eval cnt=\$FILE_${name}_count_1
208 cnt=$(($cnt + 1))
209 eval FILE_${name}_count_1=$cnt
210 tmp=$(mktemp $prefix$$/uploadXXXXXX)
211 cat > $tmp
212 case " $FILE__NAMES " in
213 *\ $name\ *)
214 ;;
215 *)
216 FILE__NAMES="$FILE__NAMES $name"
217 FILE__NAMES="${FILE__NAMES# }" ;;
218 esac
219 eval FILE_${name}_tmpname_$cnt=$tmp
220 eval FILE_${name}_name_$cnt=$filename
221 eval FILE_${name}_size_$cnt=$(stat -c %s $tmp)
222 eval FILE_${name}_type_$cnt=$type
223 elif [ -n "$name" ]; then
224 eval cnt=\$POST_${name}_count
225 cnt=$(($cnt + 1))
226 eval var=\$POST_${name}_$cnt
227 while read line; do
228 [ -n "$var" ] && var="$var"$'\n'
229 var="$var$line"
230 done
231 eval POST_${name}_$cnt="\$var"
232 eval POST_${name}_count=$cnt
233 case " $POST__NAMES " in
234 *\ $name\ *)
235 ;;
236 *)
237 POST__NAMES="$POST__NAMES $name"
238 POST__NAMES="${POST__NAMES# }" ;;
239 esac
240 fi
241 break ;;
243 *) ;;
244 esac
245 done < $i
246 rm -f $i
247 done
248 #rmdir $(dirname $post)
249 ;;
251 *)
252 rm -rf $(dirname $post)
253 read_query_string POST "$args" ;;
255 esac
256 fi