summaryrefslogtreecommitdiff
path: root/lib/Plack
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2011-01-21 11:18:15 +0100
committerfranck cuny <franck@lumberjaph.net>2011-01-21 11:18:15 +0100
commit85173eb97566c5531f047098d0b0b6209b51c25d (patch)
tree1eb1158c2906a80005eb1e5f8d39e0d6338e77b7 /lib/Plack
parentChecking in changes prior to tagging of version 0.01. Changelog diff is: (diff)
downloadplack-middleware-etag-85173eb97566c5531f047098d0b0b6209b51c25d.tar.gz
add cache-control and tests
Diffstat (limited to 'lib/Plack')
-rw-r--r--lib/Plack/Middleware/ETag.pm32
1 files changed, 30 insertions, 2 deletions
diff --git a/lib/Plack/Middleware/ETag.pm b/lib/Plack/Middleware/ETag.pm
index 90ae1e1..6adc512 100644
--- a/lib/Plack/Middleware/ETag.pm
+++ b/lib/Plack/Middleware/ETag.pm
@@ -4,7 +4,7 @@ use strict;
use warnings;
use Digest::SHA;
use Plack::Util;
-use Plack::Util::Accessor qw( file_etag );
+use Plack::Util::Accessor qw( file_etag cache_control);
our $VERSION = '0.01';
@@ -19,9 +19,11 @@ sub call {
sub {
my $res = shift;
my $headers = $res->[1];
- return if ( !defined $res->[2] );#|| ref $res->[2] ne 'ARRAY' );
+ return if ( !defined $res->[2] );
return if ( Plack::Util::header_exists( $headers, 'ETag' ) );
+
my $etag;
+
if ( Plack::Util::is_real_fh( $res->[2] ) ) {
my $file_attr = $self->file_etag || [qw/inode mtime size/];
@@ -49,11 +51,25 @@ sub call {
$etag = $sha->hexdigest;
}
Plack::Util::header_set( $headers, 'ETag', $etag );
+ $self->_set_cache_control($headers);
return;
}
);
}
+sub _set_cache_control {
+ my ( $self, $headers ) = @_;
+ return unless $self->cache_control;
+
+ if ( ref $self->cache_control && ref $self->cache_control eq 'ARRAY' ) {
+ Plack::Util::header_set( $headers, 'Cache-Control',
+ join( ', ', @{ $self->cache_control } ) );
+ }
+ else {
+ Plack::Util::header_set( $headers, 'Cache-Control', 'must-revalidate' );
+ }
+}
+
1;
__END__
@@ -90,6 +106,18 @@ If the content is a file handle, the ETag will be set using the inode, modified
enable "Plack::Middleware::ETag", file_etag => [qw/size/];
+=item cache_control
+
+It's possible to add 'Cache-Control' header.
+
+ enable "Plack::Middleware::ETag", cache_control => 1;
+
+Will add "Cache-Control: must-revalidate" to the headers.
+
+ enable "Plack::Middleware::ETag", cache_control => [ 'must-revalidate', 'max-age=3600' ];
+
+Will add "Cache-Control: must-revalidate, max-age=3600" to the headers.
+
=back
=head1 AUTHOR