diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index a2345e18..00000000 --- a/.cargo/config.toml +++ /dev/null @@ -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 --depth=4 --skip=__compress,experimental-io-uring check" -ci-check-all-feature-powerset-linux="hack --workspace --feature-powerset --depth=4 --skip=__compress check" diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 00000000..8c0cbc8b --- /dev/null +++ b/.clippy.toml @@ -0,0 +1,7 @@ +disallowed-names = [ + "e", # no single letter error bindings +] +disallowed-methods = [ + "std::cell::RefCell::default()", + "std::rc::Rc::default()", +] diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 8d509d69..f0ed2173 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -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.10.1 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.44.60 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.10.1 - - name: Install cargo-hack - uses: taiki-e/install-action@v2.33.22 + - name: Install just, cargo-hack + uses: taiki-e/install-action@v2.44.60 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 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a81f0e8e..b04213dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -59,12 +59,12 @@ jobs: 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.10.1 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.44.60 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -73,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 @@ -92,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.10.1 with: toolchain: nightly @@ -108,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.10.1 with: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.33.22 + uses: taiki-e/install-action@v2.44.60 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 943f6322..1d4e580c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -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.10.1 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.44.60 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.6.0 with: files: codecov.json fail_ci_if_error: true diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 03b25ecd..94380aee 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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.10.1 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.10.1 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.10.1 with: toolchain: nightly components: rust-docs @@ -65,6 +65,30 @@ jobs: RUSTDOCFLAGS: -D warnings run: cargo +nightly doc --no-deps --workspace --all-features + check-external-types: + if: false # disable until https://github.com/awslabs/cargo-check-external-types/pull/177 is marged + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust (nightly-2024-05-01) + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 + with: + toolchain: nightly-2024-05-01 + + - name: Install just + uses: taiki-e/install-action@v2.44.60 + 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 +100,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-09-30) + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: - toolchain: nightly-2024-04-26 + toolchain: nightly-2024-09-30 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.33.22 + uses: taiki-e/install-action@v2.44.60 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 diff --git a/.github/workflows/upload-doc.yml b/.github/workflows/upload-doc.yml deleted file mode 100644 index 6352e44d..00000000 --- a/.github/workflows/upload-doc.yml +++ /dev/null @@ -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 '' > 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 diff --git a/Cargo.toml b/Cargo.toml index 19d5dd11..d5601f8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ homepage = "https://actix.rs" repository = "https://github.com/actix/actix-web" license = "MIT OR Apache-2.0" edition = "2021" -rust-version = "1.72" +rust-version = "1.75" [profile.dev] # Disabling debug info speeds up builds a bunch and we don't rely on it for debugging that much. @@ -51,3 +51,11 @@ awc = { path = "awc" } # actix-utils = { path = "../actix-net/actix-utils" } # actix-tls = { path = "../actix-net/actix-tls" } # actix-server = { path = "../actix-net/actix-server" } + +[workspace.lints.rust] +rust_2018_idioms = { level = "deny" } +future_incompatible = { level = "deny" } +nonstandard_style = { level = "deny" } + +[workspace.lints.clippy] +# clone_on_ref_ptr = { level = "deny" } diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index 393e7b61..afb2d5d2 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -2,6 +2,11 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + +## 0.6.6 + +- Update `tokio-uring` dependency to `0.4`. - Minimum supported Rust version (MSRV) is now 1.72. ## 0.6.5 diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index 3d82f8a7..48d38874 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-files" -version = "0.6.5" +version = "0.6.6" authors = [ "Nikolay Kim ", "Rob Ede ", @@ -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"] @@ -28,7 +33,7 @@ actix-web = { version = "4", default-features = false } bitflags = "2" bytes = "1" -derive_more = "0.99.5" +derive_more = { version = "1", features = ["display", "error", "from"] } futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } http-range = "0.1.4" log = "0.4" @@ -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" @@ -49,3 +54,6 @@ actix-test = "0.1" actix-web = "4" env_logger = "0.11" tempfile = "3.2" + +[lints] +workspace = true diff --git a/actix-files/README.md b/actix-files/README.md index a6b3f63c..f6d5143f 100644 --- a/actix-files/README.md +++ b/actix-files/README.md @@ -3,11 +3,11 @@ [![crates.io](https://img.shields.io/crates/v/actix-files?label=latest)](https://crates.io/crates/actix-files) -[![Documentation](https://docs.rs/actix-files/badge.svg?version=0.6.5)](https://docs.rs/actix-files/0.6.5) +[![Documentation](https://docs.rs/actix-files/badge.svg?version=0.6.6)](https://docs.rs/actix-files/0.6.6) ![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![License](https://img.shields.io/crates/l/actix-files.svg)
-[![dependency status](https://deps.rs/crate/actix-files/0.6.5/status.svg)](https://deps.rs/crate/actix-files/0.6.5) +[![dependency status](https://deps.rs/crate/actix-files/0.6.6/status.svg)](https://deps.rs/crate/actix-files/0.6.6) [![Download](https://img.shields.io/crates/d/actix-files.svg)](https://crates.io/crates/actix-files) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) diff --git a/actix-files/src/error.rs b/actix-files/src/error.rs index d614651f..1d481888 100644 --- a/actix-files/src/error.rs +++ b/actix-files/src/error.rs @@ -1,16 +1,16 @@ use actix_web::{http::StatusCode, ResponseError}; -use derive_more::Display; +use derive_more::derive::Display; /// Errors which can occur when serving static files. #[derive(Debug, PartialEq, Eq, Display)] pub enum FilesError { /// Path is not a directory. #[allow(dead_code)] - #[display(fmt = "path is not a directory. Unable to serve static files")] + #[display("path is not a directory. Unable to serve static files")] IsNotDirectory, /// Cannot render directory. - #[display(fmt = "unable to render directory without index file")] + #[display("unable to render directory without index file")] IsDirectory, } @@ -25,19 +25,19 @@ impl ResponseError for FilesError { #[non_exhaustive] pub enum UriSegmentError { /// Segment started with the wrapped invalid character. - #[display(fmt = "segment started with invalid character: ('{_0}')")] + #[display("segment started with invalid character: ('{_0}')")] BadStart(char), /// Segment contained the wrapped invalid character. - #[display(fmt = "segment contained invalid character ('{_0}')")] + #[display("segment contained invalid character ('{_0}')")] BadChar(char), /// Segment ended with the wrapped invalid character. - #[display(fmt = "segment ended with invalid character: ('{_0}')")] + #[display("segment ended with invalid character: ('{_0}')")] BadEnd(char), /// Path is not a valid UTF-8 string after percent-decoding. - #[display(fmt = "path is not a valid UTF-8 string after percent-decoding")] + #[display("path is not a valid UTF-8 string after percent-decoding")] NotValidUtf8, } diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 167f996c..551a14fa 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -11,8 +11,7 @@ //! .service(Files::new("/static", ".").prefer_utf8(true)); //! ``` -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible, missing_docs, missing_debug_implementations)] +#![warn(missing_docs, missing_debug_implementations)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] @@ -307,11 +306,11 @@ mod tests { let resp = file.respond_to(&req); assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), - "application/javascript; charset=utf-8" + "text/javascript", ); assert_eq!( resp.headers().get(header::CONTENT_DISPOSITION).unwrap(), - "inline; filename=\"test.js\"" + "inline; filename=\"test.js\"", ); } diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index 9e4a3773..7f839890 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -21,7 +21,7 @@ use actix_web::{ Error, HttpMessage, HttpRequest, HttpResponse, Responder, }; use bitflags::bitflags; -use derive_more::{Deref, DerefMut}; +use derive_more::derive::{Deref, DerefMut}; use futures_core::future::LocalBoxFuture; use mime::Mime; diff --git a/actix-files/src/range.rs b/actix-files/src/range.rs index 528911ae..c6e34632 100644 --- a/actix-files/src/range.rs +++ b/actix-files/src/range.rs @@ -1,6 +1,6 @@ use std::fmt; -use derive_more::Error; +use derive_more::derive::Error; /// Copy of `http_range::HttpRangeParseError`. #[derive(Debug, Clone)] diff --git a/actix-files/src/service.rs b/actix-files/src/service.rs index 3d3b36c4..393ad924 100644 --- a/actix-files/src/service.rs +++ b/actix-files/src/service.rs @@ -79,7 +79,7 @@ impl FilesService { let (req, _) = req.into_parts(); - (self.renderer)(&dir, &req).unwrap_or_else(|e| ServiceResponse::from_err(e, req)) + (self.renderer)(&dir, &req).unwrap_or_else(|err| ServiceResponse::from_err(err, req)) } } diff --git a/actix-http-test/Cargo.toml b/actix-http-test/Cargo.toml index bfb0a353..7ccb70a4 100644 --- a/actix-http-test/Cargo.toml +++ b/actix-http-test/Cargo.toml @@ -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 = [] @@ -51,3 +59,6 @@ tokio = { version = "1.24.2", features = ["sync"] } [dev-dependencies] actix-http = "3" + +[lints] +workspace = true diff --git a/actix-http-test/README.md b/actix-http-test/README.md index ee242d1d..93902812 100644 --- a/actix-http-test/README.md +++ b/actix-http-test/README.md @@ -1,7 +1,5 @@ # `actix-http-test` -> Various helpers for Actix applications to use during testing. - [![crates.io](https://img.shields.io/crates/v/actix-http-test?label=latest)](https://crates.io/crates/actix-http-test) @@ -14,3 +12,9 @@ [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) + + + +Various helpers for Actix applications to use during testing. + + diff --git a/actix-http-test/src/lib.rs b/actix-http-test/src/lib.rs index 554af910..a359cec0 100644 --- a/actix-http-test/src/lib.rs +++ b/actix-http-test/src/lib.rs @@ -1,7 +1,5 @@ //! Various helpers for Actix applications to use during testing. -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] @@ -108,7 +106,7 @@ pub async fn test_server_with_addr>( builder.set_verify(SslVerifyMode::NONE); let _ = builder .set_alpn_protos(b"\x02h2\x08http/1.1") - .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e)); + .map_err(|err| log::error!("Can not set ALPN protocol: {err}")); Connector::new() .conn_lifetime(Duration::from_secs(0)) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 61eeb4be..982add26 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,27 @@ ## Unreleased +### Added + +- Add `header::CLEAR_SITE_DATA` constant. + +### Changed + +- Update `brotli` dependency to `7`. +- Minimum supported Rust version (MSRV) is now 1.75. + +## 3.9.0 + +### Added + +- Implement `FromIterator<(HeaderName, HeaderValue)>` for `HeaderMap`. + +## 3.8.0 + +### Added + +- Add `error::InvalidStatusCode` re-export. + ## 3.7.0 ### Added diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index c2551713..29420aa8 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-http" -version = "3.7.0" +version = "3.9.0" authors = [ "Nikolay Kim ", "Rob Ede ", @@ -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" @@ -89,7 +110,7 @@ ahash = "0.8" bitflags = "2" bytes = "1.7" bytestring = "1" -derive_more = "0.99.5" +derive_more = { version = "1", features = ["as_ref", "deref", "deref_mut", "display", "error", "from"] } encoding_rs = "0.8" futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } http = "0.2.7" @@ -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 } @@ -118,7 +139,7 @@ sha1 = { version = "0.10", optional = true } actix-tls = { version = "3.4", default-features = false, optional = true } # compress-* -brotli = { version = "6", optional = true } +brotli = { version = "7", optional = true } flate2 = { version = "1.0.13", optional = true } zstd = { version = "0.13", optional = true } @@ -139,13 +160,16 @@ rcgen = "0.13" regex = "1.3" rustversion = "1" rustls-pemfile = "2" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1", features = ["derive"] } serde_json = "1.0" static_assertions = "1" tls-openssl = { package = "openssl", version = "0.10.55" } tls-rustls_023 = { package = "rustls", version = "0.23" } tokio = { version = "1.24.2", features = ["net", "rt", "macros"] } +[lints] +workspace = true + [[example]] name = "ws" required-features = ["ws", "rustls-0_23"] diff --git a/actix-http/README.md b/actix-http/README.md index 0ba3fdca..f78ea86f 100644 --- a/actix-http/README.md +++ b/actix-http/README.md @@ -5,11 +5,11 @@ [![crates.io](https://img.shields.io/crates/v/actix-http?label=latest)](https://crates.io/crates/actix-http) -[![Documentation](https://docs.rs/actix-http/badge.svg?version=3.7.0)](https://docs.rs/actix-http/3.7.0) +[![Documentation](https://docs.rs/actix-http/badge.svg?version=3.9.0)](https://docs.rs/actix-http/3.9.0) ![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-http.svg)
-[![dependency status](https://deps.rs/crate/actix-http/3.7.0/status.svg)](https://deps.rs/crate/actix-http/3.7.0) +[![dependency status](https://deps.rs/crate/actix-http/3.9.0/status.svg)](https://deps.rs/crate/actix-http/3.9.0) [![Download](https://img.shields.io/crates/d/actix-http.svg)](https://crates.io/crates/actix-http) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) diff --git a/actix-http/examples/actix-web.rs b/actix-http/examples/actix-web.rs index b1feffbd..68e12301 100644 --- a/actix-http/examples/actix-web.rs +++ b/actix-http/examples/actix-web.rs @@ -3,13 +3,13 @@ use std::sync::OnceLock; use actix_http::HttpService; use actix_server::Server; use actix_service::map_config; -use actix_web::{dev::AppConfig, get, App}; +use actix_web::{dev::AppConfig, get, App, Responder}; static MEDIUM: OnceLock = OnceLock::new(); static LARGE: OnceLock = OnceLock::new(); #[get("/")] -async fn index() -> &'static str { +async fn index() -> impl Responder { "Hello, world. From Actix Web!" } diff --git a/actix-http/examples/echo.rs b/actix-http/examples/echo.rs index ae6f00cc..11fd2750 100644 --- a/actix-http/examples/echo.rs +++ b/actix-http/examples/echo.rs @@ -23,7 +23,7 @@ async fn main() -> io::Result<()> { body.extend_from_slice(&item?); } - info!("request body: {:?}", body); + info!("request body: {body:?}"); let res = Response::build(StatusCode::OK) .insert_header(("x-head", HeaderValue::from_static("dummy value!"))) @@ -31,8 +31,7 @@ async fn main() -> io::Result<()> { Ok::<_, Error>(res) }) - // No TLS - .tcp() + .tcp() // No TLS })? .run() .await diff --git a/actix-http/examples/hello-world.rs b/actix-http/examples/hello-world.rs index cf10bedd..afa3883a 100644 --- a/actix-http/examples/hello-world.rs +++ b/actix-http/examples/hello-world.rs @@ -17,7 +17,7 @@ async fn main() -> io::Result<()> { ext.insert(42u32); }) .finish(|req: Request| async move { - info!("{:?}", req); + info!("{req:?}"); let mut res = Response::build(StatusCode::OK); res.insert_header(("x-head", HeaderValue::from_static("dummy value!"))); diff --git a/actix-http/examples/streaming-error.rs b/actix-http/examples/streaming-error.rs index 8c8a249c..39f214fa 100644 --- a/actix-http/examples/streaming-error.rs +++ b/actix-http/examples/streaming-error.rs @@ -22,16 +22,16 @@ async fn main() -> io::Result<()> { .bind("streaming-error", ("127.0.0.1", 8080), || { HttpService::build() .finish(|req| async move { - info!("{:?}", req); + info!("{req:?}"); let res = Response::ok(); Ok::<_, Infallible>(res.set_body(BodyStream::new(stream! { yield Ok(Bytes::from("123")); yield Ok(Bytes::from("456")); - actix_rt::time::sleep(Duration::from_millis(1000)).await; + actix_rt::time::sleep(Duration::from_secs(1)).await; - yield Err(io::Error::new(io::ErrorKind::Other, "")); + yield Err(io::Error::new(io::ErrorKind::Other, "abc")); }))) }) .tcp() diff --git a/actix-http/examples/ws.rs b/actix-http/examples/ws.rs index fb86bc5e..af83e4c3 100644 --- a/actix-http/examples/ws.rs +++ b/actix-http/examples/ws.rs @@ -17,7 +17,6 @@ use bytes::{Bytes, BytesMut}; use bytestring::ByteString; use futures_core::{ready, Stream}; use tokio_util::codec::Encoder; -use tracing::{info, trace}; #[actix_rt::main] async fn main() -> io::Result<()> { @@ -37,12 +36,12 @@ async fn main() -> io::Result<()> { } async fn handler(req: Request) -> Result>, Error> { - info!("handshaking"); + tracing::info!("handshaking"); let mut res = ws::handshake(req.head())?; // handshake will always fail under HTTP/2 - info!("responding"); + tracing::info!("responding"); res.message_body(BodyStream::new(Heartbeat::new(ws::Codec::new()))) } @@ -64,7 +63,7 @@ impl Stream for Heartbeat { type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - trace!("poll"); + tracing::trace!("poll"); ready!(self.as_mut().interval.poll_tick(cx)); diff --git a/actix-http/src/body/body_stream.rs b/actix-http/src/body/body_stream.rs index 4574b251..2ae12c27 100644 --- a/actix-http/src/body/body_stream.rs +++ b/actix-http/src/body/body_stream.rs @@ -75,7 +75,7 @@ mod tests { time::{sleep, Sleep}, }; use actix_utils::future::poll_fn; - use derive_more::{Display, Error}; + use derive_more::derive::{Display, Error}; use futures_core::ready; use futures_util::{stream, FutureExt as _}; use pin_project_lite::pin_project; @@ -131,7 +131,7 @@ mod tests { assert_eq!(to_bytes(body).await.ok(), Some(Bytes::from("12"))); } #[derive(Debug, Display, Error)] - #[display(fmt = "stream error")] + #[display("stream error")] struct StreamErr; #[actix_rt::test] diff --git a/actix-http/src/body/utils.rs b/actix-http/src/body/utils.rs index d1449179..1fdb3ff5 100644 --- a/actix-http/src/body/utils.rs +++ b/actix-http/src/body/utils.rs @@ -3,7 +3,7 @@ use std::task::Poll; use actix_rt::pin; use actix_utils::future::poll_fn; use bytes::{Bytes, BytesMut}; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_core::ready; use super::{BodySize, MessageBody}; @@ -38,7 +38,7 @@ pub async fn to_bytes(body: B) -> Result { /// Error type returned from [`to_bytes_limited`] when body produced exceeds limit. #[derive(Debug, Display, Error)] -#[display(fmt = "limit exceeded while collecting body bytes")] +#[display("limit exceeded while collecting body bytes")] #[non_exhaustive] pub struct BodyLimitExceeded; diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 180927ac..3c9eec81 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -10,7 +10,7 @@ use std::{ use actix_rt::task::{spawn_blocking, JoinHandle}; use bytes::Bytes; -use derive_more::Display; +use derive_more::derive::Display; #[cfg(feature = "compress-gzip")] use flate2::write::{GzEncoder, ZlibEncoder}; use futures_core::ready; @@ -415,11 +415,11 @@ fn new_brotli_compressor() -> Box> { #[non_exhaustive] pub enum EncoderError { /// Wrapped body stream error. - #[display(fmt = "body")] + #[display("body")] Body(Box), /// Generic I/O error. - #[display(fmt = "io")] + #[display("io")] Io(io::Error), } diff --git a/actix-http/src/error.rs b/actix-http/src/error.rs index 69e2f14a..92126f79 100644 --- a/actix-http/src/error.rs +++ b/actix-http/src/error.rs @@ -2,8 +2,8 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Error}; -use derive_more::{Display, Error, From}; -pub use http::Error as HttpError; +use derive_more::derive::{Display, Error, From}; +pub use http::{status::InvalidStatusCode, Error as HttpError}; use http::{uri::InvalidUri, StatusCode}; use crate::{body::BoxBody, Response}; @@ -80,28 +80,28 @@ impl From for Response { #[derive(Debug, Clone, Copy, PartialEq, Eq, Display)] pub(crate) enum Kind { - #[display(fmt = "error processing HTTP")] + #[display("error processing HTTP")] Http, - #[display(fmt = "error parsing HTTP message")] + #[display("error parsing HTTP message")] Parse, - #[display(fmt = "request payload read error")] + #[display("request payload read error")] Payload, - #[display(fmt = "response body write error")] + #[display("response body write error")] Body, - #[display(fmt = "send response error")] + #[display("send response error")] SendResponse, - #[display(fmt = "error in WebSocket process")] + #[display("error in WebSocket process")] Ws, - #[display(fmt = "connection error")] + #[display("connection error")] Io, - #[display(fmt = "encoder error")] + #[display("encoder error")] Encoder, } @@ -160,44 +160,44 @@ impl From for Error { #[non_exhaustive] pub enum ParseError { /// An invalid `Method`, such as `GE.T`. - #[display(fmt = "invalid method specified")] + #[display("invalid method specified")] Method, /// An invalid `Uri`, such as `exam ple.domain`. - #[display(fmt = "URI error: {}", _0)] + #[display("URI error: {}", _0)] Uri(InvalidUri), /// An invalid `HttpVersion`, such as `HTP/1.1` - #[display(fmt = "invalid HTTP version specified")] + #[display("invalid HTTP version specified")] Version, /// An invalid `Header`. - #[display(fmt = "invalid Header provided")] + #[display("invalid Header provided")] Header, /// A message head is too large to be reasonable. - #[display(fmt = "message head is too large")] + #[display("message head is too large")] TooLarge, /// A message reached EOF, but is not complete. - #[display(fmt = "message is incomplete")] + #[display("message is incomplete")] Incomplete, /// An invalid `Status`, such as `1337 ELITE`. - #[display(fmt = "invalid status provided")] + #[display("invalid status provided")] Status, /// A timeout occurred waiting for an IO event. #[allow(dead_code)] - #[display(fmt = "timeout")] + #[display("timeout")] Timeout, /// An I/O error that occurred while trying to read or write to a network stream. - #[display(fmt = "I/O error: {}", _0)] + #[display("I/O error: {}", _0)] Io(io::Error), /// Parsing a field as string failed. - #[display(fmt = "UTF-8 error: {}", _0)] + #[display("UTF-8 error: {}", _0)] Utf8(Utf8Error), } @@ -256,28 +256,28 @@ impl From for Response { #[non_exhaustive] pub enum PayloadError { /// A payload reached EOF, but is not complete. - #[display(fmt = "payload reached EOF before completing: {:?}", _0)] + #[display("payload reached EOF before completing: {:?}", _0)] Incomplete(Option), /// Content encoding stream corruption. - #[display(fmt = "can not decode content-encoding")] + #[display("can not decode content-encoding")] EncodingCorrupted, /// Payload reached size limit. - #[display(fmt = "payload reached size limit")] + #[display("payload reached size limit")] Overflow, /// Payload length is unknown. - #[display(fmt = "payload length is unknown")] + #[display("payload length is unknown")] UnknownLength, /// HTTP/2 payload error. #[cfg(feature = "http2")] - #[display(fmt = "{}", _0)] + #[display("{}", _0)] Http2Payload(::h2::Error), /// Generic I/O error. - #[display(fmt = "{}", _0)] + #[display("{}", _0)] Io(io::Error), } @@ -326,44 +326,44 @@ impl From for Error { #[non_exhaustive] pub enum DispatchError { /// Service error. - #[display(fmt = "service error")] + #[display("service error")] Service(Response), /// Body streaming error. - #[display(fmt = "body error: {}", _0)] + #[display("body error: {}", _0)] Body(Box), /// Upgrade service error. - #[display(fmt = "upgrade error")] + #[display("upgrade error")] Upgrade, /// An `io::Error` that occurred while trying to read or write to a network stream. - #[display(fmt = "I/O error: {}", _0)] + #[display("I/O error: {}", _0)] Io(io::Error), /// Request parse error. - #[display(fmt = "request parse error: {}", _0)] + #[display("request parse error: {}", _0)] Parse(ParseError), /// HTTP/2 error. - #[display(fmt = "{}", _0)] + #[display("{}", _0)] #[cfg(feature = "http2")] H2(h2::Error), /// The first request did not complete within the specified timeout. - #[display(fmt = "request did not complete within the specified timeout")] + #[display("request did not complete within the specified timeout")] SlowRequestTimeout, /// Disconnect timeout. Makes sense for TLS streams. - #[display(fmt = "connection shutdown timeout")] + #[display("connection shutdown timeout")] DisconnectTimeout, /// Handler dropped payload before reading EOF. - #[display(fmt = "handler dropped payload before reading EOF")] + #[display("handler dropped payload before reading EOF")] HandlerDroppedPayload, /// Internal error. - #[display(fmt = "internal error")] + #[display("internal error")] InternalError, } @@ -389,11 +389,11 @@ impl StdError for DispatchError { #[non_exhaustive] pub enum ContentTypeError { /// Can not parse content type. - #[display(fmt = "could not parse content type")] + #[display("could not parse content type")] ParseError, /// Unknown content encoding. - #[display(fmt = "unknown content encoding")] + #[display("unknown content encoding")] UnknownEncoding, } diff --git a/actix-http/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs index 42d7d677..33949d2e 100644 --- a/actix-http/src/h1/encoder.rs +++ b/actix-http/src/h1/encoder.rs @@ -314,7 +314,7 @@ impl MessageType for RequestHeadType { _ => return Err(io::Error::new(io::ErrorKind::Other, "unsupported version")), } ) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) } } @@ -499,7 +499,7 @@ impl TransferEncoding { buf.extend_from_slice(b"0\r\n\r\n"); } else { writeln!(helpers::MutWriter(buf), "{:X}\r", msg.len()) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; buf.reserve(msg.len() + 2); buf.extend_from_slice(msg); diff --git a/actix-http/src/h1/service.rs b/actix-http/src/h1/service.rs index f2f8a0e4..2cf76edb 100644 --- a/actix-http/src/h1/service.rs +++ b/actix-http/src/h1/service.rs @@ -480,15 +480,15 @@ where let cfg = self.cfg.clone(); Box::pin(async move { - let expect = expect - .await - .map_err(|e| error!("Init http expect service error: {:?}", e))?; + let expect = expect.await.map_err(|err| { + tracing::error!("Initialization of HTTP expect service error: {err:?}"); + })?; let upgrade = match upgrade { Some(upgrade) => { - let upgrade = upgrade - .await - .map_err(|e| error!("Init http upgrade service error: {:?}", e))?; + let upgrade = upgrade.await.map_err(|err| { + tracing::error!("Initialization of HTTP upgrade service error: {err:?}"); + })?; Some(upgrade) } None => None, @@ -496,7 +496,7 @@ where let service = service .await - .map_err(|e| error!("Init http service error: {:?}", e))?; + .map_err(|err| error!("Initialization of HTTP service error: {err:?}"))?; Ok(H1ServiceHandler::new( cfg, @@ -541,6 +541,6 @@ where fn call(&self, (io, addr): (T, Option)) -> Self::Future { let conn_data = OnConnectData::from_io(&io, self.on_connect_ext.as_deref()); - Dispatcher::new(io, self.flow.clone(), self.cfg.clone(), addr, conn_data) + Dispatcher::new(io, Rc::clone(&self.flow), self.cfg.clone(), addr, conn_data) } } diff --git a/actix-http/src/h2/service.rs b/actix-http/src/h2/service.rs index 636ac316..debc73e5 100644 --- a/actix-http/src/h2/service.rs +++ b/actix-http/src/h2/service.rs @@ -434,7 +434,7 @@ where H2ServiceHandlerResponse { state: State::Handshake( - Some(self.flow.clone()), + Some(Rc::clone(&self.flow)), Some(self.cfg.clone()), addr, on_connect_data, diff --git a/actix-http/src/header/common.rs b/actix-http/src/header/common.rs index 6942dc26..ebdd6708 100644 --- a/actix-http/src/header/common.rs +++ b/actix-http/src/header/common.rs @@ -18,6 +18,14 @@ pub const CACHE_STATUS: HeaderName = HeaderName::from_static("cache-status"); // TODO(breaking): replace with http's version pub const CDN_CACHE_CONTROL: HeaderName = HeaderName::from_static("cdn-cache-control"); +/// Response header field that sends a signal to the user agent that it ought to remove all data of +/// a certain set of types. +/// +/// See the [W3C Clear-Site-Data spec] for full semantics. +/// +/// [W3C Clear-Site-Data spec]: https://www.w3.org/TR/clear-site-data/#header +pub const CLEAR_SITE_DATA: HeaderName = HeaderName::from_static("clear-site-data"); + /// Response header that prevents a document from loading any cross-origin resources that don't /// explicitly grant the document permission (using [CORP] or [CORS]). /// diff --git a/actix-http/src/header/map.rs b/actix-http/src/header/map.rs index b86798a4..6da01d2c 100644 --- a/actix-http/src/header/map.rs +++ b/actix-http/src/header/map.rs @@ -13,8 +13,9 @@ use super::AsHeaderName; /// `HeaderMap` is a "multi-map" of [`HeaderName`] to one or more [`HeaderValue`]s. /// /// # Examples +/// /// ``` -/// use actix_http::header::{self, HeaderMap, HeaderValue}; +/// # use actix_http::header::{self, HeaderMap, HeaderValue}; /// /// let mut map = HeaderMap::new(); /// @@ -29,6 +30,21 @@ use super::AsHeaderName; /// /// assert!(!map.contains_key(header::ORIGIN)); /// ``` +/// +/// Construct a header map using the [`FromIterator`] implementation. Note that it uses the append +/// strategy, so duplicate header names are preserved. +/// +/// ``` +/// use actix_http::header::{self, HeaderMap, HeaderValue}; +/// +/// let headers = HeaderMap::from_iter([ +/// (header::CONTENT_TYPE, HeaderValue::from_static("text/plain")), +/// (header::COOKIE, HeaderValue::from_static("foo=1")), +/// (header::COOKIE, HeaderValue::from_static("bar=1")), +/// ]); +/// +/// assert_eq!(headers.len(), 3); +/// ``` #[derive(Debug, Clone, Default)] pub struct HeaderMap { pub(crate) inner: AHashMap, @@ -368,8 +384,8 @@ impl HeaderMap { /// let removed = map.insert(header::ACCEPT, HeaderValue::from_static("text/html")); /// assert!(!removed.is_empty()); /// ``` - pub fn insert(&mut self, key: HeaderName, val: HeaderValue) -> Removed { - let value = self.inner.insert(key, Value::one(val)); + pub fn insert(&mut self, name: HeaderName, val: HeaderValue) -> Removed { + let value = self.inner.insert(name, Value::one(val)); Removed::new(value) } @@ -636,6 +652,16 @@ impl<'a> IntoIterator for &'a HeaderMap { } } +impl FromIterator<(HeaderName, HeaderValue)> for HeaderMap { + fn from_iter>(iter: T) -> Self { + iter.into_iter() + .fold(Self::new(), |mut map, (name, value)| { + map.append(name, value); + map + }) + } +} + /// Convert a `http::HeaderMap` to our `HeaderMap`. impl From for HeaderMap { fn from(mut map: http::HeaderMap) -> Self { diff --git a/actix-http/src/header/mod.rs b/actix-http/src/header/mod.rs index 79f91afe..b22c43f7 100644 --- a/actix-http/src/header/mod.rs +++ b/actix-http/src/header/mod.rs @@ -42,9 +42,9 @@ pub use self::{ as_name::AsHeaderName, // re-export list is explicit so that any updates to `http` do not conflict with this set common::{ - CACHE_STATUS, CDN_CACHE_CONTROL, CROSS_ORIGIN_EMBEDDER_POLICY, CROSS_ORIGIN_OPENER_POLICY, - CROSS_ORIGIN_RESOURCE_POLICY, PERMISSIONS_POLICY, X_FORWARDED_FOR, X_FORWARDED_HOST, - X_FORWARDED_PROTO, + CACHE_STATUS, CDN_CACHE_CONTROL, CLEAR_SITE_DATA, CROSS_ORIGIN_EMBEDDER_POLICY, + CROSS_ORIGIN_OPENER_POLICY, CROSS_ORIGIN_RESOURCE_POLICY, PERMISSIONS_POLICY, + X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO, }, into_pair::TryIntoHeaderPair, into_value::TryIntoHeaderValue, diff --git a/actix-http/src/header/shared/content_encoding.rs b/actix-http/src/header/shared/content_encoding.rs index c3b4bc4c..0b83659d 100644 --- a/actix-http/src/header/shared/content_encoding.rs +++ b/actix-http/src/header/shared/content_encoding.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use http::header::InvalidHeaderValue; use crate::{ @@ -11,7 +11,7 @@ use crate::{ /// Error returned when a content encoding is unknown. #[derive(Debug, Display, Error)] -#[display(fmt = "unsupported content encoding")] +#[display("unsupported content encoding")] pub struct ContentEncodingParseError; /// Represents a supported content encoding. diff --git a/actix-http/src/header/shared/quality.rs b/actix-http/src/header/shared/quality.rs index c2276cf1..2a76bc8c 100644 --- a/actix-http/src/header/shared/quality.rs +++ b/actix-http/src/header/shared/quality.rs @@ -1,6 +1,6 @@ use std::fmt; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; const MAX_QUALITY_INT: u16 = 1000; const MAX_QUALITY_FLOAT: f32 = 1.0; @@ -125,7 +125,7 @@ pub fn itoa_fmt(mut wr: W, value: V) -> fmt::Re } #[derive(Debug, Clone, Display, Error)] -#[display(fmt = "quality out of bounds")] +#[display("quality out of bounds")] #[non_exhaustive] pub struct QualityOutOfBounds; diff --git a/actix-http/src/lib.rs b/actix-http/src/lib.rs index 5d287a17..cf29766e 100644 --- a/actix-http/src/lib.rs +++ b/actix-http/src/lib.rs @@ -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. | @@ -20,8 +20,6 @@ //! [rustls]: https://crates.io/crates/rustls //! [trust-dns]: https://crates.io/crates/trust-dns -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![allow( clippy::type_complexity, clippy::too_many_arguments, @@ -62,13 +60,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, diff --git a/actix-http/src/message.rs b/actix-http/src/message.rs index 47b128fd..d2241b22 100644 --- a/actix-http/src/message.rs +++ b/actix-http/src/message.rs @@ -66,7 +66,7 @@ impl ops::DerefMut for Message { impl Drop for Message { fn drop(&mut self) { - T::with_pool(|p| p.release(self.head.clone())) + T::with_pool(|p| p.release(Rc::clone(&self.head))) } } diff --git a/actix-http/src/responses/builder.rs b/actix-http/src/responses/builder.rs index 91c69ba5..bb7d0f71 100644 --- a/actix-http/src/responses/builder.rs +++ b/actix-http/src/responses/builder.rs @@ -351,12 +351,9 @@ mod tests { assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain"); let resp = Response::build(StatusCode::OK) - .content_type(mime::APPLICATION_JAVASCRIPT_UTF_8) + .content_type(mime::TEXT_JAVASCRIPT) .body(Bytes::new()); - assert_eq!( - resp.headers().get(CONTENT_TYPE).unwrap(), - "application/javascript; charset=utf-8" - ); + assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/javascript"); } #[test] diff --git a/actix-http/src/service.rs b/actix-http/src/service.rs index a58be93c..3be099d9 100644 --- a/actix-http/src/service.rs +++ b/actix-http/src/service.rs @@ -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, } -#[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 { @@ -787,23 +775,23 @@ where let cfg = self.cfg.clone(); Box::pin(async move { - let expect = expect - .await - .map_err(|e| error!("Init http expect service error: {:?}", e))?; + let expect = expect.await.map_err(|err| { + tracing::error!("Initialization of HTTP expect service error: {err:?}"); + })?; let upgrade = match upgrade { Some(upgrade) => { - let upgrade = upgrade - .await - .map_err(|e| error!("Init http upgrade service error: {:?}", e))?; + let upgrade = upgrade.await.map_err(|err| { + tracing::error!("Initialization of HTTP upgrade service error: {err:?}"); + })?; Some(upgrade) } None => None, }; - let service = service - .await - .map_err(|e| error!("Init http service error: {:?}", e))?; + let service = service.await.map_err(|err| { + tracing::error!("Initialization of HTTP service error: {err:?}"); + })?; Ok(HttpServiceHandler::new( cfg, @@ -922,7 +910,7 @@ where handshake: Some(( crate::h2::handshake_with_timeout(io, &self.cfg), self.cfg.clone(), - self.flow.clone(), + Rc::clone(&self.flow), conn_data, peer_addr, )), @@ -938,7 +926,7 @@ where state: State::H1 { dispatcher: h1::Dispatcher::new( io, - self.flow.clone(), + Rc::clone(&self.flow), self.cfg.clone(), peer_addr, conn_data, diff --git a/actix-http/src/test.rs b/actix-http/src/test.rs index 3815e64c..dfa9a86c 100644 --- a/actix-http/src/test.rs +++ b/actix-http/src/test.rs @@ -159,8 +159,8 @@ impl TestBuffer { #[allow(dead_code)] pub(crate) fn clone(&self) -> Self { Self { - read_buf: self.read_buf.clone(), - write_buf: self.write_buf.clone(), + read_buf: Rc::clone(&self.read_buf), + write_buf: Rc::clone(&self.write_buf), err: self.err.clone(), } } diff --git a/actix-http/src/ws/dispatcher.rs b/actix-http/src/ws/dispatcher.rs index 1354d5ae..7d0a300b 100644 --- a/actix-http/src/ws/dispatcher.rs +++ b/actix-http/src/ws/dispatcher.rs @@ -114,14 +114,14 @@ mod inner { { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - DispatcherError::Service(ref e) => { - write!(fmt, "DispatcherError::Service({:?})", e) + DispatcherError::Service(ref err) => { + write!(fmt, "DispatcherError::Service({err:?})") } - DispatcherError::Encoder(ref e) => { - write!(fmt, "DispatcherError::Encoder({:?})", e) + DispatcherError::Encoder(ref err) => { + write!(fmt, "DispatcherError::Encoder({err:?})") } - DispatcherError::Decoder(ref e) => { - write!(fmt, "DispatcherError::Decoder({:?})", e) + DispatcherError::Decoder(ref err) => { + write!(fmt, "DispatcherError::Decoder({err:?})") } } } @@ -136,9 +136,9 @@ mod inner { { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - DispatcherError::Service(ref e) => write!(fmt, "{}", e), - DispatcherError::Encoder(ref e) => write!(fmt, "{:?}", e), - DispatcherError::Decoder(ref e) => write!(fmt, "{:?}", e), + DispatcherError::Service(ref err) => write!(fmt, "{err}"), + DispatcherError::Encoder(ref err) => write!(fmt, "{err:?}"), + DispatcherError::Decoder(ref err) => write!(fmt, "{err:?}"), } } } diff --git a/actix-http/src/ws/frame.rs b/actix-http/src/ws/frame.rs index a70063a4..c95c1508 100644 --- a/actix-http/src/ws/frame.rs +++ b/actix-http/src/ws/frame.rs @@ -178,17 +178,14 @@ impl Parser { }; if payload_len < 126 { - dst.buffer_mut() - .reserve(p_len + 2 + if mask { 4 } else { 0 }); + dst.buffer_mut().reserve(p_len + 2); dst.buffer_mut().put_slice(&[one, two | payload_len as u8]); } else if payload_len <= 65_535 { - dst.buffer_mut() - .reserve(p_len + 4 + if mask { 4 } else { 0 }); + dst.buffer_mut().reserve(p_len + 4); dst.buffer_mut().put_slice(&[one, two | 126]); dst.buffer_mut().put_u16(payload_len as u16); } else { - dst.buffer_mut() - .reserve(p_len + 10 + if mask { 4 } else { 0 }); + dst.buffer_mut().reserve(p_len + 10); dst.buffer_mut().put_slice(&[one, two | 127]); dst.buffer_mut().put_u64(payload_len as u64); }; diff --git a/actix-http/src/ws/mod.rs b/actix-http/src/ws/mod.rs index 3ed53b70..88053b25 100644 --- a/actix-http/src/ws/mod.rs +++ b/actix-http/src/ws/mod.rs @@ -5,7 +5,7 @@ use std::io; -use derive_more::{Display, Error, From}; +use derive_more::derive::{Display, Error, From}; use http::{header, Method, StatusCode}; use crate::{body::BoxBody, header::HeaderValue, RequestHead, Response, ResponseBuilder}; @@ -27,43 +27,43 @@ pub use self::{ #[derive(Debug, Display, Error, From)] pub enum ProtocolError { /// Received an unmasked frame from client. - #[display(fmt = "received an unmasked frame from client")] + #[display("received an unmasked frame from client")] UnmaskedFrame, /// Received a masked frame from server. - #[display(fmt = "received a masked frame from server")] + #[display("received a masked frame from server")] MaskedFrame, /// Encountered invalid opcode. - #[display(fmt = "invalid opcode ({})", _0)] + #[display("invalid opcode ({})", _0)] InvalidOpcode(#[error(not(source))] u8), /// Invalid control frame length - #[display(fmt = "invalid control frame length ({})", _0)] + #[display("invalid control frame length ({})", _0)] InvalidLength(#[error(not(source))] usize), /// Bad opcode. - #[display(fmt = "bad opcode")] + #[display("bad opcode")] BadOpCode, /// A payload reached size limit. - #[display(fmt = "payload reached size limit")] + #[display("payload reached size limit")] Overflow, /// Continuation has not started. - #[display(fmt = "continuation has not started")] + #[display("continuation has not started")] ContinuationNotStarted, /// Received new continuation but it is already started. - #[display(fmt = "received new continuation but it has already started")] + #[display("received new continuation but it has already started")] ContinuationStarted, /// Unknown continuation fragment. - #[display(fmt = "unknown continuation fragment: {}", _0)] + #[display("unknown continuation fragment: {}", _0)] ContinuationFragment(#[error(not(source))] OpCode), /// I/O error. - #[display(fmt = "I/O error: {}", _0)] + #[display("I/O error: {}", _0)] Io(io::Error), } @@ -71,27 +71,27 @@ pub enum ProtocolError { #[derive(Debug, Clone, Copy, PartialEq, Eq, Display, Error)] pub enum HandshakeError { /// Only get method is allowed. - #[display(fmt = "method not allowed")] + #[display("method not allowed")] GetMethodRequired, /// Upgrade header if not set to WebSocket. - #[display(fmt = "WebSocket upgrade is expected")] + #[display("WebSocket upgrade is expected")] NoWebsocketUpgrade, /// Connection header is not set to upgrade. - #[display(fmt = "connection upgrade is expected")] + #[display("connection upgrade is expected")] NoConnectionUpgrade, /// WebSocket version header is not set. - #[display(fmt = "WebSocket version header is required")] + #[display("WebSocket version header is required")] NoVersionHeader, /// Unsupported WebSocket version. - #[display(fmt = "unsupported WebSocket version")] + #[display("unsupported WebSocket version")] UnsupportedVersion, /// WebSocket key is not set or wrong. - #[display(fmt = "unknown WebSocket key")] + #[display("unknown WebSocket key")] BadWebsocketKey, } diff --git a/actix-http/tests/test_client.rs b/actix-http/tests/test_client.rs index 5888527f..c0019d06 100644 --- a/actix-http/tests/test_client.rs +++ b/actix-http/tests/test_client.rs @@ -5,7 +5,7 @@ use actix_http_test::test_server; use actix_service::ServiceFactoryExt; use actix_utils::future; use bytes::Bytes; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_util::StreamExt as _; const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ @@ -94,7 +94,7 @@ async fn with_query_parameter() { } #[derive(Debug, Display, Error)] -#[display(fmt = "expect failed")] +#[display("expect failed")] struct ExpectFailed; impl From for Response { diff --git a/actix-http/tests/test_openssl.rs b/actix-http/tests/test_openssl.rs index 4dd22b58..77a66f32 100644 --- a/actix-http/tests/test_openssl.rs +++ b/actix-http/tests/test_openssl.rs @@ -14,7 +14,7 @@ use actix_http_test::test_server; use actix_service::{fn_service, ServiceFactoryExt}; use actix_utils::future::{err, ok, ready}; use bytes::{Bytes, BytesMut}; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_core::Stream; use futures_util::{stream::once, StreamExt as _}; use openssl::{ @@ -398,7 +398,7 @@ async fn h2_response_http_error_handling() { } #[derive(Debug, Display, Error)] -#[display(fmt = "error")] +#[display("error")] struct BadRequest; impl From for Response { diff --git a/actix-http/tests/test_rustls.rs b/actix-http/tests/test_rustls.rs index 3ca0d94c..5b02637d 100644 --- a/actix-http/tests/test_rustls.rs +++ b/actix-http/tests/test_rustls.rs @@ -23,7 +23,7 @@ use actix_service::{fn_factory_with_config, fn_service}; use actix_tls::connect::rustls_0_23::webpki_roots_cert_store; use actix_utils::future::{err, ok, poll_fn}; use bytes::{Bytes, BytesMut}; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_core::{ready, Stream}; use futures_util::stream::once; use rustls::{pki_types::ServerName, ServerConfig as RustlsServerConfig}; @@ -480,7 +480,7 @@ async fn h2_response_http_error_handling() { } #[derive(Debug, Display, Error)] -#[display(fmt = "error")] +#[display("error")] struct BadRequest; impl From for Response { diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index 4ba64a53..e93d3559 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -14,7 +14,7 @@ use actix_rt::{net::TcpStream, time::sleep}; use actix_service::fn_service; use actix_utils::future::{err, ok, ready}; use bytes::Bytes; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_util::{stream::once, FutureExt as _, StreamExt as _}; use regex::Regex; @@ -62,7 +62,7 @@ async fn h1_2() { } #[derive(Debug, Display, Error)] -#[display(fmt = "expect failed")] +#[display("expect failed")] struct ExpectFailed; impl From for Response { @@ -723,7 +723,7 @@ async fn h1_response_http_error_handling() { } #[derive(Debug, Display, Error)] -#[display(fmt = "error")] +#[display("error")] struct BadRequest; impl From for Response { diff --git a/actix-http/tests/test_ws.rs b/actix-http/tests/test_ws.rs index 9a78074c..e9c4a01a 100644 --- a/actix-http/tests/test_ws.rs +++ b/actix-http/tests/test_ws.rs @@ -14,7 +14,7 @@ use actix_http::{ use actix_http_test::test_server; use actix_service::{fn_factory, Service}; use bytes::Bytes; -use derive_more::{Display, Error, From}; +use derive_more::derive::{Display, Error, From}; use futures_core::future::LocalBoxFuture; use futures_util::{SinkExt as _, StreamExt as _}; @@ -37,16 +37,16 @@ impl WsService { #[derive(Debug, Display, Error, From)] enum WsServiceError { - #[display(fmt = "HTTP error")] + #[display("HTTP error")] Http(actix_http::Error), - #[display(fmt = "WS handshake error")] + #[display("WS handshake error")] Ws(actix_http::ws::HandshakeError), - #[display(fmt = "I/O error")] + #[display("I/O error")] Io(std::io::Error), - #[display(fmt = "dispatcher error")] + #[display("dispatcher error")] Dispatcher, } diff --git a/actix-multipart-derive/CHANGES.md b/actix-multipart-derive/CHANGES.md index 1b44ba4b..d0c75929 100644 --- a/actix-multipart-derive/CHANGES.md +++ b/actix-multipart-derive/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 0.7.0 + - Minimum supported Rust version (MSRV) is now 1.72. ## 0.6.1 diff --git a/actix-multipart-derive/Cargo.toml b/actix-multipart-derive/Cargo.toml index e978864a..964ef2b7 100644 --- a/actix-multipart-derive/Cargo.toml +++ b/actix-multipart-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-multipart-derive" -version = "0.6.1" +version = "0.7.0" authors = ["Jacob Halsey "] description = "Multipart form derive macro for Actix Web" keywords = ["http", "web", "framework", "async", "futures"] @@ -25,7 +25,10 @@ quote = "1" syn = "2" [dev-dependencies] -actix-multipart = "0.6" +actix-multipart = "0.7" actix-web = "4" rustversion = "1" trybuild = "1" + +[lints] +workspace = true diff --git a/actix-multipart-derive/README.md b/actix-multipart-derive/README.md index ec0afffd..bf75613e 100644 --- a/actix-multipart-derive/README.md +++ b/actix-multipart-derive/README.md @@ -5,11 +5,11 @@ [![crates.io](https://img.shields.io/crates/v/actix-multipart-derive?label=latest)](https://crates.io/crates/actix-multipart-derive) -[![Documentation](https://docs.rs/actix-multipart-derive/badge.svg?version=0.6.1)](https://docs.rs/actix-multipart-derive/0.6.1) +[![Documentation](https://docs.rs/actix-multipart-derive/badge.svg?version=0.7.0)](https://docs.rs/actix-multipart-derive/0.7.0) ![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-multipart-derive.svg)
-[![dependency status](https://deps.rs/crate/actix-multipart-derive/0.6.1/status.svg)](https://deps.rs/crate/actix-multipart-derive/0.6.1) +[![dependency status](https://deps.rs/crate/actix-multipart-derive/0.7.0/status.svg)](https://deps.rs/crate/actix-multipart-derive/0.7.0) [![Download](https://img.shields.io/crates/d/actix-multipart-derive.svg)](https://crates.io/crates/actix-multipart-derive) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) diff --git a/actix-multipart-derive/src/lib.rs b/actix-multipart-derive/src/lib.rs index 9552ad2d..57d2fa87 100644 --- a/actix-multipart-derive/src/lib.rs +++ b/actix-multipart-derive/src/lib.rs @@ -2,11 +2,10 @@ //! //! See [`macro@MultipartForm`] for usage examples. -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![allow(clippy::disallowed_names)] // false positives in some macro expansions use std::collections::HashSet; @@ -37,6 +36,7 @@ struct MultipartFormAttrs { duplicate_field: DuplicateField, } +#[allow(clippy::disallowed_names)] // false positive in macro expansion #[derive(FromField, Default)] #[darling(attributes(multipart), default)] struct FieldAttrs { @@ -138,7 +138,7 @@ struct ParsedField<'t> { /// `#[multipart(duplicate_field = "")]` attribute: /// /// - "ignore": (default) Extra fields are ignored. I.e., the first one is persisted. -/// - "deny": A `MultipartError::UnsupportedField` error response is returned. +/// - "deny": A `MultipartError::UnknownField` error response is returned. /// - "replace": Each field is processed, but only the last one is persisted. /// /// Note that `Vec` fields will ignore this option. @@ -229,7 +229,7 @@ pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenS // Return value when a field name is not supported by the form let unknown_field_result = if attrs.deny_unknown_fields { quote!(::std::result::Result::Err( - ::actix_multipart::MultipartError::UnsupportedField(field.name().to_string()) + ::actix_multipart::MultipartError::UnknownField(field.name().unwrap().to_string()) )) } else { quote!(::std::result::Result::Ok(())) @@ -292,7 +292,7 @@ pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenS limits: &'t mut ::actix_multipart::form::Limits, state: &'t mut ::actix_multipart::form::State, ) -> ::std::pin::Pin<::std::boxed::Box> + 't>> { - match field.name() { + match field.name().unwrap() { #handle_field_impl _ => return ::std::boxed::Box::pin(::std::future::ready(#unknown_field_result)), } diff --git a/actix-multipart/CHANGES.md b/actix-multipart/CHANGES.md index 196d2ca9..a030fac4 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,30 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + +## 0.7.2 + +- Fix re-exported version of `actix-multipart-derive`. + +## 0.7.1 + +- Expose `LimitExceeded` error type. + +## 0.7.0 + +- Add `MultipartError::ContentTypeIncompatible` variant. +- Add `MultipartError::ContentDispositionNameMissing` variant. +- Add `Field::bytes()` method. +- Rename `MultipartError::{NoContentDisposition => ContentDispositionMissing}` variant. +- Rename `MultipartError::{NoContentType => ContentTypeMissing}` variant. +- Rename `MultipartError::{ParseContentType => ContentTypeParse}` variant. +- Rename `MultipartError::{Boundary => BoundaryMissing}` variant. +- Rename `MultipartError::{UnsupportedField => UnknownField}` variant. +- Remove top-level re-exports of `test` utilities. + +## 0.6.2 + - Add testing utilities under new module `test`. - Minimum supported Rust version (MSRV) is now 1.72. diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index 6e36c339..aa33bfc9 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -1,33 +1,48 @@ [package] name = "actix-multipart" -version = "0.6.1" +version = "0.7.2" authors = [ "Nikolay Kim ", "Jacob Halsey ", + "Rob Ede ", ] -description = "Multipart form support for Actix Web" -keywords = ["http", "web", "framework", "async", "futures"] -homepage = "https://actix.rs" -repository = "https://github.com/actix/actix-web" -license = "MIT OR Apache-2.0" -edition = "2021" +description = "Multipart request & form support for Actix Web" +keywords = ["http", "actix", "web", "multipart", "form"] +homepage.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true [package.metadata.docs.rs] 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"] tempfile = ["dep:tempfile", "tokio/fs"] [dependencies] -actix-multipart-derive = { version = "=0.6.1", optional = true } +actix-multipart-derive = { version = "=0.7.0", optional = true } actix-utils = "3" actix-web = { version = "4", default-features = false } -bytes = "1" -derive_more = "0.99.5" +derive_more = { version = "1", features = ["display", "error", "from"] } futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] } httparse = "1.3" @@ -48,8 +63,14 @@ actix-multipart-rfc7578 = "0.10" actix-rt = "2.2" actix-test = "0.1" actix-web = "4" +assert_matches = "1" awc = "3" +env_logger = "0.11" futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] } +futures-test = "0.3" multer = "3" tokio = { version = "1.24.2", features = ["sync"] } tokio-stream = "0.1" + +[lints] +workspace = true diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 83947b0c..ec2e94bd 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -1,38 +1,32 @@ # `actix-multipart` -> Multipart form support for Actix Web. - [![crates.io](https://img.shields.io/crates/v/actix-multipart?label=latest)](https://crates.io/crates/actix-multipart) -[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.6.1)](https://docs.rs/actix-multipart/0.6.1) +[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.7.2)](https://docs.rs/actix-multipart/0.7.2) ![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-multipart.svg)
-[![dependency status](https://deps.rs/crate/actix-multipart/0.6.1/status.svg)](https://deps.rs/crate/actix-multipart/0.6.1) +[![dependency status](https://deps.rs/crate/actix-multipart/0.7.2/status.svg)](https://deps.rs/crate/actix-multipart/0.7.2) [![Download](https://img.shields.io/crates/d/actix-multipart.svg)](https://crates.io/crates/actix-multipart) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) + -## Example +Multipart request & form support for Actix Web. -Dependencies: +The [`Multipart`] extractor aims to support all kinds of `multipart/*` requests, including `multipart/form-data`, `multipart/related` and `multipart/mixed`. This is a lower-level extractor which supports reading [multipart fields](Field), in the order they are sent by the client. -```toml -[dependencies] -actix-multipart = "0.6" -actix-web = "4.5" -serde = { version = "1.0", features = ["derive"] } -``` +Due to additional requirements for `multipart/form-data` requests, the higher level [`MultipartForm`] extractor and derive macro only supports this media type. -Code: +## Examples ```rust use actix_web::{post, App, HttpServer, Responder}; -use actix_multipart::form::{json::Json as MPJson, tempfile::TempFile, MultipartForm}; +use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm}; use serde::Deserialize; #[derive(Debug, Deserialize)] @@ -44,7 +38,7 @@ struct Metadata { struct UploadForm { #[multipart(limit = "100MB")] file: TempFile, - json: MPJson, + json: MpJson, } #[post("/videos")] @@ -64,15 +58,17 @@ async fn main() -> std::io::Result<()> { } ``` -Curl request : -```bash +cURL request: + +```sh curl -v --request POST \ --url http://localhost:8080/videos \ -F 'json={"name": "Cargo.lock"};type=application/json' \ -F file=@./Cargo.lock ``` +[`MultipartForm`]: struct@form::MultipartForm -### Examples + -https://github.com/actix/examples/tree/master/forms/multipart \ No newline at end of file +[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) diff --git a/actix-multipart/examples/form.rs b/actix-multipart/examples/form.rs new file mode 100644 index 00000000..a90aeff9 --- /dev/null +++ b/actix-multipart/examples/form.rs @@ -0,0 +1,36 @@ +use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm}; +use actix_web::{middleware::Logger, post, App, HttpServer, Responder}; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct Metadata { + name: String, +} + +#[derive(Debug, MultipartForm)] +struct UploadForm { + #[multipart(limit = "100MB")] + file: TempFile, + json: MpJson, +} + +#[post("/videos")] +async fn post_video(MultipartForm(form): MultipartForm) -> impl Responder { + format!( + "Uploaded file {}, with size: {}\ntemporary file ({}) was deleted\n", + form.json.name, + form.file.size, + form.file.file.path().display(), + ) +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + HttpServer::new(move || App::new().service(post_video).wrap(Logger::default())) + .workers(2) + .bind(("127.0.0.1", 8080))? + .run() + .await +} diff --git a/actix-multipart/src/error.rs b/actix-multipart/src/error.rs index 77b5a559..8fff5c59 100644 --- a/actix-multipart/src/error.rs +++ b/actix-multipart/src/error.rs @@ -5,83 +5,101 @@ use actix_web::{ http::StatusCode, ResponseError, }; -use derive_more::{Display, Error, From}; +use derive_more::derive::{Display, Error, From}; /// A set of errors that can occur during parsing multipart streams. #[derive(Debug, Display, From, Error)] #[non_exhaustive] -pub enum MultipartError { - /// Content-Disposition header is not found or is not equal to "form-data". +pub enum Error { + /// Could not find Content-Type header. + #[display("Could not find Content-Type header")] + ContentTypeMissing, + + /// Could not parse Content-Type header. + #[display("Could not parse Content-Type header")] + ContentTypeParse, + + /// Parsed Content-Type did not have "multipart" top-level media type. /// - /// According to [RFC 7578 §4.2](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2) a - /// Content-Disposition header must always be present and equal to "form-data". - #[display(fmt = "No Content-Disposition `form-data` header")] - NoContentDisposition, + /// Also raised when extracting a [`MultipartForm`] from a request that does not have the + /// "multipart/form-data" media type. + /// + /// [`MultipartForm`]: struct@crate::form::MultipartForm + #[display("Parsed Content-Type did not have 'multipart' top-level media type")] + ContentTypeIncompatible, - /// Content-Type header is not found - #[display(fmt = "No Content-Type header found")] - NoContentType, + /// Multipart boundary is not found. + #[display("Multipart boundary is not found")] + BoundaryMissing, - /// Can not parse Content-Type header - #[display(fmt = "Can not parse Content-Type header")] - ParseContentType, + /// Content-Disposition header was not found or not of disposition type "form-data" when parsing + /// a "form-data" field. + /// + /// As per [RFC 7578 §4.2], a "multipart/form-data" field's Content-Disposition header must + /// always be present and have a disposition type of "form-data". + /// + /// [RFC 7578 §4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 + #[display("Content-Disposition header was not found when parsing a \"form-data\" field")] + ContentDispositionMissing, - /// Multipart boundary is not found - #[display(fmt = "Multipart boundary is not found")] - Boundary, + /// Content-Disposition name parameter was not found when parsing a "form-data" field. + /// + /// As per [RFC 7578 §4.2], a "multipart/form-data" field's Content-Disposition header must + /// always include a "name" parameter. + /// + /// [RFC 7578 §4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 + #[display("Content-Disposition header was not found when parsing a \"form-data\" field")] + ContentDispositionNameMissing, - /// Nested multipart is not supported - #[display(fmt = "Nested multipart is not supported")] + /// Nested multipart is not supported. + #[display("Nested multipart is not supported")] Nested, - /// Multipart stream is incomplete - #[display(fmt = "Multipart stream is incomplete")] + /// Multipart stream is incomplete. + #[display("Multipart stream is incomplete")] Incomplete, - /// Error during field parsing - #[display(fmt = "{}", _0)] + /// Field parsing failed. + #[display("Error during field parsing")] Parse(ParseError), - /// Payload error - #[display(fmt = "{}", _0)] + /// HTTP payload error. + #[display("Payload error")] Payload(PayloadError), - /// Not consumed - #[display(fmt = "Multipart stream is not consumed")] + /// Stream is not consumed. + #[display("Stream is not consumed")] NotConsumed, - /// An error from a field handler in a form - #[display( - fmt = "An error occurred processing field `{}`: {}", - field_name, - source - )] + /// Form field handler raised error. + #[display("An error occurred processing field: {name}")] Field { - field_name: String, + name: String, source: actix_web::Error, }, - /// Duplicate field - #[display(fmt = "Duplicate field found for: `{}`", _0)] + /// Duplicate field found (for structure that opted-in to denying duplicate fields). + #[display("Duplicate field found: {_0}")] #[from(ignore)] DuplicateField(#[error(not(source))] String), - /// Missing field - #[display(fmt = "Field with name `{}` is required", _0)] + /// Required field is missing. + #[display("Required field is missing: {_0}")] #[from(ignore)] MissingField(#[error(not(source))] String), - /// Unknown field - #[display(fmt = "Unsupported field `{}`", _0)] + /// Unknown field (for structure that opted-in to denying unknown fields). + #[display("Unknown field: {_0}")] #[from(ignore)] - UnsupportedField(#[error(not(source))] String), + UnknownField(#[error(not(source))] String), } -/// Return `BadRequest` for `MultipartError` -impl ResponseError for MultipartError { +/// Return `BadRequest` for `MultipartError`. +impl ResponseError for Error { fn status_code(&self) -> StatusCode { match &self { - MultipartError::Field { source, .. } => source.as_response_error().status_code(), + Error::Field { source, .. } => source.as_response_error().status_code(), + Error::ContentTypeIncompatible => StatusCode::UNSUPPORTED_MEDIA_TYPE, _ => StatusCode::BAD_REQUEST, } } @@ -93,7 +111,7 @@ mod tests { #[test] fn test_multipart_error() { - let resp = MultipartError::Boundary.error_response(); + let resp = Error::BoundaryMissing.error_response(); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); } } diff --git a/actix-multipart/src/extractor.rs b/actix-multipart/src/extractor.rs index 56ed69ae..31999228 100644 --- a/actix-multipart/src/extractor.rs +++ b/actix-multipart/src/extractor.rs @@ -1,21 +1,20 @@ -//! Multipart payload support - use actix_utils::future::{ready, Ready}; use actix_web::{dev::Payload, Error, FromRequest, HttpRequest}; -use crate::server::Multipart; +use crate::multipart::Multipart; -/// Get request's payload as multipart stream. +/// Extract request's payload as multipart stream. /// -/// Content-type: multipart/form-data; +/// Content-type: multipart/*; /// /// # Examples +/// /// ``` -/// use actix_web::{web, HttpResponse, Error}; +/// use actix_web::{web, HttpResponse}; /// use actix_multipart::Multipart; /// use futures_util::StreamExt as _; /// -/// async fn index(mut payload: Multipart) -> Result { +/// async fn index(mut payload: Multipart) -> actix_web::Result { /// // iterate over multipart stream /// while let Some(item) = payload.next().await { /// let mut field = item?; @@ -26,7 +25,7 @@ use crate::server::Multipart; /// } /// } /// -/// Ok(HttpResponse::Ok().into()) +/// Ok(HttpResponse::Ok().finish()) /// } /// ``` impl FromRequest for Multipart { @@ -35,9 +34,6 @@ impl FromRequest for Multipart { #[inline] fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { - ready(Ok(match Multipart::boundary(req.headers()) { - Ok(boundary) => Multipart::from_boundary(boundary, payload.take()), - Err(err) => Multipart::from_error(err), - })) + ready(Ok(Multipart::from_req(req, payload))) } } diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs new file mode 100644 index 00000000..b42d8ae1 --- /dev/null +++ b/actix-multipart/src/field.rs @@ -0,0 +1,501 @@ +use std::{ + cell::RefCell, + cmp, fmt, + future::poll_fn, + mem, + pin::Pin, + rc::Rc, + task::{ready, Context, Poll}, +}; + +use actix_web::{ + error::PayloadError, + http::header::{self, ContentDisposition, HeaderMap}, + web::{Bytes, BytesMut}, +}; +use derive_more::derive::{Display, Error}; +use futures_core::Stream; +use mime::Mime; + +use crate::{ + error::Error, + payload::{PayloadBuffer, PayloadRef}, + safety::Safety, +}; + +/// Error type returned from [`Field::bytes()`] when field data is larger than limit. +#[derive(Debug, Display, Error)] +#[display("size limit exceeded while collecting field data")] +#[non_exhaustive] +pub struct LimitExceeded; + +/// A single field in a multipart stream. +pub struct Field { + /// Field's Content-Type. + content_type: Option, + + /// Field's Content-Disposition. + content_disposition: Option, + + /// Form field name. + /// + /// A non-optional storage for form field names to avoid unwraps in `form` module. Will be an + /// empty string in non-form contexts. + /// + // INVARIANT: always non-empty when request content-type is multipart/form-data. + pub(crate) form_field_name: String, + + /// Field's header map. + headers: HeaderMap, + + safety: Safety, + inner: Rc>, +} + +impl Field { + pub(crate) fn new( + content_type: Option, + content_disposition: Option, + form_field_name: Option, + headers: HeaderMap, + safety: Safety, + inner: Rc>, + ) -> Self { + Field { + content_type, + content_disposition, + form_field_name: form_field_name.unwrap_or_default(), + headers, + inner, + safety, + } + } + + /// Returns a reference to the field's header map. + pub fn headers(&self) -> &HeaderMap { + &self.headers + } + + /// Returns a reference to the field's content (mime) type, if it is supplied by the client. + /// + /// According to [RFC 7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.4), if it is not + /// present, it should default to "text/plain". Note it is the responsibility of the client to + /// provide the appropriate content type, there is no attempt to validate this by the server. + pub fn content_type(&self) -> Option<&Mime> { + self.content_type.as_ref() + } + + /// Returns this field's parsed Content-Disposition header, if set. + /// + /// # Validation + /// + /// Per [RFC 7578 §4.2], the parts of a multipart/form-data payload MUST contain a + /// Content-Disposition header field where the disposition type is `form-data` and MUST also + /// contain an additional parameter of `name` with its value being the original field name from + /// the form. This requirement is enforced during extraction for multipart/form-data requests, + /// but not other kinds of multipart requests (such as multipart/related). + /// + /// As such, it is safe to `.unwrap()` calls `.content_disposition()` if you've verified. + /// + /// The [`name()`](Self::name) method is also provided as a convenience for obtaining the + /// aforementioned name parameter. + /// + /// [RFC 7578 §4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 + pub fn content_disposition(&self) -> Option<&ContentDisposition> { + self.content_disposition.as_ref() + } + + /// Returns the field's name, if set. + /// + /// See [`content_disposition()`](Self::content_disposition) regarding guarantees on presence of + /// the "name" field. + pub fn name(&self) -> Option<&str> { + self.content_disposition()?.get_name() + } + + /// Collects the raw field data, up to `limit` bytes. + /// + /// # Errors + /// + /// Any errors produced by the data stream are returned as `Ok(Err(Error))` immediately. + /// + /// If the buffered data size would exceed `limit`, an `Err(LimitExceeded)` is returned. Note + /// that, in this case, the full data stream is exhausted before returning the error so that + /// subsequent fields can still be read. To better defend against malicious/infinite requests, + /// it is advisable to also put a timeout on this call. + pub async fn bytes(&mut self, limit: usize) -> Result, LimitExceeded> { + /// Sensible default (2kB) for initial, bounded allocation when collecting body bytes. + const INITIAL_ALLOC_BYTES: usize = 2 * 1024; + + let mut exceeded_limit = false; + let mut buf = BytesMut::with_capacity(INITIAL_ALLOC_BYTES); + + let mut field = Pin::new(self); + + match poll_fn(|cx| loop { + match ready!(field.as_mut().poll_next(cx)) { + // if already over limit, discard chunk to advance multipart request + Some(Ok(_chunk)) if exceeded_limit => {} + + // if limit is exceeded set flag to true and continue + Some(Ok(chunk)) if buf.len() + chunk.len() > limit => { + exceeded_limit = true; + // eagerly de-allocate field data buffer + let _ = mem::take(&mut buf); + } + + Some(Ok(chunk)) => buf.extend_from_slice(&chunk), + + None => return Poll::Ready(Ok(())), + Some(Err(err)) => return Poll::Ready(Err(err)), + } + }) + .await + { + // propagate error returned from body poll + Err(err) => Ok(Err(err)), + + // limit was exceeded while reading body + Ok(()) if exceeded_limit => Err(LimitExceeded), + + // otherwise return body buffer + Ok(()) => Ok(Ok(buf.freeze())), + } + } +} + +impl Stream for Field { + type Item = Result; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let this = self.get_mut(); + let mut inner = this.inner.borrow_mut(); + + 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() { + // safety violation + return Poll::Ready(Some(Err(Error::NotConsumed))); + } else { + return Poll::Pending; + } + + inner.poll(&this.safety) + } +} + +impl fmt::Debug for Field { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(ct) = &self.content_type { + writeln!(f, "\nField: {}", ct)?; + } else { + writeln!(f, "\nField:")?; + } + writeln!(f, " boundary: {}", self.inner.borrow().boundary)?; + writeln!(f, " headers:")?; + for (key, val) in self.headers.iter() { + writeln!(f, " {:?}: {:?}", key, val)?; + } + Ok(()) + } +} + +pub(crate) struct InnerField { + /// Payload is initialized as Some and is `take`n when the field stream finishes. + payload: Option, + + /// Field boundary (without "--" prefix). + boundary: String, + + /// True if request payload has been exhausted. + eof: bool, + + /// Field data's stated size according to it's Content-Length header. + length: Option, +} + +impl InnerField { + pub(crate) fn new_in_rc( + payload: PayloadRef, + boundary: String, + headers: &HeaderMap, + ) -> Result>, PayloadError> { + Self::new(payload, boundary, headers).map(|this| Rc::new(RefCell::new(this))) + } + + pub(crate) fn new( + payload: PayloadRef, + boundary: String, + headers: &HeaderMap, + ) -> Result { + let len = if let Some(len) = headers.get(&header::CONTENT_LENGTH) { + match len.to_str().ok().and_then(|len| len.parse::().ok()) { + Some(len) => Some(len), + None => return Err(PayloadError::Incomplete(None)), + } + } else { + None + }; + + Ok(InnerField { + boundary, + payload: Some(payload), + eof: false, + length: len, + }) + } + + /// Reads body part content chunk of the specified size. + /// + /// The body part must has `Content-Length` header with proper value. + pub(crate) fn read_len( + payload: &mut PayloadBuffer, + size: &mut u64, + ) -> Poll>> { + if *size == 0 { + Poll::Ready(None) + } else { + match payload.read_max(*size)? { + Some(mut chunk) => { + let len = cmp::min(chunk.len() as u64, *size); + *size -= len; + let ch = chunk.split_to(len as usize); + if !chunk.is_empty() { + payload.unprocessed(chunk); + } + Poll::Ready(Some(Ok(ch))) + } + None => { + if payload.eof && (*size != 0) { + Poll::Ready(Some(Err(Error::Incomplete))) + } else { + Poll::Pending + } + } + } + } + } + + /// Reads content chunk of body part with unknown length. + /// + /// The `Content-Length` header for body part is not necessary. + pub(crate) fn read_stream( + payload: &mut PayloadBuffer, + boundary: &str, + ) -> Poll>> { + let mut pos = 0; + + let len = payload.buf.len(); + + if len == 0 { + return if payload.eof { + Poll::Ready(Some(Err(Error::Incomplete))) + } else { + Poll::Pending + }; + } + + // check boundary + if len > 4 && payload.buf[0] == b'\r' { + let b_len = if payload.buf.starts_with(b"\r\n") && &payload.buf[2..4] == b"--" { + Some(4) + } else if &payload.buf[1..3] == b"--" { + Some(3) + } else { + None + }; + + if let Some(b_len) = b_len { + let b_size = boundary.len() + b_len; + if len < b_size { + return Poll::Pending; + } else if &payload.buf[b_len..b_size] == boundary.as_bytes() { + // found boundary + return Poll::Ready(None); + } + } + } + + loop { + return if let Some(idx) = memchr::memmem::find(&payload.buf[pos..], b"\r") { + let cur = pos + idx; + + // check if we have enough data for boundary detection + if cur + 4 > len { + if cur > 0 { + Poll::Ready(Some(Ok(payload.buf.split_to(cur).freeze()))) + } else { + Poll::Pending + } + } else { + // check boundary + if (&payload.buf[cur..cur + 2] == b"\r\n" + && &payload.buf[cur + 2..cur + 4] == b"--") + || (&payload.buf[cur..=cur] == b"\r" + && &payload.buf[cur + 1..cur + 3] == b"--") + { + if cur != 0 { + // return buffer + Poll::Ready(Some(Ok(payload.buf.split_to(cur).freeze()))) + } else { + pos = cur + 1; + continue; + } + } else { + // not boundary + pos = cur + 1; + continue; + } + } + } else { + Poll::Ready(Some(Ok(payload.buf.split().freeze()))) + }; + } + } + + pub(crate) fn poll(&mut self, safety: &Safety) -> Poll>> { + if self.payload.is_none() { + return Poll::Ready(None); + } + + let Some(mut payload) = self + .payload + .as_ref() + .expect("Field should not be polled after completion") + .get_mut(safety) + else { + return Poll::Pending; + }; + + if !self.eof { + let res = if let Some(ref mut len) = self.length { + Self::read_len(&mut payload, len) + } else { + Self::read_stream(&mut payload, &self.boundary) + }; + + match ready!(res) { + Some(Ok(bytes)) => return Poll::Ready(Some(Ok(bytes))), + Some(Err(err)) => return Poll::Ready(Some(Err(err))), + None => self.eof = true, + } + } + + let result = match payload.readline() { + Ok(None) => Poll::Pending, + Ok(Some(line)) => { + if line.as_ref() != b"\r\n" { + log::warn!("multipart field did not read all the data or it is malformed"); + } + Poll::Ready(None) + } + Err(err) => Poll::Ready(Some(Err(err))), + }; + + drop(payload); + + if let Poll::Ready(None) = result { + // drop payload buffer and make future un-poll-able + let _ = self.payload.take(); + } + + result + } +} + +#[cfg(test)] +mod tests { + use futures_util::{stream, StreamExt as _}; + + use super::*; + use crate::Multipart; + + // TODO: use test utility when multi-file support is introduced + fn create_double_request_with_header() -> (Bytes, HeaderMap) { + let bytes = Bytes::from( + "testasdadsad\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + \r\n\ + one+one+one\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + \r\n\ + two+two+two\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0--\r\n", + ); + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + ), + ); + (bytes, headers) + } + + #[actix_rt::test] + async fn bytes_unlimited() { + let (body, headers) = create_double_request_with_header(); + + let mut multipart = Multipart::new(&headers, stream::iter([Ok(body)])); + + let field = multipart + .next() + .await + .expect("multipart should have two fields") + .expect("multipart body should be well formatted") + .bytes(usize::MAX) + .await + .expect("field data should not be size limited") + .expect("reading field data should not error"); + assert_eq!(field, "one+one+one"); + + let field = multipart + .next() + .await + .expect("multipart should have two fields") + .expect("multipart body should be well formatted") + .bytes(usize::MAX) + .await + .expect("field data should not be size limited") + .expect("reading field data should not error"); + assert_eq!(field, "two+two+two"); + } + + #[actix_rt::test] + async fn bytes_limited() { + let (body, headers) = create_double_request_with_header(); + + let mut multipart = Multipart::new(&headers, stream::iter([Ok(body)])); + + multipart + .next() + .await + .expect("multipart should have two fields") + .expect("multipart body should be well formatted") + .bytes(8) // smaller than data size + .await + .expect_err("field data should be size limited"); + + // next field still readable + let field = multipart + .next() + .await + .expect("multipart should have two fields") + .expect("multipart body should be well formatted") + .bytes(usize::MAX) + .await + .expect("field data should not be size limited") + .expect("reading field data should not error"); + assert_eq!(field, "two+two+two"); + } +} diff --git a/actix-multipart/src/form/bytes.rs b/actix-multipart/src/form/bytes.rs index 3c5e2eb1..51b0cf7d 100644 --- a/actix-multipart/src/form/bytes.rs +++ b/actix-multipart/src/form/bytes.rs @@ -1,7 +1,6 @@ //! Reads a field into memory. -use actix_web::HttpRequest; -use bytes::BytesMut; +use actix_web::{web::BytesMut, HttpRequest}; use futures_core::future::LocalBoxFuture; use futures_util::TryStreamExt as _; use mime::Mime; @@ -15,7 +14,7 @@ use crate::{ #[derive(Debug)] pub struct Bytes { /// The data. - pub data: bytes::Bytes, + pub data: actix_web::web::Bytes, /// The value of the `Content-Type` header. pub content_type: Option, @@ -41,8 +40,9 @@ impl<'t> FieldReader<'t> for Bytes { content_type: field.content_type().map(ToOwned::to_owned), file_name: field .content_disposition() + .expect("multipart form fields should have a content-disposition header") .get_filename() - .map(str::to_owned), + .map(ToOwned::to_owned), }) }) } diff --git a/actix-multipart/src/form/json.rs b/actix-multipart/src/form/json.rs index bb4e03bf..8fb1f5ca 100644 --- a/actix-multipart/src/form/json.rs +++ b/actix-multipart/src/form/json.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use actix_web::{http::StatusCode, web, Error, HttpRequest, ResponseError}; -use derive_more::{Deref, DerefMut, Display, Error}; +use derive_more::derive::{Deref, DerefMut, Display, Error}; use futures_core::future::LocalBoxFuture; use serde::de::DeserializeOwned; @@ -32,7 +32,6 @@ where fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future { Box::pin(async move { let config = JsonConfig::from_req(req); - let field_name = field.name().to_owned(); if config.validate_content_type { let valid = if let Some(mime) = field.content_type() { @@ -43,17 +42,19 @@ where if !valid { return Err(MultipartError::Field { - field_name, + name: field.form_field_name, source: config.map_error(req, JsonFieldError::ContentType), }); } } + let form_field_name = field.form_field_name.clone(); + let bytes = Bytes::read_field(req, field, limits).await?; Ok(Json(serde_json::from_slice(bytes.data.as_ref()).map_err( |err| MultipartError::Field { - field_name, + name: form_field_name, source: config.map_error(req, JsonFieldError::Deserialize(err)), }, )?)) @@ -65,11 +66,11 @@ where #[non_exhaustive] pub enum JsonFieldError { /// Deserialize error. - #[display(fmt = "Json deserialize error: {}", _0)] + #[display("Json deserialize error: {}", _0)] Deserialize(serde_json::Error), /// Content type error. - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, } @@ -133,8 +134,7 @@ impl Default for JsonConfig { mod tests { use std::collections::HashMap; - use actix_web::{http::StatusCode, web, App, HttpResponse, Responder}; - use bytes::Bytes; + use actix_web::{http::StatusCode, web, web::Bytes, App, HttpResponse, Responder}; use crate::form::{ json::{Json, JsonConfig}, diff --git a/actix-multipart/src/form/mod.rs b/actix-multipart/src/form/mod.rs index 451b103f..a9a1ad56 100644 --- a/actix-multipart/src/form/mod.rs +++ b/actix-multipart/src/form/mod.rs @@ -1,4 +1,4 @@ -//! Process and extract typed data from a multipart stream. +//! Extract and process typed data from fields of a `multipart/form-data` request. use std::{ any::Any, @@ -8,7 +8,7 @@ use std::{ }; use actix_web::{dev, error::PayloadError, web, Error, FromRequest, HttpRequest}; -use derive_more::{Deref, DerefMut}; +use derive_more::derive::{Deref, DerefMut}; use futures_core::future::LocalBoxFuture; use futures_util::{TryFutureExt as _, TryStreamExt as _}; @@ -33,6 +33,14 @@ pub trait FieldReader<'t>: Sized + Any { type Future: Future>; /// 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; } @@ -72,13 +80,13 @@ where state: &'t mut State, duplicate_field: DuplicateField, ) -> Self::Future { - if state.contains_key(field.name()) { + if state.contains_key(&field.form_field_name) { match duplicate_field { DuplicateField::Ignore => return Box::pin(ready(Ok(()))), DuplicateField::Deny => { return Box::pin(ready(Err(MultipartError::DuplicateField( - field.name().to_owned(), + field.form_field_name, )))) } @@ -87,7 +95,7 @@ where } Box::pin(async move { - let field_name = field.name().to_owned(); + let field_name = field.form_field_name.clone(); let t = T::read_field(req, field, limits).await?; state.insert(field_name, Box::new(t)); Ok(()) @@ -115,10 +123,8 @@ where Box::pin(async move { // Note: Vec GroupReader always allows duplicates - let field_name = field.name().to_owned(); - let vec = state - .entry(field_name) + .entry(field.form_field_name.clone()) .or_insert_with(|| Box::>::default()) .downcast_mut::>() .unwrap(); @@ -151,13 +157,13 @@ where state: &'t mut State, duplicate_field: DuplicateField, ) -> Self::Future { - if state.contains_key(field.name()) { + if state.contains_key(&field.form_field_name) { match duplicate_field { DuplicateField::Ignore => return Box::pin(ready(Ok(()))), DuplicateField::Deny => { return Box::pin(ready(Err(MultipartError::DuplicateField( - field.name().to_owned(), + field.form_field_name, )))) } @@ -166,7 +172,7 @@ where } Box::pin(async move { - let field_name = field.name().to_owned(); + let field_name = field.form_field_name.clone(); let t = T::read_field(req, field, limits).await?; state.insert(field_name, Box::new(t)); Ok(()) @@ -273,6 +279,9 @@ impl Limits { /// [`MultipartCollect`] trait. You should use the [`macro@MultipartForm`] macro to derive this /// for your struct. /// +/// Note that this extractor rejects requests with any other Content-Type such as `multipart/mixed`, +/// `multipart/related`, or non-multipart media types. +/// /// Add a [`MultipartFormConfig`] to your app data to configure extraction. #[derive(Deref, DerefMut)] pub struct MultipartForm(pub T); @@ -286,14 +295,24 @@ impl MultipartForm { impl FromRequest for MultipartForm where - T: MultipartCollect, + T: MultipartCollect + 'static, { type Error = Error; type Future = LocalBoxFuture<'static, Result>; #[inline] fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { - let mut payload = Multipart::new(req.headers(), payload.take()); + let mut multipart = Multipart::from_req(req, payload); + + let content_type = match multipart.content_type_or_bail() { + Ok(content_type) => content_type, + Err(err) => return Box::pin(ready(Err(err.into()))), + }; + + if content_type.subtype() != mime::FORM_DATA { + // this extractor only supports multipart/form-data + return Box::pin(ready(Err(MultipartError::ContentTypeIncompatible.into()))); + }; let config = MultipartFormConfig::from_req(req); let mut limits = Limits::new(config.total_limit, config.memory_limit); @@ -305,14 +324,20 @@ where Box::pin( async move { let mut state = State::default(); - // We need to ensure field limits are shared for all instances of this field name + + // ensure limits are shared for all fields with this name let mut field_limits = HashMap::>::new(); - while let Some(field) = payload.try_next().await? { + while let Some(field) = multipart.try_next().await? { + debug_assert!( + !field.form_field_name.is_empty(), + "multipart form fields should have names", + ); + // Retrieve the limit for this field let entry = field_limits - .entry(field.name().to_owned()) - .or_insert_with(|| T::limit(field.name())); + .entry(field.form_field_name.clone()) + .or_insert_with(|| T::limit(&field.form_field_name)); limits.field_limit_remaining.clone_from(entry); @@ -321,6 +346,7 @@ where // Update the stored limit *entry = limits.field_limit_remaining; } + let inner = T::from_state(state)?; Ok(MultipartForm(inner)) } @@ -396,11 +422,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 +769,84 @@ mod tests { let response = send_form(&srv, form, "/").await; assert_eq!(response.status(), StatusCode::BAD_REQUEST); } + + #[actix_rt::test] + async fn non_multipart_form_data() { + #[derive(MultipartForm)] + struct TestNonMultipartFormData { + #[allow(unused)] + #[multipart(limit = "30B")] + foo: Text, + } + + async fn non_multipart_form_data_route( + _form: MultipartForm, + ) -> String { + unreachable!("request is sent with multipart/mixed"); + } + + let srv = actix_test::start(|| { + App::new().route("/", web::post().to(non_multipart_form_data_route)) + }); + + let mut form = multipart::Form::default(); + form.add_text("foo", "foo"); + + // mangle content-type, keeping the boundary + let ct = form.content_type().replacen("/form-data", "/mixed", 1); + + let res = Client::default() + .post(srv.url("/")) + .content_type(ct) + .send_body(multipart::Body::from(form)) + .await + .unwrap(); + + assert_eq!(res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); + } + + #[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>; + + 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) -> 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; + } } diff --git a/actix-multipart/src/form/tempfile.rs b/actix-multipart/src/form/tempfile.rs index 9371a026..df7a2368 100644 --- a/actix-multipart/src/form/tempfile.rs +++ b/actix-multipart/src/form/tempfile.rs @@ -7,7 +7,7 @@ use std::{ }; use actix_web::{http::StatusCode, web, Error, HttpRequest, ResponseError}; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_core::future::LocalBoxFuture; use futures_util::TryStreamExt as _; use mime::Mime; @@ -42,38 +42,36 @@ impl<'t> FieldReader<'t> for TempFile { fn read_field(req: &'t HttpRequest, mut field: Field, limits: &'t mut Limits) -> Self::Future { Box::pin(async move { let config = TempFileConfig::from_req(req); - let field_name = field.name().to_owned(); let mut size = 0; - let file = config - .create_tempfile() - .map_err(|err| config.map_error(req, &field_name, TempFileError::FileIo(err)))?; + let file = config.create_tempfile().map_err(|err| { + config.map_error(req, &field.form_field_name, TempFileError::FileIo(err)) + })?; - let mut file_async = - tokio::fs::File::from_std(file.reopen().map_err(|err| { - config.map_error(req, &field_name, TempFileError::FileIo(err)) - })?); + let mut file_async = tokio::fs::File::from_std(file.reopen().map_err(|err| { + config.map_error(req, &field.form_field_name, TempFileError::FileIo(err)) + })?); while let Some(chunk) = field.try_next().await? { limits.try_consume_limits(chunk.len(), false)?; size += chunk.len(); file_async.write_all(chunk.as_ref()).await.map_err(|err| { - config.map_error(req, &field_name, TempFileError::FileIo(err)) + config.map_error(req, &field.form_field_name, TempFileError::FileIo(err)) })?; } - file_async - .flush() - .await - .map_err(|err| config.map_error(req, &field_name, TempFileError::FileIo(err)))?; + file_async.flush().await.map_err(|err| { + config.map_error(req, &field.form_field_name, TempFileError::FileIo(err)) + })?; Ok(TempFile { file, content_type: field.content_type().map(ToOwned::to_owned), file_name: field .content_disposition() + .expect("multipart form fields should have a content-disposition header") .get_filename() - .map(str::to_owned), + .map(ToOwned::to_owned), size, }) }) @@ -84,7 +82,7 @@ impl<'t> FieldReader<'t> for TempFile { #[non_exhaustive] pub enum TempFileError { /// File I/O Error - #[display(fmt = "File I/O error: {}", _0)] + #[display("File I/O error: {}", _0)] FileIo(std::io::Error), } @@ -137,7 +135,7 @@ impl TempFileConfig { }; MultipartError::Field { - field_name: field_name.to_owned(), + name: field_name.to_owned(), source, } } diff --git a/actix-multipart/src/form/text.rs b/actix-multipart/src/form/text.rs index 83e21152..c4b307f8 100644 --- a/actix-multipart/src/form/text.rs +++ b/actix-multipart/src/form/text.rs @@ -3,7 +3,7 @@ use std::{str, sync::Arc}; use actix_web::{http::StatusCode, web, Error, HttpRequest, ResponseError}; -use derive_more::{Deref, DerefMut, Display, Error}; +use derive_more::derive::{Deref, DerefMut, Display, Error}; use futures_core::future::LocalBoxFuture; use serde::de::DeserializeOwned; @@ -36,7 +36,6 @@ where fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future { Box::pin(async move { let config = TextConfig::from_req(req); - let field_name = field.name().to_owned(); if config.validate_content_type { let valid = if let Some(mime) = field.content_type() { @@ -49,22 +48,24 @@ where if !valid { return Err(MultipartError::Field { - field_name, + name: field.form_field_name, source: config.map_error(req, TextError::ContentType), }); } } + let form_field_name = field.form_field_name.clone(); + let bytes = Bytes::read_field(req, field, limits).await?; let text = str::from_utf8(&bytes.data).map_err(|err| MultipartError::Field { - field_name: field_name.clone(), + name: form_field_name.clone(), source: config.map_error(req, TextError::Utf8Error(err)), })?; Ok(Text(serde_plain::from_str(text).map_err(|err| { MultipartError::Field { - field_name, + name: form_field_name, source: config.map_error(req, TextError::Deserialize(err)), } })?)) @@ -76,15 +77,15 @@ where #[non_exhaustive] pub enum TextError { /// UTF-8 decoding error. - #[display(fmt = "UTF-8 decoding error: {}", _0)] + #[display("UTF-8 decoding error: {}", _0)] Utf8Error(str::Utf8Error), /// Deserialize error. - #[display(fmt = "Plain text deserialize error: {}", _0)] + #[display("Plain text deserialize error: {}", _0)] Deserialize(serde_plain::Error), /// Content type error. - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, } diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index d19e951e..8eea35f2 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -1,9 +1,19 @@ -//! Multipart form support for Actix Web. +//! Multipart request & form support for Actix Web. +//! +//! The [`Multipart`] extractor aims to support all kinds of `multipart/*` requests, including +//! `multipart/form-data`, `multipart/related` and `multipart/mixed`. This is a lower-level +//! extractor which supports reading [multipart fields](Field), in the order they are sent by the +//! client. +//! +//! Due to additional requirements for `multipart/form-data` requests, the higher level +//! [`MultipartForm`] extractor and derive macro only supports this media type. +//! //! # Examples +//! //! ```no_run //! use actix_web::{post, App, HttpServer, Responder}; //! -//! use actix_multipart::form::{json::Json as MPJson, tempfile::TempFile, MultipartForm}; +//! use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm}; //! use serde::Deserialize; //! //! #[derive(Debug, Deserialize)] @@ -15,7 +25,7 @@ //! struct UploadForm { //! #[multipart(limit = "100MB")] //! file: TempFile, -//! json: MPJson, +//! json: MpJson, //! } //! //! #[post("/videos")] @@ -34,10 +44,18 @@ //! .await //! } //! ``` +//! +//! cURL request: +//! +//! ```sh +//! curl -v --request POST \ +//! --url http://localhost:8080/videos \ +//! -F 'json={"name": "Cargo.lock"};type=application/json' \ +//! -F file=@./Cargo.lock +//! ``` +//! +//! [`MultipartForm`]: struct@form::MultipartForm -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] -#![allow(clippy::borrow_interior_mutable_const)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] @@ -48,14 +66,15 @@ extern crate self as actix_multipart; mod error; mod extractor; +pub(crate) mod field; pub mod form; -mod server; +mod multipart; +pub(crate) mod payload; +pub(crate) mod safety; pub mod test; pub use self::{ - error::MultipartError, - server::{Field, Multipart}, - test::{ - create_form_data_payload_and_headers, create_form_data_payload_and_headers_with_boundary, - }, + error::Error as MultipartError, + field::{Field, LimitExceeded}, + multipart::Multipart, }; diff --git a/actix-multipart/src/multipart.rs b/actix-multipart/src/multipart.rs new file mode 100644 index 00000000..e38fbde9 --- /dev/null +++ b/actix-multipart/src/multipart.rs @@ -0,0 +1,883 @@ +//! Multipart response payload support. + +use std::{ + cell::RefCell, + pin::Pin, + rc::Rc, + task::{Context, Poll}, +}; + +use actix_web::{ + dev, + error::{ParseError, PayloadError}, + http::header::{self, ContentDisposition, HeaderMap, HeaderName, HeaderValue}, + web::Bytes, + HttpRequest, +}; +use futures_core::stream::Stream; +use mime::Mime; + +use crate::{ + error::Error, + field::InnerField, + payload::{PayloadBuffer, PayloadRef}, + safety::Safety, + Field, +}; + +const MAX_HEADERS: usize = 32; + +/// The server-side implementation of `multipart/form-data` requests. +/// +/// This will parse the incoming stream into `MultipartItem` instances via its `Stream` +/// implementation. `MultipartItem::Field` contains multipart field. `MultipartItem::Multipart` is +/// used for nested multipart streams. +pub struct Multipart { + flow: Flow, + safety: Safety, +} + +enum Flow { + InFlight(Inner), + + /// Error container is Some until an error is returned out of the flow. + Error(Option), +} + +impl Multipart { + /// Creates multipart instance from parts. + pub fn new(headers: &HeaderMap, stream: S) -> Self + where + S: Stream> + 'static, + { + match Self::find_ct_and_boundary(headers) { + Ok((ct, boundary)) => Self::from_ct_and_boundary(ct, boundary, stream), + Err(err) => Self::from_error(err), + } + } + + /// Creates multipart instance from parts. + pub(crate) fn from_req(req: &HttpRequest, payload: &mut dev::Payload) -> Self { + match Self::find_ct_and_boundary(req.headers()) { + Ok((ct, boundary)) => Self::from_ct_and_boundary(ct, boundary, payload.take()), + Err(err) => Self::from_error(err), + } + } + + /// Extract Content-Type and boundary info from headers. + pub(crate) fn find_ct_and_boundary(headers: &HeaderMap) -> Result<(Mime, String), Error> { + let content_type = headers + .get(&header::CONTENT_TYPE) + .ok_or(Error::ContentTypeMissing)? + .to_str() + .ok() + .and_then(|content_type| content_type.parse::().ok()) + .ok_or(Error::ContentTypeParse)?; + + if content_type.type_() != mime::MULTIPART { + return Err(Error::ContentTypeIncompatible); + } + + let boundary = content_type + .get_param(mime::BOUNDARY) + .ok_or(Error::BoundaryMissing)? + .as_str() + .to_owned(); + + Ok((content_type, boundary)) + } + + /// Constructs a new multipart reader from given Content-Type, boundary, and stream. + pub(crate) fn from_ct_and_boundary(ct: Mime, boundary: String, stream: S) -> Multipart + where + S: Stream> + 'static, + { + Multipart { + safety: Safety::new(), + flow: Flow::InFlight(Inner { + payload: PayloadRef::new(PayloadBuffer::new(stream)), + content_type: ct, + boundary, + state: State::FirstBoundary, + item: Item::None, + }), + } + } + + /// Constructs a new multipart reader from given `MultipartError`. + pub(crate) fn from_error(err: Error) -> Multipart { + Multipart { + flow: Flow::Error(Some(err)), + safety: Safety::new(), + } + } + + /// Return requests parsed Content-Type or raise the stored error. + pub(crate) fn content_type_or_bail(&mut self) -> Result { + match self.flow { + Flow::InFlight(ref inner) => Ok(inner.content_type.clone()), + Flow::Error(ref mut err) => Err(err + .take() + .expect("error should not be taken after it was returned")), + } + } +} + +impl Stream for Multipart { + type Item = Result; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let this = self.get_mut(); + + match this.flow { + Flow::InFlight(ref mut inner) => { + if let Some(mut buffer) = inner.payload.get_mut(&this.safety) { + // check safety and poll read payload to buffer. + buffer.poll_stream(cx)?; + } else if !this.safety.is_clean() { + // safety violation + return Poll::Ready(Some(Err(Error::NotConsumed))); + } else { + return Poll::Pending; + } + + inner.poll(&this.safety, cx) + } + + Flow::Error(ref mut err) => Poll::Ready(Some(Err(err + .take() + .expect("Multipart polled after finish")))), + } + } +} + +#[derive(PartialEq, Debug)] +enum State { + /// Skip data until first boundary. + FirstBoundary, + + /// Reading boundary. + Boundary, + + /// Reading Headers. + Headers, + + /// Stream EOF. + Eof, +} + +enum Item { + None, + Field(Rc>), +} + +struct Inner { + /// Request's payload stream & buffer. + payload: PayloadRef, + + /// Request's Content-Type. + /// + /// Guaranteed to have "multipart" top-level media type, i.e., `multipart/*`. + content_type: Mime, + + /// Field boundary. + boundary: String, + + state: State, + item: Item, +} + +impl Inner { + fn read_field_headers(payload: &mut PayloadBuffer) -> Result, Error> { + match payload.read_until(b"\r\n\r\n")? { + None => { + if payload.eof { + Err(Error::Incomplete) + } else { + Ok(None) + } + } + + Some(bytes) => { + let mut hdrs = [httparse::EMPTY_HEADER; MAX_HEADERS]; + + match httparse::parse_headers(&bytes, &mut hdrs).map_err(ParseError::from)? { + httparse::Status::Complete((_, hdrs)) => { + // convert headers + let mut headers = HeaderMap::with_capacity(hdrs.len()); + + for h in hdrs { + let name = + HeaderName::try_from(h.name).map_err(|_| ParseError::Header)?; + let value = + HeaderValue::try_from(h.value).map_err(|_| ParseError::Header)?; + headers.append(name, value); + } + + Ok(Some(headers)) + } + + httparse::Status::Partial => Err(ParseError::Header.into()), + } + } + } + } + + /// Reads a field boundary from the payload buffer (and discards it). + /// + /// Reads "in-between" and "final" boundaries. E.g. for boundary = "foo": + /// + /// ```plain + /// --foo <-- in-between fields + /// --foo-- <-- end of request body, should be followed by EOF + /// ``` + /// + /// Returns: + /// + /// - `Ok(Some(true))` - final field boundary read (EOF) + /// - `Ok(Some(false))` - field boundary read + /// - `Ok(None)` - boundary not found, more data needs reading + /// - `Err(BoundaryMissing)` - multipart boundary is missing + fn read_boundary(payload: &mut PayloadBuffer, boundary: &str) -> Result, Error> { + // TODO: need to read epilogue + let chunk = match payload.readline_or_eof()? { + // TODO: this might be okay as a let Some() else return Ok(None) + None => return Ok(payload.eof.then_some(true)), + Some(chunk) => chunk, + }; + + const BOUNDARY_MARKER: &[u8] = b"--"; + const LINE_BREAK: &[u8] = b"\r\n"; + + let boundary_len = boundary.len(); + + if chunk.len() < boundary_len + 2 + 2 + || !chunk.starts_with(BOUNDARY_MARKER) + || &chunk[2..boundary_len + 2] != boundary.as_bytes() + { + return Err(Error::BoundaryMissing); + } + + // chunk facts: + // - long enough to contain boundary + 2 markers or 1 marker and line-break + // - starts with boundary marker + // - chunk contains correct boundary + + if &chunk[boundary_len + 2..] == LINE_BREAK { + // boundary is followed by line-break, indicating more fields to come + return Ok(Some(false)); + } + + // boundary is followed by marker + if &chunk[boundary_len + 2..boundary_len + 4] == BOUNDARY_MARKER + && ( + // chunk is exactly boundary len + 2 markers + chunk.len() == boundary_len + 2 + 2 + // final boundary is allowed to end with a line-break + || &chunk[boundary_len + 4..] == LINE_BREAK + ) + { + return Ok(Some(true)); + } + + Err(Error::BoundaryMissing) + } + + fn skip_until_boundary( + payload: &mut PayloadBuffer, + boundary: &str, + ) -> Result, Error> { + let mut eof = false; + + loop { + match payload.readline()? { + Some(chunk) => { + if chunk.is_empty() { + return Err(Error::BoundaryMissing); + } + if chunk.len() < boundary.len() { + continue; + } + if &chunk[..2] == b"--" && &chunk[2..chunk.len() - 2] == boundary.as_bytes() { + break; + } else { + if chunk.len() < boundary.len() + 2 { + continue; + } + let b: &[u8] = boundary.as_ref(); + if &chunk[..boundary.len()] == b + && &chunk[boundary.len()..boundary.len() + 2] == b"--" + { + eof = true; + break; + } + } + } + None => { + return if payload.eof { + Err(Error::Incomplete) + } else { + Ok(None) + }; + } + } + } + Ok(Some(eof)) + } + + fn poll(&mut self, safety: &Safety, cx: &Context<'_>) -> Poll>> { + if self.state == State::Eof { + Poll::Ready(None) + } else { + // release field + loop { + // Nested multipart streams of fields has to be consumed + // before switching to next + if safety.current() { + let stop = match self.item { + Item::Field(ref mut field) => match field.borrow_mut().poll(safety) { + Poll::Pending => return Poll::Pending, + Poll::Ready(Some(Ok(_))) => continue, + Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))), + Poll::Ready(None) => true, + }, + Item::None => false, + }; + if stop { + self.item = Item::None; + } + if let Item::None = self.item { + break; + } + } + } + + let field_headers = if let Some(mut payload) = self.payload.get_mut(safety) { + match self.state { + // read until first boundary + State::FirstBoundary => { + match Inner::skip_until_boundary(&mut payload, &self.boundary)? { + None => return Poll::Pending, + Some(eof) => { + if eof { + self.state = State::Eof; + return Poll::Ready(None); + } else { + self.state = State::Headers; + } + } + } + } + + // read boundary + State::Boundary => match Inner::read_boundary(&mut payload, &self.boundary)? { + None => return Poll::Pending, + Some(eof) => { + if eof { + self.state = State::Eof; + return Poll::Ready(None); + } else { + self.state = State::Headers; + } + } + }, + + _ => {} + } + + // read field headers for next field + if self.state == State::Headers { + if let Some(headers) = Inner::read_field_headers(&mut payload)? { + self.state = State::Boundary; + headers + } else { + return Poll::Pending; + } + } else { + unreachable!() + } + } else { + log::debug!("NotReady: field is in flight"); + return Poll::Pending; + }; + + let field_content_disposition = field_headers + .get(&header::CONTENT_DISPOSITION) + .and_then(|cd| ContentDisposition::from_raw(cd).ok()) + .filter(|content_disposition| { + matches!( + content_disposition.disposition, + header::DispositionType::FormData, + ) + }); + + let form_field_name = if self.content_type.subtype() == mime::FORM_DATA { + // According to RFC 7578 §4.2, which relates to "multipart/form-data" requests + // specifically, fields must have a Content-Disposition header, its disposition + // type must be set as "form-data", and it must have a name parameter. + + let Some(cd) = &field_content_disposition else { + return Poll::Ready(Some(Err(Error::ContentDispositionMissing))); + }; + + let Some(field_name) = cd.get_name() else { + return Poll::Ready(Some(Err(Error::ContentDispositionNameMissing))); + }; + + Some(field_name.to_owned()) + } else { + None + }; + + // TODO: check out other multipart/* RFCs for specific requirements + + let field_content_type: Option = field_headers + .get(&header::CONTENT_TYPE) + .and_then(|ct| ct.to_str().ok()) + .and_then(|ct| ct.parse().ok()); + + self.state = State::Boundary; + + // nested multipart stream is not supported + if let Some(mime) = &field_content_type { + if mime.type_() == mime::MULTIPART { + return Poll::Ready(Some(Err(Error::Nested))); + } + } + + let field_inner = + InnerField::new_in_rc(self.payload.clone(), self.boundary.clone(), &field_headers)?; + + self.item = Item::Field(Rc::clone(&field_inner)); + + Poll::Ready(Some(Ok(Field::new( + field_content_type, + field_content_disposition, + form_field_name, + field_headers, + safety.clone(cx), + field_inner, + )))) + } + } +} + +impl Drop for Inner { + fn drop(&mut self) { + // InnerMultipartItem::Field has to be dropped first because of Safety. + self.item = Item::None; + } +} + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use actix_http::h1; + use actix_web::{ + http::header::{DispositionParam, DispositionType}, + rt, + test::TestRequest, + web::{BufMut as _, BytesMut}, + FromRequest, + }; + use assert_matches::assert_matches; + use futures_test::stream::StreamTestExt as _; + use futures_util::{stream, StreamExt as _}; + use tokio::sync::mpsc; + use tokio_stream::wrappers::UnboundedReceiverStream; + + use super::*; + + const BOUNDARY: &str = "abbc761f78ff4d7cb7573b5a23f96ef0"; + + #[actix_rt::test] + async fn test_boundary() { + let headers = HeaderMap::new(); + match Multipart::find_ct_and_boundary(&headers) { + Err(Error::ContentTypeMissing) => {} + _ => unreachable!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static("test"), + ); + + match Multipart::find_ct_and_boundary(&headers) { + Err(Error::ContentTypeParse) => {} + _ => unreachable!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static("multipart/mixed"), + ); + match Multipart::find_ct_and_boundary(&headers) { + Err(Error::BoundaryMissing) => {} + _ => unreachable!("should not happen"), + } + + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/mixed; boundary=\"5c02368e880e436dab70ed54e1c58209\"", + ), + ); + + assert_eq!( + Multipart::find_ct_and_boundary(&headers).unwrap().1, + "5c02368e880e436dab70ed54e1c58209", + ); + } + + fn create_stream() -> ( + mpsc::UnboundedSender>, + impl Stream>, + ) { + let (tx, rx) = mpsc::unbounded_channel(); + + ( + tx, + UnboundedReceiverStream::new(rx).map(|res| res.map_err(|_| panic!())), + ) + } + + fn create_simple_request_with_header() -> (Bytes, HeaderMap) { + let (body, headers) = crate::test::create_form_data_payload_and_headers_with_boundary( + BOUNDARY, + "file", + Some("fn.txt".to_owned()), + Some(mime::TEXT_PLAIN_UTF_8), + Bytes::from_static(b"data"), + ); + + let mut buf = BytesMut::with_capacity(body.len() + 14); + + // add junk before form to test pre-boundary data rejection + buf.put("testasdadsad\r\n".as_bytes()); + + buf.put(body); + + (buf.freeze(), headers) + } + + // TODO: use test utility when multi-file support is introduced + fn create_double_request_with_header() -> (Bytes, HeaderMap) { + let bytes = Bytes::from( + "testasdadsad\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\ + Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ + test\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\ + Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ + data\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0--\r\n", + ); + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + ), + ); + (bytes, headers) + } + + #[actix_rt::test] + async fn test_multipart_no_end_crlf() { + let (sender, payload) = create_stream(); + let (mut bytes, headers) = create_double_request_with_header(); + let bytes_stripped = bytes.split_to(bytes.len()); // strip crlf + + sender.send(Ok(bytes_stripped)).unwrap(); + drop(sender); // eof + + let mut multipart = Multipart::new(&headers, payload); + + match multipart.next().await.unwrap() { + Ok(_) => {} + _ => unreachable!(), + } + + match multipart.next().await.unwrap() { + Ok(_) => {} + _ => unreachable!(), + } + + match multipart.next().await { + None => {} + _ => unreachable!(), + } + } + + #[actix_rt::test] + async fn test_multipart() { + let (sender, payload) = create_stream(); + let (bytes, headers) = create_double_request_with_header(); + + sender.send(Ok(bytes)).unwrap(); + + let mut multipart = Multipart::new(&headers, payload); + match multipart.next().await { + Some(Ok(mut field)) => { + let cd = field.content_disposition().unwrap(); + assert_eq!(cd.disposition, DispositionType::FormData); + assert_eq!(cd.parameters[0], DispositionParam::Name("file".into())); + + assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); + assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); + + match field.next().await.unwrap() { + Ok(chunk) => assert_eq!(chunk, "test"), + _ => unreachable!(), + } + match field.next().await { + None => {} + _ => unreachable!(), + } + } + _ => unreachable!(), + } + + match multipart.next().await.unwrap() { + Ok(mut field) => { + assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); + assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); + + match field.next().await { + Some(Ok(chunk)) => assert_eq!(chunk, "data"), + _ => unreachable!(), + } + match field.next().await { + None => {} + _ => unreachable!(), + } + } + _ => unreachable!(), + } + + match multipart.next().await { + None => {} + _ => unreachable!(), + } + } + + // Loops, collecting all bytes until end-of-field + async fn get_whole_field(field: &mut Field) -> BytesMut { + let mut b = BytesMut::new(); + loop { + match field.next().await { + Some(Ok(chunk)) => b.extend_from_slice(&chunk), + None => return b, + _ => unreachable!(), + } + } + } + + #[actix_rt::test] + async fn test_stream() { + let (bytes, headers) = create_double_request_with_header(); + let payload = stream::iter(bytes) + .map(|byte| Ok(Bytes::copy_from_slice(&[byte]))) + .interleave_pending(); + + let mut multipart = Multipart::new(&headers, payload); + match multipart.next().await.unwrap() { + Ok(mut field) => { + let cd = field.content_disposition().unwrap(); + assert_eq!(cd.disposition, DispositionType::FormData); + assert_eq!(cd.parameters[0], DispositionParam::Name("file".into())); + + assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); + assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); + + assert_eq!(get_whole_field(&mut field).await, "test"); + } + _ => unreachable!(), + } + + match multipart.next().await { + Some(Ok(mut field)) => { + assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); + assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); + + assert_eq!(get_whole_field(&mut field).await, "data"); + } + _ => unreachable!(), + } + + match multipart.next().await { + None => {} + _ => unreachable!(), + } + } + + #[actix_rt::test] + async fn test_multipart_from_error() { + let err = Error::ContentTypeMissing; + let mut multipart = Multipart::from_error(err); + assert!(multipart.next().await.unwrap().is_err()) + } + + #[actix_rt::test] + async fn test_multipart_from_boundary() { + let (_, payload) = create_stream(); + let (_, headers) = create_simple_request_with_header(); + let (ct, boundary) = Multipart::find_ct_and_boundary(&headers).unwrap(); + let _ = Multipart::from_ct_and_boundary(ct, boundary, payload); + } + + #[actix_rt::test] + async fn test_multipart_payload_consumption() { + // with sample payload and HttpRequest with no headers + let (_, inner_payload) = h1::Payload::create(false); + let mut payload = actix_web::dev::Payload::from(inner_payload); + let req = TestRequest::default().to_http_request(); + + // multipart should generate an error + let mut mp = Multipart::from_request(&req, &mut payload).await.unwrap(); + assert!(mp.next().await.unwrap().is_err()); + + // and should not consume the payload + match payload { + actix_web::dev::Payload::H1 { .. } => {} //expected + _ => unreachable!(), + } + } + + #[actix_rt::test] + async fn no_content_disposition_form_data() { + let bytes = Bytes::from( + "testasdadsad\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + Content-Length: 4\r\n\ + \r\n\ + test\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n", + ); + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + ), + ); + let payload = stream::iter(bytes) + .map(|byte| Ok(Bytes::copy_from_slice(&[byte]))) + .interleave_pending(); + + let mut multipart = Multipart::new(&headers, payload); + let res = multipart.next().await.unwrap(); + assert_matches!( + res.expect_err( + "according to RFC 7578, form-data fields require a content-disposition header" + ), + Error::ContentDispositionMissing + ); + } + + #[actix_rt::test] + async fn no_content_disposition_non_form_data() { + let bytes = Bytes::from( + "testasdadsad\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + Content-Length: 4\r\n\ + \r\n\ + test\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n", + ); + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + ), + ); + let payload = stream::iter(bytes) + .map(|byte| Ok(Bytes::copy_from_slice(&[byte]))) + .interleave_pending(); + + let mut multipart = Multipart::new(&headers, payload); + let res = multipart.next().await.unwrap(); + res.unwrap(); + } + + #[actix_rt::test] + async fn no_name_in_form_data_content_disposition() { + let bytes = Bytes::from( + "testasdadsad\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ + Content-Disposition: form-data; filename=\"fn.txt\"\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + Content-Length: 4\r\n\ + \r\n\ + test\r\n\ + --abbc761f78ff4d7cb7573b5a23f96ef0\r\n", + ); + let mut headers = HeaderMap::new(); + headers.insert( + header::CONTENT_TYPE, + header::HeaderValue::from_static( + "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + ), + ); + let payload = stream::iter(bytes) + .map(|byte| Ok(Bytes::copy_from_slice(&[byte]))) + .interleave_pending(); + + let mut multipart = Multipart::new(&headers, payload); + let res = multipart.next().await.unwrap(); + assert_matches!( + res.expect_err("according to RFC 7578, form-data fields require a name attribute"), + Error::ContentDispositionNameMissing + ); + } + + #[actix_rt::test] + async fn test_drop_multipart_dont_hang() { + let (sender, payload) = create_stream(); + let (bytes, headers) = create_simple_request_with_header(); + sender.send(Ok(bytes)).unwrap(); + drop(sender); // eof + + let mut multipart = Multipart::new(&headers, payload); + let mut field = multipart.next().await.unwrap().unwrap(); + + drop(multipart); + + // should fail immediately + match field.next().await { + Some(Err(Error::NotConsumed)) => {} + _ => panic!(), + }; + } + + #[actix_rt::test] + async fn test_drop_field_awaken_multipart() { + let (sender, payload) = create_stream(); + let (bytes, headers) = create_double_request_with_header(); + sender.send(Ok(bytes)).unwrap(); + drop(sender); // eof + + let mut multipart = Multipart::new(&headers, payload); + let mut field = multipart.next().await.unwrap().unwrap(); + + let task = rt::spawn(async move { + rt::time::sleep(Duration::from_millis(500)).await; + assert_eq!(field.next().await.unwrap().unwrap(), "test"); + drop(field); + }); + + // dropping field should awaken current task + let _ = multipart.next().await.unwrap().unwrap(); + task.await.unwrap(); + } +} diff --git a/actix-multipart/src/payload.rs b/actix-multipart/src/payload.rs new file mode 100644 index 00000000..858634bc --- /dev/null +++ b/actix-multipart/src/payload.rs @@ -0,0 +1,255 @@ +use std::{ + cell::{RefCell, RefMut}, + cmp, mem, + pin::Pin, + rc::Rc, + task::{Context, Poll}, +}; + +use actix_web::{ + error::PayloadError, + web::{Bytes, BytesMut}, +}; +use futures_core::stream::{LocalBoxStream, Stream}; + +use crate::{error::Error, safety::Safety}; + +pub(crate) struct PayloadRef { + payload: Rc>, +} + +impl PayloadRef { + pub(crate) fn new(payload: PayloadBuffer) -> PayloadRef { + PayloadRef { + payload: Rc::new(RefCell::new(payload)), + } + } + + pub(crate) fn get_mut(&self, safety: &Safety) -> Option> { + if safety.current() { + Some(self.payload.borrow_mut()) + } else { + None + } + } +} + +impl Clone for PayloadRef { + fn clone(&self) -> PayloadRef { + PayloadRef { + payload: Rc::clone(&self.payload), + } + } +} + +/// Payload buffer. +pub(crate) struct PayloadBuffer { + pub(crate) stream: LocalBoxStream<'static, Result>, + pub(crate) buf: BytesMut, + /// EOF flag. If true, no more payload reads will be attempted. + pub(crate) eof: bool, +} + +impl PayloadBuffer { + /// Constructs new payload buffer. + pub(crate) fn new(stream: S) -> Self + where + S: Stream> + 'static, + { + PayloadBuffer { + stream: Box::pin(stream), + buf: BytesMut::with_capacity(1_024), // pre-allocate 1KiB + eof: false, + } + } + + pub(crate) fn poll_stream(&mut self, cx: &mut Context<'_>) -> Result<(), PayloadError> { + loop { + match Pin::new(&mut self.stream).poll_next(cx) { + Poll::Ready(Some(Ok(data))) => { + self.buf.extend_from_slice(&data); + // try to read more data + continue; + } + Poll::Ready(Some(Err(err))) => return Err(err), + Poll::Ready(None) => { + self.eof = true; + return Ok(()); + } + Poll::Pending => return Ok(()), + } + } + } + + /// Reads exact number of bytes. + #[cfg(test)] + pub(crate) fn read_exact(&mut self, size: usize) -> Option { + if size <= self.buf.len() { + Some(self.buf.split_to(size).freeze()) + } else { + None + } + } + + pub(crate) fn read_max(&mut self, size: u64) -> Result, Error> { + if !self.buf.is_empty() { + let size = cmp::min(self.buf.len() as u64, size) as usize; + Ok(Some(self.buf.split_to(size).freeze())) + } else if self.eof { + Err(Error::Incomplete) + } else { + Ok(None) + } + } + + /// Reads until specified ending. + /// + /// Returns: + /// + /// - `Ok(Some(chunk))` - `needle` is found, with chunk ending after needle + /// - `Err(Incomplete)` - `needle` is not found and we're at EOF + /// - `Ok(None)` - `needle` is not found otherwise + pub(crate) fn read_until(&mut self, needle: &[u8]) -> Result, Error> { + match memchr::memmem::find(&self.buf, needle) { + // buffer exhausted and EOF without finding needle + None if self.eof => Err(Error::Incomplete), + + // needle not yet found + None => Ok(None), + + // needle found, split chunk out of buf + Some(idx) => Ok(Some(self.buf.split_to(idx + needle.len()).freeze())), + } + } + + /// Reads bytes until new line delimiter (`\n`, `0x0A`). + /// + /// Returns: + /// + /// - `Ok(Some(chunk))` - `needle` is found, with chunk ending after needle + /// - `Err(Incomplete)` - `needle` is not found and we're at EOF + /// - `Ok(None)` - `needle` is not found otherwise + #[inline] + pub(crate) fn readline(&mut self) -> Result, Error> { + self.read_until(b"\n") + } + + /// Reads bytes until new line delimiter or until EOF. + #[inline] + pub(crate) fn readline_or_eof(&mut self) -> Result, Error> { + match self.readline() { + Err(Error::Incomplete) if self.eof => Ok(Some(self.buf.split().freeze())), + line => line, + } + } + + /// Puts unprocessed data back to the buffer. + pub(crate) fn unprocessed(&mut self, data: Bytes) { + // TODO: use BytesMut::from when it's released, see https://github.com/tokio-rs/bytes/pull/710 + let buf = BytesMut::from(&data[..]); + let buf = mem::replace(&mut self.buf, buf); + self.buf.extend_from_slice(&buf); + } +} + +#[cfg(test)] +mod tests { + use actix_http::h1; + use futures_util::future::lazy; + + use super::*; + + #[actix_rt::test] + async fn basic() { + let (_, payload) = h1::Payload::create(false); + let mut payload = PayloadBuffer::new(payload); + + assert_eq!(payload.buf.len(), 0); + lazy(|cx| payload.poll_stream(cx)).await.unwrap(); + assert_eq!(None, payload.read_max(1).unwrap()); + } + + #[actix_rt::test] + async fn eof() { + let (mut sender, payload) = h1::Payload::create(false); + let mut payload = PayloadBuffer::new(payload); + + assert_eq!(None, payload.read_max(4).unwrap()); + sender.feed_data(Bytes::from("data")); + sender.feed_eof(); + lazy(|cx| payload.poll_stream(cx)).await.unwrap(); + + assert_eq!(Some(Bytes::from("data")), payload.read_max(4).unwrap()); + assert_eq!(payload.buf.len(), 0); + assert!(payload.read_max(1).is_err()); + assert!(payload.eof); + } + + #[actix_rt::test] + async fn err() { + let (mut sender, payload) = h1::Payload::create(false); + let mut payload = PayloadBuffer::new(payload); + assert_eq!(None, payload.read_max(1).unwrap()); + sender.set_error(PayloadError::Incomplete(None)); + lazy(|cx| payload.poll_stream(cx)).await.err().unwrap(); + } + + #[actix_rt::test] + async fn read_max() { + let (mut sender, payload) = h1::Payload::create(false); + let mut payload = PayloadBuffer::new(payload); + + sender.feed_data(Bytes::from("line1")); + sender.feed_data(Bytes::from("line2")); + lazy(|cx| payload.poll_stream(cx)).await.unwrap(); + assert_eq!(payload.buf.len(), 10); + + assert_eq!(Some(Bytes::from("line1")), payload.read_max(5).unwrap()); + assert_eq!(payload.buf.len(), 5); + + assert_eq!(Some(Bytes::from("line2")), payload.read_max(5).unwrap()); + assert_eq!(payload.buf.len(), 0); + } + + #[actix_rt::test] + async fn read_exactly() { + let (mut sender, payload) = h1::Payload::create(false); + let mut payload = PayloadBuffer::new(payload); + + assert_eq!(None, payload.read_exact(2)); + + sender.feed_data(Bytes::from("line1")); + sender.feed_data(Bytes::from("line2")); + lazy(|cx| payload.poll_stream(cx)).await.unwrap(); + + assert_eq!(Some(Bytes::from_static(b"li")), payload.read_exact(2)); + assert_eq!(payload.buf.len(), 8); + + assert_eq!(Some(Bytes::from_static(b"ne1l")), payload.read_exact(4)); + assert_eq!(payload.buf.len(), 4); + } + + #[actix_rt::test] + async fn read_until() { + let (mut sender, payload) = h1::Payload::create(false); + let mut payload = PayloadBuffer::new(payload); + + assert_eq!(None, payload.read_until(b"ne").unwrap()); + + sender.feed_data(Bytes::from("line1")); + sender.feed_data(Bytes::from("line2")); + lazy(|cx| payload.poll_stream(cx)).await.unwrap(); + + assert_eq!( + Some(Bytes::from("line")), + payload.read_until(b"ne").unwrap() + ); + assert_eq!(payload.buf.len(), 6); + + assert_eq!( + Some(Bytes::from("1line2")), + payload.read_until(b"2").unwrap() + ); + assert_eq!(payload.buf.len(), 0); + } +} diff --git a/actix-multipart/src/safety.rs b/actix-multipart/src/safety.rs new file mode 100644 index 00000000..db6b3b18 --- /dev/null +++ b/actix-multipart/src/safety.rs @@ -0,0 +1,60 @@ +use std::{cell::Cell, marker::PhantomData, rc::Rc, task}; + +use local_waker::LocalWaker; + +/// Counter. It tracks of number of clones of payloads and give access to payload only to top most. +/// +/// - When dropped, parent task is awakened. This is to support the case where `Field` is dropped in +/// a separate task than `Multipart`. +/// - Assumes that parent owners don't move to different tasks; only the top-most is allowed to. +/// - If dropped and is not top most owner, is_clean flag is set to false. +#[derive(Debug)] +pub(crate) struct Safety { + task: LocalWaker, + level: usize, + payload: Rc>, + clean: Rc>, +} + +impl Safety { + pub(crate) fn new() -> Safety { + let payload = Rc::new(PhantomData); + Safety { + task: LocalWaker::new(), + level: Rc::strong_count(&payload), + clean: Rc::new(Cell::new(true)), + payload, + } + } + + pub(crate) fn current(&self) -> bool { + Rc::strong_count(&self.payload) == self.level && self.clean.get() + } + + pub(crate) fn is_clean(&self) -> bool { + self.clean.get() + } + + pub(crate) fn clone(&self, cx: &task::Context<'_>) -> Safety { + let payload = Rc::clone(&self.payload); + let s = Safety { + task: LocalWaker::new(), + level: Rc::strong_count(&payload), + clean: self.clean.clone(), + payload, + }; + s.task.register(cx.waker()); + s + } +} + +impl Drop for Safety { + fn drop(&mut self) { + if Rc::strong_count(&self.payload) != self.level { + // Multipart dropped leaving a Field + self.clean.set(false); + } + + self.task.wake(); + } +} diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs deleted file mode 100644 index d0f83331..00000000 --- a/actix-multipart/src/server.rs +++ /dev/null @@ -1,1361 +0,0 @@ -//! Multipart response payload support. - -use std::{ - cell::{Cell, RefCell, RefMut}, - cmp, fmt, - marker::PhantomData, - pin::Pin, - rc::Rc, - task::{Context, Poll}, -}; - -use actix_web::{ - error::{ParseError, PayloadError}, - http::header::{self, ContentDisposition, HeaderMap, HeaderName, HeaderValue}, -}; -use bytes::{Bytes, BytesMut}; -use futures_core::stream::{LocalBoxStream, Stream}; -use local_waker::LocalWaker; - -use crate::error::MultipartError; - -const MAX_HEADERS: usize = 32; - -/// The server-side implementation of `multipart/form-data` requests. -/// -/// This will parse the incoming stream into `MultipartItem` instances via its -/// Stream implementation. -/// `MultipartItem::Field` contains multipart field. `MultipartItem::Multipart` -/// is used for nested multipart streams. -pub struct Multipart { - safety: Safety, - error: Option, - inner: Option, -} - -enum InnerMultipartItem { - None, - Field(Rc>), -} - -#[derive(PartialEq, Debug)] -enum InnerState { - /// Stream eof - Eof, - - /// Skip data until first boundary - FirstBoundary, - - /// Reading boundary - Boundary, - - /// Reading Headers, - Headers, -} - -struct InnerMultipart { - payload: PayloadRef, - boundary: String, - state: InnerState, - item: InnerMultipartItem, -} - -impl Multipart { - /// Create multipart instance for boundary. - pub fn new(headers: &HeaderMap, stream: S) -> Multipart - where - S: Stream> + 'static, - { - match Self::boundary(headers) { - Ok(boundary) => Multipart::from_boundary(boundary, stream), - Err(err) => Multipart::from_error(err), - } - } - - /// Extract boundary info from headers. - pub(crate) fn boundary(headers: &HeaderMap) -> Result { - headers - .get(&header::CONTENT_TYPE) - .ok_or(MultipartError::NoContentType)? - .to_str() - .ok() - .and_then(|content_type| content_type.parse::().ok()) - .ok_or(MultipartError::ParseContentType)? - .get_param(mime::BOUNDARY) - .map(|boundary| boundary.as_str().to_owned()) - .ok_or(MultipartError::Boundary) - } - - /// Create multipart instance for given boundary and stream - pub(crate) fn from_boundary(boundary: String, stream: S) -> Multipart - where - S: Stream> + 'static, - { - Multipart { - error: None, - safety: Safety::new(), - inner: Some(InnerMultipart { - boundary, - payload: PayloadRef::new(PayloadBuffer::new(stream)), - state: InnerState::FirstBoundary, - item: InnerMultipartItem::None, - }), - } - } - - /// Create Multipart instance from MultipartError - pub(crate) fn from_error(err: MultipartError) -> Multipart { - Multipart { - error: Some(err), - safety: Safety::new(), - inner: None, - } - } -} - -impl Stream for Multipart { - type Item = Result; - - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.get_mut(); - - match this.inner.as_mut() { - Some(inner) => { - if let Some(mut buffer) = inner.payload.get_mut(&this.safety) { - // check safety and poll read payload to buffer. - buffer.poll_stream(cx)?; - } else if !this.safety.is_clean() { - // safety violation - return Poll::Ready(Some(Err(MultipartError::NotConsumed))); - } else { - return Poll::Pending; - } - - inner.poll(&this.safety, cx) - } - None => Poll::Ready(Some(Err(this - .error - .take() - .expect("Multipart polled after finish")))), - } - } -} - -impl InnerMultipart { - fn read_headers(payload: &mut PayloadBuffer) -> Result, MultipartError> { - match payload.read_until(b"\r\n\r\n")? { - None => { - if payload.eof { - Err(MultipartError::Incomplete) - } else { - Ok(None) - } - } - Some(bytes) => { - let mut hdrs = [httparse::EMPTY_HEADER; MAX_HEADERS]; - match httparse::parse_headers(&bytes, &mut hdrs) { - Ok(httparse::Status::Complete((_, hdrs))) => { - // convert headers - let mut headers = HeaderMap::with_capacity(hdrs.len()); - - for h in hdrs { - let name = - HeaderName::try_from(h.name).map_err(|_| ParseError::Header)?; - let value = - HeaderValue::try_from(h.value).map_err(|_| ParseError::Header)?; - headers.append(name, value); - } - - Ok(Some(headers)) - } - Ok(httparse::Status::Partial) => Err(ParseError::Header.into()), - Err(err) => Err(ParseError::from(err).into()), - } - } - } - } - - fn read_boundary( - payload: &mut PayloadBuffer, - boundary: &str, - ) -> Result, MultipartError> { - // TODO: need to read epilogue - match payload.readline_or_eof()? { - None => { - if payload.eof { - Ok(Some(true)) - } else { - Ok(None) - } - } - Some(chunk) => { - if chunk.len() < boundary.len() + 4 - || &chunk[..2] != b"--" - || &chunk[2..boundary.len() + 2] != boundary.as_bytes() - { - Err(MultipartError::Boundary) - } else if &chunk[boundary.len() + 2..] == b"\r\n" { - Ok(Some(false)) - } else if &chunk[boundary.len() + 2..boundary.len() + 4] == b"--" - && (chunk.len() == boundary.len() + 4 - || &chunk[boundary.len() + 4..] == b"\r\n") - { - Ok(Some(true)) - } else { - Err(MultipartError::Boundary) - } - } - } - } - - fn skip_until_boundary( - payload: &mut PayloadBuffer, - boundary: &str, - ) -> Result, MultipartError> { - let mut eof = false; - loop { - match payload.readline()? { - Some(chunk) => { - if chunk.is_empty() { - return Err(MultipartError::Boundary); - } - if chunk.len() < boundary.len() { - continue; - } - if &chunk[..2] == b"--" && &chunk[2..chunk.len() - 2] == boundary.as_bytes() { - break; - } else { - if chunk.len() < boundary.len() + 2 { - continue; - } - let b: &[u8] = boundary.as_ref(); - if &chunk[..boundary.len()] == b - && &chunk[boundary.len()..boundary.len() + 2] == b"--" - { - eof = true; - break; - } - } - } - None => { - return if payload.eof { - Err(MultipartError::Incomplete) - } else { - Ok(None) - }; - } - } - } - Ok(Some(eof)) - } - - fn poll( - &mut self, - safety: &Safety, - cx: &Context<'_>, - ) -> Poll>> { - if self.state == InnerState::Eof { - Poll::Ready(None) - } else { - // release field - loop { - // Nested multipart streams of fields has to be consumed - // before switching to next - if safety.current() { - let stop = match self.item { - InnerMultipartItem::Field(ref mut field) => { - match field.borrow_mut().poll(safety) { - Poll::Pending => return Poll::Pending, - Poll::Ready(Some(Ok(_))) => continue, - Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))), - Poll::Ready(None) => true, - } - } - InnerMultipartItem::None => false, - }; - if stop { - self.item = InnerMultipartItem::None; - } - if let InnerMultipartItem::None = self.item { - break; - } - } - } - - let headers = if let Some(mut payload) = self.payload.get_mut(safety) { - match self.state { - // read until first boundary - InnerState::FirstBoundary => { - match InnerMultipart::skip_until_boundary(&mut payload, &self.boundary)? { - Some(eof) => { - if eof { - self.state = InnerState::Eof; - return Poll::Ready(None); - } else { - self.state = InnerState::Headers; - } - } - None => return Poll::Pending, - } - } - // read boundary - InnerState::Boundary => { - match InnerMultipart::read_boundary(&mut payload, &self.boundary)? { - None => return Poll::Pending, - Some(eof) => { - if eof { - self.state = InnerState::Eof; - return Poll::Ready(None); - } else { - self.state = InnerState::Headers; - } - } - } - } - _ => {} - } - - // read field headers for next field - if self.state == InnerState::Headers { - if let Some(headers) = InnerMultipart::read_headers(&mut payload)? { - self.state = InnerState::Boundary; - headers - } else { - return Poll::Pending; - } - } else { - unreachable!() - } - } else { - log::debug!("NotReady: field is in flight"); - return Poll::Pending; - }; - - // According to RFC 7578 §4.2, a Content-Disposition header must always be present and - // set to "form-data". - - let content_disposition = headers - .get(&header::CONTENT_DISPOSITION) - .and_then(|cd| ContentDisposition::from_raw(cd).ok()) - .filter(|content_disposition| { - let is_form_data = - content_disposition.disposition == header::DispositionType::FormData; - - let has_field_name = content_disposition - .parameters - .iter() - .any(|param| matches!(param, header::DispositionParam::Name(_))); - - is_form_data && has_field_name - }); - - let cd = if let Some(content_disposition) = content_disposition { - content_disposition - } else { - return Poll::Ready(Some(Err(MultipartError::NoContentDisposition))); - }; - - let ct: Option = headers - .get(&header::CONTENT_TYPE) - .and_then(|ct| ct.to_str().ok()) - .and_then(|ct| ct.parse().ok()); - - self.state = InnerState::Boundary; - - // nested multipart stream is not supported - if let Some(mime) = &ct { - if mime.type_() == mime::MULTIPART { - return Poll::Ready(Some(Err(MultipartError::Nested))); - } - } - - let field = - InnerField::new_in_rc(self.payload.clone(), self.boundary.clone(), &headers)?; - - self.item = InnerMultipartItem::Field(Rc::clone(&field)); - - Poll::Ready(Some(Ok(Field::new( - safety.clone(cx), - headers, - ct, - cd, - field, - )))) - } - } -} - -impl Drop for InnerMultipart { - fn drop(&mut self) { - // InnerMultipartItem::Field has to be dropped first because of Safety. - self.item = InnerMultipartItem::None; - } -} - -/// A single field in a multipart stream -pub struct Field { - ct: Option, - cd: ContentDisposition, - headers: HeaderMap, - inner: Rc>, - safety: Safety, -} - -impl Field { - fn new( - safety: Safety, - headers: HeaderMap, - ct: Option, - cd: ContentDisposition, - inner: Rc>, - ) -> Self { - Field { - ct, - cd, - headers, - inner, - safety, - } - } - - /// Returns a reference to the field's header map. - pub fn headers(&self) -> &HeaderMap { - &self.headers - } - - /// Returns a reference to the field's content (mime) type, if it is supplied by the client. - /// - /// According to [RFC 7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.4), if it is not - /// present, it should default to "text/plain". Note it is the responsibility of the client to - /// provide the appropriate content type, there is no attempt to validate this by the server. - pub fn content_type(&self) -> Option<&mime::Mime> { - self.ct.as_ref() - } - - /// Returns the field's Content-Disposition. - /// - /// Per [RFC 7578 §4.2]: "Each part MUST contain a Content-Disposition header field where the - /// disposition type is `form-data`. The Content-Disposition header field MUST also contain an - /// additional parameter of `name`; the value of the `name` parameter is the original field name - /// from the form." - /// - /// This crate validates that it exists before returning a `Field`. As such, it is safe to - /// unwrap `.content_disposition().get_name()`. The [name](Self::name) method is provided as - /// a convenience. - /// - /// [RFC 7578 §4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 - pub fn content_disposition(&self) -> &ContentDisposition { - &self.cd - } - - /// Returns the field's name. - /// - /// See [content_disposition](Self::content_disposition) regarding guarantees about existence of - /// the name field. - pub fn name(&self) -> &str { - self.content_disposition() - .get_name() - .expect("field name should be guaranteed to exist in multipart form-data") - } -} - -impl Stream for Field { - type Item = Result; - - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - 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) { - // check safety and poll read payload to buffer. - buffer.poll_stream(cx)?; - } else if !this.safety.is_clean() { - // safety violation - return Poll::Ready(Some(Err(MultipartError::NotConsumed))); - } else { - return Poll::Pending; - } - - inner.poll(&this.safety) - } -} - -impl fmt::Debug for Field { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let Some(ct) = &self.ct { - writeln!(f, "\nField: {}", ct)?; - } else { - writeln!(f, "\nField:")?; - } - writeln!(f, " boundary: {}", self.inner.borrow().boundary)?; - writeln!(f, " headers:")?; - for (key, val) in self.headers.iter() { - writeln!(f, " {:?}: {:?}", key, val)?; - } - Ok(()) - } -} - -struct InnerField { - payload: Option, - boundary: String, - eof: bool, - length: Option, -} - -impl InnerField { - fn new_in_rc( - payload: PayloadRef, - boundary: String, - headers: &HeaderMap, - ) -> Result>, PayloadError> { - Self::new(payload, boundary, headers).map(|this| Rc::new(RefCell::new(this))) - } - - fn new( - payload: PayloadRef, - boundary: String, - headers: &HeaderMap, - ) -> Result { - let len = if let Some(len) = headers.get(&header::CONTENT_LENGTH) { - match len.to_str().ok().and_then(|len| len.parse::().ok()) { - Some(len) => Some(len), - None => return Err(PayloadError::Incomplete(None)), - } - } else { - None - }; - - Ok(InnerField { - boundary, - payload: Some(payload), - eof: false, - length: len, - }) - } - - /// Reads body part content chunk of the specified size. - /// The body part must has `Content-Length` header with proper value. - fn read_len( - payload: &mut PayloadBuffer, - size: &mut u64, - ) -> Poll>> { - if *size == 0 { - Poll::Ready(None) - } else { - match payload.read_max(*size)? { - Some(mut chunk) => { - let len = cmp::min(chunk.len() as u64, *size); - *size -= len; - let ch = chunk.split_to(len as usize); - if !chunk.is_empty() { - payload.unprocessed(chunk); - } - Poll::Ready(Some(Ok(ch))) - } - None => { - if payload.eof && (*size != 0) { - Poll::Ready(Some(Err(MultipartError::Incomplete))) - } else { - Poll::Pending - } - } - } - } - } - - /// Reads content chunk of body part with unknown length. - /// The `Content-Length` header for body part is not necessary. - fn read_stream( - payload: &mut PayloadBuffer, - boundary: &str, - ) -> Poll>> { - let mut pos = 0; - - let len = payload.buf.len(); - if len == 0 { - return if payload.eof { - Poll::Ready(Some(Err(MultipartError::Incomplete))) - } else { - Poll::Pending - }; - } - - // check boundary - if len > 4 && payload.buf[0] == b'\r' { - let b_len = if &payload.buf[..2] == b"\r\n" && &payload.buf[2..4] == b"--" { - Some(4) - } else if &payload.buf[1..3] == b"--" { - Some(3) - } else { - None - }; - - if let Some(b_len) = b_len { - let b_size = boundary.len() + b_len; - if len < b_size { - return Poll::Pending; - } else if &payload.buf[b_len..b_size] == boundary.as_bytes() { - // found boundary - return Poll::Ready(None); - } - } - } - - loop { - return if let Some(idx) = memchr::memmem::find(&payload.buf[pos..], b"\r") { - let cur = pos + idx; - - // check if we have enough data for boundary detection - if cur + 4 > len { - if cur > 0 { - Poll::Ready(Some(Ok(payload.buf.split_to(cur).freeze()))) - } else { - Poll::Pending - } - } else { - // check boundary - if (&payload.buf[cur..cur + 2] == b"\r\n" - && &payload.buf[cur + 2..cur + 4] == b"--") - || (&payload.buf[cur..=cur] == b"\r" - && &payload.buf[cur + 1..cur + 3] == b"--") - { - if cur != 0 { - // return buffer - Poll::Ready(Some(Ok(payload.buf.split_to(cur).freeze()))) - } else { - pos = cur + 1; - continue; - } - } else { - // not boundary - pos = cur + 1; - continue; - } - } - } else { - Poll::Ready(Some(Ok(payload.buf.split().freeze()))) - }; - } - } - - fn poll(&mut self, s: &Safety) -> Poll>> { - if self.payload.is_none() { - return Poll::Ready(None); - } - - let result = if let Some(mut payload) = self.payload.as_ref().unwrap().get_mut(s) { - if !self.eof { - let res = if let Some(ref mut len) = self.length { - InnerField::read_len(&mut payload, len) - } else { - InnerField::read_stream(&mut payload, &self.boundary) - }; - - match res { - Poll::Pending => return Poll::Pending, - Poll::Ready(Some(Ok(bytes))) => return Poll::Ready(Some(Ok(bytes))), - Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))), - Poll::Ready(None) => self.eof = true, - } - } - - match payload.readline() { - Ok(None) => Poll::Pending, - Ok(Some(line)) => { - if line.as_ref() != b"\r\n" { - log::warn!("multipart field did not read all the data or it is malformed"); - } - Poll::Ready(None) - } - Err(err) => Poll::Ready(Some(Err(err))), - } - } else { - Poll::Pending - }; - - if let Poll::Ready(None) = result { - self.payload.take(); - } - result - } -} - -struct PayloadRef { - payload: Rc>, -} - -impl PayloadRef { - fn new(payload: PayloadBuffer) -> PayloadRef { - PayloadRef { - payload: Rc::new(payload.into()), - } - } - - fn get_mut(&self, s: &Safety) -> Option> { - if s.current() { - Some(self.payload.borrow_mut()) - } else { - None - } - } -} - -impl Clone for PayloadRef { - fn clone(&self) -> PayloadRef { - PayloadRef { - payload: Rc::clone(&self.payload), - } - } -} - -/// Counter. It tracks of number of clones of payloads and give access to payload only to top most. -/// * When dropped, parent task is awakened. This is to support the case where Field is -/// dropped in a separate task than Multipart. -/// * Assumes that parent owners don't move to different tasks; only the top-most is allowed to. -/// * If dropped and is not top most owner, is_clean flag is set to false. -#[derive(Debug)] -struct Safety { - task: LocalWaker, - level: usize, - payload: Rc>, - clean: Rc>, -} - -impl Safety { - fn new() -> Safety { - let payload = Rc::new(PhantomData); - Safety { - task: LocalWaker::new(), - level: Rc::strong_count(&payload), - clean: Rc::new(Cell::new(true)), - payload, - } - } - - fn current(&self) -> bool { - Rc::strong_count(&self.payload) == self.level && self.clean.get() - } - - fn is_clean(&self) -> bool { - self.clean.get() - } - - fn clone(&self, cx: &Context<'_>) -> Safety { - let payload = Rc::clone(&self.payload); - let s = Safety { - task: LocalWaker::new(), - level: Rc::strong_count(&payload), - clean: self.clean.clone(), - payload, - }; - s.task.register(cx.waker()); - s - } -} - -impl Drop for Safety { - fn drop(&mut self) { - if Rc::strong_count(&self.payload) != self.level { - // Multipart dropped leaving a Field - self.clean.set(false); - } - - self.task.wake(); - } -} - -/// Payload buffer. -struct PayloadBuffer { - eof: bool, - buf: BytesMut, - stream: LocalBoxStream<'static, Result>, -} - -impl PayloadBuffer { - /// Constructs new `PayloadBuffer` instance. - fn new(stream: S) -> Self - where - S: Stream> + 'static, - { - PayloadBuffer { - eof: false, - buf: BytesMut::new(), - stream: Box::pin(stream), - } - } - - fn poll_stream(&mut self, cx: &mut Context<'_>) -> Result<(), PayloadError> { - loop { - match Pin::new(&mut self.stream).poll_next(cx) { - Poll::Ready(Some(Ok(data))) => self.buf.extend_from_slice(&data), - Poll::Ready(Some(Err(err))) => return Err(err), - Poll::Ready(None) => { - self.eof = true; - return Ok(()); - } - Poll::Pending => return Ok(()), - } - } - } - - /// Read exact number of bytes - #[cfg(test)] - fn read_exact(&mut self, size: usize) -> Option { - if size <= self.buf.len() { - Some(self.buf.split_to(size).freeze()) - } else { - None - } - } - - fn read_max(&mut self, size: u64) -> Result, MultipartError> { - if !self.buf.is_empty() { - let size = std::cmp::min(self.buf.len() as u64, size) as usize; - Ok(Some(self.buf.split_to(size).freeze())) - } else if self.eof { - Err(MultipartError::Incomplete) - } else { - Ok(None) - } - } - - /// Read until specified ending - fn read_until(&mut self, line: &[u8]) -> Result, MultipartError> { - let res = memchr::memmem::find(&self.buf, line) - .map(|idx| self.buf.split_to(idx + line.len()).freeze()); - - if res.is_none() && self.eof { - Err(MultipartError::Incomplete) - } else { - Ok(res) - } - } - - /// Read bytes until new line delimiter - fn readline(&mut self) -> Result, MultipartError> { - self.read_until(b"\n") - } - - /// Read bytes until new line delimiter or eof - fn readline_or_eof(&mut self) -> Result, MultipartError> { - match self.readline() { - Err(MultipartError::Incomplete) if self.eof => Ok(Some(self.buf.split().freeze())), - line => line, - } - } - - /// Put unprocessed data back to the buffer - fn unprocessed(&mut self, data: Bytes) { - let buf = BytesMut::from(data.as_ref()); - let buf = std::mem::replace(&mut self.buf, buf); - self.buf.extend_from_slice(&buf); - } -} - -#[cfg(test)] -mod tests { - use std::time::Duration; - - use actix_http::h1; - use actix_web::{ - http::header::{DispositionParam, DispositionType}, - rt, - test::TestRequest, - FromRequest, - }; - use bytes::BufMut as _; - use futures_util::{future::lazy, StreamExt as _}; - use tokio::sync::mpsc; - use tokio_stream::wrappers::UnboundedReceiverStream; - - use super::*; - - const BOUNDARY: &str = "abbc761f78ff4d7cb7573b5a23f96ef0"; - - #[actix_rt::test] - async fn test_boundary() { - let headers = HeaderMap::new(); - match Multipart::boundary(&headers) { - Err(MultipartError::NoContentType) => {} - _ => unreachable!("should not happen"), - } - - let mut headers = HeaderMap::new(); - headers.insert( - header::CONTENT_TYPE, - header::HeaderValue::from_static("test"), - ); - - match Multipart::boundary(&headers) { - Err(MultipartError::ParseContentType) => {} - _ => unreachable!("should not happen"), - } - - let mut headers = HeaderMap::new(); - headers.insert( - header::CONTENT_TYPE, - header::HeaderValue::from_static("multipart/mixed"), - ); - match Multipart::boundary(&headers) { - Err(MultipartError::Boundary) => {} - _ => unreachable!("should not happen"), - } - - let mut headers = HeaderMap::new(); - headers.insert( - header::CONTENT_TYPE, - header::HeaderValue::from_static( - "multipart/mixed; boundary=\"5c02368e880e436dab70ed54e1c58209\"", - ), - ); - - assert_eq!( - Multipart::boundary(&headers).unwrap(), - "5c02368e880e436dab70ed54e1c58209" - ); - } - - fn create_stream() -> ( - mpsc::UnboundedSender>, - impl Stream>, - ) { - let (tx, rx) = mpsc::unbounded_channel(); - - ( - tx, - UnboundedReceiverStream::new(rx).map(|res| res.map_err(|_| panic!())), - ) - } - - // Stream that returns from a Bytes, one char at a time and Pending every other poll() - struct SlowStream { - bytes: Bytes, - pos: usize, - ready: bool, - } - - impl SlowStream { - fn new(bytes: Bytes) -> SlowStream { - SlowStream { - bytes, - pos: 0, - ready: false, - } - } - } - - impl Stream for SlowStream { - type Item = Result; - - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let this = self.get_mut(); - if !this.ready { - this.ready = true; - cx.waker().wake_by_ref(); - return Poll::Pending; - } - - if this.pos == this.bytes.len() { - return Poll::Ready(None); - } - - let res = Poll::Ready(Some(Ok(this.bytes.slice(this.pos..(this.pos + 1))))); - this.pos += 1; - this.ready = false; - res - } - } - - fn create_simple_request_with_header() -> (Bytes, HeaderMap) { - let (body, headers) = crate::test::create_form_data_payload_and_headers_with_boundary( - BOUNDARY, - "file", - Some("fn.txt".to_owned()), - Some(mime::TEXT_PLAIN_UTF_8), - Bytes::from_static(b"data"), - ); - - let mut buf = BytesMut::with_capacity(body.len() + 14); - - // add junk before form to test pre-boundary data rejection - buf.put("testasdadsad\r\n".as_bytes()); - - buf.put(body); - - (buf.freeze(), headers) - } - - // TODO: use test utility when multi-file support is introduced - fn create_double_request_with_header() -> (Bytes, HeaderMap) { - let bytes = Bytes::from( - "testasdadsad\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ - Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\ - Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ - test\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ - Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\ - Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ - data\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0--\r\n", - ); - let mut headers = HeaderMap::new(); - headers.insert( - header::CONTENT_TYPE, - header::HeaderValue::from_static( - "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", - ), - ); - (bytes, headers) - } - - #[actix_rt::test] - async fn test_multipart_no_end_crlf() { - let (sender, payload) = create_stream(); - let (mut bytes, headers) = create_double_request_with_header(); - let bytes_stripped = bytes.split_to(bytes.len()); // strip crlf - - sender.send(Ok(bytes_stripped)).unwrap(); - drop(sender); // eof - - let mut multipart = Multipart::new(&headers, payload); - - match multipart.next().await.unwrap() { - Ok(_) => {} - _ => unreachable!(), - } - - match multipart.next().await.unwrap() { - Ok(_) => {} - _ => unreachable!(), - } - - match multipart.next().await { - None => {} - _ => unreachable!(), - } - } - - #[actix_rt::test] - async fn test_multipart() { - let (sender, payload) = create_stream(); - let (bytes, headers) = create_double_request_with_header(); - - sender.send(Ok(bytes)).unwrap(); - - let mut multipart = Multipart::new(&headers, payload); - match multipart.next().await { - Some(Ok(mut field)) => { - let cd = field.content_disposition(); - assert_eq!(cd.disposition, DispositionType::FormData); - assert_eq!(cd.parameters[0], DispositionParam::Name("file".into())); - - assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); - assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); - - match field.next().await.unwrap() { - Ok(chunk) => assert_eq!(chunk, "test"), - _ => unreachable!(), - } - match field.next().await { - None => {} - _ => unreachable!(), - } - } - _ => unreachable!(), - } - - match multipart.next().await.unwrap() { - Ok(mut field) => { - assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); - assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); - - match field.next().await { - Some(Ok(chunk)) => assert_eq!(chunk, "data"), - _ => unreachable!(), - } - match field.next().await { - None => {} - _ => unreachable!(), - } - } - _ => unreachable!(), - } - - match multipart.next().await { - None => {} - _ => unreachable!(), - } - } - - // Loops, collecting all bytes until end-of-field - async fn get_whole_field(field: &mut Field) -> BytesMut { - let mut b = BytesMut::new(); - loop { - match field.next().await { - Some(Ok(chunk)) => b.extend_from_slice(&chunk), - None => return b, - _ => unreachable!(), - } - } - } - - #[actix_rt::test] - async fn test_stream() { - let (bytes, headers) = create_double_request_with_header(); - let payload = SlowStream::new(bytes); - - let mut multipart = Multipart::new(&headers, payload); - match multipart.next().await.unwrap() { - Ok(mut field) => { - let cd = field.content_disposition(); - assert_eq!(cd.disposition, DispositionType::FormData); - assert_eq!(cd.parameters[0], DispositionParam::Name("file".into())); - - assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); - assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); - - assert_eq!(get_whole_field(&mut field).await, "test"); - } - _ => unreachable!(), - } - - match multipart.next().await { - Some(Ok(mut field)) => { - assert_eq!(field.content_type().unwrap().type_(), mime::TEXT); - assert_eq!(field.content_type().unwrap().subtype(), mime::PLAIN); - - assert_eq!(get_whole_field(&mut field).await, "data"); - } - _ => unreachable!(), - } - - match multipart.next().await { - None => {} - _ => unreachable!(), - } - } - - #[actix_rt::test] - async fn test_basic() { - let (_, payload) = h1::Payload::create(false); - let mut payload = PayloadBuffer::new(payload); - - assert_eq!(payload.buf.len(), 0); - lazy(|cx| payload.poll_stream(cx)).await.unwrap(); - assert_eq!(None, payload.read_max(1).unwrap()); - } - - #[actix_rt::test] - async fn test_eof() { - let (mut sender, payload) = h1::Payload::create(false); - let mut payload = PayloadBuffer::new(payload); - - assert_eq!(None, payload.read_max(4).unwrap()); - sender.feed_data(Bytes::from("data")); - sender.feed_eof(); - lazy(|cx| payload.poll_stream(cx)).await.unwrap(); - - assert_eq!(Some(Bytes::from("data")), payload.read_max(4).unwrap()); - assert_eq!(payload.buf.len(), 0); - assert!(payload.read_max(1).is_err()); - assert!(payload.eof); - } - - #[actix_rt::test] - async fn test_err() { - let (mut sender, payload) = h1::Payload::create(false); - let mut payload = PayloadBuffer::new(payload); - assert_eq!(None, payload.read_max(1).unwrap()); - sender.set_error(PayloadError::Incomplete(None)); - lazy(|cx| payload.poll_stream(cx)).await.err().unwrap(); - } - - #[actix_rt::test] - async fn test_readmax() { - let (mut sender, payload) = h1::Payload::create(false); - let mut payload = PayloadBuffer::new(payload); - - sender.feed_data(Bytes::from("line1")); - sender.feed_data(Bytes::from("line2")); - lazy(|cx| payload.poll_stream(cx)).await.unwrap(); - assert_eq!(payload.buf.len(), 10); - - assert_eq!(Some(Bytes::from("line1")), payload.read_max(5).unwrap()); - assert_eq!(payload.buf.len(), 5); - - assert_eq!(Some(Bytes::from("line2")), payload.read_max(5).unwrap()); - assert_eq!(payload.buf.len(), 0); - } - - #[actix_rt::test] - async fn test_readexactly() { - let (mut sender, payload) = h1::Payload::create(false); - let mut payload = PayloadBuffer::new(payload); - - assert_eq!(None, payload.read_exact(2)); - - sender.feed_data(Bytes::from("line1")); - sender.feed_data(Bytes::from("line2")); - lazy(|cx| payload.poll_stream(cx)).await.unwrap(); - - assert_eq!(Some(Bytes::from_static(b"li")), payload.read_exact(2)); - assert_eq!(payload.buf.len(), 8); - - assert_eq!(Some(Bytes::from_static(b"ne1l")), payload.read_exact(4)); - assert_eq!(payload.buf.len(), 4); - } - - #[actix_rt::test] - async fn test_readuntil() { - let (mut sender, payload) = h1::Payload::create(false); - let mut payload = PayloadBuffer::new(payload); - - assert_eq!(None, payload.read_until(b"ne").unwrap()); - - sender.feed_data(Bytes::from("line1")); - sender.feed_data(Bytes::from("line2")); - lazy(|cx| payload.poll_stream(cx)).await.unwrap(); - - assert_eq!( - Some(Bytes::from("line")), - payload.read_until(b"ne").unwrap() - ); - assert_eq!(payload.buf.len(), 6); - - assert_eq!( - Some(Bytes::from("1line2")), - payload.read_until(b"2").unwrap() - ); - assert_eq!(payload.buf.len(), 0); - } - - #[actix_rt::test] - async fn test_multipart_from_error() { - let err = MultipartError::NoContentType; - let mut multipart = Multipart::from_error(err); - assert!(multipart.next().await.unwrap().is_err()) - } - - #[actix_rt::test] - async fn test_multipart_from_boundary() { - let (_, payload) = create_stream(); - let (_, headers) = create_simple_request_with_header(); - let boundary = Multipart::boundary(&headers); - assert!(boundary.is_ok()); - let _ = Multipart::from_boundary(boundary.unwrap(), payload); - } - - #[actix_rt::test] - async fn test_multipart_payload_consumption() { - // with sample payload and HttpRequest with no headers - let (_, inner_payload) = h1::Payload::create(false); - let mut payload = actix_web::dev::Payload::from(inner_payload); - let req = TestRequest::default().to_http_request(); - - // multipart should generate an error - let mut mp = Multipart::from_request(&req, &mut payload).await.unwrap(); - assert!(mp.next().await.unwrap().is_err()); - - // and should not consume the payload - match payload { - actix_web::dev::Payload::H1 { .. } => {} //expected - _ => unreachable!(), - } - } - - #[actix_rt::test] - async fn no_content_disposition() { - let bytes = Bytes::from( - "testasdadsad\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ - Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ - test\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0\r\n", - ); - let mut headers = HeaderMap::new(); - headers.insert( - header::CONTENT_TYPE, - header::HeaderValue::from_static( - "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", - ), - ); - let payload = SlowStream::new(bytes); - - let mut multipart = Multipart::new(&headers, payload); - let res = multipart.next().await.unwrap(); - assert!(res.is_err()); - assert!(matches!( - res.unwrap_err(), - MultipartError::NoContentDisposition, - )); - } - - #[actix_rt::test] - async fn no_name_in_content_disposition() { - let bytes = Bytes::from( - "testasdadsad\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ - Content-Disposition: form-data; filename=\"fn.txt\"\r\n\ - Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ - test\r\n\ - --abbc761f78ff4d7cb7573b5a23f96ef0\r\n", - ); - let mut headers = HeaderMap::new(); - headers.insert( - header::CONTENT_TYPE, - header::HeaderValue::from_static( - "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", - ), - ); - let payload = SlowStream::new(bytes); - - let mut multipart = Multipart::new(&headers, payload); - let res = multipart.next().await.unwrap(); - assert!(res.is_err()); - assert!(matches!( - res.unwrap_err(), - MultipartError::NoContentDisposition, - )); - } - - #[actix_rt::test] - async fn test_drop_multipart_dont_hang() { - let (sender, payload) = create_stream(); - let (bytes, headers) = create_simple_request_with_header(); - sender.send(Ok(bytes)).unwrap(); - drop(sender); // eof - - let mut multipart = Multipart::new(&headers, payload); - let mut field = multipart.next().await.unwrap().unwrap(); - - drop(multipart); - - // should fail immediately - match field.next().await { - Some(Err(MultipartError::NotConsumed)) => {} - _ => panic!(), - }; - } - - #[actix_rt::test] - async fn test_drop_field_awaken_multipart() { - let (sender, payload) = create_stream(); - let (bytes, headers) = create_double_request_with_header(); - sender.send(Ok(bytes)).unwrap(); - drop(sender); // eof - - let mut multipart = Multipart::new(&headers, payload); - let mut field = multipart.next().await.unwrap().unwrap(); - - let task = rt::spawn(async move { - rt::time::sleep(Duration::from_secs(1)).await; - assert_eq!(field.next().await.unwrap().unwrap(), "test"); - drop(field); - }); - - // dropping field should awaken current task - let _ = multipart.next().await.unwrap().unwrap(); - task.await.unwrap(); - } -} diff --git a/actix-multipart/src/test.rs b/actix-multipart/src/test.rs index 77d91828..7dec85f8 100644 --- a/actix-multipart/src/test.rs +++ b/actix-multipart/src/test.rs @@ -1,5 +1,9 @@ -use actix_web::http::header::{self, HeaderMap}; -use bytes::{BufMut as _, Bytes, BytesMut}; +//! Multipart testing utilities. + +use actix_web::{ + http::header::{self, HeaderMap}, + web::{BufMut as _, Bytes, BytesMut}, +}; use mime::Mime; use rand::{ distributions::{Alphanumeric, DistString as _}, @@ -21,8 +25,7 @@ const BOUNDARY_PREFIX: &str = "------------------------"; /// /// ``` /// use actix_multipart::test::create_form_data_payload_and_headers; -/// use actix_web::test::TestRequest; -/// use bytes::Bytes; +/// use actix_web::{test::TestRequest, web::Bytes}; /// use memchr::memmem::find; /// /// let (body, headers) = create_form_data_payload_and_headers( diff --git a/actix-router/Cargo.toml b/actix-router/Cargo.toml index 56e4bed2..7def1bdb 100644 --- a/actix-router/Cargo.toml +++ b/actix-router/Cargo.toml @@ -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"] @@ -36,6 +38,9 @@ http = "0.2.7" serde = { version = "1", features = ["derive"] } percent-encoding = "2.1" +[lints] +workspace = true + [[bench]] name = "router" harness = false diff --git a/actix-router/src/de.rs b/actix-router/src/de.rs index ce2dcf8f..2f50619f 100644 --- a/actix-router/src/de.rs +++ b/actix-router/src/de.rs @@ -511,11 +511,6 @@ mod tests { value: String, } - #[derive(Deserialize)] - struct Id { - _id: String, - } - #[derive(Debug, Deserialize)] struct Test1(String, u32); diff --git a/actix-router/src/lib.rs b/actix-router/src/lib.rs index c4d0d2c8..3f5e969e 100644 --- a/actix-router/src/lib.rs +++ b/actix-router/src/lib.rs @@ -1,7 +1,5 @@ //! Resource path matching and router. -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] diff --git a/actix-router/src/path.rs b/actix-router/src/path.rs index 9031ab76..ab4a943f 100644 --- a/actix-router/src/path.rs +++ b/actix-router/src/path.rs @@ -143,9 +143,9 @@ impl Path { for (seg_name, val) in self.segments.iter() { if name == seg_name { return match val { - PathItem::Static(ref s) => Some(s), - PathItem::Segment(s, e) => { - Some(&self.path.path()[(*s as usize)..(*e as usize)]) + PathItem::Static(ref seg) => Some(seg), + PathItem::Segment(start, end) => { + Some(&self.path.path()[(*start as usize)..(*end as usize)]) } }; } @@ -193,8 +193,10 @@ impl<'a, T: ResourcePath> Iterator for PathIter<'a, T> { if self.idx < self.params.segment_count() { let idx = self.idx; let res = match self.params.segments[idx].1 { - PathItem::Static(ref s) => s, - PathItem::Segment(s, e) => &self.params.path.path()[(s as usize)..(e as usize)], + PathItem::Static(ref seg) => seg, + PathItem::Segment(start, end) => { + &self.params.path.path()[(start as usize)..(end as usize)] + } }; self.idx += 1; return Some((&self.params.segments[idx].0, res)); @@ -217,8 +219,8 @@ impl Index for Path { fn index(&self, idx: usize) -> &str { match self.segments[idx].1 { - PathItem::Static(ref s) => s, - PathItem::Segment(s, e) => &self.path.path()[(s as usize)..(e as usize)], + PathItem::Static(ref seg) => seg, + PathItem::Segment(start, end) => &self.path.path()[(start as usize)..(end as usize)], } } } diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index b55a8305..ec2dd677 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -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 diff --git a/actix-test/Cargo.toml b/actix-test/Cargo.toml index dddcabec..34fdf2c8 100644 --- a/actix-test/Cargo.toml +++ b/actix-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-test" -version = "0.1.3" +version = "0.1.5" authors = [ "Nikolay Kim ", "Rob Ede ", @@ -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 = [] @@ -57,3 +73,6 @@ tls-rustls-0_21 = { package = "rustls", version = "0.21", optional = true } tls-rustls-0_22 = { package = "rustls", version = "0.22", optional = true } tls-rustls-0_23 = { package = "rustls", version = "0.23", default-features = false, optional = true } tokio = { version = "1.24.2", features = ["sync"] } + +[lints] +workspace = true diff --git a/actix-test/README.md b/actix-test/README.md new file mode 100644 index 00000000..1a9b6f22 --- /dev/null +++ b/actix-test/README.md @@ -0,0 +1,45 @@ +# `actix-test` + + + +[![crates.io](https://img.shields.io/crates/v/actix-test?label=latest)](https://crates.io/crates/actix-test) +[![Documentation](https://docs.rs/actix-test/badge.svg?version=0.1.5)](https://docs.rs/actix-test/0.1.5) +![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) +![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-test.svg) +
+[![dependency status](https://deps.rs/crate/actix-test/0.1.5/status.svg)](https://deps.rs/crate/actix-test/0.1.5) +[![Download](https://img.shields.io/crates/d/actix-test.svg)](https://crates.io/crates/actix-test) +[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) + + + + + +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 { + 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()); +} +``` + + diff --git a/actix-test/src/lib.rs b/actix-test/src/lib.rs index 1c3d8ff1..f0da2c20 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -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}; //! @@ -26,8 +27,6 @@ //! } //! ``` -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] @@ -149,10 +148,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 +461,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 +487,7 @@ enum HttpVer { Both, } +#[allow(clippy::large_enum_variant)] #[derive(Clone)] enum StreamType { Tcp, @@ -505,8 +513,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 +532,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 +608,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) -> Self { + self.listen_address = addr.into(); + self + } + /// Sets test server port. /// /// By default, a random free port is determined by the OS. @@ -611,6 +631,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 +666,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) } } diff --git a/actix-web-actors/CHANGES.md b/actix-web-actors/CHANGES.md index 9a622d8d..3f214274 100644 --- a/actix-web-actors/CHANGES.md +++ b/actix-web-actors/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +## 4.3.1 + +- Reduce memory usage by `take`-ing (rather than `split`-ing) the encoded buffer when yielding bytes in the response stream. +- Mark crate as deprecated. - Minimum supported Rust version (MSRV) is now 1.72. ## 4.3.0 diff --git a/actix-web-actors/Cargo.toml b/actix-web-actors/Cargo.toml index 114ec5a8..e7034ab8 100644 --- a/actix-web-actors/Cargo.toml +++ b/actix-web-actors/Cargo.toml @@ -1,17 +1,24 @@ [package] name = "actix-web-actors" -version = "4.3.0" +version = "4.3.1+deprecated" authors = ["Nikolay Kim "] description = "Actix actors support for Actix Web" keywords = ["actix", "http", "web", "framework", "async"] -homepage = "https://actix.rs" -repository = "https://github.com/actix/actix-web" -license = "MIT OR Apache-2.0" -edition = "2021" +homepage.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true +rust-version.workspace = true -[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 } @@ -35,3 +42,6 @@ actix-web = { version = "4", features = ["macros"] } env_logger = "0.11" futures-util = { version = "0.3.17", default-features = false, features = ["std"] } mime = "0.3" + +[lints] +workspace = true diff --git a/actix-web-actors/README.md b/actix-web-actors/README.md index feb3d1b3..0ec91a22 100644 --- a/actix-web-actors/README.md +++ b/actix-web-actors/README.md @@ -1,15 +1,17 @@ # `actix-web-actors` > Actix actors support for Actix Web. +> +> This crate is deprecated. Migrate to [`actix-ws`](https://crates.io/crates/actix-ws). [![crates.io](https://img.shields.io/crates/v/actix-web-actors?label=latest)](https://crates.io/crates/actix-web-actors) -[![Documentation](https://docs.rs/actix-web-actors/badge.svg?version=4.3.0)](https://docs.rs/actix-web-actors/4.3.0) +[![Documentation](https://docs.rs/actix-web-actors/badge.svg?version=4.3.1)](https://docs.rs/actix-web-actors/4.3.1) ![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![License](https://img.shields.io/crates/l/actix-web-actors.svg)
-[![dependency status](https://deps.rs/crate/actix-web-actors/4.3.0/status.svg)](https://deps.rs/crate/actix-web-actors/4.3.0) +![maintenance-status](https://img.shields.io/badge/maintenance-deprecated-red.svg) [![Download](https://img.shields.io/crates/d/actix-web-actors.svg)](https://crates.io/crates/actix-web-actors) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) diff --git a/actix-web-actors/src/lib.rs b/actix-web-actors/src/lib.rs index d89b0ee3..4831d263 100644 --- a/actix-web-actors/src/lib.rs +++ b/actix-web-actors/src/lib.rs @@ -1,5 +1,7 @@ //! Actix actors support for Actix Web. //! +//! This crate is deprecated. Migrate to [`actix-ws`](https://crates.io/crates/actix-ws). +//! //! # Examples //! //! ```no_run @@ -55,8 +57,6 @@ //! * [`HttpContext`]: This struct provides actor support for streaming HTTP responses. //! -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index 1fb90322..0002f87e 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -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 { @@ -796,11 +796,8 @@ where Some(frm) => { let msg = match frm { Frame::Text(data) => { - Message::Text(ByteString::try_from(data).map_err(|e| { - ProtocolError::Io(io::Error::new( - io::ErrorKind::Other, - format!("{}", e), - )) + Message::Text(ByteString::try_from(data).map_err(|err| { + ProtocolError::Io(io::Error::new(io::ErrorKind::Other, err)) })?) } Frame::Binary(data) => Message::Binary(data), diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index a5acdd21..d143723f 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -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 diff --git a/actix-web-codegen/Cargo.toml b/actix-web-codegen/Cargo.toml index 4a45d4fe..d3938da8 100644 --- a/actix-web-codegen/Cargo.toml +++ b/actix-web-codegen/Cargo.toml @@ -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 ", @@ -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"] } @@ -31,3 +35,6 @@ actix-web = "4" futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } trybuild = "1" rustversion = "1" + +[lints] +workspace = true diff --git a/actix-web-codegen/README.md b/actix-web-codegen/README.md index 9229f8f1..e61bf5c7 100644 --- a/actix-web-codegen/README.md +++ b/actix-web-codegen/README.md @@ -5,11 +5,11 @@ [![crates.io](https://img.shields.io/crates/v/actix-web-codegen?label=latest)](https://crates.io/crates/actix-web-codegen) -[![Documentation](https://docs.rs/actix-web-codegen/badge.svg?version=4.2.2)](https://docs.rs/actix-web-codegen/4.2.2) +[![Documentation](https://docs.rs/actix-web-codegen/badge.svg?version=4.3.0)](https://docs.rs/actix-web-codegen/4.3.0) ![Version](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![License](https://img.shields.io/crates/l/actix-web-codegen.svg)
-[![dependency status](https://deps.rs/crate/actix-web-codegen/4.2.2/status.svg)](https://deps.rs/crate/actix-web-codegen/4.2.2) +[![dependency status](https://deps.rs/crate/actix-web-codegen/4.3.0/status.svg)](https://deps.rs/crate/actix-web-codegen/4.3.0) [![Download](https://img.shields.io/crates/d/actix-web-codegen.svg)](https://crates.io/crates/actix-web-codegen) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 6d6c9ab5..e22bff8c 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -73,8 +73,6 @@ //! [DELETE]: macro@delete #![recursion_limit = "512"] -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] @@ -83,6 +81,7 @@ use proc_macro::TokenStream; use quote::quote; mod route; +mod scope; /// Creates resource handler, allowing multiple HTTP method guards. /// @@ -197,6 +196,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 +276,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 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 +} diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index 7a2dfc05..e24903e3 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -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, + pub(crate) path: syn::LitStr, + pub(crate) options: Punctuated, } impl syn::parse::Parse for RouteArgs { @@ -78,7 +80,7 @@ macro_rules! standard_method_type { } } - fn from_path(method: &Path) -> Result { + pub(crate) fn from_path(method: &Path) -> Result { 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(::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 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 -} diff --git a/actix-web-codegen/src/scope.rs b/actix-web-codegen/src/scope.rs new file mode 100644 index 00000000..067d95a6 --- /dev/null +++ b/actix-web-codegen/src/scope.rs @@ -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 { + 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::(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::(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::(), 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 = 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") +} diff --git a/actix-web-codegen/tests/test_macro.rs b/actix-web-codegen/tests/routes.rs similarity index 99% rename from actix-web-codegen/tests/test_macro.rs rename to actix-web-codegen/tests/routes.rs index fb50d4ae..a6e60687 100644 --- a/actix-web-codegen/tests/test_macro.rs +++ b/actix-web-codegen/tests/routes.rs @@ -145,7 +145,7 @@ async fn custom_resource_name_test<'a>(req: HttpRequest) -> impl Responder { mod guard_module { use actix_web::{guard::GuardContext, http::header}; - pub fn guard(ctx: &GuardContext) -> bool { + pub fn guard(ctx: &GuardContext<'_>) -> bool { ctx.header::() .map(|h| h.preference() == "image/*") .unwrap_or(false) diff --git a/actix-web-codegen/tests/scopes.rs b/actix-web-codegen/tests/scopes.rs new file mode 100644 index 00000000..b8c83268 --- /dev/null +++ b/actix-web-codegen/tests/scopes.rs @@ -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::() + .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) -> 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"); +} diff --git a/actix-web-codegen/tests/trybuild.rs b/actix-web-codegen/tests/trybuild.rs index 88f77548..91073cf3 100644 --- a/actix-web-codegen/tests/trybuild.rs +++ b/actix-web-codegen/tests/trybuild.rs @@ -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"); diff --git a/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr b/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr index 93c51010..c1100c78 100644 --- a/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr +++ b/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr @@ -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 diff --git a/actix-web-codegen/tests/trybuild/scope-invalid-args.rs b/actix-web-codegen/tests/trybuild/scope-invalid-args.rs new file mode 100644 index 00000000..ec021d5e --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-invalid-args.rs @@ -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() {} diff --git a/actix-web-codegen/tests/trybuild/scope-invalid-args.stderr b/actix-web-codegen/tests/trybuild/scope-invalid-args.stderr new file mode 100644 index 00000000..0ab33596 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-invalid-args.stderr @@ -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)] + | ^^^ diff --git a/actix-web-codegen/tests/trybuild/scope-missing-args.rs b/actix-web-codegen/tests/trybuild/scope-missing-args.rs new file mode 100644 index 00000000..39bcb9d1 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-missing-args.rs @@ -0,0 +1,6 @@ +use actix_web_codegen::scope; + +#[scope] +mod api {} + +fn main() {} diff --git a/actix-web-codegen/tests/trybuild/scope-missing-args.stderr b/actix-web-codegen/tests/trybuild/scope-missing-args.stderr new file mode 100644 index 00000000..d59842e3 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-missing-args.stderr @@ -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) diff --git a/actix-web-codegen/tests/trybuild/scope-on-handler.rs b/actix-web-codegen/tests/trybuild/scope-on-handler.rs new file mode 100644 index 00000000..e5d47898 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-on-handler.rs @@ -0,0 +1,8 @@ +use actix_web_codegen::scope; + +#[scope("/api")] +async fn index() -> &'static str { + "Hello World!" +} + +fn main() {} diff --git a/actix-web-codegen/tests/trybuild/scope-on-handler.stderr b/actix-web-codegen/tests/trybuild/scope-on-handler.stderr new file mode 100644 index 00000000..4491f42d --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-on-handler.stderr @@ -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 { + | ^^^^^ diff --git a/actix-web-codegen/tests/trybuild/scope-trailing-slash.rs b/actix-web-codegen/tests/trybuild/scope-trailing-slash.rs new file mode 100644 index 00000000..84632b59 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-trailing-slash.rs @@ -0,0 +1,6 @@ +use actix_web_codegen::scope; + +#[scope("/api/")] +mod api {} + +fn main() {} diff --git a/actix-web-codegen/tests/trybuild/scope-trailing-slash.stderr b/actix-web-codegen/tests/trybuild/scope-trailing-slash.stderr new file mode 100644 index 00000000..66933432 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/scope-trailing-slash.stderr @@ -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/")] + | ^^^^^^^ diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 993c7c59..cee14dc4 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,42 @@ ## Unreleased +- On Windows, an error is now returned from `HttpServer::bind()` (or TLS variants) when binding to a socket that's already in use. +- Update `brotli` dependency to `7`. +- Minimum supported Rust version (MSRV) is now 1.75. + +## 4.9.0 + +### Added + +- Add `middleware::from_fn()` helper. +- Add `web::ThinData` extractor. + +## 4.8.0 + +### Added + +- 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>` for `Error`. + ## 4.6.0 ### Added diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 9f3ab6e5..9b10f14b 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web" -version = "4.6.0" +version = "4.9.0" description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust" authors = [ "Nikolay Kim ", @@ -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 } @@ -96,18 +139,19 @@ actix-tls = { version = "3.4", default-features = false, optional = true } actix-http = { version = "3.7", features = ["ws"] } actix-router = { version = "0.5.3", default-features = false, features = ["http"] } -actix-web-codegen = { version = "4.2", optional = true } +actix-web-codegen = { version = "4.3", optional = true, default-features = false } ahash = "0.8" bytes = "1" bytestring = "1" cfg-if = "1" cookie = { version = "0.16", features = ["percent-encode"], optional = true } -derive_more = "0.99.8" +derive_more = { version = "1", features = ["display", "error", "from"] } encoding_rs = "0.8" futures-core = { version = "0.3.17", default-features = false } futures-util = { version = "0.3.17", default-features = false } itoa = "1" +impl-more = "0.1.4" language-tags = "0.3" log = "0.4" mime = "0.3" @@ -119,6 +163,7 @@ serde = "1.0" serde_json = "1.0" serde_urlencoded = "0.7" smallvec = "1.6.1" +tracing = "0.1.30" socket2 = "0.5" time = { version = "0.3", default-features = false, features = ["formatting"] } url = "2.1" @@ -128,8 +173,9 @@ actix-files = "0.6" actix-test = { version = "0.1", features = ["openssl", "rustls-0_23"] } awc = { version = "3", features = ["openssl"] } -brotli = "6" +brotli = "7" const-str = "0.5" +core_affinity = "0.8" criterion = { version = "0.5", features = ["html_reports"] } env_logger = "0.11" flate2 = "1.0.13" @@ -137,13 +183,16 @@ futures-util = { version = "0.3.17", default-features = false, features = ["std" rand = "0.8" rcgen = "0.13" rustls-pemfile = "2" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1", features = ["derive"] } static_assertions = "1" tls-openssl = { package = "openssl", version = "0.10.55" } tls-rustls = { package = "rustls", version = "0.23" } tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] } zstd = "0.13" +[lints] +workspace = true + [[test]] name = "test_server" required-features = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"] diff --git a/actix-web/README.md b/actix-web/README.md index 4e7e785a..e5e412c8 100644 --- a/actix-web/README.md +++ b/actix-web/README.md @@ -8,10 +8,10 @@ [![crates.io](https://img.shields.io/crates/v/actix-web?label=latest)](https://crates.io/crates/actix-web) -[![Documentation](https://docs.rs/actix-web/badge.svg?version=4.6.0)](https://docs.rs/actix-web/4.6.0) +[![Documentation](https://docs.rs/actix-web/badge.svg?version=4.9.0)](https://docs.rs/actix-web/4.9.0) ![MSRV](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-web.svg) -[![Dependency Status](https://deps.rs/crate/actix-web/4.6.0/status.svg)](https://deps.rs/crate/actix-web/4.6.0) +[![Dependency Status](https://deps.rs/crate/actix-web/4.9.0/status.svg)](https://deps.rs/crate/actix-web/4.9.0)
[![CI](https://github.com/actix/actix-web/actions/workflows/ci.yml/badge.svg)](https://github.com/actix/actix-web/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) @@ -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. diff --git a/actix-web/benches/responder.rs b/actix-web/benches/responder.rs index c675eadf..489515e4 100644 --- a/actix-web/benches/responder.rs +++ b/actix-web/benches/responder.rs @@ -2,11 +2,9 @@ use std::{future::Future, time::Instant}; use actix_http::body::BoxBody; use actix_utils::future::{ready, Ready}; -use actix_web::{ - error, http::StatusCode, test::TestRequest, Error, HttpRequest, HttpResponse, Responder, -}; +use actix_web::{http::StatusCode, test::TestRequest, Error, HttpRequest, HttpResponse, Responder}; use criterion::{criterion_group, criterion_main, Criterion}; -use futures_util::future::{join_all, Either}; +use futures_util::future::join_all; // responder simulate the old responder trait. trait FutureResponder { @@ -16,9 +14,6 @@ trait FutureResponder { fn future_respond_to(self, req: &HttpRequest) -> Self::Future; } -// a simple option responder type. -struct OptionResponder(Option); - // a simple wrapper type around string struct StringResponder(String); @@ -34,22 +29,6 @@ impl FutureResponder for StringResponder { } } -impl FutureResponder for OptionResponder -where - T: FutureResponder, - T::Future: Future>, -{ - type Error = Error; - type Future = Either>>; - - fn future_respond_to(self, req: &HttpRequest) -> Self::Future { - match self.0 { - Some(t) => Either::Left(t.future_respond_to(req)), - None => Either::Right(ready(Err(error::ErrorInternalServerError("err")))), - } - } -} - impl Responder for StringResponder { type Body = BoxBody; @@ -60,17 +39,6 @@ impl Responder for StringResponder { } } -impl Responder for OptionResponder { - type Body = BoxBody; - - fn respond_to(self, req: &HttpRequest) -> HttpResponse { - match self.0 { - Some(t) => t.respond_to(req).map_into_boxed_body(), - None => HttpResponse::from_error(error::ErrorInternalServerError("err")), - } - } -} - fn future_responder(c: &mut Criterion) { let rt = actix_rt::System::new(); let req = TestRequest::default().to_http_request(); diff --git a/actix-web/examples/from_fn.rs b/actix-web/examples/from_fn.rs new file mode 100644 index 00000000..a6006d23 --- /dev/null +++ b/actix-web/examples/from_fn.rs @@ -0,0 +1,128 @@ +//! Shows a few of ways to use the `from_fn` middleware. + +use std::{collections::HashMap, io, rc::Rc, time::Duration}; + +use actix_web::{ + body::MessageBody, + dev::{Service, ServiceRequest, ServiceResponse, Transform}, + http::header::{self, HeaderValue, Range}, + middleware::{from_fn, Logger, Next}, + web::{self, Header, Query}, + App, Error, HttpResponse, HttpServer, +}; +use tracing::info; + +async fn noop(req: ServiceRequest, next: Next) -> Result, Error> { + next.call(req).await +} + +async fn print_range_header( + range_header: Option>, + req: ServiceRequest, + next: Next, +) -> Result, Error> { + if let Some(Header(range)) = range_header { + println!("Range: {range}"); + } else { + println!("No Range header"); + } + + next.call(req).await +} + +async fn mutate_body_type( + req: ServiceRequest, + next: Next, +) -> Result, Error> { + let res = next.call(req).await?; + Ok(res.map_into_left_body::<()>()) +} + +async fn mutate_body_type_with_extractors( + string_body: String, + query: Query>, + req: ServiceRequest, + next: Next, +) -> Result, Error> { + println!("body is: {string_body}"); + println!("query string: {query:?}"); + + let res = next.call(req).await?; + + Ok(res.map_body(move |_, _| string_body)) +} + +async fn timeout_10secs( + req: ServiceRequest, + next: Next, +) -> Result, Error> { + match tokio::time::timeout(Duration::from_secs(10), next.call(req)).await { + Ok(res) => res, + Err(_err) => Err(actix_web::error::ErrorRequestTimeout("")), + } +} + +struct MyMw(bool); + +impl MyMw { + async fn mw_cb( + &self, + req: ServiceRequest, + next: Next, + ) -> Result, Error> { + let mut res = match self.0 { + true => req.into_response("short-circuited").map_into_right_body(), + false => next.call(req).await?.map_into_left_body(), + }; + + res.headers_mut() + .insert(header::WARNING, HeaderValue::from_static("42")); + + Ok(res) + } + + pub fn into_middleware( + self, + ) -> impl Transform< + S, + ServiceRequest, + Response = ServiceResponse, + Error = Error, + InitError = (), + > + where + S: Service, Error = Error> + 'static, + B: MessageBody + 'static, + { + let this = Rc::new(self); + from_fn(move |req, next| { + let this = Rc::clone(&this); + async move { Self::mw_cb(&this, req, next).await } + }) + } +} + +#[actix_web::main] +async fn main() -> io::Result<()> { + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + let bind = ("127.0.0.1", 8080); + info!("staring server at http://{}:{}", &bind.0, &bind.1); + + HttpServer::new(|| { + App::new() + .wrap(from_fn(noop)) + .wrap(from_fn(print_range_header)) + .wrap(from_fn(mutate_body_type)) + .wrap(from_fn(mutate_body_type_with_extractors)) + .wrap(from_fn(timeout_10secs)) + // switch bool to true to observe early response + .wrap(MyMw(false).into_middleware()) + .wrap(Logger::default()) + .default_service(web::to(HttpResponse::Ok)) + }) + .workers(1) + .bind(bind)? + .run() + .await +} diff --git a/actix-web/examples/middleware_from_fn.rs b/actix-web/examples/middleware_from_fn.rs new file mode 100644 index 00000000..da92ef05 --- /dev/null +++ b/actix-web/examples/middleware_from_fn.rs @@ -0,0 +1,127 @@ +//! Shows a couple of ways to use the `from_fn` middleware. + +use std::{collections::HashMap, io, rc::Rc, time::Duration}; + +use actix_web::{ + body::MessageBody, + dev::{Service, ServiceRequest, ServiceResponse, Transform}, + http::header::{self, HeaderValue, Range}, + middleware::{from_fn, Logger, Next}, + web::{self, Header, Query}, + App, Error, HttpResponse, HttpServer, +}; + +async fn noop(req: ServiceRequest, next: Next) -> Result, Error> { + next.call(req).await +} + +async fn print_range_header( + range_header: Option>, + req: ServiceRequest, + next: Next, +) -> Result, Error> { + if let Some(Header(range)) = range_header { + println!("Range: {range}"); + } else { + println!("No Range header"); + } + + next.call(req).await +} + +async fn mutate_body_type( + req: ServiceRequest, + next: Next, +) -> Result, Error> { + let res = next.call(req).await?; + Ok(res.map_into_left_body::<()>()) +} + +async fn mutate_body_type_with_extractors( + string_body: String, + query: Query>, + req: ServiceRequest, + next: Next, +) -> Result, Error> { + println!("body is: {string_body}"); + println!("query string: {query:?}"); + + let res = next.call(req).await?; + + Ok(res.map_body(move |_, _| string_body)) +} + +async fn timeout_10secs( + req: ServiceRequest, + next: Next, +) -> Result, Error> { + match tokio::time::timeout(Duration::from_secs(10), next.call(req)).await { + Ok(res) => res, + Err(_err) => Err(actix_web::error::ErrorRequestTimeout("")), + } +} + +struct MyMw(bool); + +impl MyMw { + async fn mw_cb( + &self, + req: ServiceRequest, + next: Next, + ) -> Result, Error> { + let mut res = match self.0 { + true => req.into_response("short-circuited").map_into_right_body(), + false => next.call(req).await?.map_into_left_body(), + }; + + res.headers_mut() + .insert(header::WARNING, HeaderValue::from_static("42")); + + Ok(res) + } + + pub fn into_middleware( + self, + ) -> impl Transform< + S, + ServiceRequest, + Response = ServiceResponse, + Error = Error, + InitError = (), + > + where + S: Service, Error = Error> + 'static, + B: MessageBody + 'static, + { + let this = Rc::new(self); + from_fn(move |req, next| { + let this = Rc::clone(&this); + async move { Self::mw_cb(&this, req, next).await } + }) + } +} + +#[actix_web::main] +async fn main() -> io::Result<()> { + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + let bind = ("127.0.0.1", 8080); + log::info!("staring server at http://{}:{}", &bind.0, &bind.1); + + HttpServer::new(|| { + App::new() + .wrap(from_fn(noop)) + .wrap(from_fn(print_range_header)) + .wrap(from_fn(mutate_body_type)) + .wrap(from_fn(mutate_body_type_with_extractors)) + .wrap(from_fn(timeout_10secs)) + // switch bool to true to observe early response + .wrap(MyMw(false).into_middleware()) + .wrap(Logger::default()) + .default_service(web::to(HttpResponse::Ok)) + }) + .workers(1) + .bind(bind)? + .run() + .await +} diff --git a/actix-web/examples/worker-cpu-pin.rs b/actix-web/examples/worker-cpu-pin.rs new file mode 100644 index 00000000..58e06082 --- /dev/null +++ b/actix-web/examples/worker-cpu-pin.rs @@ -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 +} diff --git a/actix-web/src/app.rs b/actix-web/src/app.rs index 1a3b7908..f12d3997 100644 --- a/actix-web/src/app.rs +++ b/actix-web/src/app.rs @@ -39,7 +39,7 @@ impl App { let factory_ref = Rc::new(RefCell::new(None)); App { - endpoint: AppEntry::new(factory_ref.clone()), + endpoint: AppEntry::new(Rc::clone(&factory_ref)), data_factories: Vec::new(), services: Vec::new(), default: None, @@ -112,8 +112,8 @@ where /// }) /// ``` #[doc(alias = "manage")] - pub fn app_data(mut self, ext: U) -> Self { - self.extensions.insert(ext); + pub fn app_data(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(mut self, factory: F) -> Self where F: HttpServiceFactory + 'static, @@ -270,9 +269,9 @@ where + 'static, U::InitError: fmt::Debug, { - let svc = svc - .into_factory() - .map_init_err(|e| log::error!("Can not construct default service: {:?}", e)); + let svc = svc.into_factory().map_init_err(|err| { + log::error!("Can not construct default service: {err:?}"); + }); self.default = Some(Rc::new(boxed::factory(svc))); diff --git a/actix-web/src/app_service.rs b/actix-web/src/app_service.rs index 65a6ed87..7aa16b79 100644 --- a/actix-web/src/app_service.rs +++ b/actix-web/src/app_service.rs @@ -71,7 +71,7 @@ where }); // create App config to pass to child services - let mut config = AppService::new(config, default.clone()); + let mut config = AppService::new(config, Rc::clone(&default)); // register services mem::take(&mut *self.services.borrow_mut()) diff --git a/actix-web/src/config.rs b/actix-web/src/config.rs index 5e8b056f..0e856f57 100644 --- a/actix-web/src/config.rs +++ b/actix-web/src/config.rs @@ -68,7 +68,7 @@ impl AppService { pub(crate) fn clone_config(&self) -> Self { AppService { config: self.config.clone(), - default: self.default.clone(), + default: Rc::clone(&self.default), services: Vec::new(), root: false, } @@ -81,7 +81,7 @@ impl AppService { /// Returns default handler factory. pub fn default_service(&self) -> Rc { - self.default.clone() + Rc::clone(&self.default) } /// Register HTTP service. diff --git a/actix-web/src/data.rs b/actix-web/src/data.rs index acbb8e23..088df55d 100644 --- a/actix-web/src/data.rs +++ b/actix-web/src/data.rs @@ -184,7 +184,7 @@ impl FromRequest for Data { impl DataFactory for Data { fn create(&self, extensions: &mut Extensions) -> bool { - extensions.insert(Data(self.0.clone())); + extensions.insert(Data(Arc::clone(&self.0))); true } } diff --git a/actix-web/src/error/error.rs b/actix-web/src/error/error.rs index 3a5a128f..670a58a0 100644 --- a/actix-web/src/error/error.rs +++ b/actix-web/src/error/error.rs @@ -60,6 +60,12 @@ impl From for Error { } } +impl From> for Error { + fn from(value: Box) -> Self { + Error { cause: value } + } +} + impl From for Response { fn from(err: Error) -> Response { err.error_response().into() diff --git a/actix-web/src/error/mod.rs b/actix-web/src/error/mod.rs index 91a6bcc3..fce7ca39 100644 --- a/actix-web/src/error/mod.rs +++ b/actix-web/src/error/mod.rs @@ -6,7 +6,7 @@ // // See pub use actix_http::error::{ContentTypeError, DispatchError, HttpError, ParseError, PayloadError}; -use derive_more::{Display, Error, From}; +use derive_more::derive::{Display, Error, From}; use serde_json::error::Error as JsonError; use serde_urlencoded::{de::Error as FormDeError, ser::Error as FormError}; use url::ParseError as UrlParseError; @@ -29,7 +29,7 @@ pub type Result = std::result::Result; /// An error representing a problem running a blocking task on a thread pool. #[derive(Debug, Display, Error)] -#[display(fmt = "Blocking thread pool is shut down unexpectedly")] +#[display("Blocking thread pool is shut down unexpectedly")] #[non_exhaustive] pub struct BlockingError; @@ -40,15 +40,15 @@ impl ResponseError for crate::error::BlockingError {} #[non_exhaustive] pub enum UrlGenerationError { /// Resource not found. - #[display(fmt = "Resource not found")] + #[display("Resource not found")] ResourceNotFound, /// Not all URL parameters covered. - #[display(fmt = "Not all URL parameters covered")] + #[display("Not all URL parameters covered")] NotEnoughElements, /// URL parse error. - #[display(fmt = "{}", _0)] + #[display("{}", _0)] ParseError(UrlParseError), } @@ -59,39 +59,39 @@ impl ResponseError for UrlGenerationError {} #[non_exhaustive] pub enum UrlencodedError { /// Can not decode chunked transfer encoding. - #[display(fmt = "Can not decode chunked transfer encoding.")] + #[display("Can not decode chunked transfer encoding.")] Chunked, /// Payload size is larger than allowed. (default limit: 256kB). #[display( - fmt = "URL encoded payload is larger ({} bytes) than allowed (limit: {} bytes).", + "URL encoded payload is larger ({} bytes) than allowed (limit: {} bytes).", size, limit )] Overflow { size: usize, limit: usize }, /// Payload size is now known. - #[display(fmt = "Payload size is now known.")] + #[display("Payload size is now known.")] UnknownLength, /// Content type error. - #[display(fmt = "Content type error.")] + #[display("Content type error.")] ContentType, /// Parse error. - #[display(fmt = "Parse error: {}.", _0)] + #[display("Parse error: {}.", _0)] Parse(FormDeError), /// Encoding error. - #[display(fmt = "Encoding error.")] + #[display("Encoding error.")] Encoding, /// Serialize error. - #[display(fmt = "Serialize error: {}.", _0)] + #[display("Serialize error: {}.", _0)] Serialize(FormError), /// Payload error. - #[display(fmt = "Error that occur during reading payload: {}.", _0)] + #[display("Error that occur during reading payload: {}.", _0)] Payload(PayloadError), } @@ -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, } @@ -112,30 +113,30 @@ impl ResponseError for UrlencodedError { pub enum JsonPayloadError { /// Payload size is bigger than allowed & content length header set. (default: 2MB) #[display( - fmt = "JSON payload ({} bytes) is larger than allowed (limit: {} bytes).", + "JSON payload ({} bytes) is larger than allowed (limit: {} bytes).", length, limit )] OverflowKnownLength { length: usize, limit: usize }, /// Payload size is bigger than allowed but no content length header set. (default: 2MB) - #[display(fmt = "JSON payload has exceeded limit ({} bytes).", limit)] + #[display("JSON payload has exceeded limit ({} bytes).", limit)] Overflow { limit: usize }, /// Content type error - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, /// Deserialize error - #[display(fmt = "Json deserialize error: {}", _0)] + #[display("Json deserialize error: {}", _0)] Deserialize(JsonError), /// Serialize error - #[display(fmt = "Json serialize error: {}", _0)] + #[display("Json serialize error: {}", _0)] Serialize(JsonError), /// Payload error - #[display(fmt = "Error that occur during reading payload: {}", _0)] + #[display("Error that occur during reading payload: {}", _0)] Payload(PayloadError), } @@ -165,7 +166,7 @@ impl ResponseError for JsonPayloadError { #[non_exhaustive] pub enum PathError { /// Deserialize error - #[display(fmt = "Path deserialize error: {}", _0)] + #[display("Path deserialize error: {}", _0)] Deserialize(serde::de::value::Error), } @@ -181,7 +182,7 @@ impl ResponseError for PathError { #[non_exhaustive] pub enum QueryPayloadError { /// Query deserialize error. - #[display(fmt = "Query deserialize error: {}", _0)] + #[display("Query deserialize error: {}", _0)] Deserialize(serde::de::value::Error), } @@ -195,20 +196,20 @@ impl ResponseError for QueryPayloadError { #[derive(Debug, Display, Error, From)] #[non_exhaustive] pub enum ReadlinesError { - #[display(fmt = "Encoding error")] + #[display("Encoding error")] /// Payload size is bigger than allowed. (default: 256kB) EncodingError, /// Payload error. - #[display(fmt = "Error that occur during reading payload: {}", _0)] + #[display("Error that occur during reading payload: {}", _0)] Payload(PayloadError), /// Line limit exceeded. - #[display(fmt = "Line limit exceeded")] + #[display("Line limit exceeded")] LimitOverflow, /// ContentType error. - #[display(fmt = "Content-type error")] + #[display("Content-type error")] ContentTypeError(ContentTypeError), } @@ -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] diff --git a/actix-web/src/guard/mod.rs b/actix-web/src/guard/mod.rs index 9451a60f..41609953 100644 --- a/actix-web/src/guard/mod.rs +++ b/actix-web/src/guard/mod.rs @@ -110,6 +110,12 @@ impl<'a> GuardContext<'a> { pub fn header(&self) -> Option { H::parse(self.req).ok() } + + /// Counterpart to [HttpRequest::app_data](crate::HttpRequest::app_data). + #[inline] + pub fn app_data(&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::()) == 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())); + } } diff --git a/actix-web/src/handler.rs b/actix-web/src/handler.rs index 6e4e2250..10015cb6 100644 --- a/actix-web/src/handler.rs +++ b/actix-web/src/handler.rs @@ -19,7 +19,7 @@ use crate::{ /// 1. It is an async function (or a function/closure that returns an appropriate future); /// 1. The function parameters (up to 12) implement [`FromRequest`]; /// 1. The async function (or future) resolves to a type that can be converted into an -/// [`HttpResponse`] (i.e., it implements the [`Responder`] trait). +/// [`HttpResponse`] (i.e., it implements the [`Responder`] trait). /// /// /// # Compiler Errors diff --git a/actix-web/src/http/header/content_disposition.rs b/actix-web/src/http/header/content_disposition.rs index 9725cd19..ff3bb676 100644 --- a/actix-web/src/http/header/content_disposition.rs +++ b/actix-web/src/http/header/content_disposition.rs @@ -154,7 +154,7 @@ impl DispositionParam { #[inline] pub fn as_name(&self) -> Option<&str> { match self { - DispositionParam::Name(ref name) => Some(name.as_str()), + DispositionParam::Name(name) => Some(name.as_str()), _ => None, } } @@ -163,7 +163,7 @@ impl DispositionParam { #[inline] pub fn as_filename(&self) -> Option<&str> { match self { - DispositionParam::Filename(ref filename) => Some(filename.as_str()), + DispositionParam::Filename(filename) => Some(filename.as_str()), _ => None, } } @@ -172,7 +172,7 @@ impl DispositionParam { #[inline] pub fn as_filename_ext(&self) -> Option<&ExtendedValue> { match self { - DispositionParam::FilenameExt(ref value) => Some(value), + DispositionParam::FilenameExt(value) => Some(value), _ => None, } } @@ -206,11 +206,11 @@ impl DispositionParam { } } -/// A *Content-Disposition* header. It is compatible to be used either as -/// [a response header for the main body](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body) -/// as (re)defined in [RFC 6266](https://datatracker.ietf.org/doc/html/rfc6266), or as -/// [a header for a multipart body](https://mdn.io/Content-Disposition#As_a_header_for_a_multipart_body) -/// as (re)defined in [RFC 7587](https://datatracker.ietf.org/doc/html/rfc7578). +/// `Content-Disposition` header. +/// +/// It is compatible to be used either as [a response header for the main body][use_main_body] +/// as (re)defined in [RFC 6266], or as [a header for a multipart body][use_multipart] as +/// (re)defined in [RFC 7587]. /// /// In a regular HTTP response, the *Content-Disposition* response header is a header indicating if /// the content is expected to be displayed *inline* in the browser, that is, as a Web page or as @@ -305,6 +305,11 @@ impl DispositionParam { /// change to match local file system conventions if applicable, and do not use directory path /// information that may be present. /// See [RFC 2183 §2.3](https://datatracker.ietf.org/doc/html/rfc2183#section-2.3). +/// +/// [use_main_body]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body +/// [RFC 6266]: https://datatracker.ietf.org/doc/html/rfc6266 +/// [use_multipart]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_header_for_a_multipart_body +/// [RFC 7587]: https://datatracker.ietf.org/doc/html/rfc7578 #[derive(Debug, Clone, PartialEq, Eq)] pub struct ContentDisposition { /// The disposition type @@ -493,7 +498,7 @@ impl Header for ContentDisposition { } fn parse(msg: &T) -> Result { - if let Some(h) = msg.headers().get(&Self::name()) { + if let Some(h) = msg.headers().get(Self::name()) { Self::from_raw(h) } else { Err(crate::error::ParseError::Header) diff --git a/actix-web/src/http/header/content_length.rs b/actix-web/src/http/header/content_length.rs index 557c7c9f..734f4ef3 100644 --- a/actix-web/src/http/header/content_length.rs +++ b/actix-web/src/http/header/content_length.rs @@ -1,6 +1,6 @@ use std::{convert::Infallible, str}; -use derive_more::{Deref, DerefMut}; +use derive_more::derive::{Deref, DerefMut}; use crate::{ error::ParseError, diff --git a/actix-web/src/http/header/range.rs b/actix-web/src/http/header/range.rs index 2326bb19..4a5d95d9 100644 --- a/actix-web/src/http/header/range.rs +++ b/actix-web/src/http/header/range.rs @@ -107,16 +107,16 @@ impl ByteRangeSpec { /// satisfiable if they meet the following conditions: /// /// > If a valid byte-range-set includes at least one byte-range-spec with a first-byte-pos that - /// is less than the current length of the representation, or at least one - /// suffix-byte-range-spec with a non-zero suffix-length, then the byte-range-set - /// is satisfiable. Otherwise, the byte-range-set is unsatisfiable. + /// > is less than the current length of the representation, or at least one + /// > suffix-byte-range-spec with a non-zero suffix-length, then the byte-range-set is + /// > satisfiable. Otherwise, the byte-range-set is unsatisfiable. /// /// The function also computes remainder ranges based on the RFC: /// /// > If the last-byte-pos value is absent, or if the value is greater than or equal to the - /// current length of the representation data, the byte range is interpreted as the remainder - /// of the representation (i.e., the server replaces the value of last-byte-pos with a value - /// that is one less than the current length of the selected representation). + /// > current length of the representation data, the byte range is interpreted as the remainder + /// > of the representation (i.e., the server replaces the value of last-byte-pos with a value + /// > that is one less than the current length of the selected representation). /// /// [RFC 7233 §2.1]: https://datatracker.ietf.org/doc/html/rfc7233 pub fn to_satisfiable_range(&self, full_length: u64) -> Option<(u64, u64)> { @@ -270,7 +270,7 @@ impl Header for Range { #[inline] fn parse(msg: &T) -> Result { - header::from_one_raw_str(msg.headers().get(&Self::name())) + header::from_one_raw_str(msg.headers().get(Self::name())) } } diff --git a/actix-web/src/info.rs b/actix-web/src/info.rs index c5d9638f..0655a3df 100644 --- a/actix-web/src/info.rs +++ b/actix-web/src/info.rs @@ -1,7 +1,7 @@ use std::{convert::Infallible, net::SocketAddr}; use actix_utils::future::{err, ok, Ready}; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use crate::{ dev::{AppConfig, Payload, RequestHead}, @@ -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" => { @@ -221,7 +235,7 @@ impl FromRequest for ConnectionInfo { /// # let _svc = actix_web::web::to(handler); /// ``` #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)] -#[display(fmt = "{}", _0)] +#[display("{}", _0)] pub struct PeerAddr(pub SocketAddr); impl PeerAddr { @@ -233,7 +247,7 @@ impl PeerAddr { #[derive(Debug, Display, Error)] #[non_exhaustive] -#[display(fmt = "Missing peer address")] +#[display("Missing peer address")] pub struct MissingPeerAddr; impl ResponseError for MissingPeerAddr {} @@ -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] diff --git a/actix-web/src/lib.rs b/actix-web/src/lib.rs index f86a7440..e2a8e227 100644 --- a/actix-web/src/lib.rs +++ b/actix-web/src/lib.rs @@ -70,8 +70,6 @@ //! - `rustls-0_23` - HTTPS support via `rustls` 0.23 crate, supports `HTTP/2` //! - `secure-cookies` - secure cookies support -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] @@ -106,6 +104,7 @@ mod scope; mod server; mod service; pub mod test; +mod thin_data; pub(crate) mod types; pub mod web; @@ -145,5 +144,6 @@ codegen_reexport!(delete); codegen_reexport!(trace); codegen_reexport!(connect); codegen_reexport!(options); +codegen_reexport!(scope); pub(crate) type BoxError = Box; diff --git a/actix-web/src/middleware/compat.rs b/actix-web/src/middleware/compat.rs index 7df510a5..963dfdab 100644 --- a/actix-web/src/middleware/compat.rs +++ b/actix-web/src/middleware/compat.rs @@ -38,15 +38,6 @@ pub struct Compat { transform: T, } -#[cfg(test)] -impl Compat { - pub(crate) fn noop() -> Self { - Self { - transform: super::Noop, - } - } -} - impl Compat { /// 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(); diff --git a/actix-web/src/middleware/condition.rs b/actix-web/src/middleware/condition.rs index 55c56d49..5ee4467d 100644 --- a/actix-web/src/middleware/condition.rs +++ b/actix-web/src/middleware/condition.rs @@ -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()); diff --git a/actix-web/src/middleware/default_headers.rs b/actix-web/src/middleware/default_headers.rs index f21afe6e..2669a047 100644 --- a/actix-web/src/middleware/default_headers.rs +++ b/actix-web/src/middleware/default_headers.rs @@ -141,7 +141,7 @@ where actix_service::forward_ready!(service); fn call(&self, req: ServiceRequest) -> Self::Future { - let inner = self.inner.clone(); + let inner = Rc::clone(&self.inner); let fut = self.service.call(req); DefaultHeaderFuture { diff --git a/actix-web/src/middleware/err_handlers.rs b/actix-web/src/middleware/err_handlers.rs index aa6d1c8a..3c50d5c8 100644 --- a/actix-web/src/middleware/err_handlers.rs +++ b/actix-web/src/middleware/err_handlers.rs @@ -220,16 +220,20 @@ impl ErrorHandlers { /// [`.handler()`][ErrorHandlers::handler]) will fall back on this. /// /// Note that this will overwrite any default handlers previously set by calling - /// [`.default_handler_client()`][ErrorHandlers::default_handler_client] or - /// [`.default_handler_server()`][ErrorHandlers::default_handler_server], but not any set by - /// calling [`.handler()`][ErrorHandlers::handler]. + /// [`default_handler_client()`] or [`.default_handler_server()`], but not any set by calling + /// [`.handler()`]. + /// + /// [`default_handler_client()`]: ErrorHandlers::default_handler_client + /// [`.default_handler_server()`]: ErrorHandlers::default_handler_server + /// [`.handler()`]: ErrorHandlers::handler pub fn default_handler(self, handler: F) -> Self where F: Fn(ServiceResponse) -> Result> + 'static, { let handler = Rc::new(handler); + let handler2 = Rc::clone(&handler); Self { - default_server: Some(handler.clone()), + default_server: Some(handler2), default_client: Some(handler), ..self } @@ -288,7 +292,7 @@ where type Future = LocalBoxFuture<'static, Result>; fn new_transform(&self, service: S) -> Self::Future { - let handlers = self.handlers.clone(); + let handlers = Rc::clone(&self.handlers); let default_client = self.default_client.clone(); let default_server = self.default_server.clone(); Box::pin(async move { @@ -323,7 +327,7 @@ where actix_service::forward_ready!(service); fn call(&self, req: ServiceRequest) -> Self::Future { - let handlers = self.handlers.clone(); + let handlers = Rc::clone(&self.handlers); let default_client = self.default_client.clone(); let default_server = self.default_server.clone(); let fut = self.service.call(req); diff --git a/actix-web/src/middleware/from_fn.rs b/actix-web/src/middleware/from_fn.rs new file mode 100644 index 00000000..60883331 --- /dev/null +++ b/actix-web/src/middleware/from_fn.rs @@ -0,0 +1,349 @@ +use std::{future::Future, marker::PhantomData, rc::Rc}; + +use actix_service::boxed::{self, BoxFuture, RcService}; +use actix_utils::future::{ready, Ready}; +use futures_core::future::LocalBoxFuture; + +use crate::{ + body::MessageBody, + dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, + Error, FromRequest, +}; + +/// Wraps an async function to be used as a middleware. +/// +/// # Examples +/// +/// The wrapped function should have the following form: +/// +/// ``` +/// # use actix_web::{ +/// # App, Error, +/// # body::MessageBody, +/// # dev::{ServiceRequest, ServiceResponse, Service as _}, +/// # }; +/// use actix_web::middleware::{self, Next}; +/// +/// async fn my_mw( +/// req: ServiceRequest, +/// next: Next, +/// ) -> Result, Error> { +/// // pre-processing +/// next.call(req).await +/// // post-processing +/// } +/// # App::new().wrap(middleware::from_fn(my_mw)); +/// ``` +/// +/// Then use in an app builder like this: +/// +/// ``` +/// use actix_web::{ +/// App, Error, +/// dev::{ServiceRequest, ServiceResponse, Service as _}, +/// }; +/// use actix_web::middleware::from_fn; +/// # use actix_web::middleware::Next; +/// # async fn my_mw(req: ServiceRequest, next: Next) -> Result, Error> { +/// # next.call(req).await +/// # } +/// +/// App::new() +/// .wrap(from_fn(my_mw)) +/// # ; +/// ``` +/// +/// It is also possible to write a middleware that automatically uses extractors, similar to request +/// handlers, by declaring them as the first parameters. As usual, **take care with extractors that +/// consume the body stream**, since handlers will no longer be able to read it again without +/// putting the body "back" into the request object within your middleware. +/// +/// ``` +/// # use std::collections::HashMap; +/// # use actix_web::{ +/// # App, Error, +/// # body::MessageBody, +/// # dev::{ServiceRequest, ServiceResponse}, +/// # http::header::{Accept, Date}, +/// # web::{Header, Query}, +/// # }; +/// use actix_web::middleware::Next; +/// +/// async fn my_extracting_mw( +/// accept: Header, +/// query: Query>, +/// req: ServiceRequest, +/// next: Next, +/// ) -> Result, Error> { +/// // pre-processing +/// next.call(req).await +/// // post-processing +/// } +/// # App::new().wrap(actix_web::middleware::from_fn(my_extracting_mw)); +pub fn from_fn(mw_fn: F) -> MiddlewareFn { + MiddlewareFn { + mw_fn: Rc::new(mw_fn), + _phantom: PhantomData, + } +} + +/// Middleware transform for [`from_fn`]. +#[allow(missing_debug_implementations)] +pub struct MiddlewareFn { + mw_fn: Rc, + _phantom: PhantomData, +} + +impl Transform for MiddlewareFn +where + S: Service, Error = Error> + 'static, + F: Fn(ServiceRequest, Next) -> Fut + 'static, + Fut: Future, Error>>, + B2: MessageBody, +{ + type Response = ServiceResponse; + type Error = Error; + type Transform = MiddlewareFnService; + type InitError = (); + type Future = Ready>; + + fn new_transform(&self, service: S) -> Self::Future { + ready(Ok(MiddlewareFnService { + service: boxed::rc_service(service), + mw_fn: Rc::clone(&self.mw_fn), + _phantom: PhantomData, + })) + } +} + +/// Middleware service for [`from_fn`]. +#[allow(missing_debug_implementations)] +pub struct MiddlewareFnService { + service: RcService, Error>, + mw_fn: Rc, + _phantom: PhantomData<(B, Es)>, +} + +impl Service for MiddlewareFnService +where + F: Fn(ServiceRequest, Next) -> Fut, + Fut: Future, Error>>, + B2: MessageBody, +{ + type Response = ServiceResponse; + type Error = Error; + type Future = Fut; + + forward_ready!(service); + + fn call(&self, req: ServiceRequest) -> Self::Future { + (self.mw_fn)( + req, + Next:: { + service: Rc::clone(&self.service), + }, + ) + } +} + +macro_rules! impl_middleware_fn_service { + ($($ext_type:ident),*) => { + impl Transform for MiddlewareFn + where + S: Service, Error = Error> + 'static, + F: Fn($($ext_type),*, ServiceRequest, Next) -> Fut + 'static, + $($ext_type: FromRequest + 'static,)* + Fut: Future, Error>> + 'static, + B: MessageBody + 'static, + B2: MessageBody + 'static, + { + type Response = ServiceResponse; + type Error = Error; + type Transform = MiddlewareFnService; + type InitError = (); + type Future = Ready>; + + fn new_transform(&self, service: S) -> Self::Future { + ready(Ok(MiddlewareFnService { + service: boxed::rc_service(service), + mw_fn: Rc::clone(&self.mw_fn), + _phantom: PhantomData, + })) + } + } + + impl Service + for MiddlewareFnService + where + F: Fn( + $($ext_type),*, + ServiceRequest, + Next + ) -> Fut + 'static, + $($ext_type: FromRequest + 'static,)* + Fut: Future, Error>> + 'static, + B2: MessageBody + 'static, + { + type Response = ServiceResponse; + type Error = Error; + type Future = LocalBoxFuture<'static, Result>; + + forward_ready!(service); + + #[allow(nonstandard_style)] + fn call(&self, mut req: ServiceRequest) -> Self::Future { + let mw_fn = Rc::clone(&self.mw_fn); + let service = Rc::clone(&self.service); + + Box::pin(async move { + let ($($ext_type,)*) = req.extract::<($($ext_type,)*)>().await?; + + (mw_fn)($($ext_type),*, req, Next:: { service }).await + }) + } + } + }; +} + +impl_middleware_fn_service!(E1); +impl_middleware_fn_service!(E1, E2); +impl_middleware_fn_service!(E1, E2, E3); +impl_middleware_fn_service!(E1, E2, E3, E4); +impl_middleware_fn_service!(E1, E2, E3, E4, E5); +impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6); +impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6, E7); +impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6, E7, E8); +impl_middleware_fn_service!(E1, E2, E3, E4, E5, E6, E7, E8, E9); + +/// Wraps the "next" service in the middleware chain. +#[allow(missing_debug_implementations)] +pub struct Next { + service: RcService, Error>, +} + +impl Next { + /// Equivalent to `Service::call(self, req)`. + pub fn call(&self, req: ServiceRequest) -> >::Future { + Service::call(self, req) + } +} + +impl Service for Next { + type Response = ServiceResponse; + type Error = Error; + type Future = BoxFuture>; + + forward_ready!(service); + + fn call(&self, req: ServiceRequest) -> Self::Future { + self.service.call(req) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + http::header::{self, HeaderValue}, + middleware::{Compat, Logger}, + test, web, App, HttpResponse, + }; + + async fn noop(req: ServiceRequest, next: Next) -> Result, Error> { + next.call(req).await + } + + async fn add_res_header( + req: ServiceRequest, + next: Next, + ) -> Result, Error> { + let mut res = next.call(req).await?; + res.headers_mut() + .insert(header::WARNING, HeaderValue::from_static("42")); + Ok(res) + } + + async fn mutate_body_type( + req: ServiceRequest, + next: Next, + ) -> Result, Error> { + let res = next.call(req).await?; + Ok(res.map_into_left_body::<()>()) + } + + struct MyMw(bool); + + impl MyMw { + async fn mw_cb( + &self, + req: ServiceRequest, + next: Next, + ) -> Result, Error> { + let mut res = match self.0 { + true => req.into_response("short-circuited").map_into_right_body(), + false => next.call(req).await?.map_into_left_body(), + }; + res.headers_mut() + .insert(header::WARNING, HeaderValue::from_static("42")); + Ok(res) + } + + pub fn into_middleware( + self, + ) -> impl Transform< + S, + ServiceRequest, + Response = ServiceResponse, + Error = Error, + InitError = (), + > + where + S: Service, Error = Error> + 'static, + B: MessageBody + 'static, + { + let this = Rc::new(self); + from_fn(move |req, next| { + let this = Rc::clone(&this); + async move { Self::mw_cb(&this, req, next).await } + }) + } + } + + #[actix_rt::test] + async fn compat_compat() { + let _ = App::new().wrap(Compat::new(from_fn(noop))); + let _ = App::new().wrap(Compat::new(from_fn(mutate_body_type))); + } + + #[actix_rt::test] + async fn permits_different_in_and_out_body_types() { + let app = test::init_service( + App::new() + .wrap(from_fn(mutate_body_type)) + .wrap(from_fn(add_res_header)) + .wrap(Logger::default()) + .wrap(from_fn(noop)) + .default_service(web::to(HttpResponse::NotFound)), + ) + .await; + + let req = test::TestRequest::default().to_request(); + let res = test::call_service(&app, req).await; + assert!(res.headers().contains_key(header::WARNING)); + } + + #[actix_rt::test] + async fn closure_capture_and_return_from_fn() { + let app = test::init_service( + App::new() + .wrap(Logger::default()) + .wrap(MyMw(true).into_middleware()) + .wrap(Logger::default()), + ) + .await; + + let req = test::TestRequest::default().to_request(); + let res = test::call_service(&app, req).await; + assert!(res.headers().contains_key(header::WARNING)); + } +} diff --git a/actix-web/src/middleware/noop.rs b/actix-web/src/middleware/identity.rs similarity index 57% rename from actix-web/src/middleware/noop.rs rename to actix-web/src/middleware/identity.rs index ae7da1d8..de374a57 100644 --- a/actix-web/src/middleware/noop.rs +++ b/actix-web/src/middleware/identity.rs @@ -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, Req> Transform for Noop { +impl, Req> Transform for Identity { type Response = S::Response; type Error = S::Error; - type Transform = NoopService; + type Transform = IdentityMiddleware; type InitError = (); type Future = Ready>; + #[inline] fn new_transform(&self, service: S) -> Self::Future { - ready(Ok(NoopService { service })) + ready(Ok(IdentityMiddleware { service })) } } #[doc(hidden)] -pub(crate) struct NoopService { +pub struct IdentityMiddleware { service: S, } -impl, Req> Service for NoopService { +impl, Req> Service for IdentityMiddleware { 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) } diff --git a/actix-web/src/middleware/logger.rs b/actix-web/src/middleware/logger.rs index dc1b0239..21986baa 100644 --- a/actix-web/src/middleware/logger.rs +++ b/actix-web/src/middleware/logger.rs @@ -276,7 +276,7 @@ where ready(Ok(LoggerMiddleware { service, - inner: self.0.clone(), + inner: Rc::clone(&self.0), })) } } @@ -622,11 +622,7 @@ impl FormatText { FormatText::ResponseHeader(ref name) => { let s = if let Some(val) = res.headers().get(name) { - if let Ok(s) = val.to_str() { - s - } else { - "-" - } + val.to_str().unwrap_or("-") } else { "-" }; @@ -670,11 +666,7 @@ impl FormatText { FormatText::RequestTime => *self = FormatText::Str(now.format(&Rfc3339).unwrap()), FormatText::RequestHeader(ref name) => { let s = if let Some(val) = req.headers().get(name) { - if let Ok(s) = val.to_str() { - s - } else { - "-" - } + val.to_str().unwrap_or("-") } else { "-" }; diff --git a/actix-web/src/middleware/mod.rs b/actix-web/src/middleware/mod.rs index e924de26..4b5b3e89 100644 --- a/actix-web/src/middleware/mod.rs +++ b/actix-web/src/middleware/mod.rs @@ -15,10 +15,47 @@ //! - Access external services (e.g., [sessions](https://docs.rs/actix-session), etc.) //! //! Middleware is registered for each [`App`], [`Scope`](crate::Scope), or -//! [`Resource`](crate::Resource) and executed in opposite order as registration. In general, a -//! middleware is a pair of types that implements the [`Service`] trait and [`Transform`] trait, -//! respectively. The [`new_transform`] and [`call`] methods must return a [`Future`], though it -//! can often be [an immediately-ready one](actix_utils::future::Ready). +//! [`Resource`](crate::Resource) and executed in opposite order as registration. +//! +//! # Simple Middleware +//! +//! In many cases, you can model your middleware as an async function via the [`from_fn()`] helper +//! that provides a natural interface for implementing your desired behaviors. +//! +//! ``` +//! # use actix_web::{ +//! # App, Error, +//! # body::MessageBody, +//! # dev::{ServiceRequest, ServiceResponse, Service as _}, +//! # }; +//! use actix_web::middleware::{self, Next}; +//! +//! async fn my_mw( +//! req: ServiceRequest, +//! next: Next, +//! ) -> Result, Error> { +//! // pre-processing +//! +//! // invoke the wrapped middleware or service +//! let res = next.call(req).await?; +//! +//! // post-processing +//! +//! Ok(res) +//! } +//! +//! App::new() +//! .wrap(middleware::from_fn(my_mw)); +//! ``` +//! +//! ## Complex Middleware +//! +//! In the more general ase, a middleware is a pair of types that implements the [`Service`] trait +//! and [`Transform`] trait, respectively. The [`new_transform`] and [`call`] methods must return a +//! [`Future`], though it can often be [an immediately-ready one](actix_utils::future::Ready). +//! +//! All the built-in middleware use this pattern with pairs of builder (`Transform`) + +//! implementation (`Service`) types. //! //! # Ordering //! @@ -67,7 +104,7 @@ //! Response //! ``` //! The request _first_ gets processed by the middleware specified _last_ - `MiddlewareC`. It passes -//! the request (modified a modified one) to the next middleware - `MiddlewareB` - _or_ directly +//! the request (possibly a modified one) to the next middleware - `MiddlewareB` - _or_ directly //! responds to the request (e.g. when the request was invalid or an error occurred). `MiddlewareB` //! processes the request as well and passes it to `MiddlewareA`, which then passes it to the //! [`Service`]. In the [`Service`], the extractors will run first. They don't pass the request on, @@ -196,18 +233,6 @@ //! # } //! ``` //! -//! # Simpler Middleware -//! -//! In many cases, you _can_ actually use an async function via a helper that will provide a more -//! natural flow for your behavior. -//! -//! The experimental `actix_web_lab` crate provides a [`from_fn`][lab_from_fn] utility which allows -//! an async fn to be wrapped and used in the same way as other middleware. See the -//! [`from_fn`][lab_from_fn] docs for more info and examples of it's use. -//! -//! While [`from_fn`][lab_from_fn] is experimental currently, it's likely this helper will graduate -//! to Actix Web in some form, so feedback is appreciated. -//! //! [`Future`]: std::future::Future //! [`App`]: crate::App //! [`FromRequest`]: crate::FromRequest @@ -215,34 +240,32 @@ //! [`Transform`]: crate::dev::Transform //! [`call`]: crate::dev::Service::call() //! [`new_transform`]: crate::dev::Transform::new_transform() -//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html +//! [`from_fn`]: crate mod compat; +#[cfg(feature = "__compress")] +mod compress; mod condition; mod default_headers; mod err_handlers; +mod from_fn; +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}, + from_fn::{from_fn, Next}, + 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::*; diff --git a/actix-web/src/request.rs b/actix-web/src/request.rs index 08a222c8..47b3e3d8 100644 --- a/actix-web/src/request.rs +++ b/actix-web/src/request.rs @@ -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", + ); + } } diff --git a/actix-web/src/resource.rs b/actix-web/src/resource.rs index 00555b7b..6486fb39 100644 --- a/actix-web/src/resource.rs +++ b/actix-web/src/resource.rs @@ -62,14 +62,14 @@ pub struct Resource { impl Resource { /// Constructs new resource that matches a `path` pattern. pub fn new(path: T) -> Resource { - let fref = Rc::new(RefCell::new(None)); + let factory_ref = Rc::new(RefCell::new(None)); Resource { routes: Vec::new(), rdef: path.patterns(), name: None, - endpoint: ResourceEndpoint::new(fref.clone()), - factory_ref: fref, + endpoint: ResourceEndpoint::new(Rc::clone(&factory_ref)), + factory_ref, guards: Vec::new(), app_data: None, default: boxed::factory(fn_service(|req: ServiceRequest| async { @@ -358,10 +358,9 @@ where U::InitError: fmt::Debug, { // create and configure default resource - self.default = boxed::factory( - f.into_factory() - .map_init_err(|e| log::error!("Can not construct default service: {:?}", e)), - ); + self.default = boxed::factory(f.into_factory().map_init_err(|err| { + log::error!("Can not construct default service: {err:?}"); + })); self } diff --git a/actix-web/src/response/builder.rs b/actix-web/src/response/builder.rs index 023842ee..c23de8e3 100644 --- a/actix-web/src/response/builder.rs +++ b/actix-web/src/response/builder.rs @@ -463,7 +463,7 @@ mod tests { // content type override let res = HttpResponse::Ok() .insert_header((CONTENT_TYPE, "text/json")) - .json(&vec!["v1", "v2", "v3"]); + .json(["v1", "v2", "v3"]); let ct = res.headers().get(CONTENT_TYPE).unwrap(); assert_eq!(ct, HeaderValue::from_static("text/json")); assert_body_eq!(res, br#"["v1","v2","v3"]"#); diff --git a/actix-web/src/response/customize_responder.rs b/actix-web/src/response/customize_responder.rs index 4cbd96e2..6a43ac5e 100644 --- a/actix-web/src/response/customize_responder.rs +++ b/actix-web/src/response/customize_responder.rs @@ -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 { @@ -137,6 +137,29 @@ impl CustomizeResponder { 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 Responder for CustomizeResponder @@ -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::new("name", "value")], + ); + assert_eq!( + to_bytes(res.into_body()).await.unwrap(), + Bytes::from_static(b"test"), + ); } #[actix_rt::test] diff --git a/actix-web/src/route.rs b/actix-web/src/route.rs index 261e6b9a..e05e6be5 100644 --- a/actix-web/src/route.rs +++ b/actix-web/src/route.rs @@ -77,7 +77,7 @@ impl ServiceFactory for Route { fn new_service(&self, _: ()) -> Self::Future { let fut = self.service.new_service(()); - let guards = self.guards.clone(); + let guards = Rc::clone(&self.guards); Box::pin(async move { let service = fut.await?; diff --git a/actix-web/src/scope.rs b/actix-web/src/scope.rs index adc9f75d..e317349d 100644 --- a/actix-web/src/scope.rs +++ b/actix-web/src/scope.rs @@ -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}; @@ -279,7 +278,9 @@ where { // create and configure default resource self.default = Some(Rc::new(boxed::factory(f.into_factory().map_init_err( - |e| log::error!("Can not construct default service: {:?}", e), + |err| { + log::error!("Can not construct default service: {err:?}"); + }, )))); self diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index 33b1e189..1ea4de4c 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -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 } @@ -199,7 +193,7 @@ where /// /// One thread pool is set up **per worker**; not shared across workers. /// - /// By default set to 512 divided by the number of workers. + /// By default, set to 512 divided by [available parallelism](std::thread::available_parallelism()). pub fn worker_max_blocking_threads(mut self, num: usize) -> Self { self.builder = self.builder.worker_max_blocking_threads(num); self @@ -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() @@ -522,7 +510,7 @@ where /// No changes are made to `lst`'s configuration. Ensure it is configured properly before /// passing ownership to `listen()`. pub fn listen(mut self, lst: net::TcpListener) -> io::Result { - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let factory = self.factory.clone(); let addr = lst.local_addr().unwrap(); @@ -566,7 +554,7 @@ where /// Binds to existing listener for accepting incoming plaintext HTTP/1.x or HTTP/2 connections. #[cfg(feature = "http2")] pub fn listen_auto_h2c(mut self, lst: net::TcpListener) -> io::Result { - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let factory = self.factory.clone(); let addr = lst.local_addr().unwrap(); @@ -644,7 +632,7 @@ where config: actix_tls::accept::rustls_0_20::reexports::ServerConfig, ) -> io::Result { let factory = self.factory.clone(); - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let addr = lst.local_addr().unwrap(); self.sockets.push(Socket { addr, @@ -695,7 +683,7 @@ where config: actix_tls::accept::rustls_0_21::reexports::ServerConfig, ) -> io::Result { let factory = self.factory.clone(); - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let addr = lst.local_addr().unwrap(); self.sockets.push(Socket { addr, @@ -761,7 +749,7 @@ where config: actix_tls::accept::rustls_0_22::reexports::ServerConfig, ) -> io::Result { let factory = self.factory.clone(); - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let addr = lst.local_addr().unwrap(); self.sockets.push(Socket { addr, @@ -827,7 +815,7 @@ where config: actix_tls::accept::rustls_0_23::reexports::ServerConfig, ) -> io::Result { let factory = self.factory.clone(); - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let addr = lst.local_addr().unwrap(); self.sockets.push(Socket { addr, @@ -892,7 +880,7 @@ where acceptor: SslAcceptor, ) -> io::Result { let factory = self.factory.clone(); - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let addr = lst.local_addr().unwrap(); self.sockets.push(Socket { addr, @@ -949,7 +937,7 @@ where use actix_rt::net::UnixStream; use actix_service::{fn_service, ServiceFactoryExt as _}; - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let factory = self.factory.clone(); let socket_addr = net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)), 8080); @@ -994,7 +982,7 @@ where use actix_rt::net::UnixStream; use actix_service::{fn_service, ServiceFactoryExt as _}; - let cfg = self.config.clone(); + let cfg = Arc::clone(&self.config); let factory = self.factory.clone(); let socket_addr = net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)), 8080); @@ -1097,7 +1085,10 @@ fn create_tcp_listener(addr: net::SocketAddr, backlog: u32) -> io::Result {()}; ($($x:expr),+ $(,)?) => { ($($x,)+) } @@ -870,4 +871,40 @@ mod tests { let req = test::TestRequest::default().to_request(); let _res = test::call_service(&app, req).await; } + + #[test] + fn define_services_macro_with_multiple_arguments() { + let result = services!(1, 2, 3); + assert_eq!(result, (1, 2, 3)); + } + + #[test] + fn define_services_macro_with_single_argument() { + let result = services!(1); + assert_eq!(result, (1,)); + } + + #[test] + fn define_services_macro_with_no_arguments() { + let result = services!(); + let () = result; + } + + #[test] + fn define_services_macro_with_trailing_comma() { + let result = services!(1, 2, 3,); + assert_eq!(result, (1, 2, 3)); + } + + #[test] + fn define_services_macro_with_comments_in_arguments() { + let result = services!( + 1, // First comment + 2, // Second comment + 3 // Third comment + ); + + // Assert that comments are ignored and it correctly returns a tuple. + assert_eq!(result, (1, 2, 3)); + } } diff --git a/actix-web/src/test/mod.rs b/actix-web/src/test/mod.rs index 5e647956..86cb6095 100644 --- a/actix-web/src/test/mod.rs +++ b/actix-web/src/test/mod.rs @@ -1,6 +1,6 @@ //! Various helpers for Actix applications to use during testing. //! -//! # Creating A Test Service +//! # Initializing A Test Service //! - [`init_service`] //! //! # Off-The-Shelf Test Services @@ -49,6 +49,7 @@ pub use self::{ /// Must be used inside an async test. Works for both `ServiceRequest` and `HttpRequest`. /// /// # Examples +/// /// ``` /// use actix_web::{http::StatusCode, HttpResponse}; /// diff --git a/actix-web/src/thin_data.rs b/actix-web/src/thin_data.rs new file mode 100644 index 00000000..a9cd4e3a --- /dev/null +++ b/actix-web/src/thin_data.rs @@ -0,0 +1,121 @@ +use std::any::type_name; + +use actix_utils::future::{ready, Ready}; + +use crate::{dev::Payload, error, FromRequest, HttpRequest}; + +/// Application data wrapper and extractor for cheaply-cloned types. +/// +/// Similar to the [`Data`] wrapper but for `Clone`/`Copy` types that are already an `Arc` internally, +/// share state using some other means when cloned, or is otherwise static data that is very cheap +/// to clone. +/// +/// Unlike `Data`, this wrapper clones `T` during extraction. Therefore, it is the user's +/// responsibility to ensure that clones of `T` do actually share the same state, otherwise state +/// may be unexpectedly different across multiple requests. +/// +/// Note that if your type is literally an `Arc` then it's recommended to use the +/// [`Data::from(arc)`][data_from_arc] conversion instead. +/// +/// # Examples +/// +/// ``` +/// use actix_web::{ +/// web::{self, ThinData}, +/// App, HttpResponse, Responder, +/// }; +/// +/// // Use the `ThinData` extractor to access a database connection pool. +/// async fn index(ThinData(db_pool): ThinData) -> impl Responder { +/// // database action ... +/// +/// HttpResponse::Ok() +/// } +/// +/// # type DbPool = (); +/// let db_pool = DbPool::default(); +/// +/// App::new() +/// .app_data(ThinData(db_pool.clone())) +/// .service(web::resource("/").get(index)) +/// # ; +/// ``` +/// +/// [`Data`]: crate::web::Data +/// [data_from_arc]: crate::web::Data#impl-From>-for-Data +#[derive(Debug, Clone)] +pub struct ThinData(pub T); + +impl_more::impl_as_ref!(ThinData => T); +impl_more::impl_as_mut!(ThinData => T); +impl_more::impl_deref_and_mut!( in ThinData => T); + +impl FromRequest for ThinData { + type Error = crate::Error; + type Future = Ready>; + + #[inline] + fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { + ready(req.app_data::().cloned().ok_or_else(|| { + log::debug!( + "Failed to extract `ThinData<{}>` for `{}` handler. For the ThinData extractor to work \ + correctly, wrap the data with `ThinData()` and pass it to `App::app_data()`. \ + Ensure that types align in both the set and retrieve calls.", + type_name::(), + req.match_name().unwrap_or(req.path()) + ); + + error::ErrorInternalServerError( + "Requested application data is not configured correctly. \ + View/enable debug logs for more details.", + ) + })) + } +} + +#[cfg(test)] +mod tests { + use std::sync::{Arc, Mutex}; + + use super::*; + use crate::{ + http::StatusCode, + test::{call_service, init_service, TestRequest}, + web, App, HttpResponse, + }; + + type TestT = Arc>; + + #[actix_rt::test] + async fn thin_data() { + let test_data = TestT::default(); + + let app = init_service(App::new().app_data(ThinData(test_data.clone())).service( + web::resource("/").to(|td: ThinData| { + *td.lock().unwrap() += 1; + HttpResponse::Ok() + }), + )) + .await; + + for _ in 0..3 { + let req = TestRequest::default().to_request(); + let resp = call_service(&app, req).await; + assert_eq!(resp.status(), StatusCode::OK); + } + + assert_eq!(*test_data.lock().unwrap(), 3); + } + + #[actix_rt::test] + async fn thin_data_missing() { + let app = init_service( + App::new().service(web::resource("/").to(|_: ThinData| HttpResponse::Ok())), + ) + .await; + + let req = TestRequest::default().to_request(); + let resp = call_service(&app, req).await; + assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); + } +} diff --git a/actix-web/src/types/html.rs b/actix-web/src/types/html.rs new file mode 100644 index 00000000..c370ee07 --- /dev/null +++ b/actix-web/src/types/html.rs @@ -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("

Hello, World!

") +/// # ; +/// ``` +#[derive(Debug, Clone, PartialEq, Hash)] +pub struct Html(String); + +impl Html { + /// Constructs a new `Html` responder. + pub fn new(html: impl Into) -> Self { + Self(html.into()) + } +} + +impl Responder for Html { + type Body = String; + + fn respond_to(self, _req: &HttpRequest) -> HttpResponse { + 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("

Hello, World!

"); + 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("

")); + } +} diff --git a/actix-web/src/types/json.rs b/actix-web/src/types/json.rs index 6b75c0cf..51a322e4 100644 --- a/actix-web/src/types/json.rs +++ b/actix-web/src/types/json.rs @@ -398,7 +398,7 @@ impl JsonBody { _res: PhantomData, } } - JsonBody::Error(e) => JsonBody::Error(e), + JsonBody::Error(err) => JsonBody::Error(err), } } } @@ -434,7 +434,7 @@ impl Future for JsonBody { } } }, - JsonBody::Error(e) => Poll::Ready(Err(e.take().unwrap())), + JsonBody::Error(err) => Poll::Ready(Err(err.take().unwrap())), } } } diff --git a/actix-web/src/types/mod.rs b/actix-web/src/types/mod.rs index 792edd65..cabe53d6 100644 --- a/actix-web/src/types/mod.rs +++ b/actix-web/src/types/mod.rs @@ -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}, diff --git a/actix-web/src/types/path.rs b/actix-web/src/types/path.rs index cc87bb80..ada72a72 100644 --- a/actix-web/src/types/path.rs +++ b/actix-web/src/types/path.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use actix_router::PathDeserializer; use actix_utils::future::{ready, Ready}; -use derive_more::{AsRef, Deref, DerefMut, Display, From}; +use derive_more::derive::{AsRef, Deref, DerefMut, Display, From}; use serde::de; use crate::{ @@ -89,8 +89,8 @@ where ); if let Some(error_handler) = error_handler { - let e = PathError::Deserialize(err); - (error_handler)(e, req) + let err = PathError::Deserialize(err); + (error_handler)(err, req) } else { ErrorNotFound(err) } @@ -152,14 +152,14 @@ impl PathConfig { #[cfg(test)] mod tests { use actix_router::ResourceDef; - use derive_more::Display; + use derive_more::derive::Display; use serde::Deserialize; use super::*; use crate::{error, http, test::TestRequest, HttpResponse}; #[derive(Deserialize, Debug, Display)] - #[display(fmt = "MyStruct({}, {})", key, value)] + #[display("MyStruct({}, {})", key, value)] struct MyStruct { key: String, value: String, diff --git a/actix-web/src/types/query.rs b/actix-web/src/types/query.rs index e71b886f..a8e23f78 100644 --- a/actix-web/src/types/query.rs +++ b/actix-web/src/types/query.rs @@ -2,7 +2,7 @@ use std::{fmt, ops, sync::Arc}; -use actix_utils::future::{err, ok, Ready}; +use actix_utils::future::{ok, ready, Ready}; use serde::de::DeserializeOwned; use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequest}; @@ -118,8 +118,8 @@ impl FromRequest for Query { serde_urlencoded::from_str::(req.query_string()) .map(|val| ok(Query(val))) - .unwrap_or_else(move |e| { - let e = QueryPayloadError::Deserialize(e); + .unwrap_or_else(move |err| { + let err = QueryPayloadError::Deserialize(err); log::debug!( "Failed during Query extractor deserialization. \ @@ -127,13 +127,13 @@ impl FromRequest for Query { req.path() ); - let e = if let Some(error_handler) = error_handler { - (error_handler)(e, req) + let err = if let Some(error_handler) = error_handler { + (error_handler)(err, req) } else { - e.into() + err.into() }; - err(e) + ready(Err(err)) }) } } @@ -187,7 +187,7 @@ impl QueryConfig { #[cfg(test)] mod tests { use actix_http::StatusCode; - use derive_more::Display; + use derive_more::derive::Display; use serde::Deserialize; use super::*; diff --git a/actix-web/src/web.rs b/actix-web/src/web.rs index 20431375..3a4c4673 100644 --- a/actix-web/src/web.rs +++ b/actix-web/src/web.rs @@ -2,6 +2,7 @@ //! //! # Request Extractors //! - [`Data`]: Application data item +//! - [`ThinData`]: Cheap-to-clone application data item //! - [`ReqData`]: Request-local data item //! - [`Path`]: URL path parameters / dynamic segments //! - [`Query`]: URL query parameters @@ -22,7 +23,8 @@ use actix_router::IntoPatterns; pub use bytes::{Buf, BufMut, Bytes, BytesMut}; pub use crate::{ - config::ServiceConfig, data::Data, redirect::Redirect, request_data::ReqData, types::*, + config::ServiceConfig, data::Data, redirect::Redirect, request_data::ReqData, + thin_data::ThinData, types::*, }; use crate::{ error::BlockingError, http::Method, service::WebService, FromRequest, Handler, Resource, diff --git a/actix-web/tests/test_httpserver.rs b/actix-web/tests/test_httpserver.rs index 039c0ffb..5fd7d719 100644 --- a/actix-web/tests/test_httpserver.rs +++ b/actix-web/tests/test_httpserver.rs @@ -1,13 +1,10 @@ #[cfg(feature = "openssl")] extern crate tls_openssl as openssl; -#[cfg(any(unix, feature = "openssl"))] -use { - actix_web::{web, App, HttpResponse, HttpServer}, - std::{sync::mpsc, thread, time::Duration}, -}; +use std::{sync::mpsc, thread, time::Duration}; + +use actix_web::{web, App, HttpResponse, HttpServer}; -#[cfg(unix)] #[actix_rt::test] async fn test_start() { let addr = actix_test::unused_addr(); @@ -53,6 +50,27 @@ async fn test_start() { let response = client.get(host.clone()).send().await.unwrap(); assert!(response.status().is_success()); + // Attempt to start a second server using the same address. + let result = HttpServer::new(|| { + App::new().service( + web::resource("/").route(web::to(|| async { HttpResponse::Ok().body("test") })), + ) + }) + .workers(1) + .backlog(1) + .max_connections(10) + .max_connection_rate(10) + .keep_alive(Duration::from_secs(10)) + .client_request_timeout(Duration::from_secs(5)) + .client_disconnect_timeout(Duration::ZERO) + .server_hostname("localhost") + .system_exit() + .disable_signals() + .bind(format!("{}", addr)); + + // This should fail: the address is in use. + assert!(result.is_err()); + srv.stop(false).await; } diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 54c5e986..8a2a1ec4 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,11 +2,19 @@ ## Unreleased +- Update `brotli` dependency to `7`. +- Prevent panics on connection pool drop when Tokio runtime is shutdown early. +- Minimum supported Rust version (MSRV) is now 1.75. + +## 3.5.1 + +- Fix WebSocket `Host` request header value when using a non-default port. + ## 3.5.0 - Add `rustls-0_23`, `rustls-0_23-webpki-roots`, and `rustls-0_23-native-roots` crate features. - Add `awc::Connector::rustls_0_23()` constructor. -- Fix `rustls-0_22-native-roots` root store lookup +- Fix `rustls-0_22-native-roots` root store lookup. - Update `brotli` dependency to `6`. - Minimum supported Rust version (MSRV) is now 1.72. diff --git a/awc/Cargo.toml b/awc/Cargo.toml index f51b3904..c09f32ac 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "awc" -version = "3.5.0" +version = "3.5.1" authors = ["Nikolay Kim "] description = "Async HTTP and WebSocket client library" keywords = ["actix", "http", "framework", "async", "web"] @@ -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"] @@ -89,10 +106,10 @@ actix-utils = "3" base64 = "0.22" bytes = "1" cfg-if = "1" -derive_more = "0.99.5" +derive_more = { version = "1", features = ["display", "error", "from"] } futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } futures-util = { version = "0.3.17", default-features = false, features = ["alloc", "sink"] } -h2 = "0.3.24" +h2 = "0.3.26" http = "0.2.7" itoa = "1" log =" 0.4" @@ -124,7 +141,7 @@ actix-tls = { version = "3.4", features = ["openssl", "rustls-0_23"] } actix-utils = "3" actix-web = { version = "4", features = ["openssl"] } -brotli = "6" +brotli = "7" const-str = "0.5" env_logger = "0.11" flate2 = "1.0.13" @@ -134,7 +151,10 @@ 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 + +[lints] +workspace = true [[example]] name = "client" diff --git a/awc/README.md b/awc/README.md index 8e7b4281..28d28ed8 100644 --- a/awc/README.md +++ b/awc/README.md @@ -5,9 +5,9 @@ [![crates.io](https://img.shields.io/crates/v/awc?label=latest)](https://crates.io/crates/awc) -[![Documentation](https://docs.rs/awc/badge.svg?version=3.5.0)](https://docs.rs/awc/3.5.0) +[![Documentation](https://docs.rs/awc/badge.svg?version=3.5.1)](https://docs.rs/awc/3.5.1) ![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/awc) -[![Dependency Status](https://deps.rs/crate/awc/3.5.0/status.svg)](https://deps.rs/crate/awc/3.5.0) +[![Dependency Status](https://deps.rs/crate/awc/3.5.1/status.svg)](https://deps.rs/crate/awc/3.5.1) [![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x) diff --git a/awc/examples/client.rs b/awc/examples/client.rs index 16ad330b..41626315 100644 --- a/awc/examples/client.rs +++ b/awc/examples/client.rs @@ -1,6 +1,8 @@ use std::error::Error as StdError; -#[tokio::main] +/// If we want to make requests to addresses starting with `https`, we need to enable the rustls feature of awc +/// `awc = { version = "3.5.0", features = ["rustls"] }` +#[actix_rt::main] async fn main() -> Result<(), Box> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); diff --git a/awc/src/any_body.rs b/awc/src/any_body.rs index 08f5cc25..ef0edfb9 100644 --- a/awc/src/any_body.rs +++ b/awc/src/any_body.rs @@ -163,6 +163,7 @@ mod tests { use super::*; + #[allow(dead_code)] struct PinType(PhantomPinned); impl MessageBody for PinType { diff --git a/awc/src/client/connector.rs b/awc/src/client/connector.rs index 5d0b655a..f3d44307 100644 --- a/awc/src/client/connector.rs +++ b/awc/src/client/connector.rs @@ -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> = RefCell::new(None); + static TRUST_DNS_RESOLVER: RefCell> = const { RefCell::new(None) }; } // get from thread local or construct a new trust-dns resolver. diff --git a/awc/src/client/error.rs b/awc/src/client/error.rs index d351e106..81543fb7 100644 --- a/awc/src/client/error.rs +++ b/awc/src/client/error.rs @@ -3,7 +3,7 @@ use std::{fmt, io}; use actix_http::error::{HttpError, ParseError}; #[cfg(feature = "openssl")] use actix_tls::accept::openssl::reexports::Error as OpensslError; -use derive_more::{Display, From}; +use derive_more::derive::{Display, From}; use crate::BoxError; @@ -12,40 +12,40 @@ use crate::BoxError; #[non_exhaustive] pub enum ConnectError { /// SSL feature is not enabled - #[display(fmt = "SSL is not supported")] + #[display("SSL is not supported")] SslIsNotSupported, /// SSL error #[cfg(feature = "openssl")] - #[display(fmt = "{}", _0)] + #[display("{}", _0)] SslError(OpensslError), /// Failed to resolve the hostname - #[display(fmt = "Failed resolving hostname: {}", _0)] + #[display("Failed resolving hostname: {}", _0)] Resolver(Box), /// No dns records - #[display(fmt = "No DNS records found for the input")] + #[display("No DNS records found for the input")] NoRecords, /// Http2 error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] H2(h2::Error), /// Connecting took too long - #[display(fmt = "Timeout while establishing connection")] + #[display("Timeout while establishing connection")] Timeout, /// Connector has been disconnected - #[display(fmt = "Internal error: connector has been disconnected")] + #[display("Internal error: connector has been disconnected")] Disconnected, /// Unresolved host name - #[display(fmt = "Connector received `Connect` method with unresolved host")] + #[display("Connector received `Connect` method with unresolved host")] Unresolved, /// Connection io error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] Io(io::Error), } @@ -54,11 +54,11 @@ impl std::error::Error for ConnectError {} impl From for ConnectError { fn from(err: actix_tls::connect::ConnectError) -> ConnectError { match err { - actix_tls::connect::ConnectError::Resolver(e) => ConnectError::Resolver(e), + actix_tls::connect::ConnectError::Resolver(err) => ConnectError::Resolver(err), actix_tls::connect::ConnectError::NoRecords => ConnectError::NoRecords, actix_tls::connect::ConnectError::InvalidInput => panic!(), actix_tls::connect::ConnectError::Unresolved => ConnectError::Unresolved, - actix_tls::connect::ConnectError::Io(e) => ConnectError::Io(e), + actix_tls::connect::ConnectError::Io(err) => ConnectError::Io(err), } } } @@ -66,16 +66,16 @@ impl From for ConnectError { #[derive(Debug, Display, From)] #[non_exhaustive] pub enum InvalidUrl { - #[display(fmt = "Missing URL scheme")] + #[display("Missing URL scheme")] MissingScheme, - #[display(fmt = "Unknown URL scheme")] + #[display("Unknown URL scheme")] UnknownScheme, - #[display(fmt = "Missing host name")] + #[display("Missing host name")] MissingHost, - #[display(fmt = "URL parse error: {}", _0)] + #[display("URL parse error: {}", _0)] HttpError(http::Error), } @@ -86,11 +86,11 @@ impl std::error::Error for InvalidUrl {} #[non_exhaustive] pub enum SendRequestError { /// Invalid URL - #[display(fmt = "Invalid URL: {}", _0)] + #[display("Invalid URL: {}", _0)] Url(InvalidUrl), /// Failed to connect to host - #[display(fmt = "Failed to connect to host: {}", _0)] + #[display("Failed to connect to host: {}", _0)] Connect(ConnectError), /// Error sending request @@ -100,26 +100,26 @@ pub enum SendRequestError { Response(ParseError), /// Http error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] Http(HttpError), /// Http2 error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] H2(h2::Error), /// Response took too long - #[display(fmt = "Timeout while waiting for response")] + #[display("Timeout while waiting for response")] Timeout, /// Tunnels are not supported for HTTP/2 connection - #[display(fmt = "Tunnels are not supported for http2 connection")] + #[display("Tunnels are not supported for http2 connection")] TunnelNotSupported, /// Error sending request body Body(BoxError), /// Other errors that can occur after submitting a request. - #[display(fmt = "{:?}: {}", _1, _0)] + #[display("{:?}: {}", _1, _0)] Custom(BoxError, Box), } @@ -130,15 +130,15 @@ impl std::error::Error for SendRequestError {} #[non_exhaustive] pub enum FreezeRequestError { /// Invalid URL - #[display(fmt = "Invalid URL: {}", _0)] + #[display("Invalid URL: {}", _0)] Url(InvalidUrl), /// HTTP error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] Http(HttpError), /// Other errors that can occur after submitting a request. - #[display(fmt = "{:?}: {}", _1, _0)] + #[display("{:?}: {}", _1, _0)] Custom(BoxError, Box), } diff --git a/awc/src/client/pool.rs b/awc/src/client/pool.rs index 2938353f..5d764f72 100644 --- a/awc/src/client/pool.rs +++ b/awc/src/client/pool.rs @@ -31,7 +31,7 @@ use super::{ Connect, }; -#[derive(Hash, Eq, PartialEq, Clone, Debug)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Key { authority: Authority, } @@ -42,8 +42,8 @@ impl From for Key { } } +/// Connections pool to reuse I/O per [`Authority`]. #[doc(hidden)] -/// Connections pool for reuse Io type for certain [`http::uri::Authority`] as key. pub struct ConnectionPool where Io: AsyncWrite + Unpin + 'static, @@ -52,7 +52,7 @@ where inner: ConnectionPoolInner, } -/// wrapper type for check the ref count of Rc. +/// Wrapper type for check the ref count of Rc. pub struct ConnectionPoolInner(Rc>) where Io: AsyncWrite + Unpin + 'static; @@ -63,7 +63,7 @@ where { fn new(config: ConnectorConfig) -> Self { let permits = Arc::new(Semaphore::new(config.limit)); - let available = RefCell::new(HashMap::default()); + let available = RefCell::new(HashMap::new()); Self(Rc::new(ConnectionPoolInnerPriv { config, @@ -72,11 +72,13 @@ where })) } - /// spawn a async for graceful shutdown h1 Io type with a timeout. + /// Spawns a graceful shutdown task for the underlying I/O with a timeout. fn close(&self, conn: ConnectionInnerType) { if let Some(timeout) = self.config.disconnect_timeout { if let ConnectionInnerType::H1(io) = conn { - actix_rt::spawn(CloseConnection::new(io, timeout)); + if tokio::runtime::Handle::try_current().is_ok() { + actix_rt::spawn(CloseConnection::new(io, timeout)); + } } } } @@ -173,12 +175,15 @@ where }; // acquire an owned permit and carry it with connection - let permit = inner.permits.clone().acquire_owned().await.map_err(|_| { - ConnectError::Io(io::Error::new( - io::ErrorKind::Other, - "failed to acquire semaphore on client connection pool", - )) - })?; + let permit = Arc::clone(&inner.permits) + .acquire_owned() + .await + .map_err(|_| { + ConnectError::Io(io::Error::new( + io::ErrorKind::Other, + "failed to acquire semaphore on client connection pool", + )) + })?; let conn = { let mut conn = None; diff --git a/awc/src/error.rs b/awc/src/error.rs index 0104e5fe..59dbc8b6 100644 --- a/awc/src/error.rs +++ b/awc/src/error.rs @@ -7,7 +7,7 @@ pub use actix_http::{ ws::{HandshakeError as WsHandshakeError, ProtocolError as WsProtocolError}, StatusCode, }; -use derive_more::{Display, From}; +use derive_more::derive::{Display, From}; use serde_json::error::Error as JsonError; pub use crate::client::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError}; @@ -18,35 +18,35 @@ pub use crate::client::{ConnectError, FreezeRequestError, InvalidUrl, SendReques #[derive(Debug, Display, From)] pub enum WsClientError { /// Invalid response status - #[display(fmt = "Invalid response status")] + #[display("Invalid response status")] InvalidResponseStatus(StatusCode), /// Invalid upgrade header - #[display(fmt = "Invalid upgrade header")] + #[display("Invalid upgrade header")] InvalidUpgradeHeader, /// Invalid connection header - #[display(fmt = "Invalid connection header")] + #[display("Invalid connection header")] InvalidConnectionHeader(HeaderValue), /// Missing Connection header - #[display(fmt = "Missing Connection header")] + #[display("Missing Connection header")] MissingConnectionHeader, /// Missing Sec-Websocket-Accept header - #[display(fmt = "Missing Sec-Websocket-Accept header")] + #[display("Missing Sec-Websocket-Accept header")] MissingWebSocketAcceptHeader, /// Invalid challenge response - #[display(fmt = "Invalid challenge response")] + #[display("Invalid challenge response")] InvalidChallengeResponse([u8; 28], HeaderValue), /// Protocol error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] Protocol(WsProtocolError), /// Send request error - #[display(fmt = "{}", _0)] + #[display("{}", _0)] SendRequest(SendRequestError), } @@ -68,13 +68,13 @@ impl From for WsClientError { #[derive(Debug, Display, From)] pub enum JsonPayloadError { /// Content type error - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, /// Deserialize error - #[display(fmt = "Json deserialize error: {}", _0)] + #[display("Json deserialize error: {}", _0)] Deserialize(JsonError), /// Payload error - #[display(fmt = "Error that occur during reading payload: {}", _0)] + #[display("Error that occur during reading payload: {}", _0)] Payload(PayloadError), } diff --git a/awc/src/frozen.rs b/awc/src/frozen.rs index 8f324499..86240523 100644 --- a/awc/src/frozen.rs +++ b/awc/src/frozen.rs @@ -49,7 +49,7 @@ impl FrozenClientRequest { where B: MessageBody + 'static, { - RequestSender::Rc(self.head.clone(), None).send_body( + RequestSender::Rc(Rc::clone(&self.head), None).send_body( self.addr, self.response_decompress, self.timeout, @@ -60,7 +60,7 @@ impl FrozenClientRequest { /// Send a json body. pub fn send_json(&self, value: &T) -> SendClientRequest { - RequestSender::Rc(self.head.clone(), None).send_json( + RequestSender::Rc(Rc::clone(&self.head), None).send_json( self.addr, self.response_decompress, self.timeout, @@ -71,7 +71,7 @@ impl FrozenClientRequest { /// Send an urlencoded body. pub fn send_form(&self, value: &T) -> SendClientRequest { - RequestSender::Rc(self.head.clone(), None).send_form( + RequestSender::Rc(Rc::clone(&self.head), None).send_form( self.addr, self.response_decompress, self.timeout, @@ -86,7 +86,7 @@ impl FrozenClientRequest { S: Stream> + 'static, E: Into + 'static, { - RequestSender::Rc(self.head.clone(), None).send_stream( + RequestSender::Rc(Rc::clone(&self.head), None).send_stream( self.addr, self.response_decompress, self.timeout, @@ -97,7 +97,7 @@ impl FrozenClientRequest { /// Send an empty body. pub fn send(&self) -> SendClientRequest { - RequestSender::Rc(self.head.clone(), None).send( + RequestSender::Rc(Rc::clone(&self.head), None).send( self.addr, self.response_decompress, self.timeout, @@ -147,8 +147,8 @@ impl FrozenSendBuilder { /// Complete request construction and send a body. pub fn send_body(self, body: impl MessageBody + 'static) -> SendClientRequest { - if let Some(e) = self.err { - return e.into(); + if let Some(err) = self.err { + return err.into(); } RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_body( @@ -177,8 +177,8 @@ impl FrozenSendBuilder { /// Complete request construction and send an urlencoded body. pub fn send_form(self, value: impl Serialize) -> SendClientRequest { - if let Some(e) = self.err { - return e.into(); + if let Some(err) = self.err { + return err.into(); } RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_form( @@ -196,8 +196,8 @@ impl FrozenSendBuilder { S: Stream> + 'static, E: Into + 'static, { - if let Some(e) = self.err { - return e.into(); + if let Some(err) = self.err { + return err.into(); } RequestSender::Rc(self.req.head, Some(self.extra_headers)).send_stream( @@ -211,8 +211,8 @@ impl FrozenSendBuilder { /// Complete request construction and send an empty body. pub fn send(self) -> SendClientRequest { - if let Some(e) = self.err { - return e.into(); + if let Some(err) = self.err { + return err.into(); } RequestSender::Rc(self.req.head, Some(self.extra_headers)).send( diff --git a/awc/src/lib.rs b/awc/src/lib.rs index 46048099..b582d51e 100644 --- a/awc/src/lib.rs +++ b/awc/src/lib.rs @@ -100,8 +100,6 @@ //! # } //! ``` -#![deny(rust_2018_idioms, nonstandard_style)] -#![warn(future_incompatible)] #![allow(unknown_lints)] // temp: #[allow(non_local_definitions)] #![allow( clippy::type_complexity, diff --git a/awc/src/middleware/redirect.rs b/awc/src/middleware/redirect.rs index 0ea5f174..b2cf9c45 100644 --- a/awc/src/middleware/redirect.rs +++ b/awc/src/middleware/redirect.rs @@ -78,7 +78,7 @@ where RedirectServiceFuture::Tunnel { fut } } ConnectRequest::Client(head, body, addr) => { - let connector = self.connector.clone(); + let connector = Rc::clone(&self.connector); let max_redirect_times = self.max_redirect_times; // backup the uri and method for reuse schema and authority. diff --git a/awc/src/request.rs b/awc/src/request.rs index 28ed8b5f..5f42f67e 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -415,8 +415,8 @@ impl ClientRequest { // allow unused mut when cookies feature is disabled fn prep_for_sending(#[allow(unused_mut)] mut self) -> Result { - if let Some(e) = self.err { - return Err(e.into()); + if let Some(err) = self.err { + return Err(err.into()); } // validate uri diff --git a/awc/src/sender.rs b/awc/src/sender.rs index 8de1033a..b676ebf2 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -17,7 +17,7 @@ use actix_http::{ use actix_http::{encoding::Decoder, header::ContentEncoding, Payload}; use actix_rt::time::{sleep, Sleep}; use bytes::Bytes; -use derive_more::From; +use derive_more::derive::From; use futures_core::Stream; use serde::Serialize; @@ -54,8 +54,8 @@ impl From for FreezeRequestError { impl From for SendRequestError { fn from(err: PrepForSendingError) -> SendRequestError { match err { - PrepForSendingError::Url(e) => SendRequestError::Url(e), - PrepForSendingError::Http(e) => SendRequestError::Http(e), + PrepForSendingError::Url(err) => SendRequestError::Url(err), + PrepForSendingError::Http(err) => SendRequestError::Http(err), PrepForSendingError::Json(err) => { SendRequestError::Custom(Box::new(err), Box::new("json serialization error")) } @@ -156,20 +156,20 @@ impl Future for SendClientRequest { } impl From for SendClientRequest { - fn from(e: SendRequestError) -> Self { - SendClientRequest::Err(Some(e)) + fn from(err: SendRequestError) -> Self { + SendClientRequest::Err(Some(err)) } } impl From for SendClientRequest { - fn from(e: HttpError) -> Self { - SendClientRequest::Err(Some(e.into())) + fn from(err: HttpError) -> Self { + SendClientRequest::Err(Some(err.into())) } } impl From for SendClientRequest { - fn from(e: PrepForSendingError) -> Self { - SendClientRequest::Err(Some(e.into())) + fn from(err: PrepForSendingError) -> Self { + SendClientRequest::Err(Some(err.into())) } } diff --git a/awc/src/ws.rs b/awc/src/ws.rs index c3340206..760331e9 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -253,12 +253,13 @@ impl WebsocketsRequest { pub async fn connect( mut self, ) -> Result<(ClientResponse, Framed), WsClientError> { - if let Some(e) = self.err.take() { - return Err(e.into()); + if let Some(err) = self.err.take() { + return Err(err.into()); } - // validate uri + // validate URI let uri = &self.head.uri; + if uri.host().is_none() { return Err(InvalidUrl::MissingHost.into()); } else if uri.scheme().is_none() { @@ -273,9 +274,12 @@ impl WebsocketsRequest { } if !self.head.headers.contains_key(header::HOST) { + let hostname = uri.host().unwrap(); + let port = uri.port(); + self.head.headers.insert( header::HOST, - HeaderValue::from_str(uri.host().unwrap()).unwrap(), + HeaderValue::from_str(&Host { hostname, port }.to_string()).unwrap(), ); } @@ -434,6 +438,25 @@ impl fmt::Debug for WebsocketsRequest { } } +/// Formatter for host (hostname+port) header values. +struct Host<'a> { + hostname: &'a str, + port: Option>, +} + +impl<'a> fmt::Display for Host<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.hostname)?; + + if let Some(port) = &self.port { + f.write_str(":")?; + f.write_str(port.as_str())?; + } + + Ok(()) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/justfile b/justfile index d92f4bd3..a970b62e 100644 --- a/justfile +++ b/justfile @@ -3,12 +3,14 @@ _list: # Format workspace. fmt: + just --unstable --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] downgrade-for-msrv: + cargo update -p=parse-size --precise=1.0.0 cargo update -p=clap --precise=4.4.18 msrv := ``` @@ -17,20 +19,29 @@ msrv := ``` | sed -E 's/^1\.([0-9]{2})$/1\.\1\.0/' ``` msrv_rustup := "+" + msrv - 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(",")' ``` +all_crate_features := if os() == "linux" { "--all-features" } else { "--features='" + non_linux_all_features_list + "'" } -all_crate_features := if os() == "linux" { - "--all-features" -} else { - "--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. +check toolchain="": && (clippy toolchain) + +# 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 +60,34 @@ 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 + rm -f "$(cargo metadata --format-version=1 | jq -r '.target_directory')/doc/crates.js" + 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) | .targets | map(select(.doc) | .name)] | flatten' + 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 +97,42 @@ 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 }} diff --git a/scripts/bump b/scripts/bump index 6fd879ea..7a57e6ed 100755 --- a/scripts/bump +++ b/scripts/bump @@ -169,3 +169,5 @@ if [ "$GH_RELEASE" = 'y' ] || [ "$GH_RELEASE" = 'Y' ]; then fi echo + +cargo update >/dev/null 2>&1 || true