Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1202 from CPAN-API/leo/psgi_refactor
Stop setting session cookies on /static and /_asset
  • Loading branch information
oalders committed May 22, 2014
2 parents 2a5f72a + 4bd7536 commit f34b366
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
90 changes: 52 additions & 38 deletions app.psgi
Expand Up @@ -29,12 +29,37 @@ use Plack::Middleware::ServerStatus::Lite;

# explicitly call ->to_app on every Plack::App::* for performance
my $app = Plack::App::URLMap->new;
$app->map(
'/static/' => Plack::App::File->new( root => 'root/static' )->to_app );
$app->map( '/favicon.ico' =>
Plack::App::File->new( file => 'root/static/icons/favicon.ico' )
->to_app );
$app->map( '/' => MetaCPAN::Web->psgi_app );

# Static content
{
my $static_app = Plack::App::File->new( root => 'root/static' )->to_app;
$app->map( '/static/' => $static_app );
}

# favicon
{
my $fav_app
= Plack::App::File->new( file => 'root/static/icons/favicon.ico' )
->to_app;
$app->map( '/favicon.ico' => $fav_app );
}

# Main catalyst app
{
my $core_app = MetaCPAN::Web->psgi_app;

# Add session cookie here only
$core_app = Plack::Middleware::Session::Cookie->wrap(
$core_app,
session_key => 'metacpan_secure',
expires => 2**30,
secure => 1,
httponly => 1,
);

$app->map( '/' => $core_app );
}

$app = $app->to_app;

unless ( $ENV{HARNESS_ACTIVE} ) {
Expand Down Expand Up @@ -115,35 +140,24 @@ if ( !$ENV{PLACK_ENV} || $ENV{PLACK_ENV} ne 'development' ) {
}

# Handle surrogate (fastly caching)
my $hour_ttl = 60 * 60;
my $day_ttl = $hour_ttl * 24;

$app = builder {

# Tell fastly to cache _asset and _asset_less for a day
enable_if { $_[0]->{PATH_INFO} =~ m{^/_asset} } 'Headers',
set => [ 'Surrogate-Control' => "max-age=${day_ttl}" ];

# Tell fastly to cache /static/ for an hour
enable_if { $_[0]->{PATH_INFO} =~ m{^/static} } 'Headers',
set => [ 'Surrogate-Control' => "max-age=${hour_ttl}" ];

$app;
};

Plack::Middleware::ReverseProxy->wrap(
sub {
my $env = shift;
my $secure = $env->{'HTTP_X_FORWARDED_PORT'}
&& $env->{'HTTP_X_FORWARDED_PORT'} eq '443';
Plack::Middleware::Session::Cookie->wrap(
$app,
session_key => $secure
? 'metacpan_secure'
: 'metacpan',
expires => 2**30,
$secure ? ( secure => 1 ) : (),
httponly => 1,
)->($env);
}
);
{
my $hour_ttl = 60 * 60;
my $day_ttl = $hour_ttl * 24;

$app = builder {

# Tell fastly to cache _asset and _asset_less for a day
enable_if { $_[0]->{PATH_INFO} =~ m{^/_asset} } 'Headers',
set => [ 'Surrogate-Control' => "max-age=${day_ttl}" ];

# Tell fastly to cache /static/ for an hour
enable_if { $_[0]->{PATH_INFO} =~ m{^/static} } 'Headers',
set => [ 'Surrogate-Control' => "max-age=${hour_ttl}" ];

$app;
};
}

$app = Plack::Middleware::ReverseProxy->wrap($app);

return $app;
18 changes: 18 additions & 0 deletions t/fastly_headers.t
@@ -0,0 +1,18 @@
use strict;
use warnings;
use Test::More;
use MetaCPAN::Web::Test;

test_psgi app, sub {
my $cb = shift;
{
ok( my $res = $cb->( GET "/static/images/logo.png" ),
'GET /static/images/logo.png...' );
is( $res->code, 200, 'code 200' );
is( $res->header('Set-Cookie'), undef, "No cookie" );
is( $res->header('Surrogate-Control'),
'max-age=3600', 'Image Surrogate-Control' );
}
};

done_testing;

0 comments on commit f34b366

Please sign in to comment.