| Email-MIME-CreateHTML documentation | Contained in the Email-MIME-CreateHTML distribution. |
Email::MIME::CreateHTML::Resolver::LWP - uses LWP as a resource resolver
my $o = new Email::MIME::CreateHTML::Resolver::LWP(\%args) my ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)
This is used by Email::MIME::CreateHTML to load resources.
%args can contain:
Base URI to resolve URIs passed to get_resource.
$Revision: 1.7 $ on $Date: 2006/08/24 21:41:38 $ by $Author: johna $
Tony Hennessy, Simon Flack and John Alden
(c) BBC 2005,2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
| Email-MIME-CreateHTML documentation | Contained in the Email-MIME-CreateHTML distribution. |
############################################################################### # Purpose : Load resources using LWP # Author : John Alden # Created : Aug 2006 # CVS : $Header: /home/cvs/software/cvsroot/email/lib/Email/MIME/CreateHTML/Resolver/LWP.pm,v 1.7 2006/08/24 21:41:38 johna Exp $ ############################################################################### package Email::MIME::CreateHTML::Resolver::LWP; use strict; use Carp; use MIME::Types; use LWP::UserAgent; use vars qw($VERSION); $VERSION = sprintf "%d.%03d", q$Revision: 1.7 $ =~ /: (\d+)\.(\d+)/; sub new { my ($class, $options) = @_; $options ||= {}; my $ua = LWP::UserAgent->new(agent => __PACKAGE__); $ua->env_proxy; # Stop us getting cached resources when they have been updated on the server $ua->default_header( 'Cache-Control' => 'no-cache' ); $ua->default_header( 'Pragma' => 'no-cache' ); my $self = { %$options, 'UA' => $ua, }; return bless($self, $class); } #Resource loader using LWP sub get_resource { my ($self, $src) = @_; my $base = $self->{base}; #Resolve URIs relative to optional base URI my $uri; if(defined $base) { require URI::WithBase; $uri = URI::WithBase->new_abs( $src, $base ); } else { $uri = new URI($src); } #Fetch resource from URI using LWP my $response = $self->{UA}->get($uri->as_string); croak( "Could not fetch ".$uri->as_string." : ".$response->status_line ) unless ($response->is_success); my $content = $response->content; DUMP("HTTP response", $response); #Filename my $path = $uri->path; my ($volume,$directories,$filename) = File::Spec->splitpath( $path ); #Deduce MIME type and transfer encoding my ($mimetype, $encoding); if(defined $filename && length($filename)) { TRACE("Using file extension to deduce MIME type and transfer encoding"); ($mimetype, $encoding) = MIME::Types::by_suffix($filename); } else { $filename = 'index'; } #If we have a content-type header we can make a more informed guess at MIME type if ($response->header('content-type')) { $mimetype = $response->header('content-type'); TRACE("Content Type header: $mimetype"); $mimetype = $1 if($mimetype =~ /(\S+);\s*charset=(.*)$/); #strip down to just a MIME type } #If all else fails then some conservative and general-purpose defaults are: $mimetype ||= 'application/octet-stream'; $encoding ||= 'base64'; #Return values expected from a resource callback return ($content, $filename, $mimetype, $encoding); } sub TRACE {} sub DUMP {} 1;