diff options
author | KatolaZ <katolaz@freaknet.org> | 2018-07-29 08:07:11 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2018-07-29 08:07:11 +0100 |
commit | d25f120c10c07199844078e02436c549e3876f20 (patch) | |
tree | f8d3f12131b47acae2efcb7a9be765db083bbd42 | |
parent | a653827ff773eca46a0190f68718630f0a6286fd (diff) |
added preliminary support for CGI
-rwxr-xr-x | gosher | 77 |
1 files changed, 67 insertions, 10 deletions
@@ -19,11 +19,11 @@ ## ## gosher is a simple Gopher server in a POSIX shell script: ## -## $ ./gosher [<PORT> [<GOPHERDIR>] -## -## If PORT is not specified, the default is 70. If GOPHERDIR is not -## specified, "./" is assumed +## $ ./gosher [<GOPHERDIR> [<HOSTNAME> [<PORT>]]] ## +## If GOPHERDIR is not specified, "./" is assumed. +## If HOSTNAME is not specified, "localhost" is used. +## If PORT is not specified, the default port "70" is used. ## ## (c) 2018 Vincenzo 'KatolaZ' Nicosia <katolaz@freaknet.org> ## @@ -82,9 +82,9 @@ NETCAT=$(which "$NETCAT") if [ -z "${MYNAME#gosher}" ]; then ## we are called as gosher -- launch the server - - PORT=${1:-70} - GOPHERDIR=${2:-"./"} + GOPHERDIR=${1:-"./"} + HOSTNAME=${2:-"localhost"} + PORT=${3:-70} [ ! -f "${NETCAT}" ] || [ ! -x "${NETCAT}" ] && { echo "Wrong NETCAT -- Exiting" >&2 @@ -101,7 +101,9 @@ if [ -z "${MYNAME#gosher}" ]; then # shellcheck disable=SC2050 while [ 1 -eq 1 ]; do # shellcheck disable=SC2094 - ${GOSHER_SERVE} "${GOPHERDIR}" <"$INF" | ${NETCAT} -vvvvv -l -p "${PORT}" >"$INF" + ${GOSHER_SERVE} "${GOPHERDIR}" \ + "${HOSTNAME}" "${PORT}" <"$INF" |\ + ${NETCAT} -vvvvv -l -p "${PORT}" >"$INF" done rm -f $INF exit 0 @@ -109,7 +111,8 @@ if [ -z "${MYNAME#gosher}" ]; then [ "$STYLE" = 'fork' ] && { # shellcheck disable=SC2050 while [ 1 -eq 1 ]; do - ${NETCAT} -vv -l -p "$PORT" -c "${GOSHER_SERVE} ${GOPHERDIR}" + ${NETCAT} -vv -l -p "$PORT" -c \ + "${GOSHER_SERVE} ${GOPHERDIR} ${HOSTNAME} ${PORT}" done exit 0 } @@ -197,10 +200,36 @@ EOF } - +### Serve a CGI -- Set the environment and run the corresponding script +## function +serve_cgi(){ + script_name=$( echo "$1" | sed -r 's:^/+::') + query_string="$2" + echo "script_name: ${script_name}" >&2 + GATEWAY_INTERFACE="CGI/1.1" + PATH_INFO="${script_name}" + PATH_TRANSLATED="${script_name}" + QUERY_STRING="${query_string}" + REMOTE_ADDR= + REMOTE_HOST="${REMOTE_ADDR}" + REQUEST_METHOD="GET" + SCRIPT_NAME="${script_name}" + SERVER_NAME="${HOSTNAME}" + SERVER_PORT="${PORT}" + SERVER_PROTOCOL="gopher/1.0" + SERVER_SOFTWARE="gosher" + ####X_GOPHER_SEARCH= search (See above.) + export GATEWAY_INTERFACE PATH_INFO PATH_TRANSLATED QUERY_STRING + export REMOTE_ADDR REMOTE_HOST REQUEST_METHOD SCRIPT_NAME + export SERVER_NAME SERVER_PORT SERVER_PROTOCOL SERVER_SOFTWARE + ${GOPHERDIR}${script_name} "" "${query_string}" "${HOSTNAME}" ${PORT} + exit 0 +} GOPHERDIR=${1:-"./"} +HOSTNAME=${2:-"localhost"} +PORT=${3:-"70"} read -r selector @@ -213,10 +242,36 @@ selector=$(echo "$selector" | sed -r 's:\$.*::g;s:\r::g' ) case $selector in URL:*) + ## it's a special URL selector url=$(echo "$selector" | cut -d ":" -f 2-) serve_redirect "$url" ;; + /?*.cgi*) + ## it's a CGI + script_name=$(echo "$selector" | cut -d "?" -f 1) + query_string=$(echo "$selector" | cut -d "?" -f 2) + [ "${script_name}" = "${query_string}" ] && query_string="" + RP1=$(realpath "${GOPHERDIR}""${script_name}" || "") +# shellcheck disable=SC2181 + [ $? -eq 0 ] || invalid_selector "${selector}" + RP2=$(realpath "${GOPHERDIR}")"${script_name}" + RP2=$(echo "${RP2}" | sed -r 's/\/+/\//g') +# shellcheck disable=SC2181 + [ $? -eq 0 ] || invalid_selector "${selector}" + [ -n "$DEBUG" ] && { + echo "iRP1: ${RP1}" + echo "iRP2: ${RP2}" + } + + if [ "${RP1}" = "${RP2}" ]; then + [ -x "${RP1}" ] && { + serve_cgi "${script_name}" "${query_string}" + } + fi + invalid_selector "${selector}" + ;; /?*|"") + ## it's a regular selector RP1=$(realpath "${GOPHERDIR}"/"${selector}" || "") # shellcheck disable=SC2181 [ $? -eq 0 ] || invalid_selector "$selector" @@ -243,6 +298,8 @@ case $selector in invalid_selector "$selector" ;; *) + ## we don't know what it is -- try to default to a + ## gophermap [ -f "${GOPHERDIR}/gophermap" ] && serve_selector "${GOPHERDIR}/gophermap" [ -f "${GOPHERDIR}/index.gph" ] && serve_index "${GOPHERDIR}/index.gph" echo "got invalid selector: \"$selector\"" >&2 |