File Coverage

blib/lib/Acme/HTTP.pm
Criterion Covered Total %
statement 12 70 17.1
branch 0 26 0.0
condition n/a
subroutine 4 15 26.6
pod 7 11 63.6
total 23 122 18.8


line stmt bran cond sub pod time code
1             package Acme::HTTP;
2            
3 1     1   646 use strict;
  1         2  
  1         30  
4 1     1   4 use warnings;
  1         2  
  1         100  
5            
6             require Exporter;
7             our @ISA = qw(Exporter);
8             our @EXPORT = qw(
9             get_url_act
10             get_redir_act
11             get_code
12             get_message
13             get_response
14            
15             get_redir_max
16             get_timeout
17            
18             set_redir_max
19             set_timeout
20             );
21            
22             our %EXPORT_TAGS = (all => [ @EXPORT ]);
23            
24             our @EXPORT_OK = qw();
25             our $VERSION = '0.08';
26            
27 1     1   1722 use IO::Select;
  1         1662  
  1         550  
28            
29             our $Url_act = '';
30             our $Redir_act = 0;
31            
32             our $Code = -1;
33             our $Message = '?';
34             our %Response = ();
35            
36             our $Redir_max;
37             $Redir_max = 3 unless defined $Redir_max;
38            
39             our $TimeOut;
40             $TimeOut = 10 unless defined $TimeOut;
41            
42 0     0 1   sub get_url_act { $Url_act }
43 0     0 1   sub get_redir_act { $Redir_act }
44 0     0 1   sub get_code { $Code }
45 0     0 1   sub get_message { $Message }
46 0     0 1   sub get_response { \%Response }
47            
48 0     0 0   sub get_redir_max { $Redir_max }
49 0     0 0   sub get_timeout { $TimeOut }
50            
51 0     0 1   sub set_redir_max { $Redir_max = $_[0] }
52 0     0 1   sub set_timeout { $TimeOut = $_[0] }
53            
54             sub new {
55 0     0 0   shift;
56 0           my ($url) = @_;
57 0           my $hdl;
58            
59 0           $Url_act = '';
60 0           $Redir_act = 0;
61            
62 0           while (defined $url) {
63 0           $Redir_act++;
64 0 0         if ($Redir_act > $Redir_max) {
65 0           $@ = 'Acme::HTTP - Runaway iterations ('.$Redir_max.')';
66 0           return;
67             }
68            
69 0           $Url_act = $url;
70            
71             my ($type, $host, $get) =
72             $Url_act =~ m{\A ([^:]+) : // ([^/]+) \z}xms ? ($1, $2, '/') :
73             $Url_act =~ m{\A ([^:]+) : // ([^/]+) (/ .*) \z}xms ? ($1, $2, $3) :
74 0 0         do {
    0          
75 0           $@ = 'Acme::HTTP - Invalid structure)';
76 0           return;
77             };
78            
79             my $net_http =
80             $type eq 'http' ? 'Net::HTTP::NB' :
81             $type eq 'https' ? 'Net::HTTPS::NB' :
82 0 0         do {
    0          
83 0           $@ = 'Acme::HTTP - Can\'t identify type';
84 0           return;
85             };
86            
87 0 0         if ($net_http eq 'Net::HTTP::NB') {
    0          
88 0           require Net::HTTP::NB;
89             }
90             elsif ($net_http eq 'Net::HTTPS::NB') {
91 0           require Net::HTTPS::NB;
92             }
93             else {
94 0           $@ = 'Acme::HTTP - Internal error net_http = \''.$net_http.'\'';
95 0           return;
96             }
97            
98 0 0         $hdl = $net_http->new(Host => $host) or do {
99 0           $@ = 'Acme::HTTP - Can\'t Net::HTTP(S)->new(Host =>...)';
100 0           return;
101             };
102            
103 0           $hdl->write_request(GET => $get, 'User-Agent' => 'Mozilla/5.0');
104            
105 1     1   10 use IO::Select;
  1         2  
  1         321  
106 0           my $sel = IO::Select->new($hdl);
107            
108             READ_HEADER: {
109 0 0         unless ($sel->can_read($TimeOut)) {
  0            
110 0           $@ = 'Acme::HTTP - Header timeout('.$TimeOut.')';
111 0           return;
112             }
113            
114 0           ($Code, $Message, %Response) = $hdl->read_response_headers;
115            
116 0 0         redo READ_HEADER unless $Code;
117             }
118            
119 0           $url = $Response{'Location'};
120             }
121            
122 0 0         unless (defined $hdl) {
123 0           $@ = 'Acme::HTTP - Internal error, hdl is undefined';
124 0           return;
125             }
126            
127 0           bless { hdl => $hdl };
128             }
129            
130             sub read_entity_body {
131 0     0 0   my $self = shift;
132            
133 0           my $hdl = $self->{'hdl'};
134 0           my $sel = IO::Select->new($hdl);
135            
136 0 0         unless ($sel->can_read($Acme::HTTP::TimeOut)) {
137 0           $@ = 'Timeout ('.$Acme::HTTP::TimeOut.' sec)';
138 0           return;
139             }
140            
141 0           my $bytes = $hdl->read_entity_body($_[0], $_[1]);
142            
143 0 0         unless (defined $bytes) {
144 0           $@ = "$!";
145 0           return;
146             }
147            
148 0           return $bytes;
149             }
150            
151             1;
152            
153             __END__