@@ -154,10 +154,15 @@ def read(self, amt=None, decode_content=True):
154154
155155 return data
156156
157- def read_chunked (self ):
157+ def read_chunked (self , decode_content = True ):
158158 """
159- Reads chunked transfer encoded bodies. Each read returns a single
160- chunk.
159+ Reads chunked transfer encoded bodies. This method returns a generator:
160+ each iteration of which yields one chunk *unless* the chunks are
161+ compressed, in which case it yields whatever the decompressor provides
162+ for each chunk.
163+
164+ .. warning:: This may yield the empty string, without that being the
165+ end of the body!
161166 """
162167 if not self ._chunked :
163168 raise ChunkedDecodeError (
@@ -166,31 +171,42 @@ def read_chunked(self):
166171
167172 # Return early if possible.
168173 if self ._sock is None :
169- return b''
174+ return
170175
171- # Read to the newline to get the chunk length. This is a hexadecimal
172- # integer.
173- chunk_length = int (self ._sock .readline ().tobytes ().strip (), 16 )
176+ while True :
177+ # Read to the newline to get the chunk length. This is a
178+ # hexadecimal integer.
179+ chunk_length = int (self ._sock .readline ().tobytes ().strip (), 16 )
180+ data = b''
174181
175- # If the chunk length is zero, consume the newline and then we're done.
176- if not chunk_length :
177- self . _sock . readline ()
178- return b''
182+ # If the chunk length is zero, consume the newline and then we're
183+ # done. If we were decompressing data, return the remaining data.
184+ if not chunk_length :
185+ self . _sock . readline ()
179186
180- # Then read that many bytes.
181- data = b''
187+ if decode_content and self . _decompressobj :
188+ yield self . _decompressobj . flush ()
182189
183- while chunk_length > 0 :
184- chunk = self ._sock .recv (chunk_length ).tobytes ()
185- data += chunk
186- chunk_length -= len (chunk )
190+ break
187191
188- assert chunk_length == 0
192+ # Then read that many bytes.
193+ while chunk_length > 0 :
194+ chunk = self ._sock .recv (chunk_length ).tobytes ()
195+ data += chunk
196+ chunk_length -= len (chunk )
189197
190- # Now, consume the newline.
191- self ._sock .readline ()
198+ assert chunk_length == 0
192199
193- return data
200+ # Now, consume the newline.
201+ self ._sock .readline ()
202+
203+ # We may need to decode the body.
204+ if decode_content and self ._decompressobj and data :
205+ data = self ._decompressobj .decompress (data )
206+
207+ yield data
208+
209+ return
194210
195211 def close (self ):
196212 """
0 commit comments