diff options
| -rw-r--r-- | cgi/bashlib.sh | 235 | ||||
| -rw-r--r-- | cgi/servers.sh | 8 | ||||
| -rw-r--r-- | fileparts/footer.html | 6 | ||||
| -rw-r--r-- | fileparts/header.html (renamed from index.html) | 15 | ||||
| -rw-r--r-- | index.sh | 16 |
5 files changed, 263 insertions, 17 deletions
diff --git a/cgi/bashlib.sh b/cgi/bashlib.sh new file mode 100644 index 0000000..58e8ac2 --- /dev/null +++ b/cgi/bashlib.sh @@ -0,0 +1,235 @@ +#!/bin/bash + +# Author: darren chamberlain <dlc@users.sourceforge.net> +# Co-Author: Paul Bournival <paulb-ns@cajun.nu> +# +# bashlib is used by sourcing it at the beginning of scripts that +# needs its functionality (by using the . or source commands). + +PATH=/bin:/usr/bin + +# +# Set version number +# +VERSION=$(@ECHO@ '$Revision: 1.3 $' | @AWK@ '{print $2}') + +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# Initialization stuff begins here. These things run immediately, and +# do the parameter/cookie parsing. +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +# Global debug flag. Set to 0 to disable debugging throughout the lib +DEBUG=0 + +# capture stdin for POST methods. POST requests don't always come in +# with a newline attached, so we use cat to grab stdin and append a newline. +# This is a wonderful hack, and thanks to paulb. +STDIN=$(@CAT@) +if [ -n "${STDIN}" ]; then + QUERY_STRING="${STDIN}&${QUERY_STRING}" +fi + +# Handle GET and POST requests... (the QUERY_STRING will be set) +if [ -n "${QUERY_STRING}" ]; then + # name=value params, separated by either '&' or ';' + if echo ${QUERY_STRING} | grep '=' >/dev/null ; then + for Q in $(@ECHO@ ${QUERY_STRING} | @TR@ ";&" "\012") ; do + # + # Clear our local variables + # + unset name + unset value + unset tmpvalue + + # + # get the name of the key, and decode it + # + name=${Q%%=*} + name=$(@ECHO@ ${name} | \ + @SED@ -e 's/%\(\)/\\\x/g' | \ + @TR@ "+" " ") + name=$(@ECHO@ ${name} | \ + @TR@ -d ".-") + name=$(@PRINTF@ ${name}) + + # + # get the value and decode it. This is tricky... printf chokes on + # hex values in the form \xNN when there is another hex-ish value + # (i.e., a-fA-F) immediately after the first two. My (horrible) + # solution is to put a space aftet the \xNN, give the value to + # printf, and then remove it. + # + tmpvalue=${Q#*=} + tmpvalue=$(@ECHO@ ${tmpvalue} | \ + @SED@ -e 's/%\(..\)/\\\x\1 /g') + #echo "Intermediate \$value: ${tmpvalue}" 1>&2 + + # + # Iterate through tmpvalue and printf each string, and append it to + # value + # + for i in ${tmpvalue}; do + g=$(@PRINTF@ ${i}) + value="${value}${g}" + done + #value=$(echo ${value}) + + eval "export FORM_${name}='${value}'" + done + else # keywords: foo.cgi?a+b+c + Q=$(echo ${QUERY_STRING} | tr '+' ' ') + eval "export KEYWORDS='${Q}'" + fi +fi + +# +# this section works identically to the query string parsing code, +# with the (obvious) exception that variables are stuck into the +# environment with the prefix COOKIE_ rather than FORM_. This is to +# help distinguish them from the other variables that get set +# automatically. +# +if [ -n "${HTTP_COOKIE}" ]; then + for Q in ${HTTP_COOKIE}; do + # + # Clear our local variables + # + name= + value= + tmpvalue= + + # + # Strip trailing ; off the value + # + Q=${Q%;} + + # + # get the name of the key, and decode it + # + name=${Q%%=*} + name=$(@ECHO@ ${name} | \ + @SED@ -e 's/%\(\)/\\\x/g' | \ + @TR@ "+" " ") + name=$(@ECHO@ ${name} | \ + @TR@ -d ".-") + name=$(@PRINTF@ ${name}) + + # Decode the cookie value. See the parameter section above for + # an explanation of what this is doing. + tmpvalue=${Q#*=} + tmpvalue=$(@ECHO@ ${tmpvalue} | \ + @SED@ -e 's/%\(..\)/\\\x\1 /g') + #echo "Intermediate \$value: ${tmpvalue}" 1>&2 + + # + # Iterate through tmpvalue and printf each string, and append it to + # value + # + for i in ${tmpvalue}; do + g=$(@PRINTF@ ${i}) + value="${value}${g}" + done + #value=$(echo ${value}) + + # + # Export COOKIE_${name} into the environment + # + #echo "exporting COOKIE_${name}=${value}" 1>&2 + eval "export COOKIE_${name}='${value}'" + done +fi + +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# functions and all that groovy stuff +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +# +# Shameless plug, advertises verion. +function version { + echo "bashlib, version ${VERSION}" +} + +function version_html { + echo -n "<a href=\"http://sevenroot.org/software/bashlib/\">bashlib</a>," + echo "version ${VERSION}" +} + +# +# Parameter function. +# * When called with no arguments, returns a list of parameters that +# were passed in. +# * When called with one argument, returns the value of that parameter +# (if any) +# * When called with more than one argument, assumes that the first is a +# paramter name and the rest are values to be assigned to a paramter of +# that name. +# +function param { + local name + local value + if [ $# -eq 1 ]; then + name=$1 + name=$(echo ${name} | @SED@ -e 's/FORM_//') + value=$(@ENV@ | @GREP@ "^FORM_${name}" | @SED@ -e 's/FORM_//' | @CUT@ -d= -f2-) + elif [ $# -gt 1 ]; then + name=$1 + shift + eval "export 'FORM_${name}=$*'" + else + value=$(@ENV@ | @GREP@ '^FORM_' | @SED@ -e 's/FORM_//' | @CUT@ -d= -f1) + fi + echo ${value} + unset name + unset value +} + +# cookie function. Same explanation as param +function cookie { + local name + local value + if [ $# -eq 1 ]; then + name=$1 + name=$(echo ${name} | @SED@ -e 's/COOKIE_//') + value=$(@ENV@ | @GREP@ "^COOKIE_${name}" | @SED@ -e 's/COOKIE_//' | @CUT@ -d= -f2-) + elif [ $# -gt 1 ]; then + name=$1 + shift + eval "export 'COOKIE_${name}=$*'" + else + value=$(@ENV@ | @GREP@ '^COOKIE_' | @SED@ -e 's/COOKIE_//' | @CUT@ -d= -f1) + fi + echo ${value} + unset name + unset value +} + +# keywords returns a list of keywords. This is only set when the script is +# called with an ISINDEX form (these are pretty rare nowadays). +function keywords { + echo ${KEYWORDS} +} + +function set_cookie { + local name=$1 + shift + local value=$* + bashlib_cookies="${bashlib_cookies}; ${name}=${value}" + + bashlib_cookies=${bashlib_cookies#;} + + cookie $name $value +} + +# +# send_redirect takes a URI and redirects the browser to that uri, exiting +# the script along the way. +# +function send_redirect { + local uri + if [ $# -eq 1 ]; then + uri=$1 + else + uri="http://${SERVER_NAME}/${SCRIPT_NAME}" + fi + echo "Location: ${uri}" + echo "" +} diff --git a/cgi/servers.sh b/cgi/servers.sh index 073bc33..3c9590e 100644 --- a/cgi/servers.sh +++ b/cgi/servers.sh @@ -1,8 +1,12 @@ #!/bin/bash -. /usr/locl/lib/bashlib +./bashlib.sh echo "Content-type: text/html" echo "" -echo "<html>Hello, world!</html>" +echo "<html>" + +echo "Hello, world!" + +echo "</html>" diff --git a/fileparts/footer.html b/fileparts/footer.html new file mode 100644 index 0000000..0c96a0c --- /dev/null +++ b/fileparts/footer.html @@ -0,0 +1,6 @@ + <footer> + <id> + Page last modified on 12/19/15 - Contact the admin at <a href="mailto:admin@cogarr.org">admin@cogarr.org</a> + </id> + </footer> +</html> diff --git a/index.html b/fileparts/header.html index 1db3b5e..b5e19a5 100644 --- a/index.html +++ b/fileparts/header.html @@ -23,18 +23,3 @@ <div> <a style="text-decoration:line-through" href="./quickstart.html">quickstart</a> </div> - <id><hr></id> - <id><img style="width:32px" src="icon_home.png"><br/></id> - <body> - <p> - Welcome to Alec's Otherwise, 24/7 Winter Survival - We are playing a modified version of The Maw's winter survival on ws_abandon_night - </p> - </body> - <id><hr><br/></id> - <footer> - <id> - Page last modified on 12/19/15 - Contact the admin at <a href="mailto:admin@cogarr.org">admin@cogarr.org</a> - </id> - </footer> -</html> diff --git a/index.sh b/index.sh new file mode 100644 index 0000000..a7bbe70 --- /dev/null +++ b/index.sh @@ -0,0 +1,16 @@ + +echo "Content-type: text/html" +echo "" + +cat ./fileparts/header.html +echo ' <id><hr></id> + <id><img style="width:32px" src="icon_home.png"><br/></id> + <body> + <p> + Welcome to Alec's Otherwise, 24/7 Winter Survival + We are playing a modified version of The Maw's winter survival on ws_abandon_night + </p> + </body> + <id><hr><br/></id>' + +cat ./fileparts/footer.html |
