package Apache::Plop; use strict; use Apache::RequestRec; use Apache::RequestIO; use Apache::RequestUtil; use Apache::Const; use Apache::ServerUtil; use Apache::Response; use APR::Table; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); sub handler { my $r = shift; if( ! $r->proxyreq ) { return Apache::DECLINED; } print STDERR "Good, this is a proxyreq ...\n"; # --> # if I use this push_handlers, I never reach the "proxy_handler" sub # So I commented it out and tried to do the work directly in "handler" # $r->handler("perl-script"); #ok, let's do it # $r->push_handlers(PerlHandler => \&proxy_handler); #} # #sub proxy_handler { # my $r = shift; # <-- if( $r->method ne "GET" ) { return Apache::DECLINED; } print STDERR "Good, this is a GET method ...\n"; # prepare the "real" request my $request = HTTP::Request->new( $r->method, $r->uri ); # copy headers from client request my %headers_in; print STDERR "-- client headers --\n"; $r->headers_in()->do( sub { print STDERR "$_[0]: $_[1]\n"; $headers_in{$_[0]} = $_[1]; $request->header( {$_[0]}, $_[0] ); } ); print STDERR "-- end --\n"; # make the "real" request myself $ua->agent( $headers_in{ 'User-Agent' } ); my $response = $ua->request( $request ); if( ! $response->is_success() ) { print STDERR "== ERROR ==\n"; return Apache::DECLINED; } print STDERR "-- server headers --\n"; my %headers_out; $response->headers()->scan( sub { print STDERR "$_[0]: $_[1]\n"; $headers_out{$_[0]} = $_[1]; } ); print STDERR "-- end --\n"; # simply override the content my $content = $response->content; $content = "plop"; # adjust the headers for the new content $headers_out{ 'Content-length' } = length( $content ); $headers_out{ 'Content-type' } = 'text/html'; # copy the modified response headers back to Apache foreach (keys %headers_out) { $r->headers_out->{$_} = $headers_out{$_}; } $r->content_type( $headers_out{ 'Content-type' } ); print STDERR "-- send/print --\n"; # --> # here is where the SEGFAULT occurs $r->send_http_header(); $r->print( $content ); # I don't know how to write a content back to the client # <-- print STDERR "-- end --\n"; return Apache::OK; } 1;