Navigation Menu

Skip to content

Commit

Permalink
simplified support for non-blocking operations in bridges
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 29, 2013
1 parent a93e36d commit 0f2e52e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 45 deletions.
4 changes: 2 additions & 2 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -520,9 +520,9 @@ implements the following new ones.
=head2 continue
$c->continue;
my $success = $c->continue;
Continue dispatch chain after finishing non-blocking operations.
Continue dispatch chain.
=head2 cookie
Expand Down
12 changes: 4 additions & 8 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -573,10 +573,9 @@ be broken, this makes bridges a very powerful tool for authentication.
});
$foo->route('/bar')->to(controller => 'foo', action => 'bar');

To allow non-blocking operations to finish before continuing the dispatch
chain, you can suspend it by returning a C<Scalar> reference. The next
dispatch cycle can be reached by calling the method
L<Mojolicious::Controller/"continue">.
Broken dispatch chains can be continued by calling the method
L<Mojolicious::Controller/"continue">, this allows for example non-blocking
operations to finish before reaching the next dispatch cycle.

# /foo -> undef
# /foo/bar -> {cb => sub {...}}
Expand All @@ -594,13 +593,10 @@ L<Mojolicious::Controller/"continue">.
$self->continue;
});

return \1;
return 0;
});
$foo->route('/bar')->to(controller => 'foo', action => 'bar');

Automatic rendering will be disabled after the dispatch chain has been
suspended.

=head2 More convenient routes

From the tutorial you should already know L<Mojolicious::Lite> routes, which
Expand Down
14 changes: 5 additions & 9 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -48,11 +48,7 @@ sub continue {
return undef if !$last && !$continue;

# Continue
return $self->continue($c) unless ref $continue eq 'SCALAR';

# Suspend
$c->render_later;
return 1;
return $self->continue($c);
}

sub dispatch {
Expand Down Expand Up @@ -90,8 +86,8 @@ sub dispatch {
}
}

return undef unless @{$c->match->stack} && $self->continue($c);
$self->auto_render($c);
return undef unless @{$c->match->stack};
$self->auto_render($c) if $self->continue($c);
return 1;
}

Expand Down Expand Up @@ -333,9 +329,9 @@ Automatic rendering.
=head2 continue
my $success = $r->continue(Mojolicious::Controller->new);
$r->continue(Mojolicious::Controller->new);
Continue dispatching.
Continue dispatch chain.
=head2 dispatch
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/app.t
Expand Up @@ -230,9 +230,9 @@ $t->get_ok('/auth/authenticated' => {'X-Bender' => 'Hi there!'})
->header_is(Server => 'Mojolicious (Perl)')->content_is('authenticated');

# Foo::authenticated (authentication bridge)
$t->get_ok('/auth/authenticated')->status_is(404)
$t->get_ok('/auth/authenticated')->status_is(401)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->content_like(qr/Page not found/);
->content_is('Unauthorized!');

# Foo::test
$t->get_ok('/foo/test' => {'X-Test' => 'Hi there!'})->status_is(200)
Expand Down
43 changes: 22 additions & 21 deletions t/mojolicious/group_lite_app.t
Expand Up @@ -33,14 +33,15 @@ under '/suspended' => sub {
}
);

return \1;
return 0;
};

get '/' => sub { shift->render(inline => '<%= $suspended %>\\') };

under sub {
my $self = shift;
return undef unless $self->req->headers->header('X-Bender');
$self->render(text => 'Unauthorized!', status => 401) and return undef
unless $self->req->headers->header('X-Bender');
$self->res->headers->add('X-Under' => 23);
$self->res->headers->add('X-Under' => 24);
1;
Expand Down Expand Up @@ -117,11 +118,14 @@ get '/possible' => 'possible';

# Nothing gets past this
under sub {
shift->res->headers->header('X-Impossible' => 1);
my $self = shift;
$self->res->headers->header('X-Impossible' => 1);
$self->res->code(401);
$self->tx->resume;
0;
};

get '/impossible' => 'impossible';
get '/impossible' => {text => 'Impossible!'};

# Prefix
under '/prefix';
Expand Down Expand Up @@ -203,7 +207,7 @@ ok !$t->tx->res->cookie('mojolicious')->expires, 'no expiration';
$t->reset_session;

# Missing action behind bridge
$t->get_ok('/missing')->status_is(404);
$t->get_ok('/missing')->status_is(404)->content_is("Oops!\n");

# Suspended bridge
$t->get_ok('/suspended?ok=1')->status_is(200)
Expand All @@ -226,8 +230,8 @@ $t->get_ok('/with_under_too' => {'X-Bender' => 'Rodriguez'})->status_is(200)
->content_is('Unders are cool too!');

# Not authenticated with header
$t->get_ok('/with_under_too')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')->content_like(qr/Oops!/);
$t->get_ok('/with_under_too')->status_is(401)
->header_is(Server => 'Mojolicious (Perl)')->content_like(qr/Unauthorized/);

# Not authenticated with parameter
$t->get_ok('/param_auth')->status_is(200)
Expand Down Expand Up @@ -339,10 +343,10 @@ $t->get_ok('/possible')->status_is(200)
->header_is('X-Impossible' => undef)->content_is("Possible!\n");

# Unreachable route
$t->get_ok('/impossible')->status_is(404)
$t->get_ok('/impossible')->status_is(401)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Possible' => undef)->header_is('X-Impossible' => 1)
->content_is("Oops!\n");
->content_is('');

# Prefix
$t->get_ok('/prefix')->status_is(200)
Expand All @@ -368,7 +372,7 @@ $t->get_ok('/prefix2/bar')->status_is(200)->content_is("also prefixed!\n");
$t->get_ok('/reset')->status_is(200)->content_is('reset works!');

# Not reachable with prefix
$t->get_ok('/prefix/reset')->status_is(404);
$t->get_ok('/prefix/reset')->status_is(404)->content_is("Oops!\n");

# Group
$t->get_ok('/group')->status_is(200)->content_is("onetwo!\n");
Expand All @@ -381,7 +385,7 @@ $t->get_ok('/group/nested/whatever')->status_is(200)
->content_is("onetwothree!\n");

# Another GET request to nested group
$t->get_ok('/group/nested/something')->status_is(404);
$t->get_ok('/group/nested/something')->status_is(404)->content_is("Oops!\n");

# Authenticated by group
$t->get_ok('/authgroup?ok=1')->status_is(200)->content_is("You're ok.");
Expand All @@ -399,11 +403,11 @@ $t->get_ok('/no_format')->status_is(200)

# Invalid format
$t->get_ok('/no_format.txt')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

# Invalid format
$t->get_ok('/some_formats')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

# Format "txt" has been detected
$t->get_ok('/some_formats.txt')->status_is(200)->content_type_is('text/plain')
Expand All @@ -415,31 +419,31 @@ $t->get_ok('/some_formats.json')->status_is(200)

# Invalid format
$t->get_ok('/some_formats.xml')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

# Invalid format
$t->get_ok('/no_real_format')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

# No format detected
$t->get_ok('/no_real_format.xml')->status_is(200)
->content_type_is('text/html;charset=UTF-8')->content_is('No real format.');

# Invalid format
$t->get_ok('/no_real_format.txt')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

# Invalid format
$t->get_ok('/one_format')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

# Format "xml" detected
$t->get_ok('/one_format.xml')->status_is(200)
->content_type_is('application/xml')->content_is('One format.');

# Invalid format
$t->get_ok('/one_format.txt')->status_is(404)
->content_type_is('text/html;charset=UTF-8');
->content_type_is('text/html;charset=UTF-8')->content_is("Oops!\n");

done_testing();

Expand Down Expand Up @@ -469,6 +473,3 @@ counter
@@ possible.html.ep
Possible!
@@ impossible.html.ep
Impossible
5 changes: 3 additions & 2 deletions t/mojolicious/lib/MojoliciousTest.pm
Expand Up @@ -81,8 +81,9 @@ sub startup {
# /auth (authentication bridge)
my $auth = $r->bridge('/auth')->to(
cb => sub {
return 1 if shift->req->headers->header('X-Bender');
return undef;
my $self = shift;
return 1 if $self->req->headers->header('X-Bender');
$self->render(text => 'Unauthorized!', status => 401) and return undef;
}
);

Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lib/MojoliciousTest/Foo.pm
Expand Up @@ -65,7 +65,7 @@ sub suspended {
}
);

return \1;
return 0;
}

sub syntaxerror { shift->render('syntaxerror', format => 'html') }
Expand Down

0 comments on commit 0f2e52e

Please sign in to comment.