comparison mercurial/url.py @ 51830:208698117124

http: use urllib's cookie handler Split the logic for loading the cookies based on the configuration in a helper function and otherwise use the library implementation directly.
author Joerg Sonnenberger <joerg@bec.de>
date Sun, 30 Jun 2024 14:16:43 +0200
parents b08de326bee4
children 607e94e01851
comparison
equal deleted inserted replaced
51829:c1ed5ee2ad82 51830:208698117124
453 return self.parent.open(req) 453 return self.parent.open(req)
454 else: 454 else:
455 return None 455 return None
456 456
457 457
458 class cookiehandler(urlreq.basehandler): 458 def load_cookiejar(ui):
459 def __init__(self, ui): 459 cookiefile = ui.config(b'auth', b'cookiefile')
460 self.cookiejar = None 460 if not cookiefile:
461 461 return
462 cookiefile = ui.config(b'auth', b'cookiefile') 462 cookiefile = util.expandpath(cookiefile)
463 if not cookiefile: 463 try:
464 return 464 cookiejar = util.cookielib.MozillaCookieJar(
465 465 pycompat.fsdecode(cookiefile)
466 cookiefile = util.expandpath(cookiefile) 466 )
467 try: 467 cookiejar.load()
468 cookiejar = util.cookielib.MozillaCookieJar( 468 return cookiejar
469 pycompat.fsdecode(cookiefile) 469 except util.cookielib.LoadError as e:
470 ) 470 ui.warn(
471 cookiejar.load() 471 _(
472 self.cookiejar = cookiejar 472 b'(error loading cookie file %s: %s; continuing without '
473 except util.cookielib.LoadError as e: 473 b'cookies)\n'
474 ui.warn( 474 )
475 _( 475 % (cookiefile, stringutil.forcebytestr(e))
476 b'(error loading cookie file %s: %s; continuing without ' 476 )
477 b'cookies)\n'
478 )
479 % (cookiefile, stringutil.forcebytestr(e))
480 )
481
482 def http_request(self, request):
483 if self.cookiejar:
484 self.cookiejar.add_cookie_header(request)
485
486 return request
487
488 def https_request(self, request):
489 if self.cookiejar:
490 self.cookiejar.add_cookie_header(request)
491
492 return request
493 477
494 478
495 class readlinehandler(urlreq.basehandler): 479 class readlinehandler(urlreq.basehandler):
496 def http_response(self, request, response): 480 def http_response(self, request, response):
497 class readlineresponse(response.__class__): 481 class readlineresponse(response.__class__):
573 557
574 handlers.extend( 558 handlers.extend(
575 (httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr)) 559 (httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))
576 ) 560 )
577 handlers.extend([h(ui, passmgr) for h in handlerfuncs]) 561 handlers.extend([h(ui, passmgr) for h in handlerfuncs])
578 handlers.append(cookiehandler(ui)) 562 handlers.append(urlreq.httpcookieprocessor(cookiejar=load_cookiejar(ui)))
579 handlers.append(readlinehandler()) 563 handlers.append(readlinehandler())
580 opener = urlreq.buildopener(*handlers) 564 opener = urlreq.buildopener(*handlers)
581 565
582 # keepalive.py's handlers will populate these attributes if they exist. 566 # keepalive.py's handlers will populate these attributes if they exist.
583 opener.requestscount = 0 567 opener.requestscount = 0