wok-next view busybox/stuff/httpd_helper.sh @ rev 9606
nanochess: add title
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Apr 14 11:31:54 2011 +0200 (2011-04-14) |
parents | 13ba44bf121e |
children | 6390498a6cd1 |
line source
1 #!/bin/sh
2 #
3 # busybox/httpd helper for shell cgi scripts, providing:
4 #
5 # GET [var [index]] | POST [var [index]] | COOKIE [var [index]]
6 # FILE [var {name|tmpname|size|type}]
7 # header [strings]... | urlencode string | htmlentities string | httpinfo
8 #
10 alias urlencode='httpd -e'
12 # Send headers, example :
13 # header "Content-type: text/html" "Set-Cookie: name=value; HttpOnly"
14 header()
15 {
16 local i
17 [ -z "$1" ] && set -- "Content-type: text/html"
18 for i in "$@" "" ; do echo -e "$i\r"; done
19 }
21 htmlentities()
22 {
23 echo $1 | sed 's|&|\&|g;s|<|\<|g;s|>|\>|g;s|"|\"|g'
24 }
26 _ARRAY()
27 {
28 [ -z "$2" ] && eval echo \$${1}__NAMES ||
29 [ -n "$(eval echo \$${1}__NAMES)" ] && eval echo \$${1}_${2}_${3:-1}
30 }
32 GET() { _ARRAY GET "$1" $2; }
33 POST() { _ARRAY POST "$1" $2; }
34 FILE() { _ARRAY FILE "$1" $2; }
35 COOKIE() { _ARRAY COOKIE "$1" $2; }
37 httpinfo()
38 {
39 local i
40 local j
41 local x
42 for i in SERVER_PROTOCOL SERVER_SOFTWARE SERVER_NAME SERVER_PORT AUTH_TYPE \
43 GATEWAY_INTERFACE REMOTE_HOST REMOTE_ADDR REMOTE_PORT \
44 HTTP_REFERER HTTP_HOST HTTP_USER_AGENT HTTP_ACCEPT \
45 HTTP_ACCEPT_LANGUAGE HTTP_COOKIE AUTH_TYPE REMOTE_USER REMOTE_IDENT \
46 REQUEST_METHOD REQUEST_URI QUERY_STRING CONTENT_LENGTH CONTENT_TYPE \
47 SCRIPT_NAME SCRIPT_FILENAME PATH_INFO PATH_TRANSLATED \
48 USER HOME LOGNAME SHELL PWD ; do
49 eval x=\$$i
50 [ -n "$x" ] && echo "$i='$x'"
51 done
52 for x in GET POST COOKIE ; do
53 for i in $($x) ; do
54 if [ $($x $i count) -gt 1 ]; then
55 for j in $(seq 1 $($x $i count)); do
56 echo "$x($i,$j)='$($x $i $j)'"
57 done
58 else
59 echo "$x($i)='$($x $i)'"
60 fi
61 done
62 done
63 for i in $(FILE) ; do
64 for j in name size type tmpname ; do
65 echo "FILE($i,$j)='$(FILE $i $j)'"
66 done
67 done
68 }
70 read_query_string()
71 {
72 local i
73 local names
74 local cnt
75 names=""
76 IFS="&"
77 for i in $2 ; do
78 var=${i%%=*}
79 case " $names " in
80 *\ $var\ *) eval cnt=\$${1}_${var}_count ;;
81 *) cnt=0
82 names="$names $var" ;;
83 esac
84 eval ${1}_${var}_count=$((++cnt))
85 eval ${1}_${var}_$cnt=\'$(httpd -d "${i#*=}" | sed "s/'/\'\\\\\'\'/g")\'
86 done
87 unset IFS
88 eval ${1}__NAMES=\'${names# }\'
89 }
91 [ -z "$GET__NAMES" ] && read_query_string GET "$QUERY_STRING"
92 [ -z "$COOKIE_NAMES" ] &&
93 read_query_string COOKIE "$(echo "$HTTP_COOKIE" | sed 's/; /\&/g')"
95 ddcut()
96 {
97 page=4096
98 skip=$1
99 count=$(($2 - $1 -2))
100 tmp=$(($skip / $page))
101 [ $tmp -ne 0 ] && dd bs=$page skip=$tmp count=0
102 skip=$(($skip - ($tmp * $page) ))
103 dd bs=1 skip=$skip count=0
104 tmp=$(( ($page - $skip) % $page ))
105 if [ $tmp -ne 0 -a $tmp -le $count ]; then
106 dd bs=1 count=$tmp
107 count=$(($count - $tmp))
108 fi
109 tmp=$(($count / $page))
110 [ $tmp -ne 0 ] && dd bs=$page count=$tmp
111 dd bs=1 count=$(($count - ($tmp * $page) ))
112 }
114 if [ "$REQUEST_METHOD$POST__NAMES" == "POST" ]; then
115 prefix=/tmp/httpd_post
116 mkdir $prefix$$
117 now=$(stat -c %Y $prefix$$)
118 for i in $prefix* ; do
119 [ $(stat -c %Y $i) -lt $(($now - 3600)) ] && rm -rf $i
120 done
121 post=$prefix$$/post
122 n=1
123 cat > ${post}0
124 read delim < ${post}0
125 case "$delim" in
126 -*) awk "/${delim%?}/ { o+=index(\$0,\"$delim\")-1; print o }
127 { o+=1+length() }" < ${post}0 | while read offset; do
128 if [ $offset -ne 0 ]; then
129 ddcut $last $offset < ${post}0 > $post$n 2> /dev/null
130 n=$(($n+1))
131 fi
132 last=$offset
133 done
134 rm -f ${post}0
135 CR=`printf '\r'`
136 for i in $post* ; do
137 head -n 2 $i | grep -q filename= || echo '' >> $i
138 filename=
139 while read line; do
140 case "$line" in
141 *Content-Disposition*)
142 name=$(echo $line | sed 's/.* name="\([^"]*\)".*$/\1/')
143 case "$line" in
144 *filename=*) filename=$(echo $line | sed 's/.* filename="\([^"]*\)".*$/\1/') ;;
145 esac ;;
146 *Content-Type*)
147 type=$(echo $line | sed 's/.*-Type: \(.*\).$/\1/') ;;
148 $CR)
149 if [ -n "$filename" ]; then
150 tmp=$(mktemp $prefix$$/uploadXXXXXX)
151 cat > $tmp
152 FILE__NAMES="$FILE__NAMES $name"
153 FILE__NAMES="${FILE__NAMES# }"
154 eval FILE_${name}_tmpname=$tmp
155 eval FILE_${name}_name=$filename
156 eval FILE_${name}_size=$(stat -c %s $tmp)
157 eval FILE_${name}_type=$type
158 elif [ -n "$name" ]; then
159 eval var=\$POST_${name}
160 while read line; do
161 [ -n "$var" ] && var="$var
162 "
163 var="$line"
164 done
165 eval POST_${name}="\$var"
166 case " $POST__NAMES " in
167 *\ $name\ *) ;;
168 *) POST__NAMES="$POST__NAMES $name"
169 POST__NAMES="${POST__NAMES# }" ;;
170 esac
171 fi
172 break ;;
173 *)
174 esac
175 done < $i
176 rm -f $i
177 done
178 rmdir $(dirname $post) ;;
179 *) rm -rf $(dirname $post)
180 read_query_string POST "$delim" ;;
181 esac
182 fi