| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->request( { map { $_ => $_ } @{ $self->request } } ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless ($self->response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | 0 | $self->response( { map { $_ => $_ } @{ $self->response } } ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless ($self->max_age) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$req->method // ""
$req->method
Truth table0%0/2
A
A= $req->method
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless ($method eq "GET") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
is_coderef($base) or substr($path_query, 0, 1) eq "/"
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 "/"
| |||||||||||||||||||||||||||||
| 101 | 0 | $url = undef; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $url //= [ HTTP_BAD_REQUEST, [], [] ]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$url //= [400, [], []]
MC/DC0%0/1
$url //= [400, [], []]
$url
Truth table0%0/2
A
A= $url
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return $url if (is_plain_arrayref($url)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $cache = $self->cache; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $data = $cache ? $cache->get($path_query) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC0%0/1
$req_head->header($field) // (next)
$req_head->header($field)
Truth table0%0/2
A
A= $req_head->header($field)
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $send = $self->request->{$field} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $send = $field if $send eq "1"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | while ($mech->infinite_scroll(1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | last if $count-- < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC0%0/1
$head->header($field) // (next)
$head->header($field)
Truth table0%0/2
A
A= $head->header($field)
| |||||||||||||||||||||||||||||
| 141 | 0 | $value =~ tr/\n/ /; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $send = $self->response->{$field} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $send = $field if $send eq "1"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | $h->set( $send => $value ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ($res->code == HTTP_OK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | $data = [ HTTP_OK, $h->headers, [$body] ]; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($cache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | my $age; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | if (my $value = $head->header("Cache-Control")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ($age) = $value =~ /(?:s\-)?max-age=([0-9]+)\b/; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($age && $age > $self->max_age) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$age and $age > $self->max_age
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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$age // $self->max_age
$age
$self->max_age
Truth table0%0/3
A // B
A= $ageB= $self->max_age
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||