Permalink
Browse files

Fix infinite loop on wrong Digest Authentication

  • Loading branch information...
1 parent 8a055c5 commit c3e6c41fc164d4348f8ce197bd0075aff05637af @catwell catwell committed Apr 12, 2012
Showing with 28 additions and 1 deletion.
  1. +2 −0 requests/auth.py
  2. +12 −1 requests/models.py
  3. +14 −0 tests/test_requests.py
View
@@ -56,6 +56,8 @@ def __init__(self, username, password):
def handle_401(self, r):
"""Takes the given response and tries digest-auth, if needed."""
+ r.request.deregister_hook('response', self.handle_401)
+
s_auth = r.headers.get('www-authenticate', '')
if 'digest' in s_auth.lower():
View
@@ -408,7 +408,18 @@ def path_url(self):
def register_hook(self, event, hook):
"""Properly register a hook."""
- return self.hooks[event].append(hook)
+ self.hooks[event].append(hook)
+
+ def deregister_hook(self,event,hook):
+ """Deregister a previously registered hook.
+ Returns True if the hook existed, False if not.
+ """
+
+ try:
+ self.hooks[event].remove(hook)
+ return True
+ except ValueError:
+ return False
def send(self, anyway=False, prefetch=False):
"""Sends the request. Returns True of successful, False if not.
View
@@ -272,6 +272,20 @@ def test_DIGESTAUTH_HTTP_200_OK_GET(self):
r = get(url, session=s)
self.assertEqual(r.status_code, 200)
+ def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):
+
+ for service in SERVICES:
+
+ auth = HTTPDigestAuth('user', 'wrongpass')
+ url = service('digest-auth', 'auth', 'user', 'pass')
+
+ r = get(url, auth=auth)
+ self.assertEqual(r.status_code, 401)
+
+ s = requests.session(auth=auth)
+ r = get(url, session=s)
+ self.assertEqual(r.status_code, 401)
+
def test_POSTBIN_GET_POST_FILES(self):
for service in SERVICES:

0 comments on commit c3e6c41

Please sign in to comment.