| 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<Net::HTTP::Client> provides a simple interface to L<Net::HTTP>, and is a sub-class of it. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =over 2 | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use 5.12.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use Errno qw(EINTR EIO :POSIX); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | use parent qw/Net::HTTP/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | my $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | my $used = 0; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =item new(%options) | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | The C<Net::HTTP::Client> constructor method takes the same options as L<Net::HTTP>, with the | ||||||||||||||||||||||||||||||||||||||||||
| 65 | same requirements. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | $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<KeepAlive> is set at C<new()>, | ||||||||||||||||||||||||||||||||||||||||||
| 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<new()> if C<$uri> contains a host. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | Returns an L<HTTP::Response> object. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | sub request { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my ($self, $method, $uri, @headers) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: called CC=12 SCAR=41.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | my $content = (@headers % 2) ? pop @headers : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | if ($uri !~ /^\//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | my $host; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ($host, $uri) = split /\//, $uri, 2; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | warn "New connection to host $host\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | $self = $self->new(Host => $host) || die $@; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->new('Host', $host) || die($@)
MC/DC0%0/1
$self->new('Host', $host) || die($@)
$self->new('Host', $host)
Truth table50%1/2
A
A= $self->new('Host', $host)
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $uri = '/' . ($uri // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$uri // ""
MC/DC0%0/1
$uri // ""
$uri
Truth table0%0/2
A
A= $uri
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | ▶ | } elsif ($used and !$self->keep_alive // 0) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$used and !$self->keep_alive // 0
!$self->keep_alive // 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | warn 'Reconnecting to ', $self->peerhost, ':', $self->peerport, "\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $self = $self->new(Host => $self->peerhost, PeerPort => $self->peerport) || die $@; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->new('Host', $self->peerhost, 'PeerPort', $self->peerport) || die($@)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $used = 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | warn "$method $uri\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $success = $self->print( $self->format_request($method => $uri, @headers, $content) ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my ($status, $message, @res_headers) = $self->read_response_headers; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | 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<request()>, so don't use this if using that. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | sub get_content { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_content: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $content = ''; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $n = $self->read_entity_body($buf, 1024); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | die "read failed: $!" unless defined $n or $!{EINTR} or $!{EAGAIN}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $n or $!{"EINTR"} or $!{"EAGAIN"}
defined $n or $!{"EINTR"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | last unless $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $content .= $buf; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $content; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | Copyright (C) 2014 by Ashley Willis E<lt>ashley@laurelmail.netE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 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<Net::HTTP> | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||