| 1 | # $Id: /mirror/perl/WWW-RobotRules-Parser/trunk/lib/WWW/RobotRules/Parser.pm 31629 2007-12-01T13:31:37.690053Z daisuke $ | ||||||||||||||
| 2 | # | ||||||||||||||
| 3 | # Copyright (c) 2006-2007 Daisuke Maki <daisuke@endeworks.jp> | ||||||||||||||
| 4 | # A lot of this code is based on WWW::RobotRules. | ||||||||||||||
| 5 | |||||||||||||||
| 6 | package WWW::RobotRules::Parser; | ||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | BEGIN | ||||||||||||||
| 13 | 2 | ▶ | { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 2 | $VERSION = '0.04001'; | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 6 | ▶ | sub new { bless { }, shift } | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | sub parse_uri | ||||||||||||||
| 20 | 0 | ▶ | { | ||||||||||||
|
Subroutine
parse_uri: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 21 | 0 | my $self = shift; | |||||||||||||
| 22 | 0 | my $uri = shift; | |||||||||||||
| 23 | |||||||||||||||
| 24 | 0 | require LWP::UserAgent; | |||||||||||||
| 25 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||
| 26 | 0 | my $res = $ua->get($uri); | |||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | ▶ | if (! $res->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 0 | require Carp; | |||||||||||||
| 30 | 0 | Carp::croak("Failed to retrieve $uri: Got HTTP code " . $res->code); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | $self->parse($uri, $res->content); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub parse | ||||||||||||||
| 37 | 6 | ▶ | { | ||||||||||||
|
Subroutine
parse: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||
| 38 | 6 | my $self = shift; | |||||||||||||
| 39 | 6 | my $robot_txt_uri = shift; | |||||||||||||
| 40 | 6 | my $text = shift; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 6 | ▶ | $robot_txt_uri = URI->new($robot_txt_uri) if ($robot_txt_uri); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | |||||||||||||||
| 44 | 6 | my %result; | |||||||||||||
| 45 | # blank lines are significant, so turn CRLF into LF to avoid generating | ||||||||||||||
| 46 | # false ones | ||||||||||||||
| 47 | 6 | $text =~ s/\015\012/\012/g; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 6 | my $ua_pattern; | |||||||||||||
| 50 | # split at \012 or \015 | ||||||||||||||
| 51 | 6 | for (split(/[\012\015]/, $text)) { | |||||||||||||
| 52 | # Lines containing only a comment are discarded completely, and | ||||||||||||||
| 53 | # therefore do not indicate a record boundary | ||||||||||||||
| 54 | 50 | ▶ | next if /^\s*\#/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 39 | s/\s*\#.*//; # Remove comments at end-of-line | |||||||||||||
| 57 | |||||||||||||||
| 58 | 39 | ▶ | if (/^\s*User-Agent\s*:\s*(.*)/i) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 59 | 14 | $ua_pattern = $1; | |||||||||||||
| 60 | 14 | $ua_pattern =~ s/\s+$//; | |||||||||||||
| 61 | } elsif (/^\s*Disallow\s*:\s*(.*)/i) { | ||||||||||||||
| 62 | 19 | ▶ | if (! defined $ua_pattern) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | $ua_pattern = '*'; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 19 | my $disallow = $1; | |||||||||||||
| 67 | 19 | $disallow =~ s/\s+$//; | |||||||||||||
| 68 | 19 | ▶ | if (length $disallow) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 16 | ▶ | if ($robot_txt_uri) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 16 | my $ignore; | |||||||||||||
| 71 | 16 | eval { | |||||||||||||
| 72 | 16 | my $u = URI->new_abs($disallow, $robot_txt_uri); | |||||||||||||
| 73 | 16 | ▶ | $ignore++ if $u->scheme ne $robot_txt_uri->scheme; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 16 | ▶ | $ignore++ if lc($u->host) ne lc($robot_txt_uri->host); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 15 | ▶ | $ignore++ if $u->port ne $robot_txt_uri->port; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 15 | $disallow = $u->path_query; | |||||||||||||
| 77 | 15 | ▶ | $disallow = "/" unless length $disallow; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | }; | ||||||||||||||
| 79 | 16 | ▶ | next if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 15 | ▶ | next if $ignore; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | } | ||||||||||||||
| 82 | 11 | push @{$result{$ua_pattern}}, $disallow; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | } | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | 6 | ▶ | return wantarray ? %result : \%result | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 2 | 1; | |||||||||||||
| 90 | |||||||||||||||
| 91 | __END__ | ||||||||||||||