Skip to content

Commit

Permalink
make sure a proxy server is actually used
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 12, 2016
1 parent c1475a3 commit c33f844
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

6.46 2016-02-11
6.46 2016-02-12
- Improved Mojo::Headers performance. (batman)
- Fixed small proxy bug in Mojo::UserAgent::Transactor.

6.45 2016-02-09
- Deprecated Mojo::Util::xss_escape in favor of Mojo::Util::xml_escape.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Proxy.pm
Expand Up @@ -22,7 +22,7 @@ sub prepare {
$self->detect if $ENV{MOJO_PROXY};
my $req = $tx->req;
my $url = $req->url;
return unless $req->via_proxy && $self->is_needed($url->host);
return unless $self->is_needed($url->host);

# HTTP proxy
my $proto = $url->protocol;
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -33,7 +33,7 @@ sub endpoint {
my $socks;
if (my $proxy = $req->proxy) { $socks = $proxy->protocol eq 'socks' }
return $self->_proxy($tx, $proto, $host, $port)
if $proto eq 'http' && $req->via_proxy && !$req->is_handshake && !$socks;
if $proto eq 'http' && !$req->is_handshake && !$socks;

return $proto, $host, $port;
}
Expand Down Expand Up @@ -241,11 +241,10 @@ sub _multipart {
sub _proxy {
my ($self, $tx, $proto, $host, $port) = @_;

# Update with proxy information
if (my $proxy = $tx->req->proxy) {
$proto = $proxy->protocol;
$host = $proxy->ihost;
$port = $proxy->port || ($proto eq 'https' ? 443 : 80);
my $req = $tx->req;
if ($req->via_proxy && (my $proxy = $req->proxy)) {
return $proxy->protocol, $proxy->ihost,
$proxy->port || ($proto eq 'https' ? 443 : 80);
}

return $proto, $host, $port;
Expand Down
7 changes: 7 additions & 0 deletions t/mojo/transactor.t
Expand Up @@ -434,6 +434,13 @@ is(($t->peer($tx))[0], 'http', 'right scheme');
is(($t->peer($tx))[1], '127.0.0.1', 'right host');
is(($t->peer($tx))[2], 3000, 'right port');

# Simple peer with deactivated proxy
$tx = $t->tx(GET => 'http://mojolicious.org');
$tx->req->via_proxy(0)->proxy(Mojo::URL->new('http://127.0.0.1:3000'));
is(($t->peer($tx))[0], 'http', 'right scheme');
is(($t->peer($tx))[1], 'mojolicious.org', 'right host');
is(($t->peer($tx))[2], 80, 'right port');

# Simple peer with SOCKS proxy
$tx = $t->tx(GET => 'http://mojolicious.org');
$tx->req->proxy(Mojo::URL->new('socks://127.0.0.1:3000'));
Expand Down
10 changes: 10 additions & 0 deletions t/mojo/user_agent_socks.t
Expand Up @@ -161,4 +161,14 @@ is $tx->res->body, "https:$last", 'right content';
isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport,
$last, 'different ports');

# Disabled SOCKS proxy
$ua->server->url('http');
$ua->proxy->http("socks://foo:baz\@127.0.0.1:$port");
$tx = $ua->build_tx(GET => '/');
$tx->req->via_proxy(0);
$tx = $ua->start($tx);
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, $tx->local_port, 'right content';

done_testing();

0 comments on commit c33f844

Please sign in to comment.