@@ -13,18 +13,20 @@ use std::pin::Pin;
13
13
14
14
use crate :: { Body , Exception , MAX_HEADERS } ;
15
15
16
- pub async fn connect < ' a , F , Fut , B > (
17
- reader : & ' a mut B ,
18
- writer : & ' a mut B ,
16
+ pub async fn connect < ' a , F , Fut , R , W , O : ' a > (
17
+ reader : & ' a mut R ,
18
+ writer : & ' a mut W ,
19
19
callback : F ,
20
20
) -> Result < ( ) , Exception >
21
21
where
22
- F : Fn ( & mut Request < Body < BufReader < & ' a mut B > > > ) -> Fut ,
23
- Fut : Future < Output = Result < Response < Body < & ' a mut B > > , Exception > > ,
24
- B : Read + Write + Unpin + Send ,
22
+ R : Read + Unpin + Send ,
23
+ W : Write + Unpin ,
24
+ F : Fn ( & mut Request < Body < BufReader < & ' a mut R > > > ) -> Fut ,
25
+ Fut : Future < Output = Result < Response < Body < O > > , Exception > > ,
26
+ O : Read + Unpin + Send ,
25
27
{
26
28
let req = decode ( reader) . await ?;
27
- if let OptionalRequest :: Request ( mut req) = req {
29
+ if let RequestOrReader :: Request ( mut req) = req {
28
30
let headers = req. headers ( ) ;
29
31
let timeout = match ( headers. get ( "Connection" ) , headers. get ( "Keep-Alive" ) ) {
30
32
( Some ( connection) , Some ( _v) )
@@ -38,22 +40,19 @@ where
38
40
39
41
let beginning = Instant :: now ( ) ;
40
42
loop {
41
- println ! ( "Handling request" ) ;
42
- let mut res = encode ( callback ( & mut req) . await ?) . await . unwrap ( ) ;
43
+ // TODO: what to do when the callback returns Err
44
+ let mut res = encode ( callback ( & mut req) . await ?) . await ? ;
43
45
io:: copy ( & mut res, writer) . await ?;
44
- let mut stream = res . body . into_reader ( ) . unwrap ( ) ;
46
+ let mut stream = req . into_body ( ) . into_reader ( ) . into_inner ( ) ;
45
47
req = loop {
46
48
match decode ( stream) . await ? {
47
- OptionalRequest :: Request ( r) => {
48
- break r;
49
- }
50
- OptionalRequest :: Stream ( r) => {
49
+ RequestOrReader :: Request ( r) => break r,
50
+ RequestOrReader :: Reader ( r) => {
51
51
let now = Instant :: now ( ) ;
52
52
if now - beginning > timeout {
53
53
return Ok ( ( ) ) ;
54
- } else {
55
- stream = r;
56
54
}
55
+ stream = r;
57
56
}
58
57
}
59
58
} ;
@@ -167,13 +166,13 @@ where
167
166
}
168
167
169
168
#[ derive( Debug ) ]
170
- pub enum OptionalRequest < R : Read > {
169
+ pub enum RequestOrReader < R : Read > {
171
170
Request ( Request < Body < BufReader < R > > > ) ,
172
- Stream ( R ) ,
171
+ Reader ( R ) ,
173
172
}
174
173
175
174
/// Decode an HTTP request on the server.
176
- pub async fn decode < R > ( reader : R ) -> Result < OptionalRequest < R > , Exception >
175
+ pub async fn decode < R > ( reader : R ) -> Result < RequestOrReader < R > , Exception >
177
176
where
178
177
R : Read + Unpin + Send ,
179
178
{
@@ -187,7 +186,7 @@ where
187
186
let bytes_read = reader. read_until ( b'\n' , & mut buf) . await ?;
188
187
// No more bytes are yielded from the stream.
189
188
if bytes_read == 0 {
190
- return Ok ( OptionalRequest :: Stream ( reader. into_inner ( ) ) ) ;
189
+ return Ok ( RequestOrReader :: Reader ( reader. into_inner ( ) ) ) ;
191
190
}
192
191
193
192
// We've hit the end delimiter of the stream.
@@ -229,9 +228,9 @@ where
229
228
. find ( |h| h. name == "Content-Length" )
230
229
{
231
230
Some ( _header) => Body :: new ( reader) , // TODO: use the header value
232
- None => Body :: empty ( ) ,
231
+ None => Body :: empty ( reader ) ,
233
232
} ;
234
233
235
234
// Return the request.
236
- Ok ( OptionalRequest :: Request ( req. body ( body) ?) )
235
+ Ok ( RequestOrReader :: Request ( req. body ( body) ?) )
237
236
}
0 commit comments