about summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-03-08 15:52:48 +0100
committerfranck cuny <franck@lumberjaph.net>2010-03-08 15:52:48 +0100
commitd42c9910b8eb1735fce4c3510bfe7db8e8796af5 (patch)
tree05773d5fcfff9d1b3b182695b85a5972041bed27
parentupdate internal tests (diff)
downloadplack-middleware-etag-d42c9910b8eb1735fce4c3510bfe7db8e8796af5.tar.gz
as miyagawa suggested, use file attributes to calculte ETag in case we have a file handle
-rw-r--r--lib/Plack/Middleware/ETag.pm51
-rw-r--r--t/01_basic.t20
2 files changed, 65 insertions, 6 deletions
diff --git a/lib/Plack/Middleware/ETag.pm b/lib/Plack/Middleware/ETag.pm
index 72b8c30..d70bc35 100644
--- a/lib/Plack/Middleware/ETag.pm
+++ b/lib/Plack/Middleware/ETag.pm
@@ -3,6 +3,8 @@ package Plack::Middleware::ETag;
 use strict;
 use warnings;
 use Digest::SHA;
+use Plack::Util;
+use Plack::Util::Accessor qw( file_etag );
 
 our $VERSION = '0.01';
 
@@ -18,11 +20,36 @@ sub call {
         $res,
         sub {
             my $res = shift;
-            return if ( !defined $res->[2] || ref $res->[2] ne 'ARRAY' );
+            return if ( !defined $res->[2] );#|| ref $res->[2] ne 'ARRAY' );
             return if ( Plack::Util::header_exists( $headers, 'ETag' ) );
-            my $sha = Digest::SHA->new;
-            $sha->add( @{ $res->[2] } );
-            Plack::Util::header_set( $headers, 'ETag', $sha->hexdigest );
+            my $etag;
+            if ( Plack::Util::is_real_fh( $res->[2] ) ) {
+
+                my $file_attr = $self->file_etag || [qw/inode mtime size/];
+                my @stats = stat $res->[2];
+                if ( $stats[9] == time - 1 ) {
+                    # if the file was modified less than one second before the request
+                    # it may be modified in a near future, so we return a weak etag
+                    $etag = "W/";
+                }
+                if ( grep {/inode/} @$file_attr ) {
+                    $etag .= (sprintf "%x", $stats[2]);
+                }
+                if ( grep {/mtime/} @$file_attr ) {
+                    $etag .= "-" if ($etag && $etag !~ /-$/);
+                    $etag .= ( sprintf "%x", $stats[9] );
+                }
+                if ( grep {/size/} @$file_attr ) {
+                    $etag .= "-" if ($etag && $etag !~ /-$/);
+                    $etag .= ( sprintf "%x", $stats[7] );
+                }
+            }
+            else {
+                my $sha = Digest::SHA->new;
+                $sha->add( @{ $res->[2] } );
+                $etag = $sha->hexdigest;
+            }
+            Plack::Util::header_set( $headers, 'ETag', $etag );
             return;
         }
     );
@@ -40,7 +67,7 @@ Plack::Middleware::ETag - Adds automatically an ETag header.
   use Plack::Builder;
 
   my $app = builder {
-    enable "Plack::Middleware::ETag";
+    enable "Plack::Middleware::ETag", file_etag => [qw/inode mtime size/];
     sub {['200', ['Content-Type' => 'text/html'}, ['hello world']]};
   };
 
@@ -50,10 +77,22 @@ Plack::Middleware::ETag adds automatically an ETag header. You may want to use i
 
   my $app = builder {
     enable "Plack::Middleware::ConditionalGET";
-    enable "Plack::Middleware::ETag";
+    enable "Plack::Middleware::ETag", path => "", file_etag => "inode";
     sub {['200', ['Content-Type' => 'text/html'}, ['hello world']]};
   };
 
+=head2 CONFIGURATION
+
+=over 4
+
+=item file_etag
+
+If the content is a file handle, the ETag will be set using the inode, modified time and the file size. You can select which attributes of the file will be used to set the ETag:
+
+    enable "Plack::Middleware::ETag", file_etag => [qw/size/];
+
+=back
+
 =head1 AUTHOR
 
 franck cuny E<lt>franck@lumberjaph.netE<gt>
diff --git a/t/01_basic.t b/t/01_basic.t
index 8564ece..62e1e6e 100644
--- a/t/01_basic.t
+++ b/t/01_basic.t
@@ -32,6 +32,12 @@ my $unmodified_handler = builder {
     sub { [ '200', [ 'Content-Type' => 'text/html' ], $content ] };
 };
 
+my $file_handler = builder {
+   enable "Plack::Middleware::ETag";
+   open my $fh, 'README';
+   sub {[200, ['Content-Type' => 'text/html', ], $fh]};
+};
+
 test_psgi
     app    => $handler,
     client => sub {
@@ -69,4 +75,18 @@ test_psgi
     }
 };
 
+test_psgi
+    app    => $file_handler,
+    client => sub {
+    my $cb = shift;
+    {
+        my $req = GET "http://localhost/";
+        my $res = $cb->($req);
+        ok $res->header('ETag');
+	warn $res->header('ETag');
+	is $res->code, 200;
+	ok $res->content;
+    }
+};
+
 done_testing;