mirror of https://github.com/fafhrd91/actix-web
Compare commits
149 Commits
287c59fce1
...
43d3c6af83
Author | SHA1 | Date |
---|---|---|
|
43d3c6af83 | |
|
fdde0b8b14 | |
|
2a634a8af2 | |
|
5e0f56f572 | |
|
10266204ee | |
|
ebc8fdb29a | |
|
b61346e2ef | |
|
db0a0512d9 | |
|
eb1429d6dd | |
|
10f56f6412 | |
|
8562b7d7bb | |
|
442bb4cecf | |
|
9d98753ffa | |
|
fd81e6ee0d | |
|
1f9bc2de7d | |
|
79f2486dc1 | |
|
9a12bbd118 | |
|
072fdc182d | |
|
347c7204a6 | |
|
573090e01f | |
|
69dda5c943 | |
|
1b4b61d839 | |
|
2c55d659dd | |
|
276f5d5bd4 | |
|
5f3c02813a | |
|
3d3b31e16a | |
|
3147aaccc7 | |
|
079400a72b | |
|
a49f055561 | |
|
55268b6898 | |
|
89b5b04653 | |
|
e42cffc28d | |
|
8765b04476 | |
|
40179e2aa6 | |
|
9bbb5414d1 | |
|
65f254d1b2 | |
|
25511dfb38 | |
|
6e902d1d5c | |
|
f1b7cfb253 | |
|
e983276a78 | |
|
bbe0134523 | |
|
2dd165dc0b | |
|
9829f87cd1 | |
|
6892896a62 | |
|
bdb7b4da2d | |
|
7eea3d3657 | |
|
ad73cdc823 | |
|
e27e1e3806 | |
|
5939af88b3 | |
|
741ca85d4a | |
|
b8a612d21e | |
|
74c1c3de59 | |
|
6b9c47b0ec | |
|
9915defca3 | |
|
0ef246a846 | |
|
c284426698 | |
|
b75ae11e62 | |
|
7fddaae878 | |
|
52264448cd | |
|
90c19a835d | |
|
adf57d2b24 | |
|
fcd10fbb5e | |
|
95b6a81f43 | |
|
ab18efe0ac | |
|
cede0c6dbb | |
|
1005b6a12a | |
|
d898e8f739 | |
|
353873fc04 | |
|
1390e29705 | |
|
c6e7ebd185 | |
|
e8351cc3aa | |
|
f63cf69e6a | |
|
92c1e2230d | |
|
4bb495aba0 | |
|
aa000b429d | |
|
df0885cf21 | |
|
0796f8e796 | |
|
a2307fbb86 | |
|
98ced477f7 | |
|
98c263b3ee | |
|
b8bdee0606 | |
|
85843b9b0f | |
|
9656383646 | |
|
cee7451915 | |
|
eb6f6a1976 | |
|
04533a15fa | |
|
a4eaa7f0bb | |
|
66e2afe306 | |
|
59961a58a8 | |
|
33b487e854 | |
|
182055bcb5 | |
|
a36280466c | |
|
8690f80a08 | |
|
91e29c0ce4 | |
|
b0fe679784 | |
|
0fafb486d4 | |
|
5aeb0dd950 | |
|
856480cd90 | |
|
bb1442e20b | |
|
ac2a3bb124 | |
|
8200e4ee82 | |
|
5b60d81f57 | |
|
ee6a6ec03e | |
|
34327bd221 | |
|
472dbca64e | |
|
d8566da66f | |
|
a908afa56b | |
|
8115c818c1 | |
|
002c1b5a19 | |
|
836c75064b | |
|
2132c95b01 | |
|
eff2a20c90 | |
|
9d849c19a5 | |
|
6771be20b3 | |
|
ef977055fc | |
|
568bffeb58 | |
|
03c65d93e5 | |
|
ec05381f6f | |
|
4c05c87b11 | |
|
27c07f122b | |
|
3849cdaa6c | |
|
a5c2d0531b | |
|
049b49290d | |
|
b7a0ff0a3a | |
|
a0a6761bfe | |
|
ff9c0f7157 | |
|
1c4e265a70 | |
|
d9d22825d4 | |
|
9a685cabad | |
|
93edef8fee | |
|
d148e84aba | |
|
7360c732b3 | |
|
48aaf41638 | |
|
bb13f54180 | |
|
b52e77beb4 | |
|
b4f8bda032 | |
|
c055723997 | |
|
d6bdfac1b9 | |
|
78ac5cf482 | |
|
4303dd8c37 | |
|
f61fcbe840 | |
|
538c1bea34 | |
|
70e3758ecc | |
|
5ad92c0062 | |
|
e0918fb179 | |
|
9ba326aed0 | |
|
882fb3d25b | |
|
be28a0bd6d | |
|
a431b7356c |
|
@ -0,0 +1,8 @@
|
|||
disallowed-names = [
|
||||
"..",
|
||||
"e", # no single letter error bindings
|
||||
]
|
||||
disallowed-methods = [
|
||||
{ path = "std::cell::RefCell::default()", reason = "prefer explicit inner type default" },
|
||||
{ path = "std::rc::Rc::default()", reason = "prefer explicit inner type default" },
|
||||
]
|
|
@ -0,0 +1,12 @@
|
|||
version: "0.2"
|
||||
words:
|
||||
- actix
|
||||
- addrs
|
||||
- bytestring
|
||||
- httparse
|
||||
- msrv
|
||||
- realip
|
||||
- rustls
|
||||
- rustup
|
||||
- serde
|
||||
- zstd
|
|
@ -1,10 +1,11 @@
|
|||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: cargo
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
- package-ecosystem: cargo
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
versioning-strategy: lockfile-only
|
||||
|
|
|
@ -32,7 +32,7 @@ jobs:
|
|||
|
||||
- name: Install nasm
|
||||
if: matrix.target.os == 'windows-latest'
|
||||
uses: ilammy/setup-nasm@v1.5.1
|
||||
uses: ilammy/setup-nasm@v1.5.2
|
||||
|
||||
- name: Install OpenSSL
|
||||
if: matrix.target.os == 'windows-latest'
|
||||
|
@ -44,12 +44,12 @@ jobs:
|
|||
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
||||
|
||||
- name: Install Rust (${{ matrix.version.name }})
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
uses: taiki-e/install-action@v2.54.0
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
|
@ -80,10 +80,10 @@ jobs:
|
|||
uses: rui314/setup-mold@v1
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
|
||||
- name: Install just, cargo-hack
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
uses: taiki-e/install-action@v2.54.0
|
||||
with:
|
||||
tool: just,cargo-hack
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ jobs:
|
|||
|
||||
- name: Install nasm
|
||||
if: matrix.target.os == 'windows-latest'
|
||||
uses: ilammy/setup-nasm@v1.5.1
|
||||
uses: ilammy/setup-nasm@v1.5.2
|
||||
|
||||
- name: Install OpenSSL
|
||||
if: matrix.target.os == 'windows-latest'
|
||||
|
@ -59,12 +59,12 @@ jobs:
|
|||
uses: rui314/setup-mold@v1
|
||||
|
||||
- name: Install Rust (${{ matrix.version.name }})
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
uses: taiki-e/install-action@v2.54.0
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
|
@ -92,7 +92,7 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
|
||||
|
@ -108,12 +108,12 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
uses: taiki-e/install-action@v2.54.0
|
||||
with:
|
||||
tool: just
|
||||
|
||||
|
|
|
@ -18,13 +18,13 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
components: llvm-tools
|
||||
|
||||
- name: Install just, cargo-llvm-cov, cargo-nextest
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
uses: taiki-e/install-action@v2.54.0
|
||||
with:
|
||||
tool: just,cargo-llvm-cov,cargo-nextest
|
||||
|
||||
|
@ -32,7 +32,7 @@ jobs:
|
|||
run: just test-coverage-codecov
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v4.5.0
|
||||
uses: codecov/codecov-action@v5.4.3
|
||||
with:
|
||||
files: codecov.json
|
||||
fail_ci_if_error: true
|
||||
|
|
|
@ -18,7 +18,7 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
components: rustfmt
|
||||
|
@ -36,7 +36,7 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
components: clippy
|
||||
|
||||
|
@ -55,7 +55,7 @@ jobs:
|
|||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
components: rust-docs
|
||||
|
@ -66,51 +66,25 @@ jobs:
|
|||
run: cargo +nightly doc --no-deps --workspace --all-features
|
||||
|
||||
check-external-types:
|
||||
if: false # rustdoc mismatch currently
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly-2024-05-01)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
- name: Install Rust (${{ vars.RUST_VERSION_EXTERNAL_TYPES }})
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.13.0
|
||||
with:
|
||||
toolchain: nightly-2024-05-01
|
||||
toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
uses: taiki-e/install-action@v2.54.0
|
||||
with:
|
||||
tool: just
|
||||
|
||||
- name: Install cargo-check-external-types
|
||||
uses: taiki-e/cache-cargo-install-action@v2.0.1
|
||||
uses: taiki-e/cache-cargo-install-action@v2.1.2
|
||||
with:
|
||||
tool: cargo-check-external-types
|
||||
|
||||
- name: check external types
|
||||
run: just check-external-types-all +nightly-2024-05-01
|
||||
|
||||
public-api-diff:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout main branch
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.base_ref }}
|
||||
|
||||
- name: Checkout PR branch
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly-2024-06-07)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: nightly-2024-06-07
|
||||
|
||||
- name: Install cargo-public-api
|
||||
uses: taiki-e/install-action@v2.42.17
|
||||
with:
|
||||
tool: cargo-public-api
|
||||
|
||||
- name: Generate API diff
|
||||
run: |
|
||||
for f in $(find -mindepth 2 -maxdepth 2 -name Cargo.toml); do
|
||||
cargo public-api --manifest-path "$f" --simplified diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
|
||||
done
|
||||
run: just check-external-types-all +${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
Cargo.lock
|
||||
target/
|
||||
guide/build/
|
||||
/gh-pages
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
exclude = ["target/*"]
|
||||
include = ["**/*.toml"]
|
||||
|
||||
[formatting]
|
||||
column_width = 100
|
||||
align_comments = false
|
||||
|
||||
[[rule]]
|
||||
include = ["**/Cargo.toml"]
|
||||
keys = ["features"]
|
||||
formatting.column_width = 105
|
||||
formatting.reorder_keys = false
|
||||
|
||||
[[rule]]
|
||||
include = ["**/Cargo.toml"]
|
||||
keys = [
|
||||
"dependencies",
|
||||
"*-dependencies",
|
||||
"workspace.dependencies",
|
||||
"workspace.*-dependencies",
|
||||
"target.*.dependencies",
|
||||
"target.*.*-dependencies",
|
||||
]
|
||||
formatting.column_width = 120
|
||||
formatting.reorder_keys = true
|
||||
|
||||
[[rule]]
|
||||
include = ["**/Cargo.toml"]
|
||||
keys = [
|
||||
"dependencies.*",
|
||||
"*-dependencies.*",
|
||||
"workspace.dependencies.*",
|
||||
"workspace.*-dependencies.*",
|
||||
"target.*.dependencies",
|
||||
"target.*.*-dependencies",
|
||||
]
|
||||
formatting.column_width = 120
|
||||
formatting.reorder_keys = false
|
File diff suppressed because it is too large
Load Diff
24
Cargo.toml
24
Cargo.toml
|
@ -1,17 +1,17 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"actix-files",
|
||||
"actix-http-test",
|
||||
"actix-http",
|
||||
"actix-multipart",
|
||||
"actix-multipart-derive",
|
||||
"actix-router",
|
||||
"actix-test",
|
||||
"actix-web-actors",
|
||||
"actix-web-codegen",
|
||||
"actix-web",
|
||||
"awc",
|
||||
"actix-files",
|
||||
"actix-http-test",
|
||||
"actix-http",
|
||||
"actix-multipart",
|
||||
"actix-multipart-derive",
|
||||
"actix-router",
|
||||
"actix-test",
|
||||
"actix-web-actors",
|
||||
"actix-web-codegen",
|
||||
"actix-web",
|
||||
"awc",
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
|
@ -19,7 +19,7 @@ homepage = "https://actix.rs"
|
|||
repository = "https://github.com/actix/actix-web"
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.72"
|
||||
rust-version = "1.75"
|
||||
|
||||
[profile.dev]
|
||||
# Disabling debug info speeds up builds a bunch and we don't rely on it for debugging that much.
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
- Minimum supported Rust version (MSRV) is now 1.75.
|
||||
|
||||
## 0.6.6
|
||||
|
||||
- Update `tokio-uring` dependency to `0.4`.
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
[package]
|
||||
name = "actix-files"
|
||||
version = "0.6.6"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"]
|
||||
description = "Static file serving for Actix Web"
|
||||
keywords = ["actix", "http", "async", "futures"]
|
||||
homepage = "https://actix.rs"
|
||||
|
@ -14,13 +11,7 @@ license = "MIT OR Apache-2.0"
|
|||
edition = "2021"
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_http::*",
|
||||
"actix_service::*",
|
||||
"actix_web::*",
|
||||
"http::*",
|
||||
"mime::*",
|
||||
]
|
||||
allowed_external_types = ["actix_http::*", "actix_service::*", "actix_web::*", "http::*", "mime::*"]
|
||||
|
||||
[features]
|
||||
experimental-io-uring = ["actix-web/experimental-io-uring", "tokio-uring"]
|
||||
|
@ -33,7 +24,7 @@ actix-web = { version = "4", default-features = false }
|
|||
|
||||
bitflags = "2"
|
||||
bytes = "1"
|
||||
derive_more = "0.99.5"
|
||||
derive_more = { version = "2", features = ["display", "error", "from"] }
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
http-range = "0.1.4"
|
||||
log = "0.4"
|
||||
|
@ -46,7 +37,7 @@ v_htmlescape = "0.15.5"
|
|||
# experimental-io-uring
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
tokio-uring = { version = "0.5", optional = true, features = ["bytes"] }
|
||||
actix-server = { version = "2.4", optional = true } # ensure matching tokio-uring versions
|
||||
actix-server = { version = "2.4", optional = true } # ensure matching tokio-uring versions
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.7"
|
||||
|
|
|
@ -6,11 +6,11 @@ use derive_more::Display;
|
|||
pub enum FilesError {
|
||||
/// Path is not a directory.
|
||||
#[allow(dead_code)]
|
||||
#[display(fmt = "path is not a directory. Unable to serve static files")]
|
||||
#[display("path is not a directory. Unable to serve static files")]
|
||||
IsNotDirectory,
|
||||
|
||||
/// Cannot render directory.
|
||||
#[display(fmt = "unable to render directory without index file")]
|
||||
#[display("unable to render directory without index file")]
|
||||
IsDirectory,
|
||||
}
|
||||
|
||||
|
@ -25,19 +25,19 @@ impl ResponseError for FilesError {
|
|||
#[non_exhaustive]
|
||||
pub enum UriSegmentError {
|
||||
/// Segment started with the wrapped invalid character.
|
||||
#[display(fmt = "segment started with invalid character: ('{_0}')")]
|
||||
#[display("segment started with invalid character: ('{_0}')")]
|
||||
BadStart(char),
|
||||
|
||||
/// Segment contained the wrapped invalid character.
|
||||
#[display(fmt = "segment contained invalid character ('{_0}')")]
|
||||
#[display("segment contained invalid character ('{_0}')")]
|
||||
BadChar(char),
|
||||
|
||||
/// Segment ended with the wrapped invalid character.
|
||||
#[display(fmt = "segment ended with invalid character: ('{_0}')")]
|
||||
#[display("segment ended with invalid character: ('{_0}')")]
|
||||
BadEnd(char),
|
||||
|
||||
/// Path is not a valid UTF-8 string after percent-decoding.
|
||||
#[display(fmt = "path is not a valid UTF-8 string after percent-decoding")]
|
||||
#[display("path is not a valid UTF-8 string after percent-decoding")]
|
||||
NotValidUtf8,
|
||||
}
|
||||
|
||||
|
|
|
@ -79,7 +79,7 @@ impl FilesService {
|
|||
|
||||
let (req, _) = req.into_parts();
|
||||
|
||||
(self.renderer)(&dir, &req).unwrap_or_else(|e| ServiceResponse::from_err(e, req))
|
||||
(self.renderer)(&dir, &req).unwrap_or_else(|err| ServiceResponse::from_err(err, req))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,10 @@ keywords = ["http", "web", "framework", "async", "futures"]
|
|||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
categories = [
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
@ -20,14 +20,14 @@ features = []
|
|||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_codec::*",
|
||||
"actix_http::*",
|
||||
"actix_server::*",
|
||||
"awc::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"tokio::*",
|
||||
"actix_codec::*",
|
||||
"actix_http::*",
|
||||
"actix_server::*",
|
||||
"awc::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"tokio::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
|
@ -37,25 +37,25 @@ default = []
|
|||
openssl = ["tls-openssl", "awc/openssl"]
|
||||
|
||||
[dependencies]
|
||||
actix-service = "2"
|
||||
actix-codec = "0.5"
|
||||
actix-tls = "3"
|
||||
actix-utils = "3"
|
||||
actix-rt = "2.2"
|
||||
actix-server = "2"
|
||||
actix-service = "2"
|
||||
actix-tls = "3"
|
||||
actix-utils = "3"
|
||||
awc = { version = "3", default-features = false }
|
||||
|
||||
bytes = "1"
|
||||
futures-core = { version = "0.3.17", default-features = false }
|
||||
http = "0.2.7"
|
||||
log = "0.4"
|
||||
socket2 = "0.5"
|
||||
serde = "1"
|
||||
serde_json = "1"
|
||||
slab = "0.4"
|
||||
serde_urlencoded = "0.7"
|
||||
slab = "0.4"
|
||||
socket2 = "0.5"
|
||||
tls-openssl = { version = "0.10.55", package = "openssl", optional = true }
|
||||
tokio = { version = "1.24.2", features = ["sync"] }
|
||||
tokio = { version = "1.38.2", features = ["sync"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-http = "3"
|
||||
|
|
|
@ -106,7 +106,7 @@ pub async fn test_server_with_addr<F: ServerServiceFactory<TcpStream>>(
|
|||
builder.set_verify(SslVerifyMode::NONE);
|
||||
let _ = builder
|
||||
.set_alpn_protos(b"\x02h2\x08http/1.1")
|
||||
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
|
||||
.map_err(|err| log::error!("Can not set ALPN protocol: {err}"));
|
||||
|
||||
Connector::new()
|
||||
.conn_lifetime(Duration::from_secs(0))
|
||||
|
|
|
@ -2,6 +2,26 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
## 3.11.0
|
||||
|
||||
- Update `brotli` dependency to `8`.
|
||||
|
||||
## 3.10.0
|
||||
|
||||
### Added
|
||||
|
||||
- Add `header::CLEAR_SITE_DATA` constant.
|
||||
- Add `Extensions::get_or_insert[_with]()` methods.
|
||||
- Implement `From<Bytes>` for `Payload`.
|
||||
- Implement `From<Vec<u8>>` for `Payload`.
|
||||
|
||||
### Changed
|
||||
|
||||
- Update `brotli` dependency to `7`.
|
||||
- Minimum supported Rust version (MSRV) is now 1.75.
|
||||
|
||||
## 3.9.0
|
||||
|
||||
### Added
|
||||
|
||||
- Implement `FromIterator<(HeaderName, HeaderValue)>` for `HeaderMap`.
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
[package]
|
||||
name = "actix-http"
|
||||
version = "3.8.0"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
version = "3.11.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"]
|
||||
description = "HTTP types and services for the Actix ecosystem"
|
||||
keywords = ["actix", "http", "framework", "async", "futures"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
categories = [
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
]
|
||||
license.workspace = true
|
||||
edition.workspace = true
|
||||
|
@ -22,37 +19,37 @@ rust-version.workspace = true
|
|||
[package.metadata.docs.rs]
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
features = [
|
||||
"http2",
|
||||
"ws",
|
||||
"openssl",
|
||||
"rustls-0_20",
|
||||
"rustls-0_21",
|
||||
"rustls-0_22",
|
||||
"rustls-0_23",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
"http2",
|
||||
"ws",
|
||||
"openssl",
|
||||
"rustls-0_20",
|
||||
"rustls-0_21",
|
||||
"rustls-0_22",
|
||||
"rustls-0_23",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
]
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_codec::*",
|
||||
"actix_service::*",
|
||||
"actix_tls::*",
|
||||
"actix_utils::*",
|
||||
"bytes::*",
|
||||
"bytestring::*",
|
||||
"encoding_rs::*",
|
||||
"futures_core::*",
|
||||
"h2::*",
|
||||
"http::*",
|
||||
"httparse::*",
|
||||
"language_tags::*",
|
||||
"mime::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"tokio_util::*",
|
||||
"tokio::*",
|
||||
"actix_codec::*",
|
||||
"actix_service::*",
|
||||
"actix_tls::*",
|
||||
"actix_utils::*",
|
||||
"bytes::*",
|
||||
"bytestring::*",
|
||||
"encoding_rs::*",
|
||||
"futures_core::*",
|
||||
"h2::*",
|
||||
"http::*",
|
||||
"httparse::*",
|
||||
"language_tags::*",
|
||||
"mime::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"tokio_util::*",
|
||||
"tokio::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
|
@ -62,12 +59,7 @@ default = []
|
|||
http2 = ["dep:h2"]
|
||||
|
||||
# WebSocket protocol implementation
|
||||
ws = [
|
||||
"dep:local-channel",
|
||||
"dep:base64",
|
||||
"dep:rand",
|
||||
"dep:sha1",
|
||||
]
|
||||
ws = ["dep:local-channel", "dep:base64", "dep:rand", "dep:sha1"]
|
||||
|
||||
# TLS via OpenSSL
|
||||
openssl = ["__tls", "actix-tls/accept", "actix-tls/openssl"]
|
||||
|
@ -89,8 +81,8 @@ rustls-0_23 = ["__tls", "actix-tls/accept", "actix-tls/rustls-0_23"]
|
|||
|
||||
# Compression codecs
|
||||
compress-brotli = ["__compress", "dep:brotli"]
|
||||
compress-gzip = ["__compress", "dep:flate2"]
|
||||
compress-zstd = ["__compress", "dep:zstd"]
|
||||
compress-gzip = ["__compress", "dep:flate2"]
|
||||
compress-zstd = ["__compress", "dep:zstd"]
|
||||
|
||||
# Internal (PRIVATE!) features used to aid testing and checking feature status.
|
||||
# Don't rely on these whatsoever. They are semver-exempt and may disappear at anytime.
|
||||
|
@ -101,17 +93,17 @@ __compress = []
|
|||
__tls = []
|
||||
|
||||
[dependencies]
|
||||
actix-service = "2"
|
||||
actix-codec = "0.5"
|
||||
actix-utils = "3"
|
||||
actix-rt = { version = "2.2", default-features = false }
|
||||
actix-service = "2"
|
||||
actix-utils = "3"
|
||||
|
||||
ahash = "0.8"
|
||||
bitflags = "2"
|
||||
bytes = "1"
|
||||
bytestring = "1"
|
||||
derive_more = "0.99.5"
|
||||
derive_more = { version = "2", features = ["as_ref", "deref", "deref_mut", "display", "error", "from"] }
|
||||
encoding_rs = "0.8"
|
||||
foldhash = "0.1"
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
http = "0.2.7"
|
||||
httparse = "1.5.1"
|
||||
|
@ -122,7 +114,7 @@ mime = "0.3.4"
|
|||
percent-encoding = "2.1"
|
||||
pin-project-lite = "0.2"
|
||||
smallvec = "1.6.1"
|
||||
tokio = { version = "1.24.2", features = [] }
|
||||
tokio = { version = "1.38.2", features = [] }
|
||||
tokio-util = { version = "0.7", features = ["io", "codec"] }
|
||||
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
||||
|
||||
|
@ -130,16 +122,16 @@ tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
|||
h2 = { version = "0.3.26", optional = true }
|
||||
|
||||
# websockets
|
||||
local-channel = { version = "0.1", optional = true }
|
||||
base64 = { version = "0.22", optional = true }
|
||||
rand = { version = "0.8", optional = true }
|
||||
local-channel = { version = "0.1", optional = true }
|
||||
rand = { version = "0.9", optional = true }
|
||||
sha1 = { version = "0.10", optional = true }
|
||||
|
||||
# openssl/rustls
|
||||
actix-tls = { version = "3.4", default-features = false, optional = true }
|
||||
|
||||
# compress-*
|
||||
brotli = { version = "6", optional = true }
|
||||
brotli = { version = "8", optional = true }
|
||||
flate2 = { version = "1.0.13", optional = true }
|
||||
zstd = { version = "0.13", optional = true }
|
||||
|
||||
|
@ -155,17 +147,17 @@ divan = "0.1.8"
|
|||
env_logger = "0.11"
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
memchr = "2.4"
|
||||
once_cell = "1.9"
|
||||
once_cell = "1.21"
|
||||
rcgen = "0.13"
|
||||
regex = "1.3"
|
||||
rustversion = "1"
|
||||
rustls-pemfile = "2"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
rustversion = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
static_assertions = "1"
|
||||
tls-openssl = { package = "openssl", version = "0.10.55" }
|
||||
tls-rustls_023 = { package = "rustls", version = "0.23" }
|
||||
tokio = { version = "1.24.2", features = ["net", "rt", "macros"] }
|
||||
tokio = { version = "1.38.2", features = ["net", "rt", "macros"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://docs.rs/actix-http/3.8.0)
|
||||
[](https://docs.rs/actix-http/3.11.0)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-http/3.8.0)
|
||||
[](https://deps.rs/crate/actix-http/3.11.0)
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use actix_http::HttpService;
|
||||
use actix_server::Server;
|
||||
use actix_service::map_config;
|
||||
use actix_web::{dev::AppConfig, get, App};
|
||||
use actix_web::{dev::AppConfig, get, App, Responder};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> &'static str {
|
||||
async fn index() -> impl Responder {
|
||||
"Hello, world. From Actix Web!"
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ async fn main() -> io::Result<()> {
|
|||
body.extend_from_slice(&item?);
|
||||
}
|
||||
|
||||
info!("request body: {:?}", body);
|
||||
info!("request body: {body:?}");
|
||||
|
||||
let res = Response::build(StatusCode::OK)
|
||||
.insert_header(("x-head", HeaderValue::from_static("dummy value!")))
|
||||
|
@ -31,8 +31,7 @@ async fn main() -> io::Result<()> {
|
|||
|
||||
Ok::<_, Error>(res)
|
||||
})
|
||||
// No TLS
|
||||
.tcp()
|
||||
.tcp() // No TLS
|
||||
})?
|
||||
.run()
|
||||
.await
|
||||
|
|
|
@ -17,7 +17,7 @@ async fn main() -> io::Result<()> {
|
|||
ext.insert(42u32);
|
||||
})
|
||||
.finish(|req: Request| async move {
|
||||
info!("{:?}", req);
|
||||
info!("{req:?}");
|
||||
|
||||
let mut res = Response::build(StatusCode::OK);
|
||||
res.insert_header(("x-head", HeaderValue::from_static("dummy value!")));
|
||||
|
|
|
@ -22,16 +22,16 @@ async fn main() -> io::Result<()> {
|
|||
.bind("streaming-error", ("127.0.0.1", 8080), || {
|
||||
HttpService::build()
|
||||
.finish(|req| async move {
|
||||
info!("{:?}", req);
|
||||
info!("{req:?}");
|
||||
let res = Response::ok();
|
||||
|
||||
Ok::<_, Infallible>(res.set_body(BodyStream::new(stream! {
|
||||
yield Ok(Bytes::from("123"));
|
||||
yield Ok(Bytes::from("456"));
|
||||
|
||||
actix_rt::time::sleep(Duration::from_millis(1000)).await;
|
||||
actix_rt::time::sleep(Duration::from_secs(1)).await;
|
||||
|
||||
yield Err(io::Error::new(io::ErrorKind::Other, ""));
|
||||
yield Err(io::Error::other("abc"));
|
||||
})))
|
||||
})
|
||||
.tcp()
|
||||
|
|
|
@ -17,7 +17,6 @@ use bytes::{Bytes, BytesMut};
|
|||
use bytestring::ByteString;
|
||||
use futures_core::{ready, Stream};
|
||||
use tokio_util::codec::Encoder;
|
||||
use tracing::{info, trace};
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
|
@ -37,12 +36,12 @@ async fn main() -> io::Result<()> {
|
|||
}
|
||||
|
||||
async fn handler(req: Request) -> Result<Response<BodyStream<Heartbeat>>, Error> {
|
||||
info!("handshaking");
|
||||
tracing::info!("handshaking");
|
||||
let mut res = ws::handshake(req.head())?;
|
||||
|
||||
// handshake will always fail under HTTP/2
|
||||
|
||||
info!("responding");
|
||||
tracing::info!("responding");
|
||||
res.message_body(BodyStream::new(Heartbeat::new(ws::Codec::new())))
|
||||
}
|
||||
|
||||
|
@ -64,7 +63,7 @@ impl Stream for Heartbeat {
|
|||
type Item = Result<Bytes, Error>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
trace!("poll");
|
||||
tracing::trace!("poll");
|
||||
|
||||
ready!(self.as_mut().interval.poll_tick(cx));
|
||||
|
||||
|
|
|
@ -131,7 +131,7 @@ mod tests {
|
|||
assert_eq!(to_bytes(body).await.ok(), Some(Bytes::from("12")));
|
||||
}
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "stream error")]
|
||||
#[display("stream error")]
|
||||
struct StreamErr;
|
||||
|
||||
#[actix_rt::test]
|
||||
|
|
|
@ -38,7 +38,7 @@ pub async fn to_bytes<B: MessageBody>(body: B) -> Result<Bytes, B::Error> {
|
|||
|
||||
/// Error type returned from [`to_bytes_limited`] when body produced exceeds limit.
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "limit exceeded while collecting body bytes")]
|
||||
#[display("limit exceeded while collecting body bytes")]
|
||||
#[non_exhaustive]
|
||||
pub struct BodyLimitExceeded;
|
||||
|
||||
|
@ -190,7 +190,7 @@ mod tests {
|
|||
|
||||
#[actix_rt::test]
|
||||
async fn to_body_limit_error() {
|
||||
let err_stream = stream::once(async { Err(io::Error::new(io::ErrorKind::Other, "")) });
|
||||
let err_stream = stream::once(async { Err(io::Error::other("")) });
|
||||
let body = SizedStream::new(8, err_stream);
|
||||
// not too big, but propagates error from body stream
|
||||
assert!(to_bytes_limited(body, 10).await.unwrap().is_err());
|
||||
|
|
|
@ -100,10 +100,7 @@ where
|
|||
loop {
|
||||
if let Some(ref mut fut) = this.fut {
|
||||
let (chunk, decoder) = ready!(Pin::new(fut).poll(cx)).map_err(|_| {
|
||||
PayloadError::Io(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Blocking task was cancelled unexpectedly",
|
||||
))
|
||||
PayloadError::Io(io::Error::other("Blocking task was cancelled unexpectedly"))
|
||||
})??;
|
||||
|
||||
*this.decoder = Some(decoder);
|
||||
|
|
|
@ -183,8 +183,7 @@ where
|
|||
if let Some(ref mut fut) = this.fut {
|
||||
let mut encoder = ready!(Pin::new(fut).poll(cx))
|
||||
.map_err(|_| {
|
||||
EncoderError::Io(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
EncoderError::Io(io::Error::other(
|
||||
"Blocking task was cancelled unexpectedly",
|
||||
))
|
||||
})?
|
||||
|
@ -415,11 +414,11 @@ fn new_brotli_compressor() -> Box<brotli::CompressorWriter<Writer>> {
|
|||
#[non_exhaustive]
|
||||
pub enum EncoderError {
|
||||
/// Wrapped body stream error.
|
||||
#[display(fmt = "body")]
|
||||
#[display("body")]
|
||||
Body(Box<dyn StdError>),
|
||||
|
||||
/// Generic I/O error.
|
||||
#[display(fmt = "io")]
|
||||
#[display("io")]
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -80,28 +80,28 @@ impl From<Error> for Response<BoxBody> {
|
|||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
|
||||
pub(crate) enum Kind {
|
||||
#[display(fmt = "error processing HTTP")]
|
||||
#[display("error processing HTTP")]
|
||||
Http,
|
||||
|
||||
#[display(fmt = "error parsing HTTP message")]
|
||||
#[display("error parsing HTTP message")]
|
||||
Parse,
|
||||
|
||||
#[display(fmt = "request payload read error")]
|
||||
#[display("request payload read error")]
|
||||
Payload,
|
||||
|
||||
#[display(fmt = "response body write error")]
|
||||
#[display("response body write error")]
|
||||
Body,
|
||||
|
||||
#[display(fmt = "send response error")]
|
||||
#[display("send response error")]
|
||||
SendResponse,
|
||||
|
||||
#[display(fmt = "error in WebSocket process")]
|
||||
#[display("error in WebSocket process")]
|
||||
Ws,
|
||||
|
||||
#[display(fmt = "connection error")]
|
||||
#[display("connection error")]
|
||||
Io,
|
||||
|
||||
#[display(fmt = "encoder error")]
|
||||
#[display("encoder error")]
|
||||
Encoder,
|
||||
}
|
||||
|
||||
|
@ -160,44 +160,44 @@ impl From<crate::ws::ProtocolError> for Error {
|
|||
#[non_exhaustive]
|
||||
pub enum ParseError {
|
||||
/// An invalid `Method`, such as `GE.T`.
|
||||
#[display(fmt = "invalid method specified")]
|
||||
#[display("invalid method specified")]
|
||||
Method,
|
||||
|
||||
/// An invalid `Uri`, such as `exam ple.domain`.
|
||||
#[display(fmt = "URI error: {}", _0)]
|
||||
#[display("URI error: {}", _0)]
|
||||
Uri(InvalidUri),
|
||||
|
||||
/// An invalid `HttpVersion`, such as `HTP/1.1`
|
||||
#[display(fmt = "invalid HTTP version specified")]
|
||||
#[display("invalid HTTP version specified")]
|
||||
Version,
|
||||
|
||||
/// An invalid `Header`.
|
||||
#[display(fmt = "invalid Header provided")]
|
||||
#[display("invalid Header provided")]
|
||||
Header,
|
||||
|
||||
/// A message head is too large to be reasonable.
|
||||
#[display(fmt = "message head is too large")]
|
||||
#[display("message head is too large")]
|
||||
TooLarge,
|
||||
|
||||
/// A message reached EOF, but is not complete.
|
||||
#[display(fmt = "message is incomplete")]
|
||||
#[display("message is incomplete")]
|
||||
Incomplete,
|
||||
|
||||
/// An invalid `Status`, such as `1337 ELITE`.
|
||||
#[display(fmt = "invalid status provided")]
|
||||
#[display("invalid status provided")]
|
||||
Status,
|
||||
|
||||
/// A timeout occurred waiting for an IO event.
|
||||
#[allow(dead_code)]
|
||||
#[display(fmt = "timeout")]
|
||||
#[display("timeout")]
|
||||
Timeout,
|
||||
|
||||
/// An I/O error that occurred while trying to read or write to a network stream.
|
||||
#[display(fmt = "I/O error: {}", _0)]
|
||||
#[display("I/O error: {}", _0)]
|
||||
Io(io::Error),
|
||||
|
||||
/// Parsing a field as string failed.
|
||||
#[display(fmt = "UTF-8 error: {}", _0)]
|
||||
#[display("UTF-8 error: {}", _0)]
|
||||
Utf8(Utf8Error),
|
||||
}
|
||||
|
||||
|
@ -256,28 +256,28 @@ impl From<ParseError> for Response<BoxBody> {
|
|||
#[non_exhaustive]
|
||||
pub enum PayloadError {
|
||||
/// A payload reached EOF, but is not complete.
|
||||
#[display(fmt = "payload reached EOF before completing: {:?}", _0)]
|
||||
#[display("payload reached EOF before completing: {:?}", _0)]
|
||||
Incomplete(Option<io::Error>),
|
||||
|
||||
/// Content encoding stream corruption.
|
||||
#[display(fmt = "can not decode content-encoding")]
|
||||
#[display("can not decode content-encoding")]
|
||||
EncodingCorrupted,
|
||||
|
||||
/// Payload reached size limit.
|
||||
#[display(fmt = "payload reached size limit")]
|
||||
#[display("payload reached size limit")]
|
||||
Overflow,
|
||||
|
||||
/// Payload length is unknown.
|
||||
#[display(fmt = "payload length is unknown")]
|
||||
#[display("payload length is unknown")]
|
||||
UnknownLength,
|
||||
|
||||
/// HTTP/2 payload error.
|
||||
#[cfg(feature = "http2")]
|
||||
#[display(fmt = "{}", _0)]
|
||||
#[display("{}", _0)]
|
||||
Http2Payload(::h2::Error),
|
||||
|
||||
/// Generic I/O error.
|
||||
#[display(fmt = "{}", _0)]
|
||||
#[display("{}", _0)]
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
|
@ -326,44 +326,44 @@ impl From<PayloadError> for Error {
|
|||
#[non_exhaustive]
|
||||
pub enum DispatchError {
|
||||
/// Service error.
|
||||
#[display(fmt = "service error")]
|
||||
#[display("service error")]
|
||||
Service(Response<BoxBody>),
|
||||
|
||||
/// Body streaming error.
|
||||
#[display(fmt = "body error: {}", _0)]
|
||||
#[display("body error: {}", _0)]
|
||||
Body(Box<dyn StdError>),
|
||||
|
||||
/// Upgrade service error.
|
||||
#[display(fmt = "upgrade error")]
|
||||
#[display("upgrade error")]
|
||||
Upgrade,
|
||||
|
||||
/// An `io::Error` that occurred while trying to read or write to a network stream.
|
||||
#[display(fmt = "I/O error: {}", _0)]
|
||||
#[display("I/O error: {}", _0)]
|
||||
Io(io::Error),
|
||||
|
||||
/// Request parse error.
|
||||
#[display(fmt = "request parse error: {}", _0)]
|
||||
#[display("request parse error: {}", _0)]
|
||||
Parse(ParseError),
|
||||
|
||||
/// HTTP/2 error.
|
||||
#[display(fmt = "{}", _0)]
|
||||
#[display("{}", _0)]
|
||||
#[cfg(feature = "http2")]
|
||||
H2(h2::Error),
|
||||
|
||||
/// The first request did not complete within the specified timeout.
|
||||
#[display(fmt = "request did not complete within the specified timeout")]
|
||||
#[display("request did not complete within the specified timeout")]
|
||||
SlowRequestTimeout,
|
||||
|
||||
/// Disconnect timeout. Makes sense for TLS streams.
|
||||
#[display(fmt = "connection shutdown timeout")]
|
||||
#[display("connection shutdown timeout")]
|
||||
DisconnectTimeout,
|
||||
|
||||
/// Handler dropped payload before reading EOF.
|
||||
#[display(fmt = "handler dropped payload before reading EOF")]
|
||||
#[display("handler dropped payload before reading EOF")]
|
||||
HandlerDroppedPayload,
|
||||
|
||||
/// Internal error.
|
||||
#[display(fmt = "internal error")]
|
||||
#[display("internal error")]
|
||||
InternalError,
|
||||
}
|
||||
|
||||
|
@ -389,11 +389,11 @@ impl StdError for DispatchError {
|
|||
#[non_exhaustive]
|
||||
pub enum ContentTypeError {
|
||||
/// Can not parse content type.
|
||||
#[display(fmt = "could not parse content type")]
|
||||
#[display("could not parse content type")]
|
||||
ParseError,
|
||||
|
||||
/// Unknown content encoding.
|
||||
#[display(fmt = "unknown content encoding")]
|
||||
#[display("unknown content encoding")]
|
||||
UnknownEncoding,
|
||||
}
|
||||
|
||||
|
@ -415,7 +415,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_as_response() {
|
||||
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
||||
let orig = io::Error::other("other");
|
||||
let err: Error = ParseError::Io(orig).into();
|
||||
assert_eq!(
|
||||
format!("{}", err),
|
||||
|
@ -425,14 +425,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_error_display() {
|
||||
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
||||
let orig = io::Error::other("other");
|
||||
let err = Error::new_io().with_cause(orig);
|
||||
assert_eq!("connection error: other", err.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_http_response() {
|
||||
let orig = io::Error::new(io::ErrorKind::Other, "other");
|
||||
let orig = io::Error::other("other");
|
||||
let err = Error::new_io().with_cause(orig);
|
||||
let resp: Response<BoxBody> = err.into();
|
||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
|
@ -440,7 +440,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_payload_error() {
|
||||
let err: PayloadError = io::Error::new(io::ErrorKind::Other, "ParseError").into();
|
||||
let err: PayloadError = io::Error::other("ParseError").into();
|
||||
assert!(err.to_string().contains("ParseError"));
|
||||
|
||||
let err = PayloadError::Incomplete(None);
|
||||
|
@ -475,7 +475,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_from() {
|
||||
from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => ParseError::Io(..));
|
||||
from_and_cause!(io::Error::other("other") => ParseError::Io(..));
|
||||
from!(httparse::Error::HeaderName => ParseError::Header);
|
||||
from!(httparse::Error::HeaderName => ParseError::Header);
|
||||
from!(httparse::Error::HeaderValue => ParseError::Header);
|
||||
|
|
|
@ -31,7 +31,7 @@ impl Hasher for NoOpHasher {
|
|||
/// All entries into this map must be owned types (or static references).
|
||||
#[derive(Default)]
|
||||
pub struct Extensions {
|
||||
/// Use AHasher with a std HashMap with for faster lookups on the small `TypeId` keys.
|
||||
// use no-op hasher with a std HashMap with for faster lookups on the small `TypeId` keys
|
||||
map: HashMap<TypeId, Box<dyn Any>, BuildHasherDefault<NoOpHasher>>,
|
||||
}
|
||||
|
||||
|
@ -104,6 +104,46 @@ impl Extensions {
|
|||
.and_then(|boxed| boxed.downcast_mut())
|
||||
}
|
||||
|
||||
/// Inserts the given `value` into the extensions if it is not present, then returns a reference
|
||||
/// to the value in the extensions.
|
||||
///
|
||||
/// ```
|
||||
/// # use actix_http::Extensions;
|
||||
/// let mut map = Extensions::new();
|
||||
/// assert_eq!(map.get::<Vec<u32>>(), None);
|
||||
///
|
||||
/// map.get_or_insert(Vec::<u32>::new()).push(1);
|
||||
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1]));
|
||||
///
|
||||
/// map.get_or_insert(Vec::<u32>::new()).push(2);
|
||||
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
|
||||
/// ```
|
||||
pub fn get_or_insert<T: 'static>(&mut self, value: T) -> &mut T {
|
||||
self.get_or_insert_with(|| value)
|
||||
}
|
||||
|
||||
/// Inserts a value computed from `f` into the extensions if the given `value` is not present,
|
||||
/// then returns a reference to the value in the extensions.
|
||||
///
|
||||
/// ```
|
||||
/// # use actix_http::Extensions;
|
||||
/// let mut map = Extensions::new();
|
||||
/// assert_eq!(map.get::<Vec<u32>>(), None);
|
||||
///
|
||||
/// map.get_or_insert_with(Vec::<u32>::new).push(1);
|
||||
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1]));
|
||||
///
|
||||
/// map.get_or_insert_with(Vec::<u32>::new).push(2);
|
||||
/// assert_eq!(map.get::<Vec<u32>>(), Some(&vec![1,2]));
|
||||
/// ```
|
||||
pub fn get_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, default: F) -> &mut T {
|
||||
self.map
|
||||
.entry(TypeId::of::<T>())
|
||||
.or_insert_with(|| Box::new(default()))
|
||||
.downcast_mut()
|
||||
.expect("extensions map should now contain a T value")
|
||||
}
|
||||
|
||||
/// Remove an item from the map of a given type.
|
||||
///
|
||||
/// If an item of this type was already stored, it will be returned.
|
||||
|
|
|
@ -310,10 +310,10 @@ impl MessageType for RequestHeadType {
|
|||
Version::HTTP_11 => "HTTP/1.1",
|
||||
Version::HTTP_2 => "HTTP/2.0",
|
||||
Version::HTTP_3 => "HTTP/3.0",
|
||||
_ => return Err(io::Error::new(io::ErrorKind::Other, "unsupported version")),
|
||||
_ => return Err(io::Error::other("Unsupported version")),
|
||||
}
|
||||
)
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
|
||||
.map_err(io::Error::other)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -433,7 +433,7 @@ impl TransferEncoding {
|
|||
buf.extend_from_slice(b"0\r\n\r\n");
|
||||
} else {
|
||||
writeln!(helpers::MutWriter(buf), "{:X}\r", msg.len())
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
buf.reserve(msg.len() + 2);
|
||||
buf.extend_from_slice(msg);
|
||||
|
|
|
@ -480,15 +480,15 @@ where
|
|||
let cfg = self.cfg.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let expect = expect
|
||||
.await
|
||||
.map_err(|e| error!("Init http expect service error: {:?}", e))?;
|
||||
let expect = expect.await.map_err(|err| {
|
||||
tracing::error!("Initialization of HTTP expect service error: {err:?}");
|
||||
})?;
|
||||
|
||||
let upgrade = match upgrade {
|
||||
Some(upgrade) => {
|
||||
let upgrade = upgrade
|
||||
.await
|
||||
.map_err(|e| error!("Init http upgrade service error: {:?}", e))?;
|
||||
let upgrade = upgrade.await.map_err(|err| {
|
||||
tracing::error!("Initialization of HTTP upgrade service error: {err:?}");
|
||||
})?;
|
||||
Some(upgrade)
|
||||
}
|
||||
None => None,
|
||||
|
@ -496,7 +496,7 @@ where
|
|||
|
||||
let service = service
|
||||
.await
|
||||
.map_err(|e| error!("Init http service error: {:?}", e))?;
|
||||
.map_err(|err| error!("Initialization of HTTP service error: {err:?}"))?;
|
||||
|
||||
Ok(H1ServiceHandler::new(
|
||||
cfg,
|
||||
|
|
|
@ -18,6 +18,14 @@ pub const CACHE_STATUS: HeaderName = HeaderName::from_static("cache-status");
|
|||
// TODO(breaking): replace with http's version
|
||||
pub const CDN_CACHE_CONTROL: HeaderName = HeaderName::from_static("cdn-cache-control");
|
||||
|
||||
/// Response header field that sends a signal to the user agent that it ought to remove all data of
|
||||
/// a certain set of types.
|
||||
///
|
||||
/// See the [W3C Clear-Site-Data spec] for full semantics.
|
||||
///
|
||||
/// [W3C Clear-Site-Data spec]: https://www.w3.org/TR/clear-site-data/#header
|
||||
pub const CLEAR_SITE_DATA: HeaderName = HeaderName::from_static("clear-site-data");
|
||||
|
||||
/// Response header that prevents a document from loading any cross-origin resources that don't
|
||||
/// explicitly grant the document permission (using [CORP] or [CORS]).
|
||||
///
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::{borrow::Cow, collections::hash_map, iter, ops};
|
||||
|
||||
use ahash::AHashMap;
|
||||
use foldhash::{HashMap as FoldHashMap, HashMapExt as _};
|
||||
use http::header::{HeaderName, HeaderValue};
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
||||
|
@ -47,7 +47,7 @@ use super::AsHeaderName;
|
|||
/// ```
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct HeaderMap {
|
||||
pub(crate) inner: AHashMap<HeaderName, Value>,
|
||||
pub(crate) inner: FoldHashMap<HeaderName, Value>,
|
||||
}
|
||||
|
||||
/// A bespoke non-empty list for HeaderMap values.
|
||||
|
@ -116,7 +116,7 @@ impl HeaderMap {
|
|||
/// ```
|
||||
pub fn with_capacity(capacity: usize) -> Self {
|
||||
HeaderMap {
|
||||
inner: AHashMap::with_capacity(capacity),
|
||||
inner: FoldHashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -830,7 +830,7 @@ impl<'a> Drain<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Drain<'a> {
|
||||
impl Iterator for Drain<'_> {
|
||||
type Item = (Option<HeaderName>, HeaderValue);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
|
|
@ -42,9 +42,9 @@ pub use self::{
|
|||
as_name::AsHeaderName,
|
||||
// re-export list is explicit so that any updates to `http` do not conflict with this set
|
||||
common::{
|
||||
CACHE_STATUS, CDN_CACHE_CONTROL, CROSS_ORIGIN_EMBEDDER_POLICY, CROSS_ORIGIN_OPENER_POLICY,
|
||||
CROSS_ORIGIN_RESOURCE_POLICY, PERMISSIONS_POLICY, X_FORWARDED_FOR, X_FORWARDED_HOST,
|
||||
X_FORWARDED_PROTO,
|
||||
CACHE_STATUS, CDN_CACHE_CONTROL, CLEAR_SITE_DATA, CROSS_ORIGIN_EMBEDDER_POLICY,
|
||||
CROSS_ORIGIN_OPENER_POLICY, CROSS_ORIGIN_RESOURCE_POLICY, PERMISSIONS_POLICY,
|
||||
X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO,
|
||||
},
|
||||
into_pair::TryIntoHeaderPair,
|
||||
into_value::TryIntoHeaderValue,
|
||||
|
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
|
||||
/// Error returned when a content encoding is unknown.
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "unsupported content encoding")]
|
||||
#[display("unsupported content encoding")]
|
||||
pub struct ContentEncodingParseError;
|
||||
|
||||
/// Represents a supported content encoding.
|
||||
|
|
|
@ -125,7 +125,7 @@ pub fn itoa_fmt<W: fmt::Write, V: itoa::Integer>(mut wr: W, value: V) -> fmt::Re
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Display, Error)]
|
||||
#[display(fmt = "quality out of bounds")]
|
||||
#[display("quality out of bounds")]
|
||||
#[non_exhaustive]
|
||||
pub struct QualityOutOfBounds;
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ pub fn write_content_length<B: BufMut>(n: u64, buf: &mut B, camel_case: bool) {
|
|||
/// perform a remaining length check before writing.
|
||||
pub(crate) struct MutWriter<'a, B>(pub(crate) &'a mut B);
|
||||
|
||||
impl<'a, B> io::Write for MutWriter<'a, B>
|
||||
impl<B> io::Write for MutWriter<'_, B>
|
||||
where
|
||||
B: BufMut,
|
||||
{
|
||||
|
|
|
@ -103,7 +103,7 @@ pub trait HttpMessage: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T> HttpMessage for &'a mut T
|
||||
impl<T> HttpMessage for &mut T
|
||||
where
|
||||
T: HttpMessage,
|
||||
{
|
||||
|
|
|
@ -41,13 +41,31 @@ pin_project! {
|
|||
}
|
||||
|
||||
impl<S> From<crate::h1::Payload> for Payload<S> {
|
||||
#[inline]
|
||||
fn from(payload: crate::h1::Payload) -> Self {
|
||||
Payload::H1 { payload }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> From<Bytes> for Payload<S> {
|
||||
#[inline]
|
||||
fn from(bytes: Bytes) -> Self {
|
||||
let (_, mut pl) = crate::h1::Payload::create(true);
|
||||
pl.unread_data(bytes);
|
||||
self::Payload::from(pl)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> From<Vec<u8>> for Payload<S> {
|
||||
#[inline]
|
||||
fn from(vec: Vec<u8>) -> Self {
|
||||
Payload::from(Bytes::from(vec))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "http2")]
|
||||
impl<S> From<crate::h2::Payload> for Payload<S> {
|
||||
#[inline]
|
||||
fn from(payload: crate::h2::Payload) -> Self {
|
||||
Payload::H2 { payload }
|
||||
}
|
||||
|
@ -55,6 +73,7 @@ impl<S> From<crate::h2::Payload> for Payload<S> {
|
|||
|
||||
#[cfg(feature = "http2")]
|
||||
impl<S> From<::h2::RecvStream> for Payload<S> {
|
||||
#[inline]
|
||||
fn from(stream: ::h2::RecvStream) -> Self {
|
||||
Payload::H2 {
|
||||
payload: crate::h2::Payload::new(stream),
|
||||
|
@ -63,13 +82,15 @@ impl<S> From<::h2::RecvStream> for Payload<S> {
|
|||
}
|
||||
|
||||
impl From<BoxedPayloadStream> for Payload {
|
||||
#[inline]
|
||||
fn from(payload: BoxedPayloadStream) -> Self {
|
||||
Payload::Stream { payload }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Payload<S> {
|
||||
/// Takes current payload and replaces it with `None` value
|
||||
/// Takes current payload and replaces it with `None` value.
|
||||
#[must_use]
|
||||
pub fn take(&mut self) -> Payload<S> {
|
||||
mem::replace(self, Payload::None)
|
||||
}
|
||||
|
|
|
@ -775,23 +775,23 @@ where
|
|||
let cfg = self.cfg.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let expect = expect
|
||||
.await
|
||||
.map_err(|e| error!("Init http expect service error: {:?}", e))?;
|
||||
let expect = expect.await.map_err(|err| {
|
||||
tracing::error!("Initialization of HTTP expect service error: {err:?}");
|
||||
})?;
|
||||
|
||||
let upgrade = match upgrade {
|
||||
Some(upgrade) => {
|
||||
let upgrade = upgrade
|
||||
.await
|
||||
.map_err(|e| error!("Init http upgrade service error: {:?}", e))?;
|
||||
let upgrade = upgrade.await.map_err(|err| {
|
||||
tracing::error!("Initialization of HTTP upgrade service error: {err:?}");
|
||||
})?;
|
||||
Some(upgrade)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
let service = service
|
||||
.await
|
||||
.map_err(|e| error!("Init http service error: {:?}", e))?;
|
||||
let service = service.await.map_err(|err| {
|
||||
tracing::error!("Initialization of HTTP service error: {err:?}");
|
||||
})?;
|
||||
|
||||
Ok(HttpServiceHandler::new(
|
||||
cfg,
|
||||
|
|
|
@ -114,14 +114,14 @@ mod inner {
|
|||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
DispatcherError::Service(ref e) => {
|
||||
write!(fmt, "DispatcherError::Service({:?})", e)
|
||||
DispatcherError::Service(ref err) => {
|
||||
write!(fmt, "DispatcherError::Service({err:?})")
|
||||
}
|
||||
DispatcherError::Encoder(ref e) => {
|
||||
write!(fmt, "DispatcherError::Encoder({:?})", e)
|
||||
DispatcherError::Encoder(ref err) => {
|
||||
write!(fmt, "DispatcherError::Encoder({err:?})")
|
||||
}
|
||||
DispatcherError::Decoder(ref e) => {
|
||||
write!(fmt, "DispatcherError::Decoder({:?})", e)
|
||||
DispatcherError::Decoder(ref err) => {
|
||||
write!(fmt, "DispatcherError::Decoder({err:?})")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -136,9 +136,9 @@ mod inner {
|
|||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
DispatcherError::Service(ref e) => write!(fmt, "{}", e),
|
||||
DispatcherError::Encoder(ref e) => write!(fmt, "{:?}", e),
|
||||
DispatcherError::Decoder(ref e) => write!(fmt, "{:?}", e),
|
||||
DispatcherError::Service(ref err) => write!(fmt, "{err}"),
|
||||
DispatcherError::Encoder(ref err) => write!(fmt, "{err:?}"),
|
||||
DispatcherError::Decoder(ref err) => write!(fmt, "{err:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,43 +27,43 @@ pub use self::{
|
|||
#[derive(Debug, Display, Error, From)]
|
||||
pub enum ProtocolError {
|
||||
/// Received an unmasked frame from client.
|
||||
#[display(fmt = "received an unmasked frame from client")]
|
||||
#[display("received an unmasked frame from client")]
|
||||
UnmaskedFrame,
|
||||
|
||||
/// Received a masked frame from server.
|
||||
#[display(fmt = "received a masked frame from server")]
|
||||
#[display("received a masked frame from server")]
|
||||
MaskedFrame,
|
||||
|
||||
/// Encountered invalid opcode.
|
||||
#[display(fmt = "invalid opcode ({})", _0)]
|
||||
#[display("invalid opcode ({})", _0)]
|
||||
InvalidOpcode(#[error(not(source))] u8),
|
||||
|
||||
/// Invalid control frame length
|
||||
#[display(fmt = "invalid control frame length ({})", _0)]
|
||||
#[display("invalid control frame length ({})", _0)]
|
||||
InvalidLength(#[error(not(source))] usize),
|
||||
|
||||
/// Bad opcode.
|
||||
#[display(fmt = "bad opcode")]
|
||||
#[display("bad opcode")]
|
||||
BadOpCode,
|
||||
|
||||
/// A payload reached size limit.
|
||||
#[display(fmt = "payload reached size limit")]
|
||||
#[display("payload reached size limit")]
|
||||
Overflow,
|
||||
|
||||
/// Continuation has not started.
|
||||
#[display(fmt = "continuation has not started")]
|
||||
#[display("continuation has not started")]
|
||||
ContinuationNotStarted,
|
||||
|
||||
/// Received new continuation but it is already started.
|
||||
#[display(fmt = "received new continuation but it has already started")]
|
||||
#[display("received new continuation but it has already started")]
|
||||
ContinuationStarted,
|
||||
|
||||
/// Unknown continuation fragment.
|
||||
#[display(fmt = "unknown continuation fragment: {}", _0)]
|
||||
#[display("unknown continuation fragment: {}", _0)]
|
||||
ContinuationFragment(#[error(not(source))] OpCode),
|
||||
|
||||
/// I/O error.
|
||||
#[display(fmt = "I/O error: {}", _0)]
|
||||
#[display("I/O error: {}", _0)]
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
|
@ -71,27 +71,27 @@ pub enum ProtocolError {
|
|||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, Error)]
|
||||
pub enum HandshakeError {
|
||||
/// Only get method is allowed.
|
||||
#[display(fmt = "method not allowed")]
|
||||
#[display("method not allowed")]
|
||||
GetMethodRequired,
|
||||
|
||||
/// Upgrade header if not set to WebSocket.
|
||||
#[display(fmt = "WebSocket upgrade is expected")]
|
||||
#[display("WebSocket upgrade is expected")]
|
||||
NoWebsocketUpgrade,
|
||||
|
||||
/// Connection header is not set to upgrade.
|
||||
#[display(fmt = "connection upgrade is expected")]
|
||||
#[display("connection upgrade is expected")]
|
||||
NoConnectionUpgrade,
|
||||
|
||||
/// WebSocket version header is not set.
|
||||
#[display(fmt = "WebSocket version header is required")]
|
||||
#[display("WebSocket version header is required")]
|
||||
NoVersionHeader,
|
||||
|
||||
/// Unsupported WebSocket version.
|
||||
#[display(fmt = "unsupported WebSocket version")]
|
||||
#[display("unsupported WebSocket version")]
|
||||
UnsupportedVersion,
|
||||
|
||||
/// WebSocket key is not set or wrong.
|
||||
#[display(fmt = "unknown WebSocket key")]
|
||||
#[display("unknown WebSocket key")]
|
||||
BadWebsocketKey,
|
||||
}
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ async fn with_query_parameter() {
|
|||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "expect failed")]
|
||||
#[display("expect failed")]
|
||||
struct ExpectFailed;
|
||||
|
||||
impl From<ExpectFailed> for Response<BoxBody> {
|
||||
|
|
|
@ -398,7 +398,7 @@ async fn h2_response_http_error_handling() {
|
|||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "error")]
|
||||
#[display("error")]
|
||||
struct BadRequest;
|
||||
|
||||
impl From<BadRequest> for Response<BoxBody> {
|
||||
|
|
|
@ -480,7 +480,7 @@ async fn h2_response_http_error_handling() {
|
|||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "error")]
|
||||
#[display("error")]
|
||||
struct BadRequest;
|
||||
|
||||
impl From<BadRequest> for Response<BoxBody> {
|
||||
|
|
|
@ -16,6 +16,7 @@ use actix_utils::future::{err, ok, ready};
|
|||
use bytes::Bytes;
|
||||
use derive_more::{Display, Error};
|
||||
use futures_util::{stream::once, FutureExt as _, StreamExt as _};
|
||||
use rand::Rng as _;
|
||||
use regex::Regex;
|
||||
|
||||
#[actix_rt::test]
|
||||
|
@ -62,7 +63,7 @@ async fn h1_2() {
|
|||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "expect failed")]
|
||||
#[display("expect failed")]
|
||||
struct ExpectFailed;
|
||||
|
||||
impl From<ExpectFailed> for Response<BoxBody> {
|
||||
|
@ -164,7 +165,10 @@ async fn chunked_payload() {
|
|||
|
||||
for chunk_size in chunk_sizes.iter() {
|
||||
let mut bytes = Vec::new();
|
||||
let random_bytes: Vec<u8> = (0..*chunk_size).map(|_| rand::random::<u8>()).collect();
|
||||
let random_bytes = rand::rng()
|
||||
.sample_iter(rand::distr::StandardUniform)
|
||||
.take(*chunk_size)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
bytes.extend(format!("{:X}\r\n", chunk_size).as_bytes());
|
||||
bytes.extend(&random_bytes[..]);
|
||||
|
@ -723,7 +727,7 @@ async fn h1_response_http_error_handling() {
|
|||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "error")]
|
||||
#[display("error")]
|
||||
struct BadRequest;
|
||||
|
||||
impl From<BadRequest> for Response<BoxBody> {
|
||||
|
|
|
@ -37,16 +37,16 @@ impl WsService {
|
|||
|
||||
#[derive(Debug, Display, Error, From)]
|
||||
enum WsServiceError {
|
||||
#[display(fmt = "HTTP error")]
|
||||
#[display("HTTP error")]
|
||||
Http(actix_http::Error),
|
||||
|
||||
#[display(fmt = "WS handshake error")]
|
||||
#[display("WS handshake error")]
|
||||
Ws(actix_http::ws::HandshakeError),
|
||||
|
||||
#[display(fmt = "I/O error")]
|
||||
#[display("I/O error")]
|
||||
Io(std::io::Error),
|
||||
|
||||
#[display(fmt = "dispatcher error")]
|
||||
#[display("dispatcher error")]
|
||||
Dispatcher,
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,8 @@ all-features = true
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
bytesize = "2"
|
||||
darling = "0.20"
|
||||
parse-size = "1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = "2"
|
||||
|
@ -27,7 +27,7 @@ syn = "2"
|
|||
[dev-dependencies]
|
||||
actix-multipart = "0.7"
|
||||
actix-web = "4"
|
||||
rustversion = "1"
|
||||
rustversion-msrv = "0.100"
|
||||
trybuild = "1"
|
||||
|
||||
[lints]
|
||||
|
|
|
@ -5,11 +5,12 @@
|
|||
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
#![allow(clippy::disallowed_names)] // false positives in some macro expansions
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use bytesize::ByteSize;
|
||||
use darling::{FromDeriveInput, FromField, FromMeta};
|
||||
use parse_size::parse_size;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::quote;
|
||||
|
@ -35,6 +36,7 @@ struct MultipartFormAttrs {
|
|||
duplicate_field: DuplicateField,
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_names)] // false positive in macro expansion
|
||||
#[derive(FromField, Default)]
|
||||
#[darling(attributes(multipart), default)]
|
||||
struct FieldAttrs {
|
||||
|
@ -101,7 +103,7 @@ struct ParsedField<'t> {
|
|||
/// # Field Limits
|
||||
///
|
||||
/// You can use the `#[multipart(limit = "<size>")]` attribute to set field level limits. The limit
|
||||
/// string is parsed using [parse_size].
|
||||
/// string is parsed using [`bytesize`].
|
||||
///
|
||||
/// Note: the form is also subject to the global limits configured using `MultipartFormConfig`.
|
||||
///
|
||||
|
@ -148,7 +150,7 @@ struct ParsedField<'t> {
|
|||
/// struct Form { }
|
||||
/// ```
|
||||
///
|
||||
/// [parse_size]: https://docs.rs/parse-size/1/parse_size
|
||||
/// [`bytesize`]: https://docs.rs/bytesize/2
|
||||
#[proc_macro_derive(MultipartForm, attributes(multipart))]
|
||||
pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input: syn::DeriveInput = parse_macro_input!(input);
|
||||
|
@ -189,8 +191,8 @@ pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenS
|
|||
let attrs = FieldAttrs::from_field(field).map_err(|err| err.write_errors())?;
|
||||
let serialization_name = attrs.rename.unwrap_or_else(|| rust_name.to_string());
|
||||
|
||||
let limit = match attrs.limit.map(|limit| match parse_size(&limit) {
|
||||
Ok(size) => Ok(usize::try_from(size).unwrap()),
|
||||
let limit = match attrs.limit.map(|limit| match limit.parse::<ByteSize>() {
|
||||
Ok(ByteSize(size)) => Ok(usize::try_from(size).unwrap()),
|
||||
Err(err) => Err(syn::Error::new(
|
||||
field.ident.as_ref().unwrap().span(),
|
||||
format!("Could not parse size limit `{}`: {}", limit, err),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[rustversion::stable(1.72)] // MSRV
|
||||
#[rustversion_msrv::msrv]
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
error: Could not parse size limit `2 bytes`: invalid digit found in string
|
||||
error: Could not parse size limit `2 bytes`: couldn't parse "bytes" into a known SI unit, couldn't parse unit of "bytes"
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:6:5
|
||||
|
|
||||
6 | description: Text<String>,
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: Could not parse size limit `2 megabytes`: invalid digit found in string
|
||||
error: Could not parse size limit `2 megabytes`: couldn't parse "megabytes" into a known SI unit, couldn't parse unit of "megabytes"
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:12:5
|
||||
|
|
||||
12 | description: Text<String>,
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: Could not parse size limit `four meters`: invalid digit found in string
|
||||
error: Could not parse size limit `four meters`: couldn't parse "four meters" into a ByteSize, cannot parse float from empty string
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:18:5
|
||||
|
|
||||
18 | description: Text<String>,
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
- Minimum supported Rust version (MSRV) is now 1.75.
|
||||
|
||||
## 0.7.2
|
||||
|
||||
- Fix re-exported version of `actix-multipart-derive`.
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
name = "actix-multipart"
|
||||
version = "0.7.2"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Jacob Halsey <jacob@jhalsey.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Jacob Halsey <jacob@jhalsey.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
description = "Multipart request & form support for Actix Web"
|
||||
keywords = ["http", "actix", "web", "multipart", "form"]
|
||||
|
@ -19,17 +19,17 @@ all-features = true
|
|||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_http::*",
|
||||
"actix_multipart_derive::*",
|
||||
"actix_utils::*",
|
||||
"actix_web::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"mime::*",
|
||||
"serde_json::*",
|
||||
"serde_plain::*",
|
||||
"serde::*",
|
||||
"tempfile::*",
|
||||
"actix_http::*",
|
||||
"actix_multipart_derive::*",
|
||||
"actix_utils::*",
|
||||
"actix_web::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"mime::*",
|
||||
"serde_json::*",
|
||||
"serde_plain::*",
|
||||
"serde::*",
|
||||
"tempfile::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
|
@ -42,7 +42,7 @@ actix-multipart-derive = { version = "=0.7.0", optional = true }
|
|||
actix-utils = "3"
|
||||
actix-web = { version = "4", default-features = false }
|
||||
|
||||
derive_more = "0.99.5"
|
||||
derive_more = { version = "2", features = ["display", "error", "from"] }
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
httparse = "1.3"
|
||||
|
@ -50,26 +50,26 @@ local-waker = "0.1"
|
|||
log = "0.4"
|
||||
memchr = "2.5"
|
||||
mime = "0.3"
|
||||
rand = "0.8"
|
||||
rand = "0.9"
|
||||
serde = "1"
|
||||
serde_json = "1"
|
||||
serde_plain = "1"
|
||||
tempfile = { version = "3.4", optional = true }
|
||||
tokio = { version = "1.24.2", features = ["sync", "io-util"] }
|
||||
tokio = { version = "1.38.2", features = ["sync", "io-util"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-http = "3"
|
||||
actix-multipart-rfc7578 = "0.10"
|
||||
actix-multipart-rfc7578 = "0.11"
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1"
|
||||
actix-web = "4"
|
||||
assert_matches = "1"
|
||||
awc = "3"
|
||||
env_logger = "0.11"
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
futures-test = "0.3"
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
multer = "3"
|
||||
tokio = { version = "1.24.2", features = ["sync"] }
|
||||
tokio = { version = "1.38.2", features = ["sync"] }
|
||||
tokio-stream = "0.1"
|
||||
|
||||
[lints]
|
||||
|
|
|
@ -12,11 +12,11 @@ use derive_more::{Display, Error, From};
|
|||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
/// Could not find Content-Type header.
|
||||
#[display(fmt = "Could not find Content-Type header")]
|
||||
#[display("Could not find Content-Type header")]
|
||||
ContentTypeMissing,
|
||||
|
||||
/// Could not parse Content-Type header.
|
||||
#[display(fmt = "Could not parse Content-Type header")]
|
||||
#[display("Could not parse Content-Type header")]
|
||||
ContentTypeParse,
|
||||
|
||||
/// Parsed Content-Type did not have "multipart" top-level media type.
|
||||
|
@ -25,11 +25,11 @@ pub enum Error {
|
|||
/// "multipart/form-data" media type.
|
||||
///
|
||||
/// [`MultipartForm`]: struct@crate::form::MultipartForm
|
||||
#[display(fmt = "Parsed Content-Type did not have "multipart" top-level media type")]
|
||||
#[display("Parsed Content-Type did not have 'multipart' top-level media type")]
|
||||
ContentTypeIncompatible,
|
||||
|
||||
/// Multipart boundary is not found.
|
||||
#[display(fmt = "Multipart boundary is not found")]
|
||||
#[display("Multipart boundary is not found")]
|
||||
BoundaryMissing,
|
||||
|
||||
/// Content-Disposition header was not found or not of disposition type "form-data" when parsing
|
||||
|
@ -39,7 +39,7 @@ pub enum Error {
|
|||
/// always be present and have a disposition type of "form-data".
|
||||
///
|
||||
/// [RFC 7578 §4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
|
||||
#[display(fmt = "Content-Disposition header was not found when parsing a \"form-data\" field")]
|
||||
#[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
|
||||
ContentDispositionMissing,
|
||||
|
||||
/// Content-Disposition name parameter was not found when parsing a "form-data" field.
|
||||
|
@ -48,48 +48,48 @@ pub enum Error {
|
|||
/// always include a "name" parameter.
|
||||
///
|
||||
/// [RFC 7578 §4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
|
||||
#[display(fmt = "Content-Disposition header was not found when parsing a \"form-data\" field")]
|
||||
#[display("Content-Disposition header was not found when parsing a \"form-data\" field")]
|
||||
ContentDispositionNameMissing,
|
||||
|
||||
/// Nested multipart is not supported.
|
||||
#[display(fmt = "Nested multipart is not supported")]
|
||||
#[display("Nested multipart is not supported")]
|
||||
Nested,
|
||||
|
||||
/// Multipart stream is incomplete.
|
||||
#[display(fmt = "Multipart stream is incomplete")]
|
||||
#[display("Multipart stream is incomplete")]
|
||||
Incomplete,
|
||||
|
||||
/// Field parsing failed.
|
||||
#[display(fmt = "Error during field parsing")]
|
||||
#[display("Error during field parsing")]
|
||||
Parse(ParseError),
|
||||
|
||||
/// HTTP payload error.
|
||||
#[display(fmt = "Payload error")]
|
||||
#[display("Payload error")]
|
||||
Payload(PayloadError),
|
||||
|
||||
/// Stream is not consumed.
|
||||
#[display(fmt = "Stream is not consumed")]
|
||||
#[display("Stream is not consumed")]
|
||||
NotConsumed,
|
||||
|
||||
/// Form field handler raised error.
|
||||
#[display(fmt = "An error occurred processing field: {name}")]
|
||||
#[display("An error occurred processing field: {name}")]
|
||||
Field {
|
||||
name: String,
|
||||
source: actix_web::Error,
|
||||
},
|
||||
|
||||
/// Duplicate field found (for structure that opted-in to denying duplicate fields).
|
||||
#[display(fmt = "Duplicate field found: {_0}")]
|
||||
#[display("Duplicate field found: {_0}")]
|
||||
#[from(ignore)]
|
||||
DuplicateField(#[error(not(source))] String),
|
||||
|
||||
/// Required field is missing.
|
||||
#[display(fmt = "Required field is missing: {_0}")]
|
||||
#[display("Required field is missing: {_0}")]
|
||||
#[from(ignore)]
|
||||
MissingField(#[error(not(source))] String),
|
||||
|
||||
/// Unknown field (for structure that opted-in to denying unknown fields).
|
||||
#[display(fmt = "Unknown field: {_0}")]
|
||||
#[display("Unknown field: {_0}")]
|
||||
#[from(ignore)]
|
||||
UnknownField(#[error(not(source))] String),
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ use crate::{
|
|||
|
||||
/// Error type returned from [`Field::bytes()`] when field data is larger than limit.
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "size limit exceeded while collecting field data")]
|
||||
#[display("size limit exceeded while collecting field data")]
|
||||
#[non_exhaustive]
|
||||
pub struct LimitExceeded;
|
||||
|
||||
|
|
|
@ -66,11 +66,11 @@ where
|
|||
#[non_exhaustive]
|
||||
pub enum JsonFieldError {
|
||||
/// Deserialize error.
|
||||
#[display(fmt = "Json deserialize error: {}", _0)]
|
||||
#[display("Json deserialize error: {}", _0)]
|
||||
Deserialize(serde_json::Error),
|
||||
|
||||
/// Content type error.
|
||||
#[display(fmt = "Content type error")]
|
||||
#[display("Content type error")]
|
||||
ContentType,
|
||||
}
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ impl<'t> FieldReader<'t> for TempFile {
|
|||
#[non_exhaustive]
|
||||
pub enum TempFileError {
|
||||
/// File I/O Error
|
||||
#[display(fmt = "File I/O error: {}", _0)]
|
||||
#[display("File I/O error: {}", _0)]
|
||||
FileIo(std::io::Error),
|
||||
}
|
||||
|
||||
|
|
|
@ -77,15 +77,15 @@ where
|
|||
#[non_exhaustive]
|
||||
pub enum TextError {
|
||||
/// UTF-8 decoding error.
|
||||
#[display(fmt = "UTF-8 decoding error: {}", _0)]
|
||||
#[display("UTF-8 decoding error: {}", _0)]
|
||||
Utf8Error(str::Utf8Error),
|
||||
|
||||
/// Deserialize error.
|
||||
#[display(fmt = "Plain text deserialize error: {}", _0)]
|
||||
#[display("Plain text deserialize error: {}", _0)]
|
||||
Deserialize(serde_plain::Error),
|
||||
|
||||
/// Content type error.
|
||||
#[display(fmt = "Content type error")]
|
||||
#[display("Content type error")]
|
||||
ContentType,
|
||||
}
|
||||
|
||||
|
|
|
@ -5,10 +5,7 @@ use actix_web::{
|
|||
web::{BufMut as _, Bytes, BytesMut},
|
||||
};
|
||||
use mime::Mime;
|
||||
use rand::{
|
||||
distributions::{Alphanumeric, DistString as _},
|
||||
thread_rng,
|
||||
};
|
||||
use rand::distr::{Alphanumeric, SampleString as _};
|
||||
|
||||
const CRLF: &[u8] = b"\r\n";
|
||||
const CRLF_CRLF: &[u8] = b"\r\n\r\n";
|
||||
|
@ -64,7 +61,7 @@ pub fn create_form_data_payload_and_headers(
|
|||
content_type: Option<Mime>,
|
||||
file: Bytes,
|
||||
) -> (Bytes, HeaderMap) {
|
||||
let boundary = Alphanumeric.sample_string(&mut thread_rng(), 32);
|
||||
let boundary = Alphanumeric.sample_string(&mut rand::rng(), 32);
|
||||
|
||||
create_form_data_payload_and_headers_with_boundary(
|
||||
&boundary,
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
name = "actix-router"
|
||||
version = "0.5.3"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
description = "Resource path matching and router"
|
||||
keywords = ["actix", "router", "routing"]
|
||||
|
@ -13,10 +13,7 @@ license = "MIT OR Apache-2.0"
|
|||
edition = "2021"
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"http::*",
|
||||
"serde::*",
|
||||
]
|
||||
allowed_external_types = ["http::*", "serde::*"]
|
||||
|
||||
[features]
|
||||
default = ["http", "unicode"]
|
||||
|
@ -35,8 +32,8 @@ tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
|||
[dev-dependencies]
|
||||
criterion = { version = "0.5", features = ["html_reports"] }
|
||||
http = "0.2.7"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
percent-encoding = "2.1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -143,9 +143,9 @@ impl<T: ResourcePath> Path<T> {
|
|||
for (seg_name, val) in self.segments.iter() {
|
||||
if name == seg_name {
|
||||
return match val {
|
||||
PathItem::Static(ref s) => Some(s),
|
||||
PathItem::Segment(s, e) => {
|
||||
Some(&self.path.path()[(*s as usize)..(*e as usize)])
|
||||
PathItem::Static(ref seg) => Some(seg),
|
||||
PathItem::Segment(start, end) => {
|
||||
Some(&self.path.path()[(*start as usize)..(*end as usize)])
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -193,8 +193,10 @@ impl<'a, T: ResourcePath> Iterator for PathIter<'a, T> {
|
|||
if self.idx < self.params.segment_count() {
|
||||
let idx = self.idx;
|
||||
let res = match self.params.segments[idx].1 {
|
||||
PathItem::Static(ref s) => s,
|
||||
PathItem::Segment(s, e) => &self.params.path.path()[(s as usize)..(e as usize)],
|
||||
PathItem::Static(ref seg) => seg,
|
||||
PathItem::Segment(start, end) => {
|
||||
&self.params.path.path()[(start as usize)..(end as usize)]
|
||||
}
|
||||
};
|
||||
self.idx += 1;
|
||||
return Some((&self.params.segments[idx].0, res));
|
||||
|
@ -217,8 +219,8 @@ impl<T: ResourcePath> Index<usize> for Path<T> {
|
|||
|
||||
fn index(&self, idx: usize) -> &str {
|
||||
match self.segments[idx].1 {
|
||||
PathItem::Static(ref s) => s,
|
||||
PathItem::Segment(s, e) => &self.path.path()[(s as usize)..(e as usize)],
|
||||
PathItem::Static(ref seg) => seg,
|
||||
PathItem::Segment(start, end) => &self.path.path()[(start as usize)..(end as usize)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,7 +105,7 @@ fn hex_pair_to_char(d1: u8, d2: u8) -> Option<u8> {
|
|||
let d_low = char::from(d2).to_digit(16)?;
|
||||
|
||||
// left shift high nibble by 4 bits
|
||||
Some((d_high as u8) << 4 | (d_low as u8))
|
||||
Some(((d_high as u8) << 4) | (d_low as u8))
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
|
|
|
@ -1021,6 +1021,7 @@ impl ResourceDef {
|
|||
panic!("prefix resource definitions should not have tail segments");
|
||||
}
|
||||
|
||||
#[allow(clippy::literal_string_with_formatting_args)]
|
||||
if unprocessed.ends_with('*') {
|
||||
// unnamed tail segment
|
||||
|
||||
|
@ -1369,6 +1370,7 @@ mod tests {
|
|||
assert_eq!(path.unprocessed(), "");
|
||||
}
|
||||
|
||||
#[allow(clippy::literal_string_with_formatting_args)]
|
||||
#[test]
|
||||
fn newline_patterns_and_paths() {
|
||||
let re = ResourceDef::new("/user/a\nb");
|
||||
|
|
|
@ -19,7 +19,7 @@ impl ResourcePath for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> ResourcePath for &'a str {
|
||||
impl ResourcePath for &str {
|
||||
fn path(&self) -> &str {
|
||||
self
|
||||
}
|
||||
|
|
|
@ -145,6 +145,7 @@ mod tests {
|
|||
};
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
#[allow(clippy::literal_string_with_formatting_args)]
|
||||
#[test]
|
||||
fn test_recognizer_1() {
|
||||
let mut router = Router::<usize>::build();
|
||||
|
|
|
@ -1,37 +1,34 @@
|
|||
[package]
|
||||
name = "actix-test"
|
||||
version = "0.1.5"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"]
|
||||
description = "Integration testing tools for Actix Web applications"
|
||||
keywords = ["http", "web", "framework", "async", "futures"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
categories = [
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_codec::*",
|
||||
"actix_http_test::*",
|
||||
"actix_http::*",
|
||||
"actix_service::*",
|
||||
"actix_web::*",
|
||||
"awc::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"tokio::*",
|
||||
"actix_codec::*",
|
||||
"actix_http_test::*",
|
||||
"actix_http::*",
|
||||
"actix_service::*",
|
||||
"actix_web::*",
|
||||
"awc::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"tokio::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
|
@ -72,7 +69,7 @@ tls-rustls-0_20 = { package = "rustls", version = "0.20", optional = true }
|
|||
tls-rustls-0_21 = { package = "rustls", version = "0.21", optional = true }
|
||||
tls-rustls-0_22 = { package = "rustls", version = "0.22", optional = true }
|
||||
tls-rustls-0_23 = { package = "rustls", version = "0.23", default-features = false, optional = true }
|
||||
tokio = { version = "1.24.2", features = ["sync"] }
|
||||
tokio = { version = "1.38.2", features = ["sync"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
## 4.3.1
|
||||
## 4.3.1 <!-- v4.3.1+deprecated -->
|
||||
|
||||
- Reduce memory usage by `take`-ing (rather than `split`-ing) the encoded buffer when yielding bytes in the response stream.
|
||||
- Mark crate as deprecated.
|
||||
|
|
|
@ -12,12 +12,12 @@ rust-version.workspace = true
|
|||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix::*",
|
||||
"actix_http::*",
|
||||
"actix_web::*",
|
||||
"bytes::*",
|
||||
"bytestring::*",
|
||||
"futures_core::*",
|
||||
"actix::*",
|
||||
"actix_http::*",
|
||||
"actix_web::*",
|
||||
"bytes::*",
|
||||
"bytestring::*",
|
||||
"futures_core::*",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
|
@ -30,14 +30,14 @@ bytes = "1"
|
|||
bytestring = "1"
|
||||
futures-core = { version = "0.3.17", default-features = false }
|
||||
pin-project-lite = "0.2"
|
||||
tokio = { version = "1.24.2", features = ["sync"] }
|
||||
tokio = { version = "1.38.2", features = ["sync"] }
|
||||
tokio-util = { version = "0.7", features = ["codec"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1"
|
||||
awc = { version = "3", default-features = false }
|
||||
actix-web = { version = "4", features = ["macros"] }
|
||||
awc = { version = "3", default-features = false }
|
||||
|
||||
env_logger = "0.11"
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
||||
|
|
|
@ -776,10 +776,7 @@ where
|
|||
}
|
||||
Poll::Pending => break,
|
||||
Poll::Ready(Some(Err(err))) => {
|
||||
return Poll::Ready(Some(Err(ProtocolError::Io(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("{err}"),
|
||||
)))));
|
||||
return Poll::Ready(Some(Err(ProtocolError::Io(io::Error::other(err)))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -795,14 +792,10 @@ where
|
|||
}
|
||||
Some(frm) => {
|
||||
let msg = match frm {
|
||||
Frame::Text(data) => {
|
||||
Message::Text(ByteString::try_from(data).map_err(|e| {
|
||||
ProtocolError::Io(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("{}", e),
|
||||
))
|
||||
})?)
|
||||
}
|
||||
Frame::Text(data) => Message::Text(
|
||||
ByteString::try_from(data)
|
||||
.map_err(|err| ProtocolError::Io(io::Error::other(err)))?,
|
||||
),
|
||||
Frame::Binary(data) => Message::Binary(data),
|
||||
Frame::Ping(s) => Message::Ping(s),
|
||||
Frame::Pong(s) => Message::Pong(s),
|
||||
|
|
|
@ -2,10 +2,7 @@
|
|||
name = "actix-web-codegen"
|
||||
version = "4.3.0"
|
||||
description = "Routing and runtime macros for Actix Web"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"]
|
||||
homepage.workspace = true
|
||||
repository.workspace = true
|
||||
license.workspace = true
|
||||
|
@ -33,8 +30,8 @@ actix-utils = "3"
|
|||
actix-web = "4"
|
||||
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
rustversion-msrv = "0.100"
|
||||
trybuild = "1"
|
||||
rustversion = "1"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -59,6 +59,7 @@ macro_rules! standard_method_type {
|
|||
(
|
||||
$($variant:ident, $upper:ident, $lower:ident,)+
|
||||
) => {
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum MethodType {
|
||||
$(
|
||||
|
@ -466,7 +467,7 @@ impl ToTokens for Route {
|
|||
|
||||
let stream = quote! {
|
||||
#(#doc_attributes)*
|
||||
#[allow(non_camel_case_types, missing_docs)]
|
||||
#[allow(non_camel_case_types)]
|
||||
#vis struct #name;
|
||||
|
||||
impl ::actix_web::dev::HttpServiceFactory for #name {
|
||||
|
|
|
@ -136,7 +136,7 @@ async fn routes_overlapping_inaccessible_test(req: HttpRequest) -> impl Responde
|
|||
}
|
||||
|
||||
#[get("/custom_resource_name", name = "custom")]
|
||||
async fn custom_resource_name_test<'a>(req: HttpRequest) -> impl Responder {
|
||||
async fn custom_resource_name_test(req: HttpRequest) -> impl Responder {
|
||||
assert!(req.url_for_static("custom").is_ok());
|
||||
assert!(req.url_for_static("custom_resource_name_test").is_err());
|
||||
HttpResponse::Ok()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[rustversion::stable(1.72)] // MSRV
|
||||
#[rustversion_msrv::msrv]
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
|
|
@ -2,6 +2,44 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
## 4.11.0
|
||||
|
||||
- Add `Logger::log_level()` method.
|
||||
- Improve handling of non-UTF-8 header values in `Logger` middleware.
|
||||
- Add `HttpServer::shutdown_signal()` method.
|
||||
- Mark `HttpServer` as `#[must_use]`.
|
||||
- Allow SVG images to be compressed by the `Compress` middleware.
|
||||
- Ignore `Host` header in `Host` guard when connection protocol is HTTP/2.
|
||||
- Re-export `mime` dependency.
|
||||
- Update `brotli` dependency to `8`.
|
||||
|
||||
## 4.10.2
|
||||
|
||||
- No significant changes since `4.10.1`.
|
||||
|
||||
## 4.10.1
|
||||
|
||||
- No significant changes since `4.10.0`.
|
||||
|
||||
## 4.10.0
|
||||
|
||||
### Added
|
||||
|
||||
- Implement `Responder` for `Result<(), E: Into<Error>>`. Returning `Ok(())` responds with HTTP 204 No Content.
|
||||
|
||||
### Changed
|
||||
|
||||
- On Windows, an error is now returned from `HttpServer::bind()` (or TLS variants) when binding to a socket that's already in use.
|
||||
- Update `brotli` dependency to `7`.
|
||||
- Minimum supported Rust version (MSRV) is now 1.75.
|
||||
|
||||
## 4.9.0
|
||||
|
||||
### Added
|
||||
|
||||
- Add `middleware::from_fn()` helper.
|
||||
- Add `web::ThinData` extractor.
|
||||
|
||||
## 4.8.0
|
||||
|
||||
### Added
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
[package]
|
||||
name = "actix-web"
|
||||
version = "4.8.0"
|
||||
version = "4.11.0"
|
||||
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
]
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"]
|
||||
keywords = ["actix", "http", "web", "framework", "async"]
|
||||
categories = [
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket"
|
||||
"network-programming",
|
||||
"asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket",
|
||||
]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
|
@ -22,55 +19,55 @@ rust-version.workspace = true
|
|||
[package.metadata.docs.rs]
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
features = [
|
||||
"macros",
|
||||
"openssl",
|
||||
"rustls-0_20",
|
||||
"rustls-0_21",
|
||||
"rustls-0_22",
|
||||
"rustls-0_23",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
"cookies",
|
||||
"secure-cookies",
|
||||
"macros",
|
||||
"openssl",
|
||||
"rustls-0_20",
|
||||
"rustls-0_21",
|
||||
"rustls-0_22",
|
||||
"rustls-0_23",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
"cookies",
|
||||
"secure-cookies",
|
||||
]
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_http::*",
|
||||
"actix_router::*",
|
||||
"actix_rt::*",
|
||||
"actix_server::*",
|
||||
"actix_service::*",
|
||||
"actix_utils::*",
|
||||
"actix_web_codegen::*",
|
||||
"bytes::*",
|
||||
"cookie::*",
|
||||
"cookie",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"language_tags::*",
|
||||
"mime::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"serde_json::*",
|
||||
"serde_urlencoded::*",
|
||||
"serde::*",
|
||||
"serde::*",
|
||||
"tokio::*",
|
||||
"url::*",
|
||||
"actix_http::*",
|
||||
"actix_router::*",
|
||||
"actix_rt::*",
|
||||
"actix_server::*",
|
||||
"actix_service::*",
|
||||
"actix_utils::*",
|
||||
"actix_web_codegen::*",
|
||||
"bytes::*",
|
||||
"cookie::*",
|
||||
"cookie",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"language_tags::*",
|
||||
"mime::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"serde_json::*",
|
||||
"serde_urlencoded::*",
|
||||
"serde::*",
|
||||
"serde::*",
|
||||
"tokio::*",
|
||||
"url::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = [
|
||||
"macros",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
"cookies",
|
||||
"http2",
|
||||
"unicode",
|
||||
"compat",
|
||||
"macros",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
"cookies",
|
||||
"http2",
|
||||
"unicode",
|
||||
"compat",
|
||||
]
|
||||
|
||||
# Brotli algorithm content-encoding support
|
||||
|
@ -121,9 +118,7 @@ __tls = []
|
|||
experimental-io-uring = ["actix-server/io-uring"]
|
||||
|
||||
# Feature group which, when disabled, helps migrate code to v5.0.
|
||||
compat = [
|
||||
"compat-routing-macros-force-pub",
|
||||
]
|
||||
compat = ["compat-routing-macros-force-pub"]
|
||||
|
||||
# Opt-out forwards-compatibility for handler visibility inheritance fix.
|
||||
compat-routing-macros-force-pub = ["actix-web-codegen?/compat-routing-macros-force-pub"]
|
||||
|
@ -132,29 +127,30 @@ compat-routing-macros-force-pub = ["actix-web-codegen?/compat-routing-macros-for
|
|||
actix-codec = "0.5"
|
||||
actix-macros = { version = "0.2.3", optional = true }
|
||||
actix-rt = { version = "2.6", default-features = false }
|
||||
actix-server = "2"
|
||||
actix-server = "2.6"
|
||||
actix-service = "2"
|
||||
actix-utils = "3"
|
||||
actix-tls = { version = "3.4", default-features = false, optional = true }
|
||||
actix-utils = "3"
|
||||
|
||||
actix-http = { version = "3.7", features = ["ws"] }
|
||||
actix-http = { version = "3.11", features = ["ws"] }
|
||||
actix-router = { version = "0.5.3", default-features = false, features = ["http"] }
|
||||
actix-web-codegen = { version = "4.3", optional = true, default-features = false }
|
||||
|
||||
ahash = "0.8"
|
||||
bytes = "1"
|
||||
bytestring = "1"
|
||||
cfg-if = "1"
|
||||
cookie = { version = "0.16", features = ["percent-encode"], optional = true }
|
||||
derive_more = "0.99.8"
|
||||
derive_more = { version = "2", features = ["as_ref", "deref", "deref_mut", "display", "error", "from"] }
|
||||
encoding_rs = "0.8"
|
||||
foldhash = "0.1"
|
||||
futures-core = { version = "0.3.17", default-features = false }
|
||||
futures-util = { version = "0.3.17", default-features = false }
|
||||
impl-more = "0.1.4"
|
||||
itoa = "1"
|
||||
language-tags = "0.3"
|
||||
log = "0.4"
|
||||
mime = "0.3"
|
||||
once_cell = "1.5"
|
||||
once_cell = "1.21"
|
||||
pin-project-lite = "0.2.7"
|
||||
regex = { version = "1.5.5", optional = true }
|
||||
regex-lite = "0.1"
|
||||
|
@ -164,28 +160,30 @@ serde_urlencoded = "0.7"
|
|||
smallvec = "1.6.1"
|
||||
socket2 = "0.5"
|
||||
time = { version = "0.3", default-features = false, features = ["formatting"] }
|
||||
url = "2.1"
|
||||
tracing = "0.1.30"
|
||||
url = "2.5.4"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-files = "0.6"
|
||||
actix-test = { version = "0.1", features = ["openssl", "rustls-0_23"] }
|
||||
awc = { version = "3", features = ["openssl"] }
|
||||
|
||||
brotli = "6"
|
||||
const-str = "0.5"
|
||||
brotli = "8"
|
||||
const-str = "0.5" # TODO(MSRV 1.77): update to 0.6
|
||||
core_affinity = "0.8"
|
||||
criterion = { version = "0.5", features = ["html_reports"] }
|
||||
env_logger = "0.11"
|
||||
flate2 = "1.0.13"
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
|
||||
rand = "0.8"
|
||||
rand = "0.9"
|
||||
rcgen = "0.13"
|
||||
rustls-pemfile = "2"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
static_assertions = "1"
|
||||
tls-openssl = { package = "openssl", version = "0.10.55" }
|
||||
tls-rustls = { package = "rustls", version = "0.23" }
|
||||
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
|
||||
tokio = { version = "1.38.2", features = ["rt-multi-thread", "macros"] }
|
||||
tokio-util = "0.7"
|
||||
zstd = "0.13"
|
||||
|
||||
[lints]
|
||||
|
|
|
@ -8,13 +8,13 @@
|
|||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-web)
|
||||
[](https://docs.rs/actix-web/4.8.0)
|
||||
[](https://docs.rs/actix-web/4.11.0)
|
||||

|
||||

|
||||
[](https://deps.rs/crate/actix-web/4.8.0)
|
||||
[](https://deps.rs/crate/actix-web/4.11.0)
|
||||
<br />
|
||||
[](https://github.com/actix/actix-web/actions/workflows/ci.yml)
|
||||
[](https://codecov.io/gh/actix/actix-web)
|
||||
[](https://codecov.io/gh/actix/actix-web)
|
||||

|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
//! Shows a few of ways to use the `from_fn` middleware.
|
||||
|
||||
use std::{collections::HashMap, io, rc::Rc, time::Duration};
|
||||
|
||||
use actix_web::{
|
||||
body::MessageBody,
|
||||
dev::{Service, ServiceRequest, ServiceResponse, Transform},
|
||||
http::header::{self, HeaderValue, Range},
|
||||
middleware::{from_fn, Logger, Next},
|
||||
web::{self, Header, Query},
|
||||
App, Error, HttpResponse, HttpServer,
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
async fn noop<B>(req: ServiceRequest, next: Next<B>) -> Result<ServiceResponse<B>, Error> {
|
||||
next.call(req).await
|
||||
}
|
||||
|
||||
async fn print_range_header<B>(
|
||||
range_header: Option<Header<Range>>,
|
||||
req: ServiceRequest,
|
||||
next: Next<B>,
|
||||
) -> Result<ServiceResponse<B>, Error> {
|
||||
if let Some(Header(range)) = range_header {
|
||||
println!("Range: {range}");
|
||||
} else {
|
||||
println!("No Range header");
|
||||
}
|
||||
|
||||
next.call(req).await
|
||||
}
|
||||
|
||||
async fn mutate_body_type(
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
let res = next.call(req).await?;
|
||||
Ok(res.map_into_left_body::<()>())
|
||||
}
|
||||
|
||||
async fn mutate_body_type_with_extractors(
|
||||
string_body: String,
|
||||
query: Query<HashMap<String, String>>,
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
println!("body is: {string_body}");
|
||||
println!("query string: {query:?}");
|
||||
|
||||
let res = next.call(req).await?;
|
||||
|
||||
Ok(res.map_body(move |_, _| string_body))
|
||||
}
|
||||
|
||||
async fn timeout_10secs(
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
match tokio::time::timeout(Duration::from_secs(10), next.call(req)).await {
|
||||
Ok(res) => res,
|
||||
Err(_err) => Err(actix_web::error::ErrorRequestTimeout("")),
|
||||
}
|
||||
}
|
||||
|
||||
struct MyMw(bool);
|
||||
|
||||
impl MyMw {
|
||||
async fn mw_cb(
|
||||
&self,
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
let mut res = match self.0 {
|
||||
true => req.into_response("short-circuited").map_into_right_body(),
|
||||
false => next.call(req).await?.map_into_left_body(),
|
||||
};
|
||||
|
||||
res.headers_mut()
|
||||
.insert(header::WARNING, HeaderValue::from_static("42"));
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn into_middleware<S, B>(
|
||||
self,
|
||||
) -> impl Transform<
|
||||
S,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse<impl MessageBody>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
let this = Rc::new(self);
|
||||
from_fn(move |req, next| {
|
||||
let this = Rc::clone(&this);
|
||||
async move { Self::mw_cb(&this, req, next).await }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let bind = ("127.0.0.1", 8080);
|
||||
info!("staring server at http://{}:{}", &bind.0, &bind.1);
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(from_fn(noop))
|
||||
.wrap(from_fn(print_range_header))
|
||||
.wrap(from_fn(mutate_body_type))
|
||||
.wrap(from_fn(mutate_body_type_with_extractors))
|
||||
.wrap(from_fn(timeout_10secs))
|
||||
// switch bool to true to observe early response
|
||||
.wrap(MyMw(false).into_middleware())
|
||||
.wrap(Logger::default())
|
||||
.default_service(web::to(HttpResponse::Ok))
|
||||
})
|
||||
.workers(1)
|
||||
.bind(bind)?
|
||||
.run()
|
||||
.await
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
//! Shows a couple of ways to use the `from_fn` middleware.
|
||||
|
||||
use std::{collections::HashMap, io, rc::Rc, time::Duration};
|
||||
|
||||
use actix_web::{
|
||||
body::MessageBody,
|
||||
dev::{Service, ServiceRequest, ServiceResponse, Transform},
|
||||
http::header::{self, HeaderValue, Range},
|
||||
middleware::{from_fn, Logger, Next},
|
||||
web::{self, Header, Query},
|
||||
App, Error, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
async fn noop<B>(req: ServiceRequest, next: Next<B>) -> Result<ServiceResponse<B>, Error> {
|
||||
next.call(req).await
|
||||
}
|
||||
|
||||
async fn print_range_header<B>(
|
||||
range_header: Option<Header<Range>>,
|
||||
req: ServiceRequest,
|
||||
next: Next<B>,
|
||||
) -> Result<ServiceResponse<B>, Error> {
|
||||
if let Some(Header(range)) = range_header {
|
||||
println!("Range: {range}");
|
||||
} else {
|
||||
println!("No Range header");
|
||||
}
|
||||
|
||||
next.call(req).await
|
||||
}
|
||||
|
||||
async fn mutate_body_type(
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
let res = next.call(req).await?;
|
||||
Ok(res.map_into_left_body::<()>())
|
||||
}
|
||||
|
||||
async fn mutate_body_type_with_extractors(
|
||||
string_body: String,
|
||||
query: Query<HashMap<String, String>>,
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
println!("body is: {string_body}");
|
||||
println!("query string: {query:?}");
|
||||
|
||||
let res = next.call(req).await?;
|
||||
|
||||
Ok(res.map_body(move |_, _| string_body))
|
||||
}
|
||||
|
||||
async fn timeout_10secs(
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
match tokio::time::timeout(Duration::from_secs(10), next.call(req)).await {
|
||||
Ok(res) => res,
|
||||
Err(_err) => Err(actix_web::error::ErrorRequestTimeout("")),
|
||||
}
|
||||
}
|
||||
|
||||
struct MyMw(bool);
|
||||
|
||||
impl MyMw {
|
||||
async fn mw_cb(
|
||||
&self,
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
let mut res = match self.0 {
|
||||
true => req.into_response("short-circuited").map_into_right_body(),
|
||||
false => next.call(req).await?.map_into_left_body(),
|
||||
};
|
||||
|
||||
res.headers_mut()
|
||||
.insert(header::WARNING, HeaderValue::from_static("42"));
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn into_middleware<S, B>(
|
||||
self,
|
||||
) -> impl Transform<
|
||||
S,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse<impl MessageBody>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
let this = Rc::new(self);
|
||||
from_fn(move |req, next| {
|
||||
let this = Rc::clone(&this);
|
||||
async move { Self::mw_cb(&this, req, next).await }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let bind = ("127.0.0.1", 8080);
|
||||
log::info!("staring server at http://{}:{}", &bind.0, &bind.1);
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(from_fn(noop))
|
||||
.wrap(from_fn(print_range_header))
|
||||
.wrap(from_fn(mutate_body_type))
|
||||
.wrap(from_fn(mutate_body_type_with_extractors))
|
||||
.wrap(from_fn(timeout_10secs))
|
||||
// switch bool to true to observe early response
|
||||
.wrap(MyMw(false).into_middleware())
|
||||
.wrap(Logger::default())
|
||||
.default_service(web::to(HttpResponse::Ok))
|
||||
})
|
||||
.workers(1)
|
||||
.bind(bind)?
|
||||
.run()
|
||||
.await
|
||||
}
|
|
@ -269,9 +269,9 @@ where
|
|||
+ 'static,
|
||||
U::InitError: fmt::Debug,
|
||||
{
|
||||
let svc = svc
|
||||
.into_factory()
|
||||
.map_init_err(|e| log::error!("Can not construct default service: {:?}", e));
|
||||
let svc = svc.into_factory().map_init_err(|err| {
|
||||
log::error!("Can not construct default service: {err:?}");
|
||||
});
|
||||
|
||||
self.default = Some(Rc::new(boxed::factory(svc)));
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
|
|||
|
||||
/// An error representing a problem running a blocking task on a thread pool.
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "Blocking thread pool is shut down unexpectedly")]
|
||||
#[display("Blocking thread pool is shut down unexpectedly")]
|
||||
#[non_exhaustive]
|
||||
pub struct BlockingError;
|
||||
|
||||
|
@ -40,15 +40,15 @@ impl ResponseError for crate::error::BlockingError {}
|
|||
#[non_exhaustive]
|
||||
pub enum UrlGenerationError {
|
||||
/// Resource not found.
|
||||
#[display(fmt = "Resource not found")]
|
||||
#[display("Resource not found")]
|
||||
ResourceNotFound,
|
||||
|
||||
/// Not all URL parameters covered.
|
||||
#[display(fmt = "Not all URL parameters covered")]
|
||||
#[display("Not all URL parameters covered")]
|
||||
NotEnoughElements,
|
||||
|
||||
/// URL parse error.
|
||||
#[display(fmt = "{}", _0)]
|
||||
#[display("{}", _0)]
|
||||
ParseError(UrlParseError),
|
||||
}
|
||||
|
||||
|
@ -59,39 +59,39 @@ impl ResponseError for UrlGenerationError {}
|
|||
#[non_exhaustive]
|
||||
pub enum UrlencodedError {
|
||||
/// Can not decode chunked transfer encoding.
|
||||
#[display(fmt = "Can not decode chunked transfer encoding.")]
|
||||
#[display("Can not decode chunked transfer encoding.")]
|
||||
Chunked,
|
||||
|
||||
/// Payload size is larger than allowed. (default limit: 256kB).
|
||||
#[display(
|
||||
fmt = "URL encoded payload is larger ({} bytes) than allowed (limit: {} bytes).",
|
||||
"URL encoded payload is larger ({} bytes) than allowed (limit: {} bytes).",
|
||||
size,
|
||||
limit
|
||||
)]
|
||||
Overflow { size: usize, limit: usize },
|
||||
|
||||
/// Payload size is now known.
|
||||
#[display(fmt = "Payload size is now known.")]
|
||||
#[display("Payload size is now known.")]
|
||||
UnknownLength,
|
||||
|
||||
/// Content type error.
|
||||
#[display(fmt = "Content type error.")]
|
||||
#[display("Content type error.")]
|
||||
ContentType,
|
||||
|
||||
/// Parse error.
|
||||
#[display(fmt = "Parse error: {}.", _0)]
|
||||
#[display("Parse error: {}.", _0)]
|
||||
Parse(FormDeError),
|
||||
|
||||
/// Encoding error.
|
||||
#[display(fmt = "Encoding error.")]
|
||||
#[display("Encoding error.")]
|
||||
Encoding,
|
||||
|
||||
/// Serialize error.
|
||||
#[display(fmt = "Serialize error: {}.", _0)]
|
||||
#[display("Serialize error: {}.", _0)]
|
||||
Serialize(FormError),
|
||||
|
||||
/// Payload error.
|
||||
#[display(fmt = "Error that occur during reading payload: {}.", _0)]
|
||||
#[display("Error that occur during reading payload: {}.", _0)]
|
||||
Payload(PayloadError),
|
||||
}
|
||||
|
||||
|
@ -113,30 +113,30 @@ impl ResponseError for UrlencodedError {
|
|||
pub enum JsonPayloadError {
|
||||
/// Payload size is bigger than allowed & content length header set. (default: 2MB)
|
||||
#[display(
|
||||
fmt = "JSON payload ({} bytes) is larger than allowed (limit: {} bytes).",
|
||||
"JSON payload ({} bytes) is larger than allowed (limit: {} bytes).",
|
||||
length,
|
||||
limit
|
||||
)]
|
||||
OverflowKnownLength { length: usize, limit: usize },
|
||||
|
||||
/// Payload size is bigger than allowed but no content length header set. (default: 2MB)
|
||||
#[display(fmt = "JSON payload has exceeded limit ({} bytes).", limit)]
|
||||
#[display("JSON payload has exceeded limit ({} bytes).", limit)]
|
||||
Overflow { limit: usize },
|
||||
|
||||
/// Content type error
|
||||
#[display(fmt = "Content type error")]
|
||||
#[display("Content type error")]
|
||||
ContentType,
|
||||
|
||||
/// Deserialize error
|
||||
#[display(fmt = "Json deserialize error: {}", _0)]
|
||||
#[display("Json deserialize error: {}", _0)]
|
||||
Deserialize(JsonError),
|
||||
|
||||
/// Serialize error
|
||||
#[display(fmt = "Json serialize error: {}", _0)]
|
||||
#[display("Json serialize error: {}", _0)]
|
||||
Serialize(JsonError),
|
||||
|
||||
/// Payload error
|
||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
||||
#[display("Error that occur during reading payload: {}", _0)]
|
||||
Payload(PayloadError),
|
||||
}
|
||||
|
||||
|
@ -166,7 +166,7 @@ impl ResponseError for JsonPayloadError {
|
|||
#[non_exhaustive]
|
||||
pub enum PathError {
|
||||
/// Deserialize error
|
||||
#[display(fmt = "Path deserialize error: {}", _0)]
|
||||
#[display("Path deserialize error: {}", _0)]
|
||||
Deserialize(serde::de::value::Error),
|
||||
}
|
||||
|
||||
|
@ -182,7 +182,7 @@ impl ResponseError for PathError {
|
|||
#[non_exhaustive]
|
||||
pub enum QueryPayloadError {
|
||||
/// Query deserialize error.
|
||||
#[display(fmt = "Query deserialize error: {}", _0)]
|
||||
#[display("Query deserialize error: {}", _0)]
|
||||
Deserialize(serde::de::value::Error),
|
||||
}
|
||||
|
||||
|
@ -196,20 +196,20 @@ impl ResponseError for QueryPayloadError {
|
|||
#[derive(Debug, Display, Error, From)]
|
||||
#[non_exhaustive]
|
||||
pub enum ReadlinesError {
|
||||
#[display(fmt = "Encoding error")]
|
||||
#[display("Encoding error")]
|
||||
/// Payload size is bigger than allowed. (default: 256kB)
|
||||
EncodingError,
|
||||
|
||||
/// Payload error.
|
||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
||||
#[display("Error that occur during reading payload: {}", _0)]
|
||||
Payload(PayloadError),
|
||||
|
||||
/// Line limit exceeded.
|
||||
#[display(fmt = "Line limit exceeded")]
|
||||
#[display("Line limit exceeded")]
|
||||
LimitOverflow,
|
||||
|
||||
/// ContentType error.
|
||||
#[display(fmt = "Content-type error")]
|
||||
#[display("Content-type error")]
|
||||
ContentTypeError(ContentTypeError),
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use actix_http::{header, uri::Uri, RequestHead};
|
||||
use actix_http::{header, uri::Uri, RequestHead, Version};
|
||||
|
||||
use super::{Guard, GuardContext};
|
||||
|
||||
|
@ -66,6 +66,7 @@ fn get_host_uri(req: &RequestHead) -> Option<Uri> {
|
|||
req.headers
|
||||
.get(header::HOST)
|
||||
.and_then(|host_value| host_value.to_str().ok())
|
||||
.filter(|_| req.version < Version::HTTP_2)
|
||||
.or_else(|| req.uri.host())
|
||||
.and_then(|host| host.parse().ok())
|
||||
}
|
||||
|
@ -123,6 +124,38 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::test::TestRequest;
|
||||
|
||||
#[test]
|
||||
fn host_not_from_header_if_http2() {
|
||||
let req = TestRequest::default()
|
||||
.uri("www.rust-lang.org")
|
||||
.insert_header((
|
||||
header::HOST,
|
||||
header::HeaderValue::from_static("www.example.com"),
|
||||
))
|
||||
.to_srv_request();
|
||||
|
||||
let host = Host("www.example.com");
|
||||
assert!(host.check(&req.guard_ctx()));
|
||||
|
||||
let host = Host("www.rust-lang.org");
|
||||
assert!(!host.check(&req.guard_ctx()));
|
||||
|
||||
let req = TestRequest::default()
|
||||
.version(actix_http::Version::HTTP_2)
|
||||
.uri("www.rust-lang.org")
|
||||
.insert_header((
|
||||
header::HOST,
|
||||
header::HeaderValue::from_static("www.example.com"),
|
||||
))
|
||||
.to_srv_request();
|
||||
|
||||
let host = Host("www.example.com");
|
||||
assert!(!host.check(&req.guard_ctx()));
|
||||
|
||||
let host = Host("www.rust-lang.org");
|
||||
assert!(host.check(&req.guard_ctx()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_from_header() {
|
||||
let req = TestRequest::default()
|
||||
|
|
|
@ -70,7 +70,7 @@ use crate::{
|
|||
/// This is the source code for the 2-parameter implementation of `Handler` to help illustrate the
|
||||
/// bounds of the handler call after argument extraction:
|
||||
/// ```ignore
|
||||
/// impl<Func, Arg1, Arg2, Fut> Handler<(Arg1, Arg2)> for Func
|
||||
/// impl<Func, Fut, Arg1, Arg2> Handler<(Arg1, Arg2)> for Func
|
||||
/// where
|
||||
/// Func: Fn(Arg1, Arg2) -> Fut + Clone + 'static,
|
||||
/// Fut: Future,
|
||||
|
|
|
@ -10,7 +10,7 @@ use bytes::BufMut;
|
|||
/// perform a remaining length check before writing.
|
||||
pub(crate) struct MutWriter<'a, B>(pub(crate) &'a mut B);
|
||||
|
||||
impl<'a, B> io::Write for MutWriter<'a, B>
|
||||
impl<B> io::Write for MutWriter<'_, B>
|
||||
where
|
||||
B: BufMut,
|
||||
{
|
||||
|
|
|
@ -206,11 +206,11 @@ impl DispositionParam {
|
|||
}
|
||||
}
|
||||
|
||||
/// A *Content-Disposition* header. It is compatible to be used either as
|
||||
/// [a response header for the main body](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body)
|
||||
/// as (re)defined in [RFC 6266](https://datatracker.ietf.org/doc/html/rfc6266), or as
|
||||
/// [a header for a multipart body](https://mdn.io/Content-Disposition#As_a_header_for_a_multipart_body)
|
||||
/// as (re)defined in [RFC 7587](https://datatracker.ietf.org/doc/html/rfc7578).
|
||||
/// `Content-Disposition` header.
|
||||
///
|
||||
/// It is compatible to be used either as [a response header for the main body][use_main_body]
|
||||
/// as (re)defined in [RFC 6266], or as [a header for a multipart body][use_multipart] as
|
||||
/// (re)defined in [RFC 7587].
|
||||
///
|
||||
/// In a regular HTTP response, the *Content-Disposition* response header is a header indicating if
|
||||
/// the content is expected to be displayed *inline* in the browser, that is, as a Web page or as
|
||||
|
@ -267,7 +267,7 @@ impl DispositionParam {
|
|||
/// parameters: vec![DispositionParam::FilenameExt(ExtendedValue {
|
||||
/// charset: Charset::Iso_8859_1, // The character set for the bytes of the filename
|
||||
/// language_tag: None, // The optional language tag (see `language-tag` crate)
|
||||
/// value: b"\xa9 Copyright 1989.txt".to_vec(), // the actual bytes of the filename
|
||||
/// value: b"\xA9 Ferris 2011.txt".to_vec(), // the actual bytes of the filename
|
||||
/// })],
|
||||
/// };
|
||||
/// assert!(cd1.is_attachment());
|
||||
|
@ -305,6 +305,11 @@ impl DispositionParam {
|
|||
/// change to match local file system conventions if applicable, and do not use directory path
|
||||
/// information that may be present.
|
||||
/// See [RFC 2183 §2.3](https://datatracker.ietf.org/doc/html/rfc2183#section-2.3).
|
||||
///
|
||||
/// [use_main_body]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body
|
||||
/// [RFC 6266]: https://datatracker.ietf.org/doc/html/rfc6266
|
||||
/// [use_multipart]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_header_for_a_multipart_body
|
||||
/// [RFC 7587]: https://datatracker.ietf.org/doc/html/rfc7578
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ContentDisposition {
|
||||
/// The disposition type
|
||||
|
|
|
@ -158,7 +158,7 @@ impl ConnectionInfo {
|
|||
/// The address is resolved through the following, in order:
|
||||
/// - `Forwarded` header
|
||||
/// - `X-Forwarded-For` header
|
||||
/// - peer address of opened socket (same as [`remote_addr`](Self::remote_addr))
|
||||
/// - peer address of opened socket (same as [`peer_addr`](Self::peer_addr))
|
||||
///
|
||||
/// # Security
|
||||
/// Do not use this function for security purposes unless you can be sure that the `Forwarded`
|
||||
|
@ -235,7 +235,7 @@ impl FromRequest for ConnectionInfo {
|
|||
/// # let _svc = actix_web::web::to(handler);
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)]
|
||||
#[display(fmt = "{}", _0)]
|
||||
#[display("{}", _0)]
|
||||
pub struct PeerAddr(pub SocketAddr);
|
||||
|
||||
impl PeerAddr {
|
||||
|
@ -247,7 +247,7 @@ impl PeerAddr {
|
|||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[non_exhaustive]
|
||||
#[display(fmt = "Missing peer address")]
|
||||
#[display("Missing peer address")]
|
||||
pub struct MissingPeerAddr;
|
||||
|
||||
impl ResponseError for MissingPeerAddr {}
|
||||
|
|
|
@ -78,7 +78,7 @@ pub use actix_http::{body, HttpMessage};
|
|||
#[cfg(feature = "cookies")]
|
||||
#[doc(inline)]
|
||||
pub use cookie;
|
||||
|
||||
pub use mime;
|
||||
mod app;
|
||||
mod app_service;
|
||||
mod config;
|
||||
|
@ -104,6 +104,7 @@ mod scope;
|
|||
mod server;
|
||||
mod service;
|
||||
pub mod test;
|
||||
mod thin_data;
|
||||
pub(crate) mod types;
|
||||
pub mod web;
|
||||
|
||||
|
|
|
@ -191,8 +191,10 @@ where
|
|||
None => true,
|
||||
Some(hdr) => {
|
||||
match hdr.to_str().ok().and_then(|hdr| hdr.parse::<Mime>().ok()) {
|
||||
Some(mime) if mime.type_().as_str() == "image" => false,
|
||||
Some(mime) if mime.type_().as_str() == "video" => false,
|
||||
Some(mime) if mime.type_() == mime::IMAGE => {
|
||||
matches!(mime.subtype(), mime::SVG)
|
||||
}
|
||||
Some(mime) if mime.type_() == mime::VIDEO => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use std::{
|
|||
};
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
use ahash::AHashMap;
|
||||
use foldhash::HashMap as FoldHashMap;
|
||||
use futures_core::{future::LocalBoxFuture, ready};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
|
@ -185,7 +185,7 @@ pub struct ErrorHandlers<B> {
|
|||
handlers: Handlers<B>,
|
||||
}
|
||||
|
||||
type Handlers<B> = Rc<AHashMap<StatusCode, Box<ErrorHandler<B>>>>;
|
||||
type Handlers<B> = Rc<FoldHashMap<StatusCode, Box<ErrorHandler<B>>>>;
|
||||
|
||||
impl<B> Default for ErrorHandlers<B> {
|
||||
fn default() -> Self {
|
||||
|
|
|
@ -0,0 +1,349 @@
|
|||
use std::{future::Future, marker::PhantomData, rc::Rc};
|
||||
|
||||
use actix_service::boxed::{self, BoxFuture, RcService};
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
use crate::{
|
||||
body::MessageBody,
|
||||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
|
||||
Error, FromRequest,
|
||||
};
|
||||
|
||||
/// Wraps an async function to be used as a middleware.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// The wrapped function should have the following form:
|
||||
///
|
||||
/// ```
|
||||
/// # use actix_web::{
|
||||
/// # App, Error,
|
||||
/// # body::MessageBody,
|
||||
/// # dev::{ServiceRequest, ServiceResponse, Service as _},
|
||||
/// # };
|
||||
/// use actix_web::middleware::{self, Next};
|
||||
///
|
||||
/// async fn my_mw(
|
||||
/// req: ServiceRequest,
|
||||
/// next: Next<impl MessageBody>,
|
||||
/// ) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
/// // pre-processing
|
||||
/// next.call(req).await
|
||||
/// // post-processing
|
||||
/// }
|
||||
/// # App::new().wrap(middleware::from_fn(my_mw));
|
||||
/// ```
|
||||
///
|
||||
/// Then use in an app builder like this:
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::{
|
||||
/// App, Error,
|
||||
/// dev::{ServiceRequest, ServiceResponse, Service as _},
|
||||
/// };
|
||||
/// use actix_web::middleware::from_fn;
|
||||
/// # use actix_web::middleware::Next;
|
||||
/// # async fn my_mw<B>(req: ServiceRequest, next: Next<B>) -> Result<ServiceResponse<B>, Error> {
|
||||
/// # next.call(req).await
|
||||
/// # }
|
||||
///
|
||||
/// App::new()
|
||||
/// .wrap(from_fn(my_mw))
|
||||
/// # ;
|
||||
/// ```
|
||||
///
|
||||
/// It is also possible to write a middleware that automatically uses extractors, similar to request
|
||||
/// handlers, by declaring them as the first parameters. As usual, **take care with extractors that
|
||||
/// consume the body stream**, since handlers will no longer be able to read it again without
|
||||
/// putting the body "back" into the request object within your middleware.
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::HashMap;
|
||||
/// # use actix_web::{
|
||||
/// # App, Error,
|
||||
/// # body::MessageBody,
|
||||
/// # dev::{ServiceRequest, ServiceResponse},
|
||||
/// # http::header::{Accept, Date},
|
||||
/// # web::{Header, Query},
|
||||
/// # };
|
||||
/// use actix_web::middleware::Next;
|
||||
///
|
||||
/// async fn my_extracting_mw(
|
||||
/// accept: Header<Accept>,
|
||||
/// query: Query<HashMap<String, String>>,
|
||||
/// req: ServiceRequest,
|
||||
/// next: Next<impl MessageBody>,
|
||||
/// ) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
/// // pre-processing
|
||||
/// next.call(req).await
|
||||
/// // post-processing
|
||||
/// }
|
||||
/// # App::new().wrap(actix_web::middleware::from_fn(my_extracting_mw));
|
||||
pub fn from_fn<F, Es>(mw_fn: F) -> MiddlewareFn<F, Es> {
|
||||
MiddlewareFn {
|
||||
mw_fn: Rc::new(mw_fn),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Middleware transform for [`from_fn`].
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct MiddlewareFn<F, Es> {
|
||||
mw_fn: Rc<F>,
|
||||
_phantom: PhantomData<Es>,
|
||||
}
|
||||
|
||||
impl<S, F, Fut, B, B2> Transform<S, ServiceRequest> for MiddlewareFn<F, ()>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
F: Fn(ServiceRequest, Next<B>) -> Fut + 'static,
|
||||
Fut: Future<Output = Result<ServiceResponse<B2>, Error>>,
|
||||
B2: MessageBody,
|
||||
{
|
||||
type Response = ServiceResponse<B2>;
|
||||
type Error = Error;
|
||||
type Transform = MiddlewareFnService<F, B, ()>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(MiddlewareFnService {
|
||||
service: boxed::rc_service(service),
|
||||
mw_fn: Rc::clone(&self.mw_fn),
|
||||
_phantom: PhantomData,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// Middleware service for [`from_fn`].
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct MiddlewareFnService<F, B, Es> {
|
||||
service: RcService<ServiceRequest, ServiceResponse<B>, Error>,
|
||||
mw_fn: Rc<F>,
|
||||
_phantom: PhantomData<(B, Es)>,
|
||||
}
|
||||
|
||||
impl<F, Fut, B, B2> Service<ServiceRequest> for MiddlewareFnService<F, B, ()>
|
||||
where
|
||||
F: Fn(ServiceRequest, Next<B>) -> Fut,
|
||||
Fut: Future<Output = Result<ServiceResponse<B2>, Error>>,
|
||||
B2: MessageBody,
|
||||
{
|
||||
type Response = ServiceResponse<B2>;
|
||||
type Error = Error;
|
||||
type Future = Fut;
|
||||
|
||||
forward_ready!(service);
|
||||
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
(self.mw_fn)(
|
||||
req,
|
||||
Next::<B> {
|
||||
service: Rc::clone(&self.service),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_middleware_fn_service {
|
||||
($($ext_type:ident),*) => {
|
||||
impl<S, F, Fut, B, B2, $($ext_type),*> Transform<S, ServiceRequest> for MiddlewareFn<F, ($($ext_type),*,)>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
F: Fn($($ext_type),*, ServiceRequest, Next<B>) -> Fut + 'static,
|
||||
$($ext_type: FromRequest + 'static,)*
|
||||
Fut: Future<Output = Result<ServiceResponse<B2>, Error>> + 'static,
|
||||
B: MessageBody + 'static,
|
||||
B2: MessageBody + 'static,
|
||||
{
|
||||
type Response = ServiceResponse<B2>;
|
||||
type Error = Error;
|
||||
type Transform = MiddlewareFnService<F, B, ($($ext_type,)*)>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(MiddlewareFnService {
|
||||
service: boxed::rc_service(service),
|
||||
mw_fn: Rc::clone(&self.mw_fn),
|
||||
_phantom: PhantomData,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, $($ext_type),*, Fut, B: 'static, B2> Service<ServiceRequest>
|
||||
for MiddlewareFnService<F, B, ($($ext_type),*,)>
|
||||
where
|
||||
F: Fn(
|
||||
$($ext_type),*,
|
||||
ServiceRequest,
|
||||
Next<B>
|
||||
) -> Fut + 'static,
|
||||
$($ext_type: FromRequest + 'static,)*
|
||||
Fut: Future<Output = Result<ServiceResponse<B2>, Error>> + 'static,
|
||||
B2: MessageBody + 'static,
|
||||
{
|
||||
type Response = ServiceResponse<B2>;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
forward_ready!(service);
|
||||
|
||||
#[allow(nonstandard_style)]
|
||||
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||
let mw_fn = Rc::clone(&self.mw_fn);
|
||||
let service = Rc::clone(&self.service);
|
||||
|
||||
Box::pin(async move {
|
||||
let ($($ext_type,)*) = req.extract::<($($ext_type,)*)>().await?;
|
||||
|
||||
(mw_fn)($($ext_type),*, req, Next::<B> { service }).await
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_middleware_fn_service!(E1);
|
||||
impl_middleware_fn_service!(E1, E2);
|
||||
impl_middleware_fn_service!(E1, E2, E3);
|
||||
impl_middleware_fn_service!(E1, E2, E3, E4);
|
||||
impl_middleware_fn_service!(E1, E2, E3, E4, E5);
|
||||
impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6);
|
||||
impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6, E7);
|
||||
impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6, E7, E8);
|
||||
impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6, E7, E8, E9);
|
||||
|
||||
/// Wraps the "next" service in the middleware chain.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Next<B> {
|
||||
service: RcService<ServiceRequest, ServiceResponse<B>, Error>,
|
||||
}
|
||||
|
||||
impl<B> Next<B> {
|
||||
/// Equivalent to `Service::call(self, req)`.
|
||||
pub fn call(&self, req: ServiceRequest) -> <Self as Service<ServiceRequest>>::Future {
|
||||
Service::call(self, req)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> Service<ServiceRequest> for Next<B> {
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = BoxFuture<Result<Self::Response, Self::Error>>;
|
||||
|
||||
forward_ready!(service);
|
||||
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
self.service.call(req)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
http::header::{self, HeaderValue},
|
||||
middleware::{Compat, Logger},
|
||||
test, web, App, HttpResponse,
|
||||
};
|
||||
|
||||
async fn noop<B>(req: ServiceRequest, next: Next<B>) -> Result<ServiceResponse<B>, Error> {
|
||||
next.call(req).await
|
||||
}
|
||||
|
||||
async fn add_res_header<B>(
|
||||
req: ServiceRequest,
|
||||
next: Next<B>,
|
||||
) -> Result<ServiceResponse<B>, Error> {
|
||||
let mut res = next.call(req).await?;
|
||||
res.headers_mut()
|
||||
.insert(header::WARNING, HeaderValue::from_static("42"));
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
async fn mutate_body_type(
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
let res = next.call(req).await?;
|
||||
Ok(res.map_into_left_body::<()>())
|
||||
}
|
||||
|
||||
struct MyMw(bool);
|
||||
|
||||
impl MyMw {
|
||||
async fn mw_cb(
|
||||
&self,
|
||||
req: ServiceRequest,
|
||||
next: Next<impl MessageBody + 'static>,
|
||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
let mut res = match self.0 {
|
||||
true => req.into_response("short-circuited").map_into_right_body(),
|
||||
false => next.call(req).await?.map_into_left_body(),
|
||||
};
|
||||
res.headers_mut()
|
||||
.insert(header::WARNING, HeaderValue::from_static("42"));
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn into_middleware<S, B>(
|
||||
self,
|
||||
) -> impl Transform<
|
||||
S,
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse<impl MessageBody>,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
B: MessageBody + 'static,
|
||||
{
|
||||
let this = Rc::new(self);
|
||||
from_fn(move |req, next| {
|
||||
let this = Rc::clone(&this);
|
||||
async move { Self::mw_cb(&this, req, next).await }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn compat_compat() {
|
||||
let _ = App::new().wrap(Compat::new(from_fn(noop)));
|
||||
let _ = App::new().wrap(Compat::new(from_fn(mutate_body_type)));
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn permits_different_in_and_out_body_types() {
|
||||
let app = test::init_service(
|
||||
App::new()
|
||||
.wrap(from_fn(mutate_body_type))
|
||||
.wrap(from_fn(add_res_header))
|
||||
.wrap(Logger::default())
|
||||
.wrap(from_fn(noop))
|
||||
.default_service(web::to(HttpResponse::NotFound)),
|
||||
)
|
||||
.await;
|
||||
|
||||
let req = test::TestRequest::default().to_request();
|
||||
let res = test::call_service(&app, req).await;
|
||||
assert!(res.headers().contains_key(header::WARNING));
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn closure_capture_and_return_from_fn() {
|
||||
let app = test::init_service(
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.wrap(MyMw(true).into_middleware())
|
||||
.wrap(Logger::default()),
|
||||
)
|
||||
.await;
|
||||
|
||||
let req = test::TestRequest::default().to_request();
|
||||
let res = test::call_service(&app, req).await;
|
||||
assert!(res.headers().contains_key(header::WARNING));
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@ use actix_service::{Service, Transform};
|
|||
use actix_utils::future::{ready, Ready};
|
||||
use bytes::Bytes;
|
||||
use futures_core::ready;
|
||||
use log::{debug, warn};
|
||||
use log::{debug, warn, Level};
|
||||
use pin_project_lite::pin_project;
|
||||
#[cfg(feature = "unicode")]
|
||||
use regex::Regex;
|
||||
|
@ -92,6 +92,7 @@ struct Inner {
|
|||
exclude: HashSet<String>,
|
||||
exclude_regex: Vec<Regex>,
|
||||
log_target: Cow<'static, str>,
|
||||
log_level: Level,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
|
@ -102,6 +103,7 @@ impl Logger {
|
|||
exclude: HashSet::new(),
|
||||
exclude_regex: Vec::new(),
|
||||
log_target: Cow::Borrowed(module_path!()),
|
||||
log_level: Level::Info,
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -139,6 +141,23 @@ impl Logger {
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the log level to `level`.
|
||||
///
|
||||
/// By default, the log level is `Level::Info`.
|
||||
///
|
||||
/// # Examples
|
||||
/// Using `.log_level(Level::Debug)` would have this effect on request logs:
|
||||
/// ```diff
|
||||
/// - [2015-10-21T07:28:00Z INFO actix_web::middleware::logger] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985
|
||||
/// + [2015-10-21T07:28:00Z DEBUG actix_web::middleware::logger] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985
|
||||
/// ^^^^^^
|
||||
/// ```
|
||||
pub fn log_level(mut self, level: log::Level) -> Self {
|
||||
let inner = Rc::get_mut(&mut self.0).unwrap();
|
||||
inner.log_level = level;
|
||||
self
|
||||
}
|
||||
|
||||
/// Register a function that receives a ServiceRequest and returns a String for use in the
|
||||
/// log line. The label passed as the first argument should match a replacement substring in
|
||||
/// the logger format like `%{label}xi`.
|
||||
|
@ -242,6 +261,7 @@ impl Default for Logger {
|
|||
exclude: HashSet::new(),
|
||||
exclude_regex: Vec::new(),
|
||||
log_target: Cow::Borrowed(module_path!()),
|
||||
log_level: Level::Info,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -312,6 +332,7 @@ where
|
|||
format: None,
|
||||
time: OffsetDateTime::now_utc(),
|
||||
log_target: Cow::Borrowed(""),
|
||||
log_level: self.inner.log_level,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
} else {
|
||||
|
@ -327,6 +348,7 @@ where
|
|||
format: Some(format),
|
||||
time: now,
|
||||
log_target: self.inner.log_target.clone(),
|
||||
log_level: self.inner.log_level,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
@ -344,6 +366,7 @@ pin_project! {
|
|||
time: OffsetDateTime,
|
||||
format: Option<Format>,
|
||||
log_target: Cow<'static, str>,
|
||||
log_level: Level,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
}
|
||||
|
@ -390,6 +413,7 @@ where
|
|||
let time = *this.time;
|
||||
let format = this.format.take();
|
||||
let log_target = this.log_target.clone();
|
||||
let log_level = *this.log_level;
|
||||
|
||||
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
|
||||
body,
|
||||
|
@ -397,6 +421,7 @@ where
|
|||
format,
|
||||
size: 0,
|
||||
log_target,
|
||||
log_level,
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
@ -409,6 +434,7 @@ pin_project! {
|
|||
size: usize,
|
||||
time: OffsetDateTime,
|
||||
log_target: Cow<'static, str>,
|
||||
log_level: Level
|
||||
}
|
||||
|
||||
impl<B> PinnedDrop for StreamLog<B> {
|
||||
|
@ -421,8 +447,9 @@ pin_project! {
|
|||
Ok(())
|
||||
};
|
||||
|
||||
log::info!(
|
||||
log::log!(
|
||||
target: this.log_target.as_ref(),
|
||||
this.log_level,
|
||||
"{}", FormatDisplay(&render)
|
||||
);
|
||||
}
|
||||
|
@ -622,9 +649,9 @@ impl FormatText {
|
|||
|
||||
FormatText::ResponseHeader(ref name) => {
|
||||
let s = if let Some(val) = res.headers().get(name) {
|
||||
val.to_str().unwrap_or("-")
|
||||
String::from_utf8_lossy(val.as_bytes()).into_owned()
|
||||
} else {
|
||||
"-"
|
||||
"-".to_owned()
|
||||
};
|
||||
*self = FormatText::Str(s.to_string())
|
||||
}
|
||||
|
@ -666,11 +693,11 @@ impl FormatText {
|
|||
FormatText::RequestTime => *self = FormatText::Str(now.format(&Rfc3339).unwrap()),
|
||||
FormatText::RequestHeader(ref name) => {
|
||||
let s = if let Some(val) = req.headers().get(name) {
|
||||
val.to_str().unwrap_or("-")
|
||||
String::from_utf8_lossy(val.as_bytes()).into_owned()
|
||||
} else {
|
||||
"-"
|
||||
"-".to_owned()
|
||||
};
|
||||
*self = FormatText::Str(s.to_string());
|
||||
*self = FormatText::Str(s);
|
||||
}
|
||||
FormatText::RemoteAddr => {
|
||||
let s = if let Some(peer) = req.connection_info().peer_addr() {
|
||||
|
@ -704,7 +731,7 @@ impl FormatText {
|
|||
/// Converter to get a String from something that writes to a Formatter.
|
||||
pub(crate) struct FormatDisplay<'a>(&'a dyn Fn(&mut fmt::Formatter<'_>) -> Result<(), fmt::Error>);
|
||||
|
||||
impl<'a> fmt::Display for FormatDisplay<'a> {
|
||||
impl fmt::Display for FormatDisplay<'_> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
(self.0)(fmt)
|
||||
}
|
||||
|
|
|
@ -15,10 +15,47 @@
|
|||
//! - Access external services (e.g., [sessions](https://docs.rs/actix-session), etc.)
|
||||
//!
|
||||
//! Middleware is registered for each [`App`], [`Scope`](crate::Scope), or
|
||||
//! [`Resource`](crate::Resource) and executed in opposite order as registration. In general, a
|
||||
//! middleware is a pair of types that implements the [`Service`] trait and [`Transform`] trait,
|
||||
//! respectively. The [`new_transform`] and [`call`] methods must return a [`Future`], though it
|
||||
//! can often be [an immediately-ready one](actix_utils::future::Ready).
|
||||
//! [`Resource`](crate::Resource) and executed in opposite order as registration.
|
||||
//!
|
||||
//! # Simple Middleware
|
||||
//!
|
||||
//! In many cases, you can model your middleware as an async function via the [`from_fn()`] helper
|
||||
//! that provides a natural interface for implementing your desired behaviors.
|
||||
//!
|
||||
//! ```
|
||||
//! # use actix_web::{
|
||||
//! # App, Error,
|
||||
//! # body::MessageBody,
|
||||
//! # dev::{ServiceRequest, ServiceResponse, Service as _},
|
||||
//! # };
|
||||
//! use actix_web::middleware::{self, Next};
|
||||
//!
|
||||
//! async fn my_mw(
|
||||
//! req: ServiceRequest,
|
||||
//! next: Next<impl MessageBody>,
|
||||
//! ) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
||||
//! // pre-processing
|
||||
//!
|
||||
//! // invoke the wrapped middleware or service
|
||||
//! let res = next.call(req).await?;
|
||||
//!
|
||||
//! // post-processing
|
||||
//!
|
||||
//! Ok(res)
|
||||
//! }
|
||||
//!
|
||||
//! App::new()
|
||||
//! .wrap(middleware::from_fn(my_mw));
|
||||
//! ```
|
||||
//!
|
||||
//! ## Complex Middleware
|
||||
//!
|
||||
//! In the more general ase, a middleware is a pair of types that implements the [`Service`] trait
|
||||
//! and [`Transform`] trait, respectively. The [`new_transform`] and [`call`] methods must return a
|
||||
//! [`Future`], though it can often be [an immediately-ready one](actix_utils::future::Ready).
|
||||
//!
|
||||
//! All the built-in middleware use this pattern with pairs of builder (`Transform`) +
|
||||
//! implementation (`Service`) types.
|
||||
//!
|
||||
//! # Ordering
|
||||
//!
|
||||
|
@ -196,18 +233,6 @@
|
|||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! # Simpler Middleware
|
||||
//!
|
||||
//! In many cases, you _can_ actually use an async function via a helper that will provide a more
|
||||
//! natural flow for your behavior.
|
||||
//!
|
||||
//! The experimental `actix_web_lab` crate provides a [`from_fn`][lab_from_fn] utility which allows
|
||||
//! an async fn to be wrapped and used in the same way as other middleware. See the
|
||||
//! [`from_fn`][lab_from_fn] docs for more info and examples of it's use.
|
||||
//!
|
||||
//! While [`from_fn`][lab_from_fn] is experimental currently, it's likely this helper will graduate
|
||||
//! to Actix Web in some form, so feedback is appreciated.
|
||||
//!
|
||||
//! [`Future`]: std::future::Future
|
||||
//! [`App`]: crate::App
|
||||
//! [`FromRequest`]: crate::FromRequest
|
||||
|
@ -215,7 +240,7 @@
|
|||
//! [`Transform`]: crate::dev::Transform
|
||||
//! [`call`]: crate::dev::Service::call()
|
||||
//! [`new_transform`]: crate::dev::Transform::new_transform()
|
||||
//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html
|
||||
//! [`from_fn`]: crate
|
||||
|
||||
mod compat;
|
||||
#[cfg(feature = "__compress")]
|
||||
|
@ -223,6 +248,7 @@ mod compress;
|
|||
mod condition;
|
||||
mod default_headers;
|
||||
mod err_handlers;
|
||||
mod from_fn;
|
||||
mod identity;
|
||||
mod logger;
|
||||
mod normalize;
|
||||
|
@ -234,6 +260,7 @@ pub use self::{
|
|||
condition::Condition,
|
||||
default_headers::DefaultHeaders,
|
||||
err_handlers::{ErrorHandlerResponse, ErrorHandlers},
|
||||
from_fn::{from_fn, Next},
|
||||
identity::Identity,
|
||||
logger::Logger,
|
||||
normalize::{NormalizePath, TrailingSlash},
|
||||
|
|
|
@ -264,8 +264,10 @@ impl HttpRequest {
|
|||
///
|
||||
/// For expanded client connection information, use [`connection_info`] instead.
|
||||
///
|
||||
/// Will only return None when called in unit tests unless [`TestRequest::peer_addr`] is used.
|
||||
/// Will only return `None` when server is listening on [UDS socket] or when called in unit
|
||||
/// tests unless [`TestRequest::peer_addr`] is used.
|
||||
///
|
||||
/// [UDS socket]: crate::HttpServer::bind_uds
|
||||
/// [`TestRequest::peer_addr`]: crate::test::TestRequest::peer_addr
|
||||
/// [`connection_info`]: Self::connection_info
|
||||
#[inline]
|
||||
|
|
|
@ -28,9 +28,9 @@ use crate::{
|
|||
///
|
||||
/// Resource in turn has at least one route. Route consists of an handlers objects and list of
|
||||
/// guards (objects that implement `Guard` trait). Resources and routes uses builder-like pattern
|
||||
/// for configuration. During request handling, resource object iterate through all routes and check
|
||||
/// guards for specific route, if request matches all guards, route considered matched and route
|
||||
/// handler get called.
|
||||
/// for configuration. During request handling, the resource object iterates through all routes
|
||||
/// and checks guards for the specific route, if the request matches all the guards, then the route
|
||||
/// is considered matched and the route handler gets called.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
|
@ -358,10 +358,9 @@ where
|
|||
U::InitError: fmt::Debug,
|
||||
{
|
||||
// create and configure default resource
|
||||
self.default = boxed::factory(
|
||||
f.into_factory()
|
||||
.map_init_err(|e| log::error!("Can not construct default service: {:?}", e)),
|
||||
);
|
||||
self.default = boxed::factory(f.into_factory().map_init_err(|err| {
|
||||
log::error!("Can not construct default service: {err:?}");
|
||||
}));
|
||||
|
||||
self
|
||||
}
|
||||
|
|
|
@ -6,7 +6,8 @@ use crate::{HttpResponse, HttpResponseBuilder};
|
|||
|
||||
macro_rules! static_resp {
|
||||
($name:ident, $status:expr) => {
|
||||
#[allow(non_snake_case, missing_docs)]
|
||||
#[allow(non_snake_case)]
|
||||
#[doc = concat!("Creates a new response builder with the status code `", stringify!($status), "`.")]
|
||||
pub fn $name() -> HttpResponseBuilder {
|
||||
HttpResponseBuilder::new($status)
|
||||
}
|
||||
|
|
|
@ -131,6 +131,23 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
// Note: see https://github.com/actix/actix-web/issues/1108 for reasoning why Responder is not
|
||||
// implemented for `()`, and https://github.com/actix/actix-web/pull/3560 for discussion about this
|
||||
// impl and the decision not to include a similar one for `Option<()>`.
|
||||
impl<E> Responder for Result<(), E>
|
||||
where
|
||||
E: Into<Error>,
|
||||
{
|
||||
type Body = BoxBody;
|
||||
|
||||
fn respond_to(self, _req: &HttpRequest) -> HttpResponse {
|
||||
match self {
|
||||
Ok(()) => HttpResponse::new(StatusCode::NO_CONTENT),
|
||||
Err(err) => HttpResponse::from_error(err.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Responder> Responder for (R, StatusCode) {
|
||||
type Body = R::Body;
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::{
|
|||
};
|
||||
|
||||
use actix_router::ResourceDef;
|
||||
use ahash::AHashMap;
|
||||
use foldhash::HashMap as FoldHashMap;
|
||||
use url::Url;
|
||||
|
||||
use crate::{error::UrlGenerationError, request::HttpRequest};
|
||||
|
@ -19,7 +19,7 @@ pub struct ResourceMap {
|
|||
|
||||
/// Named resources within the tree or, for external resources, it points to isolated nodes
|
||||
/// outside the tree.
|
||||
named: AHashMap<String, Rc<ResourceMap>>,
|
||||
named: FoldHashMap<String, Rc<ResourceMap>>,
|
||||
|
||||
parent: RefCell<Weak<ResourceMap>>,
|
||||
|
||||
|
@ -32,7 +32,7 @@ impl ResourceMap {
|
|||
pub fn new(root: ResourceDef) -> Self {
|
||||
ResourceMap {
|
||||
pattern: root,
|
||||
named: AHashMap::default(),
|
||||
named: FoldHashMap::default(),
|
||||
parent: RefCell::new(Weak::new()),
|
||||
nodes: Some(Vec::new()),
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ impl ResourceMap {
|
|||
} else {
|
||||
let new_node = Rc::new(ResourceMap {
|
||||
pattern: pattern.clone(),
|
||||
named: AHashMap::default(),
|
||||
named: FoldHashMap::default(),
|
||||
parent: RefCell::new(Weak::new()),
|
||||
nodes: None,
|
||||
});
|
||||
|
|
|
@ -278,7 +278,9 @@ where
|
|||
{
|
||||
// create and configure default resource
|
||||
self.default = Some(Rc::new(boxed::factory(f.into_factory().map_init_err(
|
||||
|e| log::error!("Can not construct default service: {:?}", e),
|
||||
|err| {
|
||||
log::error!("Can not construct default service: {err:?}");
|
||||
},
|
||||
))));
|
||||
|
||||
self
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use std::{
|
||||
any::Any,
|
||||
cmp, fmt, io,
|
||||
cmp, fmt,
|
||||
future::Future,
|
||||
io,
|
||||
marker::PhantomData,
|
||||
net,
|
||||
sync::{Arc, Mutex},
|
||||
|
@ -64,6 +66,7 @@ struct Config {
|
|||
/// .await
|
||||
/// }
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub struct HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
|
@ -193,7 +196,7 @@ where
|
|||
///
|
||||
/// One thread pool is set up **per worker**; not shared across workers.
|
||||
///
|
||||
/// By default set to 512 divided by the number of workers.
|
||||
/// By default, set to 512 divided by [available parallelism](std::thread::available_parallelism()).
|
||||
pub fn worker_max_blocking_threads(mut self, num: usize) -> Self {
|
||||
self.builder = self.builder.worker_max_blocking_threads(num);
|
||||
self
|
||||
|
@ -272,19 +275,12 @@ where
|
|||
/// - `actix_web::rt::net::TcpStream` when no encryption is used.
|
||||
///
|
||||
/// See the `on_connect` example for additional details.
|
||||
pub fn on_connect<CB>(self, f: CB) -> HttpServer<F, I, S, B>
|
||||
pub fn on_connect<CB>(mut self, f: CB) -> HttpServer<F, I, S, B>
|
||||
where
|
||||
CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static,
|
||||
{
|
||||
HttpServer {
|
||||
factory: self.factory,
|
||||
config: self.config,
|
||||
backlog: self.backlog,
|
||||
sockets: self.sockets,
|
||||
builder: self.builder,
|
||||
on_connect_fn: Some(Arc::new(f)),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
self.on_connect_fn = Some(Arc::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets server host name.
|
||||
|
@ -312,6 +308,37 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Specify shutdown signal from a future.
|
||||
///
|
||||
/// Using this method will prevent OS signal handlers being set up.
|
||||
///
|
||||
/// Typically, a `CancellationToken` will be used, but any future _can_ be.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use actix_web::{App, HttpServer};
|
||||
/// use tokio_util::sync::CancellationToken;
|
||||
///
|
||||
/// # #[actix_web::main]
|
||||
/// # async fn main() -> std::io::Result<()> {
|
||||
/// let stop_signal = CancellationToken::new();
|
||||
///
|
||||
/// HttpServer::new(move || App::new())
|
||||
/// .shutdown_signal(stop_signal.cancelled_owned())
|
||||
/// .bind(("127.0.0.1", 8080))?
|
||||
/// .run()
|
||||
/// .await
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn shutdown_signal<Fut>(mut self, shutdown_signal: Fut) -> Self
|
||||
where
|
||||
Fut: Future<Output = ()> + Send + 'static,
|
||||
{
|
||||
self.builder = self.builder.shutdown_signal(shutdown_signal);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets timeout for graceful worker shutdown of workers.
|
||||
///
|
||||
/// After receiving a stop signal, workers have this much time to finish serving requests.
|
||||
|
@ -882,6 +909,7 @@ where
|
|||
let factory = self.factory.clone();
|
||||
let cfg = Arc::clone(&self.config);
|
||||
let addr = lst.local_addr().unwrap();
|
||||
|
||||
self.sockets.push(Socket {
|
||||
addr,
|
||||
scheme: "https",
|
||||
|
@ -986,6 +1014,7 @@ where
|
|||
let factory = self.factory.clone();
|
||||
let socket_addr =
|
||||
net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)), 8080);
|
||||
|
||||
self.sockets.push(Socket {
|
||||
scheme: "http",
|
||||
addr: socket_addr,
|
||||
|
@ -1073,10 +1102,7 @@ fn bind_addrs(addrs: impl net::ToSocketAddrs, backlog: u32) -> io::Result<Vec<ne
|
|||
} else if let Some(err) = err.take() {
|
||||
Err(err)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Can not bind to address.",
|
||||
))
|
||||
Err(io::Error::other("Could not bind to address"))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1085,7 +1111,10 @@ fn create_tcp_listener(addr: net::SocketAddr, backlog: u32) -> io::Result<net::T
|
|||
use socket2::{Domain, Protocol, Socket, Type};
|
||||
let domain = Domain::for_address(addr);
|
||||
let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))?;
|
||||
socket.set_reuse_address(true)?;
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
socket.set_reuse_address(true)?;
|
||||
}
|
||||
socket.bind(&addr.into())?;
|
||||
// clamp backlog to max u32 that fits in i32 range
|
||||
let backlog = cmp::min(backlog, i32::MAX as u32) as i32;
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue