Skip to content

Commit

Permalink
fixed bug in Mojo::UserAgent where HTTP/1.0 connections were sometime…
Browse files Browse the repository at this point in the history
…s kept alive
  • Loading branch information
kraih committed Jun 11, 2014
1 parent c58082b commit 15e02f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@

5.06 2014-06-11
- Improved redirect_to to behave more like url_for.
- Fixed bug in Mojo::UserAgent where HTTP/1.0 connections were sometimes
kept alive.

5.05 2014-06-08
- Fixed parsing of header fields with single character names in
Expand Down
9 changes: 5 additions & 4 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -35,11 +35,12 @@ sub keep_alive {
my $res_conn = lc($res->headers->connection // '');
return undef if $req_conn eq 'close' || $res_conn eq 'close';

# Keep-alive
return 1 if $req_conn eq 'keep-alive' || $res_conn eq 'keep-alive';
# Keep-alive is optional for 1.0
if ($res->version eq '1.0') { return $res_conn eq 'keep-alive' }
if ($req->version eq '1.0') { return $req_conn eq 'keep-alive' }

# No keep-alive for 1.0
return !($req->version eq '1.0' || $res->version eq '1.0');
# 1.1
return 1;
}

sub redirects {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -61,7 +61,7 @@ sub proxy_connect {
# CONNECT request (expect a bad response)
my $new = $self->tx(CONNECT => $url->clone->userinfo(undef));
$new->req->proxy($proxy);
$new->res->content->auto_relax(0);
$new->res->content->auto_relax(0)->headers->connection('keep-alive');

return $new;
}
Expand Down
21 changes: 21 additions & 0 deletions t/mojo/user_agent.t
Expand Up @@ -48,6 +48,12 @@ post '/echo' => sub {

any '/method' => {inline => '<%= $self->req->method =%>'};

get '/one' => sub {
my $self = shift;
$self->res->version('1.0')->headers->connection('test');
$self->render(text => 'One!');
};

# Max redirects
{
local $ENV{MOJO_MAX_REDIRECTS} = 25;
Expand Down Expand Up @@ -152,6 +158,21 @@ is $ua->patch('/method')->res->body, 'PATCH', 'right method';
is $ua->post('/method')->res->body, 'POST', 'right method';
is $ua->put('/method')->res->body, 'PUT', 'right method';

# No keep-alive
$tx = $ua->get('/one');
ok $tx->success, 'successful';
ok !$tx->keep_alive, 'connection will not be kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->headers->connection, 'test', 'right "Connection" value';
is $tx->res->body, 'One!', 'right content';
$tx = $ua->get('/one');
ok $tx->success, 'successful';
ok !$tx->kept_alive, 'kept connection not alive';
ok !$tx->keep_alive, 'connection will not be kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->headers->connection, 'test', 'right "Connection" value';
is $tx->res->body, 'One!', 'right content';

# Error in callback is logged
app->ua->once(error => sub { Mojo::IOLoop->stop });
ok app->ua->has_subscribers('error'), 'has subscribers';
Expand Down

0 comments on commit 15e02f7

Please sign in to comment.