11#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
22mod channel;
3+ #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
4+ mod h2;
35
46use std:: fmt;
57use std:: pin:: Pin ;
68use std:: task:: { Context , Poll } ;
79
810use bytes:: Bytes ;
9- #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
10- use futures_util:: ready;
1111use http_body:: { Body , Frame , SizeHint } ;
1212
1313#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
1414use self :: channel:: ChanBody ;
1515#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
1616pub ( crate ) use self :: channel:: Sender ;
1717
18+ #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
19+ use self :: h2:: H2Body ;
20+
1821#[ cfg( all(
1922 any( feature = "http1" , feature = "http2" ) ,
2023 any( feature = "client" , feature = "server" )
@@ -48,12 +51,7 @@ enum Kind {
4851 #[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
4952 Chan ( ChanBody ) ,
5053 #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
51- H2 {
52- content_length : DecodedLength ,
53- data_done : bool ,
54- ping : ping:: Recorder ,
55- recv : h2:: RecvStream ,
56- } ,
54+ H2 ( H2Body ) ,
5755 #[ cfg( feature = "ffi" ) ]
5856 Ffi ( crate :: ffi:: UserBody ) ,
5957}
@@ -81,22 +79,11 @@ impl Incoming {
8179
8280 #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
8381 pub ( crate ) fn h2 (
84- recv : h2:: RecvStream ,
85- mut content_length : DecodedLength ,
82+ recv : :: h2:: RecvStream ,
83+ content_length : DecodedLength ,
8684 ping : ping:: Recorder ,
8785 ) -> Self {
88- // If the stream is already EOS, then the "unknown length" is clearly
89- // actually ZERO.
90- if !content_length. is_exact ( ) && recv. is_end_stream ( ) {
91- content_length = DecodedLength :: ZERO ;
92- }
93-
94- Incoming :: new ( Kind :: H2 {
95- data_done : false ,
96- ping,
97- content_length,
98- recv,
99- } )
86+ Incoming :: new ( Kind :: H2 ( H2Body :: new ( recv, content_length, ping) ) )
10087 }
10188
10289 #[ cfg( feature = "ffi" ) ]
@@ -142,47 +129,7 @@ impl Body for Incoming {
142129 #[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
143130 Kind :: Chan ( ref mut body) => body. poll_frame ( cx) ,
144131 #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
145- Kind :: H2 {
146- ref mut data_done,
147- ref ping,
148- recv : ref mut h2,
149- content_length : ref mut len,
150- } => {
151- if !* data_done {
152- match ready ! ( h2. poll_data( cx) ) {
153- Some ( Ok ( bytes) ) => {
154- let _ = h2. flow_control ( ) . release_capacity ( bytes. len ( ) ) ;
155- len. sub_if ( bytes. len ( ) as u64 ) ;
156- ping. record_data ( bytes. len ( ) ) ;
157- return Poll :: Ready ( Some ( Ok ( Frame :: data ( bytes) ) ) ) ;
158- }
159- Some ( Err ( e) ) => {
160- return match e. reason ( ) {
161- // These reasons should cause the body reading to stop, but not fail it.
162- // The same logic as for `Read for H2Upgraded` is applied here.
163- Some ( h2:: Reason :: NO_ERROR ) | Some ( h2:: Reason :: CANCEL ) => {
164- Poll :: Ready ( None )
165- }
166- _ => Poll :: Ready ( Some ( Err ( crate :: Error :: new_body ( e) ) ) ) ,
167- } ;
168- }
169- None => {
170- * data_done = true ;
171- // fall through to trailers
172- }
173- }
174- }
175-
176- // after data, check trailers
177- match ready ! ( h2. poll_trailers( cx) ) {
178- Ok ( t) => {
179- ping. record_non_data ( ) ;
180- Poll :: Ready ( Ok ( t. map ( Frame :: trailers) ) . transpose ( ) )
181- }
182- Err ( e) => Poll :: Ready ( Some ( Err ( crate :: Error :: new_h2 ( e) ) ) ) ,
183- }
184- }
185-
132+ Kind :: H2 ( ref mut body) => body. poll_frame ( cx) ,
186133 #[ cfg( feature = "ffi" ) ]
187134 Kind :: Ffi ( ref mut body) => body. poll_data ( cx) ,
188135 }
@@ -194,7 +141,7 @@ impl Body for Incoming {
194141 #[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
195142 Kind :: Chan ( ref body) => body. is_end_stream ( ) ,
196143 #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
197- Kind :: H2 { recv : ref h2 , .. } => h2 . is_end_stream ( ) ,
144+ Kind :: H2 ( ref body ) => body . is_end_stream ( ) ,
198145 #[ cfg( feature = "ffi" ) ]
199146 Kind :: Ffi ( ..) => false ,
200147 }
@@ -206,7 +153,7 @@ impl Body for Incoming {
206153 #[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
207154 Kind :: Chan ( ref body) => body. size_hint ( ) ,
208155 #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
209- Kind :: H2 { content_length , .. } => opt_len ( content_length ) ,
156+ Kind :: H2 ( ref body ) => body . size_hint ( ) ,
210157 #[ cfg( feature = "ffi" ) ]
211158 Kind :: Ffi ( ..) => SizeHint :: default ( ) ,
212159 }
0 commit comments