line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################### |
2
|
|
|
|
|
|
|
# Purpose : Load resources using LWP |
3
|
|
|
|
|
|
|
# Author : John Alden |
4
|
|
|
|
|
|
|
# Created : Aug 2006 |
5
|
|
|
|
|
|
|
############################################################################### |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Email::MIME::CreateHTML::Resolver::LWP; |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
12
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
87
|
|
10
|
3
|
|
|
3
|
|
11
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
132
|
|
11
|
3
|
|
|
3
|
|
1273
|
use MIME::Types; |
|
3
|
|
|
|
|
11697
|
|
|
3
|
|
|
|
|
117
|
|
12
|
3
|
|
|
3
|
|
1884
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
95710
|
|
|
3
|
|
|
|
|
1053
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '1.041'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
3
|
|
|
3
|
1
|
4
|
my ($class, $options) = @_; |
18
|
3
|
|
50
|
|
|
8
|
$options ||= {}; |
19
|
|
|
|
|
|
|
|
20
|
3
|
|
|
|
|
18
|
my $ua = LWP::UserAgent->new(agent => __PACKAGE__); |
21
|
3
|
|
|
|
|
6865
|
$ua->env_proxy; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Stop us getting cached resources when they have been updated on the server |
24
|
3
|
|
|
|
|
12380
|
$ua->default_header( 'Cache-Control' => 'no-cache' ); |
25
|
3
|
|
|
|
|
134
|
$ua->default_header( 'Pragma' => 'no-cache' ); |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
|
|
|
95
|
my $self = { |
28
|
|
|
|
|
|
|
%$options, |
29
|
|
|
|
|
|
|
'UA' => $ua, |
30
|
|
|
|
|
|
|
}; |
31
|
3
|
|
|
|
|
19
|
return bless($self, $class); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#Resource loader using LWP |
35
|
|
|
|
|
|
|
sub get_resource { |
36
|
0
|
|
|
0
|
1
|
|
my ($self, $src) = @_; |
37
|
0
|
|
|
|
|
|
my $base = $self->{base}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
#Resolve URIs relative to optional base URI |
40
|
0
|
|
|
|
|
|
my $uri; |
41
|
0
|
0
|
|
|
|
|
if(defined $base) { |
42
|
0
|
|
|
|
|
|
require URI::WithBase; |
43
|
0
|
|
|
|
|
|
$uri = URI::WithBase->new_abs( $src, $base ); |
44
|
|
|
|
|
|
|
} else { |
45
|
0
|
|
|
|
|
|
$uri = new URI($src); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#Fetch resource from URI using LWP |
49
|
0
|
|
|
|
|
|
my $response = $self->{UA}->get($uri->as_string); |
50
|
0
|
0
|
|
|
|
|
croak( "Could not fetch ".$uri->as_string." : ".$response->status_line ) unless ($response->is_success); |
51
|
0
|
|
|
|
|
|
my $content = $response->content; |
52
|
0
|
|
|
|
|
|
DUMP("HTTP response", $response); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
#Filename |
55
|
0
|
|
|
|
|
|
my $path = $uri->path; |
56
|
0
|
|
|
|
|
|
my ($volume,$directories,$filename) = File::Spec->splitpath( $path ); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#Deduce MIME type and transfer encoding |
59
|
0
|
|
|
|
|
|
my ($mimetype, $encoding); |
60
|
0
|
0
|
0
|
|
|
|
if(defined $filename && length($filename)) { |
61
|
0
|
|
|
|
|
|
TRACE("Using file extension to deduce MIME type and transfer encoding"); |
62
|
0
|
|
|
|
|
|
($mimetype, $encoding) = MIME::Types::by_suffix($filename); |
63
|
|
|
|
|
|
|
} else { |
64
|
0
|
|
|
|
|
|
$filename = 'index'; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
#If we have a content-type header we can make a more informed guess at MIME type |
68
|
0
|
0
|
|
|
|
|
if ($response->header('content-type')) { |
69
|
0
|
|
|
|
|
|
$mimetype = $response->header('content-type'); |
70
|
0
|
|
|
|
|
|
TRACE("Content Type header: $mimetype"); |
71
|
0
|
0
|
|
|
|
|
$mimetype = $1 if($mimetype =~ /(\S+);\s*charset=(.*)$/); #strip down to just a MIME type |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
#If all else fails then some conservative and general-purpose defaults are: |
75
|
0
|
|
0
|
|
|
|
$mimetype ||= 'application/octet-stream'; |
76
|
0
|
|
0
|
|
|
|
$encoding ||= 'base64'; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
#Return values expected from a resource callback |
79
|
0
|
|
|
|
|
|
return ($content, $filename, $mimetype, $encoding); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
0
|
0
|
|
sub TRACE {} |
83
|
0
|
|
|
0
|
0
|
|
sub DUMP {} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Email::MIME::CreateHTML::Resolver::LWP - uses LWP as a resource resolver |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SYNOPSIS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $o = new Email::MIME::CreateHTML::Resolver::LWP(\%args) |
94
|
|
|
|
|
|
|
my ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 DESCRIPTION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
This is used by Email::MIME::CreateHTML to load resources. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 METHODS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over 4 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item $o = new Email::MIME::CreateHTML::Resolver::LWP(\%args) |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
%args can contain: |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=over 4 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item base |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Base URI to resolve URIs passed to get_resource. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=back |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri) |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=back |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 AUTHOR |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Tony Hennessy, Simon Flack and John Alden with additional contributions by |
123
|
|
|
|
|
|
|
Ricardo Signes and Henry Van Styn |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 COPYRIGHT |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
(c) BBC 2005,2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |