Skip to content

Commit

Permalink
fixed bug where embedded applications would deserialize sessions twice
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 27, 2014
1 parent 8dbaa78 commit c24e48a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.67 2014-11-27
- Fixed bug where embedded applications would deserialize sessions twice.

5.66 2014-11-26
- Improved many WebSocket tests in Test::Mojo to be able to fail gracefully.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -238,7 +238,7 @@ sub server_close {
sub server_handshake {
my $self = shift;

my $res_headers = $self->res->code(101)->headers;
my $res_headers = $self->res->headers;
$res_headers->upgrade('websocket')->connection('Upgrade');
my $req_headers = $self->req->headers;
($req_headers->sec_websocket_protocol // '') =~ /^\s*([^,]+)/
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious.pm
Expand Up @@ -89,18 +89,16 @@ sub defaults { Mojo::Util::_stash(defaults => @_) }
sub dispatch {
my ($self, $c) = @_;

# Prepare transaction
my $tx = $c->tx;
$tx->res->code(undef) if $tx->is_websocket;
$self->sessions->load($c);
my $stash = $c->stash;
$self->sessions->load($c) unless exists $stash->{'mojo.active_session'};
my $plugins = $self->plugins->emit_hook(before_dispatch => $c);

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

# Start timer (ignore static files)
my $stash = $c->stash;
unless ($stash->{'mojo.static'} || $stash->{'mojo.started'}) {
my $req = $c->req;
my $method = $req->method;
Expand Down
1 change: 1 addition & 0 deletions t/mojo/transactor.t
Expand Up @@ -451,6 +451,7 @@ ok $tx->req->headers->sec_websocket_version,
is $tx->req->headers->upgrade, 'websocket', 'right "Upgrade" value';
is $t->upgrade($tx), undef, 'not upgraded';
Mojo::Transaction::WebSocket->new(handshake => $tx)->server_handshake;
$tx->res->code(101);
$tx = $t->upgrade($tx);
ok $tx->is_websocket, 'is a WebSocket';

Expand Down
11 changes: 9 additions & 2 deletions t/mojolicious/embedded_app.t
Expand Up @@ -20,6 +20,13 @@ plugin(Mount => ('/x/♥' => $external));
plugin Mount => {'MOJOLICIO.US/' => $external};
plugin(Mount => ('*.foo-bar.de/♥/123' => $external));

# Make sure session can be modified from both apps
hook before_routes => sub {
my $c = shift;
return unless $c->req->url->path->contains('/x/1/secondary');
$c->session->{secondary} += 10;
};

get '/hello' => 'works';

get '/primary' => sub {
Expand All @@ -39,13 +46,13 @@ $t->get_ok('/primary')->status_is(200)->content_is(1);
$t->get_ok('/primary')->status_is(200)->content_is(2);

# Session in external app
$t->get_ok('/x/1/secondary')->status_is(200)->content_is(1);
$t->get_ok('/x/1/secondary')->status_is(200)->content_is(11);

# Session again
$t->get_ok('/primary')->status_is(200)->content_is(3);

# Session in external app again
$t->get_ok('/x/1/secondary')->status_is(200)->content_is(2);
$t->get_ok('/x/1/secondary')->status_is(200)->content_is(22);

# External app
$t->get_ok('/x/1')->status_is(200)->content_is('too%21');
Expand Down

0 comments on commit c24e48a

Please sign in to comment.