Http and basic authentication

Hi everyone.
I’m quite new to Ruby and I’m not exactly familiar with the mailing
lists and everything so scream if I’m abusing the list.
I have a problem with Net::HTTP and it’s use of headers (well, it might
be that the problem is in protocol.rb or that the problem is just me and
my grasp of programming, we’ll see).
The problem:
I want to access a site (which is provided by the firmware on my DSL
router) that requires basic authentication.

The environment:
ruby 1.6.8-8 on W2K

I use the following code

http_connection = Net::HTTP.new(‘target’).start()
headers ={}
headers[‘Authorization’] = 'Basic '+ [“id:pass”].pack(‘m’).strip
begin
resp, data = http_connection.get("/", headers)
#rescue

end

and I get the following errors

f:/devlab/ruby/lib/ruby/1.6/net/protocol.rb:221:in error!': 401 "Authorization Required" (Net::ProtoFatalError) from f:/devlab/ruby/lib/ruby/1.6/net/http.rb:1217:invalue’
from f:/devlab/ruby/lib/ruby/1.6/net/http.rb:605:in `get’

Snooping on my network traffic I see the headers in a Continuation
message so I am sending them, just not correctly :).
Shouldn’t a 401 response produce a Net::ProtoRetriableError ?
Can anybody help me get past this?
Thanks,
V.-

···


http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.

Not sure if this helps, but I use this sample code for HTTP
Authentication…

require “net/http”
require “base64”

my_id = me
my_pwd = password
auth = "Basic " + encode64( “#{my_id}:#{my_pwd}” )

url = “/some.login.required.html”
site = www.somesite.com
port = 80

req = Net::HTTP.new( site, port )
req.get( url, ‘Authorization’ => auth ) do |r|

do something with “r”

end

  • Daemon

Damphyr damphyr@freemail.gr wrote in
news:3E837EA1.3020007@freemail.gr:

···

Hi everyone.
I’m quite new to Ruby and I’m not exactly familiar with the mailing
lists and everything so scream if I’m abusing the list.
I have a problem with Net::HTTP and it’s use of headers (well, it
might be that the problem is in protocol.rb or that the problem is
just me and my grasp of programming, we’ll see).
The problem:
I want to access a site (which is provided by the firmware on my DSL
router) that requires basic authentication.

The environment:
ruby 1.6.8-8 on W2K

I use the following code

http_connection = Net::HTTP.new(‘target’).start()
headers ={}
headers[‘Authorization’] = 'Basic '+ [“id:pass”].pack(‘m’).strip
begin
resp, data = http_connection.get(“/”, headers)
#rescue

end

and I get the following errors

f:/devlab/ruby/lib/ruby/1.6/net/protocol.rb:221:in error!': 401 "Authorization Required" (Net::ProtoFatalError) from f:/devlab/ruby/lib/ruby/1.6/net/http.rb:1217:in value’
from f:/devlab/ruby/lib/ruby/1.6/net/http.rb:605:in `get’

Snooping on my network traffic I see the headers in a Continuation
message so I am sending them, just not correctly :).
Shouldn’t a 401 response produce a Net::ProtoRetriableError ?
Can anybody help me get past this?
Thanks,
V.-


http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.

daemon wrote:

Not sure if this helps, but I use this sample code for HTTP
Authentication…

require “net/http”
require “base64”

my_id = me
my_pwd = password
auth = "Basic " + encode64( “#{my_id}:#{my_pwd}” )

url = “/some.login.required.html”
site = www.somesite.com
port = 80

req = Net::HTTP.new( site, port )
req.get( url, ‘Authorization’ => auth ) do |r|

do something with “r”

end

Well, I went a few steps further today.
I was using this code:

http_connection = Net::HTTP.new(‘site’).start()
headers ={}
headers[‘User-agent’]=HEADER_USER_AGENT
headers[‘Authorization’] = 'Basic '+ [“#{user}:#{pass}”].pack(‘m’)
resp, data = http_connection.get(thePage, headers)

It’s actually the same if you take into account that
[“#{user}:#{pass}”].pack(‘m’) adds a \n at the end of the string.
So we need to use [“#{user}:#{pass}”].pack(‘m’).chomp (or the base64
encoding module - much better).
It still doesn’t work though.
I switched to http-access2 (by Hiroshi Nakamura) and the following code
works:

headers ={}
headers[‘User-agent’]=HEADER_USER_AGENT
headers[‘Authorization’]='Basic '+ [“#{user}:#{pass}”].pack(‘m’).chomp
#this is the standard request with the require fields
request = “/somepage”
uri=SERVICE_ADDRESS+request
clnt = HTTPAccess2::Client.new(nil)
resp = clnt.get(uri,nil,headers)

The only difference I can see is at the ammount of data that are sent
during the request (and the missing content-length: header for the
Net::HTTP request). The Net::HTTP does not send the headers immediately,
but does it in a Continuation packet afterwards. Can’t say I understand it.

The new problem I have is that http-access2 does not handle wrong
passwords gracefully.It bombs out with ‘Unexpected EOF’ when reading
the headers of a 401 Responce. At least with Net::HTTP the error was
‘401 “Authorization Required” (Net::ProtoFatalError)’ and it was easy to
handle it.
Well, I did try your code too - It still doesn’t work :(.
And I guess it’s the router’s fault, because I just used it on a proper
server that required authentication and it worked fine. Damn, I hate it
when hardware screws up.
V.-

···


http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.