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 embed gists in your website |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
2006
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
19
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $CSS_URL_FORMAT = 'https://fonts.googleapis.com/css?family=%s'; |
10
|
|
|
|
|
|
|
our $FONT_URL_FORMAT = 'https://fonts.gstatic.com/s/%s'; |
11
|
|
|
|
|
|
|
our $USER_AGENT_STRING = ''; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub register { |
14
|
2
|
|
|
2
|
1
|
111
|
my ($self, $app, $config) = @_; |
15
|
|
|
|
|
|
|
|
16
|
2
|
50
|
|
|
|
13
|
if ( !$config->{no_types} ) { |
17
|
2
|
|
|
|
|
24
|
$app->types->type( 'woff2' => 'font/woff2' ); |
18
|
2
|
|
|
|
|
157
|
$app->types->type( 'woff' => 'font/woff' ); |
19
|
2
|
|
|
|
|
32
|
$app->types->type( 'ttf' => 'font/ttf' ); |
20
|
2
|
|
|
|
|
27
|
$app->types->type( 'otf' => 'font/opentype' ); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$app->hook( |
24
|
|
|
|
|
|
|
after_render => sub { |
25
|
8
|
|
|
8
|
|
88169
|
my ($c, $content, $format) = @_; |
26
|
|
|
|
|
|
|
|
27
|
8
|
50
|
|
|
|
42
|
return if !$format; |
28
|
8
|
100
|
100
|
|
|
72
|
return if $format ne 'html' && $format ne 'css'; |
29
|
|
|
|
|
|
|
|
30
|
5
|
|
|
|
|
49
|
$$content =~ s{ |
31
|
|
|
|
|
|
|
https://fonts.googleapis.com/css\?family=(.*?)(['"]) |
32
|
2
|
|
|
|
|
15
|
}{$c->url_for( 'google-proxy-css', file => $1 ) . $2;}xge; |
33
|
|
|
|
|
|
|
} |
34
|
2
|
|
|
|
|
42
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$app->routes->get( '/google/css/*file' )->to( cb => sub { |
37
|
2
|
|
|
2
|
|
26551
|
my $c = shift; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
12
|
$c->render_later; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
47
|
my $url = sprintf $CSS_URL_FORMAT, $c->param('file'); |
42
|
2
|
|
33
|
|
|
82
|
my $ua_string = $USER_AGENT_STRING || $c->tx->req->headers->user_agent; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$c->ua->get( $url => { 'User-Agent' => $ua_string } => sub { |
45
|
2
|
|
|
|
|
6397
|
my ($ua, $tx) = @_; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
13
|
my $body = $tx->res->body; |
48
|
2
|
|
|
|
|
76
|
$body =~ s{ |
49
|
|
|
|
|
|
|
https?://fonts\.gstatic\.com/s/(.*?)\) \s* format\('(.*?)'\) |
50
|
8
|
|
|
|
|
9124
|
}{$c->url_for('google-proxy-font', file => $1, fformat => $2) . ") format('$2')"}xmseg; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
1613
|
return $c->render( data => $body, format => 'css' ); |
53
|
2
|
|
|
|
|
24
|
}); |
54
|
2
|
|
|
|
|
44
|
})->name( 'google-proxy-css' ); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$app->routes->get( '/google/font/:fformat/*file' )->to( cb => sub { |
57
|
1
|
|
|
1
|
|
12702
|
my $c = shift; |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
6
|
$c->render_later; |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
32
|
my $url = sprintf $FONT_URL_FORMAT, $c->param('file'); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$c->ua->get( $url => sub { |
64
|
1
|
|
|
|
|
4270
|
my ($ua, $tx) = @_; |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
22
|
my $body = $tx->res->body; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
29
|
return $c->render( data => $body, format => $c->param('fformat') ); |
69
|
1
|
|
|
|
|
64
|
}); |
70
|
2
|
|
|
|
|
1189
|
})->name( 'google-proxy-font' ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |