@@ -180,21 +180,21 @@ where
180
180
buf. has_remaining = state. buf. has_remaining( ) ,
181
181
body. is_completed = state. is_completed,
182
182
body. max_bytes_remaining = state. max_bytes,
183
- "Replay ::poll_data"
183
+ "ReplayBody ::poll_data"
184
184
) ;
185
185
186
186
// If we haven't replayed the buffer yet, and its not empty, return the
187
187
// buffered data first.
188
188
if this. replay_body {
189
189
if state. buf . has_remaining ( ) {
190
- tracing:: trace!( "replaying body" ) ;
190
+ tracing:: trace!( "Replaying body" ) ;
191
191
// Don't return the buffered data again on the next poll.
192
192
this. replay_body = false ;
193
193
return Poll :: Ready ( Some ( Ok ( Data :: Replay ( state. buf . clone ( ) ) ) ) ) ;
194
194
}
195
195
196
196
if state. is_capped ( ) {
197
- tracing:: trace!( "cannot replay buffered body, maximum buffer length reached" ) ;
197
+ tracing:: trace!( "Cannot replay buffered body, maximum buffer length reached" ) ;
198
198
return Poll :: Ready ( Some ( Err ( Capped . into ( ) ) ) ) ;
199
199
}
200
200
}
@@ -209,50 +209,50 @@ where
209
209
return Poll :: Ready ( None ) ;
210
210
}
211
211
212
- // If there's more data in the initial body, poll that...
213
- if let Some ( rest) = state. rest . as_mut ( ) {
212
+ // Poll the inner body for more data. If the body has ended, remember
213
+ // that so that future clones will not try polling it again (as
214
+ // described above).
215
+ let mut data = {
216
+ // Get access to the initial body. If we don't have access to the
217
+ // inner body, there's no more work to do.
218
+ let rest = match state. rest . as_mut ( ) {
219
+ Some ( rest) => rest,
220
+ None => return Poll :: Ready ( None ) ,
221
+ } ;
222
+
214
223
tracing:: trace!( "Polling initial body" ) ;
215
- let opt = futures:: ready!( Pin :: new( rest) . poll_data( cx) ) ;
216
-
217
- // If the body has ended, remember that so that future clones will
218
- // not try polling it again --- some `Body` types will panic if they
219
- // are polled after returning `None`.
220
- if opt . is_none ( ) {
221
- tracing :: trace! ( "Initial body completed" ) ;
222
- state . is_completed = true ;
224
+ match futures:: ready!( Pin :: new( rest) . poll_data( cx) ) {
225
+ Some ( Ok ( data ) ) => data ,
226
+ Some ( Err ( e ) ) => return Poll :: Ready ( Some ( Err ( e . into ( ) ) ) ) ,
227
+ None => {
228
+ tracing :: trace! ( "Initial body completed" ) ;
229
+ state . is_completed = true ;
230
+ return Poll :: Ready ( None ) ;
231
+ }
223
232
}
224
- return Poll :: Ready ( opt. map ( |ok| {
225
- ok. map ( |mut data| {
226
- // If we have buffered the maximum number of bytes, allow
227
- // *this* body to continue, but don't buffer any more.
228
- let length = data. remaining ( ) ;
229
- state. max_bytes = state. max_bytes . saturating_sub ( length) ;
230
- if state. is_capped ( ) {
231
- // If there's data in the buffer, discard it now, since
232
- // we won't allow any clones to have a complete body.
233
- if state. buf . has_remaining ( ) {
234
- tracing:: debug!(
235
- buf. size = state. buf. remaining( ) ,
236
- "buffered maximum capacity, discarding buffer"
237
- ) ;
238
- state. buf = Default :: default ( ) ;
239
- }
240
- return Data :: Initial ( data. copy_to_bytes ( length) ) ;
241
- }
242
-
243
- if state. is_capped ( ) {
244
- return Data :: Initial ( data. copy_to_bytes ( length) ) ;
245
- }
233
+ } ;
246
234
247
- // Buffer and return the bytes
248
- Data :: Initial ( state. buf . push_chunk ( data) )
249
- } )
250
- . map_err ( Into :: into)
251
- } ) ) ;
252
- }
235
+ // If we have buffered the maximum number of bytes, allow *this* body to
236
+ // continue, but don't buffer any more.
237
+ let length = data. remaining ( ) ;
238
+ state. max_bytes = state. max_bytes . saturating_sub ( length) ;
239
+ let chunk = if state. is_capped ( ) {
240
+ // If there's data in the buffer, discard it now, since we won't
241
+ // allow any clones to have a complete body.
242
+ if state. buf . has_remaining ( ) {
243
+ tracing:: debug!(
244
+ buf. size = state. buf. remaining( ) ,
245
+ "Buffered maximum capacity, discarding buffer"
246
+ ) ;
247
+ state. buf = Default :: default ( ) ;
248
+ }
249
+ data. copy_to_bytes ( length)
250
+ } else {
251
+ // Buffer and return the bytes.
252
+ state. buf . push_chunk ( data)
253
+ } ;
253
254
254
- // Otherwise, guess we're done!
255
- Poll :: Ready ( None )
255
+ Poll :: Ready ( Some ( Ok ( Data :: Initial ( chunk) ) ) )
256
256
}
257
257
258
258
fn poll_trailers (
@@ -319,16 +319,17 @@ where
319
319
None => return self . shared . orig_size_hint . clone ( ) ,
320
320
} ;
321
321
322
- // Otherwise, if we're holding the state but have dropped the inner body, the entire body is
323
- // buffered so we know the exact size hint.
322
+ // Otherwise, if we're holding the state but have dropped the inner
323
+ // body, the entire body is buffered so we know the exact size hint.
324
324
let buffered = state. buf . remaining ( ) as u64 ;
325
325
let rest_hint = match state. rest . as_ref ( ) {
326
326
Some ( rest) => rest. size_hint ( ) ,
327
327
None => return SizeHint :: with_exact ( buffered) ,
328
328
} ;
329
329
330
- // Otherwise, add the inner body's size hint to the amount of buffered data. An upper limit
331
- // is only set if the inner body has an upper limit.
330
+ // Otherwise, add the inner body's size hint to the amount of buffered
331
+ // data. An upper limit is only set if the inner body has an upper
332
+ // limit.
332
333
let mut hint = SizeHint :: default ( ) ;
333
334
hint. set_lower ( buffered + rest_hint. lower ( ) ) ;
334
335
if let Some ( rest_upper) = rest_hint. upper ( ) {
0 commit comments