Plack-App-Prerender
view release on metacpan or search on metacpan
lib/Plack/App/Prerender.pm view on Meta::CPAN
package Plack::App::Prerender;
# ABSTRACT: a simple prerendering proxy for Plack
use v5.10.1;
use strict;
use warnings;
our $VERSION = 'v0.2.0';
use parent qw/ Plack::Component /;
use Encode qw/ encode /;
use HTTP::Headers;
use HTTP::Request;
use HTTP::Status qw/ :constants /;
use Plack::Request;
use Plack::Util;
use Plack::Util::Accessor qw/ mech rewrite cache max_age request response wait /;
use Ref::Util qw/ is_coderef is_plain_arrayref /;
use Time::Seconds qw/ ONE_HOUR /;
use WWW::Mechanize::Chrome;
# RECOMMEND PREREQ: CHI
# RECOMMEND PREREQ: Log::Log4perl
# RECOMMEND PREREQ: Ref::Util::XS
sub prepare_app {
my ($self) = @_;
unless ($self->mech) {
my $mech = WWW::Mechanize::Chrome->new(
headless => 1,
separate_session => 1,
);
$self->mech($mech);
}
unless ($self->request) {
$self->request(
{
'User-Agent' => 'X-Forwarded-User-Agent',
( map { $_ => $_ } qw/
X-Forwarded-For
X-Forwarded-Host
X-Forwarded-Port
X-Forwarded-Proto
/
),
}
);
}
if (is_plain_arrayref($self->request)) {
$self->request( { map { $_ => $_ } @{ $self->request } } );
}
unless ($self->response) {
$self->response(
{
( map { $_ => $_ } qw/
Content-Type
Expires
Last-Modified
/
),
}
);
}
if (is_plain_arrayref($self->response)) {
$self->response( { map { $_ => $_ } @{ $self->response } } );
}
unless ($self->max_age) {
$self->max_age( ONE_HOUR );
}
}
sub call {
my ($self, $env) = @_;
my $req = Plack::Request->new($env);
my $method = $req->method // '';
unless ($method eq "GET") {
return [ HTTP_METHOD_NOT_ALLOWED, [], [] ];
}
my $path_query = $env->{REQUEST_URI};
my $base = $self->rewrite;
my $url = is_coderef($base)
? $base->($path_query, $env)
: $base . $path_query;
$url //= [ HTTP_BAD_REQUEST, [], [] ];
return $url if (is_plain_arrayref($url));
my $cache = $self->cache;
my $data = $cache ? $cache->get($path_query) : undef;
if (defined $data) {
return $data;
lib/Plack/App/Prerender.pm view on Meta::CPAN
=head2 rewrite
This can either be a base URL prefix string, or a code reference that
takes the PSGI C<REQUEST_URI> and environment hash as arguments, and
returns a full URL to pass to L</mech>.
If the code reference returns C<undef>, then the request will abort
with an HTTP 400.
If the code reference returns an array reference, then it assumes the
request is a Plack response and simply returns it.
This can be used for simple request validation. For example,
use Robots::Validate v0.2.0;
sub validator {
my ($path, $env) = @_;
state $rv = Robots::Validate->new();
unless ( $rv->validate( $env ) ) {
if (my $logger = $env->{'psgix.logger'}) {
$logger->( { level => 'warn', message => 'not a bot!' } );
}
return [ 403, [], [] ];
}
...
}
=head2 cache
This is the cache handling interface. See L<CHI>.
If no cache is specified (v0.2.0), then the result will not be cached.
=head2 max_age
This is the maximum time (in seconds) to cache content. If the page
returns a C<Cache-Control> header with a C<max-age>, then that will be
used instead.
=head2 request
This is a hash reference (since v0.2.0) of request headers to pass
through the proxy. The keys are the request header fieldss, and the
values are the headers that will be passed to the L</rewrite> URL.
Values of C<1> will be a synonym for the same header, and false values
will mean that the header is skipped.
An array reference can be used to simply pass through a list of
headers unchanged.
It will default to the following headers:
=over
=item C<X-Forwarded-For>
=item C<X-Forwarded-Host>
=item C<X-Forwarded-Port>
=item C<X-Forwarded-Proto>
=back
The C<User-Agent> is forwarded as C<X-Forwarded-User-Agent>.
=head2 response
This is a hash reference (since v0.2.0) of request headers to return
from the proxy. The keys are the response header fields, and the
values are the headers that will be returned from the proxy.
Values of C<1> will be a synonym for the same header, and false values
will mean that the header is skipped.
An array reference can be used to simply pass through a list of
headers unchanged.
It will default to the following headers:
=over
=item C<Content-Type>
=item C<Expires>
=item C<Last-Modified>
=back
=head2 wait
The number of seconds to wait for new content to be loaded.
=head1 LIMITATIONS
This does not support cache invalidation or screenshot rendering.
This only does the bare minimum necessary for proxying requests. You
may need additional middleware for reverse proxies, logging, or
security filtering.
=head1 SEE ALSO
L<Plack>
L<WWW::Mechanize::Chrome>
Rendertron L<https://github.com/GoogleChrome/rendertron>
=head1 SOURCE
The development version is on github at L<https://github.com/robrwo/perl-Plack-App-Prerender>
and may be cloned from L<git://github.com/robrwo/perl-Plack-App-Prerender.git>
( run in 2.387 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )