| 1 | package PSGI::Hector::Middleware; | ||||||||||||
| 2 | |||||||||||||
| 3 | =pod | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | PSGI::Hector::Middleware - Application middleware | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||
| 10 | |||||||||||||
| 11 | PSGI::Hector::Middleware->wrap($app); | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||
| 14 | |||||||||||||
| 15 | Wraps the application in additional middleware. | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 METHODS | ||||||||||||
| 18 | |||||||||||||
| 19 | =cut | ||||||||||||
| 20 | |||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | ▶ | use Plack::Builder; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | ######################################################### | ||||||||||||
| 25 | |||||||||||||
| 26 | =pod | ||||||||||||
| 27 | |||||||||||||
| 28 | =head2 wrap() | ||||||||||||
| 29 | |||||||||||||
| 30 | PSGI::Hector::Middleware->wrap($app) | ||||||||||||
| 31 | |||||||||||||
| 32 | Adds the following middleware to the application: | ||||||||||||
| 33 | |||||||||||||
| 34 | Serving static files from "images", "js", "style" which are located in the "htdocs" directory. These | ||||||||||||
| 35 | will be minified, cached and compressed in a production environment. | ||||||||||||
| 36 | |||||||||||||
| 37 | Non static files will only be compressed. | ||||||||||||
| 38 | |||||||||||||
| 39 | =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | ################################## | ||||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | sub wrap{ | ||||||||||
|
Subroutine
wrap: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 44 | 0 | my($class, $app) = @_; | |||||||||||
| 45 | |||||||||||||
| 46 | 0 | ▶ | builder{ | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 47 | |||||||||||||
| 48 | # ReverseProxy fixes scheme/host/port | ||||||||||||
| 49 | 0 | enable "ReverseProxy"; | |||||||||||
| 50 | |||||||||||||
| 51 | # ReverseProxyPath uses new headers | ||||||||||||
| 52 | # fixes SCRIPT_NAME and PATH_INFO | ||||||||||||
| 53 | 0 | enable "ReverseProxyPath"; | |||||||||||
| 54 | |||||||||||||
| 55 | #minify assets on production | ||||||||||||
| 56 | 0 | ▶ | enable_if{$ENV{'ENV'} && $ENV{'ENV'} eq "production"} "Plack::Middleware::MCCS", | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||
| 57 | path => qr{^/(images|js|style)/}, | ||||||||||||
| 58 | root => './htdocs' | ||||||||||||
| 59 | ; | ||||||||||||
| 60 | 0 | ▶ | enable_if{!$ENV{'ENV'} || $ENV{'ENV'} ne "production"} "Static", | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||
| 61 | path => qr{^/(images|js|style)/}, | ||||||||||||
| 62 | root => './htdocs'; | ||||||||||||
| 63 | ; | ||||||||||||
| 64 | 0 | ▶ | enable sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 65 | 0 | my $app = shift; | |||||||||||
| 66 | 0 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||
| 67 | 0 | my $env = shift; | |||||||||||
| 68 | 0 | ▶ | my $ua = $env->{HTTP_USER_AGENT} || ''; | ||||||||||
|
Condition0%0/2
$env->{'HTTP_USER_AGENT'} || ''
MC/DC0%0/1
$env->{'HTTP_USER_AGENT'} || ''
$env->{'HTTP_USER_AGENT'}
Truth table0%0/2
A
A= $env->{'HTTP_USER_AGENT'}
| |||||||||||||
| 69 | # Netscape has some problem | ||||||||||||
| 70 | 0 | ▶ | $env->{"psgix.compress-only-text/html"} = 1 if $ua =~ m!^Mozilla/4!; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | # Netscape 4.06-4.08 have some more problems | ||||||||||||
| 72 | 0 | ▶ | $env->{"psgix.no-compress"} = 1 if $ua =~ m!^Mozilla/4\.0[678]!; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | # MSIE (7|8) masquerades as Netscape, but it is fine | ||||||||||||
| 74 | 0 | ▶ | if ( $ua =~ m!\bMSIE (?:7|8)! ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 0 | $env->{"psgix.no-compress"} = 0; | |||||||||||
| 76 | 0 | $env->{"psgix.compress-only-text/html"} = 0; | |||||||||||
| 77 | } | ||||||||||||
| 78 | 0 | $app->($env); | |||||||||||
| 79 | } | ||||||||||||
| 80 | 0 | }; | |||||||||||
| 81 | |||||||||||||
| 82 | 0 | enable "Deflater", | |||||||||||
| 83 | content_type => ['text/css','text/html','text/javascript','application/javascript', 'application/json'], | ||||||||||||
| 84 | vary_user_agent => 1; | ||||||||||||
| 85 | 0 | $app; | |||||||||||
| 86 | 0 | }; | |||||||||||
| 87 | } | ||||||||||||
| 88 | ########################################################### | ||||||||||||
| 89 | |||||||||||||
| 90 | =pod | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 Notes | ||||||||||||
| 93 | |||||||||||||
| 94 | =head1 Author | ||||||||||||
| 95 | |||||||||||||
| 96 | MacGyveR <dumb@cpan.org> | ||||||||||||
| 97 | |||||||||||||
| 98 | Development questions, bug reports, and patches are welcome to the above address | ||||||||||||
| 99 | |||||||||||||
| 100 | =head1 See Also | ||||||||||||
| 101 | |||||||||||||
| 102 | L<Plack::Middleware::Static::Minifier> L<Plack::Middleware::Static> L<Plack::Middleware::Deflater> | ||||||||||||
| 103 | |||||||||||||
| 104 | =head1 Copyright | ||||||||||||
| 105 | |||||||||||||
| 106 | Copyright (c) 2017 MacGyveR. All rights reserved. | ||||||||||||
| 107 | |||||||||||||
| 108 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||
| 109 | |||||||||||||
| 110 | =cut | ||||||||||||
| 111 | ################################## | ||||||||||||
| 112 | 1 | return 1; | |||||||||||