Writing a http proxy in perl (different) 
Author Message
 Writing a http proxy in perl (different)

I'm wondering if it is possible to write a simple http proxy in Perl
that uses Apache as it's server.  Most of the web proxy examples
scripts I've seen in Perl include the server code in the script.  So
basically the Perl script sits in a loop listening on port 80 to all
requests that come in, then takes the url that the user is requesting
and makes a request to get the web page for that url then prints the
contents to the screen.
I don't want to do it this way, instead I would like to use Apache as
my server so that it receives all the requests and forwards the info
to my perl script.  I want to do it this way so I can host this script
through my web hosting company rather than having to set this up on my
own machine, which I would have to do if the Perl script acted as the
server.
So I basically would like to be able to enter a url to my perl script
(i.e. http://www.*-*-*.com/ ) in my proxy
field in my web browser and have this perl script receive the url that
I'm trying to load through my web browser and have it make a request
to get that url and just print it to the screen.  Is this possible? If
so how would I do it and how would I handle usernames/passwords too.
I hope I explained this well enough.
Any info would be appreciated.
Thanks....


Thu, 12 Aug 2004 05:48:39 GMT  
 Writing a http proxy in perl (different)

Quote:

> So I basically would like to be able to enter a url to my perl script
> (i.e. http://www.myhost.com/cgi-bin/myproxyscript.pl) in my proxy
> field in my web browser and have this perl script receive the url that
> I'm trying to load through my web browser and have it make a request
> to get that url and just print it to the screen.  Is this possible?

That bits easy:

#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use CGI;
$q = new CGI;
my $url=$q->param('url');
getprint $url;

(some error checking should be done to make sure $url _looks_ like a url,
and that getprint actually fetches a page, but its a simple demo, OK? At
least I _did_ use warnings and strict, and _still_ did in in 7 lines :-)

But it only easy 'cause you left out an important requirement...

Quote:
>If so how would I do it and how would I handle usernames/passwords too.

This bit is harder. The problem is, to make it do what you _want_ it to do,
you're going to need to detect and re-write all the url's in the returned
page to also go through your script. Think about what happens when you go
through your "proxy" to a page with a link say to www.google.com on it.
What happens when they click on that link, where does their browser sent
that request? What do you _want_ to happen? Somehow you're going to need to
re-write that link as
<a
href='http://www.myhost.com/cgi-bin/myproxyscript.pl?url=http://www.google.c
om'> (and you're going to need to properly url-encode the '://' bit of that
too)

Then you need to think about whether you want to proxy images? are you
going to need to re-write all the <img src='foo'> tags too? And form
actions <form action='script.pl'> - that gets much more complex (but still
do-able). And depending on how the 'usernames/password' bits of the target
site work, you might have to deal with cookies and basic authentication.
The LWP and CGI modules do have all the bits you need to be able to do
this, but its not going to be _easy_...

(And I'd _love_ to see Godzilla get it to work properly _without_ using the
"cargo cult" CGI & LWP modules :-)

good luck, and take a look at
<http://www.stonehenge.com/merlyn/WebTechniques/> column 34 for a really
good example of a perl proxy...

big



Fri, 13 Aug 2004 04:55:21 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Perl HTTP Proxy

2. HTTP proxy server in perl?

3. HTTP proxy server in perl?

4. perl HTTP proxy?

5. HTTP proxy server in perl?

6. Perl HTTP Proxy

7. Perl Programme written on different platforms

8. SSL-to-HTTP Inter-to-Intranet proxy

9. HTTP Proxy Module

10. ISO http + https proxy

11. HTTP GET with Proxy support?

12. How to set up an HTTP proxy?

 

 
Powered by phpBB® Forum Software