Skip to content
Myr

Decoder support

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent 665743a · Browse files · 2 files+67 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+67 added−21 removed

  • other:+67 added−21 removed

2 files+67 added−21 removed

modified:httpcore/api.py+26 added-21 removed
7 unmodified lines
8 PoolLimits,9 SSLConfig,10 TimeoutConfig,11)12from .decoders import IdentityDecoder13from .exceptions import ResponseClosed, StreamConsumed141516async def request(
69 unmodified lines
86 self.headers = list(headers)87 self.on_close = on_close88 self.is_closed = False89 self.is_streamed = False90 self.decoder = IdentityDecoder()91 if isinstance(body, bytes):92 self.is_closed = True93 self.body = body94 else:95 self.body_aiter = body9697 async def read(self) -> bytes:98 """99 Read and return the response content.100 """101 if not hasattr(self, "body"):102 body = b""103 async for part in self.stream():104 body += part105 self.body = body106 return self.body107103 async def stream(self) -> typing.AsyncIterator[bytes]:108 async def stream(self):109 """110 A byte-iterator over the decoded response content.111 This will allow us to handle gzip, deflate, and brotli encoded responses.112 """113 if hasattr(self, "body"):114 yield self.body115 else:107 if self.is_streamed:108 raise StreamConsumed()109 if self.is_closed:110 raise ResponseClosed()111 self.is_streamed = True112 async for part in self.body_aiter():113 yield part114 await self.close()116 async for chunk in self.raw():117 yield self.decoder.decode(chunk)118 yield self.decoder.flush()119120 async def raw(self) -> typing.AsyncIterator[bytes]:121 """122 A byte-iterator over the raw response content.123 """124 if self.is_streamed:125 raise StreamConsumed()126 if self.is_closed:127 raise ResponseClosed()128 self.is_streamed = True129 async for part in self.body_aiter():130 yield part131 await self.close()132133 async def close(self) -> None:134 if not self.is_closed:135 self.is_closed = True136 if self.on_close is not None:137 await self.on_close()121122 async def __aenter__(self) -> "Response":123 return self124125 async def __aexit__(126 self,127 exc_type: typing.Type[BaseException] = None,128 exc_value: BaseException = None,129 traceback: TracebackType = None,130 ) -> None:131 if not self.is_closed:132 await self.close()
added:httpcore/decoders.py+41 added
1"""2Handlers for Content-Encoding.3"""456class IdentityDecoder:7 def decode(self, chunk: bytes) -> bytes:8 return chunk910 def flush(self) -> bytes:11 return b''121314# class DeflateDecoder:15# pass16#17#18# class GZipDecoder:19# pass20#21#22# class BrotliDecoder:23# pass24#25#26# class MultiDecoder:27# def __init__(self, children):28# self.children = children29#30# def decode(self, chunk: bytes) -> bytes:31# data = chunk32# for child in children:33# data = child.decode(data)34# return data35#36# def flush(self) -> bytes:37# data = b''38# for child in children:39# data = child.decode(data)40# data = child.flush()41# return data