diff --git a/examples/tls/src/main.rs b/examples/tls/src/main.rs index 30625fdb..3d969d3a 100644 --- a/examples/tls/src/main.rs +++ b/examples/tls/src/main.rs @@ -8,7 +8,8 @@ use std::io::Read; use actix::*; use actix_web::*; -#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe}; +#[cfg(target_os = "linux")] +use actix::actors::signal::{ProcessSignals, Subscribe}; /// somple handle fn index(req: HttpRequest) -> Result { @@ -47,7 +48,9 @@ fn main() { .bind("127.0.0.1:8443").unwrap() .start_ssl(&pkcs12).unwrap(); - if cfg!(target_os = "linux") { // Subscribe to unix signals + // Subscribe to unix signals + #[cfg(target_os = "linux")] + { let signals = Arbiter::system_registry().get::(); signals.send(Subscribe(addr.subscriber())); } diff --git a/src/h1writer.rs b/src/h1writer.rs index 2deca9d1..011e56ac 100644 --- a/src/h1writer.rs +++ b/src/h1writer.rs @@ -192,7 +192,6 @@ impl Writer for H1Writer { if let Body::Binary(bytes) = body { self.encoder.write(bytes.as_ref())?; - return Ok(WriterState::Done) } else { msg.replace_body(body); } diff --git a/src/h2writer.rs b/src/h2writer.rs index d3091c6f..51027f7f 100644 --- a/src/h2writer.rs +++ b/src/h2writer.rs @@ -92,7 +92,7 @@ impl H2Writer { let cap = cmp::min(buffer.len(), CHUNK_SIZE); stream.reserve_capacity(cap); } else { - return Ok(WriterState::Done) + return Ok(WriterState::Pause) } } Err(_) => { @@ -130,9 +130,23 @@ 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); - helpers::date(&mut bytes); - msg.headers_mut().insert( - DATE, HeaderValue::try_from(bytes.freeze()).unwrap()); + helpers::date_value(&mut bytes); + msg.headers_mut().insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap()); + } + + let body = msg.replace_body(Body::Empty); + match body { + Body::Binary(ref bytes) => { + let mut val = BytesMut::new(); + helpers::convert_usize(bytes.len(), &mut val); + let l = val.len(); + msg.headers_mut().insert( + CONTENT_LENGTH, HeaderValue::try_from(val.split_to(l-2).freeze()).unwrap()); + } + Body::Empty => { + msg.headers_mut().insert(CONTENT_LENGTH, HeaderValue::from_static("0")); + }, + _ => (), } let mut resp = Response::new(()); @@ -142,19 +156,6 @@ impl Writer for H2Writer { resp.headers_mut().insert(key, value.clone()); } - match *msg.body() { - Body::Binary(ref bytes) => { - let mut val = BytesMut::new(); - helpers::convert_usize(bytes.len(), &mut val); - resp.headers_mut().insert( - CONTENT_LENGTH, HeaderValue::try_from(val.freeze()).unwrap()); - } - Body::Empty => { - resp.headers_mut().insert(CONTENT_LENGTH, HeaderValue::from_static("0")); - }, - _ => (), - } - match self.respond.send_response(resp, self.flags.contains(Flags::EOF)) { Ok(stream) => self.stream = Some(stream), @@ -162,20 +163,19 @@ impl Writer for H2Writer { return Err(io::Error::new(io::ErrorKind::Other, "err")), } - // trace!("Response: {:?}", msg); + trace!("Response: {:?}", msg); - if msg.body().is_binary() { - if let Body::Binary(bytes) = msg.replace_body(Body::Empty) { - self.flags.insert(Flags::EOF); - self.encoder.write(bytes.as_ref())?; - if let Some(ref mut stream) = self.stream { - stream.reserve_capacity(cmp::min(self.encoder.len(), CHUNK_SIZE)); - } - return Ok(WriterState::Done) + if let Body::Binary(bytes) = body { + self.flags.insert(Flags::EOF); + self.encoder.write(bytes.as_ref())?; + if let Some(ref mut stream) = self.stream { + stream.reserve_capacity(cmp::min(self.encoder.len(), CHUNK_SIZE)); } + Ok(WriterState::Pause) + } else { + msg.replace_body(body); + Ok(WriterState::Done) } - - Ok(WriterState::Done) } fn write(&mut self, payload: &[u8]) -> Result { diff --git a/src/helpers.rs b/src/helpers.rs index 8211b03d..ba6fd49d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -11,9 +11,9 @@ use http::Version; use httprequest::HttpMessage; // "Sun, 06 Nov 1994 08:49:37 GMT".len() -pub const DATE_VALUE_LENGTH: usize = 29; +pub(crate) const DATE_VALUE_LENGTH: usize = 29; -pub fn date(dst: &mut BytesMut) { +pub(crate) fn date(dst: &mut BytesMut) { CACHED.with(|cache| { let mut buf: [u8; 39] = unsafe { mem::uninitialized() }; buf[..6].copy_from_slice(b"date: "); @@ -23,7 +23,13 @@ pub fn date(dst: &mut BytesMut) { }) } -pub fn update_date() { +pub(crate) fn date_value(dst: &mut BytesMut) { + CACHED.with(|cache| { + dst.extend_from_slice(cache.borrow().buffer()); + }) +} + +pub(crate) fn update_date() { CACHED.with(|cache| { cache.borrow_mut().update(); }); diff --git a/src/pipeline.rs b/src/pipeline.rs index e8d73942..f37065f0 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -576,12 +576,10 @@ impl ProcessResponse { { if self.drain.is_none() && self.running != RunningState::Paused { // if task is paused, write buffer is probably full - loop { let result = match mem::replace(&mut self.iostate, IOState::Done) { IOState::Response => { - let result = match io.start(info.req_mut().get_inner(), - &mut self.resp) { + let result = match io.start(info.req_mut().get_inner(), &mut self.resp) { Ok(res) => res, Err(err) => { info.error = Some(err.into());