1
0
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:
Nikolay Kim 2018-06-23 10:13:09 +06:00
parent 4fadff63f4
commit 756227896b
3 changed files with 17 additions and 17 deletions

View File

@ -101,7 +101,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
#[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<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
// 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");

View File

@ -73,7 +73,7 @@ impl<H: 'static> Writer for H2Writer<H> {
#[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<H: 'static> Writer for H2Writer<H> {
// 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());
}

View File

@ -229,17 +229,17 @@ impl<H> WorkerSettings<H> {
unsafe { &mut *self.date.get() }.update();
}
pub fn set_date(&self, dst: &mut BytesMut) {
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);
}
pub fn set_date_simple(&self, dst: &mut BytesMut) {
} else {
dst.extend_from_slice(&(unsafe { &*self.date.get() }.bytes));
}
}
}
struct Date {
@ -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);
}
}