From 4bcc0519c3e9bd07fe9d8e126cb0f0d9e8d258d7 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Sun, 16 Sep 2012 21:01:28 -0400 Subject: [PATCH 1/9] Removed reliance on the external "date" command --- bashttpd.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bashttpd.sh b/bashttpd.sh index 6a69702..7fe85e6 100755 --- a/bashttpd.sh +++ b/bashttpd.sh @@ -17,7 +17,13 @@ fi # Use default /var/www/html if DOCROOT is not set. : ${DOCROOT:=/var/www/html} -DATE=$( date +"%a, %d %b %Y %H:%M:%S %Z" ) +HISTTIMEFORMAT="%a, %d %b %Y %H:%M:%S %Z" +set -o history +foo=$(history) +set +o history +DATE=$(echo ${foo%foo*}) +unset foo + REPLY_HEADERS="Date: ${DATE} Expires: ${DATE} Server: Slash Bin Slash Bash" From a2ebf7c4aa7afe67e0ade6ca3289c18ab96db2cb Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Sun, 16 Sep 2012 21:53:38 -0400 Subject: [PATCH 2/9] Removed the cut command --- bashttpd.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bashttpd.sh b/bashttpd.sh index 7fe85e6..3896ffd 100755 --- a/bashttpd.sh +++ b/bashttpd.sh @@ -76,7 +76,10 @@ while read line; do # Look for a GET request if [[ $line == GET* ]]; then - URL_PATH="${DOCROOT}$( echo ${line} | cut -d' ' -f2 )" + read ignore ignore path ignore <<< "$line" + unset ignore + URL_PATH="${DOCROOT}${path}" + unset path filter_url ${URL_PATH} fi done From 867a6de06f2397cd4d9479df6152928d2c7b6d14 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Sun, 16 Sep 2012 21:58:19 -0400 Subject: [PATCH 3/9] Removed use of cat --- bashttpd.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashttpd.sh b/bashttpd.sh index 3896ffd..afb5f24 100755 --- a/bashttpd.sh +++ b/bashttpd.sh @@ -42,9 +42,9 @@ function get_content_body() { URL_PATH=$1 CONTENT_TYPE=$2 if [[ ${CONTENT_TYPE} =~ "^text" ]]; then - CONTENT_BODY="$( cat ${URL_PATH} )" + CONTENT_BODY="$( < ${URL_PATH} )" else - CONTENT_BODY="$( cat ${URL_PATH} )" + CONTENT_BODY="$( < ${URL_PATH} )" fi } From 833ce53ef3fd1b8deebcf43a24345b43fd2dee8a Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Sun, 16 Sep 2012 22:17:14 -0400 Subject: [PATCH 4/9] Removed useless tr and replaced echo with the safer printf --- bashttpd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashttpd.sh b/bashttpd.sh index afb5f24..b9bf151 100755 --- a/bashttpd.sh +++ b/bashttpd.sh @@ -69,7 +69,7 @@ fi while read line; do # If we've reached the end of the headers, break. - line=$( echo ${line} | tr -d '\r' ) + line=$( printf '%s ' ${line} ) if [ -z "$line" ]; then break fi From 1ac1b280fe17aaf721c2a027132a86539903d472 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Thu, 20 Sep 2012 07:15:02 -0400 Subject: [PATCH 5/9] Fixed GET parameter order --- bashttpd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashttpd.sh b/bashttpd.sh index b9bf151..5eb6fc9 100755 --- a/bashttpd.sh +++ b/bashttpd.sh @@ -76,7 +76,7 @@ while read line; do # Look for a GET request if [[ $line == GET* ]]; then - read ignore ignore path ignore <<< "$line" + read ignore path ignore <<< "$line" unset ignore URL_PATH="${DOCROOT}${path}" unset path From 322f3c00d2e04375b88bd837cc05c14c5c3b03f1 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Thu, 20 Sep 2012 07:17:38 -0400 Subject: [PATCH 6/9] Possible fix for bash 3.2 --- bashttpd.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bashttpd.sh b/bashttpd.sh index 5eb6fc9..679f2ea 100755 --- a/bashttpd.sh +++ b/bashttpd.sh @@ -17,11 +17,12 @@ fi # Use default /var/www/html if DOCROOT is not set. : ${DOCROOT:=/var/www/html} -HISTTIMEFORMAT="%a, %d %b %Y %H:%M:%S %Z" +unset HISTFILE +HISTTIMEFORMAT="%a, %d %b %Y %H:%M:%S %Zfoo" set -o history -foo=$(history) +read foo <<< "$(history)" set +o history -DATE=$(echo ${foo%foo*}) +DATE=$(echo ${foo%%foo*}) unset foo REPLY_HEADERS="Date: ${DATE} From 2bc77a8e8f8025e82c3642b52cd47c1d9d4ed765 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Thu, 20 Sep 2012 07:25:27 -0400 Subject: [PATCH 7/9] Removed file extension with git mv (this should make the merge work better) --- bashttpd.sh => bashttpd | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bashttpd.sh => bashttpd (100%) diff --git a/bashttpd.sh b/bashttpd similarity index 100% rename from bashttpd.sh rename to bashttpd From f9c52953e6abb2807d23c5d580c9d9fb53b14e37 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Thu, 20 Sep 2012 08:06:36 -0400 Subject: [PATCH 8/9] This is cleaner than the previous version, but I don't know whether it works for older bash --- bashttpd | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/bashttpd b/bashttpd index 6b562d2..7831262 100755 --- a/bashttpd +++ b/bashttpd @@ -110,13 +110,7 @@ send() { echo "> $@" >&2; [[ $UID = 0 ]] && warn "It is not recommended to run bashttpd as root." -unset HISTFILE -HISTTIMEFORMAT="%a, %d %b %Y %H:%M:%S %Zfoo" -set -o history -read foo <<< "$(history)" -set +o history -DATE=$(echo ${foo%%foo*}) -unset foo +printf -v DATE '%(%a, %d %b %Y %H:%M:%S %Z)T' -1 declare -a RESPONSE_HEADERS=( "Date: $DATE" From 5b4762b1f087141899e00b30fd12f95443ac4eb5 Mon Sep 17 00:00:00 2001 From: Marc Aldorasi Date: Thu, 20 Sep 2012 12:29:47 -0400 Subject: [PATCH 9/9] Removed stat call --- bashttpd | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bashttpd b/bashttpd index 7831262..4f61f0f 100755 --- a/bashttpd +++ b/bashttpd @@ -155,7 +155,16 @@ serve_file() { read -r CONTENT_TYPE < <(file -b --mime-type "$file") && \ add_response_header "Content-Type" "$CONTENT_TYPE" - read -r CONTENT_LENGTH < <(stat -c'%s' "$file") && \ + CONTENT_LENGTH=$( + LC_ALL=C + IFS="" + a=0 + while read -r -d '' piece; do + a=$((a+${#piece}+1)) + done < "$file" + a=$((a+${#piece})) + echo $a + ) && \ add_response_header "Content-Length" "$CONTENT_LENGTH" send_response_ok_exit < "$file"