1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-16 14:45:47 +02:00

Compare commits

..

19 Commits

Author SHA1 Message Date
Rob Ede
08a9c66568 docs(files: update readme from crate docs 2024-01-10 04:03:29 +00:00
Rob Ede
83be07d77d chore(actix-files): prepare release 0.6.5 2024-01-10 04:01:14 +00:00
Rob Ede
33da480709 format project 2024-01-10 04:00:20 +00:00
Dialga
fcfc727295 actix-files: fix handling linebreaks in filenames (#3237)
Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-01-10 03:56:15 +00:00
Rob Ede
ac04d80d8e docs: better docs for peer_addr methods 2024-01-08 15:17:40 +00:00
dependabot[bot]
d2bd549eec build(deps): bump taiki-e/install-action from 2.23.7 to 2.24.1 (#3239)
Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.23.7 to 2.24.1.
- [Release notes](https://github.com/taiki-e/install-action/releases)
- [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/taiki-e/install-action/compare/v2.23.7...v2.24.1)

---
updated-dependencies:
- dependency-name: taiki-e/install-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-08 01:25:54 +00:00
Rob Ede
46dde69d50 chore(actix-files): prepare release 0.6.4 2024-01-06 10:19:15 +00:00
Sven-Hendrik Haase
febba786fa actix-files: Properly handle newlines in file names (#3235) 2024-01-06 10:11:40 +00:00
dependabot[bot]
561cc440b2 build(deps): bump taiki-e/install-action from 2.23.0 to 2.23.7 (#3232)
Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.23.0 to 2.23.7.
- [Release notes](https://github.com/taiki-e/install-action/releases)
- [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/taiki-e/install-action/compare/v2.23.0...v2.23.7)

---
updated-dependencies:
- dependency-name: taiki-e/install-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-01 14:10:13 +00:00
Rob Ede
ccb90dd5a1 docs: update changelog 2023-12-25 02:36:17 +00:00
Rob Ede
1c88af50c0 docs: fix changelog 2023-12-25 02:35:22 +00:00
Rob Ede
f4f459d420 chore(actix-http): prepare release 3.5.1 2023-12-25 02:30:14 +00:00
Rob Ede
d14e98b62b prevent hang when compressing Sized(0) bodies
fixes #3229
2023-12-25 02:27:51 +00:00
Rob Ede
f4851b3914 chore(actix-router): prepare release 0.5.2 2023-12-24 16:47:58 +00:00
dependabot[bot]
68597b5426 build(deps): bump taiki-e/install-action from 2.22.0 to 2.23.0 (#3228)
Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.22.0 to 2.23.0.
- [Release notes](https://github.com/taiki-e/install-action/releases)
- [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/taiki-e/install-action/compare/v2.22.0...v2.23.0)

---
updated-dependencies:
- dependency-name: taiki-e/install-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-23 19:54:02 +00:00
Rob Ede
9dc3ad754e chore(actix-web): prepare release 4.4.1 2023-12-23 19:19:10 +00:00
Rob Ede
17060ed993 chore(awc): prepare release 3.3.0 2023-12-23 19:18:29 +00:00
Rob Ede
0d9ca4d939 chore(actix-http): prepare release 3.5.0 2023-12-23 19:17:56 +00:00
Rob Ede
ff2904ee78 ci: prevent cargo-cache install failing MSRV builds 2023-12-23 19:13:11 +00:00
47 changed files with 689 additions and 259 deletions

View File

@@ -45,7 +45,7 @@ jobs:
toolchain: ${{ matrix.version.version }}
- name: Install cargo-hack
uses: taiki-e/install-action@v2.22.0
uses: taiki-e/install-action@v2.24.1
with:
tool: cargo-hack
@@ -71,7 +71,7 @@ jobs:
- name: Clear the cargo caches
run: |
cargo install cargo-cache --version 0.8.3 --no-default-features --features ci-autoclean
cargo --locked install cargo-cache --version 0.8.3 --no-default-features --features ci-autoclean
cargo-cache
ci_feature_powerset_check:
@@ -85,7 +85,7 @@ jobs:
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
- name: Install cargo-hack
uses: taiki-e/install-action@v2.22.0
uses: taiki-e/install-action@v2.24.1
with:
tool: cargo-hack
@@ -106,7 +106,7 @@ jobs:
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
- name: Install nextest
uses: taiki-e/install-action@v2.22.0
uses: taiki-e/install-action@v2.24.1
with:
tool: nextest

View File

@@ -50,7 +50,7 @@ jobs:
toolchain: ${{ matrix.version.version }}
- name: Install cargo-hack
uses: taiki-e/install-action@v2.22.0
uses: taiki-e/install-action@v2.24.1
with:
tool: cargo-hack
@@ -83,7 +83,7 @@ jobs:
- name: Clear the cargo caches
run: |
cargo install cargo-cache --version 0.8.3 --no-default-features --features ci-autoclean
cargo --locked install cargo-cache --version 0.8.3 --no-default-features --features ci-autoclean
cargo-cache
io-uring:

View File

@@ -23,7 +23,7 @@ jobs:
components: llvm-tools-preview
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@v2.22.0
uses: taiki-e/install-action@v2.24.1
with:
tool: cargo-llvm-cov

View File

@@ -1,5 +1,5 @@
overrides:
- files: '*.md'
- files: "*.md"
options:
printWidth: 9999
proseWrap: never

View File

@@ -2,6 +2,13 @@
## Unreleased
## 0.6.5
- Fix handling of special characters in filenames.
## 0.6.4
- Fix handling of newlines in filenames.
- Minimum supported Rust version (MSRV) is now 1.68 due to transitive `time` dependency.
## 0.6.3

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-files"
version = "0.6.3"
version = "0.6.5"
authors = [
"Nikolay Kim <fafhrd91@gmail.com>",
"Rob Ede <robjtede@icloud.com>",

View File

@@ -1,18 +1,32 @@
# actix-files
# `actix-files`
> Static file serving for Actix Web
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-files?label=latest)](https://crates.io/crates/actix-files)
[![Documentation](https://docs.rs/actix-files/badge.svg?version=0.6.3)](https://docs.rs/actix-files/0.6.3)
[![Documentation](https://docs.rs/actix-files/badge.svg?version=0.6.5)](https://docs.rs/actix-files/0.6.5)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
![License](https://img.shields.io/crates/l/actix-files.svg)
<br />
[![dependency status](https://deps.rs/crate/actix-files/0.6.3/status.svg)](https://deps.rs/crate/actix-files/0.6.3)
[![dependency status](https://deps.rs/crate/actix-files/0.6.5/status.svg)](https://deps.rs/crate/actix-files/0.6.5)
[![Download](https://img.shields.io/crates/d/actix-files.svg)](https://crates.io/crates/actix-files)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
## Documentation & Resources
<!-- prettier-ignore-end -->
- [API Documentation](https://docs.rs/actix-files)
- [Example Project](https://github.com/actix/examples/tree/master/basics/static-files)
- Minimum Supported Rust Version (MSRV): 1.68
<!-- cargo-rdme start -->
Static file serving for Actix Web.
Provides a non-blocking service for serving static files from disk.
## Examples
```rust
use actix_web::App;
use actix_files::Files;
let app = App::new()
.service(Files::new("/static", ".").prefer_utf8(true));
```
<!-- cargo-rdme end -->

View File

@@ -568,6 +568,29 @@ mod tests {
assert_eq!(bytes, data);
}
#[actix_rt::test]
async fn test_static_files_with_special_characters() {
// Create the file we want to test against ad-hoc. We can't check it in as otherwise
// Windows can't even checkout this repository.
let temp_dir = tempfile::tempdir().unwrap();
let file_with_newlines = temp_dir.path().join("test\n\x0B\x0C\rnewline.text");
fs::write(&file_with_newlines, "Look at my newlines").unwrap();
let srv = test::init_service(
App::new().service(Files::new("/", temp_dir.path()).index_file("Cargo.toml")),
)
.await;
let request = TestRequest::get()
.uri("/test%0A%0B%0C%0Dnewline.text")
.to_request();
let response = test::call_service(&srv, request).await;
assert_eq!(response.status(), StatusCode::OK);
let bytes = test::read_body(response).await;
let data = web::Bytes::from(fs::read(file_with_newlines).unwrap());
assert_eq!(bytes, data);
}
#[actix_rt::test]
async fn test_files_not_allowed() {
let srv = test::init_service(App::new().service(Files::new("/", "."))).await;
@@ -840,9 +863,9 @@ mod tests {
#[actix_rt::test]
async fn test_percent_encoding_2() {
let tmpdir = tempfile::tempdir().unwrap();
let temp_dir = tempfile::tempdir().unwrap();
let filename = match cfg!(unix) {
true => "ض:?#[]{}<>()@!$&'`|*+,;= %20.test",
true => "ض:?#[]{}<>()@!$&'`|*+,;= %20\n.test",
false => "ض#[]{}()@!$&'`+,;= %20.test",
};
let filename_encoded = filename
@@ -852,9 +875,9 @@ mod tests {
write!(&mut buf, "%{:02X}", c).unwrap();
buf
});
std::fs::File::create(tmpdir.path().join(filename)).unwrap();
std::fs::File::create(temp_dir.path().join(filename)).unwrap();
let srv = test::init_service(App::new().service(Files::new("", tmpdir.path()))).await;
let srv = test::init_service(App::new().service(Files::new("/", temp_dir.path()))).await;
let req = TestRequest::get()
.uri(&format!("/{}", filename_encoded))

View File

@@ -24,7 +24,6 @@ use bitflags::bitflags;
use derive_more::{Deref, DerefMut};
use futures_core::future::LocalBoxFuture;
use mime::Mime;
use mime_guess::from_path;
use crate::{encoding::equiv_utf8_text, range::HttpRange};
@@ -128,7 +127,7 @@ impl NamedFile {
}
};
let ct = from_path(&path).first_or_octet_stream();
let ct = mime_guess::from_path(&path).first_or_octet_stream();
let disposition = match ct.type_() {
mime::IMAGE | mime::TEXT | mime::AUDIO | mime::VIDEO => DispositionType::Inline,
@@ -140,7 +139,13 @@ impl NamedFile {
_ => DispositionType::Attachment,
};
let mut parameters = vec![DispositionParam::Filename(String::from(filename.as_ref()))];
// replace special characters in filenames which could occur on some filesystems
let filename_s = filename
.replace('\n', "%0A") // \n line break
.replace('\x0B', "%0B") // \v vertical tab
.replace('\x0C', "%0C") // \f form feed
.replace('\r', "%0D"); // \r carriage return
let mut parameters = vec![DispositionParam::Filename(filename_s)];
if !filename.is_ascii() {
parameters.push(DispositionParam::FilenameExt(ExtendedValue {

View File

@@ -3,6 +3,7 @@ use std::{
str::FromStr,
};
use actix_utils::future::{ready, Ready};
use actix_web::{dev::Payload, FromRequest, HttpRequest};
use crate::error::UriSegmentError;
@@ -87,10 +88,10 @@ impl AsRef<Path> for PathBufWrap {
impl FromRequest for PathBufWrap {
type Error = UriSegmentError;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
req.match_info().unprocessed().parse()
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(req.match_info().unprocessed().parse())
}
}

View File

@@ -1,7 +1,9 @@
# actix-http-test
# `actix-http-test`
> Various helpers for Actix applications to use during testing.
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-http-test?label=latest)](https://crates.io/crates/actix-http-test)
[![Documentation](https://docs.rs/actix-http-test/badge.svg?version=3.1.0)](https://docs.rs/actix-http-test/3.1.0)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
@@ -11,6 +13,8 @@
[![Download](https://img.shields.io/crates/d/actix-http-test.svg)](https://crates.io/crates/actix-http-test)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
## Documentation & Resources
- [API Documentation](https://docs.rs/actix-http-test)

View File

@@ -2,14 +2,25 @@
## Unreleased
### Changed
- Updated `zstd` dependency to `0.13`.
- Implemented `From<HeaderMap>` for `http::HeaderMap`.
## 3.5.1
### Fixed
- Do not encode zero-sized response bodies
- Prevent hang when returning zero-sized response bodies through compression layer.
## 3.5.0
### Added
- Implement `From<HeaderMap>` for `http::HeaderMap`.
### Changed
- Updated `zstd` dependency to `0.13`.
### Fixed
- Prevent compression of zero-sized response bodies.
## 3.4.0

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-http"
version = "3.4.0"
version = "3.5.1"
authors = [
"Nikolay Kim <fafhrd91@gmail.com>",
"Rob Ede <robjtede@icloud.com>",

View File

@@ -5,11 +5,11 @@
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-http?label=latest)](https://crates.io/crates/actix-http)
[![Documentation](https://docs.rs/actix-http/badge.svg?version=3.4.0)](https://docs.rs/actix-http/3.4.0)
[![Documentation](https://docs.rs/actix-http/badge.svg?version=3.5.1)](https://docs.rs/actix-http/3.5.1)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-http.svg)
<br />
[![dependency status](https://deps.rs/crate/actix-http/3.4.0/status.svg)](https://deps.rs/crate/actix-http/3.4.0)
[![dependency status](https://deps.rs/crate/actix-http/3.5.1/status.svg)](https://deps.rs/crate/actix-http/3.5.1)
[![Download](https://img.shields.io/crates/d/actix-http.svg)](https://crates.io/crates/actix-http)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)

View File

@@ -50,10 +50,21 @@ impl<B: MessageBody> Encoder<B> {
}
}
fn empty() -> Self {
Encoder {
body: EncoderBody::Full { body: Bytes::new() },
encoder: None,
fut: None,
eof: true,
}
}
pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self {
// no need to compress an empty body
if matches!(body.size(), BodySize::None | BodySize::Sized(0)) {
return Self::none();
// no need to compress empty bodies
match body.size() {
BodySize::None => return Self::none(),
BodySize::Sized(0) => return Self::empty(),
_ => {}
}
let should_encode = !(head.headers().contains_key(&CONTENT_ENCODING)

View File

@@ -16,7 +16,10 @@ pub struct RequestHead {
pub uri: Uri,
pub version: Version,
pub headers: HeaderMap,
/// Will only be None when called in unit tests unless [`TestRequest::peer_addr`] is used.
pub peer_addr: Option<net::SocketAddr>,
flags: Flags,
}

View File

@@ -173,7 +173,7 @@ impl<P> Request<P> {
/// Peer address is the directly connected peer's socket address. If a proxy is used in front of
/// the Actix Web server, then it would be address of this proxy.
///
/// Will only return None when called in unit tests.
/// Will only return None when called in unit tests unless set manually.
#[inline]
pub fn peer_addr(&self) -> Option<net::SocketAddr> {
self.head().peer_addr

View File

@@ -1,7 +1,9 @@
# actix-multipart-derive
# `actix-multipart-derive`
> The derive macro implementation for actix-multipart-derive.
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-multipart-derive?label=latest)](https://crates.io/crates/actix-multipart-derive)
[![Documentation](https://docs.rs/actix-multipart-derive/badge.svg?version=0.6.1)](https://docs.rs/actix-multipart-derive/0.6.1)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
@@ -11,6 +13,8 @@
[![Download](https://img.shields.io/crates/d/actix-multipart-derive.svg)](https://crates.io/crates/actix-multipart-derive)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
## Documentation & Resources
- [API Documentation](https://docs.rs/actix-multipart-derive)

View File

@@ -1,7 +1,9 @@
# actix-multipart
# `actix-multipart`
> Multipart form support for Actix Web.
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-multipart?label=latest)](https://crates.io/crates/actix-multipart)
[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.6.1)](https://docs.rs/actix-multipart/0.6.1)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
@@ -11,6 +13,8 @@
[![Download](https://img.shields.io/crates/d/actix-multipart.svg)](https://crates.io/crates/actix-multipart)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
## Documentation & Resources
- [API Documentation](https://docs.rs/actix-multipart)

View File

@@ -2,6 +2,8 @@
## Unreleased
## 0.5.2
- Minimum supported Rust version (MSRV) is now 1.68 due to transitive `time` dependency.
## 0.5.1

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-router"
version = "0.5.1"
version = "0.5.2"
authors = [
"Nikolay Kim <fafhrd91@gmail.com>",
"Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>",

View File

@@ -1,14 +1,18 @@
# `actix-router`
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-router?label=latest)](https://crates.io/crates/actix-router)
[![Documentation](https://docs.rs/actix-router/badge.svg?version=0.5.1)](https://docs.rs/actix-router/0.5.1)
[![Documentation](https://docs.rs/actix-router/badge.svg?version=0.5.2)](https://docs.rs/actix-router/0.5.2)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-router.svg)
<br />
[![dependency status](https://deps.rs/crate/actix-router/0.5.1/status.svg)](https://deps.rs/crate/actix-router/0.5.1)
[![dependency status](https://deps.rs/crate/actix-router/0.5.2/status.svg)](https://deps.rs/crate/actix-router/0.5.2)
[![Download](https://img.shields.io/crates/d/actix-router.svg)](https://crates.io/crates/actix-router)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
<!-- cargo-rdme start -->
Resource path matching and router.

View File

@@ -1,7 +1,9 @@
# actix-web-actors
# `actix-web-actors`
> Actix actors support for Actix Web.
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-web-actors?label=latest)](https://crates.io/crates/actix-web-actors)
[![Documentation](https://docs.rs/actix-web-actors/badge.svg?version=4.2.0)](https://docs.rs/actix-web-actors/4.2.0)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
@@ -11,6 +13,8 @@
[![Download](https://img.shields.io/crates/d/actix-web-actors.svg)](https://crates.io/crates/actix-web-actors)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
## Documentation & Resources
- [API Documentation](https://docs.rs/actix-web-actors)

View File

@@ -1,7 +1,9 @@
# actix-web-codegen
# `actix-web-codegen`
> Routing and runtime macros for Actix Web.
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-web-codegen?label=latest)](https://crates.io/crates/actix-web-codegen)
[![Documentation](https://docs.rs/actix-web-codegen/badge.svg?version=4.2.2)](https://docs.rs/actix-web-codegen/4.2.2)
![Version](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
@@ -11,6 +13,8 @@
[![Download](https://img.shields.io/crates/d/actix-web-codegen.svg)](https://crates.io/crates/actix-web-codegen)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
## Documentation & Resources
- [API Documentation](https://docs.rs/actix-web-codegen)

View File

@@ -2,6 +2,8 @@
## Unreleased
## 4.4.1
### Changed
- Updated `zstd` dependency to `0.13`.

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-web"
version = "4.4.0"
version = "4.4.1"
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
authors = [
"Nikolay Kim <fafhrd91@gmail.com>",
@@ -75,7 +75,7 @@ actix-service = "2"
actix-utils = "3"
actix-tls = { version = "3.1", default-features = false, optional = true }
actix-http = { version = "3.4", features = ["ws"] }
actix-http = { version = "3.5", features = ["ws"] }
actix-router = "0.5"
actix-web-codegen = { version = "4.2", optional = true }
@@ -100,7 +100,6 @@ serde_json = "1.0"
serde_urlencoded = "0.7"
smallvec = "1.6.1"
socket2 = "0.5"
tokio = { version = "1.24.2", features = ["macros"] }
time = { version = "0.3", default-features = false, features = ["formatting"] }
url = "2.1"

View File

@@ -8,10 +8,10 @@
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-web?label=latest)](https://crates.io/crates/actix-web)
[![Documentation](https://docs.rs/actix-web/badge.svg?version=4.4.0)](https://docs.rs/actix-web/4.4.0)
[![Documentation](https://docs.rs/actix-web/badge.svg?version=4.4.1)](https://docs.rs/actix-web/4.4.1)
![MSRV](https://img.shields.io/badge/rustc-1.68+-ab6000.svg)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-web.svg)
[![Dependency Status](https://deps.rs/crate/actix-web/4.4.0/status.svg)](https://deps.rs/crate/actix-web/4.4.0)
[![Dependency Status](https://deps.rs/crate/actix-web/4.4.1/status.svg)](https://deps.rs/crate/actix-web/4.4.1)
<br />
[![CI](https://github.com/actix/actix-web/actions/workflows/ci.yml/badge.svg)](https://github.com/actix/actix-web/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web)

View File

@@ -1,6 +1,7 @@
use std::{any::type_name, ops::Deref, sync::Arc};
use actix_http::Extensions;
use actix_utils::future::{err, ok, Ready};
use futures_core::future::LocalBoxFuture;
use serde::{de, Serialize};
@@ -158,11 +159,12 @@ where
impl<T: ?Sized + 'static> FromRequest for Data<T> {
type Error = Error;
type Future = Ready<Result<Self, Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
if let Some(st) = req.app_data::<Data<T>>() {
Ok(st.clone())
ok(st.clone())
} else {
log::debug!(
"Failed to extract `Data<{}>` for `{}` handler. For the Data extractor to work \
@@ -172,7 +174,7 @@ impl<T: ?Sized + 'static> FromRequest for Data<T> {
req.match_name().unwrap_or_else(|| req.path())
);
Err(error::ErrorInternalServerError(
err(error::ErrorInternalServerError(
"Requested application data is not configured correctly. \
View/enable debug logs for more details.",
))

View File

@@ -3,11 +3,13 @@
use std::{
convert::Infallible,
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
use actix_http::{Method, Uri};
use actix_utils::future::{ok, Ready};
use futures_core::ready;
use pin_project_lite::pin_project;
@@ -64,17 +66,33 @@ pub trait FromRequest: Sized {
/// The associated error which can be returned.
type Error: Into<Error>;
/// Creates a `Self` from request parts asynchronously.
fn from_request(
req: &HttpRequest,
payload: &mut Payload,
) -> impl Future<Output = Result<Self, Self::Error>>;
/// Future that resolves to a `Self`.
///
/// To use an async function or block, the futures must be boxed. The following snippet will be
/// common when creating async/await extractors (that do not consume the body).
///
/// ```ignore
/// type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
/// // or
/// type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;
///
/// fn from_request(req: HttpRequest, ...) -> Self::Future {
/// let req = req.clone();
/// Box::pin(async move {
/// ...
/// })
/// }
/// ```
type Future: Future<Output = Result<Self, Self::Error>>;
/// Create a `Self` from request parts asynchronously.
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
/// Create a `Self` from request head asynchronously.
///
/// This method is short for `T::from_request(req, &mut Payload::None)`.
fn extract(req: &HttpRequest) -> impl Future<Output = Result<Self, Self::Error>> {
async { Self::from_request(req, &mut Payload::None).await }
fn extract(req: &HttpRequest) -> Self::Future {
Self::from_request(req, &mut Payload::None)
}
}
@@ -128,19 +146,12 @@ where
T: FromRequest,
{
type Error = Infallible;
type Future = FromRequestOptFuture<T::Future>;
#[inline]
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
match T::from_request(req, payload).await {
Ok(t) => Ok(Some(t)),
Err(err) => {
log::debug!(
"Error from `Option<{}>` extractor: {}",
std::any::type_name::<T>(),
err.into()
);
Ok(None)
}
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
FromRequestOptFuture {
fut: T::from_request(req, payload),
}
}
}
@@ -192,11 +203,9 @@ where
///
/// impl FromRequest for Thing {
/// type Error = Error;
/// type Future = Ready<Result<Thing, Error>>;
///
/// async fn from_request(
/// req: &HttpRequest,
/// payload: &mut dev::Payload,
/// ) -> Result<Self, Self::Error> {
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() {
/// ok(Thing { name: "thingy".into() })
/// } else {
@@ -223,10 +232,36 @@ where
T::Error: Into<E>,
{
type Error = Infallible;
type Future = FromRequestResFuture<T::Future, E>;
#[inline]
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
Ok(T::from_request(req, payload).await.map_err(Into::into))
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
FromRequestResFuture {
fut: T::from_request(req, payload),
_phantom: PhantomData,
}
}
}
pin_project! {
pub struct FromRequestResFuture<Fut, E> {
#[pin]
fut: Fut,
_phantom: PhantomData<E>,
}
}
impl<Fut, T, Ei, E> Future for FromRequestResFuture<Fut, E>
where
Fut: Future<Output = Result<T, Ei>>,
Ei: Into<E>,
{
type Output = Result<Result<T, E>, Infallible>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let res = ready!(this.fut.poll(cx));
Poll::Ready(Ok(res.map_err(Into::into)))
}
}
@@ -244,9 +279,10 @@ where
/// ```
impl FromRequest for Uri {
type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>;
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
Ok(req.uri().clone())
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.uri().clone())
}
}
@@ -264,9 +300,10 @@ impl FromRequest for Uri {
/// ```
impl FromRequest for Method {
type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>;
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
Ok(req.method().clone())
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.method().clone())
}
}
@@ -282,24 +319,88 @@ mod tuple_from_req {
impl<$($T: FromRequest + 'static),+> FromRequest for ($($T,)+)
{
type Error = Error;
type Future = $fut<$($T),+>;
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut {
$(
$T: ExtractFuture::Future {
fut: $T::from_request(req, payload)
},
)+
}
}
}
pin_project! {
pub struct $fut<$($T: FromRequest),+> {
$(
let $T = $T::from_request(req, payload).await.map_err(Into::into)?;
#[pin]
$T: ExtractFuture<$T::Future, $T>,
)+
}
}
impl<$($T: FromRequest),+> Future for $fut<$($T),+>
{
type Output = Result<($($T,)+), Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
let mut ready = true;
$(
match this.$T.as_mut().project() {
ExtractProj::Future { fut } => match fut.poll(cx) {
Poll::Ready(Ok(output)) => {
let _ = this.$T.as_mut().project_replace(ExtractFuture::Done { output });
},
Poll::Ready(Err(err)) => return Poll::Ready(Err(err.into())),
Poll::Pending => ready = false,
},
ExtractProj::Done { .. } => {},
ExtractProj::Empty => unreachable!("FromRequest polled after finished"),
}
)+
Ok(($($T,)+))
if ready {
Poll::Ready(Ok(
($(
match this.$T.project_replace(ExtractFuture::Empty) {
ExtractReplaceProj::Done { output } => output,
_ => unreachable!("FromRequest polled after finished"),
},
)+)
))
} else {
Poll::Pending
}
}
}
};
}
pin_project! {
#[project = ExtractProj]
#[project_replace = ExtractReplaceProj]
enum ExtractFuture<Fut, Res> {
Future {
#[pin]
fut: Fut
},
Done {
output: Res,
},
Empty
}
}
impl FromRequest for () {
type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
async fn from_request(_: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
Ok(())
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(())
}
}

View File

@@ -1,5 +1,6 @@
use std::{convert::Infallible, net::SocketAddr};
use actix_utils::future::{err, ok, Ready};
use derive_more::{Display, Error};
use crate::{
@@ -197,10 +198,10 @@ impl ConnectionInfo {
impl FromRequest for ConnectionInfo {
type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
Ok(req.connection_info().clone())
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.connection_info().clone())
}
}
@@ -239,14 +240,14 @@ impl ResponseError for MissingPeerAddr {}
impl FromRequest for PeerAddr {
type Error = MissingPeerAddr;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
match req.peer_addr() {
Some(addr) => Ok(PeerAddr(addr)),
Some(addr) => ok(PeerAddr(addr)),
None => {
log::error!("Missing peer address.");
Err(MissingPeerAddr)
err(MissingPeerAddr)
}
}
}

View File

@@ -1,6 +1,5 @@
use std::{
cell::{Ref, RefCell, RefMut},
convert::Infallible,
fmt, net,
rc::Rc,
str,
@@ -8,6 +7,7 @@ use std::{
use actix_http::{Message, RequestHead};
use actix_router::{Path, Url};
use actix_utils::future::{ok, Ready};
#[cfg(feature = "cookies")]
use cookie::{Cookie, ParseError as CookieParseError};
use smallvec::SmallVec;
@@ -20,7 +20,7 @@ use crate::{
http::{header::HeaderMap, Method, Uri, Version},
info::ConnectionInfo,
rmap::ResourceMap,
FromRequest, HttpMessage,
Error, FromRequest, HttpMessage,
};
#[cfg(feature = "cookies")]
@@ -417,11 +417,12 @@ impl Drop for HttpRequest {
/// );
/// ```
impl FromRequest for HttpRequest {
type Error = Infallible;
type Error = Error;
type Future = Ready<Result<Self, Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
Ok(req.clone())
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.clone())
}
}

View File

@@ -1,4 +1,6 @@
use std::ops::Deref;
use std::{any::type_name, ops::Deref};
use actix_utils::future::{err, ok, Ready};
use crate::{
dev::Payload, error::ErrorInternalServerError, Error, FromRequest, HttpMessage as _,
@@ -64,19 +66,19 @@ impl<T: Clone + 'static> Deref for ReqData<T> {
impl<T: Clone + 'static> FromRequest for ReqData<T> {
type Error = Error;
type Future = Ready<Result<Self, Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
if let Some(st) = req.extensions().get::<T>() {
Ok(ReqData(st.clone()))
ok(ReqData(st.clone()))
} else {
log::debug!(
"Failed to extract App-level ReqData<{}>. Request path: {}",
std::any::type_name::<T>(),
"Failed to construct App-level ReqData extractor. \
Request path: {:?} (type: {})",
req.path(),
type_name::<T>(),
);
Err(ErrorInternalServerError(
err(ErrorInternalServerError(
"Missing expected request extension data",
))
}

View File

@@ -125,11 +125,11 @@ impl ServiceRequest {
/// # todo!()
/// }
/// ```
pub async fn extract<T: FromRequest>(&mut self) -> Result<T, <T as FromRequest>::Error>
pub fn extract<T>(&mut self) -> <T as FromRequest>::Future
where
T: FromRequest,
{
T::from_request(&self.req, &mut self.payload).await
T::from_request(&self.req, &mut self.payload)
}
/// Construct request from parts.
@@ -221,12 +221,9 @@ impl ServiceRequest {
/// Returns peer's socket address.
///
/// Peer address is the directly connected peer's socket address. If a proxy is used in front of
/// the Actix Web server, then it would be address of this proxy.
/// See [`HttpRequest::peer_addr`] for more details.
///
/// To get client connection information `ConnectionInfo` should be used.
///
/// Will only return None when called in unit tests.
/// [`HttpRequest::peer_addr`]: crate::HttpRequest::peer_addr
#[inline]
pub fn peer_addr(&self) -> Option<net::SocketAddr> {
self.head().peer_addr

View File

@@ -86,76 +86,77 @@ impl Default for TestRequest {
#[allow(clippy::wrong_self_convention)]
impl TestRequest {
/// Create TestRequest and set request uri
pub fn with_uri(path: &str) -> TestRequest {
TestRequest::default().uri(path)
/// Constructs test request and sets request URI.
pub fn with_uri(uri: &str) -> TestRequest {
TestRequest::default().uri(uri)
}
/// Create TestRequest and set method to `Method::GET`
/// Constructs test request with GET method.
pub fn get() -> TestRequest {
TestRequest::default().method(Method::GET)
}
/// Create TestRequest and set method to `Method::POST`
/// Constructs test request with POST method.
pub fn post() -> TestRequest {
TestRequest::default().method(Method::POST)
}
/// Create TestRequest and set method to `Method::PUT`
/// Constructs test request with PUT method.
pub fn put() -> TestRequest {
TestRequest::default().method(Method::PUT)
}
/// Create TestRequest and set method to `Method::PATCH`
/// Constructs test request with PATCH method.
pub fn patch() -> TestRequest {
TestRequest::default().method(Method::PATCH)
}
/// Create TestRequest and set method to `Method::DELETE`
/// Constructs test request with DELETE method.
pub fn delete() -> TestRequest {
TestRequest::default().method(Method::DELETE)
}
/// Set HTTP version of this request
/// Sets HTTP version of this request.
pub fn version(mut self, ver: Version) -> Self {
self.req.version(ver);
self
}
/// Set HTTP method of this request
/// Sets method of this request.
pub fn method(mut self, meth: Method) -> Self {
self.req.method(meth);
self
}
/// Set HTTP URI of this request
/// Sets URI of this request.
pub fn uri(mut self, path: &str) -> Self {
self.req.uri(path);
self
}
/// Insert a header, replacing any that were set with an equivalent field name.
/// Inserts a header, replacing any that were set with an equivalent field name.
pub fn insert_header(mut self, header: impl TryIntoHeaderPair) -> Self {
self.req.insert_header(header);
self
}
/// Append a header, keeping any that were set with an equivalent field name.
/// Appends a header, keeping any that were set with an equivalent field name.
pub fn append_header(mut self, header: impl TryIntoHeaderPair) -> Self {
self.req.append_header(header);
self
}
/// Set cookie for this request.
/// Sets cookie for this request.
#[cfg(feature = "cookies")]
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
self.cookies.add(cookie.into_owned());
self
}
/// Set request path pattern parameter.
/// Sets request path pattern parameter.
///
/// # Examples
///
/// ```
/// use actix_web::test::TestRequest;
///
@@ -171,19 +172,19 @@ impl TestRequest {
self
}
/// Set peer addr.
/// Sets peer address.
pub fn peer_addr(mut self, addr: SocketAddr) -> Self {
self.peer_addr = Some(addr);
self
}
/// Set request payload.
/// Sets request payload.
pub fn set_payload(mut self, data: impl Into<Bytes>) -> Self {
self.req.set_payload(data);
self
}
/// Serialize `data` to a URL encoded form and set it as the request payload.
/// Serializes `data` to a URL encoded form and set it as the request payload.
///
/// The `Content-Type` header is set to `application/x-www-form-urlencoded`.
pub fn set_form(mut self, data: impl Serialize) -> Self {
@@ -194,7 +195,7 @@ impl TestRequest {
self
}
/// Serialize `data` to JSON and set it as the request payload.
/// Serializes `data` to JSON and set it as the request payload.
///
/// The `Content-Type` header is set to `application/json`.
pub fn set_json(mut self, data: impl Serialize) -> Self {
@@ -204,27 +205,33 @@ impl TestRequest {
self
}
/// Set application data. This is equivalent of `App::data()` method
/// for testing purpose.
pub fn data<T: 'static>(mut self, data: T) -> Self {
self.app_data.insert(Data::new(data));
self
}
/// Set application data. This is equivalent of `App::app_data()` method
/// for testing purpose.
/// Inserts application data.
///
/// This is equivalent of `App::app_data()` method for testing purpose.
pub fn app_data<T: 'static>(mut self, data: T) -> Self {
self.app_data.insert(data);
self
}
/// Inserts application data.
///
/// This is equivalent of `App::data()` method for testing purpose.
#[doc(hidden)]
pub fn data<T: 'static>(mut self, data: T) -> Self {
self.app_data.insert(Data::new(data));
self
}
/// Sets resource map.
#[cfg(test)]
/// Set request config
pub(crate) fn rmap(mut self, rmap: ResourceMap) -> Self {
self.rmap = rmap;
self
}
/// Finalizes test request.
///
/// This request builder will be useless after calling `finish()`.
fn finish(&mut self) -> Request {
// mut used when cookie feature is enabled
#[allow(unused_mut)]
@@ -251,14 +258,14 @@ impl TestRequest {
req
}
/// Complete request creation and generate `Request` instance
/// Finalizes request creation and returns `Request` instance.
pub fn to_request(mut self) -> Request {
let mut req = self.finish();
req.head_mut().peer_addr = self.peer_addr;
req
}
/// Complete request creation and generate `ServiceRequest` instance
/// Finalizes request creation and returns `ServiceRequest` instance.
pub fn to_srv_request(mut self) -> ServiceRequest {
let (mut head, payload) = self.finish().into_parts();
head.peer_addr = self.peer_addr;
@@ -279,12 +286,12 @@ impl TestRequest {
)
}
/// Complete request creation and generate `ServiceResponse` instance
/// Finalizes request creation and returns `ServiceResponse` instance.
pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B> {
self.to_srv_request().into_response(res)
}
/// Complete request creation and generate `HttpRequest` instance
/// Finalizes request creation and returns `HttpRequest` instance.
pub fn to_http_request(mut self) -> HttpRequest {
let (mut head, _) = self.finish().into_parts();
head.peer_addr = self.peer_addr;
@@ -302,7 +309,7 @@ impl TestRequest {
)
}
/// Complete request creation and generate `HttpRequest` and `Payload` instances
/// Finalizes request creation and returns `HttpRequest` and `Payload` pair.
pub fn to_http_parts(mut self) -> (HttpRequest, Payload) {
let (mut head, payload) = self.finish().into_parts();
head.peer_addr = self.peer_addr;
@@ -322,7 +329,7 @@ impl TestRequest {
(req, payload)
}
/// Complete request creation, calls service and waits for response future completion.
/// Finalizes request creation, calls service, and waits for response future completion.
pub async fn send_request<S, B, E>(self, app: &S) -> S::Response
where
S: Service<Request, Response = ServiceResponse<B>, Error = E>,

View File

@@ -1,6 +1,15 @@
//! For either helper, see [`Either`].
use std::{
future::Future,
mem,
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use futures_core::ready;
use pin_project_lite::pin_project;
use crate::{
body::EitherBody,
@@ -149,7 +158,7 @@ where
fn from(err: EitherExtractError<L, R>) -> Error {
match err {
EitherExtractError::Bytes(err) => err,
EitherExtractError::Extract(l_err, _r_err) => l_err.into(),
EitherExtractError::Extract(a_err, _b_err) => a_err.into(),
}
}
}
@@ -161,25 +170,112 @@ where
R: FromRequest + 'static,
{
type Error = EitherExtractError<L::Error, R::Error>;
type Future = EitherExtractFut<L, R>;
async fn from_request(
req: &HttpRequest,
payload: &mut dev::Payload,
) -> Result<Self, Self::Error> {
let buf = Bytes::from_request(req, payload)
.await
.map_err(EitherExtractError::Bytes)?;
match L::from_request(req, &mut payload_from_bytes(buf.clone())).await {
Ok(left) => Ok(Either::Left(left)),
Err(l_err) => match R::from_request(req, &mut payload_from_bytes(buf)).await {
Ok(right) => Ok(Either::Right(right)),
Err(r_err) => Err(EitherExtractError::Extract(l_err, r_err)),
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
EitherExtractFut {
req: req.clone(),
state: EitherExtractState::Bytes {
bytes: Bytes::from_request(req, payload),
},
}
}
}
pin_project! {
pub struct EitherExtractFut<L, R>
where
R: FromRequest,
L: FromRequest,
{
req: HttpRequest,
#[pin]
state: EitherExtractState<L, R>,
}
}
pin_project! {
#[project = EitherExtractProj]
pub enum EitherExtractState<L, R>
where
L: FromRequest,
R: FromRequest,
{
Bytes {
#[pin]
bytes: <Bytes as FromRequest>::Future,
},
Left {
#[pin]
left: L::Future,
fallback: Bytes,
},
Right {
#[pin]
right: R::Future,
left_err: Option<L::Error>,
},
}
}
impl<R, RF, RE, L, LF, LE> Future for EitherExtractFut<L, R>
where
L: FromRequest<Future = LF, Error = LE>,
R: FromRequest<Future = RF, Error = RE>,
LF: Future<Output = Result<L, LE>> + 'static,
RF: Future<Output = Result<R, RE>> + 'static,
LE: Into<Error>,
RE: Into<Error>,
{
type Output = Result<Either<L, R>, EitherExtractError<LE, RE>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
let ready = loop {
let next = match this.state.as_mut().project() {
EitherExtractProj::Bytes { bytes } => {
let res = ready!(bytes.poll(cx));
match res {
Ok(bytes) => {
let fallback = bytes.clone();
let left = L::from_request(this.req, &mut payload_from_bytes(bytes));
EitherExtractState::Left { left, fallback }
}
Err(err) => break Err(EitherExtractError::Bytes(err)),
}
}
EitherExtractProj::Left { left, fallback } => {
let res = ready!(left.poll(cx));
match res {
Ok(extracted) => break Ok(Either::Left(extracted)),
Err(left_err) => {
let right = R::from_request(
this.req,
&mut payload_from_bytes(mem::take(fallback)),
);
EitherExtractState::Right {
left_err: Some(left_err),
right,
}
}
}
}
EitherExtractProj::Right { right, left_err } => {
let res = ready!(right.poll(cx));
match res {
Ok(data) => break Ok(Either::Right(data)),
Err(err) => {
break Err(EitherExtractError::Extract(left_err.take().unwrap(), err));
}
}
}
};
this.state.set(next);
};
Poll::Ready(ready)
}
}
fn payload_from_bytes(bytes: Bytes) -> dev::Payload {
let (_, mut h1_payload) = actix_http::h1::Payload::create(true);
h1_payload.unread_data(bytes);

View File

@@ -13,7 +13,7 @@ use std::{
use actix_http::Payload;
use bytes::BytesMut;
use encoding_rs::{Encoding, UTF_8};
use futures_core::future::LocalBoxFuture;
use futures_core::{future::LocalBoxFuture, ready};
use futures_util::{FutureExt as _, StreamExt as _};
use serde::{de::DeserializeOwned, Serialize};
@@ -126,21 +126,51 @@ where
T: DeserializeOwned + 'static,
{
type Error = Error;
type Future = FormExtractFut<T>;
#[inline]
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
let FormConfig { limit, err_handler } = FormConfig::from_req(req);
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let FormConfig { limit, err_handler } = FormConfig::from_req(req).clone();
match UrlEncoded::new(req, payload).limit(*limit).await {
Ok(form) => Ok(Form(form)),
Err(err) => Err(match err_handler {
Some(err_handler) => (err_handler)(err, req),
None => err.into(),
}),
FormExtractFut {
fut: UrlEncoded::new(req, payload).limit(limit),
req: req.clone(),
err_handler,
}
}
}
type FormErrHandler = Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>;
pub struct FormExtractFut<T> {
fut: UrlEncoded<T>,
err_handler: FormErrHandler,
req: HttpRequest,
}
impl<T> Future for FormExtractFut<T>
where
T: DeserializeOwned + 'static,
{
type Output = Result<Form<T>, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let res = ready!(Pin::new(&mut this.fut).poll(cx));
let res = match res {
Err(err) => match &this.err_handler {
Some(err_handler) => Err((err_handler)(err, &this.req)),
None => Err(err.into()),
},
Ok(item) => Ok(Form(item)),
};
Poll::Ready(res)
}
}
impl<T: fmt::Display> fmt::Display for Form<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
@@ -168,8 +198,6 @@ impl<T: Serialize> Responder for Form<T> {
}
}
type FormErrHandler = Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>;
/// [`Form`] extractor configuration.
///
/// ```

View File

@@ -2,6 +2,8 @@
use std::{fmt, ops};
use actix_utils::future::{ready, Ready};
use crate::{
dev::Payload, error::ParseError, extract::FromRequest, http::header::Header as ParseHeader,
HttpRequest,
@@ -59,12 +61,13 @@ where
T: ParseHeader,
{
type Error = ParseError;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
match ParseHeader::parse(req) {
Ok(header) => Ok(Header(header)),
Err(err) => Err(err),
Ok(header) => ready(Ok(Header(header))),
Err(err) => ready(Err(err)),
}
}
}

View File

@@ -138,8 +138,10 @@ impl<T: Serialize> Responder for Json<T> {
/// See [here](#extractor) for example of usage as an extractor.
impl<T: DeserializeOwned> FromRequest for Json<T> {
type Error = Error;
type Future = JsonExtractFut<T>;
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let config = JsonConfig::from_req(req);
let limit = config.limit;
@@ -147,30 +149,52 @@ impl<T: DeserializeOwned> FromRequest for Json<T> {
let ctype_fn = config.content_type.as_deref();
let err_handler = config.err_handler.clone();
match JsonBody::new(req, payload, ctype_fn, ctype_required)
.limit(limit)
.await
{
Ok(data) => Ok(Json(data)),
Err(err) => {
log::debug!(
"Failed to deserialize Json<{}> from payload. \
Request path: {}",
std::any::type_name::<T>(),
req.path(),
);
Err(match err_handler.as_ref() {
Some(err_handler) => (err_handler)(err, req),
None => err.into(),
})
}
JsonExtractFut {
req: Some(req.clone()),
fut: JsonBody::new(req, payload, ctype_fn, ctype_required).limit(limit),
err_handler,
}
}
}
type JsonErrorHandler = Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>;
pub struct JsonExtractFut<T> {
req: Option<HttpRequest>,
fut: JsonBody<T>,
err_handler: JsonErrorHandler,
}
impl<T: DeserializeOwned> Future for JsonExtractFut<T> {
type Output = Result<Json<T>, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let res = ready!(Pin::new(&mut this.fut).poll(cx));
let res = match res {
Err(err) => {
let req = this.req.take().unwrap();
log::debug!(
"Failed to deserialize Json from payload. \
Request path: {}",
req.path()
);
if let Some(err_handler) = this.err_handler.as_ref() {
Err((*err_handler)(err, &req))
} else {
Err(err.into())
}
}
Ok(data) => Ok(Json(data)),
};
Poll::Ready(res)
}
}
/// `Json` extractor configuration.
///
/// # Examples

View File

@@ -3,6 +3,7 @@
use std::sync::Arc;
use actix_router::PathDeserializer;
use actix_utils::future::{ready, Ready};
use derive_more::{AsRef, Deref, DerefMut, Display, From};
use serde::de;
@@ -68,29 +69,33 @@ where
T: de::DeserializeOwned,
{
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req
.app_data::<PathConfig>()
.or_else(|| req.app_data::<Data<PathConfig>>().map(Data::get_ref))
.and_then(|c| c.err_handler.clone());
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
.map(Path)
.map_err(move |err| {
log::debug!(
"Failed during Path extractor deserialization. \
ready(
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
.map(Path)
.map_err(move |err| {
log::debug!(
"Failed during Path extractor deserialization. \
Request path: {:?}",
req.path()
);
req.path()
);
if let Some(error_handler) = error_handler {
let e = PathError::Deserialize(err);
(error_handler)(e, req)
} else {
ErrorNotFound(err)
}
})
if let Some(error_handler) = error_handler {
let e = PathError::Deserialize(err);
(error_handler)(e, req)
} else {
ErrorNotFound(err)
}
}),
)
}
}

View File

@@ -9,8 +9,9 @@ use std::{
};
use actix_http::error::PayloadError;
use actix_utils::future::{ready, Either, Ready};
use bytes::{Bytes, BytesMut};
use encoding_rs::Encoding;
use encoding_rs::{Encoding, UTF_8};
use futures_core::{ready, stream::Stream};
use mime::Mime;
@@ -130,13 +131,11 @@ impl Stream for Payload {
/// See [here](#Examples) for example of usage as an extractor.
impl FromRequest for Payload {
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
async fn from_request(
_: &HttpRequest,
payload: &mut dev::Payload,
) -> Result<Self, Self::Error> {
Ok(Payload(payload.take()))
fn from_request(_: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
ready(Ok(Payload(payload.take())))
}
}
@@ -158,21 +157,33 @@ impl FromRequest for Payload {
/// ```
impl FromRequest for Bytes {
type Error = Error;
type Future = Either<BytesExtractFut, Ready<Result<Bytes, Error>>>;
async fn from_request(
req: &HttpRequest,
payload: &mut dev::Payload,
) -> Result<Self, Self::Error> {
#[inline]
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
// allow both Config and Data<Config>
let cfg = PayloadConfig::from_req(req);
// check content-type
cfg.check_mimetype(req)?;
if let Err(err) = cfg.check_mimetype(req) {
return Either::right(ready(Err(err)));
}
HttpMessageBody::new(req, payload)
.limit(cfg.limit)
.await
.map_err(Into::into)
Either::left(BytesExtractFut {
body_fut: HttpMessageBody::new(req, payload).limit(cfg.limit),
})
}
}
/// Future for `Bytes` extractor.
pub struct BytesExtractFut {
body_fut: HttpMessageBody,
}
impl Future for BytesExtractFut {
type Output = Result<Bytes, Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.body_fut).poll(cx).map_err(Into::into)
}
}
@@ -193,29 +204,49 @@ impl FromRequest for Bytes {
/// }
impl FromRequest for String {
type Error = Error;
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>;
async fn from_request(
req: &HttpRequest,
payload: &mut dev::Payload,
) -> Result<Self, Self::Error> {
#[inline]
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
let cfg = PayloadConfig::from_req(req);
// check content-type
cfg.check_mimetype(req)?;
if let Err(err) = cfg.check_mimetype(req) {
return Either::right(ready(Err(err)));
}
// check charset
let encoding = req.encoding()?;
let encoding = match req.encoding() {
Ok(enc) => enc,
Err(err) => return Either::right(ready(Err(err.into()))),
};
let limit = cfg.limit;
let body_fut = HttpMessageBody::new(req, payload).limit(limit);
let body = HttpMessageBody::new(req, payload).limit(limit).await?;
Either::left(StringExtractFut { body_fut, encoding })
}
}
bytes_to_string(body, encoding)
/// Future for `String` extractor.
pub struct StringExtractFut {
body_fut: HttpMessageBody,
encoding: &'static Encoding,
}
impl Future for StringExtractFut {
type Output = Result<String, Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let encoding = self.encoding;
Pin::new(&mut self.body_fut).poll(cx).map(|out| {
let body = out?;
bytes_to_string(body, encoding)
})
}
}
fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, Error> {
use encoding_rs::UTF_8;
if encoding == UTF_8 {
Ok(str::from_utf8(body.as_ref())
.map_err(|_| ErrorBadRequest("Can not decode body"))?

View File

@@ -2,6 +2,7 @@
use std::{fmt, ops, sync::Arc};
use actix_utils::future::{err, ok, Ready};
use serde::de::DeserializeOwned;
use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequest};
@@ -107,27 +108,32 @@ impl<T: fmt::Display> fmt::Display for Query<T> {
/// See [here](#Examples) for example of usage as an extractor.
impl<T: DeserializeOwned> FromRequest for Query<T> {
type Error = Error;
type Future = Ready<Result<Self, Error>>;
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req
.app_data::<QueryConfig>()
.and_then(|c| c.err_handler.clone());
serde_urlencoded::from_str::<T>(req.query_string())
.map(|val| Ok(Query(val)))
.map(|val| ok(Query(val)))
.unwrap_or_else(move |e| {
let err = QueryPayloadError::Deserialize(e);
let e = QueryPayloadError::Deserialize(e);
log::debug!(
"Failed during Query extractor deserialization. \
Request path: {}",
Request path: {:?}",
req.path()
);
Err(match error_handler {
Some(error_handler) => (error_handler)(err, req),
None => err.into(),
})
let e = if let Some(error_handler) = error_handler {
(error_handler)(e, req)
} else {
e.into()
};
err(e)
})
}
}

View File

@@ -2,6 +2,8 @@
## Unreleased
## 3.3.0
- Update `trust-dns-resolver` dependency to `0.23`.
- Updated `zstd` dependency to `0.13`.

View File

@@ -1,6 +1,6 @@
[package]
name = "awc"
version = "3.2.0"
version = "3.3.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Async HTTP and WebSocket client library"
keywords = ["actix", "http", "framework", "async", "web"]
@@ -61,7 +61,7 @@ dangerous-h2c = []
[dependencies]
actix-codec = "0.5"
actix-service = "2"
actix-http = { version = "3.4", features = ["http2", "ws"] }
actix-http = { version = "3.5", features = ["http2", "ws"] }
actix-rt = { version = "2.1", default-features = false }
actix-tls = { version = "3.1", features = ["connect", "uri"] }
actix-utils = "3"
@@ -94,7 +94,7 @@ tls-rustls-0_21 = { package = "rustls", version = "0.21", optional = true, featu
trust-dns-resolver = { version = "0.23", optional = true }
[dev-dependencies]
actix-http = { version = "3.4", features = ["openssl"] }
actix-http = { version = "3.5", features = ["openssl"] }
actix-http-test = { version = "3", features = ["openssl"] }
actix-server = "2"
actix-test = { version = "0.1", features = ["openssl", "rustls-0_21"] }

View File

@@ -1,13 +1,17 @@
# awc (Actix Web Client)
# `awc` (Actix Web Client)
> Async HTTP and WebSocket client library.
<!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/awc?label=latest)](https://crates.io/crates/awc)
[![Documentation](https://docs.rs/awc/badge.svg?version=3.2.0)](https://docs.rs/awc/3.2.0)
[![Documentation](https://docs.rs/awc/badge.svg?version=3.3.0)](https://docs.rs/awc/3.3.0)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/awc)
[![Dependency Status](https://deps.rs/crate/awc/3.2.0/status.svg)](https://deps.rs/crate/awc/3.2.0)
[![Dependency Status](https://deps.rs/crate/awc/3.3.0/status.svg)](https://deps.rs/crate/awc/3.3.0)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)
<!-- prettier-ignore-end -->
## Documentation & Resources
- [API Documentation](https://docs.rs/awc)

View File

@@ -1,6 +1,11 @@
_list:
@just --list
# Format workspace.
fmt:
cargo +nightly fmt
npx -y prettier --write $(fd --type=file --hidden --extension=md --extension=yml)
# Document crates in workspace.
doc:
RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc --no-deps --workspace --features=rustls,openssl
@@ -9,3 +14,8 @@ doc:
doc-watch:
RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc --no-deps --workspace --features=rustls,openssl --open
cargo watch -- RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc --no-deps --workspace --features=rustls,openssl
# Update READMEs from crate root documentation.
update-readmes: && fmt
cd ./actix-files && cargo rdme --force
cd ./actix-router && cargo rdme --force

View File

@@ -1 +0,0 @@
nightly

View File

@@ -5,7 +5,7 @@
# requires github cli tool for automatic release draft creation
set -euo pipefail
set -eEuo pipefail
DIR=$1
@@ -112,17 +112,25 @@ echo
read -p "Update all references: (y/N) " UPDATE_REFERENCES
UPDATE_REFERENCES="${UPDATE_REFERENCES:-n}"
if [ "$UPDATE_REFERENCES" = 'y' ] || [ "$UPDATE_REFERENCES" = 'Y' ]; then
if [[ $NEW_VERSION == *".0.0" ]]; then
NEW_VERSION_SPEC="${NEW_VERSION%.0.0}"
elif [[ $NEW_VERSION == *".0" ]]; then
NEW_VERSION_SPEC="${NEW_VERSION%.0}"
else
NEW_VERSION_SPEC="$NEW_VERSION"
fi
for f in $(fd Cargo.toml); do
sed -i.bak -E \
"s/^(${PACKAGE_NAME} ?= ?\")[^\"]+(\")$/\1${NEW_VERSION}\2/g" $f
"s/^(${PACKAGE_NAME} ?= ?\")[^\"]+(\")$/\1${NEW_VERSION_SPEC}\2/g" $f
sed -i.bak -E \
"s/^(${PACKAGE_NAME} ?=.*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION}\2/g" $f
"s/^(${PACKAGE_NAME} ?=.*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION_SPEC}\2/g" $f
sed -i.bak -E \
"s/^(.*package ?= ?\"${PACKAGE_NAME}\".*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION}\2/g" $f
"s/^(.*package ?= ?\"${PACKAGE_NAME}\".*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION_SPEC}\2/g" $f
sed -i.bak -E \
"s/^(.*version ?= ?\")[^\"]+(\".*package ?= ?\"${PACKAGE_NAME}\".*)$/\1${NEW_VERSION}\2/g" $f
"s/^(.*version ?= ?\")[^\"]+(\".*package ?= ?\"${PACKAGE_NAME}\".*)$/\1${NEW_VERSION_SPEC}\2/g" $f
# remove backup file
rm -f $f.bak
@@ -132,6 +140,7 @@ fi
if [ $MACOS ]; then
printf "chore($PACKAGE_NAME): prepare release $NEW_VERSION" | pbcopy
echo "placed the recommended commit message on the clipboard"
else
echo
echo "commit message:"