}
################################################################
-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);
}
################################################################
use HTML::Template;
use HTTP::Response;
use HTTP::Status;
+use HTTP::Date;
use Keitairc::Config;
use Keitairc::ClientInfo;
use strict;
################################################################
sub public{
- my $me = shift;
- my $file = shift;
+ my ($me, $request, $file) = @_;
+
my $mime_types = {
html => 'text/html',
css => 'text/css',
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;
}