line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Application::Plugin::TT::LastModified; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# Required inclusions. |
5
|
|
|
|
|
|
|
############################################################################### |
6
|
3
|
|
|
3
|
|
384557
|
use strict; |
|
3
|
|
|
|
|
51
|
|
|
3
|
|
|
|
|
105
|
|
7
|
3
|
|
|
3
|
|
20
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
107
|
|
8
|
3
|
|
|
3
|
|
1433
|
use CGI::Util qw(expires); |
|
3
|
|
|
|
|
8581
|
|
|
3
|
|
|
|
|
226
|
|
9
|
3
|
|
|
3
|
|
25
|
use List::Util qw(max); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
317
|
|
10
|
3
|
|
|
3
|
|
1538
|
use namespace::clean; |
|
3
|
|
|
|
|
48670
|
|
|
3
|
|
|
|
|
20
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
############################################################################### |
13
|
|
|
|
|
|
|
# Version numbering. |
14
|
|
|
|
|
|
|
############################################################################### |
15
|
|
|
|
|
|
|
our $VERSION = '1.03'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
# Export our methods. |
19
|
|
|
|
|
|
|
############################################################################### |
20
|
|
|
|
|
|
|
our @EXPORT = qw( |
21
|
|
|
|
|
|
|
tt_last_modified |
22
|
|
|
|
|
|
|
tt_set_last_modified_header |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
############################################################################### |
26
|
|
|
|
|
|
|
# Subroutine: import() |
27
|
|
|
|
|
|
|
############################################################################### |
28
|
|
|
|
|
|
|
# Custom import routine, which allows for 'tt_set_last_modified_header()' to be |
29
|
|
|
|
|
|
|
# auto-added in as a TT post process hook. |
30
|
|
|
|
|
|
|
############################################################################### |
31
|
|
|
|
|
|
|
sub import { |
32
|
3
|
|
|
3
|
|
22
|
my $caller = scalar caller; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# manually export our symbols |
35
|
3
|
|
|
|
|
9
|
foreach my $sym (@EXPORT) { |
36
|
3
|
|
|
3
|
|
874
|
no strict 'refs'; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
1241
|
|
37
|
6
|
|
|
|
|
13
|
*{"${caller}::$sym"} = \&{$sym}; |
|
6
|
|
|
|
|
37
|
|
|
6
|
|
|
|
|
16
|
|
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# sanity check caller package, and set up auto-header functionality |
41
|
3
|
50
|
66
|
|
|
202
|
if (not UNIVERSAL::isa($caller, 'CGI::Application')) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
warn "Calling package is not a CGI::Application module.\n"; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif (not UNIVERSAL::can($caller, 'tt_obj')) { |
45
|
0
|
|
|
|
|
0
|
warn "Calling package hasn't imported CGI::Application::Plugin::TT.\n"; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
elsif ($_[1] and ($_[1] eq ':auto')) { |
48
|
1
|
|
|
|
|
10
|
$caller->add_callback( tt_post_process => \&tt_set_last_modified_header ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
############################################################################### |
53
|
|
|
|
|
|
|
# Subroutine: tt_last_modified() |
54
|
|
|
|
|
|
|
############################################################################### |
55
|
|
|
|
|
|
|
# Returns the most recent modification time for any component of the most |
56
|
|
|
|
|
|
|
# recently processed template (via 'tt_process()'). Time is returned back to |
57
|
|
|
|
|
|
|
# the caller as "the number of seconds since the epoch". |
58
|
|
|
|
|
|
|
############################################################################### |
59
|
|
|
|
|
|
|
sub tt_last_modified { |
60
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
61
|
2
|
|
|
|
|
10
|
my $ctx = $self->tt_obj->context(); |
62
|
2
|
|
|
|
|
73
|
my $mtime = 0; |
63
|
2
|
|
|
|
|
6
|
foreach my $provider (@{$ctx->{'LOAD_TEMPLATES'}}) { |
|
2
|
|
|
|
|
9
|
|
64
|
2
|
|
|
|
|
5
|
foreach my $file (keys %{$provider->{'LOOKUP'}}) { |
|
2
|
|
|
|
|
11
|
|
65
|
6
|
|
|
|
|
20
|
my $c_mtime = $provider->{'LOOKUP'}{$file}[3]; |
66
|
6
|
|
|
|
|
29
|
$mtime = max( $mtime, $c_mtime ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
2
|
|
|
|
|
5
|
return $mtime; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
############################################################################### |
73
|
|
|
|
|
|
|
# Subroutine: tt_set_last_modified_header() |
74
|
|
|
|
|
|
|
############################################################################### |
75
|
|
|
|
|
|
|
# Sets a "Last-Modified" header in the HTTP response, equivalent to the last |
76
|
|
|
|
|
|
|
# modification time of the template components as returned by |
77
|
|
|
|
|
|
|
# 'tt_last_modified()'. |
78
|
|
|
|
|
|
|
############################################################################### |
79
|
|
|
|
|
|
|
sub tt_set_last_modified_header { |
80
|
2
|
|
|
2
|
1
|
194188
|
my $self = shift; |
81
|
2
|
|
|
|
|
24
|
my $mtime = $self->tt_last_modified(); |
82
|
2
|
50
|
|
|
|
10
|
if ($mtime) { |
83
|
2
|
|
|
|
|
13
|
my $lastmod = expires( $mtime, 'http' ); |
84
|
2
|
|
|
|
|
219
|
$self->header_add( '-last-modified' => $lastmod ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 NAME |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
CGI::Application::Plugin::TT::LastModified - Set "Last-Modified" header based on TT template |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 SYNOPSIS |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# when you want to set the "Last-Modified" header manually |
97
|
|
|
|
|
|
|
use base qw(CGI::Application); |
98
|
|
|
|
|
|
|
use CGI::Application::Plugin::TT; |
99
|
|
|
|
|
|
|
use CGI::Application::Plugin::TT::LastModified; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub my_runmode { |
102
|
|
|
|
|
|
|
my $self = shift; |
103
|
|
|
|
|
|
|
my %params = ( |
104
|
|
|
|
|
|
|
# ... |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
my $html = $self->tt_process( 'template.html', \%params ); |
107
|
|
|
|
|
|
|
$self->tt_set_last_modified_header(); |
108
|
|
|
|
|
|
|
return $html; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# when you want the "Last-Modified" header set automatically |
112
|
|
|
|
|
|
|
use base qw(CGI::Application); |
113
|
|
|
|
|
|
|
use CGI::Application::Plugin::TT; |
114
|
|
|
|
|
|
|
use CGI::Application::Plugin::TT::LastModified qw(:auto); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub my_runmode { |
117
|
|
|
|
|
|
|
my $self = shift; |
118
|
|
|
|
|
|
|
my %params = ( |
119
|
|
|
|
|
|
|
# ... |
120
|
|
|
|
|
|
|
); |
121
|
|
|
|
|
|
|
return $self->tt_process( 'template.html', \%params ); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
C adds support to |
127
|
|
|
|
|
|
|
C for setting a "Last-Modified" header based on the most |
128
|
|
|
|
|
|
|
recent modification time of I of the components of a template that was |
129
|
|
|
|
|
|
|
processed with TT. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Normally you'll want to call it manually, on as "as needed" basis; if you're |
132
|
|
|
|
|
|
|
processing templates with TT you're most likely dealing with dynamic content |
133
|
|
|
|
|
|
|
(in which case you probably don't even want a "Last-Modified" header). The odd |
134
|
|
|
|
|
|
|
time you'll want to set a "Last-Modified" header, though, this plugin helps |
135
|
|
|
|
|
|
|
make that easier. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
B you have a desire to have the "Last-Modified" header set automatically |
138
|
|
|
|
|
|
|
for you, though, C does have an |
139
|
|
|
|
|
|
|
C<:auto> import tag which auto-registers L as a |
140
|
|
|
|
|
|
|
"tt_post_process" hook for you. If you've got an app that just processes |
141
|
|
|
|
|
|
|
static TT pages and generates output, this will be useful for you. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 METHODS |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=over |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item import() |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Custom import routine, which allows for C to |
150
|
|
|
|
|
|
|
be auto-added in as a TT post process hook. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item tt_last_modified() |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Returns the most recent modification time for any component of the most |
155
|
|
|
|
|
|
|
recently processed template (via C). Time is returned back to |
156
|
|
|
|
|
|
|
the caller as "the number of seconds since the epoch". |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item tt_set_last_modified_header() |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Sets a "Last-Modified" header in the HTTP response, equivalent to the last |
161
|
|
|
|
|
|
|
modification time of the template components as returned by |
162
|
|
|
|
|
|
|
C. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 AUTHOR |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Graham TerMarsch (cpan@howlingfrog.com) |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 COPYRIGHT |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Copyright (C) 2007, Graham TerMarsch. All Rights Reserved. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the same |
175
|
|
|
|
|
|
|
terms as Perl itself. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 SEE ALSO |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=over |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item L |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item L |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item L |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=back |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |