line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
829
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
22
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
23
|
1
|
|
|
1
|
|
406
|
use Plack::Builder; |
|
1
|
|
|
|
|
7268
|
|
|
1
|
|
|
|
|
360
|
|
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
|
|
|
|
|
|
|
sub wrap{ |
44
|
0
|
|
|
0
|
1
|
|
my($class, $app) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
builder{ |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# ReverseProxy fixes scheme/host/port |
49
|
0
|
|
|
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
|
0
|
|
|
|
|
enable_if{$ENV{'ENV'} && $ENV{'ENV'} eq "production"} "Plack::Middleware::MCCS", |
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
path => qr{^/(images|js|style)/}, |
58
|
|
|
|
|
|
|
root => './htdocs' |
59
|
|
|
|
|
|
|
; |
60
|
0
|
0
|
|
|
|
|
enable_if{!$ENV{'ENV'} || $ENV{'ENV'} ne "production"} "Static", |
|
0
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
path => qr{^/(images|js|style)/}, |
62
|
|
|
|
|
|
|
root => './htdocs'; |
63
|
|
|
|
|
|
|
; |
64
|
|
|
|
|
|
|
enable sub { |
65
|
0
|
|
|
|
|
|
my $app = shift; |
66
|
|
|
|
|
|
|
sub { |
67
|
0
|
|
|
|
|
|
my $env = shift; |
68
|
0
|
|
0
|
|
|
|
my $ua = $env->{HTTP_USER_AGENT} || ''; |
69
|
|
|
|
|
|
|
# Netscape has some problem |
70
|
0
|
0
|
|
|
|
|
$env->{"psgix.compress-only-text/html"} = 1 if $ua =~ m!^Mozilla/4!; |
71
|
|
|
|
|
|
|
# Netscape 4.06-4.08 have some more problems |
72
|
0
|
0
|
|
|
|
|
$env->{"psgix.no-compress"} = 1 if $ua =~ m!^Mozilla/4\.0[678]!; |
73
|
|
|
|
|
|
|
# MSIE (7|8) masquerades as Netscape, but it is fine |
74
|
0
|
0
|
|
|
|
|
if ( $ua =~ m!\bMSIE (?:7|8)! ) { |
75
|
0
|
|
|
|
|
|
$env->{"psgix.no-compress"} = 0; |
76
|
0
|
|
|
|
|
|
$env->{"psgix.compress-only-text/html"} = 0; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
$app->($env); |
79
|
|
|
|
|
|
|
} |
80
|
0
|
|
|
|
|
|
}; |
|
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 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Development questions, bug reports, and patches are welcome to the above address |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 See Also |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
L L L |
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
|
|
|
|
|
|
|
return 1; |