snakerdlk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to create a proxy to edit images.

The problem is, the images comes in chunks and can't be worked with that way, not that I know of anyway.

I've tried setting the protocol to HTTP/1.0 in filters, but in wireshar I see the request still go as HTTP/1.1.(as do the responses)

I suspect that it is the proxies UA that is using HTTP/1.1 and the only way I found to change it is through environment variables, which didn't work.

Using the filter HTTP::Proxy::BodyFilter::Complete, I can access the whole image, not just chunks, but I can't edit it.

#!/usr/bin/perl use strict; use warnings; use Env qw(PERL_LWP_USE_HTTP_10); use HTTP::Proxy qw/:log/; use HTTP::Headers; use HTTP::Proxy::BodyFilter::complete; use HTTP::Proxy::HeaderFilter::simple; use HTTP::Proxy::BodyFilter::simple; use String::Random; #configuration my $port = 8080; my $proxyLogFile = "/tmp/sampleProxy.pl.log"; my $logMask = PROXY | STATUS | HEADERS | FILTERS | CONNECT; #logging open(LOG ,'>>'.$proxyLogFile); my $bodyFilter = HTTP::Proxy::BodyFilter::simple->new( sub { my ( $self, $dataref, $message, $protocol, $buffer ) = @_; $message->protocol("HTTP/1.0"); print "BODYFILTER\n"; #print "Protocol: '".$protocol."'\n"; print "MESSAGE:\n-------\n"; print $message->as_string."-------\n"; print "\n"; } ); my $imgFilter = HTTP::Proxy::BodyFilter::simple->new( sub { my ( $self, $dataref, $message, $protocol, $buffer ) = @_; my $rand = new String::Random; print "IMAGEFILTER\n"; print $message->as_string."\n"; print "--------\n\n"; if(length($$dataref)){ print "Image length is not 0, there is a image!\n"; my $name = $message->header('Content-Type'); $name =~ s/image\/(.+)$/$1/gi; $name = $rand->randregex('\d\d\d').'.'.$name; print "SAving image as $name\n"; open IMAGE, ">>".$name or die "erro:$!\n"; print IMAGE $$dataref; close IMAGE; print "Flipping Image\n"; system("convert $name -flip $name"); print "Sending Image\n"; open IMAGE, $name or die "erro:$!\n"; my $newImage = <IMAGE>; $$dataref = $newImage; } } ); my $proxy = HTTP::Proxy->new; $proxy->port($port); $proxy->logfh(*LOG); $proxy->logmask( $logMask ); #debug #push filters $proxy->push_filter( request => $bodyFilter ); $proxy->push_filter( mime => 'image/*', response => HTTP::Proxy::BodyFilter::complete->new, response=>$imgFilter); $proxy->agent->env_proxy; $proxy->start; close(LOG);
Any insight is appreciated.

Replies are listed 'Best First'.
Re: Chunk problem in HTTP::Proxy
by mellon85 (Monk) on Feb 09, 2011 at 07:09 UTC

    Have you tried using HTTP::Proxy::BodyFilter::complete instead of HTTP::Proxy::BodyFilter::simple?

    update: ups, i forgot you already tried that

    Anyway, for storing data, it's better to use File::Temp then random strings, as this is collision free.

      Thanks for the answer and the recomendation. Hadn't much time to bang my head against the code until it worked recently. (and I didn't find how to set a subscription to this thread...)