mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-03 17:41:30 +02:00
Compare commits
25 Commits
files-v0.6
...
files-v0.6
Author | SHA1 | Date | |
---|---|---|---|
604be5495f | |||
262c6bc828 | |||
5eba95b731 | |||
09afd033fc | |||
539697292a | |||
5a480d1d78 | |||
9a26393375 | |||
2eacb735a4 | |||
767e4efe22 | |||
e559a197cc | |||
93aa86e30b | |||
2d8d2f5ab0 | |||
083ee05d50 | |||
ed0516d724 | |||
7535a1ade8 | |||
8846808804 | |||
3b6333e65f | |||
b1148fd735 | |||
12f7720309 | |||
2d8530feb3 | |||
7faeffc5ab | |||
f81d4bdae7 | |||
6893773280 | |||
73a655544e | |||
baa5a663c4 |
@ -3,6 +3,7 @@ chk = "check --workspace --all-features --tests --examples --bins"
|
||||
lint = "clippy --workspace --tests --examples"
|
||||
ci-min = "hack check --workspace --no-default-features"
|
||||
ci-min-test = "hack check --workspace --no-default-features --tests --examples"
|
||||
ci-default = "hack check --workspace"
|
||||
ci-full = "check --workspace --bins --examples --tests"
|
||||
ci-test = "test --workspace --all-features --no-fail-fast"
|
||||
ci-default = "check --workspace --bins --tests --examples"
|
||||
ci-full = "check --workspace --all-features --bins --tests --examples"
|
||||
ci-test = "test --workspace --all-features --lib --tests --no-fail-fast -- --nocapture"
|
||||
ci-doctest = "hack test --workspace --all-features --doc --no-fail-fast -- --nocapture"
|
||||
|
13
.github/ISSUE_TEMPLATE/config.yml
vendored
13
.github/ISSUE_TEMPLATE/config.yml
vendored
@ -1,15 +1,8 @@
|
||||
blank_issues_enabled: true
|
||||
contact_links:
|
||||
- name: GitHub Discussions
|
||||
url: https://github.com/actix/actix-web/discussions
|
||||
about: Actix Web Q&A
|
||||
- name: Gitter chat (actix-web)
|
||||
url: https://gitter.im/actix/actix-web
|
||||
about: Actix Web Q&A
|
||||
- name: Gitter chat (actix)
|
||||
url: https://gitter.im/actix/actix
|
||||
about: Actix (actor framework) Q&A
|
||||
- name: Actix Discord
|
||||
url: https://discord.gg/NWpN5mmg3x
|
||||
about: Actix developer discussion and community chat
|
||||
|
||||
- name: GitHub Discussions
|
||||
url: https://github.com/actix/actix-web/discussions
|
||||
about: Actix Web Q&A
|
||||
|
43
.github/workflows/ci.yml
vendored
43
.github/workflows/ci.yml
vendored
@ -44,13 +44,6 @@ jobs:
|
||||
profile: minimal
|
||||
override: true
|
||||
|
||||
- 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:
|
||||
@ -66,43 +59,33 @@ jobs:
|
||||
|
||||
- name: check minimal
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: hack
|
||||
args: check --workspace --no-default-features
|
||||
with: { command: ci-min }
|
||||
|
||||
- name: check minimal + tests
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: hack
|
||||
args: check --workspace --no-default-features --tests --examples
|
||||
with: { command: ci-min-test }
|
||||
|
||||
- name: check default
|
||||
uses: actions-rs/cargo@v1
|
||||
with: { command: ci-default }
|
||||
|
||||
- name: check full
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: check
|
||||
args: --workspace --bins --examples --tests
|
||||
with: { command: ci-full }
|
||||
|
||||
- name: tests
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --workspace --all-features --no-fail-fast -- --nocapture
|
||||
--skip=test_h2_content_length
|
||||
--skip=test_reading_deflate_encoding_large_random_rustls
|
||||
|
||||
- name: tests (actix-http)
|
||||
uses: actions-rs/cargo@v1
|
||||
timeout-minutes: 40
|
||||
with:
|
||||
command: test
|
||||
args: --package=actix-http --no-default-features --features=rustls -- --nocapture
|
||||
command: ci-test
|
||||
args: --skip=test_reading_deflate_encoding_large_random_rustls
|
||||
|
||||
- name: tests (awc)
|
||||
- name: doc tests
|
||||
# due to unknown issue with running doc tests on macOS
|
||||
if: matrix.target.os == 'ubuntu-latest'
|
||||
uses: actions-rs/cargo@v1
|
||||
timeout-minutes: 40
|
||||
with:
|
||||
command: test
|
||||
args: --package=awc --no-default-features --features=rustls -- --nocapture
|
||||
with: { command: ci-doctest }
|
||||
|
||||
- name: Generate coverage file
|
||||
if: >
|
||||
|
2
.github/workflows/clippy-fmt.yml
vendored
2
.github/workflows/clippy-fmt.yml
vendored
@ -36,4 +36,4 @@ jobs:
|
||||
uses: actions-rs/clippy-check@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
args: --workspace --tests --all-features
|
||||
args: --workspace --all-features --tests
|
||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -16,3 +16,6 @@ guide/build/
|
||||
|
||||
# Configuration directory generated by CLion
|
||||
.idea
|
||||
|
||||
# Configuration directory generated by VSCode
|
||||
.vscode
|
||||
|
25
CHANGES.md
25
CHANGES.md
@ -3,12 +3,35 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 4.0.0-beta.8 - 2021-06-26
|
||||
### Added
|
||||
* Add `ServiceRequest::parts_mut`. [#2177]
|
||||
* Add extractors for `Uri` and `Method`. [#2263]
|
||||
* Add extractors for `ConnectionInfo` and `PeerAddr`. [#2263]
|
||||
* Add `Route::service` for using hand-written services as handlers. [#2262]
|
||||
|
||||
### Changed
|
||||
* Change compression algorithm features flags. [#2250]
|
||||
* Deprecate `App::data` and `App::data_factory`. [#2271]
|
||||
* Smarter extraction of `ConnectionInfo` parts. [#2282]
|
||||
|
||||
### Fixed
|
||||
* Scope and Resource middleware can access data items set on their own layer. [#2288]
|
||||
|
||||
[#2177]: https://github.com/actix/actix-web/pull/2177
|
||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||
[#2271]: https://github.com/actix/actix-web/pull/2271
|
||||
[#2262]: https://github.com/actix/actix-web/pull/2262
|
||||
[#2263]: https://github.com/actix/actix-web/pull/2263
|
||||
[#2282]: https://github.com/actix/actix-web/pull/2282
|
||||
[#2288]: https://github.com/actix/actix-web/pull/2288
|
||||
|
||||
|
||||
## 4.0.0-beta.7 - 2021-06-17
|
||||
### Added
|
||||
* `HttpServer::worker_max_blocking_threads` for setting block thread pool. [#2200]
|
||||
|
||||
### Changed
|
||||
|
||||
* Adjusted default JSON payload limit to 2MB (from 32kb) and included size and limits in the `JsonPayloadError::Overflow` error variant. [#2162]
|
||||
[#2162]: (https://github.com/actix/actix-web/pull/2162)
|
||||
* `ServiceResponse::error_response` now uses body type of `Body`. [#2201]
|
||||
|
34
Cargo.toml
34
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web"
|
||||
version = "4.0.0-beta.7"
|
||||
version = "4.0.0-beta.8"
|
||||
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"]
|
||||
@ -17,7 +17,7 @@ edition = "2018"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
# features that docs.rs will build with
|
||||
features = ["openssl", "rustls", "compress", "cookies", "secure-cookies"]
|
||||
features = ["openssl", "rustls", "compress-brotli", "compress-gzip", "compress-zstd", "cookies", "secure-cookies"]
|
||||
|
||||
[lib]
|
||||
name = "actix_web"
|
||||
@ -39,10 +39,14 @@ members = [
|
||||
# resolver = "2"
|
||||
|
||||
[features]
|
||||
default = ["compress", "cookies"]
|
||||
default = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
|
||||
|
||||
# content-encoding support
|
||||
compress = ["actix-http/compress"]
|
||||
# Brotli algorithm content-encoding support
|
||||
compress-brotli = ["actix-http/compress-brotli", "__compress"]
|
||||
# Gzip and deflate algorithms content-encoding support
|
||||
compress-gzip = ["actix-http/compress-gzip", "__compress"]
|
||||
# Zstd algorithm content-encoding support
|
||||
compress-zstd = ["actix-http/compress-zstd", "__compress"]
|
||||
|
||||
# support for cookies
|
||||
cookies = ["cookie"]
|
||||
@ -56,6 +60,10 @@ openssl = ["actix-http/openssl", "actix-tls/accept", "actix-tls/openssl"]
|
||||
# rustls
|
||||
rustls = ["actix-http/rustls", "actix-tls/accept", "actix-tls/rustls"]
|
||||
|
||||
# Internal (PRIVATE!) features used to aid testing and cheking feature status.
|
||||
# Don't rely on these whatsoever. They may disappear at anytime.
|
||||
__compress = []
|
||||
|
||||
[dependencies]
|
||||
actix-codec = "0.4.0"
|
||||
actix-macros = "0.2.1"
|
||||
@ -67,10 +75,11 @@ actix-utils = "3.0.0"
|
||||
actix-tls = { version = "3.0.0-beta.5", default-features = false, optional = true }
|
||||
|
||||
actix-web-codegen = "0.5.0-beta.2"
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-http = "3.0.0-beta.8"
|
||||
|
||||
ahash = "0.7"
|
||||
bytes = "1"
|
||||
cfg-if = "1"
|
||||
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
|
||||
derive_more = "0.99.5"
|
||||
either = "1.5.3"
|
||||
@ -94,17 +103,16 @@ time = { version = "0.2.23", default-features = false, features = ["std"] }
|
||||
url = "2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-test = { version = "0.1.0-beta.2", features = ["openssl", "rustls"] }
|
||||
awc = { version = "3.0.0-beta.6", features = ["openssl"] }
|
||||
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
||||
awc = { version = "3.0.0-beta.7", features = ["openssl"] }
|
||||
|
||||
brotli2 = "0.3.2"
|
||||
criterion = "0.3"
|
||||
criterion = { version = "0.3", features = ["html_reports"] }
|
||||
env_logger = "0.8"
|
||||
flate2 = "1.0.13"
|
||||
zstd = "0.7"
|
||||
rand = "0.8"
|
||||
rcgen = "0.8"
|
||||
serde_derive = "1.0"
|
||||
tls-openssl = { package = "openssl", version = "0.10.9" }
|
||||
tls-rustls = { package = "rustls", version = "0.19.0" }
|
||||
|
||||
@ -126,15 +134,15 @@ awc = { path = "awc" }
|
||||
|
||||
[[test]]
|
||||
name = "test_server"
|
||||
required-features = ["compress", "cookies"]
|
||||
required-features = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
|
||||
|
||||
[[example]]
|
||||
name = "basic"
|
||||
required-features = ["compress"]
|
||||
required-features = ["compress-gzip"]
|
||||
|
||||
[[example]]
|
||||
name = "uds"
|
||||
required-features = ["compress"]
|
||||
required-features = ["compress-gzip"]
|
||||
|
||||
[[example]]
|
||||
name = "on_connect"
|
||||
|
12
MIGRATION.md
12
MIGRATION.md
@ -10,6 +10,18 @@
|
||||
|
||||
Alternatively, explicitly require trailing slashes: `NormalizePath::new(TrailingSlash::Always)`.
|
||||
|
||||
* Feature flag `compress` has been split into its supported algorithm (brotli, gzip, zstd).
|
||||
By default all compression algorithms are enabled.
|
||||
To select algorithm you want to include with `middleware::Compress` use following flags:
|
||||
- `compress-brotli`
|
||||
- `compress-gzip`
|
||||
- `compress-zstd`
|
||||
If you have set in your `Cargo.toml` dedicated `actix-web` features and you still want
|
||||
to have compression enabled. Please change features selection like bellow:
|
||||
|
||||
Before: `"compress"`
|
||||
After: `"compress-brotli", "compress-gzip", "compress-zstd"`
|
||||
|
||||
|
||||
## 3.0.0
|
||||
|
||||
|
@ -6,10 +6,10 @@
|
||||
<p>
|
||||
|
||||
[](https://crates.io/crates/actix-web)
|
||||
[](https://docs.rs/actix-web/4.0.0-beta.7)
|
||||
[](https://docs.rs/actix-web/4.0.0-beta.8)
|
||||
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||

|
||||
[](https://deps.rs/crate/actix-web/4.0.0-beta.7)
|
||||
[](https://deps.rs/crate/actix-web/4.0.0-beta.8)
|
||||
<br />
|
||||
[](https://github.com/actix/actix-web/actions)
|
||||
[](https://codecov.io/gh/actix/actix-web)
|
||||
@ -25,7 +25,7 @@
|
||||
* Streaming and pipelining
|
||||
* Keep-alive and slow requests handling
|
||||
* Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
||||
* Transparent content compression/decompression (br, gzip, deflate)
|
||||
* Transparent content compression/decompression (br, gzip, deflate, zstd)
|
||||
* Powerful [request routing](https://actix.rs/docs/url-dispatch/)
|
||||
* Multipart streams
|
||||
* Static assets
|
||||
|
@ -3,6 +3,14 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.6.0-beta.6 - 2021-06-26
|
||||
* Added `Files::path_filter()`. [#2274]
|
||||
* `Files::show_files_listing()` can now be used with `Files::index_file()` to show files listing as a fallback when the index file is not found. [#2228]
|
||||
|
||||
[#2274]: https://github.com/actix/actix-web/pull/2274
|
||||
[#2228]: https://github.com/actix/actix-web/pull/2228
|
||||
|
||||
|
||||
## 0.6.0-beta.5 - 2021-06-17
|
||||
* `NamedFile` now implements `ServiceFactory` and `HttpServiceFactory` making it much more useful in routing. For example, it can be used directly as a default service. [#2135]
|
||||
* For symbolic links, `Content-Disposition` header no longer shows the filename of the original file. [#2156]
|
||||
@ -16,12 +24,11 @@
|
||||
|
||||
|
||||
## 0.6.0-beta.4 - 2021-04-02
|
||||
* No notable changes.
|
||||
|
||||
* Add support for `.guard` in `Files` to selectively filter `Files` services. [#2046]
|
||||
|
||||
[#2046]: https://github.com/actix/actix-web/pull/2046
|
||||
|
||||
|
||||
## 0.6.0-beta.3 - 2021-03-09
|
||||
* No notable changes.
|
||||
|
||||
|
@ -1,13 +1,11 @@
|
||||
[package]
|
||||
name = "actix-files"
|
||||
version = "0.6.0-beta.5"
|
||||
version = "0.6.0-beta.6"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Static file serving for Actix Web"
|
||||
readme = "README.md"
|
||||
keywords = ["actix", "http", "async", "futures"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web.git"
|
||||
documentation = "https://docs.rs/actix-files/"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
categories = ["asynchronous", "web-programming::http-server"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2018"
|
||||
@ -17,8 +15,8 @@ name = "actix_files"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false }
|
||||
actix-http = "3.0.0-beta.8"
|
||||
actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
|
||||
@ -35,5 +33,5 @@ percent-encoding = "2.1"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-web = "4.0.0-beta.7"
|
||||
actix-test = "0.1.0-beta.2"
|
||||
actix-web = "4.0.0-beta.8"
|
||||
actix-test = "0.1.0-beta.3"
|
||||
|
@ -3,17 +3,16 @@
|
||||
> Static file serving for Actix Web
|
||||
|
||||
[](https://crates.io/crates/actix-files)
|
||||
[](https://docs.rs/actix-files/0.6.0-beta.5)
|
||||
[](https://docs.rs/actix-files/0.6.0-beta.6)
|
||||
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-files/0.6.0-beta.5)
|
||||
[](https://deps.rs/crate/actix-files/0.6.0-beta.6)
|
||||
[](https://crates.io/crates/actix-files)
|
||||
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-files/)
|
||||
- [Example Project](https://github.com/actix/examples/tree/master/basics/static_index)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum supported Rust version: 1.46 or later
|
||||
|
@ -1,9 +1,17 @@
|
||||
use std::{cell::RefCell, fmt, io, path::PathBuf, rc::Rc};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
fmt, io,
|
||||
path::{Path, PathBuf},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use actix_service::{boxed, IntoServiceFactory, ServiceFactory, ServiceFactoryExt};
|
||||
use actix_utils::future::ok;
|
||||
use actix_web::{
|
||||
dev::{AppService, HttpServiceFactory, ResourceDef, ServiceRequest, ServiceResponse},
|
||||
dev::{
|
||||
AppService, HttpServiceFactory, RequestHead, ResourceDef, ServiceRequest,
|
||||
ServiceResponse,
|
||||
},
|
||||
error::Error,
|
||||
guard::Guard,
|
||||
http::header::DispositionType,
|
||||
@ -13,7 +21,7 @@ use futures_core::future::LocalBoxFuture;
|
||||
|
||||
use crate::{
|
||||
directory_listing, named, Directory, DirectoryRenderer, FilesService, HttpNewService,
|
||||
MimeOverride,
|
||||
MimeOverride, PathFilter,
|
||||
};
|
||||
|
||||
/// Static files handling service.
|
||||
@ -36,6 +44,7 @@ pub struct Files {
|
||||
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
||||
renderer: Rc<DirectoryRenderer>,
|
||||
mime_override: Option<Rc<MimeOverride>>,
|
||||
path_filter: Option<Rc<PathFilter>>,
|
||||
file_flags: named::Flags,
|
||||
use_guards: Option<Rc<dyn Guard>>,
|
||||
guards: Vec<Rc<dyn Guard>>,
|
||||
@ -60,6 +69,7 @@ impl Clone for Files {
|
||||
file_flags: self.file_flags,
|
||||
path: self.path.clone(),
|
||||
mime_override: self.mime_override.clone(),
|
||||
path_filter: self.path_filter.clone(),
|
||||
use_guards: self.use_guards.clone(),
|
||||
guards: self.guards.clone(),
|
||||
hidden_files: self.hidden_files,
|
||||
@ -104,6 +114,7 @@ impl Files {
|
||||
default: Rc::new(RefCell::new(None)),
|
||||
renderer: Rc::new(directory_listing),
|
||||
mime_override: None,
|
||||
path_filter: None,
|
||||
file_flags: named::Flags::default(),
|
||||
use_guards: None,
|
||||
guards: Vec::new(),
|
||||
@ -114,6 +125,9 @@ impl Files {
|
||||
/// Show files listing for directories.
|
||||
///
|
||||
/// By default show files listing is disabled.
|
||||
///
|
||||
/// When used with [`Files::index_file()`], files listing is shown as a fallback
|
||||
/// when the index file is not found.
|
||||
pub fn show_files_listing(mut self) -> Self {
|
||||
self.show_index = true;
|
||||
self
|
||||
@ -146,10 +160,45 @@ impl Files {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets path filtering closure.
|
||||
///
|
||||
/// The path provided to the closure is relative to `serve_from` path.
|
||||
/// You can safely join this path with the `serve_from` path to get the real path.
|
||||
/// However, the real path may not exist since the filter is called before checking path existence.
|
||||
///
|
||||
/// When a path doesn't pass the filter, [`Files::default_handler`] is called if set, otherwise,
|
||||
/// `404 Not Found` is returned.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::path::Path;
|
||||
/// use actix_files::Files;
|
||||
///
|
||||
/// // prevent searching subdirectories and following symlinks
|
||||
/// let files_service = Files::new("/", "./static").path_filter(|path, _| {
|
||||
/// path.components().count() == 1
|
||||
/// && Path::new("./static")
|
||||
/// .join(path)
|
||||
/// .symlink_metadata()
|
||||
/// .map(|m| !m.file_type().is_symlink())
|
||||
/// .unwrap_or(false)
|
||||
/// });
|
||||
/// ```
|
||||
pub fn path_filter<F>(mut self, f: F) -> Self
|
||||
where
|
||||
F: Fn(&Path, &RequestHead) -> bool + 'static,
|
||||
{
|
||||
self.path_filter = Some(Rc::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set index file
|
||||
///
|
||||
/// Shows specific index file for directory "/" instead of
|
||||
/// Shows specific index file for directories instead of
|
||||
/// showing files listing.
|
||||
///
|
||||
/// If the index file is not found, files listing is shown as a fallback if
|
||||
/// [`Files::show_files_listing()`] is set.
|
||||
pub fn index_file<T: Into<String>>(mut self, index: T) -> Self {
|
||||
self.index = Some(index.into());
|
||||
self
|
||||
@ -312,6 +361,7 @@ impl ServiceFactory<ServiceRequest> for Files {
|
||||
default: None,
|
||||
renderer: self.renderer.clone(),
|
||||
mime_override: self.mime_override.clone(),
|
||||
path_filter: self.path_filter.clone(),
|
||||
file_flags: self.file_flags,
|
||||
guards: self.use_guards.clone(),
|
||||
hidden_files: self.hidden_files,
|
||||
|
@ -16,11 +16,12 @@
|
||||
|
||||
use actix_service::boxed::{BoxService, BoxServiceFactory};
|
||||
use actix_web::{
|
||||
dev::{ServiceRequest, ServiceResponse},
|
||||
dev::{RequestHead, ServiceRequest, ServiceResponse},
|
||||
error::Error,
|
||||
http::header::DispositionType,
|
||||
};
|
||||
use mime_guess::from_ext;
|
||||
use std::path::Path;
|
||||
|
||||
mod chunked;
|
||||
mod directory;
|
||||
@ -56,6 +57,8 @@ pub fn file_extension_to_mime(ext: &str) -> mime::Mime {
|
||||
|
||||
type MimeOverride = dyn Fn(&mime::Name<'_>) -> DispositionType;
|
||||
|
||||
type PathFilter = dyn Fn(&Path, &RequestHead) -> bool;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{
|
||||
@ -872,4 +875,69 @@ mod tests {
|
||||
"inline; filename=\"symlink-test.png\""
|
||||
);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_index_with_show_files_listing() {
|
||||
let service = Files::new(".", ".")
|
||||
.index_file("lib.rs")
|
||||
.show_files_listing()
|
||||
.new_service(())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Serve the index if exists
|
||||
let req = TestRequest::default().uri("/src").to_srv_request();
|
||||
let resp = test::call_service(&service, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||
"text/x-rust"
|
||||
);
|
||||
|
||||
// Show files listing, otherwise.
|
||||
let req = TestRequest::default().uri("/tests").to_srv_request();
|
||||
let resp = test::call_service(&service, req).await;
|
||||
assert_eq!(
|
||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
let bytes = test::read_body(resp).await;
|
||||
assert!(format!("{:?}", bytes).contains("/tests/test.png"));
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_path_filter() {
|
||||
// prevent searching subdirectories
|
||||
let st = Files::new("/", ".")
|
||||
.path_filter(|path, _| path.components().count() == 1)
|
||||
.new_service(())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let req = TestRequest::with_uri("/Cargo.toml").to_srv_request();
|
||||
let resp = test::call_service(&st, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::with_uri("/src/lib.rs").to_srv_request();
|
||||
let resp = test::call_service(&st, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_default_handler_filter() {
|
||||
let st = Files::new("/", ".")
|
||||
.default_handler(|req: ServiceRequest| {
|
||||
ok(req.into_response(HttpResponse::Ok().body("default content")))
|
||||
})
|
||||
.path_filter(|path, _| path.extension() == Some("png".as_ref()))
|
||||
.new_service(())
|
||||
.await
|
||||
.unwrap();
|
||||
let req = TestRequest::with_uri("/Cargo.toml").to_srv_request();
|
||||
let resp = test::call_service(&st, req).await;
|
||||
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let bytes = test::read_body(resp).await;
|
||||
assert_eq!(bytes, web::Bytes::from_static(b"default content"));
|
||||
}
|
||||
}
|
||||
|
@ -355,8 +355,8 @@ impl NamedFile {
|
||||
} else if let (Some(ref m), Some(header::IfUnmodifiedSince(ref since))) =
|
||||
(last_modified, req.get_header())
|
||||
{
|
||||
let t1: SystemTime = m.clone().into();
|
||||
let t2: SystemTime = since.clone().into();
|
||||
let t1: SystemTime = (*m).into();
|
||||
let t2: SystemTime = (*since).into();
|
||||
|
||||
match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
|
||||
(Ok(t1), Ok(t2)) => t1.as_secs() > t2.as_secs(),
|
||||
@ -374,8 +374,8 @@ impl NamedFile {
|
||||
} else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) =
|
||||
(last_modified, req.get_header())
|
||||
{
|
||||
let t1: SystemTime = m.clone().into();
|
||||
let t2: SystemTime = since.clone().into();
|
||||
let t1: SystemTime = (*m).into();
|
||||
let t2: SystemTime = (*since).into();
|
||||
|
||||
match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
|
||||
(Ok(t1), Ok(t2)) => t1.as_secs() <= t2.as_secs(),
|
||||
|
@ -13,7 +13,7 @@ use futures_core::future::LocalBoxFuture;
|
||||
|
||||
use crate::{
|
||||
named, Directory, DirectoryRenderer, FilesError, HttpService, MimeOverride, NamedFile,
|
||||
PathBufWrap,
|
||||
PathBufWrap, PathFilter,
|
||||
};
|
||||
|
||||
/// Assembled file serving service.
|
||||
@ -25,6 +25,7 @@ pub struct FilesService {
|
||||
pub(crate) default: Option<HttpService>,
|
||||
pub(crate) renderer: Rc<DirectoryRenderer>,
|
||||
pub(crate) mime_override: Option<Rc<MimeOverride>>,
|
||||
pub(crate) path_filter: Option<Rc<PathFilter>>,
|
||||
pub(crate) file_flags: named::Flags,
|
||||
pub(crate) guards: Option<Rc<dyn Guard>>,
|
||||
pub(crate) hidden_files: bool,
|
||||
@ -82,6 +83,18 @@ impl Service<ServiceRequest> for FilesService {
|
||||
Err(e) => return Box::pin(ok(req.error_response(e))),
|
||||
};
|
||||
|
||||
if let Some(filter) = &self.path_filter {
|
||||
if !filter(real_path.as_ref(), req.head()) {
|
||||
if let Some(ref default) = self.default {
|
||||
return Box::pin(default.call(req));
|
||||
} else {
|
||||
return Box::pin(ok(
|
||||
req.into_response(actix_web::HttpResponse::NotFound().finish())
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// full file path
|
||||
let path = self.directory.join(&real_path);
|
||||
if let Err(err) = path.canonicalize() {
|
||||
@ -102,26 +115,20 @@ impl Service<ServiceRequest> for FilesService {
|
||||
)));
|
||||
}
|
||||
|
||||
if let Some(ref redir_index) = self.index {
|
||||
let path = path.join(redir_index);
|
||||
|
||||
match NamedFile::open(path) {
|
||||
Ok(mut named_file) => {
|
||||
if let Some(ref mime_override) = self.mime_override {
|
||||
let new_disposition =
|
||||
mime_override(&named_file.content_type.type_());
|
||||
named_file.content_disposition.disposition = new_disposition;
|
||||
}
|
||||
named_file.flags = self.file_flags;
|
||||
|
||||
let (req, _) = req.into_parts();
|
||||
let res = named_file.into_response(&req);
|
||||
Box::pin(ok(ServiceResponse::new(req, res)))
|
||||
}
|
||||
Err(err) => self.handle_err(err, req),
|
||||
let serve_named_file = |req: ServiceRequest, mut named_file: NamedFile| {
|
||||
if let Some(ref mime_override) = self.mime_override {
|
||||
let new_disposition = mime_override(&named_file.content_type.type_());
|
||||
named_file.content_disposition.disposition = new_disposition;
|
||||
}
|
||||
} else if self.show_index {
|
||||
let dir = Directory::new(self.directory.clone(), path);
|
||||
named_file.flags = self.file_flags;
|
||||
|
||||
let (req, _) = req.into_parts();
|
||||
let res = named_file.into_response(&req);
|
||||
Box::pin(ok(ServiceResponse::new(req, res)))
|
||||
};
|
||||
|
||||
let show_index = |req: ServiceRequest| {
|
||||
let dir = Directory::new(self.directory.clone(), path.clone());
|
||||
|
||||
let (req, _) = req.into_parts();
|
||||
let x = (self.renderer)(&dir, &req);
|
||||
@ -130,11 +137,19 @@ impl Service<ServiceRequest> for FilesService {
|
||||
Ok(resp) => ok(resp),
|
||||
Err(err) => ok(ServiceResponse::from_err(err, req)),
|
||||
})
|
||||
} else {
|
||||
Box::pin(ok(ServiceResponse::from_err(
|
||||
};
|
||||
|
||||
match self.index {
|
||||
Some(ref index) => match NamedFile::open(path.join(index)) {
|
||||
Ok(named_file) => serve_named_file(req, named_file),
|
||||
Err(_) if self.show_index => show_index(req),
|
||||
Err(err) => self.handle_err(err, req),
|
||||
},
|
||||
None if self.show_index => show_index(req),
|
||||
_ => Box::pin(ok(ServiceResponse::from_err(
|
||||
FilesError::IsDirectory,
|
||||
req.into_parts().0,
|
||||
)))
|
||||
))),
|
||||
}
|
||||
} else {
|
||||
match NamedFile::open(path) {
|
||||
|
@ -35,7 +35,7 @@ actix-tls = "3.0.0-beta.5"
|
||||
actix-utils = "3.0.0"
|
||||
actix-rt = "2.2"
|
||||
actix-server = "2.0.0-beta.3"
|
||||
awc = { version = "3.0.0-beta.6", default-features = false }
|
||||
awc = { version = "3.0.0-beta.7", default-features = false }
|
||||
|
||||
base64 = "0.13"
|
||||
bytes = "1"
|
||||
@ -51,5 +51,5 @@ time = { version = "0.2.23", default-features = false, features = ["std"] }
|
||||
tls-openssl = { version = "0.10.9", package = "openssl", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-web = { version = "4.0.0-beta.7", default-features = false, features = ["cookies"] }
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["cookies"] }
|
||||
actix-http = "3.0.0-beta.8"
|
||||
|
@ -4,12 +4,14 @@
|
||||
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
[](https://docs.rs/actix-http-test/3.0.0-beta.4)
|
||||
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||

|
||||
<br>
|
||||
[](https://deps.rs/crate/actix-http-test/3.0.0-beta.4)
|
||||
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-http-test)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||
|
@ -3,6 +3,17 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.8 - 2021-06-26
|
||||
### Changed
|
||||
* Change compression algorithm features flags. [#2250]
|
||||
|
||||
### Removed
|
||||
* `downcast` and `downcast_get_type_id` macros. [#2291]
|
||||
|
||||
[#2291]: https://github.com/actix/actix-web/pull/2291
|
||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||
|
||||
|
||||
## 3.0.0-beta.7 - 2021-06-17
|
||||
### Added
|
||||
* Alias `body::Body` as `body::AnyBody`. [#2215]
|
||||
|
@ -1,13 +1,11 @@
|
||||
[package]
|
||||
name = "actix-http"
|
||||
version = "3.0.0-beta.7"
|
||||
version = "3.0.0-beta.8"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "HTTP primitives for the Actix ecosystem"
|
||||
readme = "README.md"
|
||||
keywords = ["actix", "http", "framework", "async", "futures"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web.git"
|
||||
documentation = "https://docs.rs/actix-http/"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
categories = ["network-programming", "asynchronous",
|
||||
"web-programming::http-server",
|
||||
"web-programming::websocket"]
|
||||
@ -16,7 +14,7 @@ edition = "2018"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
# features that docs.rs will build with
|
||||
features = ["openssl", "rustls", "compress"]
|
||||
features = ["openssl", "rustls", "compress-brotli", "compress-gzip", "compress-zstd"]
|
||||
|
||||
[lib]
|
||||
name = "actix_http"
|
||||
@ -32,11 +30,17 @@ openssl = ["actix-tls/openssl"]
|
||||
rustls = ["actix-tls/rustls"]
|
||||
|
||||
# enable compression support
|
||||
compress = ["flate2", "brotli2", "zstd"]
|
||||
compress-brotli = ["brotli2", "__compress"]
|
||||
compress-gzip = ["flate2", "__compress"]
|
||||
compress-zstd = ["zstd", "__compress"]
|
||||
|
||||
# trust-dns as client dns resolver
|
||||
trust-dns = ["trust-dns-resolver"]
|
||||
|
||||
# Internal (PRIVATE!) features used to aid testing and cheking feature status.
|
||||
# Don't rely on these whatsoever. They may disappear at anytime.
|
||||
__compress = []
|
||||
|
||||
[dependencies]
|
||||
actix-service = "2.0.0"
|
||||
actix-codec = "0.4.0"
|
||||
|
@ -3,18 +3,17 @@
|
||||
> HTTP primitives for the Actix ecosystem.
|
||||
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://docs.rs/actix-http/3.0.0-beta.7)
|
||||
[](https://docs.rs/actix-http/3.0.0-beta.8)
|
||||
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-http/3.0.0-beta.7)
|
||||
[](https://deps.rs/crate/actix-http/3.0.0-beta.8)
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-http)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||
|
||||
## Example
|
||||
|
@ -78,12 +78,12 @@ impl HeaderIndex {
|
||||
// test cases taken from:
|
||||
// https://github.com/seanmonstar/httparse/blob/master/benches/parse.rs
|
||||
|
||||
const REQ_SHORT: &'static [u8] = b"\
|
||||
const REQ_SHORT: &[u8] = b"\
|
||||
GET / HTTP/1.0\r\n\
|
||||
Host: example.com\r\n\
|
||||
Cookie: session=60; user_id=1\r\n\r\n";
|
||||
|
||||
const REQ: &'static [u8] = b"\
|
||||
const REQ: &[u8] = b"\
|
||||
GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n\
|
||||
Host: www.kittyhell.com\r\n\
|
||||
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\n\
|
||||
@ -119,6 +119,8 @@ mod _original {
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
pub fn parse_headers(src: &mut BytesMut) -> usize {
|
||||
#![allow(clippy::uninit_assumed_init)]
|
||||
|
||||
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
||||
unsafe { MaybeUninit::uninit().assume_init() };
|
||||
|
||||
|
@ -85,7 +85,7 @@ impl Connector<()> {
|
||||
use bytes::{BufMut, BytesMut};
|
||||
|
||||
let mut alpn = BytesMut::with_capacity(20);
|
||||
for proto in protocols.iter() {
|
||||
for proto in &protocols {
|
||||
alpn.put_u8(proto.len() as u8);
|
||||
alpn.put(proto.as_slice());
|
||||
}
|
||||
@ -290,8 +290,7 @@ where
|
||||
let h2 = sock
|
||||
.ssl()
|
||||
.selected_alpn_protocol()
|
||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
||||
.unwrap_or(false);
|
||||
.map_or(false, |protos| protos.windows(2).any(|w| w == H2));
|
||||
if h2 {
|
||||
(Box::new(sock), Protocol::Http2)
|
||||
} else {
|
||||
@ -325,8 +324,7 @@ where
|
||||
.get_ref()
|
||||
.1
|
||||
.get_alpn_protocol()
|
||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
||||
.unwrap_or(false);
|
||||
.map_or(false, |protos| protos.windows(2).any(|w| w == H2));
|
||||
if h2 {
|
||||
(Box::new(sock), Protocol::Http2)
|
||||
} else {
|
||||
|
@ -168,14 +168,13 @@ where
|
||||
|
||||
if let Err(e) = send.send_data(bytes, false) {
|
||||
return Err(e.into());
|
||||
} else {
|
||||
if !b.is_empty() {
|
||||
send.reserve_capacity(b.len());
|
||||
} else {
|
||||
buf = None;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !b.is_empty() {
|
||||
send.reserve_capacity(b.len());
|
||||
} else {
|
||||
buf = None;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Some(Err(e)) => return Err(e.into()),
|
||||
}
|
||||
|
@ -152,8 +152,8 @@ impl ServiceConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Return keep-alive timer delay is configured.
|
||||
#[inline]
|
||||
pub fn keep_alive_timer(&self) -> Option<Sleep> {
|
||||
self.keep_alive().map(|ka| sleep_until(self.now() + ka))
|
||||
}
|
||||
@ -365,11 +365,11 @@ mod tests {
|
||||
let clone3 = service.clone();
|
||||
|
||||
drop(clone1);
|
||||
assert_eq!(false, notify_on_drop::is_dropped());
|
||||
assert!(!notify_on_drop::is_dropped());
|
||||
drop(clone2);
|
||||
assert_eq!(false, notify_on_drop::is_dropped());
|
||||
assert!(!notify_on_drop::is_dropped());
|
||||
drop(clone3);
|
||||
assert_eq!(false, notify_on_drop::is_dropped());
|
||||
assert!(!notify_on_drop::is_dropped());
|
||||
|
||||
drop(service);
|
||||
assert!(notify_on_drop::is_dropped());
|
||||
|
@ -8,10 +8,16 @@ use std::{
|
||||
};
|
||||
|
||||
use actix_rt::task::{spawn_blocking, JoinHandle};
|
||||
use brotli2::write::BrotliDecoder;
|
||||
use bytes::Bytes;
|
||||
use flate2::write::{GzDecoder, ZlibDecoder};
|
||||
use futures_core::{ready, Stream};
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
use brotli2::write::BrotliDecoder;
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
use flate2::write::{GzDecoder, ZlibDecoder};
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
use zstd::stream::write::Decoder as ZstdDecoder;
|
||||
|
||||
use crate::{
|
||||
@ -37,15 +43,19 @@ where
|
||||
#[inline]
|
||||
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()),
|
||||
))),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
|
||||
ZlibDecoder::new(Writer::new()),
|
||||
))),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(
|
||||
GzDecoder::new(Writer::new()),
|
||||
))),
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentEncoding::Zstd => Some(ContentDecoder::Zstd(Box::new(
|
||||
ZstdDecoder::new(Writer::new()).expect(
|
||||
"Failed to create zstd decoder. This is a bug. \
|
||||
@ -148,17 +158,22 @@ where
|
||||
}
|
||||
|
||||
enum ContentDecoder {
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
Deflate(Box<ZlibDecoder<Writer>>),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
Gzip(Box<GzDecoder<Writer>>),
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
Br(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")]
|
||||
Zstd(Box<ZstdDecoder<'static, Writer>>),
|
||||
}
|
||||
|
||||
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() {
|
||||
Ok(()) => {
|
||||
let b = decoder.get_mut().take();
|
||||
@ -172,6 +187,7 @@ impl ContentDecoder {
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentDecoder::Gzip(ref mut decoder) => match decoder.try_finish() {
|
||||
Ok(_) => {
|
||||
let b = decoder.get_mut().take();
|
||||
@ -185,6 +201,7 @@ impl ContentDecoder {
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentDecoder::Deflate(ref mut decoder) => match decoder.try_finish() {
|
||||
Ok(_) => {
|
||||
let b = decoder.get_mut().take();
|
||||
@ -197,6 +214,7 @@ impl ContentDecoder {
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentDecoder::Zstd(ref mut decoder) => match decoder.flush() {
|
||||
Ok(_) => {
|
||||
let b = decoder.get_mut().take();
|
||||
@ -213,6 +231,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) {
|
||||
Ok(_) => {
|
||||
decoder.flush()?;
|
||||
@ -227,6 +246,7 @@ impl ContentDecoder {
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentDecoder::Gzip(ref mut decoder) => match decoder.write_all(&data) {
|
||||
Ok(_) => {
|
||||
decoder.flush()?;
|
||||
@ -241,6 +261,7 @@ impl ContentDecoder {
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentDecoder::Deflate(ref mut decoder) => match decoder.write_all(&data) {
|
||||
Ok(_) => {
|
||||
decoder.flush()?;
|
||||
@ -255,6 +276,7 @@ impl ContentDecoder {
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentDecoder::Zstd(ref mut decoder) => match decoder.write_all(&data) {
|
||||
Ok(_) => {
|
||||
decoder.flush()?;
|
||||
|
@ -9,12 +9,18 @@ use std::{
|
||||
};
|
||||
|
||||
use actix_rt::task::{spawn_blocking, JoinHandle};
|
||||
use brotli2::write::BrotliEncoder;
|
||||
use bytes::Bytes;
|
||||
use derive_more::Display;
|
||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
||||
use futures_core::ready;
|
||||
use pin_project::pin_project;
|
||||
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
use brotli2::write::BrotliEncoder;
|
||||
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
||||
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
use zstd::stream::write::Encoder as ZstdEncoder;
|
||||
|
||||
use crate::{
|
||||
@ -233,28 +239,36 @@ fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
|
||||
}
|
||||
|
||||
enum ContentEncoder {
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
Deflate(ZlibEncoder<Writer>),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
Gzip(GzEncoder<Writer>),
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
Br(BrotliEncoder<Writer>),
|
||||
// We need explicit 'static lifetime here because ZstdEncoder need lifetime
|
||||
// argument, and we use `spawn_blocking` in `Encoder::poll_next` that require `FnOnce() -> R + Send + 'static`
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
Zstd(ZstdEncoder<'static, Writer>),
|
||||
}
|
||||
|
||||
impl ContentEncoder {
|
||||
fn encoder(encoding: ContentEncoding) -> Option<Self> {
|
||||
match encoding {
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoding::Deflate => Some(ContentEncoder::Deflate(ZlibEncoder::new(
|
||||
Writer::new(),
|
||||
flate2::Compression::fast(),
|
||||
))),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoding::Gzip => Some(ContentEncoder::Gzip(GzEncoder::new(
|
||||
Writer::new(),
|
||||
flate2::Compression::fast(),
|
||||
))),
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoding::Br => {
|
||||
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
|
||||
}
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentEncoding::Zstd => {
|
||||
let encoder = ZstdEncoder::new(Writer::new(), 3).ok()?;
|
||||
Some(ContentEncoder::Zstd(encoder))
|
||||
@ -266,27 +280,35 @@ impl ContentEncoder {
|
||||
#[inline]
|
||||
pub(crate) fn take(&mut self) -> Bytes {
|
||||
match *self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Gzip(ref mut encoder) => encoder.get_mut().take(),
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentEncoder::Zstd(ref mut encoder) => encoder.get_mut().take(),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(self) -> Result<Bytes, io::Error> {
|
||||
match self {
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
ContentEncoder::Br(encoder) => match encoder.finish() {
|
||||
Ok(writer) => Ok(writer.buf.freeze()),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Gzip(encoder) => match encoder.finish() {
|
||||
Ok(writer) => Ok(writer.buf.freeze()),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Deflate(encoder) => match encoder.finish() {
|
||||
Ok(writer) => Ok(writer.buf.freeze()),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentEncoder::Zstd(encoder) => match encoder.finish() {
|
||||
Ok(writer) => Ok(writer.buf.freeze()),
|
||||
Err(err) => Err(err),
|
||||
@ -296,6 +318,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) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
@ -303,6 +326,7 @@ impl ContentEncoder {
|
||||
Err(err)
|
||||
}
|
||||
},
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Gzip(ref mut encoder) => match encoder.write_all(data) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
@ -310,6 +334,7 @@ impl ContentEncoder {
|
||||
Err(err)
|
||||
}
|
||||
},
|
||||
#[cfg(feature = "compress-gzip")]
|
||||
ContentEncoder::Deflate(ref mut encoder) => match encoder.write_all(data) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
@ -317,6 +342,7 @@ impl ContentEncoder {
|
||||
Err(err)
|
||||
}
|
||||
},
|
||||
#[cfg(feature = "compress-zstd")]
|
||||
ContentEncoder::Zstd(ref mut encoder) => match encoder.write_all(data) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
|
@ -125,7 +125,7 @@ impl fmt::Display for Error {
|
||||
|
||||
impl StdError for Error {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
self.inner.cause.as_ref().map(|err| err.as_ref())
|
||||
self.inner.cause.as_ref().map(Box::as_ref)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@ pub(crate) trait MessageType: Sized {
|
||||
}
|
||||
// transfer-encoding
|
||||
header::TRANSFER_ENCODING => {
|
||||
if let Ok(s) = value.to_str().map(|s| s.trim()) {
|
||||
if let Ok(s) = value.to_str().map(str::trim) {
|
||||
chunked = s.eq_ignore_ascii_case("chunked");
|
||||
} else {
|
||||
return Err(ParseError::Header);
|
||||
@ -110,7 +110,7 @@ pub(crate) trait MessageType: Sized {
|
||||
}
|
||||
// connection keep-alive state
|
||||
header::CONNECTION => {
|
||||
ka = if let Ok(conn) = value.to_str().map(|conn| conn.trim()) {
|
||||
ka = if let Ok(conn) = value.to_str().map(str::trim) {
|
||||
if conn.eq_ignore_ascii_case("keep-alive") {
|
||||
Some(ConnectionType::KeepAlive)
|
||||
} else if conn.eq_ignore_ascii_case("close") {
|
||||
@ -125,7 +125,7 @@ pub(crate) trait MessageType: Sized {
|
||||
};
|
||||
}
|
||||
header::UPGRADE => {
|
||||
if let Ok(val) = value.to_str().map(|val| val.trim()) {
|
||||
if let Ok(val) = value.to_str().map(str::trim) {
|
||||
if val.eq_ignore_ascii_case("websocket") {
|
||||
has_upgrade_websocket = true;
|
||||
}
|
||||
|
@ -515,14 +515,13 @@ where
|
||||
cx: &mut Context<'_>,
|
||||
) -> Result<(), DispatchError> {
|
||||
// Handle `EXPECT: 100-Continue` header
|
||||
let mut this = self.as_mut().project();
|
||||
if req.head().expect() {
|
||||
// set dispatcher state so the future is pinned.
|
||||
let mut this = self.as_mut().project();
|
||||
let task = this.flow.expect.call(req);
|
||||
this.state.set(State::ExpectCall(task));
|
||||
} else {
|
||||
// the same as above.
|
||||
let mut this = self.as_mut().project();
|
||||
let task = this.flow.service.call(req);
|
||||
this.state.set(State::ServiceCall(task));
|
||||
};
|
||||
|
@ -186,8 +186,7 @@ impl Inner {
|
||||
if self
|
||||
.task
|
||||
.as_ref()
|
||||
.map(|w| !cx.waker().will_wake(w))
|
||||
.unwrap_or(true)
|
||||
.map_or(true, |w| !cx.waker().will_wake(w))
|
||||
{
|
||||
self.task = Some(cx.waker().clone());
|
||||
}
|
||||
@ -199,8 +198,7 @@ impl Inner {
|
||||
if self
|
||||
.io_task
|
||||
.as_ref()
|
||||
.map(|w| !cx.waker().will_wake(w))
|
||||
.unwrap_or(true)
|
||||
.map_or(true, |w| !cx.waker().will_wake(w))
|
||||
{
|
||||
self.io_task = Some(cx.waker().clone());
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ impl HeaderMap {
|
||||
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
||||
/// ```
|
||||
pub fn get(&self, key: impl AsHeaderName) -> Option<&HeaderValue> {
|
||||
self.get_value(key).map(|val| val.first())
|
||||
self.get_value(key).map(Value::first)
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the _first_ value associated a header name.
|
||||
@ -280,8 +280,8 @@ impl HeaderMap {
|
||||
/// ```
|
||||
pub fn get_mut(&mut self, key: impl AsHeaderName) -> Option<&mut HeaderValue> {
|
||||
match key.try_as_name(super::as_name::Seal).ok()? {
|
||||
Cow::Borrowed(name) => self.inner.get_mut(name).map(|v| v.first_mut()),
|
||||
Cow::Owned(name) => self.inner.get_mut(&name).map(|v| v.first_mut()),
|
||||
Cow::Borrowed(name) => self.inner.get_mut(name).map(Value::first_mut),
|
||||
Cow::Owned(name) => self.inner.get_mut(&name).map(Value::first_mut),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
//! HTTP primitives for the Actix ecosystem.
|
||||
//!
|
||||
//! ## Crate Features
|
||||
//! | Feature | Functionality |
|
||||
//! | ---------------- | ----------------------------------------------------- |
|
||||
//! | `openssl` | TLS support via [OpenSSL]. |
|
||||
//! | `rustls` | TLS support via [rustls]. |
|
||||
//! | `compress` | Payload compression support. (Deflate, Gzip & Brotli) |
|
||||
//! | `trust-dns` | Use [trust-dns] as the client DNS resolver. |
|
||||
//! | Feature | Functionality |
|
||||
//! | ------------------- | ------------------------------------------- |
|
||||
//! | `openssl` | TLS support via [OpenSSL]. |
|
||||
//! | `rustls` | TLS support via [rustls]. |
|
||||
//! | `compress-brotli` | Payload compression support: Brotli. |
|
||||
//! | `compress-gzip` | Payload compression support: Deflate, Gzip. |
|
||||
//! | `compress-zstd` | Payload compression support: Zstd. |
|
||||
//! | `trust-dns` | Use [trust-dns] as the client DNS resolver. |
|
||||
//!
|
||||
//! [OpenSSL]: https://crates.io/crates/openssl
|
||||
//! [rustls]: https://crates.io/crates/rustls
|
||||
@ -25,14 +27,12 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
pub mod body;
|
||||
mod builder;
|
||||
pub mod client;
|
||||
mod config;
|
||||
#[cfg(feature = "compress")]
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub mod encoding;
|
||||
mod extensions;
|
||||
pub mod header;
|
||||
|
@ -1,110 +0,0 @@
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! downcast_get_type_id {
|
||||
() => {
|
||||
/// A helper method to get the type ID of the type
|
||||
/// this trait is implemented on.
|
||||
/// This method is unsafe to *implement*, since `downcast_ref` relies
|
||||
/// on the returned `TypeId` to perform a cast.
|
||||
///
|
||||
/// Unfortunately, Rust has no notion of a trait method that is
|
||||
/// unsafe to implement (marking it as `unsafe` makes it unsafe
|
||||
/// to *call*). As a workaround, we require this method
|
||||
/// to return a private type along with the `TypeId`. This
|
||||
/// private type (`PrivateHelper`) has a private constructor,
|
||||
/// making it impossible for safe code to construct outside of
|
||||
/// this module. This ensures that safe code cannot violate
|
||||
/// type-safety by implementing this method.
|
||||
///
|
||||
/// We also take `PrivateHelper` as a parameter, to ensure that
|
||||
/// safe code cannot obtain a `PrivateHelper` instance by
|
||||
/// delegating to an existing implementation of `__private_get_type_id__`
|
||||
#[doc(hidden)]
|
||||
fn __private_get_type_id__(
|
||||
&self,
|
||||
_: PrivateHelper,
|
||||
) -> (std::any::TypeId, PrivateHelper)
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
(std::any::TypeId::of::<Self>(), PrivateHelper(()))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//Generate implementation for dyn $name
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! downcast {
|
||||
($name:ident) => {
|
||||
/// A struct with a private constructor, for use with
|
||||
/// `__private_get_type_id__`. Its single field is private,
|
||||
/// ensuring that it can only be constructed from this module
|
||||
#[doc(hidden)]
|
||||
pub struct PrivateHelper(());
|
||||
|
||||
impl dyn $name + 'static {
|
||||
/// Downcasts generic body to a specific type.
|
||||
pub fn downcast_ref<T: $name + 'static>(&self) -> Option<&T> {
|
||||
if self.__private_get_type_id__(PrivateHelper(())).0
|
||||
== std::any::TypeId::of::<T>()
|
||||
{
|
||||
// SAFETY: external crates cannot override the default
|
||||
// implementation of `__private_get_type_id__`, since
|
||||
// it requires returning a private type. We can therefore
|
||||
// rely on the returned `TypeId`, which ensures that this
|
||||
// case is correct.
|
||||
unsafe { Some(&*(self as *const dyn $name as *const T)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Downcasts a generic body to a mutable specific type.
|
||||
pub fn downcast_mut<T: $name + 'static>(&mut self) -> Option<&mut T> {
|
||||
if self.__private_get_type_id__(PrivateHelper(())).0
|
||||
== std::any::TypeId::of::<T>()
|
||||
{
|
||||
// SAFETY: external crates cannot override the default
|
||||
// implementation of `__private_get_type_id__`, since
|
||||
// it requires returning a private type. We can therefore
|
||||
// rely on the returned `TypeId`, which ensures that this
|
||||
// case is correct.
|
||||
unsafe {
|
||||
Some(&mut *(self as *const dyn $name as *const T as *mut T))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::upper_case_acronyms)]
|
||||
|
||||
trait MB {
|
||||
downcast_get_type_id!();
|
||||
}
|
||||
|
||||
downcast!(MB);
|
||||
|
||||
impl MB for String {}
|
||||
impl MB for () {}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_any_casting() {
|
||||
let mut body = String::from("hello cast");
|
||||
let resp_body: &mut dyn MB = &mut body;
|
||||
let body = resp_body.downcast_ref::<String>().unwrap();
|
||||
assert_eq!(body, "hello cast");
|
||||
let body = &mut resp_body.downcast_mut::<String>().unwrap();
|
||||
body.push('!');
|
||||
let body = resp_body.downcast_ref::<String>().unwrap();
|
||||
assert_eq!(body, "hello cast!");
|
||||
let not_body = resp_body.downcast_ref::<()>();
|
||||
assert!(not_body.is_none());
|
||||
}
|
||||
}
|
@ -152,15 +152,16 @@ impl RequestHead {
|
||||
|
||||
/// Connection upgrade status
|
||||
pub fn upgrade(&self) -> bool {
|
||||
if let Some(hdr) = self.headers().get(header::CONNECTION) {
|
||||
if let Ok(s) = hdr.to_str() {
|
||||
s.to_ascii_lowercase().contains("upgrade")
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
self.headers()
|
||||
.get(header::CONNECTION)
|
||||
.map(|hdr| {
|
||||
if let Ok(s) = hdr.to_str() {
|
||||
s.to_ascii_lowercase().contains("upgrade")
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -308,13 +309,11 @@ impl ResponseHead {
|
||||
/// Get custom reason for the response
|
||||
#[inline]
|
||||
pub fn reason(&self) -> &str {
|
||||
if let Some(reason) = self.reason {
|
||||
reason
|
||||
} else {
|
||||
self.reason.unwrap_or_else(|| {
|
||||
self.status
|
||||
.canonical_reason()
|
||||
.unwrap_or("<unknown status code>")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -356,7 +355,7 @@ pub struct Message<T: Head> {
|
||||
impl<T: Head> Message<T> {
|
||||
/// Get new message from the pool of objects
|
||||
pub fn new() -> Self {
|
||||
T::with_pool(|p| p.get_message())
|
||||
T::with_pool(MessagePool::get_message)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ name = "actix_multipart"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false }
|
||||
actix-utils = "3.0.0"
|
||||
|
||||
bytes = "1"
|
||||
@ -31,6 +31,6 @@ twoway = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-http = "3.0.0-beta.8"
|
||||
tokio = { version = "1", features = ["sync"] }
|
||||
tokio-stream = "0.1"
|
||||
|
@ -9,9 +9,9 @@
|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.5)
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-multipart)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||
|
@ -3,6 +3,10 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 0.1.0-beta.3 - 2021-06-20
|
||||
* No significant changes from `0.1.0-beta.2`.
|
||||
|
||||
|
||||
## 0.1.0-beta.2 - 2021-04-17
|
||||
* No significant changes from `0.1.0-beta.1`.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-test"
|
||||
version = "0.1.0-beta.2"
|
||||
version = "0.1.0-beta.3"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"Rob Ede <robjtede@icloud.com>",
|
||||
@ -20,13 +20,13 @@ openssl = ["tls-openssl", "actix-http/openssl"]
|
||||
|
||||
[dependencies]
|
||||
actix-codec = "0.4.0"
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-http = "3.0.0-beta.8"
|
||||
actix-http-test = { version = "3.0.0-beta.4", features = [] }
|
||||
actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
actix-web = { version = "4.0.0-beta.7", default-features = false, features = ["cookies"] }
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["cookies"] }
|
||||
actix-rt = "2.1"
|
||||
awc = { version = "3.0.0-beta.6", default-features = false, features = ["cookies"] }
|
||||
awc = { version = "3.0.0-beta.7", 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,6 +3,12 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 4.0.0-beta.6 - 2021-06-26
|
||||
* Update `actix` to `0.12`. [#2277]
|
||||
|
||||
[#2277]: https://github.com/actix/actix-web/pull/2277
|
||||
|
||||
|
||||
## 4.0.0-beta.5 - 2021-06-17
|
||||
* No notable changes.
|
||||
|
||||
|
@ -1,13 +1,11 @@
|
||||
[package]
|
||||
name = "actix-web-actors"
|
||||
version = "4.0.0-beta.5"
|
||||
version = "4.0.0-beta.6"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix actors support for Actix Web"
|
||||
readme = "README.md"
|
||||
keywords = ["actix", "http", "web", "framework", "async"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-web.git"
|
||||
documentation = "https://docs.rs/actix-web-actors/"
|
||||
repository = "https://github.com/actix/actix-web"
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2018"
|
||||
|
||||
@ -16,10 +14,10 @@ name = "actix_web_actors"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix = { version = "0.11.0-beta.3", default-features = false }
|
||||
actix = { version = "0.12.0", default-features = false }
|
||||
actix-codec = "0.4.0"
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
||||
actix-http = "3.0.0-beta.8"
|
||||
actix-web = { version = "4.0.0-beta.8", default-features = false }
|
||||
|
||||
bytes = "1"
|
||||
bytestring = "1"
|
||||
@ -29,8 +27,8 @@ tokio = { version = "1", features = ["sync"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1.0-beta.2"
|
||||
actix-test = "0.1.0-beta.3"
|
||||
|
||||
awc = { version = "3.0.0-beta.6", default-features = false }
|
||||
awc = { version = "3.0.0-beta.7", default-features = false }
|
||||
env_logger = "0.8"
|
||||
futures-util = { version = "0.3.7", default-features = false }
|
||||
|
@ -3,16 +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.5)
|
||||
[](https://docs.rs/actix-web-actors/4.0.0-beta.6)
|
||||
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-web-actors/4.0.0-beta.5)
|
||||
[](https://deps.rs/crate/actix-web-actors/4.0.0-beta.6)
|
||||
[](https://crates.io/crates/actix-web-actors)
|
||||
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-web-actors)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum supported Rust version: 1.46 or later
|
||||
|
@ -20,9 +20,9 @@ proc-macro2 = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-test = "0.1.0-beta.2"
|
||||
actix-test = "0.1.0-beta.3"
|
||||
actix-utils = "3.0.0"
|
||||
actix-web = "4.0.0-beta.7"
|
||||
actix-web = "4.0.0-beta.8"
|
||||
|
||||
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||
trybuild = "1"
|
||||
|
@ -9,12 +9,11 @@
|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-web-codegen/0.5.0-beta.3)
|
||||
[](https://crates.io/crates/actix-web-codegen)
|
||||
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/actix-web-codegen)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum supported Rust version: 1.46 or later.
|
||||
|
||||
## Compile Testing
|
||||
|
@ -6,7 +6,7 @@ use std::convert::TryFrom;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
|
||||
use syn::{parse_macro_input, AttributeArgs, Ident, NestedMeta};
|
||||
use syn::{parse_macro_input, AttributeArgs, Ident, LitStr, NestedMeta};
|
||||
|
||||
enum ResourceType {
|
||||
Async,
|
||||
@ -227,8 +227,7 @@ impl Route {
|
||||
format!(
|
||||
r#"invalid service definition, expected #[{}("<some path>")]"#,
|
||||
method
|
||||
.map(|it| it.as_str())
|
||||
.unwrap_or("route")
|
||||
.map_or("route", |it| it.as_str())
|
||||
.to_ascii_lowercase()
|
||||
),
|
||||
));
|
||||
@ -298,7 +297,7 @@ impl ToTokens for Route {
|
||||
} = self;
|
||||
let resource_name = resource_name
|
||||
.as_ref()
|
||||
.map_or_else(|| name.to_string(), |n| n.value());
|
||||
.map_or_else(|| name.to_string(), LitStr::value);
|
||||
let method_guards = {
|
||||
let mut others = methods.iter();
|
||||
// unwrapping since length is checked to be at least one
|
||||
|
@ -3,6 +3,13 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.7 - 2021-06-26
|
||||
### Changed
|
||||
* Change compression algorithm features flags. [#2250]
|
||||
|
||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||
|
||||
|
||||
## 3.0.0-beta.6 - 2021-06-17
|
||||
* No significant changes since 3.0.0-beta.5.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "awc"
|
||||
version = "3.0.0-beta.6"
|
||||
version = "3.0.0-beta.7"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"fakeshadow <24548779@qq.com>",
|
||||
@ -24,10 +24,10 @@ path = "src/lib.rs"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
# features that docs.rs will build with
|
||||
features = ["openssl", "rustls", "compress", "cookies"]
|
||||
features = ["openssl", "rustls", "compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
|
||||
|
||||
[features]
|
||||
default = ["compress", "cookies"]
|
||||
default = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
|
||||
|
||||
# openssl
|
||||
openssl = ["tls-openssl", "actix-http/openssl"]
|
||||
@ -35,8 +35,12 @@ openssl = ["tls-openssl", "actix-http/openssl"]
|
||||
# rustls
|
||||
rustls = ["tls-rustls", "actix-http/rustls"]
|
||||
|
||||
# content-encoding support
|
||||
compress = ["actix-http/compress"]
|
||||
# Brotli algorithm content-encoding support
|
||||
compress-brotli = ["actix-http/compress-brotli", "__compress"]
|
||||
# Gzip and deflate algorithms content-encoding support
|
||||
compress-gzip = ["actix-http/compress-gzip", "__compress"]
|
||||
# Zstd algorithm content-encoding support
|
||||
compress-zstd = ["actix-http/compress-zstd", "__compress"]
|
||||
|
||||
# cookie parsing and cookie jar
|
||||
cookies = ["cookie"]
|
||||
@ -44,14 +48,19 @@ cookies = ["cookie"]
|
||||
# trust-dns as dns resolver
|
||||
trust-dns = ["actix-http/trust-dns"]
|
||||
|
||||
# Internal (PRIVATE!) features used to aid testing and cheking feature status.
|
||||
# Don't rely on these whatsoever. They may disappear at anytime.
|
||||
__compress = []
|
||||
|
||||
[dependencies]
|
||||
actix-codec = "0.4.0"
|
||||
actix-service = "2.0.0"
|
||||
actix-http = "3.0.0-beta.7"
|
||||
actix-http = "3.0.0-beta.8"
|
||||
actix-rt = { version = "2.1", default-features = false }
|
||||
|
||||
base64 = "0.13"
|
||||
bytes = "1"
|
||||
cfg-if = "1"
|
||||
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
|
||||
derive_more = "0.99.5"
|
||||
futures-core = { version = "0.3.7", default-features = false }
|
||||
@ -68,13 +77,13 @@ tls-openssl = { version = "0.10.9", package = "openssl", optional = true }
|
||||
tls-rustls = { version = "0.19.0", package = "rustls", optional = true, features = ["dangerous_configuration"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-web = { version = "4.0.0-beta.7", features = ["openssl"] }
|
||||
actix-http = { version = "3.0.0-beta.7", features = ["openssl"] }
|
||||
actix-web = { version = "4.0.0-beta.8", features = ["openssl"] }
|
||||
actix-http = { version = "3.0.0-beta.8", features = ["openssl"] }
|
||||
actix-http-test = { version = "3.0.0-beta.4", features = ["openssl"] }
|
||||
actix-utils = "3.0.0"
|
||||
actix-server = "2.0.0-beta.3"
|
||||
actix-tls = { version = "3.0.0-beta.5", features = ["openssl", "rustls"] }
|
||||
actix-test = { version = "0.1.0-beta.2", features = ["openssl", "rustls"] }
|
||||
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
||||
|
||||
brotli2 = "0.3.2"
|
||||
env_logger = "0.8"
|
||||
|
@ -3,16 +3,15 @@
|
||||
> Async HTTP and WebSocket client library.
|
||||
|
||||
[](https://crates.io/crates/awc)
|
||||
[](https://docs.rs/awc/3.0.0-beta.6)
|
||||
[](https://docs.rs/awc/3.0.0-beta.7)
|
||||

|
||||
[](https://deps.rs/crate/awc/3.0.0-beta.6)
|
||||
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://deps.rs/crate/awc/3.0.0-beta.7)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
||||
- [API Documentation](https://docs.rs/awc)
|
||||
- [Example Project](https://github.com/actix/examples/tree/HEAD/security/awc_https)
|
||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||
|
||||
## Example
|
||||
|
@ -2,17 +2,19 @@ use std::error::Error as StdError;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> Result<(), Box<dyn StdError>> {
|
||||
std::env::set_var("RUST_LOG", "actix_http=trace");
|
||||
std::env::set_var("RUST_LOG", "client=trace,awc=trace,actix_http=trace");
|
||||
env_logger::init();
|
||||
|
||||
let client = awc::Client::new();
|
||||
|
||||
// Create request builder, configure request and send
|
||||
let mut response = client
|
||||
let request = client
|
||||
.get("https://www.rust-lang.org/")
|
||||
.append_header(("User-Agent", "Actix-web"))
|
||||
.send()
|
||||
.await?;
|
||||
.append_header(("User-Agent", "Actix-web"));
|
||||
|
||||
println!("Request: {:?}", request);
|
||||
|
||||
let mut response = request.send().await?;
|
||||
|
||||
// server http response
|
||||
println!("Response: {:?}", response);
|
||||
|
@ -1,7 +1,6 @@
|
||||
//! `awc` is a HTTP and WebSocket client library built on the Actix ecosystem.
|
||||
//!
|
||||
//! ## Making a GET request
|
||||
//!
|
||||
//! # Making a GET request
|
||||
//! ```no_run
|
||||
//! # #[actix_rt::main]
|
||||
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
||||
@ -16,10 +15,8 @@
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ## Making POST requests
|
||||
//!
|
||||
//! ### Raw body contents
|
||||
//!
|
||||
//! # Making POST requests
|
||||
//! ## Raw body contents
|
||||
//! ```no_run
|
||||
//! # #[actix_rt::main]
|
||||
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
||||
@ -31,8 +28,7 @@
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Forms
|
||||
//!
|
||||
//! ## Forms
|
||||
//! ```no_run
|
||||
//! # #[actix_rt::main]
|
||||
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
||||
@ -46,8 +42,7 @@
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### JSON
|
||||
//!
|
||||
//! ## JSON
|
||||
//! ```no_run
|
||||
//! # #[actix_rt::main]
|
||||
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
||||
@ -64,8 +59,24 @@
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ## WebSocket support
|
||||
//! # Response Compression
|
||||
//! All [official][iana-encodings] and common content encoding codecs are supported, optionally.
|
||||
//!
|
||||
//! The `Accept-Encoding` header will automatically be populated with enabled codecs and added to
|
||||
//! outgoing requests, allowing servers to select their `Content-Encoding` accordingly.
|
||||
//!
|
||||
//! Feature flags enable these codecs according to the table below. By default, all `compress-*`
|
||||
//! features are enabled.
|
||||
//!
|
||||
//! | Feature | Codecs |
|
||||
//! | ----------------- | ------------- |
|
||||
//! | `compress-brotli` | brotli |
|
||||
//! | `compress-gzip` | gzip, deflate |
|
||||
//! | `compress-zstd` | zstd |
|
||||
//!
|
||||
//! [iana-encodings]: https://www.iana.org/assignments/http-parameters/http-parameters.xhtml#content-coding
|
||||
//!
|
||||
//! # WebSocket support
|
||||
//! ```no_run
|
||||
//! # #[actix_rt::main]
|
||||
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -128,6 +139,9 @@ pub use self::sender::SendClientRequest;
|
||||
|
||||
/// An asynchronous HTTP and WebSocket client.
|
||||
///
|
||||
/// You should take care to create, at most, one `Client` per thread. Otherwise, expect higher CPU
|
||||
/// and memory usage.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use awc::Client;
|
||||
@ -136,10 +150,10 @@ pub use self::sender::SendClientRequest;
|
||||
/// async fn main() {
|
||||
/// let mut client = Client::default();
|
||||
///
|
||||
/// let res = client.get("http://www.rust-lang.org") // <- Create request builder
|
||||
/// .insert_header(("User-Agent", "Actix-web"))
|
||||
/// .send() // <- Send HTTP request
|
||||
/// .await; // <- send request and wait for response
|
||||
/// let res = client.get("http://www.rust-lang.org")
|
||||
/// .insert_header(("User-Agent", "my-app/1.2"))
|
||||
/// .send()
|
||||
/// .await;
|
||||
///
|
||||
/// println!("Response: {:?}", res);
|
||||
/// }
|
||||
|
@ -8,7 +8,7 @@ use actix_http::{
|
||||
body::Body,
|
||||
http::{
|
||||
header::{self, IntoHeaderPair},
|
||||
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
|
||||
ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
|
||||
},
|
||||
RequestHead,
|
||||
};
|
||||
@ -22,11 +22,6 @@ use crate::{
|
||||
ClientConfig,
|
||||
};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
const HTTPS_ENCODING: &str = "br, gzip, deflate";
|
||||
#[cfg(not(feature = "compress"))]
|
||||
const HTTPS_ENCODING: &str = "br";
|
||||
|
||||
/// An HTTP Client request builder
|
||||
///
|
||||
/// This type can be used to construct an instance of `ClientRequest` through a
|
||||
@ -480,22 +475,44 @@ impl ClientRequest {
|
||||
|
||||
let mut slf = self;
|
||||
|
||||
// Set Accept-Encoding HTTP header depending on enabled feature.
|
||||
// If decompress is not ask, then we are not able to find which encoding is
|
||||
// supported, so we cannot guess Accept-Encoding HTTP header.
|
||||
if slf.response_decompress {
|
||||
let https = slf
|
||||
.head
|
||||
.uri
|
||||
.scheme()
|
||||
.map(|s| s == &uri::Scheme::HTTPS)
|
||||
.unwrap_or(true);
|
||||
// Set Accept-Encoding with compression algorithm awc is built with.
|
||||
#[allow(clippy::vec_init_then_push)]
|
||||
#[cfg(feature = "__compress")]
|
||||
let accept_encoding = {
|
||||
let mut encoding = vec![];
|
||||
|
||||
if https {
|
||||
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, HTTPS_ENCODING));
|
||||
} else {
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "compress-brotli")]
|
||||
{
|
||||
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, "gzip, deflate"));
|
||||
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"
|
||||
);
|
||||
|
||||
encoding.join(", ")
|
||||
};
|
||||
|
||||
// Otherwise tell the server, we do not support any compression algorithm.
|
||||
// So we clearly indicate that we do want identity encoding.
|
||||
#[cfg(not(feature = "__compress"))]
|
||||
let accept_encoding = "identity";
|
||||
|
||||
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, accept_encoding));
|
||||
}
|
||||
|
||||
Ok(slf)
|
||||
|
@ -22,7 +22,7 @@ use derive_more::From;
|
||||
use futures_core::Stream;
|
||||
use serde::Serialize;
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
use actix_http::{encoding::Decoder, http::header::ContentEncoding, Payload, PayloadStream};
|
||||
|
||||
use crate::{
|
||||
@ -91,7 +91,7 @@ impl SendClientRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
impl Future for SendClientRequest {
|
||||
type Output = Result<ClientResponse<Decoder<Payload<PayloadStream>>>, SendRequestError>;
|
||||
|
||||
@ -131,7 +131,7 @@ impl Future for SendClientRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "compress"))]
|
||||
#[cfg(not(feature = "__compress"))]
|
||||
impl Future for SendClientRequest {
|
||||
type Output = Result<ClientResponse, SendRequestError>;
|
||||
|
||||
|
@ -517,7 +517,7 @@ mod tests {
|
||||
"test-origin"
|
||||
);
|
||||
assert_eq!(req.max_size, 100);
|
||||
assert_eq!(req.server_mode, true);
|
||||
assert!(req.server_mode);
|
||||
assert_eq!(req.protocols, Some("v1,v2".to_string()));
|
||||
assert_eq!(
|
||||
req.head.headers.get(header::CONTENT_TYPE).unwrap(),
|
||||
|
@ -1,6 +1,5 @@
|
||||
use std::{future::Future, time::Instant};
|
||||
|
||||
use actix_http::Response;
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::test::TestRequest;
|
||||
@ -24,11 +23,11 @@ struct StringResponder(String);
|
||||
|
||||
impl FutureResponder for StringResponder {
|
||||
type Error = Error;
|
||||
type Future = Ready<Result<Response, Self::Error>>;
|
||||
type Future = Ready<Result<HttpResponse, Self::Error>>;
|
||||
|
||||
fn future_respond_to(self, _: &HttpRequest) -> Self::Future {
|
||||
// this is default builder for string response in both new and old responder trait.
|
||||
ready(Ok(Response::build(StatusCode::OK)
|
||||
ready(Ok(HttpResponse::build(StatusCode::OK)
|
||||
.content_type("text/plain; charset=utf-8")
|
||||
.body(self.0)))
|
||||
}
|
||||
@ -37,7 +36,7 @@ impl FutureResponder for StringResponder {
|
||||
impl<T> FutureResponder for OptionResponder<T>
|
||||
where
|
||||
T: FutureResponder,
|
||||
T::Future: Future<Output = Result<Response, Error>>,
|
||||
T::Future: Future<Output = Result<HttpResponse, Error>>,
|
||||
{
|
||||
type Error = Error;
|
||||
type Future = Either<T::Future, Ready<Result<HttpResponse, Self::Error>>>;
|
||||
@ -52,7 +51,7 @@ where
|
||||
|
||||
impl Responder for StringResponder {
|
||||
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
|
||||
Response::build(StatusCode::OK)
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
.content_type("text/plain; charset=utf-8")
|
||||
.body(self.0)
|
||||
}
|
||||
@ -62,7 +61,7 @@ impl<T: Responder> Responder for OptionResponder<T> {
|
||||
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
||||
match self.0 {
|
||||
Some(t) => t.respond_to(req),
|
||||
None => Response::from_error(error::ErrorInternalServerError("err")),
|
||||
None => HttpResponse::from_error(error::ErrorInternalServerError("err")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,9 +51,8 @@ where
|
||||
fut.await.unwrap();
|
||||
}
|
||||
});
|
||||
let elapsed = start.elapsed();
|
||||
// check that at least first request succeeded
|
||||
elapsed
|
||||
start.elapsed()
|
||||
})
|
||||
});
|
||||
}
|
||||
@ -93,9 +92,8 @@ fn async_web_service(c: &mut Criterion) {
|
||||
fut.await.unwrap();
|
||||
}
|
||||
});
|
||||
let elapsed = start.elapsed();
|
||||
// check that at least first request succeeded
|
||||
elapsed
|
||||
start.elapsed()
|
||||
})
|
||||
});
|
||||
}
|
||||
|
107
src/app.rs
107
src/app.rs
@ -43,13 +43,14 @@ impl App<AppEntry, Body> {
|
||||
/// Create application builder. Application can be configured with a builder-like pattern.
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
let fref = Rc::new(RefCell::new(None));
|
||||
let factory_ref = Rc::new(RefCell::new(None));
|
||||
|
||||
App {
|
||||
endpoint: AppEntry::new(fref.clone()),
|
||||
endpoint: AppEntry::new(factory_ref.clone()),
|
||||
data_factories: Vec::new(),
|
||||
services: Vec::new(),
|
||||
default: None,
|
||||
factory_ref: fref,
|
||||
factory_ref,
|
||||
external: Vec::new(),
|
||||
extensions: Extensions::new(),
|
||||
_phantom: PhantomData,
|
||||
@ -68,43 +69,83 @@ where
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
/// Set application data. Application data could be accessed
|
||||
/// by using `Data<T>` extractor where `T` is data type.
|
||||
/// Set application (root level) data.
|
||||
///
|
||||
/// **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 shared object should be used, e.g. `Arc`. Internally `Data` type
|
||||
/// uses `Arc` so data could be created outside of app factory and clones could
|
||||
/// be stored via `App::app_data()` method.
|
||||
/// Application data stored with `App::app_data()` method is available through the
|
||||
/// [`HttpRequest::app_data`](crate::HttpRequest::app_data) method at runtime.
|
||||
///
|
||||
/// # [`Data<T>`]
|
||||
/// Any [`Data<T>`] type added here can utilize it's extractor implementation in handlers.
|
||||
/// Types not wrapped in `Data<T>` cannot use this extractor. See [its docs](Data<T>) for more
|
||||
/// about its usage and patterns.
|
||||
///
|
||||
/// ```
|
||||
/// use std::cell::Cell;
|
||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
||||
/// use actix_web::{web, App, HttpRequest, HttpResponse, Responder};
|
||||
///
|
||||
/// struct MyData {
|
||||
/// counter: Cell<usize>,
|
||||
/// count: std::cell::Cell<usize>,
|
||||
/// }
|
||||
///
|
||||
/// async fn index(data: web::Data<MyData>) -> impl Responder {
|
||||
/// data.counter.set(data.counter.get() + 1);
|
||||
/// HttpResponse::Ok()
|
||||
/// async fn handler(req: HttpRequest, counter: web::Data<MyData>) -> impl Responder {
|
||||
/// // note this cannot use the Data<T> extractor because it was not added with it
|
||||
/// let incr = *req.app_data::<usize>().unwrap();
|
||||
/// assert_eq!(incr, 3);
|
||||
///
|
||||
/// // update counter using other value from app data
|
||||
/// counter.count.set(counter.count.get() + incr);
|
||||
///
|
||||
/// HttpResponse::Ok().body(counter.count.get().to_string())
|
||||
/// }
|
||||
///
|
||||
/// let app = App::new()
|
||||
/// .data(MyData{ counter: Cell::new(0) })
|
||||
/// .service(
|
||||
/// web::resource("/index.html").route(
|
||||
/// web::get().to(index)));
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/")
|
||||
/// .app_data(3usize)
|
||||
/// .app_data(web::Data::new(MyData { count: Default::default() }))
|
||||
/// .route(web::get().to(handler))
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// # Shared Mutable State
|
||||
/// [`HttpServer::new`](crate::HttpServer::new) accepts an application factory rather than an
|
||||
/// application instance; the factory closure is called on each worker thread independently.
|
||||
/// Therefore, if you want to share a data object between different workers, a shareable object
|
||||
/// needs to be created first, outside the `HttpServer::new` closure and cloned into it.
|
||||
/// [`Data<T>`] is an example of such a sharable object.
|
||||
///
|
||||
/// ```ignore
|
||||
/// let counter = web::Data::new(AppStateWithCounter {
|
||||
/// counter: Mutex::new(0),
|
||||
/// });
|
||||
///
|
||||
/// HttpServer::new(move || {
|
||||
/// // move counter object into the closure and clone for each worker
|
||||
///
|
||||
/// App::new()
|
||||
/// .app_data(counter.clone())
|
||||
/// .route("/", web::get().to(handler))
|
||||
/// })
|
||||
/// ```
|
||||
pub fn app_data<U: 'static>(mut self, ext: U) -> Self {
|
||||
self.extensions.insert(ext);
|
||||
self
|
||||
}
|
||||
|
||||
/// Add application (root) data after wrapping in `Data<T>`.
|
||||
///
|
||||
/// Deprecated in favor of [`app_data`](Self::app_data).
|
||||
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||
self.app_data(Data::new(data))
|
||||
}
|
||||
|
||||
/// Set application data factory. This function is
|
||||
/// similar to `.data()` but it accepts data factory. Data object get
|
||||
/// constructed asynchronously during application initialization.
|
||||
/// Add application data factory. This function is similar to `.data()` but it accepts a
|
||||
/// "data factory". Data values are constructed asynchronously during application
|
||||
/// initialization, before the server starts accepting requests.
|
||||
#[deprecated(
|
||||
since = "4.0.0",
|
||||
note = "Construct data value before starting server and use `.app_data(Data::new(val))` instead."
|
||||
)]
|
||||
pub fn data_factory<F, Out, D, E>(mut self, data: F) -> Self
|
||||
where
|
||||
F: Fn() -> Out + 'static,
|
||||
@ -133,18 +174,6 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Set application level arbitrary data item.
|
||||
///
|
||||
/// Application data stored with `App::app_data()` method is available
|
||||
/// via `HttpRequest::app_data()` method at runtime.
|
||||
///
|
||||
/// This method could be used for storing `Data<T>` as well, in that case
|
||||
/// data could be accessed by using `Data<T>` extractor.
|
||||
pub fn app_data<U: 'static>(mut self, ext: U) -> Self {
|
||||
self.extensions.insert(ext);
|
||||
self
|
||||
}
|
||||
|
||||
/// Run external configuration as part of the application building
|
||||
/// process
|
||||
///
|
||||
@ -518,6 +547,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_data_factory() {
|
||||
let srv = init_service(
|
||||
@ -541,6 +572,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_data_factory_errors() {
|
||||
let srv = try_init_service(
|
||||
|
@ -1,22 +1,22 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use actix_http::{Extensions, Request};
|
||||
use actix_router::{Path, ResourceDef, Router, Url};
|
||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||
use actix_service::{fn_service, Service, ServiceFactory};
|
||||
use actix_service::{
|
||||
boxed::{self, BoxService, BoxServiceFactory},
|
||||
fn_service, Service, ServiceFactory,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use futures_util::future::join_all;
|
||||
|
||||
use crate::data::FnDataFactory;
|
||||
use crate::error::Error;
|
||||
use crate::guard::Guard;
|
||||
use crate::request::{HttpRequest, HttpRequestPool};
|
||||
use crate::rmap::ResourceMap;
|
||||
use crate::service::{AppServiceFactory, ServiceRequest, ServiceResponse};
|
||||
use crate::{
|
||||
config::{AppConfig, AppService},
|
||||
HttpResponse,
|
||||
data::FnDataFactory,
|
||||
guard::Guard,
|
||||
request::{HttpRequest, HttpRequestPool},
|
||||
rmap::ResourceMap,
|
||||
service::{AppServiceFactory, ServiceRequest, ServiceResponse},
|
||||
Error, HttpResponse,
|
||||
};
|
||||
|
||||
type Guards = Vec<Box<dyn Guard>>;
|
||||
@ -75,7 +75,7 @@ where
|
||||
let mut config = AppService::new(config, default.clone());
|
||||
|
||||
// register services
|
||||
std::mem::take(&mut *self.services.borrow_mut())
|
||||
mem::take(&mut *self.services.borrow_mut())
|
||||
.into_iter()
|
||||
.for_each(|mut srv| srv.register(&mut config));
|
||||
|
||||
@ -98,7 +98,7 @@ where
|
||||
});
|
||||
|
||||
// external resources
|
||||
for mut rdef in std::mem::take(&mut *self.external.borrow_mut()) {
|
||||
for mut rdef in mem::take(&mut *self.external.borrow_mut()) {
|
||||
rmap.add(&mut rdef, None);
|
||||
}
|
||||
|
||||
@ -131,9 +131,9 @@ where
|
||||
let service = endpoint_fut.await?;
|
||||
|
||||
// populate app data container from (async) data factories.
|
||||
async_data_factories.iter().for_each(|factory| {
|
||||
for factory in &async_data_factories {
|
||||
factory.create(&mut app_data);
|
||||
});
|
||||
}
|
||||
|
||||
Ok(AppInitService {
|
||||
service,
|
||||
@ -144,7 +144,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Service that takes a [`Request`] and delegates to a service that take a [`ServiceRequest`].
|
||||
/// The [`Service`] that is passed to `actix-http`'s server builder.
|
||||
///
|
||||
/// Wraps a service receiving a [`ServiceRequest`] into one receiving a [`Request`].
|
||||
pub struct AppInitService<T, B>
|
||||
where
|
||||
T: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
@ -275,6 +277,7 @@ impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Actix Web router default entry point.
|
||||
pub struct AppRouting {
|
||||
router: Router<HttpService, Guards>,
|
||||
default: HttpService,
|
||||
@ -349,6 +352,8 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_drop_data() {
|
||||
let data = Arc::new(AtomicBool::new(false));
|
||||
|
@ -62,6 +62,8 @@ impl AppService {
|
||||
(self.config, self.services)
|
||||
}
|
||||
|
||||
/// Clones inner config and default service, returning new `AppService` with empty service list
|
||||
/// marked as non-root.
|
||||
pub(crate) fn clone_config(&self) -> Self {
|
||||
AppService {
|
||||
config: self.config.clone(),
|
||||
@ -71,12 +73,12 @@ impl AppService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Service configuration
|
||||
/// Returns reference to configuration.
|
||||
pub fn config(&self) -> &AppConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
/// Default resource
|
||||
/// Returns default handler factory.
|
||||
pub fn default_service(&self) -> Rc<HttpNewService> {
|
||||
self.default.clone()
|
||||
}
|
||||
@ -92,9 +94,9 @@ impl AppService {
|
||||
F: IntoServiceFactory<S, ServiceRequest>,
|
||||
S: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
Config = (),
|
||||
InitError = (),
|
||||
> + 'static,
|
||||
{
|
||||
@ -116,6 +118,7 @@ impl AppConfig {
|
||||
AppConfig { secure, host, addr }
|
||||
}
|
||||
|
||||
/// Needed in actix-test crate. Semver exempt.
|
||||
#[doc(hidden)]
|
||||
pub fn __priv_test_new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
||||
AppConfig::new(secure, host, addr)
|
||||
@ -141,6 +144,11 @@ impl AppConfig {
|
||||
pub fn local_addr(&self) -> SocketAddr {
|
||||
self.addr
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn set_host(&mut self, host: &str) {
|
||||
self.host = host.to_owned();
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
@ -191,6 +199,7 @@ impl ServiceConfig {
|
||||
/// Add shared app data item.
|
||||
///
|
||||
/// Counterpart to [`App::data()`](crate::App::data).
|
||||
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||
pub fn data<U: 'static>(&mut self, data: U) -> &mut Self {
|
||||
self.app_data(Data::new(data));
|
||||
self
|
||||
@ -256,6 +265,8 @@ mod tests {
|
||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
||||
use crate::{web, App, HttpRequest, HttpResponse};
|
||||
|
||||
// allow deprecated `ServiceConfig::data`
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_data() {
|
||||
let cfg = |cfg: &mut ServiceConfig| {
|
||||
|
11
src/data.rs
11
src/data.rs
@ -36,6 +36,11 @@ pub(crate) type FnDataFactory =
|
||||
/// If route data is not set for a handler, using `Data<T>` extractor would cause *Internal
|
||||
/// Server Error* response.
|
||||
///
|
||||
// TODO: document `dyn T` functionality through converting an Arc
|
||||
// TODO: note equivalence of req.app_data<Data<T>> and Data<T> extractor
|
||||
// TODO: note that data must be inserted using Data<T> in order to extract it
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::sync::Mutex;
|
||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
||||
@ -154,6 +159,8 @@ mod tests {
|
||||
web, App, HttpResponse,
|
||||
};
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_data_extractor() {
|
||||
let srv = init_service(App::new().data("TEST".to_string()).service(
|
||||
@ -221,6 +228,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_route_data_extractor() {
|
||||
let srv = init_service(
|
||||
@ -250,6 +259,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_override_data() {
|
||||
let srv =
|
||||
|
@ -1,6 +1,4 @@
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __downcast_get_type_id {
|
||||
macro_rules! downcast_get_type_id {
|
||||
() => {
|
||||
/// A helper method to get the type ID of the type
|
||||
/// this trait is implemented on.
|
||||
@ -30,10 +28,8 @@ macro_rules! __downcast_get_type_id {
|
||||
};
|
||||
}
|
||||
|
||||
//Generate implementation for dyn $name
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! __downcast_dyn {
|
||||
// Generate implementation for dyn $name
|
||||
macro_rules! downcast_dyn {
|
||||
($name:ident) => {
|
||||
/// A struct with a private constructor, for use with
|
||||
/// `__private_get_type_id__`. Its single field is private,
|
||||
@ -80,15 +76,17 @@ macro_rules! __downcast_dyn {
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use {downcast_dyn, downcast_get_type_id};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::upper_case_acronyms)]
|
||||
|
||||
trait MB {
|
||||
__downcast_get_type_id!();
|
||||
downcast_get_type_id!();
|
||||
}
|
||||
|
||||
__downcast_dyn!(MB);
|
||||
downcast_dyn!(MB);
|
||||
|
||||
impl MB for String {}
|
||||
impl MB for () {}
|
||||
|
@ -18,6 +18,7 @@ mod response_error;
|
||||
pub use self::error::Error;
|
||||
pub use self::internal::*;
|
||||
pub use self::response_error::ResponseError;
|
||||
pub(crate) use macros::{downcast_dyn, downcast_get_type_id};
|
||||
|
||||
/// A convenience [`Result`](std::result::Result) for Actix Web operations.
|
||||
///
|
||||
|
@ -9,7 +9,7 @@ use std::{
|
||||
use actix_http::{body::AnyBody, header, Response, StatusCode};
|
||||
use bytes::BytesMut;
|
||||
|
||||
use crate::{__downcast_dyn, __downcast_get_type_id};
|
||||
use crate::error::{downcast_dyn, downcast_get_type_id};
|
||||
use crate::{helpers, HttpResponse};
|
||||
|
||||
/// Errors that can generate responses.
|
||||
@ -41,10 +41,10 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
|
||||
res.set_body(AnyBody::from(buf))
|
||||
}
|
||||
|
||||
__downcast_get_type_id!();
|
||||
downcast_get_type_id!();
|
||||
}
|
||||
|
||||
__downcast_dyn!(ResponseError);
|
||||
downcast_dyn!(ResponseError);
|
||||
|
||||
impl ResponseError for Box<dyn StdError + 'static> {}
|
||||
|
||||
@ -57,6 +57,10 @@ impl ResponseError for serde::de::value::Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for serde_json::Error {}
|
||||
|
||||
impl ResponseError for serde_urlencoded::ser::Error {}
|
||||
|
||||
impl ResponseError for std::str::Utf8Error {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
StatusCode::BAD_REQUEST
|
||||
|
@ -1,12 +1,14 @@
|
||||
//! Request extractors
|
||||
|
||||
use std::{
|
||||
convert::Infallible,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use actix_http::http::{Method, Uri};
|
||||
use actix_utils::future::{ok, Ready};
|
||||
use futures_core::ready;
|
||||
|
||||
use crate::{dev::Payload, Error, HttpRequest};
|
||||
@ -52,7 +54,7 @@ pub trait FromRequest: Sized {
|
||||
/// use actix_web::{web, dev, App, Error, HttpRequest, FromRequest};
|
||||
/// use actix_web::error::ErrorBadRequest;
|
||||
/// use futures_util::future::{ok, err, Ready};
|
||||
/// use serde_derive::Deserialize;
|
||||
/// use serde::Deserialize;
|
||||
/// use rand;
|
||||
///
|
||||
/// #[derive(Debug, Deserialize)]
|
||||
@ -143,7 +145,7 @@ where
|
||||
/// use actix_web::{web, dev, App, Result, Error, HttpRequest, FromRequest};
|
||||
/// use actix_web::error::ErrorBadRequest;
|
||||
/// use futures_util::future::{ok, err, Ready};
|
||||
/// use serde_derive::Deserialize;
|
||||
/// use serde::Deserialize;
|
||||
/// use rand;
|
||||
///
|
||||
/// #[derive(Debug, Deserialize)]
|
||||
@ -216,14 +218,58 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the request's URI.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::{http::Uri, web, App, Responder};
|
||||
///
|
||||
/// async fn handler(uri: Uri) -> impl Responder {
|
||||
/// format!("Requested path: {}", uri.path())
|
||||
/// }
|
||||
///
|
||||
/// let app = App::new().default_service(web::to(handler));
|
||||
/// ```
|
||||
impl FromRequest for Uri {
|
||||
type Error = Infallible;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
ok(req.uri().clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the request's method.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::{http::Method, web, App, Responder};
|
||||
///
|
||||
/// async fn handler(method: Method) -> impl Responder {
|
||||
/// format!("Request method: {}", method)
|
||||
/// }
|
||||
///
|
||||
/// let app = App::new().default_service(web::to(handler));
|
||||
/// ```
|
||||
impl FromRequest for Method {
|
||||
type Error = Infallible;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
ok(req.method().clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl FromRequest for () {
|
||||
type Error = Error;
|
||||
type Future = Ready<Result<(), Error>>;
|
||||
type Error = Infallible;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
ready(Ok(()))
|
||||
ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -330,7 +376,7 @@ mod m {
|
||||
mod tests {
|
||||
use actix_http::http::header;
|
||||
use bytes::Bytes;
|
||||
use serde_derive::Deserialize;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::*;
|
||||
use crate::test::TestRequest;
|
||||
@ -411,4 +457,18 @@ mod tests {
|
||||
.unwrap();
|
||||
assert!(r.is_err());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_uri() {
|
||||
let req = TestRequest::default().uri("/foo/bar").to_http_request();
|
||||
let uri = Uri::extract(&req).await.unwrap();
|
||||
assert_eq!(uri.path(), "/foo/bar");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_method() {
|
||||
let req = TestRequest::default().method(Method::GET).to_http_request();
|
||||
let method = Method::extract(&req).await.unwrap();
|
||||
assert_eq!(method, Method::GET);
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use mime::Mime;
|
||||
use super::{qitem, QualityItem};
|
||||
use crate::http::header;
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)
|
||||
///
|
||||
/// The `Accept` header field can be used by user agents to specify
|
||||
@ -81,14 +81,14 @@ crate::__define_common_header! {
|
||||
|
||||
test_accept {
|
||||
// Tests from the RFC
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test1,
|
||||
vec![b"audio/*; q=0.2, audio/basic"],
|
||||
Some(Accept(vec![
|
||||
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
||||
qitem("audio/basic".parse().unwrap()),
|
||||
])));
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test2,
|
||||
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
||||
Some(Accept(vec![
|
||||
@ -100,13 +100,13 @@ crate::__define_common_header! {
|
||||
qitem("text/x-c".parse().unwrap()),
|
||||
])));
|
||||
// Custom tests
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test3,
|
||||
vec![b"text/plain; charset=utf-8"],
|
||||
Some(Accept(vec![
|
||||
qitem(mime::TEXT_PLAIN_UTF_8),
|
||||
])));
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test4,
|
||||
vec![b"text/plain; charset=utf-8; q=0.5"],
|
||||
Some(Accept(vec
|
||||
///
|
||||
@ -57,6 +57,6 @@ crate::__define_common_header! {
|
||||
|
||||
test_accept_charset {
|
||||
// Test case from RFC
|
||||
crate::__common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
|
||||
}
|
||||
}
|
||||
|
@ -64,12 +64,12 @@ header! {
|
||||
|
||||
test_accept_encoding {
|
||||
// From the RFC
|
||||
crate::__common_header_test!(test1, vec![b"compress, gzip"]);
|
||||
crate::__common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
||||
crate::__common_header_test!(test3, vec![b"*"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]);
|
||||
crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
||||
crate::http::header::common_header_test!(test3, vec![b"*"]);
|
||||
// Note: Removed quality 1 from gzip
|
||||
crate::__common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
||||
crate::http::header::common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
||||
// Note: Removed quality 1 from gzip
|
||||
crate::__common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
||||
crate::http::header::common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use language_tags::LanguageTag;
|
||||
|
||||
use super::{QualityItem, ACCEPT_LANGUAGE};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Accept-Language` header, defined in
|
||||
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
|
||||
///
|
||||
@ -53,9 +53,9 @@ crate::__define_common_header! {
|
||||
|
||||
test_accept_language {
|
||||
// From the RFC
|
||||
crate::__common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
|
||||
// Own test
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test2, vec![b"en-US, en; q=0.5, fr"],
|
||||
Some(AcceptLanguage(vec
|
||||
///
|
||||
/// The `Allow` header field lists the set of methods advertised as
|
||||
@ -49,12 +49,12 @@ crate::__define_common_header! {
|
||||
|
||||
test_allow {
|
||||
// From the RFC
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test1,
|
||||
vec![b"GET, HEAD, PUT"],
|
||||
Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT])));
|
||||
// Own tests
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test2,
|
||||
vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"],
|
||||
Some(HeaderField(vec![
|
||||
@ -67,7 +67,7 @@ crate::__define_common_header! {
|
||||
Method::TRACE,
|
||||
Method::CONNECT,
|
||||
Method::PATCH])));
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test3,
|
||||
vec![b""],
|
||||
Some(HeaderField(Vec::<Method>::new())));
|
||||
|
@ -49,9 +49,9 @@ use crate::http::header;
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub struct CacheControl(pub Vec<CacheDirective>);
|
||||
|
||||
crate::__common_header_deref!(CacheControl => Vec<CacheDirective>);
|
||||
crate::http::header::common_header_deref!(CacheControl => Vec<CacheDirective>);
|
||||
|
||||
// TODO: this could just be the __define_common_header! macro
|
||||
// TODO: this could just be the crate::http::header::common_header! macro
|
||||
impl Header for CacheControl {
|
||||
fn name() -> header::HeaderName {
|
||||
header::CACHE_CONTROL
|
||||
|
@ -410,41 +410,33 @@ impl ContentDisposition {
|
||||
|
||||
/// Return the value of *name* if exists.
|
||||
pub fn get_name(&self) -> Option<&str> {
|
||||
self.parameters.iter().filter_map(|p| p.as_name()).next()
|
||||
self.parameters.iter().find_map(DispositionParam::as_name)
|
||||
}
|
||||
|
||||
/// Return the value of *filename* if exists.
|
||||
pub fn get_filename(&self) -> Option<&str> {
|
||||
self.parameters
|
||||
.iter()
|
||||
.filter_map(|p| p.as_filename())
|
||||
.next()
|
||||
.find_map(DispositionParam::as_filename)
|
||||
}
|
||||
|
||||
/// Return the value of *filename\** if exists.
|
||||
pub fn get_filename_ext(&self) -> Option<&ExtendedValue> {
|
||||
self.parameters
|
||||
.iter()
|
||||
.filter_map(|p| p.as_filename_ext())
|
||||
.next()
|
||||
.find_map(DispositionParam::as_filename_ext)
|
||||
}
|
||||
|
||||
/// Return the value of the parameter which the `name` matches.
|
||||
pub fn get_unknown(&self, name: impl AsRef<str>) -> Option<&str> {
|
||||
let name = name.as_ref();
|
||||
self.parameters
|
||||
.iter()
|
||||
.filter_map(|p| p.as_unknown(name))
|
||||
.next()
|
||||
self.parameters.iter().find_map(|p| p.as_unknown(name))
|
||||
}
|
||||
|
||||
/// Return the value of the extended parameter which the `name` matches.
|
||||
pub fn get_unknown_ext(&self, name: impl AsRef<str>) -> Option<&ExtendedValue> {
|
||||
let name = name.as_ref();
|
||||
self.parameters
|
||||
.iter()
|
||||
.filter_map(|p| p.as_unknown_ext(name))
|
||||
.next()
|
||||
self.parameters.iter().find_map(|p| p.as_unknown_ext(name))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{QualityItem, CONTENT_LANGUAGE};
|
||||
use language_tags::LanguageTag;
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Content-Language` header, defined in
|
||||
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
|
||||
///
|
||||
@ -50,7 +50,7 @@ crate::__define_common_header! {
|
||||
(ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+
|
||||
|
||||
test_content_language {
|
||||
crate::__common_header_test!(test1, vec![b"da"]);
|
||||
crate::__common_header_test!(test2, vec![b"mi, en"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"da"]);
|
||||
crate::http::header::common_header_test!(test2, vec![b"mi, en"]);
|
||||
}
|
||||
}
|
||||
|
@ -4,65 +4,65 @@ use std::str::FromStr;
|
||||
use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE};
|
||||
use crate::error::ParseError;
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Content-Range` header, defined in
|
||||
/// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2)
|
||||
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
|
||||
|
||||
test_content_range {
|
||||
crate::__common_header_test!(test_bytes,
|
||||
crate::http::header::common_header_test!(test_bytes,
|
||||
vec![b"bytes 0-499/500"],
|
||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||
range: Some((0, 499)),
|
||||
instance_length: Some(500)
|
||||
})));
|
||||
|
||||
crate::__common_header_test!(test_bytes_unknown_len,
|
||||
crate::http::header::common_header_test!(test_bytes_unknown_len,
|
||||
vec![b"bytes 0-499/*"],
|
||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||
range: Some((0, 499)),
|
||||
instance_length: None
|
||||
})));
|
||||
|
||||
crate::__common_header_test!(test_bytes_unknown_range,
|
||||
crate::http::header::common_header_test!(test_bytes_unknown_range,
|
||||
vec![b"bytes */500"],
|
||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||
range: None,
|
||||
instance_length: Some(500)
|
||||
})));
|
||||
|
||||
crate::__common_header_test!(test_unregistered,
|
||||
crate::http::header::common_header_test!(test_unregistered,
|
||||
vec![b"seconds 1-2"],
|
||||
Some(ContentRange(ContentRangeSpec::Unregistered {
|
||||
unit: "seconds".to_owned(),
|
||||
resp: "1-2".to_owned()
|
||||
})));
|
||||
|
||||
crate::__common_header_test!(test_no_len,
|
||||
crate::http::header::common_header_test!(test_no_len,
|
||||
vec![b"bytes 0-499"],
|
||||
None::<ContentRange>);
|
||||
|
||||
crate::__common_header_test!(test_only_unit,
|
||||
crate::http::header::common_header_test!(test_only_unit,
|
||||
vec![b"bytes"],
|
||||
None::<ContentRange>);
|
||||
|
||||
crate::__common_header_test!(test_end_less_than_start,
|
||||
crate::http::header::common_header_test!(test_end_less_than_start,
|
||||
vec![b"bytes 499-0/500"],
|
||||
None::<ContentRange>);
|
||||
|
||||
crate::__common_header_test!(test_blank,
|
||||
crate::http::header::common_header_test!(test_blank,
|
||||
vec![b""],
|
||||
None::<ContentRange>);
|
||||
|
||||
crate::__common_header_test!(test_bytes_many_spaces,
|
||||
crate::http::header::common_header_test!(test_bytes_many_spaces,
|
||||
vec![b"bytes 1-2/500 3"],
|
||||
None::<ContentRange>);
|
||||
|
||||
crate::__common_header_test!(test_bytes_many_slashes,
|
||||
crate::http::header::common_header_test!(test_bytes_many_slashes,
|
||||
vec![b"bytes 1-2/500/600"],
|
||||
None::<ContentRange>);
|
||||
|
||||
crate::__common_header_test!(test_bytes_many_dashes,
|
||||
crate::http::header::common_header_test!(test_bytes_many_dashes,
|
||||
vec![b"bytes 1-2-3/500"],
|
||||
None::<ContentRange>);
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::CONTENT_TYPE;
|
||||
use mime::Mime;
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Content-Type` header, defined in
|
||||
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
|
||||
///
|
||||
@ -52,7 +52,7 @@ crate::__define_common_header! {
|
||||
(ContentType, CONTENT_TYPE) => [Mime]
|
||||
|
||||
test_content_type {
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test1,
|
||||
vec![b"text/html"],
|
||||
Some(HeaderField(mime::TEXT_HTML)));
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{HttpDate, DATE};
|
||||
use std::time::SystemTime;
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)
|
||||
///
|
||||
/// The `Date` header field represents the date and time at which the
|
||||
@ -32,7 +32,7 @@ crate::__define_common_header! {
|
||||
(Date, DATE) => [HttpDate]
|
||||
|
||||
test_date {
|
||||
crate::__common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::{fmt, str};
|
||||
|
||||
pub use self::Encoding::{
|
||||
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers,
|
||||
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd,
|
||||
};
|
||||
|
||||
/// A value to represent an encoding used in `Transfer-Encoding`
|
||||
@ -22,6 +22,8 @@ pub enum Encoding {
|
||||
Identity,
|
||||
/// The `trailers` encoding.
|
||||
Trailers,
|
||||
/// The `zstd` encoding.
|
||||
Zstd,
|
||||
/// Some other encoding that is less common, can be any String.
|
||||
EncodingExt(String),
|
||||
}
|
||||
@ -36,6 +38,7 @@ impl fmt::Display for Encoding {
|
||||
Compress => "compress",
|
||||
Identity => "identity",
|
||||
Trailers => "trailers",
|
||||
Zstd => "zstd",
|
||||
EncodingExt(ref s) => s.as_ref(),
|
||||
})
|
||||
}
|
||||
@ -52,6 +55,7 @@ impl str::FromStr for Encoding {
|
||||
"compress" => Ok(Compress),
|
||||
"identity" => Ok(Identity),
|
||||
"trailers" => Ok(Trailers),
|
||||
"zstd" => Ok(Zstd),
|
||||
_ => Ok(EncodingExt(s.to_owned())),
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{EntityTag, ETAG};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3)
|
||||
///
|
||||
/// The `ETag` header field in a response provides the current entity-tag
|
||||
@ -50,50 +50,50 @@ crate::__define_common_header! {
|
||||
|
||||
test_etag {
|
||||
// From the RFC
|
||||
crate::__common_header_test!(test1,
|
||||
crate::http::header::common_header_test!(test1,
|
||||
vec![b"\"xyzzy\""],
|
||||
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
|
||||
crate::__common_header_test!(test2,
|
||||
crate::http::header::common_header_test!(test2,
|
||||
vec![b"W/\"xyzzy\""],
|
||||
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
|
||||
crate::__common_header_test!(test3,
|
||||
crate::http::header::common_header_test!(test3,
|
||||
vec![b"\"\""],
|
||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||
// Own tests
|
||||
crate::__common_header_test!(test4,
|
||||
crate::http::header::common_header_test!(test4,
|
||||
vec![b"\"foobar\""],
|
||||
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
|
||||
crate::__common_header_test!(test5,
|
||||
crate::http::header::common_header_test!(test5,
|
||||
vec![b"\"\""],
|
||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||
crate::__common_header_test!(test6,
|
||||
crate::http::header::common_header_test!(test6,
|
||||
vec![b"W/\"weak-etag\""],
|
||||
Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
|
||||
crate::__common_header_test!(test7,
|
||||
crate::http::header::common_header_test!(test7,
|
||||
vec![b"W/\"\x65\x62\""],
|
||||
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
|
||||
crate::__common_header_test!(test8,
|
||||
crate::http::header::common_header_test!(test8,
|
||||
vec![b"W/\"\""],
|
||||
Some(ETag(EntityTag::new(true, "".to_owned()))));
|
||||
crate::__common_header_test!(test9,
|
||||
crate::http::header::common_header_test!(test9,
|
||||
vec![b"no-dquotes"],
|
||||
None::<ETag>);
|
||||
crate::__common_header_test!(test10,
|
||||
crate::http::header::common_header_test!(test10,
|
||||
vec![b"w/\"the-first-w-is-case-sensitive\""],
|
||||
None::<ETag>);
|
||||
crate::__common_header_test!(test11,
|
||||
crate::http::header::common_header_test!(test11,
|
||||
vec![b""],
|
||||
None::<ETag>);
|
||||
crate::__common_header_test!(test12,
|
||||
crate::http::header::common_header_test!(test12,
|
||||
vec![b"\"unmatched-dquotes1"],
|
||||
None::<ETag>);
|
||||
crate::__common_header_test!(test13,
|
||||
crate::http::header::common_header_test!(test13,
|
||||
vec![b"unmatched-dquotes2\""],
|
||||
None::<ETag>);
|
||||
crate::__common_header_test!(test14,
|
||||
crate::http::header::common_header_test!(test14,
|
||||
vec![b"matched-\"dquotes\""],
|
||||
None::<ETag>);
|
||||
crate::__common_header_test!(test15,
|
||||
crate::http::header::common_header_test!(test15,
|
||||
vec![b"\""],
|
||||
None::<ETag>);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{HttpDate, EXPIRES};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
|
||||
///
|
||||
/// The `Expires` header field gives the date/time after which the
|
||||
@ -36,6 +36,6 @@ crate::__define_common_header! {
|
||||
|
||||
test_expires {
|
||||
// Test case from RFC
|
||||
crate::__common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{EntityTag, IF_MATCH};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `If-Match` header, defined in
|
||||
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
|
||||
///
|
||||
@ -53,18 +53,18 @@ crate::__define_common_header! {
|
||||
(IfMatch, IF_MATCH) => {Any / (EntityTag)+}
|
||||
|
||||
test_if_match {
|
||||
crate::__common_header_test!(
|
||||
crate::http::header::common_header_test!(
|
||||
test1,
|
||||
vec![b"\"xyzzy\""],
|
||||
Some(HeaderField::Items(
|
||||
vec![EntityTag::new(false, "xyzzy".to_owned())])));
|
||||
crate::__common_header_test!(
|
||||
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())])));
|
||||
crate::__common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
|
||||
crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{HttpDate, IF_MODIFIED_SINCE};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `If-Modified-Since` header, defined in
|
||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
|
||||
///
|
||||
@ -36,6 +36,6 @@ crate::__define_common_header! {
|
||||
|
||||
test_if_modified_since {
|
||||
// Test case from RFC
|
||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{EntityTag, IF_NONE_MATCH};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `If-None-Match` header, defined in
|
||||
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
|
||||
///
|
||||
@ -55,11 +55,11 @@ crate::__define_common_header! {
|
||||
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
|
||||
|
||||
test_if_none_match {
|
||||
crate::__common_header_test!(test1, vec![b"\"xyzzy\""]);
|
||||
crate::__common_header_test!(test2, vec![b"W/\"xyzzy\""]);
|
||||
crate::__common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
|
||||
crate::__common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
|
||||
crate::__common_header_test!(test5, vec![b"*"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"\"xyzzy\""]);
|
||||
crate::http::header::common_header_test!(test2, vec![b"W/\"xyzzy\""]);
|
||||
crate::http::header::common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
|
||||
crate::http::header::common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
|
||||
crate::http::header::common_header_test!(test5, vec![b"*"]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,7 +113,7 @@ mod test_if_range {
|
||||
use crate::http::header::*;
|
||||
use std::str;
|
||||
|
||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
crate::__common_header_test!(test2, vec![b"\"abc\""]);
|
||||
crate::__common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>);
|
||||
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
crate::http::header::common_header_test!(test2, vec![b"\"abc\""]);
|
||||
crate::http::header::common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{HttpDate, IF_UNMODIFIED_SINCE};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `If-Unmodified-Since` header, defined in
|
||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
|
||||
///
|
||||
@ -37,6 +37,6 @@ crate::__define_common_header! {
|
||||
|
||||
test_if_unmodified_since {
|
||||
// Test case from RFC
|
||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{HttpDate, LAST_MODIFIED};
|
||||
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
/// `Last-Modified` header, defined in
|
||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
|
||||
///
|
||||
@ -36,6 +36,6 @@ crate::__define_common_header! {
|
||||
|
||||
test_last_modified {
|
||||
// Test case from RFC
|
||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! __common_header_deref {
|
||||
macro_rules! common_header_deref {
|
||||
($from:ty => $to:ty) => {
|
||||
impl ::std::ops::Deref for $from {
|
||||
type Target = $to;
|
||||
@ -20,9 +18,7 @@ macro_rules! __common_header_deref {
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! __common_header_test_module {
|
||||
macro_rules! common_header_test_module {
|
||||
($id:ident, $tm:ident{$($tf:item)*}) => {
|
||||
#[allow(unused_imports)]
|
||||
#[cfg(test)]
|
||||
@ -37,9 +33,8 @@ macro_rules! __common_header_test_module {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! __common_header_test {
|
||||
#[cfg(test)]
|
||||
macro_rules! common_header_test {
|
||||
($id:ident, $raw:expr) => {
|
||||
#[test]
|
||||
fn $id() {
|
||||
@ -99,9 +94,7 @@ macro_rules! __common_header_test {
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! __define_common_header {
|
||||
macro_rules! common_header {
|
||||
// $a:meta: Attributes associated with the header item (usually docs)
|
||||
// $id:ident: Identifier of the header
|
||||
// $n:expr: Lowercase name of the header
|
||||
@ -112,7 +105,7 @@ macro_rules! __define_common_header {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct $id(pub Vec<$item>);
|
||||
crate::__common_header_deref!($id => Vec<$item>);
|
||||
crate::http::header::common_header_deref!($id => Vec<$item>);
|
||||
impl $crate::http::header::Header for $id {
|
||||
#[inline]
|
||||
fn name() -> $crate::http::header::HeaderName {
|
||||
@ -148,7 +141,7 @@ macro_rules! __define_common_header {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct $id(pub Vec<$item>);
|
||||
crate::__common_header_deref!($id => Vec<$item>);
|
||||
crate::http::header::common_header_deref!($id => Vec<$item>);
|
||||
impl $crate::http::header::Header for $id {
|
||||
#[inline]
|
||||
fn name() -> $crate::http::header::HeaderName {
|
||||
@ -184,7 +177,7 @@ macro_rules! __define_common_header {
|
||||
$(#[$a])*
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct $id(pub $value);
|
||||
crate::__common_header_deref!($id => $value);
|
||||
crate::http::header::common_header_deref!($id => $value);
|
||||
impl $crate::http::header::Header for $id {
|
||||
#[inline]
|
||||
fn name() -> $crate::http::header::HeaderName {
|
||||
@ -267,34 +260,39 @@ macro_rules! __define_common_header {
|
||||
|
||||
// optional test module
|
||||
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
$(#[$a])*
|
||||
($id, $name) => ($item)*
|
||||
}
|
||||
|
||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
$(#[$a])*
|
||||
($id, $n) => ($item)+
|
||||
}
|
||||
|
||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
($(#[$a:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
$(#[$a])* ($id, $name) => [$item]
|
||||
}
|
||||
|
||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
||||
crate::__define_common_header! {
|
||||
crate::http::header::common_header! {
|
||||
$(#[$a])*
|
||||
($id, $name) => {Any / ($item)+}
|
||||
}
|
||||
|
||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use {common_header, common_header_deref, common_header_test_module};
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use common_header_test;
|
||||
|
@ -84,4 +84,8 @@ mod if_none_match;
|
||||
mod if_range;
|
||||
mod if_unmodified_since;
|
||||
mod last_modified;
|
||||
|
||||
mod macros;
|
||||
#[cfg(test)]
|
||||
pub(crate) use macros::common_header_test;
|
||||
pub(crate) use macros::{common_header, common_header_deref, common_header_test_module};
|
||||
|
448
src/info.rs
448
src/info.rs
@ -1,13 +1,68 @@
|
||||
use std::cell::Ref;
|
||||
use std::{cell::Ref, convert::Infallible, net::SocketAddr};
|
||||
|
||||
use crate::dev::{AppConfig, RequestHead};
|
||||
use crate::http::header::{self, HeaderName};
|
||||
use actix_utils::future::{err, ok, Ready};
|
||||
use derive_more::{Display, Error};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
||||
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
||||
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
||||
use crate::{
|
||||
dev::{AppConfig, Payload, RequestHead},
|
||||
http::{
|
||||
header::{self, HeaderName},
|
||||
uri::{Authority, Scheme},
|
||||
},
|
||||
FromRequest, HttpRequest, ResponseError,
|
||||
};
|
||||
|
||||
/// `HttpRequest` connection information
|
||||
static X_FORWARDED_FOR: Lazy<HeaderName> =
|
||||
Lazy::new(|| HeaderName::from_static("x-forwarded-for"));
|
||||
static X_FORWARDED_HOST: Lazy<HeaderName> =
|
||||
Lazy::new(|| HeaderName::from_static("x-forwarded-host"));
|
||||
static X_FORWARDED_PROTO: Lazy<HeaderName> =
|
||||
Lazy::new(|| HeaderName::from_static("x-forwarded-proto"));
|
||||
|
||||
/// Trim whitespace then any quote marks.
|
||||
fn unquote(val: &str) -> &str {
|
||||
val.trim().trim_start_matches('"').trim_end_matches('"')
|
||||
}
|
||||
|
||||
/// Extracts and trims first value for given header name.
|
||||
fn first_header_value<'a>(req: &'a RequestHead, name: &'_ HeaderName) -> Option<&'a str> {
|
||||
let hdr = req.headers.get(name)?.to_str().ok()?;
|
||||
let val = hdr.split(',').next()?.trim();
|
||||
Some(val)
|
||||
}
|
||||
|
||||
/// HTTP connection information.
|
||||
///
|
||||
/// `ConnectionInfo` implements `FromRequest` and can be extracted in handlers.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use actix_web::{HttpResponse, Responder};
|
||||
/// use actix_web::dev::ConnectionInfo;
|
||||
///
|
||||
/// async fn handler(conn: ConnectionInfo) -> impl Responder {
|
||||
/// match conn.host() {
|
||||
/// "actix.rs" => HttpResponse::Ok().body("Welcome!"),
|
||||
/// "admin.actix.rs" => HttpResponse::Ok().body("Admin portal."),
|
||||
/// _ => HttpResponse::NotFound().finish()
|
||||
/// }
|
||||
/// }
|
||||
/// # let _svc = actix_web::web::to(handler);
|
||||
/// ```
|
||||
///
|
||||
/// # Implementation Notes
|
||||
/// Parses `Forwarded` header information according to [RFC 7239][rfc7239] but does not try to
|
||||
/// interpret the values for each property. As such, the getter methods on `ConnectionInfo` return
|
||||
/// strings instead of IP addresses or other types to acknowledge that they may be
|
||||
/// [obfuscated][rfc7239-63] or [unknown][rfc7239-62].
|
||||
///
|
||||
/// If the older, related headers are also present (eg. `X-Forwarded-For`), then `Forwarded`
|
||||
/// is preferred.
|
||||
///
|
||||
/// [rfc7239]: https://datatracker.ietf.org/doc/html/rfc7239
|
||||
/// [rfc7239-62]: https://datatracker.ietf.org/doc/html/rfc7239#section-6.2
|
||||
/// [rfc7239-63]: https://datatracker.ietf.org/doc/html/rfc7239#section-6.3
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ConnectionInfo {
|
||||
scheme: String,
|
||||
@ -25,105 +80,75 @@ impl ConnectionInfo {
|
||||
Ref::map(req.extensions(), |e| e.get().unwrap())
|
||||
}
|
||||
|
||||
#[allow(clippy::cognitive_complexity, clippy::borrow_interior_mutable_const)]
|
||||
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
||||
let mut host = None;
|
||||
let mut scheme = None;
|
||||
let mut realip_remote_addr = None;
|
||||
|
||||
// load forwarded header
|
||||
for hdr in req.headers.get_all(&header::FORWARDED) {
|
||||
if let Ok(val) = hdr.to_str() {
|
||||
for pair in val.split(';') {
|
||||
for el in pair.split(',') {
|
||||
let mut items = el.trim().splitn(2, '=');
|
||||
if let Some(name) = items.next() {
|
||||
if let Some(val) = items.next() {
|
||||
match &name.to_lowercase() as &str {
|
||||
"for" => {
|
||||
if realip_remote_addr.is_none() {
|
||||
realip_remote_addr = Some(val.trim());
|
||||
}
|
||||
}
|
||||
"proto" => {
|
||||
if scheme.is_none() {
|
||||
scheme = Some(val.trim());
|
||||
}
|
||||
}
|
||||
"host" => {
|
||||
if host.is_none() {
|
||||
host = Some(val.trim());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (name, val) in req
|
||||
.headers
|
||||
.get_all(&header::FORWARDED)
|
||||
.into_iter()
|
||||
.filter_map(|hdr| hdr.to_str().ok())
|
||||
// "for=1.2.3.4, for=5.6.7.8; scheme=https"
|
||||
.flat_map(|val| val.split(';'))
|
||||
// ["for=1.2.3.4, for=5.6.7.8", " scheme=https"]
|
||||
.flat_map(|vals| vals.split(','))
|
||||
// ["for=1.2.3.4", " for=5.6.7.8", " scheme=https"]
|
||||
.flat_map(|pair| {
|
||||
let mut items = pair.trim().splitn(2, '=');
|
||||
Some((items.next()?, items.next()?))
|
||||
})
|
||||
{
|
||||
// [(name , val ), ... ]
|
||||
// [("for", "1.2.3.4"), ("for", "5.6.7.8"), ("scheme", "https")]
|
||||
|
||||
// taking the first value for each property is correct because spec states that first
|
||||
// "for" value is client and rest are proxies; multiple values other properties have
|
||||
// no defined semantics
|
||||
//
|
||||
// > In a chain of proxy servers where this is fully utilized, the first
|
||||
// > "for" parameter will disclose the client where the request was first
|
||||
// > made, followed by any subsequent proxy identifiers.
|
||||
// --- https://datatracker.ietf.org/doc/html/rfc7239#section-5.2
|
||||
|
||||
match name.trim().to_lowercase().as_str() {
|
||||
"for" => realip_remote_addr.get_or_insert_with(|| unquote(val)),
|
||||
"proto" => scheme.get_or_insert_with(|| unquote(val)),
|
||||
"host" => host.get_or_insert_with(|| unquote(val)),
|
||||
"by" => {
|
||||
// TODO: implement https://datatracker.ietf.org/doc/html/rfc7239#section-5.1
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
}
|
||||
|
||||
// scheme
|
||||
if scheme.is_none() {
|
||||
if let Some(h) = req
|
||||
.headers
|
||||
.get(&HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap())
|
||||
{
|
||||
if let Ok(h) = h.to_str() {
|
||||
scheme = h.split(',').next().map(|v| v.trim());
|
||||
}
|
||||
}
|
||||
if scheme.is_none() {
|
||||
scheme = req.uri.scheme().map(|a| a.as_str());
|
||||
if scheme.is_none() && cfg.secure() {
|
||||
scheme = Some("https")
|
||||
}
|
||||
}
|
||||
}
|
||||
let scheme = scheme
|
||||
.or_else(|| first_header_value(req, &*X_FORWARDED_PROTO))
|
||||
.or_else(|| req.uri.scheme().map(Scheme::as_str))
|
||||
.or_else(|| Some("https").filter(|_| cfg.secure()))
|
||||
.unwrap_or("http")
|
||||
.to_owned();
|
||||
|
||||
// host
|
||||
if host.is_none() {
|
||||
if let Some(h) = req
|
||||
.headers
|
||||
.get(&HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap())
|
||||
{
|
||||
if let Ok(h) = h.to_str() {
|
||||
host = h.split(',').next().map(|v| v.trim());
|
||||
}
|
||||
}
|
||||
if host.is_none() {
|
||||
if let Some(h) = req.headers.get(&header::HOST) {
|
||||
host = h.to_str().ok();
|
||||
}
|
||||
if host.is_none() {
|
||||
host = req.uri.authority().map(|a| a.as_str());
|
||||
if host.is_none() {
|
||||
host = Some(cfg.host());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let host = host
|
||||
.or_else(|| first_header_value(req, &*X_FORWARDED_HOST))
|
||||
.or_else(|| req.headers.get(&header::HOST)?.to_str().ok())
|
||||
.or_else(|| req.uri.authority().map(Authority::as_str))
|
||||
.unwrap_or(cfg.host())
|
||||
.to_owned();
|
||||
|
||||
// get remote_addraddr from socketaddr
|
||||
let remote_addr = req.peer_addr.map(|addr| format!("{}", addr));
|
||||
let realip_remote_addr = realip_remote_addr
|
||||
.or_else(|| first_header_value(req, &*X_FORWARDED_FOR))
|
||||
.map(str::to_owned);
|
||||
|
||||
if realip_remote_addr.is_none() {
|
||||
if let Some(h) = req
|
||||
.headers
|
||||
.get(&HeaderName::from_lowercase(X_FORWARDED_FOR).unwrap())
|
||||
{
|
||||
if let Ok(h) = h.to_str() {
|
||||
realip_remote_addr = h.split(',').next().map(|v| v.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
let remote_addr = req.peer_addr.map(|addr| addr.to_string());
|
||||
|
||||
ConnectionInfo {
|
||||
remote_addr,
|
||||
scheme: scheme.unwrap_or("http").to_owned(),
|
||||
host: host.unwrap_or("localhost").to_owned(),
|
||||
realip_remote_addr: realip_remote_addr.map(|s| s.to_owned()),
|
||||
scheme,
|
||||
host,
|
||||
realip_remote_addr,
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,19 +177,16 @@ impl ConnectionInfo {
|
||||
&self.host
|
||||
}
|
||||
|
||||
/// remote_addr address of the request.
|
||||
/// Remote address of the connection.
|
||||
///
|
||||
/// Get remote_addr address from socket address
|
||||
/// Get remote_addr address from socket address.
|
||||
pub fn remote_addr(&self) -> Option<&str> {
|
||||
if let Some(ref remote_addr) = self.remote_addr {
|
||||
Some(remote_addr)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
self.remote_addr.as_deref()
|
||||
}
|
||||
/// Real ip remote addr of client initiated HTTP request.
|
||||
|
||||
/// Real IP (remote address) of client that initiated request.
|
||||
///
|
||||
/// The addr is resolved through the following headers, in this order:
|
||||
/// The address is resolved through the following headers, in this order:
|
||||
///
|
||||
/// - Forwarded
|
||||
/// - X-Forwarded-For
|
||||
@ -173,16 +195,72 @@ impl ConnectionInfo {
|
||||
/// # Security
|
||||
/// Do not use this function for security purposes, unless you can ensure the Forwarded and
|
||||
/// X-Forwarded-For headers cannot be spoofed by the client. If you want the client's socket
|
||||
/// address explicitly, use
|
||||
/// [`HttpRequest::peer_addr()`](super::web::HttpRequest::peer_addr()) instead.
|
||||
/// address explicitly, use [`HttpRequest::peer_addr()`][peer_addr] instead.
|
||||
///
|
||||
/// [peer_addr]: crate::web::HttpRequest::peer_addr()
|
||||
#[inline]
|
||||
pub fn realip_remote_addr(&self) -> Option<&str> {
|
||||
if let Some(ref r) = self.realip_remote_addr {
|
||||
Some(r)
|
||||
} else if let Some(ref remote_addr) = self.remote_addr {
|
||||
Some(remote_addr)
|
||||
} else {
|
||||
None
|
||||
self.realip_remote_addr
|
||||
.as_deref()
|
||||
.or_else(|| self.remote_addr.as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for ConnectionInfo {
|
||||
type Error = Infallible;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
ok(req.connection_info().clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Extractor for peer's socket address.
|
||||
///
|
||||
/// Also see [`HttpRequest::peer_addr`].
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use actix_web::Responder;
|
||||
/// use actix_web::dev::PeerAddr;
|
||||
///
|
||||
/// async fn handler(peer_addr: PeerAddr) -> impl Responder {
|
||||
/// let socket_addr = peer_addr.0;
|
||||
/// socket_addr.to_string()
|
||||
/// }
|
||||
/// # let _svc = actix_web::web::to(handler);
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)]
|
||||
#[display(fmt = "{}", _0)]
|
||||
pub struct PeerAddr(pub SocketAddr);
|
||||
|
||||
impl PeerAddr {
|
||||
/// Unwrap into inner `SocketAddr` value.
|
||||
pub fn into_inner(self) -> SocketAddr {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[non_exhaustive]
|
||||
#[display(fmt = "Missing peer address")]
|
||||
pub struct MissingPeerAddr;
|
||||
|
||||
impl ResponseError for MissingPeerAddr {}
|
||||
|
||||
impl FromRequest for PeerAddr {
|
||||
type Error = MissingPeerAddr;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
type Config = ();
|
||||
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
match req.peer_addr() {
|
||||
Some(addr) => ok(PeerAddr(addr)),
|
||||
None => {
|
||||
log::error!("Missing peer address.");
|
||||
err(MissingPeerAddr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -192,13 +270,60 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::test::TestRequest;
|
||||
|
||||
const X_FORWARDED_FOR: &str = "x-forwarded-for";
|
||||
const X_FORWARDED_HOST: &str = "x-forwarded-host";
|
||||
const X_FORWARDED_PROTO: &str = "x-forwarded-proto";
|
||||
|
||||
#[test]
|
||||
fn test_forwarded() {
|
||||
fn info_default() {
|
||||
let req = TestRequest::default().to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.scheme(), "http");
|
||||
assert_eq!(info.host(), "localhost:8080");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_header() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::HOST, "rust-lang.org"))
|
||||
.to_http_request();
|
||||
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.scheme(), "http");
|
||||
assert_eq!(info.host(), "rust-lang.org");
|
||||
assert_eq!(info.realip_remote_addr(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x_forwarded_for_header() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((X_FORWARDED_FOR, "192.0.2.60"))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x_forwarded_host_header() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((X_FORWARDED_HOST, "192.0.2.60"))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.host(), "192.0.2.60");
|
||||
assert_eq!(info.realip_remote_addr(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x_forwarded_proto_header() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((X_FORWARDED_PROTO, "https"))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.scheme(), "https");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_header() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((
|
||||
header::FORWARDED,
|
||||
@ -212,31 +337,118 @@ mod tests {
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::HOST, "rust-lang.org"))
|
||||
.insert_header((
|
||||
header::FORWARDED,
|
||||
"for=192.0.2.60; proto=https; by=203.0.113.43; host=rust-lang.org",
|
||||
))
|
||||
.to_http_request();
|
||||
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.scheme(), "http");
|
||||
assert_eq!(info.scheme(), "https");
|
||||
assert_eq!(info.host(), "rust-lang.org");
|
||||
assert_eq!(info.realip_remote_addr(), None);
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_case_sensitivity() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((X_FORWARDED_FOR, "192.0.2.60"))
|
||||
.insert_header((header::FORWARDED, "For=192.0.2.60"))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_weird_whitespace() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((X_FORWARDED_HOST, "192.0.2.60"))
|
||||
.insert_header((header::FORWARDED, "for= 1.2.3.4; proto= https"))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.host(), "192.0.2.60");
|
||||
assert_eq!(info.realip_remote_addr(), None);
|
||||
assert_eq!(info.realip_remote_addr(), Some("1.2.3.4"));
|
||||
assert_eq!(info.scheme(), "https");
|
||||
|
||||
let req = TestRequest::default()
|
||||
.insert_header((X_FORWARDED_PROTO, "https"))
|
||||
.insert_header((header::FORWARDED, " for = 1.2.3.4 "))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("1.2.3.4"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_for_quoted() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::FORWARDED, r#"for="192.0.2.60:8080""#))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60:8080"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_for_ipv6() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::FORWARDED, r#"for="[2001:db8:cafe::17]:4711""#))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.realip_remote_addr(), Some("[2001:db8:cafe::17]:4711"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forwarded_for_multiple() {
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::FORWARDED, "for=192.0.2.60, for=198.51.100.17"))
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
// takes the first value
|
||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheme_from_uri() {
|
||||
let req = TestRequest::get()
|
||||
.uri("https://actix.rs/test")
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.scheme(), "https");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_from_uri() {
|
||||
let req = TestRequest::get()
|
||||
.uri("https://actix.rs/test")
|
||||
.to_http_request();
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.host(), "actix.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_from_server_hostname() {
|
||||
let mut req = TestRequest::get();
|
||||
req.set_server_hostname("actix.rs");
|
||||
let req = req.to_http_request();
|
||||
|
||||
let info = req.connection_info();
|
||||
assert_eq!(info.host(), "actix.rs");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn conn_info_extract() {
|
||||
let req = TestRequest::default()
|
||||
.uri("https://actix.rs/test")
|
||||
.to_http_request();
|
||||
let conn_info = ConnectionInfo::extract(&req).await.unwrap();
|
||||
assert_eq!(conn_info.scheme(), "https");
|
||||
assert_eq!(conn_info.host(), "actix.rs");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn peer_addr_extract() {
|
||||
let addr = "127.0.0.1:8080".parse().unwrap();
|
||||
let req = TestRequest::default().peer_addr(addr).to_http_request();
|
||||
let peer_addr = PeerAddr::extract(&req).await.unwrap();
|
||||
assert_eq!(peer_addr, PeerAddr(addr));
|
||||
|
||||
let req = TestRequest::default().to_http_request();
|
||||
let res = PeerAddr::extract(&req).await;
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
|
16
src/lib.rs
16
src/lib.rs
@ -25,7 +25,6 @@
|
||||
//! * [Website & User Guide](https://actix.rs/)
|
||||
//! * [Examples Repository](https://github.com/actix/examples)
|
||||
//! * [Community Chat on Discord](https://discord.gg/NWpN5mmg3x)
|
||||
//! * [Community Chat on Gitter](https://gitter.im/actix/actix-web)
|
||||
//!
|
||||
//! To get started navigating the API docs, you may consider looking at the following pages first:
|
||||
//!
|
||||
@ -47,7 +46,7 @@
|
||||
//! * Streaming and pipelining
|
||||
//! * Keep-alive and slow requests handling
|
||||
//! * Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
||||
//! * Transparent content compression/decompression (br, gzip, deflate)
|
||||
//! * Transparent content compression/decompression (br, gzip, deflate, zstd)
|
||||
//! * Powerful [request routing](https://actix.rs/docs/url-dispatch/)
|
||||
//! * Multipart streams
|
||||
//! * Static assets
|
||||
@ -57,8 +56,10 @@
|
||||
//! * Runs on stable Rust 1.46+
|
||||
//!
|
||||
//! # Crate Features
|
||||
//! * `compress` - content encoding compression support (enabled by default)
|
||||
//! * `cookies` - cookies support (enabled by default)
|
||||
//! * `compress-brotli` - brotli content encoding compression support (enabled by default)
|
||||
//! * `compress-gzip` - gzip and deflate content encoding compression support (enabled by default)
|
||||
//! * `compress-zstd` - zstd content encoding compression support (enabled by default)
|
||||
//! * `openssl` - HTTPS support via `openssl` crate, supports `HTTP/2`
|
||||
//! * `rustls` - HTTPS support via `rustls` crate, supports `HTTP/2`
|
||||
//! * `secure-cookies` - secure cookies support
|
||||
@ -129,7 +130,7 @@ pub mod dev {
|
||||
pub use crate::config::{AppConfig, AppService};
|
||||
#[doc(hidden)]
|
||||
pub use crate::handler::Handler;
|
||||
pub use crate::info::ConnectionInfo;
|
||||
pub use crate::info::{ConnectionInfo, PeerAddr};
|
||||
pub use crate::rmap::ResourceMap;
|
||||
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
||||
|
||||
@ -140,13 +141,16 @@ pub mod dev {
|
||||
pub use actix_http::body::{
|
||||
AnyBody, Body, BodySize, MessageBody, ResponseBody, SizedStream,
|
||||
};
|
||||
#[cfg(feature = "compress")]
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use actix_http::encoding::Decoder as Decompress;
|
||||
pub use actix_http::ResponseBuilder as BaseHttpResponseBuilder;
|
||||
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, ResponseHead};
|
||||
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
|
||||
pub use actix_server::Server;
|
||||
pub use actix_service::{always_ready, forward_ready, Service, Transform};
|
||||
pub use actix_service::{
|
||||
always_ready, fn_factory, fn_service, forward_ready, Service, Transform,
|
||||
};
|
||||
|
||||
pub(crate) fn insert_slash(mut patterns: Vec<String>) -> Vec<String> {
|
||||
for path in &mut patterns {
|
||||
|
@ -144,7 +144,7 @@ mod tests {
|
||||
use crate::{web, App, HttpResponse};
|
||||
|
||||
#[actix_rt::test]
|
||||
#[cfg(all(feature = "cookies", feature = "compress"))]
|
||||
#[cfg(all(feature = "cookies", feature = "__compress"))]
|
||||
async fn test_scope_middleware() {
|
||||
use crate::middleware::Compress;
|
||||
|
||||
@ -167,7 +167,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
#[cfg(all(feature = "cookies", feature = "compress"))]
|
||||
#[cfg(all(feature = "cookies", feature = "__compress"))]
|
||||
async fn test_resource_scope_middleware() {
|
||||
use crate::middleware::Compress;
|
||||
|
||||
|
@ -200,8 +200,7 @@ impl AcceptEncoding {
|
||||
let mut encodings = raw
|
||||
.replace(' ', "")
|
||||
.split(',')
|
||||
.map(|l| AcceptEncoding::new(l))
|
||||
.flatten()
|
||||
.filter_map(|l| AcceptEncoding::new(l))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
encodings.sort();
|
||||
|
@ -14,7 +14,8 @@ pub use self::err_handlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
pub use self::logger::Logger;
|
||||
pub use self::normalize::{NormalizePath, TrailingSlash};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
mod compress;
|
||||
#[cfg(feature = "compress")]
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use self::compress::Compress;
|
||||
|
@ -60,18 +60,6 @@ impl HttpRequest {
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn __priv_test_new(
|
||||
path: Path<Url>,
|
||||
head: Message<RequestHead>,
|
||||
rmap: Rc<ResourceMap>,
|
||||
config: AppConfig,
|
||||
app_data: Rc<Extensions>,
|
||||
) -> HttpRequest {
|
||||
let app_state = AppInitServiceState::new(rmap, config);
|
||||
Self::new(path, head, app_state, app_data)
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpRequest {
|
||||
@ -123,11 +111,7 @@ impl HttpRequest {
|
||||
/// E.g., id=10
|
||||
#[inline]
|
||||
pub fn query_string(&self) -> &str {
|
||||
if let Some(query) = self.uri().query().as_ref() {
|
||||
query
|
||||
} else {
|
||||
""
|
||||
}
|
||||
self.uri().query().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Get a reference to the Path parameters.
|
||||
@ -359,7 +343,7 @@ impl Drop for HttpRequest {
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::{web, App, HttpRequest};
|
||||
/// use serde_derive::Deserialize;
|
||||
/// use serde::Deserialize;
|
||||
///
|
||||
/// /// extract `Thing` from request
|
||||
/// async fn index(req: HttpRequest) -> String {
|
||||
@ -723,6 +707,8 @@ mod tests {
|
||||
assert_eq!(body, Bytes::from_static(b"1"));
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_extensions_dropped() {
|
||||
struct Tracker {
|
||||
|
172
src/resource.rs
172
src/resource.rs
@ -169,40 +169,38 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Provide resource specific data. This method allows to add extractor
|
||||
/// configuration or specific state available via `Data<T>` extractor.
|
||||
/// Provided data is available for all routes registered for the current resource.
|
||||
/// Resource data overrides data registered by `App::data()` method.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::{web, App, FromRequest};
|
||||
///
|
||||
/// /// extract text data from request
|
||||
/// async fn index(body: String) -> String {
|
||||
/// format!("Body {}!", body)
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/index.html")
|
||||
/// // limit size of the payload
|
||||
/// .data(String::configure(|cfg| {
|
||||
/// cfg.limit(4096)
|
||||
/// }))
|
||||
/// .route(
|
||||
/// web::get()
|
||||
/// // register handler
|
||||
/// .to(index)
|
||||
/// ));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||
self.app_data(Data::new(data))
|
||||
}
|
||||
|
||||
/// Add resource data.
|
||||
///
|
||||
/// Data of different types from parent contexts will still be accessible.
|
||||
/// Data of different types from parent contexts will still be accessible. Any `Data<T>` types
|
||||
/// set here can be extracted in handlers using the `Data<T>` extractor.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::cell::Cell;
|
||||
/// use actix_web::{web, App, HttpRequest, HttpResponse, Responder};
|
||||
///
|
||||
/// struct MyData {
|
||||
/// count: std::cell::Cell<usize>,
|
||||
/// }
|
||||
///
|
||||
/// async fn handler(req: HttpRequest, counter: web::Data<MyData>) -> impl Responder {
|
||||
/// // note this cannot use the Data<T> extractor because it was not added with it
|
||||
/// let incr = *req.app_data::<usize>().unwrap();
|
||||
/// assert_eq!(incr, 3);
|
||||
///
|
||||
/// // update counter using other value from app data
|
||||
/// counter.count.set(counter.count.get() + incr);
|
||||
///
|
||||
/// HttpResponse::Ok().body(counter.count.get().to_string())
|
||||
/// }
|
||||
///
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/")
|
||||
/// .app_data(3usize)
|
||||
/// .app_data(web::Data::new(MyData { count: Default::default() }))
|
||||
/// .route(web::get().to(handler))
|
||||
/// );
|
||||
/// ```
|
||||
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||
self.app_data
|
||||
.get_or_insert_with(Extensions::new)
|
||||
@ -211,6 +209,14 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Add resource data after wrapping in `Data<T>`.
|
||||
///
|
||||
/// Deprecated in favor of [`app_data`](Self::app_data).
|
||||
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||
self.app_data(Data::new(data))
|
||||
}
|
||||
|
||||
/// Register a new route and add handler. This route matches all requests.
|
||||
///
|
||||
/// ```
|
||||
@ -226,7 +232,6 @@ where
|
||||
/// This is shortcut for:
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::*;
|
||||
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
||||
/// App::new().service(web::resource("/").route(web::route().to(index)));
|
||||
@ -395,34 +400,28 @@ where
|
||||
*rdef.name_mut() = name.clone();
|
||||
}
|
||||
|
||||
config.register_service(rdef, guards, self, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoServiceFactory<T, ServiceRequest> for Resource<T>
|
||||
where
|
||||
T: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Config = (),
|
||||
Response = ServiceResponse,
|
||||
Error = Error,
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
fn into_factory(self) -> T {
|
||||
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
||||
routes: self.routes,
|
||||
app_data: self.app_data.map(Rc::new),
|
||||
default: self.default,
|
||||
});
|
||||
|
||||
self.endpoint
|
||||
let resource_data = self.app_data.map(Rc::new);
|
||||
|
||||
// wraps endpoint service (including middleware) call and injects app data for this scope
|
||||
let endpoint = apply_fn_factory(self.endpoint, move |mut req: ServiceRequest, srv| {
|
||||
if let Some(ref data) = resource_data {
|
||||
req.add_data_container(Rc::clone(data));
|
||||
}
|
||||
|
||||
srv.call(req)
|
||||
});
|
||||
|
||||
config.register_service(rdef, guards, endpoint, None)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ResourceFactory {
|
||||
routes: Vec<Route>,
|
||||
app_data: Option<Rc<Extensions>>,
|
||||
default: HttpNewService,
|
||||
}
|
||||
|
||||
@ -441,8 +440,6 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||
// construct route service factory futures
|
||||
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
||||
|
||||
let app_data = self.app_data.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let default = default_fut.await?;
|
||||
let routes = factory_fut
|
||||
@ -450,18 +447,13 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(ResourceService {
|
||||
routes,
|
||||
app_data,
|
||||
default,
|
||||
})
|
||||
Ok(ResourceService { routes, default })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ResourceService {
|
||||
routes: Vec<RouteService>,
|
||||
app_data: Option<Rc<Extensions>>,
|
||||
default: HttpService,
|
||||
}
|
||||
|
||||
@ -473,20 +465,12 @@ impl Service<ServiceRequest> for ResourceService {
|
||||
actix_service::always_ready!();
|
||||
|
||||
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||
for route in self.routes.iter() {
|
||||
for route in &self.routes {
|
||||
if route.check(&mut req) {
|
||||
if let Some(ref app_data) = self.app_data {
|
||||
req.add_data_container(app_data.clone());
|
||||
}
|
||||
|
||||
return route.call(req);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref app_data) = self.app_data {
|
||||
req.add_data_container(app_data.clone());
|
||||
}
|
||||
|
||||
self.default.call(req)
|
||||
}
|
||||
}
|
||||
@ -523,11 +507,14 @@ mod tests {
|
||||
use actix_service::Service;
|
||||
use actix_utils::future::ok;
|
||||
|
||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||
use crate::middleware::DefaultHeaders;
|
||||
use crate::service::ServiceRequest;
|
||||
use crate::test::{call_service, init_service, TestRequest};
|
||||
use crate::{guard, web, App, Error, HttpResponse};
|
||||
use crate::{
|
||||
guard,
|
||||
http::{header, HeaderValue, Method, StatusCode},
|
||||
middleware::DefaultHeaders,
|
||||
service::{ServiceRequest, ServiceResponse},
|
||||
test::{call_service, init_service, TestRequest},
|
||||
web, App, Error, HttpMessage, HttpResponse,
|
||||
};
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_middleware() {
|
||||
@ -694,6 +681,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
// allow deprecated `{App, Resource}::data`
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_data() {
|
||||
let srv = init_service(
|
||||
@ -726,6 +715,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
// allow deprecated `{App, Resource}::data`
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_data_default_service() {
|
||||
let srv = init_service(
|
||||
@ -744,4 +735,39 @@ mod tests {
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_middleware_app_data() {
|
||||
let srv = init_service(
|
||||
App::new().service(
|
||||
web::resource("test")
|
||||
.app_data(1usize)
|
||||
.wrap_fn(|req, srv| {
|
||||
assert_eq!(req.app_data::<usize>(), Some(&1usize));
|
||||
req.extensions_mut().insert(1usize);
|
||||
srv.call(req)
|
||||
})
|
||||
.route(web::get().to(HttpResponse::Ok))
|
||||
.default_service(|req: ServiceRequest| async move {
|
||||
let (req, _) = req.into_parts();
|
||||
|
||||
assert_eq!(req.extensions().get::<usize>(), Some(&1));
|
||||
|
||||
Ok(ServiceResponse::new(
|
||||
req,
|
||||
HttpResponse::BadRequest().finish(),
|
||||
))
|
||||
}),
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
let req = TestRequest::get().uri("/test").to_request();
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::post().uri("/test").to_request();
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
@ -406,7 +406,7 @@ impl HttpResponseBuilder {
|
||||
return None;
|
||||
}
|
||||
|
||||
self.res.as_mut().map(|res| res.head_mut())
|
||||
self.res.as_mut().map(Response::head_mut)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,10 @@ impl HttpResponse<AnyBody> {
|
||||
/// Create an error response.
|
||||
#[inline]
|
||||
pub fn from_error(error: impl Into<Error>) -> Self {
|
||||
error.into().as_response_error().error_response()
|
||||
let error = error.into();
|
||||
let mut response = error.as_response_error().error_response();
|
||||
response.error = Some(error);
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
|
122
src/route.rs
122
src/route.rs
@ -5,7 +5,7 @@ use std::{future::Future, rc::Rc};
|
||||
use actix_http::http::Method;
|
||||
use actix_service::{
|
||||
boxed::{self, BoxService, BoxServiceFactory},
|
||||
Service, ServiceFactory,
|
||||
Service, ServiceFactory, ServiceFactoryExt,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
@ -128,9 +128,10 @@ impl Route {
|
||||
|
||||
/// Set handler function, use request extractors for parameters.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::{web, http, App};
|
||||
/// use serde_derive::Deserialize;
|
||||
/// use serde::Deserialize;
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -154,7 +155,7 @@ impl Route {
|
||||
///
|
||||
/// ```
|
||||
/// # use std::collections::HashMap;
|
||||
/// # use serde_derive::Deserialize;
|
||||
/// # use serde::Deserialize;
|
||||
/// use actix_web::{web, App};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
@ -184,6 +185,53 @@ impl Route {
|
||||
self.service = boxed::factory(HandlerService::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set raw service to be constructed and called as the request handler.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use std::convert::Infallible;
|
||||
/// # use futures_util::future::LocalBoxFuture;
|
||||
/// # use actix_web::{*, dev::*, http::header};
|
||||
/// struct HelloWorld;
|
||||
///
|
||||
/// impl Service<ServiceRequest> for HelloWorld {
|
||||
/// type Response = ServiceResponse;
|
||||
/// type Error = Infallible;
|
||||
/// type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
///
|
||||
/// always_ready!();
|
||||
///
|
||||
/// fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
/// let (req, _) = req.into_parts();
|
||||
///
|
||||
/// let res = HttpResponse::Ok()
|
||||
/// .insert_header(header::ContentType::plaintext())
|
||||
/// .body("Hello world!");
|
||||
///
|
||||
/// Box::pin(async move { Ok(ServiceResponse::new(req, res)) })
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// App::new().route(
|
||||
/// "/",
|
||||
/// web::get().service(fn_factory(|| async { Ok(HelloWorld) })),
|
||||
/// );
|
||||
/// ```
|
||||
pub fn service<S, E>(mut self, service_factory: S) -> Self
|
||||
where
|
||||
S: ServiceFactory<
|
||||
ServiceRequest,
|
||||
Response = ServiceResponse,
|
||||
Error = E,
|
||||
InitError = (),
|
||||
Config = (),
|
||||
> + 'static,
|
||||
E: Into<Error> + 'static,
|
||||
{
|
||||
self.service = boxed::factory(service_factory.map_err(Into::into));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -192,9 +240,12 @@ mod tests {
|
||||
|
||||
use actix_rt::time::sleep;
|
||||
use bytes::Bytes;
|
||||
use serde_derive::Serialize;
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::http::{Method, StatusCode};
|
||||
use crate::dev::{always_ready, fn_factory, fn_service, Service};
|
||||
use crate::http::{header, Method, StatusCode};
|
||||
use crate::service::{ServiceRequest, ServiceResponse};
|
||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
||||
use crate::{error, web, App, HttpResponse};
|
||||
|
||||
@ -268,4 +319,65 @@ mod tests {
|
||||
let body = read_body(resp).await;
|
||||
assert_eq!(body, Bytes::from_static(b"{\"name\":\"test\"}"));
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_service_handler() {
|
||||
struct HelloWorld;
|
||||
|
||||
impl Service<ServiceRequest> for HelloWorld {
|
||||
type Response = ServiceResponse;
|
||||
type Error = crate::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
always_ready!();
|
||||
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
let (req, _) = req.into_parts();
|
||||
|
||||
let res = HttpResponse::Ok()
|
||||
.insert_header(header::ContentType::plaintext())
|
||||
.body("Hello world!");
|
||||
|
||||
Box::pin(async move { Ok(ServiceResponse::new(req, res)) })
|
||||
}
|
||||
}
|
||||
|
||||
let srv = init_service(
|
||||
App::new()
|
||||
.route(
|
||||
"/hello",
|
||||
web::get().service(fn_factory(|| async { Ok(HelloWorld) })),
|
||||
)
|
||||
.route(
|
||||
"/bye",
|
||||
web::get().service(fn_factory(|| async {
|
||||
Ok::<_, ()>(fn_service(|req: ServiceRequest| async {
|
||||
let (req, _) = req.into_parts();
|
||||
|
||||
let res = HttpResponse::Ok()
|
||||
.insert_header(header::ContentType::plaintext())
|
||||
.body("Goodbye, and thanks for all the fish!");
|
||||
|
||||
Ok::<_, Infallible>(ServiceResponse::new(req, res))
|
||||
}))
|
||||
})),
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
let req = TestRequest::get().uri("/hello").to_request();
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = read_body(resp).await;
|
||||
assert_eq!(body, Bytes::from_static(b"Hello world!"));
|
||||
|
||||
let req = TestRequest::get().uri("/bye").to_request();
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let body = read_body(resp).await;
|
||||
assert_eq!(
|
||||
body,
|
||||
Bytes::from_static(b"Goodbye, and thanks for all the fish!")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
210
src/scope.rs
210
src/scope.rs
@ -1,28 +1,23 @@
|
||||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
use std::future::Future;
|
||||
use std::rc::Rc;
|
||||
use std::{cell::RefCell, fmt, future::Future, mem, rc::Rc};
|
||||
|
||||
use actix_http::Extensions;
|
||||
use actix_router::{ResourceDef, Router};
|
||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||
use actix_service::{
|
||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt,
|
||||
Transform,
|
||||
apply, apply_fn_factory,
|
||||
boxed::{self, BoxService, BoxServiceFactory},
|
||||
IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt, Transform,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use futures_util::future::join_all;
|
||||
|
||||
use crate::config::ServiceConfig;
|
||||
use crate::data::Data;
|
||||
use crate::dev::{AppService, HttpServiceFactory};
|
||||
use crate::error::Error;
|
||||
use crate::guard::Guard;
|
||||
use crate::resource::Resource;
|
||||
use crate::rmap::ResourceMap;
|
||||
use crate::route::Route;
|
||||
use crate::service::{
|
||||
AppServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
||||
use crate::{
|
||||
config::ServiceConfig,
|
||||
data::Data,
|
||||
dev::{AppService, HttpServiceFactory},
|
||||
guard::Guard,
|
||||
rmap::ResourceMap,
|
||||
service::{AppServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse},
|
||||
Error, Resource, Route,
|
||||
};
|
||||
|
||||
type Guards = Vec<Box<dyn Guard>>;
|
||||
@ -71,16 +66,17 @@ pub struct Scope<T = ScopeEndpoint> {
|
||||
impl Scope {
|
||||
/// Create a new scope
|
||||
pub fn new(path: &str) -> Scope {
|
||||
let fref = Rc::new(RefCell::new(None));
|
||||
let factory_ref = Rc::new(RefCell::new(None));
|
||||
|
||||
Scope {
|
||||
endpoint: ScopeEndpoint::new(fref.clone()),
|
||||
endpoint: ScopeEndpoint::new(Rc::clone(&factory_ref)),
|
||||
rdef: path.to_string(),
|
||||
app_data: None,
|
||||
guards: Vec::new(),
|
||||
services: Vec::new(),
|
||||
default: None,
|
||||
external: Vec::new(),
|
||||
factory_ref: fref,
|
||||
factory_ref,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,39 +116,38 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Set or override application data. Application data could be accessed
|
||||
/// by using `Data<T>` extractor where `T` is data type.
|
||||
///
|
||||
/// ```
|
||||
/// use std::cell::Cell;
|
||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
||||
///
|
||||
/// struct MyData {
|
||||
/// counter: Cell<usize>,
|
||||
/// }
|
||||
///
|
||||
/// async fn index(data: web::Data<MyData>) -> impl Responder {
|
||||
/// data.counter.set(data.counter.get() + 1);
|
||||
/// HttpResponse::Ok()
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().service(
|
||||
/// web::scope("/app")
|
||||
/// .data(MyData{ counter: Cell::new(0) })
|
||||
/// .service(
|
||||
/// web::resource("/index.html").route(
|
||||
/// web::get().to(index)))
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||
self.app_data(Data::new(data))
|
||||
}
|
||||
|
||||
/// Add scope data.
|
||||
///
|
||||
/// Data of different types from parent contexts will still be accessible.
|
||||
/// Data of different types from parent contexts will still be accessible. Any `Data<T>` types
|
||||
/// set here can be extracted in handlers using the `Data<T>` extractor.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::cell::Cell;
|
||||
/// use actix_web::{web, App, HttpRequest, HttpResponse, Responder};
|
||||
///
|
||||
/// struct MyData {
|
||||
/// count: std::cell::Cell<usize>,
|
||||
/// }
|
||||
///
|
||||
/// async fn handler(req: HttpRequest, counter: web::Data<MyData>) -> impl Responder {
|
||||
/// // note this cannot use the Data<T> extractor because it was not added with it
|
||||
/// let incr = *req.app_data::<usize>().unwrap();
|
||||
/// assert_eq!(incr, 3);
|
||||
///
|
||||
/// // update counter using other value from app data
|
||||
/// counter.count.set(counter.count.get() + incr);
|
||||
///
|
||||
/// HttpResponse::Ok().body(counter.count.get().to_string())
|
||||
/// }
|
||||
///
|
||||
/// let app = App::new().service(
|
||||
/// web::scope("/app")
|
||||
/// .app_data(3usize)
|
||||
/// .app_data(web::Data::new(MyData { count: Default::default() }))
|
||||
/// .route("/", web::get().to(handler))
|
||||
/// );
|
||||
/// ```
|
||||
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||
self.app_data
|
||||
.get_or_insert_with(Extensions::new)
|
||||
@ -161,15 +156,20 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Run external configuration as part of the scope building
|
||||
/// process
|
||||
/// Add scope data after wrapping in `Data<T>`.
|
||||
///
|
||||
/// This function is useful for moving parts of configuration to a
|
||||
/// different module or even library. For example,
|
||||
/// some of the resource's configuration could be moved to different module.
|
||||
/// Deprecated in favor of [`app_data`](Self::app_data).
|
||||
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||
self.app_data(Data::new(data))
|
||||
}
|
||||
|
||||
/// Run external configuration as part of the scope building process.
|
||||
///
|
||||
/// This function is useful for moving parts of configuration to a different module or library.
|
||||
/// For example, some of the resource's configuration could be moved to different module.
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{web, middleware, App, HttpResponse};
|
||||
///
|
||||
/// // this function could be located in different module
|
||||
@ -190,18 +190,21 @@ where
|
||||
/// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn configure<F>(mut self, f: F) -> Self
|
||||
pub fn configure<F>(mut self, cfg_fn: F) -> Self
|
||||
where
|
||||
F: FnOnce(&mut ServiceConfig),
|
||||
{
|
||||
let mut cfg = ServiceConfig::new();
|
||||
f(&mut cfg);
|
||||
cfg_fn(&mut cfg);
|
||||
|
||||
self.services.extend(cfg.services);
|
||||
self.external.extend(cfg.external);
|
||||
|
||||
// TODO: add Extensions::is_empty check and conditionally insert data
|
||||
self.app_data
|
||||
.get_or_insert_with(Extensions::new)
|
||||
.extend(cfg.app_data);
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
@ -418,13 +421,12 @@ where
|
||||
let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef));
|
||||
|
||||
// external resources
|
||||
for mut rdef in std::mem::take(&mut self.external) {
|
||||
for mut rdef in mem::take(&mut self.external) {
|
||||
rmap.add(&mut rdef, None);
|
||||
}
|
||||
|
||||
// complete scope pipeline creation
|
||||
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
||||
app_data: self.app_data.take().map(Rc::new),
|
||||
default,
|
||||
services: cfg
|
||||
.into_services()
|
||||
@ -446,18 +448,28 @@ where
|
||||
Some(self.guards)
|
||||
};
|
||||
|
||||
let scope_data = self.app_data.map(Rc::new);
|
||||
|
||||
// wraps endpoint service (including middleware) call and injects app data for this scope
|
||||
let endpoint = apply_fn_factory(self.endpoint, move |mut req: ServiceRequest, srv| {
|
||||
if let Some(ref data) = scope_data {
|
||||
req.add_data_container(Rc::clone(data));
|
||||
}
|
||||
|
||||
srv.call(req)
|
||||
});
|
||||
|
||||
// register final service
|
||||
config.register_service(
|
||||
ResourceDef::root_prefix(&self.rdef),
|
||||
guards,
|
||||
self.endpoint,
|
||||
endpoint,
|
||||
Some(Rc::new(rmap)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScopeFactory {
|
||||
app_data: Option<Rc<Extensions>>,
|
||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
||||
default: Rc<HttpNewService>,
|
||||
}
|
||||
@ -485,8 +497,6 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||
}
|
||||
}));
|
||||
|
||||
let app_data = self.app_data.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let default = default_fut.await?;
|
||||
|
||||
@ -502,17 +512,12 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||
})
|
||||
.finish();
|
||||
|
||||
Ok(ScopeService {
|
||||
app_data,
|
||||
router,
|
||||
default,
|
||||
})
|
||||
Ok(ScopeService { router, default })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScopeService {
|
||||
app_data: Option<Rc<Extensions>>,
|
||||
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
||||
default: HttpService,
|
||||
}
|
||||
@ -536,10 +541,6 @@ impl Service<ServiceRequest> for ScopeService {
|
||||
true
|
||||
});
|
||||
|
||||
if let Some(ref app_data) = self.app_data {
|
||||
req.add_data_container(app_data.clone());
|
||||
}
|
||||
|
||||
if let Some((srv, _info)) = res {
|
||||
srv.call(req)
|
||||
} else {
|
||||
@ -578,12 +579,15 @@ mod tests {
|
||||
use actix_utils::future::ok;
|
||||
use bytes::Bytes;
|
||||
|
||||
use crate::dev::Body;
|
||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||
use crate::middleware::DefaultHeaders;
|
||||
use crate::service::ServiceRequest;
|
||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
||||
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
||||
use crate::{
|
||||
dev::Body,
|
||||
guard,
|
||||
http::{header, HeaderValue, Method, StatusCode},
|
||||
middleware::DefaultHeaders,
|
||||
service::{ServiceRequest, ServiceResponse},
|
||||
test::{call_service, init_service, read_body, TestRequest},
|
||||
web, App, HttpMessage, HttpRequest, HttpResponse,
|
||||
};
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_scope() {
|
||||
@ -915,10 +919,7 @@ mod tests {
|
||||
async fn test_default_resource_propagation() {
|
||||
let srv = init_service(
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.default_service(web::resource("").to(HttpResponse::BadRequest)),
|
||||
)
|
||||
.service(web::scope("/app1").default_service(web::to(HttpResponse::BadRequest)))
|
||||
.service(web::scope("/app2"))
|
||||
.default_service(|r: ServiceRequest| {
|
||||
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
||||
@ -990,6 +991,43 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_middleware_app_data() {
|
||||
let srv = init_service(
|
||||
App::new().service(
|
||||
web::scope("app")
|
||||
.app_data(1usize)
|
||||
.wrap_fn(|req, srv| {
|
||||
assert_eq!(req.app_data::<usize>(), Some(&1usize));
|
||||
req.extensions_mut().insert(1usize);
|
||||
srv.call(req)
|
||||
})
|
||||
.route("/test", web::get().to(HttpResponse::Ok))
|
||||
.default_service(|req: ServiceRequest| async move {
|
||||
let (req, _) = req.into_parts();
|
||||
|
||||
assert_eq!(req.extensions().get::<usize>(), Some(&1));
|
||||
|
||||
Ok(ServiceResponse::new(
|
||||
req,
|
||||
HttpResponse::BadRequest().finish(),
|
||||
))
|
||||
}),
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
let req = TestRequest::with_uri("/app/test").to_request();
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::with_uri("/app/default").to_request();
|
||||
let resp = call_service(&srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
// allow deprecated {App, Scope}::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_override_data() {
|
||||
let srv = init_service(App::new().data(1usize).service(
|
||||
@ -1008,6 +1046,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
// allow deprecated `{App, Scope}::data`
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_override_data_default_service() {
|
||||
let srv = init_service(App::new().data(1usize).service(
|
||||
|
@ -292,15 +292,15 @@ where
|
||||
let c = cfg.lock().unwrap();
|
||||
let host = c.host.clone().unwrap_or_else(|| format!("{}", addr));
|
||||
|
||||
let svc = HttpService::build()
|
||||
let mut svc = HttpService::build()
|
||||
.keep_alive(c.keep_alive)
|
||||
.client_timeout(c.client_timeout)
|
||||
.local_addr(addr);
|
||||
|
||||
let svc = if let Some(handler) = on_connect_fn.clone() {
|
||||
svc.on_connect_ext(move |io: &_, ext: _| (handler)(io as &dyn Any, ext))
|
||||
} else {
|
||||
svc
|
||||
if let Some(handler) = on_connect_fn.clone() {
|
||||
svc = svc.on_connect_ext(move |io: &_, ext: _| {
|
||||
(handler)(io as &dyn Any, ext)
|
||||
})
|
||||
};
|
||||
|
||||
let fac = factory()
|
||||
@ -461,17 +461,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
if !success {
|
||||
if let Some(e) = err.take() {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Can not bind to address.",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
if success {
|
||||
Ok(sockets)
|
||||
} else if let Some(e) = err.take() {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Can not bind to address.",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@ -537,15 +535,14 @@ where
|
||||
);
|
||||
|
||||
fn_service(|io: UnixStream| async { Ok((io, Protocol::Http1, None)) }).and_then({
|
||||
let svc = HttpService::build()
|
||||
let mut svc = HttpService::build()
|
||||
.keep_alive(c.keep_alive)
|
||||
.client_timeout(c.client_timeout);
|
||||
|
||||
let svc = if let Some(handler) = on_connect_fn.clone() {
|
||||
svc.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext))
|
||||
} else {
|
||||
svc
|
||||
};
|
||||
if let Some(handler) = on_connect_fn.clone() {
|
||||
svc = svc
|
||||
.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext));
|
||||
}
|
||||
|
||||
let fac = factory()
|
||||
.into_factory()
|
||||
|
@ -17,9 +17,8 @@ use crate::{
|
||||
dev::insert_slash,
|
||||
guard::Guard,
|
||||
info::ConnectionInfo,
|
||||
request::HttpRequest,
|
||||
rmap::ResourceMap,
|
||||
Error, HttpResponse,
|
||||
Error, HttpRequest, HttpResponse,
|
||||
};
|
||||
|
||||
pub trait HttpServiceFactory {
|
||||
@ -74,18 +73,18 @@ impl ServiceRequest {
|
||||
Self { req, payload }
|
||||
}
|
||||
|
||||
/// Construct service request.
|
||||
#[doc(hidden)]
|
||||
pub fn __priv_test_new(req: HttpRequest, payload: Payload) -> Self {
|
||||
Self::new(req, payload)
|
||||
}
|
||||
|
||||
/// Deconstruct request into parts
|
||||
#[inline]
|
||||
pub fn into_parts(self) -> (HttpRequest, Payload) {
|
||||
(self.req, self.payload)
|
||||
}
|
||||
|
||||
/// Get mutable access to inner `HttpRequest` and `Payload`
|
||||
#[inline]
|
||||
pub fn parts_mut(&mut self) -> (&mut HttpRequest, &mut Payload) {
|
||||
(&mut self.req, &mut self.payload)
|
||||
}
|
||||
|
||||
/// Construct request from parts.
|
||||
pub fn from_parts(req: HttpRequest, payload: Payload) -> Self {
|
||||
Self { req, payload }
|
||||
@ -168,11 +167,7 @@ impl ServiceRequest {
|
||||
/// E.g., id=10
|
||||
#[inline]
|
||||
pub fn query_string(&self) -> &str {
|
||||
if let Some(query) = self.uri().query().as_ref() {
|
||||
query
|
||||
} else {
|
||||
""
|
||||
}
|
||||
self.uri().query().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Peer socket address.
|
||||
@ -655,6 +650,8 @@ mod tests {
|
||||
assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_service_data() {
|
||||
let srv =
|
||||
|
@ -613,6 +613,11 @@ impl TestRequest {
|
||||
let req = self.to_request();
|
||||
call_service(app, req).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn set_server_hostname(&mut self, host: &str) {
|
||||
self.config.set_host(host)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -839,6 +844,8 @@ mod tests {
|
||||
assert!(res.status().is_success());
|
||||
}
|
||||
|
||||
// allow deprecated App::data
|
||||
#[allow(deprecated)]
|
||||
#[actix_rt::test]
|
||||
async fn test_server_data() {
|
||||
async fn handler(data: web::Data<usize>) -> impl Responder {
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! For URL encoded form helper documentation, see [`Form`].
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fmt,
|
||||
future::Future,
|
||||
ops,
|
||||
@ -16,7 +17,7 @@ use futures_core::{future::LocalBoxFuture, ready};
|
||||
use futures_util::{FutureExt as _, StreamExt as _};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
use crate::dev::Decompress;
|
||||
use crate::{
|
||||
error::UrlencodedError, extract::FromRequest, http::header::CONTENT_LENGTH, web, Error,
|
||||
@ -80,6 +81,10 @@ use crate::{
|
||||
/// })
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Panics
|
||||
/// URL encoded forms consist of unordered `key=value` pairs, therefore they cannot be decoded into
|
||||
/// any type which depends upon data ordering (eg. tuples). Trying to do so will result in a panic.
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||
pub struct Form<T>(pub T);
|
||||
|
||||
@ -255,9 +260,9 @@ impl Default for FormConfig {
|
||||
/// - content type is not `application/x-www-form-urlencoded`
|
||||
/// - content length is greater than [limit](UrlEncoded::limit())
|
||||
pub struct UrlEncoded<T> {
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
stream: Option<Decompress<Payload>>,
|
||||
#[cfg(not(feature = "compress"))]
|
||||
#[cfg(not(feature = "__compress"))]
|
||||
stream: Option<Payload>,
|
||||
|
||||
limit: usize,
|
||||
@ -293,10 +298,15 @@ impl<T> UrlEncoded<T> {
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
let payload = Decompress::from_headers(payload.take(), req.headers());
|
||||
#[cfg(not(feature = "compress"))]
|
||||
let payload = payload.take();
|
||||
let payload = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "__compress")] {
|
||||
Decompress::from_headers(payload.take(), req.headers())
|
||||
} else {
|
||||
payload.take()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
UrlEncoded {
|
||||
encoding,
|
||||
@ -375,7 +385,7 @@ where
|
||||
} else {
|
||||
let body = encoding
|
||||
.decode_without_bom_handling_and_without_replacement(&body)
|
||||
.map(|s| s.into_owned())
|
||||
.map(Cow::into_owned)
|
||||
.ok_or(UrlencodedError::Encoding)?;
|
||||
|
||||
serde_urlencoded::from_str::<T>(&body).map_err(UrlencodedError::Parse)
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user