Acme-HTTP
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Acme/HTTP.pm view on Meta::CPAN
};
my $net_http =
$type eq 'http' ? 'Net::HTTP::NB' :
$type eq 'https' ? 'Net::HTTPS::NB' :
do {
$@ = 'Acme::HTTP - Can\'t identify type';
return;
};
if ($net_http eq 'Net::HTTP::NB') {
require Net::HTTP::NB;
}
elsif ($net_http eq 'Net::HTTPS::NB') {
require Net::HTTPS::NB;
}
else {
$@ = 'Acme::HTTP - Internal error net_http = \''.$net_http.'\'';
return;
}
$hdl = $net_http->new(Host => $host) or do {
$@ = 'Acme::HTTP - Can\'t Net::HTTP(S)->new(Host =>...)';
return;
};
$hdl->write_request(GET => $get, 'User-Agent' => 'Mozilla/5.0');
use IO::Select;
my $sel = IO::Select->new($hdl);
READ_HEADER: {
unless ($sel->can_read($TimeOut)) {
$@ = 'Acme::HTTP - Header timeout('.$TimeOut.')';
return;
}
($Code, $Message, %Response) = $hdl->read_response_headers;
redo READ_HEADER unless $Code;
}
$url = $Response{'Location'};
}
unless (defined $hdl) {
$@ = 'Acme::HTTP - Internal error, hdl is undefined';
return;
}
bless { hdl => $hdl };
}
sub read_entity_body {
my $self = shift;
my $hdl = $self->{'hdl'};
my $sel = IO::Select->new($hdl);
unless ($sel->can_read($Acme::HTTP::TimeOut)) {
$@ = 'Timeout ('.$Acme::HTTP::TimeOut.' sec)';
return;
}
my $bytes = $hdl->read_entity_body($_[0], $_[1]);
unless (defined $bytes) {
$@ = "$!";
return;
}
return $bytes;
}
1;
__END__
=head1 NAME
Acme::HTTP - High-level access to Net::HTTP::NB and Net::HTTPS::NB
=head1 SYNOPSIS
use Acme::HTTP;
# you can use http:
my $url = "http://perldoc.perl.org/perlfaq5.html";
# ...or, alternatively, use https:
# $url = "https://metacpan.org/pod/Data::Dumper";
set_redir_max(3); # Max. number of redirections
set_timeout(10); # Timeout in seconds
my $obj = Acme::HTTP->new($url) || die $@;
my $code = get_code();
my $msg = get_message();
if ($code eq '404') {
die "Page '$url' not found";
}
elsif ($code ne '200') {
die "Page '$url' - Error $code, Msg '$msg'";
}
print "Orig url = ", $url, "\n";
print "Real url = ", get_url_act(), "\n";
print "Redirections = ", get_redir_act(), "\n";
print "Length = ", get_response()->{'Content-Length'} // 0, "\n";
print "\n";
while (1) {
my $n = $obj->read_entity_body(my $buf, 4096);
die "read failed: $@" unless defined $n;
last unless $n;
print $buf;
}
=head1 PARAMETERS
The following parameters can be set in advance:
=over
=item set_redir_max($count)
Set the maximum number of redirections
=item set_timeout($sec)
Set the timout in seconds
=back
=head1 RETURN VALUES
The following variables are available read-only after new():
=over
=item get_url_act()
returns the actual url after redirection
=item get_redir_act()
returns the actual number of redirection that have taken place
=item get_code()
returns the HTTP status
view all matches for this distributionview release on metacpan - search on metacpan
( run in 2.860 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-72ae3ad1e6da )