OSDN Git Service

add Last-Modified, If-Last-Modified and 304 Not modified response
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Fri, 25 Jun 2010 04:21:22 +0000 (13:21 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Fri, 25 Jun 2010 04:21:22 +0000 (13:21 +0900)
keitairc
lib/Keitairc/View.pm

index 9f807df..fc7afd4 100755 (executable)
--- a/keitairc
+++ b/keitairc
@@ -240,12 +240,12 @@ sub action_error {
 }
 
 ################################################################
-sub action_public{
+sub action_public {
        my $request = shift;
        my $uri = shift;        # such as '/favicon.ico'
        my $ci = new Keitairc::ClientInfo($request);
        my $view = new Keitairc::View($cf, $ci);
-       return $view->public($uri);
+       return $view->public($request, $uri);
 }
 
 ################################################################
index ce5a608..182453b 100644 (file)
@@ -8,6 +8,7 @@ package Keitairc::View;
 use HTML::Template;
 use HTTP::Response;
 use HTTP::Status;
+use HTTP::Date;
 use Keitairc::Config;
 use Keitairc::ClientInfo;
 use strict;
@@ -133,8 +134,8 @@ sub template{
 
 ################################################################
 sub public{
-       my $me = shift;
-       my $file = shift;
+       my ($me, $request, $file) = @_;
+
        my $mime_types = {
                html => 'text/html',
                css => 'text/css',
@@ -145,18 +146,32 @@ sub public{
                js => 'application/javascript',
        };
 
+       my ($ext) = ($file =~ /\.(.*)$/);
+       my $mime_type = $mime_types->{$ext} || 'text/plain';
+
+       my $mtime = (stat($me->public_path($file)))[9] || 0;
+       my $mod;
+       if ($mod = $request->header('If-Modified-Since')){
+               my $str_mod = str2time($mod);
+               if($str_mod
+                  && $str_mod <= $mtime) {
+                       my $response = HTTP::Response->new(304);
+                       $response->push_header('Content-type', $mime_type);
+                       $response->push_header('Last-Modified', time2str($mtime));
+                       return $response;
+               }
+       }
+
        if(open(my $fh, '<', $me->public_path($file))){
                local $/;
                undef $/;
                my $buf = join('', <$fh>);
                close($fh);
 
-               my ($ext) = ($file =~ /\.(.*)$/);
-               my $mime_type = $mime_types->{$ext} || 'text/plain';
-
                my $response = HTTP::Response->new(200);
                $response->push_header('Content-type', $mime_type);
-               $response->expires(time + $me->{Config}->cache_expire()) if (!$me->{Config}->debug() && $me->{Config}->cache_expire());
+               $response->push_header('Last-Modified', time2str($mtime));
+               #$response->expires(time + $me->{Config}->cache_expire()) if (!$me->{Config}->debug() && $me->{Config}->cache_expire());
                $response->content($buf);
                return $response;
        }