| 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 |
=item B<html_find_tags> |
| 31 |
|
| 32 |
Params: \$data, \&callback_function [, $xml_flag, $funcref, \%tag_map] |
| 33 |
|
| 34 |
Return: nothing |
| 35 |
|
| 36 |
html_find_tags parses a piece of HTML and 'extracts' all found tags, |
| 37 |
passing the info to the given callback function. The callback function |
| 38 |
must accept two parameters: the current tag (as a scalar), and a hash ref |
| 39 |
of all the tag's elements. For example, the tag <a href="/file"> will |
| 40 |
pass 'a' as the current tag, and a hash reference which contains |
| 41 |
{'href'=>"/file"}. |
| 42 |
|
| 43 |
The xml_flag, when set, causes the parser to do some extra processing |
| 44 |
and checks to accomodate XML style tags such as <tag foo="bar"/>. |
| 45 |
|
| 46 |
The optional %tagmap is a hash of lowercase tag names. If a tagmap is |
| 47 |
supplied, then the parser will only call the callback function if the |
| 48 |
tag name exists in the tagmap. |
| 49 |
|
| 50 |
The optional $funcref variable is passed straight to the callback |
| 51 |
function, allowing you to pass flags or references to more complex |
| 52 |
structures to your callback function. |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
{ # contained variables |
| 57 |
$DR = undef; # data reference |
| 58 |
$c = 0; # parser pointer |
| 59 |
$LEN = 0; |
| 60 |
|
| 61 |
sub html_find_tags { |
| 62 |
my ( $dataref, $callbackfunc, $xml, $fref, $tagmap ) = @_; |
| 63 |
|
| 64 |
return if ( !( defined $dataref && ref($dataref) ) ); |
| 65 |
return if ( !( defined $callbackfunc && ref($callbackfunc) ) ); |
| 66 |
$xml ||= 0; |
| 67 |
|
| 68 |
my ( $INTAG, $CURTAG, $LCCURTAG, $ELEMENT, $VALUE, $cc ) = (0); |
| 69 |
my ( %TAG, $ret, $start, $tagstart, $tempstart, $x, $found ); |
| 70 |
my $usetagmap = ( ( defined $tagmap && ref($tagmap) ) ? 1 : 0 ); |
| 71 |
$CURTAG = $LCCURTAG = $ELEMENT = $VALUE = $cc = ''; |
| 72 |
$DR = $dataref; |
| 73 |
|
| 74 |
$LEN = length($$dataref); |
| 75 |
for ( $c = 0 ; $c < $LEN ; $c++ ) { |
| 76 |
|
| 77 |
$cc = substr( $$dataref, $c, 1 ); |
| 78 |
next if ( !$INTAG && $cc ne '>' && $cc ne '<' ); |
| 79 |
|
| 80 |
if ( $cc eq '<' ) { |
| 81 |
if ($INTAG) { |
| 82 |
|
| 83 |
# we're already in a tag... |
| 84 |
# we trick the parser into thinking we end cur tag |
| 85 |
$cc = '>'; |
| 86 |
$c--; |
| 87 |
|
| 88 |
} |
| 89 |
elsif ($xml |
| 90 |
&& $LEN > ( $c + 9 ) |
| 91 |
&& substr( $$dataref, $c + 1, 8 ) eq '![CDATA[' ) |
| 92 |
{ |
| 93 |
$c += 9; |
| 94 |
$tempstart = $c; |
| 95 |
$found = index( $$dataref, ']]>', $c ); |
| 96 |
$c = $found + 2; |
| 97 |
$c = $LEN if ( $found < 0 ); # malformed XML |
| 98 |
# what to do with CDATA? |
| 99 |
next; |
| 100 |
|
| 101 |
} |
| 102 |
elsif ( $LEN > ( $c + 3 ) |
| 103 |
&& substr( $$dataref, $c + 1, 3 ) eq '!--' ) |
| 104 |
{ |
| 105 |
$tempstart = $c; |
| 106 |
$c += 4; |
| 107 |
$found = index( $$dataref, '-->', $c ); |
| 108 |
if ( $found < 0 ) { |
| 109 |
$found = index( $$dataref, '>', $c ); |
| 110 |
$found = $LEN if ( $found < 0 ); |
| 111 |
$c = $found; |
| 112 |
} |
| 113 |
else { |
| 114 |
$c = $found + 2; |
| 115 |
} |
| 116 |
if ( $usetagmap == 0 || defined $tagmap->{'!--'} ) { |
| 117 |
my $dat = substr( |
| 118 |
$$dataref, |
| 119 |
$tempstart + 4, |
| 120 |
$found - $tempstart - 4 |
| 121 |
); |
| 122 |
&$callbackfunc( '!--', { '=' => $dat }, |
| 123 |
$dataref, $tempstart, $c - $tempstart + 1, $fref ); |
| 124 |
} |
| 125 |
next; |
| 126 |
|
| 127 |
} |
| 128 |
elsif ( !$INTAG ) { |
| 129 |
next if ( substr( $$dataref, $c + 1, 1 ) =~ tr/ \t\r\n// ); |
| 130 |
$c++; |
| 131 |
$INTAG = 1; |
| 132 |
$tagstart = $c - 1; |
| 133 |
|
| 134 |
$CURTAG = ''; |
| 135 |
while ( $c < $LEN |
| 136 |
&& ( $x = substr( $$dataref, $c, 1 ) ) !~ |
| 137 |
tr/ \t\r\n>=// ) |
| 138 |
{ |
| 139 |
$CURTAG .= $x; |
| 140 |
$c++; |
| 141 |
} |
| 142 |
|
| 143 |
chop $CURTAG if ( $xml && substr( $CURTAG, -1, 1 ) eq '/' ); |
| 144 |
$c++ if ( defined $x && $x ne '>' ); |
| 145 |
|
| 146 |
$LCCURTAG = lc($CURTAG); |
| 147 |
$INTAG = 0 if ( $LCCURTAG !~ tr/a-z0-9// ); |
| 148 |
next if ( $c >= $LEN ); |
| 149 |
$cc = substr( $$dataref, $c, 1 ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if ( $cc eq '>' ) { |
| 154 |
next if ( !$INTAG ); |
| 155 |
if ( $LCCURTAG eq 'script' && !$xml ) { |
| 156 |
$tempstart = $c + 1; |
| 157 |
pos($$dataref) = $c; |
| 158 |
if ( $$dataref !~ m#(</script.*?>)#ig ) { |
| 159 |
|
| 160 |
# what to do if closing script not found? |
| 161 |
# right now, we'll just leave the tag alone; |
| 162 |
# this won't affect the 'absorption' of the |
| 163 |
# javascript code (and thus, affect parsing) |
| 164 |
} |
| 165 |
else { |
| 166 |
$c = pos($$dataref) - 1; |
| 167 |
my $l = length($1); |
| 168 |
$TAG{'='} = |
| 169 |
substr( $$dataref, $tempstart, |
| 170 |
$c - $tempstart - $l + 1 ); |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
elsif ( $LCCURTAG eq 'textarea' && !$xml ) { |
| 175 |
$tempstart = $c + 1; |
| 176 |
pos($$dataref) = $c; |
| 177 |
if ( $$dataref !~ m#(</textarea.*?>)#ig ) { |
| 178 |
|
| 179 |
# no closing textarea... |
| 180 |
} |
| 181 |
else { |
| 182 |
$c = pos($$dataref) - 1; |
| 183 |
my $l = length($1); |
| 184 |
$TAG{'='} = |
| 185 |
substr( $$dataref, $tempstart, |
| 186 |
$c - $tempstart - $l + 1 ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
$INTAG = 0; |
| 191 |
$TAG{'/'}++ |
| 192 |
if ( $xml && substr( $$dataref, $c - 1, 1 ) eq '/' ); |
| 193 |
&$callbackfunc( $CURTAG, \%TAG, $dataref, $tagstart, |
| 194 |
$c - $tagstart + 1, $fref ) |
| 195 |
if ( $usetagmap == 0 || defined $tagmap->{$LCCURTAG} ); |
| 196 |
$CURTAG = $LCCURTAG = ''; |
| 197 |
%TAG = (); |
| 198 |
next; |
| 199 |
} |
| 200 |
|
| 201 |
if ($INTAG) { |
| 202 |
$ELEMENT = ''; |
| 203 |
$VALUE = undef; |
| 204 |
|
| 205 |
# eat whitespace |
| 206 |
pos($$dataref) = $c; |
| 207 |
if ( $$dataref !~ m/[^ \t\r\n]/g ) { |
| 208 |
$c = $LEN; |
| 209 |
next; # should we really abort? |
| 210 |
} |
| 211 |
$start = pos($$dataref) - 1; |
| 212 |
|
| 213 |
if ( $$dataref !~ m/[ \t\r\n<>=]/g ) { |
| 214 |
$c = $LEN; |
| 215 |
next; # should we really abort? |
| 216 |
} |
| 217 |
$c = pos($$dataref) - 1; |
| 218 |
|
| 219 |
if ( $c > $start ) { |
| 220 |
$ELEMENT = substr( $$dataref, $start, $c - $start ); |
| 221 |
chop $ELEMENT |
| 222 |
if ( $xml && substr( $ELEMENT, -1, 1 ) eq '/' ); |
| 223 |
} |
| 224 |
|
| 225 |
$cc = substr( $$dataref, $c, 1 ); |
| 226 |
if ( $cc ne '>' ) { |
| 227 |
|
| 228 |
# eat whitespace |
| 229 |
if ( $cc =~ tr/ \t\r\n// ) { |
| 230 |
$c++ |
| 231 |
while ( substr( $$dataref, $c, 1 ) =~ tr/ \t\r\n// ); |
| 232 |
} |
| 233 |
|
| 234 |
if ( substr( $$dataref, $c, 1 ) eq '=' ) { |
| 235 |
$c++; |
| 236 |
$start = $c; |
| 237 |
my $p = substr( $$dataref, $c, 1 ); |
| 238 |
if ( $p eq '"' || $p eq '\'' ) { |
| 239 |
$c++; |
| 240 |
$start++; |
| 241 |
$c = index( $$dataref, $p, $c ); |
| 242 |
if ( $c < 0 ) { $c = $LEN; next; } # Bad HTML |
| 243 |
$VALUE = substr( $$dataref, $start, $c - $start ); |
| 244 |
$c++; |
| 245 |
pos($$dataref) = $c; |
| 246 |
} |
| 247 |
else { |
| 248 |
pos($$dataref) = $c; |
| 249 |
if ( $$dataref !~ /[ \t\r\n>]/g ) { |
| 250 |
$c = $LEN; |
| 251 |
} |
| 252 |
else { |
| 253 |
$c = pos($$dataref) - 1; |
| 254 |
$VALUE = |
| 255 |
substr( $$dataref, $start, $c - $start ); |
| 256 |
chop $VALUE |
| 257 |
if ( $xml |
| 258 |
&& substr( $$dataref, $c - 1, 2 ) eq '/>' ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( substr( $$dataref, $c, 1 ) =~ tr/ \t\r\n// ) { |
| 263 |
if ( $$dataref !~ /[^ \t\r\n]/g ) { |
| 264 |
$c = $LEN; |
| 265 |
next; # should we really abort? |
| 266 |
} |
| 267 |
$c = pos($$dataref) - 1; |
| 268 |
} |
| 269 |
} |
| 270 |
} # if $c ne '>' |
| 271 |
$c--; |
| 272 |
$TAG{$ELEMENT} = $VALUE |
| 273 |
if ( $ELEMENT ne '' || ( $xml && $ELEMENT ne '/' ) ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
# finish off any tags we had going |
| 278 |
if ($INTAG) { |
| 279 |
&$callbackfunc( $CURTAG, \%TAG, $dataref, $tagstart, |
| 280 |
$c - $tagstart + 1, $fref ) |
| 281 |
if ( $usetagmap == 0 || defined $tagmap->{$LCCURTAG} ); |
| 282 |
} |
| 283 |
|
| 284 |
$DR = undef; # void dataref pointer |
| 285 |
} |
| 286 |
|
| 287 |
################################################################ |
| 288 |
|
| 289 |
=item B<html_find_tags_rewrite> |
| 290 |
|
| 291 |
Params: $position, $length, $replacement |
| 292 |
|
| 293 |
Return: nothing |
| 294 |
|
| 295 |
html_find_tags_rewrite() is used to 'rewrite' an HTML stream from |
| 296 |
within an html_find_tags() callback function. In general, you can |
| 297 |
think of html_find_tags_rewrite working as: |
| 298 |
|
| 299 |
substr(DATA, $position, $length) = $replacement |
| 300 |
|
| 301 |
Where DATA is the current HTML string the html parser is using. |
| 302 |
The reason you need to use this function and not substr() is |
| 303 |
because a few internal parser pointers and counters need to be |
| 304 |
adjusted to accomodate the changes. |
| 305 |
|
| 306 |
If you want to remove a piece of the string, just set the |
| 307 |
replacement to an empty string (''). If you wish to insert a |
| 308 |
string instead of overwrite, just set $length to 0; your string |
| 309 |
will be inserted at the indicated $position. |
| 310 |
|
| 311 |
=cut |
| 312 |
|
| 313 |
sub html_find_tags_rewrite { |
| 314 |
return if ( !defined $DR ); |
| 315 |
my ( $pos, $len, $replace_str ) = @_; |
| 316 |
|
| 317 |
# replace the data |
| 318 |
substr( $$DR, $pos, $len ) = $replace_str; |
| 319 |
|
| 320 |
# adjust pointer and length |
| 321 |
my $l = ( length($replace_str) - $len ); |
| 322 |
$c += $l; |
| 323 |
$LEN += $l; |
| 324 |
} |
| 325 |
|
| 326 |
################################################################ |
| 327 |
|
| 328 |
sub _html_find_tags_adjust { |
| 329 |
my ( $p, $l ) = @_; |
| 330 |
$c += $p; |
| 331 |
$LEN += $l; |
| 332 |
} |
| 333 |
} # end container |
| 334 |
|
| 335 |
################################################################ |
| 336 |
|
| 337 |
=item B<html_link_extractor> |
| 338 |
|
| 339 |
Params: \$html_data |
| 340 |
|
| 341 |
Return: @urls |
| 342 |
|
| 343 |
The html_link_extractor() function uses the internal crawl tests to |
| 344 |
extract all the HTML links from the given HTML data stream. |
| 345 |
|
| 346 |
Note: html_link_extractor() does not unique the returned array of |
| 347 |
discovered links, nor does it attempt to remove javascript links |
| 348 |
or make the links absolute. It just extracts every raw link from |
| 349 |
the HTML stream and returns it. You'll have to do your own |
| 350 |
post-processing. |
| 351 |
|
| 352 |
=cut |
| 353 |
|
| 354 |
sub html_link_extractor { |
| 355 |
my $data = shift; |
| 356 |
my $ptr; |
| 357 |
if ( ref($data) ) { |
| 358 |
$ptr = $data; |
| 359 |
} |
| 360 |
else { |
| 361 |
$ptr = \$data; |
| 362 |
} |
| 363 |
|
| 364 |
# emulate the crawl object parts we need |
| 365 |
my %OBJ = ( urls => [], forms => {} ); |
| 366 |
$OBJ{response} = {}; |
| 367 |
$OBJ{response}->{whisker} = {}; |
| 368 |
$OBJ{response}->{whisker}->{uri} = ''; |
| 369 |
|
| 370 |
html_find_tags( |
| 371 |
$ptr, # data |
| 372 |
\&_crawl_extract_links_test, # callback function |
| 373 |
0, # xml mode |
| 374 |
\%OBJ, # data object |
| 375 |
\%_crawl_linktags |
| 376 |
); # tagmap |
| 377 |
|
| 378 |
return @{ $OBJ{urls} }; |
| 379 |
} |
| 380 |
|
| 381 |
################################################################ |
| 382 |
|