[Python-checkins] python/dist/src/Lib httplib.py,1.65,1.66
jhylton@users.sourceforge.net
jhylton@users.sourceforge.net
Tue, 03 Sep 2002 13:49:08 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv17695
Modified Files:
httplib.py
Log Message:
Move code for reading chunked responses in helper function,
along with some small changes (e.g. use of +=).
Index: httplib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/httplib.py,v
retrieving revision 1.65
retrieving revision 1.66
diff -C2 -d -r1.65 -r1.66
*** httplib.py 3 Sep 2002 19:24:24 -0000 1.65
--- httplib.py 3 Sep 2002 20:49:06 -0000 1.66
***************
*** 371,418 ****
if self.chunked:
! assert self.chunked != _UNKNOWN
! chunk_left = self.chunk_left
! value = ''
! while 1:
! if chunk_left is None:
! line = self.fp.readline()
! i = line.find(';')
! if i >= 0:
! line = line[:i] # strip chunk-extensions
! chunk_left = int(line, 16)
! if chunk_left == 0:
! break
! if amt is None:
! value = value + self._safe_read(chunk_left)
! elif amt < chunk_left:
! value = value + self._safe_read(amt)
! self.chunk_left = chunk_left - amt
! return value
! elif amt == chunk_left:
! value = value + self._safe_read(amt)
! self._safe_read(2) # toss the CRLF at the end of the chunk
! self.chunk_left = None
! return value
! else:
! value = value + self._safe_read(chunk_left)
! amt = amt - chunk_left
!
! # we read the whole chunk, get another
! self._safe_read(2) # toss the CRLF at the end of the chunk
! chunk_left = None
!
! # read and discard trailer up to the CRLF terminator
! ### note: we shouldn't have any trailers!
! while 1:
! line = self.fp.readline()
! if line == '\r\n':
! break
!
! # we read everything; close the "file"
! self.close()
!
! return value
!
! elif amt is None:
# unbounded read
if self.will_close:
--- 371,377 ----
if self.chunked:
! return self._read_chunked(amt)
!
! if amt is None:
# unbounded read
if self.will_close:
***************
*** 427,431 ****
# clip the read to the "end of response"
amt = self.length
! self.length = self.length - amt
# we do not use _safe_read() here because this may be a .will_close
--- 386,390 ----
# clip the read to the "end of response"
amt = self.length
! self.length -= amt
# we do not use _safe_read() here because this may be a .will_close
***************
*** 436,439 ****
--- 395,446 ----
return s
+ def _read_chunked(self, amt):
+ assert self.chunked != _UNKNOWN
+ chunk_left = self.chunk_left
+ value = ''
+
+ # XXX This accumulates chunks by repeated string concatenation,
+ # which is not efficient as the number or size of chunks gets big.
+ while 1:
+ if chunk_left is None:
+ line = self.fp.readline()
+ i = line.find(';')
+ if i >= 0:
+ line = line[:i] # strip chunk-extensions
+ chunk_left = int(line, 16)
+ if chunk_left == 0:
+ break
+ if amt is None:
+ value += self._safe_read(chunk_left)
+ elif amt < chunk_left:
+ value += self._safe_read(amt)
+ self.chunk_left = chunk_left - amt
+ return value
+ elif amt == chunk_left:
+ value += self._safe_read(amt)
+ self._safe_read(2) # toss the CRLF at the end of the chunk
+ self.chunk_left = None
+ return value
+ else:
+ value += self._safe_read(chunk_left)
+ amt -= chunk_left
+
+ # we read the whole chunk, get another
+ self._safe_read(2) # toss the CRLF at the end of the chunk
+ chunk_left = None
+
+ # read and discard trailer up to the CRLF terminator
+ ### note: we shouldn't have any trailers!
+ while 1:
+ line = self.fp.readline()
+ if line == '\r\n':
+ break
+
+ # we read everything; close the "file"
+ # XXX Shouldn't the client close the file?
+ self.close()
+
+ return value
+
def _safe_read(self, amt):
"""Read the number of bytes requested, compensating for partial reads.