File tree 4 files changed +41
-5
lines changed
4 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -204,9 +204,14 @@ impl Body {
204
204
#[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
205
205
pub ( crate ) fn h2 (
206
206
recv : h2:: RecvStream ,
207
- content_length : DecodedLength ,
207
+ mut content_length : DecodedLength ,
208
208
ping : ping:: Recorder ,
209
209
) -> Self {
210
+ // If the stream is already EOS, then the "unknown length" is clearly
211
+ // actually ZERO.
212
+ if !content_length. is_exact ( ) && recv. is_end_stream ( ) {
213
+ content_length = DecodedLength :: ZERO ;
214
+ }
210
215
let body = Body :: new ( Kind :: H2 {
211
216
ping,
212
217
content_length,
Original file line number Diff line number Diff line change @@ -68,6 +68,16 @@ impl DecodedLength {
68
68
}
69
69
}
70
70
}
71
+
72
+ /// Returns whether this represents an exact length.
73
+ ///
74
+ /// This includes 0, which of course is an exact known length.
75
+ ///
76
+ /// It would return false if "chunked" or otherwise size-unknown.
77
+ #[ cfg( feature = "http2" ) ]
78
+ pub ( crate ) fn is_exact ( & self ) -> bool {
79
+ self . 0 <= MAX_LEN
80
+ }
71
81
}
72
82
73
83
impl fmt:: Debug for DecodedLength {
Original file line number Diff line number Diff line change @@ -484,12 +484,13 @@ where
484
484
}
485
485
}
486
486
487
- // automatically set Content-Length from body...
488
- if let Some ( len) = body. size_hint ( ) . exact ( ) {
489
- headers:: set_content_length_if_missing ( res. headers_mut ( ) , len) ;
490
- }
491
487
492
488
if !body. is_end_stream ( ) {
489
+ // automatically set Content-Length from body...
490
+ if let Some ( len) = body. size_hint ( ) . exact ( ) {
491
+ headers:: set_content_length_if_missing ( res. headers_mut ( ) , len) ;
492
+ }
493
+
493
494
let body_tx = reply ! ( me, res, false ) ;
494
495
H2StreamState :: Body {
495
496
pipe : PipeToSendStream :: new ( body, body_tx) ,
Original file line number Diff line number Diff line change @@ -361,6 +361,26 @@ mod response_body_lengths {
361
361
assert_eq ! ( res. headers( ) . get( "content-length" ) . unwrap( ) , "10" ) ;
362
362
assert_eq ! ( res. body( ) . size_hint( ) . exact( ) , Some ( 10 ) ) ;
363
363
}
364
+
365
+ #[ tokio:: test]
366
+ async fn http2_implicit_empty_size_hint ( ) {
367
+ use http_body:: Body ;
368
+
369
+ let server = serve ( ) ;
370
+ let addr_str = format ! ( "http://{}" , server. addr( ) ) ;
371
+ server. reply ( ) ;
372
+
373
+ let client = Client :: builder ( )
374
+ . http2_only ( true )
375
+ . build_http :: < hyper:: Body > ( ) ;
376
+ let uri = addr_str
377
+ . parse :: < hyper:: Uri > ( )
378
+ . expect ( "server addr should parse" ) ;
379
+
380
+ let res = client. get ( uri) . await . unwrap ( ) ;
381
+ assert_eq ! ( res. headers( ) . get( "content-length" ) , None ) ;
382
+ assert_eq ! ( res. body( ) . size_hint( ) . exact( ) , Some ( 0 ) ) ;
383
+ }
364
384
}
365
385
366
386
#[ test]
You can’t perform that action at this time.
0 commit comments