Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for POST + Binary files support #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions bashttpd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# Original author: Avleen Vig, 2012
# Reworked by: Josh Cartwright, 2012
# [email protected], 2017

warn() { echo "WARNING: $@" >&2; }

Expand Down Expand Up @@ -122,6 +123,7 @@ add_response_header() {
}

declare -a HTTP_RESPONSE=(
[100]="Continue"
[200]="OK"
[400]="Bad Request"
[403]="Forbidden"
Expand All @@ -130,22 +132,27 @@ declare -a HTTP_RESPONSE=(
[500]="Internal Server Error"
)

send_response() {
send_http_headers() {
local code=$1
send "HTTP/1.0 $1 ${HTTP_RESPONSE[$1]}"
for i in "${RESPONSE_HEADERS[@]}"; do
send "$i"
done
send
}

send_response_ok_exit() {
send_http_headers 200;

while read -r line; do
send "$line"
done
}

send_response_ok_exit() { send_response 200; exit 0; }
exit 0;
}

fail_with() {
send_response "$1" <<< "$1 ${HTTP_RESPONSE[$1]}"
send_http_headers "$1" <<< "$1 ${HTTP_RESPONSE[$1]}"
exit 1
}

Expand All @@ -170,7 +177,11 @@ serve_file() {
read -r CONTENT_LENGTH < <(stat -c'%s' "$file") && \
add_response_header "Content-Length" "$CONTENT_LENGTH"

send_response_ok_exit < "$file"
send_http_headers 200

cat "$file"

exit 0
}

serve_dir_with_tree()
Expand Down Expand Up @@ -264,7 +275,7 @@ read -r REQUEST_METHOD REQUEST_URI REQUEST_HTTP_VERSION <<<"$line"
|| fail_with 400

# Only GET is supported at this time
[ "$REQUEST_METHOD" = "GET" ] || fail_with 405
[ "$REQUEST_METHOD" = "GET" ] || [ "$REQUEST_METHOD" = "POST" ] || fail_with 405

declare -a REQUEST_HEADERS

Expand All @@ -274,9 +285,13 @@ while read -r line; do

# If we've reached the end of the headers, break.
[ -z "$line" ] && break

[ "$REQUEST_METHOD" = "POST" ] && [[ $line == Content-Length:* ]] && POST_CONTENT_LENGTH=${line##* }

REQUEST_HEADERS+=("$line")
done

[ "$REQUEST_METHOD" = "POST" ] && read -N $POST_CONTENT_LENGTH POST_PARAMETERS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to read here with and -r to not allow backslashes in the request body content to escape any characters

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribute.

Bear in mind this project is not recommended for 'production environments' at all. This is just a proof of concept, good for prototyping, and at best, something you could use on a safe environment such as a small lab network, intranet, or some IPv4 island of your own.

That being said, i consider the 'code injection' as a pretty neat feature, not a bug. Thanks for helping pointing that out to others.


source "${BASH_SOURCE[0]%/*}"/bashttpd.conf
fail_with 500