line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks/Parser.pm 9010 2007-11-13T02:08:07.210715Z daisuke $ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> |
4
|
|
|
|
|
|
|
# All rights reserved. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package GunghoX::FollowLinks::Parser; |
7
|
3
|
|
|
3
|
|
1260
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
79
|
|
8
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
68
|
|
9
|
3
|
|
|
3
|
|
16
|
use base qw(Gungho::Base); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
1177
|
|
10
|
3
|
|
|
3
|
|
16523
|
use Gungho::Request; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Gungho::Util; |
12
|
|
|
|
|
|
|
use GunghoX::FollowLinks::Rule qw(FOLLOW_ALLOW FOLLOW_DENY FOLLOW_DEFER); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors($_) for qw(rules content_type merge_rule); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub parse { die "Must override parse()" } |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub register |
19
|
|
|
|
|
|
|
{ |
20
|
|
|
|
|
|
|
my ($self, $c) = @_; |
21
|
|
|
|
|
|
|
my $ct = $self->content_type; |
22
|
|
|
|
|
|
|
$c->follow_links_parsers->{ $ct } = $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new |
26
|
|
|
|
|
|
|
{ |
27
|
|
|
|
|
|
|
my $class = shift; |
28
|
|
|
|
|
|
|
my %args = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my @rules; |
31
|
|
|
|
|
|
|
foreach my $rule (@{ $args{rules} }) { |
32
|
|
|
|
|
|
|
if (! eval { $rule->isa('GunghoX::FollowLinks::Rule') } || $@) { |
33
|
|
|
|
|
|
|
my $module = $rule->{module}; |
34
|
|
|
|
|
|
|
my $pkg = Gungho::Util::load_module($module, "GunghoX::FollowLinks::Rule"); |
35
|
|
|
|
|
|
|
$rule = $pkg->new( %{ $rule->{config} } ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
push @rules, $rule; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
return $class->next::method( |
40
|
|
|
|
|
|
|
content_type => 'DEFAULT', |
41
|
|
|
|
|
|
|
merge_rule => 'ANY', |
42
|
|
|
|
|
|
|
@_, |
43
|
|
|
|
|
|
|
rules => \@rules |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub apply_rules |
48
|
|
|
|
|
|
|
{ |
49
|
|
|
|
|
|
|
my ($self, $c, $response, $url, $attrs) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$c->log->debug( "Applying rules for $url" ); |
52
|
|
|
|
|
|
|
my $rules = $self->rules ; |
53
|
|
|
|
|
|
|
my $decision; |
54
|
|
|
|
|
|
|
my @decision; |
55
|
|
|
|
|
|
|
foreach my $rule (@{ $rules }) { |
56
|
|
|
|
|
|
|
$decision = $rule->apply( $c, $response, $url, $attrs ); |
57
|
|
|
|
|
|
|
if ($decision eq FOLLOW_ALLOW || $decision eq FOLLOW_DENY) { |
58
|
|
|
|
|
|
|
$c->log->debug( " + Rule $rule " . ( |
59
|
|
|
|
|
|
|
$decision eq FOLLOW_ALLOW ? "ALLOW" : |
60
|
|
|
|
|
|
|
$decision eq FOLLOW_DENY ? "DENY" : |
61
|
|
|
|
|
|
|
$decision eq FOLLOW_DEFER ? "DEFER" : |
62
|
|
|
|
|
|
|
"UNKNOWN" |
63
|
|
|
|
|
|
|
) . " for url $url"); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
if ($self->merge_rule eq 'ANY') { |
66
|
|
|
|
|
|
|
$c->log->debug( " * Merge rule is 'ANY', stopping rules"); |
67
|
|
|
|
|
|
|
last; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
push @decision, $decision; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
if ($self->merge_rule eq 'ALL') { |
74
|
|
|
|
|
|
|
my @allowed = grep { $_ eq FOLLOW_ALLOW } @decision; |
75
|
|
|
|
|
|
|
$c->log->debug( "Merge rule is 'ALL'. " . scalar @allowed . " ALLOWs from " . scalar @decision . " decisions"); |
76
|
|
|
|
|
|
|
$decision = (@allowed == @decision) ? FOLLOW_ALLOW : FOLLOW_DENY; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
return ($decision || FOLLOW_DEFER) eq FOLLOW_ALLOW; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub follow_if_allowed |
83
|
|
|
|
|
|
|
{ |
84
|
|
|
|
|
|
|
my ($self, $c, $response, $url, $attrs) = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $allowed = 0; |
87
|
|
|
|
|
|
|
if ($self->apply_rules( $c, $response, $url, $attrs ) ) { |
88
|
|
|
|
|
|
|
$c->log->debug( "$url is allowed" ); |
89
|
|
|
|
|
|
|
$c->pushback_request( Gungho::Request->new( GET => $url ) ); |
90
|
|
|
|
|
|
|
$allowed++; |
91
|
|
|
|
|
|
|
} else { |
92
|
|
|
|
|
|
|
$c->log->debug( "$url is denied" ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
return $allowed; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 NAME |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
GunghoX::FollowLinks::Parser - Base Class For FollowLinks Parser |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 METHODS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 new(%args) |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 content_type |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 rules |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 register |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 parse |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 apply_rules |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 follow_if_allowed |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |