wok rev 9597
busybox/httpd_helper.sh: add COOKIE support
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Apr 13 13:28:56 2011 +0200 (2011-04-13) |
parents | fbc6ffdfe825 |
children | 13ba44bf121e |
files | busybox/stuff/httpd_helper.sh |
line diff
1.1 --- a/busybox/stuff/httpd_helper.sh Wed Apr 13 11:06:30 2011 +0200 1.2 +++ b/busybox/stuff/httpd_helper.sh Wed Apr 13 13:28:56 2011 +0200 1.3 @@ -1,9 +1,11 @@ 1.4 #!/bin/sh 1.5 # 1.6 -# busybox/httpd helper for shell cgi scripts 1.7 +# busybox/httpd helper for shell cgi scripts, providing: 1.8 # 1.9 -# GET [var] | POST [var] | FILE [var {name|tmpname|size|type}] 1.10 -# urlencode string | htmlentities string | httpinfo 1.11 +# GET [var [index]] | POST [var [index]] | COOKIE [var [index]] 1.12 +# FILE [var {name|tmpname|size|type}] 1.13 +# header [strings]... | urlencode string | htmlentities string | httpinfo 1.14 +# 1.15 1.16 alias urlencode='httpd -e' 1.17 1.18 @@ -20,28 +22,16 @@ 1.19 echo $1 | sed 's|&|\&|g;s|<|\<|g;s|>|\>|g;s|"|\"|g' 1.20 } 1.21 1.22 -GET() 1.23 +_ARRAY() 1.24 { 1.25 -local n 1.26 -n=$2 1.27 -[ -n "$n" ] || n=1 1.28 -[ -z "$1" ] && echo $GET__NAMES || 1.29 - [ -n "$GET__NAMES" ] && eval echo \$GET_${1}_$n 1.30 +[ -z "$2" ] && eval echo \$${1}__NAMES || 1.31 + [ -n "$(eval echo \$${1}__NAMES)" ] && eval echo \$${1}_${2}_${3:-1} 1.32 } 1.33 1.34 -POST() 1.35 -{ 1.36 -local n 1.37 -n=$2 1.38 -[ -n "$n" ] || n=1 1.39 -[ -z "$1" ] && echo $POST__NAMES || 1.40 - [ -n "$POST__NAMES" ] && eval echo \$POST_${1}_$n 1.41 -} 1.42 - 1.43 -FILE() 1.44 -{ 1.45 -[ -z "$1" ] && echo $FILE__NAMES || [ -n "$FILE__NAMES" ] && eval echo \$FILE_${1}_$2 1.46 -} 1.47 +GET() { _ARRAY GET "$1" $2; } 1.48 +POST() { _ARRAY POST "$1" $2; } 1.49 +FILE() { _ARRAY FILE "$1" $2; } 1.50 +COOKIE() { _ARRAY COOKIE "$1" $2; } 1.51 1.52 httpinfo() 1.53 { 1.54 @@ -57,27 +47,20 @@ 1.55 eval x=\$$i 1.56 [ -n "$x" ] && echo "$i='$x'" 1.57 done 1.58 -for i in $GET__NAMES ; do 1.59 - if [ $(GET $i count) -gt 1 ]; then 1.60 - for j in $(seq 1 $(GET $i count)); do 1.61 - echo "GET[$i][$j]='$(GET $i $j)'" 1.62 - done 1.63 - else 1.64 - echo "GET[$i]='$(GET $i)'" 1.65 - fi 1.66 +for n in GET POST COOKIE ; do 1.67 + for i in $($n) ; do 1.68 + if [ $($n $i count) -gt 1 ]; then 1.69 + for j in $(seq 1 $($n $i count)); do 1.70 + echo "$n($i,$j)='$($n $i $j)'" 1.71 + done 1.72 + else 1.73 + echo "$n($i)='$($n $i)'" 1.74 + fi 1.75 + done 1.76 done 1.77 -for i in $POST__NAMES ; do 1.78 - if [ $(POST $i count) -gt 1 ]; then 1.79 - for j in $(seq 1 $(POST $i count)); do 1.80 - echo "POST[$i][$j]='$(POST $i $j)'" 1.81 - done 1.82 - else 1.83 - echo "POST[$i]='$(POST $i)'" 1.84 - fi 1.85 -done 1.86 -for i in $FILE__NAMES ; do 1.87 +for i in $(FILE) ; do 1.88 for j in name size type tmpname ; do 1.89 - echo "FILE[$i][$j]='$(FILE $i $j)'" 1.90 + echo "FILE($i,$j)='$(FILE $i $j)'" 1.91 done 1.92 done 1.93 } 1.94 @@ -89,14 +72,12 @@ 1.95 local cnt 1.96 names="" 1.97 IFS="&" 1.98 -for i in $QUERY_STRING ; do 1.99 +for i in $2 ; do 1.100 var=${i%%=*} 1.101 case " $names " in 1.102 - *\ $var\ *) 1.103 - eval cnt=\$${1}_${var}_count ;; 1.104 - *) 1.105 - cnt=0 1.106 - names="$names $var" ;; 1.107 + *\ $var\ *) eval cnt=\$${1}_${var}_count ;; 1.108 + *) cnt=0 1.109 + names="$names $var" ;; 1.110 esac 1.111 eval ${1}_${var}_count=$((++cnt)) 1.112 eval ${1}_${var}_$cnt=\'$(httpd -d "${i#*=}" | sed "s/'/\'\\\\\'\'/g")\' 1.113 @@ -105,7 +86,9 @@ 1.114 eval ${1}__NAMES=\'${names# }\' 1.115 } 1.116 1.117 -[ -z "$GET__NAMES" ] && read_query_string GET 1.118 +[ -z "$GET__NAMES" ] && read_query_string GET "$QUERY_STRING" 1.119 +[ -z "$COOKIE_NAMES" ] && 1.120 + read_query_string COOKIE "$(echo "$HTTP_COOKIE" | sed 's/; /\&/g')" 1.121 1.122 ddcut() 1.123 { 1.124 @@ -191,8 +174,7 @@ 1.125 rm -f $i 1.126 done 1.127 rmdir $(dirname $post) ;; 1.128 - *) export QUERY_STRING="$delim" 1.129 - rm -rf $(dirname $post) 1.130 - read_query_string POST ;; 1.131 + *) rm -rf $(dirname $post) 1.132 + read_query_string POST "$delim" ;; 1.133 esac 1.134 fi