/usr/share/perl5/Net/OpenID/URIFetch.pm is in libnet-openid-common-perl 1.20-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #!/usr/bin/perl
=head1 NAME
Net::OpenID::URIFetch - fetch and cache content from HTTP URLs
=head1 VERSION
version 1.20
=head1 DESCRIPTION
This is roughly based on Ben Trott's URI::Fetch module, but
URI::Fetch doesn't cache enough headers that Yadis can be implemented
with it, so this is a lame copy altered to allow Yadis support.
Hopefully one day URI::Fetch can be modified to do what we need and
this can go away.
This module is tailored to the needs of Net::OpenID::Consumer and probably
isn't much use outside of it. See URI::Fetch for a more general module.
=cut
package Net::OpenID::URIFetch;
$Net::OpenID::URIFetch::VERSION = '1.20';
use HTTP::Request;
use HTTP::Status;
use strict;
use warnings;
use Carp();
use constant URI_OK => 200;
use constant URI_MOVED_PERMANENTLY => 301;
use constant URI_NOT_MODIFIED => 304;
use constant URI_GONE => 410;
# Fetch a document, either from cache or from a server
# URI -- location of document
# CONSUMER -- where to find user-agent and cache
# CONTENT_HOOK -- applied to freshly-retrieved document
# to normalize it into some particular format/structure
# PREFIX -- used as part of the cache key, distinguishes
# different content formats and must change whenever
# CONTENT_HOOK is switched to a new format; this way,
# cache entries from a previous run of this server that
# are using a different content format will not kill us.
sub fetch {
my ($class, $uri, $consumer, $content_hook, $prefix) = @_;
$prefix ||= '';
if ($uri eq 'x-xrds-location') {
Carp::confess("Buh?");
}
my $ua = $consumer->ua;
my $cache = $consumer->cache;
my $ref;
my $cache_key = "URIFetch:${prefix}:${uri}";
if ($cache) {
if (my $blob = $cache->get($cache_key)) {
$ref = Storable::thaw($blob);
}
}
my $cached_response = sub {
return Net::OpenID::URIFetch::Response->new(
status => 200,
content => $ref->{Content},
last_modified => $ref->{LastModified},
headers => $ref->{Headers},
final_uri => $ref->{FinalURI},
);
};
# We just serve anything from the last 60 seconds right out of the cache,
# thus avoiding doing several requests to the same URL when we do
# Yadis, then HTML discovery.
# TODO: Make this tunable?
if ($ref && $ref->{CacheTime} > (time() - 60)) {
$consumer->_debug("Cache HIT for $uri");
return $cached_response->();
}
else {
$consumer->_debug("Cache MISS for $uri");
}
my $req = HTTP::Request->new(GET => $uri);
$req->header('Accept-Encoding', scalar HTTP::Message::decodable());
if ($ref) {
if (my $etag = ($ref->{Headers}->{etag})) {
$req->header('If-None-Match', $etag);
}
if (my $ts = $ref->{LastModified}) {
$req->if_modified_since($ts);
}
}
my $res = $ua->request($req);
# There are only a few headers that OpenID/Yadis care about
my @useful_headers = qw(last-modified etag content-type x-yadis-location x-xrds-location);
my %response_fields;
if ($res->code == HTTP::Status::RC_NOT_MODIFIED()) {
$consumer->_debug("Server says it's not modified. Serving from cache.");
return $cached_response->();
}
else {
my $final_uri = $res->request->uri->as_string();
my $final_cache_key = "URIFetch:${prefix}:${final_uri}";
my $content = $res->decoded_content # Decode content-encoding and charset
|| $res->decoded_content(charset => 'none') # Decode content-encoding
|| $res->content; # Undecoded content
if ($content_hook) {
$content_hook->(\$content);
}
my $headers = {};
foreach my $k (@useful_headers) {
$headers->{$k} = $res->header($k);
}
my $ret = Net::OpenID::URIFetch::Response->new(
status => $res->code,
last_modified => $res->last_modified,
content => $content,
headers => $headers,
final_uri => $final_uri,
);
if ($cache && $res->code == 200) {
my $cache_data = {
LastModified => $ret->last_modified,
Headers => $ret->headers,
Content => $ret->content,
CacheTime => time(),
FinalURI => $final_uri,
};
my $cache_blob = Storable::freeze($cache_data);
$cache->set($final_cache_key, $cache_blob);
$cache->set($cache_key, $cache_blob);
}
return $ret;
}
}
package Net::OpenID::URIFetch::Response;
$Net::OpenID::URIFetch::Response::VERSION = '1.20';
use strict;
use constant FIELDS => [qw(final_uri status content headers last_modified)];
use fields @{FIELDS()};
use Carp();
sub new {
my ($class, %opts) = @_;
my $self = fields::new($class);
@{$self}{@{FIELDS()}} = delete @opts{@{FIELDS()}};
Carp::croak("Unknown option(s): " . join(", ", keys %opts)) if %opts;
return $self;
}
BEGIN {
foreach my $field_name (@{FIELDS()}) {
no strict 'refs';
*{__PACKAGE__ . '::' . $field_name}
= sub { return $_[0]->{$field_name}; };
}
}
sub header {
return $_[0]->{headers}{lc($_[1])};
}
1;
|