Skip to content

Commit

Permalink
improved Mojolicious::Static to only handle GET and HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 17, 2014
1 parent dc1382e commit cd087ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.30 2014-08-17
- Improved Mojolicious::Static to only handle GET and HEAD requests.
- Improved Mojo::URL performance.
- Improved url_for performance slightly.
- Fixed Mojo::IOLoop::Client to use a timeout for every connection.
Expand Down
7 changes: 6 additions & 1 deletion lib/Mojolicious/Static.pm
Expand Up @@ -22,9 +22,14 @@ my $PUBLIC = $HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/public');
sub dispatch {
my ($self, $c) = @_;

# Method (GET or HEAD)
my $req = $c->req;
my $method = $req->method;
return undef unless $method eq 'GET' || $method eq 'HEAD';

# Canonical path
my $stash = $c->stash;
my $path = $c->req->url->path;
my $path = $req->url->path;
$path = $stash->{path} ? $path->new($stash->{path}) : $path->clone;
return undef unless my @parts = @{$path->canonicalize->parts};

Expand Down
13 changes: 13 additions & 0 deletions t/mojolicious/static_lite_app.t
Expand Up @@ -12,6 +12,8 @@ use Test::Mojo;

get '/hello3.txt' => sub { shift->render_static('hello2.txt') };

options '/hello.txt' => sub { shift->render(text => 'Options!') };

get '/etag' => sub {
my $c = shift;
$c->is_fresh(etag => 'abc')
Expand Down Expand Up @@ -50,6 +52,17 @@ $t->get_ok('/hello.txt')->status_is(200)
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 31)
->content_is("Hello Mojo from a static file!\n");

# Static file (HEAD)
$t->head_ok('/hello.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 31)
->content_is('');

# Route for method other than GET and HEAD
$t->options_ok('/hello.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('Content-Length' => 8)->content_is('Options!');

# Partial static file
$t->get_ok('/hello.txt' => {Range => 'bytes=2-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down

0 comments on commit cd087ba

Please sign in to comment.