Mercurial > hg
comparison mercurial/keepalive.py @ 7781:a45206455d85
keepalive: borrow code from newer httplib to patch ValueError (issue1088)
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 16 Feb 2009 17:37:23 -0600 |
parents | 4dd7b28003d2 |
children | 2b901f9b37aa |
comparison
equal
deleted
inserted
replaced
7780:9892c4d94fb7 | 7781:a45206455d85 |
---|---|
387 | 387 |
388 s = self._rbuf + self._raw_read(amt) | 388 s = self._rbuf + self._raw_read(amt) |
389 self._rbuf = '' | 389 self._rbuf = '' |
390 return s | 390 return s |
391 | 391 |
392 # stolen from Python SVN #68532 to fix issue1088 | |
393 def _read_chunked(self, amt): | |
394 chunk_left = self.chunk_left | |
395 value = '' | |
396 | |
397 # XXX This accumulates chunks by repeated string concatenation, | |
398 # which is not efficient as the number or size of chunks gets big. | |
399 while True: | |
400 if chunk_left is None: | |
401 line = self.fp.readline() | |
402 i = line.find(';') | |
403 if i >= 0: | |
404 line = line[:i] # strip chunk-extensions | |
405 try: | |
406 chunk_left = int(line, 16) | |
407 except ValueError: | |
408 # close the connection as protocol synchronisation is | |
409 # probably lost | |
410 self.close() | |
411 raise IncompleteRead(value) | |
412 if chunk_left == 0: | |
413 break | |
414 if amt is None: | |
415 value += self._safe_read(chunk_left) | |
416 elif amt < chunk_left: | |
417 value += self._safe_read(amt) | |
418 self.chunk_left = chunk_left - amt | |
419 return value | |
420 elif amt == chunk_left: | |
421 value += self._safe_read(amt) | |
422 self._safe_read(2) # toss the CRLF at the end of the chunk | |
423 self.chunk_left = None | |
424 return value | |
425 else: | |
426 value += self._safe_read(chunk_left) | |
427 amt -= chunk_left | |
428 | |
429 # we read the whole chunk, get another | |
430 self._safe_read(2) # toss the CRLF at the end of the chunk | |
431 chunk_left = None | |
432 | |
433 # read and discard trailer up to the CRLF terminator | |
434 ### note: we shouldn't have any trailers! | |
435 while True: | |
436 line = self.fp.readline() | |
437 if not line: | |
438 # a vanishingly small number of sites EOF without | |
439 # sending the trailer | |
440 break | |
441 if line == '\r\n': | |
442 break | |
443 | |
444 # we read everything; close the "file" | |
445 self.close() | |
446 | |
447 return value | |
448 | |
392 def readline(self, limit=-1): | 449 def readline(self, limit=-1): |
393 data = "" | 450 data = "" |
394 i = self._rbuf.find('\n') | 451 i = self._rbuf.find('\n') |
395 while i < 0 and not (0 < limit <= len(self._rbuf)): | 452 while i < 0 and not (0 < limit <= len(self._rbuf)): |
396 new = self._raw_read(self._rbufsize) | 453 new = self._raw_read(self._rbufsize) |