line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::JavaScript::Ectype; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23382
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
905
|
use parent qw/Plack::Middleware/; |
|
1
|
|
|
|
|
306
|
|
|
1
|
|
|
|
|
6
|
|
6
|
|
|
|
|
|
|
use HTTP::Date; |
7
|
|
|
|
|
|
|
use Plack::Util; |
8
|
|
|
|
|
|
|
use Plack::Util::Accessor qw/prefix root minify/; |
9
|
|
|
|
|
|
|
use Carp; |
10
|
|
|
|
|
|
|
use JavaScript::Ectype::Loader; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = q(0.0.1); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub call{ |
15
|
|
|
|
|
|
|
my ($self,$env) = @_; |
16
|
|
|
|
|
|
|
return $self->_handler( $env ) || $self->app->($env); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub expire_time{ |
20
|
|
|
|
|
|
|
my $self = shift; |
21
|
|
|
|
|
|
|
return $self->{expire_time} || 60 * 60 * 24 * 14; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _handler{ |
25
|
|
|
|
|
|
|
my ($self,$env) = @_; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
return unless defined $self->prefix; |
28
|
|
|
|
|
|
|
return unless defined $self->root; |
29
|
|
|
|
|
|
|
return unless my $fqn = $self->_get_fqn( $env ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $jel = JavaScript::Ectype::Loader->new( |
33
|
|
|
|
|
|
|
path => $self->root, |
34
|
|
|
|
|
|
|
target => $fqn, |
35
|
|
|
|
|
|
|
minify => int($self->minify) |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
my $ims = $self->_get_modified_since($env) || 0; |
38
|
|
|
|
|
|
|
return $self->_not_found() unless -e $jel->file_path; |
39
|
|
|
|
|
|
|
return $self->_not_modified() unless $jel->is_modified_from($ims); |
40
|
|
|
|
|
|
|
$jel->load_content; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $content = $jel->get_content; |
43
|
|
|
|
|
|
|
my $last_modified = $jel->newest_mtime; |
44
|
|
|
|
|
|
|
return [ |
45
|
|
|
|
|
|
|
200, |
46
|
|
|
|
|
|
|
[ |
47
|
|
|
|
|
|
|
'Content-Type' => 'text/javascript', |
48
|
|
|
|
|
|
|
'Content-Length' => length $content, |
49
|
|
|
|
|
|
|
'Last-Modified' => HTTP::Date::time2str($last_modified), |
50
|
|
|
|
|
|
|
'Expires' => HTTP::Date::time2str( time() + $self->expire_time ) |
51
|
|
|
|
|
|
|
], |
52
|
|
|
|
|
|
|
[$content] |
53
|
|
|
|
|
|
|
]; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
sub _not_found { |
57
|
|
|
|
|
|
|
my ($self) = @_; |
58
|
|
|
|
|
|
|
return [404,[],[]]; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _not_modified { |
62
|
|
|
|
|
|
|
my ($self ) = @_; |
63
|
|
|
|
|
|
|
return [304,[Expires => HTTP::Date::time2str( time() + $self->expire_time )],[]] |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _get_fqn { |
67
|
|
|
|
|
|
|
my ( $self, $env ) = @_; |
68
|
|
|
|
|
|
|
my $uri = $env->{REQUEST_URI}; |
69
|
|
|
|
|
|
|
my $prefix = $self->prefix; |
70
|
|
|
|
|
|
|
if ( $uri =~ m/$prefix([a-zA-Z0-9._]+)/ ){ |
71
|
|
|
|
|
|
|
return $1; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
return; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _get_modified_since{ |
77
|
|
|
|
|
|
|
my ($self,$env) = @_; |
78
|
|
|
|
|
|
|
return HTTP::Date::str2time( $env->{HTTP_IF_MODIFIED_SINCE} ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
__END__ |