-
Notifications
You must be signed in to change notification settings - Fork 200
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
rochajoel
wants to merge
2
commits into
avleen:master
Choose a base branch
from
rochajoel:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
# | ||
# Original author: Avleen Vig, 2012 | ||
# Reworked by: Josh Cartwright, 2012 | ||
# [email protected], 2017 | ||
|
||
warn() { echo "WARNING: $@" >&2; } | ||
|
||
|
@@ -122,6 +123,7 @@ add_response_header() { | |
} | ||
|
||
declare -a HTTP_RESPONSE=( | ||
[100]="Continue" | ||
[200]="OK" | ||
[400]="Bad Request" | ||
[403]="Forbidden" | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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() | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
source "${BASH_SOURCE[0]%/*}"/bashttpd.conf | ||
fail_with 500 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 charactersThere was a problem hiding this comment.
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.