python-ntlm icon indicating copy to clipboard operation
python-ntlm copied to clipboard

Fails on missing content-length

Open mikeyg123 opened this issue 8 years ago • 1 comments

I'm trying to do a simple get request but I get the TypeError below:

import urllib2
from ntlm import HTTPNtlmAuthHandler

pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
pm.add_password(None, url, 'DOMAIN\\user', 'pw')
auth = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pm)
op = urllib2.build_opener(auth)
urllib2.install_opener(op)

url = "http://server/path"
response = urllib2.urlopen(url)

Traceback (most recent call last):
  File "/bin/python2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/bin/python2.7/lib/python2.7/urllib2.py", line 435, in open
    response = meth(req, response)
  File "/bin/python2.7/lib/python2.7/urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "/bin/python2.7/lib/python2.7/urllib2.py", line 467, in error
    result = self._call_chain(*args)
  File "/bin/python2.7/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/lib/python2.7/site-packages/ntlm/HTTPNtlmAuthHandler.py", line 121, in http_error_401
    return self.http_error_authentication_required('www-authenticate', req, fp, headers)
  File "/lib/python2.7/site-packages/ntlm/HTTPNtlmAuthHandler.py", line 44, in http_error_authentication_required
    return self.retry_using_http_NTLM_auth(req, auth_header_field, None, headers)
  File "/lib/python2.7/site-packages/ntlm/HTTPNtlmAuthHandler.py", line 81, in retry_using_http_NTLM_auth
    r._safe_read(int(r.getheader('content-length')))
TypeError: int() argument must be a string or a number, not 'NoneType'

The same url works with chrome and ie. (Using the Requests library gets further through the handshake but it doesn't provide the correct response to the challenge.)

Fairly obvious that the server is not returning a content length - but is there any way around this? Why does the HTTPNtlmAuthHandler rely on this header? Thanks MikeG

mikeyg123 avatar Jun 09 '17 08:06 mikeyg123

Update: Modifying the retry_using_http_NTLM_auth() method so that it skips reading the response if there's no content-length header seems to fix it. I'm not 100% sure of the implications of this however - why is the read necessary at all? Could you make this small change?

Thank you

mikeyg123 avatar Jun 09 '17 09:06 mikeyg123