From 3eb44ab7bfc1a5af45e0e05cb1b0bc0899c11af8 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 4 Feb 2022 19:43:54 +0000 Subject: [PATCH] expose ServiceConfig::write_date_header --- actix-http/CHANGES.md | 3 ++- actix-http/src/config.rs | 10 +++++++--- actix-http/src/h1/encoder.rs | 8 ++++---- actix-web/CHANGES.md | 4 ++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 0585f4f20..2e32c62cd 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -6,12 +6,13 @@ ### Changed - `error::DispatcherError` enum is now marked `#[non_exhaustive]`. [#2624] +- Expose `ServiceConfig::write_date_header`. [#2625] ### Fixed - Issue where handlers that took payload but then dropped without reading it to EOF it would cause keep-alive connections to become stuck. [#2624] [#2624]: https://github.com/actix/actix-web/pull/2624 -[#????]: https://github.com/actix/actix-web/pull/???? +[#2625]: https://github.com/actix/actix-web/pull/2625 ## 3.0.0-rc.1 - 2022-01-31 diff --git a/actix-http/src/config.rs b/actix-http/src/config.rs index 8045910be..522c5a023 100644 --- a/actix-http/src/config.rs +++ b/actix-http/src/config.rs @@ -104,8 +104,12 @@ impl ServiceConfig { self.0.date_service.now() } - pub(crate) fn write_date_header(&self, dst: &mut BytesMut, camel_case: bool) { - let mut buf: [u8; 39] = [0; 39]; + /// Writes date header to `dst` buffer. + /// + /// Low-level method that utilizes the built-in efficient date service, requiring fewer syscalls + /// than normal. Note that a CRLF (`\r\n`) is included in what is written. + pub fn write_date_header(&self, dst: &mut BytesMut, camel_case: bool) { + let mut buf: [u8; 37] = [0; 37]; buf[..6].copy_from_slice(if camel_case { b"Date: " } else { b"date: " }); @@ -113,7 +117,7 @@ impl ServiceConfig { .date_service .with_date(|date| buf[6..35].copy_from_slice(&date.bytes)); - buf[35..].copy_from_slice(b"\r\n\r\n"); + buf[35..].copy_from_slice(b"\r\n"); dst.extend_from_slice(&buf); } diff --git a/actix-http/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs index a24ba5911..ba98f4641 100644 --- a/actix-http/src/h1/encoder.rs +++ b/actix-http/src/h1/encoder.rs @@ -210,14 +210,14 @@ pub(crate) trait MessageType: Sized { dst.advance_mut(pos); } - // optimized date header, set_date writes \r\n if !has_date { + // optimized date header, write_date_header writes its own \r\n config.write_date_header(dst, camel_case); - } else { - // msg eof - dst.extend_from_slice(b"\r\n"); } + // end-of-headers marker + dst.extend_from_slice(b"\r\n"); + Ok(()) } diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 168888bb3..24d3b2b18 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,9 +2,9 @@ ## Unreleased - 2021-xx-xx ### Added -- Implement `Responder` for `Vec`. [#????] +- Implement `Responder` for `Vec`. [#2625] -[#????]: https://github.com/actix/actix-web/pull/???? +[#2625]: https://github.com/actix/actix-web/pull/2625 ## 4.0.0-rc.2 - 2022-02-02 ### Added