@@ -13,6 +13,9 @@ use crate::chunked::ChunkedDecoder;
1313use crate :: date:: fmt_http_date;
1414use crate :: { MAX_HEADERS , MAX_HEAD_LENGTH } ;
1515
16+ const CR : u8 = b'\r' ;
17+ const LF : u8 = b'\n' ;
18+
1619/// Decode an HTTP response on the client.
1720#[ doc( hidden) ]
1821pub async fn decode < R > ( reader : R ) -> http_types:: Result < Response >
2629
2730 // Keep reading bytes from the stream until we hit the end of the stream.
2831 loop {
29- let bytes_read = reader. read_until ( b'\n' , & mut buf) . await ?;
32+ let bytes_read = reader. read_until ( LF , & mut buf) . await ?;
3033 // No more bytes are yielded from the stream.
3134 assert ! ( bytes_read != 0 , "Empty response" ) ; // TODO: ensure?
3235
3841
3942 // We've hit the end delimiter of the stream.
4043 let idx = buf. len ( ) - 1 ;
41- if idx >= 3 && & buf[ idx - 3 ..=idx] == b" \r \n \r \n " {
44+ if idx >= 3 && & buf[ idx - 3 ..=idx] == [ CR , LF , CR , LF ] {
4245 break ;
4346 }
4447 }
@@ -75,19 +78,14 @@ where
7578 "Unexpected Content-Length header"
7679 ) ;
7780
78- // Check for Transfer-Encoding
79- match transfer_encoding {
80- Some ( encoding) if !encoding. is_empty ( ) => {
81- if encoding. last ( ) . unwrap ( ) . as_str ( ) == "chunked" {
82- let trailers_sender = res. send_trailers ( ) ;
83- let reader = BufReader :: new ( ChunkedDecoder :: new ( reader, trailers_sender) ) ;
84- res. set_body ( Body :: from_reader ( reader, None ) ) ;
85- return Ok ( res) ;
86- }
87- // Fall through to Content-Length
88- }
89- _ => {
90- // Fall through to Content-Length
81+ if let Some ( encoding) = transfer_encoding {
82+ if !encoding. is_empty ( ) && encoding. last ( ) . unwrap ( ) . as_str ( ) == "chunked" {
83+ let trailers_sender = res. send_trailers ( ) ;
84+ let reader = BufReader :: new ( ChunkedDecoder :: new ( reader, trailers_sender) ) ;
85+ res. set_body ( Body :: from_reader ( reader, None ) ) ;
86+
87+ // Return the response.
88+ return Ok ( res) ;
9189 }
9290 }
9391
0 commit comments