Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed WebSocket bug in Mojo::Content
  • Loading branch information
kraih committed Oct 12, 2012
1 parent 1d8c653 commit 3ab684b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

3.47 2012-10-12
3.47 2012-10-13
- Added all method to Mojo::UserAgent::CookieJar.
- Fixed WebSocket bug in Mojo::Content.

3.46 2012-10-11
- Improved router and renderer to allow camel case controllers.
Expand Down
7 changes: 5 additions & 2 deletions lib/Mojo/Content.pm
Expand Up @@ -66,7 +66,7 @@ sub get_header_chunk {
return substr $self->{header_buffer}, $offset, 131072;
}

sub has_leftovers { !!length(shift->{buffer} || '') }
sub has_leftovers { !!length shift->leftovers }

sub header_size { length shift->build_headers }

Expand All @@ -83,7 +83,10 @@ sub is_multipart {undef}

sub is_parsing_body { (shift->{state} // '') eq 'body' }

sub leftovers { shift->{buffer} }
sub leftovers {
my $self = shift;
return ($self->{pre_buffer} // '') . ($self->{buffer} // '');
}

sub parse {
my $self = shift;
Expand Down
27 changes: 26 additions & 1 deletion t/mojo/response.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 357;
use Test::More tests => 370;

use Mojo::Asset::File;
use Mojo::Content::Single;
Expand Down Expand Up @@ -536,6 +536,31 @@ is $res->headers->sec_websocket_protocol, 'sample',
'right "Sec-WebSocket-Protocol" value';
is $res->body, '', 'no content';

# Parse WebSocket handshake response (with frame)
$res = Mojo::Message::Response->new;
$res->parse("HTTP/1.1 101 Switching Protocols\x0d\x0a");
$res->parse("Upgrade: websocket\x0d\x0a");
$res->parse("Connection: Upgrade\x0d\x0a");
$res->parse("Sec-WebSocket-Accept: abcdef=\x0d\x0a");
$res->parse("Sec-WebSocket-Protocol: sample\x0d\x0a");
$res->parse("\x0d\x0a\x81\x08\x77\x68\x61\x74\x65\x76\x65\x72");
ok $res->is_finished, 'response is finished';
ok $res->is_empty, 'response is empty';
ok $res->content->skip_body, 'body has been skipped';
is $res->code, 101, 'right status';
is $res->message, 'Switching Protocols', 'right message';
is $res->version, '1.1', 'right version';
is $res->headers->upgrade, 'websocket', 'right "Upgrade" value';
is $res->headers->connection, 'Upgrade', 'right "Connection" value';
is $res->headers->sec_websocket_accept, 'abcdef=',
'right "Sec-WebSocket-Accept" value';
is $res->headers->sec_websocket_protocol, 'sample',
'right "Sec-WebSocket-Protocol" value';
is $res->body, '', 'no content';
ok $res->has_leftovers, 'has leftovers';
is $res->leftovers, "\x81\x08\x77\x68\x61\x74\x65\x76\x65\x72",
'frame in leftovers';

# Build WebSocket handshake response
$res = Mojo::Message::Response->new;
$res->code(101);
Expand Down

0 comments on commit 3ab684b

Please sign in to comment.