'ignore'
,
'image'
,
);
sub
RTYPES { ( IMP_PASS, IMP_REPLACE ) }
sub
new_analyzer {
my
(
$factory
,
%args
) =
@_
;
my
$self
=
$factory
->SUPER::new_analyzer(
%args
);
$self
->run_callback([ IMP_PASS,0,IMP_MAXOFFSET ]);
return
$self
;
}
sub
request_hdr {}
sub
request_body {}
sub
any_data {}
sub
response_hdr {
my
(
$self
,
$hdr
) =
@_
;
my
$ignore
;
debug(
"header=$hdr"
);
my
(
$ct
) =
$hdr
=~m{\nContent-type:[ \t]*([^\s;]+)}i;
my
(
$clen
) =
$hdr
=~m{\nContent-
length
:[ \t]*(\d+)}i;
my
(
$code
) =
$hdr
=~m{\AHTTP/1\.[01][ \t]+(\d+)};
if
(
$code
!= 200 ) {
debug(
"will not rotate code=$code"
);
$ignore
++;
}
elsif
( !
$ct
or
$ct
!~m{^image/(png|gif|jpeg)$} ) {
debug(
"will not rotate content type $ct"
);
$ignore
++;
}
elsif
(
$clen
and
$clen
> MAX_SIZE ) {
debug(
"image is too big: $clen"
);
$ignore
++;
}
if
(
$ignore
) {
$self
->run_callback([ IMP_PASS,1,IMP_MAXOFFSET ]);
$self
->{ignore} = 1;
return
;
}
$self
->run_callback([ IMP_PASS,1,
$self
->offset(1) ]);
}
sub
response_body {
my
(
$self
,
$data
) =
@_
;
$self
->{ignore} and
return
;
my
$off
=
$self
->offset(1);
if
(
$data
ne
''
) {
$self
->{image} .=
$data
;
$self
->run_callback([ IMP_REPLACE,1,
$off
,
''
]);
if
(
length
(
$self
->{image}) > MAX_SIZE ) {
debug(
"image too big"
);
$self
->run_callback(
[ IMP_REPLACE,1,
$off
,
$self
->{image} ],
[ IMP_PASS,1,IMP_MAXOFFSET ]
);
$self
->{ignore} = 1;
}
return
;
}
debug(
"flip image size=%d"
,
length
(
$self
->{image}));
my
$img
= Image::Magick->new;
debug(
"failed to flip img: $@"
)
if
!
eval
{
$img
->BlobToImage(
$self
->{image});
$img
->Flip;
(
$self
->{image}) =
$img
->ImageToBlob;
debug(
"replace with "
.
length
(
$self
->{image}).
" bytes"
);
1;
};
$self
->run_callback(
[ IMP_REPLACE,1,
$self
->offset(1),
$self
->{image} ],
[ IMP_PASS,1,IMP_MAXOFFSET ],
);
}
1;