2
2
3
3
use async_std:: io:: { self , BufRead , BufReader } ;
4
4
use async_std:: task:: { Context , Poll } ;
5
- use futures_io:: AsyncRead ;
6
5
use futures_core:: ready;
6
+ use futures_io:: AsyncRead ;
7
7
use http:: { Request , Response , Version } ;
8
8
9
9
use std:: pin:: Pin ;
@@ -78,7 +78,10 @@ impl<R: AsyncRead + Unpin> AsyncRead for Encoder<R> {
78
78
79
79
/// Encode an HTTP request on the server.
80
80
// TODO: return a reader in the response
81
- pub async fn encode < R > ( res : Response < Body < R > > ) -> Result < Encoder < R > , std:: io:: Error > where R : AsyncRead {
81
+ pub async fn encode < R > ( res : Response < Body < R > > ) -> io:: Result < Encoder < R > >
82
+ where
83
+ R : AsyncRead ,
84
+ {
82
85
use std:: io:: Write ;
83
86
let mut buf: Vec < u8 > = vec ! [ ] ;
84
87
@@ -98,16 +101,22 @@ pub async fn encode<R>(res: Response<Body<R>>) -> Result<Encoder<R>, std::io::Er
98
101
}
99
102
100
103
for ( header, value) in res. headers ( ) {
101
- write ! ( & mut buf, "{}: {}\r \n " , header. as_str( ) , value. to_str( ) . unwrap( ) ) ?;
104
+ write ! (
105
+ & mut buf,
106
+ "{}: {}\r \n " ,
107
+ header. as_str( ) ,
108
+ value. to_str( ) . unwrap( )
109
+ ) ?;
102
110
}
103
111
104
112
write ! ( & mut buf, "\r \n " ) ?;
105
113
Ok ( Encoder :: new ( buf, res. into_body ( ) ) )
106
114
}
107
115
108
116
/// Decode an HTTP request on the server.
109
- pub async fn decode < R > ( reader : R ) -> Result < Request < Body < BufReader < R > > > , Exception >
110
- where R : AsyncRead + Unpin + Send ,
117
+ pub async fn decode < R > ( reader : R ) -> Result < Option < Request < Body < BufReader < R > > > > , Exception >
118
+ where
119
+ R : AsyncRead + Unpin + Send ,
111
120
{
112
121
let mut reader = BufReader :: new ( reader) ;
113
122
let mut buf = Vec :: new ( ) ;
@@ -119,7 +128,7 @@ pub async fn decode<R>(reader: R) -> Result<Request<Body<BufReader<R>>>, Excepti
119
128
let bytes_read = reader. read_until ( b'\n' , & mut buf) . await ?;
120
129
// No more bytes are yielded from the stream.
121
130
if bytes_read == 0 {
122
- break ;
131
+ return Ok ( None ) ;
123
132
}
124
133
125
134
// We've hit the end delimiter of the stream.
@@ -155,11 +164,15 @@ pub async fn decode<R>(reader: R) -> Result<Request<Body<BufReader<R>>>, Excepti
155
164
}
156
165
157
166
// Process the body if `Content-Length` was passed.
158
- let body = match httparse_req. headers . iter ( ) . find ( |h| h. name == "Content-Length" ) {
167
+ let body = match httparse_req
168
+ . headers
169
+ . iter ( )
170
+ . find ( |h| h. name == "Content-Length" )
171
+ {
159
172
Some ( _header) => Body :: new ( reader) , // TODO: use the header value
160
173
None => Body :: empty ( ) ,
161
174
} ;
162
175
163
176
// Return the request.
164
- Ok ( req. body ( body) ?)
177
+ Ok ( Some ( req. body ( body) ?) )
165
178
}
0 commit comments