File Coverage

blib/lib/Net/HTTP/Client.pm
Criterion Covered Total %
statement 41 41 100.0
branch 10 16 62.5
condition 7 17 41.1
subroutine 8 8 100.0
pod 3 3 100.0
total 69 85 81.1


line stmt bran cond sub pod time code
1             package Net::HTTP::Client;
2              
3             =head1 NAME
4              
5             Net::HTTP::Client - A Not-quite-so-low-level HTTP connection (client)
6              
7             =head1 VERSION
8              
9             Version 0.01
10              
11             =head1 SYNOPSIS
12              
13             use Net::HTTP::Client;
14              
15             my $client = Net::HTTP::Client->new(Host => 'localhost', KeepAlive => 0);
16              
17             my $res = $client->request(POST => '/foo', 'fizz buzz');
18              
19             if ($res->is_success) {
20             print $res->decoded_content;
21             } else {
22             warn $res->status_line, "\n";
23             }
24              
25             # a new connection to www.example.com
26             $res = $client->request(GET => 'www.example.com');
27              
28             # another connection to www.example.com
29             $res = $client->request(GET => 'www.example.com/foo');
30              
31             # a new connection to localhost:3335
32             $res = $client->request(GET => 'localhost/bar');
33              
34             # original connection to localhost:3335 IFF KeepAlive is set, otherwise a new connection
35             $res = $client->request(POST => '/baz', 'foo');
36              
37              
38             # or you can skip calling new()
39             $res = Net::HTTP::Client->request(POST => 'localhost:3335/foo', 'Content-Type' => 'application/x-www-form-urlencoded', 'foo=fizz+buzz');
40              
41             =head1 DESCRIPTION
42              
43             C provides a simple interface to L, and is a sub-class of it.
44              
45             =over 2
46              
47             =cut
48              
49 1     1   6984 use 5.12.0;
  1         6  
  1         47  
50 1     1   7 use warnings;
  1         3  
  1         41  
51              
52 1     1   26 use Errno qw(EINTR EIO :POSIX);
  1         2  
  1         2568  
53 1     1   11204 use HTTP::Response;
  1         120554  
  1         40  
54              
55 1     1   925 use parent qw/Net::HTTP/;
  1         293  
  1         5  
56              
57             our $VERSION = '0.01';
58              
59             my $DEBUG = 0;
60             my $used = 0;
61              
62             =item new(%options)
63              
64             The C constructor method takes the same options as L, with the
65             same requirements.
66              
67             =cut
68              
69             sub new {
70 5     5 1 2130 my $class = shift;
71 5         36 $class->SUPER::new(@_);
72             }
73              
74             =item request($method, $uri, @headers?, $content?)
75              
76             Sends a request with method C<$method> and path C<$uri>. Key-value pairs of
77             C<@headers> and C<$content> are optional. If C is set at C,
78             multiple calls to this will use the same connection. Otherwise, a new
79             connection will be created automatically. In addition, a C<$uri> may contain a
80             different host and port, in which case it will make a new connection. For
81             convenience, if you don't wish to reuse connections, you may call this method
82             directly without invoking C if C<$uri> contains a host.
83              
84             Returns an L object.
85              
86             =cut
87              
88             sub request {
89 4     4 1 91934 my ($self, $method, $uri, @headers) = @_;
90              
91 4 100       32 my $content = (@headers % 2) ? pop @headers : '';
92              
93 4 100 50     54 if ($uri !~ /^\//) {
    50 33        
94 2         4 my $host;
95 2         10 ($host, $uri) = split /\//, $uri, 2;
96 2 50       12 warn "New connection to host $host\n" if $DEBUG;
97 2   50     13 $self = $self->new(Host => $host) || die $@;
98 2   50     328201 $uri = '/' . ($uri // '');
99             } elsif ($used and !$self->keep_alive // 0) {
100 2 50       34 warn 'Reconnecting to ', $self->peerhost, ':', $self->peerport, "\n" if $DEBUG;
101 2   50     25 $self = $self->new(Host => $self->peerhost, PeerPort => $self->peerport) || die $@;
102             }
103 4         175764 $used = 1;
104 4 50       16 warn "$method $uri\n" if $DEBUG;
105              
106 4         39 my $success = $self->print( $self->format_request($method => $uri, @headers, $content) );
107 4         1199 my ($status, $message, @res_headers) = $self->read_response_headers;
108 4         325294 HTTP::Response->new($status, $message, \@res_headers, $self->get_content());
109             }
110              
111             =item get_content()
112              
113             Reads and returns the body content of the response. This is called by
114             C, so don't use this if using that.
115              
116             =cut
117              
118             sub get_content {
119 4     4 1 13 my ($self) = @_;
120 4         14 my $content = '';
121 4         18 while (1) {
122 8         22 my $buf;
123 8         73 my $n = $self->read_entity_body($buf, 1024);
124 8 0 33     704 die "read failed: $!" unless defined $n or $!{EINTR} or $!{EAGAIN};
      33        
125 8 100       27 last unless $n;
126 4         13 $content .= $buf;
127             }
128 4         57 $content;
129             }
130              
131             =back
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             Copyright (C) 2014 by Ashley Willis Eashley@laurelmail.netE
136              
137             This library is free software; you can redistribute it and/or modify
138             it under the same terms as Perl itself, either Perl version 5.12.4 or,
139             at your option, any later version of Perl 5 you may have available.
140              
141             =head1 SEE ALSO
142              
143             L
144              
145             =cut
146              
147             1;
148