Aozora2Epub
view release on metacpan or search on metacpan
lib/Aozora2Epub/CachedGet.pm view on Meta::CPAN
if ($url->isa('URI')) {
$url = $url->as_string;
}
my $content = $CACHE->get($url);
return $content if $content;
my $r = HTTP::Tiny->new->get($url);
croak "$url: $r->{status} $r->{reason}" unless $r->{success};
$content = $r->{content};
my $encoding = 'utf-8';
my $content_type = $r->{headers}{'content-type'};
unless ($content_type =~ m{text/}) {
$CACHE->set($url, $content);
return $content; # binary
}
if ($content_type =~ /charset=([^;]+)/) {
$encoding = $1;
} elsif ($content =~ m{<meta http-equiv="content-type" content="[^"]+;charset=(\w+)"}i) {
$encoding = $1;
}
$content = Encode::decode($encoding, $content);
$CACHE->set($url, $content);
return $content;
}
1;
__END__
( run in 2.009 seconds using v1.01-cache-2.11-cpan-524268b4103 )