line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::HTTP::Client; |
2
|
|
|
|
|
|
|
$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 provides a simple interface to L, and is a |
44
|
|
|
|
|
|
|
sub-class of it. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
This was written because I wanted something that did less than what |
47
|
|
|
|
|
|
|
L does when making requests. Like L, it |
48
|
|
|
|
|
|
|
returns an L object, so you can handle the response just the |
49
|
|
|
|
|
|
|
same. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over 2 |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
1
|
|
679
|
use 5.12.0; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
48
|
|
56
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
72
|
|
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
1
|
|
11
|
use Errno qw(EINTR EIO :POSIX); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
509
|
|
59
|
1
|
|
|
1
|
|
1070
|
use HTTP::Response; |
|
1
|
|
|
|
|
35071
|
|
|
1
|
|
|
|
|
35
|
|
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
|
436
|
use parent qw/Net::HTTP/; |
|
1
|
|
|
|
|
255
|
|
|
1
|
|
|
|
|
5
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $DEBUG = 0; |
64
|
|
|
|
|
|
|
my $used = 0; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item new(%options) |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The C constructor method takes the same options as |
69
|
|
|
|
|
|
|
L, with the same requirements. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub new { |
74
|
5
|
|
|
5
|
1
|
1828
|
my $class = shift; |
75
|
5
|
|
|
|
|
31
|
$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 is set at |
82
|
|
|
|
|
|
|
C, 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 if C<$uri> contains |
87
|
|
|
|
|
|
|
a host. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Returns an L object. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub request { |
94
|
4
|
|
|
4
|
1
|
82867
|
my ($self, $method, $uri, @headers) = @_; |
95
|
|
|
|
|
|
|
|
96
|
4
|
100
|
|
|
|
19
|
my $content = (@headers % 2) ? pop @headers : ''; |
97
|
|
|
|
|
|
|
|
98
|
4
|
100
|
50
|
|
|
42
|
if ($uri !~ /^\//) { |
|
|
50
|
33
|
|
|
|
|
99
|
2
|
|
|
|
|
4
|
my $host; |
100
|
2
|
|
|
|
|
7
|
($host, $uri) = split /\//, $uri, 2; |
101
|
2
|
50
|
|
|
|
7
|
warn "New connection to host $host\n" if $DEBUG; |
102
|
2
|
|
50
|
|
|
16
|
$self = $self->new(Host => $host) || die $@; |
103
|
2
|
|
50
|
|
|
316675
|
$uri = '/' . ($uri // ''); |
104
|
|
|
|
|
|
|
} elsif ($used and !$self->keep_alive // 0) { |
105
|
2
|
50
|
|
|
|
32
|
warn 'Reconnecting to ', $self->peerhost, ':', $self->peerport, "\n" if $DEBUG; |
106
|
2
|
|
50
|
|
|
25
|
$self = $self->new(Host => $self->peerhost, PeerPort => $self->peerport) || die $@; |
107
|
|
|
|
|
|
|
} |
108
|
4
|
|
|
|
|
160138
|
$used = 1; |
109
|
4
|
50
|
|
|
|
49
|
warn "$method $uri\n" if $DEBUG; |
110
|
|
|
|
|
|
|
|
111
|
4
|
|
|
|
|
24
|
my $success = $self->print( $self->format_request($method => $uri, @headers, $content) ); |
112
|
4
|
|
|
|
|
808
|
my ($status, $message, @res_headers) = $self->read_response_headers; |
113
|
4
|
|
|
|
|
336103
|
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, so don't use this if using that. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub get_content { |
124
|
4
|
|
|
4
|
1
|
8
|
my ($self) = @_; |
125
|
4
|
|
|
|
|
9
|
my $content = ''; |
126
|
4
|
|
|
|
|
4
|
while (1) { |
127
|
8
|
|
|
|
|
12
|
my $buf; |
128
|
8
|
|
|
|
|
28
|
my $n = $self->read_entity_body($buf, 1024); |
129
|
8
|
0
|
33
|
|
|
283
|
die "read failed: $!" unless defined $n or $!{EINTR} or $!{EAGAIN}; |
|
|
|
33
|
|
|
|
|
130
|
8
|
100
|
|
|
|
18
|
last unless $n; |
131
|
4
|
|
|
|
|
8
|
$content .= $buf; |
132
|
|
|
|
|
|
|
} |
133
|
4
|
|
|
|
|
37
|
$content; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=back |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Copyright (C) 2014 by Ashley Willis Eashley@laurelmail.netE |
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 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
L |
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |