/usr/share/perl5/WWW/IndexParser.pm is in libwww-indexparser-perl 0.91-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | package WWW::IndexParser;
use warnings;
use strict;
use LWP::UserAgent;
use HTML::Parser;
use Time::Local;
use WWW::IndexParser::Entry;
use URI;
use Carp;
BEGIN {
our $VERSION = "0.91";
}
our $months = {
Jan => 0, January => 0,
Feb => 1, February => 1,
Mar => 2, March => 2,
Apr => 3, April => 3,
May => 4,
Jun => 5, June => 5,
Jul => 6, July => 6,
Aug => 7, August => 7,
Sep => 8, September => 8,
Oct => 9, October => 9,
Nov => 10, November => 10,
Dec => 11, December => 11 };
sub new {
my $proto = shift;
my $class = ref($proto)||$proto;
my $self = {};
bless $self, $class;
my %args = @_;
$self->{ua} = LWP::UserAgent->new;
$self->{ua}->agent('PerlIndexParser/0.1');
if (defined $args{timeout}) {
if ($args{timeout} =~ /^\d+/) {
$self->{ua}->timeout($args{timeout});
} else {
carp "Invalid timeout: " . $args{timeout};
return;
}
} else {
$self->{ua}->timeout(10);
}
if (defined $args{proxy}) {
$self->{ua}->proxy('http', $args{proxy});
}
if (defined $args{debug}) {
$self->{debug} = $args{debug};
} else {
$self->{debug} = 0;
}
$self->{parser} = HTML::Parser->new( api_version => 3);
$self->{parser}->{debug} = $self->{debug};
if (defined $args{url}) {
$self->_url($args{url});
return @{$self->{files}} if defined $self->{files};
return;
}
return $self;
}
sub _url {
my $self = shift;
if (@_) {
my $new_url = shift;
warn "The URL was $new_url" if $self->{debug};
$self->{url} = $new_url;
if ($new_url =~ /^([^:]+):\/\/([^:\/]+)(:(\d+))?/) {
$self->{server} = $2;
$self->{protocol} = $1;
$self->{port} = $4 if defined $4;
$self->{req} = HTTP::Request->new(GET => $new_url);
$self->{res} = $self->{ua}->request($self->{req});
if (not $self->{res}->is_success) {
carp "Cannot fetch for $new_url: " . $self->{res}->status_line if $self->{debug};
return;
}
} else {
warn "Invalid URL " . $new_url;
return;
}
if (ref($self->{res}->headers->{'content-type'}) eq "ARRAY") {
my $found_html = 0;
foreach (@{$self->{res}->headers->{'content-type'}}) {
$found_html = 1 if /^text\/html/;
}
if (not $found_html) {
warn "Not an HTML page " . $self->{res}->headers->{'content-type'};
return;
}
} elsif ($self->{res}->headers->{'content-type'} !~ /^text\/html/) {
warn "Not an HTML page " . $self->{res}->headers->{'content-type'};
return;
}
if ($self->{res}->headers->{server} =~ /^Apache-Coyote/) {
warn "Server is Tomcat Coyote" if $self->{debug};
$self->{parser}->handler( start => \&_parse_html_tomcat, "self, tagname, attr, attrseq, text");
$self->{parser}->handler( text => \&_parse_html_tomcat, "self, tagname, attr, attrseq, text");
} elsif ($self->{res}->headers->{server} =~ /^Apache/) {
warn "Server is Apache" if $self->{debug};
$self->{parser}->handler( start => \&_parse_html_apache, "self, tagname, attr, attrseq, text");
$self->{parser}->handler( text => \&_parse_html_apache, "self, tagname, attr, attrseq, text");
} elsif ($self->{res}->headers->{server} =~ /^Microsoft-IIS/) {
warn "Server is IIS" if $self->{debug};
$self->{parser}->handler( start => \&_parse_html_iis, "self, tagname, attr, attrseq, text");
$self->{parser}->handler( text => \&_parse_html_iis, "self, tagname, attr, attrseq, text");
} elsif ($self->{res}->headers->{server} =~ m!^lighttpd/!) {
warn "Server is lighttpd" if $self->{debug};
$self->{parser}->handler( start => \&_parse_html_lighttpd, "self, tagname, attr, attrseq, text");
$self->{parser}->handler( text => \&_parse_html_lighttpd, "self, tagname, attr, attrseq, text");
} else {
warn "Unknown web server" if $self->{debug};
return;
}
$self->{parser}->parse($self->{res}->content);
$self->{parser}->eof();
# Add the URL to each HASH for ease of use
foreach my $entry (@{$self->{parser}->{files}}) {
if ($entry->filename =~ /^\//) {
$entry->url($self->{protocol} . "://" . $self->{server} .
(defined $self->{port}?':' . $self->{port}:'') .
$entry->filename);
} else {
$entry->url( URI->new_abs($entry->filename, $self->{url}) );
}
}
# Get this back from the parser object.
$self->{files} = $self->{parser}->{files};
}
return $self->{url};
}
sub _parse_html_tomcat {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if (not defined $tagname) {
return unless $self->{parser_state};
if ($self->{parser_state} == 2) {
warn "The title is: $origtext" if $self->{debug};
if ($origtext =~ /^Directory Listing For (.+)$/) {
$self->{directory} = $1;
}
$self->{parser_state} = 1;
return;
}
if ($self->{parser_state} == 1 && $origtext =~ /^([\d\.]+)(\s+(\w+))?/) {
$self->{current_file}->{size} = $1;
$self->{current_file}->{size_units} = $3 if defined $3;
}
if ($self->{parser_state} == 1 && $origtext =~ /^\w+,\s+(\d+)\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\w+)/) {
my $time = timelocal($6, $5, $4, $1, $months->{$2}, $3-1900);
$self->{current_file}->{time} = $time;
}
} elsif ($tagname eq 'title') {
$self->{parser_state} = 2;
} elsif ($tagname eq "hr" && $self->{parser_state} && defined $self->{current_file}) {
my $entry = WWW::IndexParser::Entry->new;
$entry->filename($self->{current_file}->{filename}) if defined $self->{current_file}->{filename};
$entry->time($self->{current_file}->{time}) if defined $self->{current_file}->{time};
$entry->size($self->{current_file}->{size}) if defined $self->{current_file}->{size};
$entry->size_units($self->{current_file}->{size_units}) if defined $self->{current_file}->{size_units};
push @{$self->{files}}, $entry;
delete $self->{current_file};
#$self->{parser_state} = 1;
} elsif ($tagname eq "tr" && defined $self->{parser_state}) {
if (defined $self->{current_file}) {
my $entry = WWW::IndexParser::Entry->new;
$entry->filename($self->{current_file}->{filename}) if defined $self->{current_file}->{filename};
$entry->time($self->{current_file}->{time}) if defined $self->{current_file}->{time};
$entry->size($self->{current_file}->{size}) if defined $self->{current_file}->{size};
$entry->size_units($self->{current_file}->{size_units}) if defined $self->{current_file}->{size_units};
push @{$self->{files}}, $entry;
delete $self->{current_file};
}
$self->{parser_state} = 1;
} elsif ($tagname eq "a" && defined $self->{parser_state}) {
warn " file name = " . $attr->{href} if $self->{debug};
$self->{current_file}->{filename} = $attr->{href} if $attr->{href};
$self->{parser_state} = 1;
}
}
sub _parse_html_apache {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if (not defined $tagname) {
return unless $self->{parser_state};
if ($self->{parser_state} == 2) {
warn "The title is: $origtext" if $self->{debug};
if ($origtext =~ /^Index of (.+)$/) {
$self->{directory} = $1;
}
$self->{parser_state} = 1;
return;
}
if ($origtext =~ /(\d\d)-(\w\w\w)-(\d{4}) (\d\d):(\d\d)\s+([\d\.]+)(\w)?/) {
my $time = timelocal(0, $5, $4, $1, $months->{$2}, $3-1900);
$self->{current_file}->{time} = $time;
$self->{current_file}->{size} = $6;
$self->{current_file}->{size_units} = $7 if defined $7;
} elsif ($origtext =~ /(\d\d)-(\w\w\w)-(\d{4}) (\d\d):(\d\d)/) {
my $time = timelocal(0, $5, $4, $1, $months->{$2}, $3-1900);
$self->{current_file}->{time} = $time;
warn " Found time (using Apache 2.2+ check)" if $self->{debug};
} elsif ($origtext =~ /^(\d[\d\.]+)(\w)?/) {
warn " Found size (using Apache 2.2+ check)" if $self->{debug};
$self->{current_file}->{size} = $1;
$self->{current_file}->{size_units} = $2 if defined $2;
}
} elsif ($tagname eq 'title') {
$self->{parser_state} = 2;
} elsif ($tagname eq "pre") {
$self->{parser_state} = 1;
} elsif (($tagname eq "img" || $tagname eq "hr") && defined $self->{parser_state}) {
if (defined $self->{current_file} && $self->{current_file}->{filename} !~ /^\?/ && $self->{current_file}->{type} !~ /Icon/) {
my $entry = WWW::IndexParser::Entry->new;
$entry->filename($self->{current_file}->{filename}) if defined $self->{current_file}->{filename};
$entry->time($self->{current_file}->{time}) if defined $self->{current_file}->{time};
$entry->type($self->{current_file}->{type}) if defined $self->{current_file}->{type};
$entry->size($self->{current_file}->{size}) if defined $self->{current_file}->{size};
$entry->size_units($self->{current_file}->{size_units}) if defined $self->{current_file}->{size_units};
push @{$self->{files}}, $entry;
warn "Added " . $self->{current_file}->{filename} if $self->{debug};
delete $self->{current_file};
}
if (defined $attr->{alt}) {
warn "Possible new file:" . $attr->{alt} if $self->{debug};
$self->{current_file}->{type} = $attr->{alt};
}
} elsif ($tagname eq "a" && defined $self->{parser_state}) {
warn " file name = " . $attr->{href} if $self->{debug};
$self->{current_file}->{filename} = $attr->{href} if defined $attr->{href};
} else {
warn $tagname if $self->{debug};
}
}
sub _parse_html_iis {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if (not defined $tagname) {
return unless $self->{parser_state};
if ($self->{parser_state} == 2) {
if ($origtext =~ /- (.+)$/) {
$self->{directory} = $1;
}
$self->{parser_state} = 1;
return;
}
if ($origtext =~ /\s*(\w+),\s+(\w+)\s+(\d+),\s+(\d{4})\s+(\d{1,2}):(\d\d) (AM|PM)\s+([\d\.]+)/) {
my $hour_of_day = $5;
$hour_of_day = 0 if ($7 eq 'AM' && $hour_of_day eq 12);
$hour_of_day += 12 if ($7 eq 'PM' && $hour_of_day ne 12);
my $time = timelocal(0, $6, $hour_of_day, $3, $months->{$2}, $4-1900);
$self->{current_file}->{time} = $time;
$self->{current_file}->{size} = $8;
}
} elsif ($tagname eq 'title') {
$self->{parser_state} = 2;
} elsif ($tagname eq "pre") {
$self->{parser_state} = 1;
} elsif ($tagname eq "br" && defined $self->{parser_state}) {
if (defined $self->{current_file}) {
my $entry = WWW::IndexParser::Entry->new;
$entry->filename($self->{current_file}->{filename}) if defined $self->{current_file}->{filename};
$entry->time($self->{current_file}->{time}) if defined $self->{current_file}->{time};
$entry->size($self->{current_file}->{size}) if defined $self->{current_file}->{size};
$entry->size_units($self->{current_file}->{size_units}) if defined $self->{current_file}->{size_units};
push @{$self->{files}}, $entry;
delete $self->{current_file};
}
} elsif ($tagname eq "a" && defined $self->{parser_state}) {
warn " file name = " . $attr->{href} if $self->{debug};
$self->{current_file}->{filename} = $attr->{href} if defined $attr->{href};
}
}
sub _parse_html_lighttpd {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if (not defined $tagname) {
return unless $self->{parser_state};
if ($self->{parser_state} eq 'title') {
warn "The title is: $origtext" if $self->{debug};
if ($origtext =~ m!^Index of (.+)/$!) {
$self->{directory} = $1;
}
$self->{parser_state} = 1;
return;
}
if ($self->{parser_state} eq 'time') {
if ($origtext =~ /^(\d{4})-(\w\w\w)-(\d\d) (\d\d):(\d\d):(\d\d)$/) {
my $time = timelocal(0, $5, $4, $3, $months->{$2}, $1-1900);
$self->{current_file}->{time} = $time;
}
} elsif ($self->{parser_state} eq 'size') {
if ($origtext =~ /^([\d\.]+)(\w)?/) {
$self->{current_file}->{size} = $1;
$self->{current_file}->{size_units} = $2 if defined $2;
}
} elsif ($self->{parser_state} eq 'type') {
if ($origtext =~ /^[\w\-\/]+$/) {
$self->{current_file}->{type} = $origtext;
}
}
} elsif ($tagname eq 'title') {
$self->{parser_state} = 'title';
} elsif ($tagname eq "td") {
my %class2state = (m => 'time', s => 'size', t => 'type');
my $class = $attr->{class};
my $state = $class2state{$class};
$self->{parser_state} = $state if $state;
} elsif ($tagname eq 'tr') {
if (defined $self->{current_file}) {
my $entry = WWW::IndexParser::Entry->new;
$entry->filename($self->{current_file}->{filename}) if defined $self->{current_file}->{filename};
$entry->time($self->{current_file}->{time}) if defined $self->{current_file}->{time};
$entry->type($self->{current_file}->{type}) if defined $self->{current_file}->{type};
$entry->size($self->{current_file}->{size}) if defined $self->{current_file}->{size};
$entry->size_units($self->{current_file}->{size_units}) if defined $self->{current_file}->{size_units};
push @{$self->{files}}, $entry;
warn "Added " . $self->{current_file}->{filename} if $self->{debug};
delete $self->{current_file};
}
warn "Possible new file row" if $self->{debug};
$self->{parser_state} = 1;
} elsif ($tagname eq "a" && defined $self->{parser_state}) {
warn " file name = " . $attr->{href} if $self->{debug};
$self->{current_file}->{filename} = $attr->{href} if defined $attr->{href};
} else {
warn $tagname if $self->{debug};
}
}
=head1 NAME
WWW::IndexParser - Fetch and parse the directory index from a web server
=head1 SYNOPSIS
use WWW::IndexParser;
my @files = WWW::IndexParser->new(url => 'http://www.example.com/dir/');
foreach my $entry (@files) {
printf "%s %s\n", $entry->filename,
scalar(localtime($entry->time)||'');
}
=head1 DESCRIPTION
B<WWW::IndexParser> is a module that uses LWP to fetch a URL from a web
server. It then atempts to parse this page as if it were an auto generated
index page. It returns an array of B<WWW::IndexParser::Entry> objects, one
per entry in the directory index that it has found. Each Entry has a
set of methods: filename(), time(), size(), and others if supported
by the autoindex generated: type() and size_units().
=head1 CONSTRUCTOR
=over 4
=item new ( url => $url, timeout => $seconds, proxy => $proxy_url, debug => 1 )
When called with a URL to examine, this method does not return an object,
but an array of WWW::IndexParser::Entry obects, one per entry in the
directory listing that was accessed.
The options to this are:
=over 4
=item url
The complete URL of the index to fetch.
=item timeout
The timeout for the request to fetch data, default 10 seconds.
=item proxy
A proxy server URL, eg, 'http://proxy:3128/'.
=item debug
Decide if to print parsing debug information. Set to 0 (the default) to
disable, or anything non-false to print. Recommened you use a digit (ie, 1)
as this may become a numeric 'level' of debug in the future.
=back
=back
=head1 METHODS
All methods are private in this module. Pass only a URL to the constructor,
and it does everything for you itself.
=head1 PREREQUISUTES
This modile depends upon C<LWP>, C<HTML::Parser>, C<Time::Local>.
=head1 OSNAMES
any
=head1 BUGS
Currently only supports Apache, IIS and Tomcat style auto indexes. Send suggestions for new Auto-Indexes to support to the author (along with sample HTML)!
=head1 AUTHOR
James Bromberger E<lt>james@rcpt.toE<gt>
=head1 COPYRIGHT
Copyright (c) 2006 James Bromberger. All rights reserved. All rights
reserved. This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
1;
|