LWP::UserAgent and basic authentication

T

Thomas Kratz

I have a third party webserver that expects xml datasets to be uploaded
one per request (buggy concept, but not mine). The URL needs basic
authentication. If I understand the docs and sources of libwww correctly,
LWP::UserAgent is supposed to work like this:

The request method of the LWP::UserAgent object first sends a
simple_request without authentication. If the response indicates that
basic authorization is required and I supplied the object with
credentials, the request is passed to LWP::Authen::Basic::authenticate,
which will rerequest the URL with proper authentication information.

The problem is, that I will have to do a lot of requests and the webserver
chokes on the many unauthorized requests (non standard webserver, buggy
handling). Is there a possibility to skip the first request and let
LWP::Authen::Basic handle the request directly?

I could of cause inherit from LWP::UserAgent and roll my own request
method. But there could be somewhat more elegant.

TIA
Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
B

Brian Wakem

Thomas said:
I have a third party webserver that expects xml datasets to be uploaded
one per request (buggy concept, but not mine). The URL needs basic
authentication. If I understand the docs and sources of libwww
correctly, LWP::UserAgent is supposed to work like this:

The request method of the LWP::UserAgent object first sends a
simple_request without authentication. If the response indicates that
basic authorization is required and I supplied the object with
credentials, the request is passed to LWP::Authen::Basic::authenticate,
which will rerequest the URL with proper authentication information.

The problem is, that I will have to do a lot of requests and the
webserver chokes on the many unauthorized requests (non standard
webserver, buggy handling). Is there a possibility to skip the first
request and let LWP::Authen::Basic handle the request directly?

I could of cause inherit from LWP::UserAgent and roll my own request
method. But there could be somewhat more elegant.

TIA
Thomas


I have not read the docs but I do it like this -

my $ua = LWP::UserAgent->new;
$ua->agent("MyAgent/1.0");
my $req = HTTP::Request->new(GET => $url);
$req->authorization_basic('user', 'pass');
my $res = $ua->request($req);

And I can say without any doubt (observing the access_log of the server
it speaks to) that it passes the user/pass *straight away*.
 
T

Thomas Kratz

Brian said:
I have not read the docs but I do it like this -

my $ua = LWP::UserAgent->new;
$ua->agent("MyAgent/1.0");
my $req = HTTP::Request->new(GET => $url);
$req->authorization_basic('user', 'pass');
my $res = $ua->request($req);

authorization_basic does not work for me, because I have to set a special
realm with $ua->credentials.
And I can say without any doubt (observing the access_log of the server
it speaks to) that it passes the user/pass *straight away*.

Perhaps I misread the code of the request method. But the first thing it
does is call $ua->simple_request, and I cannot see any call to
LWP::Authen::Basic there.

Could you please add a:
use LWP::Debug qw(+);
at the top of your code

Then I see something like this on STDOUT for each request:

LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: POST <URL>
LWP::UserAgent::_need_proxy: Not proxied
LWP::protocol::http::request: ()
LWP::protocol::collect: read 853 bytes
LWP::protocol::collect: read 2219 bytes
LWP::UserAgent::request: Simple response: Unauthorized
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: POST <URL>
LWP::UserAgent::_need_proxy: Not proxied
LWP::protocol::http::request: ()
LWP::protocol::collect: read 858 bytes
LWP::protocol::collect: read 905 bytes
LWP::UserAgent::request: Simple response: OK

Thomas


--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
B

Brian Wakem

Thomas said:
authorization_basic does not work for me, because I have to set a special
realm with $ua->credentials.


Perhaps I misread the code of the request method. But the first thing it
does is call $ua->simple_request, and I cannot see any call to
LWP::Authen::Basic there.

Could you please add a:
use LWP::Debug qw(+);
at the top of your code


Have done. I get -


LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: GET <url>
LWP::UserAgent::_need_proxy: Not proxied
LWP::protocol::http::request: ()
LWP::protocol::collect: read 841 bytes
LWP::protocol::collect: read 976 bytes
LWP::protocol::collect: read 1017 bytes
LWP::protocol::collect: read 426 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 171 bytes
LWP::protocol::collect: read 1017 bytes
LWP::protocol::collect: read 47 bytes
LWP::protocol::collect: read 7 bytes
LWP::protocol::collect: read 186 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 378 bytes
LWP::protocol::collect: read 1016 bytes
LWP::protocol::collect: read 46 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 2786 bytes
LWP::protocol::collect: read 1016 bytes
LWP::protocol::collect: read 534 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 850 bytes
LWP::protocol::collect: read 591 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1072 bytes
LWP::protocol::collect: read 369 bytes
LWP::protocol::collect: read 200 bytes
LWP::protocol::collect: read 15 bytes
LWP::UserAgent::request: Simple response: OK
 
T

Thomas Kratz

Brian said:
Have done. I get -


LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: GET <url>
LWP::UserAgent::_need_proxy: Not proxied
LWP::protocol::http::request: ()
LWP::protocol::collect: read 841 bytes
LWP::protocol::collect: read 976 bytes
LWP::protocol::collect: read 1017 bytes
LWP::protocol::collect: read 426 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 171 bytes
LWP::protocol::collect: read 1017 bytes
LWP::protocol::collect: read 47 bytes
LWP::protocol::collect: read 7 bytes
LWP::protocol::collect: read 186 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 378 bytes
LWP::protocol::collect: read 1016 bytes
LWP::protocol::collect: read 46 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 2786 bytes
LWP::protocol::collect: read 1016 bytes
LWP::protocol::collect: read 534 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 850 bytes
LWP::protocol::collect: read 591 bytes
LWP::protocol::collect: read 1448 bytes
LWP::protocol::collect: read 1072 bytes
LWP::protocol::collect: read 369 bytes
LWP::protocol::collect: read 200 bytes
LWP::protocol::collect: read 15 bytes
LWP::UserAgent::request: Simple response: OK

Thanks! I don't know exactly, why it behaves differently on my side.
But I derived my own UA class from LWP::UserAgent and changed the request
method, so that it only fails once at the beginning and uses the working
authorisation for subsequent calls to request.
Works without problem.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top