mirror of https://github.com/fafhrd91/actix-web
Merge branch 'master' into feature/h1_pending_timer
This commit is contained in:
commit
e0f3fc0ccb
|
@ -1,3 +1,7 @@
|
||||||
[alias]
|
[alias]
|
||||||
chk = "hack check --workspace --all-features --tests --examples"
|
chk = "hack check --workspace --all-features --tests --examples"
|
||||||
lint = "hack --clean-per-run clippy --workspace --tests --examples"
|
lint = "hack --clean-per-run clippy --workspace --tests --examples"
|
||||||
|
ci-min = "hack check --workspace --no-default-features"
|
||||||
|
ci-min-test = "hack check --workspace --no-default-features --tests --examples"
|
||||||
|
ci-default = "hack check --workspace"
|
||||||
|
ci-full = "check --workspace --bins --examples --tests"
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* `HttpResponse` and `HttpResponseBuilder` structs. [#2065]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* Most error types are now marked `#[non_exhaustive]`. [#2148]
|
||||||
|
|
||||||
|
[#2065]: https://github.com/actix/actix-web/pull/2065
|
||||||
|
[#2148]: https://github.com/actix/actix-web/pull/2148
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.5 - 2021-04-02
|
## 4.0.0-beta.5 - 2021-04-02
|
||||||
|
|
46
Cargo.toml
46
Cargo.toml
|
@ -16,11 +16,7 @@ edition = "2018"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
# features that docs.rs will build with
|
# features that docs.rs will build with
|
||||||
features = ["openssl", "rustls", "compress", "secure-cookies"]
|
features = ["openssl", "rustls", "compress", "cookies", "secure-cookies"]
|
||||||
|
|
||||||
[badges]
|
|
||||||
travis-ci = { repository = "actix/actix-web", branch = "master" }
|
|
||||||
codecov = { repository = "actix/actix-web", branch = "master", service = "github" }
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_web"
|
name = "actix_web"
|
||||||
|
@ -38,6 +34,8 @@ members = [
|
||||||
"actix-http-test",
|
"actix-http-test",
|
||||||
"actix-test",
|
"actix-test",
|
||||||
]
|
]
|
||||||
|
# enable when MSRV is 1.51+
|
||||||
|
# resolver = "2"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["compress", "cookies"]
|
default = ["compress", "cookies"]
|
||||||
|
@ -46,10 +44,10 @@ default = ["compress", "cookies"]
|
||||||
compress = ["actix-http/compress"]
|
compress = ["actix-http/compress"]
|
||||||
|
|
||||||
# support for cookies
|
# support for cookies
|
||||||
cookies = ["actix-http/cookies"]
|
cookies = ["cookie"]
|
||||||
|
|
||||||
# secure cookies feature
|
# secure cookies feature
|
||||||
secure-cookies = ["actix-http/secure-cookies"]
|
secure-cookies = ["cookie/secure"]
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
openssl = ["actix-http/openssl", "actix-tls/accept", "actix-tls/openssl"]
|
openssl = ["actix-http/openssl", "actix-tls/accept", "actix-tls/openssl"]
|
||||||
|
@ -57,22 +55,6 @@ openssl = ["actix-http/openssl", "actix-tls/accept", "actix-tls/openssl"]
|
||||||
# rustls
|
# rustls
|
||||||
rustls = ["actix-http/rustls", "actix-tls/accept", "actix-tls/rustls"]
|
rustls = ["actix-http/rustls", "actix-tls/accept", "actix-tls/rustls"]
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "basic"
|
|
||||||
required-features = ["compress"]
|
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "uds"
|
|
||||||
required-features = ["compress"]
|
|
||||||
|
|
||||||
[[test]]
|
|
||||||
name = "test_server"
|
|
||||||
required-features = ["compress", "cookies"]
|
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "on_connect"
|
|
||||||
required-features = []
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-macros = "0.2.0"
|
actix-macros = "0.2.0"
|
||||||
|
@ -88,11 +70,13 @@ actix-http = "3.0.0-beta.5"
|
||||||
|
|
||||||
ahash = "0.7"
|
ahash = "0.7"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
|
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
encoding_rs = "0.8"
|
encoding_rs = "0.8"
|
||||||
futures-core = { version = "0.3.7", default-features = false }
|
futures-core = { version = "0.3.7", default-features = false }
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false }
|
||||||
|
itoa = "0.4"
|
||||||
language-tags = "0.2"
|
language-tags = "0.2"
|
||||||
once_cell = "1.5"
|
once_cell = "1.5"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
@ -137,6 +121,22 @@ actix-web-actors = { path = "actix-web-actors" }
|
||||||
actix-web-codegen = { path = "actix-web-codegen" }
|
actix-web-codegen = { path = "actix-web-codegen" }
|
||||||
awc = { path = "awc" }
|
awc = { path = "awc" }
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "test_server"
|
||||||
|
required-features = ["compress", "cookies"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "basic"
|
||||||
|
required-features = ["compress"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "uds"
|
||||||
|
required-features = ["compress"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "on_connect"
|
||||||
|
required-features = []
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "server"
|
name = "server"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Copyright (c) 2017 Actix Team
|
Copyright (c) 2017-NOW Actix Team
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any
|
Permission is hereby granted, free of charge, to any
|
||||||
person obtaining a copy of this software and associated
|
person obtaining a copy of this software and associated
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
* For symbolic links, `Content-Disposition` header no longer shows the filename of the original file. [#2156]
|
||||||
|
|
||||||
|
[#2156]: https://github.com/actix/actix-web/pull/2156
|
||||||
|
|
||||||
|
|
||||||
## 0.6.0-beta.4 - 2021-04-02
|
## 0.6.0-beta.4 - 2021-04-02
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
use actix_web::{http::StatusCode, ResponseError};
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
|
|
||||||
/// Errors which can occur when serving static files.
|
/// Errors which can occur when serving static files.
|
||||||
|
@ -16,8 +16,8 @@ pub enum FilesError {
|
||||||
|
|
||||||
/// Return `NotFound` for `FilesError`
|
/// Return `NotFound` for `FilesError`
|
||||||
impl ResponseError for FilesError {
|
impl ResponseError for FilesError {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn status_code(&self) -> StatusCode {
|
||||||
HttpResponse::new(StatusCode::NOT_FOUND)
|
StatusCode::NOT_FOUND
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -754,4 +754,19 @@ mod tests {
|
||||||
let res = test::call_service(&srv, req).await;
|
let res = test::call_service(&srv, req).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_symlinks() {
|
||||||
|
let srv = test::init_service(App::new().service(Files::new("test", "."))).await;
|
||||||
|
|
||||||
|
let req = TestRequest::get()
|
||||||
|
.uri("/test/tests/symlink-test.png")
|
||||||
|
.to_request();
|
||||||
|
let res = test::call_service(&srv, req).await;
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
assert_eq!(
|
||||||
|
res.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
||||||
|
"inline; filename=\"symlink-test.png\""
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,10 +83,10 @@ impl Service<ServiceRequest> for FilesService {
|
||||||
};
|
};
|
||||||
|
|
||||||
// full file path
|
// full file path
|
||||||
let path = match self.directory.join(&real_path).canonicalize() {
|
let path = self.directory.join(&real_path);
|
||||||
Ok(path) => path,
|
if let Err(err) = path.canonicalize() {
|
||||||
Err(err) => return Box::pin(self.handle_err(err, req)),
|
return Box::pin(self.handle_err(err, req));
|
||||||
};
|
}
|
||||||
|
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
if let Some(ref redir_index) = self.index {
|
if let Some(ref redir_index) = self.index {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
test.png
|
|
@ -1,6 +1,29 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* `impl<T: MessageBody> MessageBody for Pin<Box<T>>`. [#2152]
|
||||||
|
* Helper `body::to_bytes` for async collecting message body into Bytes. [#2158]
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
* The type parameter of `Response` no longer has a default. [#2152]
|
||||||
|
* The `Message` variant of `body::Body` is now `Pin<Box<dyn MessageBody>>`. [#2152]
|
||||||
|
* `BodyStream` and `SizedStream` are no longer restricted to Unpin types. [#2152]
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
* `cookies` feature flag. [#2065]
|
||||||
|
* Top-level `cookies` mod (re-export). [#2065]
|
||||||
|
* `HttpMessage` trait loses the `cookies` and `cookie` methods. [#2065]
|
||||||
|
* `impl ResponseError for CookieParseError`. [#2065]
|
||||||
|
* Deprecated methods on `ResponseBuilder`: `if_true`, `if_some`. [#2148]
|
||||||
|
* `ResponseBuilder::json`. [#2148]
|
||||||
|
* `ResponseBuilder::{set_header, header}`. [#2148]
|
||||||
|
* `impl From<serde_json::Value> for Body`. [#2148]
|
||||||
|
|
||||||
|
[#2065]: https://github.com/actix/actix-web/pull/2065
|
||||||
|
[#2148]: https://github.com/actix/actix-web/pull/2148
|
||||||
|
[#2152]: https://github.com/actix/actix-web/pull/2152
|
||||||
|
[#2158]: https://github.com/actix/actix-web/pull/2158
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.5 - 2021-04-02
|
## 3.0.0-beta.5 - 2021-04-02
|
||||||
|
|
|
@ -16,7 +16,7 @@ edition = "2018"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
# features that docs.rs will build with
|
# features that docs.rs will build with
|
||||||
features = ["openssl", "rustls", "compress", "cookies", "secure-cookies"]
|
features = ["openssl", "rustls", "compress"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_http"
|
name = "actix_http"
|
||||||
|
@ -34,12 +34,6 @@ rustls = ["actix-tls/rustls"]
|
||||||
# enable compression support
|
# enable compression support
|
||||||
compress = ["flate2", "brotli2"]
|
compress = ["flate2", "brotli2"]
|
||||||
|
|
||||||
# support for cookies
|
|
||||||
cookies = ["cookie"]
|
|
||||||
|
|
||||||
# support for secure cookies
|
|
||||||
secure-cookies = ["cookies", "cookie/secure"]
|
|
||||||
|
|
||||||
# trust-dns as client dns resolver
|
# trust-dns as client dns resolver
|
||||||
trust-dns = ["trust-dns-resolver"]
|
trust-dns = ["trust-dns-resolver"]
|
||||||
|
|
||||||
|
@ -55,7 +49,6 @@ base64 = "0.13"
|
||||||
bitflags = "1.2"
|
bitflags = "1.2"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
bytestring = "1"
|
bytestring = "1"
|
||||||
cookie = { version = "0.14.1", features = ["percent-encode"], optional = true }
|
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
encoding_rs = "0.8"
|
encoding_rs = "0.8"
|
||||||
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
|
@ -75,8 +68,6 @@ pin-project-lite = "0.2"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
regex = "1.3"
|
regex = "1.3"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
|
||||||
serde_urlencoded = "0.7"
|
|
||||||
sha-1 = "0.9"
|
sha-1 = "0.9"
|
||||||
smallvec = "1.6"
|
smallvec = "1.6"
|
||||||
time = { version = "0.2.23", default-features = false, features = ["std"] }
|
time = { version = "0.2.23", default-features = false, features = ["std"] }
|
||||||
|
@ -95,7 +86,8 @@ actix-tls = { version = "3.0.0-beta.5", features = ["openssl"] }
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
rcgen = "0.8"
|
rcgen = "0.8"
|
||||||
serde_derive = "1.0"
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
tls-openssl = { version = "0.10", package = "openssl" }
|
tls-openssl = { version = "0.10", package = "openssl" }
|
||||||
tls-rustls = { version = "0.19", package = "rustls" }
|
tls-rustls = { version = "0.19", package = "rustls" }
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
use actix_http::http::HeaderValue;
|
use actix_http::{body::Body, http::HeaderValue};
|
||||||
use actix_http::{Error, HttpService, Request, Response};
|
use actix_http::{Error, HttpService, Request, Response};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_util::StreamExt as _;
|
use futures_util::StreamExt as _;
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
async fn handle_request(mut req: Request) -> Result<Response, Error> {
|
async fn handle_request(mut req: Request) -> Result<Response<Body>, Error> {
|
||||||
let mut body = BytesMut::new();
|
let mut body = BytesMut::new();
|
||||||
while let Some(item) = req.payload().next().await {
|
while let Some(item) = req.payload().next().await {
|
||||||
body.extend_from_slice(&item?)
|
body.extend_from_slice(&item?)
|
||||||
|
|
|
@ -11,7 +11,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_codec::Encoder;
|
use actix_codec::Encoder;
|
||||||
use actix_http::{error::Error, ws, HttpService, Request, Response};
|
use actix_http::{body::BodyStream, error::Error, ws, HttpService, Request, Response};
|
||||||
use actix_rt::time::{interval, Interval};
|
use actix_rt::time::{interval, Interval};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
@ -34,14 +34,14 @@ async fn main() -> io::Result<()> {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handler(req: Request) -> Result<Response, Error> {
|
async fn handler(req: Request) -> Result<Response<BodyStream<Heartbeat>>, Error> {
|
||||||
log::info!("handshaking");
|
log::info!("handshaking");
|
||||||
let mut res = ws::handshake(req.head())?;
|
let mut res = ws::handshake(req.head())?;
|
||||||
|
|
||||||
// handshake will always fail under HTTP/2
|
// handshake will always fail under HTTP/2
|
||||||
|
|
||||||
log::info!("responding");
|
log::info!("responding");
|
||||||
Ok(res.streaming(Heartbeat::new(ws::Codec::new())))
|
Ok(res.message_body(BodyStream::new(Heartbeat::new(ws::Codec::new()))))
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Heartbeat {
|
struct Heartbeat {
|
||||||
|
|
|
@ -12,15 +12,19 @@ use crate::error::Error;
|
||||||
use super::{BodySize, BodyStream, MessageBody, SizedStream};
|
use super::{BodySize, BodyStream, MessageBody, SizedStream};
|
||||||
|
|
||||||
/// Represents various types of HTTP message body.
|
/// Represents various types of HTTP message body.
|
||||||
|
// #[deprecated(since = "4.0.0", note = "Use body types directly.")]
|
||||||
pub enum Body {
|
pub enum Body {
|
||||||
/// Empty response. `Content-Length` header is not set.
|
/// Empty response. `Content-Length` header is not set.
|
||||||
None,
|
None,
|
||||||
|
|
||||||
/// Zero sized response body. `Content-Length` header is set to `0`.
|
/// Zero sized response body. `Content-Length` header is set to `0`.
|
||||||
Empty,
|
Empty,
|
||||||
|
|
||||||
/// Specific response body.
|
/// Specific response body.
|
||||||
Bytes(Bytes),
|
Bytes(Bytes),
|
||||||
|
|
||||||
/// Generic message body.
|
/// Generic message body.
|
||||||
Message(Box<dyn MessageBody + Unpin>),
|
Message(Pin<Box<dyn MessageBody>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Body {
|
impl Body {
|
||||||
|
@ -30,8 +34,8 @@ impl Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create body from generic message body.
|
/// Create body from generic message body.
|
||||||
pub fn from_message<B: MessageBody + Unpin + 'static>(body: B) -> Body {
|
pub fn from_message<B: MessageBody + 'static>(body: B) -> Body {
|
||||||
Body::Message(Box::new(body))
|
Body::Message(Box::pin(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +64,7 @@ impl MessageBody for Body {
|
||||||
Poll::Ready(Some(Ok(mem::take(bin))))
|
Poll::Ready(Some(Ok(mem::take(bin))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Body::Message(body) => Pin::new(&mut **body).poll_next(cx),
|
Body::Message(body) => body.as_mut().poll_next(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,15 +136,9 @@ impl From<BytesMut> for Body {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<serde_json::Value> for Body {
|
|
||||||
fn from(v: serde_json::Value) -> Body {
|
|
||||||
Body::Bytes(v.to_string().into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> From<SizedStream<S>> for Body
|
impl<S> From<SizedStream<S>> for Body
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, Error>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, Error>> + 'static,
|
||||||
{
|
{
|
||||||
fn from(s: SizedStream<S>) -> Body {
|
fn from(s: SizedStream<S>) -> Body {
|
||||||
Body::from_message(s)
|
Body::from_message(s)
|
||||||
|
@ -149,7 +147,7 @@ where
|
||||||
|
|
||||||
impl<S, E> From<BodyStream<S>> for Body
|
impl<S, E> From<BodyStream<S>> for Body
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
||||||
E: Into<Error> + 'static,
|
E: Into<Error> + 'static,
|
||||||
{
|
{
|
||||||
fn from(s: BodyStream<S>) -> Body {
|
fn from(s: BodyStream<S>) -> Body {
|
||||||
|
|
|
@ -5,21 +5,25 @@ use std::{
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::{ready, Stream};
|
use futures_core::{ready, Stream};
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
use super::{BodySize, MessageBody};
|
use super::{BodySize, MessageBody};
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
/// Streaming response wrapper.
|
/// Streaming response wrapper.
|
||||||
///
|
///
|
||||||
/// Response does not contain `Content-Length` header and appropriate transfer encoding is used.
|
/// Response does not contain `Content-Length` header and appropriate transfer encoding is used.
|
||||||
pub struct BodyStream<S: Unpin> {
|
pub struct BodyStream<S> {
|
||||||
|
#[pin]
|
||||||
stream: S,
|
stream: S,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<S, E> BodyStream<S>
|
impl<S, E> BodyStream<S>
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin,
|
S: Stream<Item = Result<Bytes, E>>,
|
||||||
E: Into<Error>,
|
E: Into<Error>,
|
||||||
{
|
{
|
||||||
pub fn new(stream: S) -> Self {
|
pub fn new(stream: S) -> Self {
|
||||||
|
@ -29,7 +33,7 @@ where
|
||||||
|
|
||||||
impl<S, E> MessageBody for BodyStream<S>
|
impl<S, E> MessageBody for BodyStream<S>
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin,
|
S: Stream<Item = Result<Bytes, E>>,
|
||||||
E: Into<Error>,
|
E: Into<Error>,
|
||||||
{
|
{
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
|
@ -46,9 +50,9 @@ where
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
loop {
|
loop {
|
||||||
let stream = &mut self.as_mut().stream;
|
let stream = self.as_mut().project().stream;
|
||||||
|
|
||||||
let chunk = match ready!(Pin::new(stream).poll_next(cx)) {
|
let chunk = match ready!(stream.poll_next(cx)) {
|
||||||
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
|
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
|
||||||
opt => opt.map(|res| res.map_err(Into::into)),
|
opt => opt.map(|res| res.map_err(Into::into)),
|
||||||
};
|
};
|
||||||
|
@ -57,3 +61,49 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use actix_rt::pin;
|
||||||
|
use actix_utils::future::poll_fn;
|
||||||
|
use futures_util::stream;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::body::to_bytes;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn skips_empty_chunks() {
|
||||||
|
let body = BodyStream::new(stream::iter(
|
||||||
|
["1", "", "2"]
|
||||||
|
.iter()
|
||||||
|
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
|
||||||
|
));
|
||||||
|
pin!(body);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
poll_fn(|cx| body.as_mut().poll_next(cx))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.ok(),
|
||||||
|
Some(Bytes::from("1")),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
poll_fn(|cx| body.as_mut().poll_next(cx))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.ok(),
|
||||||
|
Some(Bytes::from("2")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn read_to_bytes() {
|
||||||
|
let body = BodyStream::new(stream::iter(
|
||||||
|
["1", "", "2"]
|
||||||
|
.iter()
|
||||||
|
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(to_bytes(body).await.ok(), Some(Bytes::from("12")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -52,6 +52,19 @@ impl<T: MessageBody + Unpin> MessageBody for Box<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: MessageBody> MessageBody for Pin<Box<T>> {
|
||||||
|
fn size(&self) -> BodySize {
|
||||||
|
self.as_ref().size()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_next(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
|
self.as_mut().poll_next(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MessageBody for Bytes {
|
impl MessageBody for Bytes {
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
BodySize::Sized(self.len() as u64)
|
BodySize::Sized(self.len() as u64)
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
//! Traits and structures to aid consuming and writing HTTP payloads.
|
//! Traits and structures to aid consuming and writing HTTP payloads.
|
||||||
|
|
||||||
|
use std::task::Poll;
|
||||||
|
|
||||||
|
use actix_rt::pin;
|
||||||
|
use actix_utils::future::poll_fn;
|
||||||
|
use bytes::{Bytes, BytesMut};
|
||||||
|
use futures_core::ready;
|
||||||
|
|
||||||
#[allow(clippy::module_inception)]
|
#[allow(clippy::module_inception)]
|
||||||
mod body;
|
mod body;
|
||||||
mod body_stream;
|
mod body_stream;
|
||||||
|
@ -15,6 +22,50 @@ pub use self::response_body::ResponseBody;
|
||||||
pub use self::size::BodySize;
|
pub use self::size::BodySize;
|
||||||
pub use self::sized_stream::SizedStream;
|
pub use self::sized_stream::SizedStream;
|
||||||
|
|
||||||
|
/// Collects the body produced by a `MessageBody` implementation into `Bytes`.
|
||||||
|
///
|
||||||
|
/// Any errors produced by the body stream are returned immediately.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// use actix_http::body::{Body, to_bytes};
|
||||||
|
/// use bytes::Bytes;
|
||||||
|
///
|
||||||
|
/// # async fn test_to_bytes() {
|
||||||
|
/// let body = Body::Empty;
|
||||||
|
/// let bytes = to_bytes(body).await.unwrap();
|
||||||
|
/// assert!(bytes.is_empty());
|
||||||
|
///
|
||||||
|
/// let body = Body::Bytes(Bytes::from_static(b"123"));
|
||||||
|
/// let bytes = to_bytes(body).await.unwrap();
|
||||||
|
/// assert_eq!(bytes, b"123"[..]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
pub async fn to_bytes(body: impl MessageBody) -> Result<Bytes, crate::Error> {
|
||||||
|
let cap = match body.size() {
|
||||||
|
BodySize::None | BodySize::Empty | BodySize::Sized(0) => return Ok(Bytes::new()),
|
||||||
|
BodySize::Sized(size) => size as usize,
|
||||||
|
BodySize::Stream => 32_768,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut buf = BytesMut::with_capacity(cap);
|
||||||
|
|
||||||
|
pin!(body);
|
||||||
|
|
||||||
|
poll_fn(|cx| loop {
|
||||||
|
let body = body.as_mut();
|
||||||
|
|
||||||
|
match ready!(body.poll_next(cx)) {
|
||||||
|
Some(Ok(bytes)) => buf.extend(bytes),
|
||||||
|
None => return Poll::Ready(Ok(())),
|
||||||
|
Some(Err(err)) => return Poll::Ready(Err(err)),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(buf.freeze())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
@ -22,7 +73,6 @@ mod tests {
|
||||||
use actix_rt::pin;
|
use actix_rt::pin;
|
||||||
use actix_utils::future::poll_fn;
|
use actix_utils::future::poll_fn;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_util::stream;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -174,69 +224,19 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_serde_json() {
|
async fn test_serde_json() {
|
||||||
use serde_json::json;
|
use serde_json::{json, Value};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Body::from(serde_json::Value::String("test".into())).size(),
|
Body::from(serde_json::to_vec(&Value::String("test".to_owned())).unwrap())
|
||||||
|
.size(),
|
||||||
BodySize::Sized(6)
|
BodySize::Sized(6)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Body::from(json!({"test-key":"test-value"})).size(),
|
Body::from(serde_json::to_vec(&json!({"test-key":"test-value"})).unwrap())
|
||||||
|
.size(),
|
||||||
BodySize::Sized(25)
|
BodySize::Sized(25)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn body_stream_skips_empty_chunks() {
|
|
||||||
let body = BodyStream::new(stream::iter(
|
|
||||||
["1", "", "2"]
|
|
||||||
.iter()
|
|
||||||
.map(|&v| Ok(Bytes::from(v)) as Result<Bytes, ()>),
|
|
||||||
));
|
|
||||||
pin!(body);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
poll_fn(|cx| body.as_mut().poll_next(cx))
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.ok(),
|
|
||||||
Some(Bytes::from("1")),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
poll_fn(|cx| body.as_mut().poll_next(cx))
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.ok(),
|
|
||||||
Some(Bytes::from("2")),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
mod sized_stream {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn skips_empty_chunks() {
|
|
||||||
let body = SizedStream::new(
|
|
||||||
2,
|
|
||||||
stream::iter(["1", "", "2"].iter().map(|&v| Ok(Bytes::from(v)))),
|
|
||||||
);
|
|
||||||
pin!(body);
|
|
||||||
assert_eq!(
|
|
||||||
poll_fn(|cx| body.as_mut().poll_next(cx))
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.ok(),
|
|
||||||
Some(Bytes::from("1")),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
poll_fn(|cx| body.as_mut().poll_next(cx))
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.ok(),
|
|
||||||
Some(Bytes::from("2")),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_casting() {
|
async fn test_body_casting() {
|
||||||
let mut body = String::from("hello cast");
|
let mut body = String::from("hello cast");
|
||||||
|
@ -250,4 +250,15 @@ mod tests {
|
||||||
let not_body = resp_body.downcast_ref::<()>();
|
let not_body = resp_body.downcast_ref::<()>();
|
||||||
assert!(not_body.is_none());
|
assert!(not_body.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_to_bytes() {
|
||||||
|
let body = Body::Empty;
|
||||||
|
let bytes = to_bytes(body).await.unwrap();
|
||||||
|
assert!(bytes.is_empty());
|
||||||
|
|
||||||
|
let body = Body::Bytes(Bytes::from_static(b"123"));
|
||||||
|
let bytes = to_bytes(body).await.unwrap();
|
||||||
|
assert_eq!(bytes, b"123"[..]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,10 +55,7 @@ impl<B: MessageBody> MessageBody for ResponseBody<B> {
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
match self.project() {
|
Stream::poll_next(self, cx)
|
||||||
ResponseBodyProj::Body(body) => body.poll_next(cx),
|
|
||||||
ResponseBodyProj::Other(body) => Pin::new(body).poll_next(cx),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,23 +5,27 @@ use std::{
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::{ready, Stream};
|
use futures_core::{ready, Stream};
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
use super::{BodySize, MessageBody};
|
use super::{BodySize, MessageBody};
|
||||||
|
|
||||||
|
pin_project! {
|
||||||
/// Known sized streaming response wrapper.
|
/// Known sized streaming response wrapper.
|
||||||
///
|
///
|
||||||
/// This body implementation should be used if total size of stream is known. Data get sent as is
|
/// This body implementation should be used if total size of stream is known. Data get sent as is
|
||||||
/// without using transfer encoding.
|
/// without using transfer encoding.
|
||||||
pub struct SizedStream<S: Unpin> {
|
pub struct SizedStream<S> {
|
||||||
size: u64,
|
size: u64,
|
||||||
|
#[pin]
|
||||||
stream: S,
|
stream: S,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<S> SizedStream<S>
|
impl<S> SizedStream<S>
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, Error>> + Unpin,
|
S: Stream<Item = Result<Bytes, Error>>,
|
||||||
{
|
{
|
||||||
pub fn new(size: u64, stream: S) -> Self {
|
pub fn new(size: u64, stream: S) -> Self {
|
||||||
SizedStream { size, stream }
|
SizedStream { size, stream }
|
||||||
|
@ -30,7 +34,7 @@ where
|
||||||
|
|
||||||
impl<S> MessageBody for SizedStream<S>
|
impl<S> MessageBody for SizedStream<S>
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, Error>> + Unpin,
|
S: Stream<Item = Result<Bytes, Error>>,
|
||||||
{
|
{
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
BodySize::Sized(self.size as u64)
|
BodySize::Sized(self.size as u64)
|
||||||
|
@ -46,9 +50,9 @@ where
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
loop {
|
loop {
|
||||||
let stream = &mut self.as_mut().stream;
|
let stream = self.as_mut().project().stream;
|
||||||
|
|
||||||
let chunk = match ready!(Pin::new(stream).poll_next(cx)) {
|
let chunk = match ready!(stream.poll_next(cx)) {
|
||||||
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
|
Some(Ok(ref bytes)) if bytes.is_empty() => continue,
|
||||||
val => val,
|
val => val,
|
||||||
};
|
};
|
||||||
|
@ -57,3 +61,49 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use actix_rt::pin;
|
||||||
|
use actix_utils::future::poll_fn;
|
||||||
|
use futures_util::stream;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::body::to_bytes;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn skips_empty_chunks() {
|
||||||
|
let body = SizedStream::new(
|
||||||
|
2,
|
||||||
|
stream::iter(["1", "", "2"].iter().map(|&v| Ok(Bytes::from(v)))),
|
||||||
|
);
|
||||||
|
|
||||||
|
pin!(body);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
poll_fn(|cx| body.as_mut().poll_next(cx))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.ok(),
|
||||||
|
Some(Bytes::from("1")),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
poll_fn(|cx| body.as_mut().poll_next(cx))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.ok(),
|
||||||
|
Some(Bytes::from("2")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn read_to_bytes() {
|
||||||
|
let body = SizedStream::new(
|
||||||
|
2,
|
||||||
|
stream::iter(["1", "", "2"].iter().map(|&v| Ok(Bytes::from(v)))),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(to_bytes(body).await.ok(), Some(Bytes::from("12")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ impl<B: MessageBody> Encoder<B> {
|
||||||
enum EncoderBody<B> {
|
enum EncoderBody<B> {
|
||||||
Bytes(Bytes),
|
Bytes(Bytes),
|
||||||
Stream(#[pin] B),
|
Stream(#[pin] B),
|
||||||
BoxedStream(Box<dyn MessageBody + Unpin>),
|
BoxedStream(Pin<Box<dyn MessageBody>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B: MessageBody> MessageBody for EncoderBody<B> {
|
impl<B: MessageBody> MessageBody for EncoderBody<B> {
|
||||||
|
@ -117,9 +117,7 @@ impl<B: MessageBody> MessageBody for EncoderBody<B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EncoderBodyProj::Stream(b) => b.poll_next(cx),
|
EncoderBodyProj::Stream(b) => b.poll_next(cx),
|
||||||
EncoderBodyProj::BoxedStream(ref mut b) => {
|
EncoderBodyProj::BoxedStream(ref mut b) => b.as_mut().poll_next(cx),
|
||||||
Pin::new(b.as_mut()).poll_next(cx)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,20 @@
|
||||||
//! Error and Result module
|
//! Error and Result module
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::{
|
||||||
use std::io::Write;
|
cell::RefCell,
|
||||||
use std::str::Utf8Error;
|
fmt,
|
||||||
use std::string::FromUtf8Error;
|
io::{self, Write as _},
|
||||||
use std::{fmt, io, result};
|
str::Utf8Error,
|
||||||
|
string::FromUtf8Error,
|
||||||
|
};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use derive_more::{Display, From};
|
use derive_more::{Display, Error, From};
|
||||||
use http::uri::InvalidUri;
|
use http::uri::InvalidUri;
|
||||||
use http::{header, Error as HttpError, StatusCode};
|
use http::{header, Error as HttpError, StatusCode};
|
||||||
use serde::de::value::Error as DeError;
|
use serde::de::value::Error as DeError;
|
||||||
use serde_json::error::Error as JsonError;
|
|
||||||
use serde_urlencoded::ser::Error as FormError;
|
|
||||||
|
|
||||||
use crate::body::Body;
|
use crate::{body::Body, helpers::Writer, Response, ResponseBuilder};
|
||||||
use crate::helpers::Writer;
|
|
||||||
use crate::response::{Response, ResponseBuilder};
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub use crate::cookie::ParseError as CookieParseError;
|
|
||||||
|
|
||||||
/// A specialized [`std::result::Result`]
|
/// A specialized [`std::result::Result`]
|
||||||
/// for actix web operations
|
/// for actix web operations
|
||||||
|
@ -27,7 +22,7 @@ pub use crate::cookie::ParseError as CookieParseError;
|
||||||
/// This typedef is generally used to avoid writing out
|
/// This typedef is generally used to avoid writing out
|
||||||
/// `actix_http::error::Error` directly and is otherwise a direct mapping to
|
/// `actix_http::error::Error` directly and is otherwise a direct mapping to
|
||||||
/// `Result`.
|
/// `Result`.
|
||||||
pub type Result<T, E = Error> = result::Result<T, E>;
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||||
|
|
||||||
/// General purpose actix web error.
|
/// General purpose actix web error.
|
||||||
///
|
///
|
||||||
|
@ -67,7 +62,7 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
|
||||||
/// Create response for error
|
/// Create response for error
|
||||||
///
|
///
|
||||||
/// Internal server error is generated by default.
|
/// Internal server error is generated by default.
|
||||||
fn error_response(&self) -> Response {
|
fn error_response(&self) -> Response<Body> {
|
||||||
let mut resp = Response::new(self.status_code());
|
let mut resp = Response::new(self.status_code());
|
||||||
let mut buf = BytesMut::new();
|
let mut buf = BytesMut::new();
|
||||||
let _ = write!(Writer(&mut buf), "{}", self);
|
let _ = write!(Writer(&mut buf), "{}", self);
|
||||||
|
@ -116,7 +111,7 @@ impl From<std::convert::Infallible> for Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert `Error` to a `Response` instance
|
/// Convert `Error` to a `Response` instance
|
||||||
impl From<Error> for Response {
|
impl From<Error> for Response<Body> {
|
||||||
fn from(err: Error) -> Self {
|
fn from(err: Error) -> Self {
|
||||||
Response::from_error(err)
|
Response::from_error(err)
|
||||||
}
|
}
|
||||||
|
@ -132,8 +127,8 @@ impl<T: ResponseError + 'static> From<T> for Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert Response to a Error
|
/// Convert Response to a Error
|
||||||
impl From<Response> for Error {
|
impl From<Response<Body>> for Error {
|
||||||
fn from(res: Response) -> Error {
|
fn from(res: Response<Body>) -> Error {
|
||||||
InternalError::from_response("", res).into()
|
InternalError::from_response("", res).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,14 +147,8 @@ struct UnitError;
|
||||||
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`UnitError`].
|
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`UnitError`].
|
||||||
impl ResponseError for UnitError {}
|
impl ResponseError for UnitError {}
|
||||||
|
|
||||||
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`JsonError`].
|
|
||||||
impl ResponseError for JsonError {}
|
|
||||||
|
|
||||||
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`FormError`].
|
|
||||||
impl ResponseError for FormError {}
|
|
||||||
|
|
||||||
#[cfg(feature = "openssl")]
|
|
||||||
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`actix_tls::accept::openssl::SslError`].
|
/// Returns [`StatusCode::INTERNAL_SERVER_ERROR`] for [`actix_tls::accept::openssl::SslError`].
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
impl ResponseError for actix_tls::accept::openssl::SslError {}
|
impl ResponseError for actix_tls::accept::openssl::SslError {}
|
||||||
|
|
||||||
/// Returns [`StatusCode::BAD_REQUEST`] for [`DeError`].
|
/// Returns [`StatusCode::BAD_REQUEST`] for [`DeError`].
|
||||||
|
@ -378,14 +367,6 @@ impl ResponseError for PayloadError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `cookie::ParseError`
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
impl ResponseError for crate::cookie::ParseError {
|
|
||||||
fn status_code(&self) -> StatusCode {
|
|
||||||
StatusCode::BAD_REQUEST
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
/// A set of errors that can occur during dispatching HTTP requests
|
/// A set of errors that can occur during dispatching HTTP requests
|
||||||
pub enum DispatchError {
|
pub enum DispatchError {
|
||||||
|
@ -434,18 +415,17 @@ pub enum DispatchError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of error that can occur during parsing content type
|
/// A set of error that can occur during parsing content type
|
||||||
#[derive(PartialEq, Debug, Display)]
|
#[derive(Debug, PartialEq, Display, Error)]
|
||||||
pub enum ContentTypeError {
|
pub enum ContentTypeError {
|
||||||
/// Can not parse content type
|
/// Can not parse content type
|
||||||
#[display(fmt = "Can not parse content type")]
|
#[display(fmt = "Can not parse content type")]
|
||||||
ParseError,
|
ParseError,
|
||||||
|
|
||||||
/// Unknown content encoding
|
/// Unknown content encoding
|
||||||
#[display(fmt = "Unknown content encoding")]
|
#[display(fmt = "Unknown content encoding")]
|
||||||
UnknownEncoding,
|
UnknownEncoding,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for ContentTypeError {}
|
|
||||||
|
|
||||||
/// Return `BadRequest` for `ContentTypeError`
|
/// Return `BadRequest` for `ContentTypeError`
|
||||||
impl ResponseError for ContentTypeError {
|
impl ResponseError for ContentTypeError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
|
@ -474,7 +454,7 @@ pub struct InternalError<T> {
|
||||||
|
|
||||||
enum InternalErrorType {
|
enum InternalErrorType {
|
||||||
Status(StatusCode),
|
Status(StatusCode),
|
||||||
Response(RefCell<Option<Response>>),
|
Response(RefCell<Option<Response<Body>>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> InternalError<T> {
|
impl<T> InternalError<T> {
|
||||||
|
@ -487,7 +467,7 @@ impl<T> InternalError<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `InternalError` with predefined `Response`.
|
/// Create `InternalError` with predefined `Response`.
|
||||||
pub fn from_response(cause: T, response: Response) -> Self {
|
pub fn from_response(cause: T, response: Response<Body>) -> Self {
|
||||||
InternalError {
|
InternalError {
|
||||||
cause,
|
cause,
|
||||||
status: InternalErrorType::Response(RefCell::new(Some(response))),
|
status: InternalErrorType::Response(RefCell::new(Some(response))),
|
||||||
|
@ -530,7 +510,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn error_response(&self) -> Response {
|
fn error_response(&self) -> Response<Body> {
|
||||||
match self.status {
|
match self.status {
|
||||||
InternalErrorType::Status(st) => {
|
InternalErrorType::Status(st) => {
|
||||||
let mut res = Response::new(st);
|
let mut res = Response::new(st);
|
||||||
|
@ -951,21 +931,14 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_response() {
|
fn test_into_response() {
|
||||||
let resp: Response = ParseError::Incomplete.error_response();
|
let resp: Response<Body> = ParseError::Incomplete.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
|
let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
|
||||||
let resp: Response = err.error_response();
|
let resp: Response<Body> = err.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[test]
|
|
||||||
fn test_cookie_parse() {
|
|
||||||
let resp: Response = CookieParseError::EmptyName.error_response();
|
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_as_response() {
|
fn test_as_response() {
|
||||||
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
||||||
|
@ -993,7 +966,7 @@ mod tests {
|
||||||
fn test_error_http_response() {
|
fn test_error_http_response() {
|
||||||
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
||||||
let e = Error::from(orig);
|
let e = Error::from(orig);
|
||||||
let resp: Response = e.into();
|
let resp: Response<Body> = e.into();
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1050,7 +1023,7 @@ mod tests {
|
||||||
fn test_internal_error() {
|
fn test_internal_error() {
|
||||||
let err =
|
let err =
|
||||||
InternalError::from_response(ParseError::Method, Response::Ok().into());
|
InternalError::from_response(ParseError::Method, Response::Ok().into());
|
||||||
let resp: Response = err.error_response();
|
let resp: Response<Body> = err.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1066,121 +1039,121 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_error_helpers() {
|
fn test_error_helpers() {
|
||||||
let r: Response = ErrorBadRequest("err").into();
|
let res: Response<Body> = ErrorBadRequest("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let r: Response = ErrorUnauthorized("err").into();
|
let res: Response<Body> = ErrorUnauthorized("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::UNAUTHORIZED);
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
||||||
|
|
||||||
let r: Response = ErrorPaymentRequired("err").into();
|
let res: Response<Body> = ErrorPaymentRequired("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::PAYMENT_REQUIRED);
|
assert_eq!(res.status(), StatusCode::PAYMENT_REQUIRED);
|
||||||
|
|
||||||
let r: Response = ErrorForbidden("err").into();
|
let res: Response<Body> = ErrorForbidden("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::FORBIDDEN);
|
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
||||||
|
|
||||||
let r: Response = ErrorNotFound("err").into();
|
let res: Response<Body> = ErrorNotFound("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::NOT_FOUND);
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
let r: Response = ErrorMethodNotAllowed("err").into();
|
let res: Response<Body> = ErrorMethodNotAllowed("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
|
|
||||||
let r: Response = ErrorNotAcceptable("err").into();
|
let res: Response<Body> = ErrorNotAcceptable("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::NOT_ACCEPTABLE);
|
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
|
|
||||||
let r: Response = ErrorProxyAuthenticationRequired("err").into();
|
let res: Response<Body> = ErrorProxyAuthenticationRequired("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::PROXY_AUTHENTICATION_REQUIRED);
|
assert_eq!(res.status(), StatusCode::PROXY_AUTHENTICATION_REQUIRED);
|
||||||
|
|
||||||
let r: Response = ErrorRequestTimeout("err").into();
|
let res: Response<Body> = ErrorRequestTimeout("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::REQUEST_TIMEOUT);
|
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
|
||||||
|
|
||||||
let r: Response = ErrorConflict("err").into();
|
let res: Response<Body> = ErrorConflict("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::CONFLICT);
|
assert_eq!(res.status(), StatusCode::CONFLICT);
|
||||||
|
|
||||||
let r: Response = ErrorGone("err").into();
|
let res: Response<Body> = ErrorGone("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::GONE);
|
assert_eq!(res.status(), StatusCode::GONE);
|
||||||
|
|
||||||
let r: Response = ErrorLengthRequired("err").into();
|
let res: Response<Body> = ErrorLengthRequired("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::LENGTH_REQUIRED);
|
assert_eq!(res.status(), StatusCode::LENGTH_REQUIRED);
|
||||||
|
|
||||||
let r: Response = ErrorPreconditionFailed("err").into();
|
let res: Response<Body> = ErrorPreconditionFailed("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::PRECONDITION_FAILED);
|
assert_eq!(res.status(), StatusCode::PRECONDITION_FAILED);
|
||||||
|
|
||||||
let r: Response = ErrorPayloadTooLarge("err").into();
|
let res: Response<Body> = ErrorPayloadTooLarge("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
|
|
||||||
let r: Response = ErrorUriTooLong("err").into();
|
let res: Response<Body> = ErrorUriTooLong("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::URI_TOO_LONG);
|
assert_eq!(res.status(), StatusCode::URI_TOO_LONG);
|
||||||
|
|
||||||
let r: Response = ErrorUnsupportedMediaType("err").into();
|
let res: Response<Body> = ErrorUnsupportedMediaType("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
assert_eq!(res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||||
|
|
||||||
let r: Response = ErrorRangeNotSatisfiable("err").into();
|
let res: Response<Body> = ErrorRangeNotSatisfiable("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
assert_eq!(res.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
||||||
|
|
||||||
let r: Response = ErrorExpectationFailed("err").into();
|
let res: Response<Body> = ErrorExpectationFailed("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::EXPECTATION_FAILED);
|
assert_eq!(res.status(), StatusCode::EXPECTATION_FAILED);
|
||||||
|
|
||||||
let r: Response = ErrorImATeapot("err").into();
|
let res: Response<Body> = ErrorImATeapot("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::IM_A_TEAPOT);
|
assert_eq!(res.status(), StatusCode::IM_A_TEAPOT);
|
||||||
|
|
||||||
let r: Response = ErrorMisdirectedRequest("err").into();
|
let res: Response<Body> = ErrorMisdirectedRequest("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::MISDIRECTED_REQUEST);
|
assert_eq!(res.status(), StatusCode::MISDIRECTED_REQUEST);
|
||||||
|
|
||||||
let r: Response = ErrorUnprocessableEntity("err").into();
|
let res: Response<Body> = ErrorUnprocessableEntity("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||||
|
|
||||||
let r: Response = ErrorLocked("err").into();
|
let res: Response<Body> = ErrorLocked("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::LOCKED);
|
assert_eq!(res.status(), StatusCode::LOCKED);
|
||||||
|
|
||||||
let r: Response = ErrorFailedDependency("err").into();
|
let res: Response<Body> = ErrorFailedDependency("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::FAILED_DEPENDENCY);
|
assert_eq!(res.status(), StatusCode::FAILED_DEPENDENCY);
|
||||||
|
|
||||||
let r: Response = ErrorUpgradeRequired("err").into();
|
let res: Response<Body> = ErrorUpgradeRequired("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::UPGRADE_REQUIRED);
|
assert_eq!(res.status(), StatusCode::UPGRADE_REQUIRED);
|
||||||
|
|
||||||
let r: Response = ErrorPreconditionRequired("err").into();
|
let res: Response<Body> = ErrorPreconditionRequired("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::PRECONDITION_REQUIRED);
|
assert_eq!(res.status(), StatusCode::PRECONDITION_REQUIRED);
|
||||||
|
|
||||||
let r: Response = ErrorTooManyRequests("err").into();
|
let res: Response<Body> = ErrorTooManyRequests("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::TOO_MANY_REQUESTS);
|
assert_eq!(res.status(), StatusCode::TOO_MANY_REQUESTS);
|
||||||
|
|
||||||
let r: Response = ErrorRequestHeaderFieldsTooLarge("err").into();
|
let res: Response<Body> = ErrorRequestHeaderFieldsTooLarge("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE);
|
assert_eq!(res.status(), StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE);
|
||||||
|
|
||||||
let r: Response = ErrorUnavailableForLegalReasons("err").into();
|
let res: Response<Body> = ErrorUnavailableForLegalReasons("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS);
|
assert_eq!(res.status(), StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS);
|
||||||
|
|
||||||
let r: Response = ErrorInternalServerError("err").into();
|
let res: Response<Body> = ErrorInternalServerError("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
let r: Response = ErrorNotImplemented("err").into();
|
let res: Response<Body> = ErrorNotImplemented("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::NOT_IMPLEMENTED);
|
assert_eq!(res.status(), StatusCode::NOT_IMPLEMENTED);
|
||||||
|
|
||||||
let r: Response = ErrorBadGateway("err").into();
|
let res: Response<Body> = ErrorBadGateway("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::BAD_GATEWAY);
|
assert_eq!(res.status(), StatusCode::BAD_GATEWAY);
|
||||||
|
|
||||||
let r: Response = ErrorServiceUnavailable("err").into();
|
let res: Response<Body> = ErrorServiceUnavailable("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::SERVICE_UNAVAILABLE);
|
assert_eq!(res.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
|
||||||
let r: Response = ErrorGatewayTimeout("err").into();
|
let res: Response<Body> = ErrorGatewayTimeout("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::GATEWAY_TIMEOUT);
|
assert_eq!(res.status(), StatusCode::GATEWAY_TIMEOUT);
|
||||||
|
|
||||||
let r: Response = ErrorHttpVersionNotSupported("err").into();
|
let res: Response<Body> = ErrorHttpVersionNotSupported("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
assert_eq!(res.status(), StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
||||||
|
|
||||||
let r: Response = ErrorVariantAlsoNegotiates("err").into();
|
let res: Response<Body> = ErrorVariantAlsoNegotiates("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::VARIANT_ALSO_NEGOTIATES);
|
assert_eq!(res.status(), StatusCode::VARIANT_ALSO_NEGOTIATES);
|
||||||
|
|
||||||
let r: Response = ErrorInsufficientStorage("err").into();
|
let res: Response<Body> = ErrorInsufficientStorage("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::INSUFFICIENT_STORAGE);
|
assert_eq!(res.status(), StatusCode::INSUFFICIENT_STORAGE);
|
||||||
|
|
||||||
let r: Response = ErrorLoopDetected("err").into();
|
let res: Response<Body> = ErrorLoopDetected("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::LOOP_DETECTED);
|
assert_eq!(res.status(), StatusCode::LOOP_DETECTED);
|
||||||
|
|
||||||
let r: Response = ErrorNotExtended("err").into();
|
let res: Response<Body> = ErrorNotExtended("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::NOT_EXTENDED);
|
assert_eq!(res.status(), StatusCode::NOT_EXTENDED);
|
||||||
|
|
||||||
let r: Response = ErrorNetworkAuthenticationRequired("err").into();
|
let res: Response<Body> = ErrorNetworkAuthenticationRequired("err").into();
|
||||||
assert_eq!(r.status(), StatusCode::NETWORK_AUTHENTICATION_REQUIRED);
|
assert_eq!(res.status(), StatusCode::NETWORK_AUTHENTICATION_REQUIRED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1213,8 +1213,9 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_chunked_payload_chunk_extension() {
|
fn test_parse_chunked_payload_chunk_extension() {
|
||||||
let mut buf = BytesMut::from(
|
let mut buf = BytesMut::from(
|
||||||
&"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
transfer-encoding: chunked\r\n\r\n"[..],
|
transfer-encoding: chunked\r\n\
|
||||||
|
\r\n",
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut reader = MessageDecoder::<Request>::default();
|
let mut reader = MessageDecoder::<Request>::default();
|
||||||
|
@ -1233,7 +1234,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_response_http10_read_until_eof() {
|
fn test_response_http10_read_until_eof() {
|
||||||
let mut buf = BytesMut::from(&"HTTP/1.0 200 Ok\r\n\r\ntest data"[..]);
|
let mut buf = BytesMut::from("HTTP/1.0 200 Ok\r\n\r\ntest data");
|
||||||
|
|
||||||
let mut reader = MessageDecoder::<ResponseHead>::default();
|
let mut reader = MessageDecoder::<ResponseHead>::default();
|
||||||
let (_msg, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
let (_msg, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
|
|
|
@ -351,7 +351,7 @@ where
|
||||||
|
|
||||||
// send service call error as response
|
// send service call error as response
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
let res: Response = err.into().into();
|
let res = Response::from_error(err.into());
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
self.as_mut().send_response(res, body.into_body())?;
|
self.as_mut().send_response(res, body.into_body())?;
|
||||||
}
|
}
|
||||||
|
@ -441,7 +441,7 @@ where
|
||||||
}
|
}
|
||||||
// send expect error as response
|
// send expect error as response
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
let res: Response = err.into().into();
|
let res = Response::from_error(err.into());
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
self.as_mut().send_response(res, body.into_body())?;
|
self.as_mut().send_response(res, body.into_body())?;
|
||||||
}
|
}
|
||||||
|
@ -490,8 +490,7 @@ where
|
||||||
// to notify the dispatcher a new state is set and the outer loop
|
// to notify the dispatcher a new state is set and the outer loop
|
||||||
// should be continue.
|
// should be continue.
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
let err = err.into();
|
let res = Response::from_error(err.into());
|
||||||
let res: Response = err.into();
|
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
return self.send_response(res, body.into_body());
|
return self.send_response(res, body.into_body());
|
||||||
}
|
}
|
||||||
|
@ -511,7 +510,7 @@ where
|
||||||
Poll::Pending => Ok(()),
|
Poll::Pending => Ok(()),
|
||||||
// see the comment on ExpectCall state branch's Ready(Err(err)).
|
// see the comment on ExpectCall state branch's Ready(Err(err)).
|
||||||
Poll::Ready(Err(err)) => {
|
Poll::Ready(Err(err)) => {
|
||||||
let res: Response = err.into().into();
|
let res = Response::from_error(err.into());
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
self.send_response(res, body.into_body())
|
self.send_response(res, body.into_body())
|
||||||
}
|
}
|
||||||
|
@ -1031,11 +1030,11 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ok_service() -> impl Service<Request, Response = Response, Error = Error> {
|
fn ok_service() -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
||||||
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::Ok().finish())))
|
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::Ok().finish())))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pending_service() -> impl Service<Request, Response = Response, Error = Error> {
|
fn pending_service() -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
||||||
struct PendingForever;
|
struct PendingForever;
|
||||||
impl futures_core::Stream for PendingForever {
|
impl futures_core::Stream for PendingForever {
|
||||||
type Item = Result<Bytes, Error>;
|
type Item = Result<Bytes, Error>;
|
||||||
|
@ -1053,15 +1052,16 @@ mod tests {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn echo_path_service() -> impl Service<Request, Response = Response, Error = Error> {
|
fn echo_path_service(
|
||||||
|
) -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
||||||
fn_service(|req: Request| {
|
fn_service(|req: Request| {
|
||||||
let path = req.path().as_bytes();
|
let path = req.path().as_bytes();
|
||||||
ready(Ok::<_, Error>(Response::Ok().body(Body::from_slice(path))))
|
ready(Ok::<_, Error>(Response::Ok().body(Body::from_slice(path))))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn echo_payload_service() -> impl Service<Request, Response = Response, Error = Error>
|
fn echo_payload_service(
|
||||||
{
|
) -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
||||||
fn_service(|mut req: Request| {
|
fn_service(|mut req: Request| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
use futures_util::stream::StreamExt as _;
|
use futures_util::stream::StreamExt as _;
|
||||||
|
|
|
@ -252,8 +252,8 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(e) => {
|
Err(err) => {
|
||||||
let res: Response = e.into().into();
|
let res = Response::from_error(err.into());
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
|
|
||||||
let mut send = send.take().unwrap();
|
let mut send = send.take().unwrap();
|
||||||
|
|
|
@ -4,7 +4,10 @@
|
||||||
|
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
|
||||||
use crate::response::{Response, ResponseBuilder};
|
use crate::{
|
||||||
|
body::Body,
|
||||||
|
response::{Response, ResponseBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
macro_rules! static_resp {
|
macro_rules! static_resp {
|
||||||
($name:ident, $status:expr) => {
|
($name:ident, $status:expr) => {
|
||||||
|
@ -15,7 +18,7 @@ macro_rules! static_resp {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response<Body> {
|
||||||
static_resp!(Continue, StatusCode::CONTINUE);
|
static_resp!(Continue, StatusCode::CONTINUE);
|
||||||
static_resp!(SwitchingProtocols, StatusCode::SWITCHING_PROTOCOLS);
|
static_resp!(SwitchingProtocols, StatusCode::SWITCHING_PROTOCOLS);
|
||||||
static_resp!(Processing, StatusCode::PROCESSING);
|
static_resp!(Processing, StatusCode::PROCESSING);
|
||||||
|
|
|
@ -9,11 +9,6 @@ use crate::error::{ContentTypeError, ParseError};
|
||||||
use crate::extensions::Extensions;
|
use crate::extensions::Extensions;
|
||||||
use crate::header::{Header, HeaderMap};
|
use crate::header::{Header, HeaderMap};
|
||||||
use crate::payload::Payload;
|
use crate::payload::Payload;
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use crate::{cookie::Cookie, error::CookieParseError};
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
struct Cookies(Vec<Cookie<'static>>);
|
|
||||||
|
|
||||||
/// Trait that implements general purpose operations on HTTP messages.
|
/// Trait that implements general purpose operations on HTTP messages.
|
||||||
pub trait HttpMessage: Sized {
|
pub trait HttpMessage: Sized {
|
||||||
|
@ -104,41 +99,6 @@ pub trait HttpMessage: Sized {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load request cookies.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
|
||||||
if self.extensions().get::<Cookies>().is_none() {
|
|
||||||
let mut cookies = Vec::new();
|
|
||||||
for hdr in self.headers().get_all(header::COOKIE) {
|
|
||||||
let s =
|
|
||||||
str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
|
||||||
for cookie_str in s.split(';').map(|s| s.trim()) {
|
|
||||||
if !cookie_str.is_empty() {
|
|
||||||
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.extensions_mut().insert(Cookies(cookies));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Ref::map(self.extensions(), |ext| {
|
|
||||||
&ext.get::<Cookies>().unwrap().0
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return request cookie.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
|
||||||
if let Ok(cookies) = self.cookies() {
|
|
||||||
for cookie in cookies.iter() {
|
|
||||||
if cookie.name() == name {
|
|
||||||
return Some(cookie.to_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> HttpMessage for &'a mut T
|
impl<'a, T> HttpMessage for &'a mut T
|
||||||
|
|
|
@ -6,13 +6,10 @@
|
||||||
//! | `openssl` | TLS support via [OpenSSL]. |
|
//! | `openssl` | TLS support via [OpenSSL]. |
|
||||||
//! | `rustls` | TLS support via [rustls]. |
|
//! | `rustls` | TLS support via [rustls]. |
|
||||||
//! | `compress` | Payload compression support. (Deflate, Gzip & Brotli) |
|
//! | `compress` | Payload compression support. (Deflate, Gzip & Brotli) |
|
||||||
//! | `cookies` | Support for cookies backed by the [cookie] crate. |
|
|
||||||
//! | `secure-cookies` | Adds for secure cookies. Enables `cookies` feature. |
|
|
||||||
//! | `trust-dns` | Use [trust-dns] as the client DNS resolver. |
|
//! | `trust-dns` | Use [trust-dns] as the client DNS resolver. |
|
||||||
//!
|
//!
|
||||||
//! [OpenSSL]: https://crates.io/crates/openssl
|
//! [OpenSSL]: https://crates.io/crates/openssl
|
||||||
//! [rustls]: https://crates.io/crates/rustls
|
//! [rustls]: https://crates.io/crates/rustls
|
||||||
//! [cookie]: https://crates.io/crates/cookie
|
|
||||||
//! [trust-dns]: https://crates.io/crates/trust-dns
|
//! [trust-dns]: https://crates.io/crates/trust-dns
|
||||||
|
|
||||||
#![deny(rust_2018_idioms, nonstandard_style)]
|
#![deny(rust_2018_idioms, nonstandard_style)]
|
||||||
|
@ -55,9 +52,6 @@ pub mod h2;
|
||||||
pub mod test;
|
pub mod test;
|
||||||
pub mod ws;
|
pub mod ws;
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub use cookie;
|
|
||||||
|
|
||||||
pub use self::builder::HttpServiceBuilder;
|
pub use self::builder::HttpServiceBuilder;
|
||||||
pub use self::config::{KeepAlive, ServiceConfig};
|
pub use self::config::{KeepAlive, ServiceConfig};
|
||||||
pub use self::error::{Error, ResponseError, Result};
|
pub use self::error::{Error, ResponseError, Result};
|
||||||
|
@ -78,8 +72,6 @@ pub mod http {
|
||||||
pub use http::{uri, Error, Uri};
|
pub use http::{uri, Error, Uri};
|
||||||
pub use http::{Method, StatusCode, Version};
|
pub use http::{Method, StatusCode, Version};
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub use crate::cookie::{Cookie, CookieBuilder};
|
|
||||||
pub use crate::header::HeaderMap;
|
pub use crate::header::HeaderMap;
|
||||||
|
|
||||||
/// A collection of HTTP headers and helpers.
|
/// A collection of HTTP headers and helpers.
|
||||||
|
|
|
@ -70,6 +70,7 @@ macro_rules! downcast {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
#![allow(clippy::upper_case_acronyms)]
|
||||||
|
|
||||||
trait MB {
|
trait MB {
|
||||||
downcast_get_type_id!();
|
downcast_get_type_id!();
|
||||||
|
|
|
@ -345,8 +345,8 @@ impl ResponseHead {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Message<T: Head> {
|
pub struct Message<T: Head> {
|
||||||
// Rc here should not be cloned by anyone.
|
/// Rc here should not be cloned by anyone.
|
||||||
// It's used to reuse allocation of T and no shared ownership is allowed.
|
/// It's used to reuse allocation of T and no shared ownership is allowed.
|
||||||
head: Rc<T>,
|
head: Rc<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,16 +2,18 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Ref, RefMut},
|
cell::{Ref, RefMut},
|
||||||
fmt, net,
|
fmt, net, str,
|
||||||
};
|
};
|
||||||
|
|
||||||
use http::{header, Method, Uri, Version};
|
use http::{header, Method, Uri, Version};
|
||||||
|
|
||||||
use crate::extensions::Extensions;
|
use crate::{
|
||||||
use crate::header::HeaderMap;
|
extensions::Extensions,
|
||||||
use crate::message::{Message, RequestHead};
|
header::HeaderMap,
|
||||||
use crate::payload::{Payload, PayloadStream};
|
message::{Message, RequestHead},
|
||||||
use crate::HttpMessage;
|
payload::{Payload, PayloadStream},
|
||||||
|
HttpMessage,
|
||||||
|
};
|
||||||
|
|
||||||
/// Request
|
/// Request
|
||||||
pub struct Request<P = PayloadStream> {
|
pub struct Request<P = PayloadStream> {
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Ref, RefMut},
|
cell::{Ref, RefMut},
|
||||||
convert::TryInto,
|
|
||||||
fmt,
|
fmt,
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
|
@ -12,23 +11,18 @@ use std::{
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
use crate::body::{Body, BodyStream, MessageBody, ResponseBody};
|
|
||||||
use crate::error::Error;
|
|
||||||
use crate::extensions::Extensions;
|
|
||||||
use crate::header::{IntoHeaderPair, IntoHeaderValue};
|
|
||||||
use crate::http::header::{self, HeaderName};
|
|
||||||
use crate::http::{Error as HttpError, HeaderMap, StatusCode};
|
|
||||||
use crate::message::{BoxedResponseHead, ConnectionType, ResponseHead};
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cookie::{Cookie, CookieJar},
|
body::{Body, BodyStream, MessageBody, ResponseBody},
|
||||||
http::header::HeaderValue,
|
error::Error,
|
||||||
|
extensions::Extensions,
|
||||||
|
header::{IntoHeaderPair, IntoHeaderValue},
|
||||||
|
http::{header, Error as HttpError, HeaderMap, StatusCode},
|
||||||
|
message::{BoxedResponseHead, ConnectionType, ResponseHead},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An HTTP Response
|
/// An HTTP Response
|
||||||
pub struct Response<B = Body> {
|
pub struct Response<B> {
|
||||||
head: BoxedResponseHead,
|
head: BoxedResponseHead,
|
||||||
body: ResponseBody<B>,
|
body: ResponseBody<B>,
|
||||||
error: Option<Error>,
|
error: Option<Error>,
|
||||||
|
@ -49,7 +43,7 @@ impl Response<Body> {
|
||||||
|
|
||||||
/// Constructs a response
|
/// Constructs a response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(status: StatusCode) -> Response {
|
pub fn new(status: StatusCode) -> Response<Body> {
|
||||||
Response {
|
Response {
|
||||||
head: BoxedResponseHead::new(status),
|
head: BoxedResponseHead::new(status),
|
||||||
body: ResponseBody::Body(Body::Empty),
|
body: ResponseBody::Body(Body::Empty),
|
||||||
|
@ -59,7 +53,7 @@ impl Response<Body> {
|
||||||
|
|
||||||
/// Constructs an error response
|
/// Constructs an error response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_error(error: Error) -> Response {
|
pub fn from_error(error: Error) -> Response<Body> {
|
||||||
let mut resp = error.as_response_error().error_response();
|
let mut resp = error.as_response_error().error_response();
|
||||||
if resp.head.status == StatusCode::INTERNAL_SERVER_ERROR {
|
if resp.head.status == StatusCode::INTERNAL_SERVER_ERROR {
|
||||||
error!("Internal Server Error: {:?}", error);
|
error!("Internal Server Error: {:?}", error);
|
||||||
|
@ -135,54 +129,6 @@ impl<B> Response<B> {
|
||||||
&mut self.head.headers
|
&mut self.head.headers
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get an iterator for the cookies set by this response
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[inline]
|
|
||||||
pub fn cookies(&self) -> CookieIter<'_> {
|
|
||||||
CookieIter {
|
|
||||||
iter: self.head.headers.get_all(header::SET_COOKIE),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add a cookie to this response
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[inline]
|
|
||||||
pub fn add_cookie(&mut self, cookie: &Cookie<'_>) -> Result<(), HttpError> {
|
|
||||||
let h = &mut self.head.headers;
|
|
||||||
HeaderValue::from_str(&cookie.to_string())
|
|
||||||
.map(|c| {
|
|
||||||
h.append(header::SET_COOKIE, c);
|
|
||||||
})
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove all cookies with the given name from this response. Returns
|
|
||||||
/// the number of cookies removed.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[inline]
|
|
||||||
pub fn del_cookie(&mut self, name: &str) -> usize {
|
|
||||||
let h = &mut self.head.headers;
|
|
||||||
let vals: Vec<HeaderValue> = h
|
|
||||||
.get_all(header::SET_COOKIE)
|
|
||||||
.map(|v| v.to_owned())
|
|
||||||
.collect();
|
|
||||||
h.remove(header::SET_COOKIE);
|
|
||||||
|
|
||||||
let mut count: usize = 0;
|
|
||||||
for v in vals {
|
|
||||||
if let Ok(s) = v.to_str() {
|
|
||||||
if let Ok(c) = Cookie::parse_encoded(s) {
|
|
||||||
if c.name() == name {
|
|
||||||
count += 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.append(header::SET_COOKIE, v);
|
|
||||||
}
|
|
||||||
count
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connection upgrade status
|
/// Connection upgrade status
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn upgrade(&self) -> bool {
|
pub fn upgrade(&self) -> bool {
|
||||||
|
@ -292,8 +238,8 @@ impl<B: MessageBody> fmt::Debug for Response<B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for Response {
|
impl<B: Unpin> Future for Response<B> {
|
||||||
type Output = Result<Response, Error>;
|
type Output = Result<Response<B>, Error>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
Poll::Ready(Ok(Response {
|
Poll::Ready(Ok(Response {
|
||||||
|
@ -304,34 +250,12 @@ impl Future for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub struct CookieIter<'a> {
|
|
||||||
iter: header::GetAll<'a>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
impl<'a> Iterator for CookieIter<'a> {
|
|
||||||
type Item = Cookie<'a>;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn next(&mut self) -> Option<Cookie<'a>> {
|
|
||||||
for v in self.iter.by_ref() {
|
|
||||||
if let Ok(c) = Cookie::parse_encoded(v.to_str().ok()?) {
|
|
||||||
return Some(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An HTTP response builder.
|
/// An HTTP response builder.
|
||||||
///
|
///
|
||||||
/// This type can be used to construct an instance of `Response` through a builder-like pattern.
|
/// This type can be used to construct an instance of `Response` through a builder-like pattern.
|
||||||
pub struct ResponseBuilder {
|
pub struct ResponseBuilder {
|
||||||
head: Option<BoxedResponseHead>,
|
head: Option<BoxedResponseHead>,
|
||||||
err: Option<HttpError>,
|
err: Option<HttpError>,
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: Option<CookieJar>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResponseBuilder {
|
impl ResponseBuilder {
|
||||||
|
@ -341,8 +265,6 @@ impl ResponseBuilder {
|
||||||
ResponseBuilder {
|
ResponseBuilder {
|
||||||
head: Some(BoxedResponseHead::new(status)),
|
head: Some(BoxedResponseHead::new(status)),
|
||||||
err: None,
|
err: None,
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,48 +330,6 @@ impl ResponseBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replaced with [`Self::insert_header()`].
|
|
||||||
#[deprecated = "Replaced with `insert_header((key, value))`."]
|
|
||||||
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
|
||||||
where
|
|
||||||
K: TryInto<HeaderName>,
|
|
||||||
K::Error: Into<HttpError>,
|
|
||||||
V: IntoHeaderValue,
|
|
||||||
{
|
|
||||||
if self.err.is_some() {
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
match (key.try_into(), value.try_into_value()) {
|
|
||||||
(Ok(name), Ok(value)) => return self.insert_header((name, value)),
|
|
||||||
(Err(err), _) => self.err = Some(err.into()),
|
|
||||||
(_, Err(err)) => self.err = Some(err.into()),
|
|
||||||
}
|
|
||||||
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replaced with [`Self::append_header()`].
|
|
||||||
#[deprecated = "Replaced with `append_header((key, value))`."]
|
|
||||||
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
|
||||||
where
|
|
||||||
K: TryInto<HeaderName>,
|
|
||||||
K::Error: Into<HttpError>,
|
|
||||||
V: IntoHeaderValue,
|
|
||||||
{
|
|
||||||
if self.err.is_some() {
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
match (key.try_into(), value.try_into_value()) {
|
|
||||||
(Ok(name), Ok(value)) => return self.append_header((name, value)),
|
|
||||||
(Err(err), _) => self.err = Some(err.into()),
|
|
||||||
(_, Err(err)) => self.err = Some(err.into()),
|
|
||||||
}
|
|
||||||
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set the custom reason for the response.
|
/// Set the custom reason for the response.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn reason(&mut self, reason: &'static str) -> &mut Self {
|
pub fn reason(&mut self, reason: &'static str) -> &mut Self {
|
||||||
|
@ -523,89 +403,6 @@ impl ResponseBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a cookie
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use actix_http::{http, Request, Response};
|
|
||||||
///
|
|
||||||
/// fn index(req: Request) -> Response {
|
|
||||||
/// Response::Ok()
|
|
||||||
/// .cookie(
|
|
||||||
/// http::Cookie::build("name", "value")
|
|
||||||
/// .domain("www.rust-lang.org")
|
|
||||||
/// .path("/")
|
|
||||||
/// .secure(true)
|
|
||||||
/// .http_only(true)
|
|
||||||
/// .finish(),
|
|
||||||
/// )
|
|
||||||
/// .finish()
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
|
|
||||||
if self.cookies.is_none() {
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
jar.add(cookie.into_owned());
|
|
||||||
self.cookies = Some(jar)
|
|
||||||
} else {
|
|
||||||
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove cookie
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use actix_http::{http, Request, Response, HttpMessage};
|
|
||||||
///
|
|
||||||
/// fn index(req: Request) -> Response {
|
|
||||||
/// let mut builder = Response::Ok();
|
|
||||||
///
|
|
||||||
/// if let Some(ref cookie) = req.cookie("name") {
|
|
||||||
/// builder.del_cookie(cookie);
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// builder.finish()
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub fn del_cookie<'a>(&mut self, cookie: &Cookie<'a>) -> &mut Self {
|
|
||||||
if self.cookies.is_none() {
|
|
||||||
self.cookies = Some(CookieJar::new())
|
|
||||||
}
|
|
||||||
let jar = self.cookies.as_mut().unwrap();
|
|
||||||
let cookie = cookie.clone().into_owned();
|
|
||||||
jar.add_original(cookie.clone());
|
|
||||||
jar.remove(cookie);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method calls provided closure with builder reference if value is `true`.
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[deprecated = "Use an if statement."]
|
|
||||||
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
|
|
||||||
where
|
|
||||||
F: FnOnce(&mut ResponseBuilder),
|
|
||||||
{
|
|
||||||
if value {
|
|
||||||
f(self);
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method calls provided closure with builder reference if value is `Some`.
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[deprecated = "Use an if-let construction."]
|
|
||||||
pub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Self
|
|
||||||
where
|
|
||||||
F: FnOnce(T, &mut ResponseBuilder),
|
|
||||||
{
|
|
||||||
if let Some(val) = value {
|
|
||||||
f(val, self);
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Responses extensions
|
/// Responses extensions
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
||||||
|
@ -620,11 +417,11 @@ impl ResponseBuilder {
|
||||||
head.extensions.borrow_mut()
|
head.extensions.borrow_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Set a body and generate `Response`.
|
/// Set a body and generate `Response`.
|
||||||
///
|
///
|
||||||
/// `ResponseBuilder` can not be used after this call.
|
/// `ResponseBuilder` can not be used after this call.
|
||||||
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response {
|
#[inline]
|
||||||
|
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response<Body> {
|
||||||
self.message_body(body.into())
|
self.message_body(body.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -636,19 +433,7 @@ impl ResponseBuilder {
|
||||||
return Response::from(Error::from(e)).into_body();
|
return Response::from(Error::from(e)).into_body();
|
||||||
}
|
}
|
||||||
|
|
||||||
// allow unused mut when cookies feature is disabled
|
let response = self.head.take().expect("cannot reuse response builder");
|
||||||
#[allow(unused_mut)]
|
|
||||||
let mut response = self.head.take().expect("cannot reuse response builder");
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
if let Some(ref jar) = self.cookies {
|
|
||||||
for cookie in jar.delta() {
|
|
||||||
match HeaderValue::from_str(&cookie.to_string()) {
|
|
||||||
Ok(val) => response.headers.append(header::SET_COOKIE, val),
|
|
||||||
Err(e) => return Response::from(Error::from(e)).into_body(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Response {
|
Response {
|
||||||
head: response,
|
head: response,
|
||||||
|
@ -657,11 +442,11 @@ impl ResponseBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Set a streaming body and generate `Response`.
|
/// Set a streaming body and generate `Response`.
|
||||||
///
|
///
|
||||||
/// `ResponseBuilder` can not be used after this call.
|
/// `ResponseBuilder` can not be used after this call.
|
||||||
pub fn streaming<S, E>(&mut self, stream: S) -> Response
|
#[inline]
|
||||||
|
pub fn streaming<S, E>(&mut self, stream: S) -> Response<Body>
|
||||||
where
|
where
|
||||||
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
||||||
E: Into<Error> + 'static,
|
E: Into<Error> + 'static,
|
||||||
|
@ -669,33 +454,11 @@ impl ResponseBuilder {
|
||||||
self.body(Body::from_message(BodyStream::new(stream)))
|
self.body(Body::from_message(BodyStream::new(stream)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a json body and generate `Response`
|
|
||||||
///
|
|
||||||
/// `ResponseBuilder` can not be used after this call.
|
|
||||||
pub fn json(&mut self, value: impl Serialize) -> Response {
|
|
||||||
match serde_json::to_string(&value) {
|
|
||||||
Ok(body) => {
|
|
||||||
let contains = if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
||||||
parts.headers.contains_key(header::CONTENT_TYPE)
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
};
|
|
||||||
|
|
||||||
if !contains {
|
|
||||||
self.insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.body(Body::from(body))
|
|
||||||
}
|
|
||||||
Err(e) => Error::from(e).into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Set an empty body and generate `Response`
|
/// Set an empty body and generate `Response`
|
||||||
///
|
///
|
||||||
/// `ResponseBuilder` can not be used after this call.
|
/// `ResponseBuilder` can not be used after this call.
|
||||||
pub fn finish(&mut self) -> Response {
|
#[inline]
|
||||||
|
pub fn finish(&mut self) -> Response<Body> {
|
||||||
self.body(Body::Empty)
|
self.body(Body::Empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -704,8 +467,6 @@ impl ResponseBuilder {
|
||||||
ResponseBuilder {
|
ResponseBuilder {
|
||||||
head: self.head.take(),
|
head: self.head.take(),
|
||||||
err: self.err.take(),
|
err: self.err.take(),
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: self.cookies.take(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -724,29 +485,9 @@ fn parts<'a>(
|
||||||
/// Convert `Response` to a `ResponseBuilder`. Body get dropped.
|
/// Convert `Response` to a `ResponseBuilder`. Body get dropped.
|
||||||
impl<B> From<Response<B>> for ResponseBuilder {
|
impl<B> From<Response<B>> for ResponseBuilder {
|
||||||
fn from(res: Response<B>) -> ResponseBuilder {
|
fn from(res: Response<B>) -> ResponseBuilder {
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
let jar = {
|
|
||||||
// If this response has cookies, load them into a jar
|
|
||||||
let mut jar: Option<CookieJar> = None;
|
|
||||||
|
|
||||||
for c in res.cookies() {
|
|
||||||
if let Some(ref mut j) = jar {
|
|
||||||
j.add_original(c.into_owned());
|
|
||||||
} else {
|
|
||||||
let mut j = CookieJar::new();
|
|
||||||
j.add_original(c.into_owned());
|
|
||||||
jar = Some(j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jar
|
|
||||||
};
|
|
||||||
|
|
||||||
ResponseBuilder {
|
ResponseBuilder {
|
||||||
head: Some(res.head),
|
head: Some(res.head),
|
||||||
err: None,
|
err: None,
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: jar,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -764,39 +505,15 @@ impl<'a> From<&'a ResponseHead> for ResponseBuilder {
|
||||||
|
|
||||||
msg.no_chunking(!head.chunked());
|
msg.no_chunking(!head.chunked());
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
let jar = {
|
|
||||||
// If this response has cookies, load them into a jar
|
|
||||||
let mut jar: Option<CookieJar> = None;
|
|
||||||
|
|
||||||
let cookies = CookieIter {
|
|
||||||
iter: head.headers.get_all(header::SET_COOKIE),
|
|
||||||
};
|
|
||||||
|
|
||||||
for c in cookies {
|
|
||||||
if let Some(ref mut j) = jar {
|
|
||||||
j.add_original(c.into_owned());
|
|
||||||
} else {
|
|
||||||
let mut j = CookieJar::new();
|
|
||||||
j.add_original(c.into_owned());
|
|
||||||
jar = Some(j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jar
|
|
||||||
};
|
|
||||||
|
|
||||||
ResponseBuilder {
|
ResponseBuilder {
|
||||||
head: Some(msg),
|
head: Some(msg),
|
||||||
err: None,
|
err: None,
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: jar,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for ResponseBuilder {
|
impl Future for ResponseBuilder {
|
||||||
type Output = Result<Response, Error>;
|
type Output = Result<Response<Body>, Error>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
Poll::Ready(Ok(self.finish()))
|
Poll::Ready(Ok(self.finish()))
|
||||||
|
@ -823,7 +540,7 @@ impl fmt::Debug for ResponseBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper converters
|
/// Helper converters
|
||||||
impl<I: Into<Response>, E: Into<Error>> From<Result<I, E>> for Response {
|
impl<I: Into<Response<Body>>, E: Into<Error>> From<Result<I, E>> for Response<Body> {
|
||||||
fn from(res: Result<I, E>) -> Self {
|
fn from(res: Result<I, E>) -> Self {
|
||||||
match res {
|
match res {
|
||||||
Ok(val) => val.into(),
|
Ok(val) => val.into(),
|
||||||
|
@ -832,13 +549,13 @@ impl<I: Into<Response>, E: Into<Error>> From<Result<I, E>> for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ResponseBuilder> for Response {
|
impl From<ResponseBuilder> for Response<Body> {
|
||||||
fn from(mut builder: ResponseBuilder) -> Self {
|
fn from(mut builder: ResponseBuilder) -> Self {
|
||||||
builder.finish()
|
builder.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static str> for Response {
|
impl From<&'static str> for Response<Body> {
|
||||||
fn from(val: &'static str) -> Self {
|
fn from(val: &'static str) -> Self {
|
||||||
Response::Ok()
|
Response::Ok()
|
||||||
.content_type(mime::TEXT_PLAIN_UTF_8)
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
||||||
|
@ -846,7 +563,7 @@ impl From<&'static str> for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static [u8]> for Response {
|
impl From<&'static [u8]> for Response<Body> {
|
||||||
fn from(val: &'static [u8]) -> Self {
|
fn from(val: &'static [u8]) -> Self {
|
||||||
Response::Ok()
|
Response::Ok()
|
||||||
.content_type(mime::APPLICATION_OCTET_STREAM)
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
||||||
|
@ -854,7 +571,7 @@ impl From<&'static [u8]> for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Response {
|
impl From<String> for Response<Body> {
|
||||||
fn from(val: String) -> Self {
|
fn from(val: String) -> Self {
|
||||||
Response::Ok()
|
Response::Ok()
|
||||||
.content_type(mime::TEXT_PLAIN_UTF_8)
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
||||||
|
@ -862,7 +579,7 @@ impl From<String> for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a String> for Response {
|
impl<'a> From<&'a String> for Response<Body> {
|
||||||
fn from(val: &'a String) -> Self {
|
fn from(val: &'a String) -> Self {
|
||||||
Response::Ok()
|
Response::Ok()
|
||||||
.content_type(mime::TEXT_PLAIN_UTF_8)
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
||||||
|
@ -870,7 +587,7 @@ impl<'a> From<&'a String> for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Bytes> for Response {
|
impl From<Bytes> for Response<Body> {
|
||||||
fn from(val: Bytes) -> Self {
|
fn from(val: Bytes) -> Self {
|
||||||
Response::Ok()
|
Response::Ok()
|
||||||
.content_type(mime::APPLICATION_OCTET_STREAM)
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
||||||
|
@ -878,7 +595,7 @@ impl From<Bytes> for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BytesMut> for Response {
|
impl From<BytesMut> for Response<Body> {
|
||||||
fn from(val: BytesMut) -> Self {
|
fn from(val: BytesMut) -> Self {
|
||||||
Response::Ok()
|
Response::Ok()
|
||||||
.content_type(mime::APPLICATION_OCTET_STREAM)
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
||||||
|
@ -888,13 +605,9 @@ impl From<BytesMut> for Response {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::body::Body;
|
use crate::body::Body;
|
||||||
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
|
use crate::http::header::{HeaderName, HeaderValue, CONTENT_TYPE, COOKIE};
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use crate::{http::header::SET_COOKIE, HttpMessage};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug() {
|
fn test_debug() {
|
||||||
|
@ -906,68 +619,6 @@ mod tests {
|
||||||
assert!(dbg.contains("Response"));
|
assert!(dbg.contains("Response"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[test]
|
|
||||||
fn test_response_cookies() {
|
|
||||||
let req = crate::test::TestRequest::default()
|
|
||||||
.append_header((COOKIE, "cookie1=value1"))
|
|
||||||
.append_header((COOKIE, "cookie2=value2"))
|
|
||||||
.finish();
|
|
||||||
let cookies = req.cookies().unwrap();
|
|
||||||
|
|
||||||
let resp = Response::Ok()
|
|
||||||
.cookie(
|
|
||||||
crate::http::Cookie::build("name", "value")
|
|
||||||
.domain("www.rust-lang.org")
|
|
||||||
.path("/test")
|
|
||||||
.http_only(true)
|
|
||||||
.max_age(time::Duration::days(1))
|
|
||||||
.finish(),
|
|
||||||
)
|
|
||||||
.del_cookie(&cookies[0])
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
let mut val = resp
|
|
||||||
.headers()
|
|
||||||
.get_all(SET_COOKIE)
|
|
||||||
.map(|v| v.to_str().unwrap().to_owned())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
val.sort();
|
|
||||||
|
|
||||||
// the .del_cookie call
|
|
||||||
assert!(val[0].starts_with("cookie1=; Max-Age=0;"));
|
|
||||||
|
|
||||||
// the .cookie call
|
|
||||||
assert_eq!(
|
|
||||||
val[1],
|
|
||||||
"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[test]
|
|
||||||
fn test_update_response_cookies() {
|
|
||||||
let mut r = Response::Ok()
|
|
||||||
.cookie(crate::http::Cookie::new("original", "val100"))
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
r.add_cookie(&crate::http::Cookie::new("cookie2", "val200"))
|
|
||||||
.unwrap();
|
|
||||||
r.add_cookie(&crate::http::Cookie::new("cookie2", "val250"))
|
|
||||||
.unwrap();
|
|
||||||
r.add_cookie(&crate::http::Cookie::new("cookie3", "val300"))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(r.cookies().count(), 4);
|
|
||||||
r.del_cookie("cookie2");
|
|
||||||
|
|
||||||
let mut iter = r.cookies();
|
|
||||||
let v = iter.next().unwrap();
|
|
||||||
assert_eq!((v.name(), v.value()), ("original", "val100"));
|
|
||||||
let v = iter.next().unwrap();
|
|
||||||
assert_eq!((v.name(), v.value()), ("cookie3", "val300"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_basic_builder() {
|
fn test_basic_builder() {
|
||||||
let resp = Response::Ok().insert_header(("X-TEST", "value")).finish();
|
let resp = Response::Ok().insert_header(("X-TEST", "value")).finish();
|
||||||
|
@ -1000,40 +651,9 @@ mod tests {
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_json() {
|
|
||||||
let resp = Response::Ok().json(vec!["v1", "v2", "v3"]);
|
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
|
||||||
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
|
||||||
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
|
||||||
|
|
||||||
let resp = Response::Ok().json(&["v1", "v2", "v3"]);
|
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
|
||||||
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
|
||||||
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_json_ct() {
|
|
||||||
let resp = Response::build(StatusCode::OK)
|
|
||||||
.insert_header((CONTENT_TYPE, "text/json"))
|
|
||||||
.json(&vec!["v1", "v2", "v3"]);
|
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
|
||||||
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
|
||||||
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_serde_json_in_body() {
|
|
||||||
use serde_json::json;
|
|
||||||
let resp =
|
|
||||||
Response::build(StatusCode::OK).body(json!({"test-key":"test-value"}));
|
|
||||||
assert_eq!(resp.body().get_ref(), br#"{"test-key":"test-value"}"#);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_response() {
|
fn test_into_response() {
|
||||||
let resp: Response = "test".into();
|
let resp: Response<Body> = "test".into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1042,7 +662,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
|
|
||||||
let resp: Response = b"test".as_ref().into();
|
let resp: Response<Body> = b"test".as_ref().into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1051,7 +671,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
|
|
||||||
let resp: Response = "test".to_owned().into();
|
let resp: Response<Body> = "test".to_owned().into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1060,7 +680,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
|
|
||||||
let resp: Response = (&"test".to_owned()).into();
|
let resp: Response<Body> = (&"test".to_owned()).into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1070,7 +690,7 @@ mod tests {
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
|
|
||||||
let b = Bytes::from_static(b"test");
|
let b = Bytes::from_static(b"test");
|
||||||
let resp: Response = b.into();
|
let resp: Response<Body> = b.into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1080,7 +700,7 @@ mod tests {
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
|
|
||||||
let b = Bytes::from_static(b"test");
|
let b = Bytes::from_static(b"test");
|
||||||
let resp: Response = b.into();
|
let resp: Response<Body> = b.into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1090,7 +710,7 @@ mod tests {
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
|
|
||||||
let b = BytesMut::from("test");
|
let b = BytesMut::from("test");
|
||||||
let resp: Response = b.into();
|
let resp: Response<Body> = b.into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
|
@ -1101,21 +721,22 @@ mod tests {
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_builder() {
|
fn test_into_builder() {
|
||||||
let mut resp: Response = "test".into();
|
let mut resp: Response<Body> = "test".into();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
resp.add_cookie(&crate::http::Cookie::new("cookie1", "val100"))
|
resp.headers_mut().insert(
|
||||||
.unwrap();
|
HeaderName::from_static("cookie"),
|
||||||
|
HeaderValue::from_static("cookie1=val100"),
|
||||||
|
);
|
||||||
|
|
||||||
let mut builder: ResponseBuilder = resp.into();
|
let mut builder: ResponseBuilder = resp.into();
|
||||||
let resp = builder.status(StatusCode::BAD_REQUEST).finish();
|
let resp = builder.status(StatusCode::BAD_REQUEST).finish();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let cookie = resp.cookies().next().unwrap();
|
let cookie = resp.headers().get_all("Cookie").next().unwrap();
|
||||||
assert_eq!((cookie.name(), cookie.value()), ("cookie1", "val100"));
|
assert_eq!(cookie.to_str().unwrap(), "cookie1=val100");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -14,11 +14,6 @@ use actix_rt::net::{ActixStream, Ready};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use http::{Method, Uri, Version};
|
use http::{Method, Uri, Version};
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use crate::{
|
|
||||||
cookie::{Cookie, CookieJar},
|
|
||||||
header::{self, HeaderValue},
|
|
||||||
};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
header::{HeaderMap, IntoHeaderPair},
|
header::{HeaderMap, IntoHeaderPair},
|
||||||
payload::Payload,
|
payload::Payload,
|
||||||
|
@ -55,8 +50,6 @@ struct Inner {
|
||||||
method: Method,
|
method: Method,
|
||||||
uri: Uri,
|
uri: Uri,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: CookieJar,
|
|
||||||
payload: Option<Payload>,
|
payload: Option<Payload>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +60,6 @@ impl Default for TestRequest {
|
||||||
uri: Uri::from_str("/").unwrap(),
|
uri: Uri::from_str("/").unwrap(),
|
||||||
version: Version::HTTP_11,
|
version: Version::HTTP_11,
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
cookies: CookieJar::new(),
|
|
||||||
payload: None,
|
payload: None,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -135,13 +126,6 @@ impl TestRequest {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set cookie for this request.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
pub fn cookie<'a>(&mut self, cookie: Cookie<'a>) -> &mut Self {
|
|
||||||
parts(&mut self.0).cookies.add(cookie.into_owned());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set request payload.
|
/// Set request payload.
|
||||||
pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self {
|
pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self {
|
||||||
let mut payload = crate::h1::Payload::empty();
|
let mut payload = crate::h1::Payload::empty();
|
||||||
|
@ -170,22 +154,6 @@ impl TestRequest {
|
||||||
head.version = inner.version;
|
head.version = inner.version;
|
||||||
head.headers = inner.headers;
|
head.headers = inner.headers;
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
{
|
|
||||||
let cookie: String = inner
|
|
||||||
.cookies
|
|
||||||
.delta()
|
|
||||||
// ensure only name=value is written to cookie header
|
|
||||||
.map(|c| Cookie::new(c.name(), c.value()).encoded().to_string())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("; ");
|
|
||||||
|
|
||||||
if !cookie.is_empty() {
|
|
||||||
head.headers
|
|
||||||
.insert(header::COOKIE, HeaderValue::from_str(&cookie).unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,8 @@ use derive_more::{Display, Error, From};
|
||||||
use http::{header, Method, StatusCode};
|
use http::{header, Method, StatusCode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::ResponseError,
|
body::Body, error::ResponseError, header::HeaderValue, message::RequestHead,
|
||||||
header::HeaderValue,
|
response::Response, ResponseBuilder,
|
||||||
message::RequestHead,
|
|
||||||
response::{Response, ResponseBuilder},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mod codec;
|
mod codec;
|
||||||
|
@ -101,7 +99,7 @@ pub enum HandshakeError {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResponseError for HandshakeError {
|
impl ResponseError for HandshakeError {
|
||||||
fn error_response(&self) -> Response {
|
fn error_response(&self) -> Response<Body> {
|
||||||
match self {
|
match self {
|
||||||
HandshakeError::GetMethodRequired => Response::MethodNotAllowed()
|
HandshakeError::GetMethodRequired => Response::MethodNotAllowed()
|
||||||
.insert_header((header::ALLOW, "GET"))
|
.insert_header((header::ALLOW, "GET"))
|
||||||
|
@ -322,17 +320,17 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_wserror_http_response() {
|
fn test_wserror_http_response() {
|
||||||
let resp: Response = HandshakeError::GetMethodRequired.error_response();
|
let resp = HandshakeError::GetMethodRequired.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
let resp: Response = HandshakeError::NoWebsocketUpgrade.error_response();
|
let resp = HandshakeError::NoWebsocketUpgrade.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
let resp: Response = HandshakeError::NoConnectionUpgrade.error_response();
|
let resp = HandshakeError::NoConnectionUpgrade.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
let resp: Response = HandshakeError::NoVersionHeader.error_response();
|
let resp = HandshakeError::NoVersionHeader.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
let resp: Response = HandshakeError::UnsupportedVersion.error_response();
|
let resp = HandshakeError::UnsupportedVersion.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
let resp: Response = HandshakeError::BadWebsocketKey.error_response();
|
let resp = HandshakeError::BadWebsocketKey.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,15 @@ extern crate tls_openssl as openssl;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use actix_http::error::{ErrorBadRequest, PayloadError};
|
use actix_http::{
|
||||||
use actix_http::http::header::{self, HeaderName, HeaderValue};
|
body::{Body, SizedStream},
|
||||||
use actix_http::http::{Method, StatusCode, Version};
|
error::{ErrorBadRequest, PayloadError},
|
||||||
use actix_http::HttpMessage;
|
http::{
|
||||||
use actix_http::{body, Error, HttpService, Request, Response};
|
header::{self, HeaderName, HeaderValue},
|
||||||
|
Method, StatusCode, Version,
|
||||||
|
},
|
||||||
|
Error, HttpMessage, HttpService, Request, Response,
|
||||||
|
};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::{fn_service, ServiceFactoryExt};
|
use actix_service::{fn_service, ServiceFactoryExt};
|
||||||
use actix_utils::future::{err, ok, ready};
|
use actix_utils::future::{err, ok, ready};
|
||||||
|
@ -328,7 +332,7 @@ async fn test_h2_body_length() {
|
||||||
.h2(|_| {
|
.h2(|_| {
|
||||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)),
|
Response::Ok().body(SizedStream::new(STR.len() as u64, body)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
|
@ -401,7 +405,7 @@ async fn test_h2_response_http_error_handling() {
|
||||||
async fn test_h2_service_error() {
|
async fn test_h2_service_error() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| err::<Response, Error>(ErrorBadRequest("error")))
|
.h2(|_| err::<Response<Body>, Error>(ErrorBadRequest("error")))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,10 +2,15 @@
|
||||||
|
|
||||||
extern crate tls_rustls as rustls;
|
extern crate tls_rustls as rustls;
|
||||||
|
|
||||||
use actix_http::error::PayloadError;
|
use actix_http::{
|
||||||
use actix_http::http::header::{self, HeaderName, HeaderValue};
|
body::{Body, SizedStream},
|
||||||
use actix_http::http::{Method, StatusCode, Version};
|
error::{self, PayloadError},
|
||||||
use actix_http::{body, error, Error, HttpService, Request, Response};
|
http::{
|
||||||
|
header::{self, HeaderName, HeaderValue},
|
||||||
|
Method, StatusCode, Version,
|
||||||
|
},
|
||||||
|
Error, HttpService, Request, Response,
|
||||||
|
};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::{fn_factory_with_config, fn_service};
|
use actix_service::{fn_factory_with_config, fn_service};
|
||||||
use actix_utils::future::{err, ok};
|
use actix_utils::future::{err, ok};
|
||||||
|
@ -344,7 +349,7 @@ async fn test_h2_body_length() {
|
||||||
.h2(|_| {
|
.h2(|_| {
|
||||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)),
|
Response::Ok().body(SizedStream::new(STR.len() as u64, body)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
|
@ -416,7 +421,7 @@ async fn test_h2_response_http_error_handling() {
|
||||||
async fn test_h2_service_error() {
|
async fn test_h2_service_error() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
|
.h2(|_| err::<Response<Body>, Error>(error::ErrorBadRequest("error")))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -433,7 +438,7 @@ async fn test_h2_service_error() {
|
||||||
async fn test_h1_service_error() {
|
async fn test_h1_service_error() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
|
.h1(|_| err::<Response<Body>, Error>(error::ErrorBadRequest("error")))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -13,7 +13,10 @@ use regex::Regex;
|
||||||
|
|
||||||
use actix_http::HttpMessage;
|
use actix_http::HttpMessage;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body, error, http, http::header, Error, HttpService, KeepAlive, Request, Response,
|
body::{Body, SizedStream},
|
||||||
|
error, http,
|
||||||
|
http::header,
|
||||||
|
Error, HttpService, KeepAlive, Request, Response,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
@ -539,7 +542,7 @@ async fn test_h1_body_length() {
|
||||||
.h1(|_| {
|
.h1(|_| {
|
||||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)),
|
Response::Ok().body(SizedStream::new(STR.len() as u64, body)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
|
@ -646,7 +649,7 @@ async fn test_h1_response_http_error_handling() {
|
||||||
async fn test_h1_service_error() {
|
async fn test_h1_service_error() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| err::<Response, _>(error::ErrorBadRequest("error")))
|
.h1(|_| err::<Response<Body>, _>(error::ErrorBadRequest("error")))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -45,11 +45,10 @@ impl ResponseError for MultipartError {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_web::HttpResponse;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_multipart_error() {
|
fn test_multipart_error() {
|
||||||
let resp: HttpResponse = MultipartError::Boundary.error_response();
|
let resp = MultipartError::Boundary.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ openssl = ["tls-openssl", "actix-http/openssl"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-http = { version = "3.0.0-beta.5", features = ["cookies"] }
|
actix-http = "3.0.0-beta.5"
|
||||||
actix-http-test = { version = "3.0.0-beta.4", features = [] }
|
actix-http-test = { version = "3.0.0-beta.4", features = [] }
|
||||||
actix-service = "2.0.0-beta.4"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-utils = "3.0.0-beta.2"
|
actix-utils = "3.0.0-beta.2"
|
||||||
|
|
|
@ -37,12 +37,12 @@ use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||||
pub use actix_http::test::TestBuffer;
|
pub use actix_http::test::TestBuffer;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
http::{HeaderMap, Method},
|
http::{HeaderMap, Method},
|
||||||
ws, HttpService, Request,
|
ws, HttpService, Request, Response,
|
||||||
};
|
};
|
||||||
use actix_service::{map_config, IntoServiceFactory, ServiceFactory};
|
use actix_service::{map_config, IntoServiceFactory, ServiceFactory};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{AppConfig, MessageBody, Server, Service},
|
dev::{AppConfig, MessageBody, Server, Service},
|
||||||
rt, web, Error, HttpResponse,
|
rt, web, Error,
|
||||||
};
|
};
|
||||||
use awc::{error::PayloadError, Client, ClientRequest, ClientResponse, Connector};
|
use awc::{error::PayloadError, Client, ClientRequest, ClientResponse, Connector};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
@ -83,7 +83,7 @@ where
|
||||||
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<HttpResponse<B>> + 'static,
|
S::Response: Into<Response<B>> + 'static,
|
||||||
<S::Service as Service<Request>>::Future: 'static,
|
<S::Service as Service<Request>>::Future: 'static,
|
||||||
B: MessageBody + 'static,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
|
@ -122,7 +122,7 @@ where
|
||||||
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<HttpResponse<B>> + 'static,
|
S::Response: Into<Response<B>> + 'static,
|
||||||
<S::Service as Service<Request>>::Future: 'static,
|
<S::Service as Service<Request>>::Future: 'static,
|
||||||
B: MessageBody + 'static,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,9 +22,9 @@ use actix_http::{
|
||||||
http::HeaderValue,
|
http::HeaderValue,
|
||||||
ws::{hash_key, Codec},
|
ws::{hash_key, Codec},
|
||||||
};
|
};
|
||||||
use actix_web::dev::HttpResponseBuilder;
|
|
||||||
use actix_web::error::{Error, PayloadError};
|
use actix_web::error::{Error, PayloadError};
|
||||||
use actix_web::http::{header, Method, StatusCode};
|
use actix_web::http::{header, Method, StatusCode};
|
||||||
|
use actix_web::HttpResponseBuilder;
|
||||||
use actix_web::{HttpRequest, HttpResponse};
|
use actix_web::{HttpRequest, HttpResponse};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use bytestring::ByteString;
|
use bytestring::ByteString;
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_utils::future;
|
use actix_utils::future::{ok, Ready};
|
||||||
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
use actix_web::http::header::{HeaderName, HeaderValue};
|
use actix_web::http::header::{HeaderName, HeaderValue};
|
||||||
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::{http, web::Path, App, Error, HttpResponse, Responder};
|
use actix_web::{http, web::Path, App, Error, HttpResponse, Responder};
|
||||||
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, route, trace};
|
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, route, trace};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
@ -56,12 +57,12 @@ async fn trace_test() -> impl Responder {
|
||||||
|
|
||||||
#[get("/test")]
|
#[get("/test")]
|
||||||
fn auto_async() -> impl Future<Output = Result<HttpResponse, actix_web::Error>> {
|
fn auto_async() -> impl Future<Output = Result<HttpResponse, actix_web::Error>> {
|
||||||
future::ok(HttpResponse::Ok().finish())
|
ok(HttpResponse::Ok().finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/test")]
|
#[get("/test")]
|
||||||
fn auto_sync() -> impl Future<Output = Result<HttpResponse, actix_web::Error>> {
|
fn auto_sync() -> impl Future<Output = Result<HttpResponse, actix_web::Error>> {
|
||||||
future::ok(HttpResponse::Ok().finish())
|
ok(HttpResponse::Ok().finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[put("/test/{param}")]
|
#[put("/test/{param}")]
|
||||||
|
@ -103,10 +104,10 @@ where
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Transform = ChangeStatusCodeMiddleware<S>;
|
type Transform = ChangeStatusCodeMiddleware<S>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = future::Ready<Result<Self::Transform, Self::InitError>>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
future::ok(ChangeStatusCodeMiddleware { service })
|
ok(ChangeStatusCodeMiddleware { service })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,6 +145,7 @@ where
|
||||||
|
|
||||||
#[get("/test/wrap", wrap = "ChangeStatusCode")]
|
#[get("/test/wrap", wrap = "ChangeStatusCode")]
|
||||||
async fn get_wrap(_: Path<String>) -> impl Responder {
|
async fn get_wrap(_: Path<String>) -> impl Responder {
|
||||||
|
// panic!("actually never gets called because path failed to extract");
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,6 +259,10 @@ async fn test_wrap() {
|
||||||
let srv = actix_test::start(|| App::new().service(get_wrap));
|
let srv = actix_test::start(|| App::new().service(get_wrap));
|
||||||
|
|
||||||
let request = srv.request(http::Method::GET, srv.url("/test/wrap"));
|
let request = srv.request(http::Method::GET, srv.url("/test/wrap"));
|
||||||
let response = request.send().await.unwrap();
|
let mut response = request.send().await.unwrap();
|
||||||
|
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||||
assert!(response.headers().contains_key("custom-header"));
|
assert!(response.headers().contains_key("custom-header"));
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert!(body.contains("wrong number of parameters"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Removed
|
||||||
|
* Deprecated methods on `ClientRequest`: `if_true`, `if_some`. [#2148]
|
||||||
|
|
||||||
|
[#2148]: https://github.com/actix/actix-web/pull/2148
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.4 - 2021-04-02
|
## 3.0.0-beta.4 - 2021-04-02
|
||||||
|
|
|
@ -41,7 +41,7 @@ rustls = ["tls-rustls", "actix-http/rustls"]
|
||||||
compress = ["actix-http/compress"]
|
compress = ["actix-http/compress"]
|
||||||
|
|
||||||
# cookie parsing and cookie jar
|
# cookie parsing and cookie jar
|
||||||
cookies = ["actix-http/cookies"]
|
cookies = ["cookie"]
|
||||||
|
|
||||||
# trust-dns as dns resolver
|
# trust-dns as dns resolver
|
||||||
trust-dns = ["actix-http/trust-dns"]
|
trust-dns = ["actix-http/trust-dns"]
|
||||||
|
@ -54,6 +54,7 @@ actix-rt = { version = "2.1", default-features = false }
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
|
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
futures-core = { version = "0.3.7", default-features = false }
|
futures-core = { version = "0.3.7", default-features = false }
|
||||||
itoa = "0.4"
|
itoa = "0.4"
|
||||||
|
|
|
@ -93,12 +93,11 @@
|
||||||
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
||||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::{convert::TryFrom, rc::Rc, time::Duration};
|
||||||
use std::rc::Rc;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub use actix_http::cookie;
|
pub use cookie;
|
||||||
|
|
||||||
pub use actix_http::{client::Connector, http};
|
pub use actix_http::{client::Connector, http};
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
|
|
|
@ -8,14 +8,14 @@ use futures_core::Stream;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use actix_http::body::Body;
|
use actix_http::body::Body;
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use actix_http::cookie::{Cookie, CookieJar};
|
|
||||||
use actix_http::http::header::{self, IntoHeaderPair};
|
use actix_http::http::header::{self, IntoHeaderPair};
|
||||||
use actix_http::http::{
|
use actix_http::http::{
|
||||||
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
|
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
|
||||||
};
|
};
|
||||||
use actix_http::{Error, RequestHead};
|
use actix_http::{Error, RequestHead};
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
use crate::error::{FreezeRequestError, InvalidUrl};
|
use crate::error::{FreezeRequestError, InvalidUrl};
|
||||||
use crate::frozen::FrozenClientRequest;
|
use crate::frozen::FrozenClientRequest;
|
||||||
use crate::sender::{PrepForSendingError, RequestSender, SendClientRequest};
|
use crate::sender::{PrepForSendingError, RequestSender, SendClientRequest};
|
||||||
|
@ -271,7 +271,7 @@ impl ClientRequest {
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let resp = awc::Client::new().get("https://www.rust-lang.org")
|
/// let resp = awc::Client::new().get("https://www.rust-lang.org")
|
||||||
/// .cookie(
|
/// .cookie(
|
||||||
/// awc::http::Cookie::build("name", "value")
|
/// awc::cookie::Cookie::build("name", "value")
|
||||||
/// .domain("www.rust-lang.org")
|
/// .domain("www.rust-lang.org")
|
||||||
/// .path("/")
|
/// .path("/")
|
||||||
/// .secure(true)
|
/// .secure(true)
|
||||||
|
@ -311,34 +311,6 @@ impl ClientRequest {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This method calls provided closure with builder reference if value is `true`.
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[deprecated = "Use an if statement."]
|
|
||||||
pub fn if_true<F>(self, value: bool, f: F) -> Self
|
|
||||||
where
|
|
||||||
F: FnOnce(ClientRequest) -> ClientRequest,
|
|
||||||
{
|
|
||||||
if value {
|
|
||||||
f(self)
|
|
||||||
} else {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method calls provided closure with builder reference if value is `Some`.
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[deprecated = "Use an if-let construction."]
|
|
||||||
pub fn if_some<T, F>(self, value: Option<T>, f: F) -> Self
|
|
||||||
where
|
|
||||||
F: FnOnce(T, ClientRequest) -> ClientRequest,
|
|
||||||
{
|
|
||||||
if let Some(val) = value {
|
|
||||||
f(val, self)
|
|
||||||
} else {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the query part of the request
|
/// Sets the query part of the request
|
||||||
pub fn query<T: Serialize>(
|
pub fn query<T: Serialize>(
|
||||||
mut self,
|
mut self,
|
||||||
|
@ -494,7 +466,7 @@ impl ClientRequest {
|
||||||
let cookie: String = jar
|
let cookie: String = jar
|
||||||
.delta()
|
.delta()
|
||||||
// ensure only name=value is written to cookie header
|
// ensure only name=value is written to cookie header
|
||||||
.map(|c| Cookie::new(c.name(), c.value()).encoded().to_string())
|
.map(|c| c.stripped().encoded().to_string())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("; ");
|
.join("; ");
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,7 @@ use futures_core::{ready, Stream};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use actix_http::{cookie::Cookie, error::CookieParseError};
|
use crate::cookie::{Cookie, ParseError as CookieParseError};
|
||||||
|
|
||||||
use crate::error::JsonPayloadError;
|
use crate::error::JsonPayloadError;
|
||||||
|
|
||||||
/// Client Response
|
/// Client Response
|
||||||
|
@ -80,24 +79,6 @@ impl<S> HttpMessage for ClientResponse<S> {
|
||||||
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
||||||
self.head.extensions_mut()
|
self.head.extensions_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load request cookies.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
|
||||||
struct Cookies(Vec<Cookie<'static>>);
|
|
||||||
|
|
||||||
if self.extensions().get::<Cookies>().is_none() {
|
|
||||||
let mut cookies = Vec::new();
|
|
||||||
for hdr in self.headers().get_all(&header::SET_COOKIE) {
|
|
||||||
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
|
||||||
cookies.push(Cookie::parse_encoded(s)?.into_owned());
|
|
||||||
}
|
|
||||||
self.extensions_mut().insert(Cookies(cookies));
|
|
||||||
}
|
|
||||||
Ok(Ref::map(self.extensions(), |ext| {
|
|
||||||
&ext.get::<Cookies>().unwrap().0
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> ClientResponse<S> {
|
impl<S> ClientResponse<S> {
|
||||||
|
@ -180,6 +161,37 @@ impl<S> ClientResponse<S> {
|
||||||
self.timeout = ResponseTimeout::Disabled(timeout);
|
self.timeout = ResponseTimeout::Disabled(timeout);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load request cookies.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
||||||
|
struct Cookies(Vec<Cookie<'static>>);
|
||||||
|
|
||||||
|
if self.extensions().get::<Cookies>().is_none() {
|
||||||
|
let mut cookies = Vec::new();
|
||||||
|
for hdr in self.headers().get_all(&header::SET_COOKIE) {
|
||||||
|
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
||||||
|
cookies.push(Cookie::parse_encoded(s)?.into_owned());
|
||||||
|
}
|
||||||
|
self.extensions_mut().insert(Cookies(cookies));
|
||||||
|
}
|
||||||
|
Ok(Ref::map(self.extensions(), |ext| {
|
||||||
|
&ext.get::<Cookies>().unwrap().0
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return request cookie.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||||
|
if let Ok(cookies) = self.cookies() {
|
||||||
|
for cookie in cookies.iter() {
|
||||||
|
if cookie.name() == name {
|
||||||
|
return Some(cookie.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> ClientResponse<S>
|
impl<S> ClientResponse<S>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
future::Future,
|
future::Future,
|
||||||
net,
|
io, net,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
|
@ -209,7 +209,8 @@ impl RequestSender {
|
||||||
) -> SendClientRequest {
|
) -> SendClientRequest {
|
||||||
let body = match serde_json::to_string(value) {
|
let body = match serde_json::to_string(value) {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
Err(e) => return Error::from(e).into(),
|
// TODO: own error type
|
||||||
|
Err(e) => return Error::from(io::Error::new(io::ErrorKind::Other, e)).into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = self.set_header_if_none(header::CONTENT_TYPE, "application/json") {
|
if let Err(e) = self.set_header_if_none(header::CONTENT_TYPE, "application/json") {
|
||||||
|
@ -235,7 +236,8 @@ impl RequestSender {
|
||||||
) -> SendClientRequest {
|
) -> SendClientRequest {
|
||||||
let body = match serde_urlencoded::to_string(value) {
|
let body = match serde_urlencoded::to_string(value) {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
Err(e) => return Error::from(e).into(),
|
// TODO: own error type
|
||||||
|
Err(e) => return Error::from(io::Error::new(io::ErrorKind::Other, e)).into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// set content-type
|
// set content-type
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
//! Test helpers for actix http client to use during testing.
|
//! Test helpers for actix http client to use during testing.
|
||||||
use actix_http::http::header::IntoHeaderPair;
|
use actix_http::http::header::IntoHeaderPair;
|
||||||
use actix_http::http::{StatusCode, Version};
|
use actix_http::http::{StatusCode, Version};
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use actix_http::{
|
|
||||||
cookie::{Cookie, CookieJar},
|
|
||||||
http::header::{self, HeaderValue},
|
|
||||||
};
|
|
||||||
use actix_http::{h1, Payload, ResponseHead};
|
use actix_http::{h1, Payload, ResponseHead};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
use crate::ClientResponse;
|
use crate::ClientResponse;
|
||||||
|
|
||||||
/// Test `ClientResponse` builder
|
/// Test `ClientResponse` builder
|
||||||
|
@ -92,6 +89,8 @@ impl TestResponse {
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
for cookie in self.cookies.delta() {
|
for cookie in self.cookies.delta() {
|
||||||
|
use actix_http::http::header::{self, HeaderValue};
|
||||||
|
|
||||||
head.headers.insert(
|
head.headers.insert(
|
||||||
header::SET_COOKIE,
|
header::SET_COOKIE,
|
||||||
HeaderValue::from_str(&cookie.encoded().to_string()).unwrap(),
|
HeaderValue::from_str(&cookie.encoded().to_string()).unwrap(),
|
||||||
|
|
|
@ -31,8 +31,6 @@ use std::net::SocketAddr;
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
|
|
||||||
use actix_codec::Framed;
|
use actix_codec::Framed;
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use actix_http::cookie::{Cookie, CookieJar};
|
|
||||||
use actix_http::{ws, Payload, RequestHead};
|
use actix_http::{ws, Payload, RequestHead};
|
||||||
use actix_rt::time::timeout;
|
use actix_rt::time::timeout;
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
|
@ -40,6 +38,8 @@ use actix_service::Service;
|
||||||
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
||||||
|
|
||||||
use crate::connect::{BoxedSocket, ConnectRequest};
|
use crate::connect::{BoxedSocket, ConnectRequest};
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
use crate::error::{InvalidUrl, SendRequestError, WsClientError};
|
use crate::error::{InvalidUrl, SendRequestError, WsClientError};
|
||||||
use crate::http::header::{self, HeaderName, HeaderValue, IntoHeaderValue, AUTHORIZATION};
|
use crate::http::header::{self, HeaderName, HeaderValue, IntoHeaderValue, AUTHORIZATION};
|
||||||
use crate::http::{ConnectionType, Error as HttpError, Method, StatusCode, Uri, Version};
|
use crate::http::{ConnectionType, Error as HttpError, Method, StatusCode, Uri, Version};
|
||||||
|
@ -280,7 +280,7 @@ impl WebsocketsRequest {
|
||||||
let cookie: String = jar
|
let cookie: String = jar
|
||||||
.delta()
|
.delta()
|
||||||
// ensure only name=value is written to cookie header
|
// ensure only name=value is written to cookie header
|
||||||
.map(|c| Cookie::new(c.name(), c.value()).encoded().to_string())
|
.map(|c| c.stripped().encoded().to_string())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("; ");
|
.join("; ");
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ use std::time::Duration;
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
use brotli2::write::BrotliEncoder;
|
use brotli2::write::BrotliEncoder;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use cookie::Cookie;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use flate2::write::GzEncoder;
|
use flate2::write::GzEncoder;
|
||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
|
@ -22,9 +23,9 @@ use actix_http_test::test_server;
|
||||||
use actix_service::{map_config, pipeline_factory};
|
use actix_service::{map_config, pipeline_factory};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{AppConfig, BodyEncoding},
|
dev::{AppConfig, BodyEncoding},
|
||||||
http::{header, Cookie},
|
http::header,
|
||||||
middleware::Compress,
|
middleware::Compress,
|
||||||
web, App, Error, HttpMessage, HttpRequest, HttpResponse,
|
web, App, Error, HttpRequest, HttpResponse,
|
||||||
};
|
};
|
||||||
use awc::error::{JsonPayloadError, PayloadError, SendRequestError};
|
use awc::error::{JsonPayloadError, PayloadError, SendRequestError};
|
||||||
|
|
||||||
|
|
88
src/error.rs
88
src/error.rs
|
@ -3,12 +3,15 @@
|
||||||
pub use actix_http::error::*;
|
pub use actix_http::error::*;
|
||||||
use derive_more::{Display, Error, From};
|
use derive_more::{Display, Error, From};
|
||||||
use serde_json::error::Error as JsonError;
|
use serde_json::error::Error as JsonError;
|
||||||
|
use serde_urlencoded::de::Error as FormDeError;
|
||||||
|
use serde_urlencoded::ser::Error as FormError;
|
||||||
use url::ParseError as UrlParseError;
|
use url::ParseError as UrlParseError;
|
||||||
|
|
||||||
use crate::{http::StatusCode, HttpResponse};
|
use crate::http::StatusCode;
|
||||||
|
|
||||||
/// Errors which can occur when attempting to generate resource uri.
|
/// Errors which can occur when attempting to generate resource uri.
|
||||||
#[derive(Debug, PartialEq, Display, From)]
|
#[derive(Debug, PartialEq, Display, Error, From)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum UrlGenerationError {
|
pub enum UrlGenerationError {
|
||||||
/// Resource not found
|
/// Resource not found
|
||||||
#[display(fmt = "Resource not found")]
|
#[display(fmt = "Resource not found")]
|
||||||
|
@ -23,13 +26,12 @@ pub enum UrlGenerationError {
|
||||||
ParseError(UrlParseError),
|
ParseError(UrlParseError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for UrlGenerationError {}
|
|
||||||
|
|
||||||
/// `InternalServerError` for `UrlGeneratorError`
|
/// `InternalServerError` for `UrlGeneratorError`
|
||||||
impl ResponseError for UrlGenerationError {}
|
impl ResponseError for UrlGenerationError {}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing urlencoded payloads
|
/// A set of errors that can occur during parsing urlencoded payloads
|
||||||
#[derive(Debug, Display, Error, From)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum UrlencodedError {
|
pub enum UrlencodedError {
|
||||||
/// Can not decode chunked transfer encoding.
|
/// Can not decode chunked transfer encoding.
|
||||||
#[display(fmt = "Can not decode chunked transfer encoding.")]
|
#[display(fmt = "Can not decode chunked transfer encoding.")]
|
||||||
|
@ -52,8 +54,16 @@ pub enum UrlencodedError {
|
||||||
ContentType,
|
ContentType,
|
||||||
|
|
||||||
/// Parse error.
|
/// Parse error.
|
||||||
#[display(fmt = "Parse error.")]
|
#[display(fmt = "Parse error: {}.", _0)]
|
||||||
Parse,
|
Parse(FormDeError),
|
||||||
|
|
||||||
|
/// Encoding error.
|
||||||
|
#[display(fmt = "Encoding error.")]
|
||||||
|
Encoding,
|
||||||
|
|
||||||
|
/// Serialize error.
|
||||||
|
#[display(fmt = "Serialize error: {}.", _0)]
|
||||||
|
Serialize(FormError),
|
||||||
|
|
||||||
/// Payload error.
|
/// Payload error.
|
||||||
#[display(fmt = "Error that occur during reading payload: {}.", _0)]
|
#[display(fmt = "Error that occur during reading payload: {}.", _0)]
|
||||||
|
@ -63,53 +73,66 @@ pub enum UrlencodedError {
|
||||||
/// Return `BadRequest` for `UrlencodedError`
|
/// Return `BadRequest` for `UrlencodedError`
|
||||||
impl ResponseError for UrlencodedError {
|
impl ResponseError for UrlencodedError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
match *self {
|
match self {
|
||||||
UrlencodedError::Overflow { .. } => StatusCode::PAYLOAD_TOO_LARGE,
|
Self::Overflow { .. } => StatusCode::PAYLOAD_TOO_LARGE,
|
||||||
UrlencodedError::UnknownLength => StatusCode::LENGTH_REQUIRED,
|
Self::UnknownLength => StatusCode::LENGTH_REQUIRED,
|
||||||
|
Self::Payload(err) => err.status_code(),
|
||||||
_ => StatusCode::BAD_REQUEST,
|
_ => StatusCode::BAD_REQUEST,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing json payloads
|
/// A set of errors that can occur during parsing json payloads
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum JsonPayloadError {
|
pub enum JsonPayloadError {
|
||||||
/// Payload size is bigger than allowed. (default: 32kB)
|
/// Payload size is bigger than allowed. (default: 32kB)
|
||||||
#[display(fmt = "Json payload size is bigger than allowed")]
|
#[display(fmt = "Json payload size is bigger than allowed")]
|
||||||
Overflow,
|
Overflow,
|
||||||
|
|
||||||
/// Content type error
|
/// Content type error
|
||||||
#[display(fmt = "Content type error")]
|
#[display(fmt = "Content type error")]
|
||||||
ContentType,
|
ContentType,
|
||||||
|
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "Json deserialize error: {}", _0)]
|
#[display(fmt = "Json deserialize error: {}", _0)]
|
||||||
Deserialize(JsonError),
|
Deserialize(JsonError),
|
||||||
|
|
||||||
|
/// Serialize error
|
||||||
|
#[display(fmt = "Json serialize error: {}", _0)]
|
||||||
|
Serialize(JsonError),
|
||||||
|
|
||||||
/// Payload error
|
/// Payload error
|
||||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
||||||
Payload(PayloadError),
|
Payload(PayloadError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for JsonPayloadError {}
|
impl From<PayloadError> for JsonPayloadError {
|
||||||
|
fn from(err: PayloadError) -> Self {
|
||||||
|
Self::Payload(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `JsonPayloadError`
|
|
||||||
impl ResponseError for JsonPayloadError {
|
impl ResponseError for JsonPayloadError {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn status_code(&self) -> StatusCode {
|
||||||
match *self {
|
match self {
|
||||||
JsonPayloadError::Overflow => HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE),
|
Self::Overflow => StatusCode::PAYLOAD_TOO_LARGE,
|
||||||
_ => HttpResponse::new(StatusCode::BAD_REQUEST),
|
Self::Serialize(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Self::Payload(err) => err.status_code(),
|
||||||
|
_ => StatusCode::BAD_REQUEST,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing request paths
|
/// A set of errors that can occur during parsing request paths
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum PathError {
|
pub enum PathError {
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "Path deserialize error: {}", _0)]
|
#[display(fmt = "Path deserialize error: {}", _0)]
|
||||||
Deserialize(serde::de::value::Error),
|
Deserialize(serde::de::value::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for PathError {}
|
|
||||||
|
|
||||||
/// Return `BadRequest` for `PathError`
|
/// Return `BadRequest` for `PathError`
|
||||||
impl ResponseError for PathError {
|
impl ResponseError for PathError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
|
@ -119,6 +142,7 @@ impl ResponseError for PathError {
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing query strings.
|
/// A set of errors that can occur during parsing query strings.
|
||||||
#[derive(Debug, Display, Error, From)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum QueryPayloadError {
|
pub enum QueryPayloadError {
|
||||||
/// Query deserialize error.
|
/// Query deserialize error.
|
||||||
#[display(fmt = "Query deserialize error: {}", _0)]
|
#[display(fmt = "Query deserialize error: {}", _0)]
|
||||||
|
@ -133,25 +157,26 @@ impl ResponseError for QueryPayloadError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error type returned when reading body as lines.
|
/// Error type returned when reading body as lines.
|
||||||
#[derive(From, Display, Debug)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub enum ReadlinesError {
|
pub enum ReadlinesError {
|
||||||
/// Error when decoding a line.
|
|
||||||
#[display(fmt = "Encoding error")]
|
#[display(fmt = "Encoding error")]
|
||||||
/// Payload size is bigger than allowed. (default: 256kB)
|
/// Payload size is bigger than allowed. (default: 256kB)
|
||||||
EncodingError,
|
EncodingError,
|
||||||
|
|
||||||
/// Payload error.
|
/// Payload error.
|
||||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
||||||
Payload(PayloadError),
|
Payload(PayloadError),
|
||||||
|
|
||||||
/// Line limit exceeded.
|
/// Line limit exceeded.
|
||||||
#[display(fmt = "Line limit exceeded")]
|
#[display(fmt = "Line limit exceeded")]
|
||||||
LimitOverflow,
|
LimitOverflow,
|
||||||
|
|
||||||
/// ContentType error.
|
/// ContentType error.
|
||||||
#[display(fmt = "Content-type error")]
|
#[display(fmt = "Content-type error")]
|
||||||
ContentTypeError(ContentTypeError),
|
ContentTypeError(ContentTypeError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for ReadlinesError {}
|
|
||||||
|
|
||||||
/// Return `BadRequest` for `ReadlinesError`
|
/// Return `BadRequest` for `ReadlinesError`
|
||||||
impl ResponseError for ReadlinesError {
|
impl ResponseError for ReadlinesError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
|
@ -168,26 +193,25 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_urlencoded_error() {
|
fn test_urlencoded_error() {
|
||||||
let resp: HttpResponse =
|
let resp = UrlencodedError::Overflow { size: 0, limit: 0 }.error_response();
|
||||||
UrlencodedError::Overflow { size: 0, limit: 0 }.error_response();
|
|
||||||
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
let resp: HttpResponse = UrlencodedError::UnknownLength.error_response();
|
let resp = UrlencodedError::UnknownLength.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED);
|
assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED);
|
||||||
let resp: HttpResponse = UrlencodedError::ContentType.error_response();
|
let resp = UrlencodedError::ContentType.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_json_payload_error() {
|
fn test_json_payload_error() {
|
||||||
let resp: HttpResponse = JsonPayloadError::Overflow.error_response();
|
let resp = JsonPayloadError::Overflow.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
let resp: HttpResponse = JsonPayloadError::ContentType.error_response();
|
let resp = JsonPayloadError::ContentType.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_payload_error() {
|
fn test_query_payload_error() {
|
||||||
let resp: HttpResponse = QueryPayloadError::Deserialize(
|
let resp = QueryPayloadError::Deserialize(
|
||||||
serde_urlencoded::from_str::<i32>("bad query").unwrap_err(),
|
serde_urlencoded::from_str::<i32>("bad query").unwrap_err(),
|
||||||
)
|
)
|
||||||
.error_response();
|
.error_response();
|
||||||
|
@ -196,9 +220,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_readlines_error() {
|
fn test_readlines_error() {
|
||||||
let resp: HttpResponse = ReadlinesError::LimitOverflow.error_response();
|
let resp = ReadlinesError::LimitOverflow.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
let resp: HttpResponse = ReadlinesError::EncodingError.error_response();
|
let resp = ReadlinesError::EncodingError.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,23 @@ use std::marker::PhantomData;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_http::{Error, Response};
|
use actix_http::Error;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use actix_utils::future::{ready, Ready};
|
use actix_utils::future::{ready, Ready};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::extract::FromRequest;
|
use crate::{
|
||||||
use crate::request::HttpRequest;
|
extract::FromRequest,
|
||||||
use crate::responder::Responder;
|
request::HttpRequest,
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
responder::Responder,
|
||||||
|
response::HttpResponse,
|
||||||
|
service::{ServiceRequest, ServiceResponse},
|
||||||
|
};
|
||||||
|
|
||||||
/// A request handler is an async function that accepts zero or more parameters that can be
|
/// A request handler is an async function that accepts zero or more parameters that can be
|
||||||
/// extracted from a request (ie, [`impl FromRequest`](crate::FromRequest)) and returns a type that can be converted into
|
/// extracted from a request (i.e., [`impl FromRequest`](crate::FromRequest)) and returns a type
|
||||||
/// an [`HttpResponse`](crate::HttpResponse) (ie, [`impl Responder`](crate::Responder)).
|
/// that can be converted into an [`HttpResponse`] (that is, it impls the [`Responder`] trait).
|
||||||
///
|
///
|
||||||
/// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not
|
/// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not
|
||||||
/// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information.
|
/// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information.
|
||||||
|
@ -102,9 +105,7 @@ where
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = HandlerServiceFuture<F, T, R>;
|
type Future = HandlerServiceFuture<F, T, R>;
|
||||||
|
|
||||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
actix_service::always_ready!();
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let (req, mut payload) = req.into_parts();
|
let (req, mut payload) = req.into_parts();
|
||||||
|
@ -147,9 +148,9 @@ where
|
||||||
let state = HandlerServiceFuture::Handle(fut, req.take());
|
let state = HandlerServiceFuture::Handle(fut, req.take());
|
||||||
self.as_mut().set(state);
|
self.as_mut().set(state);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(err) => {
|
||||||
let res: Response = e.into().into();
|
|
||||||
let req = req.take().unwrap();
|
let req = req.take().unwrap();
|
||||||
|
let res = HttpResponse::from_error(err.into());
|
||||||
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
|
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
33
src/lib.rs
33
src/lib.rs
|
@ -86,6 +86,7 @@ mod request;
|
||||||
mod request_data;
|
mod request_data;
|
||||||
mod resource;
|
mod resource;
|
||||||
mod responder;
|
mod responder;
|
||||||
|
mod response;
|
||||||
mod rmap;
|
mod rmap;
|
||||||
mod route;
|
mod route;
|
||||||
mod scope;
|
mod scope;
|
||||||
|
@ -95,18 +96,20 @@ pub mod test;
|
||||||
pub(crate) mod types;
|
pub(crate) mod types;
|
||||||
pub mod web;
|
pub mod web;
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
pub use actix_http::Response as BaseHttpResponse;
|
||||||
pub use actix_http::cookie;
|
|
||||||
pub use actix_http::Response as HttpResponse;
|
|
||||||
pub use actix_http::{body, Error, HttpMessage, ResponseError, Result};
|
pub use actix_http::{body, Error, HttpMessage, ResponseError, Result};
|
||||||
|
#[doc(inline)]
|
||||||
pub use actix_rt as rt;
|
pub use actix_rt as rt;
|
||||||
pub use actix_web_codegen::*;
|
pub use actix_web_codegen::*;
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub use cookie;
|
||||||
|
|
||||||
pub use crate::app::App;
|
pub use crate::app::App;
|
||||||
pub use crate::extract::FromRequest;
|
pub use crate::extract::FromRequest;
|
||||||
pub use crate::request::HttpRequest;
|
pub use crate::request::HttpRequest;
|
||||||
pub use crate::resource::Resource;
|
pub use crate::resource::Resource;
|
||||||
pub use crate::responder::Responder;
|
pub use crate::responder::Responder;
|
||||||
|
pub use crate::response::{HttpResponse, HttpResponseBuilder};
|
||||||
pub use crate::route::Route;
|
pub use crate::route::Route;
|
||||||
pub use crate::scope::Scope;
|
pub use crate::scope::Scope;
|
||||||
pub use crate::server::HttpServer;
|
pub use crate::server::HttpServer;
|
||||||
|
@ -138,7 +141,7 @@ pub mod dev {
|
||||||
pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody, SizedStream};
|
pub use actix_http::body::{Body, BodySize, MessageBody, ResponseBody, SizedStream};
|
||||||
#[cfg(feature = "compress")]
|
#[cfg(feature = "compress")]
|
||||||
pub use actix_http::encoding::Decoder as Decompress;
|
pub use actix_http::encoding::Decoder as Decompress;
|
||||||
pub use actix_http::ResponseBuilder as HttpResponseBuilder;
|
pub use actix_http::ResponseBuilder as BaseHttpResponseBuilder;
|
||||||
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, ResponseHead};
|
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, ResponseHead};
|
||||||
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
|
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
|
||||||
pub use actix_server::Server;
|
pub use actix_server::Server;
|
||||||
|
@ -188,4 +191,26 @@ pub mod dev {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl BodyEncoding for crate::HttpResponseBuilder {
|
||||||
|
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||||
|
self.extensions().get::<Enc>().map(|enc| enc.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
|
||||||
|
self.extensions_mut().insert(Enc(encoding));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B> BodyEncoding for crate::HttpResponse<B> {
|
||||||
|
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||||
|
self.extensions().get::<Enc>().map(|enc| enc.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
|
||||||
|
self.extensions_mut().insert(Enc(encoding));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -380,7 +380,7 @@ impl Format {
|
||||||
results.push(match cap.get(3).unwrap().as_str() {
|
results.push(match cap.get(3).unwrap().as_str() {
|
||||||
"a" => {
|
"a" => {
|
||||||
if key.as_str() == "r" {
|
if key.as_str() == "r" {
|
||||||
FormatText::RealIPRemoteAddr
|
FormatText::RealIpRemoteAddr
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
@ -434,7 +434,7 @@ enum FormatText {
|
||||||
Time,
|
Time,
|
||||||
TimeMillis,
|
TimeMillis,
|
||||||
RemoteAddr,
|
RemoteAddr,
|
||||||
RealIPRemoteAddr,
|
RealIpRemoteAddr,
|
||||||
UrlPath,
|
UrlPath,
|
||||||
RequestHeader(HeaderName),
|
RequestHeader(HeaderName),
|
||||||
ResponseHeader(HeaderName),
|
ResponseHeader(HeaderName),
|
||||||
|
@ -554,7 +554,7 @@ impl FormatText {
|
||||||
};
|
};
|
||||||
*self = s;
|
*self = s;
|
||||||
}
|
}
|
||||||
FormatText::RealIPRemoteAddr => {
|
FormatText::RealIpRemoteAddr => {
|
||||||
let s = if let Some(remote) = req.connection_info().realip_remote_addr() {
|
let s = if let Some(remote) = req.connection_info().realip_remote_addr() {
|
||||||
FormatText::Str(remote.to_string())
|
FormatText::Str(remote.to_string())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,19 +1,27 @@
|
||||||
use std::cell::{Ref, RefCell, RefMut};
|
use std::{
|
||||||
use std::rc::Rc;
|
cell::{Ref, RefCell, RefMut},
|
||||||
use std::{fmt, net};
|
fmt, net,
|
||||||
|
rc::Rc,
|
||||||
|
str,
|
||||||
|
};
|
||||||
|
|
||||||
use actix_http::http::{HeaderMap, Method, Uri, Version};
|
use actix_http::{
|
||||||
use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
|
http::{HeaderMap, Method, Uri, Version},
|
||||||
|
Error, Extensions, HttpMessage, Message, Payload, RequestHead,
|
||||||
|
};
|
||||||
use actix_router::{Path, Url};
|
use actix_router::{Path, Url};
|
||||||
use actix_utils::future::{ok, Ready};
|
use actix_utils::future::{ok, Ready};
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use cookie::{Cookie, ParseError as CookieParseError};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
use crate::app_service::AppInitServiceState;
|
use crate::{
|
||||||
use crate::config::AppConfig;
|
app_service::AppInitServiceState, config::AppConfig, error::UrlGenerationError,
|
||||||
use crate::error::UrlGenerationError;
|
extract::FromRequest, info::ConnectionInfo, rmap::ResourceMap,
|
||||||
use crate::extract::FromRequest;
|
};
|
||||||
use crate::info::ConnectionInfo;
|
|
||||||
use crate::rmap::ResourceMap;
|
#[cfg(feature = "cookies")]
|
||||||
|
struct Cookies(Vec<Cookie<'static>>);
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
/// An HTTP Request
|
/// An HTTP Request
|
||||||
|
@ -260,6 +268,42 @@ impl HttpRequest {
|
||||||
fn app_state(&self) -> &AppInitServiceState {
|
fn app_state(&self) -> &AppInitServiceState {
|
||||||
&*self.inner.app_state
|
&*self.inner.app_state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load request cookies.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
||||||
|
use actix_http::http::header::COOKIE;
|
||||||
|
|
||||||
|
if self.extensions().get::<Cookies>().is_none() {
|
||||||
|
let mut cookies = Vec::new();
|
||||||
|
for hdr in self.headers().get_all(COOKIE) {
|
||||||
|
let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
||||||
|
for cookie_str in s.split(';').map(|s| s.trim()) {
|
||||||
|
if !cookie_str.is_empty() {
|
||||||
|
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.extensions_mut().insert(Cookies(cookies));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Ref::map(self.extensions(), |ext| {
|
||||||
|
&ext.get::<Cookies>().unwrap().0
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return request cookie.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||||
|
if let Ok(cookies) = self.cookies() {
|
||||||
|
for cookie in cookies.iter() {
|
||||||
|
if cookie.name() == name {
|
||||||
|
return Some(cookie.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpMessage for HttpRequest {
|
impl HttpMessage for HttpRequest {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
|
body::Body,
|
||||||
error::InternalError,
|
error::InternalError,
|
||||||
http::{header::IntoHeaderPair, Error as HttpError, HeaderMap, StatusCode},
|
http::{header::IntoHeaderPair, Error as HttpError, HeaderMap, StatusCode},
|
||||||
ResponseBuilder,
|
|
||||||
};
|
};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
use crate::{Error, HttpRequest, HttpResponse};
|
use crate::{Error, HttpRequest, HttpResponse, HttpResponseBuilder};
|
||||||
|
|
||||||
/// Trait implemented by types that can be converted to an HTTP response.
|
/// Trait implemented by types that can be converted to an HTTP response.
|
||||||
///
|
///
|
||||||
|
@ -66,11 +66,32 @@ impl Responder for HttpResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Responder for actix_http::Response<Body> {
|
||||||
|
#[inline]
|
||||||
|
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
|
||||||
|
HttpResponse::from(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Responder for HttpResponseBuilder {
|
||||||
|
#[inline]
|
||||||
|
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse {
|
||||||
|
self.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Responder for actix_http::ResponseBuilder {
|
||||||
|
#[inline]
|
||||||
|
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse {
|
||||||
|
HttpResponse::from(self.finish())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Responder> Responder for Option<T> {
|
impl<T: Responder> Responder for Option<T> {
|
||||||
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
||||||
match self {
|
match self {
|
||||||
Some(t) => t.respond_to(req),
|
Some(val) => val.respond_to(req),
|
||||||
None => HttpResponse::build(StatusCode::NOT_FOUND).finish(),
|
None => HttpResponse::new(StatusCode::NOT_FOUND),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,13 +109,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Responder for ResponseBuilder {
|
|
||||||
#[inline]
|
|
||||||
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse {
|
|
||||||
self.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Responder> Responder for (T, StatusCode) {
|
impl<T: Responder> Responder for (T, StatusCode) {
|
||||||
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
||||||
let mut res = self.0.respond_to(req);
|
let mut res = self.0.respond_to(req);
|
||||||
|
|
|
@ -0,0 +1,979 @@
|
||||||
|
use std::{
|
||||||
|
cell::{Ref, RefMut},
|
||||||
|
convert::TryInto,
|
||||||
|
fmt,
|
||||||
|
future::Future,
|
||||||
|
mem,
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
|
use actix_http::{
|
||||||
|
body::{Body, BodyStream, MessageBody, ResponseBody},
|
||||||
|
http::{
|
||||||
|
header::{self, HeaderMap, HeaderName, IntoHeaderPair, IntoHeaderValue},
|
||||||
|
ConnectionType, Error as HttpError, StatusCode,
|
||||||
|
},
|
||||||
|
Extensions, Response, ResponseHead,
|
||||||
|
};
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures_core::Stream;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use actix_http::http::header::HeaderValue;
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use cookie::{Cookie, CookieJar};
|
||||||
|
|
||||||
|
use crate::error::{Error, JsonPayloadError};
|
||||||
|
|
||||||
|
/// An HTTP Response
|
||||||
|
pub struct HttpResponse<B = Body> {
|
||||||
|
res: Response<B>,
|
||||||
|
error: Option<Error>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HttpResponse<Body> {
|
||||||
|
/// Create HTTP response builder with specific status.
|
||||||
|
#[inline]
|
||||||
|
pub fn build(status: StatusCode) -> HttpResponseBuilder {
|
||||||
|
HttpResponseBuilder::new(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create HTTP response builder
|
||||||
|
#[inline]
|
||||||
|
pub fn build_from<T: Into<HttpResponseBuilder>>(source: T) -> HttpResponseBuilder {
|
||||||
|
source.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a response.
|
||||||
|
#[inline]
|
||||||
|
pub fn new(status: StatusCode) -> Self {
|
||||||
|
Self {
|
||||||
|
res: Response::new(status),
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create an error response.
|
||||||
|
#[inline]
|
||||||
|
pub fn from_error(error: Error) -> Self {
|
||||||
|
let res = error.as_response_error().error_response();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
res,
|
||||||
|
error: Some(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert response to response with body
|
||||||
|
pub fn into_body<B>(self) -> HttpResponse<B> {
|
||||||
|
HttpResponse {
|
||||||
|
res: self.res.into_body(),
|
||||||
|
error: self.error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B> HttpResponse<B> {
|
||||||
|
/// Constructs a response with body
|
||||||
|
#[inline]
|
||||||
|
pub fn with_body(status: StatusCode, body: B) -> Self {
|
||||||
|
Self {
|
||||||
|
res: Response::with_body(status, body),
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to response head.
|
||||||
|
#[inline]
|
||||||
|
pub fn head(&self) -> &ResponseHead {
|
||||||
|
self.res.head()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to response head.
|
||||||
|
#[inline]
|
||||||
|
pub fn head_mut(&mut self) -> &mut ResponseHead {
|
||||||
|
self.res.head_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The source `error` for this response
|
||||||
|
#[inline]
|
||||||
|
pub fn error(&self) -> Option<&Error> {
|
||||||
|
self.error.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the response status code
|
||||||
|
#[inline]
|
||||||
|
pub fn status(&self) -> StatusCode {
|
||||||
|
self.res.status()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the `StatusCode` for this response
|
||||||
|
#[inline]
|
||||||
|
pub fn status_mut(&mut self) -> &mut StatusCode {
|
||||||
|
self.res.status_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the headers from the response
|
||||||
|
#[inline]
|
||||||
|
pub fn headers(&self) -> &HeaderMap {
|
||||||
|
self.res.headers()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a mutable reference to the headers
|
||||||
|
#[inline]
|
||||||
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||||
|
self.res.headers_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get an iterator for the cookies set by this response.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookies(&self) -> CookieIter<'_> {
|
||||||
|
CookieIter {
|
||||||
|
iter: self.headers().get_all(header::SET_COOKIE),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add a cookie to this response
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn add_cookie(&mut self, cookie: &Cookie<'_>) -> Result<(), HttpError> {
|
||||||
|
HeaderValue::from_str(&cookie.to_string())
|
||||||
|
.map(|c| {
|
||||||
|
self.headers_mut().append(header::SET_COOKIE, c);
|
||||||
|
})
|
||||||
|
.map_err(|e| e.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove all cookies with the given name from this response. Returns
|
||||||
|
/// the number of cookies removed.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn del_cookie(&mut self, name: &str) -> usize {
|
||||||
|
let headers = self.headers_mut();
|
||||||
|
|
||||||
|
let vals: Vec<HeaderValue> = headers
|
||||||
|
.get_all(header::SET_COOKIE)
|
||||||
|
.map(|v| v.to_owned())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
headers.remove(header::SET_COOKIE);
|
||||||
|
|
||||||
|
let mut count: usize = 0;
|
||||||
|
for v in vals {
|
||||||
|
if let Ok(s) = v.to_str() {
|
||||||
|
if let Ok(c) = Cookie::parse_encoded(s) {
|
||||||
|
if c.name() == name {
|
||||||
|
count += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// put set-cookie header head back if it does not validate
|
||||||
|
headers.append(header::SET_COOKIE, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connection upgrade status
|
||||||
|
#[inline]
|
||||||
|
pub fn upgrade(&self) -> bool {
|
||||||
|
self.res.upgrade()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Keep-alive status for this connection
|
||||||
|
pub fn keep_alive(&self) -> bool {
|
||||||
|
self.res.keep_alive()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Responses extensions
|
||||||
|
#[inline]
|
||||||
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
||||||
|
self.res.extensions()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mutable reference to a the response's extensions
|
||||||
|
#[inline]
|
||||||
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
||||||
|
self.res.extensions_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get body of this response
|
||||||
|
#[inline]
|
||||||
|
pub fn body(&self) -> &ResponseBody<B> {
|
||||||
|
self.res.body()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a body
|
||||||
|
pub fn set_body<B2>(self, body: B2) -> HttpResponse<B2> {
|
||||||
|
HttpResponse {
|
||||||
|
res: self.res.set_body(body),
|
||||||
|
error: None,
|
||||||
|
// error: self.error, ??
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Split response and body
|
||||||
|
pub fn into_parts(self) -> (HttpResponse<()>, ResponseBody<B>) {
|
||||||
|
let (head, body) = self.res.into_parts();
|
||||||
|
|
||||||
|
(
|
||||||
|
HttpResponse {
|
||||||
|
res: head,
|
||||||
|
error: None,
|
||||||
|
},
|
||||||
|
body,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drop request's body
|
||||||
|
pub fn drop_body(self) -> HttpResponse<()> {
|
||||||
|
HttpResponse {
|
||||||
|
res: self.res.drop_body(),
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a body and return previous body value
|
||||||
|
pub fn map_body<F, B2>(self, f: F) -> HttpResponse<B2>
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut ResponseHead, ResponseBody<B>) -> ResponseBody<B2>,
|
||||||
|
{
|
||||||
|
HttpResponse {
|
||||||
|
res: self.res.map_body(f),
|
||||||
|
error: self.error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract response body
|
||||||
|
pub fn take_body(&mut self) -> ResponseBody<B> {
|
||||||
|
self.res.take_body()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B: MessageBody> fmt::Debug for HttpResponse<B> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("HttpResponse")
|
||||||
|
.field("error", &self.error)
|
||||||
|
.field("res", &self.res)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B> From<Response<B>> for HttpResponse<B> {
|
||||||
|
fn from(res: Response<B>) -> Self {
|
||||||
|
HttpResponse { res, error: None }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Error> for HttpResponse {
|
||||||
|
fn from(err: Error) -> Self {
|
||||||
|
HttpResponse::from_error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B> From<HttpResponse<B>> for Response<B> {
|
||||||
|
fn from(res: HttpResponse<B>) -> Self {
|
||||||
|
// this impl will always be called as part of dispatcher
|
||||||
|
|
||||||
|
// TODO: expose cause somewhere?
|
||||||
|
// if let Some(err) = res.error {
|
||||||
|
// eprintln!("impl<B> From<HttpResponse<B>> for Response<B> let Some(err)");
|
||||||
|
// return Response::from_error(err).into_body();
|
||||||
|
// }
|
||||||
|
|
||||||
|
res.res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Future for HttpResponse {
|
||||||
|
type Output = Result<Response<Body>, Error>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
if let Some(err) = self.error.take() {
|
||||||
|
return Poll::Ready(Ok(Response::from_error(err).into_body()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Poll::Ready(Ok(mem::replace(
|
||||||
|
&mut self.res,
|
||||||
|
Response::new(StatusCode::default()),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An HTTP response builder.
|
||||||
|
///
|
||||||
|
/// This type can be used to construct an instance of `Response` through a builder-like pattern.
|
||||||
|
pub struct HttpResponseBuilder {
|
||||||
|
head: Option<ResponseHead>,
|
||||||
|
err: Option<HttpError>,
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
cookies: Option<CookieJar>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HttpResponseBuilder {
|
||||||
|
#[inline]
|
||||||
|
/// Create response builder
|
||||||
|
pub fn new(status: StatusCode) -> Self {
|
||||||
|
Self {
|
||||||
|
head: Some(ResponseHead::new(status)),
|
||||||
|
err: None,
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
cookies: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set HTTP status code of this response.
|
||||||
|
#[inline]
|
||||||
|
pub fn status(&mut self, status: StatusCode) -> &mut Self {
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
parts.status = status;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert a header, replacing any that were set with an equivalent field name.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{HttpResponse, http::header};
|
||||||
|
///
|
||||||
|
/// HttpResponse::Ok()
|
||||||
|
/// .insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||||
|
/// .insert_header(("X-TEST", "value"))
|
||||||
|
/// .finish();
|
||||||
|
/// ```
|
||||||
|
pub fn insert_header<H>(&mut self, header: H) -> &mut Self
|
||||||
|
where
|
||||||
|
H: IntoHeaderPair,
|
||||||
|
{
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
match header.try_into_header_pair() {
|
||||||
|
Ok((key, value)) => {
|
||||||
|
parts.headers.insert(key, value);
|
||||||
|
}
|
||||||
|
Err(e) => self.err = Some(e.into()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Append a header, keeping any that were set with an equivalent field name.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{HttpResponse, http::header};
|
||||||
|
///
|
||||||
|
/// HttpResponse::Ok()
|
||||||
|
/// .append_header(header::ContentType(mime::APPLICATION_JSON))
|
||||||
|
/// .append_header(("X-TEST", "value1"))
|
||||||
|
/// .append_header(("X-TEST", "value2"))
|
||||||
|
/// .finish();
|
||||||
|
/// ```
|
||||||
|
pub fn append_header<H>(&mut self, header: H) -> &mut Self
|
||||||
|
where
|
||||||
|
H: IntoHeaderPair,
|
||||||
|
{
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
match header.try_into_header_pair() {
|
||||||
|
Ok((key, value)) => parts.headers.append(key, value),
|
||||||
|
Err(e) => self.err = Some(e.into()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replaced with [`Self::insert_header()`].
|
||||||
|
#[deprecated(
|
||||||
|
since = "4.0.0",
|
||||||
|
note = "Replaced with `insert_header((key, value))`. Will be removed in v5."
|
||||||
|
)]
|
||||||
|
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
||||||
|
where
|
||||||
|
K: TryInto<HeaderName>,
|
||||||
|
K::Error: Into<HttpError>,
|
||||||
|
V: IntoHeaderValue,
|
||||||
|
{
|
||||||
|
if self.err.is_some() {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
match (key.try_into(), value.try_into_value()) {
|
||||||
|
(Ok(name), Ok(value)) => return self.insert_header((name, value)),
|
||||||
|
(Err(err), _) => self.err = Some(err.into()),
|
||||||
|
(_, Err(err)) => self.err = Some(err.into()),
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replaced with [`Self::append_header()`].
|
||||||
|
#[deprecated(
|
||||||
|
since = "4.0.0",
|
||||||
|
note = "Replaced with `append_header((key, value))`. Will be removed in v5."
|
||||||
|
)]
|
||||||
|
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
||||||
|
where
|
||||||
|
K: TryInto<HeaderName>,
|
||||||
|
K::Error: Into<HttpError>,
|
||||||
|
V: IntoHeaderValue,
|
||||||
|
{
|
||||||
|
if self.err.is_some() {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
match (key.try_into(), value.try_into_value()) {
|
||||||
|
(Ok(name), Ok(value)) => return self.append_header((name, value)),
|
||||||
|
(Err(err), _) => self.err = Some(err.into()),
|
||||||
|
(_, Err(err)) => self.err = Some(err.into()),
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the custom reason for the response.
|
||||||
|
#[inline]
|
||||||
|
pub fn reason(&mut self, reason: &'static str) -> &mut Self {
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
parts.reason = Some(reason);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set connection type to KeepAlive
|
||||||
|
#[inline]
|
||||||
|
pub fn keep_alive(&mut self) -> &mut Self {
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
parts.set_connection_type(ConnectionType::KeepAlive);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set connection type to Upgrade
|
||||||
|
#[inline]
|
||||||
|
pub fn upgrade<V>(&mut self, value: V) -> &mut Self
|
||||||
|
where
|
||||||
|
V: IntoHeaderValue,
|
||||||
|
{
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
parts.set_connection_type(ConnectionType::Upgrade);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(value) = value.try_into_value() {
|
||||||
|
self.insert_header((header::UPGRADE, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Force close connection, even if it is marked as keep-alive
|
||||||
|
#[inline]
|
||||||
|
pub fn force_close(&mut self) -> &mut Self {
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
parts.set_connection_type(ConnectionType::Close);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Disable chunked transfer encoding for HTTP/1.1 streaming responses.
|
||||||
|
#[inline]
|
||||||
|
pub fn no_chunking(&mut self, len: u64) -> &mut Self {
|
||||||
|
let mut buf = itoa::Buffer::new();
|
||||||
|
self.insert_header((header::CONTENT_LENGTH, buf.format(len)));
|
||||||
|
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
parts.no_chunking(true);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set response content type.
|
||||||
|
#[inline]
|
||||||
|
pub fn content_type<V>(&mut self, value: V) -> &mut Self
|
||||||
|
where
|
||||||
|
V: IntoHeaderValue,
|
||||||
|
{
|
||||||
|
if let Some(parts) = self.inner() {
|
||||||
|
match value.try_into_value() {
|
||||||
|
Ok(value) => {
|
||||||
|
parts.headers.insert(header::CONTENT_TYPE, value);
|
||||||
|
}
|
||||||
|
Err(e) => self.err = Some(e.into()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a cookie.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{HttpResponse, cookie::Cookie};
|
||||||
|
///
|
||||||
|
/// HttpResponse::Ok()
|
||||||
|
/// .cookie(
|
||||||
|
/// Cookie::build("name", "value")
|
||||||
|
/// .domain("www.rust-lang.org")
|
||||||
|
/// .path("/")
|
||||||
|
/// .secure(true)
|
||||||
|
/// .http_only(true)
|
||||||
|
/// .finish(),
|
||||||
|
/// )
|
||||||
|
/// .finish();
|
||||||
|
/// ```
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
|
||||||
|
if self.cookies.is_none() {
|
||||||
|
let mut jar = CookieJar::new();
|
||||||
|
jar.add(cookie.into_owned());
|
||||||
|
self.cookies = Some(jar)
|
||||||
|
} else {
|
||||||
|
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove cookie.
|
||||||
|
///
|
||||||
|
/// A `Set-Cookie` header is added that will delete a cookie with the same name from the client.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||||
|
///
|
||||||
|
/// async fn handler(req: HttpRequest) -> impl Responder {
|
||||||
|
/// let mut builder = HttpResponse::Ok();
|
||||||
|
///
|
||||||
|
/// if let Some(ref cookie) = req.cookie("name") {
|
||||||
|
/// builder.del_cookie(cookie);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// builder.finish()
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn del_cookie(&mut self, cookie: &Cookie<'_>) -> &mut Self {
|
||||||
|
if self.cookies.is_none() {
|
||||||
|
self.cookies = Some(CookieJar::new())
|
||||||
|
}
|
||||||
|
let jar = self.cookies.as_mut().unwrap();
|
||||||
|
let cookie = cookie.clone().into_owned();
|
||||||
|
jar.add_original(cookie.clone());
|
||||||
|
jar.remove(cookie);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Responses extensions
|
||||||
|
#[inline]
|
||||||
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
||||||
|
let head = self.head.as_ref().expect("cannot reuse response builder");
|
||||||
|
head.extensions()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mutable reference to a the response's extensions
|
||||||
|
#[inline]
|
||||||
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
||||||
|
let head = self.head.as_ref().expect("cannot reuse response builder");
|
||||||
|
head.extensions_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a body and generate `Response`.
|
||||||
|
///
|
||||||
|
/// `HttpResponseBuilder` can not be used after this call.
|
||||||
|
#[inline]
|
||||||
|
pub fn body<B: Into<Body>>(&mut self, body: B) -> HttpResponse {
|
||||||
|
self.message_body(body.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a body and generate `Response`.
|
||||||
|
///
|
||||||
|
/// `HttpResponseBuilder` can not be used after this call.
|
||||||
|
pub fn message_body<B>(&mut self, body: B) -> HttpResponse<B> {
|
||||||
|
if let Some(err) = self.err.take() {
|
||||||
|
return HttpResponse::from_error(Error::from(err)).into_body();
|
||||||
|
}
|
||||||
|
|
||||||
|
// allow unused mut when cookies feature is disabled
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut head = self.head.take().expect("cannot reuse response builder");
|
||||||
|
|
||||||
|
let mut res = HttpResponse::with_body(StatusCode::OK, body);
|
||||||
|
*res.head_mut() = head;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
if let Some(ref jar) = self.cookies {
|
||||||
|
for cookie in jar.delta() {
|
||||||
|
match HeaderValue::from_str(&cookie.to_string()) {
|
||||||
|
Ok(val) => res.headers_mut().append(header::SET_COOKIE, val),
|
||||||
|
Err(err) => return HttpResponse::from_error(Error::from(err)).into_body(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a streaming body and generate `Response`.
|
||||||
|
///
|
||||||
|
/// `HttpResponseBuilder` can not be used after this call.
|
||||||
|
#[inline]
|
||||||
|
pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponse
|
||||||
|
where
|
||||||
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
||||||
|
E: Into<Error> + 'static,
|
||||||
|
{
|
||||||
|
self.body(Body::from_message(BodyStream::new(stream)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a json body and generate `Response`
|
||||||
|
///
|
||||||
|
/// `HttpResponseBuilder` can not be used after this call.
|
||||||
|
pub fn json(&mut self, value: impl Serialize) -> HttpResponse {
|
||||||
|
match serde_json::to_string(&value) {
|
||||||
|
Ok(body) => {
|
||||||
|
let contains = if let Some(parts) = self.inner() {
|
||||||
|
parts.headers.contains_key(header::CONTENT_TYPE)
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
|
if !contains {
|
||||||
|
self.insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.body(Body::from(body))
|
||||||
|
}
|
||||||
|
Err(err) => HttpResponse::from_error(JsonPayloadError::Serialize(err).into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set an empty body and generate `Response`
|
||||||
|
///
|
||||||
|
/// `HttpResponseBuilder` can not be used after this call.
|
||||||
|
#[inline]
|
||||||
|
pub fn finish(&mut self) -> HttpResponse {
|
||||||
|
self.body(Body::Empty)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This method construct new `HttpResponseBuilder`
|
||||||
|
pub fn take(&mut self) -> Self {
|
||||||
|
Self {
|
||||||
|
head: self.head.take(),
|
||||||
|
err: self.err.take(),
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
cookies: self.cookies.take(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn inner(&mut self) -> Option<&mut ResponseHead> {
|
||||||
|
if self.err.is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.head.as_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<HttpResponseBuilder> for HttpResponse {
|
||||||
|
fn from(mut builder: HttpResponseBuilder) -> Self {
|
||||||
|
builder.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<HttpResponseBuilder> for Response<Body> {
|
||||||
|
fn from(mut builder: HttpResponseBuilder) -> Self {
|
||||||
|
builder.finish().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Future for HttpResponseBuilder {
|
||||||
|
type Output = Result<HttpResponse, Error>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
eprintln!("httpresponse future error");
|
||||||
|
Poll::Ready(Ok(self.finish()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub struct CookieIter<'a> {
|
||||||
|
iter: header::GetAll<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
impl<'a> Iterator for CookieIter<'a> {
|
||||||
|
type Item = Cookie<'a>;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn next(&mut self) -> Option<Cookie<'a>> {
|
||||||
|
for v in self.iter.by_ref() {
|
||||||
|
if let Ok(c) = Cookie::parse_encoded(v.to_str().ok()?) {
|
||||||
|
return Some(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod http_codes {
|
||||||
|
//! Status code based HTTP response builders.
|
||||||
|
|
||||||
|
use actix_http::http::StatusCode;
|
||||||
|
|
||||||
|
use super::{HttpResponse, HttpResponseBuilder};
|
||||||
|
|
||||||
|
macro_rules! static_resp {
|
||||||
|
($name:ident, $status:expr) => {
|
||||||
|
#[allow(non_snake_case, missing_docs)]
|
||||||
|
pub fn $name() -> HttpResponseBuilder {
|
||||||
|
HttpResponseBuilder::new($status)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HttpResponse {
|
||||||
|
static_resp!(Continue, StatusCode::CONTINUE);
|
||||||
|
static_resp!(SwitchingProtocols, StatusCode::SWITCHING_PROTOCOLS);
|
||||||
|
static_resp!(Processing, StatusCode::PROCESSING);
|
||||||
|
|
||||||
|
static_resp!(Ok, StatusCode::OK);
|
||||||
|
static_resp!(Created, StatusCode::CREATED);
|
||||||
|
static_resp!(Accepted, StatusCode::ACCEPTED);
|
||||||
|
static_resp!(
|
||||||
|
NonAuthoritativeInformation,
|
||||||
|
StatusCode::NON_AUTHORITATIVE_INFORMATION
|
||||||
|
);
|
||||||
|
|
||||||
|
static_resp!(NoContent, StatusCode::NO_CONTENT);
|
||||||
|
static_resp!(ResetContent, StatusCode::RESET_CONTENT);
|
||||||
|
static_resp!(PartialContent, StatusCode::PARTIAL_CONTENT);
|
||||||
|
static_resp!(MultiStatus, StatusCode::MULTI_STATUS);
|
||||||
|
static_resp!(AlreadyReported, StatusCode::ALREADY_REPORTED);
|
||||||
|
|
||||||
|
static_resp!(MultipleChoices, StatusCode::MULTIPLE_CHOICES);
|
||||||
|
static_resp!(MovedPermanently, StatusCode::MOVED_PERMANENTLY);
|
||||||
|
static_resp!(Found, StatusCode::FOUND);
|
||||||
|
static_resp!(SeeOther, StatusCode::SEE_OTHER);
|
||||||
|
static_resp!(NotModified, StatusCode::NOT_MODIFIED);
|
||||||
|
static_resp!(UseProxy, StatusCode::USE_PROXY);
|
||||||
|
static_resp!(TemporaryRedirect, StatusCode::TEMPORARY_REDIRECT);
|
||||||
|
static_resp!(PermanentRedirect, StatusCode::PERMANENT_REDIRECT);
|
||||||
|
|
||||||
|
static_resp!(BadRequest, StatusCode::BAD_REQUEST);
|
||||||
|
static_resp!(NotFound, StatusCode::NOT_FOUND);
|
||||||
|
static_resp!(Unauthorized, StatusCode::UNAUTHORIZED);
|
||||||
|
static_resp!(PaymentRequired, StatusCode::PAYMENT_REQUIRED);
|
||||||
|
static_resp!(Forbidden, StatusCode::FORBIDDEN);
|
||||||
|
static_resp!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED);
|
||||||
|
static_resp!(NotAcceptable, StatusCode::NOT_ACCEPTABLE);
|
||||||
|
static_resp!(
|
||||||
|
ProxyAuthenticationRequired,
|
||||||
|
StatusCode::PROXY_AUTHENTICATION_REQUIRED
|
||||||
|
);
|
||||||
|
static_resp!(RequestTimeout, StatusCode::REQUEST_TIMEOUT);
|
||||||
|
static_resp!(Conflict, StatusCode::CONFLICT);
|
||||||
|
static_resp!(Gone, StatusCode::GONE);
|
||||||
|
static_resp!(LengthRequired, StatusCode::LENGTH_REQUIRED);
|
||||||
|
static_resp!(PreconditionFailed, StatusCode::PRECONDITION_FAILED);
|
||||||
|
static_resp!(PreconditionRequired, StatusCode::PRECONDITION_REQUIRED);
|
||||||
|
static_resp!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
|
static_resp!(UriTooLong, StatusCode::URI_TOO_LONG);
|
||||||
|
static_resp!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||||
|
static_resp!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
|
||||||
|
static_resp!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
|
||||||
|
static_resp!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
|
||||||
|
static_resp!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS);
|
||||||
|
static_resp!(
|
||||||
|
RequestHeaderFieldsTooLarge,
|
||||||
|
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE
|
||||||
|
);
|
||||||
|
static_resp!(
|
||||||
|
UnavailableForLegalReasons,
|
||||||
|
StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS
|
||||||
|
);
|
||||||
|
|
||||||
|
static_resp!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
static_resp!(NotImplemented, StatusCode::NOT_IMPLEMENTED);
|
||||||
|
static_resp!(BadGateway, StatusCode::BAD_GATEWAY);
|
||||||
|
static_resp!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
static_resp!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT);
|
||||||
|
static_resp!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
||||||
|
static_resp!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES);
|
||||||
|
static_resp!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE);
|
||||||
|
static_resp!(LoopDetected, StatusCode::LOOP_DETECTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::dev::Body;
|
||||||
|
use crate::http::StatusCode;
|
||||||
|
use crate::HttpResponse;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build() {
|
||||||
|
let resp = HttpResponse::Ok().body(Body::Empty);
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
|
use super::{HttpResponse, HttpResponseBuilder};
|
||||||
|
use crate::dev::{Body, MessageBody, ResponseBody};
|
||||||
|
use crate::http::header::{self, HeaderValue, CONTENT_TYPE, COOKIE};
|
||||||
|
use crate::http::StatusCode;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_debug() {
|
||||||
|
let resp = HttpResponse::Ok()
|
||||||
|
.append_header((COOKIE, HeaderValue::from_static("cookie1=value1; ")))
|
||||||
|
.append_header((COOKIE, HeaderValue::from_static("cookie2=value2; ")))
|
||||||
|
.finish();
|
||||||
|
let dbg = format!("{:?}", resp);
|
||||||
|
assert!(dbg.contains("HttpResponse"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_basic_builder() {
|
||||||
|
let resp = HttpResponse::Ok()
|
||||||
|
.insert_header(("X-TEST", "value"))
|
||||||
|
.finish();
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_upgrade() {
|
||||||
|
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||||
|
.upgrade("websocket")
|
||||||
|
.finish();
|
||||||
|
assert!(resp.upgrade());
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::UPGRADE).unwrap(),
|
||||||
|
HeaderValue::from_static("websocket")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_force_close() {
|
||||||
|
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||||
|
.force_close()
|
||||||
|
.finish();
|
||||||
|
assert!(!resp.keep_alive())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_content_type() {
|
||||||
|
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||||
|
.content_type("text/plain")
|
||||||
|
.body(Body::Empty);
|
||||||
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_body<B>(mut body: ResponseBody<B>) -> Bytes
|
||||||
|
where
|
||||||
|
B: MessageBody + Unpin,
|
||||||
|
{
|
||||||
|
use futures_util::StreamExt as _;
|
||||||
|
|
||||||
|
let mut bytes = BytesMut::new();
|
||||||
|
while let Some(item) = body.next().await {
|
||||||
|
bytes.extend_from_slice(&item.unwrap());
|
||||||
|
}
|
||||||
|
bytes.freeze()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_json() {
|
||||||
|
let mut resp = HttpResponse::Ok().json(vec!["v1", "v2", "v3"]);
|
||||||
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
||||||
|
assert_eq!(
|
||||||
|
read_body(resp.take_body()).await.as_ref(),
|
||||||
|
br#"["v1","v2","v3"]"#
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut resp = HttpResponse::Ok().json(&["v1", "v2", "v3"]);
|
||||||
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
||||||
|
assert_eq!(
|
||||||
|
read_body(resp.take_body()).await.as_ref(),
|
||||||
|
br#"["v1","v2","v3"]"#
|
||||||
|
);
|
||||||
|
|
||||||
|
// content type override
|
||||||
|
let mut resp = HttpResponse::Ok()
|
||||||
|
.insert_header((CONTENT_TYPE, "text/json"))
|
||||||
|
.json(&vec!["v1", "v2", "v3"]);
|
||||||
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
|
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
||||||
|
assert_eq!(
|
||||||
|
read_body(resp.take_body()).await.as_ref(),
|
||||||
|
br#"["v1","v2","v3"]"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_serde_json_in_body() {
|
||||||
|
let mut resp = HttpResponse::Ok().body(
|
||||||
|
serde_json::to_vec(&serde_json::json!({ "test-key": "test-value" })).unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
read_body(resp.take_body()).await.as_ref(),
|
||||||
|
br#"{"test-key":"test-value"}"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn response_builder_header_insert_kv() {
|
||||||
|
let mut res = HttpResponse::Ok();
|
||||||
|
res.insert_header(("Content-Type", "application/octet-stream"));
|
||||||
|
let res = res.finish();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
res.headers().get("Content-Type"),
|
||||||
|
Some(&HeaderValue::from_static("application/octet-stream"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn response_builder_header_insert_typed() {
|
||||||
|
let mut res = HttpResponse::Ok();
|
||||||
|
res.insert_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
|
||||||
|
let res = res.finish();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
res.headers().get("Content-Type"),
|
||||||
|
Some(&HeaderValue::from_static("application/octet-stream"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn response_builder_header_append_kv() {
|
||||||
|
let mut res = HttpResponse::Ok();
|
||||||
|
res.append_header(("Content-Type", "application/octet-stream"));
|
||||||
|
res.append_header(("Content-Type", "application/json"));
|
||||||
|
let res = res.finish();
|
||||||
|
|
||||||
|
let headers: Vec<_> = res.headers().get_all("Content-Type").cloned().collect();
|
||||||
|
assert_eq!(headers.len(), 2);
|
||||||
|
assert!(headers.contains(&HeaderValue::from_static("application/octet-stream")));
|
||||||
|
assert!(headers.contains(&HeaderValue::from_static("application/json")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn response_builder_header_append_typed() {
|
||||||
|
let mut res = HttpResponse::Ok();
|
||||||
|
res.append_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
|
||||||
|
res.append_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
|
||||||
|
let res = res.finish();
|
||||||
|
|
||||||
|
let headers: Vec<_> = res.headers().get_all("Content-Type").cloned().collect();
|
||||||
|
assert_eq!(headers.len(), 2);
|
||||||
|
assert!(headers.contains(&HeaderValue::from_static("application/octet-stream")));
|
||||||
|
assert!(headers.contains(&HeaderValue::from_static("application/json")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,12 +10,15 @@ use actix_http::{
|
||||||
use actix_router::{IntoPattern, Path, Resource, ResourceDef, Url};
|
use actix_router::{IntoPattern, Path, Resource, ResourceDef, Url};
|
||||||
use actix_service::{IntoServiceFactory, ServiceFactory};
|
use actix_service::{IntoServiceFactory, ServiceFactory};
|
||||||
|
|
||||||
use crate::config::{AppConfig, AppService};
|
|
||||||
use crate::dev::insert_slash;
|
use crate::dev::insert_slash;
|
||||||
use crate::guard::Guard;
|
use crate::guard::Guard;
|
||||||
use crate::info::ConnectionInfo;
|
use crate::info::ConnectionInfo;
|
||||||
use crate::request::HttpRequest;
|
use crate::request::HttpRequest;
|
||||||
use crate::rmap::ResourceMap;
|
use crate::rmap::ResourceMap;
|
||||||
|
use crate::{
|
||||||
|
config::{AppConfig, AppService},
|
||||||
|
HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait HttpServiceFactory {
|
pub trait HttpServiceFactory {
|
||||||
fn register(self, config: &mut AppService);
|
fn register(self, config: &mut AppService);
|
||||||
|
@ -99,13 +102,14 @@ impl ServiceRequest {
|
||||||
/// Create service response
|
/// Create service response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
|
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
|
||||||
ServiceResponse::new(self.req, res.into())
|
let res = HttpResponse::from(res.into());
|
||||||
|
ServiceResponse::new(self.req, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create service response for error
|
/// Create service response for error
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn error_response<B, E: Into<Error>>(self, err: E) -> ServiceResponse<B> {
|
pub fn error_response<B, E: Into<Error>>(self, err: E) -> ServiceResponse<B> {
|
||||||
let res: Response = err.into().into();
|
let res = HttpResponse::from_error(err.into());
|
||||||
ServiceResponse::new(self.req, res.into_body())
|
ServiceResponse::new(self.req, res.into_body())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,23 +319,19 @@ impl fmt::Debug for ServiceRequest {
|
||||||
|
|
||||||
pub struct ServiceResponse<B = Body> {
|
pub struct ServiceResponse<B = Body> {
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
response: Response<B>,
|
response: HttpResponse<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B> ServiceResponse<B> {
|
impl<B> ServiceResponse<B> {
|
||||||
/// Create service response instance
|
/// Create service response instance
|
||||||
pub fn new(request: HttpRequest, response: Response<B>) -> Self {
|
pub fn new(request: HttpRequest, response: HttpResponse<B>) -> Self {
|
||||||
ServiceResponse { request, response }
|
ServiceResponse { request, response }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create service response from the error
|
/// Create service response from the error
|
||||||
pub fn from_err<E: Into<Error>>(err: E, request: HttpRequest) -> Self {
|
pub fn from_err<E: Into<Error>>(err: E, request: HttpRequest) -> Self {
|
||||||
let e: Error = err.into();
|
let response = HttpResponse::from_error(err.into()).into_body();
|
||||||
let res: Response = e.into();
|
ServiceResponse { request, response }
|
||||||
ServiceResponse {
|
|
||||||
request,
|
|
||||||
response: res.into_body(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create service response for error
|
/// Create service response for error
|
||||||
|
@ -342,7 +342,7 @@ impl<B> ServiceResponse<B> {
|
||||||
|
|
||||||
/// Create service response
|
/// Create service response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_response<B1>(self, response: Response<B1>) -> ServiceResponse<B1> {
|
pub fn into_response<B1>(self, response: HttpResponse<B1>) -> ServiceResponse<B1> {
|
||||||
ServiceResponse::new(self.request, response)
|
ServiceResponse::new(self.request, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,13 +354,13 @@ impl<B> ServiceResponse<B> {
|
||||||
|
|
||||||
/// Get reference to response
|
/// Get reference to response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn response(&self) -> &Response<B> {
|
pub fn response(&self) -> &HttpResponse<B> {
|
||||||
&self.response
|
&self.response
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get mutable reference to response
|
/// Get mutable reference to response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn response_mut(&mut self) -> &mut Response<B> {
|
pub fn response_mut(&mut self) -> &mut HttpResponse<B> {
|
||||||
&mut self.response
|
&mut self.response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -376,8 +376,8 @@ impl<B> ServiceResponse<B> {
|
||||||
self.response.headers()
|
self.response.headers()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Returns mutable response's headers.
|
/// Returns mutable response's headers.
|
||||||
|
#[inline]
|
||||||
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||||
self.response.headers_mut()
|
self.response.headers_mut()
|
||||||
}
|
}
|
||||||
|
@ -391,7 +391,7 @@ impl<B> ServiceResponse<B> {
|
||||||
match f(&mut self) {
|
match f(&mut self) {
|
||||||
Ok(_) => self,
|
Ok(_) => self,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let res: Response = err.into().into();
|
let res = HttpResponse::from_error(err.into());
|
||||||
ServiceResponse::new(self.request, res.into_body())
|
ServiceResponse::new(self.request, res.into_body())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -418,9 +418,15 @@ impl<B> ServiceResponse<B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<B> From<ServiceResponse<B>> for HttpResponse<B> {
|
||||||
|
fn from(res: ServiceResponse<B>) -> HttpResponse<B> {
|
||||||
|
res.response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<B> From<ServiceResponse<B>> for Response<B> {
|
impl<B> From<ServiceResponse<B>> for Response<B> {
|
||||||
fn from(res: ServiceResponse<B>) -> Response<B> {
|
fn from(res: ServiceResponse<B>) -> Response<B> {
|
||||||
res.response
|
res.response.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
48
src/test.rs
48
src/test.rs
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
use std::{net::SocketAddr, rc::Rc};
|
use std::{net::SocketAddr, rc::Rc};
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use actix_http::cookie::Cookie;
|
|
||||||
pub use actix_http::test::TestBuffer;
|
pub use actix_http::test::TestBuffer;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
http::{header::IntoHeaderPair, Method, StatusCode, Uri, Version},
|
http::{header::IntoHeaderPair, Method, StatusCode, Uri, Version},
|
||||||
|
@ -17,6 +15,8 @@ use futures_core::Stream;
|
||||||
use futures_util::StreamExt as _;
|
use futures_util::StreamExt as _;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
use crate::{
|
use crate::{
|
||||||
app_service::AppInitServiceState,
|
app_service::AppInitServiceState,
|
||||||
config::AppConfig,
|
config::AppConfig,
|
||||||
|
@ -26,7 +26,7 @@ use crate::{
|
||||||
rmap::ResourceMap,
|
rmap::ResourceMap,
|
||||||
service::{ServiceRequest, ServiceResponse},
|
service::{ServiceRequest, ServiceResponse},
|
||||||
web::{Bytes, BytesMut},
|
web::{Bytes, BytesMut},
|
||||||
Error, HttpRequest, HttpResponse,
|
Error, HttpRequest, HttpResponse, HttpResponseBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Create service that always responds with `HttpResponse::Ok()` and no body.
|
/// Create service that always responds with `HttpResponse::Ok()` and no body.
|
||||||
|
@ -40,7 +40,7 @@ pub fn default_service(
|
||||||
status_code: StatusCode,
|
status_code: StatusCode,
|
||||||
) -> impl Service<ServiceRequest, Response = ServiceResponse<Body>, Error = Error> {
|
) -> impl Service<ServiceRequest, Response = ServiceResponse<Body>, Error = Error> {
|
||||||
(move |req: ServiceRequest| {
|
(move |req: ServiceRequest| {
|
||||||
ok(req.into_response(HttpResponse::build(status_code).finish()))
|
ok(req.into_response(HttpResponseBuilder::new(status_code).finish()))
|
||||||
})
|
})
|
||||||
.into_service()
|
.into_service()
|
||||||
}
|
}
|
||||||
|
@ -359,6 +359,8 @@ pub struct TestRequest {
|
||||||
path: Path<Url>,
|
path: Path<Url>,
|
||||||
peer_addr: Option<SocketAddr>,
|
peer_addr: Option<SocketAddr>,
|
||||||
app_data: Extensions,
|
app_data: Extensions,
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
cookies: CookieJar,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TestRequest {
|
impl Default for TestRequest {
|
||||||
|
@ -370,6 +372,8 @@ impl Default for TestRequest {
|
||||||
path: Path::new(Url::new(Uri::default())),
|
path: Path::new(Url::new(Uri::default())),
|
||||||
peer_addr: None,
|
peer_addr: None,
|
||||||
app_data: Extensions::new(),
|
app_data: Extensions::new(),
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
cookies: CookieJar::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -445,7 +449,7 @@ impl TestRequest {
|
||||||
/// Set cookie for this request.
|
/// Set cookie for this request.
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
||||||
self.req.cookie(cookie);
|
self.cookies.add(cookie.into_owned());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -507,16 +511,42 @@ impl TestRequest {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn finish(&mut self) -> Request {
|
||||||
|
// mut used when cookie feature is enabled
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut req = self.req.finish();
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
{
|
||||||
|
use actix_http::http::header::{HeaderValue, COOKIE};
|
||||||
|
|
||||||
|
let cookie: String = self
|
||||||
|
.cookies
|
||||||
|
.delta()
|
||||||
|
// ensure only name=value is written to cookie header
|
||||||
|
.map(|c| c.stripped().encoded().to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("; ");
|
||||||
|
|
||||||
|
if !cookie.is_empty() {
|
||||||
|
req.headers_mut()
|
||||||
|
.insert(COOKIE, HeaderValue::from_str(&cookie).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req
|
||||||
|
}
|
||||||
|
|
||||||
/// Complete request creation and generate `Request` instance
|
/// Complete request creation and generate `Request` instance
|
||||||
pub fn to_request(mut self) -> Request {
|
pub fn to_request(mut self) -> Request {
|
||||||
let mut req = self.req.finish();
|
let mut req = self.finish();
|
||||||
req.head_mut().peer_addr = self.peer_addr;
|
req.head_mut().peer_addr = self.peer_addr;
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Complete request creation and generate `ServiceRequest` instance
|
/// Complete request creation and generate `ServiceRequest` instance
|
||||||
pub fn to_srv_request(mut self) -> ServiceRequest {
|
pub fn to_srv_request(mut self) -> ServiceRequest {
|
||||||
let (mut head, payload) = self.req.finish().into_parts();
|
let (mut head, payload) = self.finish().into_parts();
|
||||||
head.peer_addr = self.peer_addr;
|
head.peer_addr = self.peer_addr;
|
||||||
self.path.get_mut().update(&head.uri);
|
self.path.get_mut().update(&head.uri);
|
||||||
|
|
||||||
|
@ -535,7 +565,7 @@ impl TestRequest {
|
||||||
|
|
||||||
/// Complete request creation and generate `HttpRequest` instance
|
/// Complete request creation and generate `HttpRequest` instance
|
||||||
pub fn to_http_request(mut self) -> HttpRequest {
|
pub fn to_http_request(mut self) -> HttpRequest {
|
||||||
let (mut head, _) = self.req.finish().into_parts();
|
let (mut head, _) = self.finish().into_parts();
|
||||||
head.peer_addr = self.peer_addr;
|
head.peer_addr = self.peer_addr;
|
||||||
self.path.get_mut().update(&head.uri);
|
self.path.get_mut().update(&head.uri);
|
||||||
|
|
||||||
|
@ -546,7 +576,7 @@ impl TestRequest {
|
||||||
|
|
||||||
/// Complete request creation and generate `HttpRequest` and `Payload` instances
|
/// Complete request creation and generate `HttpRequest` and `Payload` instances
|
||||||
pub fn to_http_parts(mut self) -> (HttpRequest, Payload) {
|
pub fn to_http_parts(mut self) -> (HttpRequest, Payload) {
|
||||||
let (mut head, payload) = self.req.finish().into_parts();
|
let (mut head, payload) = self.finish().into_parts();
|
||||||
head.peer_addr = self.peer_addr;
|
head.peer_addr = self.peer_addr;
|
||||||
self.path.get_mut().update(&head.uri);
|
self.path.get_mut().update(&head.uri);
|
||||||
|
|
||||||
|
|
|
@ -80,7 +80,7 @@ use crate::{
|
||||||
/// })
|
/// })
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct Form<T>(pub T);
|
pub struct Form<T>(pub T);
|
||||||
|
|
||||||
impl<T> Form<T> {
|
impl<T> Form<T> {
|
||||||
|
@ -150,12 +150,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: fmt::Debug> fmt::Debug for Form<T> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
self.0.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for Form<T> {
|
impl<T: fmt::Display> fmt::Display for Form<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
|
@ -169,7 +163,7 @@ impl<T: Serialize> Responder for Form<T> {
|
||||||
Ok(body) => HttpResponse::Ok()
|
Ok(body) => HttpResponse::Ok()
|
||||||
.content_type(mime::APPLICATION_WWW_FORM_URLENCODED)
|
.content_type(mime::APPLICATION_WWW_FORM_URLENCODED)
|
||||||
.body(body),
|
.body(body),
|
||||||
Err(err) => HttpResponse::from_error(err.into()),
|
Err(err) => HttpResponse::from_error(UrlencodedError::Serialize(err).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -352,14 +346,14 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
if encoding == UTF_8 {
|
if encoding == UTF_8 {
|
||||||
serde_urlencoded::from_bytes::<T>(&body).map_err(|_| UrlencodedError::Parse)
|
serde_urlencoded::from_bytes::<T>(&body).map_err(UrlencodedError::Parse)
|
||||||
} else {
|
} else {
|
||||||
let body = encoding
|
let body = encoding
|
||||||
.decode_without_bom_handling_and_without_replacement(&body)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map(|s| s.into_owned())
|
.map(|s| s.into_owned())
|
||||||
.ok_or(UrlencodedError::Parse)?;
|
.ok_or(UrlencodedError::Encoding)?;
|
||||||
|
|
||||||
serde_urlencoded::from_str::<T>(&body).map_err(|_| UrlencodedError::Parse)
|
serde_urlencoded::from_str::<T>(&body).map_err(UrlencodedError::Parse)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.boxed_local(),
|
.boxed_local(),
|
||||||
|
|
|
@ -23,7 +23,7 @@ use crate::{
|
||||||
/// format!("Request was sent at {}", date.to_string())
|
/// format!("Request was sent at {}", date.to_string())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct Header<T>(pub T);
|
pub struct Header<T>(pub T);
|
||||||
|
|
||||||
impl<T> Header<T> {
|
impl<T> Header<T> {
|
||||||
|
@ -47,15 +47,6 @@ impl<T> ops::DerefMut for Header<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> fmt::Debug for Header<T>
|
|
||||||
where
|
|
||||||
T: fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "Header: {:?}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Display for Header<T>
|
impl<T> fmt::Display for Header<T>
|
||||||
where
|
where
|
||||||
T: fmt::Display,
|
T: fmt::Display,
|
||||||
|
|
|
@ -73,6 +73,7 @@ use crate::{
|
||||||
/// })
|
/// })
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Json<T>(pub T);
|
pub struct Json<T>(pub T);
|
||||||
|
|
||||||
impl<T> Json<T> {
|
impl<T> Json<T> {
|
||||||
|
@ -96,15 +97,6 @@ impl<T> ops::DerefMut for Json<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> fmt::Debug for Json<T>
|
|
||||||
where
|
|
||||||
T: fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "Json: {:?}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Display for Json<T>
|
impl<T> fmt::Display for Json<T>
|
||||||
where
|
where
|
||||||
T: fmt::Display,
|
T: fmt::Display,
|
||||||
|
@ -135,7 +127,7 @@ impl<T: Serialize> Responder for Json<T> {
|
||||||
Ok(body) => HttpResponse::Ok()
|
Ok(body) => HttpResponse::Ok()
|
||||||
.content_type(mime::APPLICATION_JSON)
|
.content_type(mime::APPLICATION_JSON)
|
||||||
.body(body),
|
.body(body),
|
||||||
Err(err) => HttpResponse::from_error(err.into()),
|
Err(err) => HttpResponse::from_error(JsonPayloadError::Serialize(err).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,7 +225,7 @@ where
|
||||||
/// .content_type(|mime| mime == mime::TEXT_PLAIN)
|
/// .content_type(|mime| mime == mime::TEXT_PLAIN)
|
||||||
/// // use custom error handler
|
/// // use custom error handler
|
||||||
/// .error_handler(|err, req| {
|
/// .error_handler(|err, req| {
|
||||||
/// error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
|
/// error::InternalError::from_response(err, HttpResponse::Conflict().into()).into()
|
||||||
/// });
|
/// });
|
||||||
///
|
///
|
||||||
/// App::new()
|
/// App::new()
|
||||||
|
@ -420,7 +412,8 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let json = serde_json::from_slice::<T>(&buf)?;
|
let json = serde_json::from_slice::<T>(&buf)
|
||||||
|
.map_err(JsonPayloadError::Deserialize)?;
|
||||||
return Poll::Ready(Ok(json));
|
return Poll::Ready(Ok(json));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -494,7 +487,7 @@ mod tests {
|
||||||
};
|
};
|
||||||
let resp =
|
let resp =
|
||||||
HttpResponse::BadRequest().body(serde_json::to_string(&msg).unwrap());
|
HttpResponse::BadRequest().body(serde_json::to_string(&msg).unwrap());
|
||||||
InternalError::from_response(err, resp).into()
|
InternalError::from_response(err, resp.into()).into()
|
||||||
}))
|
}))
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ use crate::{dev::Payload, error::PathError, FromRequest, HttpRequest};
|
||||||
/// format!("Welcome {}!", info.name)
|
/// format!("Welcome {}!", info.name)
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct Path<T>(T);
|
pub struct Path<T>(T);
|
||||||
|
|
||||||
impl<T> Path<T> {
|
impl<T> Path<T> {
|
||||||
|
@ -81,12 +81,6 @@ impl<T> From<T> for Path<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: fmt::Debug> fmt::Debug for Path<T> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
self.0.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for Path<T> {
|
impl<T: fmt::Display> fmt::Display for Path<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
|
@ -99,7 +93,7 @@ where
|
||||||
T: de::DeserializeOwned,
|
T: de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self, Error>>;
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
type Config = PathConfig;
|
type Config = PathConfig;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -112,17 +106,17 @@ where
|
||||||
ready(
|
ready(
|
||||||
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
||||||
.map(Path)
|
.map(Path)
|
||||||
.map_err(move |e| {
|
.map_err(move |err| {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Failed during Path extractor deserialization. \
|
"Failed during Path extractor deserialization. \
|
||||||
Request path: {:?}",
|
Request path: {:?}",
|
||||||
req.path()
|
req.path()
|
||||||
);
|
);
|
||||||
if let Some(error_handler) = error_handler {
|
if let Some(error_handler) = error_handler {
|
||||||
let e = PathError::Deserialize(e);
|
let e = PathError::Deserialize(err);
|
||||||
(error_handler)(e, req)
|
(error_handler)(e, req)
|
||||||
} else {
|
} else {
|
||||||
ErrorNotFound(e)
|
ErrorNotFound(err)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -155,7 +149,7 @@ where
|
||||||
/// .app_data(PathConfig::default().error_handler(|err, req| {
|
/// .app_data(PathConfig::default().error_handler(|err, req| {
|
||||||
/// error::InternalError::from_response(
|
/// error::InternalError::from_response(
|
||||||
/// err,
|
/// err,
|
||||||
/// HttpResponse::Conflict().finish(),
|
/// HttpResponse::Conflict().into(),
|
||||||
/// )
|
/// )
|
||||||
/// .into()
|
/// .into()
|
||||||
/// }))
|
/// }))
|
||||||
|
@ -261,7 +255,7 @@ mod tests {
|
||||||
assert_eq!(s.value, "user2");
|
assert_eq!(s.value, "user2");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}, {:?}", s, s),
|
format!("{}, {:?}", s, s),
|
||||||
"MyStruct(name, user2), MyStruct { key: \"name\", value: \"user2\" }"
|
"MyStruct(name, user2), Path(MyStruct { key: \"name\", value: \"user2\" })"
|
||||||
);
|
);
|
||||||
let s = s.into_inner();
|
let s = s.into_inner();
|
||||||
assert_eq!(s.value, "user2");
|
assert_eq!(s.value, "user2");
|
||||||
|
@ -298,7 +292,10 @@ mod tests {
|
||||||
async fn test_custom_err_handler() {
|
async fn test_custom_err_handler() {
|
||||||
let (req, mut pl) = TestRequest::with_uri("/name/user1/")
|
let (req, mut pl) = TestRequest::with_uri("/name/user1/")
|
||||||
.app_data(PathConfig::default().error_handler(|err, _| {
|
.app_data(PathConfig::default().error_handler(|err, _| {
|
||||||
error::InternalError::from_response(err, HttpResponse::Conflict().finish())
|
error::InternalError::from_response(
|
||||||
|
err,
|
||||||
|
HttpResponse::Conflict().finish().into(),
|
||||||
|
)
|
||||||
.into()
|
.into()
|
||||||
}))
|
}))
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
@ -306,7 +303,7 @@ mod tests {
|
||||||
let s = Path::<(usize,)>::from_request(&req, &mut pl)
|
let s = Path::<(usize,)>::from_request(&req, &mut pl)
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
let res: HttpResponse = s.into();
|
let res = HttpResponse::from_error(s);
|
||||||
|
|
||||||
assert_eq!(res.status(), http::StatusCode::CONFLICT);
|
assert_eq!(res.status(), http::StatusCode::CONFLICT);
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequ
|
||||||
/// "OK".to_string()
|
/// "OK".to_string()
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct Query<T>(pub T);
|
pub struct Query<T>(pub T);
|
||||||
|
|
||||||
impl<T> Query<T> {
|
impl<T> Query<T> {
|
||||||
|
@ -100,12 +100,6 @@ impl<T> ops::DerefMut for Query<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: fmt::Debug> fmt::Debug for Query<T> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
self.0.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for Query<T> {
|
impl<T: fmt::Display> fmt::Display for Query<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
|
@ -172,7 +166,7 @@ where
|
||||||
/// let query_cfg = web::QueryConfig::default()
|
/// let query_cfg = web::QueryConfig::default()
|
||||||
/// // use custom error handler
|
/// // use custom error handler
|
||||||
/// .error_handler(|err, req| {
|
/// .error_handler(|err, req| {
|
||||||
/// error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
|
/// error::InternalError::from_response(err, HttpResponse::Conflict().into()).into()
|
||||||
/// });
|
/// });
|
||||||
///
|
///
|
||||||
/// App::new()
|
/// App::new()
|
||||||
|
@ -226,7 +220,10 @@ mod tests {
|
||||||
let mut s = Query::<Id>::from_query(&req.query_string()).unwrap();
|
let mut s = Query::<Id>::from_query(&req.query_string()).unwrap();
|
||||||
|
|
||||||
assert_eq!(s.id, "test");
|
assert_eq!(s.id, "test");
|
||||||
assert_eq!(format!("{}, {:?}", s, s), "test, Id { id: \"test\" }");
|
assert_eq!(
|
||||||
|
format!("{}, {:?}", s, s),
|
||||||
|
"test, Query(Id { id: \"test\" })"
|
||||||
|
);
|
||||||
|
|
||||||
s.id = "test1".to_string();
|
s.id = "test1".to_string();
|
||||||
let s = s.into_inner();
|
let s = s.into_inner();
|
||||||
|
@ -244,7 +241,10 @@ mod tests {
|
||||||
|
|
||||||
let mut s = Query::<Id>::from_request(&req, &mut pl).await.unwrap();
|
let mut s = Query::<Id>::from_request(&req, &mut pl).await.unwrap();
|
||||||
assert_eq!(s.id, "test");
|
assert_eq!(s.id, "test");
|
||||||
assert_eq!(format!("{}, {:?}", s, s), "test, Id { id: \"test\" }");
|
assert_eq!(
|
||||||
|
format!("{}, {:?}", s, s),
|
||||||
|
"test, Query(Id { id: \"test\" })"
|
||||||
|
);
|
||||||
|
|
||||||
s.id = "test1".to_string();
|
s.id = "test1".to_string();
|
||||||
let s = s.into_inner();
|
let s = s.into_inner();
|
||||||
|
@ -267,7 +267,7 @@ mod tests {
|
||||||
let req = TestRequest::with_uri("/name/user1/")
|
let req = TestRequest::with_uri("/name/user1/")
|
||||||
.app_data(QueryConfig::default().error_handler(|e, _| {
|
.app_data(QueryConfig::default().error_handler(|e, _| {
|
||||||
let resp = HttpResponse::UnprocessableEntity().finish();
|
let resp = HttpResponse::UnprocessableEntity().finish();
|
||||||
InternalError::from_response(e, resp).into()
|
InternalError::from_response(e, resp.into()).into()
|
||||||
}))
|
}))
|
||||||
.to_srv_request();
|
.to_srv_request();
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ use actix_http::http::header::{
|
||||||
};
|
};
|
||||||
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use cookie::{Cookie, CookieBuilder};
|
||||||
use flate2::{
|
use flate2::{
|
||||||
read::GzDecoder,
|
read::GzDecoder,
|
||||||
write::{GzEncoder, ZlibDecoder, ZlibEncoder},
|
write::{GzEncoder, ZlibDecoder, ZlibEncoder},
|
||||||
|
@ -826,18 +827,18 @@ mod plus_rustls {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_server_cookies() {
|
async fn test_server_cookies() {
|
||||||
use actix_web::{http, HttpMessage};
|
use actix_web::http;
|
||||||
|
|
||||||
let srv = actix_test::start(|| {
|
let srv = actix_test::start(|| {
|
||||||
App::new().default_service(web::to(|| {
|
App::new().default_service(web::to(|| {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.cookie(
|
.cookie(
|
||||||
http::CookieBuilder::new("first", "first_value")
|
CookieBuilder::new("first", "first_value")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.finish(),
|
.finish(),
|
||||||
)
|
)
|
||||||
.cookie(http::Cookie::new("second", "first_value"))
|
.cookie(Cookie::new("second", "first_value"))
|
||||||
.cookie(http::Cookie::new("second", "second_value"))
|
.cookie(Cookie::new("second", "second_value"))
|
||||||
.finish()
|
.finish()
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
|
@ -846,10 +847,10 @@ async fn test_server_cookies() {
|
||||||
let res = req.send().await.unwrap();
|
let res = req.send().await.unwrap();
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let first_cookie = http::CookieBuilder::new("first", "first_value")
|
let first_cookie = CookieBuilder::new("first", "first_value")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.finish();
|
.finish();
|
||||||
let second_cookie = http::Cookie::new("second", "second_value");
|
let second_cookie = Cookie::new("second", "second_value");
|
||||||
|
|
||||||
let cookies = res.cookies().expect("To have cookies");
|
let cookies = res.cookies().expect("To have cookies");
|
||||||
assert_eq!(cookies.len(), 2);
|
assert_eq!(cookies.len(), 2);
|
||||||
|
|
Loading…
Reference in New Issue