From b1e6fe8df6ee62d1c7d8537b39f29648635c8bff Mon Sep 17 00:00:00 2001 From: "Alexis S. L. Carvalho" Date: Fri, 16 Mar 2007 00:22:53 -0300 Subject: [PATCH] Work around a urllib2 bug in Python < 2.4.2 When urllib2 base64-encodes the password needed for the Proxy-authorization header, it forgets to remove the trailing "\n". Later, a "\r\n" sequence is appended to every header, as required by the standard. Some proxies interpret the resulting "\n\r\n" sequence in the same way as "\r\n\r\n": end of headers. This usually doesn't cause trouble for this request, but when the proxy tries to read the next one, it thinks the request starts with some garbage and returns a "400 - Bad Request" error. --- mercurial/httprepo.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mercurial/httprepo.py b/mercurial/httprepo.py index 62ffa652e8..fbaf2333cf 100644 --- a/mercurial/httprepo.py +++ b/mercurial/httprepo.py @@ -76,6 +76,14 @@ def netlocunsplit(host, port, user=None, passwd=None): return userpass + '@' + hostport return hostport +# work around a bug in Python < 2.4.2 +# (it leaves a "\n" at the end of Proxy-authorization headers) +class request(urllib2.Request): + def add_header(self, key, val): + if key.lower() == 'proxy-authorization': + val = val.strip() + return urllib2.Request.add_header(self, key, val) + class httpsendfile(file): def __len__(self): return os.fstat(self.fileno()).st_size @@ -239,7 +247,7 @@ class httprepository(remoterepository): if data: self.ui.debug(_("sending %s bytes\n") % headers.get('content-length', 'X')) - resp = urllib2.urlopen(urllib2.Request(cu, data, headers)) + resp = urllib2.urlopen(request(cu, data, headers)) except urllib2.HTTPError, inst: if inst.code == 401: raise util.Abort(_('authorization failed'))