mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
update set_date impl
This commit is contained in:
parent
4fadff63f4
commit
756227896b
@ -101,7 +101,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn set_date(&self, dst: &mut BytesMut) {
|
fn set_date(&self, dst: &mut BytesMut) {
|
||||||
self.settings.set_date(dst)
|
self.settings.set_date(dst, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -214,7 +214,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
|
|
||||||
// optimized date header, set_date writes \r\n
|
// optimized date header, set_date writes \r\n
|
||||||
if !has_date {
|
if !has_date {
|
||||||
self.settings.set_date(&mut buffer);
|
self.settings.set_date(&mut buffer, true);
|
||||||
} else {
|
} else {
|
||||||
// msg eof
|
// msg eof
|
||||||
buffer.extend_from_slice(b"\r\n");
|
buffer.extend_from_slice(b"\r\n");
|
||||||
@ -298,7 +298,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
if err.kind() == io::ErrorKind::WriteZero {
|
if err.kind() == io::ErrorKind::WriteZero {
|
||||||
self.disconnected();
|
self.disconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
|
@ -73,7 +73,7 @@ impl<H: 'static> Writer for H2Writer<H> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn set_date(&self, dst: &mut BytesMut) {
|
fn set_date(&self, dst: &mut BytesMut) {
|
||||||
self.settings.set_date(dst)
|
self.settings.set_date(dst, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -97,7 +97,7 @@ impl<H: 'static> Writer for H2Writer<H> {
|
|||||||
// using helpers::date is quite a lot faster
|
// using helpers::date is quite a lot faster
|
||||||
if !msg.headers().contains_key(DATE) {
|
if !msg.headers().contains_key(DATE) {
|
||||||
let mut bytes = BytesMut::with_capacity(29);
|
let mut bytes = BytesMut::with_capacity(29);
|
||||||
self.settings.set_date_simple(&mut bytes);
|
self.settings.set_date(&mut bytes, false);
|
||||||
msg.headers_mut()
|
msg.headers_mut()
|
||||||
.insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap());
|
.insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap());
|
||||||
}
|
}
|
||||||
|
@ -229,16 +229,16 @@ impl<H> WorkerSettings<H> {
|
|||||||
unsafe { &mut *self.date.get() }.update();
|
unsafe { &mut *self.date.get() }.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_date(&self, dst: &mut BytesMut) {
|
pub fn set_date(&self, dst: &mut BytesMut, full: bool) {
|
||||||
let mut buf: [u8; 39] = unsafe { mem::uninitialized() };
|
if full {
|
||||||
buf[..6].copy_from_slice(b"date: ");
|
let mut buf: [u8; 39] = unsafe { mem::uninitialized() };
|
||||||
buf[6..35].copy_from_slice(&(unsafe { &*self.date.get() }.bytes));
|
buf[..6].copy_from_slice(b"date: ");
|
||||||
buf[35..].copy_from_slice(b"\r\n\r\n");
|
buf[6..35].copy_from_slice(&(unsafe { &*self.date.get() }.bytes));
|
||||||
dst.extend_from_slice(&buf);
|
buf[35..].copy_from_slice(b"\r\n\r\n");
|
||||||
}
|
dst.extend_from_slice(&buf);
|
||||||
|
} else {
|
||||||
pub fn set_date_simple(&self, dst: &mut BytesMut) {
|
dst.extend_from_slice(&(unsafe { &*self.date.get() }.bytes));
|
||||||
dst.extend_from_slice(&(unsafe { &*self.date.get() }.bytes));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,9 +284,9 @@ mod tests {
|
|||||||
fn test_date() {
|
fn test_date() {
|
||||||
let settings = WorkerSettings::<()>::new(Vec::new(), KeepAlive::Os);
|
let settings = WorkerSettings::<()>::new(Vec::new(), KeepAlive::Os);
|
||||||
let mut buf1 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
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);
|
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);
|
assert_eq!(buf1, buf2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user