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