line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: http_paranoid.pm 2 2005-06-01 23:12:25Z bradfitz $ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package LWPx::Protocol::http_paranoid; |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
79
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require LWP::Debug; |
9
|
|
|
|
|
|
|
require HTTP::Response; |
10
|
|
|
|
|
|
|
require HTTP::Status; |
11
|
|
|
|
|
|
|
require Net::HTTP; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
4
|
use Errno qw(EAGAIN); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
65
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
3
|
use vars qw(@ISA $TOO_LATE $TIME_REMAIN); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
66
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
require LWP::Protocol; |
18
|
|
|
|
|
|
|
@ISA = qw(LWP::Protocol); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
4
|
use vars qw(@ISA @EXTRA_SOCK_OPTS); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
2042
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $CRLF = "\015\012"; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# lame hack using globals in this package to communicate to sysread in the |
25
|
|
|
|
|
|
|
# package at bottom, but whatchya gonna do? Don't want to go modify |
26
|
|
|
|
|
|
|
# Net::HTTP::* to pass explicit timeouts to all the sysreads. |
27
|
|
|
|
|
|
|
sub _set_time_remain { |
28
|
0
|
|
|
0
|
|
0
|
my $now = time; |
29
|
0
|
0
|
|
|
|
0
|
return unless defined $TOO_LATE; |
30
|
0
|
|
|
|
|
0
|
$TIME_REMAIN = $TOO_LATE - $now; |
31
|
0
|
0
|
|
|
|
0
|
$TIME_REMAIN = 0 if $TIME_REMAIN < 0; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _extra_sock_opts # to be overridden by subclass |
36
|
|
|
|
|
|
|
{ |
37
|
0
|
|
|
0
|
|
0
|
return @EXTRA_SOCK_OPTS; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _new_socket |
41
|
|
|
|
|
|
|
{ |
42
|
1
|
|
|
1
|
|
3
|
my($self, $host, $port, $timeout, $request) = @_; |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
8
|
my $conn_cache = $self->{ua}{conn_cache}; |
45
|
1
|
50
|
|
|
|
4
|
if ($conn_cache) { |
46
|
0
|
0
|
|
|
|
0
|
if (my $sock = $conn_cache->withdraw("http", "$host:$port")) { |
47
|
0
|
0
|
0
|
|
|
0
|
return $sock if $sock && !$sock->can_read(0); |
48
|
|
|
|
|
|
|
# if the socket is readable, then either the peer has closed the |
49
|
|
|
|
|
|
|
# connection or there are some garbage bytes on it. In either |
50
|
|
|
|
|
|
|
# case we abandon it. |
51
|
0
|
|
|
|
|
0
|
$sock->close; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
6
|
my @addrs = $self->{ua}->_resolve($host, $request, $timeout); |
56
|
0
|
0
|
|
|
|
0
|
unless (@addrs) { |
57
|
0
|
|
|
|
|
0
|
die "Can't connect to $host:$port (No suitable addresses found)"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my $sock; |
61
|
0
|
|
|
|
|
0
|
local($^W) = 0; # IO::Socket::INET can be noisy |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
0
|
|
|
0
|
while (! $sock && @addrs) { |
64
|
0
|
|
|
|
|
0
|
my $addr = shift @addrs; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $conn_timeout = $request->{_timebegin} ? |
67
|
0
|
0
|
|
|
|
0
|
(time() - $request->{_timebegin}) : |
68
|
|
|
|
|
|
|
$timeout; |
69
|
0
|
|
|
|
|
0
|
$sock = $self->socket_class->new(PeerAddr => $addr, |
70
|
|
|
|
|
|
|
PeerHost => $host, |
71
|
|
|
|
|
|
|
SSL_hostname => $host, |
72
|
|
|
|
|
|
|
PeerPort => $port, |
73
|
|
|
|
|
|
|
Proto => 'tcp', |
74
|
|
|
|
|
|
|
Timeout => $conn_timeout, |
75
|
|
|
|
|
|
|
KeepAlive => !!$conn_cache, |
76
|
|
|
|
|
|
|
SendTE => 1, |
77
|
|
|
|
|
|
|
$self->_extra_sock_opts($addr,$port), |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
0
|
unless ($sock) { |
82
|
|
|
|
|
|
|
# IO::Socket::INET leaves additional error messages in $@ |
83
|
0
|
|
|
|
|
0
|
$@ =~ s/^.*?: //; |
84
|
0
|
|
|
|
|
0
|
die "Can't connect to $host:$port ($@)"; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# perl 5.005's IO::Socket does not have the blocking method. |
88
|
0
|
|
|
|
|
0
|
eval { $sock->blocking(0); }; |
|
0
|
|
|
|
|
0
|
|
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
0
|
$sock; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub socket_class |
96
|
|
|
|
|
|
|
{ |
97
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
98
|
0
|
|
0
|
|
|
0
|
(ref($self) || $self) . "::Socket"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _get_sock_info |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
0
|
|
0
|
my($self, $res, $sock) = @_; |
104
|
0
|
0
|
|
|
|
0
|
if (defined(my $peerhost = $sock->peerhost)) { |
105
|
0
|
|
|
|
|
0
|
$res->header("Client-Peer" => "$peerhost:" . $sock->peerport); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _fixup_header |
110
|
|
|
|
|
|
|
{ |
111
|
0
|
|
|
0
|
|
0
|
my($self, $h, $url, $proxy) = @_; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Extract 'Host' header |
114
|
0
|
|
|
|
|
0
|
my $hhost = $url->authority; |
115
|
0
|
0
|
|
|
|
0
|
if ($hhost =~ s/^([^\@]*)\@//) { # get rid of potential "user:pass@" |
116
|
|
|
|
|
|
|
# add authorization header if we need them. HTTP URLs do |
117
|
|
|
|
|
|
|
# not really support specification of user and password, but |
118
|
|
|
|
|
|
|
# we allow it. |
119
|
0
|
0
|
0
|
|
|
0
|
if (defined($1) && not $h->header('Authorization')) { |
120
|
0
|
|
|
|
|
0
|
require URI::Escape; |
121
|
0
|
|
|
|
|
0
|
$h->authorization_basic(map URI::Escape::uri_unescape($_), |
122
|
|
|
|
|
|
|
split(":", $1, 2)); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
0
|
|
|
|
|
0
|
$h->init_header('Host' => $hhost); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub hlist_remove { |
130
|
0
|
|
|
0
|
0
|
0
|
my($hlist, $k) = @_; |
131
|
0
|
|
|
|
|
0
|
$k = lc $k; |
132
|
0
|
|
|
|
|
0
|
for (my $i = @$hlist - 2; $i >= 0; $i -= 2) { |
133
|
0
|
0
|
|
|
|
0
|
next unless lc($hlist->[$i]) eq $k; |
134
|
0
|
|
|
|
|
0
|
splice(@$hlist, $i, 2); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub request |
139
|
|
|
|
|
|
|
{ |
140
|
1
|
|
|
1
|
1
|
2
|
my($self, $request, $proxy, $arg, $size, $timeout) = @_; |
141
|
1
|
|
|
|
|
4
|
LWP::Debug::trace('()'); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# paranoid: now $timeout means total time, not just between bytes coming in. |
144
|
|
|
|
|
|
|
# avoids attacker servers from tarpitting a service that fetches URLs. |
145
|
1
|
|
|
|
|
4
|
$TOO_LATE = undef; |
146
|
1
|
|
|
|
|
1
|
$TIME_REMAIN = undef; |
147
|
1
|
50
|
|
|
|
4
|
if ($timeout) { |
148
|
1
|
|
33
|
|
|
5
|
my $start_time = $request->{_time_begin} || time(); |
149
|
1
|
|
|
|
|
2
|
$TOO_LATE = $start_time + $timeout; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
1
|
|
50
|
|
|
6
|
$size ||= 4096; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# check method |
155
|
1
|
|
|
|
|
5
|
my $method = $request->method; |
156
|
1
|
50
|
|
|
|
17
|
unless ($method =~ /^[A-Za-z0-9_!\#\$%&\'*+\-.^\`|~]+$/) { # HTTP token |
157
|
0
|
|
|
|
|
0
|
return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST, |
158
|
|
|
|
|
|
|
'Library does not allow method ' . |
159
|
|
|
|
|
|
|
"$method for 'http:' URLs"; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
1
|
|
|
|
|
4
|
my $url = $request->url; |
163
|
1
|
|
|
|
|
7
|
my($host, $port, $fullpath); |
164
|
|
|
|
|
|
|
|
165
|
1
|
|
|
|
|
4
|
$host = $url->host; |
166
|
1
|
|
|
|
|
40
|
$port = $url->port; |
167
|
1
|
|
|
|
|
37
|
$fullpath = $url->path_query; |
168
|
1
|
50
|
|
|
|
17
|
$fullpath = "/$fullpath" unless $fullpath =~ m,^/,; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# connect to remote sites |
171
|
|
|
|
|
|
|
|
172
|
1
|
|
|
|
|
4
|
my $socket = $self->_new_socket($host, $port, $timeout, $request); |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my @h; |
175
|
0
|
|
|
|
|
|
my $request_headers = $request->headers->clone; |
176
|
0
|
|
|
|
|
|
$self->_fixup_header($request_headers, $url, $proxy); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
$request_headers->scan(sub { |
179
|
0
|
|
|
0
|
|
|
my($k, $v) = @_; |
180
|
0
|
|
|
|
|
|
$k =~ s/^://; |
181
|
0
|
|
|
|
|
|
$v =~ s/\n/ /g; |
182
|
0
|
|
|
|
|
|
push(@h, $k, $v); |
183
|
0
|
|
|
|
|
|
}); |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
my $content_ref = $request->content_ref; |
186
|
0
|
0
|
|
|
|
|
$content_ref = $$content_ref if ref($$content_ref); |
187
|
0
|
|
|
|
|
|
my $chunked; |
188
|
|
|
|
|
|
|
my $has_content; |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if (ref($content_ref) eq 'CODE') { |
191
|
0
|
|
|
|
|
|
my $clen = $request_headers->header('Content-Length'); |
192
|
0
|
0
|
|
|
|
|
$has_content++ if $clen; |
193
|
0
|
0
|
|
|
|
|
unless (defined $clen) { |
194
|
0
|
|
|
|
|
|
push(@h, "Transfer-Encoding" => "chunked"); |
195
|
0
|
|
|
|
|
|
$has_content++; |
196
|
0
|
|
|
|
|
|
$chunked++; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
else { |
200
|
|
|
|
|
|
|
# Set (or override) Content-Length header |
201
|
0
|
|
|
|
|
|
my $clen = $request_headers->header('Content-Length'); |
202
|
0
|
0
|
0
|
|
|
|
if (defined($$content_ref) && length($$content_ref)) { |
|
|
0
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
$has_content++; |
204
|
0
|
0
|
0
|
|
|
|
if (!defined($clen) || $clen ne length($$content_ref)) { |
205
|
0
|
0
|
|
|
|
|
if (defined $clen) { |
206
|
0
|
|
|
|
|
|
warn "Content-Length header value was wrong, fixed"; |
207
|
0
|
|
|
|
|
|
hlist_remove(\@h, 'Content-Length'); |
208
|
|
|
|
|
|
|
} |
209
|
0
|
|
|
|
|
|
push(@h, 'Content-Length' => length($$content_ref)); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
elsif ($clen) { |
213
|
0
|
|
|
|
|
|
warn "Content-Length set when there is not content, fixed"; |
214
|
0
|
|
|
|
|
|
hlist_remove(\@h, 'Content-Length'); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
my $req_buf = $socket->format_request($method, $fullpath, @h); |
219
|
|
|
|
|
|
|
#print "------\n$req_buf\n------\n"; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# XXX need to watch out for write timeouts |
222
|
|
|
|
|
|
|
# FIXME_BRAD: make it non-blocking and select during the write |
223
|
|
|
|
|
|
|
{ |
224
|
0
|
|
|
|
|
|
my $n = $socket->syswrite($req_buf, length($req_buf)); |
|
0
|
|
|
|
|
|
|
225
|
0
|
0
|
|
|
|
|
die $! unless defined($n); |
226
|
0
|
0
|
|
|
|
|
die "short write" unless $n == length($req_buf); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
#LWP::Debug::conns($req_buf); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
my($code, $mess, @junk); |
232
|
0
|
|
|
|
|
|
my $drop_connection; |
233
|
|
|
|
|
|
|
|
234
|
0
|
0
|
|
|
|
|
if ($has_content) { |
235
|
0
|
|
|
|
|
|
my $write_wait = 0; |
236
|
0
|
0
|
0
|
|
|
|
$write_wait = 2 |
237
|
|
|
|
|
|
|
if ($request_headers->header("Expect") || "") =~ /100-continue/; |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
my $eof; |
240
|
|
|
|
|
|
|
my $wbuf; |
241
|
0
|
|
|
|
|
|
my $woffset = 0; |
242
|
0
|
0
|
|
|
|
|
if (ref($content_ref) eq 'CODE') { |
243
|
0
|
|
|
|
|
|
my $buf = &$content_ref(); |
244
|
0
|
0
|
|
|
|
|
$buf = "" unless defined($buf); |
245
|
0
|
0
|
|
|
|
|
$buf = sprintf "%x%s%s%s", length($buf), $CRLF, $buf, $CRLF |
246
|
|
|
|
|
|
|
if $chunked; |
247
|
0
|
|
|
|
|
|
$wbuf = \$buf; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
else { |
250
|
0
|
|
|
|
|
|
$wbuf = $content_ref; |
251
|
0
|
|
|
|
|
|
$eof = 1; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
my $fbits = ''; |
255
|
0
|
|
|
|
|
|
vec($fbits, fileno($socket), 1) = 1; |
256
|
|
|
|
|
|
|
|
257
|
0
|
|
|
|
|
|
while ($woffset < length($$wbuf)) { |
258
|
|
|
|
|
|
|
|
259
|
0
|
|
|
|
|
|
my $time_before; |
260
|
|
|
|
|
|
|
|
261
|
0
|
|
|
|
|
|
my $now = time(); |
262
|
0
|
0
|
|
|
|
|
if ($now > $TOO_LATE) { |
263
|
0
|
|
|
|
|
|
die "Request took too long."; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
0
|
|
|
|
|
|
my $sel_timeout = $TOO_LATE - $now; |
267
|
0
|
0
|
|
|
|
|
if ($write_wait) { |
268
|
0
|
|
|
|
|
|
$time_before = time; |
269
|
0
|
0
|
|
|
|
|
$sel_timeout = $write_wait if $write_wait < $sel_timeout; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
|
|
|
my $rbits = $fbits; |
273
|
0
|
0
|
|
|
|
|
my $wbits = $write_wait ? undef : $fbits; |
274
|
0
|
|
|
|
|
|
my $nfound = select($rbits, $wbits, undef, $sel_timeout); |
275
|
0
|
0
|
|
|
|
|
unless (defined $nfound) { |
276
|
0
|
|
|
|
|
|
die "select failed: $!"; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
0
|
0
|
|
|
|
|
if ($write_wait) { |
280
|
0
|
|
|
|
|
|
$write_wait -= time - $time_before; |
281
|
0
|
0
|
|
|
|
|
$write_wait = 0 if $write_wait < 0; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
0
|
0
|
0
|
|
|
|
if (defined($rbits) && $rbits =~ /[^\0]/) { |
285
|
|
|
|
|
|
|
# readable |
286
|
0
|
|
|
|
|
|
my $buf = $socket->_rbuf; |
287
|
|
|
|
|
|
|
|
288
|
0
|
|
|
|
|
|
_set_time_remain(); |
289
|
|
|
|
|
|
|
|
290
|
0
|
|
|
|
|
|
my $n = $socket->sysread($buf, 1024, length($buf)); |
291
|
0
|
0
|
|
|
|
|
unless ($n) { |
292
|
0
|
|
|
|
|
|
die "EOF"; |
293
|
|
|
|
|
|
|
} |
294
|
0
|
|
|
|
|
|
$socket->_rbuf($buf); |
295
|
0
|
0
|
|
|
|
|
if ($buf =~ /\015?\012\015?\012/) { |
296
|
|
|
|
|
|
|
# a whole response present |
297
|
0
|
|
|
|
|
|
($code, $mess, @h) = $socket->read_response_headers(laxed => 1, |
298
|
|
|
|
|
|
|
junk_out => \@junk, |
299
|
|
|
|
|
|
|
); |
300
|
0
|
0
|
|
|
|
|
if ($code eq "100") { |
301
|
0
|
|
|
|
|
|
$write_wait = 0; |
302
|
0
|
|
|
|
|
|
undef($code); |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
else { |
305
|
0
|
|
|
|
|
|
$drop_connection++; |
306
|
0
|
|
|
|
|
|
last; |
307
|
|
|
|
|
|
|
# XXX should perhaps try to abort write in a nice way too |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
} |
310
|
|
|
|
|
|
|
} |
311
|
0
|
0
|
0
|
|
|
|
if (defined($wbits) && $wbits =~ /[^\0]/) { |
312
|
0
|
|
|
|
|
|
my $n = $socket->syswrite($$wbuf, length($$wbuf), $woffset); |
313
|
0
|
0
|
|
|
|
|
unless ($n) { |
314
|
0
|
0
|
|
|
|
|
die "syswrite: $!" unless defined $n; |
315
|
0
|
|
|
|
|
|
die "syswrite: no bytes written"; |
316
|
|
|
|
|
|
|
} |
317
|
0
|
|
|
|
|
|
$woffset += $n; |
318
|
|
|
|
|
|
|
|
319
|
0
|
0
|
0
|
|
|
|
if (!$eof && $woffset >= length($$wbuf)) { |
320
|
|
|
|
|
|
|
# need to refill buffer from $content_ref code |
321
|
0
|
|
|
|
|
|
my $buf = &$content_ref(); |
322
|
0
|
0
|
|
|
|
|
$buf = "" unless defined($buf); |
323
|
0
|
0
|
|
|
|
|
$eof++ unless length($buf); |
324
|
0
|
0
|
|
|
|
|
$buf = sprintf "%x%s%s%s", length($buf), $CRLF, $buf, $CRLF |
325
|
|
|
|
|
|
|
if $chunked; |
326
|
0
|
|
|
|
|
|
$wbuf = \$buf; |
327
|
0
|
|
|
|
|
|
$woffset = 0; |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
0
|
|
|
|
|
|
_set_time_remain(); |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
## Now we connected to host |
336
|
|
|
|
|
|
|
## Check host started to send any data in return |
337
|
0
|
|
|
|
|
|
my $rbits = ''; |
338
|
0
|
|
|
|
|
|
vec($rbits, fileno($socket), 1) = 1; |
339
|
0
|
|
|
|
|
|
my $nfound = select($rbits, undef, undef, $TIME_REMAIN); |
340
|
0
|
0
|
|
|
|
|
die "Headers not came for $TIME_REMAIN sec" unless $nfound; |
341
|
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
_set_time_remain(); |
343
|
|
|
|
|
|
|
|
344
|
0
|
0
|
|
|
|
|
($code, $mess, @h) = $socket->read_response_headers(laxed => 1, junk_out => \@junk) |
345
|
|
|
|
|
|
|
unless $code; |
346
|
0
|
0
|
|
|
|
|
($code, $mess, @h) = $socket->read_response_headers(laxed => 1, junk_out => \@junk) |
347
|
|
|
|
|
|
|
if $code eq "100"; |
348
|
|
|
|
|
|
|
|
349
|
0
|
|
|
|
|
|
my $response = HTTP::Response->new($code, $mess); |
350
|
0
|
|
|
|
|
|
my $peer_http_version = $socket->peer_http_version; |
351
|
0
|
|
|
|
|
|
$response->protocol("HTTP/$peer_http_version"); |
352
|
0
|
|
|
|
|
|
while (@h) { |
353
|
0
|
|
|
|
|
|
my($k, $v) = splice(@h, 0, 2); |
354
|
0
|
|
|
|
|
|
$response->push_header($k, $v); |
355
|
|
|
|
|
|
|
} |
356
|
0
|
0
|
|
|
|
|
$response->push_header("Client-Junk" => \@junk) if @junk; |
357
|
|
|
|
|
|
|
|
358
|
0
|
|
|
|
|
|
$response->request($request); |
359
|
0
|
|
|
|
|
|
$self->_get_sock_info($response, $socket); |
360
|
|
|
|
|
|
|
|
361
|
0
|
0
|
|
|
|
|
if ($method eq "CONNECT") { |
362
|
0
|
|
|
|
|
|
$response->{client_socket} = $socket; # so it can be picked up |
363
|
0
|
|
|
|
|
|
return $response; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
|
366
|
0
|
0
|
|
|
|
|
if (my @te = $response->remove_header('Transfer-Encoding')) { |
367
|
0
|
|
|
|
|
|
$response->push_header('Client-Transfer-Encoding', \@te); |
368
|
|
|
|
|
|
|
} |
369
|
0
|
|
|
|
|
|
$response->push_header('Client-Response-Num', $socket->increment_response_count); |
370
|
|
|
|
|
|
|
|
371
|
0
|
|
|
|
|
|
my $complete; |
372
|
|
|
|
|
|
|
$response = $self->collect($arg, $response, sub { |
373
|
0
|
|
|
0
|
|
|
my $buf = ""; #prevent use of uninitialized value in SSLeay.xs |
374
|
0
|
|
|
|
|
|
my $n; |
375
|
|
|
|
|
|
|
READ: |
376
|
|
|
|
|
|
|
{ |
377
|
0
|
|
|
|
|
|
_set_time_remain(); |
|
0
|
|
|
|
|
|
|
378
|
0
|
|
|
|
|
|
$n = $socket->read_entity_body($buf, $size); |
379
|
0
|
0
|
0
|
|
|
|
redo READ if not defined $n and $! == EAGAIN; |
380
|
0
|
0
|
|
|
|
|
redo READ if $n == -1; |
381
|
0
|
0
|
|
|
|
|
die "Can't read entity body: $!" unless defined $n; |
382
|
0
|
0
|
|
|
|
|
die 'read timeout' unless($TIME_REMAIN - 1); |
383
|
|
|
|
|
|
|
} |
384
|
0
|
0
|
|
|
|
|
$complete++ if !$n; |
385
|
0
|
|
|
|
|
|
return \$buf; |
386
|
0
|
|
|
|
|
|
} ); |
387
|
0
|
0
|
|
|
|
|
$drop_connection++ unless $complete; |
388
|
|
|
|
|
|
|
|
389
|
0
|
|
|
|
|
|
_set_time_remain(); |
390
|
0
|
|
|
|
|
|
@h = $socket->get_trailers; |
391
|
0
|
|
|
|
|
|
while (@h) { |
392
|
0
|
|
|
|
|
|
my($k, $v) = splice(@h, 0, 2); |
393
|
0
|
|
|
|
|
|
$response->push_header($k, $v); |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
# keep-alive support |
397
|
0
|
0
|
|
|
|
|
unless ($drop_connection) { |
398
|
0
|
0
|
|
|
|
|
if (my $conn_cache = $self->{ua}{conn_cache}) { |
399
|
0
|
|
0
|
|
|
|
my %connection = map { (lc($_) => 1) } |
|
0
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
split(/\s*,\s*/, ($response->header("Connection") || "")); |
401
|
0
|
0
|
0
|
|
|
|
if (($peer_http_version eq "1.1" && !$connection{close}) || |
|
|
|
0
|
|
|
|
|
402
|
|
|
|
|
|
|
$connection{"keep-alive"}) |
403
|
|
|
|
|
|
|
{ |
404
|
0
|
|
|
|
|
|
LWP::Debug::debug("Keep the http connection to $host:$port"); |
405
|
0
|
|
|
|
|
|
$conn_cache->deposit("http", "$host:$port", $socket); |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
|
410
|
0
|
|
|
|
|
|
$response; |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
#----------------------------------------------------------- |
415
|
|
|
|
|
|
|
package LWPx::Protocol::http_paranoid::SocketMethods; |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub sysread { |
418
|
0
|
|
|
0
|
|
|
my $self = shift; |
419
|
0
|
|
|
|
|
|
my $timeout = $LWPx::Protocol::http_paranoid::TIME_REMAIN; |
420
|
|
|
|
|
|
|
|
421
|
0
|
0
|
|
|
|
|
if (defined $timeout) { |
422
|
0
|
0
|
|
|
|
|
die "read timeout" unless $self->can_read($timeout); |
423
|
|
|
|
|
|
|
} |
424
|
|
|
|
|
|
|
else { |
425
|
|
|
|
|
|
|
# since we have made the socket non-blocking we |
426
|
|
|
|
|
|
|
# use select to wait for some data to arrive |
427
|
0
|
0
|
|
|
|
|
$self->can_read(undef) || die "Assert"; |
428
|
|
|
|
|
|
|
} |
429
|
0
|
|
0
|
|
|
|
sysread($self, $_[0], $_[1], $_[2] || 0); |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
sub can_read { |
433
|
0
|
|
|
0
|
|
|
my($self, $timeout) = @_; |
434
|
|
|
|
|
|
|
|
435
|
0
|
|
0
|
|
|
|
$timeout ||= $LWPx::Protocol::http_paranoid::TIME_REMAIN; |
436
|
0
|
|
|
|
|
|
my $fbits = ''; |
437
|
0
|
|
|
|
|
|
vec($fbits, fileno($self), 1) = 1; |
438
|
0
|
|
|
|
|
|
my $nfound = select($fbits, undef, undef, $timeout); |
439
|
0
|
0
|
|
|
|
|
die "select failed: $!" unless defined $nfound; |
440
|
0
|
|
|
|
|
|
return $nfound > 0; |
441
|
|
|
|
|
|
|
} |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
sub ping { |
444
|
0
|
|
|
0
|
|
|
my $self = shift; |
445
|
0
|
|
|
|
|
|
!$self->can_read(0); |
446
|
|
|
|
|
|
|
} |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
sub increment_response_count { |
449
|
0
|
|
|
0
|
|
|
my $self = shift; |
450
|
0
|
|
|
|
|
|
return ++${*$self}{'myhttp_response_count'}; |
|
0
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
#----------------------------------------------------------- |
454
|
|
|
|
|
|
|
package LWPx::Protocol::http_paranoid::Socket; |
455
|
1
|
|
|
1
|
|
6
|
use vars qw(@ISA); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
456
|
|
|
|
|
|
|
@ISA = qw(LWPx::Protocol::http_paranoid::SocketMethods Net::HTTP); |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
1; |