Skip to content

Commit

Permalink
better WebSocket tests and proxy example
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 9, 2012
1 parent da9ab1e commit be7f001
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 32 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@

3.28 2012-08-10
- Improved tests.

3.27 2012-08-09
- Improved documentation.
- Improved tests.
Expand Down
32 changes: 23 additions & 9 deletions examples/connect-proxy.pl
Expand Up @@ -5,59 +5,73 @@
# "Cheating in a fake fight. That's low."
use Mojo::IOLoop;

# Connection buffer
my %buffer;

# Minimal connect proxy server to test TLS tunneling
my %buffer;
Mojo::IOLoop->server(
{port => 3000} => sub {
my ($loop, $stream, $client) = @_;

# Connection to client
$stream->on(
read => sub {
my ($stream, $chunk) = @_;

# Write chunk from client to server
if (my $server = $buffer{$client}{connection}) {
return Mojo::IOLoop->stream($server)->write($chunk);
}

# Read connect request from client
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
my $address = $1;
my $port = $2 || 80;
my $server;
$server = Mojo::IOLoop->client(

# Connection to server
$buffer{$client}{connection} = Mojo::IOLoop->client(
{address => $address, port => $port} => sub {
my ($loop, $err, $stream) = @_;

# Connection to server failed
if ($err) {
say "Connection error for $address:$port: $err";
Mojo::IOLoop->remove($client);
return delete $buffer{$client};
}

# Start forwarding data in both directions
say "Forwarding to $address:$port.";
$buffer{$client}{connection} = $server;
Mojo::IOLoop->stream($client)
->write("HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
Mojo::IOLoop->stream($client)->write($chunk);
}
);

# Server closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($client);
delete $buffer{$client};
}
);
Mojo::IOLoop->stream($client)
->write("HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a\x0d\x0a");
}
);
}
}

# Invalid request from client
else { Mojo::IOLoop->remove($client) }
}
);

# Client closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}{connection})
Expand Down
4 changes: 1 addition & 3 deletions examples/microhttpd.pl
Expand Up @@ -6,10 +6,8 @@
# I'll get the powder, sir."
use Mojo::IOLoop;

# Buffer for incoming data
my %buffer;

# Minimal ioloop example demonstrating how to cheat at HTTP benchmarks :)
my %buffer;
Mojo::IOLoop->server(
{port => 3000} => sub {
my ($loop, $stream, $id) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -38,7 +38,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Rainbow';
our $VERSION = '3.27';
our $VERSION = '3.28';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/reactor_ev.t
Expand Up @@ -165,17 +165,17 @@ $reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $timer, 'timer was triggered';
ok !$timer2, 'timer was not triggered';
($timer, $timer2) = 0;
$timer = $timer2 = 0;
$reactor2->timer(0.025 => sub { shift->stop });
$reactor2->start;
ok !$timer, 'timer was not triggered';
ok $timer2, 'timer was triggered';
($timer, $timer2) = 0;
$timer = $timer2 = 0;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $timer, 'timer was triggered';
ok !$timer2, 'timer was not triggered';
($timer, $timer2) = 0;
$timer = $timer2 = 0;
$reactor2->timer(0.025 => sub { shift->stop });
$reactor2->start;
ok !$timer, 'timer was not triggered';
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/reactor_poll.t
Expand Up @@ -165,17 +165,17 @@ $reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $timer, 'timer was triggered';
ok !$timer2, 'timer was not triggered';
($timer, $timer2) = 0;
$timer = $timer2 = 0;
$reactor2->timer(0.025 => sub { shift->stop });
$reactor2->start;
ok !$timer, 'timer was not triggered';
ok $timer2, 'timer was triggered';
($timer, $timer2) = 0;
$timer = $timer2 = 0;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $timer, 'timer was triggered';
ok !$timer2, 'timer was not triggered';
($timer, $timer2) = 0;
$timer = $timer2 = 0;
$reactor2->timer(0.025 => sub { shift->stop });
$reactor2->start;
ok !$timer, 'timer was not triggered';
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/websocket.t
Expand Up @@ -84,10 +84,10 @@ websocket '/early_start' => sub {
};

# WebSocket /denied
my ($handshake, $denied) = 0;
my $handshake = my $denied = 0;
websocket '/denied' => sub {
my $self = shift;
$self->tx->handshake->on(finish => sub { $handshake += 2 });
$self->tx->handshake->on(finish => sub { $handshake += 1 });
$self->on(finish => sub { $denied += 1 });
$self->render(text => 'denied', status => 403);
};
Expand Down Expand Up @@ -274,7 +274,7 @@ $ua->websocket(
);
$loop->start;
is $code, 403, 'right status';
is $handshake, 2, 'finished handshake';
is $handshake, 1, 'finished handshake';
is $denied, 1, 'finished websocket';

# WebSocket /subreq
Expand Down
26 changes: 21 additions & 5 deletions t/mojo/websocket_proxy.t
Expand Up @@ -53,7 +53,7 @@ $daemon->listen(["http://127.0.0.1:$port"])->start;
# Connect proxy server for testing
my $proxy = Mojo::IOLoop->generate_port;
my (%buffer, $connected);
my ($read, $sent, $fail) = 0;
my $read = my $sent = my $fail = 0;
my $nf
= "HTTP/1.1 404 NOT FOUND\x0d\x0a"
. "Content-Length: 0\x0d\x0a"
Expand All @@ -62,28 +62,39 @@ my $ok = "HTTP/1.1 200 OK\x0d\x0aConnection: keep-alive\x0d\x0a\x0d\x0a";
Mojo::IOLoop->server(
{address => '127.0.0.1', port => $proxy} => sub {
my ($loop, $stream, $client) = @_;

# Connection to client
$stream->on(
read => sub {
my ($stream, $chunk) = @_;

# Write chunk from client to server
if (my $server = $buffer{$client}{connection}) {
return Mojo::IOLoop->stream($server)->write($chunk);
}

# Read connect request from client
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
$connected = "$1:$2";
$fail = 1 if $2 == $port + 1;
my $server;
$server = Mojo::IOLoop->client(

# Connection to server
$buffer{$client}{connection} = Mojo::IOLoop->client(
{address => $1, port => $fail ? $port : $2} => sub {
my ($loop, $err, $stream) = @_;

# Connection to server failed
if ($err) {
Mojo::IOLoop->remove($client);
return delete $buffer{$client};
}
$buffer{$client}{connection} = $server;

# Start forwarding data in both directions
Mojo::IOLoop->stream($client)->write($fail ? $nf : $ok);
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
Expand All @@ -92,20 +103,25 @@ Mojo::IOLoop->server(
Mojo::IOLoop->stream($client)->write($chunk);
}
);

# Server closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($client);
delete $buffer{$client};
}
);
Mojo::IOLoop->stream($client)->write($fail ? $nf : $ok);
}
);
}
}

# Invalid request from client
else { Mojo::IOLoop->remove($client) }
}
);

# Client closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}{connection})
Expand Down
26 changes: 21 additions & 5 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -71,7 +71,7 @@ $daemon->listen([$listen])->start;
# Connect proxy server for testing
my $proxy = Mojo::IOLoop->generate_port;
my (%buffer, $connected);
my ($read, $sent, $fail) = 0;
my $read = my $sent = my $fail = 0;
my $nf
= "HTTP/1.1 404 NOT FOUND\x0d\x0a"
. "Content-Length: 0\x0d\x0a"
Expand All @@ -80,28 +80,39 @@ my $ok = "HTTP/1.0 200 OK\x0d\x0aX-Something: unimportant\x0d\x0a\x0d\x0a";
Mojo::IOLoop->server(
{address => '127.0.0.1', port => $proxy} => sub {
my ($loop, $stream, $client) = @_;

# Connection to client
$stream->on(
read => sub {
my ($stream, $chunk) = @_;

# Write chunk from client to server
if (my $server = $buffer{$client}{connection}) {
return Mojo::IOLoop->stream($server)->write($chunk);
}

# Read connect request from client
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
$connected = "$1:$2";
$fail = 1 if $2 == $port + 1;
my $server;
$server = Mojo::IOLoop->client(

# Connection to server
$buffer{$client}{connection} = Mojo::IOLoop->client(
{address => $1, port => $fail ? $port : $2} => sub {
my ($loop, $err, $stream) = @_;

# Connection to server failed
if ($err) {
Mojo::IOLoop->remove($client);
return delete $buffer{$client};
}
$buffer{$client}{connection} = $server;

# Start forwarding data in both directions
Mojo::IOLoop->stream($client)->write($fail ? $nf : $ok);
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
Expand All @@ -110,20 +121,25 @@ Mojo::IOLoop->server(
Mojo::IOLoop->stream($client)->write($chunk);
}
);

# Server closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($client);
delete $buffer{$client};
}
);
Mojo::IOLoop->stream($client)->write($fail ? $nf : $ok);
}
);
}
}

# Invalid request from client
else { Mojo::IOLoop->remove($client) }
}
);

# Client closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}{connection})
Expand Down

0 comments on commit be7f001

Please sign in to comment.