Skip to content

Commit

Permalink
more consistent names
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 1, 2016
1 parent ea5fe04 commit 0f42f77
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 60 deletions.
30 changes: 15 additions & 15 deletions examples/connect-proxy.pl
Expand Up @@ -5,70 +5,70 @@
my %buffer;
Mojo::IOLoop->server(
{port => 3000} => sub {
my ($loop, $stream, $client) = @_;
my ($loop, $stream, $id) = @_;

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

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

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

# Connection to server
$buffer{$client}{connection} = Mojo::IOLoop->client(
$buffer{$id}{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};
Mojo::IOLoop->remove($id);
return delete $buffer{$id};
}

# Start forwarding data in both directions
say "Forwarding to $address:$port";
Mojo::IOLoop->stream($client)
Mojo::IOLoop->stream($id)
->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);
Mojo::IOLoop->stream($id)->write($chunk);
}
);

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

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

# Client closed connection
$stream->on(
close => sub {
my $buffer = delete $buffer{$client};
my $buffer = delete $buffer{$id};
Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
}
);
Expand Down
30 changes: 15 additions & 15 deletions t/mojo/daemon_ipv6_tls.t
Expand Up @@ -31,66 +31,66 @@ get '/' => {text => 'works!'};
my (%buffer, $forward);
my $id = Mojo::IOLoop->server(
{address => '[::1]'} => sub {
my ($loop, $stream, $client) = @_;
my ($loop, $stream, $id) = @_;

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

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

# Read connect request from client
my $buffer = $buffer{$client}{client} .= $chunk;
if ($buffer =~ /\x0d?\x0a\x0d?\x0a$/) {
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT \S+:\d+/) {
my $client = $buffer{$id}{client} .= $chunk;
if ($client =~ /\x0d?\x0a\x0d?\x0a$/) {
$buffer{$id}{client} = '';
if ($client =~ /CONNECT \S+:\d+/) {

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

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

# Start forwarding data in both directions
Mojo::IOLoop->stream($client)
Mojo::IOLoop->stream($id)
->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);
Mojo::IOLoop->stream($id)->write($chunk);
}
);

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

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

# Client closed connection
$stream->on(
close => sub {
my $buffer = delete $buffer{$client};
my $buffer = delete $buffer{$id};
Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
}
);
Expand Down
30 changes: 15 additions & 15 deletions t/mojo/websocket_proxy.t
Expand Up @@ -43,68 +43,68 @@ my $nf
my $ok = "HTTP/1.0 201 BAR\x0d\x0aX-Something: unimportant\x0d\x0a\x0d\x0a";
my $id = Mojo::IOLoop->server(
{address => '127.0.0.1'} => sub {
my ($loop, $stream, $client) = @_;
my ($loop, $stream, $id) = @_;

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

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

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

# Connection to server
$buffer{$client}{connection} = Mojo::IOLoop->client(
$buffer{$id}{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};
Mojo::IOLoop->remove($id);
return delete $buffer{$id};
}

# Start forwarding data in both directions
Mojo::IOLoop->stream($client)->write($fail ? $nf : $ok);
Mojo::IOLoop->stream($id)->write($fail ? $nf : $ok);
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
$read += length $chunk;
$sent += length $chunk;
Mojo::IOLoop->stream($client)->write($chunk);
Mojo::IOLoop->stream($id)->write($chunk);
}
);

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

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

# Client closed connection
$stream->on(
close => sub {
my $buffer = delete $buffer{$client};
my $buffer = delete $buffer{$id};
Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
}
);
Expand Down
30 changes: 15 additions & 15 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -62,68 +62,68 @@ my $nf
my $ok = "HTTP/1.1 200 OK\x0d\x0aConnection: keep-alive\x0d\x0a\x0d\x0a";
my $id = Mojo::IOLoop->server(
{address => '127.0.0.1'} => sub {
my ($loop, $stream, $client) = @_;
my ($loop, $stream, $id) = @_;

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

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

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

# Connection to server
$buffer{$client}{connection} = Mojo::IOLoop->client(
$buffer{$id}{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};
Mojo::IOLoop->remove($id);
return delete $buffer{$id};
}

# Start forwarding data in both directions
Mojo::IOLoop->stream($client)->write($fail ? $nf : $ok);
Mojo::IOLoop->stream($id)->write($fail ? $nf : $ok);
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
$read += length $chunk;
$sent += length $chunk;
Mojo::IOLoop->stream($client)->write($chunk);
Mojo::IOLoop->stream($id)->write($chunk);
}
);

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

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

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

0 comments on commit 0f42f77

Please sign in to comment.