diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index afc988d43..c28013853 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,14 +1,17 @@ # Changes ## Unreleased - 2021-xx-xx +### Added +- Implement `From>` for `Response>`. [#2625] + ### Changed - `error::DispatcherError` enum is now marked `#[non_exhaustive]`. [#2624] - ### 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 +[#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..ac95a2802 100644 --- a/actix-http/src/config.rs +++ b/actix-http/src/config.rs @@ -104,8 +104,13 @@ 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. + #[doc(hidden)] + 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 +118,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-http/src/responses/response.rs b/actix-http/src/responses/response.rs index da5503c1c..ceb158f65 100644 --- a/actix-http/src/responses/response.rs +++ b/actix-http/src/responses/response.rs @@ -285,6 +285,24 @@ impl From<&'static [u8]> for Response<&'static [u8]> { } } +impl From> for Response> { + fn from(val: Vec) -> Self { + let mut res = Response::with_body(StatusCode::OK, val); + let mime = mime::APPLICATION_OCTET_STREAM.try_into_value().unwrap(); + res.headers_mut().insert(header::CONTENT_TYPE, mime); + res + } +} + +impl From<&Vec> for Response> { + fn from(val: &Vec) -> Self { + let mut res = Response::with_body(StatusCode::OK, val.clone()); + let mime = mime::APPLICATION_OCTET_STREAM.try_into_value().unwrap(); + res.headers_mut().insert(header::CONTENT_TYPE, mime); + res + } +} + impl From for Response { fn from(val: String) -> Self { let mut res = Response::with_body(StatusCode::OK, val); diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 7ce282c8b..37fe6ca6a 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased - 2021-xx-xx +### Added +- Implement `Responder` for `Vec`. [#2625] +- Re-export `KeepAlive` in `http` mod. [#2625] + +[#2625]: https://github.com/actix/actix-web/pull/2625 ## 4.0.0-rc.2 - 2022-02-02 diff --git a/actix-web/src/http/mod.rs b/actix-web/src/http/mod.rs index 2581532cd..91c0ca377 100644 --- a/actix-web/src/http/mod.rs +++ b/actix-web/src/http/mod.rs @@ -3,4 +3,4 @@ pub mod header; // TODO: figure out how best to expose http::Error vs actix_http::Error -pub use actix_http::{uri, ConnectionType, Error, Method, StatusCode, Uri, Version}; +pub use actix_http::{uri, ConnectionType, Error, KeepAlive, Method, StatusCode, Uri, Version}; diff --git a/actix-web/src/response/responder.rs b/actix-web/src/response/responder.rs index d1b9e49e0..cb71369cf 100644 --- a/actix-web/src/response/responder.rs +++ b/actix-web/src/response/responder.rs @@ -132,6 +132,7 @@ macro_rules! impl_responder_by_forward_into_base_response { } impl_responder_by_forward_into_base_response!(&'static [u8]); +impl_responder_by_forward_into_base_response!(Vec); impl_responder_by_forward_into_base_response!(Bytes); impl_responder_by_forward_into_base_response!(BytesMut);