From 756227896b354d3f9ad7d66f181ce4b58d513268 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 23 Jun 2018 10:13:09 +0600 Subject: [PATCH] update set_date impl --- src/server/h1writer.rs | 6 +++--- src/server/h2writer.rs | 4 ++-- src/server/settings.rs | 24 ++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/server/h1writer.rs b/src/server/h1writer.rs index ebb0fff32..bf91a0302 100644 --- a/src/server/h1writer.rs +++ b/src/server/h1writer.rs @@ -101,7 +101,7 @@ impl Writer for H1Writer { #[inline] fn set_date(&self, dst: &mut BytesMut) { - self.settings.set_date(dst) + self.settings.set_date(dst, true) } #[inline] @@ -214,7 +214,7 @@ impl Writer for H1Writer { // optimized date header, set_date writes \r\n if !has_date { - self.settings.set_date(&mut buffer); + self.settings.set_date(&mut buffer, true); } else { // msg eof buffer.extend_from_slice(b"\r\n"); @@ -298,7 +298,7 @@ impl Writer for H1Writer { if err.kind() == io::ErrorKind::WriteZero { self.disconnected(); } - + return Err(err); } Ok(val) => val, diff --git a/src/server/h2writer.rs b/src/server/h2writer.rs index c2731b112..c816c12ab 100644 --- a/src/server/h2writer.rs +++ b/src/server/h2writer.rs @@ -73,7 +73,7 @@ impl Writer for H2Writer { #[inline] fn set_date(&self, dst: &mut BytesMut) { - self.settings.set_date(dst) + self.settings.set_date(dst, true) } #[inline] @@ -97,7 +97,7 @@ impl Writer for H2Writer { // using helpers::date is quite a lot faster if !msg.headers().contains_key(DATE) { let mut bytes = BytesMut::with_capacity(29); - self.settings.set_date_simple(&mut bytes); + self.settings.set_date(&mut bytes, false); msg.headers_mut() .insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap()); } diff --git a/src/server/settings.rs b/src/server/settings.rs index 7ea08c9d0..cf58e4321 100644 --- a/src/server/settings.rs +++ b/src/server/settings.rs @@ -229,16 +229,16 @@ impl WorkerSettings { unsafe { &mut *self.date.get() }.update(); } - pub fn set_date(&self, dst: &mut BytesMut) { - let mut buf: [u8; 39] = unsafe { mem::uninitialized() }; - buf[..6].copy_from_slice(b"date: "); - buf[6..35].copy_from_slice(&(unsafe { &*self.date.get() }.bytes)); - buf[35..].copy_from_slice(b"\r\n\r\n"); - dst.extend_from_slice(&buf); - } - - pub fn set_date_simple(&self, dst: &mut BytesMut) { - dst.extend_from_slice(&(unsafe { &*self.date.get() }.bytes)); + pub fn set_date(&self, dst: &mut BytesMut, full: bool) { + if full { + let mut buf: [u8; 39] = unsafe { mem::uninitialized() }; + buf[..6].copy_from_slice(b"date: "); + buf[6..35].copy_from_slice(&(unsafe { &*self.date.get() }.bytes)); + buf[35..].copy_from_slice(b"\r\n\r\n"); + dst.extend_from_slice(&buf); + } else { + dst.extend_from_slice(&(unsafe { &*self.date.get() }.bytes)); + } } } @@ -284,9 +284,9 @@ mod tests { fn test_date() { let settings = WorkerSettings::<()>::new(Vec::new(), KeepAlive::Os); let mut buf1 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10); - settings.set_date(&mut buf1); + settings.set_date(&mut buf1, true); let mut buf2 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10); - settings.set_date(&mut buf2); + settings.set_date(&mut buf2, true); assert_eq!(buf1, buf2); } }