mirror of https://github.com/fafhrd91/actix-web
commit
488365e7ec
|
@ -26,6 +26,21 @@ jobs:
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# install OpenSSL on Windows
|
||||||
|
- name: Set vcpkg root
|
||||||
|
if: matrix.target.triple == 'x86_64-pc-windows-msvc'
|
||||||
|
run: echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append
|
||||||
|
- name: Install OpenSSL
|
||||||
|
if: matrix.target.triple == 'x86_64-pc-windows-msvc'
|
||||||
|
run: vcpkg install openssl:x64-windows
|
||||||
|
|
||||||
|
- name: Install ${{ matrix.version }}
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: ${{ matrix.version }}-${{ matrix.target.triple }}
|
||||||
|
profile: minimal
|
||||||
|
override: true
|
||||||
|
|
||||||
- name: Install ${{ matrix.version }}
|
- name: Install ${{ matrix.version }}
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
name: Upload documentation
|
name: Upload Documentation
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches: [master]
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.repository == 'actix/actix-web'
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
@ -20,14 +18,14 @@ jobs:
|
||||||
profile: minimal
|
profile: minimal
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
- name: check build
|
- name: Build Docs
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
command: doc
|
command: doc
|
||||||
args: --workspace --all-features --no-deps
|
args: --workspace --all-features --no-deps
|
||||||
|
|
||||||
- name: Tweak HTML
|
- name: Tweak HTML
|
||||||
run: echo "<meta http-equiv=refresh content=0;url=os_balloon/index.html>" > target/doc/index.html
|
run: echo '<meta http-equiv="refresh" content="0;url=actix_web/index.html">' > target/doc/index.html
|
||||||
|
|
||||||
- name: Deploy to GitHub Pages
|
- name: Deploy to GitHub Pages
|
||||||
uses: JamesIves/github-pages-deploy-action@3.7.1
|
uses: JamesIves/github-pages-deploy-action@3.7.1
|
||||||
|
|
36
CHANGES.md
36
CHANGES.md
|
@ -1,10 +1,26 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Fixed
|
||||||
|
* Double ampersand in Logger format is escaped correctly. [#2067]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* `CustomResponder` would return error as `HttpResponse` when `CustomResponder::with_header` failed instead of skipping.
|
||||||
|
(Only the first error is kept when multiple error occur) [#2093]
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
* The `client` mod was removed. Clients should now use `awc` directly.
|
||||||
|
[871ca5e4](https://github.com/actix/actix-web/commit/871ca5e4ae2bdc22d1ea02701c2992fa8d04aed7)
|
||||||
|
|
||||||
|
[#2067]: https://github.com/actix/actix-web/pull/2067
|
||||||
|
[#2093]: https://github.com/actix/actix-web/pull/2093
|
||||||
|
|
||||||
|
|
||||||
|
## 4.0.0-beta.4 - 2021-03-09
|
||||||
### Changed
|
### Changed
|
||||||
* Feature `cookies` is now optional and enabled by default. [#1981]
|
* Feature `cookies` is now optional and enabled by default. [#1981]
|
||||||
* `JsonBody::new` returns a default limit of 32kB to be consistent with `JsonConfig` and the
|
* `JsonBody::new` returns a default limit of 32kB to be consistent with `JsonConfig` and the default
|
||||||
default behaviour of the `web::Json<T>` extractor. [#2010]
|
behaviour of the `web::Json<T>` extractor. [#2010]
|
||||||
|
|
||||||
[#1981]: https://github.com/actix/actix-web/pull/1981
|
[#1981]: https://github.com/actix/actix-web/pull/1981
|
||||||
[#2010]: https://github.com/actix/actix-web/pull/2010
|
[#2010]: https://github.com/actix/actix-web/pull/2010
|
||||||
|
@ -168,7 +184,7 @@
|
||||||
|
|
||||||
## 3.0.0-beta.4 - 2020-09-09
|
## 3.0.0-beta.4 - 2020-09-09
|
||||||
### Added
|
### Added
|
||||||
* `middleware::NormalizePath` now has configurable behaviour for either always having a trailing
|
* `middleware::NormalizePath` now has configurable behavior for either always having a trailing
|
||||||
slash, or as the new addition, always trimming trailing slashes. [#1639]
|
slash, or as the new addition, always trimming trailing slashes. [#1639]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -496,7 +512,7 @@
|
||||||
|
|
||||||
## [1.0.0-rc] - 2019-05-18
|
## [1.0.0-rc] - 2019-05-18
|
||||||
|
|
||||||
### Add
|
### Added
|
||||||
|
|
||||||
* Add `Query<T>::from_query()` to extract parameters from a query string. #846
|
* Add `Query<T>::from_query()` to extract parameters from a query string. #846
|
||||||
* `QueryConfig`, similar to `JsonConfig` for customizing error handling of query extractors.
|
* `QueryConfig`, similar to `JsonConfig` for customizing error handling of query extractors.
|
||||||
|
@ -512,7 +528,7 @@
|
||||||
|
|
||||||
## [1.0.0-beta.4] - 2019-05-12
|
## [1.0.0-beta.4] - 2019-05-12
|
||||||
|
|
||||||
### Add
|
### Added
|
||||||
|
|
||||||
* Allow to set/override app data on scope level
|
* Allow to set/override app data on scope level
|
||||||
|
|
||||||
|
@ -538,7 +554,7 @@
|
||||||
|
|
||||||
* CORS handling without headers #702
|
* CORS handling without headers #702
|
||||||
|
|
||||||
* Allow to construct `Data` instances to avoid double `Arc` for `Send + Sync` types.
|
* Allow constructing `Data` instances to avoid double `Arc` for `Send + Sync` types.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
@ -602,7 +618,7 @@
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* Allow to use any service as default service.
|
* Allow using any service as default service.
|
||||||
|
|
||||||
* Remove generic type for request payload, always use default.
|
* Remove generic type for request payload, always use default.
|
||||||
|
|
||||||
|
@ -665,13 +681,13 @@
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
* rustls support
|
* Rustls support
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* use forked cookie
|
* Use forked cookie
|
||||||
|
|
||||||
* multipart::Field renamed to MultipartField
|
* Multipart::Field renamed to MultipartField
|
||||||
|
|
||||||
## [1.0.0-alpha.1] - 2019-03-28
|
## [1.0.0-alpha.1] - 2019-03-28
|
||||||
|
|
||||||
|
|
22
Cargo.toml
22
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-web"
|
name = "actix-web"
|
||||||
version = "4.0.0-beta.3"
|
version = "4.0.0-beta.4"
|
||||||
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"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -66,7 +66,7 @@ required-features = ["compress"]
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "test_server"
|
name = "test_server"
|
||||||
required-features = ["compress"]
|
required-features = ["compress", "cookies"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "on_connect"
|
name = "on_connect"
|
||||||
|
@ -80,15 +80,15 @@ required-features = ["rustls"]
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-macros = "0.2.0"
|
actix-macros = "0.2.0"
|
||||||
actix-router = "0.2.7"
|
actix-router = "0.2.7"
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-service = "2.0.0-beta.4"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-utils = "3.0.0-beta.2"
|
actix-utils = "3.0.0-beta.2"
|
||||||
actix-tls = { version = "3.0.0-beta.3", default-features = false, optional = true }
|
actix-tls = { version = "3.0.0-beta.4", default-features = false, optional = true }
|
||||||
|
|
||||||
actix-web-codegen = "0.5.0-beta.1"
|
actix-web-codegen = "0.5.0-beta.2"
|
||||||
actix-http = "3.0.0-beta.3"
|
actix-http = "3.0.0-beta.4"
|
||||||
awc = { version = "3.0.0-beta.2", default-features = false }
|
awc = { version = "3.0.0-beta.3", default-features = false }
|
||||||
|
|
||||||
ahash = "0.7"
|
ahash = "0.7"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
|
@ -105,18 +105,12 @@ serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_urlencoded = "0.7"
|
serde_urlencoded = "0.7"
|
||||||
smallvec = "1.6"
|
smallvec = "1.6"
|
||||||
socket2 = "0.3.16"
|
socket2 = "0.4.0"
|
||||||
time = { version = "0.2.23", default-features = false, features = ["std"] }
|
time = { version = "0.2.23", default-features = false, features = ["std"] }
|
||||||
tls-openssl = { package = "openssl", version = "0.10.9", optional = true }
|
tls-openssl = { package = "openssl", version = "0.10.9", optional = true }
|
||||||
tls-rustls = { package = "rustls", version = "0.19.0", optional = true }
|
tls-rustls = { package = "rustls", version = "0.19.0", optional = true }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies.tls-openssl]
|
|
||||||
version = "0.10.9"
|
|
||||||
package = "openssl"
|
|
||||||
features = ["vendored"]
|
|
||||||
optional = true
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
brotli2 = "0.3.2"
|
brotli2 = "0.3.2"
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
|
|
28
README.md
28
README.md
|
@ -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.2)
|
[](https://docs.rs/actix-web/4.0.0-beta.4)
|
||||||
[](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.2)
|
[](https://deps.rs/crate/actix-web/4.0.0-beta.4)
|
||||||
<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)
|
||||||
|
@ -71,18 +71,18 @@ async fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
### More examples
|
### More examples
|
||||||
|
|
||||||
* [Basic Setup](https://github.com/actix/examples/tree/master/basics/)
|
* [Basic Setup](https://github.com/actix/examples/tree/master/basics/basics/)
|
||||||
* [Application State](https://github.com/actix/examples/tree/master/state/)
|
* [Application State](https://github.com/actix/examples/tree/master/basics/state/)
|
||||||
* [JSON Handling](https://github.com/actix/examples/tree/master/json/)
|
* [JSON Handling](https://github.com/actix/examples/tree/master/json/json/)
|
||||||
* [Multipart Streams](https://github.com/actix/examples/tree/master/multipart/)
|
* [Multipart Streams](https://github.com/actix/examples/tree/master/forms/multipart/)
|
||||||
* [Diesel Integration](https://github.com/actix/examples/tree/master/diesel/)
|
* [Diesel Integration](https://github.com/actix/examples/tree/master/database_interactions/diesel/)
|
||||||
* [r2d2 Integration](https://github.com/actix/examples/tree/master/r2d2/)
|
* [r2d2 Integration](https://github.com/actix/examples/tree/master/database_interactions/r2d2/)
|
||||||
* [Simple WebSocket](https://github.com/actix/examples/tree/master/websocket/)
|
* [Simple WebSocket](https://github.com/actix/examples/tree/master/websockets/websocket/)
|
||||||
* [Tera Templates](https://github.com/actix/examples/tree/master/template_tera/)
|
* [Tera Templates](https://github.com/actix/examples/tree/master/template_engines/tera/)
|
||||||
* [Askama Templates](https://github.com/actix/examples/tree/master/template_askama/)
|
* [Askama Templates](https://github.com/actix/examples/tree/master/template_engines/askama/)
|
||||||
* [HTTPS using Rustls](https://github.com/actix/examples/tree/master/rustls/)
|
* [HTTPS using Rustls](https://github.com/actix/examples/tree/master/security/rustls/)
|
||||||
* [HTTPS using OpenSSL](https://github.com/actix/examples/tree/master/openssl/)
|
* [HTTPS using OpenSSL](https://github.com/actix/examples/tree/master/security/openssl/)
|
||||||
* [WebSocket Chat](https://github.com/actix/examples/tree/master/websocket-chat/)
|
* [WebSocket Chat](https://github.com/actix/examples/tree/master/websockets/chat/)
|
||||||
|
|
||||||
You may consider checking out
|
You may consider checking out
|
||||||
[this directory](https://github.com/actix/examples/tree/master/) for more examples.
|
[this directory](https://github.com/actix/examples/tree/master/) for more examples.
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 0.6.0-beta.3 - 2021-03-09
|
||||||
|
* No notable changes.
|
||||||
|
|
||||||
|
|
||||||
## 0.6.0-beta.2 - 2021-02-10
|
## 0.6.0-beta.2 - 2021-02-10
|
||||||
* Fix If-Modified-Since and If-Unmodified-Since to not compare using sub-second timestamps. [#1887]
|
* Fix If-Modified-Since and If-Unmodified-Since to not compare using sub-second timestamps. [#1887]
|
||||||
* Replace `v_htmlescape` with `askama_escape`. [#1953]
|
* Replace `v_htmlescape` with `askama_escape`. [#1953]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.6.0-beta.2"
|
version = "0.6.0-beta.3"
|
||||||
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"
|
readme = "README.md"
|
||||||
|
@ -17,7 +17,7 @@ name = "actix_files"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.3", default-features = false }
|
actix-web = { version = "4.0.0-beta.4", default-features = false }
|
||||||
actix-service = "2.0.0-beta.4"
|
actix-service = "2.0.0-beta.4"
|
||||||
|
|
||||||
askama_escape = "0.10"
|
askama_escape = "0.10"
|
||||||
|
@ -33,5 +33,5 @@ mime_guess = "2.0.1"
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
actix-web = "4.0.0-beta.3"
|
actix-web = "4.0.0-beta.4"
|
||||||
|
|
|
@ -3,17 +3,17 @@
|
||||||
> 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.5.0)
|
[](https://docs.rs/actix-files/0.6.0-beta.3)
|
||||||
[](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.5.0)
|
[](https://deps.rs/crate/actix-files/0.6.0-beta.3)
|
||||||
[](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://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
## 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/static_index)
|
- [Example Project](https://github.com/actix/examples/tree/master/basics/static_index)
|
||||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
- [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
|
||||||
|
|
|
@ -19,7 +19,7 @@ use crate::{
|
||||||
///
|
///
|
||||||
/// `Files` service must be registered with `App::service()` method.
|
/// `Files` service must be registered with `App::service()` method.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_web::App;
|
/// use actix_web::App;
|
||||||
/// use actix_files::Files;
|
/// use actix_files::Files;
|
||||||
///
|
///
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
//! Provides a non-blocking service for serving static files from disk.
|
//! Provides a non-blocking service for serving static files from disk.
|
||||||
//!
|
//!
|
||||||
//! # Example
|
//! # Example
|
||||||
//! ```rust
|
//! ```
|
||||||
//! use actix_web::App;
|
//! use actix_web::App;
|
||||||
//! use actix_files::Files;
|
//! use actix_files::Files;
|
||||||
//!
|
//!
|
||||||
|
@ -662,8 +662,12 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_static_files_bad_directory() {
|
async fn test_static_files_bad_directory() {
|
||||||
let _st: Files = Files::new("/", "missing");
|
let service = Files::new("/", "./missing").new_service(()).await.unwrap();
|
||||||
let _st: Files = Files::new("/", "Cargo.toml");
|
|
||||||
|
let req = TestRequest::with_uri("/").to_srv_request();
|
||||||
|
let resp = test::call_service(&service, req).await;
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
@ -676,75 +680,34 @@ mod tests {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let req = TestRequest::with_uri("/missing").to_srv_request();
|
let req = TestRequest::with_uri("/missing").to_srv_request();
|
||||||
|
|
||||||
let resp = test::call_service(&st, req).await;
|
let resp = test::call_service(&st, req).await;
|
||||||
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let bytes = test::read_body(resp).await;
|
let bytes = test::read_body(resp).await;
|
||||||
assert_eq!(bytes, web::Bytes::from_static(b"default content"));
|
assert_eq!(bytes, web::Bytes::from_static(b"default content"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[actix_rt::test]
|
#[actix_rt::test]
|
||||||
// async fn test_serve_index() {
|
async fn test_serve_index_nested() {
|
||||||
// let st = Files::new(".").index_file("test.binary");
|
let service = Files::new(".", ".")
|
||||||
// let req = TestRequest::default().uri("/tests").finish();
|
.index_file("lib.rs")
|
||||||
|
.new_service(())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
let req = TestRequest::default().uri("/src").to_srv_request();
|
||||||
// let resp = resp.as_msg();
|
let resp = test::call_service(&service, req).await;
|
||||||
// assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
// assert_eq!(
|
|
||||||
// resp.headers()
|
|
||||||
// .get(header::CONTENT_TYPE)
|
|
||||||
// .expect("content type"),
|
|
||||||
// "application/octet-stream"
|
|
||||||
// );
|
|
||||||
// assert_eq!(
|
|
||||||
// resp.headers()
|
|
||||||
// .get(header::CONTENT_DISPOSITION)
|
|
||||||
// .expect("content disposition"),
|
|
||||||
// "attachment; filename=\"test.binary\""
|
|
||||||
// );
|
|
||||||
|
|
||||||
// let req = TestRequest::default().uri("/tests/").finish();
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
assert_eq!(
|
||||||
// let resp = resp.as_msg();
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
// assert_eq!(resp.status(), StatusCode::OK);
|
"text/x-rust"
|
||||||
// assert_eq!(
|
);
|
||||||
// resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
assert_eq!(
|
||||||
// "application/octet-stream"
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
||||||
// );
|
"inline; filename=\"lib.rs\""
|
||||||
// assert_eq!(
|
);
|
||||||
// resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
}
|
||||||
// "attachment; filename=\"test.binary\""
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // nonexistent index file
|
|
||||||
// let req = TestRequest::default().uri("/tests/unknown").finish();
|
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
||||||
// let resp = resp.as_msg();
|
|
||||||
// assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
||||||
|
|
||||||
// let req = TestRequest::default().uri("/tests/unknown/").finish();
|
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
||||||
// let resp = resp.as_msg();
|
|
||||||
// assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// #[actix_rt::test]
|
|
||||||
// async fn test_serve_index_nested() {
|
|
||||||
// let st = Files::new(".").index_file("mod.rs");
|
|
||||||
// let req = TestRequest::default().uri("/src/client").finish();
|
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
||||||
// let resp = resp.as_msg();
|
|
||||||
// assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
// assert_eq!(
|
|
||||||
// resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
||||||
// "text/x-rust"
|
|
||||||
// );
|
|
||||||
// assert_eq!(
|
|
||||||
// resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
||||||
// "inline; filename=\"mod.rs\""
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn integration_serve_index() {
|
async fn integration_serve_index() {
|
||||||
|
|
|
@ -60,7 +60,7 @@ impl NamedFile {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_files::NamedFile;
|
/// use actix_files::NamedFile;
|
||||||
/// use std::io::{self, Write};
|
/// use std::io::{self, Write};
|
||||||
/// use std::env;
|
/// use std::env;
|
||||||
|
@ -137,7 +137,7 @@ impl NamedFile {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_files::NamedFile;
|
/// use actix_files::NamedFile;
|
||||||
///
|
///
|
||||||
/// let file = NamedFile::open("foo.txt");
|
/// let file = NamedFile::open("foo.txt");
|
||||||
|
@ -156,7 +156,7 @@ impl NamedFile {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
/// use actix_files::NamedFile;
|
/// use actix_files::NamedFile;
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{fmt, io, path::PathBuf, rc::Rc, task::Poll};
|
use std::{fmt, io, path::PathBuf, rc::Rc};
|
||||||
|
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-beta.3 - 2021-03-09
|
||||||
|
* No notable changes.
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.2 - 2021-02-10
|
## 3.0.0-beta.2 - 2021-02-10
|
||||||
* No notable changes.
|
* No notable changes.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-http-test"
|
name = "actix-http-test"
|
||||||
version = "3.0.0-beta.2"
|
version = "3.0.0-beta.3"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Various helpers for Actix applications to use during testing"
|
description = "Various helpers for Actix applications to use during testing"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -31,18 +31,18 @@ openssl = ["tls-openssl", "awc/openssl"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "2.0.0-beta.4"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-tls = "3.0.0-beta.3"
|
actix-tls = "3.0.0-beta.4"
|
||||||
actix-utils = "3.0.0-beta.2"
|
actix-utils = "3.0.0-beta.2"
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
awc = "3.0.0-beta.2"
|
awc = { version = "3.0.0-beta.3", default-features = false }
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
futures-core = { version = "0.3.7", default-features = false }
|
futures-core = { version = "0.3.7", default-features = false }
|
||||||
http = "0.2.2"
|
http = "0.2.2"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
socket2 = "0.3"
|
socket2 = "0.4"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
slab = "0.4"
|
slab = "0.4"
|
||||||
|
@ -50,12 +50,6 @@ serde_urlencoded = "0.7"
|
||||||
time = { version = "0.2.23", default-features = false, features = ["std"] }
|
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 }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies.tls-openssl]
|
|
||||||
version = "0.10.9"
|
|
||||||
package = "openssl"
|
|
||||||
features = ["vendored"]
|
|
||||||
optional = true
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = "4.0.0-beta.3"
|
actix-web = { version = "4.0.0-beta.4", default-features = false, features = ["cookies"] }
|
||||||
actix-http = "3.0.0-beta.3"
|
actix-http = "3.0.0-beta.4"
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
> Various helpers for Actix applications to use during testing.
|
> Various helpers for Actix applications to use during testing.
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-http-test)
|
[](https://crates.io/crates/actix-http-test)
|
||||||
[](https://docs.rs/actix-http-test/2.1.0)
|
[](https://docs.rs/actix-http-test/3.0.0-beta.3)
|
||||||

|

|
||||||
[](https://deps.rs/crate/actix-http-test/2.1.0)
|
[](https://deps.rs/crate/actix-http-test/3.0.0-beta.3)
|
||||||
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
## Documentation & Resources
|
## Documentation & Resources
|
||||||
|
|
|
@ -26,7 +26,7 @@ use socket2::{Domain, Protocol, Socket, Type};
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_http::HttpService;
|
/// use actix_http::HttpService;
|
||||||
/// use actix_http_test::TestServer;
|
/// use actix_http_test::TestServer;
|
||||||
/// use actix_web::{web, App, HttpResponse, Error};
|
/// use actix_web::{web, App, HttpResponse, Error};
|
||||||
|
@ -118,10 +118,10 @@ pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
|
||||||
/// Get first available unused address
|
/// Get first available unused address
|
||||||
pub fn unused_addr() -> net::SocketAddr {
|
pub fn unused_addr() -> net::SocketAddr {
|
||||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
let socket = Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp())).unwrap();
|
let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP)).unwrap();
|
||||||
socket.bind(&addr.into()).unwrap();
|
socket.bind(&addr.into()).unwrap();
|
||||||
socket.set_reuse_address(true).unwrap();
|
socket.set_reuse_address(true).unwrap();
|
||||||
let tcp = socket.into_tcp_listener();
|
let tcp = net::TcpListener::from(socket);
|
||||||
tcp.local_addr().unwrap()
|
tcp.local_addr().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,32 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* `client::Connector::handshake_timeout` method for customize tls connection handshake timeout. [#2081]
|
||||||
|
* `client::ConnectorService` as `client::Connector::finish` method's return type [#2081]
|
||||||
|
* `client::ConnectionIo` trait alias [#2081]
|
||||||
|
|
||||||
|
### Chaged
|
||||||
|
* `client::Connector` type now only have one generic type for `actix_service::Service`. [#2063]
|
||||||
|
|
||||||
|
[#2063]: https://github.com/actix/actix-web/pull/2063
|
||||||
|
[#2081]: https://github.com/actix/actix-web/pull/2081
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-beta.4 - 2021-03-08
|
||||||
### Changed
|
### Changed
|
||||||
* Feature `cookies` is now optional and disabled by default. [#1981]
|
* Feature `cookies` is now optional and disabled by default. [#1981]
|
||||||
|
* `ws::hash_key` now returns array. [#2035]
|
||||||
|
* `ResponseBuilder::json` now takes `impl Serialize`. [#2052]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* re-export of `futures_channel::oneshot::Canceled` is removed from `error` mod. [#1994]
|
* Re-export of `futures_channel::oneshot::Canceled` is removed from `error` mod. [#1994]
|
||||||
* `ResponseError` impl for `futures_channel::oneshot::Canceled` is removed. [#1994]
|
* `ResponseError` impl for `futures_channel::oneshot::Canceled` is removed. [#1994]
|
||||||
|
|
||||||
[#1981]: https://github.com/actix/actix-web/pull/1981
|
[#1981]: https://github.com/actix/actix-web/pull/1981
|
||||||
[#1994]: https://github.com/actix/actix-web/pull/1994
|
[#1994]: https://github.com/actix/actix-web/pull/1994
|
||||||
|
[#2035]: https://github.com/actix/actix-web/pull/2035
|
||||||
|
[#2052]: https://github.com/actix/actix-web/pull/2052
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.3 - 2021-02-10
|
## 3.0.0-beta.3 - 2021-02-10
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-http"
|
name = "actix-http"
|
||||||
version = "3.0.0-beta.3"
|
version = "3.0.0-beta.4"
|
||||||
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"
|
readme = "README.md"
|
||||||
|
@ -47,8 +47,8 @@ trust-dns = ["trust-dns-resolver"]
|
||||||
actix-service = "2.0.0-beta.4"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-utils = "3.0.0-beta.2"
|
actix-utils = "3.0.0-beta.2"
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
actix-tls = "3.0.0-beta.2"
|
actix-tls = "3.0.0-beta.4"
|
||||||
|
|
||||||
ahash = "0.7"
|
ahash = "0.7"
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
|
@ -61,12 +61,12 @@ derive_more = "0.99.5"
|
||||||
encoding_rs = "0.8"
|
encoding_rs = "0.8"
|
||||||
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
futures-util = { version = "0.3.7", default-features = false, features = ["alloc", "sink"] }
|
futures-util = { version = "0.3.7", default-features = false, features = ["alloc", "sink"] }
|
||||||
h2 = "0.3.0"
|
h2 = "0.3.1"
|
||||||
http = "0.2.2"
|
http = "0.2.2"
|
||||||
httparse = "1.3"
|
httparse = "1.3"
|
||||||
itoa = "0.4"
|
itoa = "0.4"
|
||||||
language-tags = "0.2"
|
language-tags = "0.2"
|
||||||
lazy_static = "1.4"
|
once_cell = "1.5"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
|
@ -89,8 +89,8 @@ trust-dns-resolver = { version = "0.20.0", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-http-test = { version = "3.0.0-beta.2", features = ["openssl"] }
|
actix-http-test = { version = "3.0.0-beta.3", features = ["openssl"] }
|
||||||
actix-tls = { version = "3.0.0-beta.2", features = ["openssl"] }
|
actix-tls = { version = "3.0.0-beta.4", features = ["openssl"] }
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
rcgen = "0.8"
|
rcgen = "0.8"
|
||||||
|
@ -98,10 +98,9 @@ serde_derive = "1.0"
|
||||||
tls-openssl = { version = "0.10", package = "openssl" }
|
tls-openssl = { version = "0.10", package = "openssl" }
|
||||||
tls-rustls = { version = "0.19", package = "rustls" }
|
tls-rustls = { version = "0.19", package = "rustls" }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dev-dependencies.tls-openssl]
|
[[example]]
|
||||||
version = "0.10.9"
|
name = "ws"
|
||||||
package = "openssl"
|
required-features = ["rustls"]
|
||||||
features = ["vendored"]
|
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "write-camel-case"
|
name = "write-camel-case"
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
> 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.3)
|
[](https://docs.rs/actix-http/3.0.0-beta.4)
|
||||||
[](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.3)
|
[](https://deps.rs/crate/actix-http/3.0.0-beta.4)
|
||||||
[](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://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::{env, io};
|
||||||
use actix_http::{Error, HttpService, Request, Response};
|
use actix_http::{Error, HttpService, Request, Response};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt as _;
|
||||||
use http::header::HeaderValue;
|
use http::header::HeaderValue;
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use actix_http::http::HeaderValue;
|
||||||
use actix_http::{Error, HttpService, Request, Response};
|
use actix_http::{Error, HttpService, Request, Response};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt as _;
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
async fn handle_request(mut req: Request) -> Result<Response, Error> {
|
async fn handle_request(mut req: Request) -> Result<Response, Error> {
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
//! Sets up a WebSocket server over TCP and TLS.
|
||||||
|
//! Sends a heartbeat message every 4 seconds but does not respond to any incoming frames.
|
||||||
|
|
||||||
|
extern crate tls_rustls as rustls;
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
env, io,
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
use actix_codec::Encoder;
|
||||||
|
use actix_http::{error::Error, ws, HttpService, Request, Response};
|
||||||
|
use actix_rt::time::{interval, Interval};
|
||||||
|
use actix_server::Server;
|
||||||
|
use bytes::{Bytes, BytesMut};
|
||||||
|
use bytestring::ByteString;
|
||||||
|
use futures_core::{ready, Stream};
|
||||||
|
|
||||||
|
#[actix_rt::main]
|
||||||
|
async fn main() -> io::Result<()> {
|
||||||
|
env::set_var("RUST_LOG", "actix=info,h2_ws=info");
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
Server::build()
|
||||||
|
.bind("tcp", ("127.0.0.1", 8080), || {
|
||||||
|
HttpService::build().h1(handler).tcp()
|
||||||
|
})?
|
||||||
|
.bind("tls", ("127.0.0.1", 8443), || {
|
||||||
|
HttpService::build().finish(handler).rustls(tls_config())
|
||||||
|
})?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handler(req: Request) -> Result<Response, Error> {
|
||||||
|
log::info!("handshaking");
|
||||||
|
let mut res = ws::handshake(req.head())?;
|
||||||
|
|
||||||
|
// handshake will always fail under HTTP/2
|
||||||
|
|
||||||
|
log::info!("responding");
|
||||||
|
Ok(res.streaming(Heartbeat::new(ws::Codec::new())))
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Heartbeat {
|
||||||
|
codec: ws::Codec,
|
||||||
|
interval: Interval,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Heartbeat {
|
||||||
|
fn new(codec: ws::Codec) -> Self {
|
||||||
|
Self {
|
||||||
|
codec,
|
||||||
|
interval: interval(Duration::from_secs(4)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stream for Heartbeat {
|
||||||
|
type Item = Result<Bytes, Error>;
|
||||||
|
|
||||||
|
fn poll_next(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Option<Self::Item>> {
|
||||||
|
log::trace!("poll");
|
||||||
|
|
||||||
|
ready!(self.as_mut().interval.poll_tick(cx));
|
||||||
|
|
||||||
|
let mut buffer = BytesMut::new();
|
||||||
|
|
||||||
|
self.as_mut()
|
||||||
|
.codec
|
||||||
|
.encode(
|
||||||
|
ws::Message::Text(ByteString::from_static("hello world")),
|
||||||
|
&mut buffer,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Poll::Ready(Some(Ok(buffer.freeze())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tls_config() -> rustls::ServerConfig {
|
||||||
|
use std::io::BufReader;
|
||||||
|
|
||||||
|
use rustls::{
|
||||||
|
internal::pemfile::{certs, pkcs8_private_keys},
|
||||||
|
NoClientAuth, ServerConfig,
|
||||||
|
};
|
||||||
|
|
||||||
|
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
|
||||||
|
let cert_file = cert.serialize_pem().unwrap();
|
||||||
|
let key_file = cert.serialize_private_key_pem();
|
||||||
|
|
||||||
|
let mut config = ServerConfig::new(NoClientAuth::new());
|
||||||
|
let cert_file = &mut BufReader::new(cert_file.as_bytes());
|
||||||
|
let key_file = &mut BufReader::new(key_file.as_bytes());
|
||||||
|
|
||||||
|
let cert_chain = certs(cert_file).unwrap();
|
||||||
|
let mut keys = pkcs8_private_keys(key_file).unwrap();
|
||||||
|
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
|
||||||
|
|
||||||
|
config
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::net::IpAddr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
const DEFAULT_H2_CONN_WINDOW: u32 = 1024 * 1024 * 2; // 2MB
|
const DEFAULT_H2_CONN_WINDOW: u32 = 1024 * 1024 * 2; // 2MB
|
||||||
|
@ -7,24 +8,28 @@ const DEFAULT_H2_STREAM_WINDOW: u32 = 1024 * 1024; // 1MB
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct ConnectorConfig {
|
pub(crate) struct ConnectorConfig {
|
||||||
pub(crate) timeout: Duration,
|
pub(crate) timeout: Duration,
|
||||||
|
pub(crate) handshake_timeout: Duration,
|
||||||
pub(crate) conn_lifetime: Duration,
|
pub(crate) conn_lifetime: Duration,
|
||||||
pub(crate) conn_keep_alive: Duration,
|
pub(crate) conn_keep_alive: Duration,
|
||||||
pub(crate) disconnect_timeout: Option<Duration>,
|
pub(crate) disconnect_timeout: Option<Duration>,
|
||||||
pub(crate) limit: usize,
|
pub(crate) limit: usize,
|
||||||
pub(crate) conn_window_size: u32,
|
pub(crate) conn_window_size: u32,
|
||||||
pub(crate) stream_window_size: u32,
|
pub(crate) stream_window_size: u32,
|
||||||
|
pub(crate) local_address: Option<IpAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ConnectorConfig {
|
impl Default for ConnectorConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
timeout: Duration::from_secs(5),
|
timeout: Duration::from_secs(5),
|
||||||
|
handshake_timeout: Duration::from_secs(5),
|
||||||
conn_lifetime: Duration::from_secs(75),
|
conn_lifetime: Duration::from_secs(75),
|
||||||
conn_keep_alive: Duration::from_secs(15),
|
conn_keep_alive: Duration::from_secs(15),
|
||||||
disconnect_timeout: Some(Duration::from_millis(3000)),
|
disconnect_timeout: Some(Duration::from_millis(3000)),
|
||||||
limit: 100,
|
limit: 100,
|
||||||
conn_window_size: DEFAULT_H2_CONN_WINDOW,
|
conn_window_size: DEFAULT_H2_CONN_WINDOW,
|
||||||
stream_window_size: DEFAULT_H2_STREAM_WINDOW,
|
stream_window_size: DEFAULT_H2_STREAM_WINDOW,
|
||||||
|
local_address: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::{
|
||||||
use std::pin::Pin;
|
io,
|
||||||
use std::task::{Context, Poll};
|
ops::{Deref, DerefMut},
|
||||||
use std::{fmt, io, time};
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
time,
|
||||||
|
};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed, ReadBuf};
|
use actix_codec::{AsyncRead, AsyncWrite, Framed, ReadBuf};
|
||||||
use actix_rt::task::JoinHandle;
|
use actix_rt::task::JoinHandle;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use h2::client::SendRequest;
|
use h2::client::SendRequest;
|
||||||
use pin_project::pin_project;
|
|
||||||
|
|
||||||
use crate::body::MessageBody;
|
use crate::body::MessageBody;
|
||||||
use crate::h1::ClientCodec;
|
use crate::h1::ClientCodec;
|
||||||
|
@ -19,28 +21,148 @@ use super::error::SendRequestError;
|
||||||
use super::pool::Acquired;
|
use super::pool::Acquired;
|
||||||
use super::{h1proto, h2proto};
|
use super::{h1proto, h2proto};
|
||||||
|
|
||||||
pub(crate) enum ConnectionType<Io> {
|
/// Trait alias for types impl [tokio::io::AsyncRead] and [tokio::io::AsyncWrite].
|
||||||
H1(Io),
|
pub trait ConnectionIo: AsyncRead + AsyncWrite + Unpin + 'static {}
|
||||||
H2(H2Connection),
|
|
||||||
|
impl<T: AsyncRead + AsyncWrite + Unpin + 'static> ConnectionIo for T {}
|
||||||
|
|
||||||
|
/// HTTP client connection
|
||||||
|
pub struct H1Connection<Io: ConnectionIo> {
|
||||||
|
io: Option<Io>,
|
||||||
|
created: time::Instant,
|
||||||
|
acquired: Acquired<Io>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `H2Connection` has two parts: `SendRequest` and `Connection`.
|
impl<Io: ConnectionIo> H1Connection<Io> {
|
||||||
|
/// close or release the connection to pool based on flag input
|
||||||
|
pub(super) fn on_release(&mut self, keep_alive: bool) {
|
||||||
|
if keep_alive {
|
||||||
|
self.release();
|
||||||
|
} else {
|
||||||
|
self.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close connection
|
||||||
|
fn close(&mut self) {
|
||||||
|
let io = self.io.take().unwrap();
|
||||||
|
self.acquired.close(ConnectionInnerType::H1(io));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Release this connection to the connection pool
|
||||||
|
fn release(&mut self) {
|
||||||
|
let io = self.io.take().unwrap();
|
||||||
|
self.acquired
|
||||||
|
.release(ConnectionInnerType::H1(io), self.created);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn io_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Io> {
|
||||||
|
Pin::new(self.get_mut().io.as_mut().unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> AsyncRead for H1Connection<Io> {
|
||||||
|
fn poll_read(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &mut ReadBuf<'_>,
|
||||||
|
) -> Poll<io::Result<()>> {
|
||||||
|
self.io_pin_mut().poll_read(cx, buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> AsyncWrite for H1Connection<Io> {
|
||||||
|
fn poll_write(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &[u8],
|
||||||
|
) -> Poll<io::Result<usize>> {
|
||||||
|
self.io_pin_mut().poll_write(cx, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
|
self.io_pin_mut().poll_flush(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_shutdown(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Result<(), io::Error>> {
|
||||||
|
self.io_pin_mut().poll_shutdown(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_write_vectored(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
bufs: &[io::IoSlice<'_>],
|
||||||
|
) -> Poll<io::Result<usize>> {
|
||||||
|
self.io_pin_mut().poll_write_vectored(cx, bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_write_vectored(&self) -> bool {
|
||||||
|
self.io.as_ref().unwrap().is_write_vectored()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// HTTP2 client connection
|
||||||
|
pub struct H2Connection<Io: ConnectionIo> {
|
||||||
|
io: Option<H2ConnectionInner>,
|
||||||
|
created: time::Instant,
|
||||||
|
acquired: Acquired<Io>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> Deref for H2Connection<Io> {
|
||||||
|
type Target = SendRequest<Bytes>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.io.as_ref().unwrap().sender
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> DerefMut for H2Connection<Io> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.io.as_mut().unwrap().sender
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> H2Connection<Io> {
|
||||||
|
/// close or release the connection to pool based on flag input
|
||||||
|
pub(super) fn on_release(&mut self, close: bool) {
|
||||||
|
if close {
|
||||||
|
self.close();
|
||||||
|
} else {
|
||||||
|
self.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close connection
|
||||||
|
fn close(&mut self) {
|
||||||
|
let io = self.io.take().unwrap();
|
||||||
|
self.acquired.close(ConnectionInnerType::H2(io));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Release this connection to the connection pool
|
||||||
|
fn release(&mut self) {
|
||||||
|
let io = self.io.take().unwrap();
|
||||||
|
self.acquired
|
||||||
|
.release(ConnectionInnerType::H2(io), self.created);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `H2ConnectionInner` has two parts: `SendRequest` and `Connection`.
|
||||||
///
|
///
|
||||||
/// `Connection` is spawned as an async task on runtime and `H2Connection` holds a handle for
|
/// `Connection` is spawned as an async task on runtime and `H2ConnectionInner` holds a handle
|
||||||
/// this task. Therefore, it can wake up and quit the task when SendRequest is dropped.
|
/// for this task. Therefore, it can wake up and quit the task when SendRequest is dropped.
|
||||||
pub(crate) struct H2Connection {
|
pub(super) struct H2ConnectionInner {
|
||||||
handle: JoinHandle<()>,
|
handle: JoinHandle<()>,
|
||||||
sender: SendRequest<Bytes>,
|
sender: SendRequest<Bytes>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl H2Connection {
|
impl H2ConnectionInner {
|
||||||
pub(crate) fn new<Io>(
|
pub(super) fn new<Io: ConnectionIo>(
|
||||||
sender: SendRequest<Bytes>,
|
sender: SendRequest<Bytes>,
|
||||||
connection: h2::client::Connection<Io>,
|
connection: h2::client::Connection<Io>,
|
||||||
) -> Self
|
) -> Self {
|
||||||
where
|
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
let handle = actix_rt::spawn(async move {
|
let handle = actix_rt::spawn(async move {
|
||||||
let _ = connection.await;
|
let _ = connection.await;
|
||||||
});
|
});
|
||||||
|
@ -49,248 +171,188 @@ impl H2Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cancel spawned connection task on drop.
|
/// Cancel spawned connection task on drop.
|
||||||
impl Drop for H2Connection {
|
impl Drop for H2ConnectionInner {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.handle.abort();
|
self.handle.abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// only expose sender type to public.
|
|
||||||
impl Deref for H2Connection {
|
|
||||||
type Target = SendRequest<Bytes>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.sender
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for H2Connection {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.sender
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Connection {
|
|
||||||
type Io: AsyncRead + AsyncWrite + Unpin;
|
|
||||||
|
|
||||||
/// Send request and body
|
|
||||||
fn send_request<B: MessageBody + 'static, H: Into<RequestHeadType>>(
|
|
||||||
self,
|
|
||||||
head: H,
|
|
||||||
body: B,
|
|
||||||
) -> LocalBoxFuture<'static, Result<(ResponseHead, Payload), SendRequestError>>;
|
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
|
||||||
fn open_tunnel<H: Into<RequestHeadType> + 'static>(
|
|
||||||
self,
|
|
||||||
head: H,
|
|
||||||
) -> LocalBoxFuture<
|
|
||||||
'static,
|
|
||||||
Result<(ResponseHead, Framed<Self::Io, ClientCodec>), SendRequestError>,
|
|
||||||
>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) trait ConnectionLifetime: AsyncRead + AsyncWrite + 'static {
|
|
||||||
/// Close connection
|
|
||||||
fn close(self: Pin<&mut Self>);
|
|
||||||
|
|
||||||
/// Release connection to the connection pool
|
|
||||||
fn release(self: Pin<&mut Self>);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// HTTP client connection
|
|
||||||
pub struct IoConnection<T>
|
|
||||||
where
|
|
||||||
T: AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
io: Option<ConnectionType<T>>,
|
|
||||||
created: time::Instant,
|
|
||||||
pool: Option<Acquired<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Debug for IoConnection<T>
|
|
||||||
where
|
|
||||||
T: AsyncWrite + Unpin + fmt::Debug + 'static,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self.io {
|
|
||||||
Some(ConnectionType::H1(ref io)) => write!(f, "H1Connection({:?})", io),
|
|
||||||
Some(ConnectionType::H2(_)) => write!(f, "H2Connection"),
|
|
||||||
None => write!(f, "Connection(Empty)"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite + Unpin> IoConnection<T> {
|
|
||||||
pub(crate) fn new(
|
|
||||||
io: ConnectionType<T>,
|
|
||||||
created: time::Instant,
|
|
||||||
pool: Option<Acquired<T>>,
|
|
||||||
) -> Self {
|
|
||||||
IoConnection {
|
|
||||||
pool,
|
|
||||||
created,
|
|
||||||
io: Some(io),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn into_inner(self) -> (ConnectionType<T>, time::Instant) {
|
|
||||||
(self.io.unwrap(), self.created)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(crate) fn into_parts(self) -> (ConnectionType<T>, time::Instant, Acquired<T>) {
|
|
||||||
(self.io.unwrap(), self.created, self.pool.unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Connection for IoConnection<T>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
type Io = T;
|
|
||||||
|
|
||||||
fn send_request<B: MessageBody + 'static, H: Into<RequestHeadType>>(
|
|
||||||
mut self,
|
|
||||||
head: H,
|
|
||||||
body: B,
|
|
||||||
) -> LocalBoxFuture<'static, Result<(ResponseHead, Payload), SendRequestError>> {
|
|
||||||
match self.io.take().unwrap() {
|
|
||||||
ConnectionType::H1(io) => Box::pin(h1proto::send_request(
|
|
||||||
io,
|
|
||||||
head.into(),
|
|
||||||
body,
|
|
||||||
self.created,
|
|
||||||
self.pool,
|
|
||||||
)),
|
|
||||||
ConnectionType::H2(io) => Box::pin(h2proto::send_request(
|
|
||||||
io,
|
|
||||||
head.into(),
|
|
||||||
body,
|
|
||||||
self.created,
|
|
||||||
self.pool,
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
|
||||||
fn open_tunnel<H: Into<RequestHeadType>>(
|
|
||||||
mut self,
|
|
||||||
head: H,
|
|
||||||
) -> LocalBoxFuture<
|
|
||||||
'static,
|
|
||||||
Result<(ResponseHead, Framed<Self::Io, ClientCodec>), SendRequestError>,
|
|
||||||
> {
|
|
||||||
match self.io.take().unwrap() {
|
|
||||||
ConnectionType::H1(io) => Box::pin(h1proto::open_tunnel(io, head.into())),
|
|
||||||
ConnectionType::H2(io) => {
|
|
||||||
if let Some(mut pool) = self.pool.take() {
|
|
||||||
pool.release(IoConnection::new(
|
|
||||||
ConnectionType::H2(io),
|
|
||||||
self.created,
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
Box::pin(async { Err(SendRequestError::TunnelNotSupported) })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(crate) enum EitherIoConnection<A, B>
|
/// Unified connection type cover Http1 Plain/Tls and Http2 protocols
|
||||||
|
pub enum Connection<A, B = Box<dyn ConnectionIo>>
|
||||||
where
|
where
|
||||||
A: AsyncRead + AsyncWrite + Unpin + 'static,
|
A: ConnectionIo,
|
||||||
B: AsyncRead + AsyncWrite + Unpin + 'static,
|
B: ConnectionIo,
|
||||||
{
|
{
|
||||||
A(IoConnection<A>),
|
Tcp(ConnectionType<A>),
|
||||||
B(IoConnection<B>),
|
Tls(ConnectionType<B>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> Connection for EitherIoConnection<A, B>
|
/// Unified connection type cover Http1/2 protocols
|
||||||
where
|
pub enum ConnectionType<Io: ConnectionIo> {
|
||||||
A: AsyncRead + AsyncWrite + Unpin + 'static,
|
H1(H1Connection<Io>),
|
||||||
B: AsyncRead + AsyncWrite + Unpin + 'static,
|
H2(H2Connection<Io>),
|
||||||
{
|
}
|
||||||
type Io = EitherIo<A, B>;
|
|
||||||
|
|
||||||
fn send_request<RB: MessageBody + 'static, H: Into<RequestHeadType>>(
|
/// Helper type for storing connection types in pool.
|
||||||
|
pub(super) enum ConnectionInnerType<Io> {
|
||||||
|
H1(Io),
|
||||||
|
H2(H2ConnectionInner),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> ConnectionType<Io> {
|
||||||
|
pub(super) fn from_pool(
|
||||||
|
inner: ConnectionInnerType<Io>,
|
||||||
|
created: time::Instant,
|
||||||
|
acquired: Acquired<Io>,
|
||||||
|
) -> Self {
|
||||||
|
match inner {
|
||||||
|
ConnectionInnerType::H1(io) => Self::from_h1(io, created, acquired),
|
||||||
|
ConnectionInnerType::H2(io) => Self::from_h2(io, created, acquired),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn from_h1(
|
||||||
|
io: Io,
|
||||||
|
created: time::Instant,
|
||||||
|
acquired: Acquired<Io>,
|
||||||
|
) -> Self {
|
||||||
|
Self::H1(H1Connection {
|
||||||
|
io: Some(io),
|
||||||
|
created,
|
||||||
|
acquired,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn from_h2(
|
||||||
|
io: H2ConnectionInner,
|
||||||
|
created: time::Instant,
|
||||||
|
acquired: Acquired<Io>,
|
||||||
|
) -> Self {
|
||||||
|
Self::H2(H2Connection {
|
||||||
|
io: Some(io),
|
||||||
|
created,
|
||||||
|
acquired,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B> Connection<A, B>
|
||||||
|
where
|
||||||
|
A: ConnectionIo,
|
||||||
|
B: ConnectionIo,
|
||||||
|
{
|
||||||
|
/// Send a request through connection.
|
||||||
|
pub fn send_request<RB, H>(
|
||||||
self,
|
self,
|
||||||
head: H,
|
head: H,
|
||||||
body: RB,
|
body: RB,
|
||||||
) -> LocalBoxFuture<'static, Result<(ResponseHead, Payload), SendRequestError>> {
|
) -> LocalBoxFuture<'static, Result<(ResponseHead, Payload), SendRequestError>>
|
||||||
|
where
|
||||||
|
RB: MessageBody + 'static,
|
||||||
|
H: Into<RequestHeadType> + 'static,
|
||||||
|
{
|
||||||
|
Box::pin(async move {
|
||||||
match self {
|
match self {
|
||||||
EitherIoConnection::A(con) => con.send_request(head, body),
|
Connection::Tcp(ConnectionType::H1(conn)) => {
|
||||||
EitherIoConnection::B(con) => con.send_request(head, body),
|
h1proto::send_request(conn, head.into(), body).await
|
||||||
}
|
}
|
||||||
|
Connection::Tls(ConnectionType::H1(conn)) => {
|
||||||
|
h1proto::send_request(conn, head.into(), body).await
|
||||||
|
}
|
||||||
|
Connection::Tls(ConnectionType::H2(conn)) => {
|
||||||
|
h2proto::send_request(conn, head.into(), body).await
|
||||||
|
}
|
||||||
|
_ => unreachable!(
|
||||||
|
"Plain Tcp connection can be used only in Http1 protocol"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
/// Send request, returns Response and Framed tunnel.
|
||||||
fn open_tunnel<H: Into<RequestHeadType> + 'static>(
|
pub fn open_tunnel<H: Into<RequestHeadType> + 'static>(
|
||||||
self,
|
self,
|
||||||
head: H,
|
head: H,
|
||||||
) -> LocalBoxFuture<
|
) -> LocalBoxFuture<
|
||||||
'static,
|
'static,
|
||||||
Result<(ResponseHead, Framed<Self::Io, ClientCodec>), SendRequestError>,
|
Result<(ResponseHead, Framed<Connection<A, B>, ClientCodec>), SendRequestError>,
|
||||||
> {
|
> {
|
||||||
|
Box::pin(async move {
|
||||||
match self {
|
match self {
|
||||||
EitherIoConnection::A(con) => Box::pin(async {
|
Connection::Tcp(ConnectionType::H1(ref _conn)) => {
|
||||||
let (head, framed) = con.open_tunnel(head).await?;
|
let (head, framed) = h1proto::open_tunnel(self, head.into()).await?;
|
||||||
Ok((head, framed.into_map_io(EitherIo::A)))
|
Ok((head, framed))
|
||||||
}),
|
|
||||||
EitherIoConnection::B(con) => Box::pin(async {
|
|
||||||
let (head, framed) = con.open_tunnel(head).await?;
|
|
||||||
Ok((head, framed.into_map_io(EitherIo::B)))
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
|
Connection::Tls(ConnectionType::H1(ref _conn)) => {
|
||||||
|
let (head, framed) = h1proto::open_tunnel(self, head.into()).await?;
|
||||||
|
Ok((head, framed))
|
||||||
|
}
|
||||||
|
Connection::Tls(ConnectionType::H2(mut conn)) => {
|
||||||
|
conn.release();
|
||||||
|
Err(SendRequestError::TunnelNotSupported)
|
||||||
|
}
|
||||||
|
Connection::Tcp(ConnectionType::H2(_)) => {
|
||||||
|
unreachable!(
|
||||||
|
"Plain Tcp connection can be used only in Http1 protocol"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project(project = EitherIoProj)]
|
impl<A, B> AsyncRead for Connection<A, B>
|
||||||
pub enum EitherIo<A, B> {
|
|
||||||
A(#[pin] A),
|
|
||||||
B(#[pin] B),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B> AsyncRead for EitherIo<A, B>
|
|
||||||
where
|
where
|
||||||
A: AsyncRead,
|
A: ConnectionIo,
|
||||||
B: AsyncRead,
|
B: ConnectionIo,
|
||||||
{
|
{
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
buf: &mut ReadBuf<'_>,
|
buf: &mut ReadBuf<'_>,
|
||||||
) -> Poll<io::Result<()>> {
|
) -> Poll<io::Result<()>> {
|
||||||
match self.project() {
|
match self.get_mut() {
|
||||||
EitherIoProj::A(val) => val.poll_read(cx, buf),
|
Connection::Tcp(ConnectionType::H1(conn)) => {
|
||||||
EitherIoProj::B(val) => val.poll_read(cx, buf),
|
Pin::new(conn).poll_read(cx, buf)
|
||||||
|
}
|
||||||
|
Connection::Tls(ConnectionType::H1(conn)) => {
|
||||||
|
Pin::new(conn).poll_read(cx, buf)
|
||||||
|
}
|
||||||
|
_ => unreachable!("H2Connection can not impl AsyncRead trait"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> AsyncWrite for EitherIo<A, B>
|
const H2_UNREACHABLE_WRITE: &str = "H2Connection can not impl AsyncWrite trait";
|
||||||
|
|
||||||
|
impl<A, B> AsyncWrite for Connection<A, B>
|
||||||
where
|
where
|
||||||
A: AsyncWrite,
|
A: ConnectionIo,
|
||||||
B: AsyncWrite,
|
B: ConnectionIo,
|
||||||
{
|
{
|
||||||
fn poll_write(
|
fn poll_write(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
buf: &[u8],
|
buf: &[u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
match self.project() {
|
match self.get_mut() {
|
||||||
EitherIoProj::A(val) => val.poll_write(cx, buf),
|
Connection::Tcp(ConnectionType::H1(conn)) => {
|
||||||
EitherIoProj::B(val) => val.poll_write(cx, buf),
|
Pin::new(conn).poll_write(cx, buf)
|
||||||
|
}
|
||||||
|
Connection::Tls(ConnectionType::H1(conn)) => {
|
||||||
|
Pin::new(conn).poll_write(cx, buf)
|
||||||
|
}
|
||||||
|
_ => unreachable!(H2_UNREACHABLE_WRITE),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
match self.project() {
|
match self.get_mut() {
|
||||||
EitherIoProj::A(val) => val.poll_flush(cx),
|
Connection::Tcp(ConnectionType::H1(conn)) => Pin::new(conn).poll_flush(cx),
|
||||||
EitherIoProj::B(val) => val.poll_flush(cx),
|
Connection::Tls(ConnectionType::H1(conn)) => Pin::new(conn).poll_flush(cx),
|
||||||
|
_ => unreachable!(H2_UNREACHABLE_WRITE),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,9 +360,38 @@ where
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<io::Result<()>> {
|
) -> Poll<io::Result<()>> {
|
||||||
match self.project() {
|
match self.get_mut() {
|
||||||
EitherIoProj::A(val) => val.poll_shutdown(cx),
|
Connection::Tcp(ConnectionType::H1(conn)) => {
|
||||||
EitherIoProj::B(val) => val.poll_shutdown(cx),
|
Pin::new(conn).poll_shutdown(cx)
|
||||||
|
}
|
||||||
|
Connection::Tls(ConnectionType::H1(conn)) => {
|
||||||
|
Pin::new(conn).poll_shutdown(cx)
|
||||||
|
}
|
||||||
|
_ => unreachable!(H2_UNREACHABLE_WRITE),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_write_vectored(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
bufs: &[io::IoSlice<'_>],
|
||||||
|
) -> Poll<io::Result<usize>> {
|
||||||
|
match self.get_mut() {
|
||||||
|
Connection::Tcp(ConnectionType::H1(conn)) => {
|
||||||
|
Pin::new(conn).poll_write_vectored(cx, bufs)
|
||||||
|
}
|
||||||
|
Connection::Tls(ConnectionType::H1(conn)) => {
|
||||||
|
Pin::new(conn).poll_write_vectored(cx, bufs)
|
||||||
|
}
|
||||||
|
_ => unreachable!(H2_UNREACHABLE_WRITE),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_write_vectored(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
Connection::Tcp(ConnectionType::H1(ref conn)) => conn.is_write_vectored(),
|
||||||
|
Connection::Tls(ConnectionType::H1(ref conn)) => conn.is_write_vectored(),
|
||||||
|
_ => unreachable!(H2_UNREACHABLE_WRITE),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -323,10 +414,13 @@ mod test {
|
||||||
|
|
||||||
let tcp = TcpStream::connect(local).await.unwrap();
|
let tcp = TcpStream::connect(local).await.unwrap();
|
||||||
let (sender, connection) = h2::client::handshake(tcp).await.unwrap();
|
let (sender, connection) = h2::client::handshake(tcp).await.unwrap();
|
||||||
let conn = H2Connection::new(sender.clone(), connection);
|
let conn = H2ConnectionInner::new(sender.clone(), connection);
|
||||||
|
|
||||||
assert!(sender.clone().ready().await.is_ok());
|
assert!(sender.clone().ready().await.is_ok());
|
||||||
assert!(h2::client::SendRequest::clone(&*conn).ready().await.is_ok());
|
assert!(h2::client::SendRequest::clone(&conn.sender)
|
||||||
|
.ready()
|
||||||
|
.await
|
||||||
|
.is_ok());
|
||||||
|
|
||||||
drop(conn);
|
drop(conn);
|
||||||
|
|
||||||
|
|
|
@ -1,45 +1,53 @@
|
||||||
use std::fmt;
|
use std::{
|
||||||
use std::marker::PhantomData;
|
fmt,
|
||||||
use std::time::Duration;
|
future::Future,
|
||||||
|
net::IpAddr,
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
pin::Pin,
|
||||||
use actix_rt::net::TcpStream;
|
rc::Rc,
|
||||||
use actix_service::{apply_fn, Service, ServiceExt};
|
task::{Context, Poll},
|
||||||
use actix_tls::connect::{
|
time::Duration,
|
||||||
new_connector, Connect as TcpConnect, Connection as TcpConnection, Resolver,
|
|
||||||
};
|
};
|
||||||
use actix_utils::timeout::{TimeoutError, TimeoutService};
|
|
||||||
|
use actix_rt::{
|
||||||
|
net::TcpStream,
|
||||||
|
time::{sleep, Sleep},
|
||||||
|
};
|
||||||
|
use actix_service::Service;
|
||||||
|
use actix_tls::connect::{
|
||||||
|
new_connector, Connect as TcpConnect, ConnectError as TcpConnectError,
|
||||||
|
Connection as TcpConnection, Resolver,
|
||||||
|
};
|
||||||
|
use futures_core::{future::LocalBoxFuture, ready};
|
||||||
use http::Uri;
|
use http::Uri;
|
||||||
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use super::config::ConnectorConfig;
|
use super::config::ConnectorConfig;
|
||||||
use super::connection::Connection;
|
use super::connection::{Connection, ConnectionIo};
|
||||||
use super::error::ConnectError;
|
use super::error::ConnectError;
|
||||||
use super::pool::{ConnectionPool, Protocol};
|
use super::pool::ConnectionPool;
|
||||||
use super::Connect;
|
use super::Connect;
|
||||||
|
use super::Protocol;
|
||||||
|
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
use actix_tls::connect::ssl::openssl::SslConnector as OpensslConnector;
|
use actix_tls::connect::ssl::openssl::SslConnector as OpensslConnector;
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
use actix_tls::connect::ssl::rustls::ClientConfig;
|
use actix_tls::connect::ssl::rustls::ClientConfig;
|
||||||
#[cfg(feature = "rustls")]
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[cfg(any(feature = "openssl", feature = "rustls"))]
|
|
||||||
enum SslConnector {
|
enum SslConnector {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
None,
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
Openssl(OpensslConnector),
|
Openssl(OpensslConnector),
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
Rustls(Arc<ClientConfig>),
|
Rustls(std::sync::Arc<ClientConfig>),
|
||||||
}
|
}
|
||||||
#[cfg(not(any(feature = "openssl", feature = "rustls")))]
|
|
||||||
type SslConnector = ();
|
|
||||||
|
|
||||||
/// Manages HTTP client network connectivity.
|
/// Manages HTTP client network connectivity.
|
||||||
///
|
///
|
||||||
/// The `Connector` type uses a builder-like combinator pattern for service
|
/// The `Connector` type uses a builder-like combinator pattern for service
|
||||||
/// construction that finishes by calling the `.finish()` method.
|
/// construction that finishes by calling the `.finish()` method.
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```ignore
|
||||||
/// use std::time::Duration;
|
/// use std::time::Duration;
|
||||||
/// use actix_http::client::Connector;
|
/// use actix_http::client::Connector;
|
||||||
///
|
///
|
||||||
|
@ -47,18 +55,14 @@ type SslConnector = ();
|
||||||
/// .timeout(Duration::from_secs(5))
|
/// .timeout(Duration::from_secs(5))
|
||||||
/// .finish();
|
/// .finish();
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Connector<T, U> {
|
pub struct Connector<T> {
|
||||||
connector: T,
|
connector: T,
|
||||||
config: ConnectorConfig,
|
config: ConnectorConfig,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
ssl: SslConnector,
|
ssl: SslConnector,
|
||||||
_phantom: PhantomData<U>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Io: AsyncRead + AsyncWrite + Unpin {}
|
impl Connector<()> {
|
||||||
impl<T: AsyncRead + AsyncWrite + Unpin> Io for T {}
|
|
||||||
|
|
||||||
impl Connector<(), ()> {
|
|
||||||
#[allow(clippy::new_ret_no_self, clippy::let_unit_value)]
|
#[allow(clippy::new_ret_no_self, clippy::let_unit_value)]
|
||||||
pub fn new() -> Connector<
|
pub fn new() -> Connector<
|
||||||
impl Service<
|
impl Service<
|
||||||
|
@ -66,13 +70,11 @@ impl Connector<(), ()> {
|
||||||
Response = TcpConnection<Uri, TcpStream>,
|
Response = TcpConnection<Uri, TcpStream>,
|
||||||
Error = actix_tls::connect::ConnectError,
|
Error = actix_tls::connect::ConnectError,
|
||||||
> + Clone,
|
> + Clone,
|
||||||
TcpStream,
|
|
||||||
> {
|
> {
|
||||||
Connector {
|
Connector {
|
||||||
ssl: Self::build_ssl(vec![b"h2".to_vec(), b"http/1.1".to_vec()]),
|
ssl: Self::build_ssl(vec![b"h2".to_vec(), b"http/1.1".to_vec()]),
|
||||||
connector: new_connector(resolver::resolver()),
|
connector: new_connector(resolver::resolver()),
|
||||||
config: ConnectorConfig::default(),
|
config: ConnectorConfig::default(),
|
||||||
_phantom: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,51 +105,59 @@ impl Connector<(), ()> {
|
||||||
config.root_store.add_server_trust_anchors(
|
config.root_store.add_server_trust_anchors(
|
||||||
&actix_tls::connect::ssl::rustls::TLS_SERVER_ROOTS,
|
&actix_tls::connect::ssl::rustls::TLS_SERVER_ROOTS,
|
||||||
);
|
);
|
||||||
SslConnector::Rustls(Arc::new(config))
|
SslConnector::Rustls(std::sync::Arc::new(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ssl turned off, provides empty ssl connector
|
// ssl turned off, provides empty ssl connector
|
||||||
#[cfg(not(any(feature = "openssl", feature = "rustls")))]
|
#[cfg(not(any(feature = "openssl", feature = "rustls")))]
|
||||||
fn build_ssl(_: Vec<Vec<u8>>) -> SslConnector {}
|
fn build_ssl(_: Vec<Vec<u8>>) -> SslConnector {
|
||||||
|
SslConnector::None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Connector<T, U> {
|
impl<S> Connector<S> {
|
||||||
/// Use custom connector.
|
/// Use custom connector.
|
||||||
pub fn connector<T1, U1>(self, connector: T1) -> Connector<T1, U1>
|
pub fn connector<S1, Io1>(self, connector: S1) -> Connector<S1>
|
||||||
where
|
where
|
||||||
U1: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
Io1: ConnectionIo + fmt::Debug,
|
||||||
T1: Service<
|
S1: Service<
|
||||||
TcpConnect<Uri>,
|
TcpConnect<Uri>,
|
||||||
Response = TcpConnection<Uri, U1>,
|
Response = TcpConnection<Uri, Io1>,
|
||||||
Error = actix_tls::connect::ConnectError,
|
Error = TcpConnectError,
|
||||||
> + Clone,
|
> + Clone,
|
||||||
{
|
{
|
||||||
Connector {
|
Connector {
|
||||||
connector,
|
connector,
|
||||||
config: self.config,
|
config: self.config,
|
||||||
ssl: self.ssl,
|
ssl: self.ssl,
|
||||||
_phantom: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Connector<T, U>
|
impl<S, Io> Connector<S>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
Io: ConnectionIo + fmt::Debug,
|
||||||
T: Service<
|
S: Service<
|
||||||
TcpConnect<Uri>,
|
TcpConnect<Uri>,
|
||||||
Response = TcpConnection<Uri, U>,
|
Response = TcpConnection<Uri, Io>,
|
||||||
Error = actix_tls::connect::ConnectError,
|
Error = TcpConnectError,
|
||||||
> + Clone
|
> + Clone
|
||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
/// Connection timeout, i.e. max time to connect to remote host including dns name resolution.
|
/// Tcp connection timeout, i.e. max time to connect to remote host including dns name
|
||||||
/// Set to 1 second by default.
|
/// resolution. Set to 5 second by default.
|
||||||
pub fn timeout(mut self, timeout: Duration) -> Self {
|
pub fn timeout(mut self, timeout: Duration) -> Self {
|
||||||
self.config.timeout = timeout;
|
self.config.timeout = timeout;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tls handshake timeout, i.e. max time to do tls handshake with remote host after tcp
|
||||||
|
/// connection established. Set to 5 second by default.
|
||||||
|
pub fn handshake_timeout(mut self, timeout: Duration) -> Self {
|
||||||
|
self.config.handshake_timeout = timeout;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
/// Use custom `SslConnector` instance.
|
/// Use custom `SslConnector` instance.
|
||||||
pub fn ssl(mut self, connector: OpensslConnector) -> Self {
|
pub fn ssl(mut self, connector: OpensslConnector) -> Self {
|
||||||
|
@ -156,7 +166,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
pub fn rustls(mut self, connector: Arc<ClientConfig>) -> Self {
|
/// Use custom `SslConnector` instance.
|
||||||
|
pub fn rustls(mut self, connector: std::sync::Arc<ClientConfig>) -> Self {
|
||||||
self.ssl = SslConnector::Rustls(connector);
|
self.ssl = SslConnector::Rustls(connector);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -237,77 +248,72 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set local IP Address the connector would use for establishing connection.
|
||||||
|
pub fn local_address(mut self, addr: IpAddr) -> Self {
|
||||||
|
self.config.local_address = Some(addr);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Finish configuration process and create connector service.
|
/// Finish configuration process and create connector service.
|
||||||
/// The Connector builder always concludes by calling `finish()` last in
|
/// The Connector builder always concludes by calling `finish()` last in
|
||||||
/// its combinator chain.
|
/// its combinator chain.
|
||||||
pub fn finish(
|
pub fn finish(self) -> ConnectorService<S, Io> {
|
||||||
self,
|
let local_address = self.config.local_address;
|
||||||
) -> impl Service<Connect, Response = impl Connection, Error = ConnectError> + Clone
|
let timeout = self.config.timeout;
|
||||||
{
|
|
||||||
#[cfg(not(any(feature = "openssl", feature = "rustls")))]
|
|
||||||
{
|
|
||||||
let connector = TimeoutService::new(
|
|
||||||
self.config.timeout,
|
|
||||||
apply_fn(self.connector, |msg: Connect, srv| {
|
|
||||||
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
|
||||||
})
|
|
||||||
.map_err(ConnectError::from)
|
|
||||||
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
|
||||||
)
|
|
||||||
.map_err(|e| match e {
|
|
||||||
TimeoutError::Service(e) => e,
|
|
||||||
TimeoutError::Timeout => ConnectError::Timeout,
|
|
||||||
});
|
|
||||||
|
|
||||||
connect_impl::InnerConnector {
|
let tcp_service_inner =
|
||||||
tcp_pool: ConnectionPool::new(
|
TcpConnectorInnerService::new(self.connector, timeout, local_address);
|
||||||
connector,
|
|
||||||
self.config.no_disconnect_timeout(),
|
#[allow(clippy::redundant_clone)]
|
||||||
),
|
let tcp_service = TcpConnectorService {
|
||||||
}
|
service: tcp_service_inner.clone(),
|
||||||
}
|
};
|
||||||
#[cfg(any(feature = "openssl", feature = "rustls"))]
|
|
||||||
{
|
let tls_service = match self.ssl {
|
||||||
|
SslConnector::None => None,
|
||||||
|
#[cfg(feature = "openssl")]
|
||||||
|
SslConnector::Openssl(tls) => {
|
||||||
const H2: &[u8] = b"h2";
|
const H2: &[u8] = b"h2";
|
||||||
use actix_service::{boxed::service, pipeline};
|
|
||||||
#[cfg(feature = "openssl")]
|
|
||||||
use actix_tls::connect::ssl::openssl::OpensslConnector;
|
|
||||||
#[cfg(feature = "rustls")]
|
|
||||||
use actix_tls::connect::ssl::rustls::{RustlsConnector, Session};
|
|
||||||
|
|
||||||
let ssl_service = TimeoutService::new(
|
use actix_tls::connect::ssl::openssl::{OpensslConnector, SslStream};
|
||||||
self.config.timeout,
|
|
||||||
pipeline(
|
impl<Io: ConnectionIo> IntoConnectionIo for TcpConnection<Uri, SslStream<Io>> {
|
||||||
apply_fn(self.connector.clone(), |msg: Connect, srv| {
|
fn into_connection_io(self) -> (Box<dyn ConnectionIo>, Protocol) {
|
||||||
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
let sock = self.into_parts().0;
|
||||||
})
|
|
||||||
.map_err(ConnectError::from),
|
|
||||||
)
|
|
||||||
.and_then(match self.ssl {
|
|
||||||
#[cfg(feature = "openssl")]
|
|
||||||
SslConnector::Openssl(ssl) => service(
|
|
||||||
OpensslConnector::service(ssl)
|
|
||||||
.map(|stream| {
|
|
||||||
let sock = stream.into_parts().0;
|
|
||||||
let h2 = sock
|
let h2 = sock
|
||||||
.ssl()
|
.ssl()
|
||||||
.selected_alpn_protocol()
|
.selected_alpn_protocol()
|
||||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
.map(|protos| protos.windows(2).any(|w| w == H2))
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
if h2 {
|
if h2 {
|
||||||
(Box::new(sock) as Box<dyn Io>, Protocol::Http2)
|
(Box::new(sock), Protocol::Http2)
|
||||||
} else {
|
} else {
|
||||||
(Box::new(sock) as Box<dyn Io>, Protocol::Http1)
|
(Box::new(sock), Protocol::Http1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let handshake_timeout = self.config.handshake_timeout;
|
||||||
|
|
||||||
|
let tls_service = TlsConnectorService {
|
||||||
|
tcp_service: tcp_service_inner,
|
||||||
|
tls_service: OpensslConnector::service(tls),
|
||||||
|
timeout: handshake_timeout,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(actix_service::boxed::rc_service(tls_service))
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.map_err(ConnectError::from),
|
|
||||||
),
|
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
SslConnector::Rustls(ssl) => service(
|
SslConnector::Rustls(tls) => {
|
||||||
RustlsConnector::service(ssl)
|
const H2: &[u8] = b"h2";
|
||||||
.map_err(ConnectError::from)
|
|
||||||
.map(|stream| {
|
use actix_tls::connect::ssl::rustls::{
|
||||||
let sock = stream.into_parts().0;
|
RustlsConnector, Session, TlsStream,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<Io: ConnectionIo> IntoConnectionIo for TcpConnection<Uri, TlsStream<Io>> {
|
||||||
|
fn into_connection_io(self) -> (Box<dyn ConnectionIo>, Protocol) {
|
||||||
|
let sock = self.into_parts().0;
|
||||||
let h2 = sock
|
let h2 = sock
|
||||||
.get_ref()
|
.get_ref()
|
||||||
.1
|
.1
|
||||||
|
@ -315,185 +321,361 @@ where
|
||||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
.map(|protos| protos.windows(2).any(|w| w == H2))
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
if h2 {
|
if h2 {
|
||||||
(Box::new(sock) as Box<dyn Io>, Protocol::Http2)
|
(Box::new(sock), Protocol::Http2)
|
||||||
} else {
|
} else {
|
||||||
(Box::new(sock) as Box<dyn Io>, Protocol::Http1)
|
(Box::new(sock), Protocol::Http1)
|
||||||
}
|
}
|
||||||
}),
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.map_err(|e| match e {
|
|
||||||
TimeoutError::Service(e) => e,
|
|
||||||
TimeoutError::Timeout => ConnectError::Timeout,
|
|
||||||
});
|
|
||||||
|
|
||||||
let tcp_service = TimeoutService::new(
|
|
||||||
self.config.timeout,
|
|
||||||
apply_fn(self.connector, |msg: Connect, srv| {
|
|
||||||
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
|
||||||
})
|
|
||||||
.map_err(ConnectError::from)
|
|
||||||
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
|
||||||
)
|
|
||||||
.map_err(|e| match e {
|
|
||||||
TimeoutError::Service(e) => e,
|
|
||||||
TimeoutError::Timeout => ConnectError::Timeout,
|
|
||||||
});
|
|
||||||
|
|
||||||
connect_impl::InnerConnector {
|
|
||||||
tcp_pool: ConnectionPool::new(
|
|
||||||
tcp_service,
|
|
||||||
self.config.no_disconnect_timeout(),
|
|
||||||
),
|
|
||||||
ssl_pool: ConnectionPool::new(ssl_service, self.config),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let handshake_timeout = self.config.handshake_timeout;
|
||||||
|
|
||||||
|
let tls_service = TlsConnectorService {
|
||||||
|
tcp_service: tcp_service_inner,
|
||||||
|
tls_service: RustlsConnector::service(tls),
|
||||||
|
timeout: handshake_timeout,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(actix_service::boxed::rc_service(tls_service))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tcp_config = self.config.no_disconnect_timeout();
|
||||||
|
|
||||||
|
let tcp_pool = ConnectionPool::new(tcp_service, tcp_config);
|
||||||
|
|
||||||
|
let tls_config = self.config;
|
||||||
|
let tls_pool = tls_service
|
||||||
|
.map(move |tls_service| ConnectionPool::new(tls_service, tls_config));
|
||||||
|
|
||||||
|
ConnectorServicePriv { tcp_pool, tls_pool }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// tcp service for map `TcpConnection<Uri, Io>` type to `(Io, Protocol)`
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TcpConnectorService<S: Clone> {
|
||||||
|
service: S,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, Io> Service<Connect> for TcpConnectorService<S>
|
||||||
|
where
|
||||||
|
S: Service<Connect, Response = TcpConnection<Uri, Io>, Error = ConnectError>
|
||||||
|
+ Clone
|
||||||
|
+ 'static,
|
||||||
|
{
|
||||||
|
type Response = (Io, Protocol);
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Future = TcpConnectorFuture<S::Future>;
|
||||||
|
|
||||||
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
|
TcpConnectorFuture {
|
||||||
|
fut: self.service.call(req),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(feature = "openssl", feature = "rustls")))]
|
#[pin_project]
|
||||||
mod connect_impl {
|
pub struct TcpConnectorFuture<Fut> {
|
||||||
use std::task::{Context, Poll};
|
#[pin]
|
||||||
|
fut: Fut,
|
||||||
|
}
|
||||||
|
|
||||||
use futures_core::future::LocalBoxFuture;
|
impl<Fut, Io> Future for TcpConnectorFuture<Fut>
|
||||||
|
where
|
||||||
|
Fut: Future<Output = Result<TcpConnection<Uri, Io>, ConnectError>>,
|
||||||
|
{
|
||||||
|
type Output = Result<(Io, Protocol), ConnectError>;
|
||||||
|
|
||||||
use super::*;
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
use crate::client::connection::IoConnection;
|
self.project()
|
||||||
|
.fut
|
||||||
pub(crate) struct InnerConnector<T, Io>
|
.poll(cx)
|
||||||
where
|
.map_ok(|res| (res.into_parts().0, Protocol::Http1))
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
|
||||||
{
|
|
||||||
pub(crate) tcp_pool: ConnectionPool<T, Io>,
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T, Io> Clone for InnerConnector<T, Io>
|
/// service for establish tcp connection and do client tls handshake.
|
||||||
where
|
/// operation is canceled when timeout limit reached.
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
struct TlsConnectorService<S, St> {
|
||||||
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
/// tcp connection is canceled on `TcpConnectorInnerService`'s timeout setting.
|
||||||
{
|
tcp_service: S,
|
||||||
fn clone(&self) -> Self {
|
/// tls connection is canceled on `TlsConnectorService`'s timeout setting.
|
||||||
InnerConnector {
|
tls_service: St,
|
||||||
tcp_pool: self.tcp_pool.clone(),
|
timeout: Duration,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, Io> Service<Connect> for InnerConnector<T, Io>
|
impl<S, St, Io, Res> Service<Connect> for TlsConnectorService<S, St>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
S: Service<Connect, Response = TcpConnection<Uri, Io>, Error = ConnectError>
|
||||||
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
+ Clone
|
||||||
{
|
+ 'static,
|
||||||
type Response = IoConnection<Io>;
|
St: Service<TcpConnection<Uri, Io>, Response = Res, Error = std::io::Error>
|
||||||
|
+ Clone
|
||||||
|
+ 'static,
|
||||||
|
Io: ConnectionIo,
|
||||||
|
Res: IntoConnectionIo,
|
||||||
|
{
|
||||||
|
type Response = (Box<dyn ConnectionIo>, Protocol);
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = LocalBoxFuture<'static, Result<IoConnection<Io>, ConnectError>>;
|
type Future = TlsConnectorFuture<St, S::Future, St::Future>;
|
||||||
|
|
||||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.tcp_pool.poll_ready(cx)
|
ready!(self.tcp_service.poll_ready(cx))?;
|
||||||
|
ready!(self.tls_service.poll_ready(cx))?;
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&self, req: Connect) -> Self::Future {
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
match req.uri.scheme_str() {
|
let fut = self.tcp_service.call(req);
|
||||||
Some("https") | Some("wss") => {
|
let tls_service = self.tls_service.clone();
|
||||||
Box::pin(async { Err(ConnectError::SslIsNotSupported) })
|
let timeout = self.timeout;
|
||||||
}
|
|
||||||
_ => self.tcp_pool.call(req),
|
TlsConnectorFuture::TcpConnect {
|
||||||
}
|
fut,
|
||||||
|
tls_service: Some(tls_service),
|
||||||
|
timeout,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "openssl", feature = "rustls"))]
|
#[pin_project(project = TlsConnectorProj)]
|
||||||
mod connect_impl {
|
#[allow(clippy::large_enum_variant)]
|
||||||
use std::future::Future;
|
enum TlsConnectorFuture<S, Fut1, Fut2> {
|
||||||
use std::pin::Pin;
|
TcpConnect {
|
||||||
use std::task::{Context, Poll};
|
#[pin]
|
||||||
|
fut: Fut1,
|
||||||
|
tls_service: Option<S>,
|
||||||
|
timeout: Duration,
|
||||||
|
},
|
||||||
|
TlsConnect {
|
||||||
|
#[pin]
|
||||||
|
fut: Fut2,
|
||||||
|
#[pin]
|
||||||
|
timeout: Sleep,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
use super::*;
|
/// helper trait for generic over different TlsStream types between tls crates.
|
||||||
use crate::client::connection::EitherIoConnection;
|
trait IntoConnectionIo {
|
||||||
|
fn into_connection_io(self) -> (Box<dyn ConnectionIo>, Protocol);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct InnerConnector<S1, S2, Io1, Io2>
|
impl<S, Io, Fut1, Fut2, Res> Future for TlsConnectorFuture<S, Fut1, Fut2>
|
||||||
where
|
where
|
||||||
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
|
S: Service<
|
||||||
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
|
TcpConnection<Uri, Io>,
|
||||||
Io1: AsyncRead + AsyncWrite + Unpin + 'static,
|
Response = Res,
|
||||||
Io2: AsyncRead + AsyncWrite + Unpin + 'static,
|
Error = std::io::Error,
|
||||||
{
|
Future = Fut2,
|
||||||
pub(crate) tcp_pool: ConnectionPool<S1, Io1>,
|
>,
|
||||||
pub(crate) ssl_pool: ConnectionPool<S2, Io2>,
|
Fut1: Future<Output = Result<TcpConnection<Uri, Io>, ConnectError>>,
|
||||||
}
|
Fut2: Future<Output = Result<S::Response, S::Error>>,
|
||||||
|
Io: ConnectionIo,
|
||||||
impl<S1, S2, Io1, Io2> Clone for InnerConnector<S1, S2, Io1, Io2>
|
Res: IntoConnectionIo,
|
||||||
where
|
{
|
||||||
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
|
type Output = Result<(Box<dyn ConnectionIo>, Protocol), ConnectError>;
|
||||||
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
|
|
||||||
Io1: AsyncRead + AsyncWrite + Unpin + 'static,
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
Io2: AsyncRead + AsyncWrite + Unpin + 'static,
|
match self.as_mut().project() {
|
||||||
{
|
TlsConnectorProj::TcpConnect {
|
||||||
fn clone(&self) -> Self {
|
fut,
|
||||||
InnerConnector {
|
tls_service,
|
||||||
tcp_pool: self.tcp_pool.clone(),
|
timeout,
|
||||||
ssl_pool: self.ssl_pool.clone(),
|
} => {
|
||||||
|
let res = ready!(fut.poll(cx))?;
|
||||||
|
let fut = tls_service
|
||||||
|
.take()
|
||||||
|
.expect("TlsConnectorFuture polled after complete")
|
||||||
|
.call(res);
|
||||||
|
let timeout = sleep(*timeout);
|
||||||
|
self.set(TlsConnectorFuture::TlsConnect { fut, timeout });
|
||||||
|
self.poll(cx)
|
||||||
}
|
}
|
||||||
|
TlsConnectorProj::TlsConnect { fut, timeout } => match fut.poll(cx)? {
|
||||||
|
Poll::Ready(res) => Poll::Ready(Ok(res.into_connection_io())),
|
||||||
|
Poll::Pending => timeout.poll(cx).map(|_| Err(ConnectError::Timeout)),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<S1, S2, Io1, Io2> Service<Connect> for InnerConnector<S1, S2, Io1, Io2>
|
/// service for establish tcp connection.
|
||||||
where
|
/// operation is canceled when timeout limit reached.
|
||||||
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
|
#[derive(Clone)]
|
||||||
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
|
pub struct TcpConnectorInnerService<S: Clone> {
|
||||||
Io1: AsyncRead + AsyncWrite + Unpin + 'static,
|
service: S,
|
||||||
Io2: AsyncRead + AsyncWrite + Unpin + 'static,
|
timeout: Duration,
|
||||||
{
|
local_address: Option<std::net::IpAddr>,
|
||||||
type Response = EitherIoConnection<Io1, Io2>;
|
}
|
||||||
|
|
||||||
|
impl<S: Clone> TcpConnectorInnerService<S> {
|
||||||
|
fn new(
|
||||||
|
service: S,
|
||||||
|
timeout: Duration,
|
||||||
|
local_address: Option<std::net::IpAddr>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
service,
|
||||||
|
timeout,
|
||||||
|
local_address,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, Io> Service<Connect> for TcpConnectorInnerService<S>
|
||||||
|
where
|
||||||
|
S: Service<
|
||||||
|
TcpConnect<Uri>,
|
||||||
|
Response = TcpConnection<Uri, Io>,
|
||||||
|
Error = TcpConnectError,
|
||||||
|
> + Clone
|
||||||
|
+ 'static,
|
||||||
|
{
|
||||||
|
type Response = S::Response;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = InnerConnectorResponse<S1, S2, Io1, Io2>;
|
type Future = TcpConnectorInnerFuture<S::Future>;
|
||||||
|
|
||||||
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
|
let mut req = TcpConnect::new(req.uri).set_addr(req.addr);
|
||||||
|
|
||||||
|
if let Some(local_addr) = self.local_address {
|
||||||
|
req = req.set_local_addr(local_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpConnectorInnerFuture {
|
||||||
|
fut: self.service.call(req),
|
||||||
|
timeout: sleep(self.timeout),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
|
pub struct TcpConnectorInnerFuture<Fut> {
|
||||||
|
#[pin]
|
||||||
|
fut: Fut,
|
||||||
|
#[pin]
|
||||||
|
timeout: Sleep,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Fut, Io> Future for TcpConnectorInnerFuture<Fut>
|
||||||
|
where
|
||||||
|
Fut: Future<Output = Result<TcpConnection<Uri, Io>, TcpConnectError>>,
|
||||||
|
{
|
||||||
|
type Output = Result<TcpConnection<Uri, Io>, ConnectError>;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let this = self.project();
|
||||||
|
match this.fut.poll(cx) {
|
||||||
|
Poll::Ready(res) => Poll::Ready(res.map_err(ConnectError::from)),
|
||||||
|
Poll::Pending => this.timeout.poll(cx).map(|_| Err(ConnectError::Timeout)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connector service for pooled Plain/Tls Tcp connections.
|
||||||
|
pub type ConnectorService<S, Io> = ConnectorServicePriv<
|
||||||
|
TcpConnectorService<TcpConnectorInnerService<S>>,
|
||||||
|
Rc<
|
||||||
|
dyn Service<
|
||||||
|
Connect,
|
||||||
|
Response = (Box<dyn ConnectionIo>, Protocol),
|
||||||
|
Error = ConnectError,
|
||||||
|
Future = LocalBoxFuture<
|
||||||
|
'static,
|
||||||
|
Result<(Box<dyn ConnectionIo>, Protocol), ConnectError>,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
Io,
|
||||||
|
Box<dyn ConnectionIo>,
|
||||||
|
>;
|
||||||
|
|
||||||
|
pub struct ConnectorServicePriv<S1, S2, Io1, Io2>
|
||||||
|
where
|
||||||
|
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError>,
|
||||||
|
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError>,
|
||||||
|
Io1: ConnectionIo,
|
||||||
|
Io2: ConnectionIo,
|
||||||
|
{
|
||||||
|
tcp_pool: ConnectionPool<S1, Io1>,
|
||||||
|
tls_pool: Option<ConnectionPool<S2, Io2>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S1, S2, Io1, Io2> Service<Connect> for ConnectorServicePriv<S1, S2, Io1, Io2>
|
||||||
|
where
|
||||||
|
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError>
|
||||||
|
+ Clone
|
||||||
|
+ 'static,
|
||||||
|
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError>
|
||||||
|
+ Clone
|
||||||
|
+ 'static,
|
||||||
|
Io1: ConnectionIo,
|
||||||
|
Io2: ConnectionIo,
|
||||||
|
{
|
||||||
|
type Response = Connection<Io1, Io2>;
|
||||||
|
type Error = ConnectError;
|
||||||
|
type Future = ConnectorServiceFuture<S1, S2, Io1, Io2>;
|
||||||
|
|
||||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.tcp_pool.poll_ready(cx)
|
ready!(self.tcp_pool.poll_ready(cx))?;
|
||||||
|
if let Some(ref tls_pool) = self.tls_pool {
|
||||||
|
ready!(tls_pool.poll_ready(cx))?;
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&self, req: Connect) -> Self::Future {
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
match req.uri.scheme_str() {
|
match req.uri.scheme_str() {
|
||||||
Some("https") | Some("wss") => {
|
Some("https") | Some("wss") => match self.tls_pool {
|
||||||
InnerConnectorResponse::Io2(self.ssl_pool.call(req))
|
None => ConnectorServiceFuture::SslIsNotSupported,
|
||||||
}
|
Some(ref pool) => ConnectorServiceFuture::Tls(pool.call(req)),
|
||||||
_ => InnerConnectorResponse::Io1(self.tcp_pool.call(req)),
|
},
|
||||||
}
|
_ => ConnectorServiceFuture::Tcp(self.tcp_pool.call(req)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[pin_project::pin_project(project = InnerConnectorProj)]
|
#[pin_project(project = ConnectorServiceProj)]
|
||||||
pub(crate) enum InnerConnectorResponse<S1, S2, Io1, Io2>
|
pub enum ConnectorServiceFuture<S1, S2, Io1, Io2>
|
||||||
where
|
where
|
||||||
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
|
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError>
|
||||||
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
|
+ Clone
|
||||||
Io1: AsyncRead + AsyncWrite + Unpin + 'static,
|
+ 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + Unpin + 'static,
|
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError>
|
||||||
{
|
+ Clone
|
||||||
Io1(#[pin] <ConnectionPool<S1, Io1> as Service<Connect>>::Future),
|
+ 'static,
|
||||||
Io2(#[pin] <ConnectionPool<S2, Io2> as Service<Connect>>::Future),
|
Io1: ConnectionIo,
|
||||||
}
|
Io2: ConnectionIo,
|
||||||
|
{
|
||||||
|
Tcp(#[pin] <ConnectionPool<S1, Io1> as Service<Connect>>::Future),
|
||||||
|
Tls(#[pin] <ConnectionPool<S2, Io2> as Service<Connect>>::Future),
|
||||||
|
SslIsNotSupported,
|
||||||
|
}
|
||||||
|
|
||||||
impl<S1, S2, Io1, Io2> Future for InnerConnectorResponse<S1, S2, Io1, Io2>
|
impl<S1, S2, Io1, Io2> Future for ConnectorServiceFuture<S1, S2, Io1, Io2>
|
||||||
where
|
where
|
||||||
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
|
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError>
|
||||||
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
|
+ Clone
|
||||||
Io1: AsyncRead + AsyncWrite + Unpin + 'static,
|
+ 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + Unpin + 'static,
|
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError>
|
||||||
{
|
+ Clone
|
||||||
type Output = Result<EitherIoConnection<Io1, Io2>, ConnectError>;
|
+ 'static,
|
||||||
|
Io1: ConnectionIo,
|
||||||
|
Io2: ConnectionIo,
|
||||||
|
{
|
||||||
|
type Output = Result<Connection<Io1, Io2>, ConnectError>;
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
match self.project() {
|
match self.project() {
|
||||||
InnerConnectorProj::Io1(fut) => {
|
ConnectorServiceProj::Tcp(fut) => fut.poll(cx).map_ok(Connection::Tcp),
|
||||||
fut.poll(cx).map_ok(EitherIoConnection::A)
|
ConnectorServiceProj::Tls(fut) => fut.poll(cx).map_ok(Connection::Tls),
|
||||||
}
|
ConnectorServiceProj::SslIsNotSupported => {
|
||||||
InnerConnectorProj::Io2(fut) => {
|
Poll::Ready(Err(ConnectError::SslIsNotSupported))
|
||||||
fut.poll(cx).map_ok(EitherIoConnection::B)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,35 @@
|
||||||
use std::io::Write;
|
use std::{
|
||||||
use std::pin::Pin;
|
io::Write,
|
||||||
use std::task::{Context, Poll};
|
pin::Pin,
|
||||||
use std::{io, time};
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed, ReadBuf};
|
use actix_codec::Framed;
|
||||||
use bytes::buf::BufMut;
|
use bytes::buf::BufMut;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use futures_util::future::poll_fn;
|
use futures_util::{future::poll_fn, SinkExt as _};
|
||||||
use futures_util::{SinkExt, StreamExt};
|
|
||||||
|
|
||||||
use crate::error::PayloadError;
|
use crate::error::PayloadError;
|
||||||
use crate::h1;
|
use crate::h1;
|
||||||
use crate::header::HeaderMap;
|
use crate::http::{
|
||||||
use crate::http::header::{IntoHeaderValue, HOST};
|
header::{HeaderMap, IntoHeaderValue, EXPECT, HOST},
|
||||||
|
StatusCode,
|
||||||
|
};
|
||||||
use crate::message::{RequestHeadType, ResponseHead};
|
use crate::message::{RequestHeadType, ResponseHead};
|
||||||
use crate::payload::{Payload, PayloadStream};
|
use crate::payload::{Payload, PayloadStream};
|
||||||
|
|
||||||
use super::connection::{ConnectionLifetime, ConnectionType, IoConnection};
|
use super::connection::{ConnectionIo, H1Connection};
|
||||||
use super::error::{ConnectError, SendRequestError};
|
use super::error::{ConnectError, SendRequestError};
|
||||||
use super::pool::Acquired;
|
|
||||||
use crate::body::{BodySize, MessageBody};
|
use crate::body::{BodySize, MessageBody};
|
||||||
|
|
||||||
pub(crate) async fn send_request<T, B>(
|
pub(crate) async fn send_request<Io, B>(
|
||||||
io: T,
|
io: H1Connection<Io>,
|
||||||
mut head: RequestHeadType,
|
mut head: RequestHeadType,
|
||||||
body: B,
|
body: B,
|
||||||
created: time::Instant,
|
|
||||||
pool: Option<Acquired<T>>,
|
|
||||||
) -> Result<(ResponseHead, Payload), SendRequestError>
|
) -> Result<(ResponseHead, Payload), SendRequestError>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
Io: ConnectionIo,
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
// set request host header
|
// set request host header
|
||||||
|
@ -40,9 +39,9 @@ where
|
||||||
if let Some(host) = head.as_ref().uri.host() {
|
if let Some(host) = head.as_ref().uri.host() {
|
||||||
let mut wrt = BytesMut::with_capacity(host.len() + 5).writer();
|
let mut wrt = BytesMut::with_capacity(host.len() + 5).writer();
|
||||||
|
|
||||||
let _ = match head.as_ref().uri.port_u16() {
|
match head.as_ref().uri.port_u16() {
|
||||||
None | Some(80) | Some(443) => write!(wrt, "{}", host),
|
None | Some(80) | Some(443) => write!(wrt, "{}", host)?,
|
||||||
Some(port) => write!(wrt, "{}:{}", host, port),
|
Some(port) => write!(wrt, "{}:{}", host, port)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
match wrt.get_mut().split().freeze().try_into_value() {
|
match wrt.get_mut().split().freeze().try_into_value() {
|
||||||
|
@ -60,71 +59,102 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let io = H1Connection {
|
// create Framed and prepare sending request
|
||||||
created,
|
let mut framed = Framed::new(io, h1::ClientCodec::default());
|
||||||
pool,
|
|
||||||
io: Some(io),
|
// Check EXPECT header and enable expect handle flag accordingly.
|
||||||
|
//
|
||||||
|
// RFC: https://tools.ietf.org/html/rfc7231#section-5.1.1
|
||||||
|
let is_expect = if head.as_ref().headers.contains_key(EXPECT) {
|
||||||
|
match body.size() {
|
||||||
|
BodySize::None | BodySize::Empty | BodySize::Sized(0) => {
|
||||||
|
let keep_alive = framed.codec_ref().keepalive();
|
||||||
|
framed.io_mut().on_release(keep_alive);
|
||||||
|
|
||||||
|
// TODO: use a new variant or a new type better describing error violate
|
||||||
|
// `Requirements for clients` session of above RFC
|
||||||
|
return Err(SendRequestError::Connect(ConnectError::Disconnected));
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
// create Framed and send request
|
framed.send((head, body.size()).into()).await?;
|
||||||
let mut framed_inner = Framed::new(io, h1::ClientCodec::default());
|
|
||||||
framed_inner.send((head, body.size()).into()).await?;
|
|
||||||
|
|
||||||
|
let mut pin_framed = Pin::new(&mut framed);
|
||||||
|
|
||||||
|
// special handle for EXPECT request.
|
||||||
|
let (do_send, mut res_head) = if is_expect {
|
||||||
|
let head = poll_fn(|cx| pin_framed.as_mut().poll_next(cx))
|
||||||
|
.await
|
||||||
|
.ok_or(ConnectError::Disconnected)??;
|
||||||
|
|
||||||
|
// return response head in case status code is not continue
|
||||||
|
// and current head would be used as final response head.
|
||||||
|
(head.status == StatusCode::CONTINUE, Some(head))
|
||||||
|
} else {
|
||||||
|
(true, None)
|
||||||
|
};
|
||||||
|
|
||||||
|
if do_send {
|
||||||
// send request body
|
// send request body
|
||||||
match body.size() {
|
match body.size() {
|
||||||
BodySize::None | BodySize::Empty | BodySize::Sized(0) => {}
|
BodySize::None | BodySize::Empty | BodySize::Sized(0) => {}
|
||||||
_ => send_body(body, Pin::new(&mut framed_inner)).await?,
|
_ => send_body(body, pin_framed.as_mut()).await?,
|
||||||
};
|
};
|
||||||
|
|
||||||
// read response and init read body
|
// read response and init read body
|
||||||
let res = Pin::new(&mut framed_inner).into_future().await;
|
let head = poll_fn(|cx| pin_framed.as_mut().poll_next(cx))
|
||||||
let (head, framed) = if let (Some(result), framed) = res {
|
.await
|
||||||
let item = result.map_err(SendRequestError::from)?;
|
.ok_or(ConnectError::Disconnected)??;
|
||||||
(item, framed)
|
|
||||||
} else {
|
|
||||||
return Err(SendRequestError::from(ConnectError::Disconnected));
|
|
||||||
};
|
|
||||||
|
|
||||||
match framed.codec_ref().message_type() {
|
res_head = Some(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
let head = res_head.unwrap();
|
||||||
|
|
||||||
|
match pin_framed.codec_ref().message_type() {
|
||||||
h1::MessageType::None => {
|
h1::MessageType::None => {
|
||||||
let force_close = !framed.codec_ref().keepalive();
|
let keep_alive = pin_framed.codec_ref().keepalive();
|
||||||
release_connection(framed, force_close);
|
pin_framed.io_mut().on_release(keep_alive);
|
||||||
|
|
||||||
Ok((head, Payload::None))
|
Ok((head, Payload::None))
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let pl: PayloadStream = PlStream::new(framed_inner).boxed_local();
|
let pl: PayloadStream = Box::pin(PlStream::new(framed));
|
||||||
Ok((head, pl.into()))
|
Ok((head, pl.into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn open_tunnel<T>(
|
pub(crate) async fn open_tunnel<Io>(
|
||||||
io: T,
|
io: Io,
|
||||||
head: RequestHeadType,
|
head: RequestHeadType,
|
||||||
) -> Result<(ResponseHead, Framed<T, h1::ClientCodec>), SendRequestError>
|
) -> Result<(ResponseHead, Framed<Io, h1::ClientCodec>), SendRequestError>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
Io: ConnectionIo,
|
||||||
{
|
{
|
||||||
// create Framed and send request
|
// create Framed and send request.
|
||||||
let mut framed = Framed::new(io, h1::ClientCodec::default());
|
let mut framed = Framed::new(io, h1::ClientCodec::default());
|
||||||
framed.send((head, BodySize::None).into()).await?;
|
framed.send((head, BodySize::None).into()).await?;
|
||||||
|
|
||||||
// read response
|
// read response head.
|
||||||
if let (Some(result), framed) = framed.into_future().await {
|
let head = poll_fn(|cx| Pin::new(&mut framed).poll_next(cx))
|
||||||
let head = result.map_err(SendRequestError::from)?;
|
.await
|
||||||
|
.ok_or(ConnectError::Disconnected)??;
|
||||||
|
|
||||||
Ok((head, framed))
|
Ok((head, framed))
|
||||||
} else {
|
|
||||||
Err(SendRequestError::from(ConnectError::Disconnected))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// send request body to the peer
|
/// send request body to the peer
|
||||||
pub(crate) async fn send_body<T, B>(
|
pub(crate) async fn send_body<Io, B>(
|
||||||
body: B,
|
body: B,
|
||||||
mut framed: Pin<&mut Framed<T, h1::ClientCodec>>,
|
mut framed: Pin<&mut Framed<Io, h1::ClientCodec>>,
|
||||||
) -> Result<(), SendRequestError>
|
) -> Result<(), SendRequestError>
|
||||||
where
|
where
|
||||||
T: ConnectionLifetime + Unpin,
|
Io: ConnectionIo,
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
actix_rt::pin!(body);
|
actix_rt::pin!(body);
|
||||||
|
@ -159,95 +189,21 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SinkExt::flush(Pin::into_inner(framed)).await?;
|
framed.get_mut().flush().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// HTTP client connection
|
|
||||||
pub struct H1Connection<T>
|
|
||||||
where
|
|
||||||
T: AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
/// T should be `Unpin`
|
|
||||||
io: Option<T>,
|
|
||||||
created: time::Instant,
|
|
||||||
pool: Option<Acquired<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> ConnectionLifetime for H1Connection<T>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
/// Close connection
|
|
||||||
fn close(mut self: Pin<&mut Self>) {
|
|
||||||
if let Some(mut pool) = self.pool.take() {
|
|
||||||
if let Some(io) = self.io.take() {
|
|
||||||
pool.close(IoConnection::new(
|
|
||||||
ConnectionType::H1(io),
|
|
||||||
self.created,
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Release this connection to the connection pool
|
|
||||||
fn release(mut self: Pin<&mut Self>) {
|
|
||||||
if let Some(mut pool) = self.pool.take() {
|
|
||||||
if let Some(io) = self.io.take() {
|
|
||||||
pool.release(IoConnection::new(
|
|
||||||
ConnectionType::H1(io),
|
|
||||||
self.created,
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite + Unpin + 'static> AsyncRead for H1Connection<T> {
|
|
||||||
fn poll_read(
|
|
||||||
mut self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
buf: &mut ReadBuf<'_>,
|
|
||||||
) -> Poll<io::Result<()>> {
|
|
||||||
Pin::new(&mut self.io.as_mut().unwrap()).poll_read(cx, buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite + Unpin + 'static> AsyncWrite for H1Connection<T> {
|
|
||||||
fn poll_write(
|
|
||||||
mut self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
buf: &[u8],
|
|
||||||
) -> Poll<io::Result<usize>> {
|
|
||||||
Pin::new(&mut self.io.as_mut().unwrap()).poll_write(cx, buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_flush(
|
|
||||||
mut self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
) -> Poll<io::Result<()>> {
|
|
||||||
Pin::new(self.io.as_mut().unwrap()).poll_flush(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_shutdown(
|
|
||||||
mut self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
) -> Poll<Result<(), io::Error>> {
|
|
||||||
Pin::new(self.io.as_mut().unwrap()).poll_shutdown(cx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
#[pin_project::pin_project]
|
||||||
pub(crate) struct PlStream<Io> {
|
pub(crate) struct PlStream<Io: ConnectionIo>
|
||||||
|
where
|
||||||
|
Io: ConnectionIo,
|
||||||
|
{
|
||||||
#[pin]
|
#[pin]
|
||||||
framed: Option<Framed<Io, h1::ClientPayloadCodec>>,
|
framed: Option<Framed<H1Connection<Io>, h1::ClientPayloadCodec>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io: ConnectionLifetime> PlStream<Io> {
|
impl<Io: ConnectionIo> PlStream<Io> {
|
||||||
fn new(framed: Framed<Io, h1::ClientCodec>) -> Self {
|
fn new(framed: Framed<H1Connection<Io>, h1::ClientCodec>) -> Self {
|
||||||
let framed = framed.into_map_codec(|codec| codec.into_payload_codec());
|
let framed = framed.into_map_codec(|codec| codec.into_payload_codec());
|
||||||
|
|
||||||
PlStream {
|
PlStream {
|
||||||
|
@ -256,24 +212,23 @@ impl<Io: ConnectionLifetime> PlStream<Io> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io: ConnectionLifetime> Stream for PlStream<Io> {
|
impl<Io: ConnectionIo> Stream for PlStream<Io> {
|
||||||
type Item = Result<Bytes, PayloadError>;
|
type Item = Result<Bytes, PayloadError>;
|
||||||
|
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Self::Item>> {
|
) -> Poll<Option<Self::Item>> {
|
||||||
let mut this = self.project();
|
let mut framed = self.project().framed.as_pin_mut().unwrap();
|
||||||
|
|
||||||
match this.framed.as_mut().as_pin_mut().unwrap().next_item(cx)? {
|
match framed.as_mut().next_item(cx)? {
|
||||||
Poll::Pending => Poll::Pending,
|
Poll::Pending => Poll::Pending,
|
||||||
Poll::Ready(Some(chunk)) => {
|
Poll::Ready(Some(chunk)) => {
|
||||||
if let Some(chunk) = chunk {
|
if let Some(chunk) = chunk {
|
||||||
Poll::Ready(Some(Ok(chunk)))
|
Poll::Ready(Some(Ok(chunk)))
|
||||||
} else {
|
} else {
|
||||||
let framed = this.framed.as_mut().as_pin_mut().unwrap();
|
let keep_alive = framed.codec_ref().keepalive();
|
||||||
let force_close = !framed.codec_ref().keepalive();
|
framed.io_mut().on_release(keep_alive);
|
||||||
release_connection(framed, force_close);
|
|
||||||
Poll::Ready(None)
|
Poll::Ready(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,14 +236,3 @@ impl<Io: ConnectionLifetime> Stream for PlStream<Io> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn release_connection<T, U>(framed: Pin<&mut Framed<T, U>>, force_close: bool)
|
|
||||||
where
|
|
||||||
T: ConnectionLifetime,
|
|
||||||
{
|
|
||||||
if !force_close && framed.is_read_buf_empty() && framed.is_write_buf_empty() {
|
|
||||||
framed.io_pin().release()
|
|
||||||
} else {
|
|
||||||
framed.io_pin().close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::time;
|
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::future::poll_fn;
|
use futures_util::future::poll_fn;
|
||||||
use h2::{
|
use h2::{
|
||||||
|
@ -18,20 +15,16 @@ use crate::message::{RequestHeadType, ResponseHead};
|
||||||
use crate::payload::Payload;
|
use crate::payload::Payload;
|
||||||
|
|
||||||
use super::config::ConnectorConfig;
|
use super::config::ConnectorConfig;
|
||||||
use super::connection::{ConnectionType, IoConnection};
|
use super::connection::{ConnectionIo, H2Connection};
|
||||||
use super::error::SendRequestError;
|
use super::error::SendRequestError;
|
||||||
use super::pool::Acquired;
|
|
||||||
use crate::client::connection::H2Connection;
|
|
||||||
|
|
||||||
pub(crate) async fn send_request<T, B>(
|
pub(crate) async fn send_request<Io, B>(
|
||||||
mut io: H2Connection,
|
mut io: H2Connection<Io>,
|
||||||
head: RequestHeadType,
|
head: RequestHeadType,
|
||||||
body: B,
|
body: B,
|
||||||
created: time::Instant,
|
|
||||||
pool: Option<Acquired<T>>,
|
|
||||||
) -> Result<(ResponseHead, Payload), SendRequestError>
|
) -> Result<(ResponseHead, Payload), SendRequestError>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
Io: ConnectionIo,
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
trace!("Sending client request: {:?} {:?}", head, body.size());
|
trace!("Sending client request: {:?} {:?}", head, body.size());
|
||||||
|
@ -61,10 +54,14 @@ where
|
||||||
BodySize::Empty => req
|
BodySize::Empty => req
|
||||||
.headers_mut()
|
.headers_mut()
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
||||||
BodySize::Sized(len) => req.headers_mut().insert(
|
BodySize::Sized(len) => {
|
||||||
|
let mut buf = itoa::Buffer::new();
|
||||||
|
|
||||||
|
req.headers_mut().insert(
|
||||||
CONTENT_LENGTH,
|
CONTENT_LENGTH,
|
||||||
HeaderValue::try_from(format!("{}", len)).unwrap(),
|
HeaderValue::from_str(buf.format(len)).unwrap(),
|
||||||
),
|
)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extracting extra headers from RequestHeadType. HeaderMap::new() does not allocate.
|
// Extracting extra headers from RequestHeadType. HeaderMap::new() does not allocate.
|
||||||
|
@ -87,7 +84,10 @@ where
|
||||||
// copy headers
|
// copy headers
|
||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
match *key {
|
match *key {
|
||||||
CONNECTION | TRANSFER_ENCODING => continue, // http2 specific
|
// TODO: consider skipping other headers according to:
|
||||||
|
// https://tools.ietf.org/html/rfc7540#section-8.1.2.2
|
||||||
|
// omit HTTP/1.x only headers
|
||||||
|
CONNECTION | TRANSFER_ENCODING => continue,
|
||||||
CONTENT_LENGTH if skip_len => continue,
|
CONTENT_LENGTH if skip_len => continue,
|
||||||
// DATE => has_date = true,
|
// DATE => has_date = true,
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -97,13 +97,13 @@ where
|
||||||
|
|
||||||
let res = poll_fn(|cx| io.poll_ready(cx)).await;
|
let res = poll_fn(|cx| io.poll_ready(cx)).await;
|
||||||
if let Err(e) = res {
|
if let Err(e) = res {
|
||||||
release(io, pool, created, e.is_io());
|
io.on_release(e.is_io());
|
||||||
return Err(SendRequestError::from(e));
|
return Err(SendRequestError::from(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = match io.send_request(req, eof) {
|
let resp = match io.send_request(req, eof) {
|
||||||
Ok((fut, send)) => {
|
Ok((fut, send)) => {
|
||||||
release(io, pool, created, false);
|
io.on_release(false);
|
||||||
|
|
||||||
if !eof {
|
if !eof {
|
||||||
send_body(body, send).await?;
|
send_body(body, send).await?;
|
||||||
|
@ -111,7 +111,7 @@ where
|
||||||
fut.await.map_err(SendRequestError::from)?
|
fut.await.map_err(SendRequestError::from)?
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
release(io, pool, created, e.is_io());
|
io.on_release(e.is_io());
|
||||||
return Err(e.into());
|
return Err(e.into());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -172,28 +172,10 @@ async fn send_body<B: MessageBody>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// release SendRequest object
|
pub(crate) fn handshake<Io: ConnectionIo>(
|
||||||
fn release<T: AsyncRead + AsyncWrite + Unpin + 'static>(
|
|
||||||
io: H2Connection,
|
|
||||||
pool: Option<Acquired<T>>,
|
|
||||||
created: time::Instant,
|
|
||||||
close: bool,
|
|
||||||
) {
|
|
||||||
if let Some(mut pool) = pool {
|
|
||||||
if close {
|
|
||||||
pool.close(IoConnection::new(ConnectionType::H2(io), created, None));
|
|
||||||
} else {
|
|
||||||
pool.release(IoConnection::new(ConnectionType::H2(io), created, None));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn handshake<Io>(
|
|
||||||
io: Io,
|
io: Io,
|
||||||
config: &ConnectorConfig,
|
config: &ConnectorConfig,
|
||||||
) -> impl Future<Output = Result<(SendRequest<Bytes>, Connection<Io, Bytes>), h2::Error>>
|
) -> impl Future<Output = Result<(SendRequest<Bytes>, Connection<Io, Bytes>), h2::Error>>
|
||||||
where
|
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
{
|
||||||
let mut builder = Builder::new();
|
let mut builder = Builder::new();
|
||||||
builder
|
builder
|
||||||
|
|
|
@ -14,10 +14,10 @@ pub use actix_tls::connect::{
|
||||||
Connect as TcpConnect, ConnectError as TcpConnectError, Connection as TcpConnection,
|
Connect as TcpConnect, ConnectError as TcpConnectError, Connection as TcpConnection,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::connection::Connection;
|
pub use self::connection::{Connection, ConnectionIo};
|
||||||
pub use self::connector::Connector;
|
pub use self::connector::{Connector, ConnectorService};
|
||||||
pub use self::error::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError};
|
pub use self::error::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError};
|
||||||
pub use self::pool::Protocol;
|
pub use crate::Protocol;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Connect {
|
pub struct Connect {
|
||||||
|
|
|
@ -1,40 +1,38 @@
|
||||||
//! Client connection pooling keyed on the authority part of the connection URI.
|
//! Client connection pooling keyed on the authority part of the connection URI.
|
||||||
|
|
||||||
use std::collections::VecDeque;
|
use std::{
|
||||||
use std::future::Future;
|
cell::RefCell,
|
||||||
use std::ops::Deref;
|
collections::VecDeque,
|
||||||
use std::pin::Pin;
|
future::Future,
|
||||||
use std::rc::Rc;
|
io,
|
||||||
use std::sync::Arc;
|
ops::Deref,
|
||||||
use std::task::{Context, Poll};
|
pin::Pin,
|
||||||
use std::time::{Duration, Instant};
|
rc::Rc,
|
||||||
use std::{cell::RefCell, io};
|
sync::Arc,
|
||||||
|
task::{Context, Poll},
|
||||||
|
time::{Duration, Instant},
|
||||||
|
};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite, ReadBuf};
|
||||||
use actix_rt::time::{sleep, Sleep};
|
use actix_rt::time::{sleep, Sleep};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use ahash::AHashMap;
|
use ahash::AHashMap;
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use http::uri::Authority;
|
use http::uri::Authority;
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
use tokio::io::ReadBuf;
|
|
||||||
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
||||||
|
|
||||||
use super::config::ConnectorConfig;
|
use super::config::ConnectorConfig;
|
||||||
use super::connection::{ConnectionType, H2Connection, IoConnection};
|
use super::connection::{
|
||||||
|
ConnectionInnerType, ConnectionIo, ConnectionType, H2ConnectionInner,
|
||||||
|
};
|
||||||
use super::error::ConnectError;
|
use super::error::ConnectError;
|
||||||
use super::h2proto::handshake;
|
use super::h2proto::handshake;
|
||||||
use super::Connect;
|
use super::Connect;
|
||||||
|
use super::Protocol;
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
|
||||||
/// Protocol version
|
|
||||||
pub enum Protocol {
|
|
||||||
Http1,
|
|
||||||
Http2,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
|
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
|
||||||
pub(crate) struct Key {
|
pub struct Key {
|
||||||
authority: Authority,
|
authority: Authority,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,17 +42,18 @@ impl From<Authority> for Key {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
/// Connections pool for reuse Io type for certain [`http::uri::Authority`] as key.
|
/// Connections pool for reuse Io type for certain [`http::uri::Authority`] as key.
|
||||||
pub(crate) struct ConnectionPool<S, Io>
|
pub struct ConnectionPool<S, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncWrite + Unpin + 'static,
|
Io: AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
connector: Rc<S>,
|
connector: S,
|
||||||
inner: ConnectionPoolInner<Io>,
|
inner: ConnectionPoolInner<Io>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// wrapper type for check the ref count of Rc.
|
/// wrapper type for check the ref count of Rc.
|
||||||
struct ConnectionPoolInner<Io>(Rc<ConnectionPoolInnerPriv<Io>>)
|
pub struct ConnectionPoolInner<Io>(Rc<ConnectionPoolInnerPriv<Io>>)
|
||||||
where
|
where
|
||||||
Io: AsyncWrite + Unpin + 'static;
|
Io: AsyncWrite + Unpin + 'static;
|
||||||
|
|
||||||
|
@ -62,10 +61,21 @@ impl<Io> ConnectionPoolInner<Io>
|
||||||
where
|
where
|
||||||
Io: AsyncWrite + Unpin + 'static,
|
Io: AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
|
fn new(config: ConnectorConfig) -> Self {
|
||||||
|
let permits = Arc::new(Semaphore::new(config.limit));
|
||||||
|
let available = RefCell::new(AHashMap::default());
|
||||||
|
|
||||||
|
Self(Rc::new(ConnectionPoolInnerPriv {
|
||||||
|
config,
|
||||||
|
available,
|
||||||
|
permits,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
/// spawn a async for graceful shutdown h1 Io type with a timeout.
|
/// spawn a async for graceful shutdown h1 Io type with a timeout.
|
||||||
fn close(&self, conn: ConnectionType<Io>) {
|
fn close(&self, conn: ConnectionInnerType<Io>) {
|
||||||
if let Some(timeout) = self.config.disconnect_timeout {
|
if let Some(timeout) = self.config.disconnect_timeout {
|
||||||
if let ConnectionType::H1(io) = conn {
|
if let ConnectionInnerType::H1(io) = conn {
|
||||||
actix_rt::spawn(CloseConnection::new(io, timeout));
|
actix_rt::spawn(CloseConnection::new(io, timeout));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,7 +120,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ConnectionPoolInnerPriv<Io>
|
pub struct ConnectionPoolInnerPriv<Io>
|
||||||
where
|
where
|
||||||
Io: AsyncWrite + Unpin + 'static,
|
Io: AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
|
@ -134,40 +144,22 @@ where
|
||||||
/// Any requests beyond limit would be wait in fifo order and get notified in async manner
|
/// Any requests beyond limit would be wait in fifo order and get notified in async manner
|
||||||
/// by [`tokio::sync::Semaphore`]
|
/// by [`tokio::sync::Semaphore`]
|
||||||
pub(crate) fn new(connector: S, config: ConnectorConfig) -> Self {
|
pub(crate) fn new(connector: S, config: ConnectorConfig) -> Self {
|
||||||
let permits = Arc::new(Semaphore::new(config.limit));
|
let inner = ConnectionPoolInner::new(config);
|
||||||
let available = RefCell::new(AHashMap::default());
|
|
||||||
let connector = Rc::new(connector);
|
|
||||||
|
|
||||||
let inner = ConnectionPoolInner(Rc::new(ConnectionPoolInnerPriv {
|
|
||||||
config,
|
|
||||||
available,
|
|
||||||
permits,
|
|
||||||
}));
|
|
||||||
|
|
||||||
Self { connector, inner }
|
Self { connector, inner }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, Io> Clone for ConnectionPool<S, Io>
|
|
||||||
where
|
|
||||||
Io: AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
connector: self.connector.clone(),
|
|
||||||
inner: self.inner.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, Io> Service<Connect> for ConnectionPool<S, Io>
|
impl<S, Io> Service<Connect> for ConnectionPool<S, Io>
|
||||||
where
|
where
|
||||||
S: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
S: Service<Connect, Response = (Io, Protocol), Error = ConnectError>
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
+ Clone
|
||||||
|
+ 'static,
|
||||||
|
Io: ConnectionIo,
|
||||||
{
|
{
|
||||||
type Response = IoConnection<Io>;
|
type Response = ConnectionType<Io>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = LocalBoxFuture<'static, Result<IoConnection<Io>, ConnectError>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
actix_service::forward_ready!(connector);
|
actix_service::forward_ready!(connector);
|
||||||
|
|
||||||
|
@ -211,7 +203,7 @@ where
|
||||||
inner.close(c.conn);
|
inner.close(c.conn);
|
||||||
} else {
|
} else {
|
||||||
// check if the connection is still usable
|
// check if the connection is still usable
|
||||||
if let ConnectionType::H1(ref mut io) = c.conn {
|
if let ConnectionInnerType::H1(ref mut io) = c.conn {
|
||||||
let check = ConnectionCheckFuture { io };
|
let check = ConnectionCheckFuture { io };
|
||||||
match check.await {
|
match check.await {
|
||||||
ConnectionState::Tainted => {
|
ConnectionState::Tainted => {
|
||||||
|
@ -235,28 +227,26 @@ where
|
||||||
|
|
||||||
// construct acquired. It's used to put Io type back to pool/ close the Io type.
|
// construct acquired. It's used to put Io type back to pool/ close the Io type.
|
||||||
// permit is carried with the whole lifecycle of Acquired.
|
// permit is carried with the whole lifecycle of Acquired.
|
||||||
let acquired = Some(Acquired { key, inner, permit });
|
let acquired = Acquired { key, inner, permit };
|
||||||
|
|
||||||
// match the connection and spawn new one if did not get anything.
|
// match the connection and spawn new one if did not get anything.
|
||||||
match conn {
|
match conn {
|
||||||
Some(conn) => Ok(IoConnection::new(conn.conn, conn.created, acquired)),
|
Some(conn) => {
|
||||||
|
Ok(ConnectionType::from_pool(conn.conn, conn.created, acquired))
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
let (io, proto) = connector.call(req).await?;
|
let (io, proto) = connector.call(req).await?;
|
||||||
|
|
||||||
|
// TODO: remove when http3 is added in support.
|
||||||
|
assert!(proto != Protocol::Http3);
|
||||||
|
|
||||||
if proto == Protocol::Http1 {
|
if proto == Protocol::Http1 {
|
||||||
Ok(IoConnection::new(
|
Ok(ConnectionType::from_h1(io, Instant::now(), acquired))
|
||||||
ConnectionType::H1(io),
|
|
||||||
Instant::now(),
|
|
||||||
acquired,
|
|
||||||
))
|
|
||||||
} else {
|
} else {
|
||||||
let config = &acquired.as_ref().unwrap().inner.config;
|
let config = &acquired.inner.config;
|
||||||
let (sender, connection) = handshake(io, config).await?;
|
let (sender, connection) = handshake(io, config).await?;
|
||||||
Ok(IoConnection::new(
|
let inner = H2ConnectionInner::new(sender, connection);
|
||||||
ConnectionType::H2(H2Connection::new(sender, connection)),
|
Ok(ConnectionType::from_h2(inner, Instant::now(), acquired))
|
||||||
Instant::now(),
|
|
||||||
acquired,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,7 +297,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PooledConnection<Io> {
|
struct PooledConnection<Io> {
|
||||||
conn: ConnectionType<Io>,
|
conn: ConnectionInnerType<Io>,
|
||||||
used: Instant,
|
used: Instant,
|
||||||
created: Instant,
|
created: Instant,
|
||||||
}
|
}
|
||||||
|
@ -347,28 +337,26 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Acquired<Io>
|
pub struct Acquired<Io>
|
||||||
where
|
where
|
||||||
Io: AsyncWrite + Unpin + 'static,
|
Io: AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
|
/// authority key for identify connection.
|
||||||
key: Key,
|
key: Key,
|
||||||
|
/// handle to connection pool.
|
||||||
inner: ConnectionPoolInner<Io>,
|
inner: ConnectionPoolInner<Io>,
|
||||||
|
/// permit for limit concurrent in-flight connection for a Client object.
|
||||||
permit: OwnedSemaphorePermit,
|
permit: OwnedSemaphorePermit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io> Acquired<Io>
|
impl<Io: ConnectionIo> Acquired<Io> {
|
||||||
where
|
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
{
|
|
||||||
/// Close the IO.
|
/// Close the IO.
|
||||||
pub(crate) fn close(&mut self, conn: IoConnection<Io>) {
|
pub(super) fn close(&self, conn: ConnectionInnerType<Io>) {
|
||||||
let (conn, _) = conn.into_inner();
|
|
||||||
self.inner.close(conn);
|
self.inner.close(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Release IO back into pool.
|
/// Release IO back into pool.
|
||||||
pub(crate) fn release(&mut self, conn: IoConnection<Io>) {
|
pub(super) fn release(&self, conn: ConnectionInnerType<Io>, created: Instant) {
|
||||||
let (io, created) = conn.into_inner();
|
|
||||||
let Acquired { key, inner, .. } = self;
|
let Acquired { key, inner, .. } = self;
|
||||||
|
|
||||||
inner
|
inner
|
||||||
|
@ -377,12 +365,12 @@ where
|
||||||
.entry(key.clone())
|
.entry(key.clone())
|
||||||
.or_insert_with(VecDeque::new)
|
.or_insert_with(VecDeque::new)
|
||||||
.push_back(PooledConnection {
|
.push_back(PooledConnection {
|
||||||
conn: io,
|
conn,
|
||||||
created,
|
created,
|
||||||
used: Instant::now(),
|
used: Instant::now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let _ = &mut self.permit;
|
let _ = &self.permit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,7 +381,7 @@ mod test {
|
||||||
use http::Uri;
|
use http::Uri;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::client::connection::IoConnection;
|
use crate::client::connection::ConnectionType;
|
||||||
|
|
||||||
/// A stream type that always returns pending on async read.
|
/// A stream type that always returns pending on async read.
|
||||||
///
|
///
|
||||||
|
@ -440,6 +428,7 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct TestPoolConnector {
|
struct TestPoolConnector {
|
||||||
generated: Rc<Cell<usize>>,
|
generated: Rc<Cell<usize>>,
|
||||||
}
|
}
|
||||||
|
@ -458,12 +447,14 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn release<T>(conn: IoConnection<T>)
|
fn release<T>(conn: ConnectionType<T>)
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
let (conn, created, mut acquired) = conn.into_parts();
|
match conn {
|
||||||
acquired.release(IoConnection::new(conn, created, None));
|
ConnectionType::H1(mut conn) => conn.on_release(true),
|
||||||
|
ConnectionType::H2(mut conn) => conn.on_release(false),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
|
|
@ -126,9 +126,7 @@ impl ServiceConfig {
|
||||||
pub fn client_timer(&self) -> Option<Sleep> {
|
pub fn client_timer(&self) -> Option<Sleep> {
|
||||||
let delay_time = self.0.client_timeout;
|
let delay_time = self.0.client_timeout;
|
||||||
if delay_time != 0 {
|
if delay_time != 0 {
|
||||||
Some(sleep_until(
|
Some(sleep_until(self.now() + Duration::from_millis(delay_time)))
|
||||||
self.0.date_service.now() + Duration::from_millis(delay_time),
|
|
||||||
))
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -138,7 +136,7 @@ impl ServiceConfig {
|
||||||
pub fn client_timer_expire(&self) -> Option<Instant> {
|
pub fn client_timer_expire(&self) -> Option<Instant> {
|
||||||
let delay = self.0.client_timeout;
|
let delay = self.0.client_timeout;
|
||||||
if delay != 0 {
|
if delay != 0 {
|
||||||
Some(self.0.date_service.now() + Duration::from_millis(delay))
|
Some(self.now() + Duration::from_millis(delay))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -148,7 +146,7 @@ impl ServiceConfig {
|
||||||
pub fn client_disconnect_timer(&self) -> Option<Instant> {
|
pub fn client_disconnect_timer(&self) -> Option<Instant> {
|
||||||
let delay = self.0.client_disconnect;
|
let delay = self.0.client_disconnect;
|
||||||
if delay != 0 {
|
if delay != 0 {
|
||||||
Some(self.0.date_service.now() + Duration::from_millis(delay))
|
Some(self.now() + Duration::from_millis(delay))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -157,20 +155,12 @@ impl ServiceConfig {
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Return keep-alive timer delay is configured.
|
/// Return keep-alive timer delay is configured.
|
||||||
pub fn keep_alive_timer(&self) -> Option<Sleep> {
|
pub fn keep_alive_timer(&self) -> Option<Sleep> {
|
||||||
if let Some(ka) = self.0.keep_alive {
|
self.keep_alive().map(|ka| sleep_until(self.now() + ka))
|
||||||
Some(sleep_until(self.0.date_service.now() + ka))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Keep-alive expire time
|
/// Keep-alive expire time
|
||||||
pub fn keep_alive_expire(&self) -> Option<Instant> {
|
pub fn keep_alive_expire(&self) -> Option<Instant> {
|
||||||
if let Some(ka) = self.0.keep_alive {
|
self.keep_alive().map(|ka| self.now() + ka)
|
||||||
Some(self.0.date_service.now() + ka)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -54,7 +54,7 @@ impl Error {
|
||||||
|
|
||||||
/// Similar to `as_response_error` but downcasts.
|
/// Similar to `as_response_error` but downcasts.
|
||||||
pub fn as_error<T: ResponseError + 'static>(&self) -> Option<&T> {
|
pub fn as_error<T: ResponseError + 'static>(&self) -> Option<&T> {
|
||||||
ResponseError::downcast_ref(self.cause.as_ref())
|
<dyn ResponseError>::downcast_ref(self.cause.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -483,7 +483,7 @@ where
|
||||||
/// response as opposite to *INTERNAL SERVER ERROR* which is defined by
|
/// response as opposite to *INTERNAL SERVER ERROR* which is defined by
|
||||||
/// default.
|
/// default.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// # use std::io;
|
/// # use std::io;
|
||||||
/// # use actix_http::*;
|
/// # use actix_http::*;
|
||||||
///
|
///
|
||||||
|
@ -983,6 +983,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cookie_parse() {
|
fn test_cookie_parse() {
|
||||||
let resp: Response = CookieParseError::EmptyName.error_response();
|
let resp: Response = CookieParseError::EmptyName.error_response();
|
||||||
|
|
|
@ -951,14 +951,15 @@ mod tests {
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use actix_service::fn_service;
|
use actix_service::fn_service;
|
||||||
use futures_util::future::{lazy, ready};
|
use futures_util::future::{lazy, ready, Ready};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::TestBuffer;
|
|
||||||
use crate::{error::Error, KeepAlive};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
error::Error,
|
||||||
h1::{ExpectHandler, UpgradeHandler},
|
h1::{ExpectHandler, UpgradeHandler},
|
||||||
test::TestSeqBuffer,
|
http::Method,
|
||||||
|
test::{TestBuffer, TestSeqBuffer},
|
||||||
|
HttpMessage, KeepAlive,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn find_slice(haystack: &[u8], needle: &[u8], from: usize) -> Option<usize> {
|
fn find_slice(haystack: &[u8], needle: &[u8], from: usize) -> Option<usize> {
|
||||||
|
@ -1282,14 +1283,30 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_upgrade() {
|
async fn test_upgrade() {
|
||||||
|
struct TestUpgrade;
|
||||||
|
|
||||||
|
impl<T> Service<(Request, Framed<T, Codec>)> for TestUpgrade {
|
||||||
|
type Response = ();
|
||||||
|
type Error = Error;
|
||||||
|
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
|
actix_service::always_ready!();
|
||||||
|
|
||||||
|
fn call(&self, (req, _framed): (Request, Framed<T, Codec>)) -> Self::Future {
|
||||||
|
assert_eq!(req.method(), Method::GET);
|
||||||
|
assert!(req.upgrade());
|
||||||
|
assert_eq!(req.headers().get("upgrade").unwrap(), "websocket");
|
||||||
|
ready(Ok(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lazy(|cx| {
|
lazy(|cx| {
|
||||||
let mut buf = TestSeqBuffer::empty();
|
let mut buf = TestSeqBuffer::empty();
|
||||||
let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None);
|
let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None);
|
||||||
|
|
||||||
let services =
|
let services = HttpFlow::new(ok_service(), ExpectHandler, Some(TestUpgrade));
|
||||||
HttpFlow::new(ok_service(), ExpectHandler, Some(UpgradeHandler));
|
|
||||||
|
|
||||||
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
|
let h1 = Dispatcher::<_, _, _, _, TestUpgrade>::new(
|
||||||
buf.clone(),
|
buf.clone(),
|
||||||
cfg,
|
cfg,
|
||||||
services,
|
services,
|
||||||
|
|
|
@ -632,10 +632,9 @@ mod tests {
|
||||||
|
|
||||||
let mut res: Response<()> =
|
let mut res: Response<()> =
|
||||||
Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>();
|
Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>();
|
||||||
|
res.headers_mut().insert(DATE, HeaderValue::from_static(""));
|
||||||
res.headers_mut()
|
res.headers_mut()
|
||||||
.insert(DATE, HeaderValue::from_static(&""));
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
||||||
res.headers_mut()
|
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static(&"0"));
|
|
||||||
|
|
||||||
let _ = res.encode_headers(
|
let _ = res.encode_headers(
|
||||||
&mut bytes,
|
&mut bytes,
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use std::task::Poll;
|
|
||||||
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures_util::future::{ready, Ready};
|
use futures_util::future::{ready, Ready};
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,8 @@ use std::cell::RefCell;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::rc::{Rc, Weak};
|
use std::rc::{Rc, Weak};
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll, Waker};
|
||||||
|
|
||||||
use actix_utils::task::LocalWaker;
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
|
|
||||||
|
@ -134,7 +133,7 @@ impl PayloadSender {
|
||||||
if shared.borrow().need_read {
|
if shared.borrow().need_read {
|
||||||
PayloadStatus::Read
|
PayloadStatus::Read
|
||||||
} else {
|
} else {
|
||||||
shared.borrow_mut().io_task.register(cx.waker());
|
shared.borrow_mut().register_io(cx);
|
||||||
PayloadStatus::Pause
|
PayloadStatus::Pause
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -150,8 +149,8 @@ struct Inner {
|
||||||
err: Option<PayloadError>,
|
err: Option<PayloadError>,
|
||||||
need_read: bool,
|
need_read: bool,
|
||||||
items: VecDeque<Bytes>,
|
items: VecDeque<Bytes>,
|
||||||
task: LocalWaker,
|
task: Option<Waker>,
|
||||||
io_task: LocalWaker,
|
io_task: Option<Waker>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Inner {
|
impl Inner {
|
||||||
|
@ -162,8 +161,48 @@ impl Inner {
|
||||||
err: None,
|
err: None,
|
||||||
items: VecDeque::new(),
|
items: VecDeque::new(),
|
||||||
need_read: true,
|
need_read: true,
|
||||||
task: LocalWaker::new(),
|
task: None,
|
||||||
io_task: LocalWaker::new(),
|
io_task: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wake up future waiting for payload data to be available.
|
||||||
|
fn wake(&mut self) {
|
||||||
|
if let Some(waker) = self.task.take() {
|
||||||
|
waker.wake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wake up future feeding data to Payload.
|
||||||
|
fn wake_io(&mut self) {
|
||||||
|
if let Some(waker) = self.io_task.take() {
|
||||||
|
waker.wake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register future waiting data from payload.
|
||||||
|
/// Waker would be used in `Inner::wake`
|
||||||
|
fn register(&mut self, cx: &mut Context<'_>) {
|
||||||
|
if self
|
||||||
|
.task
|
||||||
|
.as_ref()
|
||||||
|
.map(|w| !cx.waker().will_wake(w))
|
||||||
|
.unwrap_or(true)
|
||||||
|
{
|
||||||
|
self.task = Some(cx.waker().clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register future feeding data to payload.
|
||||||
|
/// Waker would be used in `Inner::wake_io`
|
||||||
|
fn register_io(&mut self, cx: &mut Context<'_>) {
|
||||||
|
if self
|
||||||
|
.io_task
|
||||||
|
.as_ref()
|
||||||
|
.map(|w| !cx.waker().will_wake(w))
|
||||||
|
.unwrap_or(true)
|
||||||
|
{
|
||||||
|
self.io_task = Some(cx.waker().clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +221,7 @@ impl Inner {
|
||||||
self.len += data.len();
|
self.len += data.len();
|
||||||
self.items.push_back(data);
|
self.items.push_back(data);
|
||||||
self.need_read = self.len < MAX_BUFFER_SIZE;
|
self.need_read = self.len < MAX_BUFFER_SIZE;
|
||||||
self.task.wake();
|
self.wake();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -199,9 +238,9 @@ impl Inner {
|
||||||
self.need_read = self.len < MAX_BUFFER_SIZE;
|
self.need_read = self.len < MAX_BUFFER_SIZE;
|
||||||
|
|
||||||
if self.need_read && !self.eof {
|
if self.need_read && !self.eof {
|
||||||
self.task.register(cx.waker());
|
self.register(cx);
|
||||||
}
|
}
|
||||||
self.io_task.wake();
|
self.wake_io();
|
||||||
Poll::Ready(Some(Ok(data)))
|
Poll::Ready(Some(Ok(data)))
|
||||||
} else if let Some(err) = self.err.take() {
|
} else if let Some(err) = self.err.take() {
|
||||||
Poll::Ready(Some(Err(err)))
|
Poll::Ready(Some(Err(err)))
|
||||||
|
@ -209,8 +248,8 @@ impl Inner {
|
||||||
Poll::Ready(None)
|
Poll::Ready(None)
|
||||||
} else {
|
} else {
|
||||||
self.need_read = true;
|
self.need_read = true;
|
||||||
self.task.register(cx.waker());
|
self.register(cx);
|
||||||
self.io_task.wake();
|
self.wake_io();
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,10 +94,10 @@ mod openssl {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use actix_service::ServiceFactoryExt;
|
use actix_service::ServiceFactoryExt;
|
||||||
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, SslStream};
|
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, TlsStream};
|
||||||
use actix_tls::accept::TlsError;
|
use actix_tls::accept::TlsError;
|
||||||
|
|
||||||
impl<S, B, X, U> H1Service<SslStream<TcpStream>, S, B, X, U>
|
impl<S, B, X, U> H1Service<TlsStream<TcpStream>, S, B, X, U>
|
||||||
where
|
where
|
||||||
S: ServiceFactory<Request, Config = ()>,
|
S: ServiceFactory<Request, Config = ()>,
|
||||||
S::Error: Into<Error>,
|
S::Error: Into<Error>,
|
||||||
|
@ -108,7 +108,7 @@ mod openssl {
|
||||||
X::Error: Into<Error>,
|
X::Error: Into<Error>,
|
||||||
X::InitError: fmt::Debug,
|
X::InitError: fmt::Debug,
|
||||||
U: ServiceFactory<
|
U: ServiceFactory<
|
||||||
(Request, Framed<SslStream<TcpStream>, Codec>),
|
(Request, Framed<TlsStream<TcpStream>, Codec>),
|
||||||
Config = (),
|
Config = (),
|
||||||
Response = (),
|
Response = (),
|
||||||
>,
|
>,
|
||||||
|
@ -131,7 +131,7 @@ mod openssl {
|
||||||
.map_err(TlsError::Tls)
|
.map_err(TlsError::Tls)
|
||||||
.map_init_err(|_| panic!()),
|
.map_init_err(|_| panic!()),
|
||||||
)
|
)
|
||||||
.and_then(|io: SslStream<TcpStream>| {
|
.and_then(|io: TlsStream<TcpStream>| {
|
||||||
let peer_addr = io.get_ref().peer_addr().ok();
|
let peer_addr = io.get_ref().peer_addr().ok();
|
||||||
ready(Ok((io, peer_addr)))
|
ready(Ok((io, peer_addr)))
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use std::task::Poll;
|
|
||||||
|
|
||||||
use actix_codec::Framed;
|
use actix_codec::Framed;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures_util::future::{ready, Ready};
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::h1::Codec;
|
use crate::h1::Codec;
|
||||||
|
@ -16,7 +14,7 @@ impl<T> ServiceFactory<(Request, Framed<T, Codec>)> for UpgradeHandler {
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = UpgradeHandler;
|
type Service = UpgradeHandler;
|
||||||
type InitError = Error;
|
type InitError = Error;
|
||||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: ()) -> Self::Future {
|
fn new_service(&self, _: ()) -> Self::Future {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
|
@ -26,11 +24,11 @@ impl<T> ServiceFactory<(Request, Framed<T, Codec>)> for UpgradeHandler {
|
||||||
impl<T> Service<(Request, Framed<T, Codec>)> for UpgradeHandler {
|
impl<T> Service<(Request, Framed<T, Codec>)> for UpgradeHandler {
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
actix_service::always_ready!();
|
actix_service::always_ready!();
|
||||||
|
|
||||||
fn call(&self, _: (Request, Framed<T, Codec>)) -> Self::Future {
|
fn call(&self, _: (Request, Framed<T, Codec>)) -> Self::Future {
|
||||||
ready(Ok(()))
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,14 @@
|
||||||
use std::future::Future;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::net;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::{cmp, convert::TryFrom};
|
use std::{cmp, future::Future, marker::PhantomData, net, pin::Pin, rc::Rc};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_rt::time::{Instant, Sleep};
|
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use h2::server::{Connection, SendResponse};
|
use h2::{
|
||||||
use h2::SendStream;
|
server::{Connection, SendResponse},
|
||||||
|
SendStream,
|
||||||
|
};
|
||||||
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
||||||
use log::{error, trace};
|
use log::{error, trace};
|
||||||
|
|
||||||
|
@ -41,8 +37,6 @@ where
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
ka_expire: Instant,
|
|
||||||
ka_timer: Option<Sleep>,
|
|
||||||
_phantom: PhantomData<B>,
|
_phantom: PhantomData<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,33 +53,14 @@ where
|
||||||
connection: Connection<T, Bytes>,
|
connection: Connection<T, Bytes>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
timeout: Option<Sleep>,
|
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// let keepalive = config.keep_alive_enabled();
|
|
||||||
// let flags = if keepalive {
|
|
||||||
// Flags::KEEPALIVE | Flags::KEEPALIVE_ENABLED
|
|
||||||
// } else {
|
|
||||||
// Flags::empty()
|
|
||||||
// };
|
|
||||||
|
|
||||||
// keep-alive timer
|
|
||||||
let (ka_expire, ka_timer) = if let Some(delay) = timeout {
|
|
||||||
(delay.deadline(), Some(delay))
|
|
||||||
} else if let Some(delay) = config.keep_alive_timer() {
|
|
||||||
(delay.deadline(), Some(delay))
|
|
||||||
} else {
|
|
||||||
(config.now(), None)
|
|
||||||
};
|
|
||||||
|
|
||||||
Dispatcher {
|
Dispatcher {
|
||||||
flow,
|
flow,
|
||||||
config,
|
config,
|
||||||
peer_addr,
|
peer_addr,
|
||||||
connection,
|
connection,
|
||||||
on_connect_data,
|
on_connect_data,
|
||||||
ka_expire,
|
|
||||||
ka_timer,
|
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,19 +88,12 @@ where
|
||||||
Some(Err(err)) => return Poll::Ready(Err(err.into())),
|
Some(Err(err)) => return Poll::Ready(Err(err.into())),
|
||||||
|
|
||||||
Some(Ok((req, res))) => {
|
Some(Ok((req, res))) => {
|
||||||
// update keep-alive expire
|
|
||||||
if this.ka_timer.is_some() {
|
|
||||||
if let Some(expire) = this.config.keep_alive_expire() {
|
|
||||||
this.ka_expire = expire;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let (parts, body) = req.into_parts();
|
let (parts, body) = req.into_parts();
|
||||||
let pl = crate::h2::Payload::new(body);
|
let pl = crate::h2::Payload::new(body);
|
||||||
let pl = Payload::<crate::payload::PayloadStream>::H2(pl);
|
let pl = Payload::<crate::payload::PayloadStream>::H2(pl);
|
||||||
let mut req = Request::with_payload(pl);
|
let mut req = Request::with_payload(pl);
|
||||||
|
|
||||||
let head = &mut req.head_mut();
|
let head = req.head_mut();
|
||||||
head.uri = parts.uri;
|
head.uri = parts.uri;
|
||||||
head.method = parts.method;
|
head.method = parts.method;
|
||||||
head.version = parts.version;
|
head.version = parts.version;
|
||||||
|
@ -135,7 +103,7 @@ where
|
||||||
// merge on_connect_ext data into request extensions
|
// merge on_connect_ext data into request extensions
|
||||||
this.on_connect_data.merge_into(&mut req);
|
this.on_connect_data.merge_into(&mut req);
|
||||||
|
|
||||||
let svc = ServiceResponse::<S::Future, S::Response, S::Error, B> {
|
let svc = ServiceResponse {
|
||||||
state: ServiceResponseState::ServiceCall(
|
state: ServiceResponseState::ServiceCall(
|
||||||
this.flow.service.call(req),
|
this.flow.service.call(req),
|
||||||
Some(res),
|
Some(res),
|
||||||
|
@ -203,16 +171,22 @@ where
|
||||||
BodySize::Empty => res
|
BodySize::Empty => res
|
||||||
.headers_mut()
|
.headers_mut()
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
||||||
BodySize::Sized(len) => res.headers_mut().insert(
|
BodySize::Sized(len) => {
|
||||||
|
let mut buf = itoa::Buffer::new();
|
||||||
|
|
||||||
|
res.headers_mut().insert(
|
||||||
CONTENT_LENGTH,
|
CONTENT_LENGTH,
|
||||||
HeaderValue::try_from(format!("{}", len)).unwrap(),
|
HeaderValue::from_str(buf.format(*len)).unwrap(),
|
||||||
),
|
)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// copy headers
|
// copy headers
|
||||||
for (key, value) in head.headers.iter() {
|
for (key, value) in head.headers.iter() {
|
||||||
match *key {
|
match *key {
|
||||||
// omit HTTP/1 only headers
|
// TODO: consider skipping other headers according to:
|
||||||
|
// https://tools.ietf.org/html/rfc7540#section-8.1.2.2
|
||||||
|
// omit HTTP/1.x only headers
|
||||||
CONNECTION | TRANSFER_ENCODING => continue,
|
CONNECTION | TRANSFER_ENCODING => continue,
|
||||||
CONTENT_LENGTH if skip_len => continue,
|
CONTENT_LENGTH if skip_len => continue,
|
||||||
DATE => has_date = true,
|
DATE => has_date = true,
|
||||||
|
@ -310,11 +284,9 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceResponseStateProj::SendPayload(ref mut stream, ref mut body) => {
|
ServiceResponseStateProj::SendPayload(ref mut stream, ref mut body) => {
|
||||||
loop {
|
|
||||||
loop {
|
loop {
|
||||||
match this.buffer {
|
match this.buffer {
|
||||||
Some(ref mut buffer) => {
|
Some(ref mut buffer) => match ready!(stream.poll_capacity(cx)) {
|
||||||
match ready!(stream.poll_capacity(cx)) {
|
|
||||||
None => return Poll::Ready(()),
|
None => return Poll::Ready(()),
|
||||||
|
|
||||||
Some(Ok(cap)) => {
|
Some(Ok(cap)) => {
|
||||||
|
@ -336,23 +308,19 @@ where
|
||||||
warn!("{:?}", e);
|
warn!("{:?}", e);
|
||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
|
|
||||||
None => match ready!(body.as_mut().poll_next(cx)) {
|
None => match ready!(body.as_mut().poll_next(cx)) {
|
||||||
None => {
|
None => {
|
||||||
if let Err(e) = stream.send_data(Bytes::new(), true)
|
if let Err(e) = stream.send_data(Bytes::new(), true) {
|
||||||
{
|
|
||||||
warn!("{:?}", e);
|
warn!("{:?}", e);
|
||||||
}
|
}
|
||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(Ok(chunk)) => {
|
Some(Ok(chunk)) => {
|
||||||
stream.reserve_capacity(cmp::min(
|
stream
|
||||||
chunk.len(),
|
.reserve_capacity(cmp::min(chunk.len(), CHUNK_SIZE));
|
||||||
CHUNK_SIZE,
|
|
||||||
));
|
|
||||||
*this.buffer = Some(chunk);
|
*this.buffer = Some(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,5 +334,4 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ use actix_service::{
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use futures_util::future::ok;
|
use futures_util::future::ok;
|
||||||
use h2::server::{self, Handshake};
|
use h2::server::{handshake, Handshake};
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
use crate::body::MessageBody;
|
use crate::body::MessageBody;
|
||||||
|
@ -93,12 +93,12 @@ where
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
mod openssl {
|
mod openssl {
|
||||||
use actix_service::{fn_factory, fn_service, ServiceFactoryExt};
|
use actix_service::{fn_factory, fn_service, ServiceFactoryExt};
|
||||||
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, SslStream};
|
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, TlsStream};
|
||||||
use actix_tls::accept::TlsError;
|
use actix_tls::accept::TlsError;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
impl<S, B> H2Service<SslStream<TcpStream>, S, B>
|
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
|
||||||
where
|
where
|
||||||
S: ServiceFactory<Request, Config = ()>,
|
S: ServiceFactory<Request, Config = ()>,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
|
@ -123,7 +123,7 @@ mod openssl {
|
||||||
.map_init_err(|_| panic!()),
|
.map_init_err(|_| panic!()),
|
||||||
)
|
)
|
||||||
.and_then(fn_factory(|| {
|
.and_then(fn_factory(|| {
|
||||||
ok::<_, S::InitError>(fn_service(|io: SslStream<TcpStream>| {
|
ok::<_, S::InitError>(fn_service(|io: TlsStream<TcpStream>| {
|
||||||
let peer_addr = io.get_ref().peer_addr().ok();
|
let peer_addr = io.get_ref().peer_addr().ok();
|
||||||
ok((io, peer_addr))
|
ok((io, peer_addr))
|
||||||
}))
|
}))
|
||||||
|
@ -307,7 +307,7 @@ where
|
||||||
Some(self.cfg.clone()),
|
Some(self.cfg.clone()),
|
||||||
addr,
|
addr,
|
||||||
on_connect_data,
|
on_connect_data,
|
||||||
server::handshake(io),
|
handshake(io),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -368,7 +368,6 @@ where
|
||||||
conn,
|
conn,
|
||||||
on_connect_data,
|
on_connect_data,
|
||||||
config.take().unwrap(),
|
config.take().unwrap(),
|
||||||
None,
|
|
||||||
*peer_addr,
|
*peer_addr,
|
||||||
));
|
));
|
||||||
self.poll(cx)
|
self.poll(cx)
|
||||||
|
|
|
@ -36,7 +36,7 @@ use crate::header::{
|
||||||
/// builder.insert_header(CacheControl(vec![CacheDirective::MaxAge(86400u32)]));
|
/// builder.insert_header(CacheControl(vec![CacheDirective::MaxAge(86400u32)]));
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_http::Response;
|
/// use actix_http::Response;
|
||||||
/// use actix_http::http::header::{CacheControl, CacheDirective};
|
/// use actix_http::http::header::{CacheControl, CacheDirective};
|
||||||
///
|
///
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
//! Browser conformance tests at: http://greenbytes.de/tech/tc2231/
|
//! Browser conformance tests at: http://greenbytes.de/tech/tc2231/
|
||||||
//! IANA assignment: http://www.iana.org/assignments/cont-disp/cont-disp.xhtml
|
//! IANA assignment: http://www.iana.org/assignments/cont-disp/cont-disp.xhtml
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
|
|
||||||
|
@ -520,9 +520,7 @@ impl fmt::Display for DispositionParam {
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// See also comments in test_from_raw_unnecessary_percent_decode.
|
// See also comments in test_from_raw_unnecessary_percent_decode.
|
||||||
lazy_static! {
|
static RE: Lazy<Regex> = Lazy::new(|| Regex::new("[\x00-\x08\x10-\x1F\x7F\"\\\\]").unwrap());
|
||||||
static ref RE: Regex = Regex::new("[\x00-\x08\x10-\x1F\x7F\"\\\\]").unwrap();
|
|
||||||
}
|
|
||||||
match self {
|
match self {
|
||||||
DispositionParam::Name(ref value) => write!(f, "name={}", value),
|
DispositionParam::Name(ref value) => write!(f, "name={}", value),
|
||||||
DispositionParam::Filename(ref value) => {
|
DispositionParam::Filename(ref value) => {
|
||||||
|
|
|
@ -127,9 +127,8 @@ impl Display for EntityTag {
|
||||||
impl FromStr for EntityTag {
|
impl FromStr for EntityTag {
|
||||||
type Err = crate::error::ParseError;
|
type Err = crate::error::ParseError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<EntityTag, crate::error::ParseError> {
|
fn from_str(slice: &str) -> Result<EntityTag, crate::error::ParseError> {
|
||||||
let length: usize = s.len();
|
let length = slice.len();
|
||||||
let slice = &s[..];
|
|
||||||
// Early exits if it doesn't terminate in a DQUOTE.
|
// Early exits if it doesn't terminate in a DQUOTE.
|
||||||
if !slice.ends_with('"') || slice.len() < 2 {
|
if !slice.ends_with('"') || slice.len() < 2 {
|
||||||
return Err(crate::error::ParseError::Header);
|
return Err(crate::error::ParseError::Header);
|
||||||
|
|
|
@ -88,9 +88,9 @@ pub fn parse_extended_value(
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(ExtendedValue {
|
Ok(ExtendedValue {
|
||||||
value,
|
|
||||||
charset,
|
charset,
|
||||||
language_tag,
|
language_tag,
|
||||||
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use bytes::{BufMut, BytesMut};
|
use bytes::BufMut;
|
||||||
use http::Version;
|
use http::Version;
|
||||||
|
|
||||||
const DIGITS_START: u8 = b'0';
|
const DIGITS_START: u8 = b'0';
|
||||||
|
|
||||||
pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut) {
|
pub(crate) fn write_status_line<B: BufMut>(version: Version, n: u16, buf: &mut B) {
|
||||||
match version {
|
match version {
|
||||||
Version::HTTP_11 => bytes.put_slice(b"HTTP/1.1 "),
|
Version::HTTP_11 => buf.put_slice(b"HTTP/1.1 "),
|
||||||
Version::HTTP_10 => bytes.put_slice(b"HTTP/1.0 "),
|
Version::HTTP_10 => buf.put_slice(b"HTTP/1.0 "),
|
||||||
Version::HTTP_09 => bytes.put_slice(b"HTTP/0.9 "),
|
Version::HTTP_09 => buf.put_slice(b"HTTP/0.9 "),
|
||||||
_ => {
|
_ => {
|
||||||
// other HTTP version handlers do not use this method
|
// other HTTP version handlers do not use this method
|
||||||
}
|
}
|
||||||
|
@ -19,33 +19,36 @@ pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut)
|
||||||
let d10 = ((n / 10) % 10) as u8;
|
let d10 = ((n / 10) % 10) as u8;
|
||||||
let d1 = (n % 10) as u8;
|
let d1 = (n % 10) as u8;
|
||||||
|
|
||||||
bytes.put_u8(DIGITS_START + d100);
|
buf.put_u8(DIGITS_START + d100);
|
||||||
bytes.put_u8(DIGITS_START + d10);
|
buf.put_u8(DIGITS_START + d10);
|
||||||
bytes.put_u8(DIGITS_START + d1);
|
buf.put_u8(DIGITS_START + d1);
|
||||||
|
|
||||||
// trailing space before reason
|
// trailing space before reason
|
||||||
bytes.put_u8(b' ');
|
buf.put_u8(b' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// NOTE: bytes object has to contain enough space
|
/// NOTE: bytes object has to contain enough space
|
||||||
pub fn write_content_length(n: u64, bytes: &mut BytesMut) {
|
pub fn write_content_length<B: BufMut>(n: u64, buf: &mut B) {
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
bytes.put_slice(b"\r\ncontent-length: 0\r\n");
|
buf.put_slice(b"\r\ncontent-length: 0\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut buf = itoa::Buffer::new();
|
let mut buffer = itoa::Buffer::new();
|
||||||
|
|
||||||
bytes.put_slice(b"\r\ncontent-length: ");
|
buf.put_slice(b"\r\ncontent-length: ");
|
||||||
bytes.put_slice(buf.format(n).as_bytes());
|
buf.put_slice(buffer.format(n).as_bytes());
|
||||||
bytes.put_slice(b"\r\n");
|
buf.put_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Writer<'a>(pub &'a mut BytesMut);
|
pub(crate) struct Writer<'a, B>(pub &'a mut B);
|
||||||
|
|
||||||
impl<'a> io::Write for Writer<'a> {
|
impl<'a, B> io::Write for Writer<'a, B>
|
||||||
|
where
|
||||||
|
B: BufMut,
|
||||||
|
{
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
self.0.extend_from_slice(buf);
|
self.0.put_slice(buf);
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +61,8 @@ impl<'a> io::Write for Writer<'a> {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
|
use bytes::BytesMut;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -5,7 +5,6 @@ use std::{
|
||||||
convert::TryInto,
|
convert::TryInto,
|
||||||
fmt,
|
fmt,
|
||||||
future::Future,
|
future::Future,
|
||||||
ops,
|
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
str,
|
str,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
|
@ -358,7 +357,7 @@ impl ResponseBuilder {
|
||||||
|
|
||||||
/// Insert a header, replacing any that were set with an equivalent field name.
|
/// Insert a header, replacing any that were set with an equivalent field name.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// # use actix_http::Response;
|
/// # use actix_http::Response;
|
||||||
/// use actix_http::http::header::ContentType;
|
/// use actix_http::http::header::ContentType;
|
||||||
///
|
///
|
||||||
|
@ -385,7 +384,7 @@ impl ResponseBuilder {
|
||||||
|
|
||||||
/// Append a header, keeping any that were set with an equivalent field name.
|
/// Append a header, keeping any that were set with an equivalent field name.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// # use actix_http::Response;
|
/// # use actix_http::Response;
|
||||||
/// use actix_http::http::header::ContentType;
|
/// use actix_http::http::header::ContentType;
|
||||||
///
|
///
|
||||||
|
@ -498,7 +497,8 @@ impl ResponseBuilder {
|
||||||
/// Disable chunked transfer encoding for HTTP/1.1 streaming responses.
|
/// Disable chunked transfer encoding for HTTP/1.1 streaming responses.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn no_chunking(&mut self, len: u64) -> &mut Self {
|
pub fn no_chunking(&mut self, len: u64) -> &mut Self {
|
||||||
self.insert_header((header::CONTENT_LENGTH, len));
|
let mut buf = itoa::Buffer::new();
|
||||||
|
self.insert_header((header::CONTENT_LENGTH, buf.format(len)));
|
||||||
|
|
||||||
if let Some(parts) = parts(&mut self.head, &self.err) {
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
||||||
parts.no_chunking(true);
|
parts.no_chunking(true);
|
||||||
|
@ -525,7 +525,7 @@ impl ResponseBuilder {
|
||||||
|
|
||||||
/// Set a cookie
|
/// Set a cookie
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_http::{http, Request, Response};
|
/// use actix_http::{http, Request, Response};
|
||||||
///
|
///
|
||||||
/// fn index(req: Request) -> Response {
|
/// fn index(req: Request) -> Response {
|
||||||
|
@ -555,7 +555,7 @@ impl ResponseBuilder {
|
||||||
|
|
||||||
/// Remove cookie
|
/// Remove cookie
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_http::{http, Request, Response, HttpMessage};
|
/// use actix_http::{http, Request, Response, HttpMessage};
|
||||||
///
|
///
|
||||||
/// fn index(req: Request) -> Response {
|
/// fn index(req: Request) -> Response {
|
||||||
|
@ -672,12 +672,8 @@ impl ResponseBuilder {
|
||||||
/// Set a json body and generate `Response`
|
/// Set a json body and generate `Response`
|
||||||
///
|
///
|
||||||
/// `ResponseBuilder` can not be used after this call.
|
/// `ResponseBuilder` can not be used after this call.
|
||||||
pub fn json<T>(&mut self, value: T) -> Response
|
pub fn json(&mut self, value: impl Serialize) -> Response {
|
||||||
where
|
match serde_json::to_string(&value) {
|
||||||
T: ops::Deref,
|
|
||||||
T::Target: Serialize,
|
|
||||||
{
|
|
||||||
match serde_json::to_string(&*value) {
|
|
||||||
Ok(body) => {
|
Ok(body) => {
|
||||||
let contains = if let Some(parts) = parts(&mut self.head, &self.err) {
|
let contains = if let Some(parts) = parts(&mut self.head, &self.err) {
|
||||||
parts.headers.contains_key(header::CONTENT_TYPE)
|
parts.headers.contains_key(header::CONTENT_TYPE)
|
||||||
|
@ -896,8 +892,9 @@ mod tests {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::body::Body;
|
use crate::body::Body;
|
||||||
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE, SET_COOKIE};
|
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
|
||||||
use crate::HttpMessage;
|
#[cfg(feature = "cookies")]
|
||||||
|
use crate::{http::header::SET_COOKIE, HttpMessage};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug() {
|
fn test_debug() {
|
||||||
|
@ -909,6 +906,7 @@ mod tests {
|
||||||
assert!(dbg.contains("Response"));
|
assert!(dbg.contains("Response"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_response_cookies() {
|
fn test_response_cookies() {
|
||||||
let req = crate::test::TestRequest::default()
|
let req = crate::test::TestRequest::default()
|
||||||
|
@ -946,6 +944,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_update_response_cookies() {
|
fn test_update_response_cookies() {
|
||||||
let mut r = Response::Ok()
|
let mut r = Response::Ok()
|
||||||
|
@ -1003,7 +1002,12 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_json() {
|
fn test_json() {
|
||||||
let resp = Response::build(StatusCode::OK).json(vec!["v1", "v2", "v3"]);
|
let resp = Response::Ok().json(vec!["v1", "v2", "v3"]);
|
||||||
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
||||||
|
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
||||||
|
|
||||||
|
let resp = Response::Ok().json(&["v1", "v2", "v3"]);
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
||||||
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
||||||
|
@ -1097,6 +1101,7 @@ mod tests {
|
||||||
assert_eq!(resp.body().get_ref(), b"test");
|
assert_eq!(resp.body().get_ref(), b"test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into_builder() {
|
fn test_into_builder() {
|
||||||
let mut resp: Response = "test".into();
|
let mut resp: Response = "test".into();
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
use std::marker::PhantomData;
|
use std::{
|
||||||
use std::pin::Pin;
|
fmt,
|
||||||
use std::task::{Context, Poll};
|
future::Future,
|
||||||
use std::{fmt, net, rc::Rc};
|
marker::PhantomData,
|
||||||
|
net,
|
||||||
|
pin::Pin,
|
||||||
|
rc::Rc,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::{pipeline_factory, IntoServiceFactory, Service, ServiceFactory};
|
use actix_service::{pipeline_factory, IntoServiceFactory, Service, ServiceFactory};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::{ready, Future};
|
use futures_core::ready;
|
||||||
use h2::server::{self, Handshake};
|
use h2::server::{handshake, Handshake};
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::body::MessageBody;
|
use crate::body::MessageBody;
|
||||||
|
@ -185,10 +190,10 @@ where
|
||||||
mod openssl {
|
mod openssl {
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::ServiceFactoryExt;
|
use actix_service::ServiceFactoryExt;
|
||||||
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, SslStream};
|
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, TlsStream};
|
||||||
use actix_tls::accept::TlsError;
|
use actix_tls::accept::TlsError;
|
||||||
|
|
||||||
impl<S, B, X, U> HttpService<SslStream<TcpStream>, S, B, X, U>
|
impl<S, B, X, U> HttpService<TlsStream<TcpStream>, S, B, X, U>
|
||||||
where
|
where
|
||||||
S: ServiceFactory<Request, Config = ()>,
|
S: ServiceFactory<Request, Config = ()>,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
|
@ -201,13 +206,13 @@ mod openssl {
|
||||||
X::InitError: fmt::Debug,
|
X::InitError: fmt::Debug,
|
||||||
<X::Service as Service<Request>>::Future: 'static,
|
<X::Service as Service<Request>>::Future: 'static,
|
||||||
U: ServiceFactory<
|
U: ServiceFactory<
|
||||||
(Request, Framed<SslStream<TcpStream>, h1::Codec>),
|
(Request, Framed<TlsStream<TcpStream>, h1::Codec>),
|
||||||
Config = (),
|
Config = (),
|
||||||
Response = (),
|
Response = (),
|
||||||
>,
|
>,
|
||||||
U::Error: fmt::Display + Into<Error>,
|
U::Error: fmt::Display + Into<Error>,
|
||||||
U::InitError: fmt::Debug,
|
U::InitError: fmt::Debug,
|
||||||
<U::Service as Service<(Request, Framed<SslStream<TcpStream>, h1::Codec>)>>::Future: 'static,
|
<U::Service as Service<(Request, Framed<TlsStream<TcpStream>, h1::Codec>)>>::Future: 'static,
|
||||||
{
|
{
|
||||||
/// Create openssl based service
|
/// Create openssl based service
|
||||||
pub fn openssl(
|
pub fn openssl(
|
||||||
|
@ -225,7 +230,7 @@ mod openssl {
|
||||||
.map_err(TlsError::Tls)
|
.map_err(TlsError::Tls)
|
||||||
.map_init_err(|_| panic!()),
|
.map_init_err(|_| panic!()),
|
||||||
)
|
)
|
||||||
.and_then(|io: SslStream<TcpStream>| async {
|
.and_then(|io: TlsStream<TcpStream>| async {
|
||||||
let proto = if let Some(protos) = io.ssl().selected_alpn_protocol() {
|
let proto = if let Some(protos) = io.ssl().selected_alpn_protocol() {
|
||||||
if protos.windows(2).any(|window| window == b"h2") {
|
if protos.windows(2).any(|window| window == b"h2") {
|
||||||
Protocol::Http2
|
Protocol::Http2
|
||||||
|
@ -562,7 +567,7 @@ where
|
||||||
match proto {
|
match proto {
|
||||||
Protocol::Http2 => HttpServiceHandlerResponse {
|
Protocol::Http2 => HttpServiceHandlerResponse {
|
||||||
state: State::H2Handshake(Some((
|
state: State::H2Handshake(Some((
|
||||||
server::handshake(io),
|
handshake(io),
|
||||||
self.cfg.clone(),
|
self.cfg.clone(),
|
||||||
self.flow.clone(),
|
self.flow.clone(),
|
||||||
on_connect_data,
|
on_connect_data,
|
||||||
|
@ -658,7 +663,6 @@ where
|
||||||
conn,
|
conn,
|
||||||
on_connect_data,
|
on_connect_data,
|
||||||
cfg,
|
cfg,
|
||||||
None,
|
|
||||||
peer_addr,
|
peer_addr,
|
||||||
)));
|
)));
|
||||||
self.poll(cx)
|
self.poll(cx)
|
||||||
|
|
|
@ -26,7 +26,7 @@ use crate::{
|
||||||
|
|
||||||
/// Test `Request` builder
|
/// Test `Request` builder
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```ignore
|
||||||
/// # use http::{header, StatusCode};
|
/// # use http::{header, StatusCode};
|
||||||
/// # use actix_web::*;
|
/// # use actix_web::*;
|
||||||
/// use actix_web::test::TestRequest;
|
/// use actix_web::test::TestRequest;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use time::{Date, OffsetDateTime, PrimitiveDateTime};
|
use time::{Date, OffsetDateTime, PrimitiveDateTime};
|
||||||
|
|
||||||
/// Attempt to parse a `time` string as one of either RFC 1123, RFC 850, or asctime.
|
/// Attempt to parse a `time` string as one of either RFC 1123, RFC 850, or asctime.
|
||||||
pub fn parse_http_date(time: &str) -> Option<PrimitiveDateTime> {
|
pub(crate) fn parse_http_date(time: &str) -> Option<PrimitiveDateTime> {
|
||||||
try_parse_rfc_1123(time)
|
try_parse_rfc_1123(time)
|
||||||
.or_else(|| try_parse_rfc_850(time))
|
.or_else(|| try_parse_rfc_850(time))
|
||||||
.or_else(|| try_parse_asctime(time))
|
.or_else(|| try_parse_asctime(time))
|
||||||
|
|
|
@ -80,7 +80,7 @@ bitflags! {
|
||||||
|
|
||||||
impl Codec {
|
impl Codec {
|
||||||
/// Create new WebSocket frames decoder.
|
/// Create new WebSocket frames decoder.
|
||||||
pub fn new() -> Codec {
|
pub const fn new() -> Codec {
|
||||||
Codec {
|
Codec {
|
||||||
max_size: 65_536,
|
max_size: 65_536,
|
||||||
flags: Flags::SERVER,
|
flags: Flags::SERVER,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
//! WebSocket protocol.
|
//! WebSocket protocol implementation.
|
||||||
//!
|
//!
|
||||||
//! To setup a WebSocket, first perform the WebSocket handshake then on success convert `Payload` into a
|
//! To setup a WebSocket, first perform the WebSocket handshake then on success convert `Payload` into a
|
||||||
//! `WsStream` stream and then use `WsWriter` to communicate with the peer.
|
//! `WsStream` stream and then use `WsWriter` to communicate with the peer.
|
||||||
|
@ -8,9 +8,12 @@ use std::io;
|
||||||
use derive_more::{Display, Error, From};
|
use derive_more::{Display, Error, From};
|
||||||
use http::{header, Method, StatusCode};
|
use http::{header, Method, StatusCode};
|
||||||
|
|
||||||
use crate::error::ResponseError;
|
use crate::{
|
||||||
use crate::message::RequestHead;
|
error::ResponseError,
|
||||||
use crate::response::{Response, ResponseBuilder};
|
header::HeaderValue,
|
||||||
|
message::RequestHead,
|
||||||
|
response::{Response, ResponseBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
mod codec;
|
mod codec;
|
||||||
mod dispatcher;
|
mod dispatcher;
|
||||||
|
@ -89,7 +92,7 @@ pub enum HandshakeError {
|
||||||
NoVersionHeader,
|
NoVersionHeader,
|
||||||
|
|
||||||
/// Unsupported WebSocket version.
|
/// Unsupported WebSocket version.
|
||||||
#[display(fmt = "Unsupported version.")]
|
#[display(fmt = "Unsupported WebSocket version.")]
|
||||||
UnsupportedVersion,
|
UnsupportedVersion,
|
||||||
|
|
||||||
/// WebSocket key is not set or wrong.
|
/// WebSocket key is not set or wrong.
|
||||||
|
@ -105,19 +108,19 @@ impl ResponseError for HandshakeError {
|
||||||
.finish(),
|
.finish(),
|
||||||
|
|
||||||
HandshakeError::NoWebsocketUpgrade => Response::BadRequest()
|
HandshakeError::NoWebsocketUpgrade => Response::BadRequest()
|
||||||
.reason("No WebSocket UPGRADE header found")
|
.reason("No WebSocket Upgrade header found")
|
||||||
.finish(),
|
.finish(),
|
||||||
|
|
||||||
HandshakeError::NoConnectionUpgrade => Response::BadRequest()
|
HandshakeError::NoConnectionUpgrade => Response::BadRequest()
|
||||||
.reason("No CONNECTION upgrade")
|
.reason("No Connection upgrade")
|
||||||
.finish(),
|
.finish(),
|
||||||
|
|
||||||
HandshakeError::NoVersionHeader => Response::BadRequest()
|
HandshakeError::NoVersionHeader => Response::BadRequest()
|
||||||
.reason("Websocket version header is required")
|
.reason("WebSocket version header is required")
|
||||||
.finish(),
|
.finish(),
|
||||||
|
|
||||||
HandshakeError::UnsupportedVersion => Response::BadRequest()
|
HandshakeError::UnsupportedVersion => Response::BadRequest()
|
||||||
.reason("Unsupported version")
|
.reason("Unsupported WebSocket version")
|
||||||
.finish(),
|
.finish(),
|
||||||
|
|
||||||
HandshakeError::BadWebsocketKey => {
|
HandshakeError::BadWebsocketKey => {
|
||||||
|
@ -193,7 +196,11 @@ pub fn handshake_response(req: &RequestHead) -> ResponseBuilder {
|
||||||
Response::build(StatusCode::SWITCHING_PROTOCOLS)
|
Response::build(StatusCode::SWITCHING_PROTOCOLS)
|
||||||
.upgrade("websocket")
|
.upgrade("websocket")
|
||||||
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
||||||
.insert_header((header::SEC_WEBSOCKET_ACCEPT, key))
|
.insert_header((
|
||||||
|
header::SEC_WEBSOCKET_ACCEPT,
|
||||||
|
// key is known to be header value safe ascii
|
||||||
|
HeaderValue::from_bytes(&key).unwrap(),
|
||||||
|
))
|
||||||
.take()
|
.take()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use std::convert::{From, Into};
|
use std::{
|
||||||
use std::fmt;
|
convert::{From, Into},
|
||||||
|
fmt,
|
||||||
|
};
|
||||||
|
|
||||||
/// Operation codes as part of RFC6455.
|
/// Operation codes as part of RFC6455.
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
||||||
|
@ -28,8 +30,9 @@ pub enum OpCode {
|
||||||
|
|
||||||
impl fmt::Display for OpCode {
|
impl fmt::Display for OpCode {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
use self::OpCode::*;
|
use OpCode::*;
|
||||||
match *self {
|
|
||||||
|
match self {
|
||||||
Continue => write!(f, "CONTINUE"),
|
Continue => write!(f, "CONTINUE"),
|
||||||
Text => write!(f, "TEXT"),
|
Text => write!(f, "TEXT"),
|
||||||
Binary => write!(f, "BINARY"),
|
Binary => write!(f, "BINARY"),
|
||||||
|
@ -44,6 +47,7 @@ impl fmt::Display for OpCode {
|
||||||
impl From<OpCode> for u8 {
|
impl From<OpCode> for u8 {
|
||||||
fn from(op: OpCode) -> u8 {
|
fn from(op: OpCode) -> u8 {
|
||||||
use self::OpCode::*;
|
use self::OpCode::*;
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Continue => 0,
|
Continue => 0,
|
||||||
Text => 1,
|
Text => 1,
|
||||||
|
@ -62,6 +66,7 @@ impl From<OpCode> for u8 {
|
||||||
impl From<u8> for OpCode {
|
impl From<u8> for OpCode {
|
||||||
fn from(byte: u8) -> OpCode {
|
fn from(byte: u8) -> OpCode {
|
||||||
use self::OpCode::*;
|
use self::OpCode::*;
|
||||||
|
|
||||||
match byte {
|
match byte {
|
||||||
0 => Continue,
|
0 => Continue,
|
||||||
1 => Text,
|
1 => Text,
|
||||||
|
@ -77,63 +82,66 @@ impl From<u8> for OpCode {
|
||||||
/// Status code used to indicate why an endpoint is closing the WebSocket connection.
|
/// Status code used to indicate why an endpoint is closing the WebSocket connection.
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
||||||
pub enum CloseCode {
|
pub enum CloseCode {
|
||||||
/// Indicates a normal closure, meaning that the purpose for
|
/// Indicates a normal closure, meaning that the purpose for which the connection was
|
||||||
/// which the connection was established has been fulfilled.
|
/// established has been fulfilled.
|
||||||
Normal,
|
Normal,
|
||||||
/// Indicates that an endpoint is "going away", such as a server
|
|
||||||
/// going down or a browser having navigated away from a page.
|
/// Indicates that an endpoint is "going away", such as a server going down or a browser having
|
||||||
|
/// navigated away from a page.
|
||||||
Away,
|
Away,
|
||||||
/// Indicates that an endpoint is terminating the connection due
|
|
||||||
/// to a protocol error.
|
/// Indicates that an endpoint is terminating the connection due to a protocol error.
|
||||||
Protocol,
|
Protocol,
|
||||||
/// Indicates that an endpoint is terminating the connection
|
|
||||||
/// because it has received a type of data it cannot accept (e.g., an
|
/// Indicates that an endpoint is terminating the connection because it has received a type of
|
||||||
/// endpoint that understands only text data MAY send this if it
|
/// data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it
|
||||||
/// receives a binary message).
|
/// receives a binary message).
|
||||||
Unsupported,
|
Unsupported,
|
||||||
/// Indicates an abnormal closure. If the abnormal closure was due to an
|
|
||||||
/// error, this close code will not be used. Instead, the `on_error` method
|
/// Indicates an abnormal closure. If the abnormal closure was due to an error, this close code
|
||||||
/// of the handler will be called with the error. However, if the connection
|
/// will not be used. Instead, the `on_error` method of the handler will be called with
|
||||||
/// is simply dropped, without an error, this close code will be sent to the
|
/// the error. However, if the connection is simply dropped, without an error, this close code
|
||||||
/// handler.
|
/// will be sent to the handler.
|
||||||
Abnormal,
|
Abnormal,
|
||||||
/// Indicates that an endpoint is terminating the connection
|
|
||||||
/// because it has received data within a message that was not
|
/// Indicates that an endpoint is terminating the connection because it has received data within
|
||||||
/// consistent with the type of the message (e.g., non-UTF-8 \[RFC3629\]
|
/// a message that was not consistent with the type of the message (e.g., non-UTF-8 \[RFC3629\]
|
||||||
/// data within a text message).
|
/// data within a text message).
|
||||||
Invalid,
|
Invalid,
|
||||||
/// Indicates that an endpoint is terminating the connection
|
|
||||||
/// because it has received a message that violates its policy. This
|
/// Indicates that an endpoint is terminating the connection because it has received a message
|
||||||
/// is a generic status code that can be returned when there is no
|
/// that violates its policy. This is a generic status code that can be returned when there is
|
||||||
/// other more suitable status code (e.g., Unsupported or Size) or if there
|
/// no other more suitable status code (e.g., Unsupported or Size) or if there is a need to hide
|
||||||
/// is a need to hide specific details about the policy.
|
/// specific details about the policy.
|
||||||
Policy,
|
Policy,
|
||||||
/// Indicates that an endpoint is terminating the connection
|
|
||||||
/// because it has received a message that is too big for it to
|
/// Indicates that an endpoint is terminating the connection because it has received a message
|
||||||
/// process.
|
/// that is too big for it to process.
|
||||||
Size,
|
Size,
|
||||||
/// Indicates that an endpoint (client) is terminating the
|
|
||||||
/// connection because it has expected the server to negotiate one or
|
/// Indicates that an endpoint (client) is terminating the connection because it has expected
|
||||||
/// more extension, but the server didn't return them in the response
|
/// the server to negotiate one or more extension, but the server didn't return them in the
|
||||||
/// message of the WebSocket handshake. The list of extensions that
|
/// response message of the WebSocket handshake. The list of extensions that are needed should
|
||||||
/// are needed should be given as the reason for closing.
|
/// be given as the reason for closing. Note that this status code is not used by the server,
|
||||||
/// Note that this status code is not used by the server, because it
|
/// because it can fail the WebSocket handshake instead.
|
||||||
/// can fail the WebSocket handshake instead.
|
|
||||||
Extension,
|
Extension,
|
||||||
/// Indicates that a server is terminating the connection because
|
|
||||||
/// it encountered an unexpected condition that prevented it from
|
/// Indicates that a server is terminating the connection because it encountered an unexpected
|
||||||
/// fulfilling the request.
|
/// condition that prevented it from fulfilling the request.
|
||||||
Error,
|
Error,
|
||||||
/// Indicates that the server is restarting. A client may choose to
|
|
||||||
/// reconnect, and if it does, it should use a randomized delay of 5-30
|
/// Indicates that the server is restarting. A client may choose to reconnect, and if it does,
|
||||||
/// seconds between attempts.
|
/// it should use a randomized delay of 5-30 seconds between attempts.
|
||||||
Restart,
|
Restart,
|
||||||
/// Indicates that the server is overloaded and the client should either
|
|
||||||
/// connect to a different IP (when multiple targets exist), or
|
/// Indicates that the server is overloaded and the client should either connect to a different
|
||||||
/// reconnect to the same IP when a user has performed an action.
|
/// IP (when multiple targets exist), or reconnect to the same IP when a user has performed
|
||||||
|
/// an action.
|
||||||
Again,
|
Again,
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
Tls,
|
Tls,
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
Other(u16),
|
Other(u16),
|
||||||
}
|
}
|
||||||
|
@ -141,6 +149,7 @@ pub enum CloseCode {
|
||||||
impl From<CloseCode> for u16 {
|
impl From<CloseCode> for u16 {
|
||||||
fn from(code: CloseCode) -> u16 {
|
fn from(code: CloseCode) -> u16 {
|
||||||
use self::CloseCode::*;
|
use self::CloseCode::*;
|
||||||
|
|
||||||
match code {
|
match code {
|
||||||
Normal => 1000,
|
Normal => 1000,
|
||||||
Away => 1001,
|
Away => 1001,
|
||||||
|
@ -163,6 +172,7 @@ impl From<CloseCode> for u16 {
|
||||||
impl From<u16> for CloseCode {
|
impl From<u16> for CloseCode {
|
||||||
fn from(code: u16) -> CloseCode {
|
fn from(code: u16) -> CloseCode {
|
||||||
use self::CloseCode::*;
|
use self::CloseCode::*;
|
||||||
|
|
||||||
match code {
|
match code {
|
||||||
1000 => Normal,
|
1000 => Normal,
|
||||||
1001 => Away,
|
1001 => Away,
|
||||||
|
@ -210,17 +220,29 @@ impl<T: Into<String>> From<(CloseCode, T)> for CloseReason {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static WS_GUID: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
/// The WebSocket GUID as stated in the spec. See https://tools.ietf.org/html/rfc6455#section-1.3.
|
||||||
|
static WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
|
|
||||||
|
/// Hashes the `Sec-WebSocket-Key` header according to the WebSocket spec.
|
||||||
|
///
|
||||||
|
/// Result is a Base64 encoded byte array. `base64(sha1(input))` is always 28 bytes.
|
||||||
|
pub fn hash_key(key: &[u8]) -> [u8; 28] {
|
||||||
|
let hash = {
|
||||||
|
use sha1::Digest as _;
|
||||||
|
|
||||||
// TODO: hash is always same size, we don't need String
|
|
||||||
pub fn hash_key(key: &[u8]) -> String {
|
|
||||||
use sha1::Digest;
|
|
||||||
let mut hasher = sha1::Sha1::new();
|
let mut hasher = sha1::Sha1::new();
|
||||||
|
|
||||||
hasher.update(key);
|
hasher.update(key);
|
||||||
hasher.update(WS_GUID.as_bytes());
|
hasher.update(WS_GUID);
|
||||||
|
|
||||||
base64::encode(&hasher.finalize())
|
hasher.finalize()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut hash_b64 = [0; 28];
|
||||||
|
let n = base64::encode_config_slice(&hash, base64::STANDARD, &mut hash_b64);
|
||||||
|
assert_eq!(n, 28);
|
||||||
|
|
||||||
|
hash_b64
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -288,11 +310,11 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hash_key() {
|
fn test_hash_key() {
|
||||||
let hash = hash_key(b"hello actix-web");
|
let hash = hash_key(b"hello actix-web");
|
||||||
assert_eq!(&hash, "cR1dlyUUJKp0s/Bel25u5TgvC3E=");
|
assert_eq!(&hash, b"cR1dlyUUJKp0s/Bel25u5TgvC3E=");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn closecode_from_u16() {
|
fn close_code_from_u16() {
|
||||||
assert_eq!(CloseCode::from(1000u16), CloseCode::Normal);
|
assert_eq!(CloseCode::from(1000u16), CloseCode::Normal);
|
||||||
assert_eq!(CloseCode::from(1001u16), CloseCode::Away);
|
assert_eq!(CloseCode::from(1001u16), CloseCode::Away);
|
||||||
assert_eq!(CloseCode::from(1002u16), CloseCode::Protocol);
|
assert_eq!(CloseCode::from(1002u16), CloseCode::Protocol);
|
||||||
|
@ -310,7 +332,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn closecode_into_u16() {
|
fn close_code_into_u16() {
|
||||||
assert_eq!(1000u16, Into::<u16>::into(CloseCode::Normal));
|
assert_eq!(1000u16, Into::<u16>::into(CloseCode::Normal));
|
||||||
assert_eq!(1001u16, Into::<u16>::into(CloseCode::Away));
|
assert_eq!(1001u16, Into::<u16>::into(CloseCode::Away));
|
||||||
assert_eq!(1002u16, Into::<u16>::into(CloseCode::Protocol));
|
assert_eq!(1002u16, Into::<u16>::into(CloseCode::Protocol));
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
use actix_http::{http, HttpService, Request, Response};
|
use actix_http::{
|
||||||
|
error, http, http::StatusCode, HttpMessage, HttpService, Request, Response,
|
||||||
|
};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::ServiceFactoryExt;
|
use actix_service::ServiceFactoryExt;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::future::{self, ok};
|
use futures_util::{
|
||||||
|
future::{self, ok},
|
||||||
|
StreamExt as _,
|
||||||
|
};
|
||||||
|
|
||||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
Hello World Hello World Hello World Hello World Hello World \
|
Hello World Hello World Hello World Hello World Hello World \
|
||||||
|
@ -88,3 +93,55 @@ async fn test_with_query_parameter() {
|
||||||
let response = request.send().await.unwrap();
|
let response = request.send().await.unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_h1_expect() {
|
||||||
|
let srv = test_server(move || {
|
||||||
|
HttpService::build()
|
||||||
|
.expect(|req: Request| async {
|
||||||
|
if req.headers().contains_key("AUTH") {
|
||||||
|
Ok(req)
|
||||||
|
} else {
|
||||||
|
Err(error::ErrorExpectationFailed("expect failed"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.h1(|req: Request| async move {
|
||||||
|
let (_, mut body) = req.into_parts();
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
while let Some(Ok(chunk)) = body.next().await {
|
||||||
|
buf.extend_from_slice(&chunk);
|
||||||
|
}
|
||||||
|
let str = std::str::from_utf8(&buf).unwrap();
|
||||||
|
assert_eq!(str, "expect body");
|
||||||
|
|
||||||
|
Ok::<_, ()>(Response::Ok().finish())
|
||||||
|
})
|
||||||
|
.tcp()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// test expect without payload.
|
||||||
|
let request = srv
|
||||||
|
.request(http::Method::GET, srv.url("/"))
|
||||||
|
.insert_header(("Expect", "100-continue"));
|
||||||
|
|
||||||
|
let response = request.send().await;
|
||||||
|
assert!(response.is_err());
|
||||||
|
|
||||||
|
// test expect would fail to continue
|
||||||
|
let request = srv
|
||||||
|
.request(http::Method::GET, srv.url("/"))
|
||||||
|
.insert_header(("Expect", "100-continue"));
|
||||||
|
|
||||||
|
let response = request.send_body("expect body").await.unwrap();
|
||||||
|
assert_eq!(response.status(), StatusCode::EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
// test exepct would continue
|
||||||
|
let request = srv
|
||||||
|
.request(http::Method::GET, srv.url("/"))
|
||||||
|
.insert_header(("Expect", "100-continue"))
|
||||||
|
.insert_header(("AUTH", "996"));
|
||||||
|
|
||||||
|
let response = request.send_body("expect body").await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
||||||
|
|
|
@ -12,8 +12,11 @@ use actix_http::{body, Error, HttpService, Request, Response};
|
||||||
use actix_http_test::test_server;
|
use actix_http_test::test_server;
|
||||||
use actix_service::{fn_service, ServiceFactoryExt};
|
use actix_service::{fn_service, ServiceFactoryExt};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_util::future::{err, ok, ready};
|
use futures_core::Stream;
|
||||||
use futures_util::stream::{once, Stream, StreamExt};
|
use futures_util::{
|
||||||
|
future::{err, ok, ready},
|
||||||
|
stream::{once, StreamExt as _},
|
||||||
|
};
|
||||||
use openssl::{
|
use openssl::{
|
||||||
pkey::PKey,
|
pkey::PKey,
|
||||||
ssl::{SslAcceptor, SslMethod},
|
ssl::{SslAcceptor, SslMethod},
|
||||||
|
@ -123,16 +126,14 @@ async fn test_h2_content_length() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|req: Request| {
|
.h2(|req: Request| {
|
||||||
let indx: usize = req.uri().path()[1..].parse().unwrap();
|
let idx: usize = req.uri().path()[1..].parse().unwrap();
|
||||||
let statuses = [
|
let statuses = [
|
||||||
StatusCode::NO_CONTENT,
|
|
||||||
StatusCode::CONTINUE,
|
StatusCode::CONTINUE,
|
||||||
StatusCode::SWITCHING_PROTOCOLS,
|
StatusCode::NO_CONTENT,
|
||||||
StatusCode::PROCESSING,
|
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
StatusCode::NOT_FOUND,
|
StatusCode::NOT_FOUND,
|
||||||
];
|
];
|
||||||
ok::<_, ()>(Response::new(statuses[indx]))
|
ok::<_, ()>(Response::new(statuses[idx]))
|
||||||
})
|
})
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
@ -143,21 +144,29 @@ async fn test_h2_content_length() {
|
||||||
let value = HeaderValue::from_static("0");
|
let value = HeaderValue::from_static("0");
|
||||||
|
|
||||||
{
|
{
|
||||||
for i in 0..4 {
|
for &i in &[0] {
|
||||||
|
let req = srv
|
||||||
|
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
|
||||||
|
.send();
|
||||||
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
||||||
|
// assert_eq!(response.headers().get(&header), None);
|
||||||
|
|
||||||
|
let req = srv
|
||||||
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
||||||
|
.send();
|
||||||
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
||||||
|
// assert_eq!(response.headers().get(&header), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
for &i in &[1] {
|
||||||
let req = srv
|
let req = srv
|
||||||
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
||||||
.send();
|
.send();
|
||||||
let response = req.await.unwrap();
|
let response = req.await.unwrap();
|
||||||
assert_eq!(response.headers().get(&header), None);
|
assert_eq!(response.headers().get(&header), None);
|
||||||
|
|
||||||
let req = srv
|
|
||||||
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
|
|
||||||
.send();
|
|
||||||
let response = req.await.unwrap();
|
|
||||||
assert_eq!(response.headers().get(&header), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 4..6 {
|
for &i in &[2, 3] {
|
||||||
let req = srv
|
let req = srv
|
||||||
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
||||||
.send();
|
.send();
|
||||||
|
|
|
@ -10,14 +10,14 @@ use actix_http_test::test_server;
|
||||||
use actix_service::{fn_factory_with_config, fn_service};
|
use actix_service::{fn_factory_with_config, fn_service};
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
use futures_core::Stream;
|
||||||
use futures_util::future::{self, err, ok};
|
use futures_util::future::{self, err, ok};
|
||||||
use futures_util::stream::{once, Stream, StreamExt};
|
use futures_util::stream::{once, StreamExt as _};
|
||||||
use rustls::{
|
use rustls::{
|
||||||
internal::pemfile::{certs, pkcs8_private_keys},
|
internal::pemfile::{certs, pkcs8_private_keys},
|
||||||
NoClientAuth, ServerConfig as RustlsServerConfig,
|
NoClientAuth, ServerConfig as RustlsServerConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::{self, BufReader};
|
use std::io::{self, BufReader};
|
||||||
|
|
||||||
async fn load_body<S>(mut stream: S) -> Result<BytesMut, PayloadError>
|
async fn load_body<S>(mut stream: S) -> Result<BytesMut, PayloadError>
|
||||||
|
@ -139,10 +139,8 @@ async fn test_h2_content_length() {
|
||||||
.h2(|req: Request| {
|
.h2(|req: Request| {
|
||||||
let indx: usize = req.uri().path()[1..].parse().unwrap();
|
let indx: usize = req.uri().path()[1..].parse().unwrap();
|
||||||
let statuses = [
|
let statuses = [
|
||||||
StatusCode::NO_CONTENT,
|
|
||||||
StatusCode::CONTINUE,
|
StatusCode::CONTINUE,
|
||||||
StatusCode::SWITCHING_PROTOCOLS,
|
StatusCode::NO_CONTENT,
|
||||||
StatusCode::PROCESSING,
|
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
StatusCode::NOT_FOUND,
|
StatusCode::NOT_FOUND,
|
||||||
];
|
];
|
||||||
|
@ -154,22 +152,31 @@ async fn test_h2_content_length() {
|
||||||
|
|
||||||
let header = HeaderName::from_static("content-length");
|
let header = HeaderName::from_static("content-length");
|
||||||
let value = HeaderValue::from_static("0");
|
let value = HeaderValue::from_static("0");
|
||||||
|
|
||||||
{
|
{
|
||||||
for i in 0..4 {
|
for &i in &[0] {
|
||||||
|
let req = srv
|
||||||
|
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
|
||||||
|
.send();
|
||||||
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
||||||
|
// assert_eq!(response.headers().get(&header), None);
|
||||||
|
|
||||||
|
let req = srv
|
||||||
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
||||||
|
.send();
|
||||||
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
||||||
|
// assert_eq!(response.headers().get(&header), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
for &i in &[1] {
|
||||||
let req = srv
|
let req = srv
|
||||||
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
||||||
.send();
|
.send();
|
||||||
let response = req.await.unwrap();
|
let response = req.await.unwrap();
|
||||||
assert_eq!(response.headers().get(&header), None);
|
assert_eq!(response.headers().get(&header), None);
|
||||||
|
|
||||||
let req = srv
|
|
||||||
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
|
|
||||||
.send();
|
|
||||||
let response = req.await.unwrap();
|
|
||||||
assert_eq!(response.headers().get(&header), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 4..6 {
|
for &i in &[2, 3] {
|
||||||
let req = srv
|
let req = srv
|
||||||
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
||||||
.send();
|
.send();
|
||||||
|
|
|
@ -7,7 +7,7 @@ use actix_rt::time::sleep;
|
||||||
use actix_service::fn_service;
|
use actix_service::fn_service;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::future::{self, err, ok, ready, FutureExt};
|
use futures_util::future::{self, err, ok, ready, FutureExt};
|
||||||
use futures_util::stream::{once, StreamExt};
|
use futures_util::stream::{once, StreamExt as _};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use actix_http::HttpMessage;
|
use actix_http::HttpMessage;
|
||||||
|
@ -126,7 +126,7 @@ async fn test_chunked_payload() {
|
||||||
.take_payload()
|
.take_payload()
|
||||||
.map(|res| match res {
|
.map(|res| match res {
|
||||||
Ok(pl) => pl,
|
Ok(pl) => pl,
|
||||||
Err(e) => panic!(format!("Error reading payload: {}", e)),
|
Err(e) => panic!("Error reading payload: {}", e),
|
||||||
})
|
})
|
||||||
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
||||||
.map(|req_size| {
|
.map(|req_size| {
|
||||||
|
@ -162,7 +162,7 @@ async fn test_chunked_payload() {
|
||||||
let re = Regex::new(r"size=(\d+)").unwrap();
|
let re = Regex::new(r"size=(\d+)").unwrap();
|
||||||
let size: usize = match re.captures(&data) {
|
let size: usize = match re.captures(&data) {
|
||||||
Some(caps) => caps.get(1).unwrap().as_str().parse().unwrap(),
|
Some(caps) => caps.get(1).unwrap().as_str().parse().unwrap(),
|
||||||
None => panic!(format!("Failed to find size in HTTP Response: {}", data)),
|
None => panic!("Failed to find size in HTTP Response: {}", data),
|
||||||
};
|
};
|
||||||
size
|
size
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,7 +12,7 @@ use actix_utils::dispatcher::Dispatcher;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::future;
|
use futures_util::future;
|
||||||
use futures_util::task::{Context, Poll};
|
use futures_util::task::{Context, Poll};
|
||||||
use futures_util::{SinkExt, StreamExt};
|
use futures_util::{SinkExt as _, StreamExt as _};
|
||||||
|
|
||||||
struct WsService<T>(Arc<Mutex<(PhantomData<T>, Cell<bool>)>>);
|
struct WsService<T>(Arc<Mutex<(PhantomData<T>, Cell<bool>)>>);
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 0.4.0-beta.3 - 2021-03-09
|
||||||
|
* No notable changes.
|
||||||
|
|
||||||
|
|
||||||
## 0.4.0-beta.2 - 2021-02-10
|
## 0.4.0-beta.2 - 2021-02-10
|
||||||
* No notable changes.
|
* No notable changes.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-multipart"
|
name = "actix-multipart"
|
||||||
version = "0.4.0-beta.2"
|
version = "0.4.0-beta.3"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Multipart form support for Actix Web"
|
description = "Multipart form support for Actix Web"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -16,17 +16,19 @@ name = "actix_multipart"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.3", default-features = false }
|
actix-web = { version = "4.0.0-beta.4", default-features = false }
|
||||||
actix-utils = "3.0.0-beta.2"
|
actix-utils = "3.0.0-beta.2"
|
||||||
|
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
httparse = "1.3"
|
httparse = "1.3"
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
twoway = "0.2"
|
twoway = "0.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
actix-http = "3.0.0-beta.3"
|
actix-http = "3.0.0-beta.4"
|
||||||
|
tokio = { version = "1", features = ["sync"] }
|
||||||
|
tokio-stream = "0.1"
|
||||||
|
|
|
@ -3,13 +3,12 @@
|
||||||
> Multipart form support for Actix Web.
|
> Multipart form support for Actix Web.
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-multipart)
|
[](https://crates.io/crates/actix-multipart)
|
||||||
[](https://docs.rs/actix-multipart/0.4.0-beta.2)
|
[](https://docs.rs/actix-multipart/0.4.0-beta.3)
|
||||||
[](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-multipart/0.4.0-beta.2)
|
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.3)
|
||||||
[](https://crates.io/crates/actix-multipart)
|
[](https://crates.io/crates/actix-multipart)
|
||||||
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
||||||
|
|
||||||
## Documentation & Resources
|
## Documentation & Resources
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::server::Multipart;
|
||||||
///
|
///
|
||||||
/// ## Server example
|
/// ## Server example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use futures_util::stream::{Stream, StreamExt};
|
/// use futures_util::stream::{Stream, StreamExt};
|
||||||
/// use actix_web::{web, HttpResponse, Error};
|
/// use actix_web::{web, HttpResponse, Error};
|
||||||
/// use actix_multipart as mp;
|
/// use actix_multipart as mp;
|
||||||
|
|
|
@ -804,12 +804,13 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use actix_http::h1::Payload;
|
use actix_http::h1::Payload;
|
||||||
use actix_utils::mpsc;
|
|
||||||
use actix_web::http::header::{DispositionParam, DispositionType};
|
use actix_web::http::header::{DispositionParam, DispositionType};
|
||||||
use actix_web::test::TestRequest;
|
use actix_web::test::TestRequest;
|
||||||
use actix_web::FromRequest;
|
use actix_web::FromRequest;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::future::lazy;
|
use futures_util::future::lazy;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_boundary() {
|
async fn test_boundary() {
|
||||||
|
@ -855,13 +856,17 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_stream() -> (
|
fn create_stream() -> (
|
||||||
mpsc::Sender<Result<Bytes, PayloadError>>,
|
mpsc::UnboundedSender<Result<Bytes, PayloadError>>,
|
||||||
impl Stream<Item = Result<Bytes, PayloadError>>,
|
impl Stream<Item = Result<Bytes, PayloadError>>,
|
||||||
) {
|
) {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
(tx, rx.map(|res| res.map_err(|_| panic!())))
|
(
|
||||||
|
tx,
|
||||||
|
UnboundedReceiverStream::new(rx).map(|res| res.map_err(|_| panic!())),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream that returns from a Bytes, one char at a time and Pending every other poll()
|
// Stream that returns from a Bytes, one char at a time and Pending every other poll()
|
||||||
struct SlowStream {
|
struct SlowStream {
|
||||||
bytes: Bytes,
|
bytes: Bytes,
|
||||||
|
@ -889,9 +894,11 @@ mod tests {
|
||||||
cx.waker().wake_by_ref();
|
cx.waker().wake_by_ref();
|
||||||
return Poll::Pending;
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.pos == this.bytes.len() {
|
if this.pos == this.bytes.len() {
|
||||||
return Poll::Ready(None);
|
return Poll::Ready(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = Poll::Ready(Some(Ok(this.bytes.slice(this.pos..(this.pos + 1)))));
|
let res = Poll::Ready(Some(Ok(this.bytes.slice(this.pos..(this.pos + 1)))));
|
||||||
this.pos += 1;
|
this.pos += 1;
|
||||||
this.ready = false;
|
this.ready = false;
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 4.0.0-beta.3 - 2021-03-09
|
||||||
|
* No notable changes.
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.2 - 2021-02-10
|
## 4.0.0-beta.2 - 2021-02-10
|
||||||
* No notable changes.
|
* No notable changes.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-web-actors"
|
name = "actix-web-actors"
|
||||||
version = "4.0.0-beta.2"
|
version = "4.0.0-beta.3"
|
||||||
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"
|
readme = "README.md"
|
||||||
|
@ -16,10 +16,10 @@ name = "actix_web_actors"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = { version = "0.11.0-beta.2", default-features = false }
|
actix = { version = "0.11.0-beta.3", default-features = false }
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-http = "3.0.0-beta.3"
|
actix-http = "3.0.0-beta.4"
|
||||||
actix-web = { version = "4.0.0-beta.3", default-features = false }
|
actix-web = { version = "4.0.0-beta.4", default-features = false }
|
||||||
|
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
bytestring = "1"
|
bytestring = "1"
|
||||||
|
@ -28,6 +28,6 @@ pin-project = "1.0.0"
|
||||||
tokio = { version = "1", features = ["sync"] }
|
tokio = { version = "1", features = ["sync"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
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,11 +3,11 @@
|
||||||
> 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/0.5.0)
|
[](https://docs.rs/actix-web-actors/4.0.0-beta.3)
|
||||||
[](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/0.5.0)
|
[](https://deps.rs/crate/actix-web-actors/4.0.0-beta.3)
|
||||||
[](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://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ where
|
||||||
#[inline]
|
#[inline]
|
||||||
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
||||||
where
|
where
|
||||||
F: ActorFuture<Output = (), Actor = A> + 'static,
|
F: ActorFuture<A, Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
self.inner.spawn(fut)
|
self.inner.spawn(fut)
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ where
|
||||||
#[inline]
|
#[inline]
|
||||||
fn wait<F>(&mut self, fut: F)
|
fn wait<F>(&mut self, fut: F)
|
||||||
where
|
where
|
||||||
F: ActorFuture<Output = (), Actor = A> + 'static,
|
F: ActorFuture<A, Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
self.inner.wait(fut)
|
self.inner.wait(fut)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,13 @@ use actix::{
|
||||||
SpawnHandle,
|
SpawnHandle,
|
||||||
};
|
};
|
||||||
use actix_codec::{Decoder, Encoder};
|
use actix_codec::{Decoder, Encoder};
|
||||||
use actix_http::ws::{hash_key, Codec};
|
|
||||||
pub use actix_http::ws::{
|
pub use actix_http::ws::{
|
||||||
CloseCode, CloseReason, Frame, HandshakeError, Message, ProtocolError,
|
CloseCode, CloseReason, Frame, HandshakeError, Message, ProtocolError,
|
||||||
};
|
};
|
||||||
|
use actix_http::{
|
||||||
|
http::HeaderValue,
|
||||||
|
ws::{hash_key, Codec},
|
||||||
|
};
|
||||||
use actix_web::dev::HttpResponseBuilder;
|
use actix_web::dev::HttpResponseBuilder;
|
||||||
use actix_web::error::{Error, PayloadError};
|
use actix_web::error::{Error, PayloadError};
|
||||||
use actix_web::http::{header, Method, StatusCode};
|
use actix_web::http::{header, Method, StatusCode};
|
||||||
|
@ -162,7 +165,11 @@ pub fn handshake_with_protocols(
|
||||||
|
|
||||||
let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
|
let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
|
||||||
.upgrade("websocket")
|
.upgrade("websocket")
|
||||||
.insert_header((header::SEC_WEBSOCKET_ACCEPT, key))
|
.insert_header((
|
||||||
|
header::SEC_WEBSOCKET_ACCEPT,
|
||||||
|
// key is known to be header value safe ascii
|
||||||
|
HeaderValue::from_bytes(&key).unwrap(),
|
||||||
|
))
|
||||||
.take();
|
.take();
|
||||||
|
|
||||||
if let Some(protocol) = protocol {
|
if let Some(protocol) = protocol {
|
||||||
|
@ -204,14 +211,14 @@ where
|
||||||
{
|
{
|
||||||
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
|
||||||
where
|
where
|
||||||
F: ActorFuture<Output = (), Actor = A> + 'static,
|
F: ActorFuture<A, Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
self.inner.spawn(fut)
|
self.inner.spawn(fut)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait<F>(&mut self, fut: F)
|
fn wait<F>(&mut self, fut: F)
|
||||||
where
|
where
|
||||||
F: ActorFuture<Output = (), Actor = A> + 'static,
|
F: ActorFuture<A, Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
self.inner.wait(fut)
|
self.inner.wait(fut)
|
||||||
}
|
}
|
||||||
|
@ -488,7 +495,6 @@ where
|
||||||
|
|
||||||
if !*this.closed {
|
if !*this.closed {
|
||||||
loop {
|
loop {
|
||||||
this = self.as_mut().project();
|
|
||||||
match Pin::new(&mut this.stream).poll_next(cx) {
|
match Pin::new(&mut this.stream).poll_next(cx) {
|
||||||
Poll::Ready(Some(Ok(chunk))) => {
|
Poll::Ready(Some(Ok(chunk))) => {
|
||||||
this.buf.extend_from_slice(&chunk[..]);
|
this.buf.extend_from_slice(&chunk[..]);
|
||||||
|
|
|
@ -3,6 +3,14 @@
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 0.5.0-beta.2 - 2021-03-09
|
||||||
|
* Preserve doc comments when using route macros. [#2022]
|
||||||
|
* Add `name` attribute to `route` macro. [#1934]
|
||||||
|
|
||||||
|
[#2022]: https://github.com/actix/actix-web/pull/2022
|
||||||
|
[#1934]: https://github.com/actix/actix-web/pull/1934
|
||||||
|
|
||||||
|
|
||||||
## 0.5.0-beta.1 - 2021-02-10
|
## 0.5.0-beta.1 - 2021-02-10
|
||||||
* Use new call signature for `System::new`.
|
* Use new call signature for `System::new`.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-web-codegen"
|
name = "actix-web-codegen"
|
||||||
version = "0.5.0-beta.1"
|
version = "0.5.0-beta.2"
|
||||||
description = "Routing and runtime macros for Actix Web"
|
description = "Routing and runtime macros for Actix Web"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
|
@ -19,8 +19,8 @@ syn = { version = "1", features = ["full", "parsing"] }
|
||||||
proc-macro2 = "1"
|
proc-macro2 = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2"
|
actix-rt = "2.1"
|
||||||
actix-web = "4.0.0-beta.3"
|
actix-web = "4.0.0-beta.4"
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false }
|
||||||
trybuild = "1"
|
trybuild = "1"
|
||||||
rustversion = "1"
|
rustversion = "1"
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
> Routing and runtime macros for Actix Web.
|
> Routing and runtime macros for Actix Web.
|
||||||
|
|
||||||
[](https://crates.io/crates/actix-web-codegen)
|
[](https://crates.io/crates/actix-web-codegen)
|
||||||
[](https://docs.rs/actix-web-codegen/0.5.0-beta.1)
|
[](https://docs.rs/actix-web-codegen/0.5.0-beta.2)
|
||||||
[](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-codegen/0.5.0-beta.1)
|
[](https://deps.rs/crate/actix-web-codegen/0.5.0-beta.2)
|
||||||
[](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://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,7 @@ mod route;
|
||||||
///
|
///
|
||||||
/// # Attributes
|
/// # Attributes
|
||||||
/// - `"path"` - Raw literal string with path for which to register handler.
|
/// - `"path"` - Raw literal string with path for which to register handler.
|
||||||
|
/// - `name="resource_name"` - Specifies resource name for the handler. If not set, the function name of handler is used.
|
||||||
/// - `method="HTTP_METHOD"` - Registers HTTP method to provide guard for. Upper-case string, "GET", "POST" for example.
|
/// - `method="HTTP_METHOD"` - Registers HTTP method to provide guard for. Upper-case string, "GET", "POST" for example.
|
||||||
/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||||
/// - `wrap="Middleware"` - Registers a resource middleware.
|
/// - `wrap="Middleware"` - Registers a resource middleware.
|
||||||
|
@ -81,7 +82,7 @@ mod route;
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// # use actix_web::HttpResponse;
|
/// # use actix_web::HttpResponse;
|
||||||
/// # use actix_web_codegen::route;
|
/// # use actix_web_codegen::route;
|
||||||
/// #[route("/test", method="GET", method="HEAD")]
|
/// #[route("/test", method="GET", method="HEAD")]
|
||||||
|
@ -116,6 +117,7 @@ Creates route handler with `actix_web::guard::", stringify!($variant), "`.
|
||||||
|
|
||||||
# Attributes
|
# Attributes
|
||||||
- `"path"` - Raw literal string with path for which to register handler.
|
- `"path"` - Raw literal string with path for which to register handler.
|
||||||
|
- `name="resource_name"` - Specifies resource name for the handler. If not set, the function name of handler is used.
|
||||||
- `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`.
|
- `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`.
|
||||||
- `wrap="Middleware"` - Registers a resource middleware.
|
- `wrap="Middleware"` - Registers a resource middleware.
|
||||||
|
|
||||||
|
@ -125,7 +127,7 @@ code, e.g `my_guard` or `my_module::my_guard`.
|
||||||
|
|
||||||
# Example
|
# Example
|
||||||
|
|
||||||
```rust
|
```
|
||||||
# use actix_web::HttpResponse;
|
# use actix_web::HttpResponse;
|
||||||
# use actix_web_codegen::"#, stringify!($method), ";
|
# use actix_web_codegen::"#, stringify!($method), ";
|
||||||
#[", stringify!($method), r#"("/")]
|
#[", stringify!($method), r#"("/")]
|
||||||
|
@ -160,7 +162,7 @@ method_macro! {
|
||||||
/// This macro can be applied with `#[actix_web::main]` when used in Actix Web applications.
|
/// This macro can be applied with `#[actix_web::main]` when used in Actix Web applications.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```rust
|
/// ```
|
||||||
/// #[actix_web_codegen::main]
|
/// #[actix_web_codegen::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// async { println!("Hello world"); }.await
|
/// async { println!("Hello world"); }.await
|
||||||
|
|
|
@ -78,6 +78,7 @@ impl TryFrom<&syn::LitStr> for MethodType {
|
||||||
|
|
||||||
struct Args {
|
struct Args {
|
||||||
path: syn::LitStr,
|
path: syn::LitStr,
|
||||||
|
resource_name: Option<syn::LitStr>,
|
||||||
guards: Vec<Ident>,
|
guards: Vec<Ident>,
|
||||||
wrappers: Vec<syn::Type>,
|
wrappers: Vec<syn::Type>,
|
||||||
methods: HashSet<MethodType>,
|
methods: HashSet<MethodType>,
|
||||||
|
@ -86,6 +87,7 @@ struct Args {
|
||||||
impl Args {
|
impl Args {
|
||||||
fn new(args: AttributeArgs, method: Option<MethodType>) -> syn::Result<Self> {
|
fn new(args: AttributeArgs, method: Option<MethodType>) -> syn::Result<Self> {
|
||||||
let mut path = None;
|
let mut path = None;
|
||||||
|
let mut resource_name = None;
|
||||||
let mut guards = Vec::new();
|
let mut guards = Vec::new();
|
||||||
let mut wrappers = Vec::new();
|
let mut wrappers = Vec::new();
|
||||||
let mut methods = HashSet::new();
|
let mut methods = HashSet::new();
|
||||||
|
@ -109,7 +111,16 @@ impl Args {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
NestedMeta::Meta(syn::Meta::NameValue(nv)) => {
|
NestedMeta::Meta(syn::Meta::NameValue(nv)) => {
|
||||||
if nv.path.is_ident("guard") {
|
if nv.path.is_ident("name") {
|
||||||
|
if let syn::Lit::Str(lit) = nv.lit {
|
||||||
|
resource_name = Some(lit);
|
||||||
|
} else {
|
||||||
|
return Err(syn::Error::new_spanned(
|
||||||
|
nv.lit,
|
||||||
|
"Attribute name expects literal string!",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else if nv.path.is_ident("guard") {
|
||||||
if let syn::Lit::Str(lit) = nv.lit {
|
if let syn::Lit::Str(lit) = nv.lit {
|
||||||
guards.push(Ident::new(&lit.value(), Span::call_site()));
|
guards.push(Ident::new(&lit.value(), Span::call_site()));
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,6 +175,7 @@ impl Args {
|
||||||
}
|
}
|
||||||
Ok(Args {
|
Ok(Args {
|
||||||
path: path.unwrap(),
|
path: path.unwrap(),
|
||||||
|
resource_name,
|
||||||
guards,
|
guards,
|
||||||
wrappers,
|
wrappers,
|
||||||
methods,
|
methods,
|
||||||
|
@ -176,6 +188,9 @@ pub struct Route {
|
||||||
args: Args,
|
args: Args,
|
||||||
ast: syn::ItemFn,
|
ast: syn::ItemFn,
|
||||||
resource_type: ResourceType,
|
resource_type: ResourceType,
|
||||||
|
|
||||||
|
/// The doc comment attributes to copy to generated struct, if any.
|
||||||
|
doc_attributes: Vec<syn::Attribute>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn guess_resource_type(typ: &syn::Type) -> ResourceType {
|
fn guess_resource_type(typ: &syn::Type) -> ResourceType {
|
||||||
|
@ -221,6 +236,18 @@ impl Route {
|
||||||
let ast: syn::ItemFn = syn::parse(input)?;
|
let ast: syn::ItemFn = syn::parse(input)?;
|
||||||
let name = ast.sig.ident.clone();
|
let name = ast.sig.ident.clone();
|
||||||
|
|
||||||
|
// Try and pull out the doc comments so that we can reapply them to the
|
||||||
|
// generated struct.
|
||||||
|
//
|
||||||
|
// Note that multi line doc comments are converted to multiple doc
|
||||||
|
// attributes.
|
||||||
|
let doc_attributes = ast
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.filter(|attr| attr.path.is_ident("doc"))
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
let args = Args::new(args, method)?;
|
let args = Args::new(args, method)?;
|
||||||
if args.methods.is_empty() {
|
if args.methods.is_empty() {
|
||||||
return Err(syn::Error::new(
|
return Err(syn::Error::new(
|
||||||
|
@ -248,6 +275,7 @@ impl Route {
|
||||||
args,
|
args,
|
||||||
ast,
|
ast,
|
||||||
resource_type,
|
resource_type,
|
||||||
|
doc_attributes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,13 +288,17 @@ impl ToTokens for Route {
|
||||||
args:
|
args:
|
||||||
Args {
|
Args {
|
||||||
path,
|
path,
|
||||||
|
resource_name,
|
||||||
guards,
|
guards,
|
||||||
wrappers,
|
wrappers,
|
||||||
methods,
|
methods,
|
||||||
},
|
},
|
||||||
resource_type,
|
resource_type,
|
||||||
|
doc_attributes,
|
||||||
} = self;
|
} = self;
|
||||||
let resource_name = name.to_string();
|
let resource_name = resource_name
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| name.to_string(), |n| n.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
|
||||||
|
@ -287,6 +319,7 @@ impl ToTokens for Route {
|
||||||
};
|
};
|
||||||
|
|
||||||
let stream = quote! {
|
let stream = quote! {
|
||||||
|
#(#doc_attributes)*
|
||||||
#[allow(non_camel_case_types, missing_docs)]
|
#[allow(non_camel_case_types, missing_docs)]
|
||||||
pub struct #name;
|
pub struct #name;
|
||||||
|
|
||||||
|
|
|
@ -83,6 +83,13 @@ async fn route_test() -> impl Responder {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/custom_resource_name", name = "custom")]
|
||||||
|
async fn custom_resource_name_test<'a>(req: actix_web::HttpRequest) -> impl Responder {
|
||||||
|
assert!(req.url_for_static("custom").is_ok());
|
||||||
|
assert!(req.url_for_static("custom_resource_name_test").is_err());
|
||||||
|
HttpResponse::Ok()
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ChangeStatusCode;
|
pub struct ChangeStatusCode;
|
||||||
|
|
||||||
impl<S, B> Transform<S, ServiceRequest> for ChangeStatusCode
|
impl<S, B> Transform<S, ServiceRequest> for ChangeStatusCode
|
||||||
|
@ -174,6 +181,7 @@ async fn test_body() {
|
||||||
.service(patch_test)
|
.service(patch_test)
|
||||||
.service(test_handler)
|
.service(test_handler)
|
||||||
.service(route_test)
|
.service(route_test)
|
||||||
|
.service(custom_resource_name_test)
|
||||||
});
|
});
|
||||||
let request = srv.request(http::Method::GET, srv.url("/test"));
|
let request = srv.request(http::Method::GET, srv.url("/test"));
|
||||||
let response = request.send().await.unwrap();
|
let response = request.send().await.unwrap();
|
||||||
|
@ -228,6 +236,10 @@ async fn test_body() {
|
||||||
let request = srv.request(http::Method::PATCH, srv.url("/multi"));
|
let request = srv.request(http::Method::PATCH, srv.url("/multi"));
|
||||||
let response = request.send().await.unwrap();
|
let response = request.send().await.unwrap();
|
||||||
assert!(!response.status().is_success());
|
assert!(!response.status().is_success());
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::GET, srv.url("/custom_resource_name"));
|
||||||
|
let response = request.send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
|
|
@ -9,6 +9,8 @@ fn compile_macros() {
|
||||||
t.compile_fail("tests/trybuild/route-missing-method-fail.rs");
|
t.compile_fail("tests/trybuild/route-missing-method-fail.rs");
|
||||||
t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs");
|
t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs");
|
||||||
t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs");
|
t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs");
|
||||||
|
|
||||||
|
t.pass("tests/trybuild/docstring-ok.rs");
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[rustversion::not(nightly)]
|
// #[rustversion::not(nightly)]
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
use actix_web::{Responder, HttpResponse, App, test};
|
||||||
|
use actix_web_codegen::*;
|
||||||
|
|
||||||
|
/// Docstrings shouldn't break anything.
|
||||||
|
#[get("/")]
|
||||||
|
async fn index() -> impl Responder {
|
||||||
|
HttpResponse::Ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() {
|
||||||
|
let srv = test::start(|| App::new().service(index));
|
||||||
|
|
||||||
|
let request = srv.get("/");
|
||||||
|
let response = request.send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
|
@ -1,20 +1,31 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Changed
|
||||||
|
* `ConnectorService` type is renamed to `BoxConnectorService` [#2081]
|
||||||
|
|
||||||
|
[#2081]: https://github.com/actix/actix-web/pull/2081
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-beta.3 - 2021-03-08
|
||||||
### Added
|
### Added
|
||||||
* `ClientResponse::timeout` for set the timeout of collecting response body. [#1931]
|
* `ClientResponse::timeout` for set the timeout of collecting response body. [#1931]
|
||||||
|
* `ClientBuilder::local_address` for bind to a local ip address for this client. [#2024]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Feature `cookies` is now optional and enabled by default. [#1981]
|
* Feature `cookies` is now optional and enabled by default. [#1981]
|
||||||
* `ClientBuilder::connector` method would take `actix_http::client::Connector<T, U>` type. [#2008]
|
* `ClientBuilder::connector` method would take `actix_http::client::Connector<T, U>` type. [#2008]
|
||||||
|
* Basic auth password now takes blank passwords as an empty string instead of Option. [#2050]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `ClientBuilder::default` function [#2008]
|
* `ClientBuilder::default` function [#2008]
|
||||||
* `ClientBuilder::disable_redirects` and `ClientBuilder::max_redirects` method [#2008]
|
|
||||||
|
|
||||||
[#1931]: https://github.com/actix/actix-web/pull/1931
|
[#1931]: https://github.com/actix/actix-web/pull/1931
|
||||||
[#1981]: https://github.com/actix/actix-web/pull/1981
|
[#1981]: https://github.com/actix/actix-web/pull/1981
|
||||||
[#2008]: https://github.com/actix/actix-web/pull/2008
|
[#2008]: https://github.com/actix/actix-web/pull/2008
|
||||||
|
[#2024]: https://github.com/actix/actix-web/pull/2024
|
||||||
|
[#2050]: https://github.com/actix/actix-web/pull/2050
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.2 - 2021-02-10
|
## 3.0.0-beta.2 - 2021-02-10
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "awc"
|
name = "awc"
|
||||||
version = "3.0.0-beta.2"
|
version = "3.0.0-beta.3"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Async HTTP and WebSocket client library built on the Actix ecosystem"
|
description = "Async HTTP and WebSocket client library built on the Actix ecosystem"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -46,17 +46,19 @@ trust-dns = ["actix-http/trust-dns"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-service = "2.0.0-beta.4"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-http = "3.0.0-beta.3"
|
actix-http = "3.0.0-beta.4"
|
||||||
actix-rt = "2"
|
actix-rt = { version = "2.1", default-features = false }
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
cfg-if = "1.0"
|
cfg-if = "1.0"
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
futures-core = { version = "0.3.7", default-features = false }
|
futures-core = { version = "0.3.7", default-features = false }
|
||||||
|
itoa = "0.4"
|
||||||
log =" 0.4"
|
log =" 0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
|
pin-project-lite = "0.2"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
@ -64,23 +66,17 @@ serde_urlencoded = "0.7"
|
||||||
tls-openssl = { version = "0.10.9", package = "openssl", optional = true }
|
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"] }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies.tls-openssl]
|
|
||||||
version = "0.10.9"
|
|
||||||
package = "openssl"
|
|
||||||
features = ["vendored"]
|
|
||||||
optional = true
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.3", features = ["openssl"] }
|
actix-web = { version = "4.0.0-beta.4", features = ["openssl"] }
|
||||||
actix-http = { version = "3.0.0-beta.3", features = ["openssl"] }
|
actix-http = { version = "3.0.0-beta.4", features = ["openssl"] }
|
||||||
actix-http-test = { version = "3.0.0-beta.2", features = ["openssl"] }
|
actix-http-test = { version = "3.0.0-beta.3", features = ["openssl"] }
|
||||||
actix-utils = "3.0.0-beta.1"
|
actix-utils = "3.0.0-beta.1"
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-tls = { version = "3.0.0-beta.3", features = ["openssl", "rustls"] }
|
actix-tls = { version = "3.0.0-beta.4", features = ["openssl", "rustls"] }
|
||||||
|
|
||||||
brotli2 = "0.3.2"
|
brotli2 = "0.3.2"
|
||||||
|
env_logger = "0.8"
|
||||||
flate2 = "1.0.13"
|
flate2 = "1.0.13"
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false }
|
||||||
env_logger = "0.8"
|
|
||||||
rcgen = "0.8"
|
rcgen = "0.8"
|
||||||
webpki = "0.21"
|
webpki = "0.21"
|
||||||
|
|
|
@ -3,19 +3,20 @@
|
||||||
> 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.2)
|
[](https://docs.rs/awc/3.0.0-beta.3)
|
||||||

|

|
||||||
[](https://deps.rs/crate/awc/3.0.0-beta.2)
|
[](https://deps.rs/crate/awc/3.0.0-beta.3)
|
||||||
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
## 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/awc_https)
|
- [Example Project](https://github.com/actix/examples/tree/HEAD/security/awc_https)
|
||||||
- [Chat on Gitter](https://gitter.im/actix/actix-web)
|
- [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
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use awc::Client;
|
use awc::Client;
|
||||||
|
@ -26,7 +27,7 @@ fn main() {
|
||||||
|
|
||||||
let res = client
|
let res = client
|
||||||
.get("http://www.rust-lang.org") // <- Create request builder
|
.get("http://www.rust-lang.org") // <- Create request builder
|
||||||
.header("User-Agent", "Actix-web")
|
.insert_header(("User-Agent", "Actix-web"))
|
||||||
.send() // <- Send http request
|
.send() // <- Send http request
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
|
|
@ -1,31 +1,37 @@
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::net::IpAddr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
client::{Connector, TcpConnect, TcpConnectError, TcpConnection},
|
client::{Connector, ConnectorService, TcpConnect, TcpConnectError, TcpConnection},
|
||||||
http::{self, header, Error as HttpError, HeaderMap, HeaderName, Uri},
|
http::{self, header, Error as HttpError, HeaderMap, HeaderName, Uri},
|
||||||
};
|
};
|
||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::Service;
|
use actix_service::{boxed, Service};
|
||||||
|
|
||||||
use crate::connect::ConnectorWrapper;
|
use crate::connect::DefaultConnector;
|
||||||
use crate::{Client, ClientConfig};
|
use crate::error::SendRequestError;
|
||||||
|
use crate::middleware::{NestTransform, Redirect, Transform};
|
||||||
|
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse};
|
||||||
|
|
||||||
/// An HTTP Client builder
|
/// An HTTP Client builder
|
||||||
///
|
///
|
||||||
/// This type can be used to construct an instance of `Client` through a
|
/// This type can be used to construct an instance of `Client` through a
|
||||||
/// builder-like pattern.
|
/// builder-like pattern.
|
||||||
pub struct ClientBuilder<T = (), U = ()> {
|
pub struct ClientBuilder<S = (), M = ()> {
|
||||||
default_headers: bool,
|
default_headers: bool,
|
||||||
max_http_version: Option<http::Version>,
|
max_http_version: Option<http::Version>,
|
||||||
stream_window_size: Option<u32>,
|
stream_window_size: Option<u32>,
|
||||||
conn_window_size: Option<u32>,
|
conn_window_size: Option<u32>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
connector: Connector<T, U>,
|
connector: Connector<S>,
|
||||||
|
middleware: M,
|
||||||
|
local_address: Option<IpAddr>,
|
||||||
|
max_redirects: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientBuilder {
|
impl ClientBuilder {
|
||||||
|
@ -36,21 +42,24 @@ impl ClientBuilder {
|
||||||
Response = TcpConnection<Uri, TcpStream>,
|
Response = TcpConnection<Uri, TcpStream>,
|
||||||
Error = TcpConnectError,
|
Error = TcpConnectError,
|
||||||
> + Clone,
|
> + Clone,
|
||||||
TcpStream,
|
(),
|
||||||
> {
|
> {
|
||||||
ClientBuilder {
|
ClientBuilder {
|
||||||
|
middleware: (),
|
||||||
default_headers: true,
|
default_headers: true,
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
timeout: Some(Duration::from_secs(5)),
|
timeout: Some(Duration::from_secs(5)),
|
||||||
|
local_address: None,
|
||||||
connector: Connector::new(),
|
connector: Connector::new(),
|
||||||
max_http_version: None,
|
max_http_version: None,
|
||||||
stream_window_size: None,
|
stream_window_size: None,
|
||||||
conn_window_size: None,
|
conn_window_size: None,
|
||||||
|
max_redirects: 10,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, Io> ClientBuilder<S, Io>
|
impl<S, Io, M> ClientBuilder<S, M>
|
||||||
where
|
where
|
||||||
S: Service<TcpConnect<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
S: Service<TcpConnect<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
||||||
+ Clone
|
+ Clone
|
||||||
|
@ -58,7 +67,7 @@ where
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
Io: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
/// Use custom connector service.
|
/// Use custom connector service.
|
||||||
pub fn connector<S1, Io1>(self, connector: Connector<S1, Io1>) -> ClientBuilder<S1, Io1>
|
pub fn connector<S1, Io1>(self, connector: Connector<S1>) -> ClientBuilder<S1, M>
|
||||||
where
|
where
|
||||||
S1: Service<
|
S1: Service<
|
||||||
TcpConnect<Uri>,
|
TcpConnect<Uri>,
|
||||||
|
@ -69,13 +78,16 @@ where
|
||||||
Io1: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
Io1: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
ClientBuilder {
|
ClientBuilder {
|
||||||
|
middleware: self.middleware,
|
||||||
default_headers: self.default_headers,
|
default_headers: self.default_headers,
|
||||||
headers: self.headers,
|
headers: self.headers,
|
||||||
timeout: self.timeout,
|
timeout: self.timeout,
|
||||||
|
local_address: self.local_address,
|
||||||
connector,
|
connector,
|
||||||
max_http_version: self.max_http_version,
|
max_http_version: self.max_http_version,
|
||||||
stream_window_size: self.stream_window_size,
|
stream_window_size: self.stream_window_size,
|
||||||
conn_window_size: self.conn_window_size,
|
conn_window_size: self.conn_window_size,
|
||||||
|
max_redirects: self.max_redirects,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +106,12 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set local IP Address the connector would use for establishing connection.
|
||||||
|
pub fn local_address(mut self, addr: IpAddr) -> Self {
|
||||||
|
self.local_address = Some(addr);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Maximum supported HTTP major version.
|
/// Maximum supported HTTP major version.
|
||||||
///
|
///
|
||||||
/// Supported versions are HTTP/1.1 and HTTP/2.
|
/// Supported versions are HTTP/1.1 and HTTP/2.
|
||||||
|
@ -102,6 +120,22 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Do not follow redirects.
|
||||||
|
///
|
||||||
|
/// Redirects are allowed by default.
|
||||||
|
pub fn disable_redirects(mut self) -> Self {
|
||||||
|
self.max_redirects = 0;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set max number of redirects.
|
||||||
|
///
|
||||||
|
/// Max redirects is set to 10 by default.
|
||||||
|
pub fn max_redirects(mut self, num: u8) -> Self {
|
||||||
|
self.max_redirects = num;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Indicates the initial window size (in octets) for
|
/// Indicates the initial window size (in octets) for
|
||||||
/// HTTP2 stream-level flow control for received data.
|
/// HTTP2 stream-level flow control for received data.
|
||||||
///
|
///
|
||||||
|
@ -171,8 +205,55 @@ where
|
||||||
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
|
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Registers middleware, in the form of a middleware component (type),
|
||||||
|
/// that runs during inbound and/or outbound processing in the request
|
||||||
|
/// life-cycle (request -> response), modifying request/response as
|
||||||
|
/// necessary, across all requests managed by the Client.
|
||||||
|
pub fn wrap<S1, M1>(
|
||||||
|
self,
|
||||||
|
mw: M1,
|
||||||
|
) -> ClientBuilder<S, NestTransform<M, M1, S1, ConnectRequest>>
|
||||||
|
where
|
||||||
|
M: Transform<S1, ConnectRequest>,
|
||||||
|
M1: Transform<M::Transform, ConnectRequest>,
|
||||||
|
{
|
||||||
|
ClientBuilder {
|
||||||
|
middleware: NestTransform::new(self.middleware, mw),
|
||||||
|
default_headers: self.default_headers,
|
||||||
|
max_http_version: self.max_http_version,
|
||||||
|
stream_window_size: self.stream_window_size,
|
||||||
|
conn_window_size: self.conn_window_size,
|
||||||
|
headers: self.headers,
|
||||||
|
timeout: self.timeout,
|
||||||
|
connector: self.connector,
|
||||||
|
local_address: self.local_address,
|
||||||
|
max_redirects: self.max_redirects,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Finish build process and create `Client` instance.
|
/// Finish build process and create `Client` instance.
|
||||||
pub fn finish(self) -> Client {
|
pub fn finish(self) -> Client
|
||||||
|
where
|
||||||
|
M: Transform<DefaultConnector<ConnectorService<S, Io>>, ConnectRequest> + 'static,
|
||||||
|
M::Transform:
|
||||||
|
Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError>,
|
||||||
|
{
|
||||||
|
let redirect_time = self.max_redirects;
|
||||||
|
|
||||||
|
if redirect_time > 0 {
|
||||||
|
self.wrap(Redirect::new().max_redirect_times(redirect_time))
|
||||||
|
._finish()
|
||||||
|
} else {
|
||||||
|
self._finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _finish(self) -> Client
|
||||||
|
where
|
||||||
|
M: Transform<DefaultConnector<ConnectorService<S, Io>>, ConnectRequest> + 'static,
|
||||||
|
M::Transform:
|
||||||
|
Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError>,
|
||||||
|
{
|
||||||
let mut connector = self.connector;
|
let mut connector = self.connector;
|
||||||
|
|
||||||
if let Some(val) = self.max_http_version {
|
if let Some(val) = self.max_http_version {
|
||||||
|
@ -184,14 +265,18 @@ where
|
||||||
if let Some(val) = self.stream_window_size {
|
if let Some(val) = self.stream_window_size {
|
||||||
connector = connector.initial_window_size(val)
|
connector = connector.initial_window_size(val)
|
||||||
};
|
};
|
||||||
|
if let Some(val) = self.local_address {
|
||||||
|
connector = connector.local_address(val);
|
||||||
|
}
|
||||||
|
|
||||||
let config = ClientConfig {
|
let connector = DefaultConnector::new(connector.finish());
|
||||||
headers: self.headers,
|
let connector = boxed::rc_service(self.middleware.new_transform(connector));
|
||||||
|
|
||||||
|
Client(ClientConfig {
|
||||||
|
headers: Rc::new(self.headers),
|
||||||
timeout: self.timeout,
|
timeout: self.timeout,
|
||||||
connector: Box::new(ConnectorWrapper::new(connector.finish())) as _,
|
connector,
|
||||||
};
|
})
|
||||||
|
|
||||||
Client(Rc::new(config))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,32 +1,26 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt, io, net,
|
future::Future,
|
||||||
|
net,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
|
rc::Rc,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed, ReadBuf};
|
use actix_codec::Framed;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::Body,
|
body::Body,
|
||||||
client::{Connect as ClientConnect, ConnectError, Connection, SendRequestError},
|
client::{
|
||||||
|
Connect as ClientConnect, ConnectError, Connection, ConnectionIo, SendRequestError,
|
||||||
|
},
|
||||||
h1::ClientCodec,
|
h1::ClientCodec,
|
||||||
RequestHead, RequestHeadType, ResponseHead,
|
Payload, RequestHead, RequestHeadType, ResponseHead,
|
||||||
};
|
};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::{future::LocalBoxFuture, ready};
|
||||||
|
|
||||||
use crate::response::ClientResponse;
|
use crate::response::ClientResponse;
|
||||||
|
|
||||||
pub(crate) struct ConnectorWrapper<T> {
|
pub type BoxConnectorService = Rc<
|
||||||
connector: T,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> ConnectorWrapper<T> {
|
|
||||||
pub(crate) fn new(connector: T) -> Self {
|
|
||||||
Self { connector }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type ConnectService = Box<
|
|
||||||
dyn Service<
|
dyn Service<
|
||||||
ConnectRequest,
|
ConnectRequest,
|
||||||
Response = ConnectResponse,
|
Response = ConnectResponse,
|
||||||
|
@ -35,6 +29,8 @@ pub type ConnectService = Box<
|
||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
pub type BoxedSocket = Box<dyn ConnectionIo>;
|
||||||
|
|
||||||
pub enum ConnectRequest {
|
pub enum ConnectRequest {
|
||||||
Client(RequestHeadType, Body, Option<net::SocketAddr>),
|
Client(RequestHeadType, Body, Option<net::SocketAddr>),
|
||||||
Tunnel(RequestHead, Option<net::SocketAddr>),
|
Tunnel(RequestHead, Option<net::SocketAddr>),
|
||||||
|
@ -65,16 +61,24 @@ impl ConnectResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Service<ConnectRequest> for ConnectorWrapper<T>
|
pub struct DefaultConnector<S> {
|
||||||
|
connector: S,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> DefaultConnector<S> {
|
||||||
|
pub(crate) fn new(connector: S) -> Self {
|
||||||
|
Self { connector }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, Io> Service<ConnectRequest> for DefaultConnector<S>
|
||||||
where
|
where
|
||||||
T: Service<ClientConnect, Error = ConnectError>,
|
S: Service<ClientConnect, Error = ConnectError, Response = Connection<Io>>,
|
||||||
T::Response: Connection,
|
Io: ConnectionIo,
|
||||||
<T::Response as Connection>::Io: 'static,
|
|
||||||
T::Future: 'static,
|
|
||||||
{
|
{
|
||||||
type Response = ConnectResponse;
|
type Response = ConnectResponse;
|
||||||
type Error = SendRequestError;
|
type Error = SendRequestError;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
type Future = ConnectRequestFuture<S::Future, Io>;
|
||||||
|
|
||||||
actix_service::forward_ready!(connector);
|
actix_service::forward_ready!(connector);
|
||||||
|
|
||||||
|
@ -91,81 +95,77 @@ where
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::pin(async move {
|
ConnectRequestFuture::Connection {
|
||||||
let connection = fut.await?;
|
fut,
|
||||||
|
req: Some(req),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pin_project_lite::pin_project! {
|
||||||
|
#[project = ConnectRequestProj]
|
||||||
|
pub enum ConnectRequestFuture<Fut, Io>
|
||||||
|
where
|
||||||
|
Io: ConnectionIo
|
||||||
|
{
|
||||||
|
Connection {
|
||||||
|
#[pin]
|
||||||
|
fut: Fut,
|
||||||
|
req: Option<ConnectRequest>
|
||||||
|
},
|
||||||
|
Client {
|
||||||
|
fut: LocalBoxFuture<'static, Result<(ResponseHead, Payload), SendRequestError>>
|
||||||
|
},
|
||||||
|
Tunnel {
|
||||||
|
fut: LocalBoxFuture<
|
||||||
|
'static,
|
||||||
|
Result<(ResponseHead, Framed<Connection<Io>, ClientCodec>), SendRequestError>,
|
||||||
|
>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Fut, Io> Future for ConnectRequestFuture<Fut, Io>
|
||||||
|
where
|
||||||
|
Fut: Future<Output = Result<Connection<Io>, ConnectError>>,
|
||||||
|
Io: ConnectionIo,
|
||||||
|
{
|
||||||
|
type Output = Result<ConnectResponse, SendRequestError>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
match self.as_mut().project() {
|
||||||
|
ConnectRequestProj::Connection { fut, req } => {
|
||||||
|
let connection = ready!(fut.poll(cx))?;
|
||||||
|
let req = req.take().unwrap();
|
||||||
match req {
|
match req {
|
||||||
ConnectRequest::Client(head, body, ..) => {
|
ConnectRequest::Client(head, body, ..) => {
|
||||||
// send request
|
// send request
|
||||||
let (head, payload) = connection.send_request(head, body).await?;
|
let fut = ConnectRequestFuture::Client {
|
||||||
|
fut: connection.send_request(head, body),
|
||||||
Ok(ConnectResponse::Client(ClientResponse::new(head, payload)))
|
};
|
||||||
|
self.set(fut);
|
||||||
}
|
}
|
||||||
ConnectRequest::Tunnel(head, ..) => {
|
ConnectRequest::Tunnel(head, ..) => {
|
||||||
// send request
|
// send request
|
||||||
let (head, framed) =
|
let fut = ConnectRequestFuture::Tunnel {
|
||||||
connection.open_tunnel(RequestHeadType::from(head)).await?;
|
fut: connection.open_tunnel(RequestHeadType::from(head)),
|
||||||
|
};
|
||||||
let framed = framed.into_map_io(|io| BoxedSocket(Box::new(Socket(io))));
|
self.set(fut);
|
||||||
Ok(ConnectResponse::Tunnel(head, framed))
|
}
|
||||||
|
}
|
||||||
|
self.poll(cx)
|
||||||
|
}
|
||||||
|
ConnectRequestProj::Client { fut } => {
|
||||||
|
let (head, payload) = ready!(fut.as_mut().poll(cx))?;
|
||||||
|
Poll::Ready(Ok(ConnectResponse::Client(ClientResponse::new(
|
||||||
|
head, payload,
|
||||||
|
))))
|
||||||
|
}
|
||||||
|
ConnectRequestProj::Tunnel { fut } => {
|
||||||
|
let (head, framed) = ready!(fut.as_mut().poll(cx))?;
|
||||||
|
let framed = framed.into_map_io(|io| Box::new(io) as _);
|
||||||
|
Poll::Ready(Ok(ConnectResponse::Tunnel(head, framed)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait AsyncSocket {
|
|
||||||
fn as_read(&self) -> &(dyn AsyncRead + Unpin);
|
|
||||||
fn as_read_mut(&mut self) -> &mut (dyn AsyncRead + Unpin);
|
|
||||||
fn as_write(&mut self) -> &mut (dyn AsyncWrite + Unpin);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Socket<T: AsyncRead + AsyncWrite + Unpin>(T);
|
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite + Unpin> AsyncSocket for Socket<T> {
|
|
||||||
fn as_read(&self) -> &(dyn AsyncRead + Unpin) {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
fn as_read_mut(&mut self) -> &mut (dyn AsyncRead + Unpin) {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
fn as_write(&mut self) -> &mut (dyn AsyncWrite + Unpin) {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct BoxedSocket(Box<dyn AsyncSocket>);
|
|
||||||
|
|
||||||
impl fmt::Debug for BoxedSocket {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "BoxedSocket")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AsyncRead for BoxedSocket {
|
|
||||||
fn poll_read(
|
|
||||||
self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
buf: &mut ReadBuf<'_>,
|
|
||||||
) -> Poll<io::Result<()>> {
|
|
||||||
Pin::new(self.get_mut().0.as_read_mut()).poll_read(cx, buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AsyncWrite for BoxedSocket {
|
|
||||||
fn poll_write(
|
|
||||||
self: Pin<&mut Self>,
|
|
||||||
cx: &mut Context<'_>,
|
|
||||||
buf: &[u8],
|
|
||||||
) -> Poll<io::Result<usize>> {
|
|
||||||
Pin::new(self.get_mut().0.as_write()).poll_write(cx, buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
||||||
Pin::new(self.get_mut().0.as_write()).poll_flush(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
||||||
Pin::new(self.get_mut().0.as_write()).poll_shutdown(cx)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,24 +18,31 @@ pub enum WsClientError {
|
||||||
/// Invalid response status
|
/// Invalid response status
|
||||||
#[display(fmt = "Invalid response status")]
|
#[display(fmt = "Invalid response status")]
|
||||||
InvalidResponseStatus(StatusCode),
|
InvalidResponseStatus(StatusCode),
|
||||||
|
|
||||||
/// Invalid upgrade header
|
/// Invalid upgrade header
|
||||||
#[display(fmt = "Invalid upgrade header")]
|
#[display(fmt = "Invalid upgrade header")]
|
||||||
InvalidUpgradeHeader,
|
InvalidUpgradeHeader,
|
||||||
|
|
||||||
/// Invalid connection header
|
/// Invalid connection header
|
||||||
#[display(fmt = "Invalid connection header")]
|
#[display(fmt = "Invalid connection header")]
|
||||||
InvalidConnectionHeader(HeaderValue),
|
InvalidConnectionHeader(HeaderValue),
|
||||||
/// Missing CONNECTION header
|
|
||||||
#[display(fmt = "Missing CONNECTION header")]
|
/// Missing Connection header
|
||||||
|
#[display(fmt = "Missing Connection header")]
|
||||||
MissingConnectionHeader,
|
MissingConnectionHeader,
|
||||||
/// Missing SEC-WEBSOCKET-ACCEPT header
|
|
||||||
#[display(fmt = "Missing SEC-WEBSOCKET-ACCEPT header")]
|
/// Missing Sec-Websocket-Accept header
|
||||||
|
#[display(fmt = "Missing Sec-Websocket-Accept header")]
|
||||||
MissingWebSocketAcceptHeader,
|
MissingWebSocketAcceptHeader,
|
||||||
|
|
||||||
/// Invalid challenge response
|
/// Invalid challenge response
|
||||||
#[display(fmt = "Invalid challenge response")]
|
#[display(fmt = "Invalid challenge response")]
|
||||||
InvalidChallengeResponse(String, HeaderValue),
|
InvalidChallengeResponse([u8; 28], HeaderValue),
|
||||||
|
|
||||||
/// Protocol error
|
/// Protocol error
|
||||||
#[display(fmt = "{}", _0)]
|
#[display(fmt = "{}", _0)]
|
||||||
Protocol(WsProtocolError),
|
Protocol(WsProtocolError),
|
||||||
|
|
||||||
/// Send request error
|
/// Send request error
|
||||||
#[display(fmt = "{}", _0)]
|
#[display(fmt = "{}", _0)]
|
||||||
SendRequest(SendRequestError),
|
SendRequest(SendRequestError),
|
||||||
|
|
|
@ -23,7 +23,7 @@ pub struct FrozenClientRequest {
|
||||||
pub(crate) addr: Option<net::SocketAddr>,
|
pub(crate) addr: Option<net::SocketAddr>,
|
||||||
pub(crate) response_decompress: bool,
|
pub(crate) response_decompress: bool,
|
||||||
pub(crate) timeout: Option<Duration>,
|
pub(crate) timeout: Option<Duration>,
|
||||||
pub(crate) config: Rc<ClientConfig>,
|
pub(crate) config: ClientConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FrozenClientRequest {
|
impl FrozenClientRequest {
|
||||||
|
@ -51,7 +51,7 @@ impl FrozenClientRequest {
|
||||||
self.addr,
|
self.addr,
|
||||||
self.response_decompress,
|
self.response_decompress,
|
||||||
self.timeout,
|
self.timeout,
|
||||||
self.config.as_ref(),
|
&self.config,
|
||||||
body,
|
body,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ impl FrozenClientRequest {
|
||||||
self.addr,
|
self.addr,
|
||||||
self.response_decompress,
|
self.response_decompress,
|
||||||
self.timeout,
|
self.timeout,
|
||||||
self.config.as_ref(),
|
&self.config,
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ impl FrozenClientRequest {
|
||||||
self.addr,
|
self.addr,
|
||||||
self.response_decompress,
|
self.response_decompress,
|
||||||
self.timeout,
|
self.timeout,
|
||||||
self.config.as_ref(),
|
&self.config,
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ impl FrozenClientRequest {
|
||||||
self.addr,
|
self.addr,
|
||||||
self.response_decompress,
|
self.response_decompress,
|
||||||
self.timeout,
|
self.timeout,
|
||||||
self.config.as_ref(),
|
&self.config,
|
||||||
stream,
|
stream,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ impl FrozenClientRequest {
|
||||||
self.addr,
|
self.addr,
|
||||||
self.response_decompress,
|
self.response_decompress,
|
||||||
self.timeout,
|
self.timeout,
|
||||||
self.config.as_ref(),
|
&self.config,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ impl FrozenSendBuilder {
|
||||||
self.req.addr,
|
self.req.addr,
|
||||||
self.req.response_decompress,
|
self.req.response_decompress,
|
||||||
self.req.timeout,
|
self.req.timeout,
|
||||||
self.req.config.as_ref(),
|
&self.req.config,
|
||||||
body,
|
body,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -183,7 +183,7 @@ impl FrozenSendBuilder {
|
||||||
self.req.addr,
|
self.req.addr,
|
||||||
self.req.response_decompress,
|
self.req.response_decompress,
|
||||||
self.req.timeout,
|
self.req.timeout,
|
||||||
self.req.config.as_ref(),
|
&self.req.config,
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -198,7 +198,7 @@ impl FrozenSendBuilder {
|
||||||
self.req.addr,
|
self.req.addr,
|
||||||
self.req.response_decompress,
|
self.req.response_decompress,
|
||||||
self.req.timeout,
|
self.req.timeout,
|
||||||
self.req.config.as_ref(),
|
&self.req.config,
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -217,7 +217,7 @@ impl FrozenSendBuilder {
|
||||||
self.req.addr,
|
self.req.addr,
|
||||||
self.req.response_decompress,
|
self.req.response_decompress,
|
||||||
self.req.timeout,
|
self.req.timeout,
|
||||||
self.req.config.as_ref(),
|
&self.req.config,
|
||||||
stream,
|
stream,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -232,7 +232,7 @@ impl FrozenSendBuilder {
|
||||||
self.req.addr,
|
self.req.addr,
|
||||||
self.req.response_decompress,
|
self.req.response_decompress,
|
||||||
self.req.timeout,
|
self.req.timeout,
|
||||||
self.req.config.as_ref(),
|
&self.req.config,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,6 +113,7 @@ mod builder;
|
||||||
mod connect;
|
mod connect;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod frozen;
|
mod frozen;
|
||||||
|
pub mod middleware;
|
||||||
mod request;
|
mod request;
|
||||||
mod response;
|
mod response;
|
||||||
mod sender;
|
mod sender;
|
||||||
|
@ -120,19 +121,17 @@ pub mod test;
|
||||||
pub mod ws;
|
pub mod ws;
|
||||||
|
|
||||||
pub use self::builder::ClientBuilder;
|
pub use self::builder::ClientBuilder;
|
||||||
pub use self::connect::{BoxedSocket, ConnectRequest, ConnectResponse, ConnectService};
|
pub use self::connect::{BoxConnectorService, BoxedSocket, ConnectRequest, ConnectResponse};
|
||||||
pub use self::frozen::{FrozenClientRequest, FrozenSendBuilder};
|
pub use self::frozen::{FrozenClientRequest, FrozenSendBuilder};
|
||||||
pub use self::request::ClientRequest;
|
pub use self::request::ClientRequest;
|
||||||
pub use self::response::{ClientResponse, JsonBody, MessageBody};
|
pub use self::response::{ClientResponse, JsonBody, MessageBody};
|
||||||
pub use self::sender::SendClientRequest;
|
pub use self::sender::SendClientRequest;
|
||||||
|
|
||||||
use self::connect::ConnectorWrapper;
|
|
||||||
|
|
||||||
/// An asynchronous HTTP and WebSocket client.
|
/// An asynchronous HTTP and WebSocket client.
|
||||||
///
|
///
|
||||||
/// ## Examples
|
/// ## Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use awc::Client;
|
/// use awc::Client;
|
||||||
///
|
///
|
||||||
/// #[actix_rt::main]
|
/// #[actix_rt::main]
|
||||||
|
@ -148,21 +147,18 @@ use self::connect::ConnectorWrapper;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Client(Rc<ClientConfig>);
|
pub struct Client(ClientConfig);
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub(crate) struct ClientConfig {
|
pub(crate) struct ClientConfig {
|
||||||
pub(crate) connector: ConnectService,
|
pub(crate) connector: BoxConnectorService,
|
||||||
pub(crate) headers: HeaderMap,
|
pub(crate) headers: Rc<HeaderMap>,
|
||||||
pub(crate) timeout: Option<Duration>,
|
pub(crate) timeout: Option<Duration>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Client {
|
impl Default for Client {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Client(Rc::new(ClientConfig {
|
ClientBuilder::new().finish()
|
||||||
connector: Box::new(ConnectorWrapper::new(Connector::new().finish())),
|
|
||||||
headers: HeaderMap::new(),
|
|
||||||
timeout: Some(Duration::from_secs(5)),
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +176,6 @@ impl Client {
|
||||||
Response = TcpConnection<Uri, TcpStream>,
|
Response = TcpConnection<Uri, TcpStream>,
|
||||||
Error = TcpConnectError,
|
Error = TcpConnectError,
|
||||||
> + Clone,
|
> + Clone,
|
||||||
TcpStream,
|
|
||||||
> {
|
> {
|
||||||
ClientBuilder::new()
|
ClientBuilder::new()
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
mod redirect;
|
||||||
|
|
||||||
|
pub use self::redirect::Redirect;
|
||||||
|
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use actix_service::Service;
|
||||||
|
|
||||||
|
/// Trait for transform a type to another one.
|
||||||
|
/// Both the input and output type should impl [actix_service::Service] trait.
|
||||||
|
pub trait Transform<S, Req> {
|
||||||
|
type Transform: Service<Req>;
|
||||||
|
|
||||||
|
/// Creates and returns a new Transform component.
|
||||||
|
fn new_transform(self, service: S) -> Self::Transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
/// Helper struct for constructing Nested types that would call `Transform::new_transform`
|
||||||
|
/// in a chain.
|
||||||
|
///
|
||||||
|
/// The child field would be called first and the output `Service` type is
|
||||||
|
/// passed to parent as input type.
|
||||||
|
pub struct NestTransform<T1, T2, S, Req>
|
||||||
|
where
|
||||||
|
T1: Transform<S, Req>,
|
||||||
|
T2: Transform<T1::Transform, Req>,
|
||||||
|
{
|
||||||
|
child: T1,
|
||||||
|
parent: T2,
|
||||||
|
_service: PhantomData<(S, Req)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T1, T2, S, Req> NestTransform<T1, T2, S, Req>
|
||||||
|
where
|
||||||
|
T1: Transform<S, Req>,
|
||||||
|
T2: Transform<T1::Transform, Req>,
|
||||||
|
{
|
||||||
|
pub(crate) fn new(child: T1, parent: T2) -> Self {
|
||||||
|
NestTransform {
|
||||||
|
child,
|
||||||
|
parent,
|
||||||
|
_service: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T1, T2, S, Req> Transform<S, Req> for NestTransform<T1, T2, S, Req>
|
||||||
|
where
|
||||||
|
T1: Transform<S, Req>,
|
||||||
|
T2: Transform<T1::Transform, Req>,
|
||||||
|
{
|
||||||
|
type Transform = T2::Transform;
|
||||||
|
|
||||||
|
fn new_transform(self, service: S) -> Self::Transform {
|
||||||
|
let service = self.child.new_transform(service);
|
||||||
|
self.parent.new_transform(service)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dummy impl for kick start `NestTransform` type in `ClientBuilder` type
|
||||||
|
impl<S, Req> Transform<S, Req> for ()
|
||||||
|
where
|
||||||
|
S: Service<Req>,
|
||||||
|
{
|
||||||
|
type Transform = S;
|
||||||
|
|
||||||
|
fn new_transform(self, service: S) -> Self::Transform {
|
||||||
|
service
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,351 @@
|
||||||
|
use std::{
|
||||||
|
convert::TryFrom,
|
||||||
|
future::Future,
|
||||||
|
net::SocketAddr,
|
||||||
|
pin::Pin,
|
||||||
|
rc::Rc,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
|
use actix_http::{
|
||||||
|
body::Body,
|
||||||
|
client::{InvalidUrl, SendRequestError},
|
||||||
|
http::{header, Method, StatusCode, Uri},
|
||||||
|
RequestHead, RequestHeadType,
|
||||||
|
};
|
||||||
|
use actix_service::Service;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures_core::ready;
|
||||||
|
|
||||||
|
use super::Transform;
|
||||||
|
|
||||||
|
use crate::connect::{ConnectRequest, ConnectResponse};
|
||||||
|
use crate::ClientResponse;
|
||||||
|
|
||||||
|
pub struct Redirect {
|
||||||
|
max_redirect_times: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Redirect {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Redirect {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
max_redirect_times: 10,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn max_redirect_times(mut self, times: u8) -> Self {
|
||||||
|
self.max_redirect_times = times;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Transform<S, ConnectRequest> for Redirect
|
||||||
|
where
|
||||||
|
S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError> + 'static,
|
||||||
|
{
|
||||||
|
type Transform = RedirectService<S>;
|
||||||
|
|
||||||
|
fn new_transform(self, service: S) -> Self::Transform {
|
||||||
|
RedirectService {
|
||||||
|
max_redirect_times: self.max_redirect_times,
|
||||||
|
connector: Rc::new(service),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RedirectService<S> {
|
||||||
|
max_redirect_times: u8,
|
||||||
|
connector: Rc<S>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Service<ConnectRequest> for RedirectService<S>
|
||||||
|
where
|
||||||
|
S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError> + 'static,
|
||||||
|
{
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future = RedirectServiceFuture<S>;
|
||||||
|
|
||||||
|
actix_service::forward_ready!(connector);
|
||||||
|
|
||||||
|
fn call(&self, req: ConnectRequest) -> Self::Future {
|
||||||
|
match req {
|
||||||
|
ConnectRequest::Tunnel(head, addr) => {
|
||||||
|
let fut = self.connector.call(ConnectRequest::Tunnel(head, addr));
|
||||||
|
RedirectServiceFuture::Tunnel { fut }
|
||||||
|
}
|
||||||
|
ConnectRequest::Client(head, body, addr) => {
|
||||||
|
let connector = self.connector.clone();
|
||||||
|
let max_redirect_times = self.max_redirect_times;
|
||||||
|
|
||||||
|
// backup the uri and method for reuse schema and authority.
|
||||||
|
let (uri, method) = match head {
|
||||||
|
RequestHeadType::Owned(ref head) => (head.uri.clone(), head.method.clone()),
|
||||||
|
RequestHeadType::Rc(ref head, ..) => {
|
||||||
|
(head.uri.clone(), head.method.clone())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let body_opt = match body {
|
||||||
|
Body::Bytes(ref b) => Some(b.clone()),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let fut = connector.call(ConnectRequest::Client(head, body, addr));
|
||||||
|
|
||||||
|
RedirectServiceFuture::Client {
|
||||||
|
fut,
|
||||||
|
max_redirect_times,
|
||||||
|
uri: Some(uri),
|
||||||
|
method: Some(method),
|
||||||
|
body: body_opt,
|
||||||
|
addr,
|
||||||
|
connector: Some(connector),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pin_project_lite::pin_project! {
|
||||||
|
#[project = RedirectServiceProj]
|
||||||
|
pub enum RedirectServiceFuture<S>
|
||||||
|
where
|
||||||
|
S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError>,
|
||||||
|
S: 'static
|
||||||
|
{
|
||||||
|
Tunnel { #[pin] fut: S::Future },
|
||||||
|
Client {
|
||||||
|
#[pin]
|
||||||
|
fut: S::Future,
|
||||||
|
max_redirect_times: u8,
|
||||||
|
uri: Option<Uri>,
|
||||||
|
method: Option<Method>,
|
||||||
|
body: Option<Bytes>,
|
||||||
|
addr: Option<SocketAddr>,
|
||||||
|
connector: Option<Rc<S>>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Future for RedirectServiceFuture<S>
|
||||||
|
where
|
||||||
|
S: Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError> + 'static,
|
||||||
|
{
|
||||||
|
type Output = Result<ConnectResponse, SendRequestError>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
match self.as_mut().project() {
|
||||||
|
RedirectServiceProj::Tunnel { fut } => fut.poll(cx),
|
||||||
|
RedirectServiceProj::Client {
|
||||||
|
fut,
|
||||||
|
max_redirect_times,
|
||||||
|
uri,
|
||||||
|
method,
|
||||||
|
body,
|
||||||
|
addr,
|
||||||
|
connector,
|
||||||
|
} => match ready!(fut.poll(cx))? {
|
||||||
|
ConnectResponse::Client(res) => match res.head().status {
|
||||||
|
StatusCode::MOVED_PERMANENTLY
|
||||||
|
| StatusCode::FOUND
|
||||||
|
| StatusCode::SEE_OTHER
|
||||||
|
if *max_redirect_times > 0 =>
|
||||||
|
{
|
||||||
|
let org_uri = uri.take().unwrap();
|
||||||
|
// rebuild uri from the location header value.
|
||||||
|
let uri = rebuild_uri(&res, org_uri)?;
|
||||||
|
|
||||||
|
// reset method
|
||||||
|
let method = method.take().unwrap();
|
||||||
|
let method = match method {
|
||||||
|
Method::GET | Method::HEAD => method,
|
||||||
|
_ => Method::GET,
|
||||||
|
};
|
||||||
|
|
||||||
|
// take ownership of states that could be reused
|
||||||
|
let addr = addr.take();
|
||||||
|
let connector = connector.take();
|
||||||
|
let mut max_redirect_times = *max_redirect_times;
|
||||||
|
|
||||||
|
// use a new request head.
|
||||||
|
let mut head = RequestHead::default();
|
||||||
|
head.uri = uri.clone();
|
||||||
|
head.method = method.clone();
|
||||||
|
|
||||||
|
let head = RequestHeadType::Owned(head);
|
||||||
|
|
||||||
|
max_redirect_times -= 1;
|
||||||
|
|
||||||
|
let fut = connector
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
// remove body
|
||||||
|
.call(ConnectRequest::Client(head, Body::None, addr));
|
||||||
|
|
||||||
|
self.set(RedirectServiceFuture::Client {
|
||||||
|
fut,
|
||||||
|
max_redirect_times,
|
||||||
|
uri: Some(uri),
|
||||||
|
method: Some(method),
|
||||||
|
// body is dropped on 301,302,303
|
||||||
|
body: None,
|
||||||
|
addr,
|
||||||
|
connector,
|
||||||
|
});
|
||||||
|
|
||||||
|
self.poll(cx)
|
||||||
|
}
|
||||||
|
StatusCode::TEMPORARY_REDIRECT | StatusCode::PERMANENT_REDIRECT
|
||||||
|
if *max_redirect_times > 0 =>
|
||||||
|
{
|
||||||
|
let org_uri = uri.take().unwrap();
|
||||||
|
// rebuild uri from the location header value.
|
||||||
|
let uri = rebuild_uri(&res, org_uri)?;
|
||||||
|
|
||||||
|
// try to reuse body
|
||||||
|
let body = body.take();
|
||||||
|
let body_new = match body {
|
||||||
|
Some(ref bytes) => Body::Bytes(bytes.clone()),
|
||||||
|
// TODO: should this be Body::Empty or Body::None.
|
||||||
|
_ => Body::Empty,
|
||||||
|
};
|
||||||
|
|
||||||
|
let addr = addr.take();
|
||||||
|
let method = method.take().unwrap();
|
||||||
|
let connector = connector.take();
|
||||||
|
let mut max_redirect_times = *max_redirect_times;
|
||||||
|
|
||||||
|
// use a new request head.
|
||||||
|
let mut head = RequestHead::default();
|
||||||
|
head.uri = uri.clone();
|
||||||
|
head.method = method.clone();
|
||||||
|
|
||||||
|
let head = RequestHeadType::Owned(head);
|
||||||
|
|
||||||
|
max_redirect_times -= 1;
|
||||||
|
|
||||||
|
let fut = connector
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.call(ConnectRequest::Client(head, body_new, addr));
|
||||||
|
|
||||||
|
self.set(RedirectServiceFuture::Client {
|
||||||
|
fut,
|
||||||
|
max_redirect_times,
|
||||||
|
uri: Some(uri),
|
||||||
|
method: Some(method),
|
||||||
|
body,
|
||||||
|
addr,
|
||||||
|
connector,
|
||||||
|
});
|
||||||
|
|
||||||
|
self.poll(cx)
|
||||||
|
}
|
||||||
|
_ => Poll::Ready(Ok(ConnectResponse::Client(res))),
|
||||||
|
},
|
||||||
|
_ => unreachable!("ConnectRequest::Tunnel is not handled by Redirect"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rebuild_uri(res: &ClientResponse, org_uri: Uri) -> Result<Uri, SendRequestError> {
|
||||||
|
let uri = res
|
||||||
|
.headers()
|
||||||
|
.get(header::LOCATION)
|
||||||
|
.map(|value| {
|
||||||
|
// try to parse the location to a full uri
|
||||||
|
let uri = Uri::try_from(value.as_bytes())
|
||||||
|
.map_err(|e| SendRequestError::Url(InvalidUrl::HttpError(e.into())))?;
|
||||||
|
if uri.scheme().is_none() || uri.authority().is_none() {
|
||||||
|
let uri = Uri::builder()
|
||||||
|
.scheme(org_uri.scheme().cloned().unwrap())
|
||||||
|
.authority(org_uri.authority().cloned().unwrap())
|
||||||
|
.path_and_query(value.as_bytes())
|
||||||
|
.build()?;
|
||||||
|
Ok::<_, SendRequestError>(uri)
|
||||||
|
} else {
|
||||||
|
Ok(uri)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// TODO: this error type is wrong.
|
||||||
|
.ok_or(SendRequestError::Url(InvalidUrl::MissingScheme))??;
|
||||||
|
|
||||||
|
Ok(uri)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use actix_web::{test::start, web, App, Error, HttpResponse};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::ClientBuilder;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_basic_redirect() {
|
||||||
|
let client = ClientBuilder::new()
|
||||||
|
.disable_redirects()
|
||||||
|
.wrap(Redirect::new().max_redirect_times(10))
|
||||||
|
.finish();
|
||||||
|
|
||||||
|
let srv = start(|| {
|
||||||
|
App::new()
|
||||||
|
.service(web::resource("/test").route(web::to(|| async {
|
||||||
|
Ok::<_, Error>(HttpResponse::BadRequest())
|
||||||
|
})))
|
||||||
|
.service(web::resource("/").route(web::to(|| async {
|
||||||
|
Ok::<_, Error>(
|
||||||
|
HttpResponse::Found()
|
||||||
|
.append_header(("location", "/test"))
|
||||||
|
.finish(),
|
||||||
|
)
|
||||||
|
})))
|
||||||
|
});
|
||||||
|
|
||||||
|
let res = client.get(srv.url("/")).send().await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(res.status().as_u16(), 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_redirect_limit() {
|
||||||
|
let client = ClientBuilder::new()
|
||||||
|
.disable_redirects()
|
||||||
|
.wrap(Redirect::new().max_redirect_times(1))
|
||||||
|
.connector(crate::Connector::new())
|
||||||
|
.finish();
|
||||||
|
|
||||||
|
let srv = start(|| {
|
||||||
|
App::new()
|
||||||
|
.service(web::resource("/").route(web::to(|| async {
|
||||||
|
Ok::<_, Error>(
|
||||||
|
HttpResponse::Found()
|
||||||
|
.append_header(("location", "/test"))
|
||||||
|
.finish(),
|
||||||
|
)
|
||||||
|
})))
|
||||||
|
.service(web::resource("/test").route(web::to(|| async {
|
||||||
|
Ok::<_, Error>(
|
||||||
|
HttpResponse::Found()
|
||||||
|
.append_header(("location", "/test2"))
|
||||||
|
.finish(),
|
||||||
|
)
|
||||||
|
})))
|
||||||
|
.service(web::resource("/test2").route(web::to(|| async {
|
||||||
|
Ok::<_, Error>(HttpResponse::BadRequest())
|
||||||
|
})))
|
||||||
|
});
|
||||||
|
|
||||||
|
let res = client.get(srv.url("/")).send().await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(res.status().as_u16(), 302);
|
||||||
|
}
|
||||||
|
}
|
|
@ -36,7 +36,7 @@ cfg_if::cfg_if! {
|
||||||
/// This type can be used to construct an instance of `ClientRequest` through a
|
/// This type can be used to construct an instance of `ClientRequest` through a
|
||||||
/// builder-like pattern.
|
/// builder-like pattern.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// #[actix_rt::main]
|
/// #[actix_rt::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let response = awc::Client::new()
|
/// let response = awc::Client::new()
|
||||||
|
@ -57,7 +57,7 @@ pub struct ClientRequest {
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
response_decompress: bool,
|
response_decompress: bool,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
config: Rc<ClientConfig>,
|
config: ClientConfig,
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: Option<CookieJar>,
|
cookies: Option<CookieJar>,
|
||||||
|
@ -65,7 +65,7 @@ pub struct ClientRequest {
|
||||||
|
|
||||||
impl ClientRequest {
|
impl ClientRequest {
|
||||||
/// Create new client request builder.
|
/// Create new client request builder.
|
||||||
pub(crate) fn new<U>(method: Method, uri: U, config: Rc<ClientConfig>) -> Self
|
pub(crate) fn new<U>(method: Method, uri: U, config: ClientConfig) -> Self
|
||||||
where
|
where
|
||||||
Uri: TryFrom<U>,
|
Uri: TryFrom<U>,
|
||||||
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
||||||
|
@ -190,7 +190,7 @@ impl ClientRequest {
|
||||||
|
|
||||||
/// Append a header, keeping any that were set with an equivalent field name.
|
/// Append a header, keeping any that were set with an equivalent field name.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// # #[actix_rt::main]
|
/// # #[actix_rt::main]
|
||||||
/// # async fn main() {
|
/// # async fn main() {
|
||||||
/// # use awc::Client;
|
/// # use awc::Client;
|
||||||
|
@ -248,35 +248,30 @@ impl ClientRequest {
|
||||||
/// Set content length
|
/// Set content length
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn content_length(self, len: u64) -> Self {
|
pub fn content_length(self, len: u64) -> Self {
|
||||||
self.append_header((header::CONTENT_LENGTH, len))
|
let mut buf = itoa::Buffer::new();
|
||||||
|
self.insert_header((header::CONTENT_LENGTH, buf.format(len)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set HTTP basic authorization header
|
/// Set HTTP basic authorization header.
|
||||||
pub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Self
|
///
|
||||||
where
|
/// If no password is needed, just provide an empty string.
|
||||||
U: fmt::Display,
|
pub fn basic_auth(self, username: impl fmt::Display, password: impl fmt::Display) -> Self {
|
||||||
{
|
let auth = format!("{}:{}", username, password);
|
||||||
let auth = match password {
|
|
||||||
Some(password) => format!("{}:{}", username, password),
|
self.insert_header((
|
||||||
None => format!("{}:", username),
|
|
||||||
};
|
|
||||||
self.append_header((
|
|
||||||
header::AUTHORIZATION,
|
header::AUTHORIZATION,
|
||||||
format!("Basic {}", base64::encode(&auth)),
|
format!("Basic {}", base64::encode(&auth)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set HTTP bearer authentication header
|
/// Set HTTP bearer authentication header
|
||||||
pub fn bearer_auth<T>(self, token: T) -> Self
|
pub fn bearer_auth(self, token: impl fmt::Display) -> Self {
|
||||||
where
|
self.insert_header((header::AUTHORIZATION, format!("Bearer {}", token)))
|
||||||
T: fmt::Display,
|
|
||||||
{
|
|
||||||
self.append_header((header::AUTHORIZATION, format!("Bearer {}", token)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a cookie
|
/// Set a cookie
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// #[actix_rt::main]
|
/// #[actix_rt::main]
|
||||||
/// async fn main() {
|
/// async fn main() {
|
||||||
/// let resp = awc::Client::new().get("https://www.rust-lang.org")
|
/// let resp = awc::Client::new().get("https://www.rust-lang.org")
|
||||||
|
@ -403,7 +398,7 @@ impl ClientRequest {
|
||||||
slf.addr,
|
slf.addr,
|
||||||
slf.response_decompress,
|
slf.response_decompress,
|
||||||
slf.timeout,
|
slf.timeout,
|
||||||
slf.config.as_ref(),
|
&slf.config,
|
||||||
body,
|
body,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -419,7 +414,7 @@ impl ClientRequest {
|
||||||
slf.addr,
|
slf.addr,
|
||||||
slf.response_decompress,
|
slf.response_decompress,
|
||||||
slf.timeout,
|
slf.timeout,
|
||||||
slf.config.as_ref(),
|
&slf.config,
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -437,7 +432,7 @@ impl ClientRequest {
|
||||||
slf.addr,
|
slf.addr,
|
||||||
slf.response_decompress,
|
slf.response_decompress,
|
||||||
slf.timeout,
|
slf.timeout,
|
||||||
slf.config.as_ref(),
|
&slf.config,
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -457,7 +452,7 @@ impl ClientRequest {
|
||||||
slf.addr,
|
slf.addr,
|
||||||
slf.response_decompress,
|
slf.response_decompress,
|
||||||
slf.timeout,
|
slf.timeout,
|
||||||
slf.config.as_ref(),
|
&slf.config,
|
||||||
stream,
|
stream,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -473,7 +468,7 @@ impl ClientRequest {
|
||||||
slf.addr,
|
slf.addr,
|
||||||
slf.response_decompress,
|
slf.response_decompress,
|
||||||
slf.timeout,
|
slf.timeout,
|
||||||
slf.config.as_ref(),
|
&slf.config,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -643,9 +638,7 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn client_basic_auth() {
|
async fn client_basic_auth() {
|
||||||
let req = Client::new()
|
let req = Client::new().get("/").basic_auth("username", "password");
|
||||||
.get("/")
|
|
||||||
.basic_auth("username", Some("password"));
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
req.head
|
req.head
|
||||||
.headers
|
.headers
|
||||||
|
@ -656,7 +649,7 @@ mod tests {
|
||||||
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
||||||
);
|
);
|
||||||
|
|
||||||
let req = Client::new().get("/").basic_auth("username", None);
|
let req = Client::new().get("/").basic_auth("username", "");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
req.head
|
req.head
|
||||||
.headers
|
.headers
|
||||||
|
|
|
@ -228,12 +228,13 @@ impl<S> fmt::Debug for ClientResponse<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_BODY_LIMIT: usize = 2 * 1024 * 1024;
|
||||||
|
|
||||||
/// Future that resolves to a complete HTTP message body.
|
/// Future that resolves to a complete HTTP message body.
|
||||||
pub struct MessageBody<S> {
|
pub struct MessageBody<S> {
|
||||||
length: Option<usize>,
|
length: Option<usize>,
|
||||||
err: Option<PayloadError>,
|
|
||||||
timeout: ResponseTimeout,
|
timeout: ResponseTimeout,
|
||||||
fut: Option<ReadBody<S>>,
|
body: Result<ReadBody<S>, Option<PayloadError>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> MessageBody<S>
|
impl<S> MessageBody<S>
|
||||||
|
@ -242,41 +243,38 @@ where
|
||||||
{
|
{
|
||||||
/// Create `MessageBody` for request.
|
/// Create `MessageBody` for request.
|
||||||
pub fn new(res: &mut ClientResponse<S>) -> MessageBody<S> {
|
pub fn new(res: &mut ClientResponse<S>) -> MessageBody<S> {
|
||||||
let mut len = None;
|
let length = match res.headers().get(&header::CONTENT_LENGTH) {
|
||||||
if let Some(l) = res.headers().get(&header::CONTENT_LENGTH) {
|
Some(value) => {
|
||||||
if let Ok(s) = l.to_str() {
|
let len = value.to_str().ok().and_then(|s| s.parse::<usize>().ok());
|
||||||
if let Ok(l) = s.parse::<usize>() {
|
|
||||||
len = Some(l)
|
match len {
|
||||||
} else {
|
None => return Self::err(PayloadError::UnknownLength),
|
||||||
return Self::err(PayloadError::UnknownLength);
|
len => len,
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Self::err(PayloadError::UnknownLength);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
MessageBody {
|
MessageBody {
|
||||||
length: len,
|
length,
|
||||||
err: None,
|
|
||||||
timeout: std::mem::take(&mut res.timeout),
|
timeout: std::mem::take(&mut res.timeout),
|
||||||
fut: Some(ReadBody::new(res.take_payload(), 262_144)),
|
body: Ok(ReadBody::new(res.take_payload(), DEFAULT_BODY_LIMIT)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Change max size of payload. By default max size is 256kB
|
/// Change max size of payload. By default max size is 2048kB
|
||||||
pub fn limit(mut self, limit: usize) -> Self {
|
pub fn limit(mut self, limit: usize) -> Self {
|
||||||
if let Some(ref mut fut) = self.fut {
|
if let Ok(ref mut body) = self.body {
|
||||||
fut.limit = limit;
|
body.limit = limit;
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn err(e: PayloadError) -> Self {
|
fn err(e: PayloadError) -> Self {
|
||||||
MessageBody {
|
MessageBody {
|
||||||
fut: None,
|
|
||||||
err: Some(e),
|
|
||||||
length: None,
|
length: None,
|
||||||
timeout: ResponseTimeout::default(),
|
timeout: ResponseTimeout::default(),
|
||||||
|
body: Err(Some(e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -290,19 +288,20 @@ where
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
|
|
||||||
if let Some(err) = this.err.take() {
|
match this.body {
|
||||||
return Poll::Ready(Err(err));
|
Err(ref mut err) => Poll::Ready(Err(err.take().unwrap())),
|
||||||
}
|
Ok(ref mut body) => {
|
||||||
|
|
||||||
if let Some(len) = this.length.take() {
|
if let Some(len) = this.length.take() {
|
||||||
if len > this.fut.as_ref().unwrap().limit {
|
if len > body.limit {
|
||||||
return Poll::Ready(Err(PayloadError::Overflow));
|
return Poll::Ready(Err(PayloadError::Overflow));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.timeout.poll_timeout(cx)?;
|
this.timeout.poll_timeout(cx)?;
|
||||||
|
|
||||||
Pin::new(&mut this.fut.as_mut().unwrap()).poll(cx)
|
Pin::new(body).poll(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,7 +414,7 @@ impl<S> ReadBody<S> {
|
||||||
fn new(stream: Payload<S>, limit: usize) -> Self {
|
fn new(stream: Payload<S>, limit: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
stream,
|
stream,
|
||||||
buf: BytesMut::with_capacity(std::cmp::min(limit, 32768)),
|
buf: BytesMut::new(),
|
||||||
limit,
|
limit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -430,20 +429,14 @@ where
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
|
|
||||||
loop {
|
while let Some(chunk) = ready!(Pin::new(&mut this.stream).poll_next(cx)?) {
|
||||||
return match Pin::new(&mut this.stream).poll_next(cx)? {
|
|
||||||
Poll::Ready(Some(chunk)) => {
|
|
||||||
if (this.buf.len() + chunk.len()) > this.limit {
|
if (this.buf.len() + chunk.len()) > this.limit {
|
||||||
Poll::Ready(Err(PayloadError::Overflow))
|
return Poll::Ready(Err(PayloadError::Overflow));
|
||||||
} else {
|
}
|
||||||
this.buf.extend_from_slice(&chunk);
|
this.buf.extend_from_slice(&chunk);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Poll::Ready(None) => Poll::Ready(Ok(this.buf.split().freeze())),
|
|
||||||
Poll::Pending => Poll::Pending,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Poll::Ready(Ok(this.buf.split().freeze()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,7 +455,7 @@ mod tests {
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "10000000").finish();
|
||||||
match req.body().await.err().unwrap() {
|
match req.body().await.err().unwrap() {
|
||||||
PayloadError::Overflow => {}
|
PayloadError::Overflow => {}
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
|
@ -492,9 +485,7 @@ mod tests {
|
||||||
JsonPayloadError::Payload(PayloadError::Overflow) => {
|
JsonPayloadError::Payload(PayloadError::Overflow) => {
|
||||||
matches!(other, JsonPayloadError::Payload(PayloadError::Overflow))
|
matches!(other, JsonPayloadError::Payload(PayloadError::Overflow))
|
||||||
}
|
}
|
||||||
JsonPayloadError::ContentType => {
|
JsonPayloadError::ContentType => matches!(other, JsonPayloadError::ContentType),
|
||||||
matches!(other, JsonPayloadError::ContentType)
|
|
||||||
}
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::rc::Rc;
|
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
|
|
||||||
use actix_codec::Framed;
|
use actix_codec::Framed;
|
||||||
|
@ -56,7 +55,7 @@ pub struct WebsocketsRequest {
|
||||||
addr: Option<SocketAddr>,
|
addr: Option<SocketAddr>,
|
||||||
max_size: usize,
|
max_size: usize,
|
||||||
server_mode: bool,
|
server_mode: bool,
|
||||||
config: Rc<ClientConfig>,
|
config: ClientConfig,
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: Option<CookieJar>,
|
cookies: Option<CookieJar>,
|
||||||
|
@ -64,7 +63,7 @@ pub struct WebsocketsRequest {
|
||||||
|
|
||||||
impl WebsocketsRequest {
|
impl WebsocketsRequest {
|
||||||
/// Create new WebSocket connection
|
/// Create new WebSocket connection
|
||||||
pub(crate) fn new<U>(uri: U, config: Rc<ClientConfig>) -> Self
|
pub(crate) fn new<U>(uri: U, config: ClientConfig) -> Self
|
||||||
where
|
where
|
||||||
Uri: TryFrom<U>,
|
Uri: TryFrom<U>,
|
||||||
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
||||||
|
@ -381,12 +380,14 @@ impl WebsocketsRequest {
|
||||||
|
|
||||||
if let Some(hdr_key) = head.headers.get(&header::SEC_WEBSOCKET_ACCEPT) {
|
if let Some(hdr_key) = head.headers.get(&header::SEC_WEBSOCKET_ACCEPT) {
|
||||||
let encoded = ws::hash_key(key.as_ref());
|
let encoded = ws::hash_key(key.as_ref());
|
||||||
if hdr_key.as_bytes() != encoded.as_bytes() {
|
|
||||||
|
if hdr_key.as_bytes() != encoded {
|
||||||
log::trace!(
|
log::trace!(
|
||||||
"Invalid challenge response: expected: {} received: {:?}",
|
"Invalid challenge response: expected: {:?} received: {:?}",
|
||||||
encoded,
|
&encoded,
|
||||||
key
|
key
|
||||||
);
|
);
|
||||||
|
|
||||||
return Err(WsClientError::InvalidChallengeResponse(
|
return Err(WsClientError::InvalidChallengeResponse(
|
||||||
encoded,
|
encoded,
|
||||||
hdr_key.clone(),
|
hdr_key.clone(),
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -828,7 +829,7 @@ async fn client_basic_auth() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
== "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
== format!("Basic {}", base64::encode("username:password"))
|
||||||
{
|
{
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
} else {
|
} else {
|
||||||
|
@ -839,7 +840,7 @@ async fn client_basic_auth() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// set authorization header to Basic <base64 encoded username:password>
|
// set authorization header to Basic <base64 encoded username:password>
|
||||||
let request = srv.get("/").basic_auth("username", Some("password"));
|
let request = srv.get("/").basic_auth("username", "password");
|
||||||
let response = request.send().await.unwrap();
|
let response = request.send().await.unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
@ -871,3 +872,34 @@ async fn client_bearer_auth() {
|
||||||
let response = request.send().await.unwrap();
|
let response = request.send().await.unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_local_address() {
|
||||||
|
let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
|
||||||
|
|
||||||
|
let srv = test::start(move || {
|
||||||
|
App::new().service(web::resource("/").route(web::to(
|
||||||
|
move |req: HttpRequest| async move {
|
||||||
|
assert_eq!(req.peer_addr().unwrap().ip(), ip);
|
||||||
|
Ok::<_, Error>(HttpResponse::Ok())
|
||||||
|
},
|
||||||
|
)))
|
||||||
|
});
|
||||||
|
let client = awc::Client::builder().local_address(ip).finish();
|
||||||
|
|
||||||
|
let res = client.get(srv.url("/")).send().await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(res.status(), 200);
|
||||||
|
|
||||||
|
let client = awc::Client::builder()
|
||||||
|
.connector(
|
||||||
|
// connector local address setting should always be override by client builder.
|
||||||
|
awc::Connector::new().local_address(IpAddr::V4(Ipv4Addr::new(128, 0, 0, 1))),
|
||||||
|
)
|
||||||
|
.local_address(ip)
|
||||||
|
.finish();
|
||||||
|
|
||||||
|
let res = client.get(srv.url("/")).send().await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(res.status(), 200);
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ use actix_web::test::{init_service, ok_service, TestRequest};
|
||||||
|
|
||||||
/// Criterion Benchmark for async Service
|
/// Criterion Benchmark for async Service
|
||||||
/// Should be used from within criterion group:
|
/// Should be used from within criterion group:
|
||||||
/// ```rust,ignore
|
/// ```ignore
|
||||||
/// let mut criterion: ::criterion::Criterion<_> =
|
/// let mut criterion: ::criterion::Criterion<_> =
|
||||||
/// ::criterion::Criterion::default().configure_from_args();
|
/// ::criterion::Criterion::default().configure_from_args();
|
||||||
/// bench_async_service(&mut criterion, ok_service(), "async_service_direct");
|
/// bench_async_service(&mut criterion, ok_service(), "async_service_direct");
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
msrv = "1.46"
|
|
@ -4,10 +4,10 @@ coverage:
|
||||||
status:
|
status:
|
||||||
project:
|
project:
|
||||||
default:
|
default:
|
||||||
threshold: 10% # make CI green
|
threshold: 100% # make CI green
|
||||||
patch:
|
patch:
|
||||||
default:
|
default:
|
||||||
threshold: 10% # make CI green
|
threshold: 100% # make CI green
|
||||||
|
|
||||||
ignore: # ignore code coverage on following paths
|
ignore: # ignore code coverage on following paths
|
||||||
- "**/tests"
|
- "**/tests"
|
||||||
|
|
|
@ -1,35 +1,44 @@
|
||||||
digraph {
|
digraph {
|
||||||
subgraph cluster_web {
|
subgraph cluster_web {
|
||||||
label="actix/actix-web"
|
label="actix/web"
|
||||||
|
|
||||||
"awc"
|
"awc"
|
||||||
"actix-web"
|
"web"
|
||||||
"actix-files"
|
"files"
|
||||||
"actix-http"
|
"http"
|
||||||
"actix-multipart"
|
"multipart"
|
||||||
"actix-web-actors"
|
"web-actors"
|
||||||
"actix-web-codegen"
|
"web-codegen"
|
||||||
"actix-http-test"
|
"http-test"
|
||||||
|
|
||||||
|
{ rank=same; "multipart" "web-actors" "http-test" };
|
||||||
|
{ rank=same; "files" "awc" "web" };
|
||||||
|
{ rank=same; "web-codegen" "http" };
|
||||||
}
|
}
|
||||||
|
|
||||||
"actix-web" -> { "actix-codec" "actix-service" "actix-utils" "actix-router" "actix-rt" "actix-server" "macros" "threadpool" "actix-tls" "actix-web-codegen" "actix-http" "awc" }
|
"web" -> { "codec" "service" "utils" "router" "rt" "server" "macros" "web-codegen" "http" "awc" }
|
||||||
"awc" -> { "actix-codec" "actix-service" "actix-http" "actix-rt" }
|
"web" -> { "tls" }[color=blue] // optional
|
||||||
"actix-web-actors" -> { "actix" "actix-web" "actix-http" "actix-codec" }
|
"awc" -> { "codec" "service" "http" "rt" }
|
||||||
"actix-multipart" -> { "actix-web" "actix-service" "actix-utils" }
|
"web-actors" -> { "actix" "web" "http" "codec" }
|
||||||
"actix-http" -> { "actix-service" "actix-codec" "actix-tls" "actix-utils" "actix-rt" "threadpool" }
|
"multipart" -> { "web" "service" "utils" }
|
||||||
"actix-http" -> { "actix-tls" }[color=blue] // optional
|
"http" -> { "service" "codec" "utils" "rt" }
|
||||||
"actix-files" -> { "actix-web" }
|
"http" -> { "tls" }[color=blue] // optional
|
||||||
"actix-http-test" -> { "actix-service" "actix-codec" "actix-tls" "actix-utils" "actix-rt" "actix-server" "awc" }
|
"files" -> { "web" }
|
||||||
|
"http-test" -> { "service" "codec" "utils" "rt" "server" "awc" }
|
||||||
|
"http-test" -> { "tls" }[color=blue] // optional
|
||||||
|
|
||||||
// net
|
// net
|
||||||
|
|
||||||
"actix-utils" -> { "actix-service" "actix-rt" "actix-codec" }
|
"utils" -> { "service" "rt" "codec" }
|
||||||
"actix-tracing" -> { "actix-service" }
|
"tracing" -> { "service" }
|
||||||
"actix-tls" -> { "actix-service" "actix-codec" "actix-utils" }
|
"tls" -> { "service" "codec" "utils" }
|
||||||
"actix-server" -> { "actix-service" "actix-rt" "actix-codec" "actix-utils" }
|
"server" -> { "service" "rt" "codec" "utils" }
|
||||||
"actix-rt" -> { "macros" "threadpool" }
|
"rt" -> { "macros" }
|
||||||
|
|
||||||
|
{ rank=same; "utils" "codec" };
|
||||||
|
{ rank=same; "rt" "macros" "service" "router" };
|
||||||
|
|
||||||
// actix
|
// actix
|
||||||
|
|
||||||
"actix" -> { "actix-rt" }
|
"actix" -> { "rt" }
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.service(
|
.service(
|
||||||
web::resource("/resource2/index.html")
|
web::resource("/resource2/index.html")
|
||||||
.wrap(middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"))
|
.wrap(middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"))
|
||||||
.default_service(web::route().to(|| HttpResponse::MethodNotAllowed()))
|
.default_service(web::route().to(HttpResponse::MethodNotAllowed))
|
||||||
.route(web::get().to(index_async)),
|
.route(web::get().to(index_async)),
|
||||||
)
|
)
|
||||||
.service(web::resource("/test1.html").to(|| async { "Test\r\n" }))
|
.service(web::resource("/test1.html").to(|| async { "Test\r\n" }))
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//! properties and pass them to a handler through request-local data.
|
//! properties and pass them to a handler through request-local data.
|
||||||
//!
|
//!
|
||||||
//! For an example of extracting a client TLS certificate, see:
|
//! For an example of extracting a client TLS certificate, see:
|
||||||
//! <https://github.com/actix/examples/tree/HEAD/rustls-client-cert>
|
//! <https://github.com/actix/examples/tree/HEAD/security/rustls-client-cert>
|
||||||
|
|
||||||
use std::{any::Any, io, net::SocketAddr};
|
use std::{any::Any, io, net::SocketAddr};
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.service(
|
.service(
|
||||||
web::resource("/resource2/index.html")
|
web::resource("/resource2/index.html")
|
||||||
.wrap(middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"))
|
.wrap(middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"))
|
||||||
.default_service(web::route().to(|| HttpResponse::MethodNotAllowed()))
|
.default_service(web::route().to(HttpResponse::MethodNotAllowed))
|
||||||
.route(web::get().to(index_async)),
|
.route(web::get().to(index_async)),
|
||||||
)
|
)
|
||||||
.service(web::resource("/test1.html").to(|| async { "Test\r\n" }))
|
.service(web::resource("/test1.html").to(|| async { "Test\r\n" }))
|
||||||
|
|
16
src/app.rs
16
src/app.rs
|
@ -79,7 +79,7 @@ where
|
||||||
/// uses `Arc` so data could be created outside of app factory and clones could
|
/// uses `Arc` so data could be created outside of app factory and clones could
|
||||||
/// be stored via `App::app_data()` method.
|
/// be stored via `App::app_data()` method.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use std::cell::Cell;
|
/// use std::cell::Cell;
|
||||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
/// use actix_web::{web, App, HttpResponse, Responder};
|
||||||
///
|
///
|
||||||
|
@ -152,7 +152,7 @@ where
|
||||||
/// different module or even library. For example,
|
/// different module or even library. For example,
|
||||||
/// some of the resource's configuration could be moved to different module.
|
/// some of the resource's configuration could be moved to different module.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_web::{web, App, HttpResponse};
|
/// use actix_web::{web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// // this function could be located in different module
|
/// // this function could be located in different module
|
||||||
|
@ -185,7 +185,7 @@ where
|
||||||
/// This method can be used multiple times with same path, in that case
|
/// This method can be used multiple times with same path, in that case
|
||||||
/// multiple resources with one route would be registered for same resource path.
|
/// multiple resources with one route would be registered for same resource path.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_web::{web, App, HttpResponse};
|
/// use actix_web::{web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// async fn index(data: web::Path<(String, String)>) -> &'static str {
|
/// async fn index(data: web::Path<(String, String)>) -> &'static str {
|
||||||
|
@ -228,7 +228,7 @@ where
|
||||||
///
|
///
|
||||||
/// It is possible to use services like `Resource`, `Route`.
|
/// It is possible to use services like `Resource`, `Route`.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_web::{web, App, HttpResponse};
|
/// use actix_web::{web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// async fn index() -> &'static str {
|
/// async fn index() -> &'static str {
|
||||||
|
@ -246,7 +246,7 @@ where
|
||||||
///
|
///
|
||||||
/// It is also possible to use static files as default service.
|
/// It is also possible to use static files as default service.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_web::{web, App, HttpResponse};
|
/// use actix_web::{web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
|
@ -283,7 +283,7 @@ where
|
||||||
/// and are never considered for matching at request time. Calls to
|
/// and are never considered for matching at request time. Calls to
|
||||||
/// `HttpRequest::url_for()` will work as expected.
|
/// `HttpRequest::url_for()` will work as expected.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_web::{web, App, HttpRequest, HttpResponse, Result};
|
/// use actix_web::{web, App, HttpRequest, HttpResponse, Result};
|
||||||
///
|
///
|
||||||
/// async fn index(req: HttpRequest) -> Result<HttpResponse> {
|
/// async fn index(req: HttpRequest) -> Result<HttpResponse> {
|
||||||
|
@ -325,7 +325,7 @@ where
|
||||||
/// the builder chain. Consequently, the *first* middleware registered
|
/// the builder chain. Consequently, the *first* middleware registered
|
||||||
/// in the builder chain is the *last* to execute during request processing.
|
/// in the builder chain is the *last* to execute during request processing.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_service::Service;
|
/// use actix_service::Service;
|
||||||
/// use actix_web::{middleware, web, App};
|
/// use actix_web::{middleware, web, App};
|
||||||
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
||||||
|
@ -382,7 +382,7 @@ where
|
||||||
///
|
///
|
||||||
/// Use middleware when you need to read or modify *every* request or response in some way.
|
/// Use middleware when you need to read or modify *every* request or response in some way.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use actix_service::Service;
|
/// use actix_service::Service;
|
||||||
/// use actix_web::{web, App};
|
/// use actix_web::{web, App};
|
||||||
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::task::Poll;
|
|
||||||
|
|
||||||
use actix_http::{Extensions, Request, Response};
|
use actix_http::{Extensions, Request, Response};
|
||||||
use actix_router::{Path, ResourceDef, Router, Url};
|
use actix_router::{Path, ResourceDef, Router, Url};
|
||||||
|
|
|
@ -113,7 +113,7 @@ pub struct AppConfig {
|
||||||
|
|
||||||
impl AppConfig {
|
impl AppConfig {
|
||||||
pub(crate) fn new(secure: bool, addr: SocketAddr, host: String) -> Self {
|
pub(crate) fn new(secure: bool, addr: SocketAddr, host: String) -> Self {
|
||||||
AppConfig { secure, addr, host }
|
AppConfig { secure, host, addr }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Server host name.
|
/// Server host name.
|
||||||
|
|
15
src/data.rs
15
src/data.rs
|
@ -5,6 +5,7 @@ use std::sync::Arc;
|
||||||
use actix_http::error::{Error, ErrorInternalServerError};
|
use actix_http::error::{Error, ErrorInternalServerError};
|
||||||
use actix_http::Extensions;
|
use actix_http::Extensions;
|
||||||
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
|
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::dev::Payload;
|
use crate::dev::Payload;
|
||||||
use crate::extract::FromRequest;
|
use crate::extract::FromRequest;
|
||||||
|
@ -36,7 +37,7 @@ 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.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// use std::sync::Mutex;
|
/// use std::sync::Mutex;
|
||||||
/// use actix_web::{web, App, HttpResponse, Responder};
|
/// use actix_web::{web, App, HttpResponse, Responder};
|
||||||
///
|
///
|
||||||
|
@ -102,6 +103,18 @@ impl<T: ?Sized> From<Arc<T>> for Data<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Serialize for Data<T>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
self.0.serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: ?Sized + 'static> FromRequest for Data<T> {
|
impl<T: ?Sized + 'static> FromRequest for Data<T> {
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
|
@ -51,7 +51,7 @@ pub trait FromRequest: Sized {
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// 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};
|
||||||
|
@ -143,7 +143,7 @@ where
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```
|
||||||
/// 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};
|
||||||
|
@ -318,16 +318,16 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
|
||||||
mod m {
|
mod m {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
tuple_from_req!(TupleFromRequest1, (0, A));
|
tuple_from_req!(TupleFromRequest1, (0, A));
|
||||||
tuple_from_req!(TupleFromRequest2, (0, A), (1, B));
|
tuple_from_req!(TupleFromRequest2, (0, A), (1, B));
|
||||||
tuple_from_req!(TupleFromRequest3, (0, A), (1, B), (2, C));
|
tuple_from_req!(TupleFromRequest3, (0, A), (1, B), (2, C));
|
||||||
tuple_from_req!(TupleFromRequest4, (0, A), (1, B), (2, C), (3, D));
|
tuple_from_req!(TupleFromRequest4, (0, A), (1, B), (2, C), (3, D));
|
||||||
tuple_from_req!(TupleFromRequest5, (0, A), (1, B), (2, C), (3, D), (4, E));
|
tuple_from_req!(TupleFromRequest5, (0, A), (1, B), (2, C), (3, D), (4, E));
|
||||||
tuple_from_req!(TupleFromRequest6, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
|
tuple_from_req!(TupleFromRequest6, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
|
||||||
tuple_from_req!(TupleFromRequest7, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
|
tuple_from_req!(TupleFromRequest7, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
|
||||||
tuple_from_req!(TupleFromRequest8, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
|
tuple_from_req!(TupleFromRequest8, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
|
||||||
tuple_from_req!(TupleFromRequest9, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
|
tuple_from_req!(TupleFromRequest9, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
|
||||||
tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
|
tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue