| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Net::IMP::HTTP::Example::BlockContentType; | ||
| 5 | 1 | ▶ | use base 'Net::IMP::HTTP::Request'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use Net::IMP; # import IMP_ constants | ||
| 7 | use Net::IMP::Debug; | ||
| 8 | |||
| 9 | sub RTYPES { ( IMP_PASS, IMP_DENY ) } | ||
| 10 | sub new_analyzer { | ||
| 11 | my ($factory,%args) = @_; | ||
| 12 | my $self = $factory->SUPER::new_analyzer(%args); | ||
| 13 | # request data do not matter | ||
| 14 | $self->run_callback([ IMP_PASS,0,IMP_MAXOFFSET ]); | ||
| 15 | if ( ! $self->{factory_args}{whiterx} | ||
| 16 | && ! $self->{factory_args}{blackrx} ) { | ||
| 17 | # nothing to analyze | ||
| 18 | $self->run_callback([ IMP_PASS,1,IMP_MAXOFFSET ]); | ||
| 19 | } | ||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub validate_cfg { | ||
| 24 | my ($class,%cfg) = @_; | ||
| 25 | my @err; | ||
| 26 | for my $k (qw(whiterx blackrx)) { | ||
| 27 | my $rx = delete $cfg{$k} or next; | ||
| 28 | ref($rx) and next; | ||
| 29 | push @err,"$k is no valid regexp: $@" if ! eval { qr/$rx/ }; | ||
| 30 | } | ||
| 31 | return (@err,$class->SUPER::validate_cfg(%cfg)); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub str2cfg { | ||
| 35 | my ($class,$str) = @_; | ||
| 36 | my %cfg = $class->SUPER::str2cfg($str); | ||
| 37 | for my $k (qw(whiterx blackrx)) { | ||
| 38 | next if ! $cfg{$k} or ref $cfg{$k}; | ||
| 39 | $cfg{$k} = eval { qr/$cfg{$k}/ } | ||
| 40 | or die "invalid rx in $k: $@"; | ||
| 41 | } | ||
| 42 | return %cfg; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub request_hdr {} | ||
| 46 | sub request_body {} | ||
| 47 | sub response_body {} | ||
| 48 | sub any_data {} | ||
| 49 | |||
| 50 | sub response_hdr { | ||
| 51 | my ($self,$hdr) = @_; | ||
| 52 | # we only want selected image/ content types and not too big | ||
| 53 | my $ct = $hdr =~m{\nContent-type:[ \t]*([^\s;]+)}i && lc($1) | ||
| 54 | || 'unknown/unknown'; | ||
| 55 | |||
| 56 | my $reason; | ||
| 57 | if ( my $white = $self->{factory_args}{whiterx} ) { | ||
| 58 | if ( $ct =~ $white ) { | ||
| 59 | debug("allowed $ct because of white list"); | ||
| 60 | goto pass; | ||
| 61 | } else { | ||
| 62 | debug("denied $ct because not in white list"); | ||
| 63 | $reason = "denied $ct because not in white list"; | ||
| 64 | goto deny; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | if ( my $black = $self->{factory_args}{blackrx} ) { | ||
| 68 | if ( $ct =~ $black ) { | ||
| 69 | debug("denied $ct because in black list"); | ||
| 70 | $reason = "denied $ct because in black list"; | ||
| 71 | goto deny; | ||
| 72 | } else { | ||
| 73 | debug("allow $ct because not in black list"); | ||
| 74 | goto pass; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | pass: | ||
| 79 | $self->run_callback([ IMP_PASS,1,IMP_MAXOFFSET ]); | ||
| 80 | return; | ||
| 81 | |||
| 82 | deny: | ||
| 83 | $self->run_callback([ IMP_DENY,1,$reason ]); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ | ||