blib/lib/Net/HTTP/Client.pm

filter: Statementstmt 61.3% 27 / 44 Branchbran 18.7% 3 / 16 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 42.7% 41 / 96 SCAR 42.6
CC
16
Cov
40%
request
63.7
get_content
30.0
BEGIN
1.0
CRAP 70.6
↑ Summary
1 package Net::HTTP::Client;
2 1 $Net::HTTP::Client::VERSION = '0.011';
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.011
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<Net::HTTP::Client> provides a simple interface to L<Net::HTTP>, and is a
44 sub-class of it.
45  
46 This was written because I wanted something that did less than what
47 L<LWP::UserAgent> does when making requests. Like L<LWP::UserAgent>, it
48 returns an L<HTTP::Response> object, so you can handle the response just the
49 same.
50  
51 =over 2
52  
53 =cut
54  
55 1 ▶use 5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use Errno qw(EINTR EIO :POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 1 ▶use parent qw/Net::HTTP/;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 1 my $DEBUG = 0;
64 1 my $used = 0;
65  
66 =item new(%options)
67  
68 The C<Net::HTTP::Client> constructor method takes the same options as
69 L<Net::HTTP>, with the same requirements.
70  
71 =cut
72  
73 sub new {
74 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
75 1 $class->SUPER::new(@_);
76 }
77  
78 =item request($method, $uri, @headers?, $content?)
79  
80 Sends a request with method C<$method> and path C<$uri>. Key-value pairs of
81 C<@headers> and C<$content> are optional. If C<KeepAlive> is set at
82 C<new()>, multiple calls to this will use the same connection. Otherwise, a
83 new connection will be created automatically. In addition, a C<$uri> may
84 contain a different host and port, in which case it will make a new
85 connection. For convenience, if you don't wish to reuse connections, you
86 may call this method directly without invoking C<new()> if C<$uri> contains
87 a host.
88  
89 Returns an L<HTTP::Response> object.
90  
91 =cut
92  
93 sub request {
94 1 ▶ my ($self, $method, $uri, @headers) = @_;
Subroutine request: called CC=12 SCAR=41.5
Pod documented
95  
96 1 ▶ my $content = (@headers % 2) ? pop @headers : '';
Branch: 1 branch
BranchTrueFalseTotal
@headers % 2 ? : 1 0 1
97  
98 1 ▶ if ($uri !~ /^\//) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $uri =~ m[^/]u) { } 1 0 1
elsif ($used and !$self->keep_alive // 0) { } 0 0 0
99 1 my $host;
100 1 ($host, $uri) = split /\//, $uri, 2;
101 1 ▶ warn "New connection to host $host\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 1 1
102 1 ▶ $self = $self->new(Host => $host) || die $@;
Condition50%1/2
$self->new('Host', $host) || die($@)
l!l
01
MC/DC0%0/1
$self->new('Host', $host) || die($@)
$self->new('Host', $host)
Truth table50%1/2
A
A= $self->new('Host', $host)
Aresult
11
00
103 0 ▶ $uri = '/' . ($uri // '');
Condition0%0/2
$uri // ""
l!l
00
MC/DC0%0/1
$uri // ""
$uri
Truth table0%0/2
A
A= $uri
Aresult
11
00
104 ▶ } elsif ($used and !$self->keep_alive // 0) {
Condition0%0/5
$used and !$self->keep_alive // 0
!ll&&!rl&&r
000
!$self->keep_alive // 0
l!l
00
MC/DC0%0/2
$used and !$self->keep_alive // 0
$used !$self->keep_alive
Truth table0%0/3
A and not(B)
A= $usedB= !$self->keep_alive
ABresult
0X0
100
111
105 0 ▶ warn 'Reconnecting to ', $self->peerhost, ':', $self->peerport, "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
106 0 ▶ $self = $self->new(Host => $self->peerhost, PeerPort => $self->peerport) || die $@;
Condition0%0/2
$self->new('Host', $self->peerhost, 'PeerPort', $self->peerport) || die($@)
l!l
00
MC/DC0%0/1
$self->new('Host', $self->peerhost, 'PeerPort', $self->peerport) || die($@)
$self->new('Host', $self->peerhost, 'PeerPort', $self->peerport)
Truth table0%0/2
A
A= $self->new('Host', $self->peerhost, 'PeerPort', $self->peerport)
Aresult
11
00
107 }
108 0 $used = 1;
109 0 ▶ warn "$method $uri\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
110  
111 0 my $success = $self->print( $self->format_request($method => $uri, @headers, $content) );
112 0 my ($status, $message, @res_headers) = $self->read_response_headers;
113 0 HTTP::Response->new($status, $message, \@res_headers, $self->get_content());
114 }
115  
116 =item get_content()
117  
118 Reads and returns the body content of the response. This is called by
119 C<request()>, so don't use this if using that.
120  
121 =cut
122  
123 sub get_content {
124 0 ▶ my ($self) = @_;
Subroutine get_content: not called CC=5 SCAR=34.0
Pod documented
125 0 my $content = '';
126 0 while (1) {
127 0 my $buf;
128 0 my $n = $self->read_entity_body($buf, 1024);
129 0 ▶ die "read failed: $!" unless defined $n or $!{EINTR} or $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n or $!{"EINTR"} or $!{"EAGAIN"} 0 0 0
Condition0%0/6
defined $n or $!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
defined $n or $!{"EINTR"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $n or $!{"EINTR"} or $!{"EAGAIN"}
defined $n $!{"EINTR"} $!{"EAGAIN"}
Truth table0%0/4
A or B or C
A= defined $nB= $!{"EINTR"}C= $!{"EAGAIN"}
ABCresult
1XX1
01X1
0011
0000
130 0 ▶ last unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
131 0 $content .= $buf;
132 }
133 0 $content;
134 }
135  
136 =back
137  
138 =head1 COPYRIGHT AND LICENSE
139  
140 Copyright (C) 2014 by Ashley Willis E<lt>ashley@laurelmail.netE<gt>
141  
142 This library is free software; you can redistribute it and/or modify
143 it under the same terms as Perl itself, either Perl version 5.12.4 or,
144 at your option, any later version of Perl 5 you may have available.
145  
146 =head1 SEE ALSO
147  
148 L<Net::HTTP>
149  
150 L<LWP::UserAgent>
151 =cut
152  
153 1 1;
↑ Summary