sub is_info { HTTP::Status::is_info (shift->{'_rc'}); }
sub is_success { HTTP::Status::is_success (shift->{'_rc'}); }
sub is_redirect { HTTP::Status::is_redirect (shift->{'_rc'}); }
sub is_error { HTTP::Status::is_error (shift->{'_rc'}); }
sub error_as_HTML
{
require HTML::Entities;
my $self = shift;
my $title = 'An Error Occurred';
my $body = HTML::Entities::encode($self->status_line);
return <<EOM;
<html>
<head><title>$title</title></head>
<body>
<h1>$title</h1>
<p>$body</p>
</body>
</html>
EOM
}
sub current_age
{
my $self = shift;
# Implementation of RFC 2616 section 13.2.3
# (age calculations)
my $response_time = $self->client_date;
my $date = $self->date;
my $age = 0;
if ($response_time && $date) {
$age = $response_time - $date; # apparent_age
$age = 0 if $age < 0;
}
my $age_v = $self->header('Age');
if ($age_v && $age_v > $age) {
$age = $age_v; # corrected_received_age
}
my $request = $self->request;
if ($request) {
my $request_time = $request->date;
if ($request_time) {
# Add response_delay to age to get 'corrected_initial_age'
$age += $response_time - $request_time;
}
}
if ($response_time) {
$age += time - $response_time;
}
return $age;
}
sub freshness_lifetime
{
my $self = shift;
# First look for the Cache-Control: max-age=n header
my @cc = $self->header('Cache-Control');
if (@cc) {
my $cc;
for $cc (@cc) {
my $cc_dir;
for $cc_dir (split(/\s*,\s*/, $cc)) {
if ($cc_dir =~ /max-age\s*=\s*(\d+)/i) {
return $1;
}
}
}
}
# Next possibility is to look at the "Expires" header
my $date = $self->date || $self->client_date || time;
my $expires = $self->expires;
unless ($expires) {
# Must apply heuristic expiration
my $last_modified = $self->last_modified;
if ($last_modified) {
my $h_exp = ($date - $last_modified) * 0.10; # 10% since last-mod
if ($h_exp < 60) {
return 60; # minimum
}
elsif ($h_exp > 24 * 3600) {
# Should give a warning if more than 24 hours according to
# RFC 2616 section 13.2.4, but I don't know how to do it
# from this function interface, so I just make this the
# maximum value.
return 24 * 3600;
}
return $h_exp;
}
else {
return 3600; # 1 hour is fallback when all else fails
}
}
=3= |