| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Net::IMP::HTTP::Example::FlipImg; | ||
| 5 | use base 'Net::IMP::HTTP::Request'; | ||
| 6 | use fields ( | ||
| 7 | 'ignore', # irrelevant content | ||
| 8 | 'image', # collected octets for image | ||
| 9 | ); | ||
| 10 | |||
| 11 | use Net::IMP; # import IMP_ constants | ||
| 12 | use Net::IMP::Debug; | ||
| 13 | use Image::Magick; | ||
| 14 | use constant MAX_SIZE => 2**16; | ||
| 15 | |||
| 16 | sub RTYPES { ( IMP_PASS, IMP_REPLACE ) } | ||
| 17 | sub new_analyzer { | ||
| 18 | my ($factory,%args) = @_; | ||
| 19 | my $self = $factory->SUPER::new_analyzer(%args); | ||
| 20 | # request data do not matter | ||
| 21 | $self->run_callback([ IMP_PASS,0,IMP_MAXOFFSET ]); | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub request_hdr {} | ||
| 26 | sub request_body {} | ||
| 27 | sub any_data {} | ||
| 28 | |||
| 29 | sub response_hdr { | ||
| 30 | my ($self,$hdr) = @_; | ||
| 31 | my $ignore; | ||
| 32 | # we only want selected image/ content types and not too big | ||
| 33 | debug("header=$hdr"); | ||
| 34 | my ($ct) = $hdr =~m{\nContent-type:[ \t]*([^\s;]+)}i; | ||
| 35 | my ($clen) = $hdr =~m{\nContent-length:[ \t]*(\d+)}i; | ||
| 36 | my ($code) = $hdr =~m{\AHTTP/1\.[01][ \t]+(\d+)}; | ||
| 37 | if ( $code != 200 ) { | ||
| 38 | debug("will not rotate code=$code"); | ||
| 39 | $ignore++; | ||
| 40 | } elsif ( ! $ct or $ct !~m{^image/(png|gif|jpeg)$} ) { | ||
| 41 | debug("will not rotate content type $ct"); | ||
| 42 | $ignore++; | ||
| 43 | } elsif ( $clen and $clen > MAX_SIZE ) { | ||
| 44 | debug("image is too big: $clen" ); | ||
| 45 | $ignore++; | ||
| 46 | } | ||
| 47 | |||
| 48 | if ( $ignore ) { | ||
| 49 | $self->run_callback([ IMP_PASS,1,IMP_MAXOFFSET ]); | ||
| 50 | $self->{ignore} = 1; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | # pass header | ||
| 55 | $self->run_callback([ IMP_PASS,1,$self->offset(1) ]); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub response_body { | ||
| 59 | my ($self,$data) = @_; | ||
| 60 | $self->{ignore} and return; | ||
| 61 | my $off = $self->offset(1); | ||
| 62 | |||
| 63 | if ( $data ne '' ) { | ||
| 64 | $self->{image} .= $data; # append to image | ||
| 65 | # replace with '' in caller to save memory there | ||
| 66 | # at the end we will replace eof with the flipped image | ||
| 67 | $self->run_callback([ IMP_REPLACE,1,$off,'' ]); | ||
| 68 | |||
| 69 | # with chunked encoding we don't get a length up front, so check now | ||
| 70 | if ( length($self->{image}) > MAX_SIZE ) { | ||
| 71 | debug("image too big"); | ||
| 72 | $self->run_callback( | ||
| 73 | [ IMP_REPLACE,1,$off,$self->{image} ], # unchanged image | ||
| 74 | [ IMP_PASS,1,IMP_MAXOFFSET ] | ||
| 75 | ); | ||
| 76 | $self->{ignore} = 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | # end of image reached, flip with Image::Magick | ||
| 83 | debug("flip image size=%d",length($self->{image})); | ||
| 84 | my $img = Image::Magick->new; | ||
| 85 | debug("failed to flip img: $@") if ! eval { | ||
| 86 | $img->BlobToImage($self->{image}); | ||
| 87 | $img->Flip; | ||
| 88 | ($self->{image}) = $img->ImageToBlob; | ||
| 89 | debug("replace with ".length($self->{image})." bytes"); | ||
| 90 | 1; | ||
| 91 | }; | ||
| 92 | |||
| 93 | $self->run_callback( | ||
| 94 | [ IMP_REPLACE,1,$self->offset(1),$self->{image} ], | ||
| 95 | [ IMP_PASS,1,IMP_MAXOFFSET ], | ||
| 96 | ); | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | __END__ |