| 1 | package Logfile::EPrints::Filter::RobotsTxt; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Logfile::EPrints::Filter::RobotsTxt - Filter Web log hits using a database of robot's IPs | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =over 4 | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =item file | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Specify the robots DBM file to use. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =back | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | require bytes; | |||||||||||||||||||||||||||
| 20 | 6 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use SDBM_File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use constant BOT_CACHE => '/usr/local/share/Logfile/botcache.db'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 6 | ▶ | use constant CACHE_TIMEOUT => 60*60*24*30; # 30 days | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use vars qw( $AUTOLOAD ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub new | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 1 | my ($class,%args) = @_; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | my $self = bless \%args, ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | my $filename = $args{'file'} || BOT_CACHE; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'file'} || '/usr/local/share/Logfile/botcache.db'
MC/DC0%0/1
$args{'file'} || '/usr/local/share/Logfile/botcache.db'
$args{'file'}
Truth table50%1/2
A
A= $args{'file'}
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | tie %{$self->{cache}}, 'SDBM_File', $filename, O_CREAT|O_RDWR, 0644 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | or die "Unable to open robots cache database at $filename: $!"; | ||||||||||||||||||||||||||||
| 34 | 1 | my @KEYS; | |||||||||||||||||||||||||||
| 35 | 1 | ▶ | while( my ($key, $value) = each %{$self->{cache}} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | 0 | my ($utime,$agent) = unpack("la*", $value); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | push @KEYS, $key if( $utime < time - CACHE_TIMEOUT ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | delete $self->{cache}->{$_} for @KEYS; | |||||||||||||||||||||||||||
| 41 | 1 | $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub DESTROY | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 1 | untie %{$self->{cache}}; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub AUTOLOAD | ||||||||||||||||||||||||||||
| 51 | 1122 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 52 | 1122 | $AUTOLOAD =~ s/^.*:://; | |||||||||||||||||||||||||||
| 53 | 1122 | ▶ | return if $AUTOLOAD =~ /[A-Z]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1122 | my ($self,$hit) = @_; | |||||||||||||||||||||||||||
| 55 | 1122 | ▶ | if( defined($hit->page) && $hit->page =~ /robots\.txt$/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hit->page and $hit->page =~ /robots\.txt$/
MC/DC50%1/2
defined $hit->page and $hit->page =~ /robots\.txt$/
defined $hit->page
$hit->page =~ /robots\.txt$/
Truth table66%2/3
A and B
A= defined $hit->pageB= $hit->page =~ /robots\.txt$/
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 21 | $self->robotstxt($hit); | |||||||||||||||||||||||||||
| 58 | 21 | return undef; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 1101 | ▶ | if( defined(my $value = $self->{cache}->{$hit->address}) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | #warn "Ignoring hit from " . $hit->address . " (" . $self->{cache}->{$hit->address} . ")"; | ||||||||||||||||||||||||||||
| 63 | 7 | my( $utime ) = unpack("l",$value); | |||||||||||||||||||||||||||
| 64 | 7 | ▶ | if( $utime > CACHE_TIMEOUT ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 7 | delete $self->{cache}->{$hit->address}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else | ||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 0 | return undef; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1101 | return $self->{handler}->$AUTOLOAD($hit); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub robotstxt | ||||||||||||||||||||||||||||
| 78 | 21 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
robotstxt: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 21 | my ($self,$hit) = @_; | |||||||||||||||||||||||||||
| 80 | #warn "Got new robot: " . join(',',$hit->address,$hit->utime,$hit->agent) . "\n"; | ||||||||||||||||||||||||||||
| 81 | # SDBM_File format only supports upto 1008 bytes | ||||||||||||||||||||||||||||
| 82 | 21 | ▶ | $self->{cache}->{$hit->address} = bytes::substr(pack("la*",$hit->utime,$hit->agent||''),0,1008); | ||||||||||||||||||||||||||
|
Condition50%1/2
$hit->agent || ''
MC/DC0%0/1
$hit->agent || ''
$hit->agent
Truth table50%1/2
A
A= $hit->agent
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | 1; | |||||||||||||||||||||||||||