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