blib/lib/Plack/App/Prerender.pm

filter: Statementstmt 38.0% 43 / 113 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 87.5% 14 / 16 Podpod 100% 2 / 2 totaltotal 28.9% 59 / 204 CC 33 SCAR 60.7
CC
33
Cov
24%
call
756.0
prepare_app
56.0
BEGIN
1.0
CRAP 433.7
↑ Summary
1 package Plack::App::Prerender;
2  
3 # ABSTRACT: a simple prerendering proxy for Plack
4  
5 1 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v0.3.0';
10  
11 1 ▶use parent qw/ Plack::Component /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Encode qw/ encode /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use HTTP::Status qw/ :constants /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Plack::Util::Accessor qw/ mech rewrite cache max_age request response wait /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Ref::Util qw/ is_coderef is_plain_arrayref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Time::Seconds qw/ ONE_HOUR /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use WWW::Mechanize::Chrome;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # RECOMMEND PREREQ: CHI
25 # RECOMMEND PREREQ: Log::Log4perl
26 # RECOMMEND PREREQ: Ref::Util::XS
27  
28  
29 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=7 SCAR=40.3
Pod documented
30 0 my ($self) = @_;
31  
32 0 ▶ unless ($self->mech) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->mech) 0 0 0
33  
34 0 my $mech = WWW::Mechanize::Chrome->new(
35 headless => 1,
36 separate_session => 1,
37 );
38  
39 0 $self->mech($mech);
40  
41 }
42  
43 0 ▶ unless ($self->request) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->request) 0 0 0
44 $self->request(
45 {
46 'User-Agent' => 'X-Forwarded-User-Agent',
47 0 ( map { $_ => $_ } qw/
48 X-Forwarded-For
49 X-Forwarded-Host
50 X-Forwarded-Port
51 X-Forwarded-Proto
52 /
53 ),
54 }
55 );
56 }
57 0 ▶ if (is_plain_arrayref($self->request)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($self->request)) 0 0 0
58 0 $self->request( { map { $_ => $_ } @{ $self->request } } );
59 }
60  
61  
62 0 ▶ unless ($self->response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->response) 0 0 0
63 $self->response(
64 {
65 0 ( map { $_ => $_ } qw/
66 Content-Type
67 Expires
68 Last-Modified
69 /
70 ),
71 }
72 );
73 }
74 0 ▶ if (is_plain_arrayref($self->response)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($self->response)) 0 0 0
75 0 $self->response( { map { $_ => $_ } @{ $self->response } } );
76 }
77  
78 0 ▶ unless ($self->max_age) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->max_age) 0 0 0
79 0 $self->max_age( ONE_HOUR );
80 }
81 }
82  
83 0 ▶sub call {
Subroutine call: not called CC=27 SCAR=66.3
Pod documented
84 0 my ($self, $env) = @_;
85  
86 0 my $req = Plack::Request->new($env);
87  
88 0 ▶ my $method = $req->method // '';
Condition0%0/2
$req->method // ""
l!l
00
MC/DC0%0/1
$req->method // ""
$req->method
Truth table0%0/2
A
A= $req->method
Aresult
11
00
89 0 ▶ unless ($method eq "GET") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method eq "GET") 0 0 0
90 0 return [ HTTP_METHOD_NOT_ALLOWED, [], [] ];
91 }
92  
93 0 my $path_query = $env->{REQUEST_URI};
94  
95 0 my $base = $self->rewrite;
96 0 ▶ my $url = is_coderef($base)
Branch: 1 branch
BranchTrueFalseTotal
is_coderef($base) ? : 0 0 0
97 ? $base->($path_query, $env)
98 : $base . $path_query;
99  
100 0 ▶ unless ( is_coderef($base) || substr($path_query, 0, 1) eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_coderef($base) or substr($path_query, 0, 1) eq "/") 0 0 0
Condition0%0/3
is_coderef($base) or substr($path_query, 0, 1) eq "/"
l!l&&r!l&&!r
000
MC/DC0%0/2
is_coderef($base) or substr($path_query, 0, 1) eq "/"
is_coderef($base) substr($path_query, 0, 1) eq "/"
Truth table0%0/3
A or B
A= is_coderef($base)B= substr($path_query, 0, 1) eq "/"
ABresult
1X1
011
000
101 0 $url = undef;
102 }
103  
104 0 ▶ $url //= [ HTTP_BAD_REQUEST, [], [] ];
Condition0%0/2
$url //= [400, [], []]
l!l
00
MC/DC0%0/1
$url //= [400, [], []]
$url
Truth table0%0/2
A
A= $url
Aresult
11
00
105 0 ▶ return $url if (is_plain_arrayref($url));
Branch: 1 branch
BranchTrueFalseTotal
if is_plain_arrayref($url) 0 0 0
106  
107 0 my $cache = $self->cache;
108 0 ▶ my $data = $cache ? $cache->get($path_query) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cache ? : 0 0 0
109 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 0 0 0
110  
111 0 return $data;
112  
113 }
114 else {
115  
116 0 my $mech = $self->mech;
117 0 $mech->reset_headers;
118  
119 0 my $req_head = $req->headers;
120 0 for my $field (keys %{ $self->request }) {
121 0 ▶ my $value = $req_head->header($field) // next;
Condition0%0/2
$req_head->header($field) // (next)
l!l
00
MC/DC0%0/1
$req_head->header($field) // (next)
$req_head->header($field)
Truth table0%0/2
A
A= $req_head->header($field)
Aresult
11
00
122 0 ▶ my $send = $self->request->{$field} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $send = $self->request->{$field} or next 0 0 0
123 0 ▶ $send = $field if $send eq "1";
Branch: 1 branch
BranchTrueFalseTotal
if $send eq 1 0 0 0
124 0 $mech->add_header( $send => $value );
125 }
126  
127 0 my $res = $mech->get( $url );
128  
129 0 ▶ if (my $count = $self->wait) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $count = $self->wait) 0 0 0
130 0 ▶ while ($mech->infinite_scroll(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->infinite_scroll(1)) 0 0 0
131 0 ▶ last if $count-- < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count-- < 0 0 0 0
132 }
133 }
134  
135 0 my $body = encode("UTF-8", $mech->content);
136  
137 0 my $head = $res->headers;
138 0 my $h = Plack::Util::headers([ 'X-Renderer' => __PACKAGE__ ]);
139 0 for my $field (keys %{ $self->response }) {
140 0 ▶ my $value = $head->header($field) // next;
Condition0%0/2
$head->header($field) // (next)
l!l
00
MC/DC0%0/1
$head->header($field) // (next)
$head->header($field)
Truth table0%0/2
A
A= $head->header($field)
Aresult
11
00
141 0 $value =~ tr/\n/ /;
142 0 ▶ my $send = $self->response->{$field} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $send = $self->response->{$field} or next 0 0 0
143 0 ▶ $send = $field if $send eq "1";
Branch: 1 branch
BranchTrueFalseTotal
if $send eq 1 0 0 0
144 0 $h->set( $send => $value );
145 }
146  
147 0 ▶ if ($res->code == HTTP_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) { } 0 0 0
148  
149 0 $data = [ HTTP_OK, $h->headers, [$body] ];
150  
151 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
152 0 my $age;
153 0 ▶ if (my $value = $head->header("Cache-Control")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $head->header("Cache-Control")) 0 0 0
154 0 ($age) = $value =~ /(?:s\-)?max-age=([0-9]+)\b/;
155 0 ▶ if ($age && $age > $self->max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age and $age > $self->max_age) 0 0 0
Condition0%0/3
$age and $age > $self->max_age
!ll&&!rl&&r
000
MC/DC0%0/2
$age and $age > $self->max_age
$age $age > $self->max_age
Truth table0%0/3
A and B
A= $ageB= $age > $self->max_age
ABresult
0X0
100
111
156 0 $age = $self->max_age;
157 }
158 }
159  
160 0 ▶ $cache->set( $path_query, $data, $age // $self->max_age );
Condition0%0/3
$age // $self->max_age
l!l&&r!l&&!r
000
MC/DC0%0/2
$age // $self->max_age
$age $self->max_age
Truth table0%0/3
A // B
A= $ageB= $self->max_age
ABresult
1X1
011
000
161 }
162  
163 0 return $data;
164  
165 }
166 else {
167  
168 0 return [ $res->code, $h->headers, [$body] ];
169  
170 }
171 }
172  
173 }
174  
175  
176 1 1;
177  
178 __END__
↑ Summary