use strict; use warnings; use Plack::Request; use Plack::Builder; use Data::Dumper; use File::MMagic; use DateTime; use DateTime::Format::HTTP; use Log::Dispatch; my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $age = 86400; open my $log, '>>', 'log.txt'; print $log Dumper $env; close $log; ## img 条件付きアクセス if ( $env->{HTTP_IF_MODIFIED_SINCE} ) { return [ 304, [], [], ]; } ## img 通常アクセス elsif ( $req->path =~ m!^/img/(.+?)/(.+?)/(.+?)/(.+?)/c\.gif! ) { my $expires = $1; my $lastmodified = $2; my $cachecontrol = $3; my $pragma = $4; my $file = 'c.gif'; my $type = File::MMagic->new->checktype_filename($file); open my $fh, '<', $file or die $!; binmode $fh; my $data = do { local $/; <$fh> }; close $fh; my $dt1 = DateTime->from_epoch( epoch => time - $age ); my $dt2 = DateTime->from_epoch( epoch => time + $age ); my $yesterday = DateTime::Format::HTTP->format_datetime($dt1); my $tomorrow = DateTime::Format::HTTP->format_datetime($dt2); my $now = DateTime::Format::HTTP->format_datetime(); my %header; $header{'Content-type'} = $type; $header{'Pragma'} = 'no-cache' if $pragma; $header{'Cache-Control'} = 'private' if $cachecontrol == 1; $header{'Cache-Control'} = "private, max-age=$age" if $cachecontrol == 2; $header{'Cache-Control'} = 'no-cache' if $cachecontrol == 3; $header{'Cache-Control'} = "no-cache, max-age=$age" if $cachecontrol == 4; $header{'Last-Modified'} = $yesterday if $lastmodified; $header{'Expires'} = $yesterday if $expires == 1; $header{'Expires'} = $now if $expires == 2; $header{'Expires'} = $yesterday if $expires == 3; return [ 200, [%header], [$data], ]; } ## index elsif ( $req->path =~ m!^/index\.html! ) { open my $fh, '<', 'local.html' or die $!; binmode $fh; my $data = do { local $/; <$fh> }; close $fh; my $res = $req->new_response(200); $res->content_type('text/html'); $res->body($data); $res->finalize; } ## ??? else { my $res = $req->new_response(200); $res->content_type('text/html'); $res->body( $req->path ); $res->finalize; } }; builder { my $logger = Log::Dispatch->new( outputs => [ [ 'File', min_level => 'debug', filename => 'access.log' ], ], ); enable "Plack::Middleware::AccessLog", logger => sub { $logger->log( level => 'debug', message => @_ ) }; $app; };