mirror of
https://github.com/fafhrd91/actix-web
synced 2025-08-23 05:55:13 +02:00
Compare commits
20 Commits
test-v0.1.
...
actors-v4.
Author | SHA1 | Date | |
---|---|---|---|
|
604be5495f | ||
|
262c6bc828 | ||
|
5eba95b731 | ||
|
09afd033fc | ||
|
539697292a | ||
|
5a480d1d78 | ||
|
9a26393375 | ||
|
2eacb735a4 | ||
|
767e4efe22 | ||
|
e559a197cc | ||
|
93aa86e30b | ||
|
2d8d2f5ab0 | ||
|
083ee05d50 | ||
|
ed0516d724 | ||
|
7535a1ade8 | ||
|
8846808804 | ||
|
3b6333e65f | ||
|
b1148fd735 | ||
|
12f7720309 | ||
|
2d8530feb3 |
@@ -3,6 +3,7 @@ chk = "check --workspace --all-features --tests --examples --bins"
|
|||||||
lint = "clippy --workspace --tests --examples"
|
lint = "clippy --workspace --tests --examples"
|
||||||
ci-min = "hack check --workspace --no-default-features"
|
ci-min = "hack check --workspace --no-default-features"
|
||||||
ci-min-test = "hack check --workspace --no-default-features --tests --examples"
|
ci-min-test = "hack check --workspace --no-default-features --tests --examples"
|
||||||
ci-default = "hack check --workspace"
|
ci-default = "check --workspace --bins --tests --examples"
|
||||||
ci-full = "check --workspace --bins --examples --tests"
|
ci-full = "check --workspace --all-features --bins --tests --examples"
|
||||||
ci-test = "test --workspace --all-features --no-fail-fast"
|
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
|
blank_issues_enabled: true
|
||||||
contact_links:
|
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
|
- name: Actix Discord
|
||||||
url: https://discord.gg/NWpN5mmg3x
|
url: https://discord.gg/NWpN5mmg3x
|
||||||
about: Actix developer discussion and community chat
|
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
|
profile: minimal
|
||||||
override: true
|
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
|
- name: Generate Cargo.lock
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
@@ -66,43 +59,33 @@ jobs:
|
|||||||
|
|
||||||
- name: check minimal
|
- name: check minimal
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with: { command: ci-min }
|
||||||
command: hack
|
|
||||||
args: check --workspace --no-default-features
|
|
||||||
|
|
||||||
- name: check minimal + tests
|
- name: check minimal + tests
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with: { command: ci-min-test }
|
||||||
command: hack
|
|
||||||
args: check --workspace --no-default-features --tests --examples
|
|
||||||
|
|
||||||
|
- name: check default
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with: { command: ci-default }
|
||||||
|
|
||||||
- name: check full
|
- name: check full
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with: { command: ci-full }
|
||||||
command: check
|
|
||||||
args: --workspace --bins --examples --tests
|
|
||||||
|
|
||||||
- name: tests
|
- 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
|
uses: actions-rs/cargo@v1
|
||||||
timeout-minutes: 40
|
timeout-minutes: 40
|
||||||
with:
|
with:
|
||||||
command: test
|
command: ci-test
|
||||||
args: --package=actix-http --no-default-features --features=rustls -- --nocapture
|
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
|
uses: actions-rs/cargo@v1
|
||||||
timeout-minutes: 40
|
timeout-minutes: 40
|
||||||
with:
|
with: { command: ci-doctest }
|
||||||
command: test
|
|
||||||
args: --package=awc --no-default-features --features=rustls -- --nocapture
|
|
||||||
|
|
||||||
- name: Generate coverage file
|
- name: Generate coverage file
|
||||||
if: >
|
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
|
uses: actions-rs/clippy-check@v1
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
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
|
# Configuration directory generated by CLion
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
|
# Configuration directory generated by VSCode
|
||||||
|
.vscode
|
||||||
|
20
CHANGES.md
20
CHANGES.md
@@ -1,10 +1,30 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## 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
|
### Changed
|
||||||
* Change compression algorithm features flags. [#2250]
|
* 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
|
[#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
|
## 4.0.0-beta.7 - 2021-06-17
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web"
|
name = "actix-web"
|
||||||
version = "4.0.0-beta.7"
|
version = "4.0.0-beta.8"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
|
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
|
||||||
keywords = ["actix", "http", "web", "framework", "async"]
|
keywords = ["actix", "http", "web", "framework", "async"]
|
||||||
@@ -75,7 +75,7 @@ actix-utils = "3.0.0"
|
|||||||
actix-tls = { version = "3.0.0-beta.5", default-features = false, optional = true }
|
actix-tls = { version = "3.0.0-beta.5", default-features = false, optional = true }
|
||||||
|
|
||||||
actix-web-codegen = "0.5.0-beta.2"
|
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"
|
ahash = "0.7"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
@@ -104,16 +104,15 @@ url = "2.1"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
actix-test = { version = "0.1.0-beta.3", features = ["openssl", "rustls"] }
|
||||||
awc = { version = "3.0.0-beta.6", features = ["openssl"] }
|
awc = { version = "3.0.0-beta.7", features = ["openssl"] }
|
||||||
|
|
||||||
brotli2 = "0.3.2"
|
brotli2 = "0.3.2"
|
||||||
criterion = "0.3"
|
criterion = { version = "0.3", features = ["html_reports"] }
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
flate2 = "1.0.13"
|
flate2 = "1.0.13"
|
||||||
zstd = "0.7"
|
zstd = "0.7"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rcgen = "0.8"
|
rcgen = "0.8"
|
||||||
serde_derive = "1.0"
|
|
||||||
tls-openssl = { package = "openssl", version = "0.10.9" }
|
tls-openssl = { package = "openssl", version = "0.10.9" }
|
||||||
tls-rustls = { package = "rustls", version = "0.19.0" }
|
tls-rustls = { package = "rustls", version = "0.19.0" }
|
||||||
|
|
||||||
|
@@ -6,10 +6,10 @@
|
|||||||
<p>
|
<p>
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-web)
|
[](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://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 />
|
<br />
|
||||||
[](https://github.com/actix/actix-web/actions)
|
[](https://github.com/actix/actix-web/actions)
|
||||||
[](https://codecov.io/gh/actix/actix-web)
|
[](https://codecov.io/gh/actix/actix-web)
|
||||||
|
@@ -3,27 +3,32 @@
|
|||||||
## Unreleased - 2021-xx-xx
|
## 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
|
## 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]
|
* `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]
|
* For symbolic links, `Content-Disposition` header no longer shows the filename of the original file. [#2156]
|
||||||
* `Files::redirect_to_slash_directory()` now works as expected when used with `Files::show_files_listing()`. [#2225]
|
* `Files::redirect_to_slash_directory()` now works as expected when used with `Files::show_files_listing()`. [#2225]
|
||||||
* `application/{javascript, json, wasm}` mime type now have `inline` disposition by default. [#2257]
|
* `application/{javascript, json, wasm}` mime type now have `inline` disposition by default. [#2257]
|
||||||
* `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]
|
|
||||||
|
|
||||||
[#2135]: https://github.com/actix/actix-web/pull/2135
|
[#2135]: https://github.com/actix/actix-web/pull/2135
|
||||||
[#2156]: https://github.com/actix/actix-web/pull/2156
|
[#2156]: https://github.com/actix/actix-web/pull/2156
|
||||||
[#2225]: https://github.com/actix/actix-web/pull/2225
|
[#2225]: https://github.com/actix/actix-web/pull/2225
|
||||||
[#2257]: https://github.com/actix/actix-web/pull/2257
|
[#2257]: https://github.com/actix/actix-web/pull/2257
|
||||||
[#2228]: https://github.com/actix/actix-web/pull/2228
|
|
||||||
|
|
||||||
|
|
||||||
## 0.6.0-beta.4 - 2021-04-02
|
## 0.6.0-beta.4 - 2021-04-02
|
||||||
* No notable changes.
|
|
||||||
|
|
||||||
* Add support for `.guard` in `Files` to selectively filter `Files` services. [#2046]
|
* Add support for `.guard` in `Files` to selectively filter `Files` services. [#2046]
|
||||||
|
|
||||||
[#2046]: https://github.com/actix/actix-web/pull/2046
|
[#2046]: https://github.com/actix/actix-web/pull/2046
|
||||||
|
|
||||||
|
|
||||||
## 0.6.0-beta.3 - 2021-03-09
|
## 0.6.0-beta.3 - 2021-03-09
|
||||||
* No notable changes.
|
* No notable changes.
|
||||||
|
|
||||||
|
@@ -1,13 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.6.0-beta.5"
|
version = "0.6.0-beta.6"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Static file serving for Actix Web"
|
description = "Static file serving for Actix Web"
|
||||||
readme = "README.md"
|
|
||||||
keywords = ["actix", "http", "async", "futures"]
|
keywords = ["actix", "http", "async", "futures"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web"
|
||||||
documentation = "https://docs.rs/actix-files/"
|
|
||||||
categories = ["asynchronous", "web-programming::http-server"]
|
categories = ["asynchronous", "web-programming::http-server"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
@@ -17,8 +15,8 @@ name = "actix_files"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
actix-web = { version = "4.0.0-beta.8", default-features = false }
|
||||||
actix-http = "3.0.0-beta.7"
|
actix-http = "3.0.0-beta.8"
|
||||||
actix-service = "2.0.0"
|
actix-service = "2.0.0"
|
||||||
actix-utils = "3.0.0"
|
actix-utils = "3.0.0"
|
||||||
|
|
||||||
@@ -35,5 +33,5 @@ percent-encoding = "2.1"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-web = "4.0.0-beta.7"
|
actix-web = "4.0.0-beta.8"
|
||||||
actix-test = "0.1.0-beta.3"
|
actix-test = "0.1.0-beta.3"
|
||||||
|
@@ -3,17 +3,16 @@
|
|||||||
> Static file serving for Actix Web
|
> Static file serving for Actix Web
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-files)
|
[](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)
|
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||||

|

|
||||||
<br />
|
<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://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
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/actix-files/)
|
- [API Documentation](https://docs.rs/actix-files/)
|
||||||
- [Example Project](https://github.com/actix/examples/tree/master/basics/static_index)
|
- [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
|
- 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_service::{boxed, IntoServiceFactory, ServiceFactory, ServiceFactoryExt};
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{AppService, HttpServiceFactory, ResourceDef, ServiceRequest, ServiceResponse},
|
dev::{
|
||||||
|
AppService, HttpServiceFactory, RequestHead, ResourceDef, ServiceRequest,
|
||||||
|
ServiceResponse,
|
||||||
|
},
|
||||||
error::Error,
|
error::Error,
|
||||||
guard::Guard,
|
guard::Guard,
|
||||||
http::header::DispositionType,
|
http::header::DispositionType,
|
||||||
@@ -13,7 +21,7 @@ use futures_core::future::LocalBoxFuture;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
directory_listing, named, Directory, DirectoryRenderer, FilesService, HttpNewService,
|
directory_listing, named, Directory, DirectoryRenderer, FilesService, HttpNewService,
|
||||||
MimeOverride,
|
MimeOverride, PathFilter,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Static files handling service.
|
/// Static files handling service.
|
||||||
@@ -36,6 +44,7 @@ pub struct Files {
|
|||||||
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
||||||
renderer: Rc<DirectoryRenderer>,
|
renderer: Rc<DirectoryRenderer>,
|
||||||
mime_override: Option<Rc<MimeOverride>>,
|
mime_override: Option<Rc<MimeOverride>>,
|
||||||
|
path_filter: Option<Rc<PathFilter>>,
|
||||||
file_flags: named::Flags,
|
file_flags: named::Flags,
|
||||||
use_guards: Option<Rc<dyn Guard>>,
|
use_guards: Option<Rc<dyn Guard>>,
|
||||||
guards: Vec<Rc<dyn Guard>>,
|
guards: Vec<Rc<dyn Guard>>,
|
||||||
@@ -60,6 +69,7 @@ impl Clone for Files {
|
|||||||
file_flags: self.file_flags,
|
file_flags: self.file_flags,
|
||||||
path: self.path.clone(),
|
path: self.path.clone(),
|
||||||
mime_override: self.mime_override.clone(),
|
mime_override: self.mime_override.clone(),
|
||||||
|
path_filter: self.path_filter.clone(),
|
||||||
use_guards: self.use_guards.clone(),
|
use_guards: self.use_guards.clone(),
|
||||||
guards: self.guards.clone(),
|
guards: self.guards.clone(),
|
||||||
hidden_files: self.hidden_files,
|
hidden_files: self.hidden_files,
|
||||||
@@ -104,6 +114,7 @@ impl Files {
|
|||||||
default: Rc::new(RefCell::new(None)),
|
default: Rc::new(RefCell::new(None)),
|
||||||
renderer: Rc::new(directory_listing),
|
renderer: Rc::new(directory_listing),
|
||||||
mime_override: None,
|
mime_override: None,
|
||||||
|
path_filter: None,
|
||||||
file_flags: named::Flags::default(),
|
file_flags: named::Flags::default(),
|
||||||
use_guards: None,
|
use_guards: None,
|
||||||
guards: Vec::new(),
|
guards: Vec::new(),
|
||||||
@@ -149,6 +160,38 @@ impl Files {
|
|||||||
self
|
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
|
/// Set index file
|
||||||
///
|
///
|
||||||
/// Shows specific index file for directories instead of
|
/// Shows specific index file for directories instead of
|
||||||
@@ -318,6 +361,7 @@ impl ServiceFactory<ServiceRequest> for Files {
|
|||||||
default: None,
|
default: None,
|
||||||
renderer: self.renderer.clone(),
|
renderer: self.renderer.clone(),
|
||||||
mime_override: self.mime_override.clone(),
|
mime_override: self.mime_override.clone(),
|
||||||
|
path_filter: self.path_filter.clone(),
|
||||||
file_flags: self.file_flags,
|
file_flags: self.file_flags,
|
||||||
guards: self.use_guards.clone(),
|
guards: self.use_guards.clone(),
|
||||||
hidden_files: self.hidden_files,
|
hidden_files: self.hidden_files,
|
||||||
|
@@ -16,11 +16,12 @@
|
|||||||
|
|
||||||
use actix_service::boxed::{BoxService, BoxServiceFactory};
|
use actix_service::boxed::{BoxService, BoxServiceFactory};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{ServiceRequest, ServiceResponse},
|
dev::{RequestHead, ServiceRequest, ServiceResponse},
|
||||||
error::Error,
|
error::Error,
|
||||||
http::header::DispositionType,
|
http::header::DispositionType,
|
||||||
};
|
};
|
||||||
use mime_guess::from_ext;
|
use mime_guess::from_ext;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
mod chunked;
|
mod chunked;
|
||||||
mod directory;
|
mod directory;
|
||||||
@@ -56,6 +57,8 @@ pub fn file_extension_to_mime(ext: &str) -> mime::Mime {
|
|||||||
|
|
||||||
type MimeOverride = dyn Fn(&mime::Name<'_>) -> DispositionType;
|
type MimeOverride = dyn Fn(&mime::Name<'_>) -> DispositionType;
|
||||||
|
|
||||||
|
type PathFilter = dyn Fn(&Path, &RequestHead) -> bool;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{
|
use std::{
|
||||||
@@ -901,4 +904,40 @@ mod tests {
|
|||||||
let bytes = test::read_body(resp).await;
|
let bytes = test::read_body(resp).await;
|
||||||
assert!(format!("{:?}", bytes).contains("/tests/test.png"));
|
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))) =
|
} else if let (Some(ref m), Some(header::IfUnmodifiedSince(ref since))) =
|
||||||
(last_modified, req.get_header())
|
(last_modified, req.get_header())
|
||||||
{
|
{
|
||||||
let t1: SystemTime = m.clone().into();
|
let t1: SystemTime = (*m).into();
|
||||||
let t2: SystemTime = since.clone().into();
|
let t2: SystemTime = (*since).into();
|
||||||
|
|
||||||
match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
|
match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
|
||||||
(Ok(t1), Ok(t2)) => t1.as_secs() > t2.as_secs(),
|
(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))) =
|
} else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) =
|
||||||
(last_modified, req.get_header())
|
(last_modified, req.get_header())
|
||||||
{
|
{
|
||||||
let t1: SystemTime = m.clone().into();
|
let t1: SystemTime = (*m).into();
|
||||||
let t2: SystemTime = since.clone().into();
|
let t2: SystemTime = (*since).into();
|
||||||
|
|
||||||
match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
|
match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
|
||||||
(Ok(t1), Ok(t2)) => t1.as_secs() <= t2.as_secs(),
|
(Ok(t1), Ok(t2)) => t1.as_secs() <= t2.as_secs(),
|
||||||
|
@@ -13,7 +13,7 @@ use futures_core::future::LocalBoxFuture;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
named, Directory, DirectoryRenderer, FilesError, HttpService, MimeOverride, NamedFile,
|
named, Directory, DirectoryRenderer, FilesError, HttpService, MimeOverride, NamedFile,
|
||||||
PathBufWrap,
|
PathBufWrap, PathFilter,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Assembled file serving service.
|
/// Assembled file serving service.
|
||||||
@@ -25,6 +25,7 @@ pub struct FilesService {
|
|||||||
pub(crate) default: Option<HttpService>,
|
pub(crate) default: Option<HttpService>,
|
||||||
pub(crate) renderer: Rc<DirectoryRenderer>,
|
pub(crate) renderer: Rc<DirectoryRenderer>,
|
||||||
pub(crate) mime_override: Option<Rc<MimeOverride>>,
|
pub(crate) mime_override: Option<Rc<MimeOverride>>,
|
||||||
|
pub(crate) path_filter: Option<Rc<PathFilter>>,
|
||||||
pub(crate) file_flags: named::Flags,
|
pub(crate) file_flags: named::Flags,
|
||||||
pub(crate) guards: Option<Rc<dyn Guard>>,
|
pub(crate) guards: Option<Rc<dyn Guard>>,
|
||||||
pub(crate) hidden_files: bool,
|
pub(crate) hidden_files: bool,
|
||||||
@@ -82,6 +83,18 @@ impl Service<ServiceRequest> for FilesService {
|
|||||||
Err(e) => return Box::pin(ok(req.error_response(e))),
|
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
|
// full file path
|
||||||
let path = self.directory.join(&real_path);
|
let path = self.directory.join(&real_path);
|
||||||
if let Err(err) = path.canonicalize() {
|
if let Err(err) = path.canonicalize() {
|
||||||
|
@@ -35,7 +35,7 @@ actix-tls = "3.0.0-beta.5"
|
|||||||
actix-utils = "3.0.0"
|
actix-utils = "3.0.0"
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-server = "2.0.0-beta.3"
|
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"
|
base64 = "0.13"
|
||||||
bytes = "1"
|
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 }
|
tls-openssl = { version = "0.10.9", package = "openssl", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
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-http = "3.0.0-beta.7"
|
actix-http = "3.0.0-beta.8"
|
||||||
|
@@ -4,12 +4,14 @@
|
|||||||
|
|
||||||
[](https://crates.io/crates/actix-http-test)
|
[](https://crates.io/crates/actix-http-test)
|
||||||
[](https://docs.rs/actix-http-test/3.0.0-beta.4)
|
[](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://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
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/actix-http-test)
|
- [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
|
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||||
|
@@ -1,9 +1,16 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-beta.8 - 2021-06-26
|
||||||
### Changed
|
### Changed
|
||||||
* Change compression algorithm features flags. [#2250]
|
* 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
|
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,13 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-http"
|
name = "actix-http"
|
||||||
version = "3.0.0-beta.7"
|
version = "3.0.0-beta.8"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "HTTP primitives for the Actix ecosystem"
|
description = "HTTP primitives for the Actix ecosystem"
|
||||||
readme = "README.md"
|
|
||||||
keywords = ["actix", "http", "framework", "async", "futures"]
|
keywords = ["actix", "http", "framework", "async", "futures"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web"
|
||||||
documentation = "https://docs.rs/actix-http/"
|
|
||||||
categories = ["network-programming", "asynchronous",
|
categories = ["network-programming", "asynchronous",
|
||||||
"web-programming::http-server",
|
"web-programming::http-server",
|
||||||
"web-programming::websocket"]
|
"web-programming::websocket"]
|
||||||
|
@@ -3,18 +3,17 @@
|
|||||||
> HTTP primitives for the Actix ecosystem.
|
> HTTP primitives for the Actix ecosystem.
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-http)
|
[](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)
|
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||||

|

|
||||||
<br />
|
<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://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
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/actix-http)
|
- [API Documentation](https://docs.rs/actix-http)
|
||||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
|
||||||
- Minimum Supported Rust Version (MSRV): 1.46.0
|
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
@@ -78,12 +78,12 @@ impl HeaderIndex {
|
|||||||
// test cases taken from:
|
// test cases taken from:
|
||||||
// https://github.com/seanmonstar/httparse/blob/master/benches/parse.rs
|
// 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\
|
GET / HTTP/1.0\r\n\
|
||||||
Host: example.com\r\n\
|
Host: example.com\r\n\
|
||||||
Cookie: session=60; user_id=1\r\n\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\
|
GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n\
|
||||||
Host: www.kittyhell.com\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\
|
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;
|
use std::mem::MaybeUninit;
|
||||||
|
|
||||||
pub fn parse_headers(src: &mut BytesMut) -> usize {
|
pub fn parse_headers(src: &mut BytesMut) -> usize {
|
||||||
|
#![allow(clippy::uninit_assumed_init)]
|
||||||
|
|
||||||
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
||||||
unsafe { MaybeUninit::uninit().assume_init() };
|
unsafe { MaybeUninit::uninit().assume_init() };
|
||||||
|
|
||||||
|
@@ -85,7 +85,7 @@ impl Connector<()> {
|
|||||||
use bytes::{BufMut, BytesMut};
|
use bytes::{BufMut, BytesMut};
|
||||||
|
|
||||||
let mut alpn = BytesMut::with_capacity(20);
|
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_u8(proto.len() as u8);
|
||||||
alpn.put(proto.as_slice());
|
alpn.put(proto.as_slice());
|
||||||
}
|
}
|
||||||
@@ -290,8 +290,7 @@ where
|
|||||||
let h2 = sock
|
let h2 = sock
|
||||||
.ssl()
|
.ssl()
|
||||||
.selected_alpn_protocol()
|
.selected_alpn_protocol()
|
||||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
.map_or(false, |protos| protos.windows(2).any(|w| w == H2));
|
||||||
.unwrap_or(false);
|
|
||||||
if h2 {
|
if h2 {
|
||||||
(Box::new(sock), Protocol::Http2)
|
(Box::new(sock), Protocol::Http2)
|
||||||
} else {
|
} else {
|
||||||
@@ -325,8 +324,7 @@ where
|
|||||||
.get_ref()
|
.get_ref()
|
||||||
.1
|
.1
|
||||||
.get_alpn_protocol()
|
.get_alpn_protocol()
|
||||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
.map_or(false, |protos| protos.windows(2).any(|w| w == H2));
|
||||||
.unwrap_or(false);
|
|
||||||
if h2 {
|
if h2 {
|
||||||
(Box::new(sock), Protocol::Http2)
|
(Box::new(sock), Protocol::Http2)
|
||||||
} else {
|
} else {
|
||||||
|
@@ -168,14 +168,13 @@ where
|
|||||||
|
|
||||||
if let Err(e) = send.send_data(bytes, false) {
|
if let Err(e) = send.send_data(bytes, false) {
|
||||||
return Err(e.into());
|
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()),
|
Some(Err(e)) => return Err(e.into()),
|
||||||
}
|
}
|
||||||
|
@@ -152,8 +152,8 @@ impl ServiceConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Return keep-alive timer delay is configured.
|
/// Return keep-alive timer delay is configured.
|
||||||
|
#[inline]
|
||||||
pub fn keep_alive_timer(&self) -> Option<Sleep> {
|
pub fn keep_alive_timer(&self) -> Option<Sleep> {
|
||||||
self.keep_alive().map(|ka| sleep_until(self.now() + ka))
|
self.keep_alive().map(|ka| sleep_until(self.now() + ka))
|
||||||
}
|
}
|
||||||
@@ -365,11 +365,11 @@ mod tests {
|
|||||||
let clone3 = service.clone();
|
let clone3 = service.clone();
|
||||||
|
|
||||||
drop(clone1);
|
drop(clone1);
|
||||||
assert_eq!(false, notify_on_drop::is_dropped());
|
assert!(!notify_on_drop::is_dropped());
|
||||||
drop(clone2);
|
drop(clone2);
|
||||||
assert_eq!(false, notify_on_drop::is_dropped());
|
assert!(!notify_on_drop::is_dropped());
|
||||||
drop(clone3);
|
drop(clone3);
|
||||||
assert_eq!(false, notify_on_drop::is_dropped());
|
assert!(!notify_on_drop::is_dropped());
|
||||||
|
|
||||||
drop(service);
|
drop(service);
|
||||||
assert!(notify_on_drop::is_dropped());
|
assert!(notify_on_drop::is_dropped());
|
||||||
|
@@ -125,7 +125,7 @@ impl fmt::Display for Error {
|
|||||||
|
|
||||||
impl StdError for Error {
|
impl StdError for Error {
|
||||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
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
|
// transfer-encoding
|
||||||
header::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");
|
chunked = s.eq_ignore_ascii_case("chunked");
|
||||||
} else {
|
} else {
|
||||||
return Err(ParseError::Header);
|
return Err(ParseError::Header);
|
||||||
@@ -110,7 +110,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
}
|
}
|
||||||
// connection keep-alive state
|
// connection keep-alive state
|
||||||
header::CONNECTION => {
|
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") {
|
if conn.eq_ignore_ascii_case("keep-alive") {
|
||||||
Some(ConnectionType::KeepAlive)
|
Some(ConnectionType::KeepAlive)
|
||||||
} else if conn.eq_ignore_ascii_case("close") {
|
} else if conn.eq_ignore_ascii_case("close") {
|
||||||
@@ -125,7 +125,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
header::UPGRADE => {
|
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") {
|
if val.eq_ignore_ascii_case("websocket") {
|
||||||
has_upgrade_websocket = true;
|
has_upgrade_websocket = true;
|
||||||
}
|
}
|
||||||
|
@@ -515,14 +515,13 @@ where
|
|||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Result<(), DispatchError> {
|
) -> Result<(), DispatchError> {
|
||||||
// Handle `EXPECT: 100-Continue` header
|
// Handle `EXPECT: 100-Continue` header
|
||||||
|
let mut this = self.as_mut().project();
|
||||||
if req.head().expect() {
|
if req.head().expect() {
|
||||||
// set dispatcher state so the future is pinned.
|
// set dispatcher state so the future is pinned.
|
||||||
let mut this = self.as_mut().project();
|
|
||||||
let task = this.flow.expect.call(req);
|
let task = this.flow.expect.call(req);
|
||||||
this.state.set(State::ExpectCall(task));
|
this.state.set(State::ExpectCall(task));
|
||||||
} else {
|
} else {
|
||||||
// the same as above.
|
// the same as above.
|
||||||
let mut this = self.as_mut().project();
|
|
||||||
let task = this.flow.service.call(req);
|
let task = this.flow.service.call(req);
|
||||||
this.state.set(State::ServiceCall(task));
|
this.state.set(State::ServiceCall(task));
|
||||||
};
|
};
|
||||||
|
@@ -186,8 +186,7 @@ impl Inner {
|
|||||||
if self
|
if self
|
||||||
.task
|
.task
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|w| !cx.waker().will_wake(w))
|
.map_or(true, |w| !cx.waker().will_wake(w))
|
||||||
.unwrap_or(true)
|
|
||||||
{
|
{
|
||||||
self.task = Some(cx.waker().clone());
|
self.task = Some(cx.waker().clone());
|
||||||
}
|
}
|
||||||
@@ -199,8 +198,7 @@ impl Inner {
|
|||||||
if self
|
if self
|
||||||
.io_task
|
.io_task
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|w| !cx.waker().will_wake(w))
|
.map_or(true, |w| !cx.waker().will_wake(w))
|
||||||
.unwrap_or(true)
|
|
||||||
{
|
{
|
||||||
self.io_task = Some(cx.waker().clone());
|
self.io_task = Some(cx.waker().clone());
|
||||||
}
|
}
|
||||||
|
@@ -249,7 +249,7 @@ impl HeaderMap {
|
|||||||
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get(&self, key: impl AsHeaderName) -> Option<&HeaderValue> {
|
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.
|
/// 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> {
|
pub fn get_mut(&mut self, key: impl AsHeaderName) -> Option<&mut HeaderValue> {
|
||||||
match key.try_as_name(super::as_name::Seal).ok()? {
|
match key.try_as_name(super::as_name::Seal).ok()? {
|
||||||
Cow::Borrowed(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(|v| v.first_mut()),
|
Cow::Owned(name) => self.inner.get_mut(&name).map(Value::first_mut),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -27,9 +27,6 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod macros;
|
|
||||||
|
|
||||||
pub mod body;
|
pub mod body;
|
||||||
mod builder;
|
mod builder;
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
@@ -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
|
/// Connection upgrade status
|
||||||
pub fn upgrade(&self) -> bool {
|
pub fn upgrade(&self) -> bool {
|
||||||
if let Some(hdr) = self.headers().get(header::CONNECTION) {
|
self.headers()
|
||||||
if let Ok(s) = hdr.to_str() {
|
.get(header::CONNECTION)
|
||||||
s.to_ascii_lowercase().contains("upgrade")
|
.map(|hdr| {
|
||||||
} else {
|
if let Ok(s) = hdr.to_str() {
|
||||||
false
|
s.to_ascii_lowercase().contains("upgrade")
|
||||||
}
|
} else {
|
||||||
} else {
|
false
|
||||||
false
|
}
|
||||||
}
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -308,13 +309,11 @@ impl ResponseHead {
|
|||||||
/// Get custom reason for the response
|
/// Get custom reason for the response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn reason(&self) -> &str {
|
pub fn reason(&self) -> &str {
|
||||||
if let Some(reason) = self.reason {
|
self.reason.unwrap_or_else(|| {
|
||||||
reason
|
|
||||||
} else {
|
|
||||||
self.status
|
self.status
|
||||||
.canonical_reason()
|
.canonical_reason()
|
||||||
.unwrap_or("<unknown status code>")
|
.unwrap_or("<unknown status code>")
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -356,7 +355,7 @@ pub struct Message<T: Head> {
|
|||||||
impl<T: Head> Message<T> {
|
impl<T: Head> Message<T> {
|
||||||
/// Get new message from the pool of objects
|
/// Get new message from the pool of objects
|
||||||
pub fn new() -> Self {
|
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"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[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"
|
actix-utils = "3.0.0"
|
||||||
|
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
@@ -31,6 +31,6 @@ twoway = "0.2"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-http = "3.0.0-beta.7"
|
actix-http = "3.0.0-beta.8"
|
||||||
tokio = { version = "1", features = ["sync"] }
|
tokio = { version = "1", features = ["sync"] }
|
||||||
tokio-stream = "0.1"
|
tokio-stream = "0.1"
|
||||||
|
@@ -9,9 +9,9 @@
|
|||||||
<br />
|
<br />
|
||||||
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.5)
|
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.5)
|
||||||
[](https://crates.io/crates/actix-multipart)
|
[](https://crates.io/crates/actix-multipart)
|
||||||
|
[](https://discord.gg/NWpN5mmg3x)
|
||||||
|
|
||||||
## Documentation & Resources
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/actix-multipart)
|
- [API Documentation](https://docs.rs/actix-multipart)
|
||||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
|
||||||
- Minimum Supported Rust Version (MSRV): 1.46.0
|
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||||
|
@@ -20,13 +20,13 @@ openssl = ["tls-openssl", "actix-http/openssl"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0"
|
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-http-test = { version = "3.0.0-beta.4", features = [] }
|
||||||
actix-service = "2.0.0"
|
actix-service = "2.0.0"
|
||||||
actix-utils = "3.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"
|
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-core = { version = "0.3.7", default-features = false, features = ["std"] }
|
||||||
futures-util = { version = "0.3.7", default-features = false, features = [] }
|
futures-util = { version = "0.3.7", default-features = false, features = [] }
|
||||||
|
@@ -3,6 +3,12 @@
|
|||||||
## Unreleased - 2021-xx-xx
|
## 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
|
## 4.0.0-beta.5 - 2021-06-17
|
||||||
* No notable changes.
|
* No notable changes.
|
||||||
|
|
||||||
|
@@ -1,13 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web-actors"
|
name = "actix-web-actors"
|
||||||
version = "4.0.0-beta.5"
|
version = "4.0.0-beta.6"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix actors support for Actix Web"
|
description = "Actix actors support for Actix Web"
|
||||||
readme = "README.md"
|
|
||||||
keywords = ["actix", "http", "web", "framework", "async"]
|
keywords = ["actix", "http", "web", "framework", "async"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web"
|
||||||
documentation = "https://docs.rs/actix-web-actors/"
|
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
@@ -16,10 +14,10 @@ name = "actix_web_actors"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[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-codec = "0.4.0"
|
||||||
actix-http = "3.0.0-beta.7"
|
actix-http = "3.0.0-beta.8"
|
||||||
actix-web = { version = "4.0.0-beta.7", default-features = false }
|
actix-web = { version = "4.0.0-beta.8", default-features = false }
|
||||||
|
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
bytestring = "1"
|
bytestring = "1"
|
||||||
@@ -31,6 +29,6 @@ tokio = { version = "1", features = ["sync"] }
|
|||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-test = "0.1.0-beta.3"
|
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"
|
env_logger = "0.8"
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false }
|
||||||
|
@@ -3,16 +3,15 @@
|
|||||||
> Actix actors support for Actix Web.
|
> Actix actors support for Actix Web.
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-web-actors)
|
[](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)
|
[](https://blog.rust-lang.org/2020/03/12/Rust-1.46.html)
|
||||||

|

|
||||||
<br />
|
<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://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
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/actix-web-actors)
|
- [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
|
- Minimum supported Rust version: 1.46 or later
|
||||||
|
@@ -22,7 +22,7 @@ proc-macro2 = "1"
|
|||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-test = "0.1.0-beta.3"
|
actix-test = "0.1.0-beta.3"
|
||||||
actix-utils = "3.0.0"
|
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"] }
|
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
trybuild = "1"
|
trybuild = "1"
|
||||||
|
@@ -9,12 +9,11 @@
|
|||||||
<br />
|
<br />
|
||||||
[](https://deps.rs/crate/actix-web-codegen/0.5.0-beta.3)
|
[](https://deps.rs/crate/actix-web-codegen/0.5.0-beta.3)
|
||||||
[](https://crates.io/crates/actix-web-codegen)
|
[](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
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/actix-web-codegen)
|
- [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.
|
- Minimum supported Rust version: 1.46 or later.
|
||||||
|
|
||||||
## Compile Testing
|
## Compile Testing
|
||||||
|
@@ -6,7 +6,7 @@ use std::convert::TryFrom;
|
|||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||||
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
|
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 {
|
enum ResourceType {
|
||||||
Async,
|
Async,
|
||||||
@@ -227,8 +227,7 @@ impl Route {
|
|||||||
format!(
|
format!(
|
||||||
r#"invalid service definition, expected #[{}("<some path>")]"#,
|
r#"invalid service definition, expected #[{}("<some path>")]"#,
|
||||||
method
|
method
|
||||||
.map(|it| it.as_str())
|
.map_or("route", |it| it.as_str())
|
||||||
.unwrap_or("route")
|
|
||||||
.to_ascii_lowercase()
|
.to_ascii_lowercase()
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
@@ -298,7 +297,7 @@ impl ToTokens for Route {
|
|||||||
} = self;
|
} = self;
|
||||||
let resource_name = resource_name
|
let resource_name = resource_name
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(|| name.to_string(), |n| n.value());
|
.map_or_else(|| name.to_string(), LitStr::value);
|
||||||
let method_guards = {
|
let method_guards = {
|
||||||
let mut others = methods.iter();
|
let mut others = methods.iter();
|
||||||
// unwrapping since length is checked to be at least one
|
// unwrapping since length is checked to be at least one
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-beta.7 - 2021-06-26
|
||||||
### Changed
|
### Changed
|
||||||
* Change compression algorithm features flags. [#2250]
|
* Change compression algorithm features flags. [#2250]
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "awc"
|
name = "awc"
|
||||||
version = "3.0.0-beta.6"
|
version = "3.0.0-beta.7"
|
||||||
authors = [
|
authors = [
|
||||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||||
"fakeshadow <24548779@qq.com>",
|
"fakeshadow <24548779@qq.com>",
|
||||||
@@ -55,7 +55,7 @@ __compress = []
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0"
|
actix-codec = "0.4.0"
|
||||||
actix-service = "2.0.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 }
|
actix-rt = { version = "2.1", default-features = false }
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
@@ -77,8 +77,8 @@ tls-openssl = { version = "0.10.9", package = "openssl", optional = true }
|
|||||||
tls-rustls = { version = "0.19.0", package = "rustls", optional = true, features = ["dangerous_configuration"] }
|
tls-rustls = { version = "0.19.0", package = "rustls", optional = true, features = ["dangerous_configuration"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.7", features = ["openssl"] }
|
actix-web = { version = "4.0.0-beta.8", features = ["openssl"] }
|
||||||
actix-http = { version = "3.0.0-beta.7", features = ["openssl"] }
|
actix-http = { version = "3.0.0-beta.8", features = ["openssl"] }
|
||||||
actix-http-test = { version = "3.0.0-beta.4", features = ["openssl"] }
|
actix-http-test = { version = "3.0.0-beta.4", features = ["openssl"] }
|
||||||
actix-utils = "3.0.0"
|
actix-utils = "3.0.0"
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
|
@@ -3,16 +3,15 @@
|
|||||||
> Async HTTP and WebSocket client library.
|
> Async HTTP and WebSocket client library.
|
||||||
|
|
||||||
[](https://crates.io/crates/awc)
|
[](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://deps.rs/crate/awc/3.0.0-beta.7)
|
||||||
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://discord.gg/NWpN5mmg3x)
|
||||||
|
|
||||||
## Documentation & Resources
|
## Documentation & Resources
|
||||||
|
|
||||||
- [API Documentation](https://docs.rs/awc)
|
- [API Documentation](https://docs.rs/awc)
|
||||||
- [Example Project](https://github.com/actix/examples/tree/HEAD/security/awc_https)
|
- [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
|
- Minimum Supported Rust Version (MSRV): 1.46.0
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
@@ -480,12 +480,15 @@ impl ClientRequest {
|
|||||||
// supported, so we cannot guess Accept-Encoding HTTP header.
|
// supported, so we cannot guess Accept-Encoding HTTP header.
|
||||||
if slf.response_decompress {
|
if slf.response_decompress {
|
||||||
// Set Accept-Encoding with compression algorithm awc is built with.
|
// Set Accept-Encoding with compression algorithm awc is built with.
|
||||||
|
#[allow(clippy::vec_init_then_push)]
|
||||||
#[cfg(feature = "__compress")]
|
#[cfg(feature = "__compress")]
|
||||||
let accept_encoding = {
|
let accept_encoding = {
|
||||||
let mut encoding = vec![];
|
let mut encoding = vec![];
|
||||||
|
|
||||||
#[cfg(feature = "compress-brotli")]
|
#[cfg(feature = "compress-brotli")]
|
||||||
encoding.push("br");
|
{
|
||||||
|
encoding.push("br");
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "compress-gzip")]
|
#[cfg(feature = "compress-gzip")]
|
||||||
{
|
{
|
||||||
@@ -496,7 +499,11 @@ impl ClientRequest {
|
|||||||
#[cfg(feature = "compress-zstd")]
|
#[cfg(feature = "compress-zstd")]
|
||||||
encoding.push("zstd");
|
encoding.push("zstd");
|
||||||
|
|
||||||
assert!(!encoding.is_empty(), "encoding cannot be empty unless __compress feature has been explictily enabled.");
|
assert!(
|
||||||
|
!encoding.is_empty(),
|
||||||
|
"encoding can not be empty unless __compress feature has been explicitly enabled"
|
||||||
|
);
|
||||||
|
|
||||||
encoding.join(", ")
|
encoding.join(", ")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -517,7 +517,7 @@ mod tests {
|
|||||||
"test-origin"
|
"test-origin"
|
||||||
);
|
);
|
||||||
assert_eq!(req.max_size, 100);
|
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.protocols, Some("v1,v2".to_string()));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
req.head.headers.get(header::CONTENT_TYPE).unwrap(),
|
req.head.headers.get(header::CONTENT_TYPE).unwrap(),
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
use std::{future::Future, time::Instant};
|
use std::{future::Future, time::Instant};
|
||||||
|
|
||||||
use actix_http::Response;
|
|
||||||
use actix_utils::future::{ready, Ready};
|
use actix_utils::future::{ready, Ready};
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::test::TestRequest;
|
use actix_web::test::TestRequest;
|
||||||
@@ -24,11 +23,11 @@ struct StringResponder(String);
|
|||||||
|
|
||||||
impl FutureResponder for StringResponder {
|
impl FutureResponder for StringResponder {
|
||||||
type Error = Error;
|
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 {
|
fn future_respond_to(self, _: &HttpRequest) -> Self::Future {
|
||||||
// this is default builder for string response in both new and old responder trait.
|
// 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")
|
.content_type("text/plain; charset=utf-8")
|
||||||
.body(self.0)))
|
.body(self.0)))
|
||||||
}
|
}
|
||||||
@@ -37,7 +36,7 @@ impl FutureResponder for StringResponder {
|
|||||||
impl<T> FutureResponder for OptionResponder<T>
|
impl<T> FutureResponder for OptionResponder<T>
|
||||||
where
|
where
|
||||||
T: FutureResponder,
|
T: FutureResponder,
|
||||||
T::Future: Future<Output = Result<Response, Error>>,
|
T::Future: Future<Output = Result<HttpResponse, Error>>,
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Either<T::Future, Ready<Result<HttpResponse, Self::Error>>>;
|
type Future = Either<T::Future, Ready<Result<HttpResponse, Self::Error>>>;
|
||||||
@@ -52,7 +51,7 @@ where
|
|||||||
|
|
||||||
impl Responder for StringResponder {
|
impl Responder for StringResponder {
|
||||||
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
|
fn respond_to(self, _: &HttpRequest) -> HttpResponse {
|
||||||
Response::build(StatusCode::OK)
|
HttpResponse::build(StatusCode::OK)
|
||||||
.content_type("text/plain; charset=utf-8")
|
.content_type("text/plain; charset=utf-8")
|
||||||
.body(self.0)
|
.body(self.0)
|
||||||
}
|
}
|
||||||
@@ -62,7 +61,7 @@ impl<T: Responder> Responder for OptionResponder<T> {
|
|||||||
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
Some(t) => t.respond_to(req),
|
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();
|
fut.await.unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let elapsed = start.elapsed();
|
|
||||||
// check that at least first request succeeded
|
// check that at least first request succeeded
|
||||||
elapsed
|
start.elapsed()
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -93,9 +92,8 @@ fn async_web_service(c: &mut Criterion) {
|
|||||||
fut.await.unwrap();
|
fut.await.unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let elapsed = start.elapsed();
|
|
||||||
// check that at least first request succeeded
|
// 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.
|
/// Create application builder. Application can be configured with a builder-like pattern.
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let factory_ref = Rc::new(RefCell::new(None));
|
||||||
|
|
||||||
App {
|
App {
|
||||||
endpoint: AppEntry::new(fref.clone()),
|
endpoint: AppEntry::new(factory_ref.clone()),
|
||||||
data_factories: Vec::new(),
|
data_factories: Vec::new(),
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
default: None,
|
default: None,
|
||||||
factory_ref: fref,
|
factory_ref,
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
@@ -68,43 +69,83 @@ where
|
|||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
/// Set application data. Application data could be accessed
|
/// Set application (root level) data.
|
||||||
/// by using `Data<T>` extractor where `T` is data type.
|
|
||||||
///
|
///
|
||||||
/// **Note**: HTTP server accepts an application factory rather than
|
/// Application data stored with `App::app_data()` method is available through the
|
||||||
/// an application instance. Http server constructs an application
|
/// [`HttpRequest::app_data`](crate::HttpRequest::app_data) method at runtime.
|
||||||
/// instance for each thread, thus application data must be constructed
|
///
|
||||||
/// multiple times. If you want to share data between different
|
/// # [`Data<T>`]
|
||||||
/// threads, a shared object should be used, e.g. `Arc`. Internally `Data` type
|
/// Any [`Data<T>`] type added here can utilize it's extractor implementation in handlers.
|
||||||
/// uses `Arc` so data could be created outside of app factory and clones could
|
/// Types not wrapped in `Data<T>` cannot use this extractor. See [its docs](Data<T>) for more
|
||||||
/// be stored via `App::app_data()` method.
|
/// about its usage and patterns.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::cell::Cell;
|
/// use std::cell::Cell;
|
||||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
/// use actix_web::{web, App, HttpRequest, HttpResponse, Responder};
|
||||||
///
|
///
|
||||||
/// struct MyData {
|
/// struct MyData {
|
||||||
/// counter: Cell<usize>,
|
/// count: std::cell::Cell<usize>,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// async fn index(data: web::Data<MyData>) -> impl Responder {
|
/// async fn handler(req: HttpRequest, counter: web::Data<MyData>) -> impl Responder {
|
||||||
/// data.counter.set(data.counter.get() + 1);
|
/// // note this cannot use the Data<T> extractor because it was not added with it
|
||||||
/// HttpResponse::Ok()
|
/// 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()
|
/// let app = App::new().service(
|
||||||
/// .data(MyData{ counter: Cell::new(0) })
|
/// web::resource("/")
|
||||||
/// .service(
|
/// .app_data(3usize)
|
||||||
/// web::resource("/index.html").route(
|
/// .app_data(web::Data::new(MyData { count: Default::default() }))
|
||||||
/// web::get().to(index)));
|
/// .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 {
|
pub fn data<U: 'static>(self, data: U) -> Self {
|
||||||
self.app_data(Data::new(data))
|
self.app_data(Data::new(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set application data factory. This function is
|
/// Add application data factory. This function is similar to `.data()` but it accepts a
|
||||||
/// similar to `.data()` but it accepts data factory. Data object get
|
/// "data factory". Data values are constructed asynchronously during application
|
||||||
/// constructed asynchronously during application initialization.
|
/// 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
|
pub fn data_factory<F, Out, D, E>(mut self, data: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn() -> Out + 'static,
|
F: Fn() -> Out + 'static,
|
||||||
@@ -133,18 +174,6 @@ where
|
|||||||
self
|
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
|
/// Run external configuration as part of the application building
|
||||||
/// process
|
/// process
|
||||||
///
|
///
|
||||||
@@ -518,6 +547,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_factory() {
|
async fn test_data_factory() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
@@ -541,6 +572,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_factory_errors() {
|
async fn test_data_factory_errors() {
|
||||||
let srv = try_init_service(
|
let srv = try_init_service(
|
||||||
|
@@ -1,22 +1,22 @@
|
|||||||
use std::cell::RefCell;
|
use std::{cell::RefCell, mem, rc::Rc};
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use actix_http::{Extensions, Request};
|
use actix_http::{Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Router, Url};
|
use actix_router::{Path, ResourceDef, Router, Url};
|
||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
use actix_service::{
|
||||||
use actix_service::{fn_service, Service, ServiceFactory};
|
boxed::{self, BoxService, BoxServiceFactory},
|
||||||
|
fn_service, Service, ServiceFactory,
|
||||||
|
};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
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::{
|
use crate::{
|
||||||
config::{AppConfig, AppService},
|
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>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
@@ -75,7 +75,7 @@ where
|
|||||||
let mut config = AppService::new(config, default.clone());
|
let mut config = AppService::new(config, default.clone());
|
||||||
|
|
||||||
// register services
|
// register services
|
||||||
std::mem::take(&mut *self.services.borrow_mut())
|
mem::take(&mut *self.services.borrow_mut())
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|mut srv| srv.register(&mut config));
|
.for_each(|mut srv| srv.register(&mut config));
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ where
|
|||||||
});
|
});
|
||||||
|
|
||||||
// external resources
|
// 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);
|
rmap.add(&mut rdef, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,9 +131,9 @@ where
|
|||||||
let service = endpoint_fut.await?;
|
let service = endpoint_fut.await?;
|
||||||
|
|
||||||
// populate app data container from (async) data factories.
|
// 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);
|
factory.create(&mut app_data);
|
||||||
});
|
}
|
||||||
|
|
||||||
Ok(AppInitService {
|
Ok(AppInitService {
|
||||||
service,
|
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>
|
pub struct AppInitService<T, B>
|
||||||
where
|
where
|
||||||
T: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
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 {
|
pub struct AppRouting {
|
||||||
router: Router<HttpService, Guards>,
|
router: Router<HttpService, Guards>,
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
@@ -349,6 +352,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_drop_data() {
|
async fn test_drop_data() {
|
||||||
let data = Arc::new(AtomicBool::new(false));
|
let data = Arc::new(AtomicBool::new(false));
|
||||||
|
@@ -62,6 +62,8 @@ impl AppService {
|
|||||||
(self.config, self.services)
|
(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 {
|
pub(crate) fn clone_config(&self) -> Self {
|
||||||
AppService {
|
AppService {
|
||||||
config: self.config.clone(),
|
config: self.config.clone(),
|
||||||
@@ -71,12 +73,12 @@ impl AppService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Service configuration
|
/// Returns reference to configuration.
|
||||||
pub fn config(&self) -> &AppConfig {
|
pub fn config(&self) -> &AppConfig {
|
||||||
&self.config
|
&self.config
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default resource
|
/// Returns default handler factory.
|
||||||
pub fn default_service(&self) -> Rc<HttpNewService> {
|
pub fn default_service(&self) -> Rc<HttpNewService> {
|
||||||
self.default.clone()
|
self.default.clone()
|
||||||
}
|
}
|
||||||
@@ -92,9 +94,9 @@ impl AppService {
|
|||||||
F: IntoServiceFactory<S, ServiceRequest>,
|
F: IntoServiceFactory<S, ServiceRequest>,
|
||||||
S: ServiceFactory<
|
S: ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Config = (),
|
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
|
Config = (),
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> + 'static,
|
> + 'static,
|
||||||
{
|
{
|
||||||
@@ -116,7 +118,7 @@ impl AppConfig {
|
|||||||
AppConfig { secure, host, addr }
|
AppConfig { secure, host, addr }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Needed in actix-test crate.
|
/// Needed in actix-test crate. Semver exempt.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn __priv_test_new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
pub fn __priv_test_new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
||||||
AppConfig::new(secure, host, addr)
|
AppConfig::new(secure, host, addr)
|
||||||
@@ -142,6 +144,11 @@ impl AppConfig {
|
|||||||
pub fn local_addr(&self) -> SocketAddr {
|
pub fn local_addr(&self) -> SocketAddr {
|
||||||
self.addr
|
self.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) fn set_host(&mut self, host: &str) {
|
||||||
|
self.host = host.to_owned();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AppConfig {
|
impl Default for AppConfig {
|
||||||
@@ -192,6 +199,7 @@ impl ServiceConfig {
|
|||||||
/// Add shared app data item.
|
/// Add shared app data item.
|
||||||
///
|
///
|
||||||
/// Counterpart to [`App::data()`](crate::App::data).
|
/// 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 {
|
pub fn data<U: 'static>(&mut self, data: U) -> &mut Self {
|
||||||
self.app_data(Data::new(data));
|
self.app_data(Data::new(data));
|
||||||
self
|
self
|
||||||
@@ -257,6 +265,8 @@ mod tests {
|
|||||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
use crate::test::{call_service, init_service, read_body, TestRequest};
|
||||||
use crate::{web, App, HttpRequest, HttpResponse};
|
use crate::{web, App, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
|
// allow deprecated `ServiceConfig::data`
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let cfg = |cfg: &mut ServiceConfig| {
|
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
|
/// If route data is not set for a handler, using `Data<T>` extractor would cause *Internal
|
||||||
/// Server Error* response.
|
/// 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 std::sync::Mutex;
|
||||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
/// use actix_web::{web, App, HttpResponse, Responder};
|
||||||
@@ -154,6 +159,8 @@ mod tests {
|
|||||||
web, App, HttpResponse,
|
web, App, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_extractor() {
|
async fn test_data_extractor() {
|
||||||
let srv = init_service(App::new().data("TEST".to_string()).service(
|
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);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_route_data_extractor() {
|
async fn test_route_data_extractor() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
@@ -250,6 +259,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data() {
|
async fn test_override_data() {
|
||||||
let srv =
|
let srv =
|
||||||
|
@@ -1,6 +1,4 @@
|
|||||||
#[macro_export]
|
macro_rules! downcast_get_type_id {
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! __downcast_get_type_id {
|
|
||||||
() => {
|
() => {
|
||||||
/// A helper method to get the type ID of the type
|
/// A helper method to get the type ID of the type
|
||||||
/// this trait is implemented on.
|
/// this trait is implemented on.
|
||||||
@@ -30,10 +28,8 @@ macro_rules! __downcast_get_type_id {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//Generate implementation for dyn $name
|
// Generate implementation for dyn $name
|
||||||
#[doc(hidden)]
|
macro_rules! downcast_dyn {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __downcast_dyn {
|
|
||||||
($name:ident) => {
|
($name:ident) => {
|
||||||
/// A struct with a private constructor, for use with
|
/// A struct with a private constructor, for use with
|
||||||
/// `__private_get_type_id__`. Its single field is private,
|
/// `__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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#![allow(clippy::upper_case_acronyms)]
|
#![allow(clippy::upper_case_acronyms)]
|
||||||
|
|
||||||
trait MB {
|
trait MB {
|
||||||
__downcast_get_type_id!();
|
downcast_get_type_id!();
|
||||||
}
|
}
|
||||||
|
|
||||||
__downcast_dyn!(MB);
|
downcast_dyn!(MB);
|
||||||
|
|
||||||
impl MB for String {}
|
impl MB for String {}
|
||||||
impl MB for () {}
|
impl MB for () {}
|
||||||
|
@@ -18,6 +18,7 @@ mod response_error;
|
|||||||
pub use self::error::Error;
|
pub use self::error::Error;
|
||||||
pub use self::internal::*;
|
pub use self::internal::*;
|
||||||
pub use self::response_error::ResponseError;
|
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.
|
/// 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 actix_http::{body::AnyBody, header, Response, StatusCode};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
|
|
||||||
use crate::{__downcast_dyn, __downcast_get_type_id};
|
use crate::error::{downcast_dyn, downcast_get_type_id};
|
||||||
use crate::{helpers, HttpResponse};
|
use crate::{helpers, HttpResponse};
|
||||||
|
|
||||||
/// Errors that can generate responses.
|
/// Errors that can generate responses.
|
||||||
@@ -41,10 +41,10 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
|
|||||||
res.set_body(AnyBody::from(buf))
|
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> {}
|
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 {
|
impl ResponseError for std::str::Utf8Error {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
StatusCode::BAD_REQUEST
|
StatusCode::BAD_REQUEST
|
||||||
|
@@ -1,12 +1,14 @@
|
|||||||
//! Request extractors
|
//! Request extractors
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
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 futures_core::ready;
|
||||||
|
|
||||||
use crate::{dev::Payload, Error, HttpRequest};
|
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::{web, dev, App, Error, HttpRequest, FromRequest};
|
||||||
/// use actix_web::error::ErrorBadRequest;
|
/// use actix_web::error::ErrorBadRequest;
|
||||||
/// use futures_util::future::{ok, err, Ready};
|
/// use futures_util::future::{ok, err, Ready};
|
||||||
/// use serde_derive::Deserialize;
|
/// use serde::Deserialize;
|
||||||
/// use rand;
|
/// use rand;
|
||||||
///
|
///
|
||||||
/// #[derive(Debug, Deserialize)]
|
/// #[derive(Debug, Deserialize)]
|
||||||
@@ -143,7 +145,7 @@ where
|
|||||||
/// use actix_web::{web, dev, App, Result, Error, HttpRequest, FromRequest};
|
/// use actix_web::{web, dev, App, Result, Error, HttpRequest, FromRequest};
|
||||||
/// use actix_web::error::ErrorBadRequest;
|
/// use actix_web::error::ErrorBadRequest;
|
||||||
/// use futures_util::future::{ok, err, Ready};
|
/// use futures_util::future::{ok, err, Ready};
|
||||||
/// use serde_derive::Deserialize;
|
/// use serde::Deserialize;
|
||||||
/// use rand;
|
/// use rand;
|
||||||
///
|
///
|
||||||
/// #[derive(Debug, Deserialize)]
|
/// #[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)]
|
#[doc(hidden)]
|
||||||
impl FromRequest for () {
|
impl FromRequest for () {
|
||||||
type Error = Error;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
|
|
||||||
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
ready(Ok(()))
|
ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,7 +376,7 @@ mod m {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use actix_http::http::header;
|
use actix_http::http::header;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use serde_derive::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::TestRequest;
|
use crate::test::TestRequest;
|
||||||
@@ -411,4 +457,18 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(r.is_err());
|
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 super::{qitem, QualityItem};
|
||||||
use crate::http::header;
|
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)
|
/// `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
|
/// The `Accept` header field can be used by user agents to specify
|
||||||
@@ -81,14 +81,14 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_accept {
|
test_accept {
|
||||||
// Tests from the RFC
|
// Tests from the RFC
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"audio/*; q=0.2, audio/basic"],
|
vec![b"audio/*; q=0.2, audio/basic"],
|
||||||
Some(Accept(vec![
|
Some(Accept(vec![
|
||||||
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
||||||
qitem("audio/basic".parse().unwrap()),
|
qitem("audio/basic".parse().unwrap()),
|
||||||
])));
|
])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
||||||
Some(Accept(vec![
|
Some(Accept(vec![
|
||||||
@@ -100,13 +100,13 @@ crate::__define_common_header! {
|
|||||||
qitem("text/x-c".parse().unwrap()),
|
qitem("text/x-c".parse().unwrap()),
|
||||||
])));
|
])));
|
||||||
// Custom tests
|
// Custom tests
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test3,
|
test3,
|
||||||
vec![b"text/plain; charset=utf-8"],
|
vec![b"text/plain; charset=utf-8"],
|
||||||
Some(Accept(vec![
|
Some(Accept(vec![
|
||||||
qitem(mime::TEXT_PLAIN_UTF_8),
|
qitem(mime::TEXT_PLAIN_UTF_8),
|
||||||
])));
|
])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test4,
|
test4,
|
||||||
vec![b"text/plain; charset=utf-8; q=0.5"],
|
vec![b"text/plain; charset=utf-8; q=0.5"],
|
||||||
Some(Accept(vec
|
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.3)
|
||||||
///
|
///
|
||||||
@@ -57,6 +57,6 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_accept_charset {
|
test_accept_charset {
|
||||||
// Test case from RFC
|
// 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 {
|
test_accept_encoding {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(test1, vec![b"compress, gzip"]);
|
crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]);
|
||||||
crate::__common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
||||||
crate::__common_header_test!(test3, vec![b"*"]);
|
crate::http::header::common_header_test!(test3, vec![b"*"]);
|
||||||
// Note: Removed quality 1 from gzip
|
// 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
|
// 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};
|
use super::{QualityItem, ACCEPT_LANGUAGE};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Accept-Language` header, defined in
|
/// `Accept-Language` header, defined in
|
||||||
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
|
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
|
||||||
///
|
///
|
||||||
@@ -53,9 +53,9 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_accept_language {
|
test_accept_language {
|
||||||
// From the RFC
|
// 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
|
// Own test
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2, vec![b"en-US, en; q=0.5, fr"],
|
test2, vec![b"en-US, en; q=0.5, fr"],
|
||||||
Some(AcceptLanguage(vec
|
/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
|
||||||
///
|
///
|
||||||
/// The `Allow` header field lists the set of methods advertised as
|
/// The `Allow` header field lists the set of methods advertised as
|
||||||
@@ -49,12 +49,12 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_allow {
|
test_allow {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"GET, HEAD, PUT"],
|
vec![b"GET, HEAD, PUT"],
|
||||||
Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT])));
|
Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT])));
|
||||||
// Own tests
|
// Own tests
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"],
|
vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"],
|
||||||
Some(HeaderField(vec![
|
Some(HeaderField(vec![
|
||||||
@@ -67,7 +67,7 @@ crate::__define_common_header! {
|
|||||||
Method::TRACE,
|
Method::TRACE,
|
||||||
Method::CONNECT,
|
Method::CONNECT,
|
||||||
Method::PATCH])));
|
Method::PATCH])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test3,
|
test3,
|
||||||
vec![b""],
|
vec![b""],
|
||||||
Some(HeaderField(Vec::<Method>::new())));
|
Some(HeaderField(Vec::<Method>::new())));
|
||||||
|
@@ -49,9 +49,9 @@ use crate::http::header;
|
|||||||
#[derive(PartialEq, Clone, Debug)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
pub struct CacheControl(pub Vec<CacheDirective>);
|
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 {
|
impl Header for CacheControl {
|
||||||
fn name() -> header::HeaderName {
|
fn name() -> header::HeaderName {
|
||||||
header::CACHE_CONTROL
|
header::CACHE_CONTROL
|
||||||
|
@@ -410,41 +410,33 @@ impl ContentDisposition {
|
|||||||
|
|
||||||
/// Return the value of *name* if exists.
|
/// Return the value of *name* if exists.
|
||||||
pub fn get_name(&self) -> Option<&str> {
|
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.
|
/// Return the value of *filename* if exists.
|
||||||
pub fn get_filename(&self) -> Option<&str> {
|
pub fn get_filename(&self) -> Option<&str> {
|
||||||
self.parameters
|
self.parameters
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|p| p.as_filename())
|
.find_map(DispositionParam::as_filename)
|
||||||
.next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the value of *filename\** if exists.
|
/// Return the value of *filename\** if exists.
|
||||||
pub fn get_filename_ext(&self) -> Option<&ExtendedValue> {
|
pub fn get_filename_ext(&self) -> Option<&ExtendedValue> {
|
||||||
self.parameters
|
self.parameters
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|p| p.as_filename_ext())
|
.find_map(DispositionParam::as_filename_ext)
|
||||||
.next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the value of the parameter which the `name` matches.
|
/// Return the value of the parameter which the `name` matches.
|
||||||
pub fn get_unknown(&self, name: impl AsRef<str>) -> Option<&str> {
|
pub fn get_unknown(&self, name: impl AsRef<str>) -> Option<&str> {
|
||||||
let name = name.as_ref();
|
let name = name.as_ref();
|
||||||
self.parameters
|
self.parameters.iter().find_map(|p| p.as_unknown(name))
|
||||||
.iter()
|
|
||||||
.filter_map(|p| p.as_unknown(name))
|
|
||||||
.next()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the value of the extended parameter which the `name` matches.
|
/// Return the value of the extended parameter which the `name` matches.
|
||||||
pub fn get_unknown_ext(&self, name: impl AsRef<str>) -> Option<&ExtendedValue> {
|
pub fn get_unknown_ext(&self, name: impl AsRef<str>) -> Option<&ExtendedValue> {
|
||||||
let name = name.as_ref();
|
let name = name.as_ref();
|
||||||
self.parameters
|
self.parameters.iter().find_map(|p| p.as_unknown_ext(name))
|
||||||
.iter()
|
|
||||||
.filter_map(|p| p.as_unknown_ext(name))
|
|
||||||
.next()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
use super::{QualityItem, CONTENT_LANGUAGE};
|
use super::{QualityItem, CONTENT_LANGUAGE};
|
||||||
use language_tags::LanguageTag;
|
use language_tags::LanguageTag;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Content-Language` header, defined in
|
/// `Content-Language` header, defined in
|
||||||
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
|
/// [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>)+
|
(ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+
|
||||||
|
|
||||||
test_content_language {
|
test_content_language {
|
||||||
crate::__common_header_test!(test1, vec![b"da"]);
|
crate::http::header::common_header_test!(test1, vec![b"da"]);
|
||||||
crate::__common_header_test!(test2, vec![b"mi, en"]);
|
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 super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE};
|
||||||
use crate::error::ParseError;
|
use crate::error::ParseError;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Content-Range` header, defined in
|
/// `Content-Range` header, defined in
|
||||||
/// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2)
|
/// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2)
|
||||||
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
|
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
|
||||||
|
|
||||||
test_content_range {
|
test_content_range {
|
||||||
crate::__common_header_test!(test_bytes,
|
crate::http::header::common_header_test!(test_bytes,
|
||||||
vec![b"bytes 0-499/500"],
|
vec![b"bytes 0-499/500"],
|
||||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||||
range: Some((0, 499)),
|
range: Some((0, 499)),
|
||||||
instance_length: Some(500)
|
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/*"],
|
vec![b"bytes 0-499/*"],
|
||||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||||
range: Some((0, 499)),
|
range: Some((0, 499)),
|
||||||
instance_length: None
|
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"],
|
vec![b"bytes */500"],
|
||||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||||
range: None,
|
range: None,
|
||||||
instance_length: Some(500)
|
instance_length: Some(500)
|
||||||
})));
|
})));
|
||||||
|
|
||||||
crate::__common_header_test!(test_unregistered,
|
crate::http::header::common_header_test!(test_unregistered,
|
||||||
vec![b"seconds 1-2"],
|
vec![b"seconds 1-2"],
|
||||||
Some(ContentRange(ContentRangeSpec::Unregistered {
|
Some(ContentRange(ContentRangeSpec::Unregistered {
|
||||||
unit: "seconds".to_owned(),
|
unit: "seconds".to_owned(),
|
||||||
resp: "1-2".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"],
|
vec![b"bytes 0-499"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_only_unit,
|
crate::http::header::common_header_test!(test_only_unit,
|
||||||
vec![b"bytes"],
|
vec![b"bytes"],
|
||||||
None::<ContentRange>);
|
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"],
|
vec![b"bytes 499-0/500"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_blank,
|
crate::http::header::common_header_test!(test_blank,
|
||||||
vec![b""],
|
vec![b""],
|
||||||
None::<ContentRange>);
|
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"],
|
vec![b"bytes 1-2/500 3"],
|
||||||
None::<ContentRange>);
|
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"],
|
vec![b"bytes 1-2/500/600"],
|
||||||
None::<ContentRange>);
|
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"],
|
vec![b"bytes 1-2-3/500"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
use super::CONTENT_TYPE;
|
use super::CONTENT_TYPE;
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Content-Type` header, defined in
|
/// `Content-Type` header, defined in
|
||||||
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
|
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
|
||||||
///
|
///
|
||||||
@@ -52,7 +52,7 @@ crate::__define_common_header! {
|
|||||||
(ContentType, CONTENT_TYPE) => [Mime]
|
(ContentType, CONTENT_TYPE) => [Mime]
|
||||||
|
|
||||||
test_content_type {
|
test_content_type {
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"text/html"],
|
vec![b"text/html"],
|
||||||
Some(HeaderField(mime::TEXT_HTML)));
|
Some(HeaderField(mime::TEXT_HTML)));
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
use super::{HttpDate, DATE};
|
use super::{HttpDate, DATE};
|
||||||
use std::time::SystemTime;
|
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)
|
/// `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
|
/// The `Date` header field represents the date and time at which the
|
||||||
@@ -32,7 +32,7 @@ crate::__define_common_header! {
|
|||||||
(Date, DATE) => [HttpDate]
|
(Date, DATE) => [HttpDate]
|
||||||
|
|
||||||
test_date {
|
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,6 +1,6 @@
|
|||||||
use super::{EntityTag, ETAG};
|
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)
|
/// `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
|
/// The `ETag` header field in a response provides the current entity-tag
|
||||||
@@ -50,50 +50,50 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_etag {
|
test_etag {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(test1,
|
crate::http::header::common_header_test!(test1,
|
||||||
vec![b"\"xyzzy\""],
|
vec![b"\"xyzzy\""],
|
||||||
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
|
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
|
||||||
crate::__common_header_test!(test2,
|
crate::http::header::common_header_test!(test2,
|
||||||
vec![b"W/\"xyzzy\""],
|
vec![b"W/\"xyzzy\""],
|
||||||
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
|
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
|
||||||
crate::__common_header_test!(test3,
|
crate::http::header::common_header_test!(test3,
|
||||||
vec![b"\"\""],
|
vec![b"\"\""],
|
||||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||||
// Own tests
|
// Own tests
|
||||||
crate::__common_header_test!(test4,
|
crate::http::header::common_header_test!(test4,
|
||||||
vec![b"\"foobar\""],
|
vec![b"\"foobar\""],
|
||||||
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
|
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
|
||||||
crate::__common_header_test!(test5,
|
crate::http::header::common_header_test!(test5,
|
||||||
vec![b"\"\""],
|
vec![b"\"\""],
|
||||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||||
crate::__common_header_test!(test6,
|
crate::http::header::common_header_test!(test6,
|
||||||
vec![b"W/\"weak-etag\""],
|
vec![b"W/\"weak-etag\""],
|
||||||
Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
|
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\""],
|
vec![b"W/\"\x65\x62\""],
|
||||||
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
|
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/\"\""],
|
vec![b"W/\"\""],
|
||||||
Some(ETag(EntityTag::new(true, "".to_owned()))));
|
Some(ETag(EntityTag::new(true, "".to_owned()))));
|
||||||
crate::__common_header_test!(test9,
|
crate::http::header::common_header_test!(test9,
|
||||||
vec![b"no-dquotes"],
|
vec![b"no-dquotes"],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test10,
|
crate::http::header::common_header_test!(test10,
|
||||||
vec![b"w/\"the-first-w-is-case-sensitive\""],
|
vec![b"w/\"the-first-w-is-case-sensitive\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test11,
|
crate::http::header::common_header_test!(test11,
|
||||||
vec![b""],
|
vec![b""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test12,
|
crate::http::header::common_header_test!(test12,
|
||||||
vec![b"\"unmatched-dquotes1"],
|
vec![b"\"unmatched-dquotes1"],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test13,
|
crate::http::header::common_header_test!(test13,
|
||||||
vec![b"unmatched-dquotes2\""],
|
vec![b"unmatched-dquotes2\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test14,
|
crate::http::header::common_header_test!(test14,
|
||||||
vec![b"matched-\"dquotes\""],
|
vec![b"matched-\"dquotes\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test15,
|
crate::http::header::common_header_test!(test15,
|
||||||
vec![b"\""],
|
vec![b"\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use super::{HttpDate, EXPIRES};
|
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)
|
/// `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
|
/// The `Expires` header field gives the date/time after which the
|
||||||
@@ -36,6 +36,6 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_expires {
|
test_expires {
|
||||||
// Test case from RFC
|
// 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};
|
use super::{EntityTag, IF_MATCH};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-Match` header, defined in
|
/// `If-Match` header, defined in
|
||||||
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
|
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
|
||||||
///
|
///
|
||||||
@@ -53,18 +53,18 @@ crate::__define_common_header! {
|
|||||||
(IfMatch, IF_MATCH) => {Any / (EntityTag)+}
|
(IfMatch, IF_MATCH) => {Any / (EntityTag)+}
|
||||||
|
|
||||||
test_if_match {
|
test_if_match {
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"\"xyzzy\""],
|
vec![b"\"xyzzy\""],
|
||||||
Some(HeaderField::Items(
|
Some(HeaderField::Items(
|
||||||
vec![EntityTag::new(false, "xyzzy".to_owned())])));
|
vec![EntityTag::new(false, "xyzzy".to_owned())])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
|
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
|
||||||
Some(HeaderField::Items(
|
Some(HeaderField::Items(
|
||||||
vec![EntityTag::new(false, "xyzzy".to_owned()),
|
vec![EntityTag::new(false, "xyzzy".to_owned()),
|
||||||
EntityTag::new(false, "r2d2xxxx".to_owned()),
|
EntityTag::new(false, "r2d2xxxx".to_owned()),
|
||||||
EntityTag::new(false, "c3piozzzz".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};
|
use super::{HttpDate, IF_MODIFIED_SINCE};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-Modified-Since` header, defined in
|
/// `If-Modified-Since` header, defined in
|
||||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
|
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
|
||||||
///
|
///
|
||||||
@@ -36,6 +36,6 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_if_modified_since {
|
test_if_modified_since {
|
||||||
// Test case from RFC
|
// 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};
|
use super::{EntityTag, IF_NONE_MATCH};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-None-Match` header, defined in
|
/// `If-None-Match` header, defined in
|
||||||
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
|
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
|
||||||
///
|
///
|
||||||
@@ -55,11 +55,11 @@ crate::__define_common_header! {
|
|||||||
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
|
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
|
||||||
|
|
||||||
test_if_none_match {
|
test_if_none_match {
|
||||||
crate::__common_header_test!(test1, vec![b"\"xyzzy\""]);
|
crate::http::header::common_header_test!(test1, vec![b"\"xyzzy\""]);
|
||||||
crate::__common_header_test!(test2, vec![b"W/\"xyzzy\""]);
|
crate::http::header::common_header_test!(test2, vec![b"W/\"xyzzy\""]);
|
||||||
crate::__common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
|
crate::http::header::common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
|
||||||
crate::__common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
|
crate::http::header::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!(test5, vec![b"*"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -113,7 +113,7 @@ mod test_if_range {
|
|||||||
use crate::http::header::*;
|
use crate::http::header::*;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
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"]);
|
||||||
crate::__common_header_test!(test2, vec![b"\"abc\""]);
|
crate::http::header::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!(test3, vec![b"this-is-invalid"], None::<IfRange>);
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use super::{HttpDate, IF_UNMODIFIED_SINCE};
|
use super::{HttpDate, IF_UNMODIFIED_SINCE};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-Unmodified-Since` header, defined in
|
/// `If-Unmodified-Since` header, defined in
|
||||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
|
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
|
||||||
///
|
///
|
||||||
@@ -37,6 +37,6 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_if_unmodified_since {
|
test_if_unmodified_since {
|
||||||
// Test case from RFC
|
// 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};
|
use super::{HttpDate, LAST_MODIFIED};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Last-Modified` header, defined in
|
/// `Last-Modified` header, defined in
|
||||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
|
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
|
||||||
///
|
///
|
||||||
@@ -36,6 +36,6 @@ crate::__define_common_header! {
|
|||||||
|
|
||||||
test_last_modified {
|
test_last_modified {
|
||||||
// Test case from RFC
|
// 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_rules! common_header_deref {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __common_header_deref {
|
|
||||||
($from:ty => $to:ty) => {
|
($from:ty => $to:ty) => {
|
||||||
impl ::std::ops::Deref for $from {
|
impl ::std::ops::Deref for $from {
|
||||||
type Target = $to;
|
type Target = $to;
|
||||||
@@ -20,9 +18,7 @@ macro_rules! __common_header_deref {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
macro_rules! common_header_test_module {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __common_header_test_module {
|
|
||||||
($id:ident, $tm:ident{$($tf:item)*}) => {
|
($id:ident, $tm:ident{$($tf:item)*}) => {
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -37,9 +33,8 @@ macro_rules! __common_header_test_module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[cfg(test)]
|
||||||
#[macro_export]
|
macro_rules! common_header_test {
|
||||||
macro_rules! __common_header_test {
|
|
||||||
($id:ident, $raw:expr) => {
|
($id:ident, $raw:expr) => {
|
||||||
#[test]
|
#[test]
|
||||||
fn $id() {
|
fn $id() {
|
||||||
@@ -99,9 +94,7 @@ macro_rules! __common_header_test {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
macro_rules! common_header {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __define_common_header {
|
|
||||||
// $a:meta: Attributes associated with the header item (usually docs)
|
// $a:meta: Attributes associated with the header item (usually docs)
|
||||||
// $id:ident: Identifier of the header
|
// $id:ident: Identifier of the header
|
||||||
// $n:expr: Lowercase name of the header
|
// $n:expr: Lowercase name of the header
|
||||||
@@ -112,7 +105,7 @@ macro_rules! __define_common_header {
|
|||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct $id(pub Vec<$item>);
|
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 {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
@@ -148,7 +141,7 @@ macro_rules! __define_common_header {
|
|||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct $id(pub Vec<$item>);
|
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 {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
@@ -184,7 +177,7 @@ macro_rules! __define_common_header {
|
|||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct $id(pub $value);
|
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 {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
@@ -267,34 +260,39 @@ macro_rules! __define_common_header {
|
|||||||
|
|
||||||
// optional test module
|
// optional test module
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
($id, $name) => ($item)*
|
($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)*}) => {
|
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
($id, $n) => ($item)+
|
($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)*}) => {
|
($(#[$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]
|
$(#[$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)*}) => {
|
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
($id, $name) => {Any / ($item)+}
|
($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_range;
|
||||||
mod if_unmodified_since;
|
mod if_unmodified_since;
|
||||||
mod last_modified;
|
mod last_modified;
|
||||||
|
|
||||||
mod macros;
|
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 actix_utils::future::{err, ok, Ready};
|
||||||
use crate::http::header::{self, HeaderName};
|
use derive_more::{Display, Error};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
use crate::{
|
||||||
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
dev::{AppConfig, Payload, RequestHead},
|
||||||
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
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)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct ConnectionInfo {
|
pub struct ConnectionInfo {
|
||||||
scheme: String,
|
scheme: String,
|
||||||
@@ -25,105 +80,75 @@ impl ConnectionInfo {
|
|||||||
Ref::map(req.extensions(), |e| e.get().unwrap())
|
Ref::map(req.extensions(), |e| e.get().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity, clippy::borrow_interior_mutable_const)]
|
|
||||||
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
||||||
let mut host = None;
|
let mut host = None;
|
||||||
let mut scheme = None;
|
let mut scheme = None;
|
||||||
let mut realip_remote_addr = None;
|
let mut realip_remote_addr = None;
|
||||||
|
|
||||||
// load forwarded header
|
for (name, val) in req
|
||||||
for hdr in req.headers.get_all(&header::FORWARDED) {
|
.headers
|
||||||
if let Ok(val) = hdr.to_str() {
|
.get_all(&header::FORWARDED)
|
||||||
for pair in val.split(';') {
|
.into_iter()
|
||||||
for el in pair.split(',') {
|
.filter_map(|hdr| hdr.to_str().ok())
|
||||||
let mut items = el.trim().splitn(2, '=');
|
// "for=1.2.3.4, for=5.6.7.8; scheme=https"
|
||||||
if let Some(name) = items.next() {
|
.flat_map(|val| val.split(';'))
|
||||||
if let Some(val) = items.next() {
|
// ["for=1.2.3.4, for=5.6.7.8", " scheme=https"]
|
||||||
match &name.to_lowercase() as &str {
|
.flat_map(|vals| vals.split(','))
|
||||||
"for" => {
|
// ["for=1.2.3.4", " for=5.6.7.8", " scheme=https"]
|
||||||
if realip_remote_addr.is_none() {
|
.flat_map(|pair| {
|
||||||
realip_remote_addr = Some(val.trim());
|
let mut items = pair.trim().splitn(2, '=');
|
||||||
}
|
Some((items.next()?, items.next()?))
|
||||||
}
|
})
|
||||||
"proto" => {
|
{
|
||||||
if scheme.is_none() {
|
// [(name , val ), ... ]
|
||||||
scheme = Some(val.trim());
|
// [("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
|
||||||
"host" => {
|
// "for" value is client and rest are proxies; multiple values other properties have
|
||||||
if host.is_none() {
|
// no defined semantics
|
||||||
host = Some(val.trim());
|
//
|
||||||
}
|
// > 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
|
let scheme = scheme
|
||||||
if scheme.is_none() {
|
.or_else(|| first_header_value(req, &*X_FORWARDED_PROTO))
|
||||||
if let Some(h) = req
|
.or_else(|| req.uri.scheme().map(Scheme::as_str))
|
||||||
.headers
|
.or_else(|| Some("https").filter(|_| cfg.secure()))
|
||||||
.get(&HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap())
|
.unwrap_or("http")
|
||||||
{
|
.to_owned();
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// host
|
let host = host
|
||||||
if host.is_none() {
|
.or_else(|| first_header_value(req, &*X_FORWARDED_HOST))
|
||||||
if let Some(h) = req
|
.or_else(|| req.headers.get(&header::HOST)?.to_str().ok())
|
||||||
.headers
|
.or_else(|| req.uri.authority().map(Authority::as_str))
|
||||||
.get(&HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap())
|
.unwrap_or(cfg.host())
|
||||||
{
|
.to_owned();
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// get remote_addraddr from socketaddr
|
let realip_remote_addr = realip_remote_addr
|
||||||
let remote_addr = req.peer_addr.map(|addr| format!("{}", addr));
|
.or_else(|| first_header_value(req, &*X_FORWARDED_FOR))
|
||||||
|
.map(str::to_owned);
|
||||||
|
|
||||||
if realip_remote_addr.is_none() {
|
let remote_addr = req.peer_addr.map(|addr| addr.to_string());
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ConnectionInfo {
|
ConnectionInfo {
|
||||||
remote_addr,
|
remote_addr,
|
||||||
scheme: scheme.unwrap_or("http").to_owned(),
|
scheme,
|
||||||
host: host.unwrap_or("localhost").to_owned(),
|
host,
|
||||||
realip_remote_addr: realip_remote_addr.map(|s| s.to_owned()),
|
realip_remote_addr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,19 +177,16 @@ impl ConnectionInfo {
|
|||||||
&self.host
|
&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> {
|
pub fn remote_addr(&self) -> Option<&str> {
|
||||||
if let Some(ref remote_addr) = self.remote_addr {
|
self.remote_addr.as_deref()
|
||||||
Some(remote_addr)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/// 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
|
/// - Forwarded
|
||||||
/// - X-Forwarded-For
|
/// - X-Forwarded-For
|
||||||
@@ -173,16 +195,72 @@ impl ConnectionInfo {
|
|||||||
/// # Security
|
/// # Security
|
||||||
/// Do not use this function for security purposes, unless you can ensure the Forwarded and
|
/// 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
|
/// X-Forwarded-For headers cannot be spoofed by the client. If you want the client's socket
|
||||||
/// address explicitly, use
|
/// address explicitly, use [`HttpRequest::peer_addr()`][peer_addr] instead.
|
||||||
/// [`HttpRequest::peer_addr()`](super::web::HttpRequest::peer_addr()) instead.
|
///
|
||||||
|
/// [peer_addr]: crate::web::HttpRequest::peer_addr()
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn realip_remote_addr(&self) -> Option<&str> {
|
pub fn realip_remote_addr(&self) -> Option<&str> {
|
||||||
if let Some(ref r) = self.realip_remote_addr {
|
self.realip_remote_addr
|
||||||
Some(r)
|
.as_deref()
|
||||||
} else if let Some(ref remote_addr) = self.remote_addr {
|
.or_else(|| self.remote_addr.as_deref())
|
||||||
Some(remote_addr)
|
}
|
||||||
} else {
|
}
|
||||||
None
|
|
||||||
|
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 super::*;
|
||||||
use crate::test::TestRequest;
|
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]
|
#[test]
|
||||||
fn test_forwarded() {
|
fn info_default() {
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
let info = req.connection_info();
|
let info = req.connection_info();
|
||||||
assert_eq!(info.scheme(), "http");
|
assert_eq!(info.scheme(), "http");
|
||||||
assert_eq!(info.host(), "localhost:8080");
|
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()
|
let req = TestRequest::default()
|
||||||
.insert_header((
|
.insert_header((
|
||||||
header::FORWARDED,
|
header::FORWARDED,
|
||||||
@@ -212,31 +337,118 @@ mod tests {
|
|||||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||||
|
|
||||||
let req = TestRequest::default()
|
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();
|
.to_http_request();
|
||||||
|
|
||||||
let info = req.connection_info();
|
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.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()
|
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();
|
.to_http_request();
|
||||||
let info = req.connection_info();
|
let info = req.connection_info();
|
||||||
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
assert_eq!(info.realip_remote_addr(), Some("192.0.2.60"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn forwarded_weird_whitespace() {
|
||||||
let req = TestRequest::default()
|
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();
|
.to_http_request();
|
||||||
let info = req.connection_info();
|
let info = req.connection_info();
|
||||||
assert_eq!(info.host(), "192.0.2.60");
|
assert_eq!(info.realip_remote_addr(), Some("1.2.3.4"));
|
||||||
assert_eq!(info.realip_remote_addr(), None);
|
assert_eq!(info.scheme(), "https");
|
||||||
|
|
||||||
let req = TestRequest::default()
|
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();
|
.to_http_request();
|
||||||
let info = req.connection_info();
|
let info = req.connection_info();
|
||||||
assert_eq!(info.scheme(), "https");
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -25,7 +25,6 @@
|
|||||||
//! * [Website & User Guide](https://actix.rs/)
|
//! * [Website & User Guide](https://actix.rs/)
|
||||||
//! * [Examples Repository](https://github.com/actix/examples)
|
//! * [Examples Repository](https://github.com/actix/examples)
|
||||||
//! * [Community Chat on Discord](https://discord.gg/NWpN5mmg3x)
|
//! * [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:
|
//! To get started navigating the API docs, you may consider looking at the following pages first:
|
||||||
//!
|
//!
|
||||||
@@ -131,7 +130,7 @@ pub mod dev {
|
|||||||
pub use crate::config::{AppConfig, AppService};
|
pub use crate::config::{AppConfig, AppService};
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use crate::handler::Handler;
|
pub use crate::handler::Handler;
|
||||||
pub use crate::info::ConnectionInfo;
|
pub use crate::info::{ConnectionInfo, PeerAddr};
|
||||||
pub use crate::rmap::ResourceMap;
|
pub use crate::rmap::ResourceMap;
|
||||||
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
pub use crate::service::{HttpServiceFactory, ServiceRequest, ServiceResponse, WebService};
|
||||||
|
|
||||||
@@ -149,7 +148,9 @@ pub mod dev {
|
|||||||
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, ResponseHead};
|
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, ResponseHead};
|
||||||
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
|
pub use actix_router::{Path, ResourceDef, ResourcePath, Url};
|
||||||
pub use actix_server::Server;
|
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> {
|
pub(crate) fn insert_slash(mut patterns: Vec<String>) -> Vec<String> {
|
||||||
for path in &mut patterns {
|
for path in &mut patterns {
|
||||||
|
@@ -200,8 +200,7 @@ impl AcceptEncoding {
|
|||||||
let mut encodings = raw
|
let mut encodings = raw
|
||||||
.replace(' ', "")
|
.replace(' ', "")
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(|l| AcceptEncoding::new(l))
|
.filter_map(|l| AcceptEncoding::new(l))
|
||||||
.flatten()
|
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
encodings.sort();
|
encodings.sort();
|
||||||
|
@@ -111,11 +111,7 @@ impl HttpRequest {
|
|||||||
/// E.g., id=10
|
/// E.g., id=10
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn query_string(&self) -> &str {
|
pub fn query_string(&self) -> &str {
|
||||||
if let Some(query) = self.uri().query().as_ref() {
|
self.uri().query().unwrap_or_default()
|
||||||
query
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a reference to the Path parameters.
|
/// Get a reference to the Path parameters.
|
||||||
@@ -347,7 +343,7 @@ impl Drop for HttpRequest {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::{web, App, HttpRequest};
|
/// use actix_web::{web, App, HttpRequest};
|
||||||
/// use serde_derive::Deserialize;
|
/// use serde::Deserialize;
|
||||||
///
|
///
|
||||||
/// /// extract `Thing` from request
|
/// /// extract `Thing` from request
|
||||||
/// async fn index(req: HttpRequest) -> String {
|
/// async fn index(req: HttpRequest) -> String {
|
||||||
@@ -711,6 +707,8 @@ mod tests {
|
|||||||
assert_eq!(body, Bytes::from_static(b"1"));
|
assert_eq!(body, Bytes::from_static(b"1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_extensions_dropped() {
|
async fn test_extensions_dropped() {
|
||||||
struct Tracker {
|
struct Tracker {
|
||||||
|
172
src/resource.rs
172
src/resource.rs
@@ -169,40 +169,38 @@ where
|
|||||||
self
|
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.
|
/// 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 {
|
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||||
self.app_data
|
self.app_data
|
||||||
.get_or_insert_with(Extensions::new)
|
.get_or_insert_with(Extensions::new)
|
||||||
@@ -211,6 +209,14 @@ where
|
|||||||
self
|
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.
|
/// Register a new route and add handler. This route matches all requests.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@@ -226,7 +232,6 @@ where
|
|||||||
/// This is shortcut for:
|
/// This is shortcut for:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// # use actix_web::*;
|
/// # use actix_web::*;
|
||||||
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
/// # fn index(req: HttpRequest) -> HttpResponse { unimplemented!() }
|
||||||
/// App::new().service(web::resource("/").route(web::route().to(index)));
|
/// App::new().service(web::resource("/").route(web::route().to(index)));
|
||||||
@@ -395,34 +400,28 @@ where
|
|||||||
*rdef.name_mut() = name.clone();
|
*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 {
|
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
||||||
routes: self.routes,
|
routes: self.routes,
|
||||||
app_data: self.app_data.map(Rc::new),
|
|
||||||
default: self.default,
|
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 {
|
pub struct ResourceFactory {
|
||||||
routes: Vec<Route>,
|
routes: Vec<Route>,
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
default: HttpNewService,
|
default: HttpNewService,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,8 +440,6 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
|||||||
// construct route service factory futures
|
// construct route service factory futures
|
||||||
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
||||||
|
|
||||||
let app_data = self.app_data.clone();
|
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let default = default_fut.await?;
|
let default = default_fut.await?;
|
||||||
let routes = factory_fut
|
let routes = factory_fut
|
||||||
@@ -450,18 +447,13 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
Ok(ResourceService {
|
Ok(ResourceService { routes, default })
|
||||||
routes,
|
|
||||||
app_data,
|
|
||||||
default,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ResourceService {
|
pub struct ResourceService {
|
||||||
routes: Vec<RouteService>,
|
routes: Vec<RouteService>,
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,20 +465,12 @@ impl Service<ServiceRequest> for ResourceService {
|
|||||||
actix_service::always_ready!();
|
actix_service::always_ready!();
|
||||||
|
|
||||||
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
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 route.check(&mut req) {
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
return route.call(req);
|
return route.call(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
self.default.call(req)
|
self.default.call(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -523,11 +507,14 @@ mod tests {
|
|||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
|
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
use crate::{
|
||||||
use crate::middleware::DefaultHeaders;
|
guard,
|
||||||
use crate::service::ServiceRequest;
|
http::{header, HeaderValue, Method, StatusCode},
|
||||||
use crate::test::{call_service, init_service, TestRequest};
|
middleware::DefaultHeaders,
|
||||||
use crate::{guard, web, App, Error, HttpResponse};
|
service::{ServiceRequest, ServiceResponse},
|
||||||
|
test::{call_service, init_service, TestRequest},
|
||||||
|
web, App, Error, HttpMessage, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware() {
|
async fn test_middleware() {
|
||||||
@@ -694,6 +681,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated `{App, Resource}::data`
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
@@ -726,6 +715,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated `{App, Resource}::data`
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_default_service() {
|
async fn test_data_default_service() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
@@ -744,4 +735,39 @@ mod tests {
|
|||||||
let resp = call_service(&srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
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;
|
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.
|
/// Create an error response.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_error(error: impl Into<Error>) -> Self {
|
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_http::http::Method;
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
boxed::{self, BoxService, BoxServiceFactory},
|
boxed::{self, BoxService, BoxServiceFactory},
|
||||||
Service, ServiceFactory,
|
Service, ServiceFactory, ServiceFactoryExt,
|
||||||
};
|
};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
|
||||||
@@ -128,9 +128,10 @@ impl Route {
|
|||||||
|
|
||||||
/// Set handler function, use request extractors for parameters.
|
/// Set handler function, use request extractors for parameters.
|
||||||
///
|
///
|
||||||
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::{web, http, App};
|
/// use actix_web::{web, http, App};
|
||||||
/// use serde_derive::Deserialize;
|
/// use serde::Deserialize;
|
||||||
///
|
///
|
||||||
/// #[derive(Deserialize)]
|
/// #[derive(Deserialize)]
|
||||||
/// struct Info {
|
/// struct Info {
|
||||||
@@ -154,7 +155,7 @@ impl Route {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use std::collections::HashMap;
|
/// # use std::collections::HashMap;
|
||||||
/// # use serde_derive::Deserialize;
|
/// # use serde::Deserialize;
|
||||||
/// use actix_web::{web, App};
|
/// use actix_web::{web, App};
|
||||||
///
|
///
|
||||||
/// #[derive(Deserialize)]
|
/// #[derive(Deserialize)]
|
||||||
@@ -184,6 +185,53 @@ impl Route {
|
|||||||
self.service = boxed::factory(HandlerService::new(handler));
|
self.service = boxed::factory(HandlerService::new(handler));
|
||||||
self
|
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)]
|
#[cfg(test)]
|
||||||
@@ -192,9 +240,12 @@ mod tests {
|
|||||||
|
|
||||||
use actix_rt::time::sleep;
|
use actix_rt::time::sleep;
|
||||||
use bytes::Bytes;
|
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::test::{call_service, init_service, read_body, TestRequest};
|
||||||
use crate::{error, web, App, HttpResponse};
|
use crate::{error, web, App, HttpResponse};
|
||||||
|
|
||||||
@@ -268,4 +319,65 @@ mod tests {
|
|||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
assert_eq!(body, Bytes::from_static(b"{\"name\":\"test\"}"));
|
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::{cell::RefCell, fmt, future::Future, mem, rc::Rc};
|
||||||
use std::fmt;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use actix_http::Extensions;
|
use actix_http::Extensions;
|
||||||
use actix_router::{ResourceDef, Router};
|
use actix_router::{ResourceDef, Router};
|
||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt,
|
apply, apply_fn_factory,
|
||||||
Transform,
|
boxed::{self, BoxService, BoxServiceFactory},
|
||||||
|
IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt, Transform,
|
||||||
};
|
};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
|
||||||
use crate::config::ServiceConfig;
|
use crate::{
|
||||||
use crate::data::Data;
|
config::ServiceConfig,
|
||||||
use crate::dev::{AppService, HttpServiceFactory};
|
data::Data,
|
||||||
use crate::error::Error;
|
dev::{AppService, HttpServiceFactory},
|
||||||
use crate::guard::Guard;
|
guard::Guard,
|
||||||
use crate::resource::Resource;
|
rmap::ResourceMap,
|
||||||
use crate::rmap::ResourceMap;
|
service::{AppServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse},
|
||||||
use crate::route::Route;
|
Error, Resource, Route,
|
||||||
use crate::service::{
|
|
||||||
AppServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
@@ -71,16 +66,17 @@ pub struct Scope<T = ScopeEndpoint> {
|
|||||||
impl Scope {
|
impl Scope {
|
||||||
/// Create a new scope
|
/// Create a new scope
|
||||||
pub fn new(path: &str) -> Scope {
|
pub fn new(path: &str) -> Scope {
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let factory_ref = Rc::new(RefCell::new(None));
|
||||||
|
|
||||||
Scope {
|
Scope {
|
||||||
endpoint: ScopeEndpoint::new(fref.clone()),
|
endpoint: ScopeEndpoint::new(Rc::clone(&factory_ref)),
|
||||||
rdef: path.to_string(),
|
rdef: path.to_string(),
|
||||||
app_data: None,
|
app_data: None,
|
||||||
guards: Vec::new(),
|
guards: Vec::new(),
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
default: None,
|
default: None,
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
factory_ref: fref,
|
factory_ref,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,39 +116,38 @@ where
|
|||||||
self
|
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.
|
/// 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 {
|
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
|
||||||
self.app_data
|
self.app_data
|
||||||
.get_or_insert_with(Extensions::new)
|
.get_or_insert_with(Extensions::new)
|
||||||
@@ -161,15 +156,20 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run external configuration as part of the scope building
|
/// Add scope data after wrapping in `Data<T>`.
|
||||||
/// process
|
|
||||||
///
|
///
|
||||||
/// This function is useful for moving parts of configuration to a
|
/// Deprecated in favor of [`app_data`](Self::app_data).
|
||||||
/// different module or even library. For example,
|
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
||||||
/// some of the resource's configuration could be moved to different module.
|
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};
|
/// use actix_web::{web, middleware, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// // this function could be located in different module
|
/// // this function could be located in different module
|
||||||
@@ -190,18 +190,21 @@ where
|
|||||||
/// .route("/index.html", web::get().to(|| HttpResponse::Ok()));
|
/// .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
|
where
|
||||||
F: FnOnce(&mut ServiceConfig),
|
F: FnOnce(&mut ServiceConfig),
|
||||||
{
|
{
|
||||||
let mut cfg = ServiceConfig::new();
|
let mut cfg = ServiceConfig::new();
|
||||||
f(&mut cfg);
|
cfg_fn(&mut cfg);
|
||||||
|
|
||||||
self.services.extend(cfg.services);
|
self.services.extend(cfg.services);
|
||||||
self.external.extend(cfg.external);
|
self.external.extend(cfg.external);
|
||||||
|
|
||||||
|
// TODO: add Extensions::is_empty check and conditionally insert data
|
||||||
self.app_data
|
self.app_data
|
||||||
.get_or_insert_with(Extensions::new)
|
.get_or_insert_with(Extensions::new)
|
||||||
.extend(cfg.app_data);
|
.extend(cfg.app_data);
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -418,13 +421,12 @@ where
|
|||||||
let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef));
|
let mut rmap = ResourceMap::new(ResourceDef::root_prefix(&self.rdef));
|
||||||
|
|
||||||
// external resources
|
// 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);
|
rmap.add(&mut rdef, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
// complete scope pipeline creation
|
// complete scope pipeline creation
|
||||||
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
||||||
app_data: self.app_data.take().map(Rc::new),
|
|
||||||
default,
|
default,
|
||||||
services: cfg
|
services: cfg
|
||||||
.into_services()
|
.into_services()
|
||||||
@@ -446,18 +448,28 @@ where
|
|||||||
Some(self.guards)
|
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
|
// register final service
|
||||||
config.register_service(
|
config.register_service(
|
||||||
ResourceDef::root_prefix(&self.rdef),
|
ResourceDef::root_prefix(&self.rdef),
|
||||||
guards,
|
guards,
|
||||||
self.endpoint,
|
endpoint,
|
||||||
Some(Rc::new(rmap)),
|
Some(Rc::new(rmap)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeFactory {
|
pub struct ScopeFactory {
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<HttpNewService>,
|
||||||
}
|
}
|
||||||
@@ -485,8 +497,6 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let app_data = self.app_data.clone();
|
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let default = default_fut.await?;
|
let default = default_fut.await?;
|
||||||
|
|
||||||
@@ -502,17 +512,12 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
|||||||
})
|
})
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
Ok(ScopeService {
|
Ok(ScopeService { router, default })
|
||||||
app_data,
|
|
||||||
router,
|
|
||||||
default,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeService {
|
pub struct ScopeService {
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
@@ -536,10 +541,6 @@ impl Service<ServiceRequest> for ScopeService {
|
|||||||
true
|
true
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some((srv, _info)) = res {
|
if let Some((srv, _info)) = res {
|
||||||
srv.call(req)
|
srv.call(req)
|
||||||
} else {
|
} else {
|
||||||
@@ -578,12 +579,15 @@ mod tests {
|
|||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
use crate::dev::Body;
|
use crate::{
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
dev::Body,
|
||||||
use crate::middleware::DefaultHeaders;
|
guard,
|
||||||
use crate::service::ServiceRequest;
|
http::{header, HeaderValue, Method, StatusCode},
|
||||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
middleware::DefaultHeaders,
|
||||||
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
service::{ServiceRequest, ServiceResponse},
|
||||||
|
test::{call_service, init_service, read_body, TestRequest},
|
||||||
|
web, App, HttpMessage, HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope() {
|
async fn test_scope() {
|
||||||
@@ -915,10 +919,7 @@ mod tests {
|
|||||||
async fn test_default_resource_propagation() {
|
async fn test_default_resource_propagation() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(web::scope("/app1").default_service(web::to(HttpResponse::BadRequest)))
|
||||||
web::scope("/app1")
|
|
||||||
.default_service(web::resource("").to(HttpResponse::BadRequest)),
|
|
||||||
)
|
|
||||||
.service(web::scope("/app2"))
|
.service(web::scope("/app2"))
|
||||||
.default_service(|r: ServiceRequest| {
|
.default_service(|r: ServiceRequest| {
|
||||||
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
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]
|
#[actix_rt::test]
|
||||||
async fn test_override_data() {
|
async fn test_override_data() {
|
||||||
let srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
@@ -1008,6 +1046,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated `{App, Scope}::data`
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data_default_service() {
|
async fn test_override_data_default_service() {
|
||||||
let srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
|
@@ -292,15 +292,15 @@ where
|
|||||||
let c = cfg.lock().unwrap();
|
let c = cfg.lock().unwrap();
|
||||||
let host = c.host.clone().unwrap_or_else(|| format!("{}", addr));
|
let host = c.host.clone().unwrap_or_else(|| format!("{}", addr));
|
||||||
|
|
||||||
let svc = HttpService::build()
|
let mut svc = HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout)
|
.client_timeout(c.client_timeout)
|
||||||
.local_addr(addr);
|
.local_addr(addr);
|
||||||
|
|
||||||
let svc = if let Some(handler) = on_connect_fn.clone() {
|
if let Some(handler) = on_connect_fn.clone() {
|
||||||
svc.on_connect_ext(move |io: &_, ext: _| (handler)(io as &dyn Any, ext))
|
svc = svc.on_connect_ext(move |io: &_, ext: _| {
|
||||||
} else {
|
(handler)(io as &dyn Any, ext)
|
||||||
svc
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
let fac = factory()
|
let fac = factory()
|
||||||
@@ -461,17 +461,15 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !success {
|
if success {
|
||||||
if let Some(e) = err.take() {
|
|
||||||
Err(e)
|
|
||||||
} else {
|
|
||||||
Err(io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
"Can not bind to address.",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(sockets)
|
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({
|
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)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout);
|
.client_timeout(c.client_timeout);
|
||||||
|
|
||||||
let svc = if let Some(handler) = on_connect_fn.clone() {
|
if let Some(handler) = on_connect_fn.clone() {
|
||||||
svc.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext))
|
svc = svc
|
||||||
} else {
|
.on_connect_ext(move |io: &_, ext: _| (&*handler)(io as &dyn Any, ext));
|
||||||
svc
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let fac = factory()
|
let fac = factory()
|
||||||
.into_factory()
|
.into_factory()
|
||||||
|
@@ -17,9 +17,8 @@ use crate::{
|
|||||||
dev::insert_slash,
|
dev::insert_slash,
|
||||||
guard::Guard,
|
guard::Guard,
|
||||||
info::ConnectionInfo,
|
info::ConnectionInfo,
|
||||||
request::HttpRequest,
|
|
||||||
rmap::ResourceMap,
|
rmap::ResourceMap,
|
||||||
Error, HttpResponse,
|
Error, HttpRequest, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait HttpServiceFactory {
|
pub trait HttpServiceFactory {
|
||||||
@@ -80,6 +79,12 @@ impl ServiceRequest {
|
|||||||
(self.req, self.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.
|
/// Construct request from parts.
|
||||||
pub fn from_parts(req: HttpRequest, payload: Payload) -> Self {
|
pub fn from_parts(req: HttpRequest, payload: Payload) -> Self {
|
||||||
Self { req, payload }
|
Self { req, payload }
|
||||||
@@ -162,11 +167,7 @@ impl ServiceRequest {
|
|||||||
/// E.g., id=10
|
/// E.g., id=10
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn query_string(&self) -> &str {
|
pub fn query_string(&self) -> &str {
|
||||||
if let Some(query) = self.uri().query().as_ref() {
|
self.uri().query().unwrap_or_default()
|
||||||
query
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Peer socket address.
|
/// Peer socket address.
|
||||||
@@ -649,6 +650,8 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service_data() {
|
async fn test_service_data() {
|
||||||
let srv =
|
let srv =
|
||||||
|
@@ -613,6 +613,11 @@ impl TestRequest {
|
|||||||
let req = self.to_request();
|
let req = self.to_request();
|
||||||
call_service(app, req).await
|
call_service(app, req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn set_server_hostname(&mut self, host: &str) {
|
||||||
|
self.config.set_host(host)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -839,6 +844,8 @@ mod tests {
|
|||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_server_data() {
|
async fn test_server_data() {
|
||||||
async fn handler(data: web::Data<usize>) -> impl Responder {
|
async fn handler(data: web::Data<usize>) -> impl Responder {
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
//! For URL encoded form helper documentation, see [`Form`].
|
//! For URL encoded form helper documentation, see [`Form`].
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
fmt,
|
fmt,
|
||||||
future::Future,
|
future::Future,
|
||||||
ops,
|
ops,
|
||||||
@@ -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)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct Form<T>(pub T);
|
pub struct Form<T>(pub T);
|
||||||
|
|
||||||
@@ -380,7 +385,7 @@ where
|
|||||||
} else {
|
} else {
|
||||||
let body = encoding
|
let body = encoding
|
||||||
.decode_without_bom_handling_and_without_replacement(&body)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map(|s| s.into_owned())
|
.map(Cow::into_owned)
|
||||||
.ok_or(UrlencodedError::Encoding)?;
|
.ok_or(UrlencodedError::Encoding)?;
|
||||||
|
|
||||||
serde_urlencoded::from_str::<T>(&body).map_err(UrlencodedError::Parse)
|
serde_urlencoded::from_str::<T>(&body).map_err(UrlencodedError::Parse)
|
||||||
|
@@ -103,8 +103,7 @@ where
|
|||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
let error_handler = req
|
let error_handler = req
|
||||||
.app_data::<Self::Config>()
|
.app_data::<Self::Config>()
|
||||||
.map(|c| c.ehandler.clone())
|
.and_then(|c| c.ehandler.clone());
|
||||||
.unwrap_or(None);
|
|
||||||
|
|
||||||
ready(
|
ready(
|
||||||
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
//! Basic binary and string payload extractors.
|
//! Basic binary and string payload extractors.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
str,
|
str,
|
||||||
@@ -190,7 +191,7 @@ fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, E
|
|||||||
} else {
|
} else {
|
||||||
Ok(encoding
|
Ok(encoding
|
||||||
.decode_without_bom_handling_and_without_replacement(&body)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map(|s| s.into_owned())
|
.map(Cow::into_owned)
|
||||||
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
|
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -398,6 +399,8 @@ mod tests {
|
|||||||
assert!(cfg.check_mimetype(&req).is_ok());
|
assert!(cfg.check_mimetype(&req).is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_config_recall_locations() {
|
async fn test_config_recall_locations() {
|
||||||
async fn bytes_handler(_: Bytes) -> impl Responder {
|
async fn bytes_handler(_: Bytes) -> impl Responder {
|
||||||
|
@@ -119,8 +119,7 @@ where
|
|||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
let error_handler = req
|
let error_handler = req
|
||||||
.app_data::<Self::Config>()
|
.app_data::<Self::Config>()
|
||||||
.map(|c| c.err_handler.clone())
|
.and_then(|c| c.err_handler.clone());
|
||||||
.unwrap_or(None);
|
|
||||||
|
|
||||||
serde_urlencoded::from_str::<T>(req.query_string())
|
serde_urlencoded::from_str::<T>(req.query_string())
|
||||||
.map(|val| ok(Query(val)))
|
.map(|val| ok(Query(val)))
|
||||||
|
@@ -43,7 +43,6 @@ pub use crate::types::*;
|
|||||||
/// the exposed `Params` object:
|
/// the exposed `Params` object:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// use actix_web::{web, App, HttpResponse};
|
/// use actix_web::{web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// let app = App::new().service(
|
/// let app = App::new().service(
|
||||||
|
99
tests/test_error_propagation.rs
Normal file
99
tests/test_error_propagation.rs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use actix_utils::future::{ok, Ready};
|
||||||
|
use actix_web::{
|
||||||
|
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
|
||||||
|
get,
|
||||||
|
test::{call_service, init_service, TestRequest},
|
||||||
|
ResponseError,
|
||||||
|
};
|
||||||
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
use futures_util::lock::Mutex;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct MyError;
|
||||||
|
|
||||||
|
impl ResponseError for MyError {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for MyError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "A custom error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/test")]
|
||||||
|
async fn test() -> Result<actix_web::HttpResponse, actix_web::error::Error> {
|
||||||
|
return Err(MyError.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SpyMiddleware(Arc<Mutex<Option<bool>>>);
|
||||||
|
|
||||||
|
impl<S, B> Transform<S, ServiceRequest> for SpyMiddleware
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<B>;
|
||||||
|
type Error = actix_web::Error;
|
||||||
|
type Transform = Middleware<S>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
ok(Middleware {
|
||||||
|
was_error: self.0.clone(),
|
||||||
|
service,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub struct Middleware<S> {
|
||||||
|
was_error: Arc<Mutex<Option<bool>>>,
|
||||||
|
service: S,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, B> Service<ServiceRequest> for Middleware<S>
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<B>;
|
||||||
|
type Error = actix_web::Error;
|
||||||
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
|
forward_ready!(service);
|
||||||
|
|
||||||
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
|
let lock = self.was_error.clone();
|
||||||
|
let response_future = self.service.call(req);
|
||||||
|
Box::pin(async move {
|
||||||
|
let response = response_future.await;
|
||||||
|
if let Ok(success) = &response {
|
||||||
|
*lock.lock().await = Some(success.response().error().is_some());
|
||||||
|
}
|
||||||
|
response
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn error_cause_should_be_propagated_to_middlewares() {
|
||||||
|
let lock = Arc::new(Mutex::new(None));
|
||||||
|
let spy_middleware = SpyMiddleware(lock.clone());
|
||||||
|
|
||||||
|
let app = init_service(
|
||||||
|
actix_web::App::new()
|
||||||
|
.wrap(spy_middleware.clone())
|
||||||
|
.service(test),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
call_service(&app, TestRequest::with_uri("/test").to_request()).await;
|
||||||
|
|
||||||
|
let was_error_captured = lock.lock().await.unwrap();
|
||||||
|
assert!(was_error_captured);
|
||||||
|
}
|
@@ -879,7 +879,7 @@ async fn test_brotli_encoding_large_openssl() {
|
|||||||
assert_eq!(bytes, Bytes::from(data));
|
assert_eq!(bytes, Bytes::from(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "rustls", feature = "openssl"))]
|
#[cfg(feature = "rustls")]
|
||||||
mod plus_rustls {
|
mod plus_rustls {
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
|
||||||
@@ -1028,6 +1028,8 @@ async fn test_normalize() {
|
|||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow deprecated App::data
|
||||||
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_drop() {
|
async fn test_data_drop() {
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
|
Reference in New Issue
Block a user