… | |
… | |
234 | } |
234 | } |
235 | } |
235 | } |
236 | |
236 | |
237 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
237 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
238 | |
238 | |
239 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached" }) |
239 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached", URL => $url }) |
240 | if $recurse < 0; |
240 | if $recurse < 0; |
241 | |
241 | |
242 | my $proxy = $arg{proxy} || $PROXY; |
242 | my $proxy = $arg{proxy} || $PROXY; |
243 | my $timeout = $arg{timeout} || $TIMEOUT; |
243 | my $timeout = $arg{timeout} || $TIMEOUT; |
244 | |
244 | |
… | |
… | |
249 | |
249 | |
250 | $scheme = lc $scheme; |
250 | $scheme = lc $scheme; |
251 | |
251 | |
252 | my $uport = $scheme eq "http" ? 80 |
252 | my $uport = $scheme eq "http" ? 80 |
253 | : $scheme eq "https" ? 443 |
253 | : $scheme eq "https" ? 443 |
254 | : return $cb->(undef, { Status => 599, Reason => "only http and https URL schemes supported" }); |
254 | : return $cb->(undef, { Status => 599, Reason => "only http and https URL schemes supported", URL => $url }); |
255 | |
255 | |
256 | $hdr{referer} ||= "$scheme://$authority$upath"; # leave out fragment and query string, just a heuristic |
256 | $hdr{referer} ||= "$scheme://$authority$upath"; # leave out fragment and query string, just a heuristic |
257 | |
257 | |
258 | $authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
258 | $authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
259 | or return $cb->(undef, { Status => 599, Reason => "unparsable URL" }); |
259 | or return $cb->(undef, { Status => 599, Reason => "unparsable URL", URL => $url }); |
260 | |
260 | |
261 | my $uhost = $1; |
261 | my $uhost = $1; |
262 | $uport = $2 if defined $2; |
262 | $uport = $2 if defined $2; |
263 | |
263 | |
264 | $uhost =~ s/^\[(.*)\]$/$1/; |
264 | $uhost =~ s/^\[(.*)\]$/$1/; |
… | |
… | |
309 | |
309 | |
310 | return unless $state{connect_guard}; |
310 | return unless $state{connect_guard}; |
311 | |
311 | |
312 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
312 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
313 | $state{fh} = shift |
313 | $state{fh} = shift |
314 | or return $cb->(undef, { Status => 599, Reason => "$!" }); |
314 | or return $cb->(undef, { Status => 599, Reason => "$!", URL => $url }); |
315 | |
315 | |
316 | delete $state{connect_guard}; # reduce memory usage, save a tree |
316 | delete $state{connect_guard}; # reduce memory usage, save a tree |
317 | |
317 | |
318 | # get handle |
318 | # get handle |
319 | $state{handle} = new AnyEvent::Handle |
319 | $state{handle} = new AnyEvent::Handle |
… | |
… | |
333 | # (re-)configure handle |
333 | # (re-)configure handle |
334 | $state{handle}->timeout ($timeout); |
334 | $state{handle}->timeout ($timeout); |
335 | $state{handle}->on_error (sub { |
335 | $state{handle}->on_error (sub { |
336 | my $errno = "$!"; |
336 | my $errno = "$!"; |
337 | %state = (); |
337 | %state = (); |
338 | $cb->(undef, { Status => 599, Reason => $errno }); |
338 | $cb->(undef, { Status => 599, Reason => $errno, URL => $url }); |
339 | }); |
339 | }); |
340 | $state{handle}->on_eof (sub { |
340 | $state{handle}->on_eof (sub { |
341 | %state = (); |
341 | %state = (); |
342 | $cb->(undef, { Status => 599, Reason => "unexpected end-of-file" }); |
342 | $cb->(undef, { Status => 599, Reason => "unexpected end-of-file", URL => $url }); |
343 | }); |
343 | }); |
344 | |
344 | |
345 | # send request |
345 | # send request |
346 | $state{handle}->push_write ( |
346 | $state{handle}->push_write ( |
347 | "$method $rpath HTTP/1.0\015\012" |
347 | "$method $rpath HTTP/1.0\015\012" |
… | |
… | |
353 | %hdr = (); # reduce memory usage, save a kitten |
353 | %hdr = (); # reduce memory usage, save a kitten |
354 | |
354 | |
355 | # status line |
355 | # status line |
356 | $state{handle}->push_read (line => qr/\015?\012/, sub { |
356 | $state{handle}->push_read (line => qr/\015?\012/, sub { |
357 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) \s+ ([^\015\012]+)/ix |
357 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) \s+ ([^\015\012]+)/ix |
358 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])" })); |
358 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])", URL => $url })); |
359 | |
359 | |
360 | my %hdr = ( # response headers |
360 | my %hdr = ( # response headers |
361 | HTTPVersion => "\x00$1", |
361 | HTTPVersion => "\x00$1", |
362 | Status => "\x00$2", |
362 | Status => "\x00$2", |
363 | Reason => "\x00$3", |
363 | Reason => "\x00$3", |
|
|
364 | URL => "\x00$url" |
364 | ); |
365 | ); |
365 | |
366 | |
366 | # headers, could be optimized a bit |
367 | # headers, could be optimized a bit |
367 | $state{handle}->unshift_read (line => qr/\015?\012\015?\012/, sub { |
368 | $state{handle}->unshift_read (line => qr/\015?\012\015?\012/, sub { |
368 | for ("$_[1]\012") { |
369 | for ("$_[1]\012") { |
… | |
… | |
375 | ((?: [^\015\012]+ | \015?\012[\011\040] )*) |
376 | ((?: [^\015\012]+ | \015?\012[\011\040] )*) |
376 | \015?\012 |
377 | \015?\012 |
377 | /gxc; |
378 | /gxc; |
378 | |
379 | |
379 | /\G$/ |
380 | /\G$/ |
380 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "garbled response headers" })); |
381 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "garbled response headers", URL => $url })); |
381 | } |
382 | } |
382 | |
383 | |
383 | substr $_, 0, 1, "" |
384 | substr $_, 0, 1, "" |
384 | for values %hdr; |
385 | for values %hdr; |
385 | |
386 | |