1 | #! /bin/bash -wT |
---|
2 | |
---|
3 | # (internal) routine to store POST data |
---|
4 | function cgi_get_POST_vars() { |
---|
5 | # check content type |
---|
6 | # FIXME: not sure if we could handle uploads with this.. |
---|
7 | #if [ "${CONTENT_TYPE}" != "application/x-www-form-urlencoded" ]; then |
---|
8 | # return |
---|
9 | #fi |
---|
10 | # save POST variables (only first time this is called) |
---|
11 | [ -z "$QUERY_STRING_POST" \ |
---|
12 | -a "$REQUEST_METHOD" = "POST" -a ! -z "$CONTENT_LENGTH" ] && \ |
---|
13 | read -n $CONTENT_LENGTH QUERY_STRING_POST |
---|
14 | |
---|
15 | #echo $QUERY_STRING_POST |
---|
16 | return |
---|
17 | } |
---|
18 | |
---|
19 | |
---|
20 | function cgi_decodevar() { |
---|
21 | local url_encoded="${1//+/ }" |
---|
22 | printf '%b' "${url_encoded//%/\\x}" |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | # routine to get variables from http requests |
---|
27 | # usage: cgi_getvars method varname1 [.. varnameN] |
---|
28 | # method is either GET or POST or BOTH |
---|
29 | # the magic varible name ALL gets everything |
---|
30 | function cgi_getvars() { |
---|
31 | [ $# -lt 2 ] && return |
---|
32 | local q="" p k v s |
---|
33 | # get query |
---|
34 | case $1 in |
---|
35 | GET) |
---|
36 | [ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&" |
---|
37 | ;; |
---|
38 | POST) |
---|
39 | cgi_get_POST_vars |
---|
40 | [ ! -z "${QUERY_STRING_POST}" ] && q="${QUERY_STRING_POST}&" |
---|
41 | ;; |
---|
42 | BOTH) |
---|
43 | [ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&" |
---|
44 | cgi_get_POST_vars |
---|
45 | [ ! -z "${QUERY_STRING_POST}" ] && q="${q}${QUERY_STRING_POST}&" |
---|
46 | |
---|
47 | |
---|
48 | ;; |
---|
49 | esac |
---|
50 | shift |
---|
51 | s=" $* " |
---|
52 | # parse the query data |
---|
53 | while [ ! -z "$q" ]; do |
---|
54 | p="${q%%&*}" # get first part of query string |
---|
55 | k="${p%%=*}" # get the key (variable name) from it |
---|
56 | v="${p#*=}" # get the value from it |
---|
57 | q="${q#$p&*}" # strip first part from query string |
---|
58 | # decode and evaluate var if requested |
---|
59 | [ "$1" = "ALL" -o "${s/ $k /}" != "$s" ] && \ |
---|
60 | eval "$k=\"$(cgi_decodevar $v | sed 's/[\"\\\$]/\\&/g')\"" |
---|
61 | |
---|
62 | done |
---|
63 | return |
---|
64 | } |
---|
65 | |
---|
66 | function debug() { |
---|
67 | /bin/cat <<EOF |
---|
68 | Content-type: text/html |
---|
69 | |
---|
70 | |
---|
71 | <html> |
---|
72 | <head><title>Test</title></head> |
---|
73 | <body> |
---|
74 | <h1>Test for Bash CGI-Scripting</h1> |
---|
75 | <h2>POST values from Standard-In</h2> |
---|
76 | <pre> |
---|
77 | $(</dev/stdin) |
---|
78 | </pre> |
---|
79 | <h2>Values</h2> |
---|
80 | <pre> |
---|
81 | Auth: $AUTH |
---|
82 | Method: $METHOD |
---|
83 | id: $id |
---|
84 | script: $script |
---|
85 | redirect_uri: $redirect_uri |
---|
86 | </pre> |
---|
87 | <h2>Environment</h2> |
---|
88 | <pre>$(env)</pre> |
---|
89 | </body> |
---|
90 | </html |
---|
91 | EOF |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | AUTH=$HTTP_AUTHORIZATION |
---|
96 | METHOD=$REQUEST_METHOD |
---|
97 | |
---|
98 | #echo $AUTH |
---|
99 | #echo $METHOD |
---|
100 | #echo $CONTENT_LENGTH |
---|
101 | |
---|
102 | cgi_getvars BOTH ALL |
---|
103 | |
---|
104 | debug |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | #exec >&- |
---|
109 | #exec 2>&- |
---|
110 | |
---|
111 | # Una vez comprobado que el request es correcto, ejecutamos los comandos y liberamos la conexion |
---|
112 | nohup /bin/bash -c "./ogAgent.sh $id \"$script\" \"$redirect_uri\"" &>/dev/null & |
---|
113 | |
---|