Padre
view release on metacpan or search on metacpan
lib/Padre/File/HTTP.pm view on Meta::CPAN
package Padre::File::HTTP;
use 5.008;
use strict;
use warnings;
use Padre::Constant ();
use Padre::File ();
use Padre::Logger;
our $VERSION = '1.02';
our @ISA = 'Padre::File';
my $WRITE_WARNING_DONE = 0;
sub new {
my $class = shift;
# Don't add a new overall-dependency to Padre:
eval { require LWP::UserAgent; };
if ($@) {
# TO DO: This should be an error popup to the user, not a shell window warning
warn 'LWP::UserAgent is not installed, Padre::File::HTTP currently depends on it.';
return;
}
my $self = bless { filename => $_[0], UA => LWP::UserAgent->new }, $class;
# Using the config is optional, tests and other usages should run without
my $config = eval { return Padre->ide->config; };
if ( defined($config) ) {
$self->{_timeout} = $config->file_http_timeout;
} else {
# Use defaults if we have no config
$self->{_timeout} = 30;
}
$self->{protocol} = 'http'; # Should not be overridden
$self->{UA}->timeout( $self->{_timeout} );
$self->{UA}->env_proxy unless Padre::Constant::WIN32;
return $self;
}
sub _request {
my $self = shift;
my $method = shift || 'GET';
my $URL = shift || $self->{filename};
my $content = shift;
TRACE( sprintf( Wx::gettext('Sending HTTP request %s...'), $URL ) ) if DEBUG;
my $HTTP_req = HTTP::Request->new( $method, $URL, undef, $content );
my $result = $self->{UA}->request($HTTP_req);
if ( $result->is_success ) {
if (wantarray) {
return $result->content, $result;
} else {
return $result->content;
}
} else {
if (wantarray) {
return ( undef, $result );
} else {
return;
}
}
}
sub can_run {
return ();
}
sub size {
my $self = shift;
my ( $content, $result ) = $self->_request('HEAD');
return $result->header('Content-Length');
}
sub mode {
( run in 0.816 second using v1.01-cache-2.11-cpan-364913b4093 )