def parse_chunked_payload(out, buf): """Chunked transfer encoding parser.""" try: while True: # read next chunk size #line = yield from buf.readline(8196) line = yield from buf.readuntil(b'\r\n', 8196) i = line.find(b';') if i >= 0: line = line[:i] # strip chunk-extensions else: line = line.strip() try: size = int(line, 16) except ValueError: raise errors.IncompleteRead(b'') from None if size == 0: # eof marker break # read chunk and feed buffer while size: chunk = yield from buf.readsome(size) out.feed_data(chunk) size = size - len(chunk) # toss the CRLF at the end of the chunk yield from buf.skip(2) # read and discard trailer up to the CRLF terminator yield from buf.skipuntil(b'\r\n') except tulip.EofStream: raise errors.IncompleteRead(b'') from None
def feed_data(self, chunk): try: chunk = self.zlib.decompress(chunk) except Exception: raise errors.IncompleteRead(b'') from None if chunk: self.out.feed_data(chunk)
def parse_length_payload(out, buf, length): """Read specified amount of bytes.""" try: while length: chunk = yield from buf.readsome(length) out.feed_data(chunk) length -= len(chunk) except tulip.EofStream: raise errors.IncompleteRead(b'') from None
def feed_eof(self): self.out.feed_data(self.zlib.flush()) if not self.zlib.eof: raise errors.IncompleteRead(b'') self.out.feed_eof()