Skip to content

Commit

Permalink
fix encapsulation problem in Mojo::WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 12, 2016
1 parent 30ae4d4 commit 0d1365a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Changes
@@ -1,7 +1,7 @@

6.40 2016-01-12
- Removed client_challenge, client_handshake and server_handshake methods from
Mojo::Transaction::WebSocket. (batman)
- Removed client_challenge, client_handshake, server_handshake and server_open
methods from Mojo::Transaction::WebSocket. (batman, sri)
- Removed is_writing method from Mojo::Transaction.
- Removed upgrade event from Mojo::Transaction::HTTP.
- Deprecated Mojo::Transaction::WebSocket::build_frame and
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -132,10 +132,9 @@ sub _finish {

# Successful upgrade
if ($ws->handshake->res->code == 101) {
$c->{tx} = $ws;
$c->{tx} = $ws->established(1);
weaken $self;
$ws->on(resume => sub { $self->_write($id) });
$ws->server_open;
}

# Failed upgrade
Expand Down
20 changes: 9 additions & 11 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -10,7 +10,7 @@ use Mojo::Util qw(decode deprecated encode trim);
use Mojo::WebSocket
qw(WS_BINARY WS_CLOSE WS_CONTINUATION WS_PING WS_PONG WS_TEXT);

has [qw(compressed masked)];
has [qw(compressed established masked)];
has handshake => sub { Mojo::Transaction::HTTP->new };
has max_websocket_size => sub { $ENV{MOJO_MAX_WEBSOCKET_SIZE} || 262144 };

Expand Down Expand Up @@ -64,7 +64,7 @@ sub finish {
return $self;
}

sub is_established { !!shift->{open} }
sub is_established { !!shift->established }

sub is_websocket {1}

Expand Down Expand Up @@ -106,8 +106,6 @@ sub server_close {
return $self->emit(finish => $self->{close} ? (@{$self->{close}}) : 1006);
}

sub server_open { shift->{open}++ }

sub server_read {
my ($self, $chunk) = @_;

Expand Down Expand Up @@ -345,6 +343,13 @@ L<Mojo::Transaction> and implements the following new ones.
Compress messages with C<permessage-deflate> extension.
=head2 established
my $bool = $ws->established;
$ws = $ws->established($bool);
WebSocket connection established.
=head2 handshake
my $handshake = $ws->handshake;
Expand Down Expand Up @@ -508,13 +513,6 @@ will be invoked once all data has been written.
Transaction closed server-side, used to implement web servers such as
L<Mojo::Server::Daemon>.
=head2 server_open
$ws->server_open;
WebSocket connection established server-side, used to implement web servers such
as L<Mojo::Server::Daemon>.
=head2 server_read
$ws->server_read($data);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -129,7 +129,7 @@ sub upgrade {
my $code = $tx->res->code // 0;
return undef unless $tx->req->is_handshake && $code == 101;
my $ws = Mojo::Transaction::WebSocket->new(handshake => $tx, masked => 1);
return challenge($ws) ? $ws : undef;
return challenge($ws) ? $ws->established(1) : undef;
}

sub websocket {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/WebSocket.pm
Expand Up @@ -78,7 +78,7 @@ sub challenge {
if ($headers->sec_websocket_extensions // '') =~ /permessage-deflate/;

return _challenge($tx->req->headers->sec_websocket_key) eq
$headers->sec_websocket_accept && ++$tx->{open};
$headers->sec_websocket_accept;
}

sub client_handshake {
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -107,7 +107,7 @@ sub finish {

# WebSocket
my $tx = $self->tx;
$tx->finish(@_) and return $tx->is_established ? $self : $self->rendered(101)
$tx->finish(@_) and return $tx->established ? $self : $self->rendered(101)
if $tx->is_websocket;

# Chunked stream
Expand Down Expand Up @@ -138,7 +138,7 @@ sub helpers { $_[0]->app->renderer->get_helper('')->($_[0]) }
sub on {
my ($self, $name, $cb) = @_;
my $tx = $self->tx;
$self->rendered(101) if $tx->is_websocket && !$tx->is_established;
$self->rendered(101) if $tx->is_websocket && !$tx->established;
return $tx->on($name => sub { shift; $self->$cb(@_) });
}

Expand Down Expand Up @@ -252,7 +252,7 @@ sub send {
Carp::croak 'No WebSocket connection to send message to'
unless $tx->is_websocket;
$tx->send($msg, $cb ? sub { shift; $self->$cb(@_) } : ());
return $tx->is_established ? $self : $self->rendered(101);
return $tx->established ? $self : $self->rendered(101);
}

sub session {
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/websocket.t
Expand Up @@ -47,11 +47,11 @@ get '/something/else' => sub {

websocket '/early_start' => sub {
my $c = shift;
$c->send('test' . ($c->tx->is_established ? 1 : 0));
$c->send('test' . ($c->tx->established ? 1 : 0));
$c->on(
message => sub {
my ($c, $msg) = @_;
$c->send("${msg}test" . ($c->tx->is_established ? 1 : 0));
$c->send("${msg}test" . ($c->tx->established ? 1 : 0));
$c->finish(1000 => 'I ♥ Mojolicious!');
}
);
Expand Down Expand Up @@ -164,7 +164,7 @@ my ($established, $status, $msg);
$ua->websocket(
'/early_start' => sub {
my ($ua, $tx) = @_;
$established = $tx->is_established;
$established = $tx->established;
$tx->on(
finish => sub {
my ($tx, $code, $reason) = @_;
Expand Down

0 comments on commit 0d1365a

Please sign in to comment.