… | |
… | |
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.01'; |
53 | our $VERSION = '1.03'; |
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; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
… | |
… | |
235 | while (my ($k, $v) = each %$hdr) { |
235 | while (my ($k, $v) = each %$hdr) { |
236 | $hdr{lc $k} = $v; |
236 | $hdr{lc $k} = $v; |
237 | } |
237 | } |
238 | } |
238 | } |
239 | |
239 | |
240 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
240 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
241 | |
241 | |
242 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached", URL => $url }) |
242 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached", URL => $url }) |
243 | if $recurse < 0; |
243 | if $recurse < 0; |
244 | |
244 | |
245 | my $proxy = $arg{proxy} || $PROXY; |
245 | my $proxy = $arg{proxy} || $PROXY; |
… | |
… | |
412 | $arg{cookie_jar}{version} = 1; |
412 | $arg{cookie_jar}{version} = 1; |
413 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
413 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
414 | } |
414 | } |
415 | } |
415 | } |
416 | |
416 | |
417 | if ($_[1]{Status} =~ /^30[12]$/ && $recurse) { |
|
|
418 | # microsoft and other assholes don't give a shit for following standards, |
417 | # microsoft and other assholes don't give a shit for following standards, |
419 | # try to support a common form of broken Location header. |
418 | # try to support a common form of broken Location header. |
420 | $_[1]{location} =~ s%^/%$scheme://$uhost:$uport/%; |
419 | $_[1]{location} =~ s%^/%$scheme://$uhost:$uport/% |
|
|
420 | if exists $_[1]{location}; |
421 | |
421 | |
|
|
422 | if ($_[1]{Status} =~ /^30[12]$/ && $recurse && $method ne "POST") { |
|
|
423 | # apparently, mozilla et al. just change POST to GET here |
|
|
424 | # more research is needed before we do the same |
422 | http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
425 | http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
|
|
426 | } elsif ($_[1]{Status} == 303 && $recurse) { |
|
|
427 | # even http/1.1 is unlear on how to mutate the method |
|
|
428 | $method = "GET" unless $method eq "HEAD"; |
|
|
429 | http_request ($method => $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
|
|
430 | } elsif ($_[1]{Status} == 307 && $recurse && $method =~ /^(?:GET|HEAD)$/) { |
|
|
431 | http_request ($method => $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
423 | } else { |
432 | } else { |
424 | $cb->($_[0], $_[1]); |
433 | $cb->($_[0], $_[1]); |
425 | } |
434 | } |
426 | }; |
435 | }; |
427 | |
436 | |
… | |
… | |
466 | unshift @_, "HEAD"; |
475 | unshift @_, "HEAD"; |
467 | &http_request |
476 | &http_request |
468 | } |
477 | } |
469 | |
478 | |
470 | sub http_post($$@) { |
479 | sub http_post($$@) { |
|
|
480 | my $url = shift; |
471 | unshift @_, "POST", "body"; |
481 | unshift @_, "POST", $url, "body"; |
472 | &http_request |
482 | &http_request |
473 | } |
483 | } |
474 | |
484 | |
475 | =back |
485 | =back |
476 | |
486 | |