« 2002年10月 | メイン | 2003年01月 »

2002年11月29日

日付け・時間処理関係のPerlモジュール

いままで独自で組んでいたのだが、使ってみた。

Date::Simple
return 'false' unless(Date::Simple->new('2002-10-10'));
ってな感じでチェック可能。

HTTPでつかうなら
HTTP::Date
UNIX時間へも変換可能。

文字列の変換
Date::Parse
$time = str2time($date);

時間の計算によく使うのが、
Date::Handler
時間の足し算なんかもできちゃいます。mod_perlではあやしいけど。

2002年11月01日

簡易Proxy

直リンで画像を呼び出すと相手に負荷がかかるってことで、それ用のProxy。
意味あるかは不明

参考は
Apache拡張ガイド 上下巻
http://search.cpan.org/author/GAAS/libwww-perl-5.65/
あたり。

package EasyProxy;

use strict;
use Apache::Constants qw(:common);
use Apache::URI;
use HTTP::Date;
use Digest::MD5 qw(md5_hex);
use LWP::UserAgent;
use Data::Dumper;

sub handler{
my $r = shift;

my $uri=$r->parsed_uri->query();
return 404 unless $uri;

my $digest = md5_hex($uri);
my $last_modified = (-f "/tmp/_$digest") ? (stat("/tmp/_$digest"))[9] : 0;

#もしブラウザがIf-Modified-Sinceを送ってきていて、
#If-Modified-Since < Last-Modified の場合 → 解凍して200
#If-Modified-Sinceが存在 && 4時間以内 -> 304

my $if_last_modified=($r->header_in("If-Modified-Since")) ? str2time($r->header_in("If-Modified-Since"),"+0900") : 0;

if($last_modified>$if_last_modified){
open(FH,"/tmp/_$digest");
my $VAR1;
my $dump = join("",<FH>);
close(FH);

my $res = eval($dump);

unless($@){
my $header = $res->headers; 
$r->content_type($header->content_type);
$r->header_out("Last-Modified"=>time2str((stat("/tmp/_$digest"))[9]));
$r->send_http_header;
print $res->content;
return OK;
}

}elsif($if_last_modified && time - $last_modified < 60*60*4){

return 304;

}





my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new(GET=>$uri);
my $res = $ua->request($req);

#200と304とそれ以外
if($res->code eq "200"){

my $header = $res->headers; 
$r->content_type($header->content_type);
$r->header_out("Last-Modified"=>time2str(time()));
$r->send_http_header;
print $res->content;

#保存
open(FH,"> /tmp/_$digest");
print FH Dumper($res);
close(FH);

return OK;

}elsif($res->code eq "304"){

return 304;

}else{
return $res->code;
}

}

1;

httpd.confに

PerlModule EasyProxy
<Location /EasyProxy>
SetHandler perl-script
PerlHandler EasyProxy
</Location>

んで、
http://hogehoge/EasyProxy?取得したい画像URL
で完了