wok-6.x view busybox/stuff/httpd_helper.sh @ rev 23740
updated xa (2.3.9 -> 2.3.10)
author | Hans-G?nter Theisgen |
---|---|
date | Fri May 01 11:08:45 2020 +0100 (2020-05-01) |
parents | f564323fc5a4 |
children |
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%%[^A-Za-z_0-9]*}
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 delim=${delim%?}
126 case "$delim" in
127 -*) awk "/$delim/ { o+=index(\$0,\"$delim\")-1; print o }
128 { o+=1+length() }" < ${post}0 | while read offset; do
129 if [ $offset -ne 0 ]; then
130 ddcut $last $offset < ${post}0 > $post$n 2> /dev/null
131 n=$(($n+1))
132 fi
133 last=$offset
134 done
135 rm -f ${post}0
136 CR=`printf '\r'`
137 for i in $post* ; do
138 head -n 2 $i | grep -q filename= || echo '' >> $i
139 filename=
140 while read line; do
141 case "$line" in
142 *Content-Disposition*)
143 name=$(echo $line | sed 's/.* name="\([^"]*\)".*$/\1/')
144 name=${name%%[^A-Za-z_0-9]*}
145 case "$line" in
146 *filename=*) filename=$(echo $line | sed 's/.* filename="\([^"]*\)".*$/\1/') ;;
147 esac ;;
148 *Content-Type*)
149 type=$(echo $line | sed 's/.*-Type: \(.*\).$/\1/') ;;
150 $CR)
151 if [ -n "$filename" ]; then
152 tmp=$(mktemp $prefix$$/uploadXXXXXX)
153 cat > $tmp
154 FILE__NAMES="$FILE__NAMES $name"
155 FILE__NAMES="${FILE__NAMES# }"
156 eval FILE_${name}_tmpname=$tmp
157 eval FILE_${name}_name=$filename
158 eval FILE_${name}_size=$(stat -c %s $tmp)
159 eval FILE_${name}_type=$type
160 elif [ -n "$name" ]; then
161 eval cnt=\$POST_${name}_count
162 cnt=$(($cnt + 1))
163 eval var=\$POST_${name}_$cnt
164 while read line; do
165 [ -n "$var" ] && var="$var
166 "
167 var="$var$line"
168 done
169 eval POST_${name}_$cnt="\$var"
170 eval POST_${name}_count=$cnt
171 case " $POST__NAMES " in
172 *\ $name\ *) ;;
173 *) POST__NAMES="$POST__NAMES $name"
174 POST__NAMES="${POST__NAMES# }" ;;
175 esac
176 fi
177 break ;;
178 *)
179 esac
180 done < $i
181 rm -f $i
182 done
183 rmdir $(dirname $post) ;;
184 *) rm -rf $(dirname $post)
185 read_query_string POST "$delim" ;;
186 esac
187 fi