Skip to content

Commit

Permalink
do not allow range requests for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 20, 2012
1 parent 68d81d6 commit 6804499
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Mojolicious/Static.pm
Expand Up @@ -77,11 +77,11 @@ sub serve_asset {
# Range
my $size = $asset->size;
my $start = 0;
my $end = $size - 1 >= 0 ? $size - 1 : 0;
my $end = $size - 1;
if (my $range = $headers->range) {

# Satisfiable
if ($range =~ m/^bytes=(\d+)-(\d+)?/ && $1 <= $end) {
if ($size && $range =~ m/^bytes=(\d+)-(\d+)?/ && $1 <= $end) {
$start = $1;
$end = $2 if defined $2 && $2 <= $end;
$res->code(206)->headers->content_length($end - $start + 1)
Expand Down
Empty file added t/mojolicious/public/hello4.txt
Empty file.
12 changes: 12 additions & 0 deletions t/mojolicious/static_lite_app.t
Expand Up @@ -72,6 +72,18 @@ $t->get_ok('/hello3.txt' => {Range => 'bytes=0-0'})->status_is(206)
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->header_is('Content-Range' => 'bytes 0-0/1')->content_is('X');

# GET /hello4.txt (empty file)
$t->get_ok('/hello4.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Content-Length' => 0)->content_is('');

# GET /hello4.txt (empty file, invalid range)
$t->get_ok('/hello4.txt' => {Range => 'bytes=0-0'})->status_is(416)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Content-Length' => 0)->content_is('');

# GET /static.txt (base64 static inline file, If-Modified-Since)
my $modified = Mojo::Date->new->epoch(time - 3600);
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
Expand Down

0 comments on commit 6804499

Please sign in to comment.