comparison mercurial/httprepo.py @ 4025:d8b3edf88af0

Subclass file with a __len__ method instead of setting Content-length This is necessary for the next patch to work with python2.3 Refactor the subclassing of send()
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Mon, 08 Jan 2007 16:12:35 +0100
parents d1e31d7f7d44
children 9210fba03d16 0d94e4a3ddb4
comparison
equal deleted inserted replaced
4015:769be3c57564 4025:d8b3edf88af0
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 78
79 class httpconnection(keepalive.HTTPConnection): 79 class httpsendfile(file):
80 # must be able to send big bundle as stream. 80 def __len__(self):
81 81 return os.fstat(self.fileno()).st_size
82 def send(self, data): 82
83 if isinstance(data, str): 83 def _gen_sendfile(connection):
84 keepalive.HTTPConnection.send(self, data) 84 def _sendfile(self, data):
85 else: 85 # send a file
86 if isinstance(data, httpsendfile):
86 # if auth required, some data sent twice, so rewind here 87 # if auth required, some data sent twice, so rewind here
87 data.seek(0) 88 data.seek(0)
88 for chunk in util.filechunkiter(data): 89 for chunk in util.filechunkiter(data):
89 keepalive.HTTPConnection.send(self, chunk) 90 connection.send(self, chunk)
91 else:
92 connection.send(self, data)
93 return _sendfile
94
95 class httpconnection(keepalive.HTTPConnection):
96 # must be able to send big bundle as stream.
97 send = _gen_sendfile(keepalive.HTTPConnection)
90 98
91 class basehttphandler(keepalive.HTTPHandler): 99 class basehttphandler(keepalive.HTTPHandler):
92 def http_open(self, req): 100 def http_open(self, req):
93 return self.do_open(httpconnection, req) 101 return self.do_open(httpconnection, req)
94 102
95 has_https = hasattr(urllib2, 'HTTPSHandler') 103 has_https = hasattr(urllib2, 'HTTPSHandler')
96 if has_https: 104 if has_https:
97 class httpsconnection(httplib.HTTPSConnection): 105 class httpsconnection(httplib.HTTPSConnection):
98 response_class = keepalive.HTTPResponse 106 response_class = keepalive.HTTPResponse
99 # must be able to send big bundle as stream. 107 # must be able to send big bundle as stream.
100 108 send = _gen_sendfile(httplib.HTTPSConnection)
101 def send(self, data):
102 if isinstance(data, str):
103 httplib.HTTPSConnection.send(self, data)
104 else:
105 # if auth required, some data sent twice, so rewind here
106 data.seek(0)
107 for chunk in util.filechunkiter(data):
108 httplib.HTTPSConnection.send(self, chunk)
109 109
110 class httphandler(basehttphandler, urllib2.HTTPSHandler): 110 class httphandler(basehttphandler, urllib2.HTTPSHandler):
111 def https_open(self, req): 111 def https_open(self, req):
112 return self.do_open(httpsconnection, req) 112 return self.do_open(httpsconnection, req)
113 else: 113 else:
343 if x in changegroup.bundletypes: 343 if x in changegroup.bundletypes:
344 type = x 344 type = x
345 break 345 break
346 346
347 tempname = changegroup.writebundle(cg, None, type) 347 tempname = changegroup.writebundle(cg, None, type)
348 fp = file(tempname, "rb") 348 fp = httpsendfile(tempname, "rb")
349 try: 349 try:
350 length = os.stat(tempname).st_size
351 try: 350 try:
352 rfp = self.do_cmd( 351 rfp = self.do_cmd(
353 'unbundle', data=fp, 352 'unbundle', data=fp,
354 headers={'content-length': str(length), 353 headers={'content-type': 'application/octet-stream'},
355 'content-type': 'application/octet-stream'},
356 heads=' '.join(map(hex, heads))) 354 heads=' '.join(map(hex, heads)))
357 try: 355 try:
358 ret = int(rfp.readline()) 356 ret = int(rfp.readline())
359 self.ui.write(rfp.read()) 357 self.ui.write(rfp.read())
360 return ret 358 return ret