1
1
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
2
2
mod channel;
3
+ #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
4
+ mod h2;
3
5
4
6
use std:: fmt;
5
7
use std:: pin:: Pin ;
6
8
use std:: task:: { Context , Poll } ;
7
9
8
10
use bytes:: Bytes ;
9
- #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
10
- use futures_util:: ready;
11
11
use http_body:: { Body , Frame , SizeHint } ;
12
12
13
13
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
14
14
use self :: channel:: ChanBody ;
15
15
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
16
16
pub ( crate ) use self :: channel:: Sender ;
17
17
18
+ #[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
19
+ use self :: h2:: H2Body ;
20
+
18
21
#[ cfg( all(
19
22
any( feature = "http1" , feature = "http2" ) ,
20
23
any( feature = "client" , feature = "server" )
@@ -48,12 +51,7 @@ enum Kind {
48
51
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
49
52
Chan ( ChanBody ) ,
50
53
#[ 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 ) ,
57
55
#[ cfg( feature = "ffi" ) ]
58
56
Ffi ( crate :: ffi:: UserBody ) ,
59
57
}
@@ -81,22 +79,11 @@ impl Incoming {
81
79
82
80
#[ cfg( all( feature = "http2" , any( feature = "client" , feature = "server" ) ) ) ]
83
81
pub ( crate ) fn h2 (
84
- recv : h2:: RecvStream ,
85
- mut content_length : DecodedLength ,
82
+ recv : :: h2:: RecvStream ,
83
+ content_length : DecodedLength ,
86
84
ping : ping:: Recorder ,
87
85
) -> 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) ) )
100
87
}
101
88
102
89
#[ cfg( feature = "ffi" ) ]
@@ -142,47 +129,7 @@ impl Body for Incoming {
142
129
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
143
130
Kind :: Chan ( ref mut body) => body. poll_frame ( cx) ,
144
131
#[ 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) ,
186
133
#[ cfg( feature = "ffi" ) ]
187
134
Kind :: Ffi ( ref mut body) => body. poll_data ( cx) ,
188
135
}
@@ -194,7 +141,7 @@ impl Body for Incoming {
194
141
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
195
142
Kind :: Chan ( ref body) => body. is_end_stream ( ) ,
196
143
#[ 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 ( ) ,
198
145
#[ cfg( feature = "ffi" ) ]
199
146
Kind :: Ffi ( ..) => false ,
200
147
}
@@ -206,7 +153,7 @@ impl Body for Incoming {
206
153
#[ cfg( all( feature = "http1" , any( feature = "client" , feature = "server" ) ) ) ]
207
154
Kind :: Chan ( ref body) => body. size_hint ( ) ,
208
155
#[ 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 ( ) ,
210
157
#[ cfg( feature = "ffi" ) ]
211
158
Kind :: Ffi ( ..) => SizeHint :: default ( ) ,
212
159
}
0 commit comments