Skip to content

Commit

Permalink
better range handling (closes #414)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 20, 2012
1 parent d1f476b commit 77c38c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
17 changes: 8 additions & 9 deletions lib/Mojolicious/Static.pm
Expand Up @@ -80,16 +80,15 @@ sub serve_asset {
my $end = $size - 1;
if (my $range = $headers->range) {

# Satisfiable
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)
->content_range("bytes $start-$end/$size");
}

# Not satisfiable
else { return $res->code(416) }
return $res->code(416) unless $size && $range =~ m/^bytes=(\d+)?-(\d+)?/;
$start = $1 if defined $1;
$end = $2 if defined $2;
return $res->code(416) if $start > $end || $end > ($size - 1);

# Satisfiable
$res->code(206)->headers->content_length($end - $start + 1)
->content_range("bytes $start-$end/$size");
}

# Serve asset
Expand Down
18 changes: 16 additions & 2 deletions t/mojolicious/static_lite_app.t
Expand Up @@ -38,13 +38,26 @@ $t->get_ok('/hello.txt' => {Range => 'bytes=8-'})->status_is(206)
->header_is('Content-Range' => 'bytes 8-30/31')
->content_is("jo from a static file!\n");

# GET /hello.txt (partial static file, no start)
$t->get_ok('/hello.txt' => {Range => 'bytes=-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 9)
->header_is('Content-Range' => 'bytes 0-8/31')->content_is('Hello Moj');

# GET /hello.txt (partial static file, starting at first byte)
$t->get_ok('/hello.txt' => {Range => 'bytes=0-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 9)
->header_is('Content-Range' => 'bytes 0-8/31')->content_is('Hello Moj');

# GET /hello.txt (partial static file, invalid range)
$t->get_ok('/hello.txt' => {Range => 'bytes=8-1'})->status_is(416)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is('');

# GET /hello.txt (partial static file, first byte)
$t->get_ok('/hello.txt' => {Range => 'bytes=0-0'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down Expand Up @@ -80,8 +93,9 @@ $t->get_ok('/hello4.txt')->status_is(200)

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

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

0 comments on commit 77c38c0

Please sign in to comment.