Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Escape user name and password in LWP proxy call.

by Anonymous Monk
on Oct 14, 2021 at 09:52 UTC ( [id://11137504]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, What is the best way to escape special characters like @, in username and password which is passed to the ua->proxy while using LWP::Useragent.
I tried using URI::escape but it doesn't seem to be working.
Thank you.

  • Comment on Escape user name and password in LWP proxy call.

Replies are listed 'Best First'.
Re: Escape user name and password in LWP proxy call.
by kcott (Archbishop) on Oct 14, 2021 at 12:52 UTC

    This is vague and contradictory. Please read "How do I post a question effectively?" and "SSCCE". Most of what follows is pure guesswork in terms of what you actually need.

    Use of non-interpolating quotes would normally suffice:

    $ perl -Mstrict -Mwarnings -E 'my ($u, $p) = (q{W$X}, q{Y@Z}); say for + $u, $p' W$X Y@Z

    Use of interpolating quotes would normally cause problems:

    $ perl -Mstrict -Mwarnings -E 'my ($u, $p) = (qq{W$X}, qq{Y@Z}); say f +or $u, $p' Possible unintended interpolation of @Z in string at -e line 1. Global symbol "$X" requires explicit package name (did you forget to d +eclare "my $X"?) at -e line 1. Global symbol "@Z" requires explicit package name (did you forget to d +eclare "my @Z"?) at -e line 1. Execution of -e aborted due to compilation errors.

    See also quotemeta.

    What you wrote about the "URI::escape" and (later) "uri_escape" doesn't make much sense. URI has neither an escape() function nor a uri_escape() function; URI::Escape has the latter. Even my best guess at fixing typos doesn't help:

    $ perl -Mstrict -Mwarnings -E 'use URI::Escape; my ($u, $p) = (q{W$X}, + q{Y@Z}); say for uri_escape($u), uri_escape($p)' W%24X Y%40Z

    We'll need some clarification on what you're actually doing here.

    — Ken

      Thank you for revert.
      Below is what I tried to do, but didn't work, yes that was typo, i meant URI::Escape
      Is it because i tried escaping IP too, do i really need to escape ip address.

      ##proxy authentication $PROXY_USER = uri_escape($PROXY_USER); $PROXY_PASSWD = uri_escape($PROXY_PASSWD); $PROXY_IP = uri_escape($PROXY_IP); my $proxy_url = "${PROXY_USER}:${PROXY_PASSWD}"."\@${PROXY_IP}:${PROXY +_PORT}"; ### oh, just realized, looks like forgot to add "http://" +. here before proxy user that's why it failed, can you just confirm t +he same. $ua->proxy(https => "$proxy_url");
      Thank you

        UPDATE: uri_escape, doesn't seem escaping (\) in username.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Escape user name and password in LWP proxy call.
by soonix (Canon) on Oct 15, 2021 at 11:20 UTC
    I liked to put user names, passwords and email addresses in my script(s) like this:
    my ($user, $pass, $mail) = qw(root $ecret someone@somewhe.re);
    This works with sigils, but there are problems with e.g. Unicode characters, therefore I now prefer
    use Config::Tiny; my $ini = Config::Tiny->read( 'whoamI.ini', 'encoding(Windows-1252)' ) +; my ($user, $pass, $mail) = ($ini->{Main}{user},$ini->{Main}{pass},$ini +->{Main}{mail});
    and have the data in a config file like
    [Main] user=root pass=$ecret mail=someone@somewhe.re
    This has the additional advantage, that you can share your script (or brag with it :-)) without compromising confidential data, and can avoid problems with foreign or other unusual characters (of course, you have to adapt the encoding(Windows-1252) part). Plus you don't need to change the script when the password changes.

      Hi soonix, thank you for the suggestion, while reading from the file, uri_escape from URI::Escape seems working fine even for "\".
      Regards

Re: Escape user name and password in LWP proxy call.
by Anonymous Monk on Oct 14, 2021 at 10:14 UTC
    Hi. What did you try exactly?

      Hi, I used uri_escape method for username and password , like uri_escape(username) and uri_escape(password) and used this escaped values in proxy call like , ua->proxy('http://domain\username@password: ipadress:port').
      Thank you

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found