Skip to content
This repository has been archived by the owner on Mar 7, 2019. It is now read-only.

Commit

Permalink
allow using LWP::UserAgent as HTTP protocol_class
Browse files Browse the repository at this point in the history
1) Use Class::Load to load the protocol_class requested
2) Add check_http_response to check for HTTP::Response objects
  • Loading branch information
preaction committed Jan 23, 2013
1 parent 3028311 commit 7ec0fb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions Build.PL
Expand Up @@ -24,6 +24,7 @@ my $builder = Module::Build->new(
'File::ShareDir' => 0,
'Archive::Extract' => 0,
'HTTP::Tiny' => 0,
'Class::Load' => 0,
},
recommends => {
'HTML::LinkExtor' => 0,
Expand Down
32 changes: 27 additions & 5 deletions lib/Alien/Base/ModuleBuild/Repository/HTTP.pm
Expand Up @@ -8,7 +8,9 @@ $VERSION = eval $VERSION;

use Carp;

use Class::Load qw( load_class );
use HTTP::Tiny;
use Scalar::Util qw( blessed );
use URI;

use Alien::Base::ModuleBuild::Utils;
Expand All @@ -26,6 +28,7 @@ sub connection {

# allow easy use of HTTP::Tiny subclass
$self->{protocol_class} ||= 'HTTP::Tiny';
load_class( $self->{protocol_class} );

my $http = $self->{protocol_class}->new();

Expand All @@ -42,8 +45,10 @@ sub get_file {
my $host = $self->{host};
my $from = $self->location;

my $response = $self->connection->mirror('http://' . $host . $from . '/' . $file, $file );
croak "Download failed: " . $response->{reason} unless $response->{success};
my $url = 'http://' . $host . $from . '/' . $file;
my $res = $self->connection->mirror($url, $file);
my ( $is_error, $content ) = $self->check_http_response( $res );
croak "Download failed: " . $content if $is_error;

return 1;
}
Expand All @@ -57,12 +62,13 @@ sub list_files {

my $res = $self->connection->get($uri);

unless ($res->{success}) {
carp $res->{reason};
my ( $is_error, $content ) = $self->check_http_response( $res );
if ( $is_error ) {
carp $content;
return ();
}

my @links = $self->find_links($res->{content});
my @links = $self->find_links($content);

return @links;
}
Expand Down Expand Up @@ -107,5 +113,21 @@ sub find_links_textbalanced {
return Alien::Base::ModuleBuild::Utils::find_anchor_targets($html);
}

sub check_http_response {
my ( $self, $res ) = @_;
if ( blessed $res && $res->isa( 'HTTP::Response' ) ) {
if ( !$res->is_success ) {
return ( 1, $res->status_line . " " . $res->decoded_content );
}
return ( 0, $res->decoded_content );
}
else {
if ( !$res->{success} ) {
return ( 1, $res->{reason} );
}
return ( 0, $res->{content } );
}
}

1;

0 comments on commit 7ec0fb6

Please sign in to comment.