… | |
… | |
48 | use AnyEvent::Socket (); |
48 | use AnyEvent::Socket (); |
49 | use AnyEvent::Handle (); |
49 | use AnyEvent::Handle (); |
50 | |
50 | |
51 | use base Exporter::; |
51 | use base Exporter::; |
52 | |
52 | |
53 | our $VERSION = '1.4'; |
53 | our $VERSION = '1.41'; |
54 | |
54 | |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
56 | |
56 | |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
… | |
… | |
221 | This callback is useful when the data is too large to be held in memory |
221 | This callback is useful when the data is too large to be held in memory |
222 | (so the callback writes it to a file) or when only some information should |
222 | (so the callback writes it to a file) or when only some information should |
223 | be extracted, or when the body should be processed incrementally. |
223 | be extracted, or when the body should be processed incrementally. |
224 | |
224 | |
225 | It is usually preferred over doing your own body handling via |
225 | It is usually preferred over doing your own body handling via |
226 | C<want_body_handle>. |
226 | C<want_body_handle>, but in case of streaming APIs, where HTTP is |
|
|
227 | only used to create a connection, C<want_body_handle> is the better |
|
|
228 | alternative, as it allows you to install your own event handler, reducing |
|
|
229 | resource usage. |
227 | |
230 | |
228 | =item want_body_handle => $enable |
231 | =item want_body_handle => $enable |
229 | |
232 | |
230 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
233 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
231 | changes considerably: after parsing the headers, and instead of |
234 | changes considerably: after parsing the headers, and instead of |
… | |
… | |
243 | This is useful with some push-type services, where, after the initial |
246 | This is useful with some push-type services, where, after the initial |
244 | headers, an interactive protocol is used (typical example would be the |
247 | headers, an interactive protocol is used (typical example would be the |
245 | push-style twitter API which starts a JSON/XML stream). |
248 | push-style twitter API which starts a JSON/XML stream). |
246 | |
249 | |
247 | If you think you need this, first have a look at C<on_body>, to see if |
250 | If you think you need this, first have a look at C<on_body>, to see if |
248 | that doesn'T solve your problem in a better way. |
251 | that doesn't solve your problem in a better way. |
249 | |
252 | |
250 | =back |
253 | =back |
251 | |
254 | |
252 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
255 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
253 | |
256 | |
… | |
… | |
309 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
312 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
310 | |
313 | |
311 | _slot_schedule $_[0]; |
314 | _slot_schedule $_[0]; |
312 | } |
315 | } |
313 | |
316 | |
314 | our $qr_nl = qr<\015?\012>; |
317 | our $qr_nl = qr{\015?\012}; |
315 | our $qr_nlnl = qr<\015?\012\015?\012>; |
318 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
316 | |
319 | |
317 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
320 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
318 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
321 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
319 | |
322 | |
320 | sub http_request($$@) { |
323 | sub http_request($$@) { |
… | |
… | |
488 | URL => ",$url" |
491 | URL => ",$url" |
489 | ); |
492 | ); |
490 | |
493 | |
491 | # headers, could be optimized a bit |
494 | # headers, could be optimized a bit |
492 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
495 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
493 | for ("$_[1]\012") { |
496 | for ("$_[1]") { |
494 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
497 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
495 | |
498 | |
496 | # things seen, not parsed: |
499 | # things seen, not parsed: |
497 | # p3pP="NON CUR OTPi OUR NOR UNI" |
500 | # p3pP="NON CUR OTPi OUR NOR UNI" |
498 | |
501 | |