… | |
… | |
94 | C<http_request> returns a "cancellation guard" - you have to keep the |
94 | C<http_request> returns a "cancellation guard" - you have to keep the |
95 | object at least alive until the callback get called. If the object gets |
95 | object at least alive until the callback get called. If the object gets |
96 | destroyed before the callback is called, the request will be cancelled. |
96 | destroyed before the callback is called, the request will be cancelled. |
97 | |
97 | |
98 | The callback will be called with the response body data as first argument |
98 | The callback will be called with the response body data as first argument |
99 | (or C<undef> if an error occured), and a hash-ref with response headers as |
99 | (or C<undef> if an error occured), and a hash-ref with response headers |
100 | second argument. |
100 | (and trailers) as second argument. |
101 | |
101 | |
102 | All the headers in that hash are lowercased. In addition to the response |
102 | All the headers in that hash are lowercased. In addition to the response |
103 | headers, the "pseudo-headers" (uppercase to avoid clashing with possible |
103 | headers, the "pseudo-headers" (uppercase to avoid clashing with possible |
104 | response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the |
104 | response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the |
105 | three parts of the HTTP Status-Line of the same name. If an error occurs |
105 | three parts of the HTTP Status-Line of the same name. If an error occurs |
… | |
… | |
150 | Whether to recurse requests or not, e.g. on redirects, authentication |
150 | Whether to recurse requests or not, e.g. on redirects, authentication |
151 | retries and so on, and how often to do so. |
151 | retries and so on, and how often to do so. |
152 | |
152 | |
153 | =item headers => hashref |
153 | =item headers => hashref |
154 | |
154 | |
155 | The request headers to use. Currently, C<http_request> may provide its |
155 | The request headers to use. Currently, C<http_request> may provide its own |
156 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
156 | C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and |
157 | and will provide defaults for C<User-Agent:> and C<Referer:> (this can be |
157 | will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> |
158 | suppressed by using C<undef> for these headers in which case they won't be |
158 | (this can be suppressed by using C<undef> for these headers in which case |
159 | sent at all). |
159 | they won't be sent at all). |
160 | |
160 | |
161 | =item timeout => $seconds |
161 | =item timeout => $seconds |
162 | |
162 | |
163 | The time-out to use for various stages - each connect attempt will reset |
163 | The time-out to use for various stages - each connect attempt will reset |
164 | the timeout, as will read or write activity, i.e. this is not an overall |
164 | the timeout, as will read or write activity, i.e. this is not an overall |
… | |
… | |
174 | C<$scheme> must be either missing, C<http> for HTTP or C<https> for |
174 | C<$scheme> must be either missing, C<http> for HTTP or C<https> for |
175 | HTTPS. |
175 | HTTPS. |
176 | |
176 | |
177 | =item body => $string |
177 | =item body => $string |
178 | |
178 | |
179 | The request body, usually empty. Will be-sent as-is (future versions of |
179 | The request body, usually empty. Will be sent as-is (future versions of |
180 | this module might offer more options). |
180 | this module might offer more options). |
181 | |
181 | |
182 | =item cookie_jar => $hash_ref |
182 | =item cookie_jar => $hash_ref |
183 | |
183 | |
184 | Passing this parameter enables (simplified) cookie-processing, loosely |
184 | Passing this parameter enables (simplified) cookie-processing, loosely |
… | |
… | |
192 | Note that this cookie implementation is not of very high quality, nor |
192 | Note that this cookie implementation is not of very high quality, nor |
193 | meant to be complete. If you want complete cookie management you have to |
193 | meant to be complete. If you want complete cookie management you have to |
194 | do that on your own. C<cookie_jar> is meant as a quick fix to get some |
194 | do that on your own. C<cookie_jar> is meant as a quick fix to get some |
195 | cookie-using sites working. Cookies are a privacy disaster, do not use |
195 | cookie-using sites working. Cookies are a privacy disaster, do not use |
196 | them unless required to. |
196 | them unless required to. |
|
|
197 | |
|
|
198 | When cookie processing is enabled, the C<Cookie:> and C<Set-Cookie:> |
|
|
199 | headers will be ste and handled by this module, otherwise they will be |
|
|
200 | left untouched. |
197 | |
201 | |
198 | =item tls_ctx => $scheme | $tls_ctx |
202 | =item tls_ctx => $scheme | $tls_ctx |
199 | |
203 | |
200 | Specifies the AnyEvent::TLS context to be used for https connections. This |
204 | Specifies the AnyEvent::TLS context to be used for https connections. This |
201 | parameter follows the same rules as the C<tls_ctx> parameter to |
205 | parameter follows the same rules as the C<tls_ctx> parameter to |
… | |
… | |
240 | |
244 | |
241 | This callback is useful, among other things, to quickly reject unwanted |
245 | This callback is useful, among other things, to quickly reject unwanted |
242 | content, which, if it is supposed to be rare, can be faster than first |
246 | content, which, if it is supposed to be rare, can be faster than first |
243 | doing a C<HEAD> request. |
247 | doing a C<HEAD> request. |
244 | |
248 | |
|
|
249 | The downside is that cancelling the request makes it impossible to re-use |
|
|
250 | the connection. Also, the C<on_header> callback will not receive any |
|
|
251 | trailer (headers sent after the response body). |
|
|
252 | |
245 | Example: cancel the request unless the content-type is "text/html". |
253 | Example: cancel the request unless the content-type is "text/html". |
246 | |
254 | |
247 | on_header => sub { |
255 | on_header => sub { |
248 | $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/ |
256 | $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/ |
249 | }, |
257 | }, |
… | |
… | |
255 | string instead of the body data. |
263 | string instead of the body data. |
256 | |
264 | |
257 | It has to return either true (in which case AnyEvent::HTTP will continue), |
265 | It has to return either true (in which case AnyEvent::HTTP will continue), |
258 | or false, in which case AnyEvent::HTTP will cancel the download (and call |
266 | or false, in which case AnyEvent::HTTP will cancel the download (and call |
259 | the completion callback with an error code of C<598>). |
267 | the completion callback with an error code of C<598>). |
|
|
268 | |
|
|
269 | The downside to cancelling the request is that it makes it impossible to |
|
|
270 | re-use the connection. |
260 | |
271 | |
261 | This callback is useful when the data is too large to be held in memory |
272 | This callback is useful when the data is too large to be held in memory |
262 | (so the callback writes it to a file) or when only some information should |
273 | (so the callback writes it to a file) or when only some information should |
263 | be extracted, or when the body should be processed incrementally. |
274 | be extracted, or when the body should be processed incrementally. |
264 | |
275 | |
… | |
… | |
290 | If you think you need this, first have a look at C<on_body>, to see if |
301 | If you think you need this, first have a look at C<on_body>, to see if |
291 | that doesn't solve your problem in a better way. |
302 | that doesn't solve your problem in a better way. |
292 | |
303 | |
293 | =back |
304 | =back |
294 | |
305 | |
295 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
306 | Example: do a simple HTTP GET request for http://www.nethype.de/ and print |
|
|
307 | the response body. |
296 | |
308 | |
297 | http_request GET => "http://www.nethype.de/", sub { |
309 | http_request GET => "http://www.nethype.de/", sub { |
298 | my ($body, $hdr) = @_; |
310 | my ($body, $hdr) = @_; |
299 | print "$body\n"; |
311 | print "$body\n"; |
300 | }; |
312 | }; |
301 | |
313 | |
302 | Example: make a HTTP HEAD request on https://www.google.com/, use a |
314 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
303 | timeout of 30 seconds. |
315 | timeout of 30 seconds. |
304 | |
316 | |
305 | http_request |
317 | http_request |
306 | GET => "https://www.google.com", |
318 | GET => "https://www.google.com", |
307 | timeout => 30, |
319 | timeout => 30, |
… | |
… | |
310 | use Data::Dumper; |
322 | use Data::Dumper; |
311 | print Dumper $hdr; |
323 | print Dumper $hdr; |
312 | } |
324 | } |
313 | ; |
325 | ; |
314 | |
326 | |
315 | Example: make another simple HTTP GET request, but immediately try to |
327 | Example: do another simple HTTP GET request, but immediately try to |
316 | cancel it. |
328 | cancel it. |
317 | |
329 | |
318 | my $request = http_request GET => "http://www.nethype.de/", sub { |
330 | my $request = http_request GET => "http://www.nethype.de/", sub { |
319 | my ($body, $hdr) = @_; |
331 | my ($body, $hdr) = @_; |
320 | print "$body\n"; |
332 | print "$body\n"; |
… | |
… | |
350 | # wait for a free slot on host, call callback |
362 | # wait for a free slot on host, call callback |
351 | sub _get_slot($$) { |
363 | sub _get_slot($$) { |
352 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
364 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
353 | |
365 | |
354 | _slot_schedule $_[0]; |
366 | _slot_schedule $_[0]; |
|
|
367 | } |
|
|
368 | |
|
|
369 | # continue to parse $_ for headers and place them into the arg |
|
|
370 | sub parse_hdr() { |
|
|
371 | my %hdr; |
|
|
372 | |
|
|
373 | # things seen, not parsed: |
|
|
374 | # p3pP="NON CUR OTPi OUR NOR UNI" |
|
|
375 | |
|
|
376 | $hdr{lc $1} .= ",$2" |
|
|
377 | while /\G |
|
|
378 | ([^:\000-\037]*): |
|
|
379 | [\011\040]* |
|
|
380 | ((?: [^\012]+ | \012[\011\040] )*) |
|
|
381 | \012 |
|
|
382 | /gxc; |
|
|
383 | |
|
|
384 | /\G$/ |
|
|
385 | or return; |
|
|
386 | |
|
|
387 | # remove the "," prefix we added to all headers above |
|
|
388 | substr $_, 0, 1, "" |
|
|
389 | for values %hdr; |
|
|
390 | |
|
|
391 | \%hdr |
355 | } |
392 | } |
356 | |
393 | |
357 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
394 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
358 | |
395 | |
359 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
396 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
… | |
… | |
455 | } else { |
492 | } else { |
456 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
493 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
457 | } |
494 | } |
458 | |
495 | |
459 | # leave out fragment and query string, just a heuristic |
496 | # leave out fragment and query string, just a heuristic |
460 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
497 | $hdr{referer} = "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
461 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
498 | $hdr{"user-agent"} = $USERAGENT unless exists $hdr{"user-agent"}; |
462 | |
499 | |
463 | $hdr{"content-length"} = length $arg{body} |
500 | $hdr{"content-length"} = length $arg{body} |
464 | if length $arg{body} || $method ne "GET"; |
501 | if length $arg{body} || $method ne "GET"; |
|
|
502 | |
|
|
503 | $hdr{connection} = "close TE"; #1.1 |
|
|
504 | $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 |
465 | |
505 | |
466 | my %state = (connect_guard => 1); |
506 | my %state = (connect_guard => 1); |
467 | |
507 | |
468 | _get_slot $uhost, sub { |
508 | _get_slot $uhost, sub { |
469 | $state{slot_guard} = shift; |
509 | $state{slot_guard} = shift; |
… | |
… | |
505 | # ++$KA_COUNT{$_[1]}; |
545 | # ++$KA_COUNT{$_[1]}; |
506 | # $state{handle}{ka_count_guard} = AnyEvent::Util::guard { |
546 | # $state{handle}{ka_count_guard} = AnyEvent::Util::guard { |
507 | # --$KA_COUNT{$_[1]} |
547 | # --$KA_COUNT{$_[1]} |
508 | # }; |
548 | # }; |
509 | # $hdr{connection} = "keep-alive"; |
549 | # $hdr{connection} = "keep-alive"; |
510 | # } else { |
|
|
511 | delete $hdr{connection}; |
|
|
512 | # } |
550 | # } |
513 | |
551 | |
514 | $state{handle}->starttls ("connect") if $rscheme eq "https"; |
552 | $state{handle}->starttls ("connect") if $rscheme eq "https"; |
515 | |
553 | |
516 | # handle actual, non-tunneled, request |
554 | # handle actual, non-tunneled, request |
517 | my $handle_actual_request = sub { |
555 | my $handle_actual_request = sub { |
518 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
556 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
519 | |
557 | |
520 | # send request |
558 | # send request |
521 | $state{handle}->push_write ( |
559 | $state{handle}->push_write ( |
522 | "$method $rpath HTTP/1.0\015\012" |
560 | "$method $rpath HTTP/1.1\015\012" |
523 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
561 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
524 | . "\015\012" |
562 | . "\015\012" |
525 | . (delete $arg{body}) |
563 | . (delete $arg{body}) |
526 | ); |
564 | ); |
527 | |
565 | |
… | |
… | |
529 | return unless %state; |
567 | return unless %state; |
530 | |
568 | |
531 | %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use |
569 | %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use |
532 | |
570 | |
533 | # status line and headers |
571 | # status line and headers |
534 | $state{handle}->push_read (line => $qr_nlnl, sub { |
572 | $state{read_response} = sub { |
535 | my $keepalive = pop; |
|
|
536 | |
|
|
537 | for ("$_[1]") { |
573 | for ("$_[1]") { |
538 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
574 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
539 | |
575 | |
540 | /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )? \015?\012/igxc |
576 | /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\012]*) )? \012/igxc |
541 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid server response" })); |
577 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid server response" })); |
|
|
578 | |
|
|
579 | # 100 Continue handling |
|
|
580 | # should not happen as we don't send expect: 100-continue, |
|
|
581 | # but we handle it just in case. |
|
|
582 | # since we send the request body regardless, if we get an error |
|
|
583 | # we are out of-sync, which we currently do NOT handle correctly. |
|
|
584 | return $state{handle}->push_read (line => $qr_nlnl, $state{read_response}) |
|
|
585 | if $2 eq 100; |
542 | |
586 | |
543 | push @pseudo, |
587 | push @pseudo, |
544 | HTTPVersion => $1, |
588 | HTTPVersion => $1, |
545 | Status => $2, |
589 | Status => $2, |
546 | Reason => $3, |
590 | Reason => $3, |
547 | ; |
591 | ; |
548 | |
592 | |
549 | # things seen, not parsed: |
593 | my $hdr = parse_hdr |
550 | # p3pP="NON CUR OTPi OUR NOR UNI" |
|
|
551 | |
|
|
552 | $hdr{lc $1} .= ",$2" |
|
|
553 | while /\G |
|
|
554 | ([^:\000-\037]*): |
|
|
555 | [\011\040]* |
|
|
556 | ((?: [^\012]+ | \012[\011\040] )*) |
|
|
557 | \012 |
|
|
558 | /gxc; |
|
|
559 | |
|
|
560 | /\G$/ |
|
|
561 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Garbled response headers" })); |
594 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Garbled response headers" })); |
|
|
595 | |
|
|
596 | %hdr = (%$hdr, @pseudo); |
562 | } |
597 | } |
563 | |
|
|
564 | # remove the "," prefix we added to all headers above |
|
|
565 | substr $_, 0, 1, "" |
|
|
566 | for values %hdr; |
|
|
567 | |
|
|
568 | # patch in all pseudo headers |
|
|
569 | %hdr = (%hdr, @pseudo); |
|
|
570 | |
598 | |
571 | # redirect handling |
599 | # redirect handling |
572 | # microsoft and other shitheads don't give a shit for following standards, |
600 | # microsoft and other shitheads don't give a shit for following standards, |
573 | # try to support some common forms of broken Location headers. |
601 | # try to support some common forms of broken Location headers. |
574 | if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) { |
602 | if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) { |
… | |
… | |
602 | $redirect = 1; |
630 | $redirect = 1; |
603 | } |
631 | } |
604 | } |
632 | } |
605 | |
633 | |
606 | my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive]) |
634 | my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive]) |
|
|
635 | my $keepalive = pop; |
|
|
636 | |
607 | $state{handle}->destroy if $state{handle}; |
637 | $state{handle}->destroy if $state{handle}; |
608 | %state = (); |
638 | %state = (); |
609 | |
639 | |
610 | if (defined $_[1]) { |
640 | if (defined $_[1]) { |
611 | $hdr{OrigStatus} = $hdr{Status}; $hdr{Status} = $_[1]; |
641 | $hdr{OrigStatus} = $hdr{Status}; $hdr{Status} = $_[1]; |
… | |
… | |
687 | or (defined $len && !$len) |
717 | or (defined $len && !$len) |
688 | ) { |
718 | ) { |
689 | # no body |
719 | # no body |
690 | $finish->("", undef, undef, 1); |
720 | $finish->("", undef, undef, 1); |
691 | } else { |
721 | } else { |
692 | # body handling, four different code paths |
722 | # body handling, many different code paths |
693 | # for want_body_handle, on_body (2x), normal (2x) |
723 | # - no body expected |
694 | # we might read too much here, but it does not matter yet (no pipelining) |
724 | # - want_body_handle |
|
|
725 | # - te chunked |
|
|
726 | # - 2x length known (with or without on_body) |
|
|
727 | # - 2x length not known (with or without on_body) |
695 | if (!$redirect && $arg{want_body_handle}) { |
728 | if (!$redirect && $arg{want_body_handle}) { |
696 | $_[0]->on_eof (undef); |
729 | $_[0]->on_eof (undef); |
697 | $_[0]->on_error (undef); |
730 | $_[0]->on_error (undef); |
698 | $_[0]->on_read (undef); |
731 | $_[0]->on_read (undef); |
699 | |
732 | |
700 | $finish->(delete $state{handle}); |
733 | $finish->(delete $state{handle}); |
701 | |
734 | |
|
|
735 | } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { |
|
|
736 | my $cl = 0; |
|
|
737 | my $body = undef; |
|
|
738 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
|
|
739 | |
|
|
740 | $_[0]->on_error (sub { $finish->(undef, 599 => $_[2]) }); |
|
|
741 | |
|
|
742 | my $read_chunk; $read_chunk = sub { |
|
|
743 | $_[1] =~ /^([0-9a-fA-F]+)/ |
|
|
744 | or $finish->(undef, 599 => "Garbled chunked transfer encoding"); |
|
|
745 | |
|
|
746 | my $len = hex $1; |
|
|
747 | |
|
|
748 | if ($len) { |
|
|
749 | $cl += $len; |
|
|
750 | |
|
|
751 | $_[0]->push_read (chunk => $len, sub { |
|
|
752 | $on_body->($_[1], \%hdr) |
|
|
753 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
|
|
754 | |
|
|
755 | $_[0]->push_read (line => sub { |
|
|
756 | length $_[1] |
|
|
757 | and return $finish->(undef, 599 => "Garbled chunked transfer encoding"); |
|
|
758 | $_[0]->push_read (line => $read_chunk); |
|
|
759 | }); |
|
|
760 | }); |
|
|
761 | } else { |
|
|
762 | $hdr{"content-length"} ||= $cl; |
|
|
763 | |
|
|
764 | $_[0]->push_read (line => $qr_nlnl, sub { |
|
|
765 | if (length $_[1]) { |
|
|
766 | for ("$_[1]") { |
|
|
767 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
|
|
768 | |
|
|
769 | my $hdr = parse_hdr |
|
|
770 | or return $finish->(undef, 599 => "Garbled response trailers"); |
|
|
771 | |
|
|
772 | %hdr = (%hdr, %$hdr); |
|
|
773 | } |
|
|
774 | } |
|
|
775 | |
|
|
776 | $finish->($body, undef, undef, 1); |
|
|
777 | }); |
|
|
778 | } |
|
|
779 | }; |
|
|
780 | |
|
|
781 | $_[0]->push_read (line => $read_chunk); |
|
|
782 | |
702 | } elsif ($arg{on_body}) { |
783 | } elsif ($arg{on_body}) { |
703 | $_[0]->on_error (sub { $finish->(undef, 599 => $_[2]) }); |
784 | $_[0]->on_error (sub { $finish->(undef, 599 => $_[2]) }); |
|
|
785 | |
704 | if ($len) { |
786 | if ($len) { |
705 | $_[0]->on_read (sub { |
787 | $_[0]->on_read (sub { |
706 | $len -= length $_[0]{rbuf}; |
788 | $len -= length $_[0]{rbuf}; |
707 | |
789 | |
708 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
790 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
709 | or $finish->(undef, 598 => "Request cancelled by on_body"); |
791 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
710 | |
792 | |
711 | $len > 0 |
793 | $len > 0 |
712 | or $finish->("", undef, undef, 1); |
794 | or $finish->("", undef, undef, 1); |
713 | }); |
795 | }); |
714 | } else { |
796 | } else { |
… | |
… | |
737 | }); |
819 | }); |
738 | $_[0]->on_read (sub { }); |
820 | $_[0]->on_read (sub { }); |
739 | } |
821 | } |
740 | } |
822 | } |
741 | } |
823 | } |
742 | }); |
824 | }; |
|
|
825 | |
|
|
826 | $state{handle}->push_read (line => $qr_nlnl, $state{read_response}); |
743 | }; |
827 | }; |
744 | |
828 | |
745 | # now handle proxy-CONNECT method |
829 | # now handle proxy-CONNECT method |
746 | if ($proxy && $uscheme eq "https") { |
830 | if ($proxy && $uscheme eq "https") { |
747 | # oh dear, we have to wrap it into a connect request |
831 | # oh dear, we have to wrap it into a connect request |