root/trunk/plagger/lib/Plagger/UserAgent.pm

Revision 1254 (checked in by miyagawa, 14 years ago)

move AuthenRequest? hook to useragent.init

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::UserAgent;
2 use strict;
3 use base qw( LWP::UserAgent );
4
5 use Plagger::Cookies;
6 use URI::Fetch 0.06;
7
8 sub new {
9     my $class = shift;
10     my $self  = $class->SUPER::new(@_);
11
12     my $conf = Plagger->context->conf->{user_agent};
13     if ($conf->{cookies}) {
14         $self->cookie_jar( Plagger::Cookies->create($conf->{cookies}) );
15     }
16
17     $self->agent( $conf->{agent} || "Plagger/$Plagger::VERSION (http://plagger.org/)" );
18     $self->timeout( $conf->{timeout} || 15 );
19     $self->env_proxy();
20
21     Plagger->context->run_hook('useragent.init', { ua => $self });
22
23     $self;
24 }
25
26 sub fetch {
27     my($self, $url, $plugin, $opt) = @_;
28
29     my $res = URI::Fetch->fetch($url,
30         UserAgent => $self,
31         $plugin ? (Cache => $plugin->cache) : (),
32         ForceResponse => 1,
33         ($opt ? %$opt : ()),
34     );
35
36     if ($res && $url =~ m!^file://!) {
37         $res->content_type( Plagger::Util::mime_type_of(URI->new($url)) );
38     }
39
40     $res;
41 }
42
43 sub request {
44     my $self = shift;
45     my($req) = @_;
46     Plagger->context->run_hook('useragent.request', { ua => $self, url => $req->uri });
47     $self->SUPER::request(@_);
48 }
49
50 sub mirror {
51     my($self, $request, $file) = @_;
52
53     unless (ref($request)) {
54         return $self->SUPER::mirror($request, $file);
55     }
56
57     # below is copied from LWP::UserAgent
58     if (-e $file) {
59         my($mtime) = (stat($file))[9];
60         if($mtime) {
61             $request->header('If-Modified-Since' =>
62                              HTTP::Date::time2str($mtime));
63         }
64     }
65     my $tmpfile = "$file-$$";
66
67     my $response = $self->request($request, $tmpfile);
68     if ($response->is_success) {
69
70         my $file_length = (stat($tmpfile))[7];
71         my($content_length) = $response->header('Content-length');
72
73         if (defined $content_length and $file_length < $content_length) {
74             unlink($tmpfile);
75             die "Transfer truncated: " .
76                 "only $file_length out of $content_length bytes received\n";
77         }
78         elsif (defined $content_length and $file_length > $content_length) {
79             unlink($tmpfile);
80             die "Content-length mismatch: " .
81                 "expected $content_length bytes, got $file_length\n";
82         }
83         else {
84             # OK
85             if (-e $file) {
86                 # Some dosish systems fail to rename if the target exists
87                 chmod 0777, $file;
88                 unlink $file;
89             }
90             rename($tmpfile, $file) or
91                 die "Cannot rename '$tmpfile' to '$file': $!\n";
92
93             if (my $lm = $response->last_modified) {
94                 # make sure the file has the same last modification time
95                 utime $lm, $lm, $file;
96             }
97         }
98     }
99     else {
100         unlink($tmpfile);
101     }
102     return $response;
103 }
104
105 1;
106
Note: See TracBrowser for help on using the browser.