From 4f7b334d8054041296b94cdcedd162017e2869dc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 10:27:44 +0100 Subject: [PATCH 001/129] build(deps): bump taiki-e/install-action from 2.33.22 to 2.33.26 (#3376) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.33.22 to 2.33.26. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.33.22...v2.33.26) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 8d509d691..dfb0ca56d 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: 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.33.26 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -80,7 +80,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 - name: Install cargo-hack - uses: taiki-e/install-action@v2.33.22 + uses: taiki-e/install-action@v2.33.26 with: tool: cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a81f0e8ed..153ab78f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: 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.33.26 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.33.22 + uses: taiki-e/install-action@v2.33.26 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 943f63223..5eaaae8bf 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -23,7 +23,7 @@ jobs: components: llvm-tools-preview - name: Install cargo-llvm-cov - uses: taiki-e/install-action@v2.33.22 + uses: taiki-e/install-action@v2.33.26 with: tool: cargo-llvm-cov diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 03b25ecda..39392fabb 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -82,7 +82,7 @@ jobs: toolchain: nightly-2024-04-26 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.33.22 + uses: taiki-e/install-action@v2.33.26 with: tool: cargo-public-api From d4bcdf28f24728edadaa584dfc31eabcb8ca1bdf Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 10:30:27 +0100 Subject: [PATCH 002/129] build(deps): bump JamesIves/github-pages-deploy-action from 4.6.0 to 4.6.1 (#3375) build(deps): bump JamesIves/github-pages-deploy-action Bumps [JamesIves/github-pages-deploy-action](https://github.com/jamesives/github-pages-deploy-action) from 4.6.0 to 4.6.1. - [Release notes](https://github.com/jamesives/github-pages-deploy-action/releases) - [Commits](https://github.com/jamesives/github-pages-deploy-action/compare/v4.6.0...v4.6.1) --- updated-dependencies: - dependency-name: JamesIves/github-pages-deploy-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/upload-doc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/upload-doc.yml b/.github/workflows/upload-doc.yml index 6352e44d2..b38a80e5b 100644 --- a/.github/workflows/upload-doc.yml +++ b/.github/workflows/upload-doc.yml @@ -35,7 +35,7 @@ jobs: run: echo '' > target/doc/index.html - name: Deploy to GitHub Pages - uses: JamesIves/github-pages-deploy-action@v4.6.0 + uses: JamesIves/github-pages-deploy-action@v4.6.1 with: folder: target/doc single-commit: true From 1b214bc5f5160f8754f1e3fc77039156d7347793 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 10:30:48 +0100 Subject: [PATCH 003/129] build(deps): bump codecov/codecov-action from 4.3.1 to 4.4.0 (#3374) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.3.1 to 4.4.0. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v4.3.1...v4.4.0) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 5eaaae8bf..ce370b105 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -31,7 +31,7 @@ jobs: run: cargo llvm-cov --workspace --all-features --codecov --output-path codecov.json - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4.3.1 + uses: codecov/codecov-action@v4.4.0 with: files: codecov.json fail_ci_if_error: true From cc06fd6a5e6881885961156fc112e31f1ddcb5f4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 27 May 2024 00:57:59 +0000 Subject: [PATCH 004/129] build(deps): bump codecov/codecov-action from 4.4.0 to 4.4.1 (#3381) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.4.0 to 4.4.1. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v4.4.0...v4.4.1) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index ce370b105..8d9494e3e 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -31,7 +31,7 @@ jobs: run: cargo llvm-cov --workspace --all-features --codecov --output-path codecov.json - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4.4.0 + uses: codecov/codecov-action@v4.4.1 with: files: codecov.json fail_ci_if_error: true From 26efa64278ccb0ec2a0d49f0364c9d7930bb57f5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 27 May 2024 00:58:18 +0000 Subject: [PATCH 005/129] build(deps): bump taiki-e/install-action from 2.33.26 to 2.33.34 (#3380) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.33.26 to 2.33.34. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.33.26...v2.33.34) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index dfb0ca56d..4e4f266b6 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.33.26 + uses: taiki-e/install-action@v2.33.34 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -80,7 +80,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 - name: Install cargo-hack - uses: taiki-e/install-action@v2.33.26 + uses: taiki-e/install-action@v2.33.34 with: tool: cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 153ab78f7..1f7a5d812 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.33.26 + uses: taiki-e/install-action@v2.33.34 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.33.26 + uses: taiki-e/install-action@v2.33.34 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 8d9494e3e..ff1767fb3 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -23,7 +23,7 @@ jobs: components: llvm-tools-preview - name: Install cargo-llvm-cov - uses: taiki-e/install-action@v2.33.26 + uses: taiki-e/install-action@v2.33.34 with: tool: cargo-llvm-cov diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 39392fabb..630f0604c 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -82,7 +82,7 @@ jobs: toolchain: nightly-2024-04-26 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.33.26 + uses: taiki-e/install-action@v2.33.34 with: tool: cargo-public-api From 3ce97effa22e4e9ad4dc719d7791b665abf2c9eb Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 28 May 2024 01:21:23 +0100 Subject: [PATCH 006/129] ci: delete upload doc workflow --- .github/workflows/coverage.yml | 4 ++-- .github/workflows/upload-doc.yml | 41 -------------------------------- 2 files changed, 2 insertions(+), 43 deletions(-) delete mode 100644 .github/workflows/upload-doc.yml diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index ff1767fb3..d3467c698 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -22,10 +22,10 @@ jobs: with: components: llvm-tools-preview - - name: Install cargo-llvm-cov + - name: Install just,cargo-llvm-cov uses: taiki-e/install-action@v2.33.34 with: - tool: cargo-llvm-cov + tool: just,cargo-llvm-cov - name: Generate code coverage run: cargo llvm-cov --workspace --all-features --codecov --output-path codecov.json diff --git a/.github/workflows/upload-doc.yml b/.github/workflows/upload-doc.yml deleted file mode 100644 index b38a80e5b..000000000 --- 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.1 - with: - folder: target/doc - single-commit: true From dd84bcb6095aba3b95939a30b7209390ce90f9db Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 3 Jun 2024 02:24:09 +0100 Subject: [PATCH 007/129] build(deps): bump taiki-e/install-action from 2.33.34 to 2.34.0 (#3386) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.33.34 to 2.34.0. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.33.34...v2.34.0) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 4e4f266b6..4231b4dc2 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.33.34 + uses: taiki-e/install-action@v2.34.0 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -80,7 +80,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 - name: Install cargo-hack - uses: taiki-e/install-action@v2.33.34 + uses: taiki-e/install-action@v2.34.0 with: tool: cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f7a5d812..ed2930bc6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.33.34 + uses: taiki-e/install-action@v2.34.0 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.33.34 + uses: taiki-e/install-action@v2.34.0 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index d3467c698..da892bd7a 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -23,7 +23,7 @@ jobs: components: llvm-tools-preview - name: Install just,cargo-llvm-cov - uses: taiki-e/install-action@v2.33.34 + uses: taiki-e/install-action@v2.34.0 with: tool: just,cargo-llvm-cov diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 630f0604c..629396986 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -82,7 +82,7 @@ jobs: toolchain: nightly-2024-04-26 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.33.34 + uses: taiki-e/install-action@v2.34.0 with: tool: cargo-public-api From 5c18569b7896f0816b72efa172600a898e596281 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 14:17:10 +0100 Subject: [PATCH 008/129] docs: align App:app_data arg name --- actix-web/src/app.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actix-web/src/app.rs b/actix-web/src/app.rs index 1a3b79086..3d86d1f9b 100644 --- a/actix-web/src/app.rs +++ b/actix-web/src/app.rs @@ -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 } From ebd8bb266d07828d3556ee10ee8a4c3d9bd0d98e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 14:31:17 +0100 Subject: [PATCH 009/129] ci: fix cargo-public-api --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 629396986..d7a16ccb4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -79,7 +79,7 @@ jobs: - name: Install Rust uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 with: - toolchain: nightly-2024-04-26 + toolchain: nightly-2024-06-07 - name: Install cargo-public-api uses: taiki-e/install-action@v2.34.0 From 85655f731d9d23f9b472b0a2ace817031c8c5e8d Mon Sep 17 00:00:00 2001 From: Abedi Date: Fri, 7 Jun 2024 17:25:29 +0330 Subject: [PATCH 010/129] From Boxed ResponseError impl added (#3388) * From Boxed ResponseError impl added * docs: update changelog --------- Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 4 ++++ actix-web/src/error/error.rs | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 993c7c596..e0390563c 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Implement `From>` for `Error`. + ## 4.6.0 ### Added diff --git a/actix-web/src/error/error.rs b/actix-web/src/error/error.rs index 3a5a128f6..670a58a00 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() From b2d0196f342f0f90a9e9e32584f0826bebed4891 Mon Sep 17 00:00:00 2001 From: Dylan Anthony <43723790+dbanty@users.noreply.github.com> Date: Fri, 7 Jun 2024 08:08:13 -0600 Subject: [PATCH 011/129] Do not require actix-router default features from actix-web-codegen (#3372) * fix: Do not require actix-router default features from actix-web-codegen * docs: update changelog * test: update trybuild stderr --------- Co-authored-by: Dylan Anthony Co-authored-by: Rob Ede --- actix-web-codegen/CHANGES.md | 1 + actix-web-codegen/Cargo.toml | 2 +- .../tests/trybuild/route-malformed-path-fail.stderr | 5 +---- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index a5acdd21c..875c4021e 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased +- 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 4a45d4fef..31c470694 100644 --- a/actix-web-codegen/Cargo.toml +++ b/actix-web-codegen/Cargo.toml @@ -16,7 +16,7 @@ rust-version.workspace = true proc-macro = true [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"] } 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 93c510109..c1100c784 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 From 8fdf35895478b2ed5f08651e123922c012ae2937 Mon Sep 17 00:00:00 2001 From: Raphael C Date: Fri, 7 Jun 2024 16:31:53 +0200 Subject: [PATCH 012/129] Add app_data method to GuardContext (#3341) * changes: guard * fix(guard): docs link to app_data * docs: fix changelog --------- Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 1 + actix-web/src/guard/mod.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index e0390563c..757fdce68 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ ### Added +- Add `guard::GuardContext::app_data()` method. - Implement `From>` for `Error`. ## 4.6.0 diff --git a/actix-web/src/guard/mod.rs b/actix-web/src/guard/mod.rs index 9451a60f9..41609953a 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())); + } } From 8b4d23a69a0cdf5ac9a32d59ab2b3cbb21b71ffa Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Sat, 8 Jun 2024 00:40:55 +1000 Subject: [PATCH 013/129] Allow disabling redirect following in actix-test (#3356) If you're testing that redirects are being properly generated, then it's useful to not have the client go off on a wild goose chase of its own. Co-authored-by: Rob Ede --- actix-test/CHANGES.md | 3 ++- actix-test/src/lib.rs | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index b55a8305c..940b595c0 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -3,8 +3,9 @@ ## Unreleased - 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/src/lib.rs b/actix-test/src/lib.rs index 1c3d8ff11..433f14571 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -149,6 +149,8 @@ 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 { @@ -460,7 +462,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 { @@ -507,6 +515,7 @@ pub struct TestServerConfig { client_request_timeout: Duration, port: u16, workers: usize, + disable_redirects: bool, } impl Default for TestServerConfig { @@ -524,6 +533,7 @@ impl TestServerConfig { client_request_timeout: Duration::from_secs(5), port: 0, workers: 1, + disable_redirects: false, } } @@ -611,6 +621,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 From 4493aa35d006c8a479db2b1410802461655eba25 Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 7 Jun 2024 09:41:32 -0500 Subject: [PATCH 014/129] actix-http::ws: Remove redundant + 4 byte reservation when masked (#3371) * actix-http::ws: Remove redundant + 4 byte reservation when masked * actix-http: Update CHANGES wrt byte fix * docs: remove changelog entry --------- Co-authored-by: Rob Ede --- actix-http/src/ws/frame.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/actix-http/src/ws/frame.rs b/actix-http/src/ws/frame.rs index c9fb0cde9..35b3f8e66 100644 --- a/actix-http/src/ws/frame.rs +++ b/actix-http/src/ws/frame.rs @@ -178,14 +178,14 @@ impl Parser { }; if payload_len < 126 { - dst.reserve(p_len + 2 + if mask { 4 } else { 0 }); + dst.reserve(p_len + 2); dst.put_slice(&[one, two | payload_len as u8]); } else if payload_len <= 65_535 { - dst.reserve(p_len + 4 + if mask { 4 } else { 0 }); + dst.reserve(p_len + 4); dst.put_slice(&[one, two | 126]); dst.put_u16(payload_len as u16); } else { - dst.reserve(p_len + 10 + if mask { 4 } else { 0 }); + dst.reserve(p_len + 10); dst.put_slice(&[one, two | 127]); dst.put_u64(payload_len as u64); }; From 5221c1b19452cd98fa03882374295b56fa9af327 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 14:51:05 +0100 Subject: [PATCH 015/129] ci: pin msrv lookup job --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed2930bc6..ab611fc0a 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 From b9305ff59db29f6ce20740ef2ad5db3e4b60ffe5 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 16:04:04 +0100 Subject: [PATCH 016/129] chore: fmt --- actix-multipart/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 83947b0c2..35c7f9a1f 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -15,7 +15,6 @@ - ## Example Dependencies: @@ -65,6 +64,7 @@ async fn main() -> std::io::Result<()> { ``` Curl request : + ```bash curl -v --request POST \ --url http://localhost:8080/videos \ @@ -72,7 +72,6 @@ curl -v --request POST \ -F file=@./Cargo.lock ``` - ### Examples -https://github.com/actix/examples/tree/master/forms/multipart \ No newline at end of file +https://github.com/actix/examples/tree/master/forms/multipart From cff958e5187b4980d8a87c2a886a284ccbd3d9b2 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 16:10:25 +0100 Subject: [PATCH 017/129] chore: address clippy lint --- actix-test/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/actix-test/src/lib.rs b/actix-test/src/lib.rs index 433f14571..48d5079a7 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -488,6 +488,7 @@ enum HttpVer { Both, } +#[allow(clippy::large_enum_variant)] #[derive(Clone)] enum StreamType { Tcp, From 534cfe1fda1d65b9cea7d75a0a8c55f46439bb4e Mon Sep 17 00:00:00 2001 From: Sebastian Detert Date: Fri, 7 Jun 2024 17:22:48 +0200 Subject: [PATCH 018/129] feat: add .customize().add_cookie() (#3215) * feat: add .customize().add_cookie() * docs: added cookie hint * fix: added unwrap to test of add_cookie() * docs: added changelog entry for .customize().add_cookie() * chore: make append_header infallible * docs: update changelog --------- Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 1 + actix-web/src/response/customize_responder.rs | 42 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 757fdce68..75f3631c9 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ ### Added +- Add `CustomizeResponder::add_cookie()` method. - Add `guard::GuardContext::app_data()` method. - Implement `From>` for `Error`. diff --git a/actix-web/src/response/customize_responder.rs b/actix-web/src/response/customize_responder.rs index 4cbd96e20..6a43ac5e6 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] From c366649516dcd1d780ca8b7938397dac61338c15 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 7 Jun 2024 16:57:03 +0100 Subject: [PATCH 019/129] docs: example of CPU core pinning --- actix-web/Cargo.toml | 2 +- actix-web/examples/worker-cpu-pin.rs | 41 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 actix-web/examples/worker-cpu-pin.rs diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 9f3ab6e5e..a666f14be 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -35,7 +35,6 @@ features = [ "secure-cookies", ] - [lib] name = "actix_web" path = "src/lib.rs" @@ -130,6 +129,7 @@ awc = { version = "3", features = ["openssl"] } brotli = "6" const-str = "0.5" +core_affinity = "0.8" criterion = { version = "0.5", features = ["html_reports"] } env_logger = "0.11" flate2 = "1.0.13" diff --git a/actix-web/examples/worker-cpu-pin.rs b/actix-web/examples/worker-cpu-pin.rs new file mode 100644 index 000000000..58e060821 --- /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 +} From 3db7891303093d5992cfd3483405b80b495b8f8e Mon Sep 17 00:00:00 2001 From: Jonathan Lim Date: Fri, 7 Jun 2024 15:10:48 -0700 Subject: [PATCH 020/129] Scope macro (#3136) * add scope proc macro * Update scope macro code to work with current HttpServiceFactory * started some test code * add some unit tests * code formatting cleanup * add another test for combining and calling 2 scopes * format code with formatter * Update actix-web-codegen/src/lib.rs with comment documentation fix Co-authored-by: oliver <151407407+kwfn@users.noreply.github.com> * work in progress. revised procedural macro to change othe macro call * add tests again. refactor nested code. * clean up code. fix bugs with route and method attributes with parameters * clean up for rust fmt * clean up for rust fmt * fix out of date comment for scope macro * sync to master branch by adding test_wrap * needed to format code * test: split out scope tests * test: add negative tests * chore: move imports back inside (?) * docs: tweak scope docs * fix: prevent trailing slashes in scope prefixes * chore: address clippy lints --------- Co-authored-by: oliver <151407407+kwfn@users.noreply.github.com> Co-authored-by: Rob Ede --- actix-web-codegen/CHANGES.md | 1 + actix-web-codegen/src/lib.rs | 50 +++++ actix-web-codegen/src/route.rs | 20 +- actix-web-codegen/src/scope.rs | 103 +++++++++ .../tests/{test_macro.rs => routes.rs} | 0 actix-web-codegen/tests/scopes.rs | 200 ++++++++++++++++++ actix-web-codegen/tests/trybuild.rs | 5 + .../tests/trybuild/scope-invalid-args.rs | 14 ++ .../tests/trybuild/scope-invalid-args.stderr | 17 ++ .../tests/trybuild/scope-missing-args.rs | 6 + .../tests/trybuild/scope-missing-args.stderr | 7 + .../tests/trybuild/scope-on-handler.rs | 8 + .../tests/trybuild/scope-on-handler.stderr | 5 + .../tests/trybuild/scope-trailing-slash.rs | 6 + .../trybuild/scope-trailing-slash.stderr | 5 + actix-web/src/lib.rs | 1 + awc/src/client/connector.rs | 2 +- justfile | 6 +- 18 files changed, 439 insertions(+), 17 deletions(-) create mode 100644 actix-web-codegen/src/scope.rs rename actix-web-codegen/tests/{test_macro.rs => routes.rs} (100%) create mode 100644 actix-web-codegen/tests/scopes.rs create mode 100644 actix-web-codegen/tests/trybuild/scope-invalid-args.rs create mode 100644 actix-web-codegen/tests/trybuild/scope-invalid-args.stderr create mode 100644 actix-web-codegen/tests/trybuild/scope-missing-args.rs create mode 100644 actix-web-codegen/tests/trybuild/scope-missing-args.stderr create mode 100644 actix-web-codegen/tests/trybuild/scope-on-handler.rs create mode 100644 actix-web-codegen/tests/trybuild/scope-on-handler.stderr create mode 100644 actix-web-codegen/tests/trybuild/scope-trailing-slash.rs create mode 100644 actix-web-codegen/tests/trybuild/scope-trailing-slash.stderr diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index 875c4021e..792f6aa4f 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased +- Add `#[scope]` macro. - Prevent inclusion of default `actix-router` features. - Minimum supported Rust version (MSRV) is now 1.72. diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 6d6c9ab5c..c518007a0 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -83,6 +83,7 @@ use proc_macro::TokenStream; use quote::quote; mod route; +mod scope; /// Creates resource handler, allowing multiple HTTP method guards. /// @@ -197,6 +198,43 @@ method_macro!(Options, options); method_macro!(Trace, trace); method_macro!(Patch, patch); +/// Prepends a path prefix to all handlers using routing macros inside the attached module. +/// +/// # Syntax +/// +/// ``` +/// # use actix_web_codegen::scope; +/// #[scope("/prefix")] +/// mod api { +/// // ... +/// } +/// ``` +/// +/// # Arguments +/// +/// - `"/prefix"` - Raw literal string to be prefixed onto contained handlers' paths. +/// +/// # Example +/// +/// ``` +/// # use actix_web_codegen::{scope, get}; +/// # use actix_web::Responder; +/// #[scope("/api")] +/// mod api { +/// # use super::*; +/// #[get("/hello")] +/// pub async fn hello() -> impl Responder { +/// // this has path /api/hello +/// "Hello, world!" +/// } +/// } +/// # fn main() {} +/// ``` +#[proc_macro_attribute] +pub fn scope(args: TokenStream, input: TokenStream) -> TokenStream { + scope::with_scope(args, input) +} + /// Marks async main function as the Actix Web system entry-point. /// /// Note that Actix Web also works under `#[tokio::main]` since version 4.0. However, this macro is @@ -240,3 +278,15 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream { output.extend(item); output } + +/// Converts the error to a token stream and appends it to the original input. +/// +/// Returning the original input in addition to the error is good for IDEs which can gracefully +/// recover and show more precise errors within the macro body. +/// +/// See 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 7a2dfc051..d0605fc04 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(()), @@ -542,15 +544,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 000000000..067d95a60 --- /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 100% rename from actix-web-codegen/tests/test_macro.rs rename to actix-web-codegen/tests/routes.rs diff --git a/actix-web-codegen/tests/scopes.rs b/actix-web-codegen/tests/scopes.rs new file mode 100644 index 000000000..6a370a2e2 --- /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")] + 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 88f77548b..91073cf3b 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/scope-invalid-args.rs b/actix-web-codegen/tests/trybuild/scope-invalid-args.rs new file mode 100644 index 000000000..ec021d5eb --- /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 000000000..0ab335966 --- /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 000000000..39bcb9d1a --- /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 000000000..d59842e39 --- /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 000000000..e5d478981 --- /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 000000000..4491f42dd --- /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 000000000..84632b59f --- /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 000000000..66933432e --- /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/src/lib.rs b/actix-web/src/lib.rs index f86a74406..205391388 100644 --- a/actix-web/src/lib.rs +++ b/actix-web/src/lib.rs @@ -145,5 +145,6 @@ codegen_reexport!(delete); codegen_reexport!(trace); codegen_reexport!(connect); codegen_reexport!(options); +codegen_reexport!(scope); pub(crate) type BoxError = Box; diff --git a/awc/src/client/connector.rs b/awc/src/client/connector.rs index 5d0b655a4..f3d443070 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/justfile b/justfile index d92f4bd3d..4e106ef11 100644 --- a/justfile +++ b/justfile @@ -4,7 +4,7 @@ _list: # Format workspace. fmt: cargo +nightly fmt - npx -y prettier --write $(fd --type=file --hidden --extension=md --extension=yml) + fd --hidden --type=file --extension=md --extension=yml --exec-batch npx -y prettier --write # Downgrade dev-dependencies necessary to run MSRV checks/tests. [private] @@ -32,6 +32,10 @@ all_crate_features := if os() == "linux" { "--features='" + non_linux_all_features_list + "'" } +# 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) From 7c4c26d2df5d344ae524a72f733fd3af0c13a3a2 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 8 Jun 2024 05:26:26 +0100 Subject: [PATCH 021/129] feat: expose Identity middleware (#3390) --- .github/workflows/lint.yml | 2 +- actix-web/CHANGES.md | 1 + actix-web/src/middleware/compat.rs | 13 ++---------- actix-web/src/middleware/condition.rs | 4 ++-- .../src/middleware/{noop.rs => identity.rs} | 20 +++++++++++-------- actix-web/src/middleware/mod.rs | 16 ++++++--------- 6 files changed, 24 insertions(+), 32 deletions(-) rename actix-web/src/middleware/{noop.rs => identity.rs} (57%) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d7a16ccb4..dd44d4cb3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -89,5 +89,5 @@ jobs: - 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/actix-web/CHANGES.md b/actix-web/CHANGES.md index 75f3631c9..93fbbf465 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ ### Added +- Add `middleware::Identity` type. - Add `CustomizeResponder::add_cookie()` method. - Add `guard::GuardContext::app_data()` method. - Implement `From>` for `Error`. diff --git a/actix-web/src/middleware/compat.rs b/actix-web/src/middleware/compat.rs index 7df510a5c..963dfdabb 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 55c56d494..5ee4467d9 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/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 ae7da1d81..de374a57b 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/mod.rs b/actix-web/src/middleware/mod.rs index e924de261..1c27b1110 100644 --- a/actix-web/src/middleware/mod.rs +++ b/actix-web/src/middleware/mod.rs @@ -218,31 +218,27 @@ //! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html mod compat; +#[cfg(feature = "__compress")] +mod compress; mod condition; mod default_headers; mod err_handlers; +mod identity; mod logger; -#[cfg(test)] -mod noop; mod normalize; -#[cfg(test)] -pub(crate) use self::noop::Noop; +#[cfg(feature = "__compress")] +pub use self::compress::Compress; pub use self::{ compat::Compat, condition::Condition, default_headers::DefaultHeaders, err_handlers::{ErrorHandlerResponse, ErrorHandlers}, + identity::Identity, logger::Logger, normalize::{NormalizePath, TrailingSlash}, }; -#[cfg(feature = "__compress")] -mod compress; - -#[cfg(feature = "__compress")] -pub use self::compress::Compress; - #[cfg(test)] mod tests { use super::*; From ebc43dcf1b52ddf083090402b28392469ffa1997 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 00:10:15 +0100 Subject: [PATCH 022/129] feat: forwards-compatibility for handler visibility inheritance fix (#3391) --- actix-web-codegen/CHANGES.md | 1 + actix-web-codegen/Cargo.toml | 4 ++++ actix-web-codegen/src/route.rs | 9 ++++++++- actix-web-codegen/tests/scopes.rs | 2 +- actix-web/CHANGES.md | 2 ++ actix-web/Cargo.toml | 26 ++++++++++++++++++++++---- awc/Cargo.toml | 2 +- 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index 792f6aa4f..0a240680e 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -3,6 +3,7 @@ ## Unreleased - 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. diff --git a/actix-web-codegen/Cargo.toml b/actix-web-codegen/Cargo.toml index 31c470694..7eb995039 100644 --- a/actix-web-codegen/Cargo.toml +++ b/actix-web-codegen/Cargo.toml @@ -15,6 +15,10 @@ rust-version.workspace = true [lib] proc-macro = true +[features] +default = ["compat-routing-macros-force-pub"] +compat-routing-macros-force-pub = [] + [dependencies] actix-router = { version = "0.5", default-features = false } proc-macro2 = "1" diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index d0605fc04..e24903e3a 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -413,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| { @@ -460,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) { diff --git a/actix-web-codegen/tests/scopes.rs b/actix-web-codegen/tests/scopes.rs index 6a370a2e2..4ee6db16f 100644 --- a/actix-web-codegen/tests/scopes.rs +++ b/actix-web-codegen/tests/scopes.rs @@ -49,7 +49,7 @@ mod scope_module { #[connect("/test2")] #[options("/test3")] #[trace("/test4")] - async fn multiple_separate_paths() -> impl Responder { + pub async fn multiple_separate_paths() -> impl Responder { HttpResponse::Ok().finish() } diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 93fbbf465..97bdf29c2 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -7,6 +7,8 @@ - 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 diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index a666f14be..c36f317ce 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -40,7 +40,16 @@ name = "actix_web" path = "src/lib.rs" [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"] @@ -50,14 +59,15 @@ 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 @@ -84,6 +94,14 @@ __compress = [] # io-uring feature only available for Linux OSes. experimental-io-uring = ["actix-server/io-uring"] +# Feature group which, when disabled, helps migrate code to v5.0. +compat = [ + "compat-routing-macros-force-pub", +] + +# Opt-out forwards-compatibility for handler visibility inheritance fix. +compat-routing-macros-force-pub = ["actix-web-codegen?/compat-routing-macros-force-pub"] + [dependencies] actix-codec = "0.5" actix-macros = { version = "0.2.3", optional = true } @@ -95,7 +113,7 @@ 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.2", optional = true, default-features = false } ahash = "0.8" bytes = "1" diff --git a/awc/Cargo.toml b/awc/Cargo.toml index f51b3904b..09f580aff 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -64,7 +64,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"] From d6f885127d5d72096b9129085508b9af29c5cfee Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 00:16:36 +0100 Subject: [PATCH 023/129] chore(actix-test): prepare release 0.1.4 --- actix-test/CHANGES.md | 2 ++ actix-test/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index 940b595c0..8088c2504 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 0.1.4 + - Add `TestServerConfig::rustls_0_23()` method for Rustls v0.23 support behind new `rustls-0_23` crate feature. - Add `TestServerConfig::disable_redirects()` method. - Various types from `awc`, such as `ClientRequest` and `ClientResponse`, are now re-exported. diff --git a/actix-test/Cargo.toml b/actix-test/Cargo.toml index dddcabec9..1ae619145 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.4" authors = [ "Nikolay Kim ", "Rob Ede ", From b4faf8820cb14a3648bb03385e0ceca844de8d9d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 00:19:09 +0100 Subject: [PATCH 024/129] chore(actix-web-codegen): prepare release 4.3.0 --- actix-web-codegen/CHANGES.md | 2 ++ actix-web-codegen/Cargo.toml | 2 +- actix-web-codegen/README.md | 4 ++-- actix-web/Cargo.toml | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index 0a240680e..d143723f4 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -2,6 +2,8 @@ ## 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. diff --git a/actix-web-codegen/Cargo.toml b/actix-web-codegen/Cargo.toml index 7eb995039..7500807d2 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 ", diff --git a/actix-web-codegen/README.md b/actix-web-codegen/README.md index 9229f8f16..e61bf5c74 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/Cargo.toml b/actix-web/Cargo.toml index c36f317ce..243b91d29 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -113,7 +113,7 @@ 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, default-features = false } +actix-web-codegen = { version = "4.3", optional = true, default-features = false } ahash = "0.8" bytes = "1" From 12a0521ef88d13120363edeca484e75c98af8934 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 00:20:36 +0100 Subject: [PATCH 025/129] chore(actix-multipart): prepare release 0.6.2 --- actix-multipart/CHANGES.md | 2 ++ actix-multipart/Cargo.toml | 2 +- actix-multipart/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/actix-multipart/CHANGES.md b/actix-multipart/CHANGES.md index 196d2ca93..a91edf9c8 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 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 6e36c3391..f1289d3a2 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-multipart" -version = "0.6.1" +version = "0.6.2" authors = [ "Nikolay Kim ", "Jacob Halsey ", diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 35c7f9a1f..c7697785a 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -5,11 +5,11 @@ [![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.6.2)](https://docs.rs/actix-multipart/0.6.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.6.2/status.svg)](https://deps.rs/crate/actix-multipart/0.6.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) From a5c78483f9d83d3f916d75c8a5ce46909faf32bc Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 00:22:03 +0100 Subject: [PATCH 026/129] chore(actix-web): prepare release 4.7.0 --- actix-web/CHANGES.md | 2 ++ actix-web/Cargo.toml | 2 +- actix-web/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 97bdf29c2..27259dc5c 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 4.7.0 + ### Added - Add `middleware::Identity` type. diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 243b91d29..10a507680 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.7.0" description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust" authors = [ "Nikolay Kim ", diff --git a/actix-web/README.md b/actix-web/README.md index 4e7e785a5..8b4375bdd 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.7.0)](https://docs.rs/actix-web/4.7.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.7.0/status.svg)](https://deps.rs/crate/actix-web/4.7.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) From 40e1034566febd583ea7669e4fa76908e9bfe3ad Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 00:38:49 +0100 Subject: [PATCH 027/129] docs: update changelog --- actix-web/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 27259dc5c..4e74e0902 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -6,6 +6,7 @@ ### Added +- Add `#[scope]` macro. - Add `middleware::Identity` type. - Add `CustomizeResponder::add_cookie()` method. - Add `guard::GuardContext::app_data()` method. From 266834cf7c0e7b243b671b0b2ac41a20f8100286 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 04:51:53 +0100 Subject: [PATCH 028/129] chore: narrow h2 version --- actix-http/Cargo.toml | 2 +- awc/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index a999e73c8..87e2b391d 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -106,7 +106,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 } diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 09f580aff..6ab408ea6 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -92,7 +92,7 @@ cfg-if = "1" derive_more = "0.99.5" futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } futures-util = { version = "0.3.17", default-features = false, features = ["alloc", "sink"] } -h2 = "0.3.24" +h2 = "0.3.26" http = "0.2.7" itoa = "1" log =" 0.4" From 8018983a68742570d86bd8f5a0ba0b8ed389e75d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 06:08:21 +0100 Subject: [PATCH 029/129] docs: update changelog for #3393 --- actix-http/CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 61eeb4beb..85ba03100 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Add `error::InvalidStatusCode` re-export. + ## 3.7.0 ### Added From f7646bcc485f43ff4ba987cd613891883a081a07 Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 9 Jun 2024 00:04:42 -0500 Subject: [PATCH 030/129] actix-web-actors: take the internal buffer when yielding (#3369) * actix-web-actors: take the internal buffer when yielding * actix-web-actors: Add CHANGES entry re: taking buffer --- actix-web-actors/CHANGES.md | 1 + actix-web-actors/src/ws.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/actix-web-actors/CHANGES.md b/actix-web-actors/CHANGES.md index 9a622d8da..3e854c0b8 100644 --- a/actix-web-actors/CHANGES.md +++ b/actix-web-actors/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased +- Take the encoded buffer when yielding bytes in the response stream rather than splitting the buffer, reducing memory use - Minimum supported Rust version (MSRV) is now 1.72. ## 4.3.0 diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index 1fb903225..7f7607fa9 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 { From 22593a1532be29374100de0d256b493f2557bcd3 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Sun, 9 Jun 2024 01:07:56 -0400 Subject: [PATCH 031/129] Re-export `http::status::InvalidStatusCode` (#3393) * [actix-http/src/lib.rs] Expose/re-export `http::status::InvalidStatusCode` * [actix-http/src/error.rs] Re-export `http::status::InvalidStatusCode` ; [actix-http/src/lib.rs] Revert --- actix-http/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-http/src/error.rs b/actix-http/src/error.rs index 69e2f14a1..6f332118e 100644 --- a/actix-http/src/error.rs +++ b/actix-http/src/error.rs @@ -3,7 +3,7 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Error}; use derive_more::{Display, Error, From}; -pub use http::Error as HttpError; +pub use http::{status::InvalidStatusCode, Error as HttpError}; use http::{uri::InvalidUri, StatusCode}; use crate::{body::BoxBody, Response}; From 8b8eb4eae1212796216a53aa9ca445108206d7fe Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 9 Jun 2024 18:37:42 +0100 Subject: [PATCH 032/129] build(deps): update tokio-uring requirement from 0.4 to 0.5 (#3385) * build(deps): update tokio-uring requirement from 0.4 to 0.5 Updates the requirements on [tokio-uring](https://github.com/tokio-rs/tokio-uring) to permit the latest version. - [Release notes](https://github.com/tokio-rs/tokio-uring/releases) - [Changelog](https://github.com/tokio-rs/tokio-uring/blob/master/CHANGELOG.md) - [Commits](https://github.com/tokio-rs/tokio-uring/compare/v0.4.0...v0.5.0) --- updated-dependencies: - dependency-name: tokio-uring dependency-type: direct:production ... Signed-off-by: dependabot[bot] * chore: narrow actix-server requirement --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Rob Ede --- actix-files/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index 3d82f8a76..a69af3020 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -40,8 +40,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" From 37577dcb89c01834820e95d2361eed32732447a5 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Jun 2024 19:45:14 +0100 Subject: [PATCH 033/129] chore(actix-files): prepare release 0.6.6 --- actix-files/CHANGES.md | 3 +++ actix-files/Cargo.toml | 2 +- actix-files/README.md | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index 393e7b61a..e94f43907 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -2,6 +2,9 @@ ## Unreleased +## 0.6.6 + +- Update `tokio-uring` dependency to `0.4`. - Minimum supported Rust version (MSRV) is now 1.72. ## 0.6.5 diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index a69af3020..7adb8eaf5 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 ", diff --git a/actix-files/README.md b/actix-files/README.md index a6b3f63c6..f6d5143f5 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) From 758ae1dac1c5cc574698f058b30bd075bac8b681 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 10 Jun 2024 05:07:08 +1000 Subject: [PATCH 034/129] actix-test: allow the configuration of the TestServer address (#3351) * actix-test: allow the configuration of the TestServer address This is useful if you're running (say) Selenium tests against a running TestServer, and the Selenium workers are Docker containers elsewhere in the network. Not a *particularly* common use case, perhaps, but one that I can attest happens every now and then. * Update CHANGES.md * Adjust default listen address to avoid test failures --------- Co-authored-by: Rob Ede --- actix-test/CHANGES.md | 2 ++ actix-test/src/lib.rs | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index 8088c2504..dd409c917 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- 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. diff --git a/actix-test/src/lib.rs b/actix-test/src/lib.rs index 48d5079a7..803320607 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -154,7 +154,7 @@ where // 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(); @@ -514,6 +514,7 @@ pub struct TestServerConfig { tp: HttpVer, stream: StreamType, client_request_timeout: Duration, + listen_address: String, port: u16, workers: usize, disable_redirects: bool, @@ -532,6 +533,7 @@ 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, @@ -607,6 +609,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. @@ -657,9 +667,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) } } From da56de45564640dba4461d5c5f8b5d1f23f25533 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 00:01:17 +0100 Subject: [PATCH 035/129] chore(actix-test): prepare release 0.1.5 --- actix-test/CHANGES.md | 2 ++ actix-test/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/actix-test/CHANGES.md b/actix-test/CHANGES.md index dd409c917..ec2dd6776 100644 --- a/actix-test/CHANGES.md +++ b/actix-test/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 0.1.5 + - Add `TestServerConfig::listen_address()` method. ## 0.1.4 diff --git a/actix-test/Cargo.toml b/actix-test/Cargo.toml index 1ae619145..41267c969 100644 --- a/actix-test/Cargo.toml +++ b/actix-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-test" -version = "0.1.4" +version = "0.1.5" authors = [ "Nikolay Kim ", "Rob Ede ", From a2b9823d9d428b2abec75e98afb8dfd412098886 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 10 Jun 2024 09:40:09 +1000 Subject: [PATCH 036/129] Strip non-address characters from Forwarded for= (#3343) * Strip non-address characters from Forwarded for= This is something of a followup to #2528, which asked for port information to not be included in when it was taken from the local socket. The header's element may optionally contain port information (https://datatracker.ietf.org/doc/html/rfc7239#section-6). However, as I understand it, is *supposed* to only contain an IP address, without port (per #2528). This PR corrects that discrepancy, making it easier to parse the result of this method in application code. There should not be any compatibility concerns, as anyone parsing the output of would already need to handle both port and portless cases anyway. * Update CHANGES.md --------- Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 4 ++++ actix-web/src/info.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 4e74e0902..3d9176dee 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixed + +- `ConnectionInfo::realip_remote_addr()` now handles IPv6 addresses from `Forwarded` header correctly. Previously, it sometimes returned the forwarded port as well. + ## 4.7.0 ### Added diff --git a/actix-web/src/info.rs b/actix-web/src/info.rs index c5d9638f4..aee936ba9 100644 --- a/actix-web/src/info.rs +++ b/actix-web/src/info.rs @@ -21,6 +21,19 @@ 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 shouldn't *actually* ever happen + .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 +113,7 @@ impl ConnectionInfo { // --- https://datatracker.ietf.org/doc/html/rfc7239#section-5.2 match name.trim().to_lowercase().as_str() { - "for" => realip_remote_addr.get_or_insert_with(|| unquote(val)), + "for" => realip_remote_addr.get_or_insert_with(|| bare_address(unquote(val))), "proto" => scheme.get_or_insert_with(|| unquote(val)), "host" => host.get_or_insert_with(|| unquote(val)), "by" => { @@ -368,16 +381,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] From 4908fd7dea00a361cf6b37f9d66f1ac3c4805543 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 10 Jun 2024 01:44:58 +0100 Subject: [PATCH 037/129] build(deps): bump taiki-e/install-action from 2.34.0 to 2.38.0 (#3396) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.34.0 to 2.38.0. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.34.0...v2.38.0) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 4231b4dc2..d0eb3f8fd 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.34.0 + uses: taiki-e/install-action@v2.38.0 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -80,7 +80,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 - name: Install cargo-hack - uses: taiki-e/install-action@v2.34.0 + uses: taiki-e/install-action@v2.38.0 with: tool: cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab611fc0a..cc51b89f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.34.0 + uses: taiki-e/install-action@v2.38.0 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.34.0 + uses: taiki-e/install-action@v2.38.0 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index da892bd7a..5c944bece 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -23,7 +23,7 @@ jobs: components: llvm-tools-preview - name: Install just,cargo-llvm-cov - uses: taiki-e/install-action@v2.34.0 + uses: taiki-e/install-action@v2.38.0 with: tool: just,cargo-llvm-cov diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index dd44d4cb3..d5c667f99 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -82,7 +82,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.34.0 + uses: taiki-e/install-action@v2.38.0 with: tool: cargo-public-api From 7f529e35b296c8359bdf22e7480722a5844e3ce2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 10 Jun 2024 01:45:11 +0100 Subject: [PATCH 038/129] build(deps): bump actions-rust-lang/setup-rust-toolchain from 1.8.0 to 1.9.0 (#3395) build(deps): bump actions-rust-lang/setup-rust-toolchain Bumps [actions-rust-lang/setup-rust-toolchain](https://github.com/actions-rust-lang/setup-rust-toolchain) from 1.8.0 to 1.9.0. - [Release notes](https://github.com/actions-rust-lang/setup-rust-toolchain/releases) - [Changelog](https://github.com/actions-rust-lang/setup-rust-toolchain/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions-rust-lang/setup-rust-toolchain/compare/v1.8.0...v1.9.0) --- updated-dependencies: - dependency-name: actions-rust-lang/setup-rust-toolchain dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 6 +++--- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index d0eb3f8fd..1729d9a07 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -44,7 +44,7 @@ jobs: echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV - name: Install Rust (${{ matrix.version.name }}) - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: toolchain: ${{ matrix.version.version }} @@ -77,7 +77,7 @@ jobs: run: ./scripts/free-disk-space.sh - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install cargo-hack uses: taiki-e/install-action@v2.38.0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc51b89f6..1b6f7b460 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,7 +59,7 @@ 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.9.0 with: toolchain: ${{ matrix.version.version }} @@ -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.9.0 with: toolchain: nightly @@ -108,7 +108,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: toolchain: nightly diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 5c944bece..de7fd7031 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: components: llvm-tools-preview diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d5c667f99..8fe8f59d3 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.9.0 with: toolchain: nightly components: rustfmt @@ -36,7 +36,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: components: clippy @@ -55,7 +55,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: toolchain: nightly components: rust-docs @@ -77,7 +77,7 @@ jobs: uses: actions/checkout@v4 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: toolchain: nightly-2024-06-07 From 53086a90a679e609ae6afe43d0eb94cdb270f3c0 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 01:58:13 +0100 Subject: [PATCH 039/129] build: add coverage recipes to justfile --- actix-web/src/info.rs | 3 ++- justfile | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/actix-web/src/info.rs b/actix-web/src/info.rs index aee936ba9..1b2e554f9 100644 --- a/actix-web/src/info.rs +++ b/actix-web/src/info.rs @@ -27,7 +27,8 @@ fn bare_address(val: &str) -> &str { val.split("]:") .next() .map(|s| s.trim_start_matches('[').trim_end_matches(']')) - // This shouldn't *actually* ever happen + // 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) diff --git a/justfile b/justfile index 4e106ef11..7f6dbb61e 100644 --- a/justfile +++ b/justfile @@ -53,6 +53,14 @@ test-docs toolchain="": && doc # Test workspace. test-all toolchain="": (test toolchain) (test-docs toolchain) +# Test workspace and generate Codecov coverage file. +test-coverage-codecov toolchain="": + cargo {{ toolchain }} llvm-cov --workspace {{ all_crate_features }} --codecov --output-path codecov.json + +# Test workspace and generate LCOV coverage file. +test-coverage-lcov toolchain="": + cargo {{ toolchain }} llvm-cov --workspace {{ all_crate_features }} --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 }} From 59e42c1446c7524f8c72a89cd8682e58f81d0376 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Mon, 10 Jun 2024 11:19:35 +1000 Subject: [PATCH 040/129] Return 415 rather than 400 on Urlencoded Content-Type mismatch (#3334) Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 1 + actix-web/src/error/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 3d9176dee..28dd25fb5 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -5,6 +5,7 @@ ### Fixed - `ConnectionInfo::realip_remote_addr()` now handles IPv6 addresses from `Forwarded` header correctly. Previously, it sometimes returned the forwarded port as well. +- The `UrlencodedError::ContentType` variant (relevant to the `Form` extractor) now uses the 415 (Media Type Unsupported) status code in it's `ResponseError` implementation. ## 4.7.0 diff --git a/actix-web/src/error/mod.rs b/actix-web/src/error/mod.rs index 91a6bcc3f..25535332c 100644 --- a/actix-web/src/error/mod.rs +++ b/actix-web/src/error/mod.rs @@ -100,6 +100,7 @@ impl ResponseError for UrlencodedError { match self { Self::Overflow { .. } => StatusCode::PAYLOAD_TOO_LARGE, Self::UnknownLength => StatusCode::LENGTH_REQUIRED, + Self::ContentType => StatusCode::UNSUPPORTED_MEDIA_TYPE, Self::Payload(err) => err.status_code(), _ => StatusCode::BAD_REQUEST, } @@ -232,7 +233,7 @@ mod tests { let resp = UrlencodedError::UnknownLength.error_response(); assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED); let resp = UrlencodedError::ContentType.error_response(); - assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); } #[test] From 2ee92d778ec82b3a879967dd5bb690b8eed26f7c Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 03:39:06 +0100 Subject: [PATCH 041/129] ci: external types checking (#3175) --- .github/workflows/lint.yml | 25 ++++++++++++++++++++++++- actix-files/Cargo.toml | 11 ++++++++--- actix-http-test/Cargo.toml | 14 +++++++++++--- actix-http/Cargo.toml | 23 ++++++++++++++++++++--- actix-multipart/Cargo.toml | 15 +++++++++++++++ actix-router/Cargo.toml | 8 +++++--- actix-test/Cargo.toml | 16 ++++++++++++++++ actix-web-actors/Cargo.toml | 12 +++++++++--- actix-web/Cargo.toml | 28 +++++++++++++++++++++++++--- awc/Cargo.toml | 27 ++++++++++++++++++++++----- justfile | 26 ++++++++++++++++++++++++++ 11 files changed, 181 insertions(+), 24 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 8fe8f59d3..ca9d2bbeb 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -65,6 +65,29 @@ jobs: RUSTDOCFLAGS: -D warnings run: cargo +nightly doc --no-deps --workspace --all-features + check-external-types: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust (nightly-2024-05-01) + uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + with: + toolchain: nightly-2024-05-01 + + - name: Install just + uses: taiki-e/install-action@v2.38.0 + with: + tool: just + + - name: Install cargo-check-external-types + uses: taiki-e/cache-cargo-install-action@v1.2.2 + with: + tool: cargo-check-external-types + + - name: check external types + run: just check-external-types-all +nightly-2024-05-01 + public-api-diff: runs-on: ubuntu-latest steps: @@ -76,7 +99,7 @@ jobs: - name: Checkout PR branch uses: actions/checkout@v4 - - name: Install Rust + - name: Install Rust (nightly-2024-06-07) uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: toolchain: nightly-2024-06-07 diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index 7adb8eaf5..57cd4e913 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -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"] diff --git a/actix-http-test/Cargo.toml b/actix-http-test/Cargo.toml index bfb0a3539..0947579a5 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 = [] diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 87e2b391d..4dc0f0bd8 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -34,9 +34,26 @@ 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 = [] diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index f1289d3a2..5e9b78d84 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -16,6 +16,21 @@ edition = "2021" rustdoc-args = ["--cfg", "docsrs"] all-features = true +[package.metadata.cargo_check_external_types] +allowed_external_types = [ + "actix_http::*", + "actix_multipart_derive::*", + "actix_utils::*", + "actix_web::*", + "bytes::*", + "futures_core::*", + "mime::*", + "serde_json::*", + "serde_plain::*", + "serde::*", + "tempfile::*", +] + [features] default = ["tempfile", "derive"] derive = ["actix-multipart-derive"] diff --git a/actix-router/Cargo.toml b/actix-router/Cargo.toml index 56e4bed2f..7e7e3beb8 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"] diff --git a/actix-test/Cargo.toml b/actix-test/Cargo.toml index 41267c969..e810ae80b 100644 --- a/actix-test/Cargo.toml +++ b/actix-test/Cargo.toml @@ -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 = [] diff --git a/actix-web-actors/Cargo.toml b/actix-web-actors/Cargo.toml index 114ec5a87..3c74a4f47 100644 --- a/actix-web-actors/Cargo.toml +++ b/actix-web-actors/Cargo.toml @@ -9,9 +9,15 @@ repository = "https://github.com/actix/actix-web" license = "MIT OR Apache-2.0" edition = "2021" -[lib] -name = "actix_web_actors" -path = "src/lib.rs" +[package.metadata.cargo_check_external_types] +allowed_external_types = [ + "actix::*", + "actix_http::*", + "actix_web::*", + "bytes::*", + "bytestring::*", + "futures_core::*", +] [dependencies] actix = { version = ">=0.12, <0.14", default-features = false } diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 10a507680..3827d4400 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -35,9 +35,31 @@ 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 = [ diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 6ab408ea6..4fc2057f6 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -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"] @@ -134,7 +151,7 @@ rcgen = "0.13" rustls-pemfile = "2" tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] } zstd = "0.13" -tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests +tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests [[example]] name = "client" diff --git a/justfile b/justfile index 7f6dbb61e..28b4dfd0a 100644 --- a/justfile +++ b/justfile @@ -74,3 +74,29 @@ doc-watch: update-readmes: && fmt cd ./actix-files && cargo rdme --force cd ./actix-router && cargo rdme --force + +# 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}} From 7a2313cc4b6a9ff613903c7684162e3129ef7b72 Mon Sep 17 00:00:00 2001 From: Timo Caktu <74780331+TimoCak@users.noreply.github.com> Date: Mon, 10 Jun 2024 04:49:50 +0200 Subject: [PATCH 042/129] web: add `HttpRequest::full_url()` (#3096) * implemented function which returns full uir * changes added into the changelog * added test funtion for full_uri method * refactor: rename to full_url --------- Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 4 ++++ actix-web/src/request.rs | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 28dd25fb5..bb0844e05 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Add `HttpRequest::full_url()` method to get the complete URL of the request. + ### Fixed - `ConnectionInfo::realip_remote_addr()` now handles IPv6 addresses from `Forwarded` header correctly. Previously, it sometimes returned the forwarded port as well. diff --git a/actix-web/src/request.rs b/actix-web/src/request.rs index 08a222c86..47b3e3d88 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", + ); + } } From d9579cf58af2230b651d476945639c17b31ef84a Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 04:05:21 +0100 Subject: [PATCH 043/129] test: coverage for doctests --- .github/workflows/coverage.yml | 2 +- justfile | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index de7fd7031..f1d787767 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -28,7 +28,7 @@ jobs: tool: just,cargo-llvm-cov - 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.4.1 diff --git a/justfile b/justfile index 28b4dfd0a..530bf5f64 100644 --- a/justfile +++ b/justfile @@ -53,13 +53,19 @@ test-docs toolchain="": && doc # Test workspace. test-all toolchain="": (test toolchain) (test-docs toolchain) -# Test workspace and generate Codecov coverage file. -test-coverage-codecov toolchain="": - cargo {{ toolchain }} llvm-cov --workspace {{ all_crate_features }} --codecov --output-path codecov.json +# 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 LCOV coverage file. -test-coverage-lcov toolchain="": - cargo {{ toolchain }} llvm-cov --workspace {{ all_crate_features }} --lcov --output-path lcov.info +# 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: From 9553e7afff8661dd8805f77a24a1d178e1665d3a Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 04:08:10 +0100 Subject: [PATCH 044/129] ci: fix coverage --- .github/workflows/coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index f1d787767..7aeea6291 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -22,10 +22,10 @@ jobs: with: components: llvm-tools-preview - - name: Install just,cargo-llvm-cov + - name: Install just, cargo-llvm-cov, cargo-nextest uses: taiki-e/install-action@v2.38.0 with: - tool: just,cargo-llvm-cov + tool: just,cargo-llvm-cov,cargo-nextest - name: Generate code coverage run: just test-coverage-codecov From 9b3de1f1fe59bd6d3ebc645590eb49f77260a20b Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 04:15:58 +0100 Subject: [PATCH 045/129] ci: fix doctest coverage --- .github/workflows/coverage.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 7aeea6291..ca3115713 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -17,10 +17,11 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Install Rust + - name: Install Rust (nightly) uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: - components: llvm-tools-preview + toolchain: nightly + components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest uses: taiki-e/install-action@v2.38.0 From 0fd85bae2a8fa5f804c88f01aacb2202f44baed7 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 21:51:53 +0100 Subject: [PATCH 046/129] test: demonstrate panic in multipart forms (#3397) --- actix-multipart/src/form/mod.rs | 66 ++++++++++++++++++++++++++++++++- actix-multipart/src/server.rs | 19 ++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/actix-multipart/src/form/mod.rs b/actix-multipart/src/form/mod.rs index 451b103fd..6fbdfa1a1 100644 --- a/actix-multipart/src/form/mod.rs +++ b/actix-multipart/src/form/mod.rs @@ -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()`]: https://docs.rs/futures-util/0.3/futures_util/stream/trait.StreamExt.html#method.fuse fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future; } @@ -396,11 +404,20 @@ mod tests { use actix_http::encoding::Decoder; use actix_multipart_rfc7578::client::multipart; use actix_test::TestServer; - use actix_web::{dev::Payload, http::StatusCode, web, App, HttpResponse, Responder}; + use actix_web::{ + dev::Payload, http::StatusCode, web, App, HttpRequest, HttpResponse, Resource, Responder, + }; use awc::{Client, ClientResponse}; + use futures_core::future::LocalBoxFuture; + use futures_util::TryStreamExt as _; use super::MultipartForm; - use crate::form::{bytes::Bytes, tempfile::TempFile, text::Text, MultipartFormConfig}; + use crate::{ + form::{ + bytes::Bytes, tempfile::TempFile, text::Text, FieldReader, Limits, MultipartFormConfig, + }, + Field, MultipartError, + }; pub async fn send_form( srv: &TestServer, @@ -734,4 +751,49 @@ mod tests { let response = send_form(&srv, form, "/").await; assert_eq!(response.status(), StatusCode::BAD_REQUEST); } + + #[should_panic(expected = "called `Result::unwrap()` on an `Err` value: Connect(Disconnected)")] + #[actix_web::test] + async fn field_try_next_panic() { + #[derive(Debug)] + struct NullSink; + + impl<'t> FieldReader<'t> for NullSink { + type Future = LocalBoxFuture<'t, Result>; + + 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/server.rs b/actix-multipart/src/server.rs index d0f833318..0256aa7bf 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -465,7 +465,12 @@ impl Stream for Field { 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) { + if let Some(mut buffer) = inner + .payload + .as_ref() + .expect("Field should not be polled after completion") + .get_mut(&this.safety) + { // check safety and poll read payload to buffer. buffer.poll_stream(cx)?; } else if !this.safety.is_clean() { @@ -496,6 +501,7 @@ impl fmt::Debug for Field { } struct InnerField { + /// Payload is initialized as Some and is `take`n when the field stream finishes. payload: Option, boundary: String, eof: bool, @@ -643,7 +649,12 @@ impl InnerField { return Poll::Ready(None); } - let result = if let Some(mut payload) = self.payload.as_ref().unwrap().get_mut(s) { + let result = if let Some(mut payload) = self + .payload + .as_ref() + .expect("Field should not be polled after completion") + .get_mut(s) + { if !self.eof { let res = if let Some(ref mut len) = self.length { InnerField::read_len(&mut payload, len) @@ -674,8 +685,10 @@ impl InnerField { }; if let Poll::Ready(None) = result { - self.payload.take(); + // drop payload buffer and make future un-poll-able + let _ = self.payload.take(); } + result } } From 4c4c27993864249d58d9e3a04c9c80cf4f7d678c Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 23:23:38 +0100 Subject: [PATCH 047/129] docs(test): intrgrate cargo-rdme --- actix-multipart/src/form/mod.rs | 2 +- actix-test/README.md | 45 +++++++++++++++++++++++++++++++++ actix-test/src/lib.rs | 1 + justfile | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 actix-test/README.md diff --git a/actix-multipart/src/form/mod.rs b/actix-multipart/src/form/mod.rs index 6fbdfa1a1..68cdefec5 100644 --- a/actix-multipart/src/form/mod.rs +++ b/actix-multipart/src/form/mod.rs @@ -40,7 +40,7 @@ pub trait FieldReader<'t>: Sized + Any { /// `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()`]: https://docs.rs/futures-util/0.3/futures_util/stream/trait.StreamExt.html#method.fuse + /// [`fuse()`]: futures_util::stream::StreamExt::fuse() fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future; } diff --git a/actix-test/README.md b/actix-test/README.md new file mode 100644 index 000000000..1a9b6f22a --- /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 803320607..9be99978d 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}; //! diff --git a/justfile b/justfile index 530bf5f64..2ea6031f8 100644 --- a/justfile +++ b/justfile @@ -80,6 +80,7 @@ doc-watch: update-readmes: && fmt cd ./actix-files && cargo rdme --force cd ./actix-router && cargo rdme --force + cd ./actix-test && cargo rdme --force # Check for unintentional external type exposure on all crates in workspace. check-external-types-all toolchain="+nightly": From cd301a6932e274229ced4904d87600500a692229 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 23:30:51 +0100 Subject: [PATCH 048/129] docs: local docs doc everything but only list workspace crates --- justfile | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index 2ea6031f8..f44236ece 100644 --- a/justfile +++ b/justfile @@ -68,8 +68,17 @@ test-coverage-lcov toolchain="": (test-coverage toolchain) cargo {{ toolchain }} llvm-cov report --doctests --lcov --output-path=lcov.info # Document crates in workspace. -doc *args: - RUSTDOCFLAGS="--cfg=docsrs -Dwarnings" cargo +nightly doc --no-deps --workspace {{ all_crate_features }} {{ args }} +doc *args: && doc-set-workspace-crates + RUSTDOCFLAGS="--cfg=docsrs -Dwarnings" cargo +nightly doc --workspace {{ all_crate_features }} {{ args }} + +[private] +doc-set-workspace-crates: + #!/usr/bin/env bash + ( + echo "window.ALL_CRATES =" + cargo metadata --format-version=1 | jq '[.packages[] | select(.source == null) | .name]' + echo ";" + ) > "$CARGO_TARGET_DIR/doc/crates.js" # Document crates in workspace and watch for changes. doc-watch: From cc5030c542084fefee87cce743286b79b6a58fb8 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 23:31:45 +0100 Subject: [PATCH 049/129] docs(http-test): use cargo-rdme --- actix-http-test/README.md | 8 ++++++-- justfile | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/actix-http-test/README.md b/actix-http-test/README.md index ee242d1d5..939028121 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/justfile b/justfile index f44236ece..9f909bfac 100644 --- a/justfile +++ b/justfile @@ -88,6 +88,7 @@ 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-test && cargo rdme --force From 132b84d3b1f0f01a8a4923c442039ba46f845977 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 23:35:26 +0100 Subject: [PATCH 050/129] docs(multipart): use cargo-rdme --- actix-multipart/README.md | 21 ++++++--------------- actix-multipart/src/lib.rs | 2 ++ justfile | 1 + 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/actix-multipart/README.md b/actix-multipart/README.md index c7697785a..ef7630637 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -1,7 +1,5 @@ # `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) @@ -15,18 +13,13 @@ -## Example + -Dependencies: +Multipart form support for Actix Web. -```toml -[dependencies] -actix-multipart = "0.6" -actix-web = "4.5" -serde = { version = "1.0", features = ["derive"] } -``` +## Examples -Code: +[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) ```rust use actix_web::{post, App, HttpServer, Responder}; @@ -63,6 +56,8 @@ async fn main() -> std::io::Result<()> { } ``` + + Curl request : ```bash @@ -71,7 +66,3 @@ curl -v --request POST \ -F 'json={"name": "Cargo.lock"};type=application/json' \ -F file=@./Cargo.lock ``` - -### Examples - -https://github.com/actix/examples/tree/master/forms/multipart diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index d19e951e6..51b06db38 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -1,5 +1,7 @@ //! Multipart form support for Actix Web. +//! //! # Examples +//! //! ```no_run //! use actix_web::{post, App, HttpServer, Responder}; //! diff --git a/justfile b/justfile index 9f909bfac..d15ffcf67 100644 --- a/justfile +++ b/justfile @@ -90,6 +90,7 @@ 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 # Check for unintentional external type exposure on all crates in workspace. From 0ce488e57a06f8971f9dd8b51b16766c682bd9cc Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Jun 2024 23:54:16 +0100 Subject: [PATCH 051/129] docs: fix build --- justfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/justfile b/justfile index d15ffcf67..646c6b44d 100644 --- a/justfile +++ b/justfile @@ -78,7 +78,7 @@ doc-set-workspace-crates: echo "window.ALL_CRATES =" cargo metadata --format-version=1 | jq '[.packages[] | select(.source == null) | .name]' echo ";" - ) > "$CARGO_TARGET_DIR/doc/crates.js" + ) > "$(cargo metadata --format-version=1 | jq -r '.target_directory')/doc/crates.js" # Document crates in workspace and watch for changes. doc-watch: From 188206a90382111e2d8337febf4f6965c0730b6e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 11 Jun 2024 00:36:46 +0100 Subject: [PATCH 052/129] feat: Html responder (#3399) --- actix-multipart/README.md | 4 +-- actix-web/CHANGES.md | 1 + actix-web/README.md | 2 +- actix-web/src/types/html.rs | 66 +++++++++++++++++++++++++++++++++++++ actix-web/src/types/mod.rs | 2 ++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 actix-web/src/types/html.rs diff --git a/actix-multipart/README.md b/actix-multipart/README.md index ef7630637..d61347f32 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -19,8 +19,6 @@ Multipart form support for Actix Web. ## Examples -[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) - ```rust use actix_web::{post, App, HttpServer, Responder}; @@ -58,6 +56,8 @@ async fn main() -> std::io::Result<()> { +[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) + Curl request : ```bash diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index bb0844e05..54c7045ae 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ ### Added +- Add `web::Html` responder. - Add `HttpRequest::full_url()` method to get the complete URL of the request. ### Fixed diff --git a/actix-web/README.md b/actix-web/README.md index 8b4375bdd..3f9d3e0d5 100644 --- a/actix-web/README.md +++ b/actix-web/README.md @@ -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/src/types/html.rs b/actix-web/src/types/html.rs new file mode 100644 index 000000000..c370ee07b --- /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/mod.rs b/actix-web/src/types/mod.rs index 792edd650..cabe53d6a 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}, From fa74ab3dfb164f7184e4dcea82901ab62e40058f Mon Sep 17 00:00:00 2001 From: Nikolaos Chatzikonstantinou Date: Thu, 13 Jun 2024 21:51:29 -0400 Subject: [PATCH 053/129] remove references to StaticFiles (#3400) --- actix-web/src/app.rs | 1 - actix-web/src/scope.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/actix-web/src/app.rs b/actix-web/src/app.rs index 3d86d1f9b..21a4443cf 100644 --- a/actix-web/src/app.rs +++ b/actix-web/src/app.rs @@ -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, diff --git a/actix-web/src/scope.rs b/actix-web/src/scope.rs index adc9f75d3..81f3615b0 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}; From 3ecaff5f5bfa8b952dfd6e5110c48ea31c9e58b9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 00:28:10 +0000 Subject: [PATCH 054/129] build(deps): bump taiki-e/cache-cargo-install-action from 1.2.2 to 2.0.1 (#3406) Bumps [taiki-e/cache-cargo-install-action](https://github.com/taiki-e/cache-cargo-install-action) from 1.2.2 to 2.0.1. - [Release notes](https://github.com/taiki-e/cache-cargo-install-action/releases) - [Changelog](https://github.com/taiki-e/cache-cargo-install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/cache-cargo-install-action/compare/v1.2.2...v2.0.1) --- updated-dependencies: - dependency-name: taiki-e/cache-cargo-install-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ca9d2bbeb..1c8841289 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -81,7 +81,7 @@ jobs: tool: just - name: Install cargo-check-external-types - uses: taiki-e/cache-cargo-install-action@v1.2.2 + uses: taiki-e/cache-cargo-install-action@v2.0.1 with: tool: cargo-check-external-types From c076e34b5d88df9ff8975d7608aead544097b2bb Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 03:27:23 +0100 Subject: [PATCH 055/129] build(deps): bump codecov/codecov-action from 4.4.1 to 4.5.0 (#3405) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.4.1 to 4.5.0. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v4.4.1...v4.5.0) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index ca3115713..60ddb7c10 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -32,7 +32,7 @@ jobs: run: just test-coverage-codecov - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4.4.1 + uses: codecov/codecov-action@v4.5.0 with: files: codecov.json fail_ci_if_error: true From 66905efd7b02a464f0becff59685c8ce58f243c2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Jun 2024 02:28:03 +0000 Subject: [PATCH 056/129] build(deps): bump taiki-e/install-action from 2.38.0 to 2.39.1 (#3404) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.38.0 to 2.39.1. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.38.0...v2.39.1) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 1729d9a07..67a6d0f8f 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -80,7 +80,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install cargo-hack - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b6f7b460..9cb8d7b7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 60ddb7c10..630c6c864 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1c8841289..49d88df1b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.38.0 + uses: taiki-e/install-action@v2.39.1 with: tool: cargo-public-api From 643d64581a3b4b9f718827168e87703a188e6ace Mon Sep 17 00:00:00 2001 From: Yury Yarashevich Date: Thu, 20 Jun 2024 00:34:49 +0200 Subject: [PATCH 057/129] Fix Rustls 0.22 & 0.23 are limited to 256 handshakes per second. (#3408) --- actix-http/Cargo.toml | 16 ++++++++++------ actix-http/src/lib.rs | 8 +------- actix-http/src/service.rs | 16 ++-------------- actix-web/CHANGES.md | 1 + actix-web/Cargo.toml | 14 +++++++++----- actix-web/src/server.rs | 18 +++--------------- 6 files changed, 26 insertions(+), 47 deletions(-) diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 4dc0f0bd8..25e8b28f5 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -70,22 +70,22 @@ ws = [ ] # 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"] @@ -96,6 +96,10 @@ compress-zstd = ["__compress", "zstd"] # 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" diff --git a/actix-http/src/lib.rs b/actix-http/src/lib.rs index f9697c4d5..b76032ba3 100644 --- a/actix-http/src/lib.rs +++ b/actix-http/src/lib.rs @@ -61,13 +61,7 @@ pub mod ws; #[allow(deprecated)] pub use self::payload::PayloadStream; -#[cfg(any( - feature = "openssl", - feature = "rustls-0_20", - feature = "rustls-0_21", - feature = "rustls-0_22", - feature = "rustls-0_23", -))] +#[cfg(feature = "__tls")] pub use self::service::TlsAcceptorConfig; pub use self::{ builder::HttpServiceBuilder, diff --git a/actix-http/src/service.rs b/actix-http/src/service.rs index a58be93c7..37cee960f 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 { diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 54c7045ae..bf63a689e 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -11,6 +11,7 @@ - `ConnectionInfo::realip_remote_addr()` now handles IPv6 addresses from `Forwarded` header correctly. Previously, it sometimes returned the forwarded port as well. - The `UrlencodedError::ContentType` variant (relevant to the `Form` extractor) now uses the 415 (Media Type Unsupported) status code in it's `ResponseError` implementation. +- `HttpServer::method.max_connection_rate` now takes effect on any TLS implementation. Previously, the configuration was missing for rustls versions 0.22 and 0.23. ## 4.7.0 diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 3827d4400..bc441fd7e 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -93,18 +93,18 @@ secure-cookies = ["cookies", "cookie/secure"] 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"] @@ -113,6 +113,10 @@ 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"] diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index 33b1e1894..95e15581f 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 } @@ -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() From cbb55ba27d98ca24e364f1c26b6fa96b3ade6e32 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 19 Jun 2024 23:40:22 +0100 Subject: [PATCH 058/129] ci: use just for feature combos check --- .cargo/config.toml | 10 ---------- .github/workflows/ci-post-merge.yml | 14 +++++++------- actix-http/Cargo.toml | 16 ++++++++-------- actix-http/src/lib.rs | 8 ++++---- justfile | 15 ++++++++++++++- 5 files changed, 33 insertions(+), 30 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index a2345e184..000000000 --- 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/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 67a6d0f8f..59a17e70a 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -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.9.0 - - name: Install cargo-hack + - name: Install just, cargo-hack uses: taiki-e/install-action@v2.39.1 with: - tool: cargo-hack + tool: just,cargo-hack - - name: check feature combinations - run: cargo ci-check-all-feature-powerset - - - name: check feature combinations - run: cargo ci-check-all-feature-powerset-linux + - name: Check feature combinations + run: just check-feature-combinations diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 25e8b28f5..5f6a424e8 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -59,14 +59,14 @@ allowed_external_types = [ 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 @@ -88,9 +88,9 @@ rustls-0_22 = ["__tls", "actix-tls/accept", "actix-tls/rustls-0_22"] 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. diff --git a/actix-http/src/lib.rs b/actix-http/src/lib.rs index b76032ba3..ac79433c6 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. | diff --git a/justfile b/justfile index 646c6b44d..985e2b35a 100644 --- a/justfile +++ b/justfile @@ -22,7 +22,7 @@ non_linux_all_features_list := ``` cargo metadata --format-version=1 \ | jq '.packages[] | select(.source == null) | .features | keys' \ | jq -r --slurp \ - --arg exclusions "tokio-uring,io-uring,experimental-io-uring" \ + --arg exclusions "__tls,__compress,tokio-uring,io-uring,experimental-io-uring" \ 'add | unique | . - ($exclusions | split(",")) | join(",")' ``` @@ -93,6 +93,19 @@ update-readmes: && fmt 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 From c612b5ce940588268e55719c24ef78bd8d2691ce Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 20 Jun 2024 00:13:42 +0100 Subject: [PATCH 059/129] ci: fix checks --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- justfile | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 59a17e70a..e60325bf8 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -54,10 +54,10 @@ jobs: 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 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9cb8d7b7e..9ff206ffb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/justfile b/justfile index 985e2b35a..a3e81b271 100644 --- a/justfile +++ b/justfile @@ -32,6 +32,14 @@ all_crate_features := if os() == "linux" { "--features='" + non_linux_all_features_list + "'" } +[private] +check-min: + cargo hack --workspace check --no-default-features + +[private] +check-default: + cargo hack --workspace check + # Run Clippy over workspace. clippy toolchain="": cargo {{ toolchain }} clippy --workspace --all-targets {{ all_crate_features }} From d92a73eacdc575918e6ffaf965c71d6ece02be9e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 20 Jun 2024 00:18:22 +0100 Subject: [PATCH 060/129] chore(actix-http): prepare release 3.8.0 --- actix-http/CHANGES.md | 2 ++ actix-http/Cargo.toml | 2 +- actix-http/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 85ba03100..48c730bb2 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 3.8.0 + ### Added - Add `error::InvalidStatusCode` re-export. diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 5f6a424e8..0a0252e9a 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.8.0" authors = [ "Nikolay Kim ", "Rob Ede ", diff --git a/actix-http/README.md b/actix-http/README.md index 0ba3fdcac..7d56f2517 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.8.0)](https://docs.rs/actix-http/3.8.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.8.0/status.svg)](https://deps.rs/crate/actix-http/3.8.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) From 4222f92bd35b854cb07ceb9a03862402cf82299d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 20 Jun 2024 00:23:11 +0100 Subject: [PATCH 061/129] chore(actix-web): prepare release 4.8.0 --- actix-web/CHANGES.md | 6 ++++-- actix-web/Cargo.toml | 2 +- actix-web/README.md | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index bf63a689e..90c7b7e2a 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 4.8.0 + ### Added - Add `web::Html` responder. @@ -9,9 +11,9 @@ ### Fixed -- `ConnectionInfo::realip_remote_addr()` now handles IPv6 addresses from `Forwarded` header correctly. Previously, it sometimes returned the forwarded port as well. +- 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. -- `HttpServer::method.max_connection_rate` now takes effect on any TLS implementation. Previously, the configuration was missing for rustls versions 0.22 and 0.23. +- Apply `HttpServer::max_connection_rate()` setting when using rustls v0.22 or v0.23. ## 4.7.0 diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index bc441fd7e..125cfe20f 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web" -version = "4.7.0" +version = "4.8.0" description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust" authors = [ "Nikolay Kim ", diff --git a/actix-web/README.md b/actix-web/README.md index 3f9d3e0d5..d30d945a4 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.7.0)](https://docs.rs/actix-web/4.7.0) +[![Documentation](https://docs.rs/actix-web/badge.svg?version=4.8.0)](https://docs.rs/actix-web/4.8.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.7.0/status.svg)](https://deps.rs/crate/actix-web/4.7.0) +[![Dependency Status](https://deps.rs/crate/actix-web/4.8.0/status.svg)](https://deps.rs/crate/actix-web/4.8.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) From 9f45be03e1fdee207b360de40fc2140d3305360f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 24 Jun 2024 10:22:54 +0100 Subject: [PATCH 062/129] build(deps): bump taiki-e/install-action from 2.39.1 to 2.41.2 (#3412) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.39.1 to 2.41.2. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.39.1...v2.41.2) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index e60325bf8..6803fc157 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ff206ffb..48c286f9f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 630c6c864..3865aa51b 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 49d88df1b..dddde3f4f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.39.1 + uses: taiki-e/install-action@v2.41.2 with: tool: cargo-public-api From 4db4251b8ffbcd3852c6b657d5d6df076d3227ae Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 30 Jun 2024 18:45:20 +0100 Subject: [PATCH 063/129] chore: cargo update after version bumps --- scripts/bump | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/bump b/scripts/bump index 6fd879eae..7a57e6ed0 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 From 0b193c710677332d56da3a028c3c1e30983502b7 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 30 Jun 2024 18:45:38 +0100 Subject: [PATCH 064/129] build: fix doc-watch recipe --- justfile | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/justfile b/justfile index a3e81b271..5cd56b12e 100644 --- a/justfile +++ b/justfile @@ -3,6 +3,7 @@ _list: # Format workspace. fmt: + just --unstable --fmt cargo +nightly fmt fd --hidden --type=file --extension=md --extension=yml --exec-batch npx -y prettier --write @@ -17,7 +18,6 @@ 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' \ @@ -25,12 +25,7 @@ non_linux_all_features_list := ``` --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: @@ -84,7 +79,8 @@ doc-set-workspace-crates: #!/usr/bin/env bash ( echo "window.ALL_CRATES =" - cargo metadata --format-version=1 | jq '[.packages[] | select(.source == null) | .name]' + 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" @@ -101,11 +97,7 @@ update-readmes: && fmt 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" -} +feature_combo_skip_list := if os() == "linux" { "__tls,__compress" } else { "__tls,__compress,experimental-io-uring" } # Checks compatibility of feature combinations. check-feature-combinations: @@ -120,7 +112,7 @@ check-external-types-all toolchain="+nightly": 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 + if ! just check-external-types-manifest "$f" {{ toolchain }}; then exit=1; fi echo echo done @@ -133,9 +125,9 @@ check-external-types-all-table toolchain="+nightly": 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 + 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}} + cargo {{ toolchain }} check-external-types --manifest-path "{{ manifest_path }}" {{ extra_args }} From 763c58445af61531b7fe335679e773755cad73d8 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 30 Jun 2024 20:28:11 +0100 Subject: [PATCH 065/129] test: fix tests based on mime-guess inference relates to https://github.com/abonander/mime_guess/pull/86 --- actix-files/src/lib.rs | 4 ++-- actix-http/src/responses/builder.rs | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 167f996c0..0178c13de 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -307,11 +307,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-http/src/responses/builder.rs b/actix-http/src/responses/builder.rs index 91c69ba54..bb7d0f712 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] From 668b8e5745d4b67ba1c88e2dd9f729f124dec4af Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 1 Jul 2024 01:55:49 +0100 Subject: [PATCH 066/129] build(deps): bump taiki-e/install-action from 2.41.2 to 2.41.7 (#3419) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.41.2 to 2.41.7. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.41.2...v2.41.7) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 6803fc157..444f1b174 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 48c286f9f..caee78896 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 3865aa51b..260d65e27 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index dddde3f4f..1fd20f221 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.41.2 + uses: taiki-e/install-action@v2.41.7 with: tool: cargo-public-api From 71cd3a31f9b3f4b86afe9c24645a3c8211ec7905 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 1 Jul 2024 03:55:08 +0100 Subject: [PATCH 067/129] fix(multipart): optional content-disposition for non-form-data requests (#3416) --- actix-multipart-derive/src/lib.rs | 6 +- actix-multipart/CHANGES.md | 9 + actix-multipart/Cargo.toml | 2 + actix-multipart/README.md | 12 +- actix-multipart/examples/form.rs | 36 ++ actix-multipart/src/error.rs | 96 +++-- actix-multipart/src/extractor.rs | 6 +- actix-multipart/src/form/bytes.rs | 3 +- actix-multipart/src/form/json.rs | 7 +- actix-multipart/src/form/mod.rs | 83 +++- actix-multipart/src/form/tempfile.rs | 28 +- actix-multipart/src/form/text.rs | 9 +- actix-multipart/src/lib.rs | 16 +- actix-multipart/src/server.rs | 406 +++++++++++------- actix-multipart/src/test.rs | 2 + .../src/http/header/content_disposition.rs | 6 +- 16 files changed, 479 insertions(+), 248 deletions(-) create mode 100644 actix-multipart/examples/form.rs diff --git a/actix-multipart-derive/src/lib.rs b/actix-multipart-derive/src/lib.rs index 9552ad2d9..44db1fa0e 100644 --- a/actix-multipart-derive/src/lib.rs +++ b/actix-multipart-derive/src/lib.rs @@ -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 a91edf9c8..dea24ab9d 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,15 @@ ## Unreleased +- Add `MultipartError::ContentTypeIncompatible` variant. +- Add `MultipartError::ContentDispositionNameMissing` variant. +- 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`. diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index 5e9b78d84..c5ff7dd10 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -63,7 +63,9 @@ 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"] } multer = "3" tokio = { version = "1.24.2", features = ["sync"] } diff --git a/actix-multipart/README.md b/actix-multipart/README.md index d61347f32..917dceece 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -54,15 +54,15 @@ async fn main() -> std::io::Result<()> { } ``` - +cURL request: -[More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) - -Curl request : - -```bash +```sh curl -v --request POST \ --url http://localhost:8080/videos \ -F 'json={"name": "Cargo.lock"};type=application/json' \ -F file=@./Cargo.lock ``` + + + +[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 000000000..a90aeff96 --- /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 77b5a559f..30ef63c1a 100644 --- a/actix-multipart/src/error.rs +++ b/actix-multipart/src/error.rs @@ -11,77 +11,95 @@ use derive_more::{Display, Error, From}; #[derive(Debug, Display, From, Error)] #[non_exhaustive] pub enum MultipartError { - /// Content-Disposition header is not found or is not equal to "form-data". + /// Could not find Content-Type header. + #[display(fmt = "Could not find Content-Type header")] + ContentTypeMissing, + + /// Could not parse Content-Type header. + #[display(fmt = "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(fmt = "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, - - /// Can not parse Content-Type header - #[display(fmt = "Can not parse Content-Type header")] - ParseContentType, - - /// Multipart boundary is not found + /// Multipart boundary is not found. #[display(fmt = "Multipart boundary is not found")] - Boundary, + BoundaryMissing, - /// Nested multipart is not supported + /// 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(fmt = "Content-Disposition header was not found when parsing a \"form-data\" field")] + ContentDispositionMissing, + + /// Content-Disposition name parameter was not found when parsing a "form-data" field. + /// + /// 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(fmt = "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 stream is incomplete + /// Multipart stream is incomplete. #[display(fmt = "Multipart stream is incomplete")] Incomplete, - /// Error during field parsing - #[display(fmt = "{}", _0)] + /// Field parsing failed. + #[display(fmt = "Error during field parsing")] Parse(ParseError), - /// Payload error - #[display(fmt = "{}", _0)] + /// HTTP payload error. + #[display(fmt = "Payload error")] Payload(PayloadError), - /// Not consumed - #[display(fmt = "Multipart stream is not consumed")] + /// Stream is not consumed. + #[display(fmt = "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(fmt = "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(fmt = "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(fmt = "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(fmt = "Unknown field: {_0}")] #[from(ignore)] - UnsupportedField(#[error(not(source))] String), + UnknownField(#[error(not(source))] String), } -/// Return `BadRequest` for `MultipartError` +/// Return `BadRequest` for `MultipartError`. impl ResponseError for MultipartError { fn status_code(&self) -> StatusCode { match &self { MultipartError::Field { source, .. } => source.as_response_error().status_code(), + MultipartError::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 = MultipartError::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 56ed69ae4..ab98f887e 100644 --- a/actix-multipart/src/extractor.rs +++ b/actix-multipart/src/extractor.rs @@ -10,6 +10,7 @@ use crate::server::Multipart; /// Content-type: multipart/form-data; /// /// # Examples +/// /// ``` /// use actix_web::{web, HttpResponse, Error}; /// use actix_multipart::Multipart; @@ -35,9 +36,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/form/bytes.rs b/actix-multipart/src/form/bytes.rs index 3c5e2eb10..c152db3d0 100644 --- a/actix-multipart/src/form/bytes.rs +++ b/actix-multipart/src/form/bytes.rs @@ -41,8 +41,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 bb4e03bf6..3504c340c 100644 --- a/actix-multipart/src/form/json.rs +++ b/actix-multipart/src/form/json.rs @@ -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)), }, )?)) diff --git a/actix-multipart/src/form/mod.rs b/actix-multipart/src/form/mod.rs index 68cdefec5..9441d249f 100644 --- a/actix-multipart/src/form/mod.rs +++ b/actix-multipart/src/form/mod.rs @@ -80,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, )))) } @@ -95,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(()) @@ -123,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(); @@ -159,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, )))) } @@ -174,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(()) @@ -281,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); @@ -294,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); @@ -313,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); @@ -329,6 +346,7 @@ where // Update the stored limit *entry = limits.field_limit_remaining; } + let inner = T::from_state(state)?; Ok(MultipartForm(inner)) } @@ -752,6 +770,41 @@ mod tests { 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() { diff --git a/actix-multipart/src/form/tempfile.rs b/actix-multipart/src/form/tempfile.rs index 9371a026b..f329876f2 100644 --- a/actix-multipart/src/form/tempfile.rs +++ b/actix-multipart/src/form/tempfile.rs @@ -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, }) }) @@ -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 83e211524..67a434ee6 100644 --- a/actix-multipart/src/form/text.rs +++ b/actix-multipart/src/form/text.rs @@ -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)), } })?)) diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index 51b06db38..853648beb 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -5,7 +5,7 @@ //! ```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)] @@ -17,7 +17,7 @@ //! struct UploadForm { //! #[multipart(limit = "100MB")] //! file: TempFile, -//! json: MPJson, +//! json: MpJson, //! } //! //! #[post("/videos")] @@ -36,6 +36,15 @@ //! .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 +//! ``` #![deny(rust_2018_idioms, nonstandard_style)] #![warn(future_incompatible)] @@ -57,7 +66,4 @@ 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, - }, }; diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs index 0256aa7bf..76eae11ff 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -10,12 +10,15 @@ use std::{ }; use actix_web::{ + dev, error::{ParseError, PayloadError}, http::header::{self, ContentDisposition, HeaderMap, HeaderName, HeaderValue}, + HttpRequest, }; use bytes::{Bytes, BytesMut}; use futures_core::stream::{LocalBoxStream, Stream}; use local_waker::LocalWaker; +use mime::Mime; use crate::error::MultipartError; @@ -23,87 +26,79 @@ 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. +/// 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, + error: Option, } impl Multipart { - /// Create multipart instance for boundary. - pub fn new(headers: &HeaderMap, stream: S) -> Multipart + /// Creates multipart instance from parts. + pub fn new(headers: &HeaderMap, stream: S) -> Self where S: Stream> + 'static, { - match Self::boundary(headers) { - Ok(boundary) => Multipart::from_boundary(boundary, stream), - Err(err) => Multipart::from_error(err), + match Self::find_ct_and_boundary(headers) { + Ok((ct, boundary)) => Self::from_ct_and_boundary(ct, boundary, stream), + Err(err) => Self::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) + /// 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), + } } - /// Create multipart instance for given boundary and stream - pub(crate) fn from_boundary(boundary: String, stream: S) -> Multipart + /// Extract Content-Type and boundary info from headers. + pub(crate) fn find_ct_and_boundary( + headers: &HeaderMap, + ) -> Result<(Mime, String), MultipartError> { + let content_type = headers + .get(&header::CONTENT_TYPE) + .ok_or(MultipartError::ContentTypeMissing)? + .to_str() + .ok() + .and_then(|content_type| content_type.parse::().ok()) + .ok_or(MultipartError::ContentTypeParse)?; + + if content_type.type_() != mime::MULTIPART { + return Err(MultipartError::ContentTypeIncompatible); + } + + let boundary = content_type + .get_param(mime::BOUNDARY) + .ok_or(MultipartError::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 { - error: None, safety: Safety::new(), inner: Some(InnerMultipart { - boundary, payload: PayloadRef::new(PayloadBuffer::new(stream)), + content_type: ct, + boundary, state: InnerState::FirstBoundary, item: InnerMultipartItem::None, }), + error: None, } } - /// Create Multipart instance from MultipartError + /// Constructs a new multipart reader from given `MultipartError`. pub(crate) fn from_error(err: MultipartError) -> Multipart { Multipart { error: Some(err), @@ -111,6 +106,21 @@ impl Multipart { inner: None, } } + + /// Return requests parsed Content-Type or raise the stored error. + pub(crate) fn content_type_or_bail(&mut self) -> Result { + if let Some(err) = self.error.take() { + return Err(err); + } + + Ok(self + .inner + .as_ref() + // TODO: look into using enum instead of two options + .expect("multipart requests should have state") + .content_type + .clone()) + } } impl Stream for Multipart { @@ -141,8 +151,46 @@ impl Stream for Multipart { } } +#[derive(PartialEq, Debug)] +enum InnerState { + /// Stream EOF. + Eof, + + /// Skip data until first boundary. + FirstBoundary, + + /// Reading boundary. + Boundary, + + /// Reading Headers. + Headers, +} + +enum InnerMultipartItem { + None, + Field(Rc>), +} + +struct InnerMultipart { + /// 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: InnerState, + item: InnerMultipartItem, +} + impl InnerMultipart { - fn read_headers(payload: &mut PayloadBuffer) -> Result, MultipartError> { + fn read_field_headers( + payload: &mut PayloadBuffer, + ) -> Result, MultipartError> { match payload.read_until(b"\r\n\r\n")? { None => { if payload.eof { @@ -153,6 +201,7 @@ impl InnerMultipart { } Some(bytes) => { let mut hdrs = [httparse::EMPTY_HEADER; MAX_HEADERS]; + match httparse::parse_headers(&bytes, &mut hdrs) { Ok(httparse::Status::Complete((_, hdrs))) => { // convert headers @@ -193,7 +242,7 @@ impl InnerMultipart { || &chunk[..2] != b"--" || &chunk[2..boundary.len() + 2] != boundary.as_bytes() { - Err(MultipartError::Boundary) + Err(MultipartError::BoundaryMissing) } else if &chunk[boundary.len() + 2..] == b"\r\n" { Ok(Some(false)) } else if &chunk[boundary.len() + 2..boundary.len() + 4] == b"--" @@ -202,7 +251,7 @@ impl InnerMultipart { { Ok(Some(true)) } else { - Err(MultipartError::Boundary) + Err(MultipartError::BoundaryMissing) } } } @@ -217,7 +266,7 @@ impl InnerMultipart { match payload.readline()? { Some(chunk) => { if chunk.is_empty() { - return Err(MultipartError::Boundary); + return Err(MultipartError::BoundaryMissing); } if chunk.len() < boundary.len() { continue; @@ -282,7 +331,7 @@ impl InnerMultipart { } } - let headers = if let Some(mut payload) = self.payload.get_mut(safety) { + let field_headers = if let Some(mut payload) = self.payload.get_mut(safety) { match self.state { // read until first boundary InnerState::FirstBoundary => { @@ -317,7 +366,7 @@ impl InnerMultipart { // read field headers for next field if self.state == InnerState::Headers { - if let Some(headers) = InnerMultipart::read_headers(&mut payload)? { + if let Some(headers) = InnerMultipart::read_field_headers(&mut payload)? { self.state = InnerState::Boundary; headers } else { @@ -331,31 +380,37 @@ impl InnerMultipart { 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 + let field_content_disposition = field_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 + matches!( + content_disposition.disposition, + header::DispositionType::FormData, + ) }); - let cd = if let Some(content_disposition) = content_disposition { - content_disposition + 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(MultipartError::ContentDispositionMissing))); + }; + + let Some(field_name) = cd.get_name() else { + return Poll::Ready(Some(Err(MultipartError::ContentDispositionNameMissing))); + }; + + Some(field_name.to_owned()) } else { - return Poll::Ready(Some(Err(MultipartError::NoContentDisposition))); + None }; - let ct: Option = headers + // 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()); @@ -363,23 +418,24 @@ impl InnerMultipart { self.state = InnerState::Boundary; // nested multipart stream is not supported - if let Some(mime) = &ct { + if let Some(mime) = &field_content_type { 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)?; + let field_inner = + InnerField::new_in_rc(self.payload.clone(), self.boundary.clone(), &field_headers)?; - self.item = InnerMultipartItem::Field(Rc::clone(&field)); + self.item = InnerMultipartItem::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), - headers, - ct, - cd, - field, + field_inner, )))) } } @@ -392,26 +448,42 @@ impl Drop for InnerMultipart { } } -/// A single field in a multipart stream +/// A single field in a multipart stream. pub struct Field { - ct: Option, - cd: ContentDisposition, + /// 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, - inner: Rc>, + safety: Safety, + inner: Rc>, } impl Field { fn new( - safety: Safety, + content_type: Option, + content_disposition: Option, + form_field_name: Option, headers: HeaderMap, - ct: Option, - cd: ContentDisposition, + safety: Safety, inner: Rc>, ) -> Self { Field { - ct, - cd, + content_type, + content_disposition, + form_field_name: form_field_name.unwrap_or_default(), headers, inner, safety, @@ -428,34 +500,36 @@ impl Field { /// 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() + pub fn content_type(&self) -> Option<&Mime> { + self.content_type.as_ref() } - /// Returns the field's Content-Disposition. + /// Returns this field's parsed Content-Disposition header, if set. /// - /// 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." + /// # Validation /// - /// 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. + /// 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) -> &ContentDisposition { - &self.cd + pub fn content_disposition(&self) -> Option<&ContentDisposition> { + self.content_disposition.as_ref() } - /// Returns the field's name. + /// Returns the field's name, if set. /// - /// 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") + /// 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() } } @@ -465,6 +539,7 @@ impl Stream for Field { 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() @@ -486,7 +561,7 @@ impl Stream for Field { impl fmt::Debug for Field { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let Some(ct) = &self.ct { + if let Some(ct) = &self.content_type { writeln!(f, "\nField: {}", ct)?; } else { writeln!(f, "\nField:")?; @@ -570,6 +645,7 @@ impl InnerField { } /// 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, @@ -704,8 +780,8 @@ impl PayloadRef { } } - fn get_mut(&self, s: &Safety) -> Option> { - if s.current() { + fn get_mut(&self, safety: &Safety) -> Option> { + if safety.current() { Some(self.payload.borrow_mut()) } else { None @@ -722,10 +798,11 @@ impl Clone for PayloadRef { } /// 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. +/// +/// - 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, @@ -876,6 +953,7 @@ mod tests { test::TestRequest, FromRequest, }; + use assert_matches::assert_matches; use bytes::BufMut as _; use futures_util::{future::lazy, StreamExt as _}; use tokio::sync::mpsc; @@ -888,8 +966,8 @@ mod tests { #[actix_rt::test] async fn test_boundary() { let headers = HeaderMap::new(); - match Multipart::boundary(&headers) { - Err(MultipartError::NoContentType) => {} + match Multipart::find_ct_and_boundary(&headers) { + Err(MultipartError::ContentTypeMissing) => {} _ => unreachable!("should not happen"), } @@ -899,8 +977,8 @@ mod tests { header::HeaderValue::from_static("test"), ); - match Multipart::boundary(&headers) { - Err(MultipartError::ParseContentType) => {} + match Multipart::find_ct_and_boundary(&headers) { + Err(MultipartError::ContentTypeParse) => {} _ => unreachable!("should not happen"), } @@ -909,8 +987,8 @@ mod tests { header::CONTENT_TYPE, header::HeaderValue::from_static("multipart/mixed"), ); - match Multipart::boundary(&headers) { - Err(MultipartError::Boundary) => {} + match Multipart::find_ct_and_boundary(&headers) { + Err(MultipartError::BoundaryMissing) => {} _ => unreachable!("should not happen"), } @@ -923,8 +1001,8 @@ mod tests { ); assert_eq!( - Multipart::boundary(&headers).unwrap(), - "5c02368e880e436dab70ed54e1c58209" + Multipart::find_ct_and_boundary(&headers).unwrap().1, + "5c02368e880e436dab70ed54e1c58209", ); } @@ -1059,7 +1137,7 @@ mod tests { let mut multipart = Multipart::new(&headers, payload); match multipart.next().await { Some(Ok(mut field)) => { - let cd = field.content_disposition(); + let cd = field.content_disposition().unwrap(); assert_eq!(cd.disposition, DispositionType::FormData); assert_eq!(cd.parameters[0], DispositionParam::Name("file".into())); @@ -1121,7 +1199,7 @@ mod tests { let mut multipart = Multipart::new(&headers, payload); match multipart.next().await.unwrap() { Ok(mut field) => { - let cd = field.content_disposition(); + let cd = field.content_disposition().unwrap(); assert_eq!(cd.disposition, DispositionType::FormData); assert_eq!(cd.parameters[0], DispositionParam::Name("file".into())); @@ -1245,7 +1323,7 @@ mod tests { #[actix_rt::test] async fn test_multipart_from_error() { - let err = MultipartError::NoContentType; + let err = MultipartError::ContentTypeMissing; let mut multipart = Multipart::from_error(err); assert!(multipart.next().await.unwrap().is_err()) } @@ -1254,9 +1332,8 @@ mod tests { 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); + let (ct, boundary) = Multipart::find_ct_and_boundary(&headers).unwrap(); + let _ = Multipart::from_ct_and_boundary(ct, boundary, payload); } #[actix_rt::test] @@ -1278,11 +1355,43 @@ mod tests { } #[actix_rt::test] - async fn no_content_disposition() { + 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\nContent-Length: 4\r\n\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 = SlowStream::new(bytes); + + 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" + ), + MultipartError::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", ); @@ -1297,20 +1406,18 @@ mod tests { let mut multipart = Multipart::new(&headers, payload); let res = multipart.next().await.unwrap(); - assert!(res.is_err()); - assert!(matches!( - res.unwrap_err(), - MultipartError::NoContentDisposition, - )); + res.unwrap(); } #[actix_rt::test] - async fn no_name_in_content_disposition() { + 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\nContent-Length: 4\r\n\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + Content-Length: 4\r\n\ + \r\n\ test\r\n\ --abbc761f78ff4d7cb7573b5a23f96ef0\r\n", ); @@ -1318,18 +1425,17 @@ mod tests { headers.insert( header::CONTENT_TYPE, header::HeaderValue::from_static( - "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + "multipart/form-data; 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, - )); + assert_matches!( + res.expect_err("according to RFC 7578, form-data fields require a name attribute"), + MultipartError::ContentDispositionNameMissing + ); } #[actix_rt::test] @@ -1362,7 +1468,7 @@ mod tests { let mut field = multipart.next().await.unwrap().unwrap(); let task = rt::spawn(async move { - rt::time::sleep(Duration::from_secs(1)).await; + rt::time::sleep(Duration::from_millis(500)).await; assert_eq!(field.next().await.unwrap().unwrap(), "test"); drop(field); }); diff --git a/actix-multipart/src/test.rs b/actix-multipart/src/test.rs index 77d918283..828f37957 100644 --- a/actix-multipart/src/test.rs +++ b/actix-multipart/src/test.rs @@ -1,3 +1,5 @@ +//! Multipart testing utilities. + use actix_web::http::header::{self, HeaderMap}; use bytes::{BufMut as _, Bytes, BytesMut}; use mime::Mime; diff --git a/actix-web/src/http/header/content_disposition.rs b/actix-web/src/http/header/content_disposition.rs index 9725cd19b..824bf1195 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, } } From e189e4a3bf60edeff5b5259d4f60488d943eebec Mon Sep 17 00:00:00 2001 From: "Piperck(Zhinan)" Date: Mon, 1 Jul 2024 17:39:54 +0800 Subject: [PATCH 068/129] chore(awc): fix the issue where the code in the awc example cannot run (#3421) --- awc/examples/client.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/awc/examples/client.rs b/awc/examples/client.rs index 16ad330b8..41626315c 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")); From 2136e07bddb511a0f86174c76def3f39c2b66cdb Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 4 Jul 2024 00:26:10 +0100 Subject: [PATCH 069/129] refactor(multipart): move Safety to module --- actix-multipart/src/lib.rs | 1 + actix-multipart/src/safety.rs | 60 +++++++++++++++++++++++++++++++++ actix-multipart/src/server.rs | 63 ++--------------------------------- 3 files changed, 63 insertions(+), 61 deletions(-) create mode 100644 actix-multipart/src/safety.rs diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index 853648beb..63c2e890f 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -60,6 +60,7 @@ extern crate self as actix_multipart; mod error; mod extractor; pub mod form; +pub(crate) mod safety; mod server; pub mod test; diff --git a/actix-multipart/src/safety.rs b/actix-multipart/src/safety.rs new file mode 100644 index 000000000..db6b3b18b --- /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 index 76eae11ff..1a173f88a 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -1,9 +1,8 @@ //! Multipart response payload support. use std::{ - cell::{Cell, RefCell, RefMut}, + cell::{RefCell, RefMut}, cmp, fmt, - marker::PhantomData, pin::Pin, rc::Rc, task::{Context, Poll}, @@ -17,10 +16,9 @@ use actix_web::{ }; use bytes::{Bytes, BytesMut}; use futures_core::stream::{LocalBoxStream, Stream}; -use local_waker::LocalWaker; use mime::Mime; -use crate::error::MultipartError; +use crate::{error::MultipartError, safety::Safety}; const MAX_HEADERS: usize = 32; @@ -797,63 +795,6 @@ impl Clone for PayloadRef { } } -/// 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, From befb9c8196ffa124cefc533312b7361390e5d9b9 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 4 Jul 2024 00:37:25 +0100 Subject: [PATCH 070/129] refactor(multipart): move Payload* to module --- actix-multipart/Cargo.toml | 1 - actix-multipart/src/form/bytes.rs | 5 +- actix-multipart/src/form/json.rs | 3 +- actix-multipart/src/lib.rs | 1 + actix-multipart/src/payload.rs | 130 ++++++++++++++++++++++++++++++ actix-multipart/src/server.rs | 130 +++--------------------------- actix-multipart/src/test.rs | 6 +- 7 files changed, 147 insertions(+), 129 deletions(-) create mode 100644 actix-multipart/src/payload.rs diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index c5ff7dd10..e836a2c09 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -41,7 +41,6 @@ actix-multipart-derive = { version = "=0.6.1", optional = true } actix-utils = "3" actix-web = { version = "4", default-features = false } -bytes = "1" derive_more = "0.99.5" futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] } futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] } diff --git a/actix-multipart/src/form/bytes.rs b/actix-multipart/src/form/bytes.rs index c152db3d0..51b0cf7d9 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, diff --git a/actix-multipart/src/form/json.rs b/actix-multipart/src/form/json.rs index 3504c340c..0118a8fba 100644 --- a/actix-multipart/src/form/json.rs +++ b/actix-multipart/src/form/json.rs @@ -134,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/lib.rs b/actix-multipart/src/lib.rs index 63c2e890f..d61aa139b 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -60,6 +60,7 @@ extern crate self as actix_multipart; mod error; mod extractor; pub mod form; +pub(crate) mod payload; pub(crate) mod safety; mod server; pub mod test; diff --git a/actix-multipart/src/payload.rs b/actix-multipart/src/payload.rs new file mode 100644 index 000000000..d27cdbe05 --- /dev/null +++ b/actix-multipart/src/payload.rs @@ -0,0 +1,130 @@ +use std::{ + cell::{RefCell, RefMut}, + pin::Pin, + rc::Rc, + task::{Context, Poll}, +}; + +use actix_web::{ + error::PayloadError, + web::{Bytes, BytesMut}, +}; +use futures_core::stream::{LocalBoxStream, Stream}; + +use crate::{error::MultipartError, safety::Safety}; + +pub(crate) struct PayloadRef { + payload: Rc>, +} + +impl PayloadRef { + pub(crate) fn new(payload: PayloadBuffer) -> PayloadRef { + PayloadRef { + payload: Rc::new(payload.into()), + } + } + + 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) eof: bool, + pub(crate) buf: BytesMut, + pub(crate) stream: LocalBoxStream<'static, Result>, +} + +impl PayloadBuffer { + /// Constructs new `PayloadBuffer` instance. + pub(crate) fn new(stream: S) -> Self + where + S: Stream> + 'static, + { + PayloadBuffer { + eof: false, + buf: BytesMut::new(), + stream: Box::pin(stream), + } + } + + 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), + 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)] + 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, 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 + pub(crate) 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 + pub(crate) fn readline(&mut self) -> Result, MultipartError> { + self.read_until(b"\n") + } + + /// Read bytes until new line delimiter or eof + pub(crate) 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 + pub(crate) 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); + } +} diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs index 1a173f88a..bbd96621b 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -1,7 +1,7 @@ //! Multipart response payload support. use std::{ - cell::{RefCell, RefMut}, + cell::RefCell, cmp, fmt, pin::Pin, rc::Rc, @@ -12,13 +12,17 @@ use actix_web::{ dev, error::{ParseError, PayloadError}, http::header::{self, ContentDisposition, HeaderMap, HeaderName, HeaderValue}, + web::Bytes, HttpRequest, }; -use bytes::{Bytes, BytesMut}; -use futures_core::stream::{LocalBoxStream, Stream}; +use futures_core::stream::Stream; use mime::Mime; -use crate::{error::MultipartError, safety::Safety}; +use crate::{ + error::MultipartError, + payload::{PayloadBuffer, PayloadRef}, + safety::Safety, +}; const MAX_HEADERS: usize = 32; @@ -767,122 +771,6 @@ impl InnerField { } } -struct PayloadRef { - payload: Rc>, -} - -impl PayloadRef { - fn new(payload: PayloadBuffer) -> PayloadRef { - PayloadRef { - payload: Rc::new(payload.into()), - } - } - - 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. -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; @@ -892,10 +780,10 @@ mod tests { http::header::{DispositionParam, DispositionType}, rt, test::TestRequest, + web::{BufMut as _, BytesMut}, FromRequest, }; use assert_matches::assert_matches; - use bytes::BufMut as _; use futures_util::{future::lazy, StreamExt as _}; use tokio::sync::mpsc; use tokio_stream::wrappers::UnboundedReceiverStream; diff --git a/actix-multipart/src/test.rs b/actix-multipart/src/test.rs index 828f37957..956595355 100644 --- a/actix-multipart/src/test.rs +++ b/actix-multipart/src/test.rs @@ -1,7 +1,9 @@ //! Multipart testing utilities. -use actix_web::http::header::{self, HeaderMap}; -use bytes::{BufMut as _, Bytes, BytesMut}; +use actix_web::{ + http::header::{self, HeaderMap}, + web::{BufMut as _, Bytes, BytesMut}, +}; use mime::Mime; use rand::{ distributions::{Alphanumeric, DistString as _}, From 7326707599623d116bd9277e730a5a741fec1a4c Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 4 Jul 2024 00:40:25 +0100 Subject: [PATCH 071/129] refactor(multipart): move Field to module --- actix-multipart/src/field.rs | 343 ++++++++++++++++++++++++++++++++++ actix-multipart/src/lib.rs | 6 +- actix-multipart/src/server.rs | 324 +------------------------------- 3 files changed, 347 insertions(+), 326 deletions(-) create mode 100644 actix-multipart/src/field.rs diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs new file mode 100644 index 000000000..86fbc8b2d --- /dev/null +++ b/actix-multipart/src/field.rs @@ -0,0 +1,343 @@ +use std::{ + cell::RefCell, + cmp, fmt, + pin::Pin, + rc::Rc, + task::{Context, Poll}, +}; + +use actix_web::{ + error::PayloadError, + http::header::{self, ContentDisposition, HeaderMap}, + web::Bytes, +}; +use futures_core::stream::Stream; +use mime::Mime; + +use crate::{ + error::MultipartError, + payload::{PayloadBuffer, PayloadRef}, + safety::Safety, +}; + +/// 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() + } +} + +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(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.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, + boundary: String, + eof: bool, + 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(MultipartError::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(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()))) + }; + } + } + + pub(crate) fn poll(&mut self, safety: &Safety) -> Poll>> { + if self.payload.is_none() { + return Poll::Ready(None); + } + + let result = if let Some(mut payload) = self + .payload + .as_ref() + .expect("Field should not be polled after completion") + .get_mut(safety) + { + 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 { + // drop payload buffer and make future un-poll-able + let _ = self.payload.take(); + } + + result + } +} diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index d61aa139b..fefef3ffe 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -59,13 +59,11 @@ extern crate self as actix_multipart; mod error; mod extractor; +pub(crate) mod field; pub mod form; pub(crate) mod payload; pub(crate) mod safety; mod server; pub mod test; -pub use self::{ - error::MultipartError, - server::{Field, Multipart}, -}; +pub use self::{error::MultipartError, field::Field, server::Multipart}; diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs index bbd96621b..67232a82b 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -2,7 +2,6 @@ use std::{ cell::RefCell, - cmp, fmt, pin::Pin, rc::Rc, task::{Context, Poll}, @@ -20,8 +19,10 @@ use mime::Mime; use crate::{ error::MultipartError, + field::InnerField, payload::{PayloadBuffer, PayloadRef}, safety::Safety, + Field, }; const MAX_HEADERS: usize = 32; @@ -450,327 +451,6 @@ impl Drop for InnerMultipart { } } -/// 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 { - 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() - } -} - -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(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.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(()) - } -} - -struct InnerField { - /// Payload is initialized as Some and is `take`n when the field stream finishes. - 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() - .expect("Field should not be polled after completion") - .get_mut(s) - { - if !self.eof { - let res = if let Some(ref mut len) = self.length { - InnerField::read_len(&mut payload, len) - } 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 { - // drop payload buffer and make future un-poll-able - let _ = self.payload.take(); - } - - result - } -} - #[cfg(test)] mod tests { use std::time::Duration; From 00c185f617ea08070117942e23b230b28ace561b Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 4 Jul 2024 01:12:17 +0100 Subject: [PATCH 072/129] refactor(multipart): move lints to manifest --- actix-multipart/Cargo.toml | 5 ++ actix-multipart/src/lib.rs | 3 -- actix-multipart/src/payload.rs | 13 ++--- actix-multipart/src/server.rs | 99 +++++++++++++++++----------------- 4 files changed, 61 insertions(+), 59 deletions(-) diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index e836a2c09..e5d1b5b1d 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -69,3 +69,8 @@ futures-util = { version = "0.3.17", default-features = false, features = ["allo multer = "3" tokio = { version = "1.24.2", features = ["sync"] } tokio-stream = "0.1" + +[lints.rust] +future_incompatible = { level = "deny" } +rust_2018_idioms = { level = "deny" } +nonstandard_style = { level = "deny" } diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index fefef3ffe..d33f17097 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -46,9 +46,6 @@ //! -F file=@./Cargo.lock //! ``` -#![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))] diff --git a/actix-multipart/src/payload.rs b/actix-multipart/src/payload.rs index d27cdbe05..a798f2c1a 100644 --- a/actix-multipart/src/payload.rs +++ b/actix-multipart/src/payload.rs @@ -1,5 +1,6 @@ use std::{ cell::{RefCell, RefMut}, + cmp, pin::Pin, rc::Rc, task::{Context, Poll}, @@ -75,7 +76,7 @@ impl PayloadBuffer { } } - /// Read exact number of bytes + /// Read exact number of bytes. #[cfg(test)] pub(crate) fn read_exact(&mut self, size: usize) -> Option { if size <= self.buf.len() { @@ -87,7 +88,7 @@ impl PayloadBuffer { pub(crate) 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; + 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(MultipartError::Incomplete) @@ -96,7 +97,7 @@ impl PayloadBuffer { } } - /// Read until specified ending + /// Read until specified ending. pub(crate) 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()); @@ -108,12 +109,12 @@ impl PayloadBuffer { } } - /// Read bytes until new line delimiter + /// Read bytes until new line delimiter. pub(crate) fn readline(&mut self) -> Result, MultipartError> { self.read_until(b"\n") } - /// Read bytes until new line delimiter or eof + /// Read bytes until new line delimiter or EOF. pub(crate) fn readline_or_eof(&mut self) -> Result, MultipartError> { match self.readline() { Err(MultipartError::Incomplete) if self.eof => Ok(Some(self.buf.split().freeze())), @@ -121,7 +122,7 @@ impl PayloadBuffer { } } - /// Put unprocessed data back to the buffer + /// Put unprocessed data back to the buffer. pub(crate) fn unprocessed(&mut self, data: Bytes) { let buf = BytesMut::from(data.as_ref()); let buf = std::mem::replace(&mut self.buf, buf); diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs index 67232a82b..d0ed5be59 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -34,7 +34,7 @@ const MAX_HEADERS: usize = 32; /// used for nested multipart streams. pub struct Multipart { safety: Safety, - inner: Option, + inner: Option, error: Option, } @@ -90,12 +90,12 @@ impl Multipart { { Multipart { safety: Safety::new(), - inner: Some(InnerMultipart { + inner: Some(Inner { payload: PayloadRef::new(PayloadBuffer::new(stream)), content_type: ct, boundary, - state: InnerState::FirstBoundary, - item: InnerMultipartItem::None, + state: State::FirstBoundary, + item: Item::None, }), error: None, } @@ -155,10 +155,7 @@ impl Stream for Multipart { } #[derive(PartialEq, Debug)] -enum InnerState { - /// Stream EOF. - Eof, - +enum State { /// Skip data until first boundary. FirstBoundary, @@ -167,14 +164,17 @@ enum InnerState { /// Reading Headers. Headers, + + /// Stream EOF. + Eof, } -enum InnerMultipartItem { +enum Item { None, Field(Rc>), } -struct InnerMultipart { +struct Inner { /// Request's payload stream & buffer. payload: PayloadRef, @@ -186,11 +186,11 @@ struct InnerMultipart { /// Field boundary. boundary: String, - state: InnerState, - item: InnerMultipartItem, + state: State, + item: Item, } -impl InnerMultipart { +impl Inner { fn read_field_headers( payload: &mut PayloadBuffer, ) -> Result, MultipartError> { @@ -265,6 +265,7 @@ impl InnerMultipart { boundary: &str, ) -> Result, MultipartError> { let mut eof = false; + loop { match payload.readline()? { Some(chunk) => { @@ -306,7 +307,7 @@ impl InnerMultipart { safety: &Safety, cx: &Context<'_>, ) -> Poll>> { - if self.state == InnerState::Eof { + if self.state == State::Eof { Poll::Ready(None) } else { // release field @@ -315,20 +316,18 @@ impl InnerMultipart { // 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, + 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 = InnerMultipartItem::None; + self.item = Item::None; } - if let InnerMultipartItem::None = self.item { + if let Item::None = self.item { break; } } @@ -337,40 +336,40 @@ impl InnerMultipart { let field_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)? { + State::FirstBoundary => { + match Inner::skip_until_boundary(&mut payload, &self.boundary)? { Some(eof) => { if eof { - self.state = InnerState::Eof; + self.state = State::Eof; return Poll::Ready(None); } else { - self.state = InnerState::Headers; + self.state = State::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; - } + 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 == InnerState::Headers { - if let Some(headers) = InnerMultipart::read_field_headers(&mut payload)? { - self.state = InnerState::Boundary; + if self.state == State::Headers { + if let Some(headers) = Inner::read_field_headers(&mut payload)? { + self.state = State::Boundary; headers } else { return Poll::Pending; @@ -418,7 +417,7 @@ impl InnerMultipart { .and_then(|ct| ct.to_str().ok()) .and_then(|ct| ct.parse().ok()); - self.state = InnerState::Boundary; + self.state = State::Boundary; // nested multipart stream is not supported if let Some(mime) = &field_content_type { @@ -430,7 +429,7 @@ impl InnerMultipart { let field_inner = InnerField::new_in_rc(self.payload.clone(), self.boundary.clone(), &field_headers)?; - self.item = InnerMultipartItem::Field(Rc::clone(&field_inner)); + self.item = Item::Field(Rc::clone(&field_inner)); Poll::Ready(Some(Ok(Field::new( field_content_type, @@ -444,10 +443,10 @@ impl InnerMultipart { } } -impl Drop for InnerMultipart { +impl Drop for Inner { fn drop(&mut self) { // InnerMultipartItem::Field has to be dropped first because of Safety. - self.item = InnerMultipartItem::None; + self.item = Item::None; } } @@ -772,7 +771,7 @@ mod tests { } #[actix_rt::test] - async fn test_readmax() { + async fn read_max() { let (mut sender, payload) = h1::Payload::create(false); let mut payload = PayloadBuffer::new(payload); @@ -789,7 +788,7 @@ mod tests { } #[actix_rt::test] - async fn test_readexactly() { + async fn read_exactly() { let (mut sender, payload) = h1::Payload::create(false); let mut payload = PayloadBuffer::new(payload); @@ -807,7 +806,7 @@ mod tests { } #[actix_rt::test] - async fn test_readuntil() { + async fn read_until() { let (mut sender, payload) = h1::Payload::create(false); let mut payload = PayloadBuffer::new(payload); From 210c9a5eb3be0995e9df097a1079e43f87909168 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 4 Jul 2024 04:53:10 +0100 Subject: [PATCH 073/129] refactor: multipart tweaks --- actix-multipart/src/error.rs | 10 +- actix-multipart/src/extractor.rs | 6 +- actix-multipart/src/field.rs | 16 +-- actix-multipart/src/lib.rs | 2 +- actix-multipart/src/payload.rs | 72 +++++++----- actix-multipart/src/server.rs | 195 +++++++++++++++++-------------- actix-multipart/src/test.rs | 3 +- 7 files changed, 169 insertions(+), 135 deletions(-) diff --git a/actix-multipart/src/error.rs b/actix-multipart/src/error.rs index 30ef63c1a..cdb608738 100644 --- a/actix-multipart/src/error.rs +++ b/actix-multipart/src/error.rs @@ -10,7 +10,7 @@ use derive_more::{Display, Error, From}; /// A set of errors that can occur during parsing multipart streams. #[derive(Debug, Display, From, Error)] #[non_exhaustive] -pub enum MultipartError { +pub enum Error { /// Could not find Content-Type header. #[display(fmt = "Could not find Content-Type header")] ContentTypeMissing, @@ -95,11 +95,11 @@ pub enum MultipartError { } /// Return `BadRequest` for `MultipartError`. -impl ResponseError for MultipartError { +impl ResponseError for Error { fn status_code(&self) -> StatusCode { match &self { - MultipartError::Field { source, .. } => source.as_response_error().status_code(), - MultipartError::ContentTypeIncompatible => StatusCode::UNSUPPORTED_MEDIA_TYPE, + Error::Field { source, .. } => source.as_response_error().status_code(), + Error::ContentTypeIncompatible => StatusCode::UNSUPPORTED_MEDIA_TYPE, _ => StatusCode::BAD_REQUEST, } } @@ -111,7 +111,7 @@ mod tests { #[test] fn test_multipart_error() { - let resp = MultipartError::BoundaryMissing.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 ab98f887e..f7777100e 100644 --- a/actix-multipart/src/extractor.rs +++ b/actix-multipart/src/extractor.rs @@ -12,11 +12,11 @@ use crate::server::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?; @@ -27,7 +27,7 @@ use crate::server::Multipart; /// } /// } /// -/// Ok(HttpResponse::Ok().into()) +/// Ok(HttpResponse::Ok().finish()) /// } /// ``` impl FromRequest for Multipart { diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs index 86fbc8b2d..50660b5d3 100644 --- a/actix-multipart/src/field.rs +++ b/actix-multipart/src/field.rs @@ -15,7 +15,7 @@ use futures_core::stream::Stream; use mime::Mime; use crate::{ - error::MultipartError, + error::Error, payload::{PayloadBuffer, PayloadRef}, safety::Safety, }; @@ -106,7 +106,7 @@ impl Field { } impl Stream for Field { - type Item = Result; + type Item = Result; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let this = self.get_mut(); @@ -122,7 +122,7 @@ impl Stream for Field { buffer.poll_stream(cx)?; } else if !this.safety.is_clean() { // safety violation - return Poll::Ready(Some(Err(MultipartError::NotConsumed))); + return Poll::Ready(Some(Err(Error::NotConsumed))); } else { return Poll::Pending; } @@ -192,7 +192,7 @@ impl InnerField { pub(crate) fn read_len( payload: &mut PayloadBuffer, size: &mut u64, - ) -> Poll>> { + ) -> Poll>> { if *size == 0 { Poll::Ready(None) } else { @@ -208,7 +208,7 @@ impl InnerField { } None => { if payload.eof && (*size != 0) { - Poll::Ready(Some(Err(MultipartError::Incomplete))) + Poll::Ready(Some(Err(Error::Incomplete))) } else { Poll::Pending } @@ -223,13 +223,13 @@ impl InnerField { pub(crate) fn read_stream( payload: &mut PayloadBuffer, boundary: &str, - ) -> Poll>> { + ) -> Poll>> { let mut pos = 0; let len = payload.buf.len(); if len == 0 { return if payload.eof { - Poll::Ready(Some(Err(MultipartError::Incomplete))) + Poll::Ready(Some(Err(Error::Incomplete))) } else { Poll::Pending }; @@ -293,7 +293,7 @@ impl InnerField { } } - pub(crate) fn poll(&mut self, safety: &Safety) -> Poll>> { + pub(crate) fn poll(&mut self, safety: &Safety) -> Poll>> { if self.payload.is_none() { return Poll::Ready(None); } diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index d33f17097..744c27088 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -63,4 +63,4 @@ pub(crate) mod safety; mod server; pub mod test; -pub use self::{error::MultipartError, field::Field, server::Multipart}; +pub use self::{error::Error as MultipartError, field::Field, server::Multipart}; diff --git a/actix-multipart/src/payload.rs b/actix-multipart/src/payload.rs index a798f2c1a..ed5477997 100644 --- a/actix-multipart/src/payload.rs +++ b/actix-multipart/src/payload.rs @@ -1,6 +1,6 @@ use std::{ cell::{RefCell, RefMut}, - cmp, + cmp, mem, pin::Pin, rc::Rc, task::{Context, Poll}, @@ -12,7 +12,7 @@ use actix_web::{ }; use futures_core::stream::{LocalBoxStream, Stream}; -use crate::{error::MultipartError, safety::Safety}; +use crate::{error::Error, safety::Safety}; pub(crate) struct PayloadRef { payload: Rc>, @@ -21,7 +21,7 @@ pub(crate) struct PayloadRef { impl PayloadRef { pub(crate) fn new(payload: PayloadBuffer) -> PayloadRef { PayloadRef { - payload: Rc::new(payload.into()), + payload: Rc::new(RefCell::new(payload)), } } @@ -44,28 +44,33 @@ impl Clone for PayloadRef { /// Payload buffer. pub(crate) struct PayloadBuffer { - pub(crate) eof: bool, - pub(crate) buf: BytesMut, 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 `PayloadBuffer` instance. + /// Constructs new payload buffer. pub(crate) fn new(stream: S) -> Self where S: Stream> + 'static, { PayloadBuffer { - eof: false, - buf: BytesMut::new(), 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), + 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; @@ -76,7 +81,7 @@ impl PayloadBuffer { } } - /// Read exact number of bytes. + /// Reads exact number of bytes. #[cfg(test)] pub(crate) fn read_exact(&mut self, size: usize) -> Option { if size <= self.buf.len() { @@ -86,46 +91,57 @@ impl PayloadBuffer { } } - pub(crate) fn read_max(&mut self, size: u64) -> Result, MultipartError> { + 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(MultipartError::Incomplete) + Err(Error::Incomplete) } else { Ok(None) } } - /// Read until specified ending. - pub(crate) 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()); + /// 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), - if res.is_none() && self.eof { - Err(MultipartError::Incomplete) - } else { - Ok(res) + // 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())), } } - /// Read bytes until new line delimiter. - pub(crate) fn readline(&mut self) -> Result, MultipartError> { + /// Reads bytes until new line delimiter. + #[inline] + pub(crate) fn readline(&mut self) -> Result, Error> { self.read_until(b"\n") } - /// Read bytes until new line delimiter or EOF. - pub(crate) fn readline_or_eof(&mut self) -> Result, MultipartError> { + /// Reads bytes until new line delimiter or until EOF. + #[inline] + pub(crate) fn readline_or_eof(&mut self) -> Result, Error> { match self.readline() { - Err(MultipartError::Incomplete) if self.eof => Ok(Some(self.buf.split().freeze())), + Err(Error::Incomplete) if self.eof => Ok(Some(self.buf.split().freeze())), line => line, } } - /// Put unprocessed data back to the buffer. + /// Puts unprocessed data back to the buffer. pub(crate) fn unprocessed(&mut self, data: Bytes) { - let buf = BytesMut::from(data.as_ref()); - let buf = std::mem::replace(&mut self.buf, buf); + // 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); } } diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs index d0ed5be59..dc6a9ecb7 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -18,7 +18,7 @@ use futures_core::stream::Stream; use mime::Mime; use crate::{ - error::MultipartError, + error::Error, field::InnerField, payload::{PayloadBuffer, PayloadRef}, safety::Safety, @@ -33,9 +33,15 @@ const MAX_HEADERS: usize = 32; /// implementation. `MultipartItem::Field` contains multipart field. `MultipartItem::Multipart` is /// used for nested multipart streams. pub struct Multipart { + flow: Flow, safety: Safety, - inner: Option, - error: Option, +} + +enum Flow { + InFlight(Inner), + + /// Error container is Some until an error is returned out of the flow. + Error(Option), } impl Multipart { @@ -59,24 +65,22 @@ impl Multipart { } /// Extract Content-Type and boundary info from headers. - pub(crate) fn find_ct_and_boundary( - headers: &HeaderMap, - ) -> Result<(Mime, String), MultipartError> { + pub(crate) fn find_ct_and_boundary(headers: &HeaderMap) -> Result<(Mime, String), Error> { let content_type = headers .get(&header::CONTENT_TYPE) - .ok_or(MultipartError::ContentTypeMissing)? + .ok_or(Error::ContentTypeMissing)? .to_str() .ok() .and_then(|content_type| content_type.parse::().ok()) - .ok_or(MultipartError::ContentTypeParse)?; + .ok_or(Error::ContentTypeParse)?; if content_type.type_() != mime::MULTIPART { - return Err(MultipartError::ContentTypeIncompatible); + return Err(Error::ContentTypeIncompatible); } let boundary = content_type .get_param(mime::BOUNDARY) - .ok_or(MultipartError::BoundaryMissing)? + .ok_or(Error::BoundaryMissing)? .as_str() .to_owned(); @@ -90,64 +94,57 @@ impl Multipart { { Multipart { safety: Safety::new(), - inner: Some(Inner { + flow: Flow::InFlight(Inner { payload: PayloadRef::new(PayloadBuffer::new(stream)), content_type: ct, boundary, state: State::FirstBoundary, item: Item::None, }), - error: None, } } /// Constructs a new multipart reader from given `MultipartError`. - pub(crate) fn from_error(err: MultipartError) -> Multipart { + pub(crate) fn from_error(err: Error) -> Multipart { Multipart { - error: Some(err), + flow: Flow::Error(Some(err)), safety: Safety::new(), - inner: None, } } /// Return requests parsed Content-Type or raise the stored error. - pub(crate) fn content_type_or_bail(&mut self) -> Result { - if let Some(err) = self.error.take() { - return Err(err); + 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")), } - - Ok(self - .inner - .as_ref() - // TODO: look into using enum instead of two options - .expect("multipart requests should have state") - .content_type - .clone()) } } impl Stream for Multipart { - type Item = Result; + 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) => { + 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(MultipartError::NotConsumed))); + return Poll::Ready(Some(Err(Error::NotConsumed))); } else { return Poll::Pending; } inner.poll(&this.safety, cx) } - None => Poll::Ready(Some(Err(this - .error + + Flow::Error(ref mut err) => Poll::Ready(Some(Err(err .take() .expect("Multipart polled after finish")))), } @@ -191,22 +188,21 @@ struct Inner { } impl Inner { - fn read_field_headers( - payload: &mut PayloadBuffer, - ) -> Result, MultipartError> { + fn read_field_headers(payload: &mut PayloadBuffer) -> Result, Error> { match payload.read_until(b"\r\n\r\n")? { None => { if payload.eof { - Err(MultipartError::Incomplete) + Err(Error::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))) => { + 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()); @@ -220,57 +216,84 @@ impl Inner { Ok(Some(headers)) } - Ok(httparse::Status::Partial) => Err(ParseError::Header.into()), - Err(err) => Err(ParseError::from(err).into()), + + httparse::Status::Partial => Err(ParseError::Header.into()), } } } } - fn read_boundary( - payload: &mut PayloadBuffer, - boundary: &str, - ) -> Result, MultipartError> { + /// 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 - 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::BoundaryMissing) - } 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::BoundaryMissing) - } - } + 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, MultipartError> { + ) -> Result, Error> { let mut eof = false; loop { match payload.readline()? { Some(chunk) => { if chunk.is_empty() { - return Err(MultipartError::BoundaryMissing); + return Err(Error::BoundaryMissing); } if chunk.len() < boundary.len() { continue; @@ -292,7 +315,7 @@ impl Inner { } None => { return if payload.eof { - Err(MultipartError::Incomplete) + Err(Error::Incomplete) } else { Ok(None) }; @@ -302,11 +325,7 @@ impl Inner { Ok(Some(eof)) } - fn poll( - &mut self, - safety: &Safety, - cx: &Context<'_>, - ) -> Poll>> { + fn poll(&mut self, safety: &Safety, cx: &Context<'_>) -> Poll>> { if self.state == State::Eof { Poll::Ready(None) } else { @@ -338,6 +357,7 @@ impl Inner { // 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; @@ -346,7 +366,6 @@ impl Inner { self.state = State::Headers; } } - None => return Poll::Pending, } } @@ -398,11 +417,11 @@ impl Inner { // 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(MultipartError::ContentDispositionMissing))); + return Poll::Ready(Some(Err(Error::ContentDispositionMissing))); }; let Some(field_name) = cd.get_name() else { - return Poll::Ready(Some(Err(MultipartError::ContentDispositionNameMissing))); + return Poll::Ready(Some(Err(Error::ContentDispositionNameMissing))); }; Some(field_name.to_owned()) @@ -422,7 +441,7 @@ impl Inner { // nested multipart stream is not supported if let Some(mime) = &field_content_type { if mime.type_() == mime::MULTIPART { - return Poll::Ready(Some(Err(MultipartError::Nested))); + return Poll::Ready(Some(Err(Error::Nested))); } } @@ -475,7 +494,7 @@ mod tests { async fn test_boundary() { let headers = HeaderMap::new(); match Multipart::find_ct_and_boundary(&headers) { - Err(MultipartError::ContentTypeMissing) => {} + Err(Error::ContentTypeMissing) => {} _ => unreachable!("should not happen"), } @@ -486,7 +505,7 @@ mod tests { ); match Multipart::find_ct_and_boundary(&headers) { - Err(MultipartError::ContentTypeParse) => {} + Err(Error::ContentTypeParse) => {} _ => unreachable!("should not happen"), } @@ -496,7 +515,7 @@ mod tests { header::HeaderValue::from_static("multipart/mixed"), ); match Multipart::find_ct_and_boundary(&headers) { - Err(MultipartError::BoundaryMissing) => {} + Err(Error::BoundaryMissing) => {} _ => unreachable!("should not happen"), } @@ -831,7 +850,7 @@ mod tests { #[actix_rt::test] async fn test_multipart_from_error() { - let err = MultipartError::ContentTypeMissing; + let err = Error::ContentTypeMissing; let mut multipart = Multipart::from_error(err); assert!(multipart.next().await.unwrap().is_err()) } @@ -888,7 +907,7 @@ mod tests { res.expect_err( "according to RFC 7578, form-data fields require a content-disposition header" ), - MultipartError::ContentDispositionMissing + Error::ContentDispositionMissing ); } @@ -942,7 +961,7 @@ mod tests { let res = multipart.next().await.unwrap(); assert_matches!( res.expect_err("according to RFC 7578, form-data fields require a name attribute"), - MultipartError::ContentDispositionNameMissing + Error::ContentDispositionNameMissing ); } @@ -960,7 +979,7 @@ mod tests { // should fail immediately match field.next().await { - Some(Err(MultipartError::NotConsumed)) => {} + Some(Err(Error::NotConsumed)) => {} _ => panic!(), }; } diff --git a/actix-multipart/src/test.rs b/actix-multipart/src/test.rs index 956595355..7dec85f8e 100644 --- a/actix-multipart/src/test.rs +++ b/actix-multipart/src/test.rs @@ -25,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( From 611154beb29cc54698ba8ec390f4bab661eca0a6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 4 Jul 2024 05:03:42 +0100 Subject: [PATCH 074/129] refactor: rename multipart module --- actix-multipart/src/extractor.rs | 8 +++----- actix-multipart/src/lib.rs | 4 ++-- actix-multipart/src/{server.rs => multipart.rs} | 0 3 files changed, 5 insertions(+), 7 deletions(-) rename actix-multipart/src/{server.rs => multipart.rs} (100%) diff --git a/actix-multipart/src/extractor.rs b/actix-multipart/src/extractor.rs index f7777100e..31999228e 100644 --- a/actix-multipart/src/extractor.rs +++ b/actix-multipart/src/extractor.rs @@ -1,13 +1,11 @@ -//! 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 /// diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index 744c27088..ac07a172a 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -58,9 +58,9 @@ mod error; mod extractor; pub(crate) mod field; pub mod form; +mod multipart; pub(crate) mod payload; pub(crate) mod safety; -mod server; pub mod test; -pub use self::{error::Error as MultipartError, field::Field, server::Multipart}; +pub use self::{error::Error as MultipartError, field::Field, multipart::Multipart}; diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/multipart.rs similarity index 100% rename from actix-multipart/src/server.rs rename to actix-multipart/src/multipart.rs From 5c9e6e7c1d43d29c5b51638e198ae238ef5c51f7 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 6 Jul 2024 22:58:54 +0100 Subject: [PATCH 075/129] feat(multipart): add field bytes method --- actix-multipart/CHANGES.md | 1 + actix-multipart/src/field.rs | 155 ++++++++++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 4 deletions(-) diff --git a/actix-multipart/CHANGES.md b/actix-multipart/CHANGES.md index dea24ab9d..86cd26060 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -4,6 +4,7 @@ - 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. diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs index 50660b5d3..6bb1e5265 100644 --- a/actix-multipart/src/field.rs +++ b/actix-multipart/src/field.rs @@ -1,17 +1,19 @@ use std::{ cell::RefCell, - cmp, fmt, + cmp, fmt, mem, pin::Pin, rc::Rc, - task::{Context, Poll}, + task::{ready, Context, Poll}, }; +use actix_utils::future::poll_fn; use actix_web::{ error::PayloadError, http::header::{self, ContentDisposition, HeaderMap}, - web::Bytes, + web::{Bytes, BytesMut}, }; -use futures_core::stream::Stream; +use derive_more::{Display, Error}; +use futures_core::Stream; use mime::Mime; use crate::{ @@ -20,6 +22,10 @@ use crate::{ safety::Safety, }; +#[derive(Debug, Display, Error)] +#[display(fmt = "limit exceeded")] +pub struct LimitExceeded; + /// A single field in a multipart stream. pub struct Field { /// Field's Content-Type. @@ -103,6 +109,56 @@ impl 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 { @@ -341,3 +397,94 @@ impl InnerField { 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"); + } +} From 6ae131ce298330c7388b426ec06e7a5969023b75 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 6 Jul 2024 23:38:37 +0100 Subject: [PATCH 076/129] test(multipart): replace SlowStream helper --- actix-multipart/Cargo.toml | 1 + actix-multipart/src/multipart.rs | 58 ++++++++------------------------ 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index e5d1b5b1d..19761bb2e 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -66,6 +66,7 @@ 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" diff --git a/actix-multipart/src/multipart.rs b/actix-multipart/src/multipart.rs index dc6a9ecb7..e511cb6df 100644 --- a/actix-multipart/src/multipart.rs +++ b/actix-multipart/src/multipart.rs @@ -482,7 +482,8 @@ mod tests { FromRequest, }; use assert_matches::assert_matches; - use futures_util::{future::lazy, StreamExt as _}; + use futures_test::stream::StreamTestExt as _; + use futures_util::{future::lazy, stream, StreamExt as _}; use tokio::sync::mpsc; use tokio_stream::wrappers::UnboundedReceiverStream; @@ -545,45 +546,6 @@ mod tests { ) } - // 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, @@ -721,7 +683,9 @@ mod tests { #[actix_rt::test] async fn test_stream() { let (bytes, headers) = create_double_request_with_header(); - let payload = SlowStream::new(bytes); + 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() { @@ -899,7 +863,9 @@ mod tests { "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", ), ); - let payload = SlowStream::new(bytes); + 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(); @@ -929,7 +895,9 @@ mod tests { "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", ), ); - let payload = SlowStream::new(bytes); + 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(); @@ -955,7 +923,9 @@ mod tests { "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", ), ); - let payload = SlowStream::new(bytes); + 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(); From 01d60f331586d036a0ccee516abc7b6208f2ecbd Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 00:05:53 +0100 Subject: [PATCH 077/129] chore(actix-multipart): prepare release 0.7.0 --- actix-multipart-derive/Cargo.toml | 2 +- actix-multipart/CHANGES.md | 2 ++ actix-multipart/Cargo.toml | 2 +- actix-multipart/README.md | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/actix-multipart-derive/Cargo.toml b/actix-multipart-derive/Cargo.toml index e978864a3..35cbcc130 100644 --- a/actix-multipart-derive/Cargo.toml +++ b/actix-multipart-derive/Cargo.toml @@ -25,7 +25,7 @@ quote = "1" syn = "2" [dev-dependencies] -actix-multipart = "0.6" +actix-multipart = "0.7" actix-web = "4" rustversion = "1" trybuild = "1" diff --git a/actix-multipart/CHANGES.md b/actix-multipart/CHANGES.md index 86cd26060..e9d1314e5 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 0.7.0 + - Add `MultipartError::ContentTypeIncompatible` variant. - Add `MultipartError::ContentDispositionNameMissing` variant. - Add `Field::bytes()` method. diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index 19761bb2e..61a9b5089 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-multipart" -version = "0.6.2" +version = "0.7.0" authors = [ "Nikolay Kim ", "Jacob Halsey ", diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 917dceece..0356976b2 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -3,11 +3,11 @@ [![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.2)](https://docs.rs/actix-multipart/0.6.2) +[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.7.0)](https://docs.rs/actix-multipart/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.svg)
-[![dependency status](https://deps.rs/crate/actix-multipart/0.6.2/status.svg)](https://deps.rs/crate/actix-multipart/0.6.2) +[![dependency status](https://deps.rs/crate/actix-multipart/0.7.0/status.svg)](https://deps.rs/crate/actix-multipart/0.7.0) [![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) From ffee672909db5db809d09f6b67ff52352505107b Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 00:19:22 +0100 Subject: [PATCH 078/129] chore(actix-multipart): prepare release 0.7.1 --- actix-multipart/CHANGES.md | 4 ++++ actix-multipart/Cargo.toml | 2 +- actix-multipart/README.md | 4 ++-- actix-multipart/src/field.rs | 4 +++- actix-multipart/src/lib.rs | 6 +++++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/actix-multipart/CHANGES.md b/actix-multipart/CHANGES.md index e9d1314e5..adc568253 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +## 0.7.1 + +- Expose `LimitExceeded` error type. + ## 0.7.0 - Add `MultipartError::ContentTypeIncompatible` variant. diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index 61a9b5089..f567d2d7c 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-multipart" -version = "0.7.0" +version = "0.7.1" authors = [ "Nikolay Kim ", "Jacob Halsey ", diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 0356976b2..20673d524 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -3,11 +3,11 @@ [![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.7.0)](https://docs.rs/actix-multipart/0.7.0) +[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.7.1)](https://docs.rs/actix-multipart/0.7.1) ![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.7.0/status.svg)](https://deps.rs/crate/actix-multipart/0.7.0) +[![dependency status](https://deps.rs/crate/actix-multipart/0.7.1/status.svg)](https://deps.rs/crate/actix-multipart/0.7.1) [![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) diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs index 6bb1e5265..0e4167c8e 100644 --- a/actix-multipart/src/field.rs +++ b/actix-multipart/src/field.rs @@ -22,8 +22,10 @@ use crate::{ safety::Safety, }; +/// Error type returned from [`Field::bytes()`] when field data is larger than limit. #[derive(Debug, Display, Error)] -#[display(fmt = "limit exceeded")] +#[display(fmt = "size limit exceeded while collecting field data")] +#[non_exhaustive] pub struct LimitExceeded; /// A single field in a multipart stream. diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index ac07a172a..a56b9846b 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -63,4 +63,8 @@ pub(crate) mod payload; pub(crate) mod safety; pub mod test; -pub use self::{error::Error as MultipartError, field::Field, multipart::Multipart}; +pub use self::{ + error::Error as MultipartError, + field::{Field, LimitExceeded}, + multipart::Multipart, +}; From 215a294584fc2d209aef974a2e3b0759c1488c08 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 00:30:27 +0100 Subject: [PATCH 079/129] chore(actix-multipart-derive): prepare release 0.7.0 --- actix-multipart-derive/CHANGES.md | 2 ++ actix-multipart-derive/Cargo.toml | 2 +- actix-multipart-derive/README.md | 4 ++-- actix-multipart/CHANGES.md | 2 ++ actix-multipart/Cargo.toml | 2 +- actix-multipart/src/field.rs | 5 +++-- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/actix-multipart-derive/CHANGES.md b/actix-multipart-derive/CHANGES.md index 1b44ba4b7..d0c759297 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 35cbcc130..33eacd460 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"] diff --git a/actix-multipart-derive/README.md b/actix-multipart-derive/README.md index ec0afffdd..bf75613ed 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/CHANGES.md b/actix-multipart/CHANGES.md index adc568253..201502691 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Fix re-exported version of `actix-multipart-derive`. + ## 0.7.1 - Expose `LimitExceeded` error type. diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index f567d2d7c..5d81f6973 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -37,7 +37,7 @@ 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 } diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs index 0e4167c8e..652b3e8b7 100644 --- a/actix-multipart/src/field.rs +++ b/actix-multipart/src/field.rs @@ -1,12 +1,13 @@ use std::{ cell::RefCell, - cmp, fmt, mem, + cmp, fmt, + future::poll_fn, + mem, pin::Pin, rc::Rc, task::{ready, Context, Poll}, }; -use actix_utils::future::poll_fn; use actix_web::{ error::PayloadError, http::header::{self, ContentDisposition, HeaderMap}, From b01fbddba484a52309d3fd50dafd862e29684453 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 00:34:18 +0100 Subject: [PATCH 080/129] chore(actix-multipart): prepare release 0.7.2 --- actix-multipart/CHANGES.md | 2 ++ actix-multipart/Cargo.toml | 2 +- actix-multipart/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/actix-multipart/CHANGES.md b/actix-multipart/CHANGES.md index 201502691..c3f3b6e39 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 0.7.2 + - Fix re-exported version of `actix-multipart-derive`. ## 0.7.1 diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index 5d81f6973..e1172e8cf 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-multipart" -version = "0.7.1" +version = "0.7.2" authors = [ "Nikolay Kim ", "Jacob Halsey ", diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 20673d524..52df77d13 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -3,11 +3,11 @@ [![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.7.1)](https://docs.rs/actix-multipart/0.7.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.7.1/status.svg)](https://deps.rs/crate/actix-multipart/0.7.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) From e0e4d1e66124a8dfcc2259dc6c74f86159fbffb3 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 03:54:00 +0100 Subject: [PATCH 081/129] chore: move deny lints to manifests --- Cargo.toml | 8 ++++++++ actix-files/Cargo.toml | 3 +++ actix-files/src/lib.rs | 3 +-- actix-http-test/Cargo.toml | 3 +++ actix-http-test/src/lib.rs | 2 -- actix-http/Cargo.toml | 3 +++ actix-http/src/h1/service.rs | 2 +- actix-http/src/h2/service.rs | 2 +- actix-http/src/lib.rs | 2 -- actix-http/src/message.rs | 2 +- actix-http/src/service.rs | 4 ++-- actix-http/src/test.rs | 4 ++-- actix-multipart-derive/Cargo.toml | 3 +++ actix-multipart-derive/src/lib.rs | 2 -- actix-multipart/Cargo.toml | 6 ++---- actix-router/Cargo.toml | 3 +++ actix-router/src/lib.rs | 2 -- actix-test/Cargo.toml | 3 +++ actix-test/src/lib.rs | 2 -- actix-web-actors/Cargo.toml | 3 +++ actix-web-actors/src/lib.rs | 2 -- actix-web-codegen/Cargo.toml | 3 +++ actix-web-codegen/src/lib.rs | 2 -- actix-web-codegen/tests/routes.rs | 2 +- actix-web-codegen/tests/scopes.rs | 2 +- actix-web/Cargo.toml | 3 +++ actix-web/src/app.rs | 2 +- actix-web/src/app_service.rs | 2 +- actix-web/src/config.rs | 4 ++-- actix-web/src/data.rs | 2 +- actix-web/src/lib.rs | 2 -- actix-web/src/middleware/default_headers.rs | 2 +- actix-web/src/middleware/err_handlers.rs | 16 ++++++++++------ actix-web/src/middleware/logger.rs | 2 +- actix-web/src/resource.rs | 6 +++--- actix-web/src/route.rs | 2 +- actix-web/src/server.rs | 18 +++++++++--------- awc/Cargo.toml | 3 +++ awc/src/client/pool.rs | 15 +++++++++------ awc/src/frozen.rs | 10 +++++----- awc/src/lib.rs | 2 -- awc/src/middleware/redirect.rs | 2 +- 42 files changed, 95 insertions(+), 71 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 19d5dd116..51f998314 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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/Cargo.toml b/actix-files/Cargo.toml index 57cd4e913..0c02359c4 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -54,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/src/lib.rs b/actix-files/src/lib.rs index 0178c13de..551a14fa4 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))] diff --git a/actix-http-test/Cargo.toml b/actix-http-test/Cargo.toml index 0947579a5..7ccb70a45 100644 --- a/actix-http-test/Cargo.toml +++ b/actix-http-test/Cargo.toml @@ -59,3 +59,6 @@ tokio = { version = "1.24.2", features = ["sync"] } [dev-dependencies] actix-http = "3" + +[lints] +workspace = true diff --git a/actix-http-test/src/lib.rs b/actix-http-test/src/lib.rs index 554af9102..d83b0b3ea 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))] diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 0a0252e9a..8fb31da4f 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -167,6 +167,9 @@ 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/src/h1/service.rs b/actix-http/src/h1/service.rs index f2f8a0e48..4fbccf844 100644 --- a/actix-http/src/h1/service.rs +++ b/actix-http/src/h1/service.rs @@ -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 636ac3161..debc73e59 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/lib.rs b/actix-http/src/lib.rs index ac79433c6..734e6e1e1 100644 --- a/actix-http/src/lib.rs +++ b/actix-http/src/lib.rs @@ -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, diff --git a/actix-http/src/message.rs b/actix-http/src/message.rs index 47b128fd0..d2241b229 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/service.rs b/actix-http/src/service.rs index 37cee960f..3ea88274a 100644 --- a/actix-http/src/service.rs +++ b/actix-http/src/service.rs @@ -910,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, )), @@ -926,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 3815e64c6..dfa9a86c9 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-multipart-derive/Cargo.toml b/actix-multipart-derive/Cargo.toml index 33eacd460..964ef2b74 100644 --- a/actix-multipart-derive/Cargo.toml +++ b/actix-multipart-derive/Cargo.toml @@ -29,3 +29,6 @@ actix-multipart = "0.7" actix-web = "4" rustversion = "1" trybuild = "1" + +[lints] +workspace = true diff --git a/actix-multipart-derive/src/lib.rs b/actix-multipart-derive/src/lib.rs index 44db1fa0e..6818d477c 100644 --- a/actix-multipart-derive/src/lib.rs +++ b/actix-multipart-derive/src/lib.rs @@ -2,8 +2,6 @@ //! //! 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))] diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index e1172e8cf..a5b0e3a2c 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -71,7 +71,5 @@ multer = "3" tokio = { version = "1.24.2", features = ["sync"] } tokio-stream = "0.1" -[lints.rust] -future_incompatible = { level = "deny" } -rust_2018_idioms = { level = "deny" } -nonstandard_style = { level = "deny" } +[lints] +workspace = true diff --git a/actix-router/Cargo.toml b/actix-router/Cargo.toml index 7e7e3beb8..7def1bdb4 100644 --- a/actix-router/Cargo.toml +++ b/actix-router/Cargo.toml @@ -38,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/lib.rs b/actix-router/src/lib.rs index c4d0d2c87..3f5e969e7 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-test/Cargo.toml b/actix-test/Cargo.toml index e810ae80b..34fdf2c82 100644 --- a/actix-test/Cargo.toml +++ b/actix-test/Cargo.toml @@ -73,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/src/lib.rs b/actix-test/src/lib.rs index 9be99978d..f0da2c20d 100644 --- a/actix-test/src/lib.rs +++ b/actix-test/src/lib.rs @@ -27,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))] diff --git a/actix-web-actors/Cargo.toml b/actix-web-actors/Cargo.toml index 3c74a4f47..f753b7f7f 100644 --- a/actix-web-actors/Cargo.toml +++ b/actix-web-actors/Cargo.toml @@ -41,3 +41,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/src/lib.rs b/actix-web-actors/src/lib.rs index d89b0ee35..0198c830f 100644 --- a/actix-web-actors/src/lib.rs +++ b/actix-web-actors/src/lib.rs @@ -55,8 +55,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-codegen/Cargo.toml b/actix-web-codegen/Cargo.toml index 7500807d2..d3938da8e 100644 --- a/actix-web-codegen/Cargo.toml +++ b/actix-web-codegen/Cargo.toml @@ -35,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/src/lib.rs b/actix-web-codegen/src/lib.rs index c518007a0..e22bff8cd 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))] diff --git a/actix-web-codegen/tests/routes.rs b/actix-web-codegen/tests/routes.rs index fb50d4ae0..a6e606871 100644 --- a/actix-web-codegen/tests/routes.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 index 4ee6db16f..b8c832682 100644 --- a/actix-web-codegen/tests/scopes.rs +++ b/actix-web-codegen/tests/scopes.rs @@ -1,7 +1,7 @@ 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 { +pub fn image_guard(ctx: &GuardContext<'_>) -> bool { ctx.header::() .map(|h| h.preference() == "image/*") .unwrap_or(false) diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 125cfe20f..543f44400 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -188,6 +188,9 @@ 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/src/app.rs b/actix-web/src/app.rs index 21a4443cf..240e5b982 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, diff --git a/actix-web/src/app_service.rs b/actix-web/src/app_service.rs index 65a6ed87b..7aa16b790 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 5e8b056f1..0e856f574 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 acbb8e23a..088df55d2 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/lib.rs b/actix-web/src/lib.rs index 205391388..752852525 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))] diff --git a/actix-web/src/middleware/default_headers.rs b/actix-web/src/middleware/default_headers.rs index f21afe6eb..2669a047e 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 aa6d1c8a4..3c50d5c8f 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/logger.rs b/actix-web/src/middleware/logger.rs index dc1b02399..e9ac24d50 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), })) } } diff --git a/actix-web/src/resource.rs b/actix-web/src/resource.rs index 00555b7b2..d89438edb 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 { diff --git a/actix-web/src/route.rs b/actix-web/src/route.rs index 261e6b9ae..e05e6be52 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/server.rs b/actix-web/src/server.rs index 95e15581f..d8519fb9e 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -510,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(); @@ -554,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(); @@ -632,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, @@ -683,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, @@ -749,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, @@ -815,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, @@ -880,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, @@ -937,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); @@ -982,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); diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 4fc2057f6..353ca0d54 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -153,6 +153,9 @@ 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 +[lints] +workspace = true + [[example]] name = "client" required-features = ["rustls-0_23-webpki-roots"] diff --git a/awc/src/client/pool.rs b/awc/src/client/pool.rs index 2938353fd..4c439e4eb 100644 --- a/awc/src/client/pool.rs +++ b/awc/src/client/pool.rs @@ -173,12 +173,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/frozen.rs b/awc/src/frozen.rs index 8f3244997..90b2c6efd 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, diff --git a/awc/src/lib.rs b/awc/src/lib.rs index 460480994..b582d51e4 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 0ea5f174e..b2cf9c45b 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. From e9ccfbc866d53dcbad82c9816ac25c32d409f587 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 20:19:35 +0100 Subject: [PATCH 082/129] refactor(multipart): clean up InnerField::poll --- actix-multipart/src/field.rs | 68 ++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs index 652b3e8b7..f4eb601fb 100644 --- a/actix-multipart/src/field.rs +++ b/actix-multipart/src/field.rs @@ -209,8 +209,14 @@ impl fmt::Debug for Field { 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, } @@ -286,6 +292,7 @@ impl InnerField { let mut pos = 0; let len = payload.buf.len(); + if len == 0 { return if payload.eof { Poll::Ready(Some(Err(Error::Incomplete))) @@ -296,7 +303,7 @@ impl InnerField { // 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"--" { + 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) @@ -357,41 +364,42 @@ impl InnerField { return Poll::Ready(None); } - let result = if let Some(mut payload) = self + let Some(mut payload) = self .payload .as_ref() .expect("Field should not be polled after completion") .get_mut(safety) - { - 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 + 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(); From 16125bd3be26ea458eb58c079d8c96c71df47cc0 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 20:19:56 +0100 Subject: [PATCH 083/129] docs(multipart): doc PayloadBuffer::readline --- actix-multipart/src/payload.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/actix-multipart/src/payload.rs b/actix-multipart/src/payload.rs index ed5477997..fae4ba032 100644 --- a/actix-multipart/src/payload.rs +++ b/actix-multipart/src/payload.rs @@ -122,7 +122,13 @@ impl PayloadBuffer { } } - /// Reads bytes until new line delimiter. + /// 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") From e97e28db4f939e1a0db7fb0ff8128e7a2fc4678f Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 20:32:56 +0100 Subject: [PATCH 084/129] docs(multipart): improve crate root docs --- actix-multipart/Cargo.toml | 13 ++-- actix-multipart/README.md | 12 +++- actix-multipart/src/form/mod.rs | 2 +- actix-multipart/src/lib.rs | 12 +++- actix-multipart/src/multipart.rs | 96 +---------------------------- actix-multipart/src/payload.rs | 102 +++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 106 deletions(-) diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index a5b0e3a2c..7a80b265f 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -4,13 +4,14 @@ 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"] diff --git a/actix-multipart/README.md b/actix-multipart/README.md index 52df77d13..ec2e94bd8 100644 --- a/actix-multipart/README.md +++ b/actix-multipart/README.md @@ -15,14 +15,18 @@ -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 ```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)] @@ -34,7 +38,7 @@ struct Metadata { struct UploadForm { #[multipart(limit = "100MB")] file: TempFile, - json: MPJson, + json: MpJson, } #[post("/videos")] @@ -63,6 +67,8 @@ curl -v --request POST \ -F file=@./Cargo.lock ``` +[`MultipartForm`]: struct@form::MultipartForm + [More available in the examples repo →](https://github.com/actix/examples/tree/master/forms/multipart) diff --git a/actix-multipart/src/form/mod.rs b/actix-multipart/src/form/mod.rs index 9441d249f..693a45e8e 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, diff --git a/actix-multipart/src/lib.rs b/actix-multipart/src/lib.rs index a56b9846b..8eea35f2e 100644 --- a/actix-multipart/src/lib.rs +++ b/actix-multipart/src/lib.rs @@ -1,4 +1,12 @@ -//! 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 //! @@ -45,6 +53,8 @@ //! -F 'json={"name": "Cargo.lock"};type=application/json' \ //! -F file=@./Cargo.lock //! ``` +//! +//! [`MultipartForm`]: struct@form::MultipartForm #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] diff --git a/actix-multipart/src/multipart.rs b/actix-multipart/src/multipart.rs index e511cb6df..e38fbde9e 100644 --- a/actix-multipart/src/multipart.rs +++ b/actix-multipart/src/multipart.rs @@ -483,7 +483,7 @@ mod tests { }; use assert_matches::assert_matches; use futures_test::stream::StreamTestExt as _; - use futures_util::{future::lazy, stream, StreamExt as _}; + use futures_util::{stream, StreamExt as _}; use tokio::sync::mpsc; use tokio_stream::wrappers::UnboundedReceiverStream; @@ -718,100 +718,6 @@ mod tests { } } - #[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 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); - } - #[actix_rt::test] async fn test_multipart_from_error() { let err = Error::ContentTypeMissing; diff --git a/actix-multipart/src/payload.rs b/actix-multipart/src/payload.rs index fae4ba032..858634bc0 100644 --- a/actix-multipart/src/payload.rs +++ b/actix-multipart/src/payload.rs @@ -151,3 +151,105 @@ impl PayloadBuffer { 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); + } +} From 5c6e0e17d34ac58fcb3fcc8d16fa712f47d10e97 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Jul 2024 21:16:25 +0100 Subject: [PATCH 085/129] feat(http): impl FromIter for HeaderMap --- actix-http/CHANGES.md | 4 ++++ actix-http/src/header/map.rs | 32 +++++++++++++++++++++++++++++--- awc/src/any_body.rs | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 48c730bb2..b2e5af79f 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Implement `FromIterator<(HeaderName, HeaderValue)>` for `HeaderMap`. + ## 3.8.0 ### Added diff --git a/actix-http/src/header/map.rs b/actix-http/src/header/map.rs index b86798a4c..6da01d2c0 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/awc/src/any_body.rs b/awc/src/any_body.rs index 08f5cc25e..ef0edfb9e 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 { From b6bee346f7c42b083b4e3cb44f4ff8d38ef8f0fe Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:31:50 +0100 Subject: [PATCH 086/129] build(deps): bump taiki-e/install-action from 2.41.7 to 2.41.10 (#3423) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.41.7 to 2.41.10. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.41.7...v2.41.10) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 444f1b174..dcf6ae4aa 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index caee78896..178294355 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 260d65e27..6dc99ebd8 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1fd20f221..f2f7473c5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.41.7 + uses: taiki-e/install-action@v2.41.10 with: tool: cargo-public-api From f71f9ca66b28bc33b263f0e4c86f4d112c1afec1 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 20 Jul 2024 01:13:57 +0100 Subject: [PATCH 087/129] build(deps): bump taiki-e/install-action from 2.41.10 to 2.41.17 (#3431) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.41.10 to 2.41.17. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.41.10...v2.41.17) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index dcf6ae4aa..516edc574 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 178294355..32ae0a73d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 6dc99ebd8..56fa4e5a8 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f2f7473c5..cdb94f1da 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.41.10 + uses: taiki-e/install-action@v2.41.17 with: tool: cargo-public-api From 07f720f716ee6df9b1e8804ee3146d1db390669c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?On=C3=A8?= <43485962+c-git@users.noreply.github.com> Date: Sun, 21 Jul 2024 13:34:42 -0400 Subject: [PATCH 088/129] docs: fix typo (#3439) --- actix-web/src/middleware/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web/src/middleware/mod.rs b/actix-web/src/middleware/mod.rs index 1c27b1110..79c94658b 100644 --- a/actix-web/src/middleware/mod.rs +++ b/actix-web/src/middleware/mod.rs @@ -67,7 +67,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, From 270a6a3b7074264dca3438b0ab05bc6a6fb68f02 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 22 Jul 2024 01:49:25 +0100 Subject: [PATCH 089/129] build(deps): bump taiki-e/install-action from 2.41.17 to 2.42.4 (#3440) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.41.17 to 2.42.4. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.41.17...v2.42.4) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 516edc574..25583acc3 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 32ae0a73d..2b91d0f7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 56fa4e5a8..969ce7404 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index cdb94f1da..1db7411a5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.41.17 + uses: taiki-e/install-action@v2.42.4 with: tool: cargo-public-api From 9aa62112aab8249b7cc90180d5102221ebac3d42 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 29 Jul 2024 00:25:30 +0000 Subject: [PATCH 090/129] build(deps): bump taiki-e/install-action from 2.42.4 to 2.42.9 (#3441) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.4 to 2.42.9. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.4...v2.42.9) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 25583acc3..4fa650431 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2b91d0f7b..73f90535c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 969ce7404..45391e18d 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1db7411a5..4487d5fc7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.4 + uses: taiki-e/install-action@v2.42.9 with: tool: cargo-public-api From 323d1fa64fb5e92c8bc4ee897b239fe58c28d6bd Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 7 Aug 2024 02:05:32 +0100 Subject: [PATCH 091/129] build(deps): bump taiki-e/install-action from 2.42.9 to 2.42.17 (#3442) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.9 to 2.42.17. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.9...v2.42.17) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 4fa650431..c1afdb1cc 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 73f90535c..f98e17a87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 45391e18d..f30c8ff42 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4487d5fc7..f3bdb3515 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.9 + uses: taiki-e/install-action@v2.42.17 with: tool: cargo-public-api From e4e4bb799ca0387712d67c416aee3959be6a0b40 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 7 Aug 2024 04:01:20 +0100 Subject: [PATCH 092/129] chore(actix-web-actors): prepare release 4.3.1 --- actix-web-actors/CHANGES.md | 5 ++++- actix-web-actors/Cargo.toml | 11 ++++++----- actix-web-actors/README.md | 6 ++++-- actix-web-actors/src/lib.rs | 2 ++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/actix-web-actors/CHANGES.md b/actix-web-actors/CHANGES.md index 3e854c0b8..da39a349b 100644 --- a/actix-web-actors/CHANGES.md +++ b/actix-web-actors/CHANGES.md @@ -2,7 +2,10 @@ ## Unreleased -- Take the encoded buffer when yielding bytes in the response stream rather than splitting the buffer, reducing memory use +## 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 f753b7f7f..e7034ab84 100644 --- a/actix-web-actors/Cargo.toml +++ b/actix-web-actors/Cargo.toml @@ -1,13 +1,14 @@ [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 [package.metadata.cargo_check_external_types] allowed_external_types = [ diff --git a/actix-web-actors/README.md b/actix-web-actors/README.md index feb3d1b33..ea5db55a6 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) +[![dependency status](https://deps.rs/crate/actix-web-actors/4.3.1/status.svg)](https://deps.rs/crate/actix-web-actors/4.3.1) [![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 0198c830f..4831d2637 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 From d7d9000b19e5e6fb96c0c37c3417ce6fc91fd9cc Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 7 Aug 2024 04:06:18 +0100 Subject: [PATCH 093/129] chore: address clippy warnings --- actix-router/src/de.rs | 5 --- actix-web/benches/responder.rs | 36 ++----------------- actix-web/src/handler.rs | 2 +- .../src/http/header/content_disposition.rs | 2 +- actix-web/src/http/header/range.rs | 14 ++++---- actix-web/src/middleware/logger.rs | 12 ++----- actix-web/src/response/builder.rs | 2 +- 7 files changed, 14 insertions(+), 59 deletions(-) diff --git a/actix-router/src/de.rs b/actix-router/src/de.rs index ce2dcf8f3..2f50619f8 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-web/benches/responder.rs b/actix-web/benches/responder.rs index c675eadff..489515e40 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/src/handler.rs b/actix-web/src/handler.rs index 6e4e2250a..10015cb69 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 824bf1195..592fc9f6a 100644 --- a/actix-web/src/http/header/content_disposition.rs +++ b/actix-web/src/http/header/content_disposition.rs @@ -493,7 +493,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/range.rs b/actix-web/src/http/header/range.rs index 2326bb19c..4a5d95d93 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/middleware/logger.rs b/actix-web/src/middleware/logger.rs index e9ac24d50..21986baae 100644 --- a/actix-web/src/middleware/logger.rs +++ b/actix-web/src/middleware/logger.rs @@ -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/response/builder.rs b/actix-web/src/response/builder.rs index 023842ee5..c23de8e36 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"]"#); From 5be53820f0718260f6f3573d2edff49f09653206 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 7 Aug 2024 04:32:16 +0100 Subject: [PATCH 094/129] docs(actors): add maintenance badge --- actix-web-actors/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web-actors/README.md b/actix-web-actors/README.md index ea5db55a6..0ec91a224 100644 --- a/actix-web-actors/README.md +++ b/actix-web-actors/README.md @@ -11,7 +11,7 @@ ![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.1/status.svg)](https://deps.rs/crate/actix-web-actors/4.3.1) +![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) From a431b7356c1f987ceffc482be7cbdf43fe4132a5 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 00:42:34 +0100 Subject: [PATCH 095/129] feat: add ThinData wrapper (#3446) --- actix-web/CHANGES.md | 4 ++ actix-web/Cargo.toml | 1 + actix-web/src/lib.rs | 1 + actix-web/src/thin_data.rs | 121 +++++++++++++++++++++++++++++++++++++ actix-web/src/web.rs | 4 +- 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 actix-web/src/thin_data.rs diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 90c7b7e2a..10a0e8038 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Add `web::ThinData` extractor. + ## 4.8.0 ### Added diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 543f44400..f27a5a5b6 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -151,6 +151,7 @@ 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" diff --git a/actix-web/src/lib.rs b/actix-web/src/lib.rs index 752852525..e2a8e2275 100644 --- a/actix-web/src/lib.rs +++ b/actix-web/src/lib.rs @@ -104,6 +104,7 @@ mod scope; mod server; mod service; pub mod test; +mod thin_data; pub(crate) mod types; pub mod web; diff --git a/actix-web/src/thin_data.rs b/actix-web/src/thin_data.rs new file mode 100644 index 000000000..a9cd4e3a4 --- /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/web.rs b/actix-web/src/web.rs index 204313752..3a4c46730 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, From be28a0bd6d404e883e6bef6111a7d9606bab39d6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 01:41:27 +0100 Subject: [PATCH 096/129] feat: add from_fn middleware (#3447) --- actix-web/CHANGES.md | 1 + actix-web/examples/middleware_from_fn.rs | 127 +++++++++ actix-web/src/middleware/from_fn.rs | 349 +++++++++++++++++++++++ actix-web/src/middleware/mod.rs | 61 ++-- 4 files changed, 521 insertions(+), 17 deletions(-) create mode 100644 actix-web/examples/middleware_from_fn.rs create mode 100644 actix-web/src/middleware/from_fn.rs diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 10a0e8038..d26859a36 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ ### Added +- Add `middleware::from_fn()` helper. - Add `web::ThinData` extractor. ## 4.8.0 diff --git a/actix-web/examples/middleware_from_fn.rs b/actix-web/examples/middleware_from_fn.rs new file mode 100644 index 000000000..da92ef05b --- /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/src/middleware/from_fn.rs b/actix-web/src/middleware/from_fn.rs new file mode 100644 index 000000000..608833319 --- /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/mod.rs b/actix-web/src/middleware/mod.rs index 79c94658b..4b5b3e896 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 //! @@ -196,18 +233,6 @@ //! # } //! ``` //! -//! # Simpler Middleware -//! -//! In many cases, you _can_ actually use an async function via a helper that will provide a more -//! natural flow for your behavior. -//! -//! The experimental `actix_web_lab` crate provides a [`from_fn`][lab_from_fn] utility which allows -//! an async fn to be wrapped and used in the same way as other middleware. See the -//! [`from_fn`][lab_from_fn] docs for more info and examples of it's use. -//! -//! While [`from_fn`][lab_from_fn] is experimental currently, it's likely this helper will graduate -//! to Actix Web in some form, so feedback is appreciated. -//! //! [`Future`]: std::future::Future //! [`App`]: crate::App //! [`FromRequest`]: crate::FromRequest @@ -215,7 +240,7 @@ //! [`Transform`]: crate::dev::Transform //! [`call`]: crate::dev::Service::call() //! [`new_transform`]: crate::dev::Transform::new_transform() -//! [lab_from_fn]: https://docs.rs/actix-web-lab/latest/actix_web_lab/middleware/fn.from_fn.html +//! [`from_fn`]: crate mod compat; #[cfg(feature = "__compress")] @@ -223,6 +248,7 @@ mod compress; mod condition; mod default_headers; mod err_handlers; +mod from_fn; mod identity; mod logger; mod normalize; @@ -234,6 +260,7 @@ pub use self::{ condition::Condition, default_headers::DefaultHeaders, err_handlers::{ErrorHandlerResponse, ErrorHandlers}, + from_fn::{from_fn, Next}, identity::Identity, logger::Logger, normalize::{NormalizePath, TrailingSlash}, From 882fb3d25b323994dc5caa471ae05d127cd3dc34 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 03:08:18 +0100 Subject: [PATCH 097/129] chore(actors): add version marker in changelog --- actix-web-actors/CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web-actors/CHANGES.md b/actix-web-actors/CHANGES.md index da39a349b..3f214274d 100644 --- a/actix-web-actors/CHANGES.md +++ b/actix-web-actors/CHANGES.md @@ -2,7 +2,7 @@ ## Unreleased -## 4.3.1 +## 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. From 9ba326aed0dfe1a992c43a07fcb74958e4cfb3c6 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 03:09:09 +0100 Subject: [PATCH 098/129] chore(actix-http): prepare release 3.9.0 --- actix-http/CHANGES.md | 2 ++ actix-http/Cargo.toml | 2 +- actix-http/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index b2e5af79f..15b211c1a 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 3.9.0 + ### Added - Implement `FromIterator<(HeaderName, HeaderValue)>` for `HeaderMap`. diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 8fb31da4f..e8dea0bd0 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-http" -version = "3.8.0" +version = "3.9.0" authors = [ "Nikolay Kim ", "Rob Ede ", diff --git a/actix-http/README.md b/actix-http/README.md index 7d56f2517..f78ea86f5 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.8.0)](https://docs.rs/actix-http/3.8.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.8.0/status.svg)](https://deps.rs/crate/actix-http/3.8.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) From e0918fb1795c899ac38fb903694ca1aa3b58d46d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 03:21:55 +0100 Subject: [PATCH 099/129] chore(actix-web): prepare release 4.9.0 --- actix-web/CHANGES.md | 2 ++ actix-web/Cargo.toml | 2 +- actix-web/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index d26859a36..588320f4d 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 4.9.0 + ### Added - Add `middleware::from_fn()` helper. diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index f27a5a5b6..1e69973d0 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web" -version = "4.8.0" +version = "4.9.0" description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust" authors = [ "Nikolay Kim ", diff --git a/actix-web/README.md b/actix-web/README.md index d30d945a4..e5e412c85 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.8.0)](https://docs.rs/actix-web/4.8.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.8.0/status.svg)](https://deps.rs/crate/actix-web/4.8.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) From 5ad92c0062c3ee972005b9c2b0bbb0c2d5d59fbb Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 03:58:45 +0100 Subject: [PATCH 100/129] fix(awc): ws host req header includes port --- awc/CHANGES.md | 4 +++- awc/src/ws.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 54c5e9869..9a344ceb0 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,11 +2,13 @@ ## Unreleased +- 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/src/ws.rs b/awc/src/ws.rs index c3340206d..794dccbfe 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -257,8 +257,9 @@ impl WebsocketsRequest { return Err(e.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::*; From 70e3758ecc9bde13ef12a6f96576524c36050277 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 04:08:38 +0100 Subject: [PATCH 101/129] chore(awc): prepare release 3.5.1 --- awc/CHANGES.md | 2 ++ awc/Cargo.toml | 2 +- awc/README.md | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 9a344ceb0..9c0f3b607 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +## 3.5.1 + - Fix WebSocket `Host` request header value when using a non-default port. ## 3.5.0 diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 353ca0d54..26b483bfa 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"] diff --git a/awc/README.md b/awc/README.md index 8e7b42812..28d28ed89 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) From 538c1bea34911f50be12679b19f81e5d180ff922 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Aug 2024 05:15:49 +0100 Subject: [PATCH 102/129] chore: disallow e bindings --- .clippy.toml | 7 +++++++ actix-files/src/service.rs | 2 +- actix-http-test/src/lib.rs | 2 +- actix-http/src/h1/encoder.rs | 4 ++-- actix-http/src/h1/service.rs | 14 +++++++------- actix-http/src/service.rs | 18 +++++++++--------- actix-http/src/ws/dispatcher.rs | 18 +++++++++--------- actix-multipart-derive/src/lib.rs | 2 ++ actix-router/src/path.rs | 16 +++++++++------- actix-web-actors/src/ws.rs | 7 ++----- actix-web/src/app.rs | 6 +++--- actix-web/src/resource.rs | 7 +++---- actix-web/src/scope.rs | 4 +++- actix-web/src/types/json.rs | 4 ++-- actix-web/src/types/path.rs | 4 ++-- actix-web/src/types/query.rs | 14 +++++++------- awc/src/client/error.rs | 4 ++-- awc/src/client/pool.rs | 10 +++++----- awc/src/frozen.rs | 16 ++++++++-------- awc/src/request.rs | 4 ++-- awc/src/sender.rs | 16 ++++++++-------- awc/src/ws.rs | 4 ++-- 22 files changed, 96 insertions(+), 87 deletions(-) create mode 100644 .clippy.toml diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 000000000..8c0cbc8bf --- /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/actix-files/src/service.rs b/actix-files/src/service.rs index 3d3b36c40..393ad9244 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/src/lib.rs b/actix-http-test/src/lib.rs index d83b0b3ea..a359cec09 100644 --- a/actix-http-test/src/lib.rs +++ b/actix-http-test/src/lib.rs @@ -106,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/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs index abe396ce2..77e34bcdc 100644 --- a/actix-http/src/h1/encoder.rs +++ b/actix-http/src/h1/encoder.rs @@ -313,7 +313,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)) } } @@ -433,7 +433,7 @@ impl TransferEncoding { buf.extend_from_slice(b"0\r\n\r\n"); } else { writeln!(helpers::MutWriter(buf), "{:X}\r", msg.len()) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(|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 4fbccf844..2cf76edb2 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, diff --git a/actix-http/src/service.rs b/actix-http/src/service.rs index 3ea88274a..3be099d9f 100644 --- a/actix-http/src/service.rs +++ b/actix-http/src/service.rs @@ -775,23 +775,23 @@ where let cfg = self.cfg.clone(); Box::pin(async move { - let expect = expect - .await - .map_err(|e| error!("Init http expect service error: {:?}", e))?; + let expect = expect.await.map_err(|err| { + tracing::error!("Initialization of HTTP expect service error: {err:?}"); + })?; let upgrade = match upgrade { Some(upgrade) => { - let upgrade = upgrade - .await - .map_err(|e| error!("Init http upgrade service error: {:?}", e))?; + let upgrade = upgrade.await.map_err(|err| { + tracing::error!("Initialization of HTTP upgrade service error: {err:?}"); + })?; Some(upgrade) } None => None, }; - let service = service - .await - .map_err(|e| error!("Init http service error: {:?}", e))?; + let service = service.await.map_err(|err| { + tracing::error!("Initialization of HTTP service error: {err:?}"); + })?; Ok(HttpServiceHandler::new( cfg, diff --git a/actix-http/src/ws/dispatcher.rs b/actix-http/src/ws/dispatcher.rs index 1354d5ae1..7d0a300b7 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-multipart-derive/src/lib.rs b/actix-multipart-derive/src/lib.rs index 6818d477c..57d2fa875 100644 --- a/actix-multipart-derive/src/lib.rs +++ b/actix-multipart-derive/src/lib.rs @@ -5,6 +5,7 @@ #![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; @@ -35,6 +36,7 @@ struct MultipartFormAttrs { duplicate_field: DuplicateField, } +#[allow(clippy::disallowed_names)] // false positive in macro expansion #[derive(FromField, Default)] #[darling(attributes(multipart), default)] struct FieldAttrs { diff --git a/actix-router/src/path.rs b/actix-router/src/path.rs index 9031ab763..ab4a943fe 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-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index 7f7607fa9..0002f87e2 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -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/src/app.rs b/actix-web/src/app.rs index 240e5b982..f12d39979 100644 --- a/actix-web/src/app.rs +++ b/actix-web/src/app.rs @@ -269,9 +269,9 @@ where + 'static, U::InitError: fmt::Debug, { - let svc = svc - .into_factory() - .map_init_err(|e| log::error!("Can not construct default service: {:?}", e)); + let svc = svc.into_factory().map_init_err(|err| { + log::error!("Can not construct default service: {err:?}"); + }); self.default = Some(Rc::new(boxed::factory(svc))); diff --git a/actix-web/src/resource.rs b/actix-web/src/resource.rs index d89438edb..6486fb39d 100644 --- a/actix-web/src/resource.rs +++ b/actix-web/src/resource.rs @@ -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/scope.rs b/actix-web/src/scope.rs index 81f3615b0..e317349da 100644 --- a/actix-web/src/scope.rs +++ b/actix-web/src/scope.rs @@ -278,7 +278,9 @@ where { // create and configure default resource self.default = Some(Rc::new(boxed::factory(f.into_factory().map_init_err( - |e| log::error!("Can not construct default service: {:?}", e), + |err| { + log::error!("Can not construct default service: {err:?}"); + }, )))); self diff --git a/actix-web/src/types/json.rs b/actix-web/src/types/json.rs index 6b75c0cfe..51a322e43 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/path.rs b/actix-web/src/types/path.rs index cc87bb80f..d6cf186f6 100644 --- a/actix-web/src/types/path.rs +++ b/actix-web/src/types/path.rs @@ -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) } diff --git a/actix-web/src/types/query.rs b/actix-web/src/types/query.rs index e71b886f2..e5505131e 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)) }) } } diff --git a/awc/src/client/error.rs b/awc/src/client/error.rs index d351e1067..22cf2c200 100644 --- a/awc/src/client/error.rs +++ b/awc/src/client/error.rs @@ -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), } } } diff --git a/awc/src/client/pool.rs b/awc/src/client/pool.rs index 4c439e4eb..1736f2b02 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,7 +72,7 @@ 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 { diff --git a/awc/src/frozen.rs b/awc/src/frozen.rs index 90b2c6efd..862405234 100644 --- a/awc/src/frozen.rs +++ b/awc/src/frozen.rs @@ -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/request.rs b/awc/src/request.rs index 28ed8b5f5..5f42f67ec 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 8de1033a3..0015743bd 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -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 794dccbfe..760331e9d 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -253,8 +253,8 @@ 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 From f61fcbe8407019ad6ec958ab78ef2401f2ccd68f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:12:48 +0100 Subject: [PATCH 103/129] build(deps): bump taiki-e/install-action from 2.42.17 to 2.42.22 (#3451) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.17 to 2.42.22. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.17...v2.42.22) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index c1afdb1cc..f94f0b20b 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f98e17a87..f4a526e38 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index f30c8ff42..8282ac18e 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f3bdb3515..d105b67d5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.17 + uses: taiki-e/install-action@v2.42.22 with: tool: cargo-public-api From 4303dd8c37f1e320a861e79ca27637633e2e6eb8 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 18 Aug 2024 14:10:33 +0100 Subject: [PATCH 104/129] docs(web): mention try_init_service --- actix-web/src/test/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/actix-web/src/test/mod.rs b/actix-web/src/test/mod.rs index 5e647956b..701771588 100644 --- a/actix-web/src/test/mod.rs +++ b/actix-web/src/test/mod.rs @@ -1,7 +1,8 @@ //! Various helpers for Actix applications to use during testing. //! -//! # Creating A Test Service +//! # Initializing A Test Service //! - [`init_service`] +//! - [`try_init_service`] //! //! # Off-The-Shelf Test Services //! - [`ok_service`] @@ -49,6 +50,7 @@ pub use self::{ /// Must be used inside an async test. Works for both `ServiceRequest` and `HttpRequest`. /// /// # Examples +/// /// ``` /// use actix_web::{http::StatusCode, HttpResponse}; /// From 78ac5cf4829dd923b88c52145f509ec74b34a41a Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 18 Aug 2024 14:33:28 +0100 Subject: [PATCH 105/129] docs(web): unmention try_init_service --- actix-http/examples/actix-web.rs | 4 ++-- actix-http/examples/echo.rs | 5 ++--- actix-http/examples/hello-world.rs | 2 +- actix-http/examples/streaming-error.rs | 6 +++--- actix-http/examples/ws.rs | 7 +++---- actix-web/src/test/mod.rs | 1 - 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/actix-http/examples/actix-web.rs b/actix-http/examples/actix-web.rs index 449e5899b..e07abfd97 100644 --- a/actix-http/examples/actix-web.rs +++ b/actix-http/examples/actix-web.rs @@ -1,10 +1,10 @@ use actix_http::HttpService; use actix_server::Server; use actix_service::map_config; -use actix_web::{dev::AppConfig, get, App}; +use actix_web::{dev::AppConfig, get, App, Responder}; #[get("/")] -async fn index() -> &'static str { +async fn index() -> impl Responder { "Hello, world. From Actix Web!" } diff --git a/actix-http/examples/echo.rs b/actix-http/examples/echo.rs index ae6f00cce..11fd2750e 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 cf10beddf..afa3883a4 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 8c8a249cb..39f214fa1 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 fb86bc5ea..af83e4c3d 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-web/src/test/mod.rs b/actix-web/src/test/mod.rs index 701771588..86cb60956 100644 --- a/actix-web/src/test/mod.rs +++ b/actix-web/src/test/mod.rs @@ -2,7 +2,6 @@ //! //! # Initializing A Test Service //! - [`init_service`] -//! - [`try_init_service`] //! //! # Off-The-Shelf Test Services //! - [`ok_service`] From d6bdfac1b9d3ec7bba50c37600346f9a8dcf8ecb Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Sun, 18 Aug 2024 22:17:03 +0800 Subject: [PATCH 106/129] build(deps): update derive_more to v1.0 (#3453) * build(deps): update derive_more to v1.0 * refactor: use from derive module --------- Co-authored-by: Rob Ede --- Cargo.toml | 2 +- actix-files/CHANGES.md | 2 + actix-files/Cargo.toml | 2 +- actix-files/src/error.rs | 14 ++-- actix-files/src/named.rs | 2 +- actix-files/src/range.rs | 2 +- actix-http/CHANGES.md | 2 + actix-http/Cargo.toml | 4 +- actix-http/src/body/body_stream.rs | 4 +- actix-http/src/body/utils.rs | 4 +- actix-http/src/encoding/encoder.rs | 6 +- actix-http/src/error.rs | 74 +++++++++---------- .../src/header/shared/content_encoding.rs | 4 +- actix-http/src/header/shared/quality.rs | 4 +- actix-http/src/ws/mod.rs | 34 ++++----- actix-http/tests/test_client.rs | 4 +- actix-http/tests/test_openssl.rs | 4 +- actix-http/tests/test_rustls.rs | 4 +- actix-http/tests/test_server.rs | 6 +- actix-http/tests/test_ws.rs | 10 +-- actix-multipart/CHANGES.md | 2 + actix-multipart/Cargo.toml | 2 +- actix-multipart/src/error.rs | 32 ++++---- actix-multipart/src/field.rs | 4 +- actix-multipart/src/form/json.rs | 6 +- actix-multipart/src/form/mod.rs | 2 +- actix-multipart/src/form/tempfile.rs | 4 +- actix-multipart/src/form/text.rs | 8 +- actix-web/CHANGES.md | 2 + actix-web/Cargo.toml | 4 +- actix-web/src/error/mod.rs | 50 ++++++------- actix-web/src/http/header/content_length.rs | 2 +- actix-web/src/info.rs | 6 +- actix-web/src/types/path.rs | 6 +- actix-web/src/types/query.rs | 2 +- awc/CHANGES.md | 2 + awc/Cargo.toml | 2 +- awc/src/client/error.rs | 48 ++++++------ awc/src/error.rs | 24 +++--- awc/src/sender.rs | 2 +- 40 files changed, 204 insertions(+), 194 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 51f998314..d5601f8f1 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. diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index e94f43907..afb2d5d20 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + ## 0.6.6 - Update `tokio-uring` dependency to `0.4`. diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index 0c02359c4..48d38874f 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -33,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" diff --git a/actix-files/src/error.rs b/actix-files/src/error.rs index d614651fc..1d4818887 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/named.rs b/actix-files/src/named.rs index 9e4a37737..7f839890e 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 528911ae0..c6e346329 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-http/CHANGES.md b/actix-http/CHANGES.md index 15b211c1a..95b51254b 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + ## 3.9.0 ### Added diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index e8dea0bd0..f910276f1 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -110,7 +110,7 @@ ahash = "0.8" bitflags = "2" bytes = "1" 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" @@ -160,7 +160,7 @@ 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" } diff --git a/actix-http/src/body/body_stream.rs b/actix-http/src/body/body_stream.rs index 4574b2519..2ae12c279 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 d1449179f..1fdb3ff5a 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 180927ac6..3c9eec811 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 6f332118e..92126f797 100644 --- a/actix-http/src/error.rs +++ b/actix-http/src/error.rs @@ -2,7 +2,7 @@ use std::{error::Error as StdError, fmt, io, str::Utf8Error, string::FromUtf8Error}; -use derive_more::{Display, Error, From}; +use derive_more::derive::{Display, Error, From}; pub use http::{status::InvalidStatusCode, Error as HttpError}; use http::{uri::InvalidUri, StatusCode}; @@ -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/header/shared/content_encoding.rs b/actix-http/src/header/shared/content_encoding.rs index c3b4bc4c2..0b83659d0 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 c2276cf1b..2a76bc8ca 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/ws/mod.rs b/actix-http/src/ws/mod.rs index 3ed53b70a..88053b254 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 5888527f1..c0019d06b 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 4dd22b585..77a66f32f 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 3ca0d94c2..5b02637dc 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 4ba64a53c..e93d35598 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 9a78074c4..e9c4a01af 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/CHANGES.md b/actix-multipart/CHANGES.md index c3f3b6e39..a030fac44 100644 --- a/actix-multipart/CHANGES.md +++ b/actix-multipart/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + ## 0.7.2 - Fix re-exported version of `actix-multipart-derive`. diff --git a/actix-multipart/Cargo.toml b/actix-multipart/Cargo.toml index 7a80b265f..aa33bfc93 100644 --- a/actix-multipart/Cargo.toml +++ b/actix-multipart/Cargo.toml @@ -42,7 +42,7 @@ actix-multipart-derive = { version = "=0.7.0", optional = true } actix-utils = "3" actix-web = { version = "4", default-features = false } -derive_more = "0.99.5" +derive_more = { version = "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" diff --git a/actix-multipart/src/error.rs b/actix-multipart/src/error.rs index cdb608738..8fff5c593 100644 --- a/actix-multipart/src/error.rs +++ b/actix-multipart/src/error.rs @@ -5,18 +5,18 @@ 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 Error { /// Could not find Content-Type header. - #[display(fmt = "Could not find Content-Type header")] + #[display("Could not find Content-Type header")] ContentTypeMissing, /// Could not parse Content-Type header. - #[display(fmt = "Could not parse Content-Type header")] + #[display("Could not parse Content-Type header")] ContentTypeParse, /// Parsed Content-Type did not have "multipart" top-level media type. @@ -25,11 +25,11 @@ pub enum Error { /// "multipart/form-data" media type. /// /// [`MultipartForm`]: struct@crate::form::MultipartForm - #[display(fmt = "Parsed Content-Type did not have "multipart" top-level media type")] + #[display("Parsed Content-Type did not have 'multipart' top-level media type")] ContentTypeIncompatible, /// Multipart boundary is not found. - #[display(fmt = "Multipart boundary is not found")] + #[display("Multipart boundary is not found")] BoundaryMissing, /// Content-Disposition header was not found or not of disposition type "form-data" when parsing @@ -39,7 +39,7 @@ pub enum Error { /// always be present and have a disposition type of "form-data". /// /// [RFC 7578 ยง4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 - #[display(fmt = "Content-Disposition header was not found when parsing a \"form-data\" field")] + #[display("Content-Disposition header was not found when parsing a \"form-data\" field")] ContentDispositionMissing, /// Content-Disposition name parameter was not found when parsing a "form-data" field. @@ -48,48 +48,48 @@ pub enum Error { /// always include a "name" parameter. /// /// [RFC 7578 ยง4.2]: https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 - #[display(fmt = "Content-Disposition header was not found when parsing a \"form-data\" field")] + #[display("Content-Disposition header was not found when parsing a \"form-data\" field")] ContentDispositionNameMissing, /// Nested multipart is not supported. - #[display(fmt = "Nested multipart is not supported")] + #[display("Nested multipart is not supported")] Nested, /// Multipart stream is incomplete. - #[display(fmt = "Multipart stream is incomplete")] + #[display("Multipart stream is incomplete")] Incomplete, /// Field parsing failed. - #[display(fmt = "Error during field parsing")] + #[display("Error during field parsing")] Parse(ParseError), /// HTTP payload error. - #[display(fmt = "Payload error")] + #[display("Payload error")] Payload(PayloadError), /// Stream is not consumed. - #[display(fmt = "Stream is not consumed")] + #[display("Stream is not consumed")] NotConsumed, /// Form field handler raised error. - #[display(fmt = "An error occurred processing field: {name}")] + #[display("An error occurred processing field: {name}")] Field { name: String, source: actix_web::Error, }, /// Duplicate field found (for structure that opted-in to denying duplicate fields). - #[display(fmt = "Duplicate field found: {_0}")] + #[display("Duplicate field found: {_0}")] #[from(ignore)] DuplicateField(#[error(not(source))] String), /// Required field is missing. - #[display(fmt = "Required field is missing: {_0}")] + #[display("Required field is missing: {_0}")] #[from(ignore)] MissingField(#[error(not(source))] String), /// Unknown field (for structure that opted-in to denying unknown fields). - #[display(fmt = "Unknown field: {_0}")] + #[display("Unknown field: {_0}")] #[from(ignore)] UnknownField(#[error(not(source))] String), } diff --git a/actix-multipart/src/field.rs b/actix-multipart/src/field.rs index f4eb601fb..b42d8ae1b 100644 --- a/actix-multipart/src/field.rs +++ b/actix-multipart/src/field.rs @@ -13,7 +13,7 @@ use actix_web::{ http::header::{self, ContentDisposition, HeaderMap}, web::{Bytes, BytesMut}, }; -use derive_more::{Display, Error}; +use derive_more::derive::{Display, Error}; use futures_core::Stream; use mime::Mime; @@ -25,7 +25,7 @@ use crate::{ /// Error type returned from [`Field::bytes()`] when field data is larger than limit. #[derive(Debug, Display, Error)] -#[display(fmt = "size limit exceeded while collecting field data")] +#[display("size limit exceeded while collecting field data")] #[non_exhaustive] pub struct LimitExceeded; diff --git a/actix-multipart/src/form/json.rs b/actix-multipart/src/form/json.rs index 0118a8fba..8fb1f5ca7 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; @@ -66,11 +66,11 @@ where #[non_exhaustive] pub enum JsonFieldError { /// Deserialize error. - #[display(fmt = "Json deserialize error: {}", _0)] + #[display("Json deserialize error: {}", _0)] Deserialize(serde_json::Error), /// Content type error. - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, } diff --git a/actix-multipart/src/form/mod.rs b/actix-multipart/src/form/mod.rs index 693a45e8e..a9a1ad569 100644 --- a/actix-multipart/src/form/mod.rs +++ b/actix-multipart/src/form/mod.rs @@ -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 _}; diff --git a/actix-multipart/src/form/tempfile.rs b/actix-multipart/src/form/tempfile.rs index f329876f2..df7a2368a 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; @@ -82,7 +82,7 @@ impl<'t> FieldReader<'t> for TempFile { #[non_exhaustive] pub enum TempFileError { /// File I/O Error - #[display(fmt = "File I/O error: {}", _0)] + #[display("File I/O error: {}", _0)] FileIo(std::io::Error), } diff --git a/actix-multipart/src/form/text.rs b/actix-multipart/src/form/text.rs index 67a434ee6..c4b307f83 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; @@ -77,15 +77,15 @@ where #[non_exhaustive] pub enum TextError { /// UTF-8 decoding error. - #[display(fmt = "UTF-8 decoding error: {}", _0)] + #[display("UTF-8 decoding error: {}", _0)] Utf8Error(str::Utf8Error), /// Deserialize error. - #[display(fmt = "Plain text deserialize error: {}", _0)] + #[display("Plain text deserialize error: {}", _0)] Deserialize(serde_plain::Error), /// Content type error. - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, } diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 588320f4d..36e8b62dd 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + ## 4.9.0 ### Added diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index 1e69973d0..f97eab7c9 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -146,7 +146,7 @@ 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 } @@ -182,7 +182,7 @@ 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" } diff --git a/actix-web/src/error/mod.rs b/actix-web/src/error/mod.rs index 25535332c..fce7ca399 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), } @@ -113,30 +113,30 @@ impl ResponseError for UrlencodedError { pub enum JsonPayloadError { /// Payload size is bigger than allowed & content length header set. (default: 2MB) #[display( - fmt = "JSON payload ({} bytes) is larger than allowed (limit: {} bytes).", + "JSON payload ({} bytes) is larger than allowed (limit: {} bytes).", length, limit )] OverflowKnownLength { length: usize, limit: usize }, /// Payload size is bigger than allowed but no content length header set. (default: 2MB) - #[display(fmt = "JSON payload has exceeded limit ({} bytes).", limit)] + #[display("JSON payload has exceeded limit ({} bytes).", limit)] Overflow { limit: usize }, /// Content type error - #[display(fmt = "Content type error")] + #[display("Content type error")] ContentType, /// Deserialize error - #[display(fmt = "Json deserialize error: {}", _0)] + #[display("Json deserialize error: {}", _0)] Deserialize(JsonError), /// Serialize error - #[display(fmt = "Json serialize error: {}", _0)] + #[display("Json serialize error: {}", _0)] Serialize(JsonError), /// Payload error - #[display(fmt = "Error that occur during reading payload: {}", _0)] + #[display("Error that occur during reading payload: {}", _0)] Payload(PayloadError), } @@ -166,7 +166,7 @@ impl ResponseError for JsonPayloadError { #[non_exhaustive] pub enum PathError { /// Deserialize error - #[display(fmt = "Path deserialize error: {}", _0)] + #[display("Path deserialize error: {}", _0)] Deserialize(serde::de::value::Error), } @@ -182,7 +182,7 @@ impl ResponseError for PathError { #[non_exhaustive] pub enum QueryPayloadError { /// Query deserialize error. - #[display(fmt = "Query deserialize error: {}", _0)] + #[display("Query deserialize error: {}", _0)] Deserialize(serde::de::value::Error), } @@ -196,20 +196,20 @@ impl ResponseError for QueryPayloadError { #[derive(Debug, Display, Error, From)] #[non_exhaustive] pub enum ReadlinesError { - #[display(fmt = "Encoding error")] + #[display("Encoding error")] /// Payload size is bigger than allowed. (default: 256kB) EncodingError, /// Payload error. - #[display(fmt = "Error that occur during reading payload: {}", _0)] + #[display("Error that occur during reading payload: {}", _0)] Payload(PayloadError), /// Line limit exceeded. - #[display(fmt = "Line limit exceeded")] + #[display("Line limit exceeded")] LimitOverflow, /// ContentType error. - #[display(fmt = "Content-type error")] + #[display("Content-type error")] ContentTypeError(ContentTypeError), } diff --git a/actix-web/src/http/header/content_length.rs b/actix-web/src/http/header/content_length.rs index 557c7c9f5..734f4ef38 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/info.rs b/actix-web/src/info.rs index 1b2e554f9..0655a3df2 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}, @@ -235,7 +235,7 @@ impl FromRequest for ConnectionInfo { /// # let _svc = actix_web::web::to(handler); /// ``` #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)] -#[display(fmt = "{}", _0)] +#[display("{}", _0)] pub struct PeerAddr(pub SocketAddr); impl PeerAddr { @@ -247,7 +247,7 @@ impl PeerAddr { #[derive(Debug, Display, Error)] #[non_exhaustive] -#[display(fmt = "Missing peer address")] +#[display("Missing peer address")] pub struct MissingPeerAddr; impl ResponseError for MissingPeerAddr {} diff --git a/actix-web/src/types/path.rs b/actix-web/src/types/path.rs index d6cf186f6..ada72a72e 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::{ @@ -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 e5505131e..a8e23f78c 100644 --- a/actix-web/src/types/query.rs +++ b/actix-web/src/types/query.rs @@ -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/awc/CHANGES.md b/awc/CHANGES.md index 9c0f3b607..a2e51c8d2 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Minimum supported Rust version (MSRV) is now 1.75. + ## 3.5.1 - Fix WebSocket `Host` request header value when using a non-default port. diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 26b483bfa..849ca571f 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -106,7 +106,7 @@ 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.26" diff --git a/awc/src/client/error.rs b/awc/src/client/error.rs index 22cf2c200..81543fb7d 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), } @@ -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/error.rs b/awc/src/error.rs index 0104e5fe8..59dbc8b67 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/sender.rs b/awc/src/sender.rs index 0015743bd..b676ebf28 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; From c05572399799e8c2d961209a03026eb291166c42 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 18 Aug 2024 15:54:36 +0100 Subject: [PATCH 107/129] fix(awc): prevent panics in pool drop for h1 connections (#3448) --- awc/CHANGES.md | 1 + awc/src/client/pool.rs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/awc/CHANGES.md b/awc/CHANGES.md index a2e51c8d2..11cb150ab 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased +- Prevent panics on connection pool drop when Tokio runtime is shutdown early. - Minimum supported Rust version (MSRV) is now 1.75. ## 3.5.1 diff --git a/awc/src/client/pool.rs b/awc/src/client/pool.rs index 1736f2b02..5d764f729 100644 --- a/awc/src/client/pool.rs +++ b/awc/src/client/pool.rs @@ -76,7 +76,9 @@ where 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)); + } } } } From b4f8bda0329a96009521999eb23ac9f874880504 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 19 Aug 2024 02:24:23 +0100 Subject: [PATCH 108/129] build(deps): bump taiki-e/install-action from 2.42.22 to 2.42.26 (#3456) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.22 to 2.42.26. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.22...v2.42.26) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index f94f0b20b..69e14ebb3 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f4a526e38..317577a56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 8282ac18e..b6fa35559 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d105b67d5..418432a16 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.22 + uses: taiki-e/install-action@v2.42.26 with: tool: cargo-public-api From b52e77beb4e44db9438f037c498e2a6123e84739 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 02:00:33 +0100 Subject: [PATCH 109/129] build(deps): bump taiki-e/install-action from 2.42.26 to 2.42.33 (#3459) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.26 to 2.42.33. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.26...v2.42.33) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 69e14ebb3..b2be7d342 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 317577a56..434612877 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index b6fa35559..4d412670b 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 418432a16..2448d9a41 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.26 + uses: taiki-e/install-action@v2.42.33 with: tool: cargo-public-api From bb13f541800f8892edbfd34ad2e0cbafb05bf8d9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 2 Sep 2024 02:13:30 +0100 Subject: [PATCH 110/129] build(deps): bump taiki-e/install-action from 2.42.33 to 2.42.37 (#3464) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.33 to 2.42.37. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.33...v2.42.37) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index b2be7d342..a976e6a30 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 434612877..7b05e4ce4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 4d412670b..4d7fe348b 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2448d9a41..1c668477a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.33 + uses: taiki-e/install-action@v2.42.37 with: tool: cargo-public-api From 48aaf41638e86ef932a4003c8153d714f30d47bd Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:15:07 +0100 Subject: [PATCH 111/129] build(deps): bump taiki-e/install-action from 2.42.37 to 2.43.1 (#3465) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.42.37 to 2.43.1. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.42.37...v2.43.1) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index a976e6a30..7473fb96b 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b05e4ce4..f3582606b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 4d7fe348b..876769a96 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1c668477a..82f3858d1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -76,7 +76,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: just @@ -105,7 +105,7 @@ jobs: toolchain: nightly-2024-06-07 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.42.37 + uses: taiki-e/install-action@v2.43.1 with: tool: cargo-public-api From 7360c732b3089fd4fcf51658b0411c9ca55fbc4e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 1 Oct 2024 04:50:09 +0100 Subject: [PATCH 112/129] ci: downgrade parse-size for msrv --- justfile | 1 + 1 file changed, 1 insertion(+) diff --git a/justfile b/justfile index 5cd56b12e..2de247a6a 100644 --- a/justfile +++ b/justfile @@ -10,6 +10,7 @@ fmt: # 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 := ``` From d148e84aba0da0a0d10afb0ea818ec5782fff3ea Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 1 Oct 2024 05:01:32 +0100 Subject: [PATCH 113/129] ci: fix nightly toolchain requirements --- .github/workflows/lint.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 82f3858d1..87e32d7e8 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -70,10 +70,10 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Install Rust (nightly-2024-05-01) + - name: Install Rust (nightly-2024-09-30) uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: - toolchain: nightly-2024-05-01 + toolchain: nightly-2024-09-30 - name: Install just uses: taiki-e/install-action@v2.43.1 @@ -86,7 +86,7 @@ jobs: tool: cargo-check-external-types - name: check external types - run: just check-external-types-all +nightly-2024-05-01 + run: just check-external-types-all +nightly-2024-09-30 public-api-diff: runs-on: ubuntu-latest @@ -99,10 +99,10 @@ jobs: - name: Checkout PR branch uses: actions/checkout@v4 - - name: Install Rust (nightly-2024-06-07) + - name: Install Rust (nightly-2024-09-30) uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: - toolchain: nightly-2024-06-07 + toolchain: nightly-2024-09-30 - name: Install cargo-public-api uses: taiki-e/install-action@v2.43.1 From 93edef8fee0cdcd7e1599d5bd116d65f56548c7b Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 1 Oct 2024 05:13:20 +0100 Subject: [PATCH 114/129] ci: disable check-external-types job --- .github/workflows/lint.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 87e32d7e8..68e8b7c77 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -66,14 +66,15 @@ jobs: 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-09-30) + - name: Install Rust (nightly-2024-05-01) uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 with: - toolchain: nightly-2024-09-30 + toolchain: nightly-2024-05-01 - name: Install just uses: taiki-e/install-action@v2.43.1 @@ -86,7 +87,7 @@ jobs: tool: cargo-check-external-types - name: check external types - run: just check-external-types-all +nightly-2024-09-30 + run: just check-external-types-all +nightly-2024-05-01 public-api-diff: runs-on: ubuntu-latest From 9a685cabad9785ff8cd8baf3379e4bb9fa7b3ac0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 04:31:19 +0000 Subject: [PATCH 115/129] build(deps): bump taiki-e/install-action from 2.43.1 to 2.44.15 (#3476) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.43.1 to 2.44.15. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.43.1...v2.44.15) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 7473fb96b..f56e9b0c0 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f3582606b..e2735cde4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 876769a96..1a4bef793 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 68e8b7c77..738a405b5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -77,7 +77,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: just @@ -106,7 +106,7 @@ jobs: toolchain: nightly-2024-09-30 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.43.1 + uses: taiki-e/install-action@v2.44.15 with: tool: cargo-public-api From d9d22825d44576512863a4abe8933fc67129ea83 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 04:32:19 +0000 Subject: [PATCH 116/129] build(deps): bump actions-rust-lang/setup-rust-toolchain from 1.9.0 to 1.10.0 (#3471) build(deps): bump actions-rust-lang/setup-rust-toolchain Bumps [actions-rust-lang/setup-rust-toolchain](https://github.com/actions-rust-lang/setup-rust-toolchain) from 1.9.0 to 1.10.0. - [Release notes](https://github.com/actions-rust-lang/setup-rust-toolchain/releases) - [Changelog](https://github.com/actions-rust-lang/setup-rust-toolchain/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions-rust-lang/setup-rust-toolchain/compare/v1.9.0...v1.10.0) --- updated-dependencies: - dependency-name: actions-rust-lang/setup-rust-toolchain dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 6 +++--- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index f56e9b0c0..c0a2676a4 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -44,7 +44,7 @@ jobs: echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV - name: Install Rust (${{ matrix.version.name }}) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: ${{ matrix.version.version }} @@ -80,7 +80,7 @@ jobs: uses: rui314/setup-mold@v1 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 - name: Install just, cargo-hack uses: taiki-e/install-action@v2.44.15 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e2735cde4..b3164b57b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,7 +59,7 @@ jobs: uses: rui314/setup-mold@v1 - name: Install Rust (${{ matrix.version.name }}) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: ${{ matrix.version.version }} @@ -92,7 +92,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly @@ -108,7 +108,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 1a4bef793..63797507c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly components: llvm-tools diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 738a405b5..7325e87fc 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.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly components: rustfmt @@ -36,7 +36,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: components: clippy @@ -55,7 +55,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly components: rust-docs @@ -72,7 +72,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly-2024-05-01) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly-2024-05-01 @@ -101,7 +101,7 @@ jobs: uses: actions/checkout@v4 - name: Install Rust (nightly-2024-09-30) - uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 with: toolchain: nightly-2024-09-30 From 1c4e265a708b7ed600d96c8e03f3ac0e5b801502 Mon Sep 17 00:00:00 2001 From: "Bryan A. Jones" <2125584+bjones1@users.noreply.github.com> Date: Tue, 1 Oct 2024 02:08:34 -0500 Subject: [PATCH 117/129] Set `SO_REUSEADDR` only non-Windows platforms (#3473) * Fix: Per discussion in #2958, set `SO_REUSEADDR` only non-Windows platforms. Add: Tests confirming that only a single webserver instance may bind to a given address. * Clean: Lint. * Clean: another guess at making the formatter happy. * Clean: More cargo fmt fun. (Running cargo fmt locally doesn't help.) --------- Co-authored-by: Bryan A. Jones Co-authored-by: Rob Ede --- actix-web/CHANGES.md | 1 + actix-web/src/server.rs | 5 ++++- actix-web/tests/test_httpserver.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 36e8b62dd..aca4ccfae 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -3,6 +3,7 @@ ## Unreleased - Minimum supported Rust version (MSRV) is now 1.75. +- On Windows platforms, produce an error when invoking `HttpServer::bind` on a socket that's already in use. See [issue 2958](https://github.com/actix/actix-web/issues/2958). ## 4.9.0 diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index d8519fb9e..4fbeb0186 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -1085,7 +1085,10 @@ fn create_tcp_listener(addr: net::SocketAddr, backlog: u32) -> io::Result Date: Mon, 7 Oct 2024 00:44:11 +0000 Subject: [PATCH 118/129] build(deps): bump taiki-e/install-action from 2.44.15 to 2.44.24 (#3480) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.44.15 to 2.44.24. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.44.15...v2.44.24) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index c0a2676a4..6ef1e89dd 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3164b57b..63c628b88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 63797507c..f48fbe5de 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 7325e87fc..13d0fff0c 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -77,7 +77,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: just @@ -106,7 +106,7 @@ jobs: toolchain: nightly-2024-09-30 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.44.15 + uses: taiki-e/install-action@v2.44.24 with: tool: cargo-public-api From a0a6761bfe1c9f6b59e21082f69d05c81f905495 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 00:44:29 +0000 Subject: [PATCH 119/129] build(deps): bump actions-rust-lang/setup-rust-toolchain from 1.10.0 to 1.10.1 (#3479) build(deps): bump actions-rust-lang/setup-rust-toolchain Bumps [actions-rust-lang/setup-rust-toolchain](https://github.com/actions-rust-lang/setup-rust-toolchain) from 1.10.0 to 1.10.1. - [Release notes](https://github.com/actions-rust-lang/setup-rust-toolchain/releases) - [Changelog](https://github.com/actions-rust-lang/setup-rust-toolchain/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions-rust-lang/setup-rust-toolchain/compare/v1.10.0...v1.10.1) --- updated-dependencies: - dependency-name: actions-rust-lang/setup-rust-toolchain dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 6 +++--- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 6ef1e89dd..76cad6c17 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -44,7 +44,7 @@ jobs: echo "RUSTFLAGS=-C target-feature=+crt-static" >> $GITHUB_ENV - name: Install Rust (${{ matrix.version.name }}) - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: ${{ matrix.version.version }} @@ -80,7 +80,7 @@ jobs: uses: rui314/setup-mold@v1 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 - name: Install just, cargo-hack uses: taiki-e/install-action@v2.44.24 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 63c628b88..f30590708 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,7 +59,7 @@ jobs: uses: rui314/setup-mold@v1 - name: Install Rust (${{ matrix.version.name }}) - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: ${{ matrix.version.version }} @@ -92,7 +92,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: nightly @@ -108,7 +108,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: nightly diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index f48fbe5de..c64f104e7 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly) - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: nightly components: llvm-tools diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 13d0fff0c..ab9e3a8e4 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.10.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.10.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.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: nightly components: rust-docs @@ -72,7 +72,7 @@ jobs: - uses: actions/checkout@v4 - name: Install Rust (nightly-2024-05-01) - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: nightly-2024-05-01 @@ -101,7 +101,7 @@ jobs: uses: actions/checkout@v4 - name: Install Rust (nightly-2024-09-30) - uses: actions-rust-lang/setup-rust-toolchain@v1.10.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 with: toolchain: nightly-2024-09-30 From b7a0ff0a3a35741093a71381ac887246341bcbe1 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 00:45:37 +0000 Subject: [PATCH 120/129] build(deps): bump codecov/codecov-action from 4.5.0 to 4.6.0 (#3481) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.5.0 to 4.6.0. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v4.5.0...v4.6.0) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index c64f104e7..cb713608c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -32,7 +32,7 @@ jobs: run: just test-coverage-codecov - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4.5.0 + uses: codecov/codecov-action@v4.6.0 with: files: codecov.json fail_ci_if_error: true From 049b49290d87859f1e6ac57374aa69387589ab90 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 7 Oct 2024 21:23:17 +0100 Subject: [PATCH 121/129] docs: fix long paragraph warning --- actix-web/src/http/header/content_disposition.rs | 15 ++++++++++----- justfile | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/actix-web/src/http/header/content_disposition.rs b/actix-web/src/http/header/content_disposition.rs index 592fc9f6a..ff3bb6761 100644 --- a/actix-web/src/http/header/content_disposition.rs +++ b/actix-web/src/http/header/content_disposition.rs @@ -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 diff --git a/justfile b/justfile index 2de247a6a..4ab2796c0 100644 --- a/justfile +++ b/justfile @@ -73,6 +73,7 @@ test-coverage-lcov toolchain="": (test-coverage toolchain) # Document crates in workspace. 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] From a5c2d0531b30bc6f80beba13dc2916b50e9787ea Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 20:40:14 +0000 Subject: [PATCH 122/129] build(deps): update brotli requirement from 6 to 7 (#3482) * build(deps): update brotli requirement from 6 to 7 Updates the requirements on [brotli](https://github.com/dropbox/rust-brotli) to permit the latest version. - [Release notes](https://github.com/dropbox/rust-brotli/releases) - [Commits](https://github.com/dropbox/rust-brotli/commits) --- updated-dependencies: - dependency-name: brotli dependency-type: direct:production ... Signed-off-by: dependabot[bot] * docs: update changelogs --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Rob Ede --- actix-http/CHANGES.md | 1 + actix-http/Cargo.toml | 2 +- actix-web/CHANGES.md | 3 ++- actix-web/Cargo.toml | 2 +- awc/CHANGES.md | 1 + awc/Cargo.toml | 2 +- 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 95b51254b..2bbcebc07 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased +- Update `brotli` dependency to `7`. - Minimum supported Rust version (MSRV) is now 1.75. ## 3.9.0 diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index f910276f1..3f81ea9f0 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -139,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 } diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index aca4ccfae..cee14dc4b 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -2,8 +2,9 @@ ## 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. -- On Windows platforms, produce an error when invoking `HttpServer::bind` on a socket that's already in use. See [issue 2958](https://github.com/actix/actix-web/issues/2958). ## 4.9.0 diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index f97eab7c9..ab70bf07c 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -172,7 +172,7 @@ 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"] } diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 11cb150ab..8a2a1ec43 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,6 +2,7 @@ ## 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. diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 849ca571f..c09f32ac8 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -141,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" From 3849cdaa6c525501bf5666a34b429f46dbb9529d Mon Sep 17 00:00:00 2001 From: Luca Iachini <77624486+luca-iachini@users.noreply.github.com> Date: Mon, 7 Oct 2024 23:16:10 +0200 Subject: [PATCH 123/129] Improve worker_max_blocking_threads documentation (#3477) * improve worker_max_blocking_threads doc * docs: tweak doc --------- Co-authored-by: Rob Ede --- actix-web/src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index 4fbeb0186..1ea4de4ca 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -193,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 From 27c07f122bafcd26a9ef688f0689340f8718294c Mon Sep 17 00:00:00 2001 From: Durairaj Subramaniam Date: Mon, 7 Oct 2024 23:03:38 +0100 Subject: [PATCH 124/129] fix: service macro comments (#3474) * fix: service macro comments #3472 * test: service macro comments #3472 * fix: add case for empty tuple seperately * doc: add case for empty tuple seperately * test: move test_services into lib --------- Co-authored-by: Rob Ede --- actix-web/src/service.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/actix-web/src/service.rs b/actix-web/src/service.rs index a1672eba2..6c7f6f5c8 100644 --- a/actix-web/src/service.rs +++ b/actix-web/src/service.rs @@ -662,6 +662,7 @@ where /// ``` #[macro_export] macro_rules! services { + () => {()}; ($($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)); + } } From 4c05c87b11a9a4264d668f78e0c086bfb396a313 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:07:05 +0200 Subject: [PATCH 125/129] build(deps): bump taiki-e/install-action from 2.44.24 to 2.44.34 (#3485) --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 76cad6c17..3b3bbee95 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f30590708..c17afd635 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index cb713608c..85aee1c4f 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ab9e3a8e4..a889da2bb 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -77,7 +77,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: just @@ -106,7 +106,7 @@ jobs: toolchain: nightly-2024-09-30 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.44.24 + uses: taiki-e/install-action@v2.44.34 with: tool: cargo-public-api From ec05381f6f8644ff97880b000b592b58e5a23c2e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 15 Oct 2024 07:00:04 +0100 Subject: [PATCH 126/129] feat: add CLEAR_SITE_DATA header --- actix-http/CHANGES.md | 6 ++++++ actix-http/src/header/common.rs | 8 ++++++++ actix-http/src/header/mod.rs | 6 +++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 2bbcebc07..982add26a 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,12 @@ ## Unreleased +### Added + +- Add `header::CLEAR_SITE_DATA` constant. + +### Changed + - Update `brotli` dependency to `7`. - Minimum supported Rust version (MSRV) is now 1.75. diff --git a/actix-http/src/header/common.rs b/actix-http/src/header/common.rs index 6942dc26a..ebdd6708f 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/mod.rs b/actix-http/src/header/mod.rs index 79f91afef..b22c43f76 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, From 03c65d93e54ee9cf473bcd7112b98ae584dbe41f Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 15 Oct 2024 08:35:39 +0100 Subject: [PATCH 127/129] docs: add from_fn examples --- actix-web/Cargo.toml | 1 + actix-web/examples/from_fn.rs | 128 ++++++++++++++++++++++++++++++++++ justfile | 3 + 3 files changed, 132 insertions(+) create mode 100644 actix-web/examples/from_fn.rs diff --git a/actix-web/Cargo.toml b/actix-web/Cargo.toml index ab70bf07c..9b10f14b1 100644 --- a/actix-web/Cargo.toml +++ b/actix-web/Cargo.toml @@ -163,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" diff --git a/actix-web/examples/from_fn.rs b/actix-web/examples/from_fn.rs new file mode 100644 index 000000000..a6006d23c --- /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/justfile b/justfile index 4ab2796c0..a970b62e7 100644 --- a/justfile +++ b/justfile @@ -36,6 +36,9 @@ check-min: 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 }} From 568bffeb58db083d63d887bd0f1cceaa0464d1c9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:19:31 +0100 Subject: [PATCH 128/129] build(deps): bump taiki-e/install-action from 2.44.34 to 2.44.43 (#3488) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.44.34 to 2.44.43. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.44.34...v2.44.43) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 3b3bbee95..e8c260921 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c17afd635..522d20014 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 85aee1c4f..5615ecefe 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index a889da2bb..1b2c5a24d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -77,7 +77,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: just @@ -106,7 +106,7 @@ jobs: toolchain: nightly-2024-09-30 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.44.34 + uses: taiki-e/install-action@v2.44.43 with: tool: cargo-public-api From ef977055fcba8aa6b9fa42686764c87c2d701caf Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:48:34 +0000 Subject: [PATCH 129/129] build(deps): bump taiki-e/install-action from 2.44.43 to 2.44.60 (#3494) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/coverage.yml | 2 +- .github/workflows/lint.yml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index e8c260921..f0ed2173b 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -49,7 +49,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -83,7 +83,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.10.1 - name: Install just, cargo-hack - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: just,cargo-hack diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 522d20014..b04213dc3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean @@ -113,7 +113,7 @@ jobs: toolchain: nightly - name: Install just - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: just diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 5615ecefe..1d4e580c9 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -24,7 +24,7 @@ jobs: components: llvm-tools - name: Install just, cargo-llvm-cov, cargo-nextest - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: just,cargo-llvm-cov,cargo-nextest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1b2c5a24d..94380aee5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -77,7 +77,7 @@ jobs: toolchain: nightly-2024-05-01 - name: Install just - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: just @@ -106,7 +106,7 @@ jobs: toolchain: nightly-2024-09-30 - name: Install cargo-public-api - uses: taiki-e/install-action@v2.44.43 + uses: taiki-e/install-action@v2.44.60 with: tool: cargo-public-api