mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-06 10:50:17 +02:00
Compare commits
29 Commits
files-v0.6
...
codegen-v0
Author | SHA1 | Date | |
---|---|---|---|
8bbf2b5052 | |||
8c975bcc1f | |||
742ad56d30 | |||
bcc8d5c441 | |||
f659098d21 | |||
8621ae12f8 | |||
b338eb8473 | |||
5abd1c2c2c | |||
05336269f9 | |||
86df295ee2 | |||
85c9b1a263 | |||
577597a80a | |||
374dc9bfc9 | |||
93754f307f | |||
c7639bc3be | |||
0bc4ae9158 | |||
19a46e3925 | |||
68cd853aa2 | |||
25fe1bbaa5 | |||
e890307091 | |||
b708924590 | |||
5dcb250237 | |||
b4ff6addfe | |||
231a24ef8d | |||
6df4974234 | |||
a80e93d6db | |||
542c92c9a7 | |||
74738c63a7 | |||
a87e01f0d1 |
2
.github/workflows/bench.yml
vendored
2
.github/workflows/bench.yml
vendored
@ -1,8 +1,6 @@
|
||||
name: Benchmark
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
87
.github/workflows/ci-master.yml
vendored
87
.github/workflows/ci-master.yml
vendored
@ -5,6 +5,93 @@ on:
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
build_and_test_nightly:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target:
|
||||
- { name: Linux, os: ubuntu-latest, triple: x86_64-unknown-linux-gnu }
|
||||
- { name: macOS, os: macos-latest, triple: x86_64-apple-darwin }
|
||||
- { name: Windows, os: windows-2022, triple: x86_64-pc-windows-msvc }
|
||||
version:
|
||||
- nightly
|
||||
|
||||
name: ${{ matrix.target.name }} / ${{ matrix.version }}
|
||||
runs-on: ${{ matrix.target.os }}
|
||||
|
||||
env:
|
||||
CI: 1
|
||||
CARGO_INCREMENTAL: 0
|
||||
VCPKGRS_DYNAMIC: 1
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
# install OpenSSL on Windows
|
||||
# TODO: GitHub actions docs state that OpenSSL is
|
||||
# already installed on these Windows machines somewhere
|
||||
- name: Set vcpkg root
|
||||
if: matrix.target.triple == 'x86_64-pc-windows-msvc'
|
||||
run: echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append
|
||||
- name: Install OpenSSL
|
||||
if: matrix.target.triple == 'x86_64-pc-windows-msvc'
|
||||
run: vcpkg install openssl:x64-windows
|
||||
|
||||
- name: Install ${{ matrix.version }}
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ matrix.version }}-${{ matrix.target.triple }}
|
||||
profile: minimal
|
||||
override: true
|
||||
|
||||
- name: Generate Cargo.lock
|
||||
uses: actions-rs/cargo@v1
|
||||
with: { command: generate-lockfile }
|
||||
- name: Cache Dependencies
|
||||
uses: Swatinem/rust-cache@v1.2.0
|
||||
|
||||
- name: Install cargo-hack
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: install
|
||||
args: cargo-hack
|
||||
|
||||
- name: check minimal
|
||||
uses: actions-rs/cargo@v1
|
||||
with: { command: ci-check-min }
|
||||
|
||||
- name: check default
|
||||
uses: actions-rs/cargo@v1
|
||||
with: { command: ci-check-default }
|
||||
|
||||
- name: tests
|
||||
timeout-minutes: 60
|
||||
run: |
|
||||
cargo test --lib --tests -p=actix-router --all-features
|
||||
cargo test --lib --tests -p=actix-http --all-features
|
||||
cargo test --lib --tests -p=actix-web --features=rustls,openssl -- --skip=test_reading_deflate_encoding_large_random_rustls
|
||||
cargo test --lib --tests -p=actix-web-codegen --all-features
|
||||
cargo test --lib --tests -p=awc --all-features
|
||||
cargo test --lib --tests -p=actix-http-test --all-features
|
||||
cargo test --lib --tests -p=actix-test --all-features
|
||||
cargo test --lib --tests -p=actix-files
|
||||
cargo test --lib --tests -p=actix-multipart --all-features
|
||||
cargo test --lib --tests -p=actix-web-actors --all-features
|
||||
|
||||
- name: tests (io-uring)
|
||||
if: matrix.target.os == 'ubuntu-latest'
|
||||
timeout-minutes: 60
|
||||
run: >
|
||||
sudo bash -c "ulimit -Sl 512
|
||||
&& ulimit -Hl 512
|
||||
&& PATH=$PATH:/usr/share/rust/.cargo/bin
|
||||
&& RUSTUP_TOOLCHAIN=${{ matrix.version }} cargo test --lib --tests -p=actix-files --all-features"
|
||||
|
||||
- name: Clear the cargo caches
|
||||
run: |
|
||||
cargo install cargo-cache --version 0.6.3 --no-default-features --features ci-autoclean
|
||||
cargo-cache
|
||||
|
||||
ci_feature_powerset_check:
|
||||
name: Verify Feature Combinations
|
||||
runs-on: ubuntu-latest
|
||||
|
3
.github/workflows/ci.yml
vendored
3
.github/workflows/ci.yml
vendored
@ -16,9 +16,8 @@ jobs:
|
||||
- { name: macOS, os: macos-latest, triple: x86_64-apple-darwin }
|
||||
- { name: Windows, os: windows-2022, triple: x86_64-pc-windows-msvc }
|
||||
version:
|
||||
- 1.52.0 # MSRV
|
||||
- 1.54.0 # MSRV
|
||||
- stable
|
||||
- nightly
|
||||
|
||||
name: ${{ matrix.target.name }} / ${{ matrix.version }}
|
||||
runs-on: ${{ matrix.target.os }}
|
||||
|
11
.github/workflows/clippy-fmt.yml
vendored
11
.github/workflows/clippy-fmt.yml
vendored
@ -14,6 +14,7 @@ jobs:
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
profile: minimal
|
||||
components: rustfmt
|
||||
- name: Check with rustfmt
|
||||
uses: actions-rs/cargo@v1
|
||||
@ -30,10 +31,18 @@ jobs:
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
profile: minimal
|
||||
components: clippy
|
||||
override: true
|
||||
|
||||
- name: Generate Cargo.lock
|
||||
uses: actions-rs/cargo@v1
|
||||
with: { command: generate-lockfile }
|
||||
- name: Cache Dependencies
|
||||
uses: Swatinem/rust-cache@v1.2.0
|
||||
|
||||
- name: Check with Clippy
|
||||
uses: actions-rs/clippy-check@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
args: --workspace --all-features --tests
|
||||
args: --workspace --tests --examples --all-features
|
||||
|
33
CHANGES.md
33
CHANGES.md
@ -3,6 +3,39 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 4.0.0-beta.19 - 2022-01-04
|
||||
### Added
|
||||
- `impl Hash` for `http::header::Encoding`. [#2501]
|
||||
- `AcceptEncoding::negotiate()`. [#2501]
|
||||
|
||||
### Changed
|
||||
- `AcceptEncoding::preference` now returns `Option<Preference<Encoding>>`. [#2501]
|
||||
- Rename methods `BodyEncoding::{encoding => encode_with, get_encoding => preferred_encoding}`. [#2501]
|
||||
- `http::header::Encoding` now only represents `Content-Encoding` types. [#2501]
|
||||
|
||||
### Fixed
|
||||
- Auto-negotiation of content encoding is more fault-tolerant when using the `Compress` middleware. [#2501]
|
||||
|
||||
### Removed
|
||||
- `Compress::new`; restricting compression algorithm is done through feature flags. [#2501]
|
||||
- `BodyEncoding` trait; signalling content encoding is now only done via the `Content-Encoding` header. [#2565]
|
||||
|
||||
[#2501]: https://github.com/actix/actix-web/pull/2501
|
||||
[#2565]: https://github.com/actix/actix-web/pull/2565
|
||||
|
||||
|
||||
## 4.0.0-beta.18 - 2021-12-29
|
||||
### Changed
|
||||
- Update `cookie` dependency (re-exported) to `0.16`. [#2555]
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
### Security
|
||||
- `cookie` upgrade addresses [`RUSTSEC-2020-0071`].
|
||||
|
||||
[#2555]: https://github.com/actix/actix-web/pull/2555
|
||||
[`RUSTSEC-2020-0071`]: https://rustsec.org/advisories/RUSTSEC-2020-0071.html
|
||||
|
||||
|
||||
## 4.0.0-beta.17 - 2021-12-29
|
||||
### Added
|
||||
- `guard::GuardContext` for use with the `Guard` trait. [#2552]
|
||||
|
29
Cargo.toml
29
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web"
|
||||
version = "4.0.0-beta.17"
|
||||
version = "4.0.0-beta.19"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
|
||||
keywords = ["actix", "http", "web", "framework", "async"]
|
||||
@ -28,15 +28,15 @@ path = "src/lib.rs"
|
||||
resolver = "2"
|
||||
members = [
|
||||
".",
|
||||
"awc",
|
||||
"actix-http",
|
||||
"actix-files",
|
||||
"actix-http-test",
|
||||
"actix-http",
|
||||
"actix-multipart",
|
||||
"actix-router",
|
||||
"actix-test",
|
||||
"actix-web-actors",
|
||||
"actix-web-codegen",
|
||||
"actix-http-test",
|
||||
"actix-test",
|
||||
"actix-router",
|
||||
"awc",
|
||||
]
|
||||
|
||||
[features]
|
||||
@ -77,14 +77,14 @@ actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
actix-tls = { version = "3.0.0", default-features = false, optional = true }
|
||||
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-router = "0.5.0-beta.3"
|
||||
actix-web-codegen = "0.5.0-beta.6"
|
||||
actix-http = "3.0.0-beta.18"
|
||||
actix-router = "0.5.0-beta.4"
|
||||
actix-web-codegen = "0.5.0-rc.1"
|
||||
|
||||
ahash = "0.7"
|
||||
bytes = "1"
|
||||
cfg-if = "1"
|
||||
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
|
||||
cookie = { version = "0.16", features = ["percent-encode"], optional = true }
|
||||
derive_more = "0.99.5"
|
||||
encoding_rs = "0.8"
|
||||
futures-core = { version = "0.3.7", default-features = false }
|
||||
@ -94,7 +94,6 @@ language-tags = "0.3"
|
||||
once_cell = "1.5"
|
||||
log = "0.4"
|
||||
mime = "0.3"
|
||||
paste = "1"
|
||||
pin-project-lite = "0.2.7"
|
||||
regex = "1.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
@ -106,10 +105,12 @@ time = { version = "0.3", default-features = false, features = ["formatting"] }
|
||||
url = "2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-test = { version = "0.1.0-beta.10", features = ["openssl", "rustls"] }
|
||||
awc = { version = "3.0.0-beta.16", features = ["openssl"] }
|
||||
actix-files = "0.6.0-beta.13"
|
||||
actix-test = { version = "0.1.0-beta.11", features = ["openssl", "rustls"] }
|
||||
awc = { version = "3.0.0-beta.18", features = ["openssl"] }
|
||||
|
||||
brotli2 = "0.3.2"
|
||||
const-str = "0.3"
|
||||
criterion = { version = "0.3", features = ["html_reports"] }
|
||||
env_logger = "0.9"
|
||||
flate2 = "1.0.13"
|
||||
@ -164,7 +165,7 @@ name = "uds"
|
||||
required-features = ["compress-gzip"]
|
||||
|
||||
[[example]]
|
||||
name = "on_connect"
|
||||
name = "on-connect"
|
||||
required-features = []
|
||||
|
||||
[[bench]]
|
||||
|
10
README.md
10
README.md
@ -6,12 +6,12 @@
|
||||
<p>
|
||||
|
||||
[](https://crates.io/crates/actix-web)
|
||||
[](https://docs.rs/actix-web/4.0.0-beta.17)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-web/4.0.0-beta.19)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

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

|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
@ -32,7 +32,7 @@
|
||||
- SSL support using OpenSSL or Rustls
|
||||
- Middlewares ([Logger, Session, CORS, etc](https://actix.rs/docs/middleware/))
|
||||
- Includes an async [HTTP client](https://docs.rs/awc/)
|
||||
- Runs on stable Rust 1.52+
|
||||
- Runs on stable Rust 1.54+
|
||||
|
||||
## Documentation
|
||||
|
||||
|
@ -3,12 +3,20 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.6.0-beta.13 - 2022-01-04
|
||||
- The `Files` service now rejects requests with URL paths that include `%2F` (decoded: `/`). [#2398]
|
||||
- The `Files` service now correctly decodes `%25` in the URL path to `%` for the file path. [#2398]
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
[#2398]: https://github.com/actix/actix-web/pull/2398
|
||||
|
||||
|
||||
## 0.6.0-beta.12 - 2021-12-29
|
||||
* No significant changes since `0.6.0-beta.11`.
|
||||
- No significant changes since `0.6.0-beta.11`.
|
||||
|
||||
|
||||
## 0.6.0-beta.11 - 2021-12-27
|
||||
* No significant changes since `0.6.0-beta.10`.
|
||||
- No significant changes since `0.6.0-beta.10`.
|
||||
|
||||
|
||||
## 0.6.0-beta.10 - 2021-12-11
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-files"
|
||||
version = "0.6.0-beta.12"
|
||||
version = "0.6.0-beta.13"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"fakeshadow <24548779@qq.com>",
|
||||
@ -22,10 +22,10 @@ path = "src/lib.rs"
|
||||
experimental-io-uring = ["actix-web/experimental-io-uring", "tokio-uring"]
|
||||
|
||||
[dependencies]
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-http = "3.0.0-beta.18"
|
||||
actix-service = "2"
|
||||
actix-utils = "3"
|
||||
actix-web = { version = "4.0.0-beta.17", default-features = false }
|
||||
actix-web = { version = "4.0.0-beta.19", default-features = false }
|
||||
|
||||
askama_escape = "0.10"
|
||||
bitflags = "1"
|
||||
@ -43,5 +43,6 @@ tokio-uring = { version = "0.1", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1.0-beta.10"
|
||||
actix-web = "4.0.0-beta.17"
|
||||
actix-test = "0.1.0-beta.11"
|
||||
actix-web = "4.0.0-beta.19"
|
||||
tempfile = "3.2"
|
||||
|
@ -3,11 +3,11 @@
|
||||
> Static file serving for Actix Web
|
||||
|
||||
[](https://crates.io/crates/actix-files)
|
||||
[](https://docs.rs/actix-files/0.6.0-beta.12)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-files/0.6.0-beta.13)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-files/0.6.0-beta.12)
|
||||
[](https://deps.rs/crate/actix-files/0.6.0-beta.13)
|
||||
[](https://crates.io/crates/actix-files)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
@ -15,4 +15,4 @@
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-files/)
|
||||
- [Example Project](https://github.com/actix/examples/tree/master/basics/static_index)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
@ -40,14 +40,23 @@ impl Directory {
|
||||
pub(crate) type DirectoryRenderer =
|
||||
dyn Fn(&Directory, &HttpRequest) -> Result<ServiceResponse, io::Error>;
|
||||
|
||||
// show file url as relative to static path
|
||||
/// Returns percent encoded file URL path.
|
||||
macro_rules! encode_file_url {
|
||||
($path:ident) => {
|
||||
utf8_percent_encode(&$path, CONTROLS)
|
||||
};
|
||||
}
|
||||
|
||||
// " -- " & -- & ' -- ' < -- < > -- > / -- /
|
||||
/// Returns HTML entity encoded formatter.
|
||||
///
|
||||
/// ```plain
|
||||
/// " => "
|
||||
/// & => &
|
||||
/// ' => '
|
||||
/// < => <
|
||||
/// > => >
|
||||
/// / => /
|
||||
/// ```
|
||||
macro_rules! encode_file_name {
|
||||
($entry:ident) => {
|
||||
escape_html_entity(&$entry.file_name().to_string_lossy(), Html)
|
||||
|
@ -23,16 +23,23 @@ impl ResponseError for FilesError {
|
||||
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
#[derive(Display, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum UriSegmentError {
|
||||
/// The segment started with the wrapped invalid character.
|
||||
#[display(fmt = "The segment started with the wrapped invalid character")]
|
||||
BadStart(char),
|
||||
|
||||
/// The segment contained the wrapped invalid character.
|
||||
#[display(fmt = "The segment contained the wrapped invalid character")]
|
||||
BadChar(char),
|
||||
|
||||
/// The segment ended with the wrapped invalid character.
|
||||
#[display(fmt = "The segment ended with the wrapped invalid character")]
|
||||
BadEnd(char),
|
||||
|
||||
/// The path is not a valid UTF-8 string after doing percent decoding.
|
||||
#[display(fmt = "The path is not a valid UTF-8 string after percent-decoding")]
|
||||
NotValidUtf8,
|
||||
}
|
||||
|
||||
/// Return `BadRequest` for `UriSegmentError`
|
||||
|
@ -28,6 +28,7 @@ use crate::{
|
||||
///
|
||||
/// `Files` service must be registered with `App::service()` method.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::App;
|
||||
/// use actix_files::Files;
|
||||
|
@ -597,7 +597,8 @@ mod tests {
|
||||
.to_request();
|
||||
let res = test::call_service(&srv, request).await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert!(!res.headers().contains_key(header::CONTENT_ENCODING));
|
||||
assert!(res.headers().contains_key(header::CONTENT_ENCODING));
|
||||
assert!(!test::read_body(res).await.is_empty());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
@ -802,6 +803,38 @@ mod tests {
|
||||
let req = TestRequest::get().uri("/test/%43argo.toml").to_request();
|
||||
let res = test::call_service(&srv, req).await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// `%2F` == `/`
|
||||
let req = TestRequest::get().uri("/test%2Ftest.binary").to_request();
|
||||
let res = test::call_service(&srv, req).await;
|
||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||
|
||||
let req = TestRequest::get().uri("/test/Cargo.toml%00").to_request();
|
||||
let res = test::call_service(&srv, req).await;
|
||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_percent_encoding_2() {
|
||||
let tmpdir = tempfile::tempdir().unwrap();
|
||||
let filename = match cfg!(unix) {
|
||||
true => "ض:?#[]{}<>()@!$&'`|*+,;= %20.test",
|
||||
false => "ض#[]{}()@!$&'`+,;= %20.test",
|
||||
};
|
||||
let filename_encoded = filename
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.map(|c| format!("%{:02X}", c))
|
||||
.collect::<String>();
|
||||
std::fs::File::create(tmpdir.path().join(filename)).unwrap();
|
||||
|
||||
let srv = test::init_service(App::new().service(Files::new("", tmpdir.path()))).await;
|
||||
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/{}", filename_encoded))
|
||||
.to_request();
|
||||
let res = test::call_service(&srv, req).await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
|
@ -9,14 +9,11 @@ use std::{
|
||||
use actix_service::{Service, ServiceFactory};
|
||||
use actix_web::{
|
||||
body::{self, BoxBody, SizedStream},
|
||||
dev::{
|
||||
AppService, BodyEncoding, HttpServiceFactory, ResourceDef, ServiceRequest,
|
||||
ServiceResponse,
|
||||
},
|
||||
dev::{AppService, HttpServiceFactory, ResourceDef, ServiceRequest, ServiceResponse},
|
||||
http::{
|
||||
header::{
|
||||
self, Charset, ContentDisposition, ContentEncoding, DispositionParam,
|
||||
DispositionType, ExtendedValue,
|
||||
DispositionType, ExtendedValue, HeaderValue,
|
||||
},
|
||||
StatusCode,
|
||||
},
|
||||
@ -224,7 +221,6 @@ impl NamedFile {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "experimental-io-uring"))]
|
||||
/// Attempts to open a file in read-only mode.
|
||||
///
|
||||
/// # Examples
|
||||
@ -232,6 +228,7 @@ impl NamedFile {
|
||||
/// use actix_files::NamedFile;
|
||||
/// let file = NamedFile::open("foo.txt");
|
||||
/// ```
|
||||
#[cfg(not(feature = "experimental-io-uring"))]
|
||||
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<NamedFile> {
|
||||
let file = File::open(&path)?;
|
||||
Self::from_file(file, path)
|
||||
@ -295,23 +292,21 @@ impl NamedFile {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the MIME Content-Type for serving this file. By default
|
||||
/// the Content-Type is inferred from the filename extension.
|
||||
/// Set the MIME Content-Type for serving this file. By default the Content-Type is inferred
|
||||
/// from the filename extension.
|
||||
#[inline]
|
||||
pub fn set_content_type(mut self, mime_type: mime::Mime) -> Self {
|
||||
self.content_type = mime_type;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the Content-Disposition for serving this file. This allows
|
||||
/// changing the inline/attachment disposition as well as the filename
|
||||
/// sent to the peer.
|
||||
/// Set the Content-Disposition for serving this file. This allows changing the
|
||||
/// `inline/attachment` disposition as well as the filename sent to the peer.
|
||||
///
|
||||
/// By default the disposition is `inline` for `text/*`, `image/*`, `video/*` and
|
||||
/// `application/{javascript, json, wasm}` mime types, and `attachment` otherwise,
|
||||
/// and the filename is taken from the path provided in the `open` method
|
||||
/// after converting it to UTF-8 using.
|
||||
/// [`std::ffi::OsStr::to_string_lossy`]
|
||||
/// `application/{javascript, json, wasm}` mime types, and `attachment` otherwise, and the
|
||||
/// filename is taken from the path provided in the `open` method after converting it to UTF-8
|
||||
/// (using `to_string_lossy`).
|
||||
#[inline]
|
||||
pub fn set_content_disposition(mut self, cd: header::ContentDisposition) -> Self {
|
||||
self.content_disposition = cd;
|
||||
@ -337,7 +332,7 @@ impl NamedFile {
|
||||
self
|
||||
}
|
||||
|
||||
/// Specifies whether to use ETag or not.
|
||||
/// Specifies whether to return `ETag` header in response.
|
||||
///
|
||||
/// Default is true.
|
||||
#[inline]
|
||||
@ -346,7 +341,7 @@ impl NamedFile {
|
||||
self
|
||||
}
|
||||
|
||||
/// Specifies whether to use Last-Modified or not.
|
||||
/// Specifies whether to return `Last-Modified` header in response.
|
||||
///
|
||||
/// Default is true.
|
||||
#[inline]
|
||||
@ -364,7 +359,7 @@ impl NamedFile {
|
||||
self
|
||||
}
|
||||
|
||||
/// Creates a etag in a format is similar to Apache's.
|
||||
/// Creates an `ETag` in a format is similar to Apache's.
|
||||
pub(crate) fn etag(&self) -> Option<header::EntityTag> {
|
||||
self.modified.as_ref().map(|mtime| {
|
||||
let ino = {
|
||||
@ -386,7 +381,7 @@ impl NamedFile {
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("modification time must be after epoch");
|
||||
|
||||
header::EntityTag::strong(format!(
|
||||
header::EntityTag::new_strong(format!(
|
||||
"{:x}:{:x}:{:x}:{:x}",
|
||||
ino,
|
||||
self.md.len(),
|
||||
@ -405,12 +400,13 @@ impl NamedFile {
|
||||
if self.status_code != StatusCode::OK {
|
||||
let mut res = HttpResponse::build(self.status_code);
|
||||
|
||||
if self.flags.contains(Flags::PREFER_UTF8) {
|
||||
let ct = equiv_utf8_text(self.content_type.clone());
|
||||
res.insert_header((header::CONTENT_TYPE, ct.to_string()));
|
||||
let ct = if self.flags.contains(Flags::PREFER_UTF8) {
|
||||
equiv_utf8_text(self.content_type.clone())
|
||||
} else {
|
||||
res.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
|
||||
}
|
||||
self.content_type
|
||||
};
|
||||
|
||||
res.insert_header((header::CONTENT_TYPE, ct.to_string()));
|
||||
|
||||
if self.flags.contains(Flags::CONTENT_DISPOSITION) {
|
||||
res.insert_header((
|
||||
@ -420,7 +416,7 @@ impl NamedFile {
|
||||
}
|
||||
|
||||
if let Some(current_encoding) = self.encoding {
|
||||
res.encoding(current_encoding);
|
||||
res.insert_header((header::CONTENT_ENCODING, current_encoding.as_str()));
|
||||
}
|
||||
|
||||
let reader = chunked::new_chunked_read(self.md.len(), 0, self.file);
|
||||
@ -478,12 +474,13 @@ impl NamedFile {
|
||||
|
||||
let mut res = HttpResponse::build(self.status_code);
|
||||
|
||||
if self.flags.contains(Flags::PREFER_UTF8) {
|
||||
let ct = equiv_utf8_text(self.content_type.clone());
|
||||
res.insert_header((header::CONTENT_TYPE, ct.to_string()));
|
||||
let ct = if self.flags.contains(Flags::PREFER_UTF8) {
|
||||
equiv_utf8_text(self.content_type.clone())
|
||||
} else {
|
||||
res.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
|
||||
}
|
||||
self.content_type
|
||||
};
|
||||
|
||||
res.insert_header((header::CONTENT_TYPE, ct.to_string()));
|
||||
|
||||
if self.flags.contains(Flags::CONTENT_DISPOSITION) {
|
||||
res.insert_header((
|
||||
@ -492,9 +489,8 @@ impl NamedFile {
|
||||
));
|
||||
}
|
||||
|
||||
// default compressing
|
||||
if let Some(current_encoding) = self.encoding {
|
||||
res.encoding(current_encoding);
|
||||
res.insert_header((header::CONTENT_ENCODING, current_encoding.as_str()));
|
||||
}
|
||||
|
||||
if let Some(lm) = last_modified {
|
||||
@ -517,7 +513,12 @@ impl NamedFile {
|
||||
length = ranges[0].length;
|
||||
offset = ranges[0].start;
|
||||
|
||||
res.encoding(ContentEncoding::Identity);
|
||||
// don't allow compression middleware to modify partial content
|
||||
res.insert_header((
|
||||
header::CONTENT_ENCODING,
|
||||
HeaderValue::from_static("identity"),
|
||||
));
|
||||
|
||||
res.insert_header((
|
||||
header::CONTENT_RANGE,
|
||||
format!("bytes {}-{}/{}", offset, offset + length - 1, self.md.len()),
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
path::{Component, Path, PathBuf},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
@ -26,8 +26,23 @@ impl PathBufWrap {
|
||||
pub fn parse_path(path: &str, hidden_files: bool) -> Result<Self, UriSegmentError> {
|
||||
let mut buf = PathBuf::new();
|
||||
|
||||
// equivalent to `path.split('/').count()`
|
||||
let mut segment_count = path.matches('/').count() + 1;
|
||||
|
||||
// we can decode the whole path here (instead of per-segment decoding)
|
||||
// because we will reject `%2F` in paths using `segement_count`.
|
||||
let path = percent_encoding::percent_decode_str(path)
|
||||
.decode_utf8()
|
||||
.map_err(|_| UriSegmentError::NotValidUtf8)?;
|
||||
|
||||
// disallow decoding `%2F` into `/`
|
||||
if segment_count != path.matches('/').count() + 1 {
|
||||
return Err(UriSegmentError::BadChar('/'));
|
||||
}
|
||||
|
||||
for segment in path.split('/') {
|
||||
if segment == ".." {
|
||||
segment_count -= 1;
|
||||
buf.pop();
|
||||
} else if !hidden_files && segment.starts_with('.') {
|
||||
return Err(UriSegmentError::BadStart('.'));
|
||||
@ -40,6 +55,7 @@ impl PathBufWrap {
|
||||
} else if segment.ends_with('<') {
|
||||
return Err(UriSegmentError::BadEnd('<'));
|
||||
} else if segment.is_empty() {
|
||||
segment_count -= 1;
|
||||
continue;
|
||||
} else if cfg!(windows) && segment.contains('\\') {
|
||||
return Err(UriSegmentError::BadChar('\\'));
|
||||
@ -48,6 +64,12 @@ impl PathBufWrap {
|
||||
}
|
||||
}
|
||||
|
||||
// make sure we agree with stdlib parser
|
||||
for (i, component) in buf.components().enumerate() {
|
||||
assert!(matches!(component, Component::Normal(_)));
|
||||
assert!(i < segment_count);
|
||||
}
|
||||
|
||||
Ok(PathBufWrap(buf))
|
||||
}
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ impl Service<ServiceRequest> for FilesService {
|
||||
Box::pin(async move {
|
||||
if !is_method_valid {
|
||||
return Ok(req.into_response(
|
||||
actix_web::HttpResponse::MethodNotAllowed()
|
||||
HttpResponse::MethodNotAllowed()
|
||||
.insert_header(header::ContentType(mime::TEXT_PLAIN_UTF_8))
|
||||
.body("Request did not meet this resource's requirements."),
|
||||
));
|
||||
@ -123,7 +123,7 @@ impl Service<ServiceRequest> for FilesService {
|
||||
let real_path =
|
||||
match PathBufWrap::parse_path(req.match_info().path(), this.hidden_files) {
|
||||
Ok(item) => item,
|
||||
Err(e) => return Ok(req.error_response(e)),
|
||||
Err(err) => return Ok(req.error_response(err)),
|
||||
};
|
||||
|
||||
if let Some(filter) = &this.path_filter {
|
||||
@ -131,9 +131,7 @@ impl Service<ServiceRequest> for FilesService {
|
||||
if let Some(ref default) = this.default {
|
||||
return default.call(req).await;
|
||||
} else {
|
||||
return Ok(
|
||||
req.into_response(actix_web::HttpResponse::NotFound().finish())
|
||||
);
|
||||
return Ok(req.into_response(HttpResponse::NotFound().finish()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,10 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.11 - 2022-01-04
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
|
||||
## 3.0.0-beta.10 - 2021-12-27
|
||||
- Update `actix-server` to `2.0.0-rc.2`. [#2550]
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-http-test"
|
||||
version = "3.0.0-beta.10"
|
||||
version = "3.0.0-beta.11"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Various helpers for Actix applications to use during testing"
|
||||
keywords = ["http", "web", "framework", "async", "futures"]
|
||||
@ -35,7 +35,7 @@ actix-tls = "3.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
actix-rt = "2.2"
|
||||
actix-server = "2.0.0-rc.2"
|
||||
awc = { version = "3.0.0-beta.16", default-features = false }
|
||||
awc = { version = "3.0.0-beta.18", default-features = false }
|
||||
|
||||
base64 = "0.13"
|
||||
bytes = "1"
|
||||
@ -51,5 +51,5 @@ tls-openssl = { version = "0.10.9", package = "openssl", optional = true }
|
||||
tokio = { version = "1.8.4", features = ["sync"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-web = { version = "4.0.0-beta.17", default-features = false, features = ["cookies"] }
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-web = { version = "4.0.0-beta.19", default-features = false, features = ["cookies"] }
|
||||
actix-http = "3.0.0-beta.18"
|
||||
|
@ -3,15 +3,15 @@
|
||||
> Various helpers for Actix applications to use during testing.
|
||||
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
[](https://docs.rs/actix-http-test/3.0.0-beta.10)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-http-test/3.0.0-beta.11)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

|
||||
<br>
|
||||
[](https://deps.rs/crate/actix-http-test/3.0.0-beta.10)
|
||||
[](https://deps.rs/crate/actix-http-test/3.0.0-beta.11)
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-http-test)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
@ -3,6 +3,33 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.18 - 2022-01-04
|
||||
### Added
|
||||
- `impl Eq` for `header::ContentEncoding`. [#2501]
|
||||
- `impl Copy` for `QualityItem` where `T: Copy`. [#2501]
|
||||
- `Quality::ZERO` equivalent to `q=0`. [#2501]
|
||||
- `QualityItem::zero` that uses `Quality::ZERO`. [#2501]
|
||||
- `ContentEncoding::to_header_value()`. [#2501]
|
||||
|
||||
### Changed
|
||||
- `Quality::MIN` is now the smallest non-zero value. [#2501]
|
||||
- `QualityItem::min` semantics changed with `QualityItem::MIN`. [#2501]
|
||||
- Rename `ContentEncoding::{Br => Brotli}`. [#2501]
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
- Rename `header::EntityTag::{weak => new_weak, strong => new_strong}`. [#2565]
|
||||
|
||||
### Fixed
|
||||
- `ContentEncoding::Identity` can now be parsed from a string. [#2501]
|
||||
- A `Vary` header is now correctly sent along with compressed content. [#2501]
|
||||
|
||||
### Removed
|
||||
- `ContentEncoding::Auto` variant. [#2501]
|
||||
- `ContentEncoding::is_compression()`. [#2501]
|
||||
|
||||
[#2501]: https://github.com/actix/actix-web/pull/2501
|
||||
[#2565]: https://github.com/actix/actix-web/pull/2565
|
||||
|
||||
|
||||
## 3.0.0-beta.17 - 2021-12-27
|
||||
### Changes
|
||||
- `HeaderMap::get_all` now returns a `std::slice::Iter`. [#2527]
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-http"
|
||||
version = "3.0.0-beta.17"
|
||||
version = "3.0.0-beta.18"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "HTTP primitives for the Actix ecosystem"
|
||||
keywords = ["actix", "http", "framework", "async", "futures"]
|
||||
@ -79,10 +79,10 @@ flate2 = { version = "1.0.13", optional = true }
|
||||
zstd = { version = "0.9", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-http-test = { version = "3.0.0-beta.10", features = ["openssl"] }
|
||||
actix-http-test = { version = "3.0.0-beta.11", features = ["openssl"] }
|
||||
actix-server = "2.0.0-rc.2"
|
||||
actix-tls = { version = "3.0.0", features = ["openssl"] }
|
||||
actix-web = "4.0.0-beta.17"
|
||||
actix-web = "4.0.0-beta.19"
|
||||
|
||||
async-stream = "0.3"
|
||||
criterion = { version = "0.3", features = ["html_reports"] }
|
||||
|
@ -3,18 +3,18 @@
|
||||
> HTTP primitives for the Actix ecosystem.
|
||||
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://docs.rs/actix-http/3.0.0-beta.17)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-http/3.0.0-beta.18)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-http/3.0.0-beta.17)
|
||||
[](https://deps.rs/crate/actix-http/3.0.0-beta.18)
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-http)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
||||
## Example
|
||||
|
||||
|
@ -47,9 +47,9 @@ where
|
||||
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
|
||||
let decoder = match encoding {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(BrotliDecoder::new(
|
||||
Writer::new(),
|
||||
)))),
|
||||
ContentEncoding::Brotli => Some(ContentDecoder::Brotli(Box::new(
|
||||
BrotliDecoder::new(Writer::new()),
|
||||
))),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
|
||||
ZlibDecoder::new(Writer::new()),
|
||||
@ -165,7 +165,7 @@ enum ContentDecoder {
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
Gzip(Box<GzDecoder<Writer>>),
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
Br(Box<BrotliDecoder<Writer>>),
|
||||
Brotli(Box<BrotliDecoder<Writer>>),
|
||||
// We need explicit 'static lifetime here because ZstdDecoder need lifetime
|
||||
// argument, and we use `spawn_blocking` in `Decoder::poll_next` that require `FnOnce() -> R + Send + 'static`
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
@ -176,7 +176,7 @@ impl ContentDecoder {
|
||||
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
|
||||
match self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentDecoder::Br(ref mut decoder) => match decoder.flush() {
|
||||
ContentDecoder::Brotli(ref mut decoder) => match decoder.flush() {
|
||||
Ok(()) => {
|
||||
let b = decoder.get_mut().take();
|
||||
|
||||
@ -234,7 +234,7 @@ impl ContentDecoder {
|
||||
fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
|
||||
match self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentDecoder::Br(ref mut decoder) => match decoder.write_all(&data) {
|
||||
ContentDecoder::Brotli(ref mut decoder) => match decoder.write_all(&data) {
|
||||
Ok(_) => {
|
||||
decoder.flush()?;
|
||||
let b = decoder.get_mut().take();
|
||||
|
@ -56,25 +56,24 @@ impl<B: MessageBody> Encoder<B> {
|
||||
}
|
||||
|
||||
pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self {
|
||||
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|
||||
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
||||
|| head.status == StatusCode::NO_CONTENT
|
||||
|| encoding == ContentEncoding::Identity
|
||||
|| encoding == ContentEncoding::Auto);
|
||||
|
||||
// no need to compress an empty body
|
||||
if matches!(body.size(), BodySize::None) {
|
||||
return Self::none();
|
||||
}
|
||||
|
||||
let should_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|
||||
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
||||
|| head.status == StatusCode::NO_CONTENT
|
||||
|| encoding == ContentEncoding::Identity);
|
||||
|
||||
let body = match body.try_into_bytes() {
|
||||
Ok(body) => EncoderBody::Full { body },
|
||||
Err(body) => EncoderBody::Stream { body },
|
||||
};
|
||||
|
||||
if can_encode {
|
||||
// Modify response body only if encoder is set
|
||||
if let Some(enc) = ContentEncoder::encoder(encoding) {
|
||||
if should_encode {
|
||||
// wrap body only if encoder is feature-enabled
|
||||
if let Some(enc) = ContentEncoder::select(encoding) {
|
||||
update_head(encoding, head);
|
||||
|
||||
return Encoder {
|
||||
@ -169,6 +168,7 @@ where
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
||||
let mut this = self.project();
|
||||
|
||||
loop {
|
||||
if *this.eof {
|
||||
return Poll::Ready(None);
|
||||
@ -252,10 +252,10 @@ where
|
||||
}
|
||||
|
||||
fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
|
||||
head.headers_mut().insert(
|
||||
header::CONTENT_ENCODING,
|
||||
HeaderValue::from_static(encoding.as_str()),
|
||||
);
|
||||
head.headers_mut()
|
||||
.insert(header::CONTENT_ENCODING, encoding.to_header_value());
|
||||
head.headers_mut()
|
||||
.insert(header::VARY, HeaderValue::from_static("accept-encoding"));
|
||||
|
||||
head.no_chunking(false);
|
||||
}
|
||||
@ -268,7 +268,7 @@ enum ContentEncoder {
|
||||
Gzip(GzEncoder<Writer>),
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
Br(BrotliEncoder<Writer>),
|
||||
Brotli(BrotliEncoder<Writer>),
|
||||
|
||||
// Wwe need explicit 'static lifetime here because ZstdEncoder needs a lifetime argument and we
|
||||
// use `spawn_blocking` in `Encoder::poll_next` that requires `FnOnce() -> R + Send + 'static`.
|
||||
@ -277,7 +277,7 @@ enum ContentEncoder {
|
||||
}
|
||||
|
||||
impl ContentEncoder {
|
||||
fn encoder(encoding: ContentEncoding) -> Option<Self> {
|
||||
fn select(encoding: ContentEncoding) -> Option<Self> {
|
||||
match encoding {
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoding::Deflate => Some(ContentEncoder::Deflate(ZlibEncoder::new(
|
||||
@ -292,8 +292,8 @@ impl ContentEncoder {
|
||||
))),
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoding::Br => {
|
||||
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
|
||||
ContentEncoding::Brotli => {
|
||||
Some(ContentEncoder::Brotli(BrotliEncoder::new(Writer::new(), 3)))
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
@ -310,7 +310,7 @@ impl ContentEncoder {
|
||||
pub(crate) fn take(&mut self) -> Bytes {
|
||||
match *self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(),
|
||||
ContentEncoder::Brotli(ref mut encoder) => encoder.get_mut().take(),
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
|
||||
@ -326,7 +326,7 @@ impl ContentEncoder {
|
||||
fn finish(self) -> Result<Bytes, io::Error> {
|
||||
match self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoder::Br(encoder) => match encoder.finish() {
|
||||
ContentEncoder::Brotli(encoder) => match encoder.finish() {
|
||||
Ok(writer) => Ok(writer.buf.freeze()),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
@ -354,7 +354,7 @@ impl ContentEncoder {
|
||||
fn write(&mut self, data: &[u8]) -> Result<(), io::Error> {
|
||||
match *self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoder::Br(ref mut encoder) => match encoder.write_all(data) {
|
||||
ContentEncoder::Brotli(ref mut encoder) => match encoder.write_all(data) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
trace!("Error decoding br encoding: {}", err);
|
||||
|
@ -250,6 +250,7 @@ impl From<ParseError> for Response<BoxBody> {
|
||||
/// A set of errors that can occur running blocking tasks in thread pool.
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "Blocking thread pool is gone")]
|
||||
// TODO: non-exhaustive
|
||||
pub struct BlockingError;
|
||||
|
||||
/// A set of errors that can occur during payload parsing.
|
||||
|
@ -6,7 +6,7 @@ use ahash::AHashMap;
|
||||
use http::header::{HeaderName, HeaderValue};
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
||||
use crate::header::AsHeaderName;
|
||||
use super::AsHeaderName;
|
||||
|
||||
/// A multi-map of HTTP headers.
|
||||
///
|
||||
@ -605,6 +605,13 @@ impl<'a> IntoIterator for &'a HeaderMap {
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `http::HeaderMap` to our `HeaderMap`.
|
||||
impl From<http::HeaderMap> for HeaderMap {
|
||||
fn from(mut map: http::HeaderMap) -> HeaderMap {
|
||||
HeaderMap::from_drain(map.drain())
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator over removed, owned values with the same associated name.
|
||||
///
|
||||
/// Returned from methods that remove or replace items. See [`HeaderMap::insert`]
|
||||
|
@ -50,20 +50,13 @@ pub use self::utils::{
|
||||
|
||||
/// An interface for types that already represent a valid header.
|
||||
pub trait Header: TryIntoHeaderValue {
|
||||
/// Returns the name of the header field
|
||||
/// Returns the name of the header field.
|
||||
fn name() -> HeaderName;
|
||||
|
||||
/// Parse a header
|
||||
/// Parse the header from a HTTP message.
|
||||
fn parse<M: HttpMessage>(msg: &M) -> Result<Self, ParseError>;
|
||||
}
|
||||
|
||||
/// Convert `http::HeaderMap` to our `HeaderMap`.
|
||||
impl From<http::HeaderMap> for HeaderMap {
|
||||
fn from(mut map: http::HeaderMap) -> HeaderMap {
|
||||
HeaderMap::from_drain(map.drain())
|
||||
}
|
||||
}
|
||||
|
||||
/// This encode set is used for HTTP header values and is defined at
|
||||
/// <https://datatracker.ietf.org/doc/html/rfc5987#section-3.2>.
|
||||
pub(crate) const HTTP_VALUE: &AsciiSet = &CONTROLS
|
||||
|
@ -20,14 +20,16 @@ pub struct ContentEncodingParseError;
|
||||
/// See [IANA HTTP Content Coding Registry].
|
||||
///
|
||||
/// [IANA HTTP Content Coding Registry]: https://www.iana.org/assignments/http-parameters/http-parameters.xhtml
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum ContentEncoding {
|
||||
/// Automatically select encoding based on encoding negotiation.
|
||||
Auto,
|
||||
/// Indicates the no-op identity encoding.
|
||||
///
|
||||
/// I.e., no compression or modification.
|
||||
Identity,
|
||||
|
||||
/// A format using the Brotli algorithm.
|
||||
Br,
|
||||
Brotli,
|
||||
|
||||
/// A format using the zlib structure with deflate algorithm.
|
||||
Deflate,
|
||||
@ -37,32 +39,36 @@ pub enum ContentEncoding {
|
||||
|
||||
/// Zstd algorithm.
|
||||
Zstd,
|
||||
|
||||
/// Indicates the identity function (i.e. no compression, nor modification).
|
||||
Identity,
|
||||
}
|
||||
|
||||
impl ContentEncoding {
|
||||
/// Is the content compressed?
|
||||
#[inline]
|
||||
pub const fn is_compression(self) -> bool {
|
||||
matches!(self, ContentEncoding::Identity | ContentEncoding::Auto)
|
||||
}
|
||||
|
||||
/// Convert content encoding to string.
|
||||
#[inline]
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
ContentEncoding::Br => "br",
|
||||
ContentEncoding::Brotli => "br",
|
||||
ContentEncoding::Gzip => "gzip",
|
||||
ContentEncoding::Deflate => "deflate",
|
||||
ContentEncoding::Zstd => "zstd",
|
||||
ContentEncoding::Identity | ContentEncoding::Auto => "identity",
|
||||
ContentEncoding::Identity => "identity",
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert content encoding to header value.
|
||||
#[inline]
|
||||
pub const fn to_header_value(self) -> HeaderValue {
|
||||
match self {
|
||||
ContentEncoding::Brotli => HeaderValue::from_static("br"),
|
||||
ContentEncoding::Gzip => HeaderValue::from_static("gzip"),
|
||||
ContentEncoding::Deflate => HeaderValue::from_static("deflate"),
|
||||
ContentEncoding::Zstd => HeaderValue::from_static("zstd"),
|
||||
ContentEncoding::Identity => HeaderValue::from_static("identity"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ContentEncoding {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self::Identity
|
||||
}
|
||||
@ -71,16 +77,18 @@ impl Default for ContentEncoding {
|
||||
impl FromStr for ContentEncoding {
|
||||
type Err = ContentEncodingParseError;
|
||||
|
||||
fn from_str(val: &str) -> Result<Self, Self::Err> {
|
||||
let val = val.trim();
|
||||
fn from_str(enc: &str) -> Result<Self, Self::Err> {
|
||||
let enc = enc.trim();
|
||||
|
||||
if val.eq_ignore_ascii_case("br") {
|
||||
Ok(ContentEncoding::Br)
|
||||
} else if val.eq_ignore_ascii_case("gzip") {
|
||||
if enc.eq_ignore_ascii_case("br") {
|
||||
Ok(ContentEncoding::Brotli)
|
||||
} else if enc.eq_ignore_ascii_case("gzip") {
|
||||
Ok(ContentEncoding::Gzip)
|
||||
} else if val.eq_ignore_ascii_case("deflate") {
|
||||
} else if enc.eq_ignore_ascii_case("deflate") {
|
||||
Ok(ContentEncoding::Deflate)
|
||||
} else if val.eq_ignore_ascii_case("zstd") {
|
||||
} else if enc.eq_ignore_ascii_case("identity") {
|
||||
Ok(ContentEncoding::Identity)
|
||||
} else if enc.eq_ignore_ascii_case("zstd") {
|
||||
Ok(ContentEncoding::Zstd)
|
||||
} else {
|
||||
Err(ContentEncodingParseError)
|
||||
|
@ -27,7 +27,8 @@ const MAX_QUALITY_FLOAT: f32 = 1.0;
|
||||
///
|
||||
/// assert_eq!(q(0.42).to_string(), "0.42");
|
||||
/// assert_eq!(q(1.0).to_string(), "1");
|
||||
/// assert_eq!(Quality::MIN.to_string(), "0");
|
||||
/// assert_eq!(Quality::MIN.to_string(), "0.001");
|
||||
/// assert_eq!(Quality::ZERO.to_string(), "0");
|
||||
/// ```
|
||||
///
|
||||
/// [RFC 7231 §5.3.1]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1
|
||||
@ -38,8 +39,11 @@ impl Quality {
|
||||
/// The maximum quality value, equivalent to `q=1.0`.
|
||||
pub const MAX: Quality = Quality(MAX_QUALITY_INT);
|
||||
|
||||
/// The minimum quality value, equivalent to `q=0.0`.
|
||||
pub const MIN: Quality = Quality(0);
|
||||
/// The minimum, non-zero quality value, equivalent to `q=0.001`.
|
||||
pub const MIN: Quality = Quality(1);
|
||||
|
||||
/// The zero quality value, equivalent to `q=0.0`.
|
||||
pub const ZERO: Quality = Quality(0);
|
||||
|
||||
/// Converts a float in the range 0.0–1.0 to a `Quality`.
|
||||
///
|
||||
@ -51,7 +55,7 @@ impl Quality {
|
||||
// Check that `value` is within range should be done before calling this method.
|
||||
// Just in case, this debug_assert should catch if we were forgetful.
|
||||
debug_assert!(
|
||||
(0.0f32..=1.0f32).contains(&value),
|
||||
(0.0..=MAX_QUALITY_FLOAT).contains(&value),
|
||||
"q value must be between 0.0 and 1.0"
|
||||
);
|
||||
|
||||
@ -154,10 +158,13 @@ impl TryFrom<f32> for Quality {
|
||||
/// let q1 = q(1.0);
|
||||
/// assert_eq!(q1, Quality::MAX);
|
||||
///
|
||||
/// let q2 = q(0.0);
|
||||
/// let q2 = q(0.001);
|
||||
/// assert_eq!(q2, Quality::MIN);
|
||||
///
|
||||
/// let q3 = q(0.42);
|
||||
/// let q3 = q(0.0);
|
||||
/// assert_eq!(q3, Quality::ZERO);
|
||||
///
|
||||
/// let q4 = q(0.42);
|
||||
/// ```
|
||||
///
|
||||
/// An out-of-range `f32` quality will panic.
|
||||
@ -185,6 +192,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn display_output() {
|
||||
assert_eq!(Quality::ZERO.to_string(), "0");
|
||||
assert_eq!(Quality::MIN.to_string(), "0.001");
|
||||
assert_eq!(Quality::MAX.to_string(), "1");
|
||||
|
||||
assert_eq!(q(0.0).to_string(), "0");
|
||||
assert_eq!(q(1.0).to_string(), "1");
|
||||
assert_eq!(q(0.001).to_string(), "0.001");
|
||||
|
@ -31,7 +31,7 @@ use super::Quality;
|
||||
/// let q_item_fallback: QualityItem<String> = "abc;q=0.1".parse().unwrap();
|
||||
/// assert!(q_item > q_item_fallback);
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct QualityItem<T> {
|
||||
/// The wrapped contents of the field.
|
||||
pub item: T,
|
||||
@ -53,10 +53,15 @@ impl<T> QualityItem<T> {
|
||||
Self::new(item, Quality::MAX)
|
||||
}
|
||||
|
||||
/// Constructs a new `QualityItem` from an item, using the minimum q-value.
|
||||
/// Constructs a new `QualityItem` from an item, using the minimum, non-zero q-value.
|
||||
pub fn min(item: T) -> Self {
|
||||
Self::new(item, Quality::MIN)
|
||||
}
|
||||
|
||||
/// Constructs a new `QualityItem` from an item, using zero q-value of zero.
|
||||
pub fn zero(item: T) -> Self {
|
||||
Self::new(item, Quality::ZERO)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> PartialOrd for QualityItem<T> {
|
||||
@ -73,7 +78,10 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
|
||||
// q-factor value is implied for max value
|
||||
Quality::MAX => Ok(()),
|
||||
|
||||
Quality::MIN => f.write_str("; q=0"),
|
||||
// fast path for zero
|
||||
Quality::ZERO => f.write_str("; q=0"),
|
||||
|
||||
// quality formatting is already using itoa
|
||||
q => write!(f, "; q={}", q),
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,12 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.4.0-beta.12 - 2022-01-04
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
|
||||
## 0.4.0-beta.11 - 2021-12-27
|
||||
* No significant changes since `0.4.0-beta.10`.
|
||||
- No significant changes since `0.4.0-beta.10`.
|
||||
|
||||
|
||||
## 0.4.0-beta.10 - 2021-12-11
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-multipart"
|
||||
version = "0.4.0-beta.11"
|
||||
version = "0.4.0-beta.12"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Multipart form support for Actix Web"
|
||||
keywords = ["http", "web", "framework", "async", "futures"]
|
||||
@ -15,7 +15,7 @@ path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-utils = "3.0.0"
|
||||
actix-web = { version = "4.0.0-beta.17", default-features = false }
|
||||
actix-web = { version = "4.0.0-beta.19", default-features = false }
|
||||
|
||||
bytes = "1"
|
||||
derive_more = "0.99.5"
|
||||
@ -28,7 +28,7 @@ twoway = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-http = "3.0.0-beta.18"
|
||||
futures-util = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||
tokio = { version = "1.8.4", features = ["sync"] }
|
||||
tokio-stream = "0.1"
|
||||
|
@ -3,15 +3,15 @@
|
||||
> Multipart form support for Actix Web.
|
||||
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://docs.rs/actix-multipart/0.4.0-beta.11)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-multipart/0.4.0-beta.12)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.11)
|
||||
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.12)
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-multipart)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
@ -3,6 +3,13 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.5.0-beta.4 - 2022-01-04
|
||||
- `PathDeserializer` now decodes all percent encoded characters in dynamic segments. [#2566]
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
[#2566]: https://github.com/actix/actix-net/pull/2566
|
||||
|
||||
|
||||
## 0.5.0-beta.3 - 2021-12-17
|
||||
- Minimum supported Rust version (MSRV) is now 1.52.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-router"
|
||||
version = "0.5.0-beta.3"
|
||||
version = "0.5.0-beta.4"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>",
|
||||
|
@ -2,7 +2,11 @@ use serde::de::{self, Deserializer, Error as DeError, Visitor};
|
||||
use serde::forward_to_deserialize_any;
|
||||
|
||||
use crate::path::{Path, PathIter};
|
||||
use crate::ResourcePath;
|
||||
use crate::{Quoter, ResourcePath};
|
||||
|
||||
thread_local! {
|
||||
static FULL_QUOTER: Quoter = Quoter::new(b"+/%", b"");
|
||||
}
|
||||
|
||||
macro_rules! unsupported_type {
|
||||
($trait_fn:ident, $name:expr) => {
|
||||
@ -10,16 +14,13 @@ macro_rules! unsupported_type {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
Err(de::value::Error::custom(concat!(
|
||||
"unsupported type: ",
|
||||
$name
|
||||
)))
|
||||
Err(de::Error::custom(concat!("unsupported type: ", $name)))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! parse_single_value {
|
||||
($trait_fn:ident, $visit_fn:ident, $tp:tt) => {
|
||||
($trait_fn:ident, $visit_fn:ident, $tp:expr) => {
|
||||
fn $trait_fn<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
@ -33,18 +34,39 @@ macro_rules! parse_single_value {
|
||||
.as_str(),
|
||||
))
|
||||
} else {
|
||||
let v = self.path[0].parse().map_err(|_| {
|
||||
de::value::Error::custom(format!(
|
||||
"can not parse {:?} to a {}",
|
||||
&self.path[0], $tp
|
||||
))
|
||||
let decoded = FULL_QUOTER
|
||||
.with(|q| q.requote(self.path[0].as_bytes()))
|
||||
.unwrap_or_else(|| self.path[0].to_owned());
|
||||
|
||||
let v = decoded.parse().map_err(|_| {
|
||||
de::Error::custom(format!("can not parse {:?} to a {}", &self.path[0], $tp))
|
||||
})?;
|
||||
|
||||
visitor.$visit_fn(v)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! parse_value {
|
||||
($trait_fn:ident, $visit_fn:ident, $tp:tt) => {
|
||||
fn $trait_fn<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let decoded = FULL_QUOTER
|
||||
.with(|q| q.requote(self.value.as_bytes()))
|
||||
.unwrap_or_else(|| self.value.to_owned());
|
||||
|
||||
let v = decoded.parse().map_err(|_| {
|
||||
de::value::Error::custom(format!("can not parse {:?} to a {}", self.value, $tp))
|
||||
})?;
|
||||
|
||||
visitor.$visit_fn(v)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub struct PathDeserializer<'de, T: ResourcePath> {
|
||||
path: &'de Path<T>,
|
||||
}
|
||||
@ -172,23 +194,6 @@ impl<'de, T: ResourcePath + 'de> Deserializer<'de> for PathDeserializer<'de, T>
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
if self.path.segment_count() != 1 {
|
||||
Err(de::value::Error::custom(
|
||||
format!(
|
||||
"wrong number of parameters: {} expected 1",
|
||||
self.path.segment_count()
|
||||
)
|
||||
.as_str(),
|
||||
))
|
||||
} else {
|
||||
visitor.visit_str(&self.path[0])
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
@ -215,6 +220,7 @@ impl<'de, T: ResourcePath + 'de> Deserializer<'de> for PathDeserializer<'de, T>
|
||||
parse_single_value!(deserialize_u64, visit_u64, "u64");
|
||||
parse_single_value!(deserialize_f32, visit_f32, "f32");
|
||||
parse_single_value!(deserialize_f64, visit_f64, "f64");
|
||||
parse_single_value!(deserialize_str, visit_string, "String");
|
||||
parse_single_value!(deserialize_string, visit_string, "String");
|
||||
parse_single_value!(deserialize_byte_buf, visit_string, "String");
|
||||
parse_single_value!(deserialize_char, visit_char, "char");
|
||||
@ -279,20 +285,6 @@ impl<'de> Deserializer<'de> for Key<'de> {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! parse_value {
|
||||
($trait_fn:ident, $visit_fn:ident, $tp:tt) => {
|
||||
fn $trait_fn<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let v = self.value.parse().map_err(|_| {
|
||||
de::value::Error::custom(format!("can not parse {:?} to a {}", self.value, $tp))
|
||||
})?;
|
||||
visitor.$visit_fn(v)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
struct Value<'de> {
|
||||
value: &'de str,
|
||||
}
|
||||
@ -497,6 +489,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::path::Path;
|
||||
use crate::router::Router;
|
||||
use crate::ResourceDef;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct MyStruct {
|
||||
@ -657,6 +650,53 @@ mod tests {
|
||||
assert!(format!("{:?}", s).contains("can not parse"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_path_decode_string() {
|
||||
let rdef = ResourceDef::new("/{key}");
|
||||
|
||||
let mut path = Path::new("/%25");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let segment: String = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(segment, "%");
|
||||
|
||||
let mut path = Path::new("/%2F");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let segment: String = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(segment, "/")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_path_decode_seq() {
|
||||
let rdef = ResourceDef::new("/{key}/{value}");
|
||||
|
||||
let mut path = Path::new("/%25/%2F");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let segment: (String, String) = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(segment.0, "%");
|
||||
assert_eq!(segment.1, "/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_path_decode_map() {
|
||||
#[derive(Deserialize)]
|
||||
struct Vals {
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
let rdef = ResourceDef::new("/{key}/{value}");
|
||||
|
||||
let mut path = Path::new("/%25/%2F");
|
||||
rdef.capture_match_info(&mut path);
|
||||
let de = PathDeserializer::new(&path);
|
||||
let vals: Vals = serde::Deserialize::deserialize(de).unwrap();
|
||||
assert_eq!(vals.key, "%");
|
||||
assert_eq!(vals.value, "/");
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_extract_path_decode() {
|
||||
// let mut router = Router::<()>::default();
|
||||
|
@ -8,6 +8,7 @@
|
||||
mod de;
|
||||
mod path;
|
||||
mod pattern;
|
||||
mod quoter;
|
||||
mod resource;
|
||||
mod resource_path;
|
||||
mod router;
|
||||
@ -18,9 +19,10 @@ mod url;
|
||||
pub use self::de::PathDeserializer;
|
||||
pub use self::path::Path;
|
||||
pub use self::pattern::{IntoPatterns, Patterns};
|
||||
pub use self::quoter::Quoter;
|
||||
pub use self::resource::ResourceDef;
|
||||
pub use self::resource_path::{Resource, ResourcePath};
|
||||
pub use self::router::{ResourceInfo, Router, RouterBuilder};
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
pub use self::url::{Quoter, Url};
|
||||
pub use self::url::Url;
|
||||
|
219
actix-router/src/quoter.rs
Normal file
219
actix-router/src/quoter.rs
Normal file
@ -0,0 +1,219 @@
|
||||
#[allow(dead_code)]
|
||||
const GEN_DELIMS: &[u8] = b":/?#[]@";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const SUB_DELIMS_WITHOUT_QS: &[u8] = b"!$'()*,";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const SUB_DELIMS: &[u8] = b"!$'()*,+?=;";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const RESERVED: &[u8] = b":/?#[]@!$'()*,+?=;";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const UNRESERVED: &[u8] = b"abcdefghijklmnopqrstuvwxyz
|
||||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
1234567890
|
||||
-._~";
|
||||
|
||||
const ALLOWED: &[u8] = b"abcdefghijklmnopqrstuvwxyz
|
||||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
1234567890
|
||||
-._~
|
||||
!$'()*,";
|
||||
|
||||
const QS: &[u8] = b"+&=;b";
|
||||
|
||||
/// A quoter
|
||||
pub struct Quoter {
|
||||
/// Simple bit-map of safe values in the 0-127 ASCII range.
|
||||
safe_table: [u8; 16],
|
||||
|
||||
/// Simple bit-map of protected values in the 0-127 ASCII range.
|
||||
protected_table: [u8; 16],
|
||||
}
|
||||
|
||||
impl Quoter {
|
||||
pub fn new(safe: &[u8], protected: &[u8]) -> Quoter {
|
||||
let mut quoter = Quoter {
|
||||
safe_table: [0; 16],
|
||||
protected_table: [0; 16],
|
||||
};
|
||||
|
||||
// prepare safe table
|
||||
for ch in 0..128 {
|
||||
if ALLOWED.contains(&ch) {
|
||||
set_bit(&mut quoter.safe_table, ch);
|
||||
}
|
||||
|
||||
if QS.contains(&ch) {
|
||||
set_bit(&mut quoter.safe_table, ch);
|
||||
}
|
||||
}
|
||||
|
||||
for &ch in safe {
|
||||
set_bit(&mut quoter.safe_table, ch)
|
||||
}
|
||||
|
||||
// prepare protected table
|
||||
for &ch in protected {
|
||||
set_bit(&mut quoter.safe_table, ch);
|
||||
set_bit(&mut quoter.protected_table, ch);
|
||||
}
|
||||
|
||||
quoter
|
||||
}
|
||||
|
||||
/// Re-quotes... ?
|
||||
///
|
||||
/// Returns `None` when no modification to the original string was required.
|
||||
pub fn requote(&self, val: &[u8]) -> Option<String> {
|
||||
let mut has_pct = 0;
|
||||
let mut pct = [b'%', 0, 0];
|
||||
let mut idx = 0;
|
||||
let mut cloned: Option<Vec<u8>> = None;
|
||||
|
||||
let len = val.len();
|
||||
|
||||
while idx < len {
|
||||
let ch = val[idx];
|
||||
|
||||
if has_pct != 0 {
|
||||
pct[has_pct] = val[idx];
|
||||
has_pct += 1;
|
||||
|
||||
if has_pct == 3 {
|
||||
has_pct = 0;
|
||||
let buf = cloned.as_mut().unwrap();
|
||||
|
||||
if let Some(ch) = hex_pair_to_char(pct[1], pct[2]) {
|
||||
if ch < 128 {
|
||||
if bit_at(&self.protected_table, ch) {
|
||||
buf.extend_from_slice(&pct);
|
||||
idx += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if bit_at(&self.safe_table, ch) {
|
||||
buf.push(ch);
|
||||
idx += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
buf.push(ch);
|
||||
} else {
|
||||
buf.extend_from_slice(&pct[..]);
|
||||
}
|
||||
}
|
||||
} else if ch == b'%' {
|
||||
has_pct = 1;
|
||||
|
||||
if cloned.is_none() {
|
||||
let mut c = Vec::with_capacity(len);
|
||||
c.extend_from_slice(&val[..idx]);
|
||||
cloned = Some(c);
|
||||
}
|
||||
} else if let Some(ref mut cloned) = cloned {
|
||||
cloned.push(ch)
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
cloned.map(|data| String::from_utf8_lossy(&data).into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts an ASCII character in the hex-encoded set (`0-9`, `A-F`, `a-f`) to its integer
|
||||
/// representation from `0x0`–`0xF`.
|
||||
///
|
||||
/// - `0x30 ('0') => 0x0`
|
||||
/// - `0x39 ('9') => 0x9`
|
||||
/// - `0x41 ('a') => 0xA`
|
||||
/// - `0x61 ('A') => 0xA`
|
||||
/// - `0x46 ('f') => 0xF`
|
||||
/// - `0x66 ('F') => 0xF`
|
||||
fn from_ascii_hex(v: u8) -> Option<u8> {
|
||||
match v {
|
||||
b'0'..=b'9' => Some(v - 0x30), // ord('0') == 0x30
|
||||
b'A'..=b'F' => Some(v - 0x41 + 10), // ord('A') == 0x41
|
||||
b'a'..=b'f' => Some(v - 0x61 + 10), // ord('a') == 0x61
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode a ASCII hex-encoded pair to an integer.
|
||||
///
|
||||
/// Returns `None` if either portion of the decoded pair does not evaluate to a valid hex value.
|
||||
///
|
||||
/// - `0x33 ('3'), 0x30 ('0') => 0x30 ('0')`
|
||||
/// - `0x34 ('4'), 0x31 ('1') => 0x41 ('A')`
|
||||
/// - `0x36 ('6'), 0x31 ('1') => 0x61 ('a')`
|
||||
fn hex_pair_to_char(d1: u8, d2: u8) -> Option<u8> {
|
||||
let (d_high, d_low) = (from_ascii_hex(d1)?, from_ascii_hex(d2)?);
|
||||
|
||||
// left shift high nibble by 4 bits
|
||||
Some(d_high << 4 | d_low)
|
||||
}
|
||||
|
||||
/// Sets bit in given bit-map to 1=true.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if `ch` index is out of bounds.
|
||||
fn set_bit(array: &mut [u8], ch: u8) {
|
||||
array[(ch >> 3) as usize] |= 0b1 << (ch & 0b111)
|
||||
}
|
||||
|
||||
/// Returns true if bit to true in given bit-map.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if `ch` index is out of bounds.
|
||||
fn bit_at(array: &[u8], ch: u8) -> bool {
|
||||
array[(ch >> 3) as usize] & (0b1 << (ch & 0b111)) != 0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn hex_encoding() {
|
||||
let hex = b"0123456789abcdefABCDEF";
|
||||
|
||||
for i in 0..256 {
|
||||
let c = i as u8;
|
||||
if hex.contains(&c) {
|
||||
assert!(from_ascii_hex(c).is_some())
|
||||
} else {
|
||||
assert!(from_ascii_hex(c).is_none())
|
||||
}
|
||||
}
|
||||
|
||||
let expected = [
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15,
|
||||
];
|
||||
for i in 0..hex.len() {
|
||||
assert_eq!(from_ascii_hex(hex[i]).unwrap(), expected[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_quoter() {
|
||||
let q = Quoter::new(b"", b"+");
|
||||
assert_eq!(q.requote(b"/a%25c").unwrap(), "/a%c");
|
||||
assert_eq!(q.requote(b"/a%2Bc").unwrap(), "/a%2Bc");
|
||||
|
||||
let q = Quoter::new(b"%+", b"/");
|
||||
assert_eq!(q.requote(b"/a%25b%2Bc").unwrap(), "/a%b+c");
|
||||
assert_eq!(q.requote(b"/a%2fb").unwrap(), "/a%2fb");
|
||||
assert_eq!(q.requote(b"/a%2Fb").unwrap(), "/a%2Fb");
|
||||
assert_eq!(q.requote(b"/a%0Ab").unwrap(), "/a\nb");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quoter_no_modification() {
|
||||
let q = Quoter::new(b"", b"");
|
||||
assert_eq!(q.requote(b"/abc/../efg"), None);
|
||||
}
|
||||
}
|
@ -1,40 +1,6 @@
|
||||
use crate::ResourcePath;
|
||||
|
||||
#[allow(dead_code)]
|
||||
const GEN_DELIMS: &[u8] = b":/?#[]@";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const SUB_DELIMS_WITHOUT_QS: &[u8] = b"!$'()*,";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const SUB_DELIMS: &[u8] = b"!$'()*,+?=;";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const RESERVED: &[u8] = b":/?#[]@!$'()*,+?=;";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const UNRESERVED: &[u8] = b"abcdefghijklmnopqrstuvwxyz
|
||||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
1234567890
|
||||
-._~";
|
||||
|
||||
const ALLOWED: &[u8] = b"abcdefghijklmnopqrstuvwxyz
|
||||
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
1234567890
|
||||
-._~
|
||||
!$'()*,";
|
||||
|
||||
const QS: &[u8] = b"+&=;b";
|
||||
|
||||
#[inline]
|
||||
fn bit_at(array: &[u8], ch: u8) -> bool {
|
||||
array[(ch >> 3) as usize] & (1 << (ch & 7)) != 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_bit(array: &mut [u8], ch: u8) {
|
||||
array[(ch >> 3) as usize] |= 1 << (ch & 7)
|
||||
}
|
||||
use crate::Quoter;
|
||||
|
||||
thread_local! {
|
||||
static DEFAULT_QUOTER: Quoter = Quoter::new(b"@:", b"%/+");
|
||||
@ -54,18 +20,20 @@ impl Url {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_quoter(uri: http::Uri, quoter: &Quoter) -> Url {
|
||||
pub fn new_with_quoter(uri: http::Uri, quoter: &Quoter) -> Url {
|
||||
Url {
|
||||
path: quoter.requote(uri.path().as_bytes()),
|
||||
uri,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns URI.
|
||||
#[inline]
|
||||
pub fn uri(&self) -> &http::Uri {
|
||||
&self.uri
|
||||
}
|
||||
|
||||
/// Returns path.
|
||||
#[inline]
|
||||
pub fn path(&self) -> &str {
|
||||
match self.path {
|
||||
@ -94,113 +62,6 @@ impl ResourcePath for Url {
|
||||
}
|
||||
}
|
||||
|
||||
/// A quoter
|
||||
pub struct Quoter {
|
||||
safe_table: [u8; 16],
|
||||
protected_table: [u8; 16],
|
||||
}
|
||||
|
||||
impl Quoter {
|
||||
pub fn new(safe: &[u8], protected: &[u8]) -> Quoter {
|
||||
let mut quoter = Quoter {
|
||||
safe_table: [0; 16],
|
||||
protected_table: [0; 16],
|
||||
};
|
||||
|
||||
// prepare safe table
|
||||
for i in 0..128 {
|
||||
if ALLOWED.contains(&i) {
|
||||
set_bit(&mut quoter.safe_table, i);
|
||||
}
|
||||
if QS.contains(&i) {
|
||||
set_bit(&mut quoter.safe_table, i);
|
||||
}
|
||||
}
|
||||
|
||||
for ch in safe {
|
||||
set_bit(&mut quoter.safe_table, *ch)
|
||||
}
|
||||
|
||||
// prepare protected table
|
||||
for ch in protected {
|
||||
set_bit(&mut quoter.safe_table, *ch);
|
||||
set_bit(&mut quoter.protected_table, *ch);
|
||||
}
|
||||
|
||||
quoter
|
||||
}
|
||||
|
||||
pub fn requote(&self, val: &[u8]) -> Option<String> {
|
||||
let mut has_pct = 0;
|
||||
let mut pct = [b'%', 0, 0];
|
||||
let mut idx = 0;
|
||||
let mut cloned: Option<Vec<u8>> = None;
|
||||
|
||||
let len = val.len();
|
||||
while idx < len {
|
||||
let ch = val[idx];
|
||||
|
||||
if has_pct != 0 {
|
||||
pct[has_pct] = val[idx];
|
||||
has_pct += 1;
|
||||
if has_pct == 3 {
|
||||
has_pct = 0;
|
||||
let buf = cloned.as_mut().unwrap();
|
||||
|
||||
if let Some(ch) = restore_ch(pct[1], pct[2]) {
|
||||
if ch < 128 {
|
||||
if bit_at(&self.protected_table, ch) {
|
||||
buf.extend_from_slice(&pct);
|
||||
idx += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if bit_at(&self.safe_table, ch) {
|
||||
buf.push(ch);
|
||||
idx += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
buf.push(ch);
|
||||
} else {
|
||||
buf.extend_from_slice(&pct[..]);
|
||||
}
|
||||
}
|
||||
} else if ch == b'%' {
|
||||
has_pct = 1;
|
||||
if cloned.is_none() {
|
||||
let mut c = Vec::with_capacity(len);
|
||||
c.extend_from_slice(&val[..idx]);
|
||||
cloned = Some(c);
|
||||
}
|
||||
} else if let Some(ref mut cloned) = cloned {
|
||||
cloned.push(ch)
|
||||
}
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
cloned.map(|data| String::from_utf8_lossy(&data).into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_hex(v: u8) -> Option<u8> {
|
||||
if (b'0'..=b'9').contains(&v) {
|
||||
Some(v - 0x30) // ord('0') == 0x30
|
||||
} else if (b'A'..=b'F').contains(&v) {
|
||||
Some(v - 0x41 + 10) // ord('A') == 0x41
|
||||
} else if (b'a'..=b'f').contains(&v) {
|
||||
Some(v - 0x61 + 10) // ord('a') == 0x61
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn restore_ch(d1: u8, d2: u8) -> Option<u8> {
|
||||
from_hex(d1).and_then(|d1| from_hex(d2).map(move |d2| d1 << 4 | d2))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use http::Uri;
|
||||
@ -229,6 +90,16 @@ mod tests {
|
||||
|
||||
let path = match_url(re, "/user/2345/test");
|
||||
assert_eq!(path.get("id").unwrap(), "2345");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protected_chars() {
|
||||
let re = "/user/{id}/test";
|
||||
|
||||
let encoded = percent_encode(PROTECTED);
|
||||
let path = match_url(re, format!("/user/{}/test", encoded));
|
||||
// characters in captured segment remain unencoded
|
||||
assert_eq!(path.get("id").unwrap(), &encoded);
|
||||
|
||||
// "%25" should never be decoded into '%' to guarantee the output is a valid
|
||||
// percent-encoded format
|
||||
@ -239,13 +110,6 @@ mod tests {
|
||||
assert_eq!(path.get("id").unwrap(), "qwe%25rty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protected_chars() {
|
||||
let encoded = percent_encode(PROTECTED);
|
||||
let path = match_url("/user/{id}/test", format!("/user/{}/test", encoded));
|
||||
assert_eq!(path.get("id").unwrap(), &encoded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_protected_ascii() {
|
||||
let non_protected_ascii = ('\u{0}'..='\u{7F}')
|
||||
@ -273,25 +137,4 @@ mod tests {
|
||||
// We should always get a valid utf8 string
|
||||
assert!(String::from_utf8(path.path().as_bytes().to_owned()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hex_encoding() {
|
||||
let hex = b"0123456789abcdefABCDEF";
|
||||
|
||||
for i in 0..256 {
|
||||
let c = i as u8;
|
||||
if hex.contains(&c) {
|
||||
assert!(from_hex(c).is_some())
|
||||
} else {
|
||||
assert!(from_hex(c).is_none())
|
||||
}
|
||||
}
|
||||
|
||||
let expected = [
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15,
|
||||
];
|
||||
for i in 0..hex.len() {
|
||||
assert_eq!(from_hex(hex[i]).unwrap(), expected[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,12 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.1.0-beta.11 - 2022-01-04
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
|
||||
## 0.1.0-beta.10 - 2021-12-27
|
||||
* No significant changes since `0.1.0-beta.9`.
|
||||
- No significant changes since `0.1.0-beta.9`.
|
||||
|
||||
|
||||
## 0.1.0-beta.9 - 2021-12-17
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-test"
|
||||
version = "0.1.0-beta.10"
|
||||
version = "0.1.0-beta.11"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
@ -29,13 +29,13 @@ openssl = ["tls-openssl", "actix-http/openssl", "awc/openssl"]
|
||||
|
||||
[dependencies]
|
||||
actix-codec = "0.4.1"
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-http-test = "3.0.0-beta.10"
|
||||
actix-http = "3.0.0-beta.18"
|
||||
actix-http-test = "3.0.0-beta.11"
|
||||
actix-rt = "2.1"
|
||||
actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
actix-web = { version = "4.0.0-beta.17", default-features = false, features = ["cookies"] }
|
||||
awc = { version = "3.0.0-beta.16", default-features = false, features = ["cookies"] }
|
||||
actix-web = { version = "4.0.0-beta.19", default-features = false, features = ["cookies"] }
|
||||
awc = { version = "3.0.0-beta.18", default-features = false, features = ["cookies"] }
|
||||
|
||||
futures-core = { version = "0.3.7", default-features = false, features = ["std"] }
|
||||
futures-util = { version = "0.3.7", default-features = false, features = [] }
|
||||
|
@ -3,8 +3,12 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 4.0.0-beta.10 - 2022-01-04
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
|
||||
## 4.0.0-beta.9 - 2021-12-27
|
||||
* No significant changes since `4.0.0-beta.8`.
|
||||
- No significant changes since `4.0.0-beta.8`.
|
||||
|
||||
|
||||
## 4.0.0-beta.8 - 2021-12-11
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web-actors"
|
||||
version = "4.0.0-beta.9"
|
||||
version = "4.0.0-beta.10"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix actors support for Actix Web"
|
||||
keywords = ["actix", "http", "web", "framework", "async"]
|
||||
@ -16,8 +16,8 @@ path = "src/lib.rs"
|
||||
[dependencies]
|
||||
actix = { version = "0.12.0", default-features = false }
|
||||
actix-codec = "0.4.1"
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-web = { version = "4.0.0-beta.17", default-features = false }
|
||||
actix-http = "3.0.0-beta.18"
|
||||
actix-web = { version = "4.0.0-beta.19", default-features = false }
|
||||
|
||||
bytes = "1"
|
||||
bytestring = "1"
|
||||
@ -27,8 +27,8 @@ tokio = { version = "1.8.4", features = ["sync"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1.0-beta.10"
|
||||
awc = { version = "3.0.0-beta.16", default-features = false }
|
||||
actix-test = "0.1.0-beta.11"
|
||||
awc = { version = "3.0.0-beta.18", default-features = false }
|
||||
|
||||
env_logger = "0.9"
|
||||
futures-util = { version = "0.3.7", default-features = false }
|
||||
|
@ -3,15 +3,15 @@
|
||||
> Actix actors support for Actix Web.
|
||||
|
||||
[](https://crates.io/crates/actix-web-actors)
|
||||
[](https://docs.rs/actix-web-actors/4.0.0-beta.9)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-web-actors/4.0.0-beta.10)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-web-actors/4.0.0-beta.9)
|
||||
[](https://deps.rs/crate/actix-web-actors/4.0.0-beta.10)
|
||||
[](https://crates.io/crates/actix-web-actors)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-web-actors)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
@ -3,6 +3,10 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.5.0-rc.1 - 2022-01-04
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
|
||||
## 0.5.0-beta.6 - 2021-12-11
|
||||
- No significant changes since `0.5.0-beta.5`.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web-codegen"
|
||||
version = "0.5.0-beta.6"
|
||||
version = "0.5.0-rc.1"
|
||||
description = "Routing and runtime macros for Actix Web"
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web.git"
|
||||
@ -15,17 +15,17 @@ edition = "2018"
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
actix-router = "0.5.0-beta.4"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["full", "parsing"] }
|
||||
proc-macro2 = "1"
|
||||
actix-router = "0.5.0-beta.3"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-macros = "0.2.3"
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1.0-beta.10"
|
||||
actix-test = "0.1.0-beta.11"
|
||||
actix-utils = "3.0.0"
|
||||
actix-web = "4.0.0-beta.17"
|
||||
actix-web = "4.0.0-beta.19"
|
||||
|
||||
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||
trybuild = "1"
|
||||
|
@ -3,18 +3,18 @@
|
||||
> Routing and runtime macros for Actix Web.
|
||||
|
||||
[](https://crates.io/crates/actix-web-codegen)
|
||||
[](https://docs.rs/actix-web-codegen/0.5.0-beta.6)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||
[](https://docs.rs/actix-web-codegen/0.5.0-rc.1)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.54.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-web-codegen/0.5.0-beta.6)
|
||||
[](https://deps.rs/crate/actix-web-codegen/0.5.0-rc.1)
|
||||
[](https://crates.io/crates/actix-web-codegen)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-web-codegen)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
||||
## Compile Testing
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#[rustversion::stable(1.52)] // MSRV
|
||||
#[rustversion::stable(1.54)] // MSRV
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
@ -1,13 +1,13 @@
|
||||
error: The #[route(..)] macro requires at least one `method` attribute
|
||||
--> $DIR/route-missing-method-fail.rs:3:1
|
||||
--> tests/trybuild/route-missing-method-fail.rs:3:1
|
||||
|
|
||||
3 | #[route("/")]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
= note: this error originates in the attribute macro `route` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error[E0277]: the trait bound `fn() -> impl std::future::Future {index}: HttpServiceFactory` is not satisfied
|
||||
--> $DIR/route-missing-method-fail.rs:12:55
|
||||
--> tests/trybuild/route-missing-method-fail.rs:12:55
|
||||
|
|
||||
12 | let srv = actix_test::start(|| App::new().service(index));
|
||||
| ^^^^^ the trait `HttpServiceFactory` is not implemented for `fn() -> impl std::future::Future {index}`
|
||||
|
@ -3,6 +3,21 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.18 - 2022-01-04
|
||||
- Minimum supported Rust version (MSRV) is now 1.54.
|
||||
|
||||
|
||||
## 3.0.0-beta.17 - 2021-12-29
|
||||
### Changed
|
||||
- Update `cookie` dependency (re-exported) to `0.16`. [#2555]
|
||||
|
||||
### Security
|
||||
- `cookie` upgrade addresses [`RUSTSEC-2020-0071`].
|
||||
|
||||
[#2555]: https://github.com/actix/actix-web/pull/2555
|
||||
[`RUSTSEC-2020-0071`]: https://rustsec.org/advisories/RUSTSEC-2020-0071.html
|
||||
|
||||
|
||||
## 3.0.0-beta.16 - 2021-12-29
|
||||
- `*::send_json` and `*::send_form` methods now receive `impl Serialize`. [#2553]
|
||||
- `FrozenClientRequest::extra_header` now uses receives an `impl TryIntoHeaderPair`. [#2553]
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "awc"
|
||||
version = "3.0.0-beta.16"
|
||||
version = "3.0.0-beta.18"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"fakeshadow <24548779@qq.com>",
|
||||
@ -60,7 +60,7 @@ dangerous-h2c = []
|
||||
[dependencies]
|
||||
actix-codec = "0.4.1"
|
||||
actix-service = "2.0.0"
|
||||
actix-http = "3.0.0-beta.17"
|
||||
actix-http = "3.0.0-beta.18"
|
||||
actix-rt = { version = "2.1", default-features = false }
|
||||
actix-tls = { version = "3.0.0", features = ["connect", "uri"] }
|
||||
actix-utils = "3.0.0"
|
||||
@ -85,7 +85,7 @@ serde_json = "1.0"
|
||||
serde_urlencoded = "0.7"
|
||||
tokio = { version = "1.8.4", features = ["sync"] }
|
||||
|
||||
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
|
||||
cookie = { version = "0.16", features = ["percent-encode"], optional = true }
|
||||
|
||||
tls-openssl = { package = "openssl", version = "0.10.9", optional = true }
|
||||
tls-rustls = { package = "rustls", version = "0.20.0", optional = true, features = ["dangerous_configuration"] }
|
||||
@ -93,21 +93,23 @@ tls-rustls = { package = "rustls", version = "0.20.0", optional = true, features
|
||||
trust-dns-resolver = { version = "0.20.0", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-http = { version = "3.0.0-beta.17", features = ["openssl"] }
|
||||
actix-http-test = { version = "3.0.0-beta.10", features = ["openssl"] }
|
||||
actix-http = { version = "3.0.0-beta.18", features = ["openssl"] }
|
||||
actix-http-test = { version = "3.0.0-beta.11", features = ["openssl"] }
|
||||
actix-server = "2.0.0-rc.2"
|
||||
actix-test = { version = "0.1.0-beta.10", features = ["openssl", "rustls"] }
|
||||
actix-test = { version = "0.1.0-beta.11", features = ["openssl", "rustls"] }
|
||||
actix-tls = { version = "3.0.0", features = ["openssl", "rustls"] }
|
||||
actix-utils = "3.0.0"
|
||||
actix-web = { version = "4.0.0-beta.17", features = ["openssl"] }
|
||||
actix-web = { version = "4.0.0-beta.19", features = ["openssl"] }
|
||||
|
||||
brotli2 = "0.3.2"
|
||||
const-str = "0.3"
|
||||
env_logger = "0.9"
|
||||
flate2 = "1.0.13"
|
||||
futures-util = { version = "0.3.7", default-features = false }
|
||||
static_assertions = "1.1"
|
||||
rcgen = "0.8"
|
||||
rustls-pemfile = "0.2"
|
||||
zstd = "0.9"
|
||||
|
||||
[[example]]
|
||||
name = "client"
|
||||
|
@ -3,16 +3,16 @@
|
||||
> Async HTTP and WebSocket client library.
|
||||
|
||||
[](https://crates.io/crates/awc)
|
||||
[](https://docs.rs/awc/3.0.0-beta.16)
|
||||
[](https://docs.rs/awc/3.0.0-beta.18)
|
||||

|
||||
[](https://deps.rs/crate/awc/3.0.0-beta.16)
|
||||
[](https://deps.rs/crate/awc/3.0.0-beta.18)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/awc)
|
||||
- [Example Project](https://github.com/actix/examples/tree/HEAD/security/awc_https)
|
||||
- Minimum Supported Rust Version (MSRV): 1.52
|
||||
- Minimum Supported Rust Version (MSRV): 1.54
|
||||
|
||||
## Example
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::Infallible,
|
||||
io::{Read, Write},
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
sync::{
|
||||
@ -15,43 +16,16 @@ use cookie::Cookie;
|
||||
use futures_util::stream;
|
||||
use rand::Rng;
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
use brotli2::write::BrotliEncoder;
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
|
||||
|
||||
use actix_http::{ContentEncoding, HttpService, StatusCode};
|
||||
use actix_http::{HttpService, StatusCode};
|
||||
use actix_http_test::test_server;
|
||||
use actix_service::{fn_service, map_config, ServiceFactoryExt as _};
|
||||
use actix_web::{
|
||||
dev::{AppConfig, BodyEncoding},
|
||||
http::header,
|
||||
web, App, Error, HttpRequest, HttpResponse,
|
||||
};
|
||||
use actix_web::{dev::AppConfig, http::header, web, App, Error, HttpRequest, HttpResponse};
|
||||
use awc::error::{JsonPayloadError, PayloadError, SendRequestError};
|
||||
|
||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World";
|
||||
mod utils;
|
||||
|
||||
const S: &str = "Hello World ";
|
||||
const STR: &str = const_str::repeat!(S, 100);
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_simple() {
|
||||
@ -471,15 +445,12 @@ async fn test_no_decompress() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new()
|
||||
.wrap(actix_web::middleware::Compress::default())
|
||||
.service(web::resource("/").route(web::to(|| {
|
||||
let mut res = HttpResponse::Ok().body(STR);
|
||||
res.encoding(header::ContentEncoding::Gzip);
|
||||
res
|
||||
})))
|
||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
let mut res = awc::Client::new()
|
||||
.get(srv.url("/"))
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
@ -488,15 +459,12 @@ async fn test_no_decompress() {
|
||||
|
||||
// read response
|
||||
let bytes = res.body().await.unwrap();
|
||||
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
assert_eq!(utils::gzip::decode(bytes), STR.as_bytes());
|
||||
|
||||
// POST
|
||||
let mut res = awc::Client::new()
|
||||
.post(srv.url("/"))
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
@ -504,10 +472,7 @@ async fn test_no_decompress() {
|
||||
assert!(res.status().is_success());
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
assert_eq!(utils::gzip::decode(bytes), STR.as_bytes());
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
@ -515,13 +480,9 @@ async fn test_no_decompress() {
|
||||
async fn test_client_gzip_encoding() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().service(web::resource("/").route(web::to(|| {
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(STR.as_ref()).unwrap();
|
||||
let data = e.finish().unwrap();
|
||||
|
||||
HttpResponse::Ok()
|
||||
.insert_header(("content-encoding", "gzip"))
|
||||
.body(data)
|
||||
.insert_header(header::ContentEncoding::Gzip)
|
||||
.body(utils::gzip::encode(STR))
|
||||
})))
|
||||
});
|
||||
|
||||
@ -531,7 +492,7 @@ async fn test_client_gzip_encoding() {
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
assert_eq!(bytes, STR);
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
@ -539,13 +500,9 @@ async fn test_client_gzip_encoding() {
|
||||
async fn test_client_gzip_encoding_large() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().service(web::resource("/").route(web::to(|| {
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(STR.repeat(10).as_ref()).unwrap();
|
||||
let data = e.finish().unwrap();
|
||||
|
||||
HttpResponse::Ok()
|
||||
.insert_header(("content-encoding", "gzip"))
|
||||
.body(data)
|
||||
.insert_header(header::ContentEncoding::Gzip)
|
||||
.body(utils::gzip::encode(STR.repeat(10)))
|
||||
})))
|
||||
});
|
||||
|
||||
@ -555,7 +512,7 @@ async fn test_client_gzip_encoding_large() {
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(STR.repeat(10)));
|
||||
assert_eq!(bytes, STR.repeat(10));
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
@ -569,12 +526,9 @@ async fn test_client_gzip_encoding_large_random() {
|
||||
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(&data).unwrap();
|
||||
let data = e.finish().unwrap();
|
||||
HttpResponse::Ok()
|
||||
.insert_header(("content-encoding", "gzip"))
|
||||
.body(data)
|
||||
.insert_header(header::ContentEncoding::Gzip)
|
||||
.body(utils::gzip::encode(data))
|
||||
})))
|
||||
});
|
||||
|
||||
@ -584,7 +538,7 @@ async fn test_client_gzip_encoding_large_random() {
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
assert_eq!(bytes, data);
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
@ -592,12 +546,9 @@ async fn test_client_gzip_encoding_large_random() {
|
||||
async fn test_client_brotli_encoding() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
|
||||
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
||||
e.write_all(&data).unwrap();
|
||||
let data = e.finish().unwrap();
|
||||
HttpResponse::Ok()
|
||||
.insert_header(("content-encoding", "br"))
|
||||
.body(data)
|
||||
.body(utils::brotli::encode(data))
|
||||
})))
|
||||
});
|
||||
|
||||
@ -621,12 +572,9 @@ async fn test_client_brotli_encoding_large_random() {
|
||||
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
|
||||
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
||||
e.write_all(&data).unwrap();
|
||||
let data = e.finish().unwrap();
|
||||
HttpResponse::Ok()
|
||||
.insert_header(("content-encoding", "br"))
|
||||
.body(data)
|
||||
.insert_header(header::ContentEncoding::Brotli)
|
||||
.body(utils::brotli::encode(&data))
|
||||
})))
|
||||
});
|
||||
|
||||
@ -636,25 +584,25 @@ async fn test_client_brotli_encoding_large_random() {
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes.len(), data.len());
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
assert_eq!(bytes, data);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_client_deflate_encoding() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().default_service(web::to(|body: Bytes| {
|
||||
HttpResponse::Ok().encoding(ContentEncoding::Br).body(body)
|
||||
}))
|
||||
App::new().default_service(web::to(|body: Bytes| HttpResponse::Ok().body(body)))
|
||||
});
|
||||
|
||||
let req = srv.post("/").send_body(STR);
|
||||
let req = srv
|
||||
.post("/")
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.send_body(STR);
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
assert_eq!(bytes, STR);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
@ -666,12 +614,13 @@ async fn test_client_deflate_encoding_large_random() {
|
||||
.collect::<String>();
|
||||
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().default_service(web::to(|body: Bytes| {
|
||||
HttpResponse::Ok().encoding(ContentEncoding::Br).body(body)
|
||||
}))
|
||||
App::new().default_service(web::to(|body: Bytes| HttpResponse::Ok().body(body)))
|
||||
});
|
||||
|
||||
let req = srv.post("/").send_body(data.clone());
|
||||
let req = srv
|
||||
.post("/")
|
||||
.insert_header((header::ACCEPT_ENCODING, "br"))
|
||||
.send_body(data.clone());
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
@ -684,15 +633,16 @@ async fn test_client_deflate_encoding_large_random() {
|
||||
async fn test_client_streaming_explicit() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().default_service(web::to(|body: web::Payload| {
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Identity)
|
||||
.streaming(body)
|
||||
HttpResponse::Ok().streaming(body)
|
||||
}))
|
||||
});
|
||||
|
||||
let body =
|
||||
stream::once(async { Ok::<_, actix_http::Error>(Bytes::from_static(STR.as_bytes())) });
|
||||
let req = srv.post("/").send_stream(Box::pin(body));
|
||||
let req = srv
|
||||
.post("/")
|
||||
.insert_header((header::ACCEPT_ENCODING, "identity"))
|
||||
.send_stream(Box::pin(body));
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert!(res.status().is_success());
|
||||
@ -705,17 +655,16 @@ async fn test_client_streaming_explicit() {
|
||||
async fn test_body_streaming_implicit() {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().default_service(web::to(|| {
|
||||
let body = stream::once(async {
|
||||
Ok::<_, actix_http::Error>(Bytes::from_static(STR.as_bytes()))
|
||||
});
|
||||
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Gzip)
|
||||
.streaming(Box::pin(body))
|
||||
let body =
|
||||
stream::once(async { Ok::<_, Infallible>(Bytes::from_static(STR.as_bytes())) });
|
||||
HttpResponse::Ok().streaming(body)
|
||||
}))
|
||||
});
|
||||
|
||||
let req = srv.get("/").send();
|
||||
let req = srv
|
||||
.get("/")
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert!(res.status().is_success());
|
||||
|
76
awc/tests/utils.rs
Normal file
76
awc/tests/utils.rs
Normal file
@ -0,0 +1,76 @@
|
||||
// compiling some tests will trigger unused function warnings even though other tests use them
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::io::{Read as _, Write as _};
|
||||
|
||||
pub mod gzip {
|
||||
use super::*;
|
||||
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = GzDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub mod deflate {
|
||||
use super::*;
|
||||
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::fast());
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = ZlibDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub mod brotli {
|
||||
use super::*;
|
||||
use ::brotli2::{read::BrotliDecoder, write::BrotliEncoder};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = BrotliEncoder::new(Vec::new(), 3);
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = BrotliDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub mod zstd {
|
||||
use super::*;
|
||||
use ::zstd::stream::{read::Decoder, write::Encoder};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = Encoder::new(Vec::new(), 3).unwrap();
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = Decoder::new(bytes.as_ref()).unwrap();
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
msrv = "1.52"
|
||||
msrv = "1.54"
|
||||
|
@ -15,6 +15,7 @@ digraph {
|
||||
|
||||
"actix-web" -> { "actix-web-codegen" "actix-http" "actix-router" }
|
||||
"awc" -> { "actix-http" }
|
||||
"actix-web-codegen" -> { "actix-router" }
|
||||
"actix-web-actors" -> { "actix" "actix-web" "actix-http" }
|
||||
"actix-multipart" -> { "actix-web" }
|
||||
"actix-files" -> { "actix-web" }
|
||||
|
@ -40,7 +40,7 @@ cat "$CHANGELOG_FILE" |
|
||||
|
||||
# if word count of changelog chunk is 0 then insert filler changelog chunk
|
||||
if [ "$(wc -w "$CHANGE_CHUNK_FILE" | awk '{ print $1 }')" = "0" ]; then
|
||||
echo "* No significant changes since \`$CURRENT_VERSION\`." >"$CHANGE_CHUNK_FILE"
|
||||
echo "- No significant changes since \`$CURRENT_VERSION\`." >"$CHANGE_CHUNK_FILE"
|
||||
echo >>"$CHANGE_CHUNK_FILE"
|
||||
echo >>"$CHANGE_CHUNK_FILE"
|
||||
fi
|
||||
|
@ -12,16 +12,16 @@ save_exit_code() {
|
||||
[ "$CMD_EXIT" = "0" ] || EXIT=$CMD_EXIT
|
||||
}
|
||||
|
||||
save_exit_code cargo test --lib --tests -p=actix-router --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-http --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-web --features=rustls,openssl -- --skip=test_reading_deflate_encoding_large_random_rustls
|
||||
save_exit_code cargo test --lib --tests -p=actix-web-codegen --all-features
|
||||
save_exit_code cargo test --lib --tests -p=awc --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-http-test --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-test --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-files
|
||||
save_exit_code cargo test --lib --tests -p=actix-multipart --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-web-actors --all-features
|
||||
save_exit_code cargo test --lib --tests -p=actix-router --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-http --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-web --features=rustls,openssl -- --nocapture --skip=test_reading_deflate_encoding_large_random_rustls
|
||||
save_exit_code cargo test --lib --tests -p=actix-web-codegen --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=awc --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-http-test --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-test --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-files -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-multipart --all-features -- --nocapture
|
||||
save_exit_code cargo test --lib --tests -p=actix-web-actors --all-features -- --nocapture
|
||||
|
||||
save_exit_code cargo test --workspace --doc
|
||||
|
||||
|
@ -109,6 +109,7 @@ impl<T> App<T> {
|
||||
/// .route("/", web::get().to(handler))
|
||||
/// })
|
||||
/// ```
|
||||
#[doc(alias = "manage")]
|
||||
pub fn app_data<U: 'static>(mut self, ext: U) -> Self {
|
||||
self.extensions.insert(ext);
|
||||
self
|
||||
|
45
src/data.rs
45
src/data.rs
@ -19,23 +19,32 @@ pub(crate) trait DataFactory {
|
||||
pub(crate) type FnDataFactory =
|
||||
Box<dyn Fn() -> LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>;
|
||||
|
||||
/// Application data.
|
||||
/// Application data wrapper and extractor.
|
||||
///
|
||||
/// Application level data is a piece of arbitrary data attached to the app, scope, or resource.
|
||||
/// Application data is available to all routes and can be added during the application
|
||||
/// configuration process via `App::data()`.
|
||||
/// # Setting Data
|
||||
/// Data is set using the `app_data` methods on `App`, `Scope`, and `Resource`. If data is wrapped
|
||||
/// in this `Data` type for those calls, it can be used as an extractor.
|
||||
///
|
||||
/// Application data can be accessed by using `Data<T>` extractor where `T` is data type.
|
||||
/// Note that `Data` should be constructed _outside_ the `HttpServer::new` closure if shared,
|
||||
/// potentially mutable state is desired. `Data` is cheap to clone; internally, it uses an `Arc`.
|
||||
///
|
||||
/// **Note**: HTTP server accepts an application factory rather than an application instance. HTTP
|
||||
/// server constructs an application instance for each thread, thus application data must be
|
||||
/// constructed multiple times. If you want to share data between different threads, a shareable
|
||||
/// object should be used, e.g. `Send + Sync`. Application data does not need to be `Send`
|
||||
/// or `Sync`. Internally `Data` contains an `Arc`.
|
||||
/// See also [`App::app_data`](crate::App::app_data), [`Scope::app_data`](crate::Scope::app_data),
|
||||
/// and [`Resource::app_data`](crate::Resource::app_data).
|
||||
///
|
||||
/// # Extracting `Data`
|
||||
/// Since the Actix Web router layers application data, the returned object will reference the
|
||||
/// "closest" instance of the type. For example, if an `App` stores a `u32`, a nested `Scope`
|
||||
/// also stores a `u32`, and the delegated request handler falls within that `Scope`, then
|
||||
/// extracting a `web::<Data<u32>>` for that handler will return the `Scope`'s instance.
|
||||
/// However, using the same router set up and a request that does not get captured by the `Scope`,
|
||||
/// `web::<Data<u32>>` would return the `App`'s instance.
|
||||
///
|
||||
/// If route data is not set for a handler, using `Data<T>` extractor would cause a `500 Internal
|
||||
/// Server Error` response.
|
||||
///
|
||||
/// See also [`HttpRequest::app_data`]
|
||||
/// and [`ServiceRequest::app_data`](crate::dev::ServiceRequest::app_data).
|
||||
///
|
||||
/// # Unsized Data
|
||||
/// For types that are unsized, most commonly `dyn T`, `Data` can wrap these types by first
|
||||
/// constructing an `Arc<dyn T>` and using the `From` implementation to convert it.
|
||||
@ -79,6 +88,7 @@ pub(crate) type FnDataFactory =
|
||||
/// .route("/index.html", web::get().to(index))
|
||||
/// .route("/index-alt.html", web::get().to(index_alt));
|
||||
/// ```
|
||||
#[doc(alias = "state")]
|
||||
#[derive(Debug)]
|
||||
pub struct Data<T: ?Sized>(Arc<T>);
|
||||
|
||||
@ -90,12 +100,12 @@ impl<T> Data<T> {
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Data<T> {
|
||||
/// Get reference to inner app data.
|
||||
/// Returns reference to inner `T`.
|
||||
pub fn get_ref(&self) -> &T {
|
||||
self.0.as_ref()
|
||||
}
|
||||
|
||||
/// Convert to the internal Arc<T>
|
||||
/// Unwraps to the internal `Arc<T>`
|
||||
pub fn into_inner(self) -> Arc<T> {
|
||||
self.0
|
||||
}
|
||||
@ -143,13 +153,16 @@ impl<T: ?Sized + 'static> FromRequest for Data<T> {
|
||||
ok(st.clone())
|
||||
} else {
|
||||
log::debug!(
|
||||
"Failed to construct App-level Data extractor. \
|
||||
Request path: {:?} (type: {})",
|
||||
req.path(),
|
||||
"Failed to extract `Data<{}>` for `{}` handler. For the Data extractor to work \
|
||||
correctly, wrap the data with `Data::new()` and pass it to `App::app_data()`. \
|
||||
Ensure that types align in both the set and retrieve calls.",
|
||||
type_name::<T>(),
|
||||
req.match_name().unwrap_or_else(|| req.path())
|
||||
);
|
||||
|
||||
err(ErrorInternalServerError(
|
||||
"App data is not configured, to configure construct it with web::Data::new() and pass it to App::app_data()",
|
||||
"Requested application data is not configured correctly. \
|
||||
View/enable debug logs for more details.",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
63
src/dev.rs
63
src/dev.rs
@ -20,11 +20,7 @@ pub use crate::info::{ConnectionInfo, PeerAddr};
|
||||
pub use crate::rmap::ResourceMap;
|
||||
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
||||
|
||||
pub use crate::types::form::UrlEncoded;
|
||||
pub use crate::types::json::JsonBody;
|
||||
pub use crate::types::readlines::Readlines;
|
||||
|
||||
use crate::http::header::ContentEncoding;
|
||||
pub use crate::types::{JsonBody, Readlines, UrlEncoded};
|
||||
|
||||
use actix_router::Patterns;
|
||||
|
||||
@ -46,60 +42,3 @@ pub(crate) fn ensure_leading_slash(mut patterns: Patterns) -> Patterns {
|
||||
|
||||
patterns
|
||||
}
|
||||
|
||||
/// Helper trait that allows to set specific encoding for response.
|
||||
pub trait BodyEncoding {
|
||||
/// Get content encoding
|
||||
fn get_encoding(&self) -> Option<ContentEncoding>;
|
||||
|
||||
/// Set content encoding
|
||||
///
|
||||
/// Must be used with [`crate::middleware::Compress`] to take effect.
|
||||
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
|
||||
}
|
||||
|
||||
impl BodyEncoding for actix_http::ResponseBuilder {
|
||||
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||
self.extensions().get::<Enc>().map(|enc| enc.0)
|
||||
}
|
||||
|
||||
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
|
||||
self.extensions_mut().insert(Enc(encoding));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
struct Enc(ContentEncoding);
|
||||
|
||||
impl<B> BodyEncoding for actix_http::Response<B> {
|
||||
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||
self.extensions().get::<Enc>().map(|enc| enc.0)
|
||||
}
|
||||
|
||||
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
|
||||
self.extensions_mut().insert(Enc(encoding));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl BodyEncoding for crate::HttpResponseBuilder {
|
||||
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||
self.extensions().get::<Enc>().map(|enc| enc.0)
|
||||
}
|
||||
|
||||
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
|
||||
self.extensions_mut().insert(Enc(encoding));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> BodyEncoding for crate::HttpResponse<B> {
|
||||
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||
self.extensions().get::<Enc>().map(|enc| enc.0)
|
||||
}
|
||||
|
||||
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
|
||||
self.extensions_mut().insert(Enc(encoding));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@ -118,15 +118,13 @@ where
|
||||
|
||||
macro_rules! error_helper {
|
||||
($name:ident, $status:ident) => {
|
||||
paste::paste! {
|
||||
#[doc = "Helper function that wraps any error and generates a `" $status "` response."]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn $name<T>(err: T) -> Error
|
||||
where
|
||||
T: fmt::Debug + fmt::Display + 'static,
|
||||
{
|
||||
InternalError::new(err, StatusCode::$status).into()
|
||||
}
|
||||
#[doc = concat!("Helper function that wraps any error and generates a `", stringify!($status), "` response.")]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn $name<T>(err: T) -> Error
|
||||
where
|
||||
T: fmt::Debug + fmt::Display + 'static,
|
||||
{
|
||||
InternalError::new(err, StatusCode::$status).into()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
30
src/guard.rs
30
src/guard.rs
@ -270,22 +270,20 @@ impl Guard for MethodGuard {
|
||||
|
||||
macro_rules! method_guard {
|
||||
($method_fn:ident, $method_const:ident) => {
|
||||
paste::paste! {
|
||||
#[doc = " Creates a guard that matches the `" $method_const "` request method."]
|
||||
///
|
||||
/// # Examples
|
||||
#[doc = " The route in this example will only respond to `" $method_const "` requests."]
|
||||
/// ```
|
||||
/// use actix_web::{guard, web, HttpResponse};
|
||||
///
|
||||
/// web::route()
|
||||
#[doc = " .guard(guard::" $method_fn "())"]
|
||||
/// .to(|| HttpResponse::Ok());
|
||||
/// ```
|
||||
#[allow(non_snake_case)]
|
||||
pub fn $method_fn() -> impl Guard {
|
||||
MethodGuard(HttpMethod::$method_const)
|
||||
}
|
||||
#[doc = concat!("Creates a guard that matches the `", stringify!($method_const), "` request method.")]
|
||||
///
|
||||
/// # Examples
|
||||
#[doc = concat!("The route in this example will only respond to `", stringify!($method_const), "` requests.")]
|
||||
/// ```
|
||||
/// use actix_web::{guard, web, HttpResponse};
|
||||
///
|
||||
/// web::route()
|
||||
#[doc = concat!(" .guard(guard::", stringify!($method_fn), "())")]
|
||||
/// .to(|| HttpResponse::Ok());
|
||||
/// ```
|
||||
#[allow(non_snake_case)]
|
||||
pub fn $method_fn() -> impl Guard {
|
||||
MethodGuard(HttpMethod::$method_const)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -147,6 +147,39 @@ impl Accept {
|
||||
Accept(vec![QualityItem::max(mime::TEXT_HTML)])
|
||||
}
|
||||
|
||||
// TODO: method for getting best content encoding based on q-factors, available from server side
|
||||
// and if none are acceptable return None
|
||||
|
||||
/// Extracts the most preferable mime type, accounting for [q-factor weighting].
|
||||
///
|
||||
/// If no q-factors are provided, the first mime type is chosen. Note that items without
|
||||
/// q-factors are given the maximum preference value.
|
||||
///
|
||||
/// As per the spec, will return [`mime::STAR_STAR`] (indicating no preference) if the contained
|
||||
/// list is empty.
|
||||
///
|
||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||
pub fn preference(&self) -> Mime {
|
||||
use actix_http::header::Quality;
|
||||
|
||||
let mut max_item = None;
|
||||
let mut max_pref = Quality::ZERO;
|
||||
|
||||
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||
|
||||
for pref in &self.0 {
|
||||
// only change if strictly greater
|
||||
// equal items, even while unsorted, still have higher preference if they appear first
|
||||
if pref.quality > max_pref {
|
||||
max_pref = pref.quality;
|
||||
max_item = Some(pref.item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
max_item.unwrap_or(mime::STAR_STAR)
|
||||
}
|
||||
|
||||
/// Returns a sorted list of mime types from highest to lowest preference, accounting for
|
||||
/// [q-factor weighting] and specificity.
|
||||
///
|
||||
@ -196,36 +229,6 @@ impl Accept {
|
||||
|
||||
types.into_iter().map(|qitem| qitem.item).collect()
|
||||
}
|
||||
|
||||
/// Extracts the most preferable mime type, accounting for [q-factor weighting].
|
||||
///
|
||||
/// If no q-factors are provided, the first mime type is chosen. Note that items without
|
||||
/// q-factors are given the maximum preference value.
|
||||
///
|
||||
/// As per the spec, will return [`mime::STAR_STAR`] (indicating no preference) if the contained
|
||||
/// list is empty.
|
||||
///
|
||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||
pub fn preference(&self) -> Mime {
|
||||
use actix_http::header::Quality;
|
||||
|
||||
let mut max_item = None;
|
||||
let mut max_pref = Quality::MIN;
|
||||
|
||||
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||
|
||||
for pref in &self.0 {
|
||||
// only change if strictly greater
|
||||
// equal items, even while unsorted, still have higher preference if they appear first
|
||||
if pref.quality > max_pref {
|
||||
max_pref = pref.quality;
|
||||
max_item = Some(pref.item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
max_item.unwrap_or(mime::STAR_STAR)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -239,7 +242,7 @@ mod tests {
|
||||
assert!(test.ranked().is_empty());
|
||||
|
||||
let test = Accept(vec![QualityItem::max(mime::APPLICATION_JSON)]);
|
||||
assert_eq!(test.ranked(), vec!(mime::APPLICATION_JSON));
|
||||
assert_eq!(test.ranked(), vec![mime::APPLICATION_JSON]);
|
||||
|
||||
let test = Accept(vec
|
||||
common_header! {
|
||||
/// `Accept-Charset` header, defined in [RFC 7231 §5.3.3].
|
||||
///
|
||||
/// The `Accept-Charset` header field can be sent by a user agent to
|
||||
/// indicate what charsets are acceptable in textual response content.
|
||||
@ -52,10 +51,12 @@ crate::http::header::common_header! {
|
||||
/// AcceptCharset(vec![QualityItem::max(Charset::Ext("utf-8".to_owned()))])
|
||||
/// );
|
||||
/// ```
|
||||
(AcceptCharset, ACCEPT_CHARSET) => (QualityItem<Charset>)+
|
||||
///
|
||||
/// [RFC 7231 §5.3.3]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.3
|
||||
(AcceptCharset, ACCEPT_CHARSET) => (QualityItem<Charset>)*
|
||||
|
||||
test_parse_and_format {
|
||||
// Test case from RFC
|
||||
crate::http::header::common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
|
||||
common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,15 @@
|
||||
use actix_http::header::QualityItem;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use super::{common_header, Encoding};
|
||||
use super::{common_header, ContentEncoding, Encoding, Preference, Quality, QualityItem};
|
||||
use crate::http::header;
|
||||
|
||||
common_header! {
|
||||
/// `Accept-Encoding` header, defined
|
||||
/// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
|
||||
///
|
||||
/// The `Accept-Encoding` header field can be used by user agents to
|
||||
/// indicate what response content-codings are
|
||||
/// acceptable in the response. An `identity` token is used as a synonym
|
||||
/// for "no encoding" in order to communicate when no encoding is
|
||||
/// preferred.
|
||||
/// The `Accept-Encoding` header field can be used by user agents to indicate what response
|
||||
/// content-codings are acceptable in the response. An `identity` token is used as a synonym
|
||||
/// for "no encoding" in order to communicate when no encoding is preferred.
|
||||
///
|
||||
/// # ABNF
|
||||
/// ```plain
|
||||
@ -29,11 +27,11 @@ common_header! {
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::HttpResponse;
|
||||
/// use actix_web::http::header::{AcceptEncoding, Encoding, QualityItem};
|
||||
/// use actix_web::http::header::{AcceptEncoding, Encoding, Preference, QualityItem};
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// AcceptEncoding(vec![QualityItem::max(Encoding::Chunked)])
|
||||
/// AcceptEncoding(vec![QualityItem::max(Preference::Specific(Encoding::gzip()))])
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
@ -44,40 +42,388 @@ common_header! {
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// AcceptEncoding(vec![
|
||||
/// QualityItem::max(Encoding::Chunked),
|
||||
/// QualityItem::max(Encoding::Gzip),
|
||||
/// QualityItem::max(Encoding::Deflate),
|
||||
/// "gzip".parse().unwrap(),
|
||||
/// "br".parse().unwrap(),
|
||||
/// ])
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::HttpResponse;
|
||||
/// use actix_web::http::header::{AcceptEncoding, Encoding, QualityItem, q};
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// AcceptEncoding(vec![
|
||||
/// QualityItem::max(Encoding::Chunked),
|
||||
/// QualityItem::new(Encoding::Gzip, q(0.60)),
|
||||
/// QualityItem::min(Encoding::EncodingExt("*".to_owned())),
|
||||
/// ])
|
||||
/// );
|
||||
/// ```
|
||||
(AcceptEncoding, header::ACCEPT_ENCODING) => (QualityItem<Encoding>)*
|
||||
(AcceptEncoding, header::ACCEPT_ENCODING) => (QualityItem<Preference<Encoding>>)*
|
||||
|
||||
test_parse_and_format {
|
||||
// From the RFC
|
||||
common_header_test!(test1, vec![b"compress, gzip"]);
|
||||
common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
||||
common_header_test!(test3, vec![b"*"]);
|
||||
common_header_test!(no_headers, vec![b""; 0], Some(AcceptEncoding(vec![])));
|
||||
common_header_test!(empty_header, vec![b""; 1], Some(AcceptEncoding(vec![])));
|
||||
|
||||
common_header_test!(
|
||||
order_of_appearance,
|
||||
vec![b"br, gzip"],
|
||||
Some(AcceptEncoding(vec![
|
||||
QualityItem::max(Preference::Specific(Encoding::brotli())),
|
||||
QualityItem::max(Preference::Specific(Encoding::gzip())),
|
||||
]))
|
||||
);
|
||||
|
||||
common_header_test!(any, vec![b"*"], Some(AcceptEncoding(vec![
|
||||
QualityItem::max(Preference::Any),
|
||||
])));
|
||||
|
||||
// Note: Removed quality 1 from gzip
|
||||
common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
||||
common_header_test!(implicit_quality, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
||||
|
||||
// Note: Removed quality 1 from gzip
|
||||
common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
||||
common_header_test!(implicit_quality_out_of_order, vec![b"compress;q=0.5, gzip"]);
|
||||
|
||||
common_header_test!(
|
||||
only_gzip_no_identity,
|
||||
vec![b"gzip, *; q=0"],
|
||||
Some(AcceptEncoding(vec![
|
||||
QualityItem::max(Preference::Specific(Encoding::gzip())),
|
||||
QualityItem::zero(Preference::Any),
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: shortcut for EncodingExt(*) = Any
|
||||
impl AcceptEncoding {
|
||||
/// Selects the most acceptable encoding according to client preference and supported types.
|
||||
///
|
||||
/// The "identity" encoding is not assumed and should be included in the `supported` iterator
|
||||
/// if a non-encoded representation can be selected.
|
||||
///
|
||||
/// If `None` is returned, this indicates that none of the supported encodings are acceptable to
|
||||
/// the client. The caller should generate a 406 Not Acceptable response (unencoded) that
|
||||
/// includes the server's supported encodings in the body plus a [`Vary`] header.
|
||||
///
|
||||
/// [`Vary`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary
|
||||
pub fn negotiate<'a>(
|
||||
&self,
|
||||
supported: impl Iterator<Item = &'a Encoding>,
|
||||
) -> Option<Encoding> {
|
||||
// 1. If no Accept-Encoding field is in the request, any content-coding is considered
|
||||
// acceptable by the user agent.
|
||||
|
||||
let supported_set = supported.collect::<HashSet<_>>();
|
||||
|
||||
if supported_set.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if self.0.is_empty() {
|
||||
// though it is not recommended to encode in this case, return identity encoding
|
||||
return Some(Encoding::identity());
|
||||
}
|
||||
|
||||
// 2. If the representation has no content-coding, then it is acceptable by default unless
|
||||
// specifically excluded by the Accept-Encoding field stating either "identity;q=0" or
|
||||
// "*;q=0" without a more specific entry for "identity".
|
||||
|
||||
let acceptable_items = self.ranked_items().collect::<Vec<_>>();
|
||||
|
||||
let identity_acceptable = is_identity_acceptable(&acceptable_items);
|
||||
let identity_supported = supported_set.contains(&Encoding::identity());
|
||||
|
||||
if identity_acceptable && identity_supported && supported_set.len() == 1 {
|
||||
return Some(Encoding::identity());
|
||||
}
|
||||
|
||||
// 3. If the representation's content-coding is one of the content-codings listed in the
|
||||
// Accept-Encoding field, then it is acceptable unless it is accompanied by a qvalue of 0.
|
||||
|
||||
// 4. If multiple content-codings are acceptable, then the acceptable content-coding with
|
||||
// the highest non-zero qvalue is preferred.
|
||||
|
||||
let matched = acceptable_items
|
||||
.into_iter()
|
||||
.filter(|q| q.quality > Quality::ZERO)
|
||||
// search relies on item list being in descending order of quality
|
||||
.find(|q| {
|
||||
let enc = &q.item;
|
||||
matches!(enc, Preference::Specific(enc) if supported_set.contains(enc))
|
||||
})
|
||||
.map(|q| q.item);
|
||||
|
||||
match matched {
|
||||
Some(Preference::Specific(enc)) => Some(enc),
|
||||
|
||||
_ if identity_acceptable => Some(Encoding::identity()),
|
||||
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts the most preferable encoding, accounting for [q-factor weighting].
|
||||
///
|
||||
/// If no q-factors are provided, the first encoding is chosen. Note that items without
|
||||
/// q-factors are given the maximum preference value.
|
||||
///
|
||||
/// As per the spec, returns [`Preference::Any`] if acceptable list is empty. Though, if this is
|
||||
/// returned, it is recommended to use an un-encoded representation.
|
||||
///
|
||||
/// If `None` is returned, it means that the client has signalled that no representations
|
||||
/// are acceptable. This should never occur for a well behaved user-agent.
|
||||
///
|
||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||
pub fn preference(&self) -> Option<Preference<Encoding>> {
|
||||
// empty header indicates no preference
|
||||
if self.0.is_empty() {
|
||||
return Some(Preference::Any);
|
||||
}
|
||||
|
||||
let mut max_item = None;
|
||||
let mut max_pref = Quality::ZERO;
|
||||
|
||||
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||
|
||||
for pref in &self.0 {
|
||||
// only change if strictly greater
|
||||
// equal items, even while unsorted, still have higher preference if they appear first
|
||||
if pref.quality > max_pref {
|
||||
max_pref = pref.quality;
|
||||
max_item = Some(pref.item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Return max_item if any items were above 0 quality...
|
||||
max_item.or_else(|| {
|
||||
// ...or else check for "*" or "identity". We can elide quality checks since
|
||||
// entering this block means all items had "q=0".
|
||||
match self.0.iter().find(|pref| {
|
||||
matches!(
|
||||
pref.item,
|
||||
Preference::Any
|
||||
| Preference::Specific(Encoding::Known(ContentEncoding::Identity))
|
||||
)
|
||||
}) {
|
||||
// "identity" or "*" found so no representation is acceptable
|
||||
Some(_) => None,
|
||||
|
||||
// implicit "identity" is acceptable
|
||||
None => Some(Preference::Specific(Encoding::identity())),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns a sorted list of encodings from highest to lowest precedence, accounting
|
||||
/// for [q-factor weighting].
|
||||
///
|
||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||
pub fn ranked(&self) -> Vec<Preference<Encoding>> {
|
||||
self.ranked_items().map(|q| q.item).collect()
|
||||
}
|
||||
|
||||
fn ranked_items(&self) -> impl Iterator<Item = QualityItem<Preference<Encoding>>> {
|
||||
if self.0.is_empty() {
|
||||
return vec![].into_iter();
|
||||
}
|
||||
|
||||
let mut types = self.0.clone();
|
||||
|
||||
// use stable sort so items with equal q-factor retain listed order
|
||||
types.sort_by(|a, b| {
|
||||
// sort by q-factor descending
|
||||
b.quality.cmp(&a.quality)
|
||||
});
|
||||
|
||||
types.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if "identity" is an acceptable encoding.
|
||||
///
|
||||
/// Internal algorithm relies on item list being in descending order of quality.
|
||||
fn is_identity_acceptable(items: &'_ [QualityItem<Preference<Encoding>>]) -> bool {
|
||||
if items.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Loop algorithm depends on items being sorted in descending order of quality. As such, it
|
||||
// is sufficient to return (q > 0) when reaching either an "identity" or "*" item.
|
||||
for q in items {
|
||||
match (q.quality, &q.item) {
|
||||
// occurrence of "identity;q=n"; return true if quality is non-zero
|
||||
(q, Preference::Specific(Encoding::Known(ContentEncoding::Identity))) => {
|
||||
return q > Quality::ZERO
|
||||
}
|
||||
|
||||
// occurrence of "*;q=n"; return true if quality is non-zero
|
||||
(q, Preference::Any) => return q > Quality::ZERO,
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// implicit acceptable identity
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::http::header::*;
|
||||
|
||||
macro_rules! accept_encoding {
|
||||
() => { AcceptEncoding(vec![]) };
|
||||
($($q:expr),+ $(,)?) => { AcceptEncoding(vec![$($q.parse().unwrap()),+]) };
|
||||
}
|
||||
|
||||
/// Parses an encoding string.
|
||||
fn enc(enc: &str) -> Preference<Encoding> {
|
||||
enc.parse().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detect_identity_acceptable() {
|
||||
macro_rules! accept_encoding_ranked {
|
||||
() => { accept_encoding!().ranked_items().collect::<Vec<_>>() };
|
||||
($($q:expr),+ $(,)?) => { accept_encoding!($($q),+).ranked_items().collect::<Vec<_>>() };
|
||||
}
|
||||
|
||||
let test = accept_encoding_ranked!();
|
||||
assert!(is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip");
|
||||
assert!(is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "br");
|
||||
assert!(is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "*;q=0.1");
|
||||
assert!(is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "identity;q=0.1");
|
||||
assert!(is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "identity;q=0.1", "*;q=0");
|
||||
assert!(is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "*;q=0", "identity;q=0.1");
|
||||
assert!(is_identity_acceptable(&test));
|
||||
|
||||
let test = accept_encoding_ranked!("gzip", "*;q=0");
|
||||
assert!(!is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "identity;q=0");
|
||||
assert!(!is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "identity;q=0", "*;q=0");
|
||||
assert!(!is_identity_acceptable(&test));
|
||||
let test = accept_encoding_ranked!("gzip", "*;q=0", "identity;q=0");
|
||||
assert!(!is_identity_acceptable(&test));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoding_negotiation() {
|
||||
// no preference
|
||||
let test = accept_encoding!();
|
||||
assert_eq!(test.negotiate([].iter()), None);
|
||||
|
||||
let test = accept_encoding!();
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::identity()].iter()),
|
||||
Some(Encoding::identity()),
|
||||
);
|
||||
|
||||
let test = accept_encoding!("identity;q=0");
|
||||
assert_eq!(test.negotiate([Encoding::identity()].iter()), None);
|
||||
|
||||
let test = accept_encoding!("*;q=0");
|
||||
assert_eq!(test.negotiate([Encoding::identity()].iter()), None);
|
||||
|
||||
let test = accept_encoding!();
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::identity()),
|
||||
);
|
||||
|
||||
let test = accept_encoding!("gzip");
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip()),
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::brotli(), Encoding::identity()].iter()),
|
||||
Some(Encoding::identity()),
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::brotli(), Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip()),
|
||||
);
|
||||
|
||||
let test = accept_encoding!("gzip", "identity;q=0");
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip()),
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::brotli(), Encoding::identity()].iter()),
|
||||
None
|
||||
);
|
||||
|
||||
let test = accept_encoding!("gzip", "*;q=0");
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip()),
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::brotli(), Encoding::identity()].iter()),
|
||||
None
|
||||
);
|
||||
|
||||
let test = accept_encoding!("gzip", "deflate", "br");
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip()),
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::brotli(), Encoding::identity()].iter()),
|
||||
Some(Encoding::brotli())
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::deflate(), Encoding::identity()].iter()),
|
||||
Some(Encoding::deflate())
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate(
|
||||
[Encoding::gzip(), Encoding::deflate(), Encoding::identity()].iter()
|
||||
),
|
||||
Some(Encoding::gzip())
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::gzip(), Encoding::brotli(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip())
|
||||
);
|
||||
assert_eq!(
|
||||
test.negotiate([Encoding::brotli(), Encoding::gzip(), Encoding::identity()].iter()),
|
||||
Some(Encoding::gzip())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ranking_precedence() {
|
||||
let test = accept_encoding!();
|
||||
assert!(test.ranked().is_empty());
|
||||
|
||||
let test = accept_encoding!("gzip");
|
||||
assert_eq!(test.ranked(), vec![enc("gzip")]);
|
||||
|
||||
let test = accept_encoding!("gzip;q=0.900", "*;q=0.700", "br;q=1.0");
|
||||
assert_eq!(test.ranked(), vec![enc("br"), enc("gzip"), enc("*")]);
|
||||
|
||||
let test = accept_encoding!("br", "gzip", "*");
|
||||
assert_eq!(test.ranked(), vec![enc("br"), enc("gzip"), enc("*")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preference_selection() {
|
||||
assert_eq!(accept_encoding!().preference(), Some(Preference::Any));
|
||||
|
||||
assert_eq!(accept_encoding!("identity;q=0").preference(), None);
|
||||
assert_eq!(accept_encoding!("*;q=0").preference(), None);
|
||||
assert_eq!(accept_encoding!("compress;q=0", "*;q=0").preference(), None);
|
||||
assert_eq!(accept_encoding!("identity;q=0", "*;q=0").preference(), None);
|
||||
|
||||
let test = accept_encoding!("*;q=0.5");
|
||||
assert_eq!(test.preference().unwrap(), enc("*"));
|
||||
|
||||
let test = accept_encoding!("br;q=0");
|
||||
assert_eq!(test.preference().unwrap(), enc("identity"));
|
||||
|
||||
let test = accept_encoding!("br;q=0.900", "gzip;q=1.0", "*;q=0.500");
|
||||
assert_eq!(test.preference().unwrap(), enc("gzip"));
|
||||
|
||||
let test = accept_encoding!("br", "gzip", "*");
|
||||
assert_eq!(test.preference().unwrap(), enc("br"));
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ common_header! {
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// AcceptLanguage(vec![
|
||||
/// QualityItem::max("en-US".parse().unwrap())
|
||||
/// "en-US".parse().unwrap(),
|
||||
/// ])
|
||||
/// );
|
||||
/// ```
|
||||
@ -49,9 +49,9 @@ common_header! {
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// AcceptLanguage(vec![
|
||||
/// QualityItem::max("da".parse().unwrap()),
|
||||
/// QualityItem::new("en-GB".parse().unwrap(), q(0.8)),
|
||||
/// QualityItem::new("en".parse().unwrap(), q(0.7)),
|
||||
/// "da".parse().unwrap(),
|
||||
/// "en-GB;q=0.8".parse().unwrap(),
|
||||
/// "en;q=0.7".parse().unwrap(),
|
||||
/// ])
|
||||
/// );
|
||||
/// ```
|
||||
@ -93,6 +93,33 @@ common_header! {
|
||||
}
|
||||
|
||||
impl AcceptLanguage {
|
||||
/// Extracts the most preferable language, accounting for [q-factor weighting].
|
||||
///
|
||||
/// If no q-factors are provided, the first language is chosen. Note that items without
|
||||
/// q-factors are given the maximum preference value.
|
||||
///
|
||||
/// As per the spec, returns [`Preference::Any`] if contained list is empty.
|
||||
///
|
||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||
pub fn preference(&self) -> Preference<LanguageTag> {
|
||||
let mut max_item = None;
|
||||
let mut max_pref = Quality::ZERO;
|
||||
|
||||
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||
|
||||
for pref in &self.0 {
|
||||
// only change if strictly greater
|
||||
// equal items, even while unsorted, still have higher preference if they appear first
|
||||
if pref.quality > max_pref {
|
||||
max_pref = pref.quality;
|
||||
max_item = Some(pref.item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
max_item.unwrap_or(Preference::Any)
|
||||
}
|
||||
|
||||
/// Returns a sorted list of languages from highest to lowest precedence, accounting
|
||||
/// for [q-factor weighting].
|
||||
///
|
||||
@ -112,33 +139,6 @@ impl AcceptLanguage {
|
||||
|
||||
types.into_iter().map(|qitem| qitem.item).collect()
|
||||
}
|
||||
|
||||
/// Extracts the most preferable language, accounting for [q-factor weighting].
|
||||
///
|
||||
/// If no q-factors are provided, the first language is chosen. Note that items without
|
||||
/// q-factors are given the maximum preference value.
|
||||
///
|
||||
/// As per the spec, returns [`Preference::Any`] if contained list is empty.
|
||||
///
|
||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||
pub fn preference(&self) -> Preference<LanguageTag> {
|
||||
let mut max_item = None;
|
||||
let mut max_pref = Quality::MIN;
|
||||
|
||||
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||
|
||||
for pref in &self.0 {
|
||||
// only change if strictly greater
|
||||
// equal items, even while unsorted, still have higher preference if they appear first
|
||||
if pref.quality > max_pref {
|
||||
max_pref = pref.quality;
|
||||
max_item = Some(pref.item.clone());
|
||||
}
|
||||
}
|
||||
|
||||
max_item.unwrap_or(Preference::Any)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -152,7 +152,7 @@ mod tests {
|
||||
assert!(test.ranked().is_empty());
|
||||
|
||||
let test = AcceptLanguage(vec![QualityItem::max("fr-CH".parse().unwrap())]);
|
||||
assert_eq!(test.ranked(), vec!("fr-CH".parse().unwrap()));
|
||||
assert_eq!(test.ranked(), vec!["fr-CH".parse().unwrap()]);
|
||||
|
||||
let test = AcceptLanguage(vec.
|
||||
// TODO: think about using private fields and smallvec
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ContentDisposition {
|
||||
/// The disposition type
|
||||
|
@ -1,69 +1,55 @@
|
||||
use std::{fmt, str};
|
||||
|
||||
pub use self::Encoding::{
|
||||
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd,
|
||||
};
|
||||
use actix_http::ContentEncoding;
|
||||
|
||||
/// A value to represent an encoding used in `Transfer-Encoding` or `Accept-Encoding` header.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
/// A value to represent an encoding used in the `Accept-Encoding` and `Content-Encoding` header.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Encoding {
|
||||
/// The `chunked` encoding.
|
||||
Chunked,
|
||||
/// A supported content encoding. See [`ContentEncoding`] for variants.
|
||||
Known(ContentEncoding),
|
||||
|
||||
/// The `br` encoding.
|
||||
Brotli,
|
||||
/// Some other encoding that is less common, can be any string.
|
||||
Unknown(String),
|
||||
}
|
||||
|
||||
/// The `gzip` encoding.
|
||||
Gzip,
|
||||
impl Encoding {
|
||||
pub const fn identity() -> Self {
|
||||
Self::Known(ContentEncoding::Identity)
|
||||
}
|
||||
|
||||
/// The `deflate` encoding.
|
||||
Deflate,
|
||||
pub const fn brotli() -> Self {
|
||||
Self::Known(ContentEncoding::Brotli)
|
||||
}
|
||||
|
||||
/// The `compress` encoding.
|
||||
Compress,
|
||||
pub const fn deflate() -> Self {
|
||||
Self::Known(ContentEncoding::Deflate)
|
||||
}
|
||||
|
||||
/// The `identity` encoding.
|
||||
Identity,
|
||||
pub const fn gzip() -> Self {
|
||||
Self::Known(ContentEncoding::Gzip)
|
||||
}
|
||||
|
||||
/// The `trailers` encoding.
|
||||
Trailers,
|
||||
|
||||
/// The `zstd` encoding.
|
||||
Zstd,
|
||||
|
||||
/// Some other encoding that is less common, can be any String.
|
||||
EncodingExt(String),
|
||||
pub const fn zstd() -> Self {
|
||||
Self::Known(ContentEncoding::Zstd)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Encoding {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match *self {
|
||||
Chunked => "chunked",
|
||||
Brotli => "br",
|
||||
Gzip => "gzip",
|
||||
Deflate => "deflate",
|
||||
Compress => "compress",
|
||||
Identity => "identity",
|
||||
Trailers => "trailers",
|
||||
Zstd => "zstd",
|
||||
EncodingExt(ref s) => s.as_ref(),
|
||||
f.write_str(match self {
|
||||
Encoding::Known(enc) => enc.as_str(),
|
||||
Encoding::Unknown(enc) => enc.as_str(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl str::FromStr for Encoding {
|
||||
type Err = crate::error::ParseError;
|
||||
fn from_str(s: &str) -> Result<Encoding, crate::error::ParseError> {
|
||||
match s {
|
||||
"chunked" => Ok(Chunked),
|
||||
"br" => Ok(Brotli),
|
||||
"deflate" => Ok(Deflate),
|
||||
"gzip" => Ok(Gzip),
|
||||
"compress" => Ok(Compress),
|
||||
"identity" => Ok(Identity),
|
||||
"trailers" => Ok(Trailers),
|
||||
"zstd" => Ok(Zstd),
|
||||
_ => Ok(EncodingExt(s.to_owned())),
|
||||
|
||||
fn from_str(enc: &str) -> Result<Self, crate::error::ParseError> {
|
||||
match enc.parse::<ContentEncoding>() {
|
||||
Ok(enc) => Ok(Self::Known(enc)),
|
||||
Err(_) => Ok(Self::Unknown(enc.to_owned())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,7 @@ fn check_slice_validity(slice: &str) -> bool {
|
||||
slice.bytes().all(entity_validate_char)
|
||||
}
|
||||
|
||||
/// An entity tag, defined
|
||||
/// in [RFC 7232 §2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
|
||||
/// An entity tag, defined in [RFC 7232 §2.3].
|
||||
///
|
||||
/// An entity tag consists of a string enclosed by two literal double quotes.
|
||||
/// Preceding the first double quote is an optional weakness indicator,
|
||||
@ -48,16 +47,20 @@ fn check_slice_validity(slice: &str) -> bool {
|
||||
/// | `W/"1"` | `W/"2"` | no match | no match |
|
||||
/// | `W/"1"` | `"1"` | no match | match |
|
||||
/// | `"1"` | `"1"` | match | match |
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
///
|
||||
/// [RFC 7232 §2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EntityTag {
|
||||
/// Weakness indicator for the tag
|
||||
pub weak: bool,
|
||||
|
||||
/// The opaque string in between the DQUOTEs
|
||||
tag: String,
|
||||
}
|
||||
|
||||
impl EntityTag {
|
||||
/// Constructs a new EntityTag.
|
||||
/// Constructs a new `EntityTag`.
|
||||
///
|
||||
/// # Panics
|
||||
/// If the tag contains invalid characters.
|
||||
pub fn new(weak: bool, tag: String) -> EntityTag {
|
||||
@ -66,51 +69,64 @@ impl EntityTag {
|
||||
}
|
||||
|
||||
/// Constructs a new weak EntityTag.
|
||||
///
|
||||
/// # Panics
|
||||
/// If the tag contains invalid characters.
|
||||
pub fn weak(tag: String) -> EntityTag {
|
||||
pub fn new_weak(tag: String) -> EntityTag {
|
||||
EntityTag::new(true, tag)
|
||||
}
|
||||
|
||||
#[deprecated(since = "3.0.0", note = "Renamed to `new_weak`.")]
|
||||
pub fn weak(tag: String) -> EntityTag {
|
||||
Self::new_weak(tag)
|
||||
}
|
||||
|
||||
/// Constructs a new strong EntityTag.
|
||||
///
|
||||
/// # Panics
|
||||
/// If the tag contains invalid characters.
|
||||
pub fn strong(tag: String) -> EntityTag {
|
||||
pub fn new_strong(tag: String) -> EntityTag {
|
||||
EntityTag::new(false, tag)
|
||||
}
|
||||
|
||||
/// Get the tag.
|
||||
#[deprecated(since = "3.0.0", note = "Renamed to `new_strong`.")]
|
||||
pub fn strong(tag: String) -> EntityTag {
|
||||
Self::new_strong(tag)
|
||||
}
|
||||
|
||||
/// Returns tag.
|
||||
pub fn tag(&self) -> &str {
|
||||
self.tag.as_ref()
|
||||
}
|
||||
|
||||
/// Set the tag.
|
||||
/// Sets tag.
|
||||
///
|
||||
/// # Panics
|
||||
/// If the tag contains invalid characters.
|
||||
pub fn set_tag(&mut self, tag: String) {
|
||||
pub fn set_tag(&mut self, tag: impl Into<String>) {
|
||||
let tag = tag.into();
|
||||
assert!(check_slice_validity(&tag), "Invalid tag: {:?}", tag);
|
||||
self.tag = tag
|
||||
}
|
||||
|
||||
/// For strong comparison two entity-tags are equivalent if both are not
|
||||
/// weak and their opaque-tags match character-by-character.
|
||||
/// For strong comparison two entity-tags are equivalent if both are not weak and their
|
||||
/// opaque-tags match character-by-character.
|
||||
pub fn strong_eq(&self, other: &EntityTag) -> bool {
|
||||
!self.weak && !other.weak && self.tag == other.tag
|
||||
}
|
||||
|
||||
/// For weak comparison two entity-tags are equivalent if their
|
||||
/// opaque-tags match character-by-character, regardless of either or
|
||||
/// both being tagged as "weak".
|
||||
/// For weak comparison two entity-tags are equivalent if their opaque-tags match
|
||||
/// character-by-character, regardless of either or both being tagged as "weak".
|
||||
pub fn weak_eq(&self, other: &EntityTag) -> bool {
|
||||
self.tag == other.tag
|
||||
}
|
||||
|
||||
/// The inverse of `EntityTag.strong_eq()`.
|
||||
/// Returns the inverse of `strong_eq()`.
|
||||
pub fn strong_ne(&self, other: &EntityTag) -> bool {
|
||||
!self.strong_eq(other)
|
||||
}
|
||||
|
||||
/// The inverse of `EntityTag.weak_eq()`.
|
||||
/// Returns inverse of `weak_eq()`.
|
||||
pub fn weak_ne(&self, other: &EntityTag) -> bool {
|
||||
!self.weak_eq(other)
|
||||
}
|
||||
@ -178,23 +194,23 @@ mod tests {
|
||||
// Expected success
|
||||
assert_eq!(
|
||||
"\"foobar\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::strong("foobar".to_owned())
|
||||
EntityTag::new_strong("foobar".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"\"\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::strong("".to_owned())
|
||||
EntityTag::new_strong("".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"W/\"weaktag\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("weaktag".to_owned())
|
||||
EntityTag::new_weak("weaktag".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"W/\"\x65\x62\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("\x65\x62".to_owned())
|
||||
EntityTag::new_weak("\x65\x62".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
"W/\"\"".parse::<EntityTag>().unwrap(),
|
||||
EntityTag::weak("".to_owned())
|
||||
EntityTag::new_weak("".to_owned())
|
||||
);
|
||||
}
|
||||
|
||||
@ -214,19 +230,19 @@ mod tests {
|
||||
#[test]
|
||||
fn test_etag_fmt() {
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::strong("foobar".to_owned())),
|
||||
format!("{}", EntityTag::new_strong("foobar".to_owned())),
|
||||
"\"foobar\""
|
||||
);
|
||||
assert_eq!(format!("{}", EntityTag::strong("".to_owned())), "\"\"");
|
||||
assert_eq!(format!("{}", EntityTag::new_strong("".to_owned())), "\"\"");
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::weak("weak-etag".to_owned())),
|
||||
format!("{}", EntityTag::new_weak("weak-etag".to_owned())),
|
||||
"W/\"weak-etag\""
|
||||
);
|
||||
assert_eq!(
|
||||
format!("{}", EntityTag::weak("\u{0065}".to_owned())),
|
||||
format!("{}", EntityTag::new_weak("\u{0065}".to_owned())),
|
||||
"W/\"\x65\""
|
||||
);
|
||||
assert_eq!(format!("{}", EntityTag::weak("".to_owned())), "W/\"\"");
|
||||
assert_eq!(format!("{}", EntityTag::new_weak("".to_owned())), "W/\"\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -237,29 +253,29 @@ mod tests {
|
||||
// | `W/"1"` | `W/"2"` | no match | no match |
|
||||
// | `W/"1"` | `"1"` | no match | match |
|
||||
// | `"1"` | `"1"` | match | match |
|
||||
let mut etag1 = EntityTag::weak("1".to_owned());
|
||||
let mut etag2 = EntityTag::weak("1".to_owned());
|
||||
let mut etag1 = EntityTag::new_weak("1".to_owned());
|
||||
let mut etag2 = EntityTag::new_weak("1".to_owned());
|
||||
assert!(!etag1.strong_eq(&etag2));
|
||||
assert!(etag1.weak_eq(&etag2));
|
||||
assert!(etag1.strong_ne(&etag2));
|
||||
assert!(!etag1.weak_ne(&etag2));
|
||||
|
||||
etag1 = EntityTag::weak("1".to_owned());
|
||||
etag2 = EntityTag::weak("2".to_owned());
|
||||
etag1 = EntityTag::new_weak("1".to_owned());
|
||||
etag2 = EntityTag::new_weak("2".to_owned());
|
||||
assert!(!etag1.strong_eq(&etag2));
|
||||
assert!(!etag1.weak_eq(&etag2));
|
||||
assert!(etag1.strong_ne(&etag2));
|
||||
assert!(etag1.weak_ne(&etag2));
|
||||
|
||||
etag1 = EntityTag::weak("1".to_owned());
|
||||
etag2 = EntityTag::strong("1".to_owned());
|
||||
etag1 = EntityTag::new_weak("1".to_owned());
|
||||
etag2 = EntityTag::new_strong("1".to_owned());
|
||||
assert!(!etag1.strong_eq(&etag2));
|
||||
assert!(etag1.weak_eq(&etag2));
|
||||
assert!(etag1.strong_ne(&etag2));
|
||||
assert!(!etag1.weak_ne(&etag2));
|
||||
|
||||
etag1 = EntityTag::strong("1".to_owned());
|
||||
etag2 = EntityTag::strong("1".to_owned());
|
||||
etag1 = EntityTag::new_strong("1".to_owned());
|
||||
etag2 = EntityTag::new_strong("1".to_owned());
|
||||
assert!(etag1.strong_eq(&etag2));
|
||||
assert!(etag1.weak_eq(&etag2));
|
||||
assert!(!etag1.strong_ne(&etag2));
|
||||
|
@ -31,7 +31,7 @@ crate::http::header::common_header! {
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// ETag(EntityTag::new(false, "xyzzy".to_owned()))
|
||||
/// ETag(EntityTag::new_strong("xyzzy".to_owned()))
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
@ -41,7 +41,7 @@ crate::http::header::common_header! {
|
||||
///
|
||||
/// let mut builder = HttpResponse::Ok();
|
||||
/// builder.insert_header(
|
||||
/// ETag(EntityTag::new(true, "xyzzy".to_owned()))
|
||||
/// ETag(EntityTag::new_weak("xyzzy".to_owned()))
|
||||
/// );
|
||||
/// ```
|
||||
(ETag, ETAG) => [EntityTag]
|
||||
@ -50,29 +50,29 @@ crate::http::header::common_header! {
|
||||
// From the RFC
|
||||
crate::http::header::common_header_test!(test1,
|
||||
vec![b"\"xyzzy\""],
|
||||
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
|
||||
Some(ETag(EntityTag::new_strong("xyzzy".to_owned()))));
|
||||
crate::http::header::common_header_test!(test2,
|
||||
vec![b"W/\"xyzzy\""],
|
||||
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
|
||||
Some(ETag(EntityTag::new_weak("xyzzy".to_owned()))));
|
||||
crate::http::header::common_header_test!(test3,
|
||||
vec![b"\"\""],
|
||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||
Some(ETag(EntityTag::new_strong("".to_owned()))));
|
||||
// Own tests
|
||||
crate::http::header::common_header_test!(test4,
|
||||
vec![b"\"foobar\""],
|
||||
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
|
||||
Some(ETag(EntityTag::new_strong("foobar".to_owned()))));
|
||||
crate::http::header::common_header_test!(test5,
|
||||
vec![b"\"\""],
|
||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||
Some(ETag(EntityTag::new_strong("".to_owned()))));
|
||||
crate::http::header::common_header_test!(test6,
|
||||
vec![b"W/\"weak-etag\""],
|
||||
Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
|
||||
Some(ETag(EntityTag::new_weak("weak-etag".to_owned()))));
|
||||
crate::http::header::common_header_test!(test7,
|
||||
vec![b"W/\"\x65\x62\""],
|
||||
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
|
||||
Some(ETag(EntityTag::new_weak("\u{0065}\u{0062}".to_owned()))));
|
||||
crate::http::header::common_header_test!(test8,
|
||||
vec![b"W/\"\""],
|
||||
Some(ETag(EntityTag::new(true, "".to_owned()))));
|
||||
Some(ETag(EntityTag::new_weak("".to_owned()))));
|
||||
crate::http::header::common_header_test!(test9,
|
||||
vec![b"no-dquotes"],
|
||||
None::<ETag>);
|
||||
|
@ -54,14 +54,15 @@ common_header! {
|
||||
test1,
|
||||
vec![b"\"xyzzy\""],
|
||||
Some(HeaderField::Items(
|
||||
vec![EntityTag::new(false, "xyzzy".to_owned())])));
|
||||
vec![EntityTag::new_strong("xyzzy".to_owned())])));
|
||||
|
||||
crate::http::header::common_header_test!(
|
||||
test2,
|
||||
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
|
||||
Some(HeaderField::Items(
|
||||
vec![EntityTag::new(false, "xyzzy".to_owned()),
|
||||
EntityTag::new(false, "r2d2xxxx".to_owned()),
|
||||
EntityTag::new(false, "c3piozzzz".to_owned())])));
|
||||
vec![EntityTag::new_strong("xyzzy".to_owned()),
|
||||
EntityTag::new_strong("r2d2xxxx".to_owned()),
|
||||
EntityTag::new_strong("c3piozzzz".to_owned())])));
|
||||
crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
|
||||
}
|
||||
}
|
||||
|
@ -82,8 +82,8 @@ mod tests {
|
||||
|
||||
if_none_match = Header::parse(&req);
|
||||
let mut entities: Vec<EntityTag> = Vec::new();
|
||||
let foobar_etag = EntityTag::new(false, "foobar".to_owned());
|
||||
let weak_etag = EntityTag::new(true, "weak-etag".to_owned());
|
||||
let foobar_etag = EntityTag::new_strong("foobar".to_owned());
|
||||
let weak_etag = EntityTag::new_weak("weak-etag".to_owned());
|
||||
entities.push(foobar_etag);
|
||||
entities.push(weak_etag);
|
||||
assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));
|
||||
|
@ -53,7 +53,7 @@
|
||||
//! * SSL support using OpenSSL or Rustls
|
||||
//! * Middlewares ([Logger, Session, CORS, etc](https://actix.rs/docs/middleware/))
|
||||
//! * Includes an async [HTTP client](https://docs.rs/awc/)
|
||||
//! * Runs on stable Rust 1.52+
|
||||
//! * Runs on stable Rust 1.54+
|
||||
//!
|
||||
//! # Crate Features
|
||||
//! * `cookies` - cookies support (enabled by default)
|
||||
|
@ -1,20 +1,13 @@
|
||||
//! For middleware documentation, see [`Compress`].
|
||||
|
||||
use std::{
|
||||
cmp,
|
||||
convert::TryFrom as _,
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_http::{
|
||||
body::{EitherBody, MessageBody},
|
||||
encoding::Encoder,
|
||||
header::{ContentEncoding, ACCEPT_ENCODING},
|
||||
StatusCode,
|
||||
};
|
||||
use actix_http::encoding::Encoder;
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_utils::future::{ok, Either, Ready};
|
||||
use futures_core::ready;
|
||||
@ -22,39 +15,65 @@ use once_cell::sync::Lazy;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{
|
||||
dev::BodyEncoding,
|
||||
body::{EitherBody, MessageBody},
|
||||
http::{
|
||||
header::{self, AcceptEncoding, Encoding, HeaderValue},
|
||||
StatusCode,
|
||||
},
|
||||
service::{ServiceRequest, ServiceResponse},
|
||||
Error, HttpResponse,
|
||||
Error, HttpMessage, HttpResponse,
|
||||
};
|
||||
|
||||
/// Middleware for compressing response payloads.
|
||||
///
|
||||
/// Use `BodyEncoding` trait for overriding response compression. To disable compression set
|
||||
/// encoding to `ContentEncoding::Identity`.
|
||||
/// # Encoding Negotiation
|
||||
/// `Compress` will read the `Accept-Encoding` header to negotiate which compression codec to use.
|
||||
/// Payloads are not compressed if the header is not sent. The `compress-*` [feature flags] are also
|
||||
/// considered in this selection process.
|
||||
///
|
||||
/// # Pre-compressed Payload
|
||||
/// If you are serving some data is already using a compressed representation (e.g., a gzip
|
||||
/// compressed HTML file from disk) you can signal this to `Compress` by setting an appropriate
|
||||
/// `Content-Encoding` header. In addition to preventing double compressing the payload, this header
|
||||
/// is required by the spec when using compressed representations and will inform the client that
|
||||
/// the content should be uncompressed.
|
||||
///
|
||||
/// However, it is not advised to unconditionally serve encoded representations of content because
|
||||
/// the client may not support it. The [`AcceptEncoding`] typed header has some utilities to help
|
||||
/// perform manual encoding negotiation, if required. When negotiating content encoding, it is also
|
||||
/// required by the spec to send a `Vary: Accept-Encoding` header.
|
||||
///
|
||||
/// A (naïve) example serving an pre-compressed Gzip file is included below.
|
||||
///
|
||||
/// # Examples
|
||||
/// To enable automatic payload compression just include `Compress` as a top-level middleware:
|
||||
/// ```
|
||||
/// use actix_web::{web, middleware, App, HttpResponse};
|
||||
/// use actix_web::{middleware, web, App, HttpResponse};
|
||||
///
|
||||
/// let app = App::new()
|
||||
/// .wrap(middleware::Compress::default())
|
||||
/// .default_service(web::to(|| HttpResponse::NotFound()));
|
||||
/// .default_service(web::to(|| HttpResponse::Ok().body("hello world")));
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Compress(ContentEncoding);
|
||||
|
||||
impl Compress {
|
||||
/// Create new `Compress` middleware with the specified encoding.
|
||||
pub fn new(encoding: ContentEncoding) -> Self {
|
||||
Compress(encoding)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Compress {
|
||||
fn default() -> Self {
|
||||
Compress::new(ContentEncoding::Auto)
|
||||
}
|
||||
}
|
||||
///
|
||||
/// Pre-compressed Gzip file being served from disk with correct headers added to bypass middleware:
|
||||
/// ```no_run
|
||||
/// use actix_web::{middleware, http::header, web, App, HttpResponse, Responder};
|
||||
///
|
||||
/// async fn index_handler() -> actix_web::Result<impl Responder> {
|
||||
/// Ok(actix_files::NamedFile::open_async("./assets/index.html.gz").await?
|
||||
/// .customize()
|
||||
/// .insert_header(header::ContentEncoding::Gzip))
|
||||
/// }
|
||||
///
|
||||
/// let app = App::new()
|
||||
/// .wrap(middleware::Compress::default())
|
||||
/// .default_service(web::to(index_handler));
|
||||
/// ```
|
||||
///
|
||||
/// [feature flags]: ../index.html#crate-features
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[non_exhaustive]
|
||||
pub struct Compress;
|
||||
|
||||
impl<S, B> Transform<S, ServiceRequest> for Compress
|
||||
where
|
||||
@ -68,44 +87,14 @@ where
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(CompressMiddleware {
|
||||
service,
|
||||
encoding: self.0,
|
||||
})
|
||||
ok(CompressMiddleware { service })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompressMiddleware<S> {
|
||||
service: S,
|
||||
encoding: ContentEncoding,
|
||||
}
|
||||
|
||||
static SUPPORTED_ALGORITHM_NAMES: Lazy<String> = Lazy::new(|| {
|
||||
#[allow(unused_mut)] // only unused when no compress features enabled
|
||||
let mut encoding: Vec<&str> = vec![];
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
{
|
||||
encoding.push("br");
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
{
|
||||
encoding.push("gzip");
|
||||
encoding.push("deflate");
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
encoding.push("zstd");
|
||||
|
||||
assert!(
|
||||
!encoding.is_empty(),
|
||||
"encoding can not be empty unless __compress feature has been explicitly enabled by itself"
|
||||
);
|
||||
|
||||
encoding.join(", ")
|
||||
});
|
||||
|
||||
impl<S, B> Service<ServiceRequest> for CompressMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
@ -121,39 +110,43 @@ where
|
||||
#[allow(clippy::borrow_interior_mutable_const)]
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
// negotiate content-encoding
|
||||
let encoding_result = req
|
||||
.headers()
|
||||
.get(&ACCEPT_ENCODING)
|
||||
.and_then(|val| val.to_str().ok())
|
||||
.map(|enc| AcceptEncoding::try_parse(enc, self.encoding));
|
||||
let accept_encoding = req.get_header::<AcceptEncoding>();
|
||||
|
||||
match encoding_result {
|
||||
// Missing header => fallback to identity
|
||||
None => Either::left(CompressResponse {
|
||||
encoding: ContentEncoding::Identity,
|
||||
fut: self.service.call(req),
|
||||
_phantom: PhantomData,
|
||||
}),
|
||||
let accept_encoding = match accept_encoding {
|
||||
// missing header; fallback to identity
|
||||
None => {
|
||||
return Either::left(CompressResponse {
|
||||
encoding: Encoding::identity(),
|
||||
fut: self.service.call(req),
|
||||
_phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
// Valid encoding
|
||||
Some(Ok(encoding)) => Either::left(CompressResponse {
|
||||
encoding,
|
||||
fut: self.service.call(req),
|
||||
_phantom: PhantomData,
|
||||
}),
|
||||
// valid accept-encoding header
|
||||
Some(accept_encoding) => accept_encoding,
|
||||
};
|
||||
|
||||
// There is an HTTP header but we cannot match what client as asked for
|
||||
Some(Err(_)) => {
|
||||
let res = HttpResponse::with_body(
|
||||
match accept_encoding.negotiate(SUPPORTED_ENCODINGS.iter()) {
|
||||
None => {
|
||||
let mut res = HttpResponse::with_body(
|
||||
StatusCode::NOT_ACCEPTABLE,
|
||||
SUPPORTED_ALGORITHM_NAMES.clone(),
|
||||
SUPPORTED_ENCODINGS_STRING.as_str(),
|
||||
);
|
||||
|
||||
res.headers_mut()
|
||||
.insert(header::VARY, HeaderValue::from_static("Accept-Encoding"));
|
||||
|
||||
Either::right(ok(req
|
||||
.into_response(res)
|
||||
.map_into_boxed_body()
|
||||
.map_into_right_body()))
|
||||
}
|
||||
|
||||
Some(encoding) => Either::left(CompressResponse {
|
||||
fut: self.service.call(req),
|
||||
encoding,
|
||||
_phantom: PhantomData,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,7 +158,7 @@ pin_project! {
|
||||
{
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
encoding: ContentEncoding,
|
||||
encoding: Encoding,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
}
|
||||
@ -182,10 +175,11 @@ where
|
||||
|
||||
match ready!(this.fut.poll(cx)) {
|
||||
Ok(resp) => {
|
||||
let enc = if let Some(enc) = resp.response().get_encoding() {
|
||||
enc
|
||||
} else {
|
||||
*this.encoding
|
||||
let enc = match this.encoding {
|
||||
Encoding::Known(enc) => *enc,
|
||||
Encoding::Unknown(enc) => {
|
||||
unimplemented!("encoding {} should not be here", enc);
|
||||
}
|
||||
};
|
||||
|
||||
Poll::Ready(Ok(resp.map_body(move |head, body| {
|
||||
@ -198,178 +192,117 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
struct AcceptEncoding {
|
||||
encoding: ContentEncoding,
|
||||
// TODO: use Quality or QualityItem<ContentEncoding>
|
||||
quality: f64,
|
||||
}
|
||||
static SUPPORTED_ENCODINGS_STRING: Lazy<String> = Lazy::new(|| {
|
||||
#[allow(unused_mut)] // only unused when no compress features enabled
|
||||
let mut encoding: Vec<&str> = vec![];
|
||||
|
||||
impl Eq for AcceptEncoding {}
|
||||
|
||||
impl Ord for AcceptEncoding {
|
||||
#[allow(clippy::comparison_chain)]
|
||||
fn cmp(&self, other: &AcceptEncoding) -> cmp::Ordering {
|
||||
if self.quality > other.quality {
|
||||
cmp::Ordering::Less
|
||||
} else if self.quality < other.quality {
|
||||
cmp::Ordering::Greater
|
||||
} else {
|
||||
cmp::Ordering::Equal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for AcceptEncoding {
|
||||
fn partial_cmp(&self, other: &AcceptEncoding) -> Option<cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for AcceptEncoding {
|
||||
fn eq(&self, other: &AcceptEncoding) -> bool {
|
||||
self.encoding == other.encoding && self.quality == other.quality
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse q-factor from quality strings.
|
||||
///
|
||||
/// If parse fail, then fallback to default value which is 1.
|
||||
/// More details available here: <https://developer.mozilla.org/en-US/docs/Glossary/Quality_values>
|
||||
fn parse_quality(parts: &[&str]) -> f64 {
|
||||
for part in parts {
|
||||
if part.trim().starts_with("q=") {
|
||||
return part[2..].parse().unwrap_or(1.0);
|
||||
}
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
{
|
||||
encoding.push("br");
|
||||
}
|
||||
|
||||
1.0
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum AcceptEncodingError {
|
||||
/// This error occurs when client only support compressed response and server do not have any
|
||||
/// algorithm that match client accepted algorithms.
|
||||
CompressionAlgorithmMismatch,
|
||||
}
|
||||
|
||||
impl AcceptEncoding {
|
||||
fn new(tag: &str) -> Option<AcceptEncoding> {
|
||||
let parts: Vec<&str> = tag.split(';').collect();
|
||||
let encoding = match parts.len() {
|
||||
0 => return None,
|
||||
_ => match ContentEncoding::try_from(parts[0]) {
|
||||
Err(_) => return None,
|
||||
Ok(x) => x,
|
||||
},
|
||||
};
|
||||
|
||||
let quality = parse_quality(&parts[1..]);
|
||||
if quality <= 0.0 || quality > 1.0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(AcceptEncoding { encoding, quality })
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
{
|
||||
encoding.push("gzip");
|
||||
encoding.push("deflate");
|
||||
}
|
||||
|
||||
/// Parse a raw Accept-Encoding header value into an ordered list then return the best match
|
||||
/// based on middleware configuration.
|
||||
pub fn try_parse(
|
||||
raw: &str,
|
||||
encoding: ContentEncoding,
|
||||
) -> Result<ContentEncoding, AcceptEncodingError> {
|
||||
let mut encodings = raw
|
||||
.replace(' ', "")
|
||||
.split(',')
|
||||
.filter_map(AcceptEncoding::new)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
encodings.sort();
|
||||
|
||||
for enc in encodings {
|
||||
if encoding == ContentEncoding::Auto || encoding == enc.encoding {
|
||||
return Ok(enc.encoding);
|
||||
}
|
||||
}
|
||||
|
||||
// Special case if user cannot accept uncompressed data.
|
||||
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
|
||||
// TODO: account for whitespace
|
||||
if raw.contains("*;q=0") || raw.contains("identity;q=0") {
|
||||
return Err(AcceptEncodingError::CompressionAlgorithmMismatch);
|
||||
}
|
||||
|
||||
Ok(ContentEncoding::Identity)
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
{
|
||||
encoding.push("zstd");
|
||||
}
|
||||
}
|
||||
|
||||
assert!(
|
||||
!encoding.is_empty(),
|
||||
"encoding can not be empty unless __compress feature has been explicitly enabled by itself"
|
||||
);
|
||||
|
||||
encoding.join(", ")
|
||||
});
|
||||
|
||||
static SUPPORTED_ENCODINGS: Lazy<Vec<Encoding>> = Lazy::new(|| {
|
||||
let mut encodings = vec![Encoding::identity()];
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
{
|
||||
encodings.push(Encoding::brotli());
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
{
|
||||
encodings.push(Encoding::gzip());
|
||||
encodings.push(Encoding::deflate());
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
{
|
||||
encodings.push(Encoding::zstd());
|
||||
}
|
||||
|
||||
assert!(
|
||||
!encodings.is_empty(),
|
||||
"encodings can not be empty unless __compress feature has been explicitly enabled by itself"
|
||||
);
|
||||
|
||||
encodings
|
||||
});
|
||||
|
||||
// move cfg(feature) to prevents_double_compressing if more tests are added
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{middleware::DefaultHeaders, test, web, App};
|
||||
|
||||
macro_rules! assert_parse_eq {
|
||||
($raw:expr, $result:expr) => {
|
||||
assert_eq!(
|
||||
AcceptEncoding::try_parse($raw, ContentEncoding::Auto),
|
||||
Ok($result)
|
||||
);
|
||||
};
|
||||
pub fn gzip_decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
use std::io::Read as _;
|
||||
let mut decoder = flate2::read::GzDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
|
||||
macro_rules! assert_parse_fail {
|
||||
($raw:expr) => {
|
||||
assert!(AcceptEncoding::try_parse($raw, ContentEncoding::Auto).is_err());
|
||||
};
|
||||
}
|
||||
#[actix_rt::test]
|
||||
async fn prevents_double_compressing() {
|
||||
const D: &str = "hello world ";
|
||||
const DATA: &str = const_str::repeat!(D, 100);
|
||||
|
||||
#[test]
|
||||
fn test_parse_encoding() {
|
||||
// Test simple case
|
||||
assert_parse_eq!("br", ContentEncoding::Br);
|
||||
assert_parse_eq!("gzip", ContentEncoding::Gzip);
|
||||
assert_parse_eq!("deflate", ContentEncoding::Deflate);
|
||||
assert_parse_eq!("zstd", ContentEncoding::Zstd);
|
||||
let app = test::init_service({
|
||||
App::new()
|
||||
.wrap(Compress::default())
|
||||
.route(
|
||||
"/single",
|
||||
web::get().to(move || HttpResponse::Ok().body(DATA)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/double")
|
||||
.wrap(Compress::default())
|
||||
.wrap(DefaultHeaders::new().add(("x-double", "true")))
|
||||
.route(web::get().to(move || HttpResponse::Ok().body(DATA))),
|
||||
)
|
||||
})
|
||||
.await;
|
||||
|
||||
// Test space, trim, missing values
|
||||
assert_parse_eq!("br,,,,", ContentEncoding::Br);
|
||||
assert_parse_eq!("gzip , br, zstd", ContentEncoding::Gzip);
|
||||
let req = test::TestRequest::default()
|
||||
.uri("/single")
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.to_request();
|
||||
let res = test::call_service(&app, req).await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get("x-double"), None);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "gzip");
|
||||
let bytes = test::read_body(res).await;
|
||||
assert_eq!(gzip_decode(bytes), DATA.as_bytes());
|
||||
|
||||
// Test float number parsing
|
||||
assert_parse_eq!("br;q=1 ,", ContentEncoding::Br);
|
||||
assert_parse_eq!("br;q=1.0 , br", ContentEncoding::Br);
|
||||
|
||||
// Test wildcard
|
||||
assert_parse_eq!("*", ContentEncoding::Identity);
|
||||
assert_parse_eq!("*;q=1.0", ContentEncoding::Identity);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_encoding_qfactor_ordering() {
|
||||
assert_parse_eq!("gzip, br, zstd", ContentEncoding::Gzip);
|
||||
assert_parse_eq!("zstd, br, gzip", ContentEncoding::Zstd);
|
||||
|
||||
assert_parse_eq!("gzip;q=0.4, br;q=0.6", ContentEncoding::Br);
|
||||
assert_parse_eq!("gzip;q=0.8, br;q=0.4", ContentEncoding::Gzip);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_encoding_qfactor_invalid() {
|
||||
// Out of range
|
||||
assert_parse_eq!("gzip;q=-5.0", ContentEncoding::Identity);
|
||||
assert_parse_eq!("gzip;q=5.0", ContentEncoding::Identity);
|
||||
|
||||
// Disabled
|
||||
assert_parse_eq!("gzip;q=0", ContentEncoding::Identity);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_compression_required() {
|
||||
// Check we fallback to identity if there is an unsupported compression algorithm
|
||||
assert_parse_eq!("compress", ContentEncoding::Identity);
|
||||
|
||||
// User do not want any compression
|
||||
assert_parse_fail!("compress, identity;q=0");
|
||||
assert_parse_fail!("compress, identity;q=0.0");
|
||||
assert_parse_fail!("compress, *;q=0");
|
||||
assert_parse_fail!("compress, *;q=0.0");
|
||||
let req = test::TestRequest::default()
|
||||
.uri("/double")
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.to_request();
|
||||
let res = test::call_service(&app, req).await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get("x-double").unwrap(), "true");
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "gzip");
|
||||
let bytes = test::read_body(res).await;
|
||||
assert_eq!(gzip_decode(bytes), DATA.as_bytes());
|
||||
}
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ impl HttpRequest {
|
||||
|
||||
/// Returns a reference to the URL parameters container.
|
||||
///
|
||||
/// A url parameter is specified in the form `{identifier}`, where the identifier can be used
|
||||
/// A URL parameter is specified in the form `{identifier}`, where the identifier can be used
|
||||
/// later in a request handler to access the matched value for that parameter.
|
||||
///
|
||||
/// # Percent Encoding and URL Parameters
|
||||
@ -266,14 +266,34 @@ impl HttpRequest {
|
||||
self.app_state().config()
|
||||
}
|
||||
|
||||
/// Get an application data object stored with `App::data` or `App::app_data`
|
||||
/// methods during application configuration.
|
||||
/// Retrieves a piece of application state.
|
||||
///
|
||||
/// If `App::data` was used to store object, use `Data<T>`:
|
||||
/// Extracts any object stored with [`App::app_data()`](crate::App::app_data) (or the
|
||||
/// counterpart methods on [`Scope`](crate::Scope::app_data) and
|
||||
/// [`Resource`](crate::Resource::app_data)) during application configuration.
|
||||
///
|
||||
/// ```ignore
|
||||
/// let opt_t = req.app_data::<Data<T>>();
|
||||
/// Since the Actix Web router layers application data, the returned object will reference the
|
||||
/// "closest" instance of the type. For example, if an `App` stores a `u32`, a nested `Scope`
|
||||
/// also stores a `u32`, and the delegated request handler falls within that `Scope`, then
|
||||
/// calling `.app_data::<u32>()` on an `HttpRequest` within that handler will return the
|
||||
/// `Scope`'s instance. However, using the same router set up and a request that does not get
|
||||
/// captured by the `Scope`, `.app_data::<u32>()` would return the `App`'s instance.
|
||||
///
|
||||
/// If the state was stored using the [`Data`] wrapper, then it must also be retrieved using
|
||||
/// this same type.
|
||||
///
|
||||
/// See also the [`Data`] extractor.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```no_run
|
||||
/// # use actix_web::{test::TestRequest, web::Data};
|
||||
/// # let req = TestRequest::default().to_http_request();
|
||||
/// # type T = u32;
|
||||
/// let opt_t: Option<&Data<T>> = req.app_data::<Data<T>>();
|
||||
/// ```
|
||||
///
|
||||
/// [`Data`]: crate::web::Data
|
||||
#[doc(alias = "state")]
|
||||
pub fn app_data<T: 'static>(&self) -> Option<&T> {
|
||||
for container in self.inner.app_data.iter().rev() {
|
||||
if let Some(data) = container.get::<T>() {
|
||||
|
@ -195,6 +195,7 @@ where
|
||||
/// .route(web::get().to(handler))
|
||||
/// );
|
||||
/// ```
|
||||
#[doc(alias = "manage")]
|
||||
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||
self.app_data
|
||||
.get_or_insert_with(Extensions::new)
|
||||
|
@ -154,6 +154,7 @@ where
|
||||
/// .route("/", web::get().to(handler))
|
||||
/// );
|
||||
/// ```
|
||||
#[doc(alias = "manage")]
|
||||
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||
self.app_data
|
||||
.get_or_insert_with(Extensions::new)
|
||||
|
@ -11,7 +11,7 @@ use std::{
|
||||
};
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures_core::{ready, stream::Stream as _};
|
||||
use futures_core::{ready, Stream as _};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
use actix_http::Payload;
|
||||
@ -515,7 +515,7 @@ mod tests {
|
||||
.to_http_parts();
|
||||
|
||||
let s = Json::<MyObject>::from_request(&req, &mut pl).await;
|
||||
let resp = HttpResponse::from_error(s.err().unwrap());
|
||||
let resp = HttpResponse::from_error(s.unwrap_err());
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
|
||||
let body = body::to_bytes(resp.into_body()).await.unwrap();
|
||||
|
@ -1,19 +1,18 @@
|
||||
//! Common extractors and responders.
|
||||
|
||||
// TODO: review visibility
|
||||
mod either;
|
||||
pub(crate) mod form;
|
||||
mod form;
|
||||
mod header;
|
||||
pub(crate) mod json;
|
||||
mod json;
|
||||
mod path;
|
||||
pub(crate) mod payload;
|
||||
mod payload;
|
||||
mod query;
|
||||
pub(crate) mod readlines;
|
||||
mod readlines;
|
||||
|
||||
pub use self::either::{Either, EitherExtractError};
|
||||
pub use self::form::{Form, FormConfig};
|
||||
pub use self::either::Either;
|
||||
pub use self::form::{Form, FormConfig, UrlEncoded};
|
||||
pub use self::header::Header;
|
||||
pub use self::json::{Json, JsonConfig};
|
||||
pub use self::json::{Json, JsonBody, JsonConfig};
|
||||
pub use self::path::{Path, PathConfig};
|
||||
pub use self::payload::{Payload, PayloadConfig};
|
||||
pub use self::query::{Query, QueryConfig};
|
||||
|
@ -9,6 +9,7 @@ use serde::de;
|
||||
use crate::{
|
||||
dev::Payload,
|
||||
error::{Error, ErrorNotFound, PathError},
|
||||
web::Data,
|
||||
FromRequest, HttpRequest,
|
||||
};
|
||||
|
||||
@ -102,6 +103,7 @@ where
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
let error_handler = req
|
||||
.app_data::<PathConfig>()
|
||||
.or_else(|| req.app_data::<Data<PathConfig>>().map(Data::get_ref))
|
||||
.and_then(|c| c.err_handler.clone());
|
||||
|
||||
ready(
|
||||
@ -113,6 +115,7 @@ where
|
||||
Request path: {:?}",
|
||||
req.path()
|
||||
);
|
||||
|
||||
if let Some(error_handler) = error_handler {
|
||||
let e = PathError::Deserialize(err);
|
||||
(error_handler)(e, req)
|
||||
@ -135,6 +138,7 @@ where
|
||||
/// enum Folder {
|
||||
/// #[serde(rename = "inbox")]
|
||||
/// Inbox,
|
||||
///
|
||||
/// #[serde(rename = "outbox")]
|
||||
/// Outbox,
|
||||
/// }
|
||||
@ -144,19 +148,17 @@ where
|
||||
/// format!("Selected folder: {:?}!", folder)
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/messages/{folder}")
|
||||
/// .app_data(PathConfig::default().error_handler(|err, req| {
|
||||
/// error::InternalError::from_response(
|
||||
/// err,
|
||||
/// HttpResponse::Conflict().into(),
|
||||
/// )
|
||||
/// .into()
|
||||
/// }))
|
||||
/// .route(web::post().to(index)),
|
||||
/// );
|
||||
/// }
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/messages/{folder}")
|
||||
/// .app_data(PathConfig::default().error_handler(|err, req| {
|
||||
/// error::InternalError::from_response(
|
||||
/// err,
|
||||
/// HttpResponse::Conflict().into(),
|
||||
/// )
|
||||
/// .into()
|
||||
/// }))
|
||||
/// .route(web::post().to(index)),
|
||||
/// );
|
||||
/// ```
|
||||
#[derive(Clone, Default)]
|
||||
pub struct PathConfig {
|
||||
@ -164,7 +166,7 @@ pub struct PathConfig {
|
||||
}
|
||||
|
||||
impl PathConfig {
|
||||
/// Set custom error handler
|
||||
/// Set custom error handler.
|
||||
pub fn error_handler<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: Fn(PathError, &HttpRequest) -> Error + Send + Sync + 'static,
|
||||
@ -283,6 +285,18 @@ mod tests {
|
||||
assert_eq!(res[1], "32".to_owned());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn paths_decoded() {
|
||||
let resource = ResourceDef::new("/{key}/{value}");
|
||||
let mut req = TestRequest::with_uri("/na%2Bme/us%2Fer%251").to_srv_request();
|
||||
resource.capture_match_info(req.match_info_mut());
|
||||
|
||||
let (req, mut pl) = req.into_parts();
|
||||
let path_items = Path::<MyStruct>::from_request(&req, &mut pl).await.unwrap();
|
||||
assert_eq!(path_items.key, "na+me");
|
||||
assert_eq!(path_items.value, "us/er%1");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_custom_err_handler() {
|
||||
let (req, mut pl) = TestRequest::with_uri("/name/user1/")
|
||||
|
@ -248,6 +248,7 @@ impl PayloadConfig {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
37
src/web.rs
37
src/web.rs
@ -2,13 +2,12 @@
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
use actix_http::Method;
|
||||
use actix_router::IntoPatterns;
|
||||
pub use bytes::{Buf, BufMut, Bytes, BytesMut};
|
||||
|
||||
use crate::{
|
||||
error::BlockingError, extract::FromRequest, handler::Handler, resource::Resource,
|
||||
route::Route, scope::Scope, service::WebService, Responder,
|
||||
error::BlockingError, http::Method, service::WebService, FromRequest, Handler, Resource,
|
||||
Responder, Route, Scope,
|
||||
};
|
||||
|
||||
pub use crate::config::ServiceConfig;
|
||||
@ -86,23 +85,21 @@ pub fn route() -> Route {
|
||||
|
||||
macro_rules! method_route {
|
||||
($method_fn:ident, $method_const:ident) => {
|
||||
paste::paste! {
|
||||
#[doc = " Creates a new route with `" $method_const "` method guard."]
|
||||
///
|
||||
/// # Examples
|
||||
#[doc = " In this example, one `" $method_const " /{project_id}` route is set up:"]
|
||||
/// ```
|
||||
/// use actix_web::{web, App, HttpResponse};
|
||||
///
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/{project_id}")
|
||||
#[doc = " .route(web::" $method_fn "().to(|| HttpResponse::Ok()))"]
|
||||
///
|
||||
/// );
|
||||
/// ```
|
||||
pub fn $method_fn() -> Route {
|
||||
method(Method::$method_const)
|
||||
}
|
||||
#[doc = concat!(" Creates a new route with `", stringify!($method_const), "` method guard.")]
|
||||
///
|
||||
/// # Examples
|
||||
#[doc = concat!(" In this example, one `", stringify!($method_const), " /{project_id}` route is set up:")]
|
||||
/// ```
|
||||
/// use actix_web::{web, App, HttpResponse};
|
||||
///
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/{project_id}")
|
||||
#[doc = concat!(" .route(web::", stringify!($method_fn), "().to(|| HttpResponse::Ok()))")]
|
||||
///
|
||||
/// );
|
||||
/// ```
|
||||
pub fn $method_fn() -> Route {
|
||||
method(Method::$method_const)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
307
tests/compression.rs
Normal file
307
tests/compression.rs
Normal file
@ -0,0 +1,307 @@
|
||||
use actix_http::ContentEncoding;
|
||||
use actix_web::{
|
||||
http::{header, StatusCode},
|
||||
middleware::Compress,
|
||||
web, App, HttpResponse,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
|
||||
mod utils;
|
||||
|
||||
static LOREM: &[u8] = include_bytes!("fixtures/lorem.txt");
|
||||
static LOREM_GZIP: &[u8] = include_bytes!("fixtures/lorem.txt.gz");
|
||||
static LOREM_BR: &[u8] = include_bytes!("fixtures/lorem.txt.br");
|
||||
static LOREM_ZSTD: &[u8] = include_bytes!("fixtures/lorem.txt.zst");
|
||||
static LOREM_XZ: &[u8] = include_bytes!("fixtures/lorem.txt.xz");
|
||||
|
||||
macro_rules! test_server {
|
||||
() => {
|
||||
actix_test::start(|| {
|
||||
App::new()
|
||||
.wrap(Compress::default())
|
||||
.route("/static", web::to(|| HttpResponse::Ok().body(LOREM)))
|
||||
.route(
|
||||
"/static-gzip",
|
||||
web::to(|| {
|
||||
HttpResponse::Ok()
|
||||
// signal to compressor that content should not be altered
|
||||
// signal to client that content is encoded
|
||||
.insert_header(ContentEncoding::Gzip)
|
||||
.body(LOREM_GZIP)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/static-br",
|
||||
web::to(|| {
|
||||
HttpResponse::Ok()
|
||||
// signal to compressor that content should not be altered
|
||||
// signal to client that content is encoded
|
||||
.insert_header(ContentEncoding::Brotli)
|
||||
.body(LOREM_BR)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/static-zstd",
|
||||
web::to(|| {
|
||||
HttpResponse::Ok()
|
||||
// signal to compressor that content should not be altered
|
||||
// signal to client that content is encoded
|
||||
.insert_header(ContentEncoding::Zstd)
|
||||
.body(LOREM_ZSTD)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/static-xz",
|
||||
web::to(|| {
|
||||
HttpResponse::Ok()
|
||||
// signal to compressor that content should not be altered
|
||||
// signal to client that content is encoded as 7zip
|
||||
.insert_header((header::CONTENT_ENCODING, "xz"))
|
||||
.body(LOREM_XZ)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/echo",
|
||||
web::to(|body: Bytes| HttpResponse::Ok().body(body)),
|
||||
)
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn negotiate_encoding_identity() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static")
|
||||
.insert_header((header::ACCEPT_ENCODING, "identity"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None);
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn negotiate_encoding_gzip() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static")
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "gzip");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||
|
||||
let mut res = srv
|
||||
.post("/static")
|
||||
.no_decompress()
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::gzip::decode(bytes), LOREM);
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn negotiate_encoding_br() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static")
|
||||
.insert_header((header::ACCEPT_ENCODING, "br,zstd,gzip"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||
|
||||
let mut res = srv
|
||||
.post("/static")
|
||||
.no_decompress()
|
||||
.insert_header((header::ACCEPT_ENCODING, "br,zstd,gzip"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::brotli::decode(bytes), LOREM);
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn negotiate_encoding_zstd() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static")
|
||||
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip,br"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "zstd");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||
|
||||
let mut res = srv
|
||||
.post("/static")
|
||||
.no_decompress()
|
||||
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip,br"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::zstd::decode(bytes), LOREM);
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "compress-brotli",
|
||||
feature = "compress-gzip",
|
||||
feature = "compress-zstd",
|
||||
))]
|
||||
#[actix_rt::test]
|
||||
async fn client_encoding_prefers_brotli() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv.post("/static").send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn gzip_no_decompress() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static-gzip")
|
||||
// don't decompress response body
|
||||
.no_decompress()
|
||||
// signal that we want a compressed body
|
||||
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "gzip");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM_GZIP));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn manual_custom_coding() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static-xz")
|
||||
// don't decompress response body
|
||||
.no_decompress()
|
||||
// signal that we want a compressed body
|
||||
.insert_header((header::ACCEPT_ENCODING, "xz"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "xz");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM_XZ));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn deny_identity_coding() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static")
|
||||
// signal that we want a compressed body
|
||||
.insert_header((header::ACCEPT_ENCODING, "br, identity;q=0"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn deny_identity_coding_no_decompress() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static-br")
|
||||
// don't decompress response body
|
||||
.no_decompress()
|
||||
// signal that we want a compressed body
|
||||
.insert_header((header::ACCEPT_ENCODING, "br, identity;q=0"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM_BR));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
// TODO: fix test
|
||||
// currently fails because negotiation doesn't consider unknown encoding types
|
||||
#[ignore]
|
||||
#[actix_rt::test]
|
||||
async fn deny_identity_for_manual_coding() {
|
||||
let srv = test_server!();
|
||||
|
||||
let req = srv
|
||||
.post("/static-xz")
|
||||
// don't decompress response body
|
||||
.no_decompress()
|
||||
// signal that we want a compressed body
|
||||
.insert_header((header::ACCEPT_ENCODING, "xz, identity;q=0"))
|
||||
.send();
|
||||
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "xz");
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(LOREM_XZ));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
5
tests/fixtures/lorem.txt
vendored
Normal file
5
tests/fixtures/lorem.txt
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin interdum tincidunt lacus, sed tempor lorem consectetur et. Pellentesque et egestas sem, at cursus massa. Nunc feugiat elit sit amet ipsum commodo luctus. Proin auctor dignissim pharetra. Integer iaculis quam a tellus auctor, vitae auctor nisl viverra. Nullam consequat maximus porttitor. Pellentesque tortor enim, molestie at varius non, tempor non nibh. Suspendisse tempus erat lorem, vel faucibus magna blandit vel. Sed pellentesque ligula augue, vitae fermentum eros blandit et. Cras dignissim in massa ut varius. Vestibulum commodo nunc sit amet pellentesque dignissim.
|
||||
|
||||
Donec imperdiet blandit lobortis. Suspendisse fringilla nunc quis venenatis tempor. Nunc tempor sed erat sed convallis. Pellentesque aliquet elit lectus, quis vulputate arcu pharetra sed. Etiam laoreet aliquet arcu cursus vehicula. Maecenas odio odio, elementum faucibus sollicitudin vitae, pellentesque ac purus. Donec venenatis faucibus lorem, et finibus lacus tincidunt vitae. Quisque laoreet metus sapien, vitae euismod mauris lobortis malesuada. Integer sit amet elementum turpis. Maecenas ex mauris, dapibus eu placerat vitae, rutrum convallis enim. Nulla vitae orci ultricies, sagittis turpis et, lacinia dui. Praesent egestas urna turpis, sit amet feugiat mauris tristique eu. Quisque id tempor libero. Donec ullamcorper dapibus lorem, vel consequat risus congue a.
|
||||
|
||||
Nullam dignissim ut lectus vitae tempor. Pellentesque ut odio fringilla, volutpat mi et, vulputate tellus. Fusce eget diam non odio tincidunt viverra eu vel augue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Nullam sed eleifend purus, vitae aliquam orci. Cras fringilla justo eget tempus bibendum. Phasellus venenatis, odio nec pulvinar commodo, quam neque lacinia turpis, ut rutrum tortor massa eu nulla. Vivamus tincidunt ut lectus a gravida. Donec varius mi quis enim interdum ultrices. Sed aliquam consectetur nisi vitae viverra. Praesent nec ligula egestas, porta lectus sed, consectetur augue.
|
BIN
tests/fixtures/lorem.txt.br
vendored
Normal file
BIN
tests/fixtures/lorem.txt.br
vendored
Normal file
Binary file not shown.
BIN
tests/fixtures/lorem.txt.gz
vendored
Normal file
BIN
tests/fixtures/lorem.txt.gz
vendored
Normal file
Binary file not shown.
BIN
tests/fixtures/lorem.txt.xz
vendored
Normal file
BIN
tests/fixtures/lorem.txt.xz
vendored
Normal file
Binary file not shown.
BIN
tests/fixtures/lorem.txt.zst
vendored
Normal file
BIN
tests/fixtures/lorem.txt.zst
vendored
Normal file
Binary file not shown.
@ -11,52 +11,26 @@ use std::{
|
||||
};
|
||||
|
||||
use actix_web::{
|
||||
dev::BodyEncoding,
|
||||
http::header::{
|
||||
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING,
|
||||
},
|
||||
cookie::{Cookie, CookieBuilder},
|
||||
http::{header, StatusCode},
|
||||
middleware::{Compress, NormalizePath, TrailingSlash},
|
||||
web, App, Error, HttpResponse,
|
||||
};
|
||||
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
||||
use bytes::Bytes;
|
||||
use cookie::{Cookie, CookieBuilder};
|
||||
use flate2::{
|
||||
read::GzDecoder,
|
||||
write::{GzEncoder, ZlibDecoder, ZlibEncoder},
|
||||
Compression,
|
||||
};
|
||||
use futures_core::ready;
|
||||
use rand::{distributions::Alphanumeric, Rng as _};
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
use openssl::{
|
||||
pkey::PKey,
|
||||
ssl::{SslAcceptor, SslMethod},
|
||||
x509::X509,
|
||||
};
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
use zstd::stream::{read::Decoder as ZstdDecoder, write::Encoder as ZstdEncoder};
|
||||
|
||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World";
|
||||
mod utils;
|
||||
|
||||
const S: &str = "Hello World ";
|
||||
const STR: &str = const_str::repeat!(S, 100);
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
fn openssl_config() -> SslAcceptor {
|
||||
@ -122,165 +96,86 @@ async fn test_body() {
|
||||
App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
let mut response = srv.get("/").send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
let mut res = srv.get("/").send().await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_gzip() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
// enforcing an encoding per-response is removed
|
||||
// #[actix_rt::test]
|
||||
// async fn test_body_encoding_override() {
|
||||
// let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
// App::new()
|
||||
// .wrap(Compress::default())
|
||||
// .service(web::resource("/").route(web::to(|| {
|
||||
// HttpResponse::Ok()
|
||||
// .encode_with(ContentEncoding::Deflate)
|
||||
// .body(STR)
|
||||
// })))
|
||||
// .service(web::resource("/raw").route(web::to(|| {
|
||||
// let mut res = HttpResponse::with_body(actix_web::http::StatusCode::OK, STR);
|
||||
// res.encode_with(ContentEncoding::Deflate);
|
||||
// res.map_into_boxed_body()
|
||||
// })))
|
||||
// });
|
||||
|
||||
let mut response = srv
|
||||
.get("/")
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "gzip"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
// // Builder
|
||||
// let mut res = srv
|
||||
// .get("/")
|
||||
// .no_decompress()
|
||||
// .append_header((ACCEPT_ENCODING, "deflate"))
|
||||
// .send()
|
||||
// .await
|
||||
// .unwrap();
|
||||
// assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
// let bytes = res.body().await.unwrap();
|
||||
// assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
|
||||
|
||||
// decode
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
// // Raw Response
|
||||
// let mut res = srv
|
||||
// .request(actix_web::http::Method::GET, srv.url("/raw"))
|
||||
// .no_decompress()
|
||||
// .append_header((ACCEPT_ENCODING, "deflate"))
|
||||
// .send()
|
||||
// .await
|
||||
// .unwrap();
|
||||
// assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
// let bytes = res.body().await.unwrap();
|
||||
// assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
|
||||
|
||||
// srv.stop().await;
|
||||
// }
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_gzip2() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
let mut response = srv
|
||||
.get("/")
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "gzip"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_encoding_override() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::to(|| {
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Deflate)
|
||||
.body(STR)
|
||||
})))
|
||||
.service(web::resource("/raw").route(web::to(|| {
|
||||
let mut response =
|
||||
HttpResponse::with_body(actix_web::http::StatusCode::OK, STR);
|
||||
response.encoding(ContentEncoding::Deflate);
|
||||
response.map_into_boxed_body()
|
||||
})))
|
||||
});
|
||||
|
||||
// Builder
|
||||
let mut response = srv
|
||||
.get("/")
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "deflate"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = ZlibDecoder::new(Vec::new());
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
|
||||
// Raw Response
|
||||
let mut response = srv
|
||||
.request(actix_web::http::Method::GET, srv.url("/raw"))
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "deflate"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = ZlibDecoder::new(Vec::new());
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_gzip_large() {
|
||||
async fn body_gzip_large() {
|
||||
let data = STR.repeat(10);
|
||||
let srv_data = data.clone();
|
||||
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), move || {
|
||||
let data = srv_data.clone();
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(
|
||||
web::resource("/")
|
||||
.route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
||||
)
|
||||
|
||||
App::new().wrap(Compress::default()).service(
|
||||
web::resource("/").route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
||||
)
|
||||
});
|
||||
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "gzip"))
|
||||
.append_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from(data));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::gzip::decode(bytes), data.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -296,32 +191,22 @@ async fn test_body_gzip_large_random() {
|
||||
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), move || {
|
||||
let data = srv_data.clone();
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(
|
||||
web::resource("/")
|
||||
.route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
||||
)
|
||||
App::new().wrap(Compress::default()).service(
|
||||
web::resource("/").route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
||||
)
|
||||
});
|
||||
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "gzip"))
|
||||
.append_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(dec.len(), data.len());
|
||||
assert_eq!(Bytes::from(dec), Bytes::from(data));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::gzip::decode(bytes), data.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -330,34 +215,28 @@ async fn test_body_gzip_large_random() {
|
||||
async fn test_body_chunked_implicit() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.wrap(Compress::default())
|
||||
.service(web::resource("/").route(web::get().to(move || {
|
||||
HttpResponse::Ok()
|
||||
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
||||
})))
|
||||
});
|
||||
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.no_decompress()
|
||||
.append_header((ACCEPT_ENCODING, "gzip"))
|
||||
.append_header((header::ACCEPT_ENCODING, "gzip"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
response.headers().get(TRANSFER_ENCODING).unwrap(),
|
||||
&b"chunked"[..]
|
||||
res.headers().get(header::TRANSFER_ENCODING).unwrap(),
|
||||
"chunked"
|
||||
);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = GzDecoder::new(&bytes[..]);
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::gzip::decode(bytes), STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -366,32 +245,24 @@ async fn test_body_chunked_implicit() {
|
||||
async fn test_body_br_streaming() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Br))
|
||||
.wrap(Compress::default())
|
||||
.service(web::resource("/").route(web::to(move || {
|
||||
HttpResponse::Ok()
|
||||
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
||||
})))
|
||||
});
|
||||
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.append_header((ACCEPT_ENCODING, "br"))
|
||||
.append_header((header::ACCEPT_ENCODING, "br"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
println!("TEST: {:?}", bytes.len());
|
||||
|
||||
// decode br
|
||||
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
println!("T: {:?}", Bytes::copy_from_slice(&dec));
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::brotli::decode(bytes), STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -404,16 +275,13 @@ async fn test_head_binary() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut response = srv.head("/").send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
let mut res = srv.head("/").send().await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
{
|
||||
let len = response.headers().get(CONTENT_LENGTH).unwrap();
|
||||
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
||||
}
|
||||
let len = res.headers().get(header::CONTENT_LENGTH).unwrap();
|
||||
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert!(bytes.is_empty());
|
||||
|
||||
srv.stop().await;
|
||||
@ -429,12 +297,11 @@ async fn test_no_chunking() {
|
||||
})))
|
||||
});
|
||||
|
||||
let mut response = srv.get("/").send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert!(!response.headers().contains_key(TRANSFER_ENCODING));
|
||||
let mut res = srv.get("/").send().await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert!(!res.headers().contains_key(header::TRANSFER_ENCODING));
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
@ -444,27 +311,21 @@ async fn test_no_chunking() {
|
||||
async fn test_body_deflate() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Deflate))
|
||||
.wrap(Compress::default())
|
||||
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.append_header((ACCEPT_ENCODING, "deflate"))
|
||||
.append_header((header::ACCEPT_ENCODING, "deflate"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
let mut e = ZlibDecoder::new(Vec::new());
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -473,28 +334,21 @@ async fn test_body_deflate() {
|
||||
async fn test_body_brotli() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Br))
|
||||
.wrap(Compress::default())
|
||||
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.append_header((ACCEPT_ENCODING, "br"))
|
||||
.append_header((header::ACCEPT_ENCODING, "br"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode brotli
|
||||
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
||||
e.write_all(bytes.as_ref()).unwrap();
|
||||
let dec = e.finish().unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::brotli::decode(bytes), STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -503,28 +357,21 @@ async fn test_body_brotli() {
|
||||
async fn test_body_zstd() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Zstd))
|
||||
.wrap(Compress::default())
|
||||
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.append_header((ACCEPT_ENCODING, "zstd"))
|
||||
.append_header((header::ACCEPT_ENCODING, "zstd"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = ZstdDecoder::new(&bytes[..]).unwrap();
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::zstd::decode(bytes), STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -533,31 +380,24 @@ async fn test_body_zstd() {
|
||||
async fn test_body_zstd_streaming() {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Zstd))
|
||||
.wrap(Compress::default())
|
||||
.service(web::resource("/").route(web::to(move || {
|
||||
HttpResponse::Ok()
|
||||
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
||||
})))
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.append_header((ACCEPT_ENCODING, "zstd"))
|
||||
.append_header((header::ACCEPT_ENCODING, "zstd"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
|
||||
// decode
|
||||
let mut e = ZstdDecoder::new(&bytes[..]).unwrap();
|
||||
let mut dec = Vec::new();
|
||||
e.read_to_end(&mut dec).unwrap();
|
||||
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(utils::zstd::decode(bytes), STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -570,20 +410,14 @@ async fn test_zstd_encoding() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = ZstdEncoder::new(Vec::new(), 5).unwrap();
|
||||
e.write_all(STR.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "zstd"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "zstd"))
|
||||
.send_body(utils::zstd::encode(STR));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
@ -607,21 +441,15 @@ async fn test_zstd_encoding_large() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = ZstdEncoder::new(Vec::new(), 5).unwrap();
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "zstd"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "zstd"))
|
||||
.send_body(utils::zstd::encode(&data));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().limit(320_000).await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
let bytes = res.body().limit(320_000).await.unwrap();
|
||||
assert_eq!(bytes, data.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -634,20 +462,14 @@ async fn test_encoding() {
|
||||
)
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(STR.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
let request = srv
|
||||
.post("/")
|
||||
.insert_header((CONTENT_ENCODING, "gzip"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.insert_header((header::CONTENT_ENCODING, "gzip"))
|
||||
.send_body(utils::gzip::encode(STR));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
@ -661,21 +483,15 @@ async fn test_gzip_encoding() {
|
||||
)
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(STR.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "gzip"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "gzip"))
|
||||
.send_body(utils::gzip::encode(STR));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, STR.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -689,21 +505,15 @@ async fn test_gzip_encoding_large() {
|
||||
)
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
let request = srv
|
||||
let req = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "gzip"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "gzip"))
|
||||
.send_body(utils::gzip::encode(&data));
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, data);
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -722,22 +532,15 @@ async fn test_reading_gzip_encoding_large_random() {
|
||||
)
|
||||
});
|
||||
|
||||
// client request
|
||||
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "gzip"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "gzip"))
|
||||
.send_body(utils::gzip::encode(&data));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
assert_eq!(bytes.len(), data.len());
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, data.as_bytes());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
@ -750,20 +553,14 @@ async fn test_reading_deflate_encoding() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(STR.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "deflate"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "deflate"))
|
||||
.send_body(utils::deflate::encode(STR));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
@ -778,20 +575,14 @@ async fn test_reading_deflate_encoding_large() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "deflate"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "deflate"))
|
||||
.send_body(utils::deflate::encode(&data));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
|
||||
srv.stop().await;
|
||||
@ -811,20 +602,14 @@ async fn test_reading_deflate_encoding_large_random() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "deflate"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "deflate"))
|
||||
.send_body(utils::deflate::encode(&data));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes.len(), data.len());
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
|
||||
@ -839,20 +624,14 @@ async fn test_brotli_encoding() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
||||
e.write_all(STR.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "br"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "br"))
|
||||
.send_body(utils::brotli::encode(STR));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||
|
||||
srv.stop().await;
|
||||
@ -876,20 +655,14 @@ async fn test_brotli_encoding_large() {
|
||||
)
|
||||
});
|
||||
|
||||
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let request = srv
|
||||
.post("/")
|
||||
.append_header((CONTENT_ENCODING, "br"))
|
||||
.send_body(enc.clone());
|
||||
let mut response = request.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
.append_header((header::CONTENT_ENCODING, "br"))
|
||||
.send_body(utils::brotli::encode(&data));
|
||||
let mut res = request.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().limit(320_000).await.unwrap();
|
||||
let bytes = res.body().limit(320_000).await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
|
||||
srv.stop().await;
|
||||
@ -898,32 +671,28 @@ async fn test_brotli_encoding_large() {
|
||||
#[cfg(feature = "openssl")]
|
||||
#[actix_rt::test]
|
||||
async fn test_brotli_encoding_large_openssl() {
|
||||
use actix_web::http::header;
|
||||
|
||||
let data = STR.repeat(10);
|
||||
let srv =
|
||||
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || {
|
||||
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||
// echo decompressed request body back in response
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Identity)
|
||||
.insert_header(header::ContentEncoding::Identity)
|
||||
.body(bytes)
|
||||
})))
|
||||
});
|
||||
|
||||
// body
|
||||
let mut enc = BrotliEncoder::new(Vec::new(), 3);
|
||||
enc.write_all(data.as_ref()).unwrap();
|
||||
let enc = enc.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let mut response = srv
|
||||
let mut res = srv
|
||||
.post("/")
|
||||
.append_header((actix_web::http::header::CONTENT_ENCODING, "br"))
|
||||
.send_body(enc)
|
||||
.append_header((header::CONTENT_ENCODING, "br"))
|
||||
.send_body(utils::brotli::encode(&data))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
|
||||
srv.stop().await;
|
||||
@ -970,28 +739,25 @@ mod plus_rustls {
|
||||
|
||||
let srv = actix_test::start_with(actix_test::config().rustls(tls_config()), || {
|
||||
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||
// echo decompressed request body back in response
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Identity)
|
||||
.insert_header(header::ContentEncoding::Identity)
|
||||
.body(bytes)
|
||||
})))
|
||||
});
|
||||
|
||||
// encode data
|
||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
e.write_all(data.as_ref()).unwrap();
|
||||
let enc = e.finish().unwrap();
|
||||
|
||||
// client request
|
||||
let req = srv
|
||||
.post("/")
|
||||
.insert_header((actix_web::http::header::CONTENT_ENCODING, "deflate"))
|
||||
.send_stream(TestBody::new(Bytes::from(enc), 1024));
|
||||
.insert_header((header::CONTENT_ENCODING, "deflate"))
|
||||
.send_stream(TestBody::new(
|
||||
Bytes::from(utils::deflate::encode(&data)),
|
||||
1024,
|
||||
));
|
||||
|
||||
let mut response = req.await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
let mut res = req.await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
// read response
|
||||
let bytes = response.body().await.unwrap();
|
||||
let bytes = res.body().await.unwrap();
|
||||
assert_eq!(bytes.len(), data.len());
|
||||
assert_eq!(bytes, Bytes::from(data));
|
||||
|
||||
@ -1084,8 +850,8 @@ async fn test_normalize() {
|
||||
.service(web::resource("/one").route(web::to(HttpResponse::Ok)))
|
||||
});
|
||||
|
||||
let response = srv.get("/one/").send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
let res = srv.get("/one/").send().await.unwrap();
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
|
||||
srv.stop().await
|
||||
}
|
||||
@ -1148,15 +914,20 @@ async fn test_accept_encoding_no_match() {
|
||||
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().finish())))
|
||||
});
|
||||
|
||||
let response = srv
|
||||
let mut res = srv
|
||||
.get("/")
|
||||
.append_header((ACCEPT_ENCODING, "compress, identity;q=0"))
|
||||
.insert_header((header::ACCEPT_ENCODING, "xz, identity;q=0"))
|
||||
.no_decompress()
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status().as_u16(), 406);
|
||||
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
|
||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None);
|
||||
|
||||
let bytes = res.body().await.unwrap();
|
||||
// body should contain the supported encodings
|
||||
assert!(!bytes.is_empty());
|
||||
|
||||
srv.stop().await;
|
||||
}
|
||||
|
76
tests/utils.rs
Normal file
76
tests/utils.rs
Normal file
@ -0,0 +1,76 @@
|
||||
// compiling some tests will trigger unused function warnings even though other tests use them
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::io::{Read as _, Write as _};
|
||||
|
||||
pub mod gzip {
|
||||
use super::*;
|
||||
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = GzDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub mod deflate {
|
||||
use super::*;
|
||||
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::fast());
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = ZlibDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub mod brotli {
|
||||
use super::*;
|
||||
use ::brotli2::{read::BrotliDecoder, write::BrotliEncoder};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = BrotliEncoder::new(Vec::new(), 3);
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = BrotliDecoder::new(bytes.as_ref());
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub mod zstd {
|
||||
use super::*;
|
||||
use ::zstd::stream::{read::Decoder, write::Encoder};
|
||||
|
||||
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut encoder = Encoder::new(Vec::new(), 3).unwrap();
|
||||
encoder.write_all(bytes.as_ref()).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
|
||||
let mut decoder = Decoder::new(bytes.as_ref()).unwrap();
|
||||
let mut buf = Vec::new();
|
||||
decoder.read_to_end(&mut buf).unwrap();
|
||||
buf
|
||||
}
|
||||
}
|
30
tests/weird_poll.rs
Normal file
30
tests/weird_poll.rs
Normal file
@ -0,0 +1,30 @@
|
||||
//! Regression test for https://github.com/actix/actix-web/issues/1321
|
||||
|
||||
// use actix_http::body::{BodyStream, MessageBody};
|
||||
// use bytes::Bytes;
|
||||
// use futures_channel::oneshot;
|
||||
// use futures_util::{
|
||||
// stream::once,
|
||||
// task::{noop_waker, Context},
|
||||
// };
|
||||
|
||||
// #[test]
|
||||
// fn weird_poll() {
|
||||
// let (sender, receiver) = oneshot::channel();
|
||||
// let mut body_stream = Ok(BodyStream::new(once(async {
|
||||
// let x = Box::new(0);
|
||||
// let y = &x;
|
||||
// receiver.await.unwrap();
|
||||
// let _z = **y;
|
||||
// Ok::<_, ()>(Bytes::new())
|
||||
// })));
|
||||
|
||||
// let waker = noop_waker();
|
||||
// let mut cx = Context::from_waker(&waker);
|
||||
|
||||
// let _ = body_stream.as_mut().unwrap().poll_next(&mut cx);
|
||||
// sender.send(()).unwrap();
|
||||
// let _ = std::mem::replace(&mut body_stream, Err([0; 32]))
|
||||
// .unwrap()
|
||||
// .poll_next(&mut cx);
|
||||
// }
|
Reference in New Issue
Block a user