##// END OF EJS Templates
keepalive: borrow code from newer httplib to patch ValueError (issue1088)
Matt Mackall -
r7781:a4520645 default
parent child Browse files
Show More
@@ -389,6 +389,63 b' class HTTPResponse(httplib.HTTPResponse)'
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')
General Comments 0
You need to be logged in to leave comments. Login now