Skip to content

Commit

Permalink
better connect proxy example
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 9, 2012
1 parent be22a7b commit 2b05362
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 62 deletions.
15 changes: 6 additions & 9 deletions examples/connect-proxy.pl
Expand Up @@ -17,14 +17,12 @@
my ($stream, $chunk) = @_;

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

# Read connect request from client
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
my $buffer = $buffer{$client}{client} .= $chunk;
if ($buffer =~ /\x0d?\x0a\x0d?\x0a$/) {
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
my $address = $1;
Expand Down Expand Up @@ -74,9 +72,8 @@
# Client closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}{connection})
if $buffer{$client}{connection};
delete $buffer{$client};
my $buffer = delete $buffer{$client};
Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
}
);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Exception.pm
Expand Up @@ -102,9 +102,8 @@ sub _detect {
while ($message =~ /at\s+(.+?)\s+line\s+(\d+)/g) { push @trace, [$1, $2] }

# Extract file and line from stacktrace
if (my $first = $self->frames->[0]) {
unshift @trace, [$first->[1], $first->[2]] if $first->[1];
}
my $first = $self->frames->[0];
unshift @trace, [$first->[1], $first->[2]] if $first && $first->[1];

# Search for context
for my $frame (reverse @trace) {
Expand Down
10 changes: 4 additions & 6 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -174,14 +174,12 @@ sub parse {
if (!$base->host && (my $host = $headers->host)) { $base->authority($host) }

# Basic authentication
if (my $userinfo = _parse_basic_auth($headers->authorization)) {
$base->userinfo($userinfo);
}
my $auth = _parse_basic_auth($headers->authorization);
$base->userinfo($auth) if $auth;

# Basic proxy authentication
if (my $userinfo = _parse_basic_auth($headers->proxy_authorization)) {
$self->proxy(Mojo::URL->new->userinfo($userinfo));
}
my $proxy_auth = _parse_basic_auth($headers->proxy_authorization);
$self->proxy(Mojo::URL->new->userinfo($proxy_auth)) if $proxy_auth;

# "X-Forwarded-HTTPS"
$base->scheme('https')
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Template.pm
Expand Up @@ -298,10 +298,9 @@ sub render_file {
my $tmpl = slurp $path;

# Decode and render
if (my $encoding = $self->encoding) {
croak qq{Template "$path" has invalid encoding.}
unless defined($tmpl = decode $encoding, $tmpl);
}
my $encoding = $self->encoding;
croak qq{Template "$path" has invalid encoding.}
if $encoding && !defined($tmpl = decode $encoding, $tmpl);
return $self->render($tmpl, @_);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Mojo/URL.pm
Expand Up @@ -34,9 +34,9 @@ sub authority {
}

# Format
if (my $userinfo = $self->userinfo) {
$authority .= url_escape($userinfo, '^A-Za-z0-9\-._~!$&\'()*+,;=\:') . '@';
}
my $userinfo = $self->userinfo;
$authority .= url_escape($userinfo, '^A-Za-z0-9\-._~!$&\'()*+,;=\:') . '@'
if $userinfo;
$authority .= lc($self->ihost || '');
if (my $port = $self->port) { $authority .= ":$port" }

Expand Down Expand Up @@ -220,9 +220,9 @@ sub to_string {
$url .= "?$query" if length $query;

# Fragment
if (my $fragment = $self->fragment) {
$url .= '#' . url_escape $fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?';
}
my $fragment = $self->fragment;
$url .= '#' . url_escape $fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?'
if $fragment;

return $url;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -441,14 +441,13 @@ sub _start {
if ($self->need_proxy($url->host)) {

# HTTP proxy
if (my $proxy = $self->http_proxy) {
$req->proxy($proxy) if !defined $req->proxy && $scheme eq 'http';
}
my $http = $self->http_proxy;
$req->proxy($http) if $http && !defined $req->proxy && $scheme eq 'http';

# HTTPS proxy
if (my $proxy = $self->https_proxy) {
$req->proxy($proxy) if !defined $req->proxy && $scheme eq 'https';
}
my $https = $self->https_proxy;
$req->proxy($https)
if $https && !defined $req->proxy && $scheme eq 'https';
}

# We identify ourselves
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -185,12 +185,11 @@ sub websocket {
my $self = shift;

# New WebSocket transaction
my $tx = $self->tx(GET => @_);
my $req = $tx->req;
my $abs = $req->url->to_abs;
if (my $scheme = $abs->scheme) {
$req->url($abs->scheme($scheme eq 'wss' ? 'https' : 'http'));
}
my $tx = $self->tx(GET => @_);
my $req = $tx->req;
my $abs = $req->url->to_abs;
my $scheme = $abs->scheme;
$req->url($abs->scheme($scheme eq 'wss' ? 'https' : 'http')) if $scheme;

# Handshake
Mojo::Transaction::WebSocket->new(handshake => $tx, masked => 1)
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -192,10 +192,9 @@ sub _detect_handler {

sub _extends {
my ($self, $c) = @_;
my $stash = $c->stash;
if (my $layout = delete $stash->{layout}) {
$stash->{extends} ||= 'layouts' . '/' . $layout;
}
my $stash = $c->stash;
my $layout = delete $stash->{layout};
$stash->{extends} ||= join('/', 'layouts', $layout) if $layout;
return delete $stash->{extends};
}

Expand Down
15 changes: 6 additions & 9 deletions t/mojo/websocket_proxy.t
Expand Up @@ -68,14 +68,12 @@ Mojo::IOLoop->server(
my ($stream, $chunk) = @_;

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

# Read connect request from client
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
my $buffer = $buffer{$client}{client} .= $chunk;
if ($buffer =~ /\x0d?\x0a\x0d?\x0a$/) {
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
$connected = "$1:$2";
Expand Down Expand Up @@ -123,9 +121,8 @@ Mojo::IOLoop->server(
# Client closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}{connection})
if $buffer{$client}{connection};
delete $buffer{$client};
my $buffer = delete $buffer{$client};
Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
}
);
}
Expand Down
15 changes: 6 additions & 9 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -86,14 +86,12 @@ Mojo::IOLoop->server(
my ($stream, $chunk) = @_;

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

# Read connect request from client
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
my $buffer = $buffer{$client}{client} .= $chunk;
if ($buffer =~ /\x0d?\x0a\x0d?\x0a$/) {
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
$connected = "$1:$2";
Expand Down Expand Up @@ -141,9 +139,8 @@ Mojo::IOLoop->server(
# Client closed connection
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}{connection})
if $buffer{$client}{connection};
delete $buffer{$client};
my $buffer = delete $buffer{$client};
Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
}
);
}
Expand Down

0 comments on commit 2b05362

Please sign in to comment.