@@ -4,7 +4,7 @@ use std::str::FromStr;
44
55use async_std:: io:: { BufReader , Read , Write } ;
66use async_std:: prelude:: * ;
7- use http_types:: headers:: { HeaderName , HeaderValue , CONTENT_LENGTH , HOST , TRANSFER_ENCODING } ;
7+ use http_types:: headers:: { CONTENT_LENGTH , EXPECT , HOST , TRANSFER_ENCODING } ;
88use http_types:: { ensure, ensure_eq, format_err} ;
99use http_types:: { Body , Method , Request } ;
1010
@@ -75,16 +75,14 @@ where
7575 ) ;
7676
7777 for header in httparse_req. headers . iter ( ) {
78- let name = HeaderName :: from_str ( header. name ) ?;
79- let value = HeaderValue :: from_str ( std:: str:: from_utf8 ( header. value ) ?) ?;
80- req. insert_header ( name, value) ?;
78+ req. insert_header ( header. name , std:: str:: from_utf8 ( header. value ) ?) ;
8179 }
8280
8381 set_url_and_port_from_host_header ( & mut req) ?;
8482 handle_100_continue ( & req, & mut io) . await ?;
8583
86- let content_length = req. header ( & CONTENT_LENGTH ) ;
87- let transfer_encoding = req. header ( & TRANSFER_ENCODING ) ;
84+ let content_length = req. header ( CONTENT_LENGTH ) ;
85+ let transfer_encoding = req. header ( TRANSFER_ENCODING ) ;
8886
8987 http_types:: ensure!(
9088 content_length. is_none( ) || transfer_encoding. is_none( ) ,
9391
9492 // Check for Transfer-Encoding
9593 if let Some ( encoding) = transfer_encoding {
96- if ! encoding. is_empty ( ) && encoding . last ( ) . unwrap ( ) . as_str ( ) == "chunked" {
94+ if encoding. last ( ) . as_str ( ) == "chunked" {
9795 let trailer_sender = req. send_trailers ( ) ;
9896 let reader = BufReader :: new ( ChunkedDecoder :: new ( reader, trailer_sender) ) ;
9997 req. set_body ( Body :: from_reader ( reader, None ) ) ;
@@ -104,7 +102,7 @@ where
104102
105103 // Check for Content-Length.
106104 if let Some ( len) = content_length {
107- let len = len. last ( ) . unwrap ( ) . as_str ( ) . parse :: < usize > ( ) ?;
105+ let len = len. last ( ) . as_str ( ) . parse :: < usize > ( ) ?;
108106 req. set_body ( Body :: from_reader ( reader. take ( len as u64 ) , Some ( len) ) ) ;
109107 }
110108
@@ -113,11 +111,11 @@ where
113111
114112fn set_url_and_port_from_host_header ( req : & mut Request ) -> http_types:: Result < ( ) > {
115113 let host = req
116- . header ( & HOST )
117- . and_then ( |header| header. last ( ) ) // There must only exactly one Host header, so this is permissive
118- . ok_or_else ( || format_err ! ( "Mandatory Host header missing" ) ) ?; // https://tools.ietf.org/html/rfc7230#section-5.4
114+ . header ( HOST )
115+ . map ( |header| header. last ( ) ) // There must only exactly one Host header, so this is permissive
116+ . ok_or_else ( || format_err ! ( "Mandatory Host header missing" ) ) ? // https://tools.ietf.org/html/rfc7230#section-5.4
117+ . to_string ( ) ;
119118
120- let host = host. to_string ( ) ;
121119 if let Some ( colon) = host. find ( ":" ) {
122120 req. url_mut ( ) . set_host ( Some ( & host[ 0 ..colon] ) ) ?;
123121 req. url_mut ( )
@@ -130,17 +128,15 @@ fn set_url_and_port_from_host_header(req: &mut Request) -> http_types::Result<()
130128 Ok ( ( ) )
131129}
132130
133- async fn handle_100_continue < IO : Write + Unpin > (
134- req : & Request ,
135- io : & mut IO ,
136- ) -> http_types:: Result < ( ) > {
137- let expect_header_value = req
138- . header ( & HeaderName :: from_str ( "expect" ) . unwrap ( ) )
139- . and_then ( |v| v. last ( ) )
140- . map ( |v| v. as_str ( ) ) ;
141-
142- if let Some ( "100-continue" ) = expect_header_value {
143- io. write_all ( "HTTP/1.1 100 Continue\r \n " . as_bytes ( ) ) . await ?;
131+ const EXPECT_HEADER_VALUE : & str = "100-continue" ;
132+ const EXPECT_RESPONSE : & [ u8 ] = b"HTTP/1.1 100 Continue\r \n " ;
133+
134+ async fn handle_100_continue < IO > ( req : & Request , io : & mut IO ) -> http_types:: Result < ( ) >
135+ where
136+ IO : Write + Unpin ,
137+ {
138+ if let Some ( EXPECT_HEADER_VALUE ) = req. header ( EXPECT ) . map ( |h| h. as_str ( ) ) {
139+ io. write_all ( EXPECT_RESPONSE ) . await ?;
144140 }
145141
146142 Ok ( ( ) )
@@ -162,7 +158,7 @@ mod tests {
162158 #[ test]
163159 fn handle_100_continue_sends_header_if_expects_is_exactly_right ( ) {
164160 let mut request = Request :: new ( Method :: Get , url:: Url :: parse ( "x:" ) . unwrap ( ) ) ;
165- request. append_header ( "expect" , "100-continue" ) . unwrap ( ) ;
161+ request. append_header ( "expect" , "100-continue" ) ;
166162 let mut io = async_std:: io:: Cursor :: new ( vec ! [ ] ) ;
167163 let result = async_std:: task:: block_on ( handle_100_continue ( & request, & mut io) ) ;
168164 assert_eq ! (
@@ -175,9 +171,7 @@ mod tests {
175171 #[ test]
176172 fn handle_100_continue_does_nothing_if_expects_header_is_wrong ( ) {
177173 let mut request = Request :: new ( Method :: Get , url:: Url :: parse ( "x:" ) . unwrap ( ) ) ;
178- request
179- . append_header ( "expect" , "110-extensions-not-allowed" )
180- . unwrap ( ) ;
174+ request. append_header ( "expect" , "110-extensions-not-allowed" ) ;
181175 let mut io = async_std:: io:: Cursor :: new ( vec ! [ ] ) ;
182176 let result = async_std:: task:: block_on ( handle_100_continue ( & request, & mut io) ) ;
183177 assert_eq ! ( std:: str :: from_utf8( & io. into_inner( ) ) . unwrap( ) , "" ) ;
@@ -247,16 +241,8 @@ mod tests {
247241 }
248242
249243 fn request_with_host_header ( host : & str ) -> Request {
250- let mut req = Request :: new (
251- Method :: Get ,
252- url:: Url :: parse ( "http://_" )
253- . unwrap ( )
254- . join ( "/some/path" )
255- . unwrap ( ) ,
256- ) ;
257-
258- req. insert_header ( HOST , host) . unwrap ( ) ;
259-
244+ let mut req = Request :: new ( Method :: Get , url:: Url :: parse ( "http://_/some/path" ) . unwrap ( ) ) ;
245+ req. insert_header ( HOST , host) ;
260246 req
261247 }
262248}
0 commit comments