Issue #2334 - HTTPDigestAuth - Renamed thread local attribute
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Per @sigmavirus24 suggestion: private and more readable.
authored by exvito · committed by exvito · parent 36dea43 · Browse files · 1 file·+30 added−31 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+30 added−31 removed
- other:+30 added−31 removed
Provenance
- Commit
5a69137- Tree
5efb486- Author
exvito <ex.vitorino@gmail.com>- Committer
exvito <ex.vitorino@gmail.com>- Parent
36dea43
1 file changed+30 added−31 removed
modified:requests/auth.py+30 added-31 removed
63 unmodified lines64 def __init__(self, username, password):65 self.username = username66 self.password = password67 # Keep state in per-thread local storage68- self.tl = threading.local()68+ self._thread_local = threading.local()69 70 def init_per_thread_state(self):71 # Ensure state is initialized just once per-thread72- if not hasattr(self.tl, 'init'):73- self.tl.init = True74- self.tl.last_nonce = ''75- self.tl.nonce_count = 076- self.tl.chal = {}77- self.tl.pos = None78- self.tl.num_401_calls = None72+ if not hasattr(self._thread_local, 'init'):73+ self._thread_local.init = True74+ self._thread_local.last_nonce = ''75+ self._thread_local.nonce_count = 076+ self._thread_local.chal = {}77+ self._thread_local.pos = None78+ self._thread_local.num_401_calls = None79 80 def build_digest_header(self, method, url):81 82- realm = self.tl.chal['realm']83- nonce = self.tl.chal['nonce']84- qop = self.tl.chal.get('qop')85- algorithm = self.tl.chal.get('algorithm')86- opaque = self.tl.chal.get('opaque')82+ realm = self._thread_local.chal['realm']83+ nonce = self._thread_local.chal['nonce']84+ qop = self._thread_local.chal.get('qop')85+ algorithm = self._thread_local.chal.get('algorithm')86+ opaque = self._thread_local.chal.get('opaque')87 88 if algorithm is None:89 _algorithm = 'MD5'90 else:29 unmodified lines120 121 HA1 = hash_utf8(A1)122 HA2 = hash_utf8(A2)123 124- if nonce == self.tl.last_nonce:125- self.tl.nonce_count += 1124+ if nonce == self._thread_local.last_nonce:125+ self._thread_local.nonce_count += 1126 else:127- self.tl.nonce_count = 1128- ncvalue = '%08x' % self.tl.nonce_count129- s = str(self.tl.nonce_count).encode('utf-8')127+ self._thread_local.nonce_count = 1128+ ncvalue = '%08x' % self._thread_local.nonce_count129+ s = str(self._thread_local.nonce_count).encode('utf-8')130 s += nonce.encode('utf-8')131 s += time.ctime().encode('utf-8')132 s += os.urandom(8)133 11 unmodified lines145 else:146 # XXX handle auth-int.147 return None148 149- self.tl.last_nonce = nonce149+ self._thread_local.last_nonce = nonce150 151 # XXX should the partial digests be encoded too?152 base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \153 'response="%s"' % (self.username, realm, nonce, path, respdig)10 unmodified lines164 165 def handle_redirect(self, r, **kwargs):166 """Reset num_401_calls counter on redirects."""167 if r.is_redirect:168- self.tl.num_401_calls = 1168+ self._thread_local.num_401_calls = 1169 170 def handle_401(self, r, **kwargs):171 """Takes the given response and tries digest-auth, if needed."""172 173- if self.tl.pos is not None:173+ if self._thread_local.pos is not None:174 # Rewind the file position indicator of the body to where175 # it was to resend the request.176- r.request.body.seek(self.tl.pos)177- num_401_calls = self.tl.num_401_calls176+ r.request.body.seek(self._thread_local.pos)177 s_auth = r.headers.get('www-authenticate', '')178 180- if 'digest' in s_auth.lower() and num_401_calls < 2:179+ if 'digest' in s_auth.lower() and self._thread_local.num_401_calls < 2:180 182- self.tl.num_401_calls += 1181+ self._thread_local.num_401_calls += 1182 pat = re.compile(r'digest ', flags=re.IGNORECASE)184- self.tl.chal = parse_dict_header(pat.sub('', s_auth, count=1))183+ self._thread_local.chal = parse_dict_header(pat.sub('', s_auth, count=1))184 185 # Consume content and release the original connection186 # to allow our new request to reuse the same one.187 r.content9 unmodified lines197 _r.request = prep198 199 return _r200 202- self.tl.num_401_calls = 1201+ self._thread_local.num_401_calls = 1202 return r203 204 def __call__(self, r):205 # Initialize per-thread state, if needed206 self.init_per_thread_state()207 # If we have a saved nonce, skip the 401209- if self.tl.last_nonce:208+ if self._thread_local.last_nonce:209 r.headers['Authorization'] = self.build_digest_header(r.method, r.url)210 try:212- self.tl.pos = r.body.tell()211+ self._thread_local.pos = r.body.tell()212 except AttributeError:213 # In the case of HTTPDigestAuth being reused and the body of214 # the previous request was a file-like object, pos has the215 # file position of the previous body. Ensure it's set to216 # None.218- self.tl.pos = None217+ self._thread_local.pos = None218 r.register_hook('response', self.handle_401)219 r.register_hook('response', self.handle_redirect)221- self.tl.num_401_calls = 1220+ self._thread_local.num_401_calls = 1221 222 return r