@@ -13,6 +13,9 @@ use crate::chunked::ChunkedDecoder;
13
13
use crate :: date:: fmt_http_date;
14
14
use crate :: { MAX_HEADERS , MAX_HEAD_LENGTH } ;
15
15
16
+ const CR : u8 = b'\r' ;
17
+ const LF : u8 = b'\n' ;
18
+
16
19
/// Decode an HTTP response on the client.
17
20
#[ doc( hidden) ]
18
21
pub async fn decode < R > ( reader : R ) -> http_types:: Result < Response >
26
29
27
30
// Keep reading bytes from the stream until we hit the end of the stream.
28
31
loop {
29
- let bytes_read = reader. read_until ( b'\n' , & mut buf) . await ?;
32
+ let bytes_read = reader. read_until ( LF , & mut buf) . await ?;
30
33
// No more bytes are yielded from the stream.
31
34
assert ! ( bytes_read != 0 , "Empty response" ) ; // TODO: ensure?
32
35
38
41
39
42
// We've hit the end delimiter of the stream.
40
43
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 ] {
42
45
break ;
43
46
}
44
47
}
@@ -75,19 +78,14 @@ where
75
78
"Unexpected Content-Length header"
76
79
) ;
77
80
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) ;
91
89
}
92
90
}
93
91
0 commit comments