Skip to content

Commit

Permalink
fixed empty frame handling in Mojo::Transaction::WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 3, 2012
1 parent 91262ba commit 6b803f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojolicious.
(mikemagowan, sri)
- Improved documentation.
- Improved tests.
- Fixed empty frame handling in Mojo::Transaction::WebSocket.

2.92 2012-04-30
- Added commands attribute to Mojolicious.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -125,7 +125,7 @@ sub parse_frame {

# Head
my $clone = $$buffer;
return unless length $clone > 2;
return unless length $clone >= 2;
my $head = substr $clone, 0, 2;

# FIN
Expand Down
1 change: 0 additions & 1 deletion t/mojo/websocket.t
Expand Up @@ -117,7 +117,6 @@ websocket '/subreq' => sub {
# WebSocket /echo
websocket '/echo' => sub {
my $self = shift;
$self->tx->max_websocket_size(500000);
$self->on(
message => sub {
my ($self, $message) = @_;
Expand Down
26 changes: 25 additions & 1 deletion t/mojo/websocket_frames.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

# "Being eaten by crocodile is just like going to sleep...
# in a giant blender."
use Test::More tests => 84;
use Test::More tests => 98;

use Mojo::Transaction::WebSocket;

Expand Down Expand Up @@ -158,3 +158,27 @@ is $frame->[3], 0, 'rsv3 flag is not set';
is $frame->[4], 1, 'text frame';
is $frame->[5], 'hi' x 200000, 'right payload';
is $ws->build_frame(1, 0, 0, 0, 1, 'hi' x 200000), $bytes, 'frames are equal';

# Empty text frame roundtrip
$ws = Mojo::Transaction::WebSocket->new;
$bytes = $ws->build_frame(1, 0, 0, 0, 1, '');
$frame = $ws->parse_frame(\($dummy = $bytes));
is $frame->[0], 1, 'fin flag is set';
is $frame->[1], 0, 'rsv1 flag is not set';
is $frame->[2], 0, 'rsv2 flag is not set';
is $frame->[3], 0, 'rsv3 flag is not set';
is $frame->[4], 1, 'text frame';
is $frame->[5], '', 'no payload';
is $ws->build_frame(1, 0, 0, 0, 1, ''), $bytes, 'frames are equal';

# Empty close frame roundtrip
$ws = Mojo::Transaction::WebSocket->new;
$bytes = $ws->build_frame(1, 0, 0, 0, 8, '');
$frame = $ws->parse_frame(\($dummy = $bytes));
is $frame->[0], 1, 'fin flag is set';
is $frame->[1], 0, 'rsv1 flag is not set';
is $frame->[2], 0, 'rsv2 flag is not set';
is $frame->[3], 0, 'rsv3 flag is not set';
is $frame->[4], 8, 'close frame';
is $frame->[5], '', 'no payload';
is $ws->build_frame(1, 0, 0, 0, 8, ''), $bytes, 'frames are equal';

0 comments on commit 6b803f9

Please sign in to comment.