def handle_407(self, r, **kwargs): if r.status_code == 407 and self.stale_rejects < 2: pat = re.compile(r'digest ', flags=re.IGNORECASE) if "proxy-authenticate" not in r.headers: raise IOError( "proxy server violated RFC 7235:" "407 response MUST contain header proxy-authenticate") self.chal = parse_dict_header( pat.sub('', r.headers['proxy-authenticate'], count=1)) if 'Proxy-Authorization' in r.request.headers and 'stale' in self.chal: if self.chal['stale'].lower() == 'true': # try again self.stale_rejects += 1 elif self.chal['stale'].lower() == 'false': raise IOError("User or password is invalid") r.content r.raw.release_conn() prep = r.request.copy() extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) prep.headers['Proxy-Authorization'] = self.build_digest_header( prep.method, prep.url) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r else: # give up authenticate return r
def handle_407(self, r, **kwargs): """Handle HTTP 407 only once, otherwise give up :param r: current response :returns: responses, along with the new response """ if r.status_code == 407 and self.stale_rejects < 2: pat = re.compile(r'digest ', flags=re.IGNORECASE) if "proxy-authenticate" not in r.headers: raise IOError( "proxy server violated RFC 7235:" "407 response MUST contain header proxy-authenticate") self.chal = parse_dict_header( pat.sub('', r.headers['proxy-authenticate'], count=1)) # if we present the user/passwd and still get rejected # http://tools.ietf.org/html/rfc2617#section-3.2.1 if 'Proxy-Authorization' in r.request.headers and\ 'stale' in self.chal: if self.chal['stale'].lower() == 'true': # try again self.stale_rejects += 1 # wrong user/passwd elif self.chal['stale'].lower() == 'false': raise IOError("User or password is invalid") # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.close() prep = r.request.copy() extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) prep.headers['Proxy-Authorization'] = self.build_digest_header( prep.method, prep.url) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r else: # give up authenticate return r