Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

lwp proxy problem

by dannoura (Pilgrim)
on Apr 27, 2004 at 15:36 UTC ( #348540=perlquestion: print w/replies, xml ) Need Help??
dannoura has asked for the wisdom of the Perl Monks concerning the following question:


I'm trying to work through my ISP provider's proxy, but for some reason every time I issue a request with it I get the ISP's homepage, which is obviously not the page I requested. I tried two scripts. The first is:

use LWP::Simple qw(&getstore $ua); $ua->proxy(http => ''); my $url=''; my $file='test.html'; print "success" if (getstore($url, $file));

The second is:

use LWP::UserAgent; open WFH, ">test.html" or die($!); my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0"); $ua->env_proxy; $ua->proxy('http', ''); $ua->timeout(10); my $response = $ua->get(''); if ($response->is_success) { print WFH $response->content; } else { die $response->status_line; }

Any ideas as to the reason?


Any comments about coding style are welcome.

Replies are listed 'Best First'.
Re: lwp proxy problem
by bronto (Priest) on Apr 27, 2004 at 15:52 UTC

    I downloaded the file using wget, getting:

    It is easy to read, so I think that once you know your IP address you can pick a proxy from the list by hand

    Or you can write a script yourself to parse the pac file, at the moment I have no time to sketch one, sorry :-(

    Update: see HTTP::ProxyAutoConfig

    Update: try this and tell me if it works. It seems I can't use that proxy, so I couldn't make any test myself, apart from checking that it parses and runs correctly.

    Update: actually, that won't work, since you need to parse the return value from FindProxy and pass it to $ua->proxy only if it matches /PROXY: host:port/ (after cleaning up the PROXY: parts, of course). Unfortunately, I am having no luck in making this module work (the FindProxyForURL function doesn't get defined). I'll update the above script as soon as I make sense of the module...

    Update: Ok, I checked with ptkdb and saw that:

    • the module tries to parse the function contained in the pac file (javascript) to convert it to Perl
    • the parser in the subroutine is very buggy, and probabily expects a certain format for the PAC file, that is: is not general enough to parse any pac
    • the converted function is finally eval'ed, but no check is done on the $@ variable. Therefore, the user doesn't know if the pac failed to parse until he tries to use the FindProxy* functions and sees them fail because FindProxyForURL doesn't exist

    Possible solutions:

    • write a bug report for the module and wait for the author to patch his module
    • write a better parsing routine, or ask for one (I can't write one, I am a JavaScript illiterate :-)
    • rewrite the parser routine using one of the JavaScript modules from CPAN. This one, for example, uses the SpiderMonkey engine from Mozilla, and I expect that it will know how to parse the PAC

    Sorry, I can't help further :-(


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz

      Thanks for your help. It's not working, though. I get two errors:

      Useless use of a constant in void context at C:/perl/site/lib/HTTP/Pro line 367. Undefined subroutine &HTTP::ProxyAutoConfig::FindProxyForURL called at + C:/perl/site/lib/HTTP/ line 131.


      Any comments about coding style are welcome.
Re: lwp proxy problem
by cyocum (Curate) on Apr 27, 2004 at 15:46 UTC

    This is a completely off the cuff comment but the file you point to in $ua->proxy(http => ''); is an automatic configuration script, which is used in web browsers. I do not think that LWP supports that.

Re: lwp proxy problem
by matija (Priest) on Apr 27, 2004 at 15:56 UTC
    As cyocum correctly deduced, your problem is that you're setting the proxy to the configuration file.

    That file is Javascript, which is supposed to be executed by the browser on a URL-by-URL basis to determine which proxy to use for which URL.

    LWP does not (and probably never will) support that.

    You need to read the file, understand it, and select a proxy from it yourself: $ua->proxy('http',''); for example.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://348540]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-05-24 08:06 GMT
Find Nodes?
    Voting Booth?