1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-30 16:40:21 +02:00

Compare commits

..

15 Commits

Author SHA1 Message Date
Rob Ede
8d2abe4b35 expose encoder error 2021-12-08 22:53:08 +00:00
Rob Ede
f12f62ba73 body ergo v4 using any body 2021-12-08 22:45:54 +00:00
Rob Ede
fb5b4734a4 Merge remote-tracking branch 'origin/master' into remove-extensions-from-head 2021-12-08 07:30:14 +00:00
Rob Ede
07f2fe385b standardize crate level lints 2021-12-08 06:09:56 +00:00
Rob Ede
406f694095 standardize rustfmt max_width 2021-12-08 06:01:11 +00:00
Rob Ede
e49e559f47 fix some docs 2021-12-08 05:43:50 +00:00
Rob Ede
4d9dd30c72 fix doc test 2021-12-07 21:50:20 +00:00
Rob Ede
d7a1b434cb update changelog 2021-12-07 21:26:28 +00:00
Rob Ede
d5ad250193 update changelog 2021-12-07 21:04:35 +00:00
Rob Ede
818c0f8cad split request data container out from requesthead 2021-12-07 20:56:28 +00:00
Rob Ede
d35b7644dc add connection level data container (#2491) 2021-12-07 17:23:34 +00:00
fakeshadow
069cf2da07 enable scope middleware with generic res body. (#2492)
Co-authored-by: Rob Ede <robjtede@icloud.com>
2021-12-07 16:26:28 +00:00
fakeshadow
6460e67f84 remove generic body type in App. (#2493) 2021-12-07 15:53:04 +00:00
Rob Ede
9587261c20 add fakeshadow's actix-web in actix-http example 2021-12-07 15:31:15 +00:00
Rob Ede
606a371ec3 improve Data docs 2021-12-06 17:14:56 +00:00
95 changed files with 1077 additions and 1028 deletions

View File

@@ -7,30 +7,36 @@
* `Range` typed header. [#2485]
* `HttpResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468]
* `ServiceResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468]
* `HttpServer::on_connect` now receives a `CloneableExtensions` object. [#2327]
[#2325]: https://github.com/actix/actix-web/pull/2325
[#2327]: https://github.com/actix/actix-web/pull/2327
* Connection data set through the `HttpServer::on_connect` callback is now accessible only from the new `HttpRequest::conn_data()` and `ServiceRequest::conn_data()` methods. [#2491]
* `HttpRequest::{req_data,req_data_mut}`. [#2487]
### Changed
* Rename `Accept::{mime_precedence => ranked}`. [#2480]
* Rename `Accept::{mime_preference => preference}`. [#2480]
* Un-deprecate `App::data_factory`. [#2484]
* `HttpRequest::url_for` no longer constructs URLs with query or fragment components. [#2430]
* `HttpServer::on_connect` now receives a `CloneableExtensions` object. [#2327]
* Remove `B` (body) type parameter on `App`. [#2493]
* Add `B` (body) type parameter on `Scope`. [#2492]
* Request-local data container is no longer part of a `RequestHead`. Instead it is a distinct part of a `Request`. [#2487]
### Fixed
* Accept wildcard `*` items in `AcceptLanguage`. [#2480]
* Re-exports `dev::{BodySize, MessageBody, SizedStream}`. They are exposed through the `body` module. [#2468]
* Typed headers containing lists that require one or more items now enforce this minimum. [#2482]
[#2327]: https://github.com/actix/actix-web/pull/2327
### Removed
* `ConnectionInfo::get`. [#2487]
[#2430]: https://github.com/actix/actix-web/pull/2430
[#2468]: https://github.com/actix/actix-web/pull/2468
[#2480]: https://github.com/actix/actix-web/pull/2480
[#2482]: https://github.com/actix/actix-web/pull/2482
[#2484]: https://github.com/actix/actix-web/pull/2484
[#2485]: https://github.com/actix/actix-web/pull/2485
[#2487]: https://github.com/actix/actix-web/pull/2487
[#2491]: https://github.com/actix/actix-web/pull/2491
[#2492]: https://github.com/actix/actix-web/pull/2492
[#2493]: https://github.com/actix/actix-web/pull/2493
## 4.0.0-beta.13 - 2021-11-30

View File

@@ -118,6 +118,7 @@ futures-util = { version = "0.3.7", default-features = false, features = ["std"]
rand = "0.8"
rcgen = "0.8"
rustls-pemfile = "0.2"
static_assertions = "1"
tls-openssl = { package = "openssl", version = "0.10.9" }
tls-rustls = { package = "rustls", version = "0.20.0" }
zstd = "0.9"

View File

@@ -262,9 +262,9 @@ impl Files {
self
}
/// See [`Files::method_guard`].
#[doc(hidden)]
#[deprecated(since = "0.6.0", note = "Renamed to `method_guard`.")]
/// See [`Files::method_guard`].
pub fn use_guards<G: Guard + 'static>(self, guard: G) -> Self {
self.method_guard(guard)
}

View File

@@ -11,8 +11,8 @@
//! .service(Files::new("/static", ".").prefer_utf8(true));
//! ```
#![deny(rust_2018_idioms)]
#![warn(missing_docs, missing_debug_implementations)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible, missing_docs, missing_debug_implementations)]
use actix_service::boxed::{BoxService, BoxServiceFactory};
use actix_web::{

View File

@@ -1,6 +1,7 @@
//! Various helpers for Actix applications to use during testing.
#![deny(rust_2018_idioms)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]

View File

@@ -14,7 +14,10 @@
* `header::QualityItem::{max, min}`. [#2486]
* `header::Quality::{MAX, MIN}`. [#2486]
* `impl Display` for `header::Quality`. [#2486]
* `CloneableExtensions` object for use in `on_connect` handlers. [#2327]
* Connection data set through the `on_connect_ext` callbacks is now accessible only from the new `Request::conn_data()` method. [#2491]
* `Request::take_conn_data()`. [#2491]
* `Request::take_req_data()`. [#2487]
* `impl Clone` for `RequestHead`. [#2487]
### Changed
* Rename `body::BoxBody::{from_body => new}`. [#2468]
@@ -24,7 +27,6 @@
* `From` implementations on error types now return a `Response<BoxBody>`. [#2468]
* `ResponseBuilder::body(B)` now returns `Response<EitherBody<B>>`. [#2468]
* `ResponseBuilder::finish()` now returns `Response<EitherBody<()>>`. [#2468]
* `on_connect_ext` methods now receive a `CloneableExtensions` object. [#2327]
### Removed
* `ResponseBuilder::streaming`. [#2468]
@@ -36,12 +38,13 @@
* `impl TryFrom<u16>` for `header::Quality`. [#2486]
* `http` module. Most everything it contained is exported at the crate root. [#2488]
[#2327]: https://github.com/actix/actix-web/pull/2327
[#2483]: https://github.com/actix/actix-web/pull/2483
[#2468]: https://github.com/actix/actix-web/pull/2468
[#1920]: https://github.com/actix/actix-web/pull/1920
[#2486]: https://github.com/actix/actix-web/pull/2486
[#2487]: https://github.com/actix/actix-web/pull/2487
[#2488]: https://github.com/actix/actix-web/pull/2488
[#2491]: https://github.com/actix/actix-web/pull/2491
## 3.0.0-beta.14 - 2021-11-30

View File

@@ -84,6 +84,7 @@ zstd = { version = "0.9", optional = true }
actix-server = "2.0.0-rc.1"
actix-http-test = { version = "3.0.0-beta.7", features = ["openssl"] }
actix-tls = { version = "3.0.0-rc.1", features = ["openssl"] }
actix-web = "4.0.0-beta.13"
async-stream = "0.3"
criterion = { version = "0.3", features = ["html_reports"] }
env_logger = "0.9"
@@ -95,7 +96,7 @@ serde_json = "1.0"
static_assertions = "1"
tls-openssl = { package = "openssl", version = "0.10.9" }
tls-rustls = { package = "rustls", version = "0.20.0" }
tokio = { version = "1.2", features = ["net", "rt"] }
tokio = { version = "1.2", features = ["net", "rt", "macros"] }
[[example]]
name = "ws"

View File

@@ -189,11 +189,7 @@ mod _original {
n /= 100;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(
lut_ptr.offset(d1 as isize),
buf_ptr.offset(curr),
2,
);
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(curr), 2);
}
// decode last 1 or 2 chars
@@ -206,11 +202,7 @@ mod _original {
let d1 = n << 1;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(
lut_ptr.offset(d1 as isize),
buf_ptr.offset(curr),
2,
);
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(curr), 2);
}
}

View File

@@ -54,15 +54,10 @@ const EMPTY_HEADER_INDEX: HeaderIndex = HeaderIndex {
value: (0, 0),
};
const EMPTY_HEADER_INDEX_ARRAY: [HeaderIndex; MAX_HEADERS] =
[EMPTY_HEADER_INDEX; MAX_HEADERS];
const EMPTY_HEADER_INDEX_ARRAY: [HeaderIndex; MAX_HEADERS] = [EMPTY_HEADER_INDEX; MAX_HEADERS];
impl HeaderIndex {
fn record(
bytes: &[u8],
headers: &[httparse::Header<'_>],
indices: &mut [HeaderIndex],
) {
fn record(bytes: &[u8], headers: &[httparse::Header<'_>], indices: &mut [HeaderIndex]) {
let bytes_ptr = bytes.as_ptr() as usize;
for (header, indices) in headers.iter().zip(indices.iter_mut()) {
let name_start = header.name.as_ptr() as usize - bytes_ptr;

View File

@@ -0,0 +1,26 @@
use actix_http::HttpService;
use actix_server::Server;
use actix_service::map_config;
use actix_web::{dev::AppConfig, get, App};
#[get("/")]
async fn index() -> &'static str {
"Hello, world. From Actix Web!"
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> std::io::Result<()> {
Server::build()
.bind("hello-world", "127.0.0.1:8080", || {
// construct actix-web app
let app = App::new().service(index);
HttpService::build()
// pass the app to service builder
// map_config is used to map App's configuration to ServiceBuilder
.finish(map_config(app, |_| AppConfig::default()))
.tcp()
})?
.run()
.await
}

View File

@@ -25,10 +25,7 @@ async fn main() -> io::Result<()> {
Ok::<_, Error>(
Response::build(StatusCode::OK)
.insert_header((
"x-head",
HeaderValue::from_static("dummy value!"),
))
.insert_header(("x-head", HeaderValue::from_static("dummy value!")))
.body(body),
)
})

View File

@@ -1,8 +1,7 @@
use std::io;
use actix_http::{
body::MessageBody, header::HeaderValue, Error, HttpService, Request, Response,
StatusCode,
body::MessageBody, header::HeaderValue, Error, HttpService, Request, Response, StatusCode,
};
use actix_server::Server;
use bytes::BytesMut;

View File

@@ -1,8 +1,9 @@
use std::{convert::Infallible, io};
use actix_http::{HttpService, Response, StatusCode};
use actix_http::{
header::HeaderValue, HttpMessage, HttpService, Request, Response, StatusCode,
};
use actix_server::Server;
use http::header::HeaderValue;
#[actix_rt::main]
async fn main() -> io::Result<()> {
@@ -13,13 +14,19 @@ async fn main() -> io::Result<()> {
HttpService::build()
.client_timeout(1000)
.client_disconnect(1000)
.finish(|req| async move {
.on_connect_ext(|_, ext| {
ext.insert(42u32);
})
.finish(|req: Request| async move {
log::info!("{:?}", req);
let mut res = Response::build(StatusCode::OK);
res.insert_header(("x-head", HeaderValue::from_static("dummy value!")));
let forty_two = req.extensions().get::<u32>().unwrap().to_string();
res.insert_header((
"x-head",
HeaderValue::from_static("dummy value!"),
"x-forty-two",
HeaderValue::from_str(&forty_two).unwrap(),
));
Ok::<_, Infallible>(res.body("Hello world!"))

View File

@@ -60,10 +60,7 @@ impl Heartbeat {
impl Stream for Heartbeat {
type Item = Result<Bytes, Error>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
log::trace!("poll");
ready!(self.as_mut().interval.poll_tick(cx));

View File

@@ -1,5 +0,0 @@
max_width = 89
reorder_imports = true
#wrap_comments = true
#fn_args_density = "Compressed"
#use_small_heuristics = false

View File

@@ -165,8 +165,7 @@ mod tests {
#[actix_rt::test]
async fn stream_delayed_error() {
let body =
BodyStream::new(stream::iter(vec![Ok(Bytes::from("1")), Err(StreamErr)]));
let body = BodyStream::new(stream::iter(vec![Ok(Bytes::from("1")), Err(StreamErr)]));
assert!(matches!(to_bytes(body).await, Err(StreamErr)));
pin_project! {

View File

@@ -24,9 +24,7 @@ impl BoxBody {
}
/// Returns a mutable pinned reference to the inner message body type.
pub fn as_pin_mut(
&mut self,
) -> Pin<&mut (dyn MessageBody<Error = Box<dyn StdError>>)> {
pub fn as_pin_mut(&mut self) -> Pin<&mut (dyn MessageBody<Error = Box<dyn StdError>>)> {
self.0.as_mut()
}
}

View File

@@ -19,7 +19,7 @@ use super::BodySize;
pub trait MessageBody {
// TODO: consider this bound to only fmt::Display since the error type is not really used
// and there is an impl for Into<Box<StdError>> on String
type Error: Into<Box<dyn StdError>>;
type Error: Into<Box<dyn StdError>> + 'static;
/// Body size hint.
fn size(&self) -> BodySize;
@@ -272,7 +272,7 @@ impl<B, F, E> MessageBody for MessageBodyMapErr<B, F>
where
B: MessageBody,
F: FnOnce(B::Error) -> E,
E: Into<Box<dyn StdError>>,
E: Into<Box<dyn StdError>> + 'static,
{
type Error = E;
@@ -306,6 +306,8 @@ mod tests {
use super::*;
// static_assertions::assert_obj_safe!(MessageBody<()>);
macro_rules! assert_poll_next {
($pin:expr, $exp:expr) => {
assert_eq!(

View File

@@ -1,5 +1,6 @@
//! Traits and structures to aid consuming and writing HTTP payloads.
// mod any;
mod body_stream;
mod boxed;
mod either;
@@ -9,6 +10,7 @@ mod size;
mod sized_stream;
mod utils;
// pub use self::any::AnyBody;
pub use self::body_stream::BodyStream;
pub use self::boxed::BoxBody;
pub use self::either::EitherBody;

View File

@@ -68,9 +68,8 @@ mod test {
let bytes = to_bytes(body).await.unwrap();
assert_eq!(bytes, b"123"[..]);
let stream =
stream::iter(vec![Bytes::from_static(b"123"), Bytes::from_static(b"abc")])
.map(Ok::<_, Error>);
let stream = stream::iter(vec![Bytes::from_static(b"123"), Bytes::from_static(b"abc")])
.map(Ok::<_, Error>);
let body = BodyStream::new(stream);
let bytes = to_bytes(body).await.unwrap();
assert_eq!(bytes, b"123abc"[..]);

View File

@@ -6,11 +6,10 @@ use actix_service::{IntoServiceFactory, Service, ServiceFactory};
use crate::{
body::{BoxBody, MessageBody},
config::{KeepAlive, ServiceConfig},
extensions::CloneableExtensions,
h1::{self, ExpectHandler, H1Service, UpgradeHandler},
h2::H2Service,
service::HttpService,
ConnectCallback, Request, Response,
ConnectCallback, Extensions, Request, Response,
};
/// A HTTP service builder
@@ -168,7 +167,7 @@ where
/// and handlers.
pub fn on_connect_ext<F>(mut self, f: F) -> Self
where
F: Fn(&T, &mut CloneableExtensions) + 'static,
F: Fn(&T, &mut Extensions) + 'static,
{
self.on_connect_ext = Some(Rc::new(f));
self
@@ -215,8 +214,7 @@ where
self.local_addr,
);
H2Service::with_config(cfg, service.into_factory())
.on_connect_ext(self.on_connect_ext)
H2Service::with_config(cfg, service.into_factory()).on_connect_ext(self.on_connect_ext)
}
/// Finish service configuration and create `HttpService` instance.

View File

@@ -44,17 +44,17 @@ where
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
let decoder = match encoding {
#[cfg(feature = "compress-brotli")]
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
BrotliDecoder::new(Writer::new()),
))),
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(BrotliDecoder::new(
Writer::new(),
)))),
#[cfg(feature = "compress-gzip")]
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
ZlibDecoder::new(Writer::new()),
))),
#[cfg(feature = "compress-gzip")]
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(
GzDecoder::new(Writer::new()),
))),
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(GzDecoder::new(
Writer::new(),
)))),
#[cfg(feature = "compress-zstd")]
ContentEncoding::Zstd => Some(ContentDecoder::Zstd(Box::new(
ZstdDecoder::new(Writer::new()).expect(
@@ -93,10 +93,7 @@ where
{
type Item = Result<Bytes, PayloadError>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
if let Some(ref mut fut) = self.fut {
let (chunk, decoder) =

View File

@@ -1,7 +1,6 @@
//! Stream encoders.
use std::{
error::Error as StdError,
future::Future,
io::{self, Write as _},
pin::Pin,
@@ -10,7 +9,6 @@ use std::{
use actix_rt::task::{spawn_blocking, JoinHandle};
use bytes::Bytes;
use derive_more::Display;
use futures_core::ready;
use pin_project_lite::pin_project;
@@ -23,7 +21,7 @@ use flate2::write::{GzEncoder, ZlibEncoder};
#[cfg(feature = "compress-zstd")]
use zstd::stream::write::Encoder as ZstdEncoder;
use super::Writer;
use super::{EncoderError, Writer};
use crate::{
body::{BodySize, MessageBody},
error::BlockingError,
@@ -53,11 +51,16 @@ impl<B: MessageBody> Encoder<B> {
}
}
pub fn response(
encoding: ContentEncoding,
head: &mut ResponseHead,
body: B,
) -> Self {
pub fn not_acceptable(body: Bytes) -> Self {
Encoder {
body: EncoderBody::Bytes { body },
encoder: None,
fut: None,
eof: false,
}
}
pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self {
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|| head.status == StatusCode::NO_CONTENT
@@ -103,6 +106,7 @@ pin_project! {
#[project = EncoderBodyProj]
enum EncoderBody<B> {
None,
Bytes { body: Bytes },
Stream { #[pin] body: B },
}
}
@@ -116,6 +120,7 @@ where
fn size(&self) -> BodySize {
match self {
EncoderBody::None => BodySize::None,
EncoderBody::Bytes { body } => body.size(),
EncoderBody::Stream { body } => body.size(),
}
}
@@ -126,7 +131,9 @@ where
) -> Poll<Option<Result<Bytes, Self::Error>>> {
match self.project() {
EncoderBodyProj::None => Poll::Ready(None),
EncoderBodyProj::Bytes { body } => {
Pin::new(body).poll_next(cx).map_err(|err| match err {})
}
EncoderBodyProj::Stream { body } => body
.poll_next(cx)
.map_err(|err| EncoderError::Body(err.into())),
@@ -355,32 +362,3 @@ impl ContentEncoder {
}
}
}
#[derive(Debug, Display)]
#[non_exhaustive]
pub enum EncoderError {
#[display(fmt = "body")]
Body(Box<dyn StdError>),
#[display(fmt = "blocking")]
Blocking(BlockingError),
#[display(fmt = "io")]
Io(io::Error),
}
impl StdError for EncoderError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
EncoderError::Body(err) => Some(&**err),
EncoderError::Blocking(err) => Some(err),
EncoderError::Io(err) => Some(err),
}
}
}
impl From<EncoderError> for crate::Error {
fn from(err: EncoderError) -> Self {
crate::Error::new_encoder().with_cause(err)
}
}

View File

@@ -1,22 +1,60 @@
//! Content-Encoding support.
use std::io;
use std::{error::Error as StdError, io};
use bytes::{Bytes, BytesMut};
use derive_more::Display;
use crate::error::BlockingError;
#[cfg(feature = "__compress")]
mod decoder;
#[cfg(feature = "__compress")]
mod encoder;
#[cfg(feature = "__compress")]
pub use self::decoder::Decoder;
#[cfg(feature = "__compress")]
pub use self::encoder::Encoder;
#[derive(Debug, Display)]
#[non_exhaustive]
pub enum EncoderError {
#[display(fmt = "body")]
Body(Box<dyn StdError>),
#[display(fmt = "blocking")]
Blocking(BlockingError),
#[display(fmt = "io")]
Io(io::Error),
}
impl StdError for EncoderError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
EncoderError::Body(err) => Some(&**err),
EncoderError::Blocking(err) => Some(err),
EncoderError::Io(err) => Some(err),
}
}
}
impl From<EncoderError> for crate::Error {
fn from(err: EncoderError) -> Self {
crate::Error::new_encoder().with_cause(err)
}
}
/// Special-purpose writer for streaming (de-)compression.
///
/// Pre-allocates 8KiB of capacity.
#[cfg(feature = "__compress")]
pub(self) struct Writer {
buf: BytesMut,
}
#[cfg(feature = "__compress")]
impl Writer {
fn new() -> Writer {
Writer {
@@ -29,6 +67,7 @@ impl Writer {
}
}
#[cfg(feature = "__compress")]
impl io::Write for Writer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.extend_from_slice(buf);

View File

@@ -9,6 +9,8 @@ use crate::{body::BoxBody, ws, Response};
pub use http::Error as HttpError;
pub use crate::encoding::EncoderError;
pub struct Error {
inner: Box<ErrorInner>,
}
@@ -457,8 +459,7 @@ mod tests {
#[test]
fn test_payload_error() {
let err: PayloadError =
io::Error::new(io::ErrorKind::Other, "ParseError").into();
let err: PayloadError = io::Error::new(io::ErrorKind::Other, "ParseError").into();
assert!(err.to_string().contains("ParseError"));
let err = PayloadError::Incomplete(None);

View File

@@ -19,7 +19,7 @@ impl Extensions {
#[inline]
pub fn new() -> Extensions {
Extensions {
map: AHashMap::default(),
map: AHashMap::new(),
}
}
@@ -122,13 +122,6 @@ impl Extensions {
pub fn extend(&mut self, other: Extensions) {
self.map.extend(other.map);
}
/// Sets (or overrides) items from cloneable extensions map into this map.
pub(crate) fn clone_from(&mut self, other: &CloneableExtensions) {
for (k, val) in &other.map {
self.map.insert(*k, (**val).clone_to_any());
}
}
}
impl fmt::Debug for Extensions {
@@ -141,104 +134,6 @@ fn downcast_owned<T: 'static>(boxed: Box<dyn Any>) -> Option<T> {
boxed.downcast().ok().map(|boxed| *boxed)
}
#[doc(hidden)]
pub trait CloneToAny {
/// Cast `self` into an `Any` reference.
#[cfg(test)]
fn any_ref(&self) -> &dyn Any;
/// Clone `self` to a new `Box<Any>` object.
fn clone_to_any(&self) -> Box<dyn Any>;
/// Clone `self` to a new `Box<CloneAny>` object.
fn clone_to_clone_any(&self) -> Box<dyn CloneAny>;
}
impl<T: Clone + Any> CloneToAny for T {
#[cfg(test)]
fn any_ref(&self) -> &dyn Any {
&*self
}
#[inline]
fn clone_to_any(&self) -> Box<dyn Any> {
Box::new(self.clone())
}
#[inline]
fn clone_to_clone_any(&self) -> Box<dyn CloneAny> {
Box::new(self.clone())
}
}
/// An [`Any`] trait with an additional [`Clone`] requirement.
pub trait CloneAny: CloneToAny + Any {}
impl<T: Any + Clone> CloneAny for T {}
impl Clone for Box<dyn CloneAny> {
#[inline]
fn clone(&self) -> Self {
(**self).clone_to_clone_any()
}
}
trait UncheckedAnyExt {
/// # Safety
/// Caller must ensure type `T` is true type.
#[inline]
unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
Box::from_raw(Box::into_raw(self) as *mut T)
}
}
impl UncheckedAnyExt for dyn CloneAny {}
/// A type map for `on_connect` extensions.
///
/// All entries into this map must be owned types and implement `Clone` trait.
///
/// Many requests can be processed for each connection but the `on_connect` will only be run once
/// when the connection is opened. Therefore, items added to this special map type need to be cloned
/// into the regular extensions map for each request. Most useful connection information types are
/// cloneable already but you can use reference counted wrappers if not.
#[derive(Default)]
pub struct CloneableExtensions {
/// Use AHasher with a std HashMap with for faster lookups on the small `TypeId` keys.
map: AHashMap<TypeId, Box<dyn CloneAny>>,
}
impl CloneableExtensions {
/// Insert an item into the map.
///
/// If an item of this type was already stored, it will be replaced and returned.
///
/// # Examples
/// ```
/// # use actix_http::Extensions;
/// let mut map = Extensions::new();
/// assert_eq!(map.insert(""), None);
/// assert_eq!(map.insert(1u32), None);
/// assert_eq!(map.insert(2u32), Some(1u32));
/// assert_eq!(*map.get::<u32>().unwrap(), 2u32);
/// ```
pub fn insert<T: CloneAny>(&mut self, val: T) -> Option<T> {
self.map
.insert(TypeId::of::<T>(), Box::new(val))
.map(|boxed| {
// Safety:
// Box is owned and `T` is known to be true type from map.
*unsafe { UncheckedAnyExt::downcast_unchecked::<T>(boxed) }
})
}
#[cfg(test)]
fn get<T: CloneAny>(&self) -> Option<&T> {
self.map
.get(&TypeId::of::<T>())
.and_then(|boxed| boxed.as_ref().any_ref().downcast_ref())
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -382,43 +277,4 @@ mod tests {
assert_eq!(extensions.get(), Some(&20u8));
assert_eq!(extensions.get_mut(), Some(&mut 20u8));
}
#[test]
fn test_clone_from() {
#[derive(Clone)]
struct NonCopy {
num: u8,
}
let mut ext = Extensions::new();
ext.insert(2isize);
assert_eq!(ext.get::<isize>(), Some(&2isize));
let mut more_ext = CloneableExtensions::default();
more_ext.insert(3isize);
more_ext.insert(3usize);
more_ext.insert(NonCopy { num: 8 });
ext.clone_from(&more_ext);
assert_eq!(ext.get::<isize>(), Some(&3isize));
assert_eq!(ext.get::<usize>(), Some(&3usize));
assert_eq!(more_ext.get::<isize>(), Some(&3isize));
assert_eq!(more_ext.get::<usize>(), Some(&3usize));
assert!(ext.get::<NonCopy>().is_some());
assert!(more_ext.get::<NonCopy>().is_some());
}
#[test]
fn boxes_not_aliased() {
let a: Box<dyn CloneAny> = Box::new(42);
let b = a.clone_to_clone_any();
assert_ne!(Box::into_raw(a) as *const (), Box::into_raw(b) as *const ());
let a: Box<dyn CloneAny> = Box::new(42);
let b = a.clone_to_any();
assert_ne!(Box::into_raw(a) as *const (), Box::into_raw(b) as *const ());
}
}

View File

@@ -50,10 +50,7 @@ impl ChunkedState {
}
}
fn read_size(
rdr: &mut BytesMut,
size: &mut u64,
) -> Poll<Result<ChunkedState, io::Error>> {
fn read_size(rdr: &mut BytesMut, size: &mut u64) -> Poll<Result<ChunkedState, io::Error>> {
let radix = 16;
let rem = match byte!(rdr) {
@@ -111,10 +108,7 @@ impl ChunkedState {
_ => Poll::Ready(Ok(ChunkedState::Extension)), // no supported extensions
}
}
fn read_size_lf(
rdr: &mut BytesMut,
size: u64,
) -> Poll<Result<ChunkedState, io::Error>> {
fn read_size_lf(rdr: &mut BytesMut, size: u64) -> Poll<Result<ChunkedState, io::Error>> {
match byte!(rdr) {
b'\n' if size > 0 => Poll::Ready(Ok(ChunkedState::Body)),
b'\n' if size == 0 => Poll::Ready(Ok(ChunkedState::EndCr)),

View File

@@ -74,8 +74,7 @@ pub(crate) trait MessageType: Sized {
let headers = self.headers_mut();
for idx in raw_headers.iter() {
let name =
HeaderName::from_bytes(&slice[idx.name.0..idx.name.1]).unwrap();
let name = HeaderName::from_bytes(&slice[idx.name.0..idx.name.1]).unwrap();
// SAFETY: httparse already checks header value is only visible ASCII bytes
// from_maybe_shared_unchecked contains debug assertions so they are omitted here
@@ -605,8 +604,7 @@ mod tests {
#[test]
fn test_parse_body() {
let mut buf =
BytesMut::from("GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
let mut reader = MessageDecoder::<Request>::default();
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
@@ -622,8 +620,7 @@ mod tests {
#[test]
fn test_parse_body_crlf() {
let mut buf =
BytesMut::from("\r\nGET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
let mut buf = BytesMut::from("\r\nGET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
let mut reader = MessageDecoder::<Request>::default();
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();

View File

@@ -22,11 +22,12 @@ use crate::{
config::ServiceConfig,
error::{DispatchError, ParseError, PayloadError},
service::HttpFlow,
OnConnectData, Request, Response, StatusCode,
Extensions, OnConnectData, Request, Response, StatusCode,
};
use super::{
codec::Codec,
decoder::MAX_BUFFER_SIZE,
payload::{Payload, PayloadSender, PayloadStatus},
Message, MessageType,
};
@@ -100,9 +101,9 @@ where
U::Error: fmt::Display,
{
flow: Rc<HttpFlow<S, X, U>>,
on_connect_data: OnConnectData,
flags: Flags,
peer_addr: Option<net::SocketAddr>,
conn_data: Option<Rc<Extensions>>,
error: Option<DispatchError>,
#[pin]
@@ -179,10 +180,10 @@ where
/// Create HTTP/1 dispatcher.
pub(crate) fn new(
io: T,
config: ServiceConfig,
flow: Rc<HttpFlow<S, X, U>>,
on_connect_data: OnConnectData,
config: ServiceConfig,
peer_addr: Option<net::SocketAddr>,
conn_data: OnConnectData,
) -> Self {
let flags = if config.keep_alive_enabled() {
Flags::KEEPALIVE
@@ -198,20 +199,23 @@ where
Dispatcher {
inner: DispatcherState::Normal(InnerDispatcher {
read_buf: BytesMut::with_capacity(HW_BUFFER_SIZE),
write_buf: BytesMut::with_capacity(HW_BUFFER_SIZE),
payload: None,
state: State::None,
error: None,
messages: VecDeque::new(),
io: Some(io),
codec: Codec::new(config),
flow,
on_connect_data,
flags,
peer_addr,
conn_data: conn_data.0.map(Rc::new),
error: None,
state: State::None,
payload: None,
messages: VecDeque::new(),
ka_expire,
ka_timer,
io: Some(io),
read_buf: BytesMut::with_capacity(HW_BUFFER_SIZE),
write_buf: BytesMut::with_capacity(HW_BUFFER_SIZE),
codec: Codec::new(config),
}),
#[cfg(test)]
@@ -256,10 +260,7 @@ where
}
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), io::Error>> {
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
let InnerDispatcherProj { io, write_buf, .. } = self.project();
let mut io = Pin::new(io.as_mut().unwrap());
@@ -269,10 +270,7 @@ where
while written < len {
match io.as_mut().poll_write(cx, &write_buf[written..])? {
Poll::Ready(0) => {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::WriteZero,
"",
)))
return Poll::Ready(Err(io::Error::new(io::ErrorKind::WriteZero, "")))
}
Poll::Ready(n) => written += n,
Poll::Pending => {
@@ -415,15 +413,12 @@ where
while this.write_buf.len() < super::payload::MAX_BUFFER_SIZE {
match stream.as_mut().poll_next(cx) {
Poll::Ready(Some(Ok(item))) => {
this.codec.encode(
Message::Chunk(Some(item)),
this.write_buf,
)?;
this.codec
.encode(Message::Chunk(Some(item)), this.write_buf)?;
}
Poll::Ready(None) => {
this.codec
.encode(Message::Chunk(None), this.write_buf)?;
this.codec.encode(Message::Chunk(None), this.write_buf)?;
// payload stream finished.
// set state to None and handle next message
this.state.set(State::None);
@@ -450,15 +445,12 @@ where
while this.write_buf.len() < super::payload::MAX_BUFFER_SIZE {
match stream.as_mut().poll_next(cx) {
Poll::Ready(Some(Ok(item))) => {
this.codec.encode(
Message::Chunk(Some(item)),
this.write_buf,
)?;
this.codec
.encode(Message::Chunk(Some(item)), this.write_buf)?;
}
Poll::Ready(None) => {
this.codec
.encode(Message::Chunk(None), this.write_buf)?;
this.codec.encode(Message::Chunk(None), this.write_buf)?;
// payload stream finished.
// set state to None and handle next message
this.state.set(State::None);
@@ -564,9 +556,11 @@ where
}
};
}
_ => unreachable!(
"State must be set to ServiceCall or ExceptCall in handle_request"
),
_ => {
unreachable!(
"State must be set to ServiceCall or ExceptCall in handle_request"
)
}
}
}
}
@@ -593,16 +587,14 @@ where
Message::Item(mut req) => {
req.head_mut().peer_addr = *this.peer_addr;
// merge on_connect_ext data into request extensions
this.on_connect_data.merge_into(&mut req);
req.conn_data = this.conn_data.as_ref().map(Rc::clone);
match this.codec.message_type() {
// Request is upgradable. add upgrade message and break.
// everything remain in read buffer would be handed to
// upgraded Request.
MessageType::Stream if this.flow.upgrade.is_some() => {
this.messages
.push_back(DispatcherMessage::Upgrade(req));
this.messages.push_back(DispatcherMessage::Upgrade(req));
break;
}
@@ -617,8 +609,7 @@ where
where the state can be collected and consumed.
*/
let (ps, pl) = Payload::create(false);
let (req1, _) =
req.replace_payload(crate::Payload::H1(pl));
let (req1, _) = req.replace_payload(crate::Payload::H1(pl));
req = req1;
*this.payload = Some(ps);
}
@@ -639,9 +630,7 @@ where
if let Some(ref mut payload) = this.payload {
payload.feed_data(chunk);
} else {
error!(
"Internal server error: unexpected payload chunk"
);
error!("Internal server error: unexpected payload chunk");
this.flags.insert(Flags::READ_DISCONNECT);
this.messages.push_back(DispatcherMessage::Error(
Response::internal_server_error().drop_body(),
@@ -679,12 +668,11 @@ where
payload.set_error(PayloadError::Overflow);
}
// Requests overflow buffer size should be responded with 431
this.messages.push_back(DispatcherMessage::Error(
Response::with_body(
this.messages
.push_back(DispatcherMessage::Error(Response::with_body(
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE,
(),
),
));
)));
this.flags.insert(Flags::READ_DISCONNECT);
*this.error = Some(ParseError::TooLarge.into());
break;
@@ -726,8 +714,7 @@ where
None => {
// conditionally go into shutdown timeout
if this.flags.contains(Flags::SHUTDOWN) {
if let Some(deadline) = this.codec.config().client_disconnect_timer()
{
if let Some(deadline) = this.codec.config().client_disconnect_timer() {
// write client disconnect time out and poll again to
// go into Some<Pin<&mut Sleep>> branch
this.ka_timer.set(Some(sleep_until(deadline)));
@@ -770,9 +757,7 @@ where
this.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
}
// still have unfinished task. try to reset and register keep-alive.
} else if let Some(deadline) =
this.codec.config().keep_alive_expire()
{
} else if let Some(deadline) = this.codec.config().keep_alive_expire() {
timer.as_mut().reset(deadline);
let _ = timer.poll(cx);
}
@@ -791,7 +776,6 @@ where
/// Returns true when io stream can be disconnected after write to it.
///
/// It covers these conditions:
///
/// - `std::io::ErrorKind::ConnectionReset` after partial read.
/// - all data read done.
#[inline(always)]
@@ -811,46 +795,39 @@ where
loop {
// Return early when read buf exceed decoder's max buffer size.
if this.read_buf.len() >= super::decoder::MAX_BUFFER_SIZE {
/*
At this point it's not known IO stream is still scheduled
to be waked up. so force wake up dispatcher just in case.
if this.read_buf.len() >= MAX_BUFFER_SIZE {
// At this point it's not known IO stream is still scheduled to be waked up so
// force wake up dispatcher just in case.
//
// Reason:
// AsyncRead mostly would only have guarantee wake up when the poll_read
// return Poll::Pending.
//
// Case:
// When read_buf is beyond max buffer size the early return could be successfully
// be parsed as a new Request. This case would not generate ParseError::TooLarge and
// at this point IO stream is not fully read to Pending and would result in
// dispatcher stuck until timeout (KA)
//
// Note:
// This is a perf choice to reduce branch on <Request as MessageType>::decode.
//
// A Request head too large to parse is only checked on
// `httparse::Status::Partial` condition.
Reason:
AsyncRead mostly would only have guarantee wake up
when the poll_read return Poll::Pending.
Case:
When read_buf is beyond max buffer size the early return
could be successfully be parsed as a new Request.
This case would not generate ParseError::TooLarge
and at this point IO stream is not fully read to Pending
and would result in dispatcher stuck until timeout (KA)
Note:
This is a perf choice to reduce branch on
<Request as MessageType>::decode.
A Request head too large to parse is only checked on
httparse::Status::Partial condition.
*/
if this.payload.is_none() {
/*
When dispatcher has a payload the responsibility of
wake up it would be shift to h1::payload::Payload.
Reason:
Self wake up when there is payload would waste poll
and/or result in over read.
Case:
When payload is (partial) dropped by user there is
no need to do read anymore.
At this case read_buf could always remain beyond
MAX_BUFFER_SIZE and self wake up would be busy poll
dispatcher and waste resource.
*/
// When dispatcher has a payload the responsibility of wake up it would be shift
// to h1::payload::Payload.
//
// Reason:
// Self wake up when there is payload would waste poll and/or result in
// over read.
//
// Case:
// When payload is (partial) dropped by user there is no need to do
// read anymore. At this case read_buf could always remain beyond
// MAX_BUFFER_SIZE and self wake up would be busy poll dispatcher and
// waste resources.
cx.waker().wake_by_ref();
}
@@ -1058,14 +1035,12 @@ mod tests {
}
fn ok_service(
) -> impl Service<Request, Response = Response<impl MessageBody>, Error = Error>
{
) -> impl Service<Request, Response = Response<impl MessageBody>, Error = Error> {
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::ok())))
}
fn echo_path_service(
) -> impl Service<Request, Response = Response<impl MessageBody>, Error = Error>
{
) -> impl Service<Request, Response = Response<impl MessageBody>, Error = Error> {
fn_service(|req: Request| {
let path = req.path().as_bytes();
ready(Ok::<_, Error>(
@@ -1074,8 +1049,8 @@ mod tests {
})
}
fn echo_payload_service(
) -> impl Service<Request, Response = Response<Bytes>, Error = Error> {
fn echo_payload_service() -> impl Service<Request, Response = Response<Bytes>, Error = Error>
{
fn_service(|mut req: Request| {
Box::pin(async move {
use futures_util::stream::StreamExt as _;
@@ -1100,10 +1075,10 @@ mod tests {
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
buf,
ServiceConfig::default(),
services,
OnConnectData::default(),
ServiceConfig::default(),
None,
OnConnectData::default(),
);
actix_rt::pin!(h1);
@@ -1140,10 +1115,10 @@ mod tests {
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
buf,
cfg,
services,
OnConnectData::default(),
cfg,
None,
OnConnectData::default(),
);
actix_rt::pin!(h1);
@@ -1194,10 +1169,10 @@ mod tests {
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
buf,
cfg,
services,
OnConnectData::default(),
cfg,
None,
OnConnectData::default(),
);
actix_rt::pin!(h1);
@@ -1244,10 +1219,10 @@ mod tests {
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
buf.clone(),
cfg,
services,
OnConnectData::default(),
cfg,
None,
OnConnectData::default(),
);
buf.extend_read_buf(
@@ -1316,10 +1291,10 @@ mod tests {
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
buf.clone(),
cfg,
services,
OnConnectData::default(),
cfg,
None,
OnConnectData::default(),
);
buf.extend_read_buf(
@@ -1393,10 +1368,10 @@ mod tests {
let h1 = Dispatcher::<_, _, _, _, TestUpgrade>::new(
buf.clone(),
cfg,
services,
OnConnectData::default(),
cfg,
None,
OnConnectData::default(),
);
buf.extend_read_buf(

View File

@@ -103,9 +103,7 @@ pub(crate) trait MessageType: Sized {
dst.put_slice(b"\r\n");
}
}
BodySize::Sized(0) if camel_case => {
dst.put_slice(b"\r\nContent-Length: 0\r\n")
}
BodySize::Sized(0) if camel_case => dst.put_slice(b"\r\nContent-Length: 0\r\n"),
BodySize::Sized(0) => dst.put_slice(b"\r\ncontent-length: 0\r\n"),
BodySize::Sized(len) => helpers::write_content_length(len, dst),
BodySize::None => dst.put_slice(b"\r\n"),
@@ -307,11 +305,7 @@ impl MessageType for RequestHeadType {
Version::HTTP_11 => "HTTP/1.1",
Version::HTTP_2 => "HTTP/2.0",
Version::HTTP_3 => "HTTP/3.0",
_ =>
return Err(io::Error::new(
io::ErrorKind::Other,
"unsupported version"
)),
_ => return Err(io::Error::new(io::ErrorKind::Other, "unsupported version")),
}
)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
@@ -568,8 +562,7 @@ mod tests {
ConnectionType::Close,
&ServiceConfig::default(),
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
let data = String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
assert!(data.contains("Content-Length: 0\r\n"));
assert!(data.contains("Connection: close\r\n"));
@@ -583,8 +576,7 @@ mod tests {
ConnectionType::KeepAlive,
&ServiceConfig::default(),
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
let data = String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
assert!(data.contains("Transfer-Encoding: chunked\r\n"));
assert!(data.contains("Content-Type: plain/text\r\n"));
assert!(data.contains("Date: date\r\n"));
@@ -605,8 +597,7 @@ mod tests {
ConnectionType::KeepAlive,
&ServiceConfig::default(),
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
let data = String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
assert!(data.contains("transfer-encoding: chunked\r\n"));
assert!(data.contains("content-type: xml\r\n"));
assert!(data.contains("content-type: plain/text\r\n"));
@@ -639,8 +630,7 @@ mod tests {
ConnectionType::Close,
&ServiceConfig::default(),
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
let data = String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
assert!(data.contains("content-length: 0\r\n"));
assert!(data.contains("connection: close\r\n"));
assert!(data.contains("authorization: another authorization\r\n"));
@@ -663,8 +653,7 @@ mod tests {
ConnectionType::Upgrade,
&ServiceConfig::default(),
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
let data = String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
assert!(!data.contains("content-length: 0\r\n"));
assert!(!data.contains("transfer-encoding: chunked\r\n"));
}

View File

@@ -227,10 +227,7 @@ impl Inner {
self.len
}
fn readany(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, PayloadError>>> {
fn readany(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, PayloadError>>> {
if let Some(data) = self.items.pop_front() {
self.len -= data.len();
self.need_read = self.len < MAX_BUFFER_SIZE;

View File

@@ -266,8 +266,7 @@ where
}
}
impl<T, S, B, X, U> ServiceFactory<(T, Option<net::SocketAddr>)>
for H1Service<T, S, B, X, U>
impl<T, S, B, X, U> ServiceFactory<(T, Option<net::SocketAddr>)> for H1Service<T, S, B, X, U>
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
@@ -310,9 +309,9 @@ where
let upgrade = match upgrade {
Some(upgrade) => {
let upgrade = upgrade.await.map_err(|e| {
log::error!("Init http upgrade service error: {:?}", e)
})?;
let upgrade = upgrade
.await
.map_err(|e| log::error!("Init http upgrade service error: {:?}", e))?;
Some(upgrade)
}
None => None,
@@ -336,8 +335,7 @@ where
/// `Service` implementation for HTTP/1 transport
pub type H1ServiceHandler<T, S, B, X, U> = HttpServiceHandler<T, S, B, X, U>;
impl<T, S, B, X, U> Service<(T, Option<net::SocketAddr>)>
for HttpServiceHandler<T, S, B, X, U>
impl<T, S, B, X, U> Service<(T, Option<net::SocketAddr>)> for HttpServiceHandler<T, S, B, X, U>
where
T: AsyncRead + AsyncWrite + Unpin,
@@ -365,15 +363,7 @@ where
}
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
let on_connect_data =
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
Dispatcher::new(
io,
self.cfg.clone(),
self.flow.clone(),
on_connect_data,
addr,
)
let conn_data = OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
Dispatcher::new(io, self.flow.clone(), self.cfg.clone(), addr, conn_data)
}
}

View File

@@ -70,15 +70,12 @@ where
.unwrap()
.is_write_buf_full()
{
let next =
match this.body.as_mut().as_pin_mut().unwrap().poll_next(cx) {
Poll::Ready(Some(Ok(item))) => Poll::Ready(Some(item)),
Poll::Ready(Some(Err(err))) => {
return Poll::Ready(Err(err.into()))
}
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
};
let next = match this.body.as_mut().as_pin_mut().unwrap().poll_next(cx) {
Poll::Ready(Some(Ok(item))) => Poll::Ready(Some(item)),
Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err.into())),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
};
match next {
Poll::Ready(item) => {
@@ -88,9 +85,9 @@ where
let _ = this.body.take();
}
let framed = this.framed.as_mut().as_pin_mut().unwrap();
framed.write(Message::Chunk(item)).map_err(|err| {
Error::new_send_response().with_cause(err)
})?;
framed
.write(Message::Chunk(item))
.map_err(|err| Error::new_send_response().with_cause(err))?;
}
Poll::Pending => body_ready = false,
}

View File

@@ -27,7 +27,7 @@ use crate::{
body::{BodySize, BoxBody, MessageBody},
config::ServiceConfig,
service::HttpFlow,
OnConnectData, Payload, Request, Response, ResponseHead,
Extensions, OnConnectData, Payload, Request, Response, ResponseHead,
};
const CHUNK_SIZE: usize = 16_384;
@@ -37,7 +37,7 @@ pin_project! {
pub struct Dispatcher<T, S, B, X, U> {
flow: Rc<HttpFlow<S, X, U>>,
connection: Connection<T, Bytes>,
on_connect_data: OnConnectData,
conn_data: Option<Rc<Extensions>>,
config: ServiceConfig,
peer_addr: Option<net::SocketAddr>,
ping_pong: Option<H2PingPong>,
@@ -50,11 +50,11 @@ where
T: AsyncRead + AsyncWrite + Unpin,
{
pub(crate) fn new(
flow: Rc<HttpFlow<S, X, U>>,
mut conn: Connection<T, Bytes>,
on_connect_data: OnConnectData,
flow: Rc<HttpFlow<S, X, U>>,
config: ServiceConfig,
peer_addr: Option<net::SocketAddr>,
conn_data: OnConnectData,
timer: Option<Pin<Box<Sleep>>>,
) -> Self {
let ping_pong = config.keep_alive().map(|dur| H2PingPong {
@@ -74,7 +74,7 @@ where
config,
peer_addr,
connection: conn,
on_connect_data,
conn_data: conn_data.0.map(Rc::new),
ping_pong,
_phantom: PhantomData,
}
@@ -109,7 +109,7 @@ where
Poll::Ready(Some((req, tx))) => {
let (parts, body) = req.into_parts();
let pl = crate::h2::Payload::new(body);
let pl = Payload::<crate::payload::PayloadStream>::H2(pl);
let pl = Payload::H2(pl);
let mut req = Request::with_payload(pl);
let head = req.head_mut();
@@ -119,8 +119,7 @@ where
head.headers = parts.headers.into();
head.peer_addr = this.peer_addr;
// merge on_connect_ext data into request extensions
this.on_connect_data.merge_into(&mut req);
req.conn_data = this.conn_data.as_ref().map(Rc::clone);
let fut = this.flow.service.call(req);
let config = this.config.clone();
@@ -161,16 +160,11 @@ where
Poll::Ready(_) => {
ping_pong.on_flight = false;
let dead_line =
this.config.keep_alive_expire().unwrap();
let dead_line = this.config.keep_alive_expire().unwrap();
ping_pong.timer.as_mut().reset(dead_line);
}
Poll::Pending => {
return ping_pong
.timer
.as_mut()
.poll(cx)
.map(|_| Ok(()))
return ping_pong.timer.as_mut().poll(cx).map(|_| Ok(()))
}
}
} else {

View File

@@ -40,10 +40,7 @@ impl Payload {
impl Stream for Payload {
type Item = Result<Bytes, PayloadError>;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
match ready!(Pin::new(&mut this.stream).poll_data(cx)) {

View File

@@ -1,7 +1,7 @@
use std::{
future::Future,
marker::PhantomData,
net,
mem, net,
pin::Pin,
rc::Rc,
task::{Context, Poll},
@@ -10,8 +10,7 @@ use std::{
use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::net::TcpStream;
use actix_service::{
fn_factory, fn_service, IntoServiceFactory, Service, ServiceFactory,
ServiceFactoryExt as _,
fn_factory, fn_service, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _,
};
use actix_utils::future::ready;
use futures_core::{future::LocalBoxFuture, ready};
@@ -279,8 +278,7 @@ where
}
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
let on_connect_data =
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
let on_connect_data = OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
H2ServiceHandlerResponse {
state: State::Handshake(
@@ -339,21 +337,24 @@ where
ref mut srv,
ref mut config,
ref peer_addr,
ref mut on_connect_data,
ref mut conn_data,
ref mut handshake,
) => match ready!(Pin::new(handshake).poll(cx)) {
Ok((conn, timer)) => {
let on_connect_data = std::mem::take(on_connect_data);
let on_connect_data = mem::take(conn_data);
self.state = State::Incoming(Dispatcher::new(
srv.take().unwrap(),
conn,
on_connect_data,
srv.take().unwrap(),
config.take().unwrap(),
*peer_addr,
on_connect_data,
timer,
));
self.poll(cx)
}
Err(err) => {
trace!("H2 handshake error: {}", err);
Poll::Ready(Err(err))

View File

@@ -6,7 +6,7 @@ use http::header::{HeaderName, InvalidHeaderName};
/// Sealed trait implemented for types that can be effectively borrowed as a [`HeaderValue`].
///
/// [`HeaderValue`]: crate::http::HeaderValue
/// [`HeaderValue`]: super::HeaderValue
pub trait AsHeaderName: Sealed {}
pub struct Seal;

View File

@@ -12,7 +12,7 @@ use super::{Header, IntoHeaderValue};
/// An interface for types that can be converted into a [`HeaderName`]/[`HeaderValue`] pair for
/// insertion into a [`HeaderMap`].
///
/// [`HeaderMap`]: crate::http::HeaderMap
/// [`HeaderMap`]: super::HeaderMap
pub trait IntoHeaderPair: Sized {
type Error: Into<HttpError>;

View File

@@ -123,12 +123,11 @@ impl HeaderMap {
let mut map = HeaderMap::with_capacity(capacity);
map.append(first_name.clone(), first_value);
let (map, _) =
drain.fold((map, first_name), |(mut map, prev_name), (name, value)| {
let name = name.unwrap_or(prev_name);
map.append(name.clone(), value);
(map, name)
});
let (map, _) = drain.fold((map, first_name), |(mut map, prev_name), (name, value)| {
let name = name.unwrap_or(prev_name);
map.append(name.clone(), value);
(map, name)
});
map
}

View File

@@ -11,22 +11,20 @@ pub use http::header::{
pub use http::header::{
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC,
AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING,
CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_RANGE,
CONTENT_SECURITY_POLICY, CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE,
DATE, DNT, ETAG, EXPECT, EXPIRES, FORWARDED, FROM, HOST, IF_MATCH,
IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED,
LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA, PROXY_AUTHENTICATE,
PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY, RANGE, REFERER,
REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT,
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_EXPOSE_HEADERS,
ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE,
ALLOW, ALT_SVC, AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION,
CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_RANGE,
CONTENT_SECURITY_POLICY, CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE, DATE,
DNT, ETAG, EXPECT, EXPIRES, FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE,
IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK, LOCATION, MAX_FORWARDS,
ORIGIN, PRAGMA, PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, PUBLIC_KEY_PINS,
PUBLIC_KEY_PINS_REPORT_ONLY, RANGE, REFERER, REFERRER_POLICY, REFRESH, RETRY_AFTER,
SEC_WEBSOCKET_ACCEPT, SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE, STRICT_TRANSPORT_SECURITY, TE, TRAILER,
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA,
WARNING, WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL,
X_FRAME_OPTIONS, X_XSS_PROTECTION,
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA, WARNING,
WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL, X_FRAME_OPTIONS,
X_XSS_PROTECTION,
};
use crate::{error::ParseError, HttpMessage};
@@ -43,8 +41,8 @@ pub use self::into_pair::IntoHeaderPair;
pub use self::into_value::IntoHeaderValue;
pub use self::map::HeaderMap;
pub use self::shared::{
parse_extended_value, q, Charset, ContentEncoding, ExtendedValue, HttpDate,
LanguageTag, Quality, QualityItem,
parse_extended_value, q, Charset, ContentEncoding, ExtendedValue, HttpDate, LanguageTag,
Quality, QualityItem,
};
pub use self::utils::{
fmt_comma_delimited, from_comma_delimited, from_one_raw_str, http_percent_encode,

View File

@@ -63,9 +63,7 @@ pub struct ExtendedValue {
/// [RFC 2231 §7]: https://datatracker.ietf.org/doc/html/rfc2231#section-7
/// [RFC 2978 §2.3]: https://datatracker.ietf.org/doc/html/rfc2978#section-2.3
/// [RFC 3986 §2.1]: https://datatracker.ietf.org/doc/html/rfc5646#section-2.1
pub fn parse_extended_value(
val: &str,
) -> Result<ExtendedValue, crate::error::ParseError> {
pub fn parse_extended_value(val: &str) -> Result<ExtendedValue, crate::error::ParseError> {
// Break into three pieces separated by the single-quote character
let mut parts = val.splitn(3, '\'');
@@ -100,8 +98,7 @@ pub fn parse_extended_value(
impl fmt::Display for ExtendedValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let encoded_value =
percent_encoding::percent_encode(&self.value[..], HTTP_VALUE);
let encoded_value = percent_encoding::percent_encode(&self.value[..], HTTP_VALUE);
if let Some(ref lang) = self.language_tag {
write!(f, "{}'{}'{}", self.charset, lang, encoded_value)
} else {
@@ -143,8 +140,8 @@ mod tests {
assert!(extended_value.language_tag.is_none());
assert_eq!(
vec![
194, 163, b' ', b'a', b'n', b'd', b' ', 226, 130, 172, b' ', b'r', b'a',
b't', b'e', b's',
194, 163, b' ', b'a', b'n', b'd', b' ', 226, 130, 172, b' ', b'r', b'a', b't',
b'e', b's',
],
extended_value.value
);
@@ -185,8 +182,8 @@ mod tests {
charset: Charset::Ext("UTF-8".to_string()),
language_tag: None,
value: vec![
194, 163, b' ', b'a', b'n', b'd', b' ', 226, 130, 172, b' ', b'r', b'a',
b't', b'e', b's',
194, 163, b' ', b'a', b'n', b'd', b' ', 226, 130, 172, b' ', b'r', b'a', b't',
b'e', b's',
],
};
assert_eq!(

View File

@@ -4,8 +4,7 @@ use bytes::BytesMut;
use http::header::{HeaderValue, InvalidHeaderValue};
use crate::{
config::DATE_VALUE_LENGTH, error::ParseError, header::IntoHeaderValue,
helpers::MutWriter,
config::DATE_VALUE_LENGTH, error::ParseError, header::IntoHeaderValue, helpers::MutWriter,
};
/// A timestamp with HTTP-style formatting and parsing.

View File

@@ -120,8 +120,7 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
}
let q_value = q_val.parse::<f32>().map_err(|_| ParseError::Header)?;
let q_value =
Quality::try_from(q_value).map_err(|_| ParseError::Header)?;
let q_value = Quality::try_from(q_value).map_err(|_| ParseError::Header)?;
quality = q_value;
raw_item = val;

View File

@@ -14,7 +14,8 @@
//! [rustls]: https://crates.io/crates/rustls
//! [trust-dns]: https://crates.io/crates/trust-dns
#![deny(rust_2018_idioms, nonstandard_style, clippy::uninit_assumed_init)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![allow(
clippy::type_complexity,
clippy::too_many_arguments,
@@ -31,7 +32,6 @@ pub mod body;
mod builder;
mod config;
#[cfg(feature = "__compress")]
pub mod encoding;
mod extensions;
pub mod header;
@@ -53,7 +53,7 @@ pub mod ws;
pub use self::builder::HttpServiceBuilder;
pub use self::config::{KeepAlive, ServiceConfig};
pub use self::error::Error;
pub use self::extensions::{CloneableExtensions, Extensions};
pub use self::extensions::Extensions;
pub use self::header::ContentEncoding;
pub use self::http_message::HttpMessage;
pub use self::message::ConnectionType;
@@ -76,35 +76,24 @@ pub enum Protocol {
Http3,
}
type ConnectCallback<IO> = dyn Fn(&IO, &mut CloneableExtensions);
type ConnectCallback<IO> = dyn Fn(&IO, &mut Extensions);
/// Container for data that extract with ConnectCallback.
///
/// # Implementation Details
/// Uses Option to reduce necessary allocations when merging with request extensions.
#[derive(Default)]
pub(crate) struct OnConnectData(Option<CloneableExtensions>);
pub(crate) struct OnConnectData(Option<Extensions>);
impl OnConnectData {
/// Construct by calling the on-connect callback with the underlying transport I/O.
pub(crate) fn from_io<T>(
io: &T,
on_connect_ext: Option<&ConnectCallback<T>>,
) -> Self {
pub(crate) fn from_io<T>(io: &T, on_connect_ext: Option<&ConnectCallback<T>>) -> Self {
let ext = on_connect_ext.map(|handler| {
let mut extensions = CloneableExtensions::default();
let mut extensions = Extensions::default();
handler(io, &mut extensions);
extensions
});
Self(ext)
}
/// Merge self into given request's extensions.
#[inline]
pub(crate) fn merge_into(&mut self, req: &mut Request) {
if let Some(ref ext) = self.0 {
req.head.extensions.get_mut().clone_from(ext);
}
}
}

View File

@@ -44,13 +44,12 @@ pub trait Head: Default + 'static {
F: FnOnce(&MessagePool<Self>) -> R;
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct RequestHead {
pub method: Method,
pub uri: Uri,
pub version: Version,
pub headers: HeaderMap,
pub extensions: RefCell<Extensions>,
pub peer_addr: Option<net::SocketAddr>,
flags: Flags,
}
@@ -62,7 +61,6 @@ impl Default for RequestHead {
uri: Uri::default(),
version: Version::HTTP_11,
headers: HeaderMap::with_capacity(16),
extensions: RefCell::new(Extensions::new()),
peer_addr: None,
flags: Flags::empty(),
}
@@ -73,7 +71,6 @@ impl Head for RequestHead {
fn clear(&mut self) {
self.flags = Flags::empty();
self.headers.clear();
self.extensions.get_mut().clear();
}
fn with_pool<F, R>(f: F) -> R
@@ -85,18 +82,6 @@ impl Head for RequestHead {
}
impl RequestHead {
/// Message extensions
#[inline]
pub fn extensions(&self) -> Ref<'_, Extensions> {
self.extensions.borrow()
}
/// Mutable reference to a the message's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<'_, Extensions> {
self.extensions.borrow_mut()
}
/// Read the message headers.
pub fn headers(&self) -> &HeaderMap {
&self.headers

View File

@@ -56,10 +56,7 @@ where
type Item = Result<Bytes, PayloadError>;
#[inline]
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.get_mut() {
Payload::None => Poll::Ready(None),
Payload::H1(ref mut pl) => pl.readany(cx),

View File

@@ -1,8 +1,10 @@
//! HTTP requests.
use std::{
cell::{Ref, RefMut},
fmt, net, str,
cell::{Ref, RefCell, RefMut},
fmt, mem, net,
rc::Rc,
str,
};
use http::{header, Method, Uri, Version};
@@ -19,6 +21,8 @@ use crate::{
pub struct Request<P = PayloadStream> {
pub(crate) payload: Payload<P>,
pub(crate) head: Message<RequestHead>,
pub(crate) conn_data: Option<Rc<Extensions>>,
pub(crate) req_data: RefCell<Extensions>,
}
impl<P> HttpMessage for Request<P> {
@@ -30,19 +34,19 @@ impl<P> HttpMessage for Request<P> {
}
fn take_payload(&mut self) -> Payload<P> {
std::mem::replace(&mut self.payload, Payload::None)
mem::replace(&mut self.payload, Payload::None)
}
/// Request extensions
#[inline]
fn extensions(&self) -> Ref<'_, Extensions> {
self.head.extensions()
self.req_data.borrow()
}
/// Mutable reference to a the request's extensions
#[inline]
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
self.head.extensions_mut()
self.req_data.borrow_mut()
}
}
@@ -51,6 +55,8 @@ impl From<Message<RequestHead>> for Request<PayloadStream> {
Request {
head,
payload: Payload::None,
req_data: RefCell::new(Extensions::default()),
conn_data: None,
}
}
}
@@ -61,6 +67,8 @@ impl Request<PayloadStream> {
Request {
head: Message::new(),
payload: Payload::None,
req_data: RefCell::new(Extensions::default()),
conn_data: None,
}
}
}
@@ -71,16 +79,21 @@ impl<P> Request<P> {
Request {
payload,
head: Message::new(),
req_data: RefCell::new(Extensions::default()),
conn_data: None,
}
}
/// Create new Request instance
pub fn replace_payload<P1>(self, payload: Payload<P1>) -> (Request<P1>, Payload<P>) {
let pl = self.payload;
(
Request {
payload,
head: self.head,
req_data: self.req_data,
conn_data: self.conn_data,
},
pl,
)
@@ -93,7 +106,7 @@ impl<P> Request<P> {
/// Get request's payload
pub fn take_payload(&mut self) -> Payload<P> {
std::mem::replace(&mut self.payload, Payload::None)
mem::replace(&mut self.payload, Payload::None)
}
/// Split request into request head and payload
@@ -116,7 +129,7 @@ impl<P> Request<P> {
/// Mutable reference to the message's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.head_mut().headers
&mut self.head.headers
}
/// Request's uri.
@@ -128,7 +141,7 @@ impl<P> Request<P> {
/// Mutable reference to the request's uri.
#[inline]
pub fn uri_mut(&mut self) -> &mut Uri {
&mut self.head_mut().uri
&mut self.head.uri
}
/// Read the Request method.
@@ -170,6 +183,31 @@ impl<P> Request<P> {
pub fn peer_addr(&self) -> Option<net::SocketAddr> {
self.head().peer_addr
}
/// Returns a reference a piece of connection data set in an [on-connect] callback.
///
/// ```ignore
/// let opt_t = req.conn_data::<PeerCertificate>();
/// ```
///
/// [on-connect]: crate::HttpServiceBuilder::on_connect_ext
pub fn conn_data<T: 'static>(&self) -> Option<&T> {
self.conn_data
.as_deref()
.and_then(|container| container.get::<T>())
}
/// Returns the connection data container if an [on-connect] callback was registered.
///
/// [on-connect]: crate::HttpServiceBuilder::on_connect_ext
pub fn take_conn_data(&mut self) -> Option<Rc<Extensions>> {
self.conn_data.take()
}
/// Returns the request data container, leaving an empty one in it's place.
pub fn take_req_data(&mut self) -> Extensions {
mem::take(&mut self.req_data.get_mut())
}
}
impl<P> fmt::Debug for Request<P> {

View File

@@ -2,7 +2,7 @@
use std::{
cell::{Ref, RefMut},
fmt, str,
fmt, mem, str,
};
use bytes::{Bytes, BytesMut};
@@ -203,6 +203,12 @@ impl<B> Response<B> {
}
}
impl<B: Default> Response<B> {
pub fn take_body(&mut self) -> B {
mem::take(&mut self.body)
}
}
impl<B> fmt::Debug for Response<B>
where
B: MessageBody,
@@ -231,9 +237,7 @@ impl<B: Default> Default for Response<B> {
}
}
impl<I: Into<Response<BoxBody>>, E: Into<Error>> From<Result<I, E>>
for Response<BoxBody>
{
impl<I: Into<Response<BoxBody>>, E: Into<Error>> From<Result<I, E>> for Response<BoxBody> {
fn from(res: Result<I, E>) -> Self {
match res {
Ok(val) => val.into(),

View File

@@ -47,7 +47,8 @@ impl ResponseBuilder {
/// Create response builder
///
/// # Examples
// /// use actix_http::{Response, ResponseBuilder, StatusCode};, / ``
/// ```
/// use actix_http::{Response, ResponseBuilder, StatusCode};
/// let res: Response<_> = ResponseBuilder::default().finish();
/// assert_eq!(res.status(), StatusCode::OK);
/// ```
@@ -62,7 +63,8 @@ impl ResponseBuilder {
/// Set HTTP status code of this response.
///
/// # Examples
// /// use actix_http::{ResponseBuilder, StatusCode};, / ``
/// ```
/// use actix_http::{ResponseBuilder, StatusCode};
/// let res = ResponseBuilder::default().status(StatusCode::NOT_FOUND).finish();
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
/// ```

View File

@@ -161,11 +161,7 @@ where
X::Error: Into<Response<BoxBody>>,
X::InitError: fmt::Debug,
U: ServiceFactory<
(Request, Framed<TcpStream, h1::Codec>),
Config = (),
Response = (),
>,
U: ServiceFactory<(Request, Framed<TcpStream, h1::Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: fmt::Display + Into<Response<BoxBody>>,
U::InitError: fmt::Debug,
@@ -381,9 +377,9 @@ where
let upgrade = match upgrade {
Some(upgrade) => {
let upgrade = upgrade.await.map_err(|e| {
log::error!("Init http upgrade service error: {:?}", e)
})?;
let upgrade = upgrade
.await
.map_err(|e| log::error!("Init http upgrade service error: {:?}", e))?;
Some(upgrade)
}
None => None,
@@ -507,8 +503,7 @@ where
&self,
(io, proto, peer_addr): (T, Protocol, Option<net::SocketAddr>),
) -> Self::Future {
let on_connect_data =
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
let conn_data = OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
match proto {
Protocol::Http2 => HttpServiceHandlerResponse {
@@ -517,7 +512,7 @@ where
h2::handshake_with_timeout(io, &self.cfg),
self.cfg.clone(),
self.flow.clone(),
on_connect_data,
conn_data,
peer_addr,
)),
},
@@ -527,10 +522,10 @@ where
state: State::H1 {
dispatcher: h1::Dispatcher::new(
io,
self.cfg.clone(),
self.flow.clone(),
on_connect_data,
self.cfg.clone(),
peer_addr,
conn_data,
),
},
},
@@ -627,17 +622,11 @@ where
StateProj::H2Handshake { handshake: data } => {
match ready!(Pin::new(&mut data.as_mut().unwrap().0).poll(cx)) {
Ok((conn, timer)) => {
let (_, config, flow, on_connect_data, peer_addr) =
data.take().unwrap();
let (_, config, flow, conn_data, peer_addr) = data.take().unwrap();
self.as_mut().project().state.set(State::H2 {
dispatcher: h2::Dispatcher::new(
flow,
conn,
on_connect_data,
config,
peer_addr,
timer,
conn, flow, config, peer_addr, conn_data, timer,
),
});
self.poll(cx)

View File

@@ -224,9 +224,7 @@ impl Decoder for Codec {
OpCode::Continue => {
if self.flags.contains(Flags::CONTINUATION) {
Ok(Some(Frame::Continuation(Item::Continue(
payload
.map(|pl| pl.freeze())
.unwrap_or_else(Bytes::new),
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
))))
} else {
Err(ProtocolError::ContinuationNotStarted)
@@ -236,9 +234,7 @@ impl Decoder for Codec {
if !self.flags.contains(Flags::CONTINUATION) {
self.flags.insert(Flags::CONTINUATION);
Ok(Some(Frame::Continuation(Item::FirstBinary(
payload
.map(|pl| pl.freeze())
.unwrap_or_else(Bytes::new),
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
))))
} else {
Err(ProtocolError::ContinuationStarted)
@@ -248,9 +244,7 @@ impl Decoder for Codec {
if !self.flags.contains(Flags::CONTINUATION) {
self.flags.insert(Flags::CONTINUATION);
Ok(Some(Frame::Continuation(Item::FirstText(
payload
.map(|pl| pl.freeze())
.unwrap_or_else(Bytes::new),
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
))))
} else {
Err(ProtocolError::ContinuationStarted)

View File

@@ -304,8 +304,7 @@ mod inner {
let item = match this.framed.next_item(cx) {
Poll::Ready(Some(Ok(el))) => el,
Poll::Ready(Some(Err(err))) => {
*this.state =
State::FramedError(DispatcherError::Decoder(err));
*this.state = State::FramedError(DispatcherError::Decoder(err));
return true;
}
Poll::Pending => return false,
@@ -348,8 +347,7 @@ mod inner {
match Pin::new(&mut this.rx).poll_next(cx) {
Poll::Ready(Some(Ok(Message::Item(msg)))) => {
if let Err(err) = this.framed.as_mut().write(msg) {
*this.state =
State::FramedError(DispatcherError::Encoder(err));
*this.state = State::FramedError(DispatcherError::Encoder(err));
return true;
}
}
@@ -371,8 +369,7 @@ mod inner {
Poll::Ready(Ok(_)) => {}
Poll::Ready(Err(err)) => {
debug!("Error sending data: {:?}", err);
*this.state =
State::FramedError(DispatcherError::Encoder(err));
*this.state = State::FramedError(DispatcherError::Encoder(err));
return true;
}
}
@@ -432,9 +429,7 @@ mod inner {
Poll::Ready(Ok(()))
}
}
State::FramedError(_) => {
Poll::Ready(Err(this.state.take_framed_error()))
}
State::FramedError(_) => Poll::Ready(Err(this.state.take_framed_error())),
State::Stopping => Poll::Ready(Ok(())),
};
}

View File

@@ -16,8 +16,7 @@ impl Parser {
src: &[u8],
server: bool,
max_size: usize,
) -> Result<Option<(usize, bool, OpCode, usize, Option<[u8; 4]>)>, ProtocolError>
{
) -> Result<Option<(usize, bool, OpCode, usize, Option<[u8; 4]>)>, ProtocolError> {
let chunk_len = src.len();
let mut idx = 2;
@@ -228,15 +227,11 @@ mod tests {
payload: Bytes,
}
fn is_none(
frm: &Result<Option<(bool, OpCode, Option<BytesMut>)>, ProtocolError>,
) -> bool {
fn is_none(frm: &Result<Option<(bool, OpCode, Option<BytesMut>)>, ProtocolError>) -> bool {
matches!(*frm, Ok(None))
}
fn extract(
frm: Result<Option<(bool, OpCode, Option<BytesMut>)>, ProtocolError>,
) -> F {
fn extract(frm: Result<Option<(bool, OpCode, Option<BytesMut>)>, ProtocolError>) -> F {
match frm {
Ok(Some((finished, opcode, payload))) => F {
finished,

View File

@@ -54,8 +54,8 @@ mod tests {
let mask = [0x6d, 0xb6, 0xb2, 0x80];
let unmasked = vec![
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17,
0x74, 0xf9, 0x12, 0x03,
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17, 0x74, 0xf9,
0x12, 0x03,
];
// Check masking with proper alignment.
@@ -85,8 +85,8 @@ mod tests {
fn test_apply_mask() {
let mask = [0x6d, 0xb6, 0xb2, 0x80];
let unmasked = vec![
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17,
0x74, 0xf9, 0x12, 0x03,
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17, 0x74, 0xf9,
0x12, 0x03,
];
for data_len in 0..=unmasked.len() {

View File

@@ -9,9 +9,7 @@ use derive_more::{Display, Error, From};
use http::{header, Method, StatusCode};
use crate::body::BoxBody;
use crate::{
header::HeaderValue, message::RequestHead, response::Response, ResponseBuilder,
};
use crate::{header::HeaderValue, message::RequestHead, response::Response, ResponseBuilder};
mod codec;
mod dispatcher;

View File

@@ -1,8 +1,6 @@
use std::convert::Infallible;
use actix_http::{
body::BoxBody, HttpMessage, HttpService, Request, Response, StatusCode,
};
use actix_http::{body::BoxBody, HttpMessage, HttpService, Request, Response, StatusCode};
use actix_http_test::test_server;
use actix_service::ServiceFactoryExt;
use actix_utils::future;

View File

@@ -8,7 +8,7 @@ use actix_http::{
body::{BodyStream, BoxBody, SizedStream},
error::PayloadError,
header::{self, HeaderValue},
Error, HttpMessage, HttpService, Method, Request, Response, StatusCode, Version,
Error, HttpService, Method, Request, Response, StatusCode, Version,
};
use actix_http_test::test_server;
use actix_service::{fn_service, ServiceFactoryExt};
@@ -170,10 +170,11 @@ async fn test_h2_headers() {
let mut srv = test_server(move || {
let data = data.clone();
HttpService::build().h2(move |_| {
let mut builder = Response::build(StatusCode::OK);
for idx in 0..90 {
builder.insert_header(
HttpService::build()
.h2(move |_| {
let mut builder = Response::build(StatusCode::OK);
for idx in 0..90 {
builder.insert_header(
(format!("X-TEST-{}", idx).as_str(),
"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
@@ -189,12 +190,13 @@ async fn test_h2_headers() {
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ",
));
}
ok::<_, Infallible>(builder.body(data.clone()))
})
}
ok::<_, Infallible>(builder.body(data.clone()))
})
.openssl(tls_config())
.map_err(|_| ())
}).await;
.map_err(|_| ())
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -315,9 +317,8 @@ async fn test_h2_body_length() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| async {
let body = once(async {
Ok::<_, Infallible>(Bytes::from_static(STR.as_ref()))
});
let body =
once(async { Ok::<_, Infallible>(Bytes::from_static(STR.as_ref())) });
Ok::<_, Infallible>(
Response::ok().set_body(SizedStream::new(STR.len() as u64, body)),
@@ -430,7 +431,7 @@ async fn test_h2_on_connect() {
data.insert(20isize);
})
.h2(|req: Request| {
assert!(req.extensions().contains::<isize>());
assert!(req.conn_data::<isize>().is_some());
ok::<_, Infallible>(Response::ok())
})
.openssl(tls_config())

View File

@@ -238,10 +238,11 @@ async fn test_h2_headers() {
let mut srv = test_server(move || {
let data = data.clone();
HttpService::build().h2(move |_| {
let mut config = Response::build(StatusCode::OK);
for idx in 0..90 {
config.insert_header((
HttpService::build()
.h2(move |_| {
let mut config = Response::build(StatusCode::OK);
for idx in 0..90 {
config.insert_header((
format!("X-TEST-{}", idx).as_str(),
"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
@@ -257,11 +258,12 @@ async fn test_h2_headers() {
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ",
));
}
ok::<_, Infallible>(config.body(data.clone()))
})
}
ok::<_, Infallible>(config.body(data.clone()))
})
.rustls(tls_config())
}).await;
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());

View File

@@ -7,7 +7,7 @@ use std::{
use actix_http::{
body::{self, BodyStream, BoxBody, SizedStream},
header, Error, HttpMessage, HttpService, KeepAlive, Request, Response, StatusCode,
header, Error, HttpService, KeepAlive, Request, Response, StatusCode,
};
use actix_http_test::test_server;
use actix_rt::time::sleep;
@@ -154,9 +154,7 @@ async fn test_chunked_payload() {
})
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
.map(|req_size| {
Ok::<_, Error>(
Response::ok().set_body(format!("size={}", req_size)),
)
Ok::<_, Error>(Response::ok().set_body(format!("size={}", req_size)))
})
}))
.tcp()
@@ -165,8 +163,7 @@ async fn test_chunked_payload() {
let returned_size = {
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream
.write_all(b"POST /test HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n");
let _ = stream.write_all(b"POST /test HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n");
for chunk_size in chunk_sizes.iter() {
let mut bytes = Vec::new();
@@ -293,8 +290,7 @@ async fn test_http1_keepalive_close() {
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ =
stream.write_all(b"GET /test/tests/test HTTP/1.1\r\nconnection: close\r\n\r\n");
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\nconnection: close\r\n\r\n");
let mut data = vec![0; 1024];
let _ = stream.read(&mut data);
assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n");
@@ -338,8 +334,8 @@ async fn test_http10_keepalive() {
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream
.write_all(b"GET /test/tests/test HTTP/1.0\r\nconnection: keep-alive\r\n\r\n");
let _ =
stream.write_all(b"GET /test/tests/test HTTP/1.0\r\nconnection: keep-alive\r\n\r\n");
let mut data = vec![0; 1024];
let _ = stream.read(&mut data);
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
@@ -436,10 +432,11 @@ async fn test_h1_headers() {
let mut srv = test_server(move || {
let data = data.clone();
HttpService::build().h1(move |_| {
let mut builder = Response::build(StatusCode::OK);
for idx in 0..90 {
builder.insert_header((
HttpService::build()
.h1(move |_| {
let mut builder = Response::build(StatusCode::OK);
for idx in 0..90 {
builder.insert_header((
format!("X-TEST-{}", idx).as_str(),
"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
@@ -455,10 +452,12 @@ async fn test_h1_headers() {
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ",
));
}
ok::<_, Infallible>(builder.body(data.clone()))
}).tcp()
}).await;
}
ok::<_, Infallible>(builder.body(data.clone()))
})
.tcp()
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -655,9 +654,7 @@ async fn test_h1_body_chunked_implicit() {
HttpService::build()
.h1(|_| {
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
ok::<_, Infallible>(
Response::build(StatusCode::OK).body(BodyStream::new(body)),
)
ok::<_, Infallible>(Response::build(StatusCode::OK).body(BodyStream::new(body)))
})
.tcp()
})
@@ -748,7 +745,7 @@ async fn test_h1_on_connect() {
data.insert(20isize);
})
.h1(|req: Request| {
assert!(req.extensions().contains::<isize>());
assert!(req.conn_data::<isize>().is_some());
ok::<_, Infallible>(Response::ok())
})
.tcp()
@@ -776,10 +773,8 @@ async fn test_not_modified_spec_h1() {
.h1(|req: Request| {
let res: Response<BoxBody> = match req.path() {
// with no content-length
"/none" => {
Response::with_body(StatusCode::NOT_MODIFIED, body::None::new())
.map_into_boxed_body()
}
"/none" => Response::with_body(StatusCode::NOT_MODIFIED, body::None::new())
.map_into_boxed_body(),
// with no content-length
"/body" => Response::with_body(StatusCode::NOT_MODIFIED, "1234")
@@ -787,10 +782,8 @@ async fn test_not_modified_spec_h1() {
// with manual content-length header and specific None body
"/cl-none" => {
let mut res = Response::with_body(
StatusCode::NOT_MODIFIED,
body::None::new(),
);
let mut res =
Response::with_body(StatusCode::NOT_MODIFIED, body::None::new());
res.headers_mut()
.insert(CL.clone(), header::HeaderValue::from_static("24"));
res.map_into_boxed_body()
@@ -798,8 +791,7 @@ async fn test_not_modified_spec_h1() {
// with manual content-length header and ignore-able body
"/cl-body" => {
let mut res =
Response::with_body(StatusCode::NOT_MODIFIED, "1234");
let mut res = Response::with_body(StatusCode::NOT_MODIFIED, "1234");
res.headers_mut()
.insert(CL.clone(), header::HeaderValue::from_static("4"));
res.map_into_boxed_body()

View File

@@ -56,8 +56,9 @@ impl From<WsServiceError> for Response<BoxBody> {
WsServiceError::Http(err) => err.into(),
WsServiceError::Ws(err) => err.into(),
WsServiceError::Io(_err) => unreachable!(),
WsServiceError::Dispatcher => Response::internal_server_error()
.set_body(BoxBody::new(format!("{}", err))),
WsServiceError::Dispatcher => {
Response::internal_server_error().set_body(BoxBody::new(format!("{}", err)))
}
}
}
}
@@ -97,9 +98,7 @@ where
async fn service(msg: Frame) -> Result<Message, Error> {
let msg = match msg {
Frame::Ping(msg) => Message::Pong(msg),
Frame::Text(text) => {
Message::Text(String::from_utf8_lossy(&text).into_owned().into())
}
Frame::Text(text) => Message::Text(String::from_utf8_lossy(&text).into_owned().into()),
Frame::Binary(bin) => Message::Binary(bin),
Frame::Continuation(item) => Message::Continuation(item),
Frame::Close(reason) => Message::Close(reason),

View File

@@ -1,6 +1,7 @@
//! Multipart form support for Actix Web.
#![deny(rust_2018_idioms)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![allow(clippy::borrow_interior_mutable_const)]
mod error;

View File

@@ -1,6 +1,7 @@
//! Resource path matching and router.
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]

View File

@@ -168,7 +168,7 @@ const REGEX_FLAGS: &str = "(?s-m)";
/// extracted in the same way as non-tail dynamic segments.
///
/// ## Examples
/// ```rust
/// ```
/// # use actix_router::{Path, ResourceDef};
/// let resource = ResourceDef::new("/blob/{tail}*");
/// assert!(resource.is_match("/blob/HEAD/Cargo.toml"));
@@ -191,7 +191,7 @@ const REGEX_FLAGS: &str = "(?s-m)";
/// expectations in the router using these definitions and cause runtime panics.
///
/// ## Examples
/// ```rust
/// ```
/// # use actix_router::ResourceDef;
/// let resource = ResourceDef::new(["/home", "/index"]);
/// assert!(resource.is_match("/home"));
@@ -206,7 +206,7 @@ const REGEX_FLAGS: &str = "(?s-m)";
/// resource-path pairs that would not be compatible.
///
/// ## Examples
/// ```rust
/// ```
/// # use actix_router::ResourceDef;
/// assert!(!ResourceDef::new("/root").is_match("/root/"));
/// assert!(!ResourceDef::new("/root/").is_match("/root"));

View File

@@ -26,6 +26,9 @@
//! }
//! ```
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#[cfg(feature = "openssl")]
extern crate tls_openssl as openssl;
#[cfg(feature = "rustls")]
@@ -160,9 +163,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -176,9 +181,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -192,9 +199,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -211,9 +220,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -227,9 +238,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -243,9 +256,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -262,9 +277,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -278,9 +295,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)
@@ -294,9 +313,11 @@ where
local_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> =
err.into().error_response().into();
res.map_into_boxed_body()
});
HttpService::build()
.client_timeout(timeout)

View File

@@ -1,7 +1,7 @@
//! Actix actors support for Actix Web.
#![deny(rust_2018_idioms)]
#![allow(clippy::borrow_interior_mutable_const)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
mod context;
pub mod ws;

View File

@@ -57,6 +57,8 @@
//! [DELETE]: macro@delete
#![recursion_limit = "512"]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
use proc_macro::TokenStream;
use quote::quote;

View File

@@ -1,7 +1,4 @@
extern crate proc_macro;
use std::collections::HashSet;
use std::convert::TryFrom;
use std::{collections::HashSet, convert::TryFrom};
use actix_router::ResourceDef;
use proc_macro::TokenStream;

View File

@@ -95,7 +95,8 @@
//! # }
//! ```
#![deny(rust_2018_idioms)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![allow(
clippy::type_complexity,
clippy::borrow_interior_mutable_const,

View File

@@ -6,8 +6,10 @@
use std::{any::Any, io, net::SocketAddr};
use actix_http::CloneableExtensions;
use actix_web::{rt::net::TcpStream, web, App, HttpServer};
use actix_web::{
dev::Extensions, rt::net::TcpStream, web, App, HttpRequest, HttpResponse, HttpServer,
Responder,
};
#[allow(dead_code)]
#[derive(Debug, Clone)]
@@ -17,14 +19,19 @@ struct ConnectionInfo {
ttl: Option<u32>,
}
async fn route_whoami(conn_info: web::ReqData<ConnectionInfo>) -> String {
format!(
"Here is some info about your connection:\n\n{:#?}",
conn_info
)
async fn route_whoami(req: HttpRequest) -> impl Responder {
match req.conn_data::<ConnectionInfo>() {
Some(info) => HttpResponse::Ok().body(format!(
"Here is some info about your connection:\n\n{:#?}",
info
)),
None => {
HttpResponse::InternalServerError().body("Missing expected request extension data")
}
}
}
fn get_conn_info(connection: &dyn Any, data: &mut CloneableExtensions) {
fn get_conn_info(connection: &dyn Any, data: &mut Extensions) {
if let Some(sock) = connection.downcast_ref::<TcpStream>() {
data.insert(ConnectionInfo {
bind: sock.local_addr().unwrap(),
@@ -40,9 +47,12 @@ fn get_conn_info(connection: &dyn Any, data: &mut CloneableExtensions) {
async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let bind = ("127.0.0.1", 8080);
log::info!("staring server at http://{}:{}", &bind.0, &bind.1);
HttpServer::new(|| App::new().default_service(web::to(route_whoami)))
.on_connect(get_conn_info)
.bind(("127.0.0.1", 8080))?
.bind(bind)?
.workers(1)
.run()
.await

146
src/any_body.rs Normal file
View File

@@ -0,0 +1,146 @@
use std::{
error::Error as StdError,
mem,
pin::Pin,
task::{Context, Poll},
};
use actix_http::body::{BodySize, BoxBody, MessageBody};
use bytes::Bytes;
use pin_project_lite::pin_project;
use crate::Error;
pin_project! {
#[derive(Debug)]
#[project = AnyBodyProj]
pub enum AnyBody<B = BoxBody> {
None,
Full { body: Bytes },
Stream { #[pin] body: B },
Boxed { body: BoxBody },
}
}
impl<B: MessageBody + 'static> AnyBody<B> {
pub fn into_body<B1>(self) -> AnyBody<B1> {
match self {
AnyBody::None => AnyBody::None,
AnyBody::Full { body } => AnyBody::Full { body },
AnyBody::Stream { body } => AnyBody::Boxed {
body: BoxBody::new(body),
},
AnyBody::Boxed { body } => AnyBody::Boxed { body },
}
}
}
impl<B> Default for AnyBody<B> {
fn default() -> Self {
Self::Full { body: Bytes::new() }
}
}
impl<B> MessageBody for AnyBody<B>
where
B: MessageBody,
B::Error: 'static,
{
type Error = Box<dyn StdError>;
fn size(&self) -> BodySize {
match self {
Self::None => BodySize::None,
Self::Full { body } => body.size(),
Self::Stream { body } => body.size(),
Self::Boxed { body } => body.size(),
}
}
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> {
match self.project() {
AnyBodyProj::None => Poll::Ready(None),
AnyBodyProj::Full { body } => {
let bytes = mem::take(body);
Poll::Ready(Some(Ok(bytes)))
}
AnyBodyProj::Stream { body } => body.poll_next(cx).map_err(|err| err.into()),
AnyBodyProj::Boxed { body } => body.as_pin_mut().poll_next(cx),
}
}
}
pin_project! {
#[project = EitherAnyBodyProj]
#[derive(Debug)]
pub enum EitherAnyBody<L, R = BoxBody> {
/// A body of type `L`.
Left { #[pin] body: AnyBody<L> },
/// A body of type `R`.
Right { #[pin] body: AnyBody<R> },
}
}
// impl<L> EitherAnyBody<L, BoxBody> {
// /// Creates new `EitherBody` using left variant and boxed right variant.
// pub fn new(body: L) -> Self {
// Self::Left {
// body: AnyBody::Stream { body },
// }
// }
// }
// impl<L, R> EitherAnyBody<L, R> {
// /// Creates new `EitherBody` using left variant.
// pub fn left(body: L) -> Self {
// Self::Left {
// body: AnyBody::Stream { body },
// }
// }
// /// Creates new `EitherBody` using right variant.
// pub fn right(body: R) -> Self {
// Self::Right {
// body: AnyBody::Stream { body },
// }
// }
// }
impl<L, R> MessageBody for EitherAnyBody<L, R>
where
L: MessageBody + 'static,
R: MessageBody + 'static,
{
type Error = Error;
fn size(&self) -> BodySize {
match self {
Self::Left { body } => body.size(),
Self::Right { body } => body.size(),
}
}
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> {
match self.project() {
EitherAnyBodyProj::Left { body } => body.poll_next(cx).map_err(Error::from),
EitherAnyBodyProj::Right { body } => body.poll_next(cx).map_err(Error::from),
}
}
}
#[cfg(test)]
mod tests {
use static_assertions::assert_eq_size;
use super::*;
assert_eq_size!(AnyBody<()>, [u8; 40]);
assert_eq_size!(AnyBody<u64>, [u8; 40]); // how is this the same size as ()
}

View File

@@ -1,9 +1,6 @@
use std::{cell::RefCell, fmt, future::Future, marker::PhantomData, rc::Rc};
use std::{cell::RefCell, fmt, future::Future, rc::Rc};
use actix_http::{
body::{BoxBody, MessageBody},
Extensions, Request,
};
use actix_http::{body::MessageBody, Extensions, Request};
use actix_service::{
apply, apply_fn_factory, boxed, IntoServiceFactory, ServiceFactory, ServiceFactoryExt,
Transform,
@@ -26,7 +23,7 @@ use crate::{
/// Application builder - structure that follows the builder pattern
/// for building application instances.
pub struct App<T, B> {
pub struct App<T> {
endpoint: T,
services: Vec<Box<dyn AppServiceFactory>>,
default: Option<Rc<BoxedHttpServiceFactory>>,
@@ -34,10 +31,9 @@ pub struct App<T, B> {
data_factories: Vec<FnDataFactory>,
external: Vec<ResourceDef>,
extensions: Extensions,
_phantom: PhantomData<B>,
}
impl App<AppEntry, BoxBody> {
impl App<AppEntry> {
/// Create application builder. Application can be configured with a builder-like pattern.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
@@ -51,22 +47,11 @@ impl App<AppEntry, BoxBody> {
factory_ref,
external: Vec::new(),
extensions: Extensions::new(),
_phantom: PhantomData,
}
}
}
impl<T, B> App<T, B>
where
B: MessageBody,
T: ServiceFactory<
ServiceRequest,
Config = (),
Response = ServiceResponse<B>,
Error = Error,
InitError = (),
>,
{
impl<T> App<T> {
/// Set application (root level) data.
///
/// Application data stored with `App::app_data()` method is available through the
@@ -365,7 +350,7 @@ where
/// .route("/index.html", web::get().to(index));
/// }
/// ```
pub fn wrap<M, B1>(
pub fn wrap<M, B, B1>(
self,
mw: M,
) -> App<
@@ -376,9 +361,16 @@ where
Error = Error,
InitError = (),
>,
B1,
>
where
T: ServiceFactory<
ServiceRequest,
Response = ServiceResponse<B>,
Error = Error,
Config = (),
InitError = (),
>,
B: MessageBody,
M: Transform<
T::Service,
ServiceRequest,
@@ -396,7 +388,6 @@ where
factory_ref: self.factory_ref,
external: self.external,
extensions: self.extensions,
_phantom: PhantomData,
}
}
@@ -431,7 +422,7 @@ where
/// .route("/index.html", web::get().to(index));
/// }
/// ```
pub fn wrap_fn<B1, F, R>(
pub fn wrap_fn<F, R, B, B1>(
self,
mw: F,
) -> App<
@@ -442,12 +433,19 @@ where
Error = Error,
InitError = (),
>,
B1,
>
where
B1: MessageBody,
T: ServiceFactory<
ServiceRequest,
Response = ServiceResponse<B>,
Error = Error,
Config = (),
InitError = (),
>,
B: MessageBody,
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
R: Future<Output = Result<ServiceResponse<B1>, Error>>,
B1: MessageBody,
{
App {
endpoint: apply_fn_factory(self.endpoint, mw),
@@ -457,12 +455,11 @@ where
factory_ref: self.factory_ref,
external: self.external,
extensions: self.extensions,
_phantom: PhantomData,
}
}
}
impl<T, B> IntoServiceFactory<AppInit<T, B>, Request> for App<T, B>
impl<T, B> IntoServiceFactory<AppInit<T, B>, Request> for App<T>
where
B: MessageBody,
T: ServiceFactory<

View File

@@ -197,7 +197,9 @@ where
actix_service::forward_ready!(service);
fn call(&self, req: Request) -> Self::Future {
fn call(&self, mut req: Request) -> Self::Future {
let req_data = Rc::new(RefCell::new(req.take_req_data()));
let conn_data = req.take_conn_data();
let (head, payload) = req.into_parts();
let req = if let Some(mut req) = self.app_state.pool().pop() {
@@ -205,6 +207,8 @@ where
inner.path.get_mut().update(&head.uri);
inner.path.reset();
inner.head = head;
inner.conn_data = conn_data;
inner.req_data = req_data;
req
} else {
HttpRequest::new(
@@ -212,6 +216,8 @@ where
head,
self.app_state.clone(),
self.app_data.clone(),
conn_data,
req_data,
)
};
self.service.call(ServiceRequest::new(req, payload))

View File

@@ -31,41 +31,53 @@ pub(crate) type FnDataFactory =
/// server constructs an application instance for each thread, thus application data must be
/// constructed multiple times. If you want to share data between different threads, a shareable
/// object should be used, e.g. `Send + Sync`. Application data does not need to be `Send`
/// or `Sync`. Internally `Data` uses `Arc`.
/// or `Sync`. Internally `Data` contains an `Arc`.
///
/// If route data is not set for a handler, using `Data<T>` extractor would cause *Internal
/// Server Error* response.
/// If route data is not set for a handler, using `Data<T>` extractor would cause a `500 Internal
/// Server Error` response.
///
// TODO: document `dyn T` functionality through converting an Arc
// TODO: note equivalence of req.app_data<Data<T>> and Data<T> extractor
// TODO: note that data must be inserted using Data<T> in order to extract it
/// # Unsized Data
/// For types that are unsized, most commonly `dyn T`, `Data` can wrap these types by first
/// constructing an `Arc<dyn T>` and using the `From` implementation to convert it.
///
/// ```
/// # use std::{fmt::Display, sync::Arc};
/// # use actix_web::web::Data;
/// let displayable_arc: Arc<dyn Display> = Arc::new(42usize);
/// let displayable_data: Data<dyn Display> = Data::from(displayable_arc);
/// ```
///
/// # Examples
/// ```
/// use std::sync::Mutex;
/// use actix_web::{web, App, HttpResponse, Responder};
/// use actix_web::{App, HttpRequest, HttpResponse, Responder, web::{self, Data}};
///
/// struct MyData {
/// counter: usize,
/// }
///
/// /// Use the `Data<T>` extractor to access data in a handler.
/// async fn index(data: web::Data<Mutex<MyData>>) -> impl Responder {
/// let mut data = data.lock().unwrap();
/// data.counter += 1;
/// async fn index(data: Data<Mutex<MyData>>) -> impl Responder {
/// let mut my_data = data.lock().unwrap();
/// my_data.counter += 1;
/// HttpResponse::Ok()
/// }
///
/// fn main() {
/// let data = web::Data::new(Mutex::new(MyData{ counter: 0 }));
///
/// let app = App::new()
/// // Store `MyData` in application storage.
/// .app_data(data.clone())
/// .service(
/// web::resource("/index.html").route(
/// web::get().to(index)));
/// /// Alteratively, use the `HttpRequest::app_data` method to access data in a handler.
/// async fn index_alt(req: HttpRequest) -> impl Responder {
/// let data = req.app_data::<Data<Mutex<MyData>>>().unwrap();
/// let mut my_data = data.lock().unwrap();
/// my_data.counter += 1;
/// HttpResponse::Ok()
/// }
///
/// let data = Data::new(Mutex::new(MyData { counter: 0 }));
///
/// let app = App::new()
/// // Store `MyData` in application storage.
/// .app_data(Data::clone(&data))
/// .route("/index.html", web::get().to(index))
/// .route("/index-alt.html", web::get().to(index_alt));
/// ```
#[derive(Debug)]
pub struct Data<T: ?Sized>(Arc<T>);

View File

@@ -14,10 +14,7 @@ pub use crate::types::form::UrlEncoded;
pub use crate::types::json::JsonBody;
pub use crate::types::readlines::Readlines;
pub use actix_http::{
CloneableExtensions, Extensions, Payload, PayloadStream, RequestHead, Response,
ResponseHead,
};
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, Response, ResponseHead};
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
pub use actix_server::{Server, ServerHandle};
pub use actix_service::{
@@ -105,41 +102,3 @@ impl<B> BodyEncoding for crate::HttpResponse<B> {
self
}
}
// TODO: remove this if it doesn't appear to be needed
#[allow(dead_code)]
#[derive(Debug)]
pub(crate) enum AnyBody {
None,
Full { body: crate::web::Bytes },
Boxed { body: actix_http::body::BoxBody },
}
impl crate::body::MessageBody for AnyBody {
type Error = crate::BoxError;
/// Body size hint.
fn size(&self) -> crate::body::BodySize {
match self {
AnyBody::None => crate::body::BodySize::None,
AnyBody::Full { body } => body.size(),
AnyBody::Boxed { body } => body.size(),
}
}
/// Attempt to pull out the next chunk of body bytes.
fn poll_next(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Result<crate::web::Bytes, Self::Error>>> {
match self.get_mut() {
AnyBody::None => std::task::Poll::Ready(None),
AnyBody::Full { body } => {
let bytes = std::mem::take(body);
std::task::Poll::Ready(Some(Ok(bytes)))
}
AnyBody::Boxed { body } => body.as_pin_mut().poll_next(cx),
}
}
}

View File

@@ -2,7 +2,7 @@ use std::{error::Error as StdError, fmt};
use actix_http::{body::BoxBody, Response};
use crate::{HttpResponse, ResponseError};
use crate::{any_body::AnyBody, HttpResponse, ResponseError};
/// General purpose actix web error.
///
@@ -69,8 +69,15 @@ impl<T: ResponseError + 'static> From<T> for Error {
}
}
impl From<Error> for Response<BoxBody> {
fn from(err: Error) -> Response<BoxBody> {
impl From<Error> for Response<AnyBody<BoxBody>> {
fn from(err: Error) -> Self {
err.error_response().into()
}
}
impl From<Error> for actix_http::Response<BoxBody> {
fn from(err: Error) -> Self {
let res: actix_http::Response<_> = err.error_response().into();
res.map_into_boxed_body()
}
}

View File

@@ -7,7 +7,7 @@ use actix_http::{
};
use bytes::{BufMut as _, BytesMut};
use crate::{Error, HttpRequest, HttpResponse, Responder, ResponseError};
use crate::{any_body::AnyBody, Error, HttpRequest, HttpResponse, Responder, ResponseError};
/// Wraps errors to alter the generated response status code.
///
@@ -91,7 +91,9 @@ where
let mime = mime::TEXT_PLAIN_UTF_8.try_into_value().unwrap();
res.headers_mut().insert(header::CONTENT_TYPE, mime);
res.set_body(BoxBody::new(buf.into_inner()))
res.set_body(AnyBody::Full {
body: buf.into_inner().freeze(),
})
}
InternalErrorType::Response(ref resp) => {
@@ -128,7 +130,7 @@ macro_rules! error_helper {
InternalError::new(err, StatusCode::$status).into()
}
}
}
};
}
error_helper!(ErrorBadRequest, BAD_REQUEST);

View File

@@ -1,8 +1,9 @@
//! Error and Result module
/// This is meant to be a glob import of the whole error module, but rustdoc can't handle
/// shadowing `Error` type, so it is expanded manually.
/// See https://github.com/rust-lang/rust/issues/83375
// This is meant to be a glob import of the whole error module except for `Error`. Rustdoc can't yet
// correctly resolve the conflicting `Error` type defined in this module, so these re-exports are
// expanded manually.
//
// See <https://github.com/rust-lang/rust/issues/83375>
pub use actix_http::error::{
BlockingError, ContentTypeError, DispatchError, HttpError, ParseError, PayloadError,
};

View File

@@ -14,6 +14,7 @@ use actix_http::{
use bytes::BytesMut;
use crate::{
any_body::AnyBody,
error::{downcast_dyn, downcast_get_type_id},
helpers, HttpResponse,
};
@@ -33,7 +34,7 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
///
/// By default, the generated response uses a 500 Internal Server Error status code, a
/// `Content-Type` of `text/plain`, and the body is set to `Self`'s `Display` impl.
fn error_response(&self) -> HttpResponse<BoxBody> {
fn error_response(&self) -> HttpResponse {
let mut res = HttpResponse::new(self.status_code());
let mut buf = BytesMut::new();
@@ -42,7 +43,7 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
let mime = mime::TEXT_PLAIN_UTF_8.try_into_value().unwrap();
res.headers_mut().insert(header::CONTENT_TYPE, mime);
res.set_body(BoxBody::new(buf))
res.set_body(AnyBody::Full { body: buf.freeze() })
}
downcast_get_type_id!();
@@ -50,7 +51,7 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
downcast_dyn!(ResponseError);
impl ResponseError for Box<dyn StdError + 'static> {}
impl ResponseError for Box<dyn StdError> {}
#[cfg(feature = "openssl")]
impl ResponseError for actix_tls::accept::openssl::reexports::Error {}
@@ -128,7 +129,15 @@ impl ResponseError for actix_http::error::ContentTypeError {
impl ResponseError for actix_http::ws::HandshakeError {
fn error_response(&self) -> HttpResponse<BoxBody> {
Response::from(self).map_into_boxed_body().into()
Response::from(self)
.map_body(|_, body| AnyBody::Boxed { body })
.into()
}
}
impl ResponseError for actix_http::error::EncoderError {
fn error_response(&self) -> HttpResponse {
todo!("")
}
}

View File

@@ -1,4 +1,4 @@
use std::{cell::Ref, convert::Infallible, net::SocketAddr};
use std::{convert::Infallible, net::SocketAddr};
use actix_utils::future::{err, ok, Ready};
use derive_more::{Display, Error};
@@ -72,15 +72,7 @@ pub struct ConnectionInfo {
}
impl ConnectionInfo {
/// Create *ConnectionInfo* instance for a request.
pub fn get<'a>(req: &'a RequestHead, cfg: &AppConfig) -> Ref<'a, Self> {
if !req.extensions().contains::<ConnectionInfo>() {
req.extensions_mut().insert(ConnectionInfo::new(req, cfg));
}
Ref::map(req.extensions(), |e| e.get().unwrap())
}
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
pub(crate) fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
let mut host = None;
let mut scheme = None;
let mut realip_remote_addr = None;

View File

@@ -65,10 +65,12 @@
//! * `secure-cookies` - secure cookies support
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![allow(clippy::needless_doctest_main, clippy::type_complexity)]
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
mod any_body;
mod app;
mod app_service;
mod config;

View File

@@ -154,7 +154,7 @@ mod tests {
let srv = init_service(
App::new().service(
web::scope("app")
.wrap(Compat::new(logger))
.wrap(logger)
.wrap(Compat::new(compress))
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok))),
),

View File

@@ -10,7 +10,7 @@ use std::{
};
use actix_http::{
body::{EitherBody, MessageBody},
body::MessageBody,
encoding::Encoder,
header::{ContentEncoding, ACCEPT_ENCODING},
StatusCode,
@@ -22,6 +22,7 @@ use once_cell::sync::Lazy;
use pin_project_lite::pin_project;
use crate::{
any_body::AnyBody,
dev::BodyEncoding,
service::{ServiceRequest, ServiceResponse},
Error, HttpResponse,
@@ -61,7 +62,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Response = ServiceResponse<EitherBody<Encoder<B>>>;
type Response = ServiceResponse<Encoder<AnyBody<B>>>;
type Error = Error;
type Transform = CompressMiddleware<S>;
type InitError = ();
@@ -111,7 +112,7 @@ where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
{
type Response = ServiceResponse<EitherBody<Encoder<B>>>;
type Response = ServiceResponse<Encoder<AnyBody<B>>>;
type Error = Error;
type Future = Either<CompressResponse<S, B>, Ready<Result<Self::Response, Self::Error>>>;
@@ -146,12 +147,15 @@ where
let res = HttpResponse::with_body(
StatusCode::NOT_ACCEPTABLE,
SUPPORTED_ALGORITHM_NAMES.clone(),
);
)
.map_body(|_, body| match body {
AnyBody::Full { body } => AnyBody::Stream {
body: Encoder::not_acceptable(body),
},
_ => unreachable!("probably"),
});
Either::right(ok(req
.into_response(res)
.map_into_boxed_body()
.map_into_right_body()))
Either::right(ok(req.into_response(res)))
}
}
}
@@ -174,7 +178,7 @@ where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
{
type Output = Result<ServiceResponse<EitherBody<Encoder<B>>>, Error>;
type Output = Result<ServiceResponse<Encoder<AnyBody<B>>>, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@@ -187,8 +191,8 @@ where
*this.encoding
};
Poll::Ready(Ok(resp.map_body(move |head, body| {
EitherBody::left(Encoder::response(enc, head, body))
Poll::Ready(Ok(resp.map_body(move |head, body| AnyBody::Stream {
body: Encoder::response(enc, head, body),
})))
}

View File

@@ -22,6 +22,7 @@ use regex::{Regex, RegexSet};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use crate::{
any_body::AnyBody,
body::{BodySize, MessageBody},
http::header::HeaderName,
service::{ServiceRequest, ServiceResponse},
@@ -175,7 +176,7 @@ impl Default for Logger {
}
}
impl<S, B> Transform<S, ServiceRequest> for Logger
impl<S, B: 'static> Transform<S, ServiceRequest> for Logger
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
@@ -210,7 +211,7 @@ pub struct LoggerMiddleware<S> {
service: S,
}
impl<S, B> Service<ServiceRequest> for LoggerMiddleware<S>
impl<S, B: 'static> Service<ServiceRequest> for LoggerMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
@@ -262,7 +263,7 @@ pin_project! {
}
}
impl<S, B> Future for LoggerResponse<S, B>
impl<S, B: 'static> Future for LoggerResponse<S, B>
where
B: MessageBody,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
@@ -290,11 +291,13 @@ where
let time = *this.time;
let format = this.format.take();
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
body,
time,
format,
size: 0,
Poll::Ready(Ok(res.map_body(move |_, body| AnyBody::Stream {
body: StreamLog {
body: body.into_body(),
time,
format,
size: 0,
},
})))
}
}
@@ -302,7 +305,7 @@ where
pin_project! {
pub struct StreamLog<B> {
#[pin]
body: B,
body: AnyBody<B>,
format: Option<Format>,
size: usize,
time: OffsetDateTime,

View File

@@ -37,6 +37,8 @@ pub(crate) struct HttpRequestInner {
pub(crate) head: Message<RequestHead>,
pub(crate) path: Path<Url>,
pub(crate) app_data: SmallVec<[Rc<Extensions>; 4]>,
pub(crate) conn_data: Option<Rc<Extensions>>,
pub(crate) req_data: Rc<RefCell<Extensions>>,
app_state: Rc<AppInitServiceState>,
}
@@ -47,6 +49,8 @@ impl HttpRequest {
head: Message<RequestHead>,
app_state: Rc<AppInitServiceState>,
app_data: Rc<Extensions>,
conn_data: Option<Rc<Extensions>>,
req_data: Rc<RefCell<Extensions>>,
) -> HttpRequest {
let mut data = SmallVec::<[Rc<Extensions>; 4]>::new();
data.push(app_data);
@@ -57,6 +61,8 @@ impl HttpRequest {
path,
app_state,
app_data: data,
conn_data,
req_data,
}),
}
}
@@ -153,16 +159,26 @@ impl HttpRequest {
self.resource_map().match_name(self.path())
}
/// Request extensions
#[inline]
pub fn extensions(&self) -> Ref<'_, Extensions> {
self.head().extensions()
pub fn req_data(&self) -> Ref<'_, Extensions> {
self.inner.req_data.borrow()
}
/// Mutable reference to a the request's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<'_, Extensions> {
self.head().extensions_mut()
pub fn req_data_mut(&self) -> RefMut<'_, Extensions> {
self.inner.req_data.borrow_mut()
}
/// Returns a reference a piece of connection data set in an [on-connect] callback.
///
/// ```ignore
/// let opt_t = req.conn_data::<PeerCertificate>();
/// ```
///
/// [on-connect]: crate::HttpServer::on_connect
pub fn conn_data<T: 'static>(&self) -> Option<&T> {
self.inner
.conn_data
.as_deref()
.and_then(|container| container.get::<T>())
}
/// Generates URL for a named resource.
@@ -231,7 +247,12 @@ impl HttpRequest {
/// borrowed.
#[inline]
pub fn connection_info(&self) -> Ref<'_, ConnectionInfo> {
ConnectionInfo::get(self.head(), self.app_config())
if !self.extensions().contains::<ConnectionInfo>() {
let info = ConnectionInfo::new(self.head(), &*self.app_config());
self.extensions_mut().insert(info);
}
Ref::map(self.extensions(), |e| e.get().unwrap())
}
/// App config
@@ -304,21 +325,18 @@ impl HttpMessage for HttpRequest {
type Stream = ();
#[inline]
/// Returns Request's headers.
fn headers(&self) -> &HeaderMap {
&self.head().headers
}
/// Request extensions
#[inline]
fn extensions(&self) -> Ref<'_, Extensions> {
self.inner.head.extensions()
self.req_data()
}
/// Mutable reference to a the request's extensions
#[inline]
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
self.inner.head.extensions_mut()
self.req_data_mut()
}
#[inline]
@@ -331,14 +349,15 @@ impl Drop for HttpRequest {
fn drop(&mut self) {
// if possible, contribute to current worker's HttpRequest allocation pool
// This relies on no Weak<HttpRequestInner> exists anywhere.(There is none)
// This relies on no Weak<HttpRequestInner> exists anywhere. (There is none.)
if let Some(inner) = Rc::get_mut(&mut self.inner) {
if inner.app_state.pool().is_available() {
// clear additional app_data and keep the root one for reuse.
inner.app_data.truncate(1);
// inner is borrowed mut here. get head's Extension mutably
// to reduce borrow check
inner.head.extensions.get_mut().clear();
// Inner is borrowed mut here and; get req data mutably to reduce borrow check. Also
// we know the req_data Rc will not have any cloned at this point to unwrap is okay.
Rc::get_mut(&mut inner.req_data).unwrap().get_mut().clear();
// a re-borrow of pool is necessary here.
let req = self.inner.clone();

View File

@@ -33,12 +33,11 @@ use crate::{dev::Payload, error::ErrorInternalServerError, Error, FromRequest, H
/// req: HttpRequest,
/// opt_flag: Option<web::ReqData<FlagFromMiddleware>>,
/// ) -> impl Responder {
/// // use an optional extractor if the middleware is
/// // not guaranteed to add this type of requests data
/// // use an option extractor if middleware is not guaranteed to add this type of req data
/// if let Some(flag) = opt_flag {
/// assert_eq!(&flag.into_inner(), req.extensions().get::<FlagFromMiddleware>().unwrap());
/// assert_eq!(&flag.into_inner(), req.req_data().get::<FlagFromMiddleware>().unwrap());
/// }
///
///
/// HttpResponse::Ok()
/// }
/// ```
@@ -68,7 +67,7 @@ impl<T: Clone + 'static> FromRequest for ReqData<T> {
type Future = Ready<Result<Self, Error>>;
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
if let Some(st) = req.extensions().get::<T>() {
if let Some(st) = req.req_data().get::<T>() {
ok(ReqData(st.clone()))
} else {
log::debug!(

View File

@@ -1,7 +1,7 @@
use std::borrow::Cow;
use actix_http::{
body::{BoxBody, EitherBody, MessageBody},
body::{BoxBody, MessageBody},
error::HttpError,
header::HeaderMap,
header::IntoHeaderPair,
@@ -9,7 +9,9 @@ use actix_http::{
};
use bytes::{Bytes, BytesMut};
use crate::{BoxError, Error, HttpRequest, HttpResponse, HttpResponseBuilder};
use crate::{
any_body::AnyBody, BoxError, Error, HttpRequest, HttpResponse, HttpResponseBuilder,
};
/// Trait implemented by types that can be converted to an HTTP response.
///
@@ -72,7 +74,7 @@ impl Responder for HttpResponse {
}
}
impl Responder for actix_http::Response<BoxBody> {
impl Responder for actix_http::Response<AnyBody<BoxBody>> {
type Body = BoxBody;
#[inline]
@@ -95,7 +97,10 @@ impl Responder for actix_http::ResponseBuilder {
#[inline]
fn respond_to(mut self, req: &HttpRequest) -> HttpResponse<Self::Body> {
self.finish().map_into_boxed_body().respond_to(req)
self.finish()
.map_into_boxed_body()
.map_body(|_, body| AnyBody::Boxed { body })
.respond_to(req)
}
}
@@ -104,12 +109,12 @@ where
T: Responder,
<T::Body as MessageBody>::Error: Into<BoxError>,
{
type Body = EitherBody<T::Body>;
type Body = T::Body;
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
match self {
Some(val) => val.respond_to(req).map_into_left_body(),
None => HttpResponse::new(StatusCode::NOT_FOUND).map_into_right_body(),
Some(val) => val.respond_to(req),
None => HttpResponse::new(StatusCode::NOT_FOUND).map_into_body(),
}
}
}
@@ -120,12 +125,12 @@ where
<T::Body as MessageBody>::Error: Into<BoxError>,
E: Into<Error>,
{
type Body = EitherBody<T::Body>;
type Body = T::Body;
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
match self {
Ok(val) => val.respond_to(req).map_into_left_body(),
Err(err) => HttpResponse::from_error(err.into()).map_into_right_body(),
Ok(val) => val.respond_to(req),
Err(err) => HttpResponse::from_error(err.into()).map_into_body(),
}
}
}
@@ -146,7 +151,12 @@ macro_rules! impl_responder_by_forward_into_base_response {
type Body = $body;
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
let res: actix_http::Response<_> = self.into();
let res = actix_http::Response::with_body(
StatusCode::default(),
AnyBody::Full {
body: Bytes::from(self),
},
);
res.into()
}
}
@@ -171,7 +181,12 @@ macro_rules! impl_into_string_responder {
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
let string: String = self.into();
let res: actix_http::Response<_> = string.into();
let res = actix_http::Response::with_body(
StatusCode::default(),
AnyBody::Full {
body: Bytes::from(string),
},
);
res.into()
}
}
@@ -250,12 +265,12 @@ where
T: Responder,
<T::Body as MessageBody>::Error: Into<BoxError>,
{
type Body = EitherBody<T::Body>;
type Body = T::Body;
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
let headers = match self.headers {
Ok(headers) => headers,
Err(err) => return HttpResponse::from_error(err).map_into_right_body(),
Err(err) => return HttpResponse::from_error(err).map_into_body(),
};
let mut res = self.responder.respond_to(req);
@@ -269,7 +284,7 @@ where
res.headers_mut().insert(k, v);
}
res.map_into_left_body()
res
}
}

View File

@@ -22,6 +22,7 @@ use actix_http::header::HeaderValue;
use cookie::{Cookie, CookieJar};
use crate::{
any_body::AnyBody,
error::{Error, JsonPayloadError},
BoxError, HttpResponse,
};
@@ -109,6 +110,7 @@ impl HttpResponseBuilder {
}
/// Replaced with [`Self::insert_header()`].
#[doc(hidden)]
#[deprecated(
since = "4.0.0",
note = "Replaced with `insert_header((key, value))`. Will be removed in v5."
@@ -133,6 +135,7 @@ impl HttpResponseBuilder {
}
/// Replaced with [`Self::append_header()`].
#[doc(hidden)]
#[deprecated(
since = "4.0.0",
note = "Replaced with `append_header((key, value))`. Will be removed in v5."
@@ -331,7 +334,7 @@ impl HttpResponseBuilder {
.set_body(body);
#[allow(unused_mut)] // mut is only unused when cookies are disabled
let mut res = HttpResponse::from(res);
let mut res = HttpResponse::from(res.map_body(|_, body| AnyBody::Stream { body }));
#[cfg(feature = "cookies")]
if let Some(ref jar) = self.cookies {
@@ -414,7 +417,7 @@ impl From<HttpResponseBuilder> for HttpResponse {
}
}
impl From<HttpResponseBuilder> for Response<BoxBody> {
impl From<HttpResponseBuilder> for Response<AnyBody<BoxBody>> {
fn from(mut builder: HttpResponseBuilder) -> Self {
builder.finish().into()
}

View File

@@ -8,7 +8,7 @@ use std::{
};
use actix_http::{
body::{BoxBody, EitherBody, MessageBody},
body::{BoxBody, MessageBody, None as NoneBody},
header::HeaderMap,
Extensions, Response, ResponseHead, StatusCode,
};
@@ -22,11 +22,11 @@ use {
cookie::Cookie,
};
use crate::{error::Error, HttpResponseBuilder};
use crate::{any_body::AnyBody, error::Error, HttpResponseBuilder};
/// An outgoing response.
pub struct HttpResponse<B = BoxBody> {
res: Response<B>,
res: Response<AnyBody<B>>,
pub(crate) error: Option<Error>,
}
@@ -35,7 +35,7 @@ impl HttpResponse<BoxBody> {
#[inline]
pub fn new(status: StatusCode) -> Self {
Self {
res: Response::new(status),
res: Response::with_body(status, AnyBody::default()),
error: None,
}
}
@@ -54,6 +54,13 @@ impl HttpResponse<BoxBody> {
response.error = Some(error);
response
}
pub fn map_into_body<B>(self) -> HttpResponse<B>
where
B: MessageBody + 'static,
{
self.map_body(|_, body| body.into_body())
}
}
impl<B> HttpResponse<B> {
@@ -61,7 +68,7 @@ impl<B> HttpResponse<B> {
#[inline]
pub fn with_body(status: StatusCode, body: B) -> Self {
Self {
res: Response::with_body(status, body),
res: Response::with_body(status, AnyBody::Stream { body }),
error: None,
}
}
@@ -182,12 +189,12 @@ impl<B> HttpResponse<B> {
/// Get body of this response
#[inline]
pub fn body(&self) -> &B {
pub fn body(&self) -> &AnyBody<B> {
self.res.body()
}
/// Set a body
pub fn set_body<B2>(self, body: B2) -> HttpResponse<B2> {
pub fn set_body<B2>(self, body: AnyBody<B2>) -> HttpResponse<B2> {
HttpResponse {
res: self.res.set_body(body),
error: None,
@@ -196,12 +203,12 @@ impl<B> HttpResponse<B> {
}
/// Split response and body
pub fn into_parts(self) -> (HttpResponse<()>, B) {
pub fn into_parts(self) -> (HttpResponse<()>, AnyBody<B>) {
let (head, body) = self.res.into_parts();
(
HttpResponse {
res: head,
res: head.map_body(|_, _b| AnyBody::default()),
error: None,
},
body,
@@ -211,7 +218,7 @@ impl<B> HttpResponse<B> {
/// Drop request's body
pub fn drop_body(self) -> HttpResponse<()> {
HttpResponse {
res: self.res.drop_body(),
res: self.res.drop_body().map_body(|_, _b| AnyBody::default()),
error: None,
}
}
@@ -219,7 +226,7 @@ impl<B> HttpResponse<B> {
/// Set a body and return previous body value
pub fn map_body<F, B2>(self, f: F) -> HttpResponse<B2>
where
F: FnOnce(&mut ResponseHead, B) -> B2,
F: FnOnce(&mut ResponseHead, AnyBody<B>) -> AnyBody<B2>,
{
HttpResponse {
res: self.res.map_body(f),
@@ -229,27 +236,28 @@ impl<B> HttpResponse<B> {
// TODO: docs for the body map methods below
#[inline]
pub fn map_into_left_body<R>(self) -> HttpResponse<EitherBody<B, R>> {
self.map_body(|_, body| EitherBody::left(body))
}
#[inline]
pub fn map_into_right_body<L>(self) -> HttpResponse<EitherBody<L, B>> {
self.map_body(|_, body| EitherBody::right(body))
}
#[inline]
pub fn map_into_boxed_body(self) -> HttpResponse<BoxBody>
where
B: MessageBody + 'static,
{
// TODO: avoid double boxing with down-casting, if it improves perf
self.map_body(|_, body| BoxBody::new(body))
self.map_body(|_, body| AnyBody::Boxed {
body: match body {
AnyBody::None => BoxBody::new(NoneBody::new()),
AnyBody::Full { body } => BoxBody::new(body),
AnyBody::Stream { body } => BoxBody::new(body),
AnyBody::Boxed { body } => body,
},
})
}
/// Extract response body
pub fn into_body(self) -> B {
pub fn take_body(&mut self) -> AnyBody<B> {
self.res.take_body()
}
/// Extract response body
pub fn into_body(self) -> AnyBody<B> {
self.res.into_body()
}
}
@@ -266,8 +274,8 @@ where
}
}
impl<B> From<Response<B>> for HttpResponse<B> {
fn from(res: Response<B>) -> Self {
impl<B> From<Response<AnyBody<B>>> for HttpResponse<B> {
fn from(res: Response<AnyBody<B>>) -> Self {
HttpResponse { res, error: None }
}
}
@@ -278,7 +286,7 @@ impl From<Error> for HttpResponse {
}
}
impl<B> From<HttpResponse<B>> for Response<B> {
impl<B> From<HttpResponse<B>> for Response<AnyBody<B>> {
fn from(res: HttpResponse<B>) -> Self {
// this impl will always be called as part of dispatcher
@@ -291,14 +299,14 @@ impl<B> From<HttpResponse<B>> for Response<B> {
}
}
// Future is only implemented for BoxBody payload type because it's the most useful for making
// Future is only implemented for default payload type because it's the most useful for making
// simple handlers without async blocks. Making it generic over all MessageBody types requires a
// future impl on Response which would cause it's body field to be, undesirably, Option<B>.
//
// This impl is not particularly efficient due to the Response construction and should probably
// not be invoked if performance is important. Prefer an async fn/block in such cases.
impl Future for HttpResponse<BoxBody> {
type Output = Result<Response<BoxBody>, Error>;
impl Future for HttpResponse {
type Output = Result<Response<AnyBody>, Error>;
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(err) = self.error.take() {
@@ -307,7 +315,7 @@ impl Future for HttpResponse<BoxBody> {
Poll::Ready(Ok(mem::replace(
&mut self.res,
Response::new(StatusCode::default()),
Response::with_body(StatusCode::default(), AnyBody::None),
)))
}
}

View File

@@ -1,6 +1,6 @@
use std::{cell::RefCell, fmt, future::Future, mem, rc::Rc};
use std::{cell::RefCell, fmt, future::Future, marker::PhantomData, mem, rc::Rc};
use actix_http::Extensions;
use actix_http::{body::BoxBody, Extensions};
use actix_router::{ResourceDef, Router};
use actix_service::{
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
@@ -52,7 +52,7 @@ type Guards = Vec<Box<dyn Guard>>;
/// * /{project_id}/path1 - responds to all http method
/// * /{project_id}/path2 - `GET` requests
/// * /{project_id}/path3 - `HEAD` requests
pub struct Scope<T = ScopeEndpoint> {
pub struct Scope<T = ScopeEndpoint, B = BoxBody> {
endpoint: T,
rdef: String,
app_data: Option<Extensions>,
@@ -61,6 +61,7 @@ pub struct Scope<T = ScopeEndpoint> {
default: Option<Rc<BoxedHttpServiceFactory>>,
external: Vec<ResourceDef>,
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
_phantom: PhantomData<B>,
}
impl Scope {
@@ -77,19 +78,21 @@ impl Scope {
default: None,
external: Vec::new(),
factory_ref,
_phantom: Default::default(),
}
}
}
impl<T> Scope<T>
impl<T, B> Scope<T, B>
where
T: ServiceFactory<
ServiceRequest,
Config = (),
Response = ServiceResponse,
Response = ServiceResponse<B>,
Error = Error,
InitError = (),
>,
B: 'static,
{
/// Add match guard to a scope.
///
@@ -295,32 +298,29 @@ where
self
}
/// Registers middleware, in the form of a middleware component (type),
/// that runs during inbound processing in the request
/// life-cycle (request -> response), modifying request as
/// necessary, across all requests managed by the *Scope*. Scope-level
/// middleware is more limited in what it can modify, relative to Route or
/// Application level middleware, in that Scope-level middleware can not modify
/// ServiceResponse.
/// Registers middleware, in the form of a middleware component (type), that runs during inbound
/// processing in the request life-cycle (request -> response), modifying request as necessary,
/// across all requests managed by the *Scope*.
///
/// Use middleware when you need to read or modify *every* request in some way.
pub fn wrap<M>(
pub fn wrap<M, B1>(
self,
mw: M,
) -> Scope<
impl ServiceFactory<
ServiceRequest,
Config = (),
Response = ServiceResponse,
Response = ServiceResponse<B1>,
Error = Error,
InitError = (),
>,
B1,
>
where
M: Transform<
T::Service,
ServiceRequest,
Response = ServiceResponse,
Response = ServiceResponse<B1>,
Error = Error,
InitError = (),
>,
@@ -334,16 +334,15 @@ where
default: self.default,
external: self.external,
factory_ref: self.factory_ref,
_phantom: PhantomData,
}
}
/// Registers middleware, in the form of a closure, that runs during inbound
/// processing in the request life-cycle (request -> response), modifying
/// request as necessary, across all requests managed by the *Scope*.
/// Scope-level middleware is more limited in what it can modify, relative
/// to Route or Application level middleware, in that Scope-level middleware
/// can not modify ServiceResponse.
/// Registers middleware, in the form of a closure, that runs during inbound processing in the
/// request life-cycle (request -> response), modifying request as necessary, across all
/// requests managed by the *Scope*.
///
/// # Examples
/// ```
/// use actix_service::Service;
/// use actix_web::{web, App};
@@ -369,21 +368,22 @@ where
/// .route("/index.html", web::get().to(index)));
/// }
/// ```
pub fn wrap_fn<F, R>(
pub fn wrap_fn<F, R, B1>(
self,
mw: F,
) -> Scope<
impl ServiceFactory<
ServiceRequest,
Config = (),
Response = ServiceResponse,
Response = ServiceResponse<B1>,
Error = Error,
InitError = (),
>,
B1,
>
where
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
R: Future<Output = Result<ServiceResponse, Error>>,
R: Future<Output = Result<ServiceResponse<B1>, Error>>,
{
Scope {
endpoint: apply_fn_factory(self.endpoint, mw),
@@ -394,6 +394,7 @@ where
default: self.default,
external: self.external,
factory_ref: self.factory_ref,
_phantom: PhantomData,
}
}
}

View File

@@ -6,9 +6,7 @@ use std::{
sync::{Arc, Mutex},
};
use actix_http::{
body::MessageBody, CloneableExtensions, HttpService, KeepAlive, Request, Response,
};
use actix_http::{body::MessageBody, Extensions, HttpService, KeepAlive, Request, Response};
use actix_server::{Server, ServerBuilder};
use actix_service::{
map_config, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _,
@@ -19,7 +17,7 @@ use actix_tls::accept::openssl::reexports::{AlpnError, SslAcceptor, SslAcceptorB
#[cfg(feature = "rustls")]
use actix_tls::accept::rustls::reexports::ServerConfig as RustlsServerConfig;
use crate::{config::AppConfig, Error};
use crate::{any_body::AnyBody, config::AppConfig, Error};
struct Socket {
scheme: &'static str,
@@ -57,7 +55,7 @@ where
S: ServiceFactory<Request, Config = AppConfig>,
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
S::Response: Into<Response<AnyBody<B>>>,
B: MessageBody,
{
pub(super) factory: F,
@@ -65,7 +63,7 @@ where
backlog: u32,
sockets: Vec<Socket>,
builder: ServerBuilder,
on_connect_fn: Option<Arc<dyn Fn(&dyn Any, &mut CloneableExtensions) + Send + Sync>>,
on_connect_fn: Option<Arc<dyn Fn(&dyn Any, &mut Extensions) + Send + Sync>>,
_phantom: PhantomData<(S, B)>,
}
@@ -77,7 +75,7 @@ where
S: ServiceFactory<Request, Config = AppConfig> + 'static,
S::Error: Into<Error> + 'static,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + 'static,
S::Response: Into<Response<AnyBody<B>>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
S::Service: 'static,
@@ -102,9 +100,8 @@ where
}
/// Sets function that will be called once before each connection is handled.
/// It will receive a `&std::any::Any`, which contains underlying connection type and a
/// [CloneableExtensions] container so that request-local data can be passed to middleware
/// and handlers.
/// It will receive a `&std::any::Any`, which contains underlying connection type and an
/// [Extensions] container so that connection data can be accessed in middleware and handlers.
///
/// # Connection Types
/// - `actix_tls::accept::openssl::TlsStream<actix_web::rt::net::TcpStream>` when using openssl.
@@ -114,7 +111,7 @@ where
/// See the `on_connect` example for additional details.
pub fn on_connect<CB>(self, f: CB) -> HttpServer<F, I, S, B>
where
CB: Fn(&dyn Any, &mut CloneableExtensions) + Send + Sync + 'static,
CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static,
{
HttpServer {
factory: self.factory,
@@ -303,9 +300,10 @@ where
})
};
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> = err.into().error_response().into();
res.map_into_boxed_body()
});
svc.finish(map_config(fac, move |_| {
AppConfig::new(false, host.clone(), addr)
@@ -363,9 +361,10 @@ where
svc
};
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> = err.into().error_response().into();
res.map_into_boxed_body()
});
svc.finish(map_config(fac, move |_| {
AppConfig::new(true, host.clone(), addr)
@@ -547,9 +546,10 @@ where
.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext));
}
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> = err.into().error_response().into();
res.map_into_boxed_body()
});
svc.finish(map_config(fac, move |_| config.clone()))
})
@@ -588,9 +588,10 @@ where
socket_addr,
);
let fac = factory()
.into_factory()
.map_err(|err| err.into().error_response());
let fac = factory().into_factory().map_err(|err| {
let res: actix_http::Response<_> = err.into().error_response().into();
res.map_into_boxed_body()
});
fn_service(|io: UnixStream| async { Ok((io, Protocol::Http1, None)) }).and_then(
HttpService::build()
@@ -613,7 +614,7 @@ where
S: ServiceFactory<Request, Config = AppConfig>,
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
S::Response: Into<Response<AnyBody<B>>>,
S::Service: 'static,
B: MessageBody,
{

View File

@@ -5,7 +5,7 @@ use std::{
};
use actix_http::{
body::{BoxBody, EitherBody, MessageBody},
body::{self, BoxBody, MessageBody},
header::HeaderMap,
Extensions, HttpMessage, Method, Payload, PayloadStream, RequestHead, Response,
ResponseHead, StatusCode, Uri, Version,
@@ -19,6 +19,7 @@ use actix_service::{
use cookie::{Cookie, ParseError as CookieParseError};
use crate::{
any_body::AnyBody,
config::{AppConfig, AppService},
dev::ensure_leading_slash,
guard::Guard,
@@ -112,7 +113,7 @@ impl ServiceRequest {
/// Create service response
#[inline]
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
pub fn into_response<B, R: Into<Response<AnyBody<B>>>>(self, res: R) -> ServiceResponse<B> {
let res = HttpResponse::from(res.into());
ServiceResponse::new(self.req, res)
}
@@ -172,12 +173,10 @@ impl ServiceRequest {
self.head().uri.path()
}
/// The query string in the URL.
///
/// E.g., id=10
/// Counterpart to [`HttpRequest::query_string`](super::HttpRequest::query_string()).
#[inline]
pub fn query_string(&self) -> &str {
self.uri().query().unwrap_or_default()
self.req.query_string()
}
/// Peer socket address.
@@ -196,7 +195,7 @@ impl ServiceRequest {
/// Get *ConnectionInfo* for the current request.
#[inline]
pub fn connection_info(&self) -> Ref<'_, ConnectionInfo> {
ConnectionInfo::get(self.head(), &*self.app_config())
self.req.connection_info()
}
/// Get a reference to the Path parameters.
@@ -241,6 +240,7 @@ impl ServiceRequest {
}
/// Counterpart to [`HttpRequest::app_data`](super::HttpRequest::app_data()).
#[inline]
pub fn app_data<T: 'static>(&self) -> Option<&T> {
for container in self.req.inner.app_data.iter().rev() {
if let Some(data) = container.get::<T>() {
@@ -251,6 +251,12 @@ impl ServiceRequest {
None
}
/// Counterpart to [`HttpRequest::conn_data`](super::HttpRequest::conn_data()).
#[inline]
pub fn conn_data<T: 'static>(&self) -> Option<&T> {
self.req.conn_data()
}
#[cfg(feature = "cookies")]
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
self.req.cookies()
@@ -263,6 +269,7 @@ impl ServiceRequest {
}
/// Set request payload.
#[inline]
pub fn set_payload(&mut self, payload: Payload) {
self.payload = payload;
}
@@ -280,6 +287,7 @@ impl ServiceRequest {
}
impl Resource<Url> for ServiceRequest {
#[inline]
fn resource_path(&mut self) -> &mut Path<Url> {
self.match_info_mut()
}
@@ -403,18 +411,22 @@ impl<B> ServiceResponse<B> {
self.response.headers_mut()
}
#[inline]
pub fn take_body(&mut self) -> AnyBody<B> {
self.response.take_body()
}
/// Extract response body
pub fn into_body(self) -> B {
#[inline]
pub fn into_body(self) -> AnyBody<B> {
self.response.into_body()
}
}
impl<B> ServiceResponse<B> {
/// Set a new body
#[inline]
pub fn map_body<F, B2>(self, f: F) -> ServiceResponse<B2>
where
F: FnOnce(&mut ResponseHead, B) -> B2,
F: FnOnce(&mut ResponseHead, AnyBody<B>) -> AnyBody<B2>,
{
let response = self.response.map_body(f);
@@ -424,22 +436,29 @@ impl<B> ServiceResponse<B> {
}
}
#[inline]
pub fn map_into_left_body<R>(self) -> ServiceResponse<EitherBody<B, R>> {
self.map_body(|_, body| EitherBody::left(body))
}
// #[inline]
// pub fn map_into_left_body<R>(self) -> ServiceResponse<EitherBody<B, R>> {
// self.map_body(|_, body| EitherBody::left(body))
// }
#[inline]
pub fn map_into_right_body<L>(self) -> ServiceResponse<EitherBody<L, B>> {
self.map_body(|_, body| EitherBody::right(body))
}
// #[inline]
// pub fn map_into_right_body<L>(self) -> ServiceResponse<EitherBody<L, B>> {
// self.map_body(|_, body| EitherBody::right(body))
// }
#[inline]
pub fn map_into_boxed_body(self) -> ServiceResponse<BoxBody>
where
B: MessageBody + 'static,
{
self.map_body(|_, body| BoxBody::new(body))
self.map_body(|_, body| AnyBody::Stream {
body: match body {
AnyBody::None => BoxBody::new(body::None::new()),
AnyBody::Full { body } => BoxBody::new(body),
AnyBody::Stream { body } => BoxBody::new(body),
AnyBody::Boxed { body } => body,
},
})
}
}
@@ -449,8 +468,8 @@ impl<B> From<ServiceResponse<B>> for HttpResponse<B> {
}
}
impl<B> From<ServiceResponse<B>> for Response<B> {
fn from(res: ServiceResponse<B>) -> Response<B> {
impl<B> From<ServiceResponse<B>> for Response<AnyBody<B>> {
fn from(res: ServiceResponse<B>) -> Response<AnyBody<B>> {
res.response.into()
}
}

View File

@@ -163,7 +163,7 @@ where
actix_rt::pin!(body);
while let Some(item) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
bytes.extend_from_slice(&item.map_err(Into::into).unwrap());
bytes.extend_from_slice(&item.unwrap());
}
bytes.freeze()
@@ -205,7 +205,7 @@ where
actix_rt::pin!(body);
while let Some(item) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
bytes.extend_from_slice(&item.map_err(Into::into).unwrap());
bytes.extend_from_slice(&item.unwrap());
}
bytes.freeze()
@@ -581,7 +581,14 @@ impl TestRequest {
let app_state = AppInitServiceState::new(Rc::new(self.rmap), self.config.clone());
ServiceRequest::new(
HttpRequest::new(self.path, head, app_state, Rc::new(self.app_data)),
HttpRequest::new(
self.path,
head,
app_state,
Rc::new(self.app_data),
None,
Default::default(),
),
payload,
)
}
@@ -599,7 +606,14 @@ impl TestRequest {
let app_state = AppInitServiceState::new(Rc::new(self.rmap), self.config.clone());
HttpRequest::new(self.path, head, app_state, Rc::new(self.app_data))
HttpRequest::new(
self.path,
head,
app_state,
Rc::new(self.app_data),
None,
Default::default(),
)
}
/// Complete request creation and generate `HttpRequest` and `Payload` instances
@@ -610,7 +624,14 @@ impl TestRequest {
let app_state = AppInitServiceState::new(Rc::new(self.rmap), self.config.clone());
let req = HttpRequest::new(self.path, head, app_state, Rc::new(self.app_data));
let req = HttpRequest::new(
self.path,
head,
app_state,
Rc::new(self.app_data),
None,
Default::default(),
);
(req, payload)
}

View File

@@ -12,9 +12,9 @@ use futures_core::ready;
use pin_project_lite::pin_project;
use crate::{
body, dev,
dev,
web::{Form, Json},
Error, FromRequest, HttpRequest, HttpResponse, Responder,
Error, FromRequest, HttpRequest,
};
/// Combines two extractor or responder types into a single type.
@@ -140,21 +140,23 @@ impl<L, R> Either<L, R> {
}
}
/// See [here](#responder) for example of usage as a handler return type.
impl<L, R> Responder for Either<L, R>
where
L: Responder,
R: Responder,
{
type Body = body::EitherBody<L::Body, R::Body>;
// /// See [here](#responder) for example of usage as a handler return type.
// impl<L, R> Responder for Either<L, R>
// where
// L: Responder,
// R: Responder,
// {
// type Body = EitherAnyBody<L::Body, R::Body>;
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
match self {
Either::Left(a) => a.respond_to(req).map_into_left_body(),
Either::Right(b) => b.respond_to(req).map_into_right_body(),
}
}
}
// fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
// match self {
// Either::Left(a) => a.respond_to(req).map_body(|_, body| EitherAnyBodyProj::left(body)),
// Either::Right(b) => b
// .respond_to(req)
// .map_body(|_, body| EitherAnyBodyProj::right(body)),
// }
// }
// }
/// A composite error resulting from failure to extract an `Either<L, R>`.
///

View File

@@ -20,9 +20,8 @@ use serde::{de::DeserializeOwned, Serialize};
#[cfg(feature = "__compress")]
use crate::dev::Decompress;
use crate::{
body::EitherBody, error::UrlencodedError, extract::FromRequest,
http::header::CONTENT_LENGTH, web, Error, HttpMessage, HttpRequest, HttpResponse,
Responder,
error::UrlencodedError, extract::FromRequest, http::header::CONTENT_LENGTH, web, Error,
HttpMessage, HttpRequest, HttpResponse, Responder,
};
/// URL encoded payload extractor and responder.
@@ -181,7 +180,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// See [here](#responder) for example of usage as a handler return type.
impl<T: Serialize> Responder for Form<T> {
type Body = EitherBody<String>;
type Body = String;
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
match serde_urlencoded::to_string(&self.0) {
@@ -189,12 +188,12 @@ impl<T: Serialize> Responder for Form<T> {
.content_type(mime::APPLICATION_WWW_FORM_URLENCODED)
.message_body(body)
{
Ok(res) => res.map_into_left_body(),
Err(err) => HttpResponse::from_error(err).map_into_right_body(),
Ok(res) => res,
Err(err) => HttpResponse::from_error(err).map_into_body(),
},
Err(err) => {
HttpResponse::from_error(UrlencodedError::Serialize(err)).map_into_right_body()
HttpResponse::from_error(UrlencodedError::Serialize(err)).map_into_body()
}
}
}

View File

@@ -19,7 +19,6 @@ use actix_http::Payload;
#[cfg(feature = "__compress")]
use crate::dev::Decompress;
use crate::{
body::EitherBody,
error::{Error, JsonPayloadError},
extract::FromRequest,
http::header::CONTENT_LENGTH,
@@ -117,7 +116,7 @@ impl<T: Serialize> Serialize for Json<T> {
///
/// If serialization failed
impl<T: Serialize> Responder for Json<T> {
type Body = EitherBody<String>;
type Body = String;
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
match serde_json::to_string(&self.0) {
@@ -125,12 +124,12 @@ impl<T: Serialize> Responder for Json<T> {
.content_type(mime::APPLICATION_JSON)
.message_body(body)
{
Ok(res) => res.map_into_left_body(),
Err(err) => HttpResponse::from_error(err).map_into_right_body(),
Ok(res) => res,
Err(err) => HttpResponse::from_error(err).map_into_body(),
},
Err(err) => {
HttpResponse::from_error(JsonPayloadError::Serialize(err)).map_into_right_body()
HttpResponse::from_error(JsonPayloadError::Serialize(err)).map_into_body()
}
}
}