Navigation Menu

Skip to content

Commit

Permalink
use better content types for source requests
Browse files Browse the repository at this point in the history
If the API source endpoint is accessed directly, rather than through
st.aticpan.org, provide a more reasonable content type.  Images are
given a the appropriate type for the file, text files are served as
text/plain, and everything else is octet-stream.
  • Loading branch information
haarg committed Dec 20, 2016
1 parent ae5652b commit f67221f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/MetaCPAN/Server/Controller/Source.pm
Expand Up @@ -40,9 +40,19 @@ sub get : Chained('index') : PathPart('') : Args {
$c->stash->{path} = $file;

# 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');
my $type = Plack::MIME->mime_type($file) || 'text/plain';
$c->res->header( 'X-Content-Type' => $type );
if ( $type =~ m{^image/} ) {
$c->res->content_type($type);
}
elsif ( $type
=~ m{^(?:text/.*|application/javascript|application/json)$} )
{
$c->res->content_type('text/plain');
}
else {
$c->res->content_type('application/octet-stream');
}
$c->res->body( $file->openr );
}
}
Expand Down

0 comments on commit f67221f

Please sign in to comment.