Skip to content

Commit

Permalink
there is no need to use the Connection header all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 12, 2015
1 parent 1d35726 commit fe2d4f5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

6.15 2015-08-12
- Improved Mojo::Message to generate slightly smaller HTTP messages.
- Improved Mojo::Message and Mojo::Transaction::HTTP to generate slightly
smaller HTTP messages.
- Fixed warnings in Mojo::DOM.

6.14 2015-07-12
Expand Down
12 changes: 2 additions & 10 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -142,16 +142,8 @@ sub _write {
# Nothing written yet
$self->{$_} ||= 0 for qw(offset write);
my $msg = $server ? $self->res : $self->req;
unless ($self->{http_state}) {

# Connection header
my $headers = $msg->headers;
$headers->connection($self->keep_alive ? 'keep-alive' : 'close')
unless $headers->connection;

# Switch to start-line
@$self{qw(http_state write)} = ('start_line', $msg->start_line_size);
}
@$self{qw(http_state write)} = ('start_line', $msg->start_line_size)
unless $self->{http_state};

# Start-line
my $chunk = '';
Expand Down
13 changes: 5 additions & 8 deletions t/mojo/daemon.t
Expand Up @@ -162,27 +162,24 @@ is $tx->res->body, 'Whatever!', 'right content';
$tx = $ua->get('/close/' => {Connection => 'close'});
ok !$tx->keep_alive, 'will not be kept alive';
ok $tx->kept_alive, 'was kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->headers->connection, 'close', 'right "Connection" value';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Whatever!', 'right content';

# Second non-keep-alive request
$tx = $ua->get('/close/' => {Connection => 'close'});
ok !$tx->keep_alive, 'will not be kept alive';
ok !$tx->kept_alive, 'was not kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->headers->connection, 'close', 'right "Connection" value';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Whatever!', 'right content';

# HTTP/1.0 request
$tx = $ua->build_tx(GET => '/normal/');
$tx->req->version('1.0');
$tx = $ua->start($tx);
ok !$tx->keep_alive, 'will not be kept alive';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code, 200, 'right status';
is $tx->res->headers->connection, 'close', 'right "Connection" value';
is $tx->res->body, 'Whatever!', 'right content';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Whatever!', 'right content';

# POST request
$tx = $ua->post('/fun/' => {Expect => 'fun'} => 'foo bar baz' x 128);
Expand Down
37 changes: 29 additions & 8 deletions t/mojo/user_agent.t
Expand Up @@ -49,7 +49,12 @@ any '/method' => {inline => '<%= $c->req->method =%>'};

get '/one' => sub {
my $c = shift;
$c->res->version('1.0')->headers->connection('test');

$c->res->version('1.0');
if (my $connection = $c->param('connection')) {
$c->res->headers->connection($connection);
}

$c->render(text => 'One!');
};

Expand Down Expand Up @@ -141,18 +146,24 @@ like $tx->error->{message}, qr/IO::Socket::SSL/, 'right error';
$tx = $ua->get('/');
ok $tx->success, 'successful';
ok !$tx->kept_alive, 'kept connection not alive';
is $tx->res->code, 200, 'right status';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code, 200, 'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->body, 'works!', 'right content';

# Again
$tx = $ua->get('/');
ok $tx->success, 'successful';
ok $tx->kept_alive, 'kept connection alive';
is $tx->res->code, 200, 'right status';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code, 200, 'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->body, 'works!', 'right content';
$tx = $ua->get('/');
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code, 200, 'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->body, 'works!', 'right content';

# Shortcuts for common request methods
Expand All @@ -165,19 +176,29 @@ 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');
$tx = $ua->get('/one?connection=test');
ok $tx->success, 'successful';
ok !$tx->keep_alive, 'connection will not be kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->version, '1.0', 'right version';
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');
$tx = $ua->get('/one?connection=test');
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->version, '1.0', 'right version';
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->version, '1.0', 'right version';
is $tx->res->code, 200, 'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->body, 'One!', 'right content';

# Error in callback
Mojo::IOLoop->singleton->reactor->unsubscribe('error');
Expand Down

0 comments on commit fe2d4f5

Please sign in to comment.