Skip to content

Commit

Permalink
fixed keep alive bug in Mojo::Server::Daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 20, 2012
1 parent 5661a84 commit 6c03571
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 90 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.59 2012-11-20
- Improved tests.
- Fixed keep alive bug in Mojo::Server::Daemon.

3.58 2012-11-19
- Improved documentation.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -211,9 +211,9 @@ sub _read {
warn "-- Server <<< Client (@{[$tx->req->url->to_abs]})\n$chunk\n" if DEBUG;
$tx->server_read($chunk);

# Last keep alive request
# Last keep alive request or corrupted connection
$tx->res->headers->connection('close')
if ($c->{requests} || 0) >= $self->max_requests;
if (($c->{requests} || 0) >= $self->max_requests) || $tx->req->error;

# Finish or start writing
if ($tx->is_finished) { $self->_finish($id, $tx) }
Expand Down
90 changes: 2 additions & 88 deletions t/mojolicious/lite_app.t
Expand Up @@ -12,7 +12,6 @@ BEGIN {
use Test::More;
use Mojo::ByteStream 'b';
use Mojo::Cookie::Response;
use Mojo::Date;
use Mojo::IOLoop;
use Mojo::JSON;
use Mojo::Transaction::HTTP;
Expand Down Expand Up @@ -448,9 +447,7 @@ get '/redirect_callback' => sub {
};

# GET /static_render
get '/static_render' => sub {
shift->render_static('hello.txt');
};
get '/static_render' => sub { shift->render_static('hello.txt') };

# GET /koi8-r
app->types->type('koi8-r' => 'text/html; charset=koi8-r');
Expand All @@ -460,9 +457,6 @@ get '/koi8-r' => sub {
app->renderer->encoding(undef);
};

# GET /hello3.txt
get '/hello3.txt' => sub { shift->render_static('hello2.txt') };

# GET /captures/*/*
get '/captures/:foo/:bar' => sub {
my $self = shift;
Expand Down Expand Up @@ -715,48 +709,13 @@ $t->get_ok('/static.txt')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("Just some\ntext!\n\n");

# GET /static.txt (static inline file, If-Modified-Since)
my $modified = Mojo::Date->new->epoch(time - 3600);
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("Just some\ntext!\n\n");
$modified = $t->tx->res->headers->last_modified;
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
->status_is(304)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('');

# GET /static.txt (partial inline file)
$t->get_ok('/static.txt' => {'Range' => 'bytes=2-5'})->status_is(206)
$t->get_ok('/static.txt' => {Range => 'bytes=2-5'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 4)
->content_is('st s');

# GET /static.txt (base64 static inline file)
$t->get_ok('/static2.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("test 123\nlalala");

# GET /static.txt (base64 static inline file, If-Modified-Since)
$modified = Mojo::Date->new->epoch(time - 3600);
$t->get_ok('/static2.txt' => {'If-Modified-Since' => $modified})
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("test 123\nlalala");
$modified = $t->tx->res->headers->last_modified;
$t->get_ok('/static2.txt' => {'If-Modified-Since' => $modified})
->status_is(304)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('');

# GET /static.txt (base64 partial inline file)
$t->get_ok('/static2.txt' => {'Range' => 'bytes=2-5'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 4)
->content_is('st 1');

# GET /template.txt.epl (protected DATA template)
$t->get_ok('/template.txt.epl')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down Expand Up @@ -1219,48 +1178,6 @@ $t->get_ok('/koi8-r')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_type_is('text/html; charset=koi8-r')->content_like(qr/^$koi8/);

# GET /hello.txt (static file)
$t->get_ok('/hello.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 31)
->content_is("Hello Mojo from a static file!\n");

# GET /hello.txt (partial static file)
$t->get_ok('/hello.txt' => {'Range' => 'bytes=2-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 7)
->header_is('Content-Range: bytes 2-8/31')->content_is('llo Moj');

# GET /hello.txt (partial static file, starting at first byte)
$t->get_ok('/hello.txt' => {'Range' => 'bytes=0-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 9)
->header_is('Content-Range: bytes 0-8/31')->content_is('Hello Moj');

# GET /hello.txt (partial static file, first byte)
$t->get_ok('/hello.txt' => {'Range' => 'bytes=0-0'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->header_is('Content-Range: bytes 0-0/31')->content_is('H');

# GET /hello3.txt (render_static and single byte file)
$t->get_ok('/hello3.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->content_is('X');

# GET /hello3.txt (render_static and partial single byte file)
$t->get_ok('/hello3.txt' => {'Range' => 'bytes=0-0'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->header_is('Content-Range: bytes 0-0/1')->content_is('X');

# GET /default/condition
$t->get_ok('/default/condition')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down Expand Up @@ -1359,9 +1276,6 @@ text!
@@ test(test)(\Qtest\E)(.html.ep
<%= $self->match->endpoint->name %>
@@ static2.txt (base64)
dGVzdCAxMjMKbGFsYWxh
@@ with_header_condition.html.ep
Test ok<%= base_tag %>
Expand Down
108 changes: 108 additions & 0 deletions t/mojolicious/static_lite_app.t
@@ -0,0 +1,108 @@
use Mojo::Base -strict;

# Disable IPv6 and libev
BEGIN {
$ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More;
use Mojo::Date;
use Mojolicious::Lite;
use Test::Mojo;

# GET /hello3.txt
get '/hello3.txt' => sub { shift->render_static('hello2.txt') };

my $t = Test::Mojo->new;

# GET /hello.txt (static file)
$t->get_ok('/hello.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 31)
->content_is("Hello Mojo from a static file!\n");

# GET /hello.txt (partial static file)
$t->get_ok('/hello.txt' => {Range => 'bytes=2-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 7)
->header_is('Content-Range: bytes 2-8/31')->content_is('llo Moj');

# GET /hello.txt (partial static file, no end)
$t->get_ok('/hello.txt' => {Range => 'bytes=8-'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 23)
->header_is('Content-Range: bytes 8-31/31')
->content_is("jo from a static file!\n");

# GET /hello.txt (partial static file, starting at first byte)
$t->get_ok('/hello.txt' => {Range => 'bytes=0-8'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 9)
->header_is('Content-Range: bytes 0-8/31')->content_is('Hello Moj');

# GET /hello.txt (partial static file, first byte)
$t->get_ok('/hello.txt' => {Range => 'bytes=0-0'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->header_is('Content-Range: bytes 0-0/31')->content_is('H');

# GET /hello3.txt (render_static and single byte file)
$t->get_ok('/hello3.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->content_is('X');

# GET /hello3.txt (render_static and partial single byte file)
$t->get_ok('/hello3.txt' => {Range => 'bytes=0-0'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 1)
->header_is('Content-Range: bytes 0-0/1')->content_is('X');

# GET /static.txt (base64 static inline file, If-Modified-Since)
my $modified = Mojo::Date->new->epoch(time - 3600);
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("test 123\nlalala");
$modified = $t->tx->res->headers->last_modified;
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
->status_is(304)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('');

# GET /static.txt (base64 static inline file)
$t->get_ok('/static.txt')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("test 123\nlalala");

# GET /static.txt (base64 static inline file, If-Modified-Since)
$modified = Mojo::Date->new->epoch(time - 3600);
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->content_is("test 123\nlalala");
$modified = $t->tx->res->headers->last_modified;
$t->get_ok('/static.txt' => {'If-Modified-Since' => $modified})
->status_is(304)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('');

# GET /static.txt (base64 partial inline file)
$t->get_ok('/static.txt' => {Range => 'bytes=2-5'})->status_is(206)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Accept-Ranges' => 'bytes')->header_is('Content-Length' => 4)
->content_is('st 1');

done_testing();

__DATA__
@@ static.txt (base64)
dGVzdCAxMjMKbGFsYWxh

0 comments on commit 6c03571

Please sign in to comment.