Skip to content

Commit

Permalink
fix support for CONNECT requests without keep alive connections in Mo…
Browse files Browse the repository at this point in the history
…jo::UserAgent (closes #970)
  • Loading branch information
kraih committed Jun 16, 2016
1 parent 3394000 commit 3be34dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

6.66 2016-06-15
6.66 2016-06-16
- Fixed support for CONNECT requests without keep alive connections in
Mojo::UserAgent. (anparker, sri)

6.65 2016-06-14
- Added password and username methods to Mojo::URL.
Expand Down
21 changes: 12 additions & 9 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -125,18 +125,20 @@ sub _connect_proxy {
($loop, $new) => sub {
my ($self, $tx) = @_;

# CONNECT failed (connection needs to be kept alive)
$old->res->error({message => 'Proxy connection failed'})
and return $self->$cb($old)
if $tx->error || !$tx->res->is_status_class(200) || !$tx->keep_alive;
# CONNECT failed
my $id = $tx->connection;
if ($tx->error || !$tx->res->is_status_class(200) || !$tx->keep_alive) {
$old->res->error({message => 'Proxy connection failed'});
$self->_remove($id);
return $self->$cb($old);
}

# Start real transaction
# Start real transaction without TLS upgrade
$old->req->via_proxy(0);
my $id = $tx->connection;
return $self->_start($loop, $old->connection($id), $cb)
unless $tx->req->url->protocol eq 'https';

# TLS upgrade
# TLS upgrade before starting the real transaction
my $handle = $loop->stream($id)->steal_handle;
$self->_remove($id);
$id = $self->_connect($loop, 0, $old, $handle,
Expand Down Expand Up @@ -273,8 +275,9 @@ sub _reuse {
my ($self, $id, $close) = @_;

# Connection close
my $c = $self->{connections}{$id};
my $tx = delete $c->{tx};
my $c = $self->{connections}{$id};
my $tx = delete $c->{tx};
return if $tx && uc $tx->req->method eq 'CONNECT';
my $max = $self->max_connections;
return $self->_remove($id)
if $close || !$tx || !$max || !$tx->keep_alive || $tx->error;
Expand Down
8 changes: 6 additions & 2 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -248,9 +248,13 @@ is $result, 'test1test2', 'right result';
ok $read > 25, 'read enough';
ok $sent > 25, 'sent enough';

# Blocking proxy request
# Blocking proxy requests
$ua->proxy->https("http://sri:secr3t\@127.0.0.1:$proxy");
my $tx = $ua->get("https://127.0.0.1:$port/proxy");
my $tx = $ua->max_connections(0)->get("https://127.0.0.1:$port/proxy");
is $tx->res->code, 200, 'right status';
is $tx->res->body, "https://127.0.0.1:$port/proxy", 'right content';
$tx = $ua->max_connections(5)->get("https://127.0.0.1:$port/proxy");
ok !$tx->kept_alive, 'connection was not kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, "https://127.0.0.1:$port/proxy", 'right content';

Expand Down

0 comments on commit 3be34dd

Please sign in to comment.