[Dancer-users] Conditional HTTP proposal

Oleg A. Mamontov oleg at mamontov.net
Thu Feb 10 18:36:40 CET 2011


Hello.

I create small patch to enhance static files serving functionality.
During development process its important to refresh pages quickly.
Conditional HTTP requests is the good choise. 
Seems like working, but needs more testing :)


--- Dancer/Renderer.pm.orig 2011-02-10 19:31:07.000000000 +0300
+++ Dancer/Renderer.pm  2011-02-10 20:28:46.000000000 +0300
@@ -3,7 +3,9 @@
 use strict;
 use warnings;
 use Carp;
+use File::stat;
 use HTTP::Headers;
+use POSIX qw( strftime );
 use Dancer::Route;
 use Dancer::HTTP;
 use Dancer::Cookie;
@@ -164,10 +166,16 @@
         my $fh = open_file('<', $static_file);
         binmode $fh;

-        Dancer::Response->status($status);
+        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
+        # Wed, 15 Nov 1995 04:58:08 GMT
+        my $lmod = strftime '%a, %d %b %Y %H:%M:%S GMT', gmtime(stat($fh)->mtime);
+        my $imod = Dancer::SharedData->request->header('If-Modified-Since') || '';
+
+        Dancer::Response->status( $lmod eq $imod ? 304 : $status );
         Dancer::Response->content_type(
             get_mime_type($static_file));
-        Dancer::Response::set_current_content($fh);
+        Dancer::Response::set_current_content($fh) unless $lmod eq $imod;
+        Dancer::Response->header('Last-Modified' => $lmod );
         return Dancer::Response->current;
     }
     return;


-- 
Cheers,
Oleg A. Mamontov

mailto:  oleg at mamontov.net

jabber:  lonerr at charla.mamontov.net
icq uin: 79-521-617
cell:    +7-903-798-1352



More information about the Dancer-users mailing list