ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/HTTP.pm
(Generate patch)

Comparing AnyEvent-HTTP/HTTP.pm (file contents):
Revision 1.130 by root, Thu Aug 30 01:21:27 2018 UTC vs.
Revision 1.137 by root, Mon Apr 27 12:14:12 2020 UTC

46use AnyEvent::Util (); 46use AnyEvent::Util ();
47use AnyEvent::Handle (); 47use AnyEvent::Handle ();
48 48
49use base Exporter::; 49use base Exporter::;
50 50
51our $VERSION = 2.24; 51our $VERSION = 2.25;
52 52
53our @EXPORT = qw(http_get http_post http_head http_request); 53our @EXPORT = qw(http_get http_post http_head http_request);
54 54
55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
56our $MAX_RECURSE = 10; 56our $MAX_RECURSE = 10;
198C<AnyEvent::HTTP::set_proxy>). 198C<AnyEvent::HTTP::set_proxy>).
199 199
200Currently, if your proxy requires authorization, you have to specify an 200Currently, if your proxy requires authorization, you have to specify an
201appropriate "Proxy-Authorization" header in every request. 201appropriate "Proxy-Authorization" header in every request.
202 202
203Note that this module will prefer an existing persistent connection,
204even if that connection was made using another proxy. If you need to
205ensure that a new connection is made in this case, you can either force
206C<persistent> to false or e.g. use the proxy address in your C<sessionid>.
207
203=item body => $string 208=item body => $string
204 209
205The request body, usually empty. Will be sent as-is (future versions of 210The request body, usually empty. Will be sent as-is (future versions of
206this module might offer more options). 211this module might offer more options).
207 212
239The default for this option is C<low>, which could be interpreted as "give 244The default for this option is C<low>, which could be interpreted as "give
240me the page, no matter what". 245me the page, no matter what".
241 246
242See also the C<sessionid> parameter. 247See also the C<sessionid> parameter.
243 248
244=item session => $string 249=item sessionid => $string
245 250
246The module might reuse connections to the same host internally. Sometimes 251The module might reuse connections to the same host internally (regardless
247(e.g. when using TLS), you do not want to reuse connections from other 252of other settings, such as C<tcp_connect> or C<proxy>). Sometimes (e.g.
253when using TLS or a specfic proxy), you do not want to reuse connections
248sessions. This can be achieved by setting this parameter to some unique 254from other sessions. This can be achieved by setting this parameter to
249ID (such as the address of an object storing your state data, or the TLS 255some unique ID (such as the address of an object storing your state data
250context) - only connections using the same unique ID will be reused. 256or the TLS context, or the proxy IP) - only connections using the same
257unique ID will be reused.
251 258
252=item on_prepare => $callback->($fh) 259=item on_prepare => $callback->($fh)
253 260
254In rare cases you need to "tune" the socket before it is used to 261In rare cases you need to "tune" the socket before it is used to
255connect (for example, to bind it on a given IP address). This parameter 262connect (for example, to bind it on a given IP address). This parameter
263In even rarer cases you want total control over how AnyEvent::HTTP 270In even rarer cases you want total control over how AnyEvent::HTTP
264establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> 271establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect>
265to do this, but you can provide your own C<tcp_connect> function - 272to do this, but you can provide your own C<tcp_connect> function -
266obviously, it has to follow the same calling conventions, except that it 273obviously, it has to follow the same calling conventions, except that it
267may always return a connection guard object. 274may always return a connection guard object.
275
276The connections made by this hook will be treated as equivalent to
277connections made the built-in way, specifically, they will be put into
278and taken from the persistent connection cache. If your C<$tcp_connect>
279function is incompatible with this kind of re-use, consider switching off
280C<persistent> connections and/or providing a C<sessionid> identifier.
268 281
269There are probably lots of weird uses for this function, starting from 282There are probably lots of weird uses for this function, starting from
270tracing the hosts C<http_request> actually tries to connect, to (inexact 283tracing the hosts C<http_request> actually tries to connect, to (inexact
271but fast) host => IP address caching or even socks protocol support. 284but fast) host => IP address caching or even socks protocol support.
272 285
342=item persistent => $boolean 355=item persistent => $boolean
343 356
344Try to create/reuse a persistent connection. When this flag is set 357Try to create/reuse a persistent connection. When this flag is set
345(default: true for idempotent requests, false for all others), then 358(default: true for idempotent requests, false for all others), then
346C<http_request> tries to re-use an existing (previously-created) 359C<http_request> tries to re-use an existing (previously-created)
347persistent connection to the host and, failing that, tries to create a new 360persistent connection to same host (i.e. identical URL scheme, hostname,
348one. 361port and sessionid) and, failing that, tries to create a new one.
349 362
350Requests failing in certain ways will be automatically retried once, which 363Requests failing in certain ways will be automatically retried once, which
351is dangerous for non-idempotent requests, which is why it defaults to off 364is dangerous for non-idempotent requests, which is why it defaults to off
352for them. The reason for this is because the bozos who designed HTTP/1.1 365for them. The reason for this is because the bozos who designed HTTP/1.1
353made it impossible to distinguish between a fatal error and a normal 366made it impossible to distinguish between a fatal error and a normal
354connection timeout, so you never know whether there was a problem with 367connection timeout, so you never know whether there was a problem with
355your request or not. 368your request or not.
356 369
357When reusing an existent connection, many parameters (such as TLS context) 370When reusing an existent connection, many parameters (such as TLS context)
358will be ignored. See the C<session> parameter for a workaround. 371will be ignored. See the C<sessionid> parameter for a workaround.
359 372
360=item keepalive => $boolean 373=item keepalive => $boolean
361 374
362Only used when C<persistent> is also true. This parameter decides whether 375Only used when C<persistent> is also true. This parameter decides whether
363C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection 376C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection
1225# $state{handle}->destroyed 1238# $state{handle}->destroyed
1226# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# 1239# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d#
1227 $prepare_handle->(); 1240 $prepare_handle->();
1228# $state{handle}->destroyed 1241# $state{handle}->destroyed
1229# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# 1242# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d#
1243 $rpath = $upath;
1230 $handle_actual_request->(); 1244 $handle_actual_request->();
1231 1245
1232 } else { 1246 } else {
1233 my $tcp_connect = $arg{tcp_connect} 1247 my $tcp_connect = $arg{tcp_connect}
1234 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; 1248 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect };
1294function from time to time. 1308function from time to time.
1295 1309
1296A cookie jar is initially an empty hash-reference that is managed by this 1310A cookie jar is initially an empty hash-reference that is managed by this
1297module. Its format is subject to change, but currently it is as follows: 1311module. Its format is subject to change, but currently it is as follows:
1298 1312
1299The key C<version> has to contain C<1>, otherwise the hash gets 1313The key C<version> has to contain C<2>, otherwise the hash gets
1300emptied. All other keys are hostnames or IP addresses pointing to 1314cleared. All other keys are hostnames or IP addresses pointing to
1301hash-references. The key for these inner hash references is the 1315hash-references. The key for these inner hash references is the
1302server path for which this cookie is meant, and the values are again 1316server path for which this cookie is meant, and the values are again
1303hash-references. Each key of those hash-references is a cookie name, and 1317hash-references. Each key of those hash-references is a cookie name, and
1304the value, you guessed it, is another hash-reference, this time with the 1318the value, you guessed it, is another hash-reference, this time with the
1305key-value pairs from the cookie, except for C<expires> and C<max-age>, 1319key-value pairs from the cookie, except for C<expires> and C<max-age>,
1309 1323
1310Here is an example of a cookie jar with a single cookie, so you have a 1324Here is an example of a cookie jar with a single cookie, so you have a
1311chance of understanding the above paragraph: 1325chance of understanding the above paragraph:
1312 1326
1313 { 1327 {
1314 version => 1, 1328 version => 2,
1315 "10.0.0.1" => { 1329 "10.0.0.1" => {
1316 "/" => { 1330 "/" => {
1317 "mythweb_id" => { 1331 "mythweb_id" => {
1318 _expires => 1293917923, 1332 _expires => 1293917923,
1319 value => "ooRung9dThee3ooyXooM1Ohm", 1333 value => "ooRung9dThee3ooyXooM1Ohm",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines