Thread

Posted on Tue Aug 16 21:11:00 2005 by randomhajile
Changing the request hostname in GET and POST
I was wondering: is there a way to modify the hostname that is sent before the request is sent to a web server when using an HTTP proxy server? I set HTTP-Proxy up to connect to our corporate proxy as an intermediary, so that browsers send POST/GET requests as "GET http://slashdot.org/" rather than "GET /". I'd like to be able to change the 'slashdot.org' to an IP address or different web page (for instance, the HTTP tunnel I have set up at home). I've tried other solutions, and none can get me to that point, but I am most comfortable with Perl, so I figured I'd give HTTP-Proxy a shot. I like it the way it abstracts objects nicely, while also allowing you to use real Perl code. Any ideas would be much appreciated.

EP
Direct Responses: 899 | Write a response
Posted on Tue Aug 16 21:31:54 2005 by book in response to 898
Re: Changing the request hostname in GET and POST

I do not understand your explanations, but I can answer your question anyway. :-)

HTTP::Proxy has two types of filters: HeaderFilter and BodyFilter. Those can be set up (with the push_filter() method) to act either on the request or the response. Most requests (with the notable exception of POST requests) do not have bodies, so if you want to change something in a request, it's usually the headers (or the query itself). For this, you'll need to use a subclass of HTTP::Proxy::HeaderFilter. The filter() method has the following signature:

my ( $self, $headers, $message ) = @_;

Where $message is a HTTP::Request (or a HTTP::Response) object. So, to change the request on the fly, you just need to set $message->uri to whatever you want.

Also note that you do not have to write a full subclass: most of my filters are written using HTTP::Proxy::HeaderFilter::simple (or HTTP::Proxy::BodyFilter::simple)

Check out http://http-proxy.mongueurs.net/ for CVS snapshot and mailing list for HTTP::Proxy

Direct Responses: 900 | Write a response
Posted on Tue Aug 16 23:21:18 2005 by randomhajile in response to 899
Re: Changing the request hostname in GET and POST
Stupid, stupid, stupid (not you)...I got it. I had tried that, but I got an error message. Well, I went back and tried it again, and realized that I was doing $message->uri =~ s/replacethis/withthis/; rather than $message->uri($settothisvariable);. Duh. I used eg/logger.pl as a starting point. uri() is an accessor method, not an lvalue. Plus, IE sucks and it was caching the image once I got it working...even with a shift-refresh. Generally I use Firefox, but I didn't want to mess it up while I was working on something. I think that will do it. Thanks for the quick response and such.
Write a response