| 1 | |||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | package Net::IMP::HTTP::SaveResponse; | ||
| 5 | 1 | ▶ | use base 'Net::IMP::HTTP::Request'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use fields qw(root file); | ||
| 7 | |||
| 8 | use Net::IMP; | ||
| 9 | use Net::IMP::Debug; | ||
| 10 | use File::Path 'make_path'; | ||
| 11 | use File::Temp 'tempfile'; | ||
| 12 | use Digest::MD5; | ||
| 13 | use Carp; | ||
| 14 | use Scalar::Util 'looks_like_number'; | ||
| 15 | |||
| 16 | my $DEFAULT_LIMIT = 10_000_000; | ||
| 17 | |||
| 18 | sub RTYPES { (IMP_PREPASS) } | ||
| 19 | |||
| 20 | sub new_factory { | ||
| 21 | my ($class,%args) = @_; | ||
| 22 | my $dir = $args{root} or croak("no root directory given"); | ||
| 23 | -d $dir && -r _ && -x _ or croak("cannot use base dir $dir: $!"); | ||
| 24 | $args{limit} = $DEFAULT_LIMIT if ! defined $args{limit}; | ||
| 25 | return $class->SUPER::new_factory(%args); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub new_analyzer { | ||
| 29 | my ($factory,%args) = @_; | ||
| 30 | my $self = $factory->SUPER::new_analyzer(%args); | ||
| 31 | # we don't modify | ||
| 32 | $self->run_callback( | ||
| 33 | [ IMP_PREPASS,0,IMP_MAXOFFSET ], | ||
| 34 | [ IMP_PREPASS,1,IMP_MAXOFFSET ] | ||
| 35 | ); | ||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub DESTROY { | ||
| 41 | my $self = shift; | ||
| 42 | $self->{file} && $self->{file}{tname} && unlink($self->{file}{tname}); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub request_hdr { | ||
| 46 | my ($self,$hdr) = @_; | ||
| 47 | |||
| 48 | my ($method,$proto,$host,$path) = $hdr =~m{\A([A-Z]+) +(?:(\w+)://([^/]+))?(\S+)}; | ||
| 49 | $host = $1 if $hdr =~m{\nHost: *(\S+)}i; | ||
| 50 | $host or goto IGNORE; | ||
| 51 | $proto ||= 'http'; | ||
| 52 | $host = lc($host); | ||
| 53 | my $port = | ||
| 54 | $host=~s{^(?:\[(\w._\-:)+\]|(\w._\-))(?::(\d+))?$}{ $1 || $2 }e ? | ||
| 55 | $3:80; | ||
| 56 | |||
| 57 | if ( my $rx = $self->{factory_args}{only_url} ) { | ||
| 58 | goto IGNORE if "$proto://$host:$port$path" !~ $rx | ||
| 59 | and "$proto://$host$path" !~ $rx | ||
| 60 | } | ||
| 61 | if ( my $rx = $self->{factory_args}{exclude_url} ) { | ||
| 62 | goto IGNORE if "$proto://$host:$port$path" =~ $rx | ||
| 63 | or "$proto://$host$path" =~ $rx | ||
| 64 | } | ||
| 65 | if ( my $srh = $self->{factory_args}{method} ) { | ||
| 66 | goto IGNORE if ! _check_srh($srh,$method); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | my $dir = $self->{factory_args}{root}."/$host:$port"; | ||
| 71 | if ( ! -d $dir ) { | ||
| 72 | my $err; | ||
| 73 | make_path($dir, { error => \$err }); | ||
| 74 | } | ||
| 75 | my ($fh,$fname) = tempfile( "tmpXXXXXXX", DIR => $dir ) | ||
| 76 | or goto IGNORE; | ||
| 77 | |||
| 78 | $hdr =~s{^(Content-encoding:|Transfer-encoding:|Content-length:)}{X-Original-$1}mig; | ||
| 79 | print $fh $hdr; | ||
| 80 | |||
| 81 | my $qstring = $path =~s{\?(.+)}{} ? $1 : undef; | ||
| 82 | $self->{file} = { | ||
| 83 | tfh => $fh, | ||
| 84 | tname => $fname, | ||
| 85 | dir => $dir, | ||
| 86 | method => $method, | ||
| 87 | md5path => Digest::MD5->new->add($path)->hexdigest, | ||
| 88 | md5data => undef, | ||
| 89 | size => [ length($hdr),0,0,0 ], | ||
| 90 | rphdr => '', | ||
| 91 | rpbody => '', | ||
| 92 | eof => 0, | ||
| 93 | }; | ||
| 94 | ( $self->{file}{md5data} = Digest::MD5->new )->add("\000$qstring\001") | ||
| 95 | if defined $qstring and ! $self->{factory_args}{ignore_parameters}; | ||
| 96 | return; # continue in request body | ||
| 97 | |||
| 98 | IGNORE: | ||
| 99 | # pass thru w/o saving | ||
| 100 | debug("no save $host:$port/$path"); | ||
| 101 | $self->run_callback( | ||
| 102 | # pass thru everything | ||
| 103 | [ IMP_PASS,0,IMP_MAXOFFSET ], | ||
| 104 | [ IMP_PASS,1,IMP_MAXOFFSET ], | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub request_body { | ||
| 109 | my ($self,$data) = @_; | ||
| 110 | my $f = $self->{file} or return; | ||
| 111 | print { $f->{tfh} } $data; | ||
| 112 | my $md = $f->{md5data}; | ||
| 113 | if ( $data ne '' ) { | ||
| 114 | $f->{size}[1] += length($data); | ||
| 115 | if ( my $l = $self->{factory_args}{limit} ) { | ||
| 116 | return _stop_saving($self) if $f->{size}[1] > $l; | ||
| 117 | } | ||
| 118 | if ( ! $md ) { | ||
| 119 | return if $self->{factory_args}{ignore_parameters}; | ||
| 120 | $md = $f->{md5data} = Digest::MD5->new; | ||
| 121 | } | ||
| 122 | $md->add($data); | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | if ( defined( my $rp = $f->{rphdr} )) { | ||
| 126 | print { $f->{tfh} } $rp; | ||
| 127 | $f->{rphdr} = undef; | ||
| 128 | if ( defined( $rp = $f->{rpbody} )) { | ||
| 129 | print { $f->{tfh} } $rp; | ||
| 130 | $f->{rpbody} = undef; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | _check_eof($self,1); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub response_hdr { | ||
| 137 | my ($self,$hdr) = @_; | ||
| 138 | my $f = $self->{file} or return; | ||
| 139 | return _stop_saving($self) if $hdr =~m{\AHTTP/1\.[01] (100|304|5\d\d)}; | ||
| 140 | if ( my $srh = $self->{factory_args}{content_type} ) { | ||
| 141 | my ($ct) = $hdr =~m{^Content-type:\s*([^\s;]+)}mi; | ||
| 142 | $ct ||= 'application/octet-stream'; | ||
| 143 | return _stop_saving($self) if ! _check_srh( $srh, lc($ct)); | ||
| 144 | } | ||
| 145 | $hdr =~s{^(Content-encoding:|Transfer-encoding:|Content-length:)}{X-Original-$1}mig; | ||
| 146 | $f->{size}[2] = length($hdr); | ||
| 147 | if ( defined $f->{rphdr} ) { | ||
| 148 | # defer, request body not fully read | ||
| 149 | $f->{rphdr} = $hdr; | ||
| 150 | } else { | ||
| 151 | print {$f->{tfh}} $hdr; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | sub response_body { | ||
| 156 | my ($self,$data) = @_; | ||
| 157 | my $f = $self->{file} or return; | ||
| 158 | $f->{size}[3] += length($data); | ||
| 159 | if ( my $l = $self->{factory_args}{limit} ) { | ||
| 160 | return _stop_saving($self) if $f->{size}[3] > $l; | ||
| 161 | } | ||
| 162 | if ( $data eq '' ) { | ||
| 163 | _check_eof($self,2) | ||
| 164 | } elsif ( defined $f->{rpbody} ) { | ||
| 165 | $f->{rpbody} .= $data; | ||
| 166 | } else { | ||
| 167 | print {$f->{tfh}} $data; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _check_eof { | ||
| 172 | my ($self,$bit) = @_; | ||
| 173 | my $f = $self->{file} or return; | ||
| 174 | ( $f->{eof} |= $bit ) == 3 or return; | ||
| 175 | $self->{file} = undef; | ||
| 176 | print {$f->{tfh}} pack("NNNN",@{ $f->{size} }); | ||
| 177 | close($f->{tfh}); | ||
| 178 | my $fname = "$f->{dir}/".join( "-", | ||
| 179 | lc($f->{method}), | ||
| 180 | $f->{md5path}, | ||
| 181 | $f->{md5data} ? ($f->{md5data}->hexdigest):() | ||
| 182 | ); | ||
| 183 | rename($f->{tname}, $fname); | ||
| 184 | } | ||
| 185 | |||
| 186 | # will not be tracked | ||
| 187 | sub any_data { | ||
| 188 | my $self = shift; | ||
| 189 | my $f = $self->{file} or return; | ||
| 190 | unlink($f->{tname}); | ||
| 191 | $self->{file} = undef; | ||
| 192 | } | ||
| 193 | |||
| 194 | ### config stuff ###### | ||
| 195 | sub validate_cfg { | ||
| 196 | my ($class,%cfg) = @_; | ||
| 197 | my $dir = delete $cfg{root}; | ||
| 198 | my @err; | ||
| 199 | push @err, "no or non-existing root dir given" | ||
| 200 | if ! defined $dir or ! -d $dir; | ||
| 201 | if ( my $limit = delete $cfg{limit} ) { | ||
| 202 | push @err, "limit should be number" if ! looks_like_number($limit) | ||
| 203 | } | ||
| 204 | for my $k (qw(content_type method)) { | ||
| 205 | my $v = delete $cfg{$k} // next; | ||
| 206 | push @err,"$k should be string, hash or regexp" if | ||
| 207 | ref($v) and not ref($v) ~~ [ 'Regexp','HASH' ]; | ||
| 208 | } | ||
| 209 | for my $k (qw(exclude_url only_url)) { | ||
| 210 | my $v = delete $cfg{$k} // next; | ||
| 211 | push @err,"$k should be regexp" if ref($v) ne 'Regexp'; | ||
| 212 | } | ||
| 213 | delete $cfg{ignore_parameters}; | ||
| 214 | |||
| 215 | push @err, $class->SUPER::validate_cfg(%cfg); | ||
| 216 | return @err; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub str2cfg { | ||
| 220 | my $self = shift; | ||
| 221 | my %cfg = $self->SUPER::str2cfg(@_); | ||
| 222 | for my $k (qw(content_type method)) { | ||
| 223 | my $v = $cfg{$k} // next; | ||
| 224 | if ( $v =~m{^/(.*)/$}s ) { | ||
| 225 | $cfg{$k} = eval { qr/$1/ } or croak("invalid regexp '$v': $@"); | ||
| 226 | } elsif (( my @v = split( /,/,$v )) > 1 ) { | ||
| 227 | $cfg{$k} = map { lc($_) => 1 } @v | ||
| 228 | } else { | ||
| 229 | $cfg{$k} = lc($v) | ||
| 230 | } | ||
| 231 | } | ||
| 232 | for my $k (qw(exclude_url only_url)) { | ||
| 233 | my $v = $cfg{$k} // next; | ||
| 234 | $v =~m{^/(.*)/$}s or croak("$k should be /regex/"); | ||
| 235 | $cfg{$k} = eval { qr/$1/ } or croak("invalid regexp '$v': $@"); | ||
| 236 | } | ||
| 237 | return %cfg; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub _check_srh { | ||
| 241 | my ($srh,$v) = @_; | ||
| 242 | return $v =~ $srh if ref($srh) eq 'Regexp'; | ||
| 243 | return $srh->{$_} if ref($srh) eq 'HASH'; | ||
| 244 | return $srh eq $v; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub _stop_saving { | ||
| 248 | my $self = shift; | ||
| 249 | my $f = $self->{file} or return; | ||
| 250 | unlink($f->{tname}); | ||
| 251 | $self->{file} = undef; | ||
| 252 | $self->run_callback( | ||
| 253 | [ IMP_PASS,0,IMP_MAXOFFSET ], | ||
| 254 | [ IMP_PASS,1,IMP_MAXOFFSET ], | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | 1; | ||
| 260 | |||
| 261 | __END__ | ||