File Coverage

blib/lib/Mojolicious/Plugin/GoogleFontProxy.pm
Criterion Covered Total %
statement 35 35 100.0
branch 4 6 66.6
condition 8 10 80.0
subroutine 5 5 100.0
pod 1 1 100.0
total 53 57 92.9


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::GoogleFontProxy;
2              
3             # ABSTRACT: a small proxy that can be useful when you use Google fonts in your website
4              
5 3     3   2314 use Mojo::Base 'Mojolicious::Plugin';
  3         8  
  3         25  
6              
7             our $VERSION = '0.03';
8              
9             our $CSS_URL_FORMAT = 'https://fonts.googleapis.com/css%s?family=%s';
10             our $FONT_URL_FORMAT = 'https://fonts.gstatic.com/s/%s';
11             our $USER_AGENT_STRING = '';
12              
13             sub register {
14 3     3 1 133 my ($self, $app, $config) = @_;
15              
16 3 50       16 if ( !$config->{no_types} ) {
17 3         25 $app->types->type( 'woff2' => 'font/woff2' );
18 3         240 $app->types->type( 'woff' => 'font/woff' );
19 3         50 $app->types->type( 'ttf' => 'font/ttf' );
20 3         45 $app->types->type( 'otf' => 'font/opentype' );
21             }
22              
23             $app->hook(
24             after_render => sub {
25 13     13   124939 my ($c, $content, $format) = @_;
26              
27 13 50       48 return if !$format;
28 13 100 100     78 return if $format ne 'html' && $format ne 'css';
29              
30 8         73 $$content =~ s{
31             https://fonts.googleapis.com/css
32             (?[0-9]?)
33             \?family=(?.*?)
34             (?['"])
35             }{$c->url_for(
36             'google-proxy-css',
37             version => $+{version} || 0,
38             file => $+{file},
39 3   100     66 ) . $+{suffix};
40             }xge;
41             }
42 3         66 );
43              
44             $app->routes->get( '/google/css/:version/*file' )->to( cb => sub {
45 3     3   34179 my $c = shift;
46              
47 3         14 $c->render_later;
48              
49 3   100     91 my $version = $c->param('version') || '';
50 3         146 my $url = sprintf $CSS_URL_FORMAT, $version, $c->param('file');
51 3   33     102 my $ua_string = $USER_AGENT_STRING || $c->tx->req->headers->user_agent;
52              
53             $c->ua->get( $url => { 'User-Agent' => $ua_string } => sub {
54 3         8135 my ($ua, $tx) = @_;
55              
56 3         17 my $body = $tx->res->body;
57 3         92 $body =~ s{
58             https?://fonts\.gstatic\.com/s/(.*?)\) \s* format\('(.*?)'\)
59 16         10961 }{$c->url_for('google-proxy-font', file => $1, fformat => $2) . ") format('$2')"}xmseg;
60              
61 3         1554 return $c->render( data => $body, format => 'css' );
62 3         32 });
63 3         81 })->name( 'google-proxy-css' );
64              
65             $app->routes->get( '/google/font/:fformat/*file' )->to( cb => sub {
66 2     2   19766 my $c = shift;
67              
68 2         11 $c->render_later;
69              
70 2         45 my $url = sprintf $FONT_URL_FORMAT, $c->param('file');
71              
72             $c->ua->get( $url => sub {
73 2         5579 my ($ua, $tx) = @_;
74              
75 2         9 my $body = $tx->res->body;
76              
77 2         54 return $c->render( data => $body, format => $c->param('fformat') );
78 2         71 });
79 3         1905 })->name( 'google-proxy-font' );
80             }
81              
82             1;
83              
84             __END__