root/branches/feature-server/plagger/lib/Plagger/UserAgent.pm

Revision 856 (checked in by miyagawa, 6 years ago)

merge from trunk to plagger-server for Enclosures support and such. Sorry for the big commit

  • 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     $self;
22 }
23
24 sub fetch {
25     my($self, $url, $plugin, $opt) = @_;
26
27     URI::Fetch->fetch($url,
28         UserAgent => $self,
29         $plugin ? (Cache => $plugin->cache) : (),
30         ForceResponse => 1,
31         ($opt ? %$opt : ()),
32     );
33 }
34
35 sub mirror {
36     my($self, $request, $file) = @_;
37
38     unless (ref($request)) {
39         return $self->SUPER::mirror($request, $file);
40     }
41
42     # below is copied from LWP::UserAgent
43     if (-e $file) {
44         my($mtime) = (stat($file))[9];
45         if($mtime) {
46             $request->header('If-Modified-Since' =>
47                              HTTP::Date::time2str($mtime));
48         }
49     }
50     my $tmpfile = "$file-$$";
51
52     my $response = $self->request($request, $tmpfile);
53     if ($response->is_success) {
54
55         my $file_length = (stat($tmpfile))[7];
56         my($content_length) = $response->header('Content-length');
57
58         if (defined $content_length and $file_length < $content_length) {
59             unlink($tmpfile);
60             die "Transfer truncated: " .
61                 "only $file_length out of $content_length bytes received\n";
62         }
63         elsif (defined $content_length and $file_length > $content_length) {
64             unlink($tmpfile);
65             die "Content-length mismatch: " .
66                 "expected $content_length bytes, got $file_length\n";
67         }
68         else {
69             # OK
70             if (-e $file) {
71                 # Some dosish systems fail to rename if the target exists
72                 chmod 0777, $file;
73                 unlink $file;
74             }
75             rename($tmpfile, $file) or
76                 die "Cannot rename '$tmpfile' to '$file': $!\n";
77
78             if (my $lm = $response->last_modified) {
79                 # make sure the file has the same last modification time
80                 utime $lm, $lm, $file;
81             }
82         }
83     }
84     else {
85         unlink($tmpfile);
86     }
87     return $response;
88 }
89
90 1;
91
Note: See TracBrowser for help on using the browser.