Skip to content

Commit

Permalink
deprecated after_static_dispatch hook in favor of before_routes and a…
Browse files Browse the repository at this point in the history
…dded after_static hook
  • Loading branch information
kraih committed Jan 29, 2013
1 parent 3de23f6 commit 662973e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

3.84 2013-01-30
- Deprecated after_static_dispatch hook in favor of before_routes.
- Added after_static hook.
- Fixed small file descriptor leak in Mojo::UserAgent.

3.83 2013-01-27
Expand Down
37 changes: 29 additions & 8 deletions lib/Mojolicious.pm
Expand Up @@ -117,10 +117,18 @@ sub dispatch {
my $plugins = $self->plugins->emit_hook(before_dispatch => $c);

# Try to find a static file
$self->static->dispatch($c) unless $tx->res->code;
$plugins->emit_hook_reverse(after_static_dispatch => $c);
$self->static->dispatch($c) and $plugins->emit_hook(after_static => $c)
unless $tx->res->code;

# DEPRECATED in Rainbow!
if ($plugins->has_subscribers('after_static_dispatch')) {
warn <<EOF and $plugins->emit_hook_reverse(after_static_dispatch => $c);
after_static_dispatch hook is DEPRECATED in favor of before_routes hook!!!
EOF
}

# Routes
$plugins->emit_hook(before_routes => $c);
my $res = $tx->res;
return if $res->code;
if (my $code = ($tx->req->error)[1]) { $res->code($code) }
Expand Down Expand Up @@ -476,18 +484,31 @@ Emitted right before the static dispatcher and router start their work.
Very useful for rewriting incoming requests and other preprocessing tasks.
(Passed the default controller object)
=item after_static_dispatch
=item after_static
Emitted in reverse order after the static dispatcher determined if a static
file should be served and before the router starts its work.
Emitted after the static dispatcher determined that a static file should be
served.
$app->hook(after_static_dispatch => sub {
$app->hook(after_static => sub {
my $c = shift;
...
});
Mostly used for custom dispatchers and post-processing static file responses.
(Passed the default controller object)
Mostly used for post-processing static file responses. (Passed the default
controller object)
=item before_routes
Emitted after the static dispatcher determined if a static file should be
served and before the router starts its work.
$app->hook(before_routes => sub {
my $c = shift;
...
});
Mostly used for custom dispatchers and collecting metrics. (Passed the default
controller object)
=item after_render
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -623,10 +623,9 @@ Post-processing tasks such as setting additional response headers are a very
common use.

# Make sure static files are cached
$self->hook(after_static_dispatch => sub {
$self->hook(after_static => sub {
my $self = shift;
$self->res->headers->cache_control('max-age=3600, must-revalidate')
if $self->res->code;
$self->res->headers->cache_control('max-age=3600, must-revalidate');
});

Same for monitoring tasks.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/RequestTimer.pm
Expand Up @@ -5,8 +5,8 @@ use Time::HiRes qw(gettimeofday tv_interval);

sub register {
my ($self, $app) = @_;
$app->hook(after_static_dispatch => \&_start);
$app->hook(after_dispatch => \&_end);
$app->hook(before_routes => \&_start);
$app->hook(after_dispatch => \&_end);
}

sub _end {
Expand Down
18 changes: 14 additions & 4 deletions t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -54,21 +54,27 @@ hook before_dispatch => sub {
};

# Custom dispatcher /custom_too
hook after_static_dispatch => sub {
hook before_routes => sub {
my $self = shift;
$self->render_text('this works too')
if $self->req->url->path->contains('/custom_too');
};

# Cleared response for /res.txt
hook after_static_dispatch => sub {
hook before_routes => sub {
my $self = shift;
return
unless $self->req->url->path->contains('/res.txt')
&& $self->param('route');
$self->tx->res(Mojo::Message::Response->new);
};

# Set additional headers for static files
hook after_static => sub {
my $self = shift;
$self->res->headers->cache_control('max-age=3600, must-revalidate');
};

# Response generating condition "res" for /res.txt
app->routes->add_condition(
res => sub {
Expand Down Expand Up @@ -108,10 +114,14 @@ $t->get_ok('/hello.txt')->status_is(200)
$t->get_ok('/custom?a=works+too')->status_is(205)->content_is('works too');

# Static file
$t->get_ok('/res.txt')->status_is(200)->content_is("Static response!\n");
$t->get_ok('/res.txt')->status_is(200)
->header_is('Cache-Control' => 'max-age=3600, must-revalidate')
->content_is("Static response!\n");

# Custom response
$t->get_ok('/res.txt?route=1')->status_is(202)->content_is('Custom response!');
$t->get_ok('/res.txt?route=1')->status_is(202)
->header_isnt('Cache-Control' => 'max-age=3600, must-revalidate')
->content_is('Custom response!');

# Conditional response
$t->get_ok('/res.txt?route=1&res=1')->status_is(201)
Expand Down

0 comments on commit 662973e

Please sign in to comment.