From dc77752eb7bfe528c60dc0ac028717877342e22a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 30 Dec 2010 08:32:21 +0000 Subject: coolio_thread_*: fix 416 response handling After beefing up and enabling byte range tests for "sendfile" (and no just IO.copy_stream), we noticed threaded-Coolio variants did not handle invalid byte ranges correctly. --- t/byte-range-common.sh | 118 ++++++++++++++++++++++++++++++++++++++ t/t0022-copy_stream-byte-range.sh | 118 +------------------------------------- t/t0023-sendfile-byte-range.sh | 34 +---------- 3 files changed, 122 insertions(+), 148 deletions(-) create mode 100644 t/byte-range-common.sh (limited to 't') diff --git a/t/byte-range-common.sh b/t/byte-range-common.sh new file mode 100644 index 0000000..120aad6 --- /dev/null +++ b/t/byte-range-common.sh @@ -0,0 +1,118 @@ +t_begin "byte-range setup vars" && { + random_blob_size=$(wc -c < random_blob) + rb_1=$(( $random_blob_size - 1 )) + range_head=-r-365 + range_tail=-r155- + range_mid=-r200-300 + range_n1=-r0-$rb_1 + range_n2=-r0-$(($rb_1 - 1)) + range_1b_head=-r0-0 + range_1b_tail=-r$rb_1-$rb_1 + range_1b_mid=-r200-200 + range_all=-r0-$random_blob_size + url=http://$listen/random_blob +} + +check_content_range () { + grep 'Range:' $err + # Content-Range: bytes #{offset}-#{offset+count-1}/#{clen} + d='\([0-9]\+\)' + start= end= size= + eval $(< $err sed -n -e \ + "s/^< Content-Range: bytes $d-$d\/$d"'.*$/start=\1 end=\2 size=\3/p') + test -n "$start" + test -n "$end" + test -n "$size" + + # ensure we didn't screw up the sed invocation + expect="< Content-Range: bytes $start-$end/$size" + test x"$(grep -F "$expect" $err)" = x"$(grep '^< Content-Range:' $err)" + + test $start -le $end + test $end -lt $size +} + +t_begin "read random blob sha1s" && { + sha1_head=$(curl -sSff $range_head file://random_blob | rsha1) + sha1_tail=$(curl -sSff $range_tail file://random_blob | rsha1) + sha1_mid=$(curl -sSff $range_mid file://random_blob | rsha1) + sha1_n1=$(curl -sSff $range_n1 file://random_blob | rsha1) + sha1_n2=$(curl -sSff $range_n2 file://random_blob | rsha1) + sha1_1b_head=$(curl -sSff $range_1b_head file://random_blob | rsha1) + sha1_1b_tail=$(curl -sSff $range_1b_tail file://random_blob | rsha1) + sha1_1b_mid=$(curl -sSff $range_1b_mid file://random_blob | rsha1) + sha1_all=$(rsha1 < random_blob) + echo "$sha1_all=$sha1_n1" +} + +t_begin "normal full request matches" && { + sha1="$(curl -v 2>$err -sSf $url | rsha1)" + test x"$sha1_all" = x"$sha1" + grep 'Content-Range:' $err && die "Content-Range unexpected" + grep 'HTTP/1.1 200 OK' $err || die "200 response expected" +} + +t_begin "crazy offset goes over" && { + range_insane=-r$(($random_blob_size * 2))-$(($random_blob_size * 4)) + curl -vsS 2>$err $range_insane $url + grep 'HTTP/1\.[01] 416 ' $err || die "expected 416 error" +} + +t_begin "full request matches with explicit ranges" && { + sha1="$(curl -v 2>$err $range_all -sSf $url | rsha1)" + check_content_range + test x"$sha1_all" = x"$sha1" + + sha1="$(curl -v 2>$err $range_n1 -sSf $url | rsha1)" + check_content_range + test x"$sha1_all" = x"$sha1" + + range_over=-r0-$(($random_blob_size * 2)) + sha1="$(curl -v 2>$err $range_over -sSf $url | rsha1)" + check_content_range + test x"$sha1_all" = x"$sha1" +} + +t_begin "no fence post errors" && { + sha1="$(curl -v 2>$err $range_n2 -sSf $url | rsha1)" + check_content_range + test x"$sha1_n2" = x"$sha1" + + sha1="$(curl -v 2>$err $range_1b_head -sSf $url | rsha1)" + check_content_range + test x"$sha1_1b_head" = x"$sha1" + + sha1="$(curl -v 2>$err $range_1b_tail -sSf $url | rsha1)" + check_content_range + test x"$sha1_1b_tail" = x"$sha1" + + sha1="$(curl -v 2>$err $range_1b_mid -sSf $url | rsha1)" + check_content_range + test x"$sha1_1b_mid" = x"$sha1" +} + +t_begin "head range matches" && { + sha1="$(curl -sSfv 2>$err $range_head $url | rsha1)" + check_content_range + test x"$sha1_head" = x"$sha1" +} + +t_begin "tail range matches" && { + sha1="$(curl -sSfv 2>$err $range_tail $url | rsha1)" + check_content_range + test x"$sha1_tail" = x"$sha1" +} + +t_begin "mid range matches" && { + sha1="$(curl -sSfv 2>$err $range_mid $url | rsha1)" + check_content_range + test x"$sha1_mid" = x"$sha1" +} + +t_begin "shutdown server" && { + kill -QUIT $rainbows_pid +} + +t_begin "check stderr" && check_stderr + +t_done diff --git a/t/t0022-copy_stream-byte-range.sh b/t/t0022-copy_stream-byte-range.sh index 0b14ee2..e4fcdbf 100755 --- a/t/t0022-copy_stream-byte-range.sh +++ b/t/t0022-copy_stream-byte-range.sh @@ -17,7 +17,7 @@ ThreadSpawn|WriterThreadSpawn|ThreadPool|WriterThreadPool|Base) ;; ;; esac -t_plan 11 "IO.copy_stream byte range response for $model" +t_plan 12 "IO.copy_stream byte range response for $model" t_begin "setup and startup" && { rtmpfiles out err @@ -25,120 +25,6 @@ t_begin "setup and startup" && { # can't load Rack::Lint here since it clobbers body#to_path rainbows -E none -D large-file-response.ru -c $unicorn_config rainbows_wait_start - random_blob_size=$(wc -c < random_blob) - rb_1=$(( $random_blob_size - 1 )) - range_head=-r-365 - range_tail=-r155- - range_mid=-r200-300 - range_n1=-r0-$rb_1 - range_n2=-r0-$(($rb_1 - 1)) - range_1b_head=-r0-0 - range_1b_tail=-r$rb_1-$rb_1 - range_1b_mid=-r200-200 - range_all=-r0-$random_blob_size - url=http://$listen/random_blob } -check_content_range () { - # Content-Range: bytes #{offset}-#{offset+count-1}/#{clen} - d='\([0-9]\+\)' - start= end= size= - eval $(< $err sed -n -e \ - "s/^< Content-Range: bytes $d-$d\/$d"'.*$/start=\1 end=\2 size=\3/p') - test -n "$start" - test -n "$end" - test -n "$size" - - # ensure we didn't screw up the sed invocation - expect="< Content-Range: bytes $start-$end/$size" - test x"$(grep -F "$expect" $err)" = x"$(grep '^< Content-Range:' $err)" - - test $start -le $end - test $end -lt $size - grep 'Range:' $err -} - -t_begin "read random blob sha1s" && { - sha1_head=$(curl -sSff $range_head file://random_blob | rsha1) - sha1_tail=$(curl -sSff $range_tail file://random_blob | rsha1) - sha1_mid=$(curl -sSff $range_mid file://random_blob | rsha1) - sha1_n1=$(curl -sSff $range_n1 file://random_blob | rsha1) - sha1_n2=$(curl -sSff $range_n2 file://random_blob | rsha1) - sha1_1b_head=$(curl -sSff $range_1b_head file://random_blob | rsha1) - sha1_1b_tail=$(curl -sSff $range_1b_tail file://random_blob | rsha1) - sha1_1b_mid=$(curl -sSff $range_1b_mid file://random_blob | rsha1) - sha1_all=$(rsha1 < random_blob) - echo "$sha1_all=$sha1_n1" -} - -t_begin "normal full request matches" && { - sha1="$(curl -v 2>$err -sSf $url | rsha1)" - test x"$sha1_all" = x"$sha1" - grep 'Content-Range:' $err && die "Content-Range unexpected" - grep 'HTTP/1.1 200 OK' $err || die "200 response expected" -} - -t_begin "crazy offset goes over" && { - range_insane=-r$(($random_blob_size * 2))-$(($random_blob_size * 4)) - curl -vsS 2>$err $range_insane $url - grep 'HTTP/1\.[01] 416 ' $err || die "expected 416 error" -} - -t_begin "full request matches with explicit ranges" && { - sha1="$(curl -v 2>$err $range_all -sSf $url | rsha1)" - check_content_range - test x"$sha1_all" = x"$sha1" - - sha1="$(curl -v 2>$err $range_n1 -sSf $url | rsha1)" - check_content_range - test x"$sha1_all" = x"$sha1" - - range_over=-r0-$(($random_blob_size * 2)) - sha1="$(curl -v 2>$err $range_over -sSf $url | rsha1)" - check_content_range - test x"$sha1_all" = x"$sha1" -} - -t_begin "no fence post errors" && { - sha1="$(curl -v 2>$err $range_n2 -sSf $url | rsha1)" - check_content_range - test x"$sha1_n2" = x"$sha1" - - sha1="$(curl -v 2>$err $range_1b_head -sSf $url | rsha1)" - check_content_range - test x"$sha1_1b_head" = x"$sha1" - - sha1="$(curl -v 2>$err $range_1b_tail -sSf $url | rsha1)" - check_content_range - test x"$sha1_1b_tail" = x"$sha1" - - sha1="$(curl -v 2>$err $range_1b_mid -sSf $url | rsha1)" - check_content_range - test x"$sha1_1b_mid" = x"$sha1" -} - -t_begin "head range matches" && { - sha1="$(curl -sSfv 2>$err $range_head $url | rsha1)" - check_content_range - test x"$sha1_head" = x"$sha1" -} - -t_begin "tail range matches" && { - sha1="$(curl -sSfv 2>$err $range_tail $url | rsha1)" - check_content_range - test x"$sha1_tail" = x"$sha1" -} - -t_begin "mid range matches" && { - sha1="$(curl -sSfv 2>$err $range_mid $url | rsha1)" - check_content_range - test x"$sha1_mid" = x"$sha1" -} - -t_begin "shutdown server" && { - kill -QUIT $rainbows_pid -} - -t_begin "check stderr" && check_stderr - -t_done +. ./byte-range-common.sh diff --git a/t/t0023-sendfile-byte-range.sh b/t/t0023-sendfile-byte-range.sh index 419d89a..89fbd7a 100755 --- a/t/t0023-sendfile-byte-range.sh +++ b/t/t0023-sendfile-byte-range.sh @@ -17,7 +17,7 @@ EventMachine|NeverBlock) *) ;; esac -t_plan 7 "sendfile byte range response for $model" +t_plan 12 "sendfile byte range response for $model" t_begin "setup and startup" && { rtmpfiles out err @@ -28,36 +28,6 @@ t_begin "setup and startup" && { # can't load Rack::Lint here since it clobbers body#to_path rainbows -E none -D large-file-response.ru -c $unicorn_config rainbows_wait_start - range_head=-r-365 - range_tail=-r155- - range_mid=-r200-300 } -t_begin "read random blob sha1s" && { - sha1_head=$(curl -sSf $range_head file://random_blob | rsha1) - sha1_tail=$(curl -sSf $range_tail file://random_blob | rsha1) - sha1_mid=$(curl -sSf $range_mid file://random_blob | rsha1) -} - -t_begin "head range matches" && { - sha1="$(curl -sSv $range_head http://$listen/random_blob | rsha1)" - test x"$sha1_head" = x"$sha1" -} - -t_begin "tail range matches" && { - sha1="$(curl -sS $range_tail http://$listen/random_blob | rsha1)" - test x"$sha1_tail" = x"$sha1" -} - -t_begin "mid range matches" && { - sha1="$(curl -sS $range_mid http://$listen/random_blob | rsha1)" - test x"$sha1_mid" = x"$sha1" -} - -t_begin "shutdown server" && { - kill -QUIT $rainbows_pid -} - -t_begin "check stderr" && check_stderr - -t_done +. ./byte-range-common.sh -- cgit v1.2.3-24-ge0c7