mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-06 19:00:18 +02:00
Compare commits
64 Commits
web-v4.6.0
...
http-v3.8.
Author | SHA1 | Date | |
---|---|---|---|
4222f92bd3 | |||
d92a73eacd | |||
c612b5ce94 | |||
cbb55ba27d | |||
643d64581a | |||
66905efd7b | |||
c076e34b5d | |||
3ecaff5f5b | |||
fa74ab3dfb | |||
188206a903 | |||
0ce488e57a | |||
132b84d3b1 | |||
cc5030c542 | |||
cd301a6932 | |||
4c4c279938 | |||
0fd85bae2a | |||
9b3de1f1fe | |||
9553e7afff | |||
d9579cf58a | |||
7a2313cc4b | |||
2ee92d778e | |||
59e42c1446 | |||
53086a90a6 | |||
7f529e35b2 | |||
4908fd7dea | |||
a2b9823d9d | |||
da56de4556 | |||
758ae1dac1 | |||
37577dcb89 | |||
8b8eb4eae1 | |||
22593a1532 | |||
f7646bcc48 | |||
8018983a68 | |||
266834cf7c | |||
40e1034566 | |||
a5c78483f9 | |||
12a0521ef8 | |||
b4faf8820c | |||
d6f885127d | |||
ebc43dcf1b | |||
7c4c26d2df | |||
3db7891303 | |||
c366649516 | |||
534cfe1fda | |||
cff958e518 | |||
b9305ff59d | |||
5221c1b194 | |||
4493aa35d0 | |||
8b4d23a69a | |||
8fdf358954 | |||
b2d0196f34 | |||
85655f731d | |||
ebd8bb266d | |||
5c18569b78 | |||
dd84bcb609 | |||
3ce97effa2 | |||
26efa64278 | |||
cc06fd6a5e | |||
1b214bc5f5 | |||
d4bcdf28f2 | |||
4f7b334d80 | |||
fdff3775a8 | |||
b342b8fc82 | |||
804a344565 |
@ -1,10 +0,0 @@
|
||||
[alias]
|
||||
lint = "clippy --workspace --all-targets -- -Dclippy::todo"
|
||||
lint-all = "clippy --workspace --all-features --all-targets -- -Dclippy::todo"
|
||||
|
||||
# lib checking
|
||||
ci-check-min = "hack --workspace check --no-default-features"
|
||||
ci-check-default = "hack --workspace check"
|
||||
ci-check-default-tests = "check --workspace --tests"
|
||||
ci-check-all-feature-powerset="hack --workspace --feature-powerset --skip=__compress,experimental-io-uring check"
|
||||
ci-check-all-feature-powerset-linux="hack --workspace --feature-powerset --skip=__compress check"
|
26
.github/workflows/ci-post-merge.yml
vendored
26
.github/workflows/ci-post-merge.yml
vendored
@ -44,20 +44,20 @@ jobs:
|
||||
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
||||
|
||||
- name: Install Rust (${{ matrix.version.name }})
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.33.22
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
- name: check minimal
|
||||
run: cargo ci-check-min
|
||||
run: just check-min
|
||||
|
||||
- name: check default
|
||||
run: cargo ci-check-default
|
||||
run: just check-default
|
||||
|
||||
- name: tests
|
||||
timeout-minutes: 60
|
||||
@ -76,16 +76,16 @@ jobs:
|
||||
- name: Free Disk Space
|
||||
run: ./scripts/free-disk-space.sh
|
||||
|
||||
- name: Setup mold linker
|
||||
uses: rui314/setup-mold@v1
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
|
||||
- name: Install cargo-hack
|
||||
uses: taiki-e/install-action@v2.33.22
|
||||
- name: Install just, cargo-hack
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
with:
|
||||
tool: cargo-hack
|
||||
tool: just,cargo-hack
|
||||
|
||||
- name: check feature combinations
|
||||
run: cargo ci-check-all-feature-powerset
|
||||
|
||||
- name: check feature combinations
|
||||
run: cargo ci-check-all-feature-powerset-linux
|
||||
- name: Check feature combinations
|
||||
run: just check-feature-combinations
|
||||
|
20
.github/workflows/ci.yml
vendored
20
.github/workflows/ci.yml
vendored
@ -18,7 +18,7 @@ concurrency:
|
||||
jobs:
|
||||
read_msrv:
|
||||
name: Read MSRV
|
||||
uses: actions-rust-lang/msrv/.github/workflows/msrv.yml@main
|
||||
uses: actions-rust-lang/msrv/.github/workflows/msrv.yml@v0.1.0
|
||||
|
||||
build_and_test:
|
||||
needs: read_msrv
|
||||
@ -54,13 +54,17 @@ jobs:
|
||||
echo 'OPENSSL_DIR=C:\Program Files\OpenSSL' >> $GITHUB_ENV
|
||||
echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV
|
||||
|
||||
- name: Setup mold linker
|
||||
if: matrix.target.os == 'ubuntu-latest'
|
||||
uses: rui314/setup-mold@v1
|
||||
|
||||
- name: Install Rust (${{ matrix.version.name }})
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.33.22
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
@ -69,10 +73,10 @@ jobs:
|
||||
run: just downgrade-for-msrv
|
||||
|
||||
- name: check minimal
|
||||
run: cargo ci-check-min
|
||||
run: just check-min
|
||||
|
||||
- name: check default
|
||||
run: cargo ci-check-default
|
||||
run: just check-default
|
||||
|
||||
- name: tests
|
||||
timeout-minutes: 60
|
||||
@ -88,7 +92,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
|
||||
@ -104,12 +108,12 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.33.22
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
with:
|
||||
tool: just
|
||||
|
||||
|
17
.github/workflows/coverage.yml
vendored
17
.github/workflows/coverage.yml
vendored
@ -17,21 +17,22 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
components: llvm-tools-preview
|
||||
toolchain: nightly
|
||||
components: llvm-tools
|
||||
|
||||
- name: Install cargo-llvm-cov
|
||||
uses: taiki-e/install-action@v2.33.22
|
||||
- name: Install just, cargo-llvm-cov, cargo-nextest
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
with:
|
||||
tool: cargo-llvm-cov
|
||||
tool: just,cargo-llvm-cov,cargo-nextest
|
||||
|
||||
- name: Generate code coverage
|
||||
run: cargo llvm-cov --workspace --all-features --codecov --output-path codecov.json
|
||||
run: just test-coverage-codecov
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v4.3.1
|
||||
uses: codecov/codecov-action@v4.5.0
|
||||
with:
|
||||
files: codecov.json
|
||||
fail_ci_if_error: true
|
||||
|
39
.github/workflows/lint.yml
vendored
39
.github/workflows/lint.yml
vendored
@ -18,7 +18,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust (nightly)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.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.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.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.8.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
components: rust-docs
|
||||
@ -65,6 +65,29 @@ jobs:
|
||||
RUSTDOCFLAGS: -D warnings
|
||||
run: cargo +nightly doc --no-deps --workspace --all-features
|
||||
|
||||
check-external-types:
|
||||
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
|
||||
with:
|
||||
toolchain: nightly-2024-05-01
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
with:
|
||||
tool: just
|
||||
|
||||
- name: Install cargo-check-external-types
|
||||
uses: taiki-e/cache-cargo-install-action@v2.0.1
|
||||
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:
|
||||
@ -76,18 +99,18 @@ jobs:
|
||||
- name: Checkout PR branch
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
- name: Install Rust (nightly-2024-06-07)
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
with:
|
||||
toolchain: nightly-2024-04-26
|
||||
toolchain: nightly-2024-06-07
|
||||
|
||||
- name: Install cargo-public-api
|
||||
uses: taiki-e/install-action@v2.33.22
|
||||
uses: taiki-e/install-action@v2.39.1
|
||||
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" diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
|
||||
cargo public-api --manifest-path "$f" --simplified diff ${{ github.event.pull_request.base.sha }}..${{ github.sha }}
|
||||
done
|
||||
|
41
.github/workflows/upload-doc.yml
vendored
41
.github/workflows/upload-doc.yml
vendored
@ -1,41 +0,0 @@
|
||||
name: Upload Documentation
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
|
||||
with:
|
||||
toolchain: nightly
|
||||
|
||||
- name: Build Docs
|
||||
run: cargo +nightly doc --no-deps --workspace --all-features
|
||||
env:
|
||||
RUSTDOCFLAGS: --cfg=docsrs
|
||||
|
||||
- name: Tweak HTML
|
||||
run: echo '<meta http-equiv="refresh" content="0;url=actix_web/index.html">' > target/doc/index.html
|
||||
|
||||
- name: Deploy to GitHub Pages
|
||||
uses: JamesIves/github-pages-deploy-action@v4.6.0
|
||||
with:
|
||||
folder: target/doc
|
||||
single-commit: true
|
@ -2,6 +2,9 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 0.6.6
|
||||
|
||||
- Update `tokio-uring` dependency to `0.4`.
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
|
||||
## 0.6.5
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-files"
|
||||
version = "0.6.5"
|
||||
version = "0.6.6"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
@ -13,9 +13,14 @@ categories = ["asynchronous", "web-programming::http-server"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "actix_files"
|
||||
path = "src/lib.rs"
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_http::*",
|
||||
"actix_service::*",
|
||||
"actix_web::*",
|
||||
"http::*",
|
||||
"mime::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
experimental-io-uring = ["actix-web/experimental-io-uring", "tokio-uring"]
|
||||
@ -40,8 +45,8 @@ v_htmlescape = "0.15.5"
|
||||
|
||||
# experimental-io-uring
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
tokio-uring = { version = "0.4", optional = true, features = ["bytes"] }
|
||||
actix-server = { version = "2.2", optional = true } # ensure matching tokio-uring versions
|
||||
tokio-uring = { version = "0.5", optional = true, features = ["bytes"] }
|
||||
actix-server = { version = "2.4", optional = true } # ensure matching tokio-uring versions
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.7"
|
||||
|
@ -3,11 +3,11 @@
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-files)
|
||||
[](https://docs.rs/actix-files/0.6.5)
|
||||
[](https://docs.rs/actix-files/0.6.6)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-files/0.6.5)
|
||||
[](https://deps.rs/crate/actix-files/0.6.6)
|
||||
[](https://crates.io/crates/actix-files)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -18,9 +18,17 @@ edition = "2021"
|
||||
[package.metadata.docs.rs]
|
||||
features = []
|
||||
|
||||
[lib]
|
||||
name = "actix_http_test"
|
||||
path = "src/lib.rs"
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_codec::*",
|
||||
"actix_http::*",
|
||||
"actix_server::*",
|
||||
"awc::*",
|
||||
"bytes::*",
|
||||
"futures_core::*",
|
||||
"http::*",
|
||||
"tokio::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
@ -1,7 +1,5 @@
|
||||
# `actix-http-test`
|
||||
|
||||
> Various helpers for Actix applications to use during testing.
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
@ -14,3 +12,9 @@
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
<!-- cargo-rdme start -->
|
||||
|
||||
Various helpers for Actix applications to use during testing.
|
||||
|
||||
<!-- cargo-rdme end -->
|
||||
|
@ -2,6 +2,12 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 3.8.0
|
||||
|
||||
### Added
|
||||
|
||||
- Add `error::InvalidStatusCode` re-export.
|
||||
|
||||
## 3.7.0
|
||||
|
||||
### Added
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-http"
|
||||
version = "3.7.0"
|
||||
version = "3.8.0"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
@ -34,51 +34,72 @@ features = [
|
||||
"compress-zstd",
|
||||
]
|
||||
|
||||
[lib]
|
||||
name = "actix_http"
|
||||
path = "src/lib.rs"
|
||||
[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::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
# HTTP/2 protocol support
|
||||
http2 = ["h2"]
|
||||
http2 = ["dep:h2"]
|
||||
|
||||
# WebSocket protocol implementation
|
||||
ws = [
|
||||
"local-channel",
|
||||
"base64",
|
||||
"rand",
|
||||
"sha1",
|
||||
"dep:local-channel",
|
||||
"dep:base64",
|
||||
"dep:rand",
|
||||
"dep:sha1",
|
||||
]
|
||||
|
||||
# TLS via OpenSSL
|
||||
openssl = ["actix-tls/accept", "actix-tls/openssl"]
|
||||
openssl = ["__tls", "actix-tls/accept", "actix-tls/openssl"]
|
||||
|
||||
# TLS via Rustls v0.20
|
||||
rustls = ["rustls-0_20"]
|
||||
rustls = ["__tls", "rustls-0_20"]
|
||||
|
||||
# TLS via Rustls v0.20
|
||||
rustls-0_20 = ["actix-tls/accept", "actix-tls/rustls-0_20"]
|
||||
rustls-0_20 = ["__tls", "actix-tls/accept", "actix-tls/rustls-0_20"]
|
||||
|
||||
# TLS via Rustls v0.21
|
||||
rustls-0_21 = ["actix-tls/accept", "actix-tls/rustls-0_21"]
|
||||
rustls-0_21 = ["__tls", "actix-tls/accept", "actix-tls/rustls-0_21"]
|
||||
|
||||
# TLS via Rustls v0.22
|
||||
rustls-0_22 = ["actix-tls/accept", "actix-tls/rustls-0_22"]
|
||||
rustls-0_22 = ["__tls", "actix-tls/accept", "actix-tls/rustls-0_22"]
|
||||
|
||||
# TLS via Rustls v0.23
|
||||
rustls-0_23 = ["actix-tls/accept", "actix-tls/rustls-0_23"]
|
||||
rustls-0_23 = ["__tls", "actix-tls/accept", "actix-tls/rustls-0_23"]
|
||||
|
||||
# Compression codecs
|
||||
compress-brotli = ["__compress", "brotli"]
|
||||
compress-gzip = ["__compress", "flate2"]
|
||||
compress-zstd = ["__compress", "zstd"]
|
||||
compress-brotli = ["__compress", "dep:brotli"]
|
||||
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.
|
||||
__compress = []
|
||||
|
||||
# Internal (PRIVATE!) features used to aid checking feature status.
|
||||
# Don't rely on these whatsoever. They may disappear at anytime.
|
||||
__tls = []
|
||||
|
||||
[dependencies]
|
||||
actix-service = "2"
|
||||
actix-codec = "0.5"
|
||||
@ -106,7 +127,7 @@ tokio-util = { version = "0.7", features = ["io", "codec"] }
|
||||
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
||||
|
||||
# http2
|
||||
h2 = { version = "0.3.24", optional = true }
|
||||
h2 = { version = "0.3.26", optional = true }
|
||||
|
||||
# websockets
|
||||
local-channel = { version = "0.1", optional = true }
|
||||
|
@ -5,11 +5,11 @@
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://docs.rs/actix-http/3.7.0)
|
||||
[](https://docs.rs/actix-http/3.8.0)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-http/3.7.0)
|
||||
[](https://deps.rs/crate/actix-http/3.8.0)
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Error};
|
||||
|
||||
use derive_more::{Display, Error, From};
|
||||
pub use http::Error as HttpError;
|
||||
pub use http::{status::InvalidStatusCode, Error as HttpError};
|
||||
use http::{uri::InvalidUri, StatusCode};
|
||||
|
||||
use crate::{body::BoxBody, Response};
|
||||
|
@ -6,10 +6,10 @@
|
||||
//! | ------------------- | ------------------------------------------- |
|
||||
//! | `http2` | HTTP/2 support via [h2]. |
|
||||
//! | `openssl` | TLS support via [OpenSSL]. |
|
||||
//! | `rustls` | TLS support via [rustls] 0.20. |
|
||||
//! | `rustls-0_21` | TLS support via [rustls] 0.21. |
|
||||
//! | `rustls-0_22` | TLS support via [rustls] 0.22. |
|
||||
//! | `rustls-0_23` | TLS support via [rustls] 0.23. |
|
||||
//! | `rustls-0_20` | TLS support via rustls 0.20. |
|
||||
//! | `rustls-0_21` | TLS support via rustls 0.21. |
|
||||
//! | `rustls-0_22` | TLS support via rustls 0.22. |
|
||||
//! | `rustls-0_23` | TLS support via [rustls] 0.23. |
|
||||
//! | `compress-brotli` | Payload compression support: Brotli. |
|
||||
//! | `compress-gzip` | Payload compression support: Deflate, Gzip. |
|
||||
//! | `compress-zstd` | Payload compression support: Zstd. |
|
||||
@ -61,13 +61,7 @@ pub mod ws;
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use self::payload::PayloadStream;
|
||||
#[cfg(any(
|
||||
feature = "openssl",
|
||||
feature = "rustls-0_20",
|
||||
feature = "rustls-0_21",
|
||||
feature = "rustls-0_22",
|
||||
feature = "rustls-0_23",
|
||||
))]
|
||||
#[cfg(feature = "__tls")]
|
||||
pub use self::service::TlsAcceptorConfig;
|
||||
pub use self::{
|
||||
builder::HttpServiceBuilder,
|
||||
|
@ -241,25 +241,13 @@ where
|
||||
}
|
||||
|
||||
/// Configuration options used when accepting TLS connection.
|
||||
#[cfg(any(
|
||||
feature = "openssl",
|
||||
feature = "rustls-0_20",
|
||||
feature = "rustls-0_21",
|
||||
feature = "rustls-0_22",
|
||||
feature = "rustls-0_23",
|
||||
))]
|
||||
#[cfg(feature = "__tls")]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TlsAcceptorConfig {
|
||||
pub(crate) handshake_timeout: Option<std::time::Duration>,
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
feature = "openssl",
|
||||
feature = "rustls-0_20",
|
||||
feature = "rustls-0_21",
|
||||
feature = "rustls-0_22",
|
||||
feature = "rustls-0_23",
|
||||
))]
|
||||
#[cfg(feature = "__tls")]
|
||||
impl TlsAcceptorConfig {
|
||||
/// Set TLS handshake timeout duration.
|
||||
pub fn handshake_timeout(self, dur: std::time::Duration) -> Self {
|
||||
|
@ -178,14 +178,14 @@ impl Parser {
|
||||
};
|
||||
|
||||
if payload_len < 126 {
|
||||
dst.reserve(p_len + 2 + if mask { 4 } else { 0 });
|
||||
dst.reserve(p_len + 2);
|
||||
dst.put_slice(&[one, two | payload_len as u8]);
|
||||
} else if payload_len <= 65_535 {
|
||||
dst.reserve(p_len + 4 + if mask { 4 } else { 0 });
|
||||
dst.reserve(p_len + 4);
|
||||
dst.put_slice(&[one, two | 126]);
|
||||
dst.put_u16(payload_len as u16);
|
||||
} else {
|
||||
dst.reserve(p_len + 10 + if mask { 4 } else { 0 });
|
||||
dst.reserve(p_len + 10);
|
||||
dst.put_slice(&[one, two | 127]);
|
||||
dst.put_u64(payload_len as u64);
|
||||
};
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 0.6.2
|
||||
|
||||
- Add testing utilities under new module `test`.
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-multipart"
|
||||
version = "0.6.1"
|
||||
version = "0.6.2"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Jacob Halsey <jacob@jhalsey.com>",
|
||||
@ -16,6 +16,21 @@ edition = "2021"
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
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::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = ["tempfile", "derive"]
|
||||
derive = ["actix-multipart-derive"]
|
||||
|
@ -1,33 +1,23 @@
|
||||
# `actix-multipart`
|
||||
|
||||
> Multipart form support for Actix Web.
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://docs.rs/actix-multipart/0.6.1)
|
||||
[](https://docs.rs/actix-multipart/0.6.2)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-multipart/0.6.1)
|
||||
[](https://deps.rs/crate/actix-multipart/0.6.2)
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
<!-- cargo-rdme start -->
|
||||
|
||||
## Example
|
||||
Multipart form support for Actix Web.
|
||||
|
||||
Dependencies:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
actix-multipart = "0.6"
|
||||
actix-web = "4.5"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
```
|
||||
|
||||
Code:
|
||||
## Examples
|
||||
|
||||
```rust
|
||||
use actix_web::{post, App, HttpServer, Responder};
|
||||
@ -64,15 +54,15 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
```
|
||||
|
||||
<!-- cargo-rdme end -->
|
||||
|
||||
[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart)
|
||||
|
||||
Curl request :
|
||||
|
||||
```bash
|
||||
curl -v --request POST \
|
||||
--url http://localhost:8080/videos \
|
||||
-F 'json={"name": "Cargo.lock"};type=application/json' \
|
||||
-F file=@./Cargo.lock
|
||||
```
|
||||
|
||||
|
||||
### Examples
|
||||
|
||||
https://github.com/actix/examples/tree/master/forms/multipart
|
@ -33,6 +33,14 @@ pub trait FieldReader<'t>: Sized + Any {
|
||||
type Future: Future<Output = Result<Self, MultipartError>>;
|
||||
|
||||
/// The form will call this function to handle the field.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When reading the `field` payload using its `Stream` implementation, polling (manually or via
|
||||
/// `next()`/`try_next()`) may panic after the payload is exhausted. If this is a problem for
|
||||
/// your implementation of this method, you should [`fuse()`] the `Field` first.
|
||||
///
|
||||
/// [`fuse()`]: futures_util::stream::StreamExt::fuse()
|
||||
fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future;
|
||||
}
|
||||
|
||||
@ -396,11 +404,20 @@ mod tests {
|
||||
use actix_http::encoding::Decoder;
|
||||
use actix_multipart_rfc7578::client::multipart;
|
||||
use actix_test::TestServer;
|
||||
use actix_web::{dev::Payload, http::StatusCode, web, App, HttpResponse, Responder};
|
||||
use actix_web::{
|
||||
dev::Payload, http::StatusCode, web, App, HttpRequest, HttpResponse, Resource, Responder,
|
||||
};
|
||||
use awc::{Client, ClientResponse};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use futures_util::TryStreamExt as _;
|
||||
|
||||
use super::MultipartForm;
|
||||
use crate::form::{bytes::Bytes, tempfile::TempFile, text::Text, MultipartFormConfig};
|
||||
use crate::{
|
||||
form::{
|
||||
bytes::Bytes, tempfile::TempFile, text::Text, FieldReader, Limits, MultipartFormConfig,
|
||||
},
|
||||
Field, MultipartError,
|
||||
};
|
||||
|
||||
pub async fn send_form(
|
||||
srv: &TestServer,
|
||||
@ -734,4 +751,49 @@ mod tests {
|
||||
let response = send_form(&srv, form, "/").await;
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
#[should_panic(expected = "called `Result::unwrap()` on an `Err` value: Connect(Disconnected)")]
|
||||
#[actix_web::test]
|
||||
async fn field_try_next_panic() {
|
||||
#[derive(Debug)]
|
||||
struct NullSink;
|
||||
|
||||
impl<'t> FieldReader<'t> for NullSink {
|
||||
type Future = LocalBoxFuture<'t, Result<Self, MultipartError>>;
|
||||
|
||||
fn read_field(
|
||||
_: &'t HttpRequest,
|
||||
mut field: Field,
|
||||
_limits: &'t mut Limits,
|
||||
) -> Self::Future {
|
||||
Box::pin(async move {
|
||||
// exhaust field stream
|
||||
while let Some(_chunk) = field.try_next().await? {}
|
||||
|
||||
// poll again, crash
|
||||
let _post = field.try_next().await;
|
||||
|
||||
Ok(Self)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(MultipartForm)]
|
||||
struct NullSinkForm {
|
||||
foo: NullSink,
|
||||
}
|
||||
|
||||
async fn null_sink(_form: MultipartForm<NullSinkForm>) -> impl Responder {
|
||||
"unreachable"
|
||||
}
|
||||
|
||||
let srv = actix_test::start(|| App::new().service(Resource::new("/").post(null_sink)));
|
||||
|
||||
let mut form = multipart::Form::default();
|
||||
form.add_text("foo", "data is not important to this test");
|
||||
|
||||
// panics with Err(Connect(Disconnected)) due to form NullSink panic
|
||||
let _res = send_form(&srv, form, "/").await;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
//! Multipart form support for Actix Web.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use actix_web::{post, App, HttpServer, Responder};
|
||||
//!
|
||||
|
@ -465,7 +465,12 @@ impl Stream for Field {
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let this = self.get_mut();
|
||||
let mut inner = this.inner.borrow_mut();
|
||||
if let Some(mut buffer) = inner.payload.as_ref().unwrap().get_mut(&this.safety) {
|
||||
if let Some(mut buffer) = inner
|
||||
.payload
|
||||
.as_ref()
|
||||
.expect("Field should not be polled after completion")
|
||||
.get_mut(&this.safety)
|
||||
{
|
||||
// check safety and poll read payload to buffer.
|
||||
buffer.poll_stream(cx)?;
|
||||
} else if !this.safety.is_clean() {
|
||||
@ -496,6 +501,7 @@ impl fmt::Debug for Field {
|
||||
}
|
||||
|
||||
struct InnerField {
|
||||
/// Payload is initialized as Some and is `take`n when the field stream finishes.
|
||||
payload: Option<PayloadRef>,
|
||||
boundary: String,
|
||||
eof: bool,
|
||||
@ -643,7 +649,12 @@ impl InnerField {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
|
||||
let result = if let Some(mut payload) = self.payload.as_ref().unwrap().get_mut(s) {
|
||||
let result = if let Some(mut payload) = self
|
||||
.payload
|
||||
.as_ref()
|
||||
.expect("Field should not be polled after completion")
|
||||
.get_mut(s)
|
||||
{
|
||||
if !self.eof {
|
||||
let res = if let Some(ref mut len) = self.length {
|
||||
InnerField::read_len(&mut payload, len)
|
||||
@ -674,8 +685,10 @@ impl InnerField {
|
||||
};
|
||||
|
||||
if let Poll::Ready(None) = result {
|
||||
self.payload.take();
|
||||
// drop payload buffer and make future un-poll-able
|
||||
let _ = self.payload.take();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 0.5.3
|
||||
|
||||
- Add `unicode` crate feature (on-by-default) to switch between `regex` and `regex-lite` as a trade-off between full unicode support and binary size.
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-router"
|
||||
version = "0.5.2"
|
||||
version = "0.5.3"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>",
|
||||
@ -12,9 +12,11 @@ repository = "https://github.com/actix/actix-web"
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "actix_router"
|
||||
path = "src/lib.rs"
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"http::*",
|
||||
"serde::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = ["http", "unicode"]
|
||||
|
@ -3,11 +3,11 @@
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-router)
|
||||
[](https://docs.rs/actix-router/0.5.2)
|
||||
[](https://docs.rs/actix-router/0.5.3)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-router/0.5.2)
|
||||
[](https://deps.rs/crate/actix-router/0.5.3)
|
||||
[](https://crates.io/crates/actix-router)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -2,9 +2,16 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 0.1.5
|
||||
|
||||
- Add `TestServerConfig::listen_address()` method.
|
||||
|
||||
## 0.1.4
|
||||
|
||||
- Add `TestServerConfig::rustls_0_23()` method for Rustls v0.23 support behind new `rustls-0_23` crate feature.
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
- Add `TestServerConfig::disable_redirects()` method.
|
||||
- Various types from `awc`, such as `ClientRequest` and `ClientResponse`, are now re-exported.
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
|
||||
## 0.1.3
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-test"
|
||||
version = "0.1.3"
|
||||
version = "0.1.5"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
@ -18,6 +18,22 @@ categories = [
|
||||
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::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
|
45
actix-test/README.md
Normal file
45
actix-test/README.md
Normal file
@ -0,0 +1,45 @@
|
||||
# `actix-test`
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-test)
|
||||
[](https://docs.rs/actix-test/0.1.5)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-test/0.1.5)
|
||||
[](https://crates.io/crates/actix-test)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
<!-- cargo-rdme start -->
|
||||
|
||||
Integration testing tools for Actix Web applications.
|
||||
|
||||
The main integration testing tool is [`TestServer`]. It spawns a real HTTP server on an unused port and provides methods that use a real HTTP client. Therefore, it is much closer to real-world cases than using `init_service`, which skips HTTP encoding and decoding.
|
||||
|
||||
## Examples
|
||||
|
||||
```rust
|
||||
use actix_web::{get, web, test, App, HttpResponse, Error, Responder};
|
||||
|
||||
#[get("/")]
|
||||
async fn my_handler() -> Result<impl Responder, Error> {
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_example() {
|
||||
let srv = actix_test::start(||
|
||||
App::new().service(my_handler)
|
||||
);
|
||||
|
||||
let req = srv.get("/");
|
||||
let res = req.send().await.unwrap();
|
||||
|
||||
assert!(res.status().is_success());
|
||||
}
|
||||
```
|
||||
|
||||
<!-- cargo-rdme end -->
|
@ -5,6 +5,7 @@
|
||||
//! real-world cases than using `init_service`, which skips HTTP encoding and decoding.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! use actix_web::{get, web, test, App, HttpResponse, Error, Responder};
|
||||
//!
|
||||
@ -149,10 +150,12 @@ where
|
||||
StreamType::Rustls023(_) => true,
|
||||
};
|
||||
|
||||
let client_cfg = cfg.clone();
|
||||
|
||||
// run server in separate orphaned thread
|
||||
thread::spawn(move || {
|
||||
rt::System::new().block_on(async move {
|
||||
let tcp = net::TcpListener::bind(("127.0.0.1", cfg.port)).unwrap();
|
||||
let tcp = net::TcpListener::bind((cfg.listen_address.clone(), cfg.port)).unwrap();
|
||||
let local_addr = tcp.local_addr().unwrap();
|
||||
let factory = factory.clone();
|
||||
let srv_cfg = cfg.clone();
|
||||
@ -460,7 +463,13 @@ where
|
||||
}
|
||||
};
|
||||
|
||||
Client::builder().connector(connector).finish()
|
||||
let mut client_builder = Client::builder().connector(connector);
|
||||
|
||||
if client_cfg.disable_redirects {
|
||||
client_builder = client_builder.disable_redirects();
|
||||
}
|
||||
|
||||
client_builder.finish()
|
||||
};
|
||||
|
||||
TestServer {
|
||||
@ -480,6 +489,7 @@ enum HttpVer {
|
||||
Both,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Clone)]
|
||||
enum StreamType {
|
||||
Tcp,
|
||||
@ -505,8 +515,10 @@ pub struct TestServerConfig {
|
||||
tp: HttpVer,
|
||||
stream: StreamType,
|
||||
client_request_timeout: Duration,
|
||||
listen_address: String,
|
||||
port: u16,
|
||||
workers: usize,
|
||||
disable_redirects: bool,
|
||||
}
|
||||
|
||||
impl Default for TestServerConfig {
|
||||
@ -522,8 +534,10 @@ impl TestServerConfig {
|
||||
tp: HttpVer::Both,
|
||||
stream: StreamType::Tcp,
|
||||
client_request_timeout: Duration::from_secs(5),
|
||||
listen_address: "127.0.0.1".to_string(),
|
||||
port: 0,
|
||||
workers: 1,
|
||||
disable_redirects: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -596,6 +610,14 @@ impl TestServerConfig {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the address the server will listen on.
|
||||
///
|
||||
/// By default, only listens on `127.0.0.1`.
|
||||
pub fn listen_address(mut self, addr: impl Into<String>) -> Self {
|
||||
self.listen_address = addr.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets test server port.
|
||||
///
|
||||
/// By default, a random free port is determined by the OS.
|
||||
@ -611,6 +633,15 @@ impl TestServerConfig {
|
||||
self.workers = workers;
|
||||
self
|
||||
}
|
||||
|
||||
/// Instruct the client to not follow redirects.
|
||||
///
|
||||
/// By default, the client will follow up to 10 consecutive redirects
|
||||
/// before giving up.
|
||||
pub fn disable_redirects(mut self) -> Self {
|
||||
self.disable_redirects = true;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A basic HTTP server controller that simplifies the process of writing integration tests for
|
||||
@ -637,9 +668,9 @@ impl TestServer {
|
||||
let scheme = if self.tls { "https" } else { "http" };
|
||||
|
||||
if uri.starts_with('/') {
|
||||
format!("{}://localhost:{}{}", scheme, self.addr.port(), uri)
|
||||
format!("{}://{}{}", scheme, self.addr, uri)
|
||||
} else {
|
||||
format!("{}://localhost:{}/{}", scheme, self.addr.port(), uri)
|
||||
format!("{}://{}/{}", scheme, self.addr, uri)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Take the encoded buffer when yielding bytes in the response stream rather than splitting the buffer, reducing memory use
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
|
||||
## 4.3.0
|
||||
|
@ -9,9 +9,15 @@ repository = "https://github.com/actix/actix-web"
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "actix_web_actors"
|
||||
path = "src/lib.rs"
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix::*",
|
||||
"actix_http::*",
|
||||
"actix_web::*",
|
||||
"bytes::*",
|
||||
"bytestring::*",
|
||||
"futures_core::*",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
actix = { version = ">=0.12, <0.14", default-features = false }
|
||||
|
@ -710,7 +710,7 @@ where
|
||||
}
|
||||
|
||||
if !this.buf.is_empty() {
|
||||
Poll::Ready(Some(Ok(this.buf.split().freeze())))
|
||||
Poll::Ready(Some(Ok(std::mem::take(&mut this.buf).freeze())))
|
||||
} else if this.fut.alive() && !this.closed {
|
||||
Poll::Pending
|
||||
} else {
|
||||
|
@ -2,6 +2,11 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 4.3.0
|
||||
|
||||
- Add `#[scope]` macro.
|
||||
- Add `compat-routing-macros-force-pub` crate feature which, on-by-default, which when disabled causes handlers to inherit their attached function's visibility.
|
||||
- Prevent inclusion of default `actix-router` features.
|
||||
- Minimum supported Rust version (MSRV) is now 1.72.
|
||||
|
||||
## 4.2.2
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web-codegen"
|
||||
version = "4.2.2"
|
||||
version = "4.3.0"
|
||||
description = "Routing and runtime macros for Actix Web"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
@ -15,8 +15,12 @@ rust-version.workspace = true
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[features]
|
||||
default = ["compat-routing-macros-force-pub"]
|
||||
compat-routing-macros-force-pub = []
|
||||
|
||||
[dependencies]
|
||||
actix-router = "0.5"
|
||||
actix-router = { version = "0.5", default-features = false }
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "2", features = ["full", "extra-traits"] }
|
||||
|
@ -5,11 +5,11 @@
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-web-codegen)
|
||||
[](https://docs.rs/actix-web-codegen/4.2.2)
|
||||
[](https://docs.rs/actix-web-codegen/4.3.0)
|
||||

|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-web-codegen/4.2.2)
|
||||
[](https://deps.rs/crate/actix-web-codegen/4.3.0)
|
||||
[](https://crates.io/crates/actix-web-codegen)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -83,6 +83,7 @@ use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
mod route;
|
||||
mod scope;
|
||||
|
||||
/// Creates resource handler, allowing multiple HTTP method guards.
|
||||
///
|
||||
@ -197,6 +198,43 @@ method_macro!(Options, options);
|
||||
method_macro!(Trace, trace);
|
||||
method_macro!(Patch, patch);
|
||||
|
||||
/// Prepends a path prefix to all handlers using routing macros inside the attached module.
|
||||
///
|
||||
/// # Syntax
|
||||
///
|
||||
/// ```
|
||||
/// # use actix_web_codegen::scope;
|
||||
/// #[scope("/prefix")]
|
||||
/// mod api {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// - `"/prefix"` - Raw literal string to be prefixed onto contained handlers' paths.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use actix_web_codegen::{scope, get};
|
||||
/// # use actix_web::Responder;
|
||||
/// #[scope("/api")]
|
||||
/// mod api {
|
||||
/// # use super::*;
|
||||
/// #[get("/hello")]
|
||||
/// pub async fn hello() -> impl Responder {
|
||||
/// // this has path /api/hello
|
||||
/// "Hello, world!"
|
||||
/// }
|
||||
/// }
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn scope(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
scope::with_scope(args, input)
|
||||
}
|
||||
|
||||
/// Marks async main function as the Actix Web system entry-point.
|
||||
///
|
||||
/// Note that Actix Web also works under `#[tokio::main]` since version 4.0. However, this macro is
|
||||
@ -240,3 +278,15 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||
output.extend(item);
|
||||
output
|
||||
}
|
||||
|
||||
/// Converts the error to a token stream and appends it to the original input.
|
||||
///
|
||||
/// Returning the original input in addition to the error is good for IDEs which can gracefully
|
||||
/// recover and show more precise errors within the macro body.
|
||||
///
|
||||
/// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info.
|
||||
fn input_and_compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream {
|
||||
let compile_err = TokenStream::from(err.to_compile_error());
|
||||
item.extend(compile_err);
|
||||
item
|
||||
}
|
||||
|
@ -6,10 +6,12 @@ use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||
use quote::{quote, ToTokens, TokenStreamExt};
|
||||
use syn::{punctuated::Punctuated, Ident, LitStr, Path, Token};
|
||||
|
||||
use crate::input_and_compile_error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RouteArgs {
|
||||
path: syn::LitStr,
|
||||
options: Punctuated<syn::MetaNameValue, Token![,]>,
|
||||
pub(crate) path: syn::LitStr,
|
||||
pub(crate) options: Punctuated<syn::MetaNameValue, Token![,]>,
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for RouteArgs {
|
||||
@ -78,7 +80,7 @@ macro_rules! standard_method_type {
|
||||
}
|
||||
}
|
||||
|
||||
fn from_path(method: &Path) -> Result<Self, ()> {
|
||||
pub(crate) fn from_path(method: &Path) -> Result<Self, ()> {
|
||||
match () {
|
||||
$(_ if method.is_ident(stringify!($lower)) => Ok(Self::$variant),)+
|
||||
_ => Err(()),
|
||||
@ -411,6 +413,13 @@ impl ToTokens for Route {
|
||||
doc_attributes,
|
||||
} = self;
|
||||
|
||||
#[allow(unused_variables)] // used when force-pub feature is disabled
|
||||
let vis = &ast.vis;
|
||||
|
||||
// TODO(breaking): remove this force-pub forwards-compatibility feature
|
||||
#[cfg(feature = "compat-routing-macros-force-pub")]
|
||||
let vis = syn::Visibility::Public(<Token![pub]>::default());
|
||||
|
||||
let registrations: TokenStream2 = args
|
||||
.iter()
|
||||
.map(|args| {
|
||||
@ -458,7 +467,7 @@ impl ToTokens for Route {
|
||||
let stream = quote! {
|
||||
#(#doc_attributes)*
|
||||
#[allow(non_camel_case_types, missing_docs)]
|
||||
pub struct #name;
|
||||
#vis struct #name;
|
||||
|
||||
impl ::actix_web::dev::HttpServiceFactory for #name {
|
||||
fn register(self, __config: &mut actix_web::dev::AppService) {
|
||||
@ -542,15 +551,3 @@ pub(crate) fn with_methods(input: TokenStream) -> TokenStream {
|
||||
Err(err) => input_and_compile_error(input, err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the error to a token stream and appends it to the original input.
|
||||
///
|
||||
/// Returning the original input in addition to the error is good for IDEs which can gracefully
|
||||
/// recover and show more precise errors within the macro body.
|
||||
///
|
||||
/// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info.
|
||||
fn input_and_compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream {
|
||||
let compile_err = TokenStream::from(err.to_compile_error());
|
||||
item.extend(compile_err);
|
||||
item
|
||||
}
|
||||
|
103
actix-web-codegen/src/scope.rs
Normal file
103
actix-web-codegen/src/scope.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||
use quote::{quote, ToTokens as _};
|
||||
|
||||
use crate::{
|
||||
input_and_compile_error,
|
||||
route::{MethodType, RouteArgs},
|
||||
};
|
||||
|
||||
pub fn with_scope(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
match with_scope_inner(args, input.clone()) {
|
||||
Ok(stream) => stream,
|
||||
Err(err) => input_and_compile_error(input, err),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_scope_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
|
||||
if args.is_empty() {
|
||||
return Err(syn::Error::new(
|
||||
Span::call_site(),
|
||||
"missing arguments for scope macro, expected: #[scope(\"/prefix\")]",
|
||||
));
|
||||
}
|
||||
|
||||
let scope_prefix = syn::parse::<syn::LitStr>(args.clone()).map_err(|err| {
|
||||
syn::Error::new(
|
||||
err.span(),
|
||||
"argument to scope macro is not a string literal, expected: #[scope(\"/prefix\")]",
|
||||
)
|
||||
})?;
|
||||
|
||||
let scope_prefix_value = scope_prefix.value();
|
||||
|
||||
if scope_prefix_value.ends_with('/') {
|
||||
// trailing slashes cause non-obvious problems
|
||||
// it's better to point them out to developers rather than
|
||||
|
||||
return Err(syn::Error::new(
|
||||
scope_prefix.span(),
|
||||
"scopes should not have trailing slashes; see https://docs.rs/actix-web/4/actix_web/struct.Scope.html#avoid-trailing-slashes",
|
||||
));
|
||||
}
|
||||
|
||||
let mut module = syn::parse::<syn::ItemMod>(input).map_err(|err| {
|
||||
syn::Error::new(err.span(), "#[scope] macro must be attached to a module")
|
||||
})?;
|
||||
|
||||
// modify any routing macros (method or route[s]) attached to
|
||||
// functions by prefixing them with this scope macro's argument
|
||||
if let Some((_, items)) = &mut module.content {
|
||||
for item in items {
|
||||
if let syn::Item::Fn(fun) = item {
|
||||
fun.attrs = fun
|
||||
.attrs
|
||||
.iter()
|
||||
.map(|attr| modify_attribute_with_scope(attr, &scope_prefix_value))
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(module.to_token_stream().into())
|
||||
}
|
||||
|
||||
/// Checks if the attribute is a method type and has a route path, then modifies it.
|
||||
fn modify_attribute_with_scope(attr: &syn::Attribute, scope_path: &str) -> syn::Attribute {
|
||||
match (attr.parse_args::<RouteArgs>(), attr.clone().meta) {
|
||||
(Ok(route_args), syn::Meta::List(meta_list)) if has_allowed_methods_in_scope(attr) => {
|
||||
let modified_path = format!("{}{}", scope_path, route_args.path.value());
|
||||
|
||||
let options_tokens: Vec<TokenStream2> = route_args
|
||||
.options
|
||||
.iter()
|
||||
.map(|option| {
|
||||
quote! { ,#option }
|
||||
})
|
||||
.collect();
|
||||
|
||||
let combined_options_tokens: TokenStream2 =
|
||||
options_tokens
|
||||
.into_iter()
|
||||
.fold(TokenStream2::new(), |mut acc, ts| {
|
||||
acc.extend(std::iter::once(ts));
|
||||
acc
|
||||
});
|
||||
|
||||
syn::Attribute {
|
||||
meta: syn::Meta::List(syn::MetaList {
|
||||
tokens: quote! { #modified_path #combined_options_tokens },
|
||||
..meta_list.clone()
|
||||
}),
|
||||
..attr.clone()
|
||||
}
|
||||
}
|
||||
_ => attr.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn has_allowed_methods_in_scope(attr: &syn::Attribute) -> bool {
|
||||
MethodType::from_path(attr.path()).is_ok()
|
||||
|| attr.path().is_ident("route")
|
||||
|| attr.path().is_ident("ROUTE")
|
||||
}
|
200
actix-web-codegen/tests/scopes.rs
Normal file
200
actix-web-codegen/tests/scopes.rs
Normal file
@ -0,0 +1,200 @@
|
||||
use actix_web::{guard::GuardContext, http, http::header, web, App, HttpResponse, Responder};
|
||||
use actix_web_codegen::{delete, get, post, route, routes, scope};
|
||||
|
||||
pub fn image_guard(ctx: &GuardContext) -> bool {
|
||||
ctx.header::<header::Accept>()
|
||||
.map(|h| h.preference() == "image/*")
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[scope("/test")]
|
||||
mod scope_module {
|
||||
// ensure that imports can be brought into the scope
|
||||
use super::*;
|
||||
|
||||
#[get("/test/guard", guard = "image_guard")]
|
||||
pub async fn guard() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
|
||||
#[get("/test")]
|
||||
pub async fn test() -> impl Responder {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
#[get("/twice-test/{value}")]
|
||||
pub async fn twice(value: web::Path<String>) -> impl actix_web::Responder {
|
||||
let int_value: i32 = value.parse().unwrap_or(0);
|
||||
let doubled = int_value * 2;
|
||||
HttpResponse::Ok().body(format!("Twice value: {}", doubled))
|
||||
}
|
||||
|
||||
#[post("/test")]
|
||||
pub async fn post() -> impl Responder {
|
||||
HttpResponse::Ok().body("post works")
|
||||
}
|
||||
|
||||
#[delete("/test")]
|
||||
pub async fn delete() -> impl Responder {
|
||||
"delete works"
|
||||
}
|
||||
|
||||
#[route("/test", method = "PUT", method = "PATCH", method = "CUSTOM")]
|
||||
pub async fn multiple_shared_path() -> impl Responder {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
#[routes]
|
||||
#[head("/test1")]
|
||||
#[connect("/test2")]
|
||||
#[options("/test3")]
|
||||
#[trace("/test4")]
|
||||
pub async fn multiple_separate_paths() -> impl Responder {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
// test calling this from other mod scope with scope attribute...
|
||||
pub fn mod_common(message: String) -> impl actix_web::Responder {
|
||||
HttpResponse::Ok().body(message)
|
||||
}
|
||||
}
|
||||
|
||||
/// Scope doc string to check in cargo expand.
|
||||
#[scope("/v1")]
|
||||
mod mod_scope_v1 {
|
||||
use super::*;
|
||||
|
||||
/// Route doc string to check in cargo expand.
|
||||
#[get("/test")]
|
||||
pub async fn test() -> impl Responder {
|
||||
scope_module::mod_common("version1 works".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[scope("/v2")]
|
||||
mod mod_scope_v2 {
|
||||
use super::*;
|
||||
|
||||
// check to make sure non-function tokens in the scope block are preserved...
|
||||
enum TestEnum {
|
||||
Works,
|
||||
}
|
||||
|
||||
#[get("/test")]
|
||||
pub async fn test() -> impl Responder {
|
||||
// make sure this type still exists...
|
||||
let test_enum = TestEnum::Works;
|
||||
|
||||
match test_enum {
|
||||
TestEnum::Works => scope_module::mod_common("version2 works".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn scope_get_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::test));
|
||||
|
||||
let request = srv.request(http::Method::GET, srv.url("/test/test"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn scope_get_param_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::twice));
|
||||
|
||||
let request = srv.request(http::Method::GET, srv.url("/test/twice-test/4"));
|
||||
let mut response = request.send().await.unwrap();
|
||||
let body = response.body().await.unwrap();
|
||||
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||
assert_eq!(body_str, "Twice value: 8");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn scope_post_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::post));
|
||||
|
||||
let request = srv.request(http::Method::POST, srv.url("/test/test"));
|
||||
let mut response = request.send().await.unwrap();
|
||||
let body = response.body().await.unwrap();
|
||||
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||
assert_eq!(body_str, "post works");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn multiple_shared_path_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::multiple_shared_path));
|
||||
|
||||
let request = srv.request(http::Method::PUT, srv.url("/test/test"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
let request = srv.request(http::Method::PATCH, srv.url("/test/test"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn multiple_multi_path_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::multiple_separate_paths));
|
||||
|
||||
let request = srv.request(http::Method::HEAD, srv.url("/test/test1"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
let request = srv.request(http::Method::CONNECT, srv.url("/test/test2"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
let request = srv.request(http::Method::OPTIONS, srv.url("/test/test3"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
let request = srv.request(http::Method::TRACE, srv.url("/test/test4"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn scope_delete_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::delete));
|
||||
|
||||
let request = srv.request(http::Method::DELETE, srv.url("/test/test"));
|
||||
let mut response = request.send().await.unwrap();
|
||||
let body = response.body().await.unwrap();
|
||||
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||
assert_eq!(body_str, "delete works");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn scope_get_with_guard_async() {
|
||||
let srv = actix_test::start(|| App::new().service(scope_module::guard));
|
||||
|
||||
let request = srv
|
||||
.request(http::Method::GET, srv.url("/test/test/guard"))
|
||||
.insert_header(("Accept", "image/*"));
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn scope_v1_v2_async() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new()
|
||||
.service(mod_scope_v1::test)
|
||||
.service(mod_scope_v2::test)
|
||||
});
|
||||
|
||||
let request = srv.request(http::Method::GET, srv.url("/v1/test"));
|
||||
let mut response = request.send().await.unwrap();
|
||||
let body = response.body().await.unwrap();
|
||||
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||
assert_eq!(body_str, "version1 works");
|
||||
|
||||
let request = srv.request(http::Method::GET, srv.url("/v2/test"));
|
||||
let mut response = request.send().await.unwrap();
|
||||
let body = response.body().await.unwrap();
|
||||
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||
assert_eq!(body_str, "version2 works");
|
||||
}
|
@ -18,6 +18,11 @@ fn compile_macros() {
|
||||
t.compile_fail("tests/trybuild/routes-missing-method-fail.rs");
|
||||
t.compile_fail("tests/trybuild/routes-missing-args-fail.rs");
|
||||
|
||||
t.compile_fail("tests/trybuild/scope-on-handler.rs");
|
||||
t.compile_fail("tests/trybuild/scope-missing-args.rs");
|
||||
t.compile_fail("tests/trybuild/scope-invalid-args.rs");
|
||||
t.compile_fail("tests/trybuild/scope-trailing-slash.rs");
|
||||
|
||||
t.pass("tests/trybuild/docstring-ok.rs");
|
||||
|
||||
t.pass("tests/trybuild/test-runtime.rs");
|
||||
|
@ -20,10 +20,7 @@ error: custom attribute panicked
|
||||
13 | #[get("/{}")]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= help: message: Wrong path pattern: "/{}" regex parse error:
|
||||
((?s-m)^/(?P<>[^/]+))$
|
||||
^
|
||||
error: empty capture group name
|
||||
= help: message: Wrong path pattern: "/{}" empty capture group names are not allowed
|
||||
|
||||
error: custom attribute panicked
|
||||
--> $DIR/route-malformed-path-fail.rs:23:1
|
||||
|
14
actix-web-codegen/tests/trybuild/scope-invalid-args.rs
Normal file
14
actix-web-codegen/tests/trybuild/scope-invalid-args.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use actix_web_codegen::scope;
|
||||
|
||||
const PATH: &str = "/api";
|
||||
|
||||
#[scope(PATH)]
|
||||
mod api_const {}
|
||||
|
||||
#[scope(true)]
|
||||
mod api_bool {}
|
||||
|
||||
#[scope(123)]
|
||||
mod api_num {}
|
||||
|
||||
fn main() {}
|
17
actix-web-codegen/tests/trybuild/scope-invalid-args.stderr
Normal file
17
actix-web-codegen/tests/trybuild/scope-invalid-args.stderr
Normal file
@ -0,0 +1,17 @@
|
||||
error: argument to scope macro is not a string literal, expected: #[scope("/prefix")]
|
||||
--> tests/trybuild/scope-invalid-args.rs:5:9
|
||||
|
|
||||
5 | #[scope(PATH)]
|
||||
| ^^^^
|
||||
|
||||
error: argument to scope macro is not a string literal, expected: #[scope("/prefix")]
|
||||
--> tests/trybuild/scope-invalid-args.rs:8:9
|
||||
|
|
||||
8 | #[scope(true)]
|
||||
| ^^^^
|
||||
|
||||
error: argument to scope macro is not a string literal, expected: #[scope("/prefix")]
|
||||
--> tests/trybuild/scope-invalid-args.rs:11:9
|
||||
|
|
||||
11 | #[scope(123)]
|
||||
| ^^^
|
6
actix-web-codegen/tests/trybuild/scope-missing-args.rs
Normal file
6
actix-web-codegen/tests/trybuild/scope-missing-args.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use actix_web_codegen::scope;
|
||||
|
||||
#[scope]
|
||||
mod api {}
|
||||
|
||||
fn main() {}
|
@ -0,0 +1,7 @@
|
||||
error: missing arguments for scope macro, expected: #[scope("/prefix")]
|
||||
--> tests/trybuild/scope-missing-args.rs:3:1
|
||||
|
|
||||
3 | #[scope]
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= note: this error originates in the attribute macro `scope` (in Nightly builds, run with -Z macro-backtrace for more info)
|
8
actix-web-codegen/tests/trybuild/scope-on-handler.rs
Normal file
8
actix-web-codegen/tests/trybuild/scope-on-handler.rs
Normal file
@ -0,0 +1,8 @@
|
||||
use actix_web_codegen::scope;
|
||||
|
||||
#[scope("/api")]
|
||||
async fn index() -> &'static str {
|
||||
"Hello World!"
|
||||
}
|
||||
|
||||
fn main() {}
|
5
actix-web-codegen/tests/trybuild/scope-on-handler.stderr
Normal file
5
actix-web-codegen/tests/trybuild/scope-on-handler.stderr
Normal file
@ -0,0 +1,5 @@
|
||||
error: #[scope] macro must be attached to a module
|
||||
--> tests/trybuild/scope-on-handler.rs:4:1
|
||||
|
|
||||
4 | async fn index() -> &'static str {
|
||||
| ^^^^^
|
6
actix-web-codegen/tests/trybuild/scope-trailing-slash.rs
Normal file
6
actix-web-codegen/tests/trybuild/scope-trailing-slash.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use actix_web_codegen::scope;
|
||||
|
||||
#[scope("/api/")]
|
||||
mod api {}
|
||||
|
||||
fn main() {}
|
@ -0,0 +1,5 @@
|
||||
error: scopes should not have trailing slashes; see https://docs.rs/actix-web/4/actix_web/struct.Scope.html#avoid-trailing-slashes
|
||||
--> tests/trybuild/scope-trailing-slash.rs:3:9
|
||||
|
|
||||
3 | #[scope("/api/")]
|
||||
| ^^^^^^^
|
@ -2,6 +2,31 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 4.8.0
|
||||
|
||||
### Added
|
||||
|
||||
- Add `web::Html` responder.
|
||||
- Add `HttpRequest::full_url()` method to get the complete URL of the request.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Always remove port from return value of `ConnectionInfo::realip_remote_addr()` when handling IPv6 addresses. from the `Forwarded` header.
|
||||
- The `UrlencodedError::ContentType` variant (relevant to the `Form` extractor) now uses the 415 (Media Type Unsupported) status code in it's `ResponseError` implementation.
|
||||
- Apply `HttpServer::max_connection_rate()` setting when using rustls v0.22 or v0.23.
|
||||
|
||||
## 4.7.0
|
||||
|
||||
### Added
|
||||
|
||||
- Add `#[scope]` macro.
|
||||
- Add `middleware::Identity` type.
|
||||
- Add `CustomizeResponder::add_cookie()` method.
|
||||
- Add `guard::GuardContext::app_data()` method.
|
||||
- Add `compat-routing-macros-force-pub` crate feature which (on-by-default) which, when disabled, causes handlers to inherit their attached function's visibility.
|
||||
- Add `compat` crate feature group (on-by-default) which, when disabled, helps with transitioning to some planned v5.0 breaking changes, starting only with `compat-routing-macros-force-pub`.
|
||||
- Implement `From<Box<dyn ResponseError>>` for `Error`.
|
||||
|
||||
## 4.6.0
|
||||
|
||||
### Added
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web"
|
||||
version = "4.6.0"
|
||||
version = "4.8.0"
|
||||
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
@ -35,13 +35,43 @@ features = [
|
||||
"secure-cookies",
|
||||
]
|
||||
|
||||
|
||||
[lib]
|
||||
name = "actix_web"
|
||||
path = "src/lib.rs"
|
||||
[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::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = ["macros", "compress-brotli", "compress-gzip", "compress-zstd", "cookies", "http2", "unicode"]
|
||||
default = [
|
||||
"macros",
|
||||
"compress-brotli",
|
||||
"compress-gzip",
|
||||
"compress-zstd",
|
||||
"cookies",
|
||||
"http2",
|
||||
"unicode",
|
||||
"compat",
|
||||
]
|
||||
|
||||
# Brotli algorithm content-encoding support
|
||||
compress-brotli = ["actix-http/compress-brotli", "__compress"]
|
||||
@ -51,29 +81,30 @@ compress-gzip = ["actix-http/compress-gzip", "__compress"]
|
||||
compress-zstd = ["actix-http/compress-zstd", "__compress"]
|
||||
|
||||
# Routing and runtime proc macros
|
||||
macros = ["actix-macros", "actix-web-codegen"]
|
||||
macros = ["dep:actix-macros", "dep:actix-web-codegen"]
|
||||
|
||||
# Cookies support
|
||||
cookies = ["cookie"]
|
||||
cookies = ["dep:cookie"]
|
||||
|
||||
# Secure & signed cookies
|
||||
secure-cookies = ["cookies", "cookie/secure"]
|
||||
|
||||
# HTTP/2 support (including h2c).
|
||||
http2 = ["actix-http/http2"]
|
||||
|
||||
# TLS via OpenSSL
|
||||
openssl = ["http2", "actix-http/openssl", "actix-tls/accept", "actix-tls/openssl"]
|
||||
openssl = ["__tls", "http2", "actix-http/openssl", "actix-tls/accept", "actix-tls/openssl"]
|
||||
|
||||
# TLS via Rustls v0.20
|
||||
rustls = ["rustls-0_20"]
|
||||
# TLS via Rustls v0.20
|
||||
rustls-0_20 = ["http2", "actix-http/rustls-0_20", "actix-tls/accept", "actix-tls/rustls-0_20"]
|
||||
rustls-0_20 = ["__tls", "http2", "actix-http/rustls-0_20", "actix-tls/accept", "actix-tls/rustls-0_20"]
|
||||
# TLS via Rustls v0.21
|
||||
rustls-0_21 = ["http2", "actix-http/rustls-0_21", "actix-tls/accept", "actix-tls/rustls-0_21"]
|
||||
rustls-0_21 = ["__tls", "http2", "actix-http/rustls-0_21", "actix-tls/accept", "actix-tls/rustls-0_21"]
|
||||
# TLS via Rustls v0.22
|
||||
rustls-0_22 = ["http2", "actix-http/rustls-0_22", "actix-tls/accept", "actix-tls/rustls-0_22"]
|
||||
rustls-0_22 = ["__tls", "http2", "actix-http/rustls-0_22", "actix-tls/accept", "actix-tls/rustls-0_22"]
|
||||
# TLS via Rustls v0.23
|
||||
rustls-0_23 = ["http2", "actix-http/rustls-0_23", "actix-tls/accept", "actix-tls/rustls-0_23"]
|
||||
rustls-0_23 = ["__tls", "http2", "actix-http/rustls-0_23", "actix-tls/accept", "actix-tls/rustls-0_23"]
|
||||
|
||||
# Full unicode support
|
||||
unicode = ["dep:regex", "actix-router/unicode"]
|
||||
@ -82,9 +113,21 @@ unicode = ["dep:regex", "actix-router/unicode"]
|
||||
# Don't rely on these whatsoever. They may disappear at anytime.
|
||||
__compress = []
|
||||
|
||||
# Internal (PRIVATE!) features used to aid checking feature status.
|
||||
# Don't rely on these whatsoever. They may disappear at anytime.
|
||||
__tls = []
|
||||
|
||||
# io-uring feature only available for Linux OSes.
|
||||
experimental-io-uring = ["actix-server/io-uring"]
|
||||
|
||||
# Feature group which, when disabled, helps migrate code to v5.0.
|
||||
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"]
|
||||
|
||||
[dependencies]
|
||||
actix-codec = "0.5"
|
||||
actix-macros = { version = "0.2.3", optional = true }
|
||||
@ -95,8 +138,8 @@ actix-utils = "3"
|
||||
actix-tls = { version = "3.4", default-features = false, optional = true }
|
||||
|
||||
actix-http = { version = "3.7", features = ["ws"] }
|
||||
actix-router = { version = "0.5", default-features = false, features = ["http"] }
|
||||
actix-web-codegen = { version = "4.2", optional = true }
|
||||
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"
|
||||
@ -130,6 +173,7 @@ awc = { version = "3", features = ["openssl"] }
|
||||
|
||||
brotli = "6"
|
||||
const-str = "0.5"
|
||||
core_affinity = "0.8"
|
||||
criterion = { version = "0.5", features = ["html_reports"] }
|
||||
env_logger = "0.11"
|
||||
flate2 = "1.0.13"
|
||||
|
@ -8,10 +8,10 @@
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[](https://crates.io/crates/actix-web)
|
||||
[](https://docs.rs/actix-web/4.6.0)
|
||||
[](https://docs.rs/actix-web/4.8.0)
|
||||

|
||||

|
||||
[](https://deps.rs/crate/actix-web/4.6.0)
|
||||
[](https://deps.rs/crate/actix-web/4.8.0)
|
||||
<br />
|
||||
[](https://github.com/actix/actix-web/actions/workflows/ci.yml)
|
||||
[](https://codecov.io/gh/actix/actix-web)
|
||||
@ -109,4 +109,4 @@ This project is licensed under either of the following licenses, at your option:
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
Contribution to the actix-web repo is organized under the terms of the Contributor Covenant. The Actix team promises to intervene to uphold that code of conduct.
|
||||
Contribution to the `actix/actix-web` repo is organized under the terms of the Contributor Covenant. The Actix team promises to intervene to uphold that code of conduct.
|
||||
|
41
actix-web/examples/worker-cpu-pin.rs
Normal file
41
actix-web/examples/worker-cpu-pin.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use std::{
|
||||
io,
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread,
|
||||
};
|
||||
|
||||
use actix_web::{middleware, web, App, HttpServer};
|
||||
|
||||
async fn hello() -> &'static str {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let core_ids = core_affinity::get_core_ids().unwrap();
|
||||
let n_core_ids = core_ids.len();
|
||||
let next_core_id = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
HttpServer::new(move || {
|
||||
let pin = Arc::clone(&next_core_id).fetch_add(1, Ordering::AcqRel);
|
||||
log::info!(
|
||||
"setting CPU affinity for worker {}: pinning to core {}",
|
||||
thread::current().name().unwrap(),
|
||||
pin,
|
||||
);
|
||||
core_affinity::set_for_current(core_ids[pin]);
|
||||
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").get(hello))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.workers(n_core_ids)
|
||||
.run()
|
||||
.await
|
||||
}
|
@ -112,8 +112,8 @@ where
|
||||
/// })
|
||||
/// ```
|
||||
#[doc(alias = "manage")]
|
||||
pub fn app_data<U: 'static>(mut self, ext: U) -> Self {
|
||||
self.extensions.insert(ext);
|
||||
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||
self.extensions.insert(data);
|
||||
self
|
||||
}
|
||||
|
||||
@ -234,7 +234,6 @@ where
|
||||
///
|
||||
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
||||
/// * *Scope* is a set of resources with common root path.
|
||||
/// * "StaticFiles" is a service for static files support
|
||||
pub fn service<F>(mut self, factory: F) -> Self
|
||||
where
|
||||
F: HttpServiceFactory + 'static,
|
||||
|
@ -60,6 +60,12 @@ impl<T: ResponseError + 'static> From<T> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<dyn ResponseError>> for Error {
|
||||
fn from(value: Box<dyn ResponseError>) -> Self {
|
||||
Error { cause: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for Response<BoxBody> {
|
||||
fn from(err: Error) -> Response<BoxBody> {
|
||||
err.error_response().into()
|
||||
|
@ -100,6 +100,7 @@ impl ResponseError for UrlencodedError {
|
||||
match self {
|
||||
Self::Overflow { .. } => StatusCode::PAYLOAD_TOO_LARGE,
|
||||
Self::UnknownLength => StatusCode::LENGTH_REQUIRED,
|
||||
Self::ContentType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
||||
Self::Payload(err) => err.status_code(),
|
||||
_ => StatusCode::BAD_REQUEST,
|
||||
}
|
||||
@ -232,7 +233,7 @@ mod tests {
|
||||
let resp = UrlencodedError::UnknownLength.error_response();
|
||||
assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED);
|
||||
let resp = UrlencodedError::ContentType.error_response();
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -110,6 +110,12 @@ impl<'a> GuardContext<'a> {
|
||||
pub fn header<H: Header>(&self) -> Option<H> {
|
||||
H::parse(self.req).ok()
|
||||
}
|
||||
|
||||
/// Counterpart to [HttpRequest::app_data](crate::HttpRequest::app_data).
|
||||
#[inline]
|
||||
pub fn app_data<T: 'static>(&self) -> Option<&T> {
|
||||
self.req.app_data()
|
||||
}
|
||||
}
|
||||
|
||||
/// Interface for routing guards.
|
||||
@ -512,4 +518,18 @@ mod tests {
|
||||
.to_srv_request();
|
||||
assert!(guard.check(&req.guard_ctx()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn app_data() {
|
||||
const TEST_VALUE: u32 = 42;
|
||||
let guard = fn_guard(|ctx| dbg!(ctx.app_data::<u32>()) == Some(&TEST_VALUE));
|
||||
|
||||
let req = TestRequest::default().app_data(TEST_VALUE).to_srv_request();
|
||||
assert!(guard.check(&req.guard_ctx()));
|
||||
|
||||
let req = TestRequest::default()
|
||||
.app_data(TEST_VALUE * 2)
|
||||
.to_srv_request();
|
||||
assert!(!guard.check(&req.guard_ctx()));
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,20 @@ fn unquote(val: &str) -> &str {
|
||||
val.trim().trim_start_matches('"').trim_end_matches('"')
|
||||
}
|
||||
|
||||
/// Remove port and IPv6 square brackets from a peer specification.
|
||||
fn bare_address(val: &str) -> &str {
|
||||
if val.starts_with('[') {
|
||||
val.split("]:")
|
||||
.next()
|
||||
.map(|s| s.trim_start_matches('[').trim_end_matches(']'))
|
||||
// this indicates that the IPv6 address is malformed so shouldn't
|
||||
// usually happen, but if it does, just return the original input
|
||||
.unwrap_or(val)
|
||||
} else {
|
||||
val.split(':').next().unwrap_or(val)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts and trims first value for given header name.
|
||||
fn first_header_value<'a>(req: &'a RequestHead, name: &'_ HeaderName) -> Option<&'a str> {
|
||||
let hdr = req.headers.get(name)?.to_str().ok()?;
|
||||
@ -100,7 +114,7 @@ impl ConnectionInfo {
|
||||
// --- https://datatracker.ietf.org/doc/html/rfc7239#section-5.2
|
||||
|
||||
match name.trim().to_lowercase().as_str() {
|
||||
"for" => realip_remote_addr.get_or_insert_with(|| unquote(val)),
|
||||
"for" => realip_remote_addr.get_or_insert_with(|| bare_address(unquote(val))),
|
||||
"proto" => scheme.get_or_insert_with(|| unquote(val)),
|
||||
"host" => host.get_or_insert_with(|| unquote(val)),
|
||||
"by" => {
|
||||
@ -368,16 +382,25 @@ mod tests {
|
||||
.insert_header((header::FORWARDED, r#"for="192.0.2.60:8080""#))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60:8080"));
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_for_ipv6() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::FORWARDED, r#"for="[2001:db8:cafe::17]""#))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("2001:db8:cafe::17"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_for_ipv6_with_port() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::FORWARDED, r#"for="[2001:db8:cafe::17]:4711""#))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("[2001:db8:cafe::17]:4711"));
|
||||
assert_eq!(info.realip_remote_addr(), Some("2001:db8:cafe::17"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -145,5 +145,6 @@ codegen_reexport!(delete);
|
||||
codegen_reexport!(trace);
|
||||
codegen_reexport!(connect);
|
||||
codegen_reexport!(options);
|
||||
codegen_reexport!(scope);
|
||||
|
||||
pub(crate) type BoxError = Box<dyn std::error::Error>;
|
||||
|
@ -38,15 +38,6 @@ pub struct Compat<T> {
|
||||
transform: T,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Compat<super::Noop> {
|
||||
pub(crate) fn noop() -> Self {
|
||||
Self {
|
||||
transform: super::Noop,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Compat<T> {
|
||||
/// Wrap a middleware to give it broader compatibility.
|
||||
pub fn new(middleware: T) -> Self {
|
||||
@ -152,7 +143,7 @@ mod tests {
|
||||
use crate::{
|
||||
dev::ServiceRequest,
|
||||
http::StatusCode,
|
||||
middleware::{self, Condition, Logger},
|
||||
middleware::{self, Condition, Identity, Logger},
|
||||
test::{self, call_service, init_service, TestRequest},
|
||||
web, App, HttpResponse,
|
||||
};
|
||||
@ -225,7 +216,7 @@ mod tests {
|
||||
async fn compat_noop_is_noop() {
|
||||
let srv = test::ok_service();
|
||||
|
||||
let mw = Compat::noop()
|
||||
let mw = Compat::new(Identity)
|
||||
.new_transform(srv.into_service())
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -141,7 +141,7 @@ mod tests {
|
||||
header::{HeaderValue, CONTENT_TYPE},
|
||||
StatusCode,
|
||||
},
|
||||
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
|
||||
middleware::{self, ErrorHandlerResponse, ErrorHandlers, Identity},
|
||||
test::{self, TestRequest},
|
||||
web::Bytes,
|
||||
HttpResponse,
|
||||
@ -158,7 +158,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn compat_with_builtin_middleware() {
|
||||
let _ = Condition::new(true, middleware::Compat::noop());
|
||||
let _ = Condition::new(true, middleware::Compat::new(Identity));
|
||||
let _ = Condition::new(true, middleware::Logger::default());
|
||||
let _ = Condition::new(true, middleware::Compress::default());
|
||||
let _ = Condition::new(true, middleware::NormalizePath::trim());
|
||||
|
@ -2,35 +2,39 @@
|
||||
|
||||
use actix_utils::future::{ready, Ready};
|
||||
|
||||
use crate::dev::{Service, Transform};
|
||||
use crate::dev::{forward_ready, Service, Transform};
|
||||
|
||||
/// A no-op middleware that passes through request and response untouched.
|
||||
pub(crate) struct Noop;
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[non_exhaustive]
|
||||
pub struct Identity;
|
||||
|
||||
impl<S: Service<Req>, Req> Transform<S, Req> for Noop {
|
||||
impl<S: Service<Req>, Req> Transform<S, Req> for Identity {
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Transform = NoopService<S>;
|
||||
type Transform = IdentityMiddleware<S>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
#[inline]
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(NoopService { service }))
|
||||
ready(Ok(IdentityMiddleware { service }))
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub(crate) struct NoopService<S> {
|
||||
pub struct IdentityMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<S: Service<Req>, Req> Service<Req> for NoopService<S> {
|
||||
impl<S: Service<Req>, Req> Service<Req> for IdentityMiddleware<S> {
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
crate::dev::forward_ready!(service);
|
||||
forward_ready!(service);
|
||||
|
||||
#[inline]
|
||||
fn call(&self, req: Req) -> Self::Future {
|
||||
self.service.call(req)
|
||||
}
|
@ -218,31 +218,27 @@
|
||||
//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html
|
||||
|
||||
mod compat;
|
||||
#[cfg(feature = "__compress")]
|
||||
mod compress;
|
||||
mod condition;
|
||||
mod default_headers;
|
||||
mod err_handlers;
|
||||
mod identity;
|
||||
mod logger;
|
||||
#[cfg(test)]
|
||||
mod noop;
|
||||
mod normalize;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use self::noop::Noop;
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use self::compress::Compress;
|
||||
pub use self::{
|
||||
compat::Compat,
|
||||
condition::Condition,
|
||||
default_headers::DefaultHeaders,
|
||||
err_handlers::{ErrorHandlerResponse, ErrorHandlers},
|
||||
identity::Identity,
|
||||
logger::Logger,
|
||||
normalize::{NormalizePath, TrailingSlash},
|
||||
};
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
mod compress;
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use self::compress::Compress;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -91,6 +91,35 @@ impl HttpRequest {
|
||||
&self.head().uri
|
||||
}
|
||||
|
||||
/// Returns request's original full URL.
|
||||
///
|
||||
/// Reconstructed URL is best-effort, using [`connection_info`](HttpRequest::connection_info())
|
||||
/// to get forwarded scheme & host.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::test::TestRequest;
|
||||
/// let req = TestRequest::with_uri("http://10.1.2.3:8443/api?id=4&name=foo")
|
||||
/// .insert_header(("host", "example.com"))
|
||||
/// .to_http_request();
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// req.full_url().as_str(),
|
||||
/// "http://example.com/api?id=4&name=foo",
|
||||
/// );
|
||||
/// ```
|
||||
pub fn full_url(&self) -> url::Url {
|
||||
let info = self.connection_info();
|
||||
let scheme = info.scheme();
|
||||
let host = info.host();
|
||||
let path_and_query = self
|
||||
.uri()
|
||||
.path_and_query()
|
||||
.map(|paq| paq.as_str())
|
||||
.unwrap_or("/");
|
||||
|
||||
url::Url::parse(&format!("{scheme}://{host}{path_and_query}")).unwrap()
|
||||
}
|
||||
|
||||
/// Read the Request method.
|
||||
#[inline]
|
||||
pub fn method(&self) -> &Method {
|
||||
@ -963,4 +992,27 @@ mod tests {
|
||||
|
||||
assert!(format!("{:?}", req).contains(location_header));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_full_url() {
|
||||
let req = TestRequest::with_uri("/api?id=4&name=foo").to_http_request();
|
||||
assert_eq!(
|
||||
req.full_url().as_str(),
|
||||
"http://localhost:8080/api?id=4&name=foo",
|
||||
);
|
||||
|
||||
let req = TestRequest::with_uri("https://example.com/api?id=4&name=foo").to_http_request();
|
||||
assert_eq!(
|
||||
req.full_url().as_str(),
|
||||
"https://example.com/api?id=4&name=foo",
|
||||
);
|
||||
|
||||
let req = TestRequest::with_uri("http://10.1.2.3:8443/api?id=4&name=foo")
|
||||
.insert_header(("host", "example.com"))
|
||||
.to_http_request();
|
||||
assert_eq!(
|
||||
req.full_url().as_str(),
|
||||
"http://example.com/api?id=4&name=foo",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use actix_http::{
|
||||
|
||||
use crate::{HttpRequest, HttpResponse, Responder};
|
||||
|
||||
/// Allows overriding status code and headers for a [`Responder`].
|
||||
/// Allows overriding status code and headers (including cookies) for a [`Responder`].
|
||||
///
|
||||
/// Created by calling the [`customize`](Responder::customize) method on a [`Responder`] type.
|
||||
pub struct CustomizeResponder<R> {
|
||||
@ -137,6 +137,29 @@ impl<R: Responder> CustomizeResponder<R> {
|
||||
Some(&mut self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
/// Appends a `cookie` to the final response.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Final response will be an error if `cookie` cannot be converted into a valid header value.
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn add_cookie(mut self, cookie: &crate::cookie::Cookie<'_>) -> Self {
|
||||
use actix_http::header::{TryIntoHeaderValue as _, SET_COOKIE};
|
||||
|
||||
if let Some(inner) = self.inner() {
|
||||
match cookie.to_string().try_into_value() {
|
||||
Ok(val) => {
|
||||
inner.append_headers.append(SET_COOKIE, val);
|
||||
}
|
||||
Err(err) => {
|
||||
self.error = Some(err.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Responder for CustomizeResponder<T>
|
||||
@ -175,6 +198,7 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
cookie::Cookie,
|
||||
http::header::{HeaderValue, CONTENT_TYPE},
|
||||
test::TestRequest,
|
||||
};
|
||||
@ -209,6 +233,22 @@ mod tests {
|
||||
to_bytes(res.into_body()).await.unwrap(),
|
||||
Bytes::from_static(b"test"),
|
||||
);
|
||||
|
||||
let res = "test"
|
||||
.to_string()
|
||||
.customize()
|
||||
.add_cookie(&Cookie::new("name", "value"))
|
||||
.respond_to(&req);
|
||||
|
||||
assert!(res.status().is_success());
|
||||
assert_eq!(
|
||||
res.cookies().collect::<Vec<Cookie<'_>>>(),
|
||||
vec![Cookie::new("name", "value")],
|
||||
);
|
||||
assert_eq!(
|
||||
to_bytes(res.into_body()).await.unwrap(),
|
||||
Bytes::from_static(b"test"),
|
||||
);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
|
@ -213,7 +213,6 @@ where
|
||||
///
|
||||
/// * *Resource* is an entry in resource table which corresponds to requested URL.
|
||||
/// * *Scope* is a set of resources with common root path.
|
||||
/// * "StaticFiles" is a service for static files support
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::{web, App, HttpRequest};
|
||||
|
@ -7,13 +7,7 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
#[cfg(any(
|
||||
feature = "openssl",
|
||||
feature = "rustls-0_20",
|
||||
feature = "rustls-0_21",
|
||||
feature = "rustls-0_22",
|
||||
feature = "rustls-0_23",
|
||||
))]
|
||||
#[cfg(feature = "__tls")]
|
||||
use actix_http::TlsAcceptorConfig;
|
||||
use actix_http::{body::MessageBody, Extensions, HttpService, KeepAlive, Request, Response};
|
||||
use actix_server::{Server, ServerBuilder};
|
||||
@ -190,7 +184,7 @@ where
|
||||
/// By default max connections is set to a 256.
|
||||
#[allow(unused_variables)]
|
||||
pub fn max_connection_rate(self, num: usize) -> Self {
|
||||
#[cfg(any(feature = "rustls-0_20", feature = "rustls-0_21", feature = "openssl"))]
|
||||
#[cfg(feature = "__tls")]
|
||||
actix_tls::accept::max_concurrent_tls_connect(num);
|
||||
self
|
||||
}
|
||||
@ -243,13 +237,7 @@ where
|
||||
/// time, the connection is closed.
|
||||
///
|
||||
/// By default, the handshake timeout is 3 seconds.
|
||||
#[cfg(any(
|
||||
feature = "openssl",
|
||||
feature = "rustls-0_20",
|
||||
feature = "rustls-0_21",
|
||||
feature = "rustls-0_22",
|
||||
feature = "rustls-0_23",
|
||||
))]
|
||||
#[cfg(feature = "__tls")]
|
||||
pub fn tls_handshake_timeout(self, dur: Duration) -> Self {
|
||||
self.config
|
||||
.lock()
|
||||
|
66
actix-web/src/types/html.rs
Normal file
66
actix-web/src/types/html.rs
Normal file
@ -0,0 +1,66 @@
|
||||
//! Semantic HTML responder. See [`Html`].
|
||||
|
||||
use crate::{
|
||||
http::{
|
||||
header::{self, ContentType, TryIntoHeaderValue},
|
||||
StatusCode,
|
||||
},
|
||||
HttpRequest, HttpResponse, Responder,
|
||||
};
|
||||
|
||||
/// Semantic HTML responder.
|
||||
///
|
||||
/// When used as a responder, creates a 200 OK response, sets the correct HTML content type, and
|
||||
/// uses the string passed to [`Html::new()`] as the body.
|
||||
///
|
||||
/// ```
|
||||
/// # use actix_web::web::Html;
|
||||
/// Html::new("<p>Hello, World!</p>")
|
||||
/// # ;
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct Html(String);
|
||||
|
||||
impl Html {
|
||||
/// Constructs a new `Html` responder.
|
||||
pub fn new(html: impl Into<String>) -> Self {
|
||||
Self(html.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Responder for Html {
|
||||
type Body = String;
|
||||
|
||||
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||
let mut res = HttpResponse::with_body(StatusCode::OK, self.0);
|
||||
res.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
ContentType::html().try_into_value().unwrap(),
|
||||
);
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test::TestRequest;
|
||||
|
||||
#[test]
|
||||
fn responder() {
|
||||
let req = TestRequest::default().to_http_request();
|
||||
|
||||
let res = Html::new("<p>Hello, World!</p>");
|
||||
let res = res.respond_to(&req);
|
||||
|
||||
assert!(res.status().is_success());
|
||||
assert!(res
|
||||
.headers()
|
||||
.get(header::CONTENT_TYPE)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.starts_with("text/html"));
|
||||
assert!(res.body().starts_with("<p>"));
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
mod either;
|
||||
mod form;
|
||||
mod header;
|
||||
mod html;
|
||||
mod json;
|
||||
mod path;
|
||||
mod payload;
|
||||
@ -13,6 +14,7 @@ pub use self::{
|
||||
either::Either,
|
||||
form::{Form, FormConfig, UrlEncoded},
|
||||
header::Header,
|
||||
html::Html,
|
||||
json::{Json, JsonBody, JsonConfig},
|
||||
path::{Path, PathConfig},
|
||||
payload::{Payload, PayloadConfig},
|
||||
|
@ -15,10 +15,6 @@ repository = "https://github.com/actix/actix-web"
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "awc"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
features = [
|
||||
@ -33,6 +29,27 @@ features = [
|
||||
"compress-zstd",
|
||||
]
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
allowed_external_types = [
|
||||
"actix_codec::*",
|
||||
"actix_http::*",
|
||||
"actix_rt::*",
|
||||
"actix_service::*",
|
||||
"actix_tls::*",
|
||||
"bytes::*",
|
||||
"cookie::*",
|
||||
"cookie",
|
||||
"futures_core::*",
|
||||
"h2::*",
|
||||
"http::*",
|
||||
"openssl::*",
|
||||
"rustls::*",
|
||||
"serde_json::*",
|
||||
"serde_urlencoded::*",
|
||||
"serde::*",
|
||||
"tokio::*",
|
||||
]
|
||||
|
||||
[features]
|
||||
default = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
|
||||
|
||||
@ -64,7 +81,7 @@ compress-gzip = ["actix-http/compress-gzip", "__compress"]
|
||||
compress-zstd = ["actix-http/compress-zstd", "__compress"]
|
||||
|
||||
# Cookie parsing and cookie jar
|
||||
cookies = ["cookie"]
|
||||
cookies = ["dep:cookie"]
|
||||
|
||||
# Use `trust-dns-resolver` crate as DNS resolver
|
||||
trust-dns = ["trust-dns-resolver"]
|
||||
@ -92,7 +109,7 @@ cfg-if = "1"
|
||||
derive_more = "0.99.5"
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc", "sink"] }
|
||||
h2 = "0.3.24"
|
||||
h2 = "0.3.26"
|
||||
http = "0.2.7"
|
||||
itoa = "1"
|
||||
log =" 0.4"
|
||||
@ -134,7 +151,7 @@ rcgen = "0.13"
|
||||
rustls-pemfile = "2"
|
||||
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
|
||||
zstd = "0.13"
|
||||
tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests
|
||||
tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests
|
||||
|
||||
[[example]]
|
||||
name = "client"
|
||||
|
@ -1080,7 +1080,7 @@ mod resolver {
|
||||
|
||||
// resolver struct is cached in thread local so new clients can reuse the existing instance
|
||||
thread_local! {
|
||||
static TRUST_DNS_RESOLVER: RefCell<Option<Resolver>> = RefCell::new(None);
|
||||
static TRUST_DNS_RESOLVER: RefCell<Option<Resolver>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
// get from thread local or construct a new trust-dns resolver.
|
||||
|
85
justfile
85
justfile
@ -4,7 +4,7 @@ _list:
|
||||
# Format workspace.
|
||||
fmt:
|
||||
cargo +nightly fmt
|
||||
npx -y prettier --write $(fd --type=file --hidden --extension=md --extension=yml)
|
||||
fd --hidden --type=file --extension=md --extension=yml --exec-batch npx -y prettier --write
|
||||
|
||||
# Downgrade dev-dependencies necessary to run MSRV checks/tests.
|
||||
[private]
|
||||
@ -22,7 +22,7 @@ non_linux_all_features_list := ```
|
||||
cargo metadata --format-version=1 \
|
||||
| jq '.packages[] | select(.source == null) | .features | keys' \
|
||||
| jq -r --slurp \
|
||||
--arg exclusions "tokio-uring,io-uring,experimental-io-uring" \
|
||||
--arg exclusions "__tls,__compress,tokio-uring,io-uring,experimental-io-uring" \
|
||||
'add | unique | . - ($exclusions | split(",")) | join(",")'
|
||||
```
|
||||
|
||||
@ -32,6 +32,18 @@ all_crate_features := if os() == "linux" {
|
||||
"--features='" + non_linux_all_features_list + "'"
|
||||
}
|
||||
|
||||
[private]
|
||||
check-min:
|
||||
cargo hack --workspace check --no-default-features
|
||||
|
||||
[private]
|
||||
check-default:
|
||||
cargo hack --workspace check
|
||||
|
||||
# Run Clippy over workspace.
|
||||
clippy toolchain="":
|
||||
cargo {{ toolchain }} clippy --workspace --all-targets {{ all_crate_features }}
|
||||
|
||||
# Test workspace using MSRV.
|
||||
test-msrv: downgrade-for-msrv (test msrv_rustup)
|
||||
|
||||
@ -49,9 +61,32 @@ test-docs toolchain="": && doc
|
||||
# Test workspace.
|
||||
test-all toolchain="": (test toolchain) (test-docs toolchain)
|
||||
|
||||
# Test workspace and collect coverage info.
|
||||
[private]
|
||||
test-coverage toolchain="":
|
||||
cargo {{ toolchain }} llvm-cov nextest --no-report {{ all_crate_features }}
|
||||
cargo {{ toolchain }} llvm-cov --doc --no-report {{ all_crate_features }}
|
||||
|
||||
# Test workspace and generate Codecov report.
|
||||
test-coverage-codecov toolchain="": (test-coverage toolchain)
|
||||
cargo {{ toolchain }} llvm-cov report --doctests --codecov --output-path=codecov.json
|
||||
|
||||
# Test workspace and generate LCOV report.
|
||||
test-coverage-lcov toolchain="": (test-coverage toolchain)
|
||||
cargo {{ toolchain }} llvm-cov report --doctests --lcov --output-path=lcov.info
|
||||
|
||||
# Document crates in workspace.
|
||||
doc *args:
|
||||
RUSTDOCFLAGS="--cfg=docsrs -Dwarnings" cargo +nightly doc --no-deps --workspace {{ all_crate_features }} {{ args }}
|
||||
doc *args: && doc-set-workspace-crates
|
||||
RUSTDOCFLAGS="--cfg=docsrs -Dwarnings" cargo +nightly doc --workspace {{ all_crate_features }} {{ args }}
|
||||
|
||||
[private]
|
||||
doc-set-workspace-crates:
|
||||
#!/usr/bin/env bash
|
||||
(
|
||||
echo "window.ALL_CRATES ="
|
||||
cargo metadata --format-version=1 | jq '[.packages[] | select(.source == null) | .name]'
|
||||
echo ";"
|
||||
) > "$(cargo metadata --format-version=1 | jq -r '.target_directory')/doc/crates.js"
|
||||
|
||||
# Document crates in workspace and watch for changes.
|
||||
doc-watch:
|
||||
@ -61,4 +96,46 @@ doc-watch:
|
||||
# Update READMEs from crate root documentation.
|
||||
update-readmes: && fmt
|
||||
cd ./actix-files && cargo rdme --force
|
||||
cd ./actix-http-test && cargo rdme --force
|
||||
cd ./actix-router && cargo rdme --force
|
||||
cd ./actix-multipart && cargo rdme --force
|
||||
cd ./actix-test && cargo rdme --force
|
||||
|
||||
feature_combo_skip_list := if os() == "linux" {
|
||||
"__tls,__compress"
|
||||
} else {
|
||||
"__tls,__compress,experimental-io-uring"
|
||||
}
|
||||
|
||||
# Checks compatibility of feature combinations.
|
||||
check-feature-combinations:
|
||||
cargo hack --workspace \
|
||||
--feature-powerset --depth=4 \
|
||||
--skip={{ feature_combo_skip_list }} \
|
||||
check
|
||||
|
||||
# Check for unintentional external type exposure on all crates in workspace.
|
||||
check-external-types-all toolchain="+nightly":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
exit=0
|
||||
for f in $(find . -mindepth 2 -maxdepth 2 -name Cargo.toml | grep -vE "\-codegen/|\-derive/|\-macros/"); do
|
||||
if ! just check-external-types-manifest "$f" {{toolchain}}; then exit=1; fi
|
||||
echo
|
||||
echo
|
||||
done
|
||||
exit $exit
|
||||
|
||||
# Check for unintentional external type exposure on all crates in workspace.
|
||||
check-external-types-all-table toolchain="+nightly":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
for f in $(find . -mindepth 2 -maxdepth 2 -name Cargo.toml | grep -vE "\-codegen/|\-derive/|\-macros/"); do
|
||||
echo
|
||||
echo "Checking for $f"
|
||||
just check-external-types-manifest "$f" {{toolchain}} --output-format=markdown-table
|
||||
done
|
||||
|
||||
# Check for unintentional external type exposure on a crate.
|
||||
check-external-types-manifest manifest_path toolchain="+nightly" *extra_args="":
|
||||
cargo {{toolchain}} check-external-types --manifest-path "{{manifest_path}}" {{extra_args}}
|
||||
|
Reference in New Issue
Block a user