Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Forcing LWP-UserAgent to use a proxy server without modifying code

by mvaline (Friar)
on Jan 04, 2008 at 17:54 UTC ( [id://660443]=perlquestion: print w/replies, xml ) Need Help??

mvaline has asked for the wisdom of the Perl Monks concerning the following question:

My environment requires an HTTP proxy server. I am using modules that use LWP::UserAgent internally, but do not provide a programmatic way to set a proxy server.

I know that LWP::Simple will pay attention to the http_proxy environment variable, but LWP::UserAgent does not appear to do so.

Is there a way I can force LWP::UserAgent within the modules I am using to use our proxy server without modifying their source code?

Replies are listed 'Best First'.
Re: Forcing LWP-UserAgent to use a proxy server without modifying code
by Corion (Patriarch) on Jan 04, 2008 at 18:04 UTC

    Reading the LWP::UserAgent documentation, you will find that $ENV{HTTP_PROXY} is only evaluated if you call the $ua->env_proxy method. You can either modify your code or modify (say) LWP/UserAgent.pm so that the ->new constructor automatically calls env_proxy.

      I successfully modified the source to LWP/UserAgent as you suggested. Line 98 of the version of UserAgent.pm that I have calls the env_proxy method if the correct parameter has been passed to it. I simply modified it so it always calls the method. Thanks!
        Umph. And the next time you get a New version of LWP from CPAN? What happens to your change on line 98, then?

        Hand-editing CPAN modules is a chancy proposition at best. You are setting yourself up to strange bugs in the future; and, what have you just done to the other folks using LWP::UserAgent? Suddenly their code breaks because they aren't using a proxy...? Also, is this code going to be run on other machines? Are you going to make the change to line 98 everywhere? Sigh.

        I'd strongly suggest that you rethink this policy. Make a local copy of LWP::UA in your ~home directory and edit *that* if you have absolutely to. Changing the universal copy of a CPAN module -- this is just Not A Good Idea.

        ----
        I Go Back to Sleep, Now.

        OGB

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://660443]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found