wok-stable annotate busybox/stuff/httpd_helper.sh @ rev 7093

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