File Coverage

blib/lib/HTTP/Client.pm
Criterion Covered Total %
statement 34 65 52.3
branch 2 18 11.1
condition 1 12 8.3
subroutine 9 21 42.8
pod 16 16 100.0
total 62 132 46.9


line stmt bran cond sub pod time code
1             package HTTP::Client;
2              
3 1     1   697 use 5.006;
  1         4  
  1         30  
4 1     1   6 use strict;
  1         1  
  1         39  
5 1     1   17 use warnings;
  1         2  
  1         34  
6 1     1   6 use Carp;
  1         1  
  1         65  
7 1     1   998 use HTTP::Lite;
  1         5967  
  1         689  
8              
9             our $VERSION = '1.57';
10              
11             #array of headers sendable for requests.
12              
13             my @request_headers = qw(
14             Accept Accept-Charset Accept-Encoding Accept-Language
15             Authorization Expect From Host
16             If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since
17             Max-Forwards Proxy-Authorization Range Referer TE User-Agent
18             );
19              
20             my $http = HTTP::Lite->new; #make new HTTP::Lite object.
21             #class constructor
22             sub new {
23 2     2 1 1764 my $class = shift;
24 2         5 my $self = {};
25 2         8 $self->{agent} = shift; #user agent
26 2         9 $self->{from } = shift;
27 2         9 bless $self, $class;
28             }
29              
30             sub get {
31 2     2 1 12 my $self = shift;
32 2         4 my $uri = shift;
33 2         7 $uri =~ s/#//; #get rid of fragment, according to RFC 2616
34 2         3 my $request;
35              
36 2         10 $http->reset();
37 2 50       98 $request = $http->request($uri) or croak "Can't get $uri; may be a result of a bad hostname: $!"; #get it.
38 2         368275 my $fullmessage = $http->status . ' ' . $http->status_message; #full status message.
39             #give them the body of the request if it has a body and sent an OK response.
40 2 50 33     38 return $http->body if ($http->body and $fullmessage =~ /200 OK/);
41             #return the message if the message isn't 200 OK, and there is no body.
42 0 0 0     0 return $fullmessage unless ($fullmessage eq '200 OK' and $http->body);
43             }
44              
45              
46             sub response_headers {
47 1     1 1 28 my $self = shift;
48             #gather all response headers.
49 1         7 my @headers = $http->headers_array;
50 1         96 return @headers;
51             }
52              
53              
54             sub agent {
55 0     0 1 0 my $self = shift;
56 0         0 my $agent = shift; #user agent
57             #send useragent header if they supplied one.
58 0 0 0     0 $agent = $self->{agent} if ($self->{agent} and not $agent);
59 0 0       0 $http->add_req_header('User-Agent', $agent) if ($agent);
60             #return the current one if they didn't supply the useragent.
61 0 0       0 return $http->get_req_header('User-Agent') unless ($agent);
62             }
63              
64             sub from {
65 0     0 1 0 my $self = shift;
66 0         0 my $from = shift;
67 0 0 0     0 $from = $self->{from} if ($self->{from} and not $from);
68 0 0       0 $http->add_req_header('From', $from) if ($from); #set the from header.
69 0 0       0 $http->get_req_header('From') unless ($from);
70             }
71              
72             ##
73             ## Methods to get response headers
74             ##
75              
76             sub status_message {
77 1     1 1 44 my $self = shift;
78 1         6 my $fullmessage = $http->status . ' ' . $http->status_message;
79 1         18 return $fullmessage;
80             }
81              
82 0     0 1   sub server { my $self = shift; $http->get_header('Server') }
  0            
83 0     0 1   sub content_type { my $self = shift; $http->get_header('Content-Type') }
  0            
84 0     0 1   sub last_modified { my $self = shift; $http->get_header('Last-Modified') }
  0            
85 0     0 1   sub protocol { my $self = shift; $http->protocol }
  0            
86 0     0 1   sub content_encoding { my $self = shift; $http->get_header('Content-Encoding') }
  0            
87 0     0 1   sub content_length { my $self = shift; $http->get_header('Content-Length') }
  0            
88 0     0 1   sub warning { my $self = shift; $http->get_header('Warning') }
  0            
89 0     0 1   sub title { my $self = shift; $http->get_header('Title') }
  0            
90 0     0 1   sub date { my $self = shift; $http->get_header('Date') }
  0            
91 0     0 1   sub host { my $self = shift; $http->get_header('Host') }
  0            
92              
93             1;
94              
95             __END__