Skip to content

Commit

Permalink
Remove public constant MAX_WEBSOCKET_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Jan 9, 2016
1 parent 6d79b07 commit 61b3fdc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
7 changes: 3 additions & 4 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -6,8 +6,8 @@ use Config;
use List::Util 'first';
use Mojo::JSON qw(encode_json j);
use Mojo::Transaction::HTTP;
use Mojo::Util qw(decode dumper encode trim xor_encode);
use Mojo::WebSocket qw(build_frame MAX_WEBSOCKET_SIZE);
use Mojo::Util qw(decode encode trim);
use Mojo::WebSocket qw(build_frame);

use constant DEBUG => $ENV{MOJO_WEBSOCKET_DEBUG} || 0;

Expand All @@ -27,8 +27,7 @@ use constant {

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

sub build_message {
my ($self, $frame) = @_;
Expand Down
6 changes: 2 additions & 4 deletions lib/Mojo/WebSocket.pm
Expand Up @@ -6,13 +6,12 @@ use Config;
use Mojo::Util qw(b64_encode sha1_bytes xor_encode);

our @EXPORT_OK
= qw(build_frame challenge client_handshake parse_frame server_handshake MAX_WEBSOCKET_SIZE);
= qw(build_frame challenge client_handshake parse_frame server_handshake);

# Unique value from RFC 6455
use constant GUID => '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';

use constant DEBUG => $ENV{MOJO_WEBSOCKET_DEBUG} || 0;
use constant MAX_WEBSOCKET_SIZE => 262144;

# Perl with support for quads
use constant MODERN =>
Expand Down Expand Up @@ -87,8 +86,7 @@ sub client_handshake {
}

sub parse_frame {
my $buffer = shift;
my $max_websocket_size = shift || MAX_WEBSOCKET_SIZE;
my ($buffer, $max_websocket_size) = @_;

# Head
return undef unless length $$buffer >= 2;
Expand Down
32 changes: 16 additions & 16 deletions t/mojo/websocket_frames.t
Expand Up @@ -8,7 +8,7 @@ use Mojo::WebSocket qw(build_frame parse_frame);
my $ws = Mojo::Transaction::WebSocket->new;
my $bytes = build_frame $ws->masked, 1, 0, 0, 0, 1, 'whatever';
is $bytes, "\x81\x08\x77\x68\x61\x74\x65\x76\x65\x72", 'right frame';
my $frame = parse_frame \(my $dummy = $bytes);
my $frame = parse_frame \(my $dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -22,7 +22,7 @@ is build_frame($ws->masked, 1, 0, 0, 0, 1, 'whatever'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame $ws->masked, 1, 1, 1, 1, 1, 'whatever';
is $bytes, "\xf1\x08\x77\x68\x61\x74\x65\x76\x65\x72", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
is $frame->[0], 1, 'fin flag is set';
is $frame->[1], 1, 'rsv1 flag is set';
is $frame->[2], 1, 'rsv2 flag is set';
Expand All @@ -36,7 +36,7 @@ is build_frame($ws->masked, 1, 1, 1, 1, 1, 'whatever'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame $ws->masked, 0, 0, 0, 0, 1, 'whatever';
is $bytes, "\x01\x08\x77\x68\x61\x74\x65\x76\x65\x72", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
is $frame->[0], 0, 'fin flag is not set';
is $frame->[1], 0, 'rsv1 flag is not set';
is $frame->[2], 0, 'rsv2 flag is not set';
Expand All @@ -50,7 +50,7 @@ is build_frame($ws->masked, 0, 0, 0, 0, 1, 'whatever'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 1, 0, 0, 1, 'whatever');
is $bytes, "\xc1\x08\x77\x68\x61\x74\x65\x76\x65\x72", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
is $frame->[0], 1, 'fin flag is set';
is $frame->[1], 1, 'rsv1 flag is set';
is $frame->[2], 0, 'rsv2 flag is not set';
Expand All @@ -64,7 +64,7 @@ is build_frame($ws->masked, 1, 1, 0, 0, 1, 'whatever'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 1, 0, 1, 'whatever');
is $bytes, "\xa1\x08\x77\x68\x61\x74\x65\x76\x65\x72", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
is $frame->[0], 1, 'fin flag is set';
is $frame->[1], 0, 'rsv1 flag is not set';
is $frame->[2], 1, 'rsv2 flag is set';
Expand All @@ -78,7 +78,7 @@ is build_frame($ws->masked, 1, 0, 1, 0, 1, 'whatever'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 1, 1, 'whatever');
is $bytes, "\x91\x08\x77\x68\x61\x74\x65\x76\x65\x72", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -92,7 +92,7 @@ is build_frame($ws->masked, 1, 0, 0, 1, 1, 'whatever'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 2, 'works');
is $bytes, "\x82\x05\x77\x6f\x72\x6b\x73", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -105,7 +105,7 @@ is $bytes = build_frame($ws->masked, 1, 0, 0, 0, 2, 'works'), $bytes,
# Masked text frame roundtrip
$ws = Mojo::Transaction::WebSocket->new(masked => 1);
$bytes = build_frame $ws->masked, 1, 0, 0, 0, 1, 'also works';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -118,7 +118,7 @@ isnt(build_frame(0, 1, 0, 0, 0, 2, 'also works'),
# Masked binary frame roundtrip
$ws = Mojo::Transaction::WebSocket->new(masked => 1);
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 2, 'just works');
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -132,7 +132,7 @@ isnt(build_frame(0, 1, 0, 0, 0, 2, 'just works'),
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 1, 'a');
is $bytes, "\x81\x01\x61", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -145,7 +145,7 @@ is build_frame($ws->masked, 1, 0, 0, 0, 1, 'a'), $bytes, 'frames are equal';
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 2, 'a');
is $bytes, "\x82\x01\x61", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -159,7 +159,7 @@ is $bytes = build_frame($ws->masked, 1, 0, 0, 0, 2, 'a'), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 1, 'hi' x 10000);
is $bytes, "\x81\x7e\x4e\x20" . ("\x68\x69" x 10000), 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand Down Expand Up @@ -188,7 +188,7 @@ is build_frame($ws->masked, 1, 0, 0, 0, 1, 'hi' x 200000), $bytes,
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 1, '');
is $bytes, "\x81\x00", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -201,7 +201,7 @@ is build_frame($ws->masked, 1, 0, 0, 0, 1, ''), $bytes, 'frames are equal';
$ws = Mojo::Transaction::WebSocket->new;
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 8, '');
is $bytes, "\x88\x00", 'right frame';
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -213,7 +213,7 @@ is build_frame($ws->masked, 1, 0, 0, 0, 8, ''), $bytes, 'frames are equal';
# Masked empty binary frame roundtrip
$ws = Mojo::Transaction::WebSocket->new(masked => 1);
$bytes = build_frame($ws->masked, 1, 0, 0, 0, 2, '');
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
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';
Expand All @@ -225,7 +225,7 @@ isnt(build_frame(0, 1, 0, 0, 0, 2, ''), $bytes, 'frames are not equal');
# Compressed binary message roundtrip
$ws = Mojo::Transaction::WebSocket->new({compressed => 1});
$bytes = $ws->build_message({binary => 'just works'});
$frame = parse_frame \($dummy = $bytes);
$frame = parse_frame \($dummy = $bytes), $ws->max_websocket_size;
is $frame->[0], 1, 'fin flag is set';
is $frame->[1], 1, 'rsv1 flag is set';
is $frame->[2], 0, 'rsv2 flag is not set';
Expand Down

0 comments on commit 61b3fdc

Please sign in to comment.