Re: anonymous proxy server in 100 lines of Perl

Randal Schwartz (merlyn@stonehenge.com)
24 Nov 1996 08:42:03 -0700


>>>>> "Gisle" == Gisle Aas <aas@bergen.sn.no> writes:

Gisle> I have a few comments below:

>> $SIG{__DIE__} = sub { die prefix @_ };

Gisle> I am not so sure that it is a good idea to set up such a __DIE__
Gisle> handler since the library use eval{ ....die ...} a lot to implement
Gisle> exceptions.  You will confuse the catch-code that tries to match
Gisle> values of $@.

A properly written library is going to have to have:

	eval {
		local($SIG{__DIE__});
		...;
	}

and if you *don't* have that, *you* are asking for trouble, since the
user is free to write whatever they want as a die handler.  It just
happens that this die handler is (mostly) compatible with yours, but
an earlier version did indeed break it (where I just logged the
message and then did an "exit 1").

>> &handle_connection($slave) while $slave = $master->accept;
>> exit 0;

Gisle> For some reason this daemon would only handle one connection when I
Gisle> tried it.  I don't know why it happended.

Hmm.  handle_connection is supposed to fork and return.  It was
working for me.

>> } elsif (not $url->rel->netloc) {

Gisle> What good does the "rel" call do?

Well, you "abs" the incoming URL against a base of the URL of the
server.  This means that I cannot detect someone using the server as
an ordinary server (not a proxy server), which would appear as a fetch
for a non-netloc'ed "/fred/barney" whatever.  But $url is already
http://thishost:1234/fred/barney, meaning that I'd have to know that I
was /thishost:1234 way down in the guts.  But then I got this neat
idea to call ->rel which gives me the /fred/barney again.  If I ask
for the netloc of that, it's undef.

This also more-or-less prevents the proxy server from being asked to
fetch something below itself, which would cause a pretty weird
meltdown as it kept looping back and looping back.

Gisle> If you want to be more fancy (and efficient), you should start to send
Gisle> the response back to the client before you have received all the
Gisle> content from the origin server. Something like this ought to do the
Gisle> trick:

Gisle>    $headers_sent = 0;

Gisle>    $ua->request($req,
Gisle>           sub {
Gisle>              my($content, $response) = @_;
Gisle>              unless ($headers_sent++) {
Gisle> 	         $response->remove_header(qw(Set-Cookie));  # or whatever
Gisle>                  $connection->send_response($response);
Gisle>              }
Gisle>              $connection->print($content);
Gisle>           });

Gisle> This should help a lot when large files pass though the proxy.

Yes, that sounds much better.  However, my ultimate goal for the
commmunity service project is a caching filtering proxyserver, so I
can't use measures such as that.

Thanks for the code review.

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me