def parse_chunk_size(self, unreader, data=None): buf = StringIO() if data is not None: buf.write(data) idx = buf.getvalue().find("\r\n") while idx < 0: self.get_data(unreader, buf) idx = buf.getvalue().find("\r\n") data = buf.getvalue() line, rest_chunk = data[:idx], data[idx + 2:] chunk_size = line.split(";", 1)[0].strip() try: chunk_size = int(chunk_size, 16) except ValueError: raise InvalidChunkSize(chunk_size) if chunk_size == 0: try: self.parse_trailers(unreader, rest_chunk) except NoMoreData: pass return (0, None) return (chunk_size, rest_chunk)
def parse_chunk_size(self, unreader, data=None): buf = io.BytesIO() if data is not None: buf.write(data) # chunk内容是16进制, 构成如: <len_of_chunk>\r\n<chunk_content>\r\n # 最后一个chunk必须是 0\r\n<此处没有数据>\r\n idx = buf.getvalue().find(b"\r\n") while idx < 0: self.get_data(unreader, buf) idx = buf.getvalue().find(b"\r\n") data = buf.getvalue() line, rest_chunk = data[:idx], data[idx + 2:] chunk_size = line.split(b";", 1)[0].strip() try: chunk_size = int(chunk_size, 16) except ValueError: raise InvalidChunkSize(chunk_size) if chunk_size == 0: try: self.parse_trailers(unreader, rest_chunk) except NoMoreData: pass return 0, None return chunk_size, rest_chunk