From 71ac1a8a5f7cdf2fd6d503364d3c140611bd067e Mon Sep 17 00:00:00 2001 From: kcz Date: Sun, 24 Nov 2024 15:47:54 -0500 Subject: [PATCH] Fix CRLF sequence --- core/src/http.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/src/http.rs b/core/src/http.rs index fc48d60..637487e 100644 --- a/core/src/http.rs +++ b/core/src/http.rs @@ -39,7 +39,7 @@ pub struct HttpResponse { /// [ message-body ] impl HttpResponse { pub fn new(raw_response: String) -> Result { - let preprocessed_response = raw_response.trim_start().replace("\n\r", "\n"); + let preprocessed_response = raw_response.trim_start().replace("\r\n", "\n"); let (status_line, remaining) = match preprocessed_response.split_once('\n') { Some((s, r)) => (s, r), @@ -162,4 +162,17 @@ mod tests { assert_eq!(res.body(), "body message".to_string()); } + + #[test] + fn test_crlf() { + let raw = "HTTP/1.1 200 OK\r\nDate: xx xx xx\r\n\r\nbody message".to_string(); + let res = HttpResponse::new(raw).expect("failed to parse http response"); + assert_eq!(res.version(), "HTTP/1.1"); + assert_eq!(res.status_code(), 200); + assert_eq!(res.reason(), "OK"); + + assert_eq!(res.header_value("Date"), Ok("xx xx xx".to_string())); + + assert_eq!(res.body(), "body message".to_string()); + } }