Skip to content

Commit

Permalink
fix bug where Mojo::UserAgent would try to follow redirects for CONNE…
Browse files Browse the repository at this point in the history
…CT requests
  • Loading branch information
kraih committed Aug 21, 2016
1 parent 25ae8ab commit e9f640d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

7.04 2016-08-18
7.04 2016-08-21
- Fixed bug where Mojo::UserAgent would try to follow redirects for CONNECT
requests.

7.03 2016-08-17
- Fixed packaging errors.
Expand Down
7 changes: 5 additions & 2 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -71,16 +71,19 @@ sub redirect {
my $code = $res->code // 0;
return undef unless grep { $_ == $code } 301, 302, 303, 307, 308;

# CONNECT requests cannot be redirected
my $req = $old->req;
return undef if uc $req->method eq 'CONNECT';

# Fix location without authority and/or scheme
return undef unless my $location = $res->headers->location;
$location = Mojo::URL->new($location);
$location = $location->base($old->req->url)->to_abs unless $location->is_abs;
$location = $location->base($req->url)->to_abs unless $location->is_abs;
my $proto = $location->protocol;
return undef if ($proto ne 'http' && $proto ne 'https') || !$location->host;

# Clone request if necessary
my $new = Mojo::Transaction::HTTP->new;
my $req = $old->req;
if ($code == 307 || $code == 308) {
return undef unless my $clone = $req->clone;
$new->req($clone);
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/transactor.t
Expand Up @@ -927,6 +927,12 @@ is $tx->req->body, '', 'no content';
is $tx->res->code, undef, 'no status';
is $tx->res->headers->location, undef, 'no "Location" value';

# 302 redirect for CONNECT request
$tx = $t->tx(CONNECT => 'http://mojolicious.org');
$tx->res->code(302);
$tx->res->headers->location('http://example.com/bar');
is $t->redirect($tx), undef, 'unsupported redirect';

# Abstract methods
eval { Mojo::Transaction->client_read };
like $@, qr/Method "client_read" not implemented by subclass/, 'right error';
Expand Down

0 comments on commit e9f640d

Please sign in to comment.