| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
##################################################### |
| 29 |
|
| 30 |
# cluster global variables |
| 31 |
%_crawl_config = ( |
| 32 |
'save_cookies' => 0, |
| 33 |
'reuse_cookies' => 1, |
| 34 |
'save_offsites' => 0, |
| 35 |
'save_non_http' => 0, |
| 36 |
'follow_moves' => 1, |
| 37 |
'url_limit' => 1000, |
| 38 |
'use_params' => 0, |
| 39 |
'params_double_record' => 0, |
| 40 |
'skip_ext' => { |
| 41 |
gif => 1, |
| 42 |
jpg => 1, |
| 43 |
png => 1, |
| 44 |
gz => 1, |
| 45 |
swf => 1, |
| 46 |
pdf => 1, |
| 47 |
zip => 1, |
| 48 |
wav => 1, |
| 49 |
mp3 => 1, |
| 50 |
asf => 1, |
| 51 |
tgz => 1 |
| 52 |
}, |
| 53 |
'save_skipped' => 0, |
| 54 |
'save_referrers' => 0, |
| 55 |
'use_referrers' => 1, |
| 56 |
'do_head' => 0, |
| 57 |
'callback' => 0, |
| 58 |
'netloc_bug' => 1, |
| 59 |
'normalize_uri' => 1, |
| 60 |
'source_callback' => 0 |
| 61 |
); |
| 62 |
|
| 63 |
%_crawl_linktags = ( |
| 64 |
'a' => 'href', |
| 65 |
'applet' => [qw(codebase archive code)], |
| 66 |
'area' => 'href', |
| 67 |
'base' => 'href', |
| 68 |
'bgsound' => 'src', |
| 69 |
'blockquote' => 'cite', |
| 70 |
'body' => 'background', |
| 71 |
'del' => 'cite', |
| 72 |
'embed' => [qw(src pluginspage)], |
| 73 |
'form' => 'action', |
| 74 |
'frame' => [qw(src longdesc)], |
| 75 |
'iframe' => [qw(src longdesc)], |
| 76 |
'ilayer' => 'background', |
| 77 |
'img' => [qw(src lowsrc longdesc usemap)], |
| 78 |
'input' => [qw(src usemap)], |
| 79 |
'ins' => 'cite', |
| 80 |
'isindex' => 'action', |
| 81 |
'head' => 'profile', |
| 82 |
'layer' => [qw(background src)], |
| 83 |
'link' => 'href', |
| 84 |
|
| 85 |
# 'meta' => 'http-equiv', |
| 86 |
'object' => [qw(codebase data archive usemap)], |
| 87 |
'q' => 'cite', |
| 88 |
'script' => 'src', |
| 89 |
'table' => 'background', |
| 90 |
'td' => 'background', |
| 91 |
'th' => 'background', |
| 92 |
'xmp' => 'href', |
| 93 |
); |
| 94 |
|
| 95 |
##################################################### |
| 96 |
|
| 97 |
=item B<crawl_new> |
| 98 |
|
| 99 |
Params: $START, $MAX_DEPTH, \%request_hash [, \%tracking_hash ] |
| 100 |
|
| 101 |
Return: $crawl_object |
| 102 |
|
| 103 |
The crawl_new() functions initializes a crawl object (hash) to the default |
| 104 |
values, and then returns it for later use by crawl(). $START is the starting |
| 105 |
URL (in the form of 'http://www.host.com/url'), and MAX_DEPTH is the maximum |
| 106 |
number of levels to crawl (the START URL counts as 1, so a value of 2 will |
| 107 |
crawl the START URL and all URLs found on that page). The request_hash |
| 108 |
is a standard initialized request hash to be used for requests; you should |
| 109 |
set any authentication information or headers in this hash in order for |
| 110 |
the crawler to use them. The optional tracking_hash lets you supply a |
| 111 |
hash for use in tracking URL results (otherwise crawl_new() will allocate |
| 112 |
a new anon hash). |
| 113 |
|
| 114 |
=cut |
| 115 |
|
| 116 |
sub crawl_new { |
| 117 |
my ( $start, $depth, $reqref, $trackref ) = @_; |
| 118 |
my %X; |
| 119 |
|
| 120 |
return undef if ( !defined $start || !defined $depth ); |
| 121 |
return undef if ( !defined $reqref || !ref($reqref) ); |
| 122 |
$trackref = {} if ( !defined $trackref || !ref($trackref) ); |
| 123 |
|
| 124 |
$X{track} = $trackref; |
| 125 |
$X{request} = $reqref; |
| 126 |
$X{depth} = $depth || 2; |
| 127 |
$X{start} = $start; |
| 128 |
$X{magic} = 7340; |
| 129 |
|
| 130 |
$X{reset} = sub { |
| 131 |
$X{errors} = []; # all errors encountered |
| 132 |
$X{urls} = []; # temp; used to hold all URLs on page |
| 133 |
$X{server_tags} = {}; # all server tags found |
| 134 |
$X{referrers} = {}; # who refers to what URLs |
| 135 |
$X{offsites} = {}; # all URLs that point offsite |
| 136 |
$X{response} = {}; # temp; the response hash |
| 137 |
$X{non_http} = {}; # all non_http URLs found |
| 138 |
$X{cookies} = {}; # all cookies found |
| 139 |
$X{forms} = {}; # all forms found |
| 140 |
$X{jar} = {}; # temp; cookie jar |
| 141 |
$X{url_queue} = []; # temp; URLs to still fetch |
| 142 |
|
| 143 |
$X{config} = {}; |
| 144 |
%{ $X{config} } = %_crawl_config; |
| 145 |
|
| 146 |
%{ $X{track} } = (); |
| 147 |
$X{parsed_page_count} = 0; |
| 148 |
}; |
| 149 |
|
| 150 |
$X{crawl} = sub { crawl( \%X, @_ ) }; |
| 151 |
$X{reset}->(); |
| 152 |
|
| 153 |
return \%X; |
| 154 |
} |
| 155 |
|
| 156 |
##################################################### |
| 157 |
|
| 158 |
=item B<crawl> |
| 159 |
|
| 160 |
Params: $crawl_object [, $START, $MAX_DEPTH ] |
| 161 |
|
| 162 |
Return: $count [ undef on error ] |
| 163 |
|
| 164 |
The heart of the crawl package. Will perform an HTTP crawl on the |
| 165 |
specified HOST, starting at START URI, proceeding up to MAX_DEPTH. |
| 166 |
|
| 167 |
Crawl_object needs to be the variable returned by crawl_new(). You can |
| 168 |
also indirectly call crawl() via the crawl_object itself: |
| 169 |
|
| 170 |
$crawl_object->{crawl}->($START,$MAX_DEPTH) |
| 171 |
|
| 172 |
Returns the number of URLs actually crawled (not including those skipped). |
| 173 |
|
| 174 |
=cut |
| 175 |
|
| 176 |
{ # START OF CRAWL CONTAINER |
| 177 |
|
| 178 |
sub crawl { |
| 179 |
my ( $C, $START, $MAX_DEPTH ) = @_; |
| 180 |
return undef if ( !defined $C || !ref($C) || $C->{magic} != 7340 ); |
| 181 |
|
| 182 |
# shortcuts, to reduce dereferences and typing |
| 183 |
my $CONFIG = $C->{config}; |
| 184 |
my $TRACK = $C->{track}; |
| 185 |
my $URLS = $C->{urls}; |
| 186 |
my $RESP = $C->{response}; |
| 187 |
my $REQ = $C->{request}; |
| 188 |
my $Q = $C->{url_queue}; |
| 189 |
|
| 190 |
$START ||= $C->{start}; |
| 191 |
$C->{depth} = $MAX_DEPTH || $C->{depth}; |
| 192 |
|
| 193 |
my ( $COUNT, $T, @ST ) = ( 0, '' ); |
| 194 |
|
| 195 |
# ST[] = [ 0.HOST, 1.PORT, 2.URL, 3.DEPTH, 4.CWD, 5.REF ] |
| 196 |
|
| 197 |
my @v = uri_split($START); |
| 198 |
|
| 199 |
my $error = undef; |
| 200 |
$error = 'Start protocol not http or https' |
| 201 |
if ( $v[1] ne 'http' && $v[1] ne 'https' ); |
| 202 |
$error = 'Bad start host' if ( !defined $v[2] || $v[2] eq '' ); |
| 203 |
push( @{ $C->{errors} }, $error ) && return undef if ( defined $error ); |
| 204 |
|
| 205 |
@ST = ( $v[2], $v[3], $v[0], 1, '', '' ); |
| 206 |
|
| 207 |
$REQ->{whisker}->{ssl} = 1 if ( $v[1] eq 'https' ); |
| 208 |
$REQ->{whisker}->{host} = $ST[0]; |
| 209 |
$REQ->{whisker}->{port} = $ST[1]; |
| 210 |
$REQ->{whisker}->{lowercase_incoming_headers} = 1; |
| 211 |
$REQ->{whisker}->{ignore_duplicate_headers} = 0; |
| 212 |
delete $REQ->{whisker}->{parameters}; |
| 213 |
http_fixup_request($REQ); |
| 214 |
|
| 215 |
push @$Q, \@ST; |
| 216 |
|
| 217 |
while (@$Q) { |
| 218 |
@ST = @{ shift @$Q }; |
| 219 |
|
| 220 |
next if ( defined $TRACK->{ $ST[2] } && $TRACK->{ $ST[2] } ne '?' ); |
| 221 |
if ( $ST[3] > $C->{depth} ) { |
| 222 |
$TRACK->{ $ST[2] } = '?' if ( $CONFIG->{save_skipped} > 0 ); |
| 223 |
next; |
| 224 |
} |
| 225 |
|
| 226 |
$ST[4] = uri_get_dir( $ST[2] ); |
| 227 |
$REQ->{whisker}->{uri} = $ST[2]; |
| 228 |
if ( $ST[5] ne '' && $CONFIG->{use_referrers} > 0 ) { |
| 229 |
$REQ->{Referrer} = $ST[5]; |
| 230 |
} |
| 231 |
|
| 232 |
my $result = _crawl_do_request( $REQ, $RESP, $C ); |
| 233 |
if ( $result == 1 || $result == 2 ) { |
| 234 |
push @{ $C->{errors} }, "$ST[2]: $RESP->{whisker}->{error}"; |
| 235 |
next; |
| 236 |
} |
| 237 |
|
| 238 |
$COUNT++; |
| 239 |
$TRACK->{ $ST[2] } = $RESP->{whisker}->{code} |
| 240 |
if ( $result == 0 || $result == 4 ); |
| 241 |
$TRACK->{ $ST[2] } = '?' |
| 242 |
if ( ( $result == 3 || $result == 5 ) |
| 243 |
&& $CONFIG->{save_skipped} > 0 ); |
| 244 |
|
| 245 |
if ( defined $RESP->{server} && !ref( $RESP->{server} ) ) { |
| 246 |
$C->{server_tags}->{ $RESP->{server} }++; |
| 247 |
} |
| 248 |
|
| 249 |
if ( defined $RESP->{'set-cookie'} ) { |
| 250 |
if ( $CONFIG->{save_cookies} > 0 ) { |
| 251 |
if ( ref( $RESP->{'set-cookie'} ) ) { |
| 252 |
$C->{cookies}->{$_}++ |
| 253 |
foreach ( @{ $RESP->{'set-cookie'} } ); |
| 254 |
} |
| 255 |
else { |
| 256 |
$C->{cookies}->{ $RESP->{'set-cookie'} }++; |
| 257 |
} |
| 258 |
} |
| 259 |
cookie_read( $C->{jar}, $RESP ) |
| 260 |
if ( $CONFIG->{reuse_cookies} > 0 ); |
| 261 |
} |
| 262 |
|
| 263 |
next if ( $result == 4 || $result == 5 ); |
| 264 |
next if ( scalar @$Q > $CONFIG->{url_limit} ); |
| 265 |
|
| 266 |
if ( $result == 0 ) { # page should be parsed |
| 267 |
if ( $CONFIG->{source_callback} != 0 |
| 268 |
&& ref( $CONFIG->{source_callback} ) eq 'CODE' ) |
| 269 |
{ |
| 270 |
&{ $CONFIG->{source_callback} }($C); |
| 271 |
} |
| 272 |
|
| 273 |
html_find_tags( \$RESP->{whisker}->{data}, |
| 274 |
\&_crawl_extract_links_test, 0, $C, \%_crawl_linktags ); |
| 275 |
$C->{parsed_page_count}++; |
| 276 |
} |
| 277 |
|
| 278 |
push @$URLS, $RESP->{location} if ( $result == 3 ); |
| 279 |
|
| 280 |
foreach $T (@$URLS) { |
| 281 |
$T =~ tr/\0\r\n//d; |
| 282 |
next if ( length($T) == 0 ); |
| 283 |
next if ( $T =~ /^#/i ); # fragment |
| 284 |
|
| 285 |
push @{ $C->{referrers}->{$T} }, $ST[2] |
| 286 |
if ( $CONFIG->{save_referrers} > 0 ); |
| 287 |
|
| 288 |
if ( $T =~ /^([a-zA-Z0-9]*):/ |
| 289 |
&& lc($1) ne 'http' |
| 290 |
&& lc($1) ne 'https' ) |
| 291 |
{ |
| 292 |
push @{ $C->{non_http}->{$T} }, $ST[2] |
| 293 |
if ( $CONFIG->{save_non_http} > 0 ); |
| 294 |
next; |
| 295 |
} |
| 296 |
|
| 297 |
if ( substr( $T, 0, 2 ) eq '//' && $CONFIG->{netloc_bug} > 0 ) { |
| 298 |
if ( $REQ->{whisker}->{ssl} > 0 ) { $T = 'https:' . $T; } |
| 299 |
else { $T = 'http:' . $T; } |
| 300 |
} |
| 301 |
if ( $CONFIG->{callback} != 0 ) { |
| 302 |
next if &{ $CONFIG->{callback} }( $T, $C ); |
| 303 |
} |
| 304 |
|
| 305 |
$T = uri_absolute( $T, $ST[4], $CONFIG->{normalize_uri} ); |
| 306 |
|
| 307 |
# (uri,protocol,host,port,params,frag,user,pass) |
| 308 |
@v = uri_split($T); |
| 309 |
|
| 310 |
# make sure URL is on same host and port |
| 311 |
if ( ( defined $v[2] && $v[2] ne $ST[0] ) |
| 312 |
|| ( $v[3] > 0 && $v[3] != $ST[1] ) ) |
| 313 |
{ |
| 314 |
$C->{offsites}->{ uri_join(@v) }++ |
| 315 |
if ( $CONFIG->{save_offsites} > 0 ); |
| 316 |
next; |
| 317 |
} |
| 318 |
|
| 319 |
if ( $v[0] =~ /\.([a-z0-9]+)$/i ) { |
| 320 |
if ( defined $CONFIG->{skip_ext}->{ lc($1) } ) { |
| 321 |
$TRACK->{ $v[0] } = '?' |
| 322 |
if ( $CONFIG->{save_skipped} > 0 ); |
| 323 |
next; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if ( defined $v[4] && $CONFIG->{use_params} > 0 ) { |
| 328 |
$TRACK->{ $v[0] } = '?' |
| 329 |
if ( $CONFIG->{params_double_record} > 0 |
| 330 |
&& !defined $TRACK->{ $v[0] } ); |
| 331 |
$v[0] = $v[0] . '?' . $v[4]; |
| 332 |
} |
| 333 |
|
| 334 |
next |
| 335 |
if ( defined $TRACK->{ $v[0] } ) |
| 336 |
; # we've processed this already |
| 337 |
|
| 338 |
# ST[] = [ 0.HOST, 1.PORT, 2.URL, 3.DEPTH, 4.CWD, 5.REF ] |
| 339 |
push @$Q, [ $ST[0], $ST[1], $v[0], $ST[3] + 1, '', $ST[2] ]; |
| 340 |
} # foreach |
| 341 |
|
| 342 |
@$URLS = (); # reset for next round |
| 343 |
} # while |
| 344 |
|
| 345 |
return $COUNT; |
| 346 |
} # end sub crawl |
| 347 |
|
| 348 |
##################################################### |
| 349 |
|
| 350 |
sub _crawl_extract_links_test { |
| 351 |
my ( $TAG, $hr, $dr, $start, $len, $OBJ ) = ( lc(shift), @_ ); |
| 352 |
|
| 353 |
return undef if ( !scalar %$hr ); # fastpath quickie |
| 354 |
|
| 355 |
# we know this is defined, due to our tagmap |
| 356 |
my $t = $_crawl_linktags{$TAG}; |
| 357 |
|
| 358 |
while ( my ( $key, $val ) = each %$hr ) { # normalize element values |
| 359 |
$$hr{ lc($key) } = $val; |
| 360 |
} |
| 361 |
|
| 362 |
# all of this just to catch meta refresh URLs |
| 363 |
if ( $TAG eq 'meta' |
| 364 |
&& defined $$hr{'http-equiv'} |
| 365 |
&& $$hr{'http-equiv'} eq 'refresh' |
| 366 |
&& defined $$hr{'content'} |
| 367 |
&& $$hr{'content'} =~ m/url=(.+)/i ) |
| 368 |
{ |
| 369 |
push( @{ $OBJ->{urls} }, $1 ); |
| 370 |
|
| 371 |
} |
| 372 |
elsif ( ref($t) ) { |
| 373 |
foreach (@$t) { |
| 374 |
push( @{ $OBJ->{urls} }, $$hr{$_} ) if ( defined $$hr{$_} ); |
| 375 |
} |
| 376 |
} |
| 377 |
else { |
| 378 |
push( @{ $OBJ->{urls} }, $$hr{$t} ) if ( defined $$hr{$t} ); |
| 379 |
} |
| 380 |
|
| 381 |
if ( $TAG eq 'form' && defined $$hr{action} ) { |
| 382 |
my $u = $OBJ->{response}->{whisker}->{uri}; |
| 383 |
$OBJ->{forms}->{ uri_absolute( $$hr{action}, $u, 1 ) }++; |
| 384 |
} |
| 385 |
|
| 386 |
return undef; |
| 387 |
} |
| 388 |
|
| 389 |
################################################################ |
| 390 |
|
| 391 |
sub _crawl_do_request_ex { |
| 392 |
my ( $hrin, $hrout, $OBJ ) = @_; |
| 393 |
my $ret; |
| 394 |
|
| 395 |
$ret = http_do_request( $hrin, $hrout ); |
| 396 |
|
| 397 |
return ( 2, $ret ) |
| 398 |
if ( $ret == 2 ); # if there was connection error, do not continue |
| 399 |
if ( $ret == 0 ) { # successful request |
| 400 |
|
| 401 |
# WARNING: what if *all* HEAD respones are 302'd on purpose, but |
| 402 |
# all GETs are normal? |
| 403 |
if ( $$hrout{whisker}->{code} < 308 |
| 404 |
&& $$hrout{whisker}->{code} > 300 ) |
| 405 |
{ |
| 406 |
if ( $OBJ->{config}->{follow_moves} > 0 ) { |
| 407 |
return ( 3, $ret ) |
| 408 |
if ( defined $$hrout{location} |
| 409 |
&& !ref( $$hrout{location} ) ); |
| 410 |
} |
| 411 |
return ( 5, $ret ); # not avail |
| 412 |
} |
| 413 |
|
| 414 |
if ( $$hrout{whisker}->{code} == 200 ) { |
| 415 |
|
| 416 |
# no content-type is treated as text/htm |
| 417 |
if ( defined $$hrout{'content-type'} |
| 418 |
&& $$hrout{'content-type'} !~ /^text\/htm/i ) |
| 419 |
{ |
| 420 |
return ( 4, $ret ); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
return ( -1, $ret ); # fallthrough |
| 425 |
} |
| 426 |
|
| 427 |
################################################################ |
| 428 |
|
| 429 |
sub _crawl_do_request { |
| 430 |
my ( $hrin, $hrout, $OBJ ) = @_; |
| 431 |
my ( $cret, $lwret ); |
| 432 |
|
| 433 |
if ( $OBJ->{config}->{do_head} && $$hrin{whisker}->{method} ne 'HEAD' ) |
| 434 |
{ |
| 435 |
my $save = $$hrin{whisker}->{method}; |
| 436 |
$$hrin{whisker}->{method} = 'HEAD'; |
| 437 |
( $cret, $lwret ) = _crawl_do_request_ex( $hrin, $hrout, $OBJ ); |
| 438 |
$$hrin{whisker}->{method} = $save; |
| 439 |
|
| 440 |
return $cret if ( $cret > 0 ); |
| 441 |
|
| 442 |
if ( $lwret == 0 ) { # successful request |
| 443 |
if ( $$hrout{whisker}->{code} == 501 ) { # HEAD not allowed |
| 444 |
$OBJ->{config}->{do_head} = 0; # no more HEAD requests |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
# request errors are essentially redone via GET, below |
| 449 |
} |
| 450 |
|
| 451 |
( $cret, $lwret ) = _crawl_do_request_ex( $hrin, $hrout, $OBJ ); |
| 452 |
return $lwret if ( $cret < 0 ); |
| 453 |
return $cret; |
| 454 |
} |
| 455 |
|
| 456 |
} # CRAWL_CONTAINER |
| 457 |
|
| 458 |
################################################################ |
| 459 |
|