@@ -210,12 +210,14 @@ def _retry():
210
210
211
211
# Handle authentication challenge
212
212
if status == 401 :
213
- auth_header = resp .getheader ('www-authenticate' , '' )
214
- if auth_header :
215
- if self ._authenticate (auth_header , headers , credentials ):
216
- resp .read () # read the 401 response
217
- resp = _try_request ()
218
- status = resp .status
213
+ # Assume basic auth (CouchDB 0.11.x doesn't send a www-authenticate
214
+ # header).
215
+ authorization = basic_auth (credentials )
216
+ if authorization :
217
+ resp .read () # read the 401 response
218
+ headers ['Authorization' ] = authorization
219
+ resp = _try_request ()
220
+ status = resp .status
219
221
220
222
# Handle conditional response
221
223
if status == 304 and method in ('GET' , 'HEAD' ):
@@ -301,17 +303,6 @@ def _clean_cache(self):
301
303
ls = sorted (self .cache .iteritems (), key = cache_sort )
302
304
self .cache = dict (ls [- CACHE_SIZE [0 ]:])
303
305
304
- def _authenticate (self , info , headers , credentials ):
305
- # Naive Basic authentication support
306
- match = re .match (r'''(\w*)\s+realm=['"]([^'"]+)['"]''' , info )
307
- if match :
308
- scheme , realm = match .groups ()
309
- if scheme .lower () == 'basic' :
310
- headers ['Authorization' ] = 'Basic %s' % b64encode (
311
- '%s:%s' % credentials
312
- )
313
- return True
314
-
315
306
def _get_connection (self , url ):
316
307
scheme , host = urlsplit (url , 'http' , False )[:2 ]
317
308
self .lock .acquire ()
@@ -425,6 +416,12 @@ def extract_credentials(url):
425
416
return urlunsplit (parts ), (username , password )
426
417
427
418
419
+ def basic_auth (credentials ):
420
+ if credentials == (None , None ):
421
+ return None
422
+ return 'Basic %s' % b64encode ('%s:%s' % credentials )
423
+
424
+
428
425
def quote (string , safe = '' ):
429
426
if isinstance (string , unicode ):
430
427
string = string .encode ('utf-8' )
0 commit comments