Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
the request does not need to know about SOCKS
  • Loading branch information
kraih committed Jul 29, 2014
1 parent ff1eab3 commit 1cad9c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
5 changes: 2 additions & 3 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -101,15 +101,14 @@ sub get_start_line_chunk {

# CONNECT
my $method = uc $self->method;
my $proxy = $self->proxy;
if ($method eq 'CONNECT') {
my $port = $url->port || ($url->protocol eq 'https' ? '443' : '80');
$path = $url->ihost . ":$port";
}

# Proxy
elsif ($proxy && $proxy->protocol ne 'socks' && !$self->is_handshake) {
$path = $url->clone->userinfo(undef) unless $url->protocol eq 'https';
elsif ((my $proxy = $self->proxy) && $url->protocol ne 'https') {
$path = $url->clone->userinfo(undef) unless $self->is_handshake;
}

$self->{start_buffer} = "$method $path HTTP/@{[$self->version]}\x0d\x0a";
Expand Down
38 changes: 20 additions & 18 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -85,30 +85,33 @@ sub _cleanup {
}

sub _connect {
my ($self, $nb, $tx, $proto, $host, $port, $handle, $cb) = @_;
my ($self, $nb, $peer, $tx, $handle, $cb) = @_;

my $options
= {address => $host, port => $port, timeout => $self->connect_timeout};
if (my $local = $self->local_address) { $options->{local_address} = $local }
$options->{handle} = $handle if $handle;
my $t = $self->transactor;
my ($proto, $host, $port) = $peer ? $t->peer($tx) : $t->endpoint($tx);
my %options
= (address => $host, port => $port, timeout => $self->connect_timeout);
if (my $local = $self->local_address) { $options{local_address} = $local }
$options{handle} = $handle if $handle;

# SOCKS
if ($proto eq 'socks') {
@$options{qw(socks_address socks_port)} = @$options{qw(address port)};
($proto, @$options{qw(address port)}) = $self->transactor->endpoint($tx);
my $userinfo = $tx->req->proxy->userinfo;
@$options{qw(socks_user socks_pass)} = split ':', $userinfo if $userinfo;
@options{qw(socks_address socks_port)} = @options{qw(address port)};
($proto, @options{qw(address port)}) = $t->endpoint($tx);
my $req = $tx->req;
my $userinfo = $req->proxy->userinfo;
$req->proxy(0);
@options{qw(socks_user socks_pass)} = split ':', $userinfo if $userinfo;
}

# TLS
if ($options->{tls} = $proto eq 'https') {
$options->{"tls_$_"} = $self->$_ for qw(ca cert key);
}
map { $options{"tls_$_"} = $self->$_ } qw(ca cert key)
if ($options{tls} = $proto eq 'https');

weaken $self;
my $id;
return $id = $self->_loop($nb)->client(
$options => sub {
%options => sub {
my ($loop, $err, $stream) = @_;

# Connection error
Expand Down Expand Up @@ -141,7 +144,7 @@ sub _connect_proxy {
return $self->$cb($old);
}

# Prevent proxy reassignment and start real transaction
# Start real transaction
$old->req->proxy(0);
my $id = $tx->connection;
return $self->_start($nb, $old->connection($id), $cb)
Expand All @@ -152,8 +155,8 @@ sub _connect_proxy {
my $handle = $loop->stream($id)->steal_handle;
my $c = delete $self->{connections}{$id};
$loop->remove($id);
$id = $self->_connect($nb, $old, $self->transactor->endpoint($old),
$handle, sub { shift->_start($nb, $old->connection($id), $cb) });
$id = $self->_connect($nb, 0, $old, $handle,
sub { shift->_start($nb, $old->connection($id), $cb) });
$self->{connections}{$id} = $c;
}
);
Expand Down Expand Up @@ -199,8 +202,7 @@ sub _connection {

# Connect
warn "-- Connect ($proto:$host:$port)\n" if DEBUG;
($proto, $host, $port) = $self->transactor->peer($tx);
$id = $self->_connect(($nb, $tx, $proto, $host, $port, $id) => \&_connected);
$id = $self->_connect($nb, 1, $tx, $id, \&_connected);
$self->{connections}{$id} = {cb => $cb, nb => $nb, tx => $tx};

return $id;
Expand Down
6 changes: 4 additions & 2 deletions t/mojo/user_agent_socks.t
Expand Up @@ -22,6 +22,8 @@ use Mojo::UserAgent;
use Mojolicious::Lite;
use Scalar::Util 'weaken';

app->log->level('fatal');

get '/' => sub {
my $c = shift;
$c->render(text => $c->tx->remote_port);
Expand Down Expand Up @@ -79,9 +81,9 @@ Mojo::IOLoop->singleton->reactor->io(
$client = Mojo::IOLoop::Stream->new($client);
Mojo::IOLoop->stream($client);
$client->on(read => sub { $server->write(pop) });
$client->on(close => sub { $server->close });
$client->on(close => sub { $server && $server->close });
$server->on(read => sub { $client->write(pop) });
$server->on(close => sub { $client->close });
$server->on(close => sub { $client && $client->close });
}
);
}
Expand Down

0 comments on commit 1cad9c9

Please sign in to comment.