Skip to content

Commit

Permalink
Merge pull request #310 from CPAN-API/leo/add-content-type-meta
Browse files Browse the repository at this point in the history
Add a X-Content-Type header start for #272
  • Loading branch information
ranguard committed Apr 13, 2014
2 parents 45c0241 + 3c55985 commit 1c52882
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/MetaCPAN/Server/Controller/Source.pm
Expand Up @@ -5,6 +5,7 @@ use warnings;

use Moose;
use Plack::App::Directory;
use Plack::MIME;

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand Down Expand Up @@ -32,6 +33,12 @@ sub get : Chained('index') : PathPart('') : Args {
}
else {
$c->stash->{path} = $file;
# Tell fastly to cache for a day (for st.aticpan.org,
# api.metacpan.org does not go through fastly)
my $max_age_seconds = 60 * 60 * 24;
$c->res->header('Surrogate-Control' => "max-age=${max_age_seconds}");
# Add X-Content-Type header, for fastly to rewrite on st.aticpan.org
$c->res->header( 'X-Content-Type' => Plack::MIME->mime_type($file) || 'text/plain' );
$c->res->content_type('text/plain');
$c->res->body( $file->openr );
}
Expand Down
10 changes: 9 additions & 1 deletion t/server/controller/source.t
Expand Up @@ -21,10 +21,18 @@ test_psgi app, sub {
is( $res->code, $v, "code $v" );
if ( $k eq '/source/Moose' ) {
like( $res->content, qr/package Moose/, 'Moose source' );
is( $res->header('content-type'),
is(
$res->header('content-type'),
'text/plain; charset=UTF-8',
'Content-type'
);
# Used for fastly on st.aticpan.org
is( $res->header('X-Content-Type'),
'text/x-script.perl-module', 'X-Content-Type' );

is( $res->header('Surrogate-Control'),
'max-age=86400', 'Surrogate-Control' );

}
elsif ( $k =~ /MANIFEST/ ) {

Expand Down

0 comments on commit 1c52882

Please sign in to comment.