comparison mercurial/url.py @ 37045:a708e1e4d7a8

url: support suppressing Accept header Sending this header automatically could interfere with future testing and client behavior. Let's add a knob to disable the behavior. We don't have a control for User-Agent because urllib will send it if we don't set something. I don't feel like hacking into the bowels of urllib to figure out how to suppress that. UA shouldn't be used for anything meaningful. So it shouldn't pose any problems beyond non-determinism (since the header has the Mercurial version in it). Differential Revision: https://phab.mercurial-scm.org/D2843
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 13 Mar 2018 10:34:36 -0700
parents 02221d6fb041
children f0b6fbea00cf
comparison
equal deleted inserted replaced
37044:d3a9036d9ae9 37045:a708e1e4d7a8
492 return request 492 return request
493 493
494 handlerfuncs = [] 494 handlerfuncs = []
495 495
496 def opener(ui, authinfo=None, useragent=None, loggingfh=None, 496 def opener(ui, authinfo=None, useragent=None, loggingfh=None,
497 loggingname=b's', loggingopts=None): 497 loggingname=b's', loggingopts=None, sendaccept=True):
498 ''' 498 '''
499 construct an opener suitable for urllib2 499 construct an opener suitable for urllib2
500 authinfo will be added to the password manager 500 authinfo will be added to the password manager
501 501
502 The opener can be configured to log socket events if the various 502 The opener can be configured to log socket events if the various
504 504
505 ``loggingfh`` denotes a file object to log events to. 505 ``loggingfh`` denotes a file object to log events to.
506 ``loggingname`` denotes the name of the to print when logging. 506 ``loggingname`` denotes the name of the to print when logging.
507 ``loggingopts`` is a dict of keyword arguments to pass to the constructed 507 ``loggingopts`` is a dict of keyword arguments to pass to the constructed
508 ``util.socketobserver`` instance. 508 ``util.socketobserver`` instance.
509
510 ``sendaccept`` allows controlling whether the ``Accept`` request header
511 is sent. The header is sent by default.
509 ''' 512 '''
510 handlers = [] 513 handlers = []
511 514
512 if loggingfh: 515 if loggingfh:
513 handlers.append(logginghttphandler(loggingfh, loggingname, 516 handlers.append(logginghttphandler(loggingfh, loggingname,
560 563
561 # This header should only be needed by wire protocol requests. But it has 564 # This header should only be needed by wire protocol requests. But it has
562 # been sent on all requests since forever. We keep sending it for backwards 565 # been sent on all requests since forever. We keep sending it for backwards
563 # compatibility reasons. Modern versions of the wire protocol use 566 # compatibility reasons. Modern versions of the wire protocol use
564 # X-HgProto-<N> for advertising client support. 567 # X-HgProto-<N> for advertising client support.
565 opener.addheaders.append((r'Accept', r'application/mercurial-0.1')) 568 if sendaccept:
569 opener.addheaders.append((r'Accept', r'application/mercurial-0.1'))
570
566 return opener 571 return opener
567 572
568 def open(ui, url_, data=None): 573 def open(ui, url_, data=None):
569 u = util.url(url_) 574 u = util.url(url_)
570 if u.scheme: 575 if u.scheme: