Skip to content

Commit

Permalink
Merge branch 'feature/static_304'
Browse files Browse the repository at this point in the history
  • Loading branch information
cromedome committed Oct 10, 2016
2 parents 6dc4a48 + 8d9cee4 commit a803b0a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
files in addition to standard Dancer conf files (Jonathan Scott Duff)
* PR #1269: Stash decoded body_parameters separately from those
in Plack::Request (Russell @veryrusty Jenkins)
* GH #1253: Static middleware should send 304 Not Modified to enable
intermediate level caching. (Russell @veryrusty Jenkins)

[ DOCUMENTATION ]
* GH #608: Remove extra general COPYRIGHT notice in Tutorial.
Expand Down
21 changes: 14 additions & 7 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use Sub::Quote;
use File::Spec;
use Module::Runtime 'use_module';

use Plack::App::File;
use Plack::Middleware::FixMissingBodyInRedirect;
use Plack::Middleware::Head;
use Plack::Middleware::Static;
use Plack::Middleware::Conditional;
use Plack::Middleware::ConditionalGET;

use Dancer2::FileUtils 'path';
use Dancer2::Core;
Expand Down Expand Up @@ -1368,14 +1370,19 @@ sub to_app {
# FixMissingBodyInRedirect
$psgi = Plack::Middleware::FixMissingBodyInRedirect->wrap( $psgi );

# Static content passes through to app on 404, conditionally applied.
# Construct the statis app to avoid a closure over $psgi
# Only add static content handler if requires
if ( $self->config->{'static_handler'} ) {
$psgi = Plack::Middleware::Static->wrap(
$psgi,
path => sub { -f path( $self->config->{public_dir}, shift ) },
root => $self->config->{public_dir},
# Use App::File to "serve" the static content
my $static_app = Plack::App::File->new(
root => $self->config->{public_dir},
content_type => sub { $self->mime_type->for_name(shift) },
)->to_app;
# Conditionally use the static handler wrapped with ConditionalGET
# when the file exists. Otherwise the request passes into our app.
$psgi = Plack::Middleware::Conditional->wrap(
$psgi,
condition => sub { -f path( $self->config->{public_dir}, shift->{PATH_INFO} ) },
builder => sub { Plack::Middleware::ConditionalGET->wrap( $static_app ) },
);
}

Expand Down
30 changes: 30 additions & 0 deletions t/static_content.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use strict;
use warnings;
use utf8;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;

{
package PublicContent;
use Dancer2;

set public_dir => 't/corpus/static';

get '/' => sub { return 'Welcome Home' };

}

my $test = Plack::Test->create( PublicContent->to_app );

subtest 'public content' => sub {
my $res = $test->request( GET '/1x1.png' );
is $res->code, 200, "200 response";
my $last_modified = $res->header('Last-Modified');

$res = $test->request( GET '/1x1.png', 'If-Modified-Since' => $last_modified );
is $res->code, 304, "304 response";
};

done_testing();

0 comments on commit a803b0a

Please sign in to comment.