From d5df0c4bff2e8081d5edb2c49f18797f55598367 Mon Sep 17 00:00:00 2001 From: Leonardo Soto M Date: Thu, 13 Feb 2014 00:32:42 -0300 Subject: [PATCH] Avoid waiting for a WWW-Authenticate under HTTP Basic auth HTTPClient doesn't send any credentials unless it has seen a previous challenge for the requested URL in the form of a WWW-Authenticate header. Such challenge is sent by the server after it receives an unauthenticated request to such URL. The wasted roundtrip wouldn't be too bad if it were limited to the first request. However, httpclient only remembers the challenge for each individual URL and their 'child' URLs. This means that, for example, HTTP requests to /db/data/node/ will probably incur in a new wasteful rountrip for each different value of . The fix makes httpclient think that it has received a challenge for the root URL of the neo4j server. --- lib/neography/connection.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index 90e4667..d07241c 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -66,9 +66,18 @@ def log(path, body) end def authenticate(path = nil) - @client.set_auth(path, - @authentication[@authentication.keys.first][:username], - @authentication[@authentication.keys.first][:password]) unless @authentication.empty? + unless @authentication.empty? + auth_type = @authentication.keys.first + @client.set_auth( + path, + @authentication[auth_type][:username], + @authentication[auth_type][:password] + ) + # Force http client to always send auth credentials without + # waiting for WWW-Authenticate headers, thus saving one request + # roundtrip for each URI. Only works for HTTP basic auth. + @client.www_auth.basic_auth.challenge(configuration) if auth_type == 'basic_auth' + end end private