From 0425e2776f5cbcaca3489a5dd565b12e63bc688c Mon Sep 17 00:00:00 2001 From: Robert Gabriel Jakabosky Date: Sat, 1 Sep 2018 17:00:32 +0800 Subject: [PATCH] Fix Issue #490 (#498) * Add failing testcase for HTTP 404 response with no reason text. * Include canonical reason test for HTTP error responses. * Don't send a reason for unknown status codes. --- src/server/error.rs | 5 +++++ tests/test_server.rs | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/server/error.rs b/src/server/error.rs index 5bd0bf83..d08ccf87 100644 --- a/src/server/error.rs +++ b/src/server/error.rs @@ -21,7 +21,12 @@ impl HttpHandlerTask for ServerError { bytes.reserve(helpers::STATUS_LINE_BUF_SIZE + 1); helpers::write_status_line(self.0, self.1.as_u16(), bytes); } + // Convert Status Code to Reason. + let reason = self.1.canonical_reason().unwrap_or(""); + io.buffer().extend_from_slice(reason.as_bytes()); + // No response body. io.buffer().extend_from_slice(b"\r\ncontent-length: 0\r\n"); + // date header io.set_date(); Ok(Async::Ready(true)) } diff --git a/tests/test_server.rs b/tests/test_server.rs index c573c4e1..8235be6b 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -932,6 +932,29 @@ fn test_application() { assert!(response.status().is_success()); } +#[test] +fn test_default_404_handler_response() { + let mut srv = test::TestServer::with_factory(|| { + App::new() + .prefix("/app") + .resource("", |r| r.f(|_| HttpResponse::Ok())) + .resource("/", |r| r.f(|_| HttpResponse::Ok())) + }); + let addr = srv.addr(); + + let mut buf = [0; 24]; + let request = TcpStream::connect(&addr) + .and_then(|sock| { + tokio::io::write_all(sock, "HEAD / HTTP/1.1\r\nHost: localhost\r\n\r\n") + .and_then(|(sock, _)| tokio::io::read_exact(sock, &mut buf)) + .and_then(|(_, buf)| Ok(buf)) + }) + .map_err(|e| panic!("{:?}", e)); + let response = srv.execute(request).unwrap(); + let rep = String::from_utf8_lossy(&response[..]); + assert!(rep.contains("HTTP/1.1 404 Not Found")); +} + #[test] fn test_server_cookies() { use actix_web::http;