Skip to content

first commit #9

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
* More unit tests
* Content-Range support
* Reverse proxy mode
* If-Modified-Since support
* Content-Disposition
Expand Down
35 changes: 31 additions & 4 deletions shellweb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -e
verbose=false
auth_enabled=false
proxy_mode=false
content_range=(false 0 0 0)
nc=$(command -v nc.openbsd || command -v nc || { echo nc not found; exit 1; })

if [ `id -u` = 0 ]; then
Expand Down Expand Up @@ -193,6 +194,9 @@ cgi() {

# It's okay to call this routine even after senderr(), see $sent
sendfile() {
if [[ ${content_range[0]} == 'true' ]]; then
range_handle_verify "$root/$1"
fi
$sent && return 0
test -d "$1" && return
exec 3<"$root/$1" || { senderr 500; return 0; }
Expand All @@ -203,9 +207,15 @@ sendfile() {
printf "Content-Type: %s\r\n" "$ct"
printf "Content-Length: %u\r\n" "$sz"
printf "Date: %s\r\n" "$(env LC_ALL=en_US.UTF-8 date)"
printf "\r\n"
cat <&3
exec 3<&-
if [[ ${content_range[0]} == 'true' ]]; then
printf "Content-Range: bytes ${content_range[1]}-${content_range[2]}/${content_range[3]}\r\n"
printf "\r\n"
range_sendfile "$root/$1"
else
printf "\r\n"
cat <&3
exec 3<&-
fi
}

html_escape() {
Expand Down Expand Up @@ -337,7 +347,19 @@ proxy() {
fi
done | $nc ${proxy_spec%%:*} $proxy_port >&p
}


range_handle_verify() {
content_range[3]=$(stat --printf="%s" "$1")
if [[ ${content_range[3]} -ne 0 && ${content_range[1]} -ge 0 && ${content_range[2]} -le ${content_range[3]} ]]; then
return 0
else
senderr 400
fi
}

range_sendfile(){
dd if="$1" ibs=1 skip=${content_range[1]} count=${content_range[2]} 2> /dev/null
}

NC_SERVER=$nc
if $verbose; then
Expand Down Expand Up @@ -371,6 +393,11 @@ while true; do

elif [[ $header == "connection:" && $v2 == $(printf "close\r") ]]; then
keep_conn=false

elif [[ $header == "range:" ]]; then
content_range[0]='true'
content_range[1]=$(echo "$v3" | awk -F '-' '{print $1}')
content_range[2]=$(echo "$v3" | awk -F '-' '{print $2}' | sed 's/[^0-9]*//g')

elif [[ $header = $(printf "\r\n") && -z $v2 ]]; then
# empty line (end of headers)
Expand Down