wok rev 5685
busybox: add httpd_helper.sh
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Jun 04 17:12:30 2010 +0200 (2010-06-04) |
parents | 2a5e098a34ac |
children | 2eef070456a4 |
files | busybox/stuff/httpd_helper.sh |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/busybox/stuff/httpd_helper.sh Fri Jun 04 17:12:30 2010 +0200 1.3 @@ -0,0 +1,164 @@ 1.4 +#!/bin/sh 1.5 +# 1.6 +# busybox/httpd helper for shell cgi scripts 1.7 +# 1.8 +# GET [var] | POST [var] | FILE [var {name|tmpname|size|type}] 1.9 +# urlencode string | htmlentities string | httpinfo 1.10 + 1.11 +alias urlencode='httpd -e' 1.12 + 1.13 +header() 1.14 +{ 1.15 +echo -e "$1\r" 1.16 +} 1.17 + 1.18 +htmlentities() 1.19 +{ 1.20 +echo $1 | sed -e 's|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|g' -e 's|"|\"|g' 1.21 +} 1.22 + 1.23 +GET() 1.24 +{ 1.25 +[ -z "$1" ] && echo $GET__NAMES || [ -n "$GET__NAMES" ] && eval echo \$GET_$1 1.26 +} 1.27 + 1.28 +POST() 1.29 +{ 1.30 +[ -z "$1" ] && echo $POST__NAMES || [ -n "$POST__NAMES" ] && eval echo \$POST_$1 1.31 +} 1.32 + 1.33 +FILE() 1.34 +{ 1.35 +[ -z "$1" ] && echo $FILE__NAMES || [ -n "$FILE__NAMES" ] && eval echo \$FILE_${1}_$2 1.36 +} 1.37 + 1.38 +httpinfo() 1.39 +{ 1.40 +local i 1.41 +local x 1.42 +for i in SERVER_PROTOCOL SERVER_SOFTWARE SERVER_NAME SERVER_PORT AUTH_TYPE \ 1.43 + GATEWAY_INTERFACE REMOTE_ADDR REMOTE_PORT HTTP_REFERER HTTP_HOST \ 1.44 + HTTP_USER_AGENT HTTP_ACCEPT HTTP_ACCEPT_LANGUAGE HTTP_COOKIE \ 1.45 + REQUEST_METHOD REQUEST_URI QUERY_STRING CONTENT_LENGTH CONTENT_TYPE \ 1.46 + SCRIPT_NAME SCRIPT_FILENAME PATH_INFO USER HOME LOGNAME SHELL PWD ; do 1.47 + eval x=\$$i 1.48 + [ -n "$x" ] && echo "$i='$x'" 1.49 +done 1.50 +for i in $GET__NAMES ; do 1.51 + echo "GET[$i]='$(GET $i)'" 1.52 +done 1.53 +for i in $POST__NAMES ; do 1.54 + echo "POST[$i]='$(POST $i)'" 1.55 +done 1.56 +for i in $FILE__NAMES ; do 1.57 + for j in name size type tmpname ; do 1.58 + echo "FILE[$i][$j]='$(FILE $i $j)'" 1.59 + done 1.60 +done 1.61 +} 1.62 + 1.63 +read_query_string() 1.64 +{ 1.65 +local i 1.66 +local names 1.67 +names="" 1.68 +IFS="&" 1.69 +for i in $QUERY_STRING ; do 1.70 + names="$names ${i%%=*}" 1.71 + eval ${1}_${i%%=*}=\'$(httpd -d "${i#*=}" | sed "s/'/\'\\\\\'\'/g")\' 1.72 +done 1.73 +unset IFS 1.74 +eval ${1}__NAMES=\'${names# }\' 1.75 +} 1.76 + 1.77 +[ "$REQUEST_METHOD" == "GET" -a -z "$GET__NAMES" ] && read_query_string GET 1.78 + 1.79 +ddcut() 1.80 +{ 1.81 +page=4096 1.82 +skip=$1 1.83 +count=$(($2 - $1 -2)) 1.84 +tmp=$(($skip / $page)) 1.85 +[ $tmp -ne 0 ] && dd bs=$page skip=$tmp count=0 1.86 +skip=$(($skip - ($tmp * $page) )) 1.87 +dd bs=1 skip=$skip count=0 1.88 +tmp=$(( ($page - $skip) % $page )) 1.89 +if [ $tmp -ne 0 -a $tmp -le $count ]; then 1.90 + dd bs=1 count=$tmp 1.91 + count=$(($count - $tmp)) 1.92 +fi 1.93 +tmp=$(($count / $page)) 1.94 +[ $tmp -ne 0 ] && dd bs=$page count=$tmp 1.95 +dd bs=1 count=$(($count - ($tmp * $page) )) 1.96 +} 1.97 + 1.98 +if [ "$REQUEST_METHOD" == "POST" -a -z "$POST__NAMES" ]; then 1.99 + prefix=/tmp/httpd_post 1.100 + mkdir $prefix$$ 1.101 + now=$(stat -c %Y $prefix$$) 1.102 + for i in $prefix* ; do 1.103 + [ $(stat -c %Y $i) -lt $(($now - 3600)) ] && rm -rf $i 1.104 + done 1.105 + post=$prefix$$/post 1.106 + n=1 1.107 + cat > ${post}0 1.108 + read delim < ${post}0 1.109 + case "$delim" in 1.110 + -*) awk "/$delim/ { o+=index(\$0,\"$delim\")-1; print o } 1.111 + { o+=1+length() }" < ${post}0 | while read offset; do 1.112 + if [ $offset -ne 0 ]; then 1.113 + ddcut $last $offset < ${post}0 > $post$n 2> /dev/null 1.114 + n=$(($n+1)) 1.115 + fi 1.116 + last=$offset 1.117 + done 1.118 + rm -f ${post}0 1.119 + CR=`printf '\r'` 1.120 + for i in $post* ; do 1.121 + head -n 2 $i | grep -q filename= || echo '' >> $i 1.122 + filename= 1.123 + while read line; do 1.124 + case "$line" in 1.125 + *Content-Disposition*) 1.126 + name=$(echo $line | sed 's/.* name="\([^"]*\)".*$/\1/') 1.127 + case "$line" in 1.128 + *filename=*) filename=$(echo $line | sed 's/.* filename="\([^"]*\)".*$/\1/') ;; 1.129 + esac ;; 1.130 + *Content-Type*) 1.131 + type=$(echo $line | sed 's/.*-Type: \(.*\).$/\1/') ;; 1.132 + $CR) 1.133 + if [ -n "$filename" ]; then 1.134 + tmp=$(mktemp $prefix$$/uploadXXXXXX) 1.135 + cat > $tmp 1.136 + FILE__NAMES="$FILE__NAMES $name" 1.137 + FILE__NAMES="${FILE__NAMES# }" 1.138 + eval FILE_${name}_tmpname=$tmp 1.139 + eval FILE_${name}_name=$filename 1.140 + eval FILE_${name}_size=$(stat -c %s $tmp) 1.141 + eval FILE_${name}_type=$type 1.142 + elif [ -n "$name" ]; then 1.143 + eval var=\$POST_${name} 1.144 + while read line; do 1.145 + [ -n "$var" ] && var="$var 1.146 +" 1.147 + var="$line" 1.148 + done 1.149 + eval POST_${name}="\$var" 1.150 + case " $POST__NAMES " in 1.151 + *\ $name\ *) ;; 1.152 + *) POST__NAMES="$POST__NAMES $name" 1.153 + POST__NAMES="${POST__NAMES# }" ;; 1.154 + esac 1.155 + fi 1.156 + break ;; 1.157 + *) 1.158 + esac 1.159 + done < $i 1.160 + rm -f $i 1.161 + done 1.162 + rmdir $(dirname $post) ;; 1.163 + *) export QUERY_STRING="$delim" 1.164 + rm -rf $(dirname $post) 1.165 + read_query_string POST ;; 1.166 + esac 1.167 +fi