wok-next view busybox/stuff/httpd_helper.sh @ rev 8064
Fix: openttd needs icu to run
author | Antoine Bodin <gokhlayeh@slitaz.org> |
---|---|
date | Fri Jan 21 00:49:11 2011 +0100 (2011-01-21) |
parents | 8c1e893567be |
children | 0c9dab57bef3 |
line source
1 #!/bin/sh
2 #
3 # busybox/httpd helper for shell cgi scripts
4 #
5 # GET [var] | POST [var] | FILE [var {name|tmpname|size|type}]
6 # urlencode string | htmlentities string | httpinfo
8 alias urlencode='httpd -e'
10 header()
11 {
12 [ -z "$1" ] && echo -e "Content-type: text/html\r\n" || echo -e "$1\r\n"
13 }
15 htmlentities()
16 {
17 echo $1 | sed -e 's|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|g' -e 's|"|\"|g'
18 }
20 GET()
21 {
22 [ -z "$1" ] && echo $GET__NAMES || [ -n "$GET__NAMES" ] && eval echo \$GET_$1
23 }
25 POST()
26 {
27 [ -z "$1" ] && echo $POST__NAMES || [ -n "$POST__NAMES" ] && eval echo \$POST_$1
28 }
30 FILE()
31 {
32 [ -z "$1" ] && echo $FILE__NAMES || [ -n "$FILE__NAMES" ] && eval echo \$FILE_${1}_$2
33 }
35 httpinfo()
36 {
37 local i
38 local x
39 for i in SERVER_PROTOCOL SERVER_SOFTWARE SERVER_NAME SERVER_PORT AUTH_TYPE \
40 GATEWAY_INTERFACE REMOTE_HOST REMOTE_ADDR REMOTE_PORT \
41 HTTP_REFERER HTTP_HOST HTTP_USER_AGENT HTTP_ACCEPT \
42 HTTP_ACCEPT_LANGUAGE HTTP_COOKIE AUTH_TYPE REMOTE_USER REMOTE_IDENT \
43 REQUEST_METHOD REQUEST_URI QUERY_STRING CONTENT_LENGTH CONTENT_TYPE \
44 SCRIPT_NAME SCRIPT_FILENAME PATH_INFO PATH_TRANSLATED \
45 USER HOME LOGNAME SHELL PWD ; do
46 eval x=\$$i
47 [ -n "$x" ] && echo "$i='$x'"
48 done
49 for i in $GET__NAMES ; do
50 echo "GET[$i]='$(GET $i)'"
51 done
52 for i in $POST__NAMES ; do
53 echo "POST[$i]='$(POST $i)'"
54 done
55 for i in $FILE__NAMES ; do
56 for j in name size type tmpname ; do
57 echo "FILE[$i][$j]='$(FILE $i $j)'"
58 done
59 done
60 }
62 read_query_string()
63 {
64 local i
65 local names
66 names=""
67 IFS="&"
68 for i in $QUERY_STRING ; do
69 names="$names ${i%%=*}"
70 eval ${1}_${i%%=*}=\'$(httpd -d "${i#*=}" | sed "s/'/\'\\\\\'\'/g")\'
71 done
72 unset IFS
73 eval ${1}__NAMES=\'${names# }\'
74 }
76 [ "$REQUEST_METHOD" == "GET" -a -z "$GET__NAMES" ] && read_query_string GET
78 ddcut()
79 {
80 page=4096
81 skip=$1
82 count=$(($2 - $1 -2))
83 tmp=$(($skip / $page))
84 [ $tmp -ne 0 ] && dd bs=$page skip=$tmp count=0
85 skip=$(($skip - ($tmp * $page) ))
86 dd bs=1 skip=$skip count=0
87 tmp=$(( ($page - $skip) % $page ))
88 if [ $tmp -ne 0 -a $tmp -le $count ]; then
89 dd bs=1 count=$tmp
90 count=$(($count - $tmp))
91 fi
92 tmp=$(($count / $page))
93 [ $tmp -ne 0 ] && dd bs=$page count=$tmp
94 dd bs=1 count=$(($count - ($tmp * $page) ))
95 }
97 if [ "$REQUEST_METHOD" == "POST" -a -z "$POST__NAMES" ]; then
98 prefix=/tmp/httpd_post
99 mkdir $prefix$$
100 now=$(stat -c %Y $prefix$$)
101 for i in $prefix* ; do
102 [ $(stat -c %Y $i) -lt $(($now - 3600)) ] && rm -rf $i
103 done
104 post=$prefix$$/post
105 n=1
106 cat > ${post}0
107 read delim < ${post}0
108 case "$delim" in
109 -*) awk "/$delim/ { o+=index(\$0,\"$delim\")-1; print o }
110 { o+=1+length() }" < ${post}0 | while read offset; do
111 if [ $offset -ne 0 ]; then
112 ddcut $last $offset < ${post}0 > $post$n 2> /dev/null
113 n=$(($n+1))
114 fi
115 last=$offset
116 done
117 rm -f ${post}0
118 CR=`printf '\r'`
119 for i in $post* ; do
120 head -n 2 $i | grep -q filename= || echo '' >> $i
121 filename=
122 while read line; do
123 case "$line" in
124 *Content-Disposition*)
125 name=$(echo $line | sed 's/.* name="\([^"]*\)".*$/\1/')
126 case "$line" in
127 *filename=*) filename=$(echo $line | sed 's/.* filename="\([^"]*\)".*$/\1/') ;;
128 esac ;;
129 *Content-Type*)
130 type=$(echo $line | sed 's/.*-Type: \(.*\).$/\1/') ;;
131 $CR)
132 if [ -n "$filename" ]; then
133 tmp=$(mktemp $prefix$$/uploadXXXXXX)
134 cat > $tmp
135 FILE__NAMES="$FILE__NAMES $name"
136 FILE__NAMES="${FILE__NAMES# }"
137 eval FILE_${name}_tmpname=$tmp
138 eval FILE_${name}_name=$filename
139 eval FILE_${name}_size=$(stat -c %s $tmp)
140 eval FILE_${name}_type=$type
141 elif [ -n "$name" ]; then
142 eval var=\$POST_${name}
143 while read line; do
144 [ -n "$var" ] && var="$var
145 "
146 var="$line"
147 done
148 eval POST_${name}="\$var"
149 case " $POST__NAMES " in
150 *\ $name\ *) ;;
151 *) POST__NAMES="$POST__NAMES $name"
152 POST__NAMES="${POST__NAMES# }" ;;
153 esac
154 fi
155 break ;;
156 *)
157 esac
158 done < $i
159 rm -f $i
160 done
161 rmdir $(dirname $post) ;;
162 *) export QUERY_STRING="$delim"
163 rm -rf $(dirname $post)
164 read_query_string POST ;;
165 esac
166 fi