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