Cleanup #800
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent fa44921 · Browse files · 1 file·+17 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+17 added−14 removed
- other:+17 added−14 removed
Provenance
- Commit
f65b790- Tree
0477fb0- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
fa44921
1 file changed+17 added−14 removed
modified:requests/auth.py+17 added-14 removed
76 unmodified lines77 contenttype = r.headers.get('Content-Type', None)78 # extract_params will not give params unless the body is a properly79 # formatted string, a dictionary or a list of 2-tuples.80 decoded_body = extract_params(r.data)81- if (contenttype is None or contenttype.lower() == "application/x-www-form-urlencoded")\82- and decoded_body != None:81+82+ _ct = (contenttype is None)83+ _ct = _ct or contenttype.lower() == CONTENT_TYPE_FORM_URLENCODED84+85+ if _ct and decoded_body != None:86 # extract_params can only check the present r.data and does not know87 # of r.files, thus an extra check is performed. We know that88 # if files are present the request will not have89 # Content-type: x-www-form-urlencoded. We guess it will have67 unmodified lines157 nonce = self.chal['nonce']158 qop = self.chal.get('qop')159 algorithm = self.chal.get('algorithm', 'MD5')160 opaque = self.chal.get('opaque', None)158- 161+162 algorithm = algorithm.upper()163 # lambdas assume digest modules are imported at the top level164 if algorithm == 'MD5':165 def md5_utf8(x):8 unmodified lines174 return hashlib.sha1(x).hexdigest()175 hash_utf8 = sha_utf8176 # XXX MD5-sess177 KD = lambda s, d: hash_utf8("%s:%s" % (s, d))175- 178+179 if hash_utf8 is None:180 return None178- 181+182 # XXX not implemented yet183 entdig = None184 p_parsed = urlparse(url)185 path = p_parsed.path186 if p_parsed.query:187 path += '?' + p_parsed.query185- 188+189 A1 = '%s:%s:%s' % (self.username, realm, self.password)190 A2 = '%s:%s' % (method, path)188- 191+192 if qop == 'auth':193 if nonce == self.last_nonce:194 self.nonce_count += 1195 else:196 self.nonce_count = 1194- 197+198 ncvalue = '%08x' % self.nonce_count199 s = str(self.nonce_count).encode('utf-8')200 s += nonce.encode('utf-8')201 s += time.ctime().encode('utf-8')202 s += os.urandom(8)200- 203+204 cnonce = (hashlib.sha1(s).hexdigest()[:16])205 noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, hash_utf8(A2))206 respdig = KD(hash_utf8(A1), noncebit)207 elif qop is None:208 respdig = KD(hash_utf8(A1), "%s:%s" % (nonce, hash_utf8(A2)))209 else:210 # XXX handle auth-int.211 return None209- 212+213 self.last_nonce = nonce211- 214+215 # XXX should the partial digests be encoded too?216 base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \217 'response="%s"' % (self.username, realm, nonce, path, respdig)218 if opaque:2 unmodified lines221 base += ', digest="%s"' % entdig222 base += ', algorithm="%s"' % algorithm223 if qop:224 base += ', qop=auth, nc=%s, cnonce="%s"' % (ncvalue, cnonce)222- 225+226 return 'Digest %s' % (base)227 228 def handle_401(self, r):229 """Takes the given response and tries digest-auth, if needed."""5 unmodified lines235 if 'digest' in s_auth.lower() and num_401_calls < 2:236 237 self.chal = parse_dict_header(s_auth.replace('Digest ', ''))238 236- # Consume content and release the original connection 239+ # Consume content and release the original connection240 # to allow our new request to reuse the same one.241 r.content242 r.raw.release_conn()243 8 unmodified lines252 253 def __call__(self, r):254 # If we have a saved nonce, skip the 401255 if self.last_nonce:253- r.headers['Authorization'] = self.build_digest_header(r.method, r.url) 256+ r.headers['Authorization'] = self.build_digest_header(r.method, r.url)257 r.register_hook('response', self.handle_401)258 return r259 260 121 unmodified lines