| 1 | # $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks/Rule/MIME.pm 8922 2007-11-12T03:06:00.677781Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package GunghoX::FollowLinks::Rule::MIME; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(GunghoX::FollowLinks::Rule); | ||
| 10 | use MIME::Types; | ||
| 11 | use GunghoX::FollowLinks::Rule qw(FOLLOW_ALLOW FOLLOW_DENY FOLLOW_DEFER); | ||
| 12 | |||
| 13 | __PACKAGE__->mk_accessors($_) for qw(types mime unknown); | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $class = shift; | ||
| 18 | my %args = @_; | ||
| 19 | |||
| 20 | $class->next::method(action => FOLLOW_ALLOW, @_, mime => MIME::Types->new); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub apply | ||
| 24 | { | ||
| 25 | my ($self, $c, $response, $url, $attrs) = @_; | ||
| 26 | |||
| 27 | my $mime = $self->mime->mimeTypeOf( $url ); | ||
| 28 | |||
| 29 | if (! defined $mime) { | ||
| 30 | $c->log->debug("MIME type of $url is unknown"); | ||
| 31 | return ($self->unknown || FOLLOW_DEFER); | ||
| 32 | } | ||
| 33 | |||
| 34 | my @types = $self->types || []; | ||
| 35 | foreach my $type (@types) { | ||
| 36 | return $self->action if $mime->type eq $type; | ||
| 37 | } | ||
| 38 | return FOLLOW_DENY; | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | __END__ |