kennethreitz/requests — pull request #1242 by oviboy (2013-03-11 08:56:27+00:00)
Title: HTTP Digest Auth case insensitive replacement of "Digest "

Hi,

I've encountered this error:

File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/api.py", line 87, in post
return request('post', url, data=data, **kwargs)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/sessions.py", line 279, in request
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/adapters.py", line 219, in send
r = self.build_response(request, resp)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/adapters.py", line 113, in build_response
response = dispatch_hook('response', req.hooks, response)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/hooks.py", line 39, in dispatch_hook
_hook_data = hook(hook_data)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/auth.py", line 155, in handle_401
r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url)
File "/sites/.ro-api/api/local/lib/python2.7/site-packages/requests/auth.py", line 69, in build_digest_header
nonce = self.chal['nonce']
KeyError: 'nonce'

While investigating i found that the site was sending me the following string:
"WWW-Authenticate: digest nonce="***********************", qop="auth", realm="***************", algorithm="md5", opaque="*******************=""
The parsed key in this case was: "digest nonce" and not "nonce", thus, the error above.

So I've modified the s_auth in requests/auth.py (line 154) string to replace "Digest " or "digest " with '' using re module and re.sub function. I've tested the feature and it works.

Regards,
Ovidiu Negrut