Skip to content

Commit

Permalink
replaced compressed attribute with has_compression method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 28, 2013
1 parent 6ed889c commit 0a777a0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +1,7 @@

4.59 2013-11-28
- Added support for permessage-deflate WebSocket compression.
- Added compressed attribute to Mojo::Transaction::WebSocket.
- Added has_compression method to Mojo::Transaction::WebSocket.
- Relicensed all artwork to CC-SA version 4.0.

4.58 2013-11-19
Expand Down
23 changes: 12 additions & 11 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -26,8 +26,8 @@ use constant {
PONG => 10
};

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

sub new {
Expand Down Expand Up @@ -125,6 +125,8 @@ sub finish {
return $self;
}

sub has_compression { !!shift->{compress} }

sub is_websocket {1}

sub kept_alive { shift->handshake->kept_alive }
Expand Down Expand Up @@ -221,7 +223,7 @@ sub send {
else { $frame = [1, 0, 0, 0, BINARY, $frame->{binary}] }

# "permessage-deflate" extension
if ($self->compressed) {
if ($self->has_compression) {
$frame->[1] = 1;
my $deflate = $self->{deflate}
||= Compress::Raw::Zlib::Deflate->new(WindowBits => -15,
Expand Down Expand Up @@ -285,7 +287,7 @@ sub server_write {

sub _challenge { b64_encode(sha1_bytes(($_[0] || '') . GUID), '') }

sub _deflate { ($_[1] // '') =~ /permessage-deflate/i && $_[0]->compressed(1) }
sub _deflate { ($_[1] // '') =~ /permessage-deflate/i && ++$_[0]->{compress} }

sub _message {
my ($self, $frame) = @_;
Expand Down Expand Up @@ -315,7 +317,7 @@ sub _message {

# "permessage-deflate" extension (handshake and RSV1)
my $msg = delete $self->{message};
if ($self->compressed && $frame->[1]) {
if ($self->has_compression && $frame->[1]) {
my $inflate = $self->{inflate}
||= Compress::Raw::Zlib::Inflate->new(WindowBits => -15);
$inflate->inflate(\($msg .= "\x00\x00\xff\xff"), my $out);
Expand Down Expand Up @@ -473,13 +475,6 @@ Emitted when a complete WebSocket text message has been received.
L<Mojo::Transaction::WebSocket> inherits all attributes from
L<Mojo::Transaction> and implements the following new ones.
=head2 compressed
my $bool = $ws->compressed;
$ws = $ws->compressed(1);
Compress messages with C<permessage-deflate> extension.
=head2 handshake
my $handshake = $ws->handshake;
Expand Down Expand Up @@ -579,6 +574,12 @@ Connection identifier or socket.
Close WebSocket connection gracefully.
=head2 has_compression
my $bool = $ws->has_compression;
Check if messages will be compressed with C<permessage-deflate> extension.
=head2 is_websocket
my $true = $ws->is_websocket;
Expand Down
8 changes: 5 additions & 3 deletions t/mojo/websocket.t
Expand Up @@ -456,15 +456,16 @@ like $log, qr/Inactivity timeout\./, 'right log message';
app->log->unsubscribe(message => $msg);

# Ping/pong (with negotiated compression)
my ($pong, $extensions);
my ($pong, $compression, $extensions);
$ua->websocket(
'/echo' => sub {
my ($ua, $tx) = @_;
$tx->on(
frame => sub {
my ($tx, $frame) = @_;
$pong = $frame->[5] if $frame->[4] == 10;
$extensions = $tx->res->headers->sec_websocket_extensions;
$pong = $frame->[5] if $frame->[4] == 10;
$compression = $tx->has_compression;
$extensions = $tx->res->headers->sec_websocket_extensions;
Mojo::IOLoop->stop;
}
);
Expand All @@ -473,6 +474,7 @@ $ua->websocket(
);
Mojo::IOLoop->start;
is $pong, 'test', 'received pong with payload';
ok $compression, 'WebSocket has compression';
is $extensions, 'permessage-deflate', 'right "Sec-WebSocket-Extensions" value';

done_testing();
1 change: 1 addition & 0 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -215,6 +215,7 @@ $t->websocket_ok('/unicode')->send_ok('hello again')
# Binary frame and events (no compression)
my $bytes = b("I ♥ Mojolicious")->encode('UTF-16LE')->to_string;
$t->websocket_ok('/bytes' => {'Sec-WebSocket-Extensions' => 'nothing'});
ok !$t->tx->has_compression, 'WebSocket has no compression';
ok !$t->tx->res->headers->sec_websocket_extensions,
'no "Sec-WebSocket-Extensions" value';
my $binary;
Expand Down

0 comments on commit 0a777a0

Please sign in to comment.