comparison mercurial/httprepo.py @ 4226:fffacca46f09

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.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Fri, 16 Mar 2007 00:22:53 -0300
parents 0d94e4a3ddb4
children 0d51eb296fb9 aed9e6dceb85
comparison
equal deleted inserted replaced
4225:281f9f8f1bd4 4226:fffacca46f09
73 userpass = urllib.quote(user) + ':' + urllib.quote(passwd) 73 userpass = urllib.quote(user) + ':' + urllib.quote(passwd)
74 else: 74 else:
75 userpass = urllib.quote(user) 75 userpass = urllib.quote(user)
76 return userpass + '@' + hostport 76 return userpass + '@' + hostport
77 return hostport 77 return hostport
78
79 # work around a bug in Python < 2.4.2
80 # (it leaves a "\n" at the end of Proxy-authorization headers)
81 class request(urllib2.Request):
82 def add_header(self, key, val):
83 if key.lower() == 'proxy-authorization':
84 val = val.strip()
85 return urllib2.Request.add_header(self, key, val)
78 86
79 class httpsendfile(file): 87 class httpsendfile(file):
80 def __len__(self): 88 def __len__(self):
81 return os.fstat(self.fileno()).st_size 89 return os.fstat(self.fileno()).st_size
82 90
237 cu = "%s%s" % (self._url, qs) 245 cu = "%s%s" % (self._url, qs)
238 try: 246 try:
239 if data: 247 if data:
240 self.ui.debug(_("sending %s bytes\n") % 248 self.ui.debug(_("sending %s bytes\n") %
241 headers.get('content-length', 'X')) 249 headers.get('content-length', 'X'))
242 resp = urllib2.urlopen(urllib2.Request(cu, data, headers)) 250 resp = urllib2.urlopen(request(cu, data, headers))
243 except urllib2.HTTPError, inst: 251 except urllib2.HTTPError, inst:
244 if inst.code == 401: 252 if inst.code == 401:
245 raise util.Abort(_('authorization failed')) 253 raise util.Abort(_('authorization failed'))
246 raise 254 raise
247 except httplib.HTTPException, inst: 255 except httplib.HTTPException, inst: