diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 3e62958d8..d8c6d66ca 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,8 +1,15 @@ blank_issues_enabled: true contact_links: - - name: Gitter channel (actix-web) + - name: GitHub Discussions + url: https://github.com/actix/actix-web/discussions + about: Actix Web Q&A + - name: Gitter chat (actix-web) url: https://gitter.im/actix/actix-web - about: Please ask and answer questions about the actix-web here. - - name: Gitter channel (actix) + about: Actix Web Q&A + - name: Gitter chat (actix) url: https://gitter.im/actix/actix - about: Please ask and answer questions about the actix here. + about: Actix (actor framework) Q&A + - name: Actix Discord + url: https://discord.gg/NWpN5mmg3x + about: Actix developer discussion and community chat + diff --git a/.github/workflows/upload-doc.yml b/.github/workflows/upload-doc.yml index 99fa663cc..ba87a5637 100644 --- a/.github/workflows/upload-doc.yml +++ b/.github/workflows/upload-doc.yml @@ -16,7 +16,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: stable-x86_64-unknown-linux-gnu + toolchain: nightly-x86_64-unknown-linux-gnu profile: minimal override: true @@ -30,7 +30,7 @@ jobs: run: echo "" > target/doc/index.html - name: Deploy to GitHub Pages - uses: JamesIves/github-pages-deploy-action@3.5.8 + uses: JamesIves/github-pages-deploy-action@3.7.1 with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} BRANCH: gh-pages diff --git a/CHANGES.md b/CHANGES.md index 5fd3869f9..ee9b9308d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,62 @@ # Changes ## Unreleased - 2020-xx-xx +### Changed +* Bumped `rand` to `0.8` + +### Fixed +* added the actual parsing error to `test::read_body_json` [#1812] + +[#1812]: https://github.com/actix/actix-web/pull/1812 + + + +## 3.3.2 - 2020-12-01 +### Fixed +* Removed an occasional `unwrap` on `None` panic in `NormalizePathNormalization`. [#1762] +* Fix `match_pattern()` returning `None` for scope with empty path resource. [#1798] +* Increase minimum `socket2` version. [#1803] + +[#1762]: https://github.com/actix/actix-web/pull/1762 +[#1798]: https://github.com/actix/actix-web/pull/1798 +[#1803]: https://github.com/actix/actix-web/pull/1803 + + +## 3.3.1 - 2020-11-29 +* Ensure `actix-http` dependency uses same `serde_urlencoded`. + + +## 3.3.0 - 2020-11-25 +### Added +* Add `Either` extractor helper. [#1788] + +### Changed +* Upgrade `serde_urlencoded` to `0.7`. [#1773] + +[#1773]: https://github.com/actix/actix-web/pull/1773 +[#1788]: https://github.com/actix/actix-web/pull/1788 + + +## 3.2.0 - 2020-10-30 +### Added +* Implement `exclude_regex` for Logger middleware. [#1723] +* Add request-local data extractor `web::ReqData`. [#1748] +* Add ability to register closure for request middleware logging. [#1749] +* Add `app_data` to `ServiceConfig`. [#1757] +* Expose `on_connect` for access to the connection stream before request is handled. [#1754] + +### Changed +* Updated actix-web-codegen dependency for access to new `#[route(...)]` multi-method macro. +* Print non-configured `Data` type when attempting extraction. [#1743] +* Re-export bytes::Buf{Mut} in web module. [#1750] +* Upgrade `pin-project` to `1.0`. + +[#1723]: https://github.com/actix/actix-web/pull/1723 +[#1743]: https://github.com/actix/actix-web/pull/1743 +[#1748]: https://github.com/actix/actix-web/pull/1748 +[#1750]: https://github.com/actix/actix-web/pull/1750 +[#1754]: https://github.com/actix/actix-web/pull/1754 +[#1749]: https://github.com/actix/actix-web/pull/1749 ## 3.1.0 - 2020-09-29 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 599b28c0d..ae97b3240 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -34,10 +34,13 @@ This Code of Conduct applies both within project spaces and in public spaces whe ## Enforcement -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at fafhrd91@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at robjtede@icloud.com ([@robjtede]) or huyuumi@neet.club ([@JohnTitor]). The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. +[@robjtede]: https://github.com/robjtede +[@JohnTitor]: https://github.com/JohnTitor + ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] diff --git a/Cargo.toml b/Cargo.toml index 56158389c..6ed327f56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "actix-web" -version = "3.1.0" +version = "3.3.2" authors = ["Nikolay Kim "] -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" keywords = ["actix", "http", "web", "framework", "async"] homepage = "https://actix.rs" @@ -34,7 +34,7 @@ members = [ "actix-multipart", "actix-web-actors", "actix-web-codegen", - "test-server", + "actix-http-test", ] [features] @@ -64,6 +64,14 @@ required-features = ["compress"] name = "test_server" required-features = ["compress"] +[[example]] +name = "on_connect" +required-features = [] + +[[example]] +name = "client" +required-features = ["rustls"] + [dependencies] actix-codec = "0.3.0" actix-service = "1.0.6" @@ -76,12 +84,12 @@ actix-macros = "0.1.0" actix-threadpool = "0.3.1" actix-tls = "2.0.0" -actix-web-codegen = "0.3.0" -actix-http = "2.0.0" -awc = { version = "2.0.0", default-features = false } +actix-web-codegen = "0.4.0" +actix-http = "2.2.0" +awc = { version = "2.0.3", default-features = false } bytes = "0.5.3" -derive_more = "0.99.2" +derive_more = "0.99.5" encoding_rs = "0.8" futures-channel = { version = "0.3.5", default-features = false } futures-core = { version = "0.3.5", default-features = false } @@ -89,12 +97,12 @@ futures-util = { version = "0.3.5", default-features = false } fxhash = "0.2.1" log = "0.4" mime = "0.3" -socket2 = "0.3" -pin-project = "0.4.17" -regex = "1.3" +socket2 = "0.3.16" +pin-project = "1.0.0" +regex = "1.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -serde_urlencoded = "0.6.1" +serde_urlencoded = "0.7" time = { version = "0.2.7", default-features = false, features = ["std"] } url = "2.1" open-ssl = { package = "openssl", version = "0.10", optional = true } @@ -103,9 +111,9 @@ tinyvec = { version = "1", features = ["alloc"] } [dev-dependencies] actix = "0.10.0" -actix-http = { version = "2.0.0", features = ["actors"] } -rand = "0.7" -env_logger = "0.7" +actix-http = { version = "2.1.0", features = ["actors"] } +rand = "0.8" +env_logger = "0.8" serde_derive = "1.0" brotli2 = "0.3.2" flate2 = "1.0.13" @@ -119,16 +127,12 @@ codegen-units = 1 [patch.crates-io] actix-web = { path = "." } actix-http = { path = "actix-http" } -actix-http-test = { path = "test-server" } +actix-http-test = { path = "actix-http-test" } actix-web-codegen = { path = "actix-web-codegen" } -actix-files = { path = "actix-files" } actix-multipart = { path = "actix-multipart" } +actix-files = { path = "actix-files" } awc = { path = "awc" } -[[example]] -name = "client" -required-features = ["rustls"] - [[bench]] name = "server" harness = false diff --git a/README.md b/README.md index 3e3ce8bf1..b9f2b7594 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,21 @@

Actix web

- Actix web is a powerful, pragmatic, and extremely fast web framework for Rust + Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust

-[![crates.io](https://meritbadge.herokuapp.com/actix-web)](https://crates.io/crates/actix-web) -[![Documentation](https://docs.rs/actix-web/badge.svg)](https://docs.rs/actix-web) +[![crates.io](https://img.shields.io/crates/v/actix-web?label=latest)](https://crates.io/crates/actix-web) +[![Documentation](https://docs.rs/actix-web/badge.svg?version=3.3.2)](https://docs.rs/actix-web/3.3.2) [![Version](https://img.shields.io/badge/rustc-1.42+-ab6000.svg)](https://blog.rust-lang.org/2020/03/12/Rust-1.42.html) ![License](https://img.shields.io/crates/l/actix-web.svg) +[![Dependency Status](https://deps.rs/crate/actix-web/3.3.2/status.svg)](https://deps.rs/crate/actix-web/3.3.2)
[![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![Download](https://img.shields.io/crates/d/actix-web.svg)](https://crates.io/crates/actix-web) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)

diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index 75d616ff9..c4d56010f 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -1,12 +1,28 @@ # Changes -## [Unreleased] - 2020-xx-xx +## Unreleased - 2020-xx-xx -## [0.3.0-beta.1] - 2020-07-15 + +## 0.4.1 - 2020-11-24 +* Clarify order of parameters in `Files::new` and improve docs. + + +## 0.4.0 - 2020-10-06 +* Add `Files::prefer_utf8` option that adds UTF-8 charset on certain response types. [#1714] + +[#1714]: https://github.com/actix/actix-web/pull/1714 + + +## 0.3.0 - 2020-09-11 +* No significant changes from 0.3.0-beta.1. + + +## 0.3.0-beta.1 - 2020-07-15 * Update `v_htmlescape` to 0.10 * Update `actix-web` and `actix-http` dependencies to beta.1 -## [0.3.0-alpha.1] - 2020-05-23 + +## 0.3.0-alpha.1 - 2020-05-23 * Update `actix-web` and `actix-http` dependencies to alpha * Fix some typos in the docs * Bump minimum supported Rust version to 1.40 @@ -14,77 +30,73 @@ [#1384]: https://github.com/actix/actix-web/pull/1384 -## [0.2.1] - 2019-12-22 +## 0.2.1 - 2019-12-22 * Use the same format for file URLs regardless of platforms -## [0.2.0] - 2019-12-20 +## 0.2.0 - 2019-12-20 * Fix BodyEncoding trait import #1220 -## [0.2.0-alpha.1] - 2019-12-07 +## 0.2.0-alpha.1 - 2019-12-07 * Migrate to `std::future` -## [0.1.7] - 2019-11-06 -* Add an additional `filename*` param in the `Content-Disposition` header of `actix_files::NamedFile` to be more compatible. (#1151) - -## [0.1.6] - 2019-10-14 +## 0.1.7 - 2019-11-06 +* Add an additional `filename*` param in the `Content-Disposition` header of + `actix_files::NamedFile` to be more compatible. (#1151) +## 0.1.6 - 2019-10-14 * Add option to redirect to a slash-ended path `Files` #1132 -## [0.1.5] - 2019-10-08 +## 0.1.5 - 2019-10-08 * Bump up `mime_guess` crate version to 2.0.1 - * Bump up `percent-encoding` crate version to 2.1 - * Allow user defined request guards for `Files` #1113 -## [0.1.4] - 2019-07-20 +## 0.1.4 - 2019-07-20 * Allow to disable `Content-Disposition` header #686 -## [0.1.3] - 2019-06-28 +## 0.1.3 - 2019-06-28 * Do not set `Content-Length` header, let actix-http set it #930 -## [0.1.2] - 2019-06-13 +## 0.1.2 - 2019-06-13 * Content-Length is 0 for NamedFile HEAD request #914 - * Fix ring dependency from actix-web default features for #741 -## [0.1.1] - 2019-06-01 +## 0.1.1 - 2019-06-01 * Static files are incorrectly served as both chunked and with length #812 -## [0.1.0] - 2019-05-25 -* NamedFile last-modified check always fails due to nano-seconds - in file modified date #820 +## 0.1.0 - 2019-05-25 +* NamedFile last-modified check always fails due to nano-seconds in file modified date #820 -## [0.1.0-beta.4] - 2019-05-12 +## 0.1.0-beta.4 - 2019-05-12 * Update actix-web to beta.4 -## [0.1.0-beta.1] - 2019-04-20 +## 0.1.0-beta.1 - 2019-04-20 * Update actix-web to beta.1 -## [0.1.0-alpha.6] - 2019-04-14 +## 0.1.0-alpha.6 - 2019-04-14 * Update actix-web to alpha6 -## [0.1.0-alpha.4] - 2019-04-08 +## 0.1.0-alpha.4 - 2019-04-08 * Update actix-web to alpha4 -## [0.1.0-alpha.2] - 2019-04-02 +## 0.1.0-alpha.2 - 2019-04-02 * Add default handler support -## [0.1.0-alpha.1] - 2019-03-28 +## 0.1.0-alpha.1 - 2019-03-28 * Initial impl diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index 634296c56..f7d32f8ec 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "actix-files" -version = "0.3.0" +version = "0.4.1" authors = ["Nikolay Kim "] -description = "Static files support for actix web." +description = "Static file serving for Actix Web" readme = "README.md" keywords = ["actix", "http", "async", "futures"] homepage = "https://actix.rs" @@ -21,14 +21,14 @@ actix-web = { version = "3.0.0", default-features = false } actix-service = "1.0.6" bitflags = "1" bytes = "0.5.3" -futures-core = { version = "0.3.5", default-features = false } -futures-util = { version = "0.3.5", default-features = false } +futures-core = { version = "0.3.7", default-features = false } +futures-util = { version = "0.3.7", default-features = false } derive_more = "0.99.2" log = "0.4" mime = "0.3" mime_guess = "2.0.1" percent-encoding = "2.1" -v_htmlescape = "0.10" +v_htmlescape = "0.11" [dev-dependencies] actix-rt = "1.0.0" diff --git a/actix-files/README.md b/actix-files/README.md index 5a5a62083..685e5dbe5 100644 --- a/actix-files/README.md +++ b/actix-files/README.md @@ -1,9 +1,19 @@ -# Static files support for actix web [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-files)](https://crates.io/crates/actix-files) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +# actix-files -## Documentation & community resources +> Static file serving for Actix Web -* [User Guide](https://actix.rs/docs/) -* [API Documentation](https://docs.rs/actix-files/) -* [Chat on gitter](https://gitter.im/actix/actix) -* Cargo package: [actix-files](https://crates.io/crates/actix-files) -* Minimum supported Rust version: 1.40 or later +[![crates.io](https://img.shields.io/crates/v/actix-files?label=latest)](https://crates.io/crates/actix-files) +[![Documentation](https://docs.rs/actix-files/badge.svg?version=0.4.1)](https://docs.rs/actix-files/0.4.1) +[![Version](https://img.shields.io/badge/rustc-1.42+-ab6000.svg)](https://blog.rust-lang.org/2020/03/12/Rust-1.42.html) +![License](https://img.shields.io/crates/l/actix-files.svg) +
+[![dependency status](https://deps.rs/crate/actix-files/0.4.1/status.svg)](https://deps.rs/crate/actix-files/0.4.1) +[![Download](https://img.shields.io/crates/d/actix-files.svg)](https://crates.io/crates/actix-files) +[![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +## Documentation & Resources + +- [API Documentation](https://docs.rs/actix-files/) +- [Example Project](https://github.com/actix/examples/tree/master/static_index) +- [Chat on Gitter](https://gitter.im/actix/actix-web) +- Minimum supported Rust version: 1.42 or later diff --git a/actix-files/src/encoding.rs b/actix-files/src/encoding.rs new file mode 100644 index 000000000..95997e313 --- /dev/null +++ b/actix-files/src/encoding.rs @@ -0,0 +1,52 @@ +use mime::Mime; + +/// Transforms MIME `text/*` types into their UTF-8 equivalent, if supported. +/// +/// MIME types that are converted +/// - application/javascript +/// - text/html +/// - text/css +/// - text/plain +/// - text/csv +/// - text/tab-separated-values +pub(crate) fn equiv_utf8_text(ct: Mime) -> Mime { + // use (roughly) order of file-type popularity for a web server + + if ct == mime::APPLICATION_JAVASCRIPT { + return mime::APPLICATION_JAVASCRIPT_UTF_8; + } + + if ct == mime::TEXT_HTML { + return mime::TEXT_HTML_UTF_8; + } + + if ct == mime::TEXT_CSS { + return mime::TEXT_CSS_UTF_8; + } + + if ct == mime::TEXT_PLAIN { + return mime::TEXT_PLAIN_UTF_8; + } + + if ct == mime::TEXT_CSV { + return mime::TEXT_CSV_UTF_8; + } + + if ct == mime::TEXT_TAB_SEPARATED_VALUES { + return mime::TEXT_TAB_SEPARATED_VALUES_UTF_8; + } + + ct +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_equiv_utf8_text() { + assert_eq!(equiv_utf8_text(mime::TEXT_PLAIN), mime::TEXT_PLAIN_UTF_8); + assert_eq!(equiv_utf8_text(mime::TEXT_XML), mime::TEXT_XML); + assert_eq!(equiv_utf8_text(mime::IMAGE_PNG), mime::IMAGE_PNG); + } +} diff --git a/actix-files/src/files.rs b/actix-files/src/files.rs index 2b55e1aa9..a99b4699e 100644 --- a/actix-files/src/files.rs +++ b/actix-files/src/files.rs @@ -65,13 +65,25 @@ impl Clone for Files { } impl Files { - /// Create new `Files` instance for specified base directory. + /// Create new `Files` instance for a specified base directory. /// - /// `File` uses `ThreadPool` for blocking filesystem operations. - /// By default pool with 5x threads of available cpus is used. - /// Pool size can be changed by setting ACTIX_THREADPOOL environment variable. - pub fn new>(path: &str, dir: T) -> Files { - let orig_dir = dir.into(); + /// # Argument Order + /// The first argument (`mount_path`) is the root URL at which the static files are served. + /// For example, `/assets` will serve files at `example.com/assets/...`. + /// + /// The second argument (`serve_from`) is the location on disk at which files are loaded. + /// This can be a relative path. For example, `./` would serve files from the current + /// working directory. + /// + /// # Implementation Notes + /// If the mount path is set as the root path `/`, services registered after this one will + /// be inaccessible. Register more specific handlers and services first. + /// + /// `Files` uses a threadpool for blocking filesystem operations. By default, the pool uses a + /// number of threads equal to 5x the number of available logical CPUs. Pool size can be changed + /// by setting ACTIX_THREADPOOL environment variable. + pub fn new>(mount_path: &str, serve_from: T) -> Files { + let orig_dir = serve_from.into(); let dir = match orig_dir.canonicalize() { Ok(canon_dir) => canon_dir, Err(_) => { @@ -81,7 +93,7 @@ impl Files { }; Files { - path: path.to_string(), + path: mount_path.to_owned(), directory: dir, index: None, show_index: false, @@ -138,24 +150,33 @@ impl Files { self } - #[inline] /// Specifies whether to use ETag or not. /// /// Default is true. + #[inline] pub fn use_etag(mut self, value: bool) -> Self { self.file_flags.set(named::Flags::ETAG, value); self } - #[inline] /// Specifies whether to use Last-Modified or not. /// /// Default is true. + #[inline] pub fn use_last_modified(mut self, value: bool) -> Self { self.file_flags.set(named::Flags::LAST_MD, value); self } + /// Specifies whether text responses should signal a UTF-8 encoding. + /// + /// Default is false (but will default to true in a future version). + #[inline] + pub fn prefer_utf8(mut self, value: bool) -> Self { + self.file_flags.set(named::Flags::PREFER_UTF8, value); + self + } + /// Specifies custom guards to use for directory listings and files. /// /// Default behaviour allows GET and HEAD. diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 1fc7cb3f3..662fba0a3 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -1,4 +1,4 @@ -//! Static files support for Actix Web. +//! Static file serving for Actix Web. //! //! Provides a non-blocking service for serving static files from disk. //! @@ -8,12 +8,8 @@ //! use actix_files::Files; //! //! let app = App::new() -//! .service(Files::new("/static", ".")); +//! .service(Files::new("/static", ".").prefer_utf8(true)); //! ``` -//! -//! # Implementation Quirks -//! - If a filename contains non-ascii characters, that file will be served with the `charset=utf-8` -//! extension on the Content-Type header. #![deny(rust_2018_idioms)] #![warn(missing_docs, missing_debug_implementations)] @@ -30,6 +26,7 @@ use mime_guess::from_ext; mod chunked; mod directory; +mod encoding; mod error; mod files; mod named; @@ -93,6 +90,9 @@ mod tests { #[actix_rt::test] async fn test_file_extension_to_mime() { + let m = file_extension_to_mime(""); + assert_eq!(m, mime::APPLICATION_OCTET_STREAM); + let m = file_extension_to_mime("jpg"); assert_eq!(m, mime::IMAGE_JPEG); diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index 3caa4a809..a9b95bad1 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -22,20 +22,21 @@ use bitflags::bitflags; use futures_util::future::{ready, Ready}; use mime_guess::from_path; -use crate::range::HttpRange; use crate::ChunkedReadFile; +use crate::{encoding::equiv_utf8_text, range::HttpRange}; bitflags! { pub(crate) struct Flags: u8 { - const ETAG = 0b0000_0001; - const LAST_MD = 0b0000_0010; + const ETAG = 0b0000_0001; + const LAST_MD = 0b0000_0010; const CONTENT_DISPOSITION = 0b0000_0100; + const PREFER_UTF8 = 0b0000_1000; } } impl Default for Flags { fn default() -> Self { - Flags::all() + Flags::from_bits_truncate(0b0000_0111) } } @@ -92,6 +93,7 @@ impl NamedFile { }; let ct = from_path(&path).first_or_octet_stream(); + let disposition = match ct.type_() { mime::IMAGE | mime::TEXT | mime::VIDEO => DispositionType::Inline, _ => DispositionType::Attachment, @@ -191,7 +193,7 @@ impl NamedFile { /// image, and video content types, and `attachment` otherwise, and /// the filename is taken from the path provided in the `open` method /// after converting it to UTF-8 using. - /// [to_string_lossy](https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.to_string_lossy). + /// [`std::ffi::OsStr::to_string_lossy`] #[inline] pub fn set_content_disposition(mut self, cd: header::ContentDisposition) -> Self { self.content_disposition = cd; @@ -215,24 +217,33 @@ impl NamedFile { self } - #[inline] - ///Specifies whether to use ETag or not. + /// Specifies whether to use ETag or not. /// - ///Default is true. + /// Default is true. + #[inline] pub fn use_etag(mut self, value: bool) -> Self { self.flags.set(Flags::ETAG, value); self } - #[inline] - ///Specifies whether to use Last-Modified or not. + /// Specifies whether to use Last-Modified or not. /// - ///Default is true. + /// Default is true. + #[inline] pub fn use_last_modified(mut self, value: bool) -> Self { self.flags.set(Flags::LAST_MD, value); self } + /// Specifies whether text responses should signal a UTF-8 encoding. + /// + /// Default is false (but will default to true in a future version). + #[inline] + pub fn prefer_utf8(mut self, value: bool) -> Self { + self.flags.set(Flags::PREFER_UTF8, value); + self + } + pub(crate) fn etag(&self) -> Option { // This etag format is similar to Apache's. self.modified.as_ref().map(|mtime| { @@ -268,18 +279,24 @@ impl NamedFile { /// Creates an `HttpResponse` with file as a streaming body. pub fn into_response(self, req: &HttpRequest) -> Result { if self.status_code != StatusCode::OK { - let mut resp = HttpResponse::build(self.status_code); + let mut res = HttpResponse::build(self.status_code); - resp.set(header::ContentType(self.content_type.clone())) - .if_true(self.flags.contains(Flags::CONTENT_DISPOSITION), |res| { - res.header( - header::CONTENT_DISPOSITION, - self.content_disposition.to_string(), - ); - }); + if self.flags.contains(Flags::PREFER_UTF8) { + let ct = equiv_utf8_text(self.content_type.clone()); + res.header(header::CONTENT_TYPE, ct.to_string()); + } else { + res.header(header::CONTENT_TYPE, self.content_type.to_string()); + } + + if self.flags.contains(Flags::CONTENT_DISPOSITION) { + res.header( + header::CONTENT_DISPOSITION, + self.content_disposition.to_string(), + ); + } if let Some(current_encoding) = self.encoding { - resp.encoding(current_encoding); + res.encoding(current_encoding); } let reader = ChunkedReadFile { @@ -290,7 +307,7 @@ impl NamedFile { counter: 0, }; - return Ok(resp.streaming(reader)); + return Ok(res.streaming(reader)); } let etag = if self.flags.contains(Flags::ETAG) { @@ -342,25 +359,33 @@ impl NamedFile { }; let mut resp = HttpResponse::build(self.status_code); - resp.set(header::ContentType(self.content_type.clone())) - .if_true(self.flags.contains(Flags::CONTENT_DISPOSITION), |res| { - res.header( - header::CONTENT_DISPOSITION, - self.content_disposition.to_string(), - ); - }); + + if self.flags.contains(Flags::PREFER_UTF8) { + let ct = equiv_utf8_text(self.content_type.clone()); + resp.header(header::CONTENT_TYPE, ct.to_string()); + } else { + resp.header(header::CONTENT_TYPE, self.content_type.to_string()); + } + + if self.flags.contains(Flags::CONTENT_DISPOSITION) { + resp.header( + header::CONTENT_DISPOSITION, + self.content_disposition.to_string(), + ); + } // default compressing if let Some(current_encoding) = self.encoding { resp.encoding(current_encoding); } - resp.if_some(last_modified, |lm, resp| { - resp.set(header::LastModified(lm)); - }) - .if_some(etag, |etag, resp| { - resp.set(header::ETag(etag)); - }); + if let Some(lm) = last_modified { + resp.header(header::LAST_MODIFIED, lm.to_string()); + } + + if let Some(etag) = etag { + resp.header(header::ETAG, etag.to_string()); + } resp.header(header::ACCEPT_RANGES, "bytes"); diff --git a/actix-files/tests/encoding.rs b/actix-files/tests/encoding.rs new file mode 100644 index 000000000..d7e01b305 --- /dev/null +++ b/actix-files/tests/encoding.rs @@ -0,0 +1,40 @@ +use actix_files::Files; +use actix_web::{ + http::{ + header::{self, HeaderValue}, + StatusCode, + }, + test::{self, TestRequest}, + App, +}; + +#[actix_rt::test] +async fn test_utf8_file_contents() { + // use default ISO-8859-1 encoding + let mut srv = + test::init_service(App::new().service(Files::new("/", "./tests"))).await; + + let req = TestRequest::with_uri("/utf8.txt").to_request(); + let res = test::call_service(&mut srv, req).await; + + assert_eq!(res.status(), StatusCode::OK); + assert_eq!( + res.headers().get(header::CONTENT_TYPE), + Some(&HeaderValue::from_static("text/plain")), + ); + + // prefer UTF-8 encoding + let mut srv = test::init_service( + App::new().service(Files::new("/", "./tests").prefer_utf8(true)), + ) + .await; + + let req = TestRequest::with_uri("/utf8.txt").to_request(); + let res = test::call_service(&mut srv, req).await; + + assert_eq!(res.status(), StatusCode::OK); + assert_eq!( + res.headers().get(header::CONTENT_TYPE), + Some(&HeaderValue::from_static("text/plain; charset=utf-8")), + ); +} diff --git a/actix-files/tests/utf8.txt b/actix-files/tests/utf8.txt new file mode 100644 index 000000000..d8590e2f5 --- /dev/null +++ b/actix-files/tests/utf8.txt @@ -0,0 +1,3 @@ +中文内容显示正确。 + +English is OK. diff --git a/test-server/CHANGES.md b/actix-http-test/CHANGES.md similarity index 67% rename from test-server/CHANGES.md rename to actix-http-test/CHANGES.md index 0a11e2cae..835b75ddc 100644 --- a/test-server/CHANGES.md +++ b/actix-http-test/CHANGES.md @@ -2,13 +2,20 @@ ## Unreleased - 2020-xx-xx -* add ability to set address for `TestServer` [#1645] +## 2.1.0 - 2020-11-25 +* Add ability to set address for `TestServer`. [#1645] +* Upgrade `base64` to `0.13`. +* Upgrade `serde_urlencoded` to `0.7`. [#1773] + +[#1773]: https://github.com/actix/actix-web/pull/1773 [#1645]: https://github.com/actix/actix-web/pull/1645 + ## 2.0.0 - 2020-09-11 * Update actix-codec and actix-utils dependencies. + ## 2.0.0-alpha.1 - 2020-05-23 * Update the `time` dependency to 0.2.7 * Update `actix-connect` dependency to 2.0.0-alpha.2 @@ -18,74 +25,56 @@ * Update `base64` dependency to 0.12 * Update `env_logger` dependency to 0.7 -## [1.0.0] - 2019-12-13 - -### Changed - +## 1.0.0 - 2019-12-13 * Replaced `TestServer::start()` with `test_server()` -## [1.0.0-alpha.3] - 2019-12-07 - -### Changed - +## 1.0.0-alpha.3 - 2019-12-07 * Migrate to `std::future` -## [0.2.5] - 2019-09-17 - -### Changed - +## 0.2.5 - 2019-09-17 * Update serde_urlencoded to "0.6.1" * Increase TestServerRuntime timeouts from 500ms to 3000ms - -### Fixed - * Do not override current `System` -## [0.2.4] - 2019-07-18 - +## 0.2.4 - 2019-07-18 * Update actix-server to 0.6 -## [0.2.3] - 2019-07-16 +## 0.2.3 - 2019-07-16 * Add `delete`, `options`, `patch` methods to `TestServerRunner` -## [0.2.2] - 2019-06-16 +## 0.2.2 - 2019-06-16 * Add .put() and .sput() methods -## [0.2.1] - 2019-06-05 +## 0.2.1 - 2019-06-05 * Add license files -## [0.2.0] - 2019-05-12 +## 0.2.0 - 2019-05-12 * Update awc and actix-http deps -## [0.1.1] - 2019-04-24 +## 0.1.1 - 2019-04-24 * Always make new connection for http client -## [0.1.0] - 2019-04-16 - +## 0.1.0 - 2019-04-16 * No changes -## [0.1.0-alpha.3] - 2019-04-02 - +## 0.1.0-alpha.3 - 2019-04-02 * Request functions accept path #743 -## [0.1.0-alpha.2] - 2019-03-29 - +## 0.1.0-alpha.2 - 2019-03-29 * Added TestServerRuntime::load_body() method - * Update actix-http and awc libraries -## [0.1.0-alpha.1] - 2019-03-28 - +## 0.1.0-alpha.1 - 2019-03-28 * Initial impl diff --git a/test-server/Cargo.toml b/actix-http-test/Cargo.toml similarity index 90% rename from test-server/Cargo.toml rename to actix-http-test/Cargo.toml index d06bd5dec..8b23bef1c 100644 --- a/test-server/Cargo.toml +++ b/actix-http-test/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "actix-http-test" -version = "2.0.0" +version = "2.1.0" authors = ["Nikolay Kim "] -description = "Actix HTTP test server" +description = "Various helpers for Actix applications to use during testing" readme = "README.md" keywords = ["http", "web", "framework", "async", "futures"] homepage = "https://actix.rs" @@ -38,7 +38,7 @@ actix-server = "1.0.0" actix-testing = "1.0.0" awc = "2.0.0" -base64 = "0.12" +base64 = "0.13" bytes = "0.5.3" futures-core = { version = "0.3.5", default-features = false } http = "0.2.0" @@ -47,7 +47,7 @@ socket2 = "0.3" serde = "1.0" serde_json = "1.0" slab = "0.4" -serde_urlencoded = "0.6.1" +serde_urlencoded = "0.7" time = { version = "0.2.7", default-features = false, features = ["std"] } open-ssl = { version = "0.10", package = "openssl", optional = true } diff --git a/test-server/LICENSE-APACHE b/actix-http-test/LICENSE-APACHE similarity index 100% rename from test-server/LICENSE-APACHE rename to actix-http-test/LICENSE-APACHE diff --git a/test-server/LICENSE-MIT b/actix-http-test/LICENSE-MIT similarity index 100% rename from test-server/LICENSE-MIT rename to actix-http-test/LICENSE-MIT diff --git a/actix-http-test/README.md b/actix-http-test/README.md new file mode 100644 index 000000000..c847c8515 --- /dev/null +++ b/actix-http-test/README.md @@ -0,0 +1,15 @@ +# actix-http-test + +> Various helpers for Actix applications to use during testing. + +[![crates.io](https://img.shields.io/crates/v/actix-http-test?label=latest)](https://crates.io/crates/actix-http-test) +[![Documentation](https://docs.rs/actix-http-test/badge.svg?version=2.1.0)](https://docs.rs/actix-http-test/2.1.0) +![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/actix-http-test) +[![Dependency Status](https://deps.rs/crate/actix-http-test/2.1.0/status.svg)](https://deps.rs/crate/actix-http-test/2.1.0) +[![Join the chat at https://gitter.im/actix/actix-web](https://badges.gitter.im/actix/actix-web.svg)](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +## Documentation & Resources + +- [API Documentation](https://docs.rs/actix-http-test) +- [Chat on Gitter](https://gitter.im/actix/actix-web) +- Minimum Supported Rust Version (MSRV): 1.42.0 diff --git a/test-server/src/lib.rs b/actix-http-test/src/lib.rs similarity index 97% rename from test-server/src/lib.rs rename to actix-http-test/src/lib.rs index 4159c8d86..3ab3f8a0d 100644 --- a/test-server/src/lib.rs +++ b/actix-http-test/src/lib.rs @@ -1,4 +1,9 @@ //! Various helpers for Actix applications to use during testing. + +#![deny(rust_2018_idioms)] +#![doc(html_logo_url = "https://actix.rs/img/logo.png")] +#![doc(html_favicon_url = "https://actix.rs/favicon.ico")] + use std::sync::mpsc; use std::{net, thread, time}; @@ -48,7 +53,7 @@ pub async fn test_server>(factory: F) -> TestServer test_server_with_addr(tcp, factory).await } -/// Start [`test server`](./fn.test_server.html) on a concrete Address +/// Start [`test server`](test_server()) on a concrete Address pub async fn test_server_with_addr>( tcp: net::TcpListener, factory: F, diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 6a98c4ca7..81577688d 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,6 +1,41 @@ # Changes ## Unreleased - 2020-xx-xx +### Changed +* Bumped `rand` to `0.8` + +## 2.2.0 - 2020-11-25 +### Added +* HttpResponse builders for 1xx status codes. [#1768] +* `Accept::mime_precedence` and `Accept::mime_preference`. [#1793] +* `TryFrom` and `TryFrom` for `http::header::Quality`. [#1797] + +### Fixed +* Started dropping `transfer-encoding: chunked` and `Content-Length` for 1XX and 204 responses. [#1767] + +### Changed +* Upgrade `serde_urlencoded` to `0.7`. [#1773] + +[#1773]: https://github.com/actix/actix-web/pull/1773 +[#1767]: https://github.com/actix/actix-web/pull/1767 +[#1768]: https://github.com/actix/actix-web/pull/1768 +[#1793]: https://github.com/actix/actix-web/pull/1793 +[#1797]: https://github.com/actix/actix-web/pull/1797 + + +## 2.1.0 - 2020-10-30 +### Added +* Added more flexible `on_connect_ext` methods for on-connect handling. [#1754] + +### Changed +* Upgrade `base64` to `0.13`. [#1744] +* Upgrade `pin-project` to `1.0`. [#1733] +* Deprecate `ResponseBuilder::{if_some, if_true}`. [#1760] + +[#1760]: https://github.com/actix/actix-web/pull/1760 +[#1754]: https://github.com/actix/actix-web/pull/1754 +[#1733]: https://github.com/actix/actix-web/pull/1733 +[#1744]: https://github.com/actix/actix-web/pull/1744 ## 2.0.0 - 2020-09-11 diff --git a/actix-http/CODE_OF_CONDUCT.md b/actix-http/CODE_OF_CONDUCT.md deleted file mode 100644 index 599b28c0d..000000000 --- a/actix-http/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,46 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. - -## Our Standards - -Examples of behavior that contributes to creating a positive environment include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting - -## Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. - -## Scope - -This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at fafhrd91@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] - -[homepage]: http://contributor-covenant.org -[version]: http://contributor-covenant.org/version/1/4/ diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 0bbde881d..7cf344487 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "actix-http" -version = "2.0.0" +version = "2.2.0" authors = ["Nikolay Kim "] -description = "Actix HTTP primitives" +description = "HTTP primitives for the Actix ecosystem" readme = "README.md" keywords = ["actix", "http", "framework", "async", "futures"] homepage = "https://actix.rs" @@ -49,7 +49,7 @@ actix-threadpool = "0.3.1" actix-tls = { version = "2.0.0", optional = true } actix = { version = "0.10.0", optional = true } -base64 = "0.12" +base64 = "0.13" bitflags = "1.2" bytes = "0.5.3" cookie = { version = "0.14.1", features = ["percent-encode"] } @@ -71,14 +71,14 @@ language-tags = "0.2" log = "0.4" mime = "0.3" percent-encoding = "2.1" -pin-project = "0.4.17" -rand = "0.7" +pin-project = "1.0.0" +rand = "0.8" regex = "1.3" serde = "1.0" serde_json = "1.0" sha-1 = "0.9" slab = "0.4" -serde_urlencoded = "0.6.1" +serde_urlencoded = "0.7" time = { version = "0.2.7", default-features = false, features = ["std"] } # compression diff --git a/actix-http/README.md b/actix-http/README.md index 96fc54d2e..9103cd184 100644 --- a/actix-http/README.md +++ b/actix-http/README.md @@ -1,14 +1,18 @@ -# Actix http [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-http)](https://crates.io/crates/actix-http) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +# actix-http -Actix http +> HTTP primitives for the Actix ecosystem. -## Documentation & community resources +[![crates.io](https://img.shields.io/crates/v/actix-http?label=latest)](https://crates.io/crates/actix-http) +[![Documentation](https://docs.rs/actix-http/badge.svg?version=2.2.0)](https://docs.rs/actix-http/2.2.0) +![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/actix-http) +[![Dependency Status](https://deps.rs/crate/actix-http/2.2.0/status.svg)](https://deps.rs/crate/actix-http/2.2.0) +[![Join the chat at https://gitter.im/actix/actix-web](https://badges.gitter.im/actix/actix-web.svg)](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -* [User Guide](https://actix.rs/docs/) -* [API Documentation](https://docs.rs/actix-http/) -* [Chat on gitter](https://gitter.im/actix/actix) -* Cargo package: [actix-http](https://crates.io/crates/actix-http) -* Minimum supported Rust version: 1.40 or later +## Documentation & Resources + +- [API Documentation](https://docs.rs/actix-http) +- [Chat on Gitter](https://gitter.im/actix/actix-web) +- Minimum Supported Rust Version (MSRV): 1.42.0 ## Example diff --git a/actix-http/src/builder.rs b/actix-http/src/builder.rs index 271abd43f..b28c69761 100644 --- a/actix-http/src/builder.rs +++ b/actix-http/src/builder.rs @@ -14,10 +14,11 @@ use crate::helpers::{Data, DataFactory}; use crate::request::Request; use crate::response::Response; use crate::service::HttpService; +use crate::{ConnectCallback, Extensions}; -/// A http service builder +/// A HTTP service builder /// -/// This type can be used to construct an instance of `http service` through a +/// This type can be used to construct an instance of [`HttpService`] through a /// builder-like pattern. pub struct HttpServiceBuilder> { keep_alive: KeepAlive, @@ -27,7 +28,9 @@ pub struct HttpServiceBuilder> { local_addr: Option, expect: X, upgrade: Option, + // DEPRECATED: in favor of on_connect_ext on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, S)>, } @@ -49,6 +52,7 @@ where expect: ExpectHandler, upgrade: None, on_connect: None, + on_connect_ext: None, _t: PhantomData, } } @@ -138,6 +142,7 @@ where expect: expect.into_factory(), upgrade: self.upgrade, on_connect: self.on_connect, + on_connect_ext: self.on_connect_ext, _t: PhantomData, } } @@ -167,14 +172,16 @@ where expect: self.expect, upgrade: Some(upgrade.into_factory()), on_connect: self.on_connect, + on_connect_ext: self.on_connect_ext, _t: PhantomData, } } /// Set on-connect callback. /// - /// It get called once per connection and result of the call - /// get stored to the request's extensions. + /// Called once per connection. Return value of the call is stored in request extensions. + /// + /// *SOFT DEPRECATED*: Prefer the `on_connect_ext` style callback. pub fn on_connect(mut self, f: F) -> Self where F: Fn(&T) -> I + 'static, @@ -184,7 +191,20 @@ where self } - /// Finish service configuration and create *http service* for HTTP/1 protocol. + /// Sets the callback to be run on connection establishment. + /// + /// Has mutable access to a data container that will be merged into request extensions. + /// This enables transport layer data (like client certificates) to be accessed in middleware + /// and handlers. + pub fn on_connect_ext(mut self, f: F) -> Self + where + F: Fn(&T, &mut Extensions) + 'static, + { + self.on_connect_ext = Some(Rc::new(f)); + self + } + + /// Finish service configuration and create a HTTP Service for HTTP/1 protocol. pub fn h1(self, service: F) -> H1Service where B: MessageBody, @@ -200,13 +220,15 @@ where self.secure, self.local_addr, ); + H1Service::with_config(cfg, service.into_factory()) .expect(self.expect) .upgrade(self.upgrade) .on_connect(self.on_connect) + .on_connect_ext(self.on_connect_ext) } - /// Finish service configuration and create *http service* for HTTP/2 protocol. + /// Finish service configuration and create a HTTP service for HTTP/2 protocol. pub fn h2(self, service: F) -> H2Service where B: MessageBody + 'static, @@ -223,7 +245,10 @@ where self.secure, self.local_addr, ); - H2Service::with_config(cfg, service.into_factory()).on_connect(self.on_connect) + + H2Service::with_config(cfg, service.into_factory()) + .on_connect(self.on_connect) + .on_connect_ext(self.on_connect_ext) } /// Finish service configuration and create `HttpService` instance. @@ -243,9 +268,11 @@ where self.secure, self.local_addr, ); + HttpService::with_config(cfg, service.into_factory()) .expect(self.expect) .upgrade(self.upgrade) .on_connect(self.on_connect) + .on_connect_ext(self.on_connect_ext) } } diff --git a/actix-http/src/client/pool.rs b/actix-http/src/client/pool.rs index 08abc6277..a8687dbeb 100644 --- a/actix-http/src/client/pool.rs +++ b/actix-http/src/client/pool.rs @@ -9,8 +9,9 @@ use std::time::{Duration, Instant}; use actix_codec::{AsyncRead, AsyncWrite}; use actix_rt::time::{delay_for, Delay}; use actix_service::Service; -use actix_utils::{oneshot, task::LocalWaker}; +use actix_utils::task::LocalWaker; use bytes::Bytes; +use futures_channel::oneshot; use futures_util::future::{poll_fn, FutureExt, LocalBoxFuture}; use fxhash::FxHashMap; use h2::client::{Connection, SendRequest}; diff --git a/actix-http/src/cloneable.rs b/actix-http/src/cloneable.rs index b64c299fc..0e77c455c 100644 --- a/actix-http/src/cloneable.rs +++ b/actix-http/src/cloneable.rs @@ -4,12 +4,12 @@ use std::task::{Context, Poll}; use actix_service::Service; -#[doc(hidden)] /// Service that allows to turn non-clone service to a service with `Clone` impl /// /// # Panics /// CloneableService might panic with some creative use of thread local storage. /// See https://github.com/actix/actix-web/issues/1295 for example +#[doc(hidden)] pub(crate) struct CloneableService(Rc>); impl CloneableService { diff --git a/actix-http/src/error.rs b/actix-http/src/error.rs index e93c077af..0ebd4c05c 100644 --- a/actix-http/src/error.rs +++ b/actix-http/src/error.rs @@ -25,7 +25,7 @@ pub use crate::cookie::ParseError as CookieParseError; use crate::helpers::Writer; use crate::response::{Response, ResponseBuilder}; -/// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) +/// A specialized [`std::result::Result`] /// for actix web operations /// /// This typedef is generally used to avoid writing out diff --git a/actix-http/src/extensions.rs b/actix-http/src/extensions.rs index 96e01767b..b20dfe11d 100644 --- a/actix-http/src/extensions.rs +++ b/actix-http/src/extensions.rs @@ -1,10 +1,10 @@ use std::any::{Any, TypeId}; -use std::fmt; +use std::{fmt, mem}; use fxhash::FxHashMap; -#[derive(Default)] /// A type map of request extensions. +#[derive(Default)] pub struct Extensions { /// Use FxHasher with a std HashMap with for faster /// lookups on the small `TypeId` (u64 equivalent) keys. @@ -61,6 +61,16 @@ impl Extensions { pub fn clear(&mut self) { self.map.clear(); } + + /// Extends self with the items from another `Extensions`. + pub fn extend(&mut self, other: Extensions) { + self.map.extend(other.map); + } + + /// Sets (or overrides) items from `other` into this map. + pub(crate) fn drain_from(&mut self, other: &mut Self) { + self.map.extend(mem::take(&mut other.map)); + } } impl fmt::Debug for Extensions { @@ -178,4 +188,57 @@ mod tests { assert_eq!(extensions.get::(), None); assert_eq!(extensions.get(), Some(&MyType(10))); } + + #[test] + fn test_extend() { + #[derive(Debug, PartialEq)] + struct MyType(i32); + + let mut extensions = Extensions::new(); + + extensions.insert(5i32); + extensions.insert(MyType(10)); + + let mut other = Extensions::new(); + + other.insert(15i32); + other.insert(20u8); + + extensions.extend(other); + + assert_eq!(extensions.get(), Some(&15i32)); + assert_eq!(extensions.get_mut(), Some(&mut 15i32)); + + assert_eq!(extensions.remove::(), Some(15i32)); + assert!(extensions.get::().is_none()); + + assert_eq!(extensions.get::(), None); + assert_eq!(extensions.get(), Some(&MyType(10))); + + assert_eq!(extensions.get(), Some(&20u8)); + assert_eq!(extensions.get_mut(), Some(&mut 20u8)); + } + + #[test] + fn test_drain_from() { + let mut ext = Extensions::new(); + ext.insert(2isize); + + let mut more_ext = Extensions::new(); + + more_ext.insert(5isize); + more_ext.insert(5usize); + + assert_eq!(ext.get::(), Some(&2isize)); + assert_eq!(ext.get::(), None); + assert_eq!(more_ext.get::(), Some(&5isize)); + assert_eq!(more_ext.get::(), Some(&5usize)); + + ext.drain_from(&mut more_ext); + + assert_eq!(ext.get::(), Some(&5isize)); + assert_eq!(ext.get::(), Some(&5usize)); + assert_eq!(more_ext.get::(), None); + assert_eq!(more_ext.get::(), None); + } } diff --git a/actix-http/src/h1/codec.rs b/actix-http/src/h1/codec.rs index 036f16670..c9a62dc30 100644 --- a/actix-http/src/h1/codec.rs +++ b/actix-http/src/h1/codec.rs @@ -58,6 +58,7 @@ impl Codec { } else { Flags::empty() }; + Codec { config, flags, @@ -69,26 +70,26 @@ impl Codec { } } + /// Check if request is upgrade. #[inline] - /// Check if request is upgrade pub fn upgrade(&self) -> bool { self.ctype == ConnectionType::Upgrade } + /// Check if last response is keep-alive. #[inline] - /// Check if last response is keep-alive pub fn keepalive(&self) -> bool { self.ctype == ConnectionType::KeepAlive } + /// Check if keep-alive enabled on server level. #[inline] - /// Check if keep-alive enabled on server level pub fn keepalive_enabled(&self) -> bool { self.flags.contains(Flags::KEEPALIVE_ENABLED) } + /// Check last request's message type. #[inline] - /// Check last request's message type pub fn message_type(&self) -> MessageType { if self.flags.contains(Flags::STREAM) { MessageType::Stream diff --git a/actix-http/src/h1/dispatcher.rs b/actix-http/src/h1/dispatcher.rs index 7c4de9707..1311a0987 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -1,8 +1,11 @@ -use std::collections::VecDeque; -use std::future::Future; -use std::pin::Pin; -use std::task::{Context, Poll}; -use std::{fmt, io, net}; +use std::{ + collections::VecDeque, + fmt, + future::Future, + io, mem, net, + pin::Pin, + task::{Context, Poll}, +}; use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed, FramedParts}; use actix_rt::time::{delay_until, Delay, Instant}; @@ -12,7 +15,6 @@ use bytes::{Buf, BytesMut}; use log::{error, trace}; use pin_project::pin_project; -use crate::body::{Body, BodySize, MessageBody, ResponseBody}; use crate::cloneable::CloneableService; use crate::config::ServiceConfig; use crate::error::{DispatchError, Error}; @@ -21,6 +23,10 @@ use crate::helpers::DataFactory; use crate::httpmessage::HttpMessage; use crate::request::Request; use crate::response::Response; +use crate::{ + body::{Body, BodySize, MessageBody, ResponseBody}, + Extensions, +}; use super::codec::Codec; use super::payload::{Payload, PayloadSender, PayloadStatus}; @@ -56,6 +62,9 @@ where { #[pin] inner: DispatcherState, + + #[cfg(test)] + poll_count: u64, } #[pin_project(project = DispatcherStateProj)] @@ -88,6 +97,7 @@ where expect: CloneableService, upgrade: Option>, on_connect: Option>, + on_connect_data: Extensions, flags: Flags, peer_addr: Option, error: Option, @@ -167,7 +177,7 @@ where U: Service), Response = ()>, U::Error: fmt::Display, { - /// Create http/1 dispatcher. + /// Create HTTP/1 dispatcher. pub(crate) fn new( stream: T, config: ServiceConfig, @@ -175,6 +185,7 @@ where expect: CloneableService, upgrade: Option>, on_connect: Option>, + on_connect_data: Extensions, peer_addr: Option, ) -> Self { Dispatcher::with_timeout( @@ -187,6 +198,7 @@ where expect, upgrade, on_connect, + on_connect_data, peer_addr, ) } @@ -202,6 +214,7 @@ where expect: CloneableService, upgrade: Option>, on_connect: Option>, + on_connect_data: Extensions, peer_addr: Option, ) -> Self { let keepalive = config.keep_alive_enabled(); @@ -234,11 +247,15 @@ where expect, upgrade, on_connect, + on_connect_data, flags, peer_addr, ka_expire, ka_timer, }), + + #[cfg(test)] + poll_count: 0, } } } @@ -503,12 +520,12 @@ where } } - /// Process one incoming requests + /// Process one incoming request. pub(self) fn poll_request( mut self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Result { - // limit a mount of non processed requests + // limit amount of non-processed requests if self.messages.len() >= MAX_PIPELINED_MESSAGES || !self.can_read(cx) { return Ok(false); } @@ -526,11 +543,15 @@ where let pl = this.codec.message_type(); req.head_mut().peer_addr = *this.peer_addr; + // DEPRECATED // set on_connect data if let Some(ref on_connect) = this.on_connect { on_connect.set(&mut req.extensions_mut()); } + // merge on_connect_ext data into request extensions + req.extensions_mut().drain_from(this.on_connect_data); + if pl == MessageType::Stream && this.upgrade.is_some() { this.messages.push_back(DispatcherMessage::Upgrade(req)); break; @@ -713,6 +734,12 @@ where #[inline] fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.as_mut().project(); + + #[cfg(test)] + { + *this.poll_count += 1; + } + match this.inner.project() { DispatcherStateProj::Normal(mut inner) => { inner.as_mut().poll_keepalive(cx)?; @@ -776,10 +803,10 @@ where let inner_p = inner.as_mut().project(); let mut parts = FramedParts::with_read_buf( inner_p.io.take().unwrap(), - std::mem::take(inner_p.codec), - std::mem::take(inner_p.read_buf), + mem::take(inner_p.codec), + mem::take(inner_p.read_buf), ); - parts.write_buf = std::mem::take(inner_p.write_buf); + parts.write_buf = mem::take(inner_p.write_buf); let framed = Framed::from_parts(parts); let upgrade = inner_p.upgrade.take().unwrap().call((req, framed)); @@ -791,8 +818,11 @@ where } // we didn't get WouldBlock from write operation, - // so data get written to kernel completely (OSX) + // so data get written to kernel completely (macOS) // and we have to write again otherwise response can get stuck + // + // TODO: what? is WouldBlock good or bad? + // want to find a reference for this macOS behavior if inner.as_mut().poll_flush(cx)? || !drain { break; } @@ -842,6 +872,11 @@ where } } +/// Returns either: +/// - `Ok(Some(true))` - data was read and done reading all data. +/// - `Ok(Some(false))` - data was read but there should be more to read. +/// - `Ok(None)` - no data was read but there should be more to read later. +/// - Unhandled Errors fn read_available( cx: &mut Context<'_>, io: &mut T, @@ -875,17 +910,17 @@ where read_some = true; } } - Poll::Ready(Err(e)) => { - return if e.kind() == io::ErrorKind::WouldBlock { + Poll::Ready(Err(err)) => { + return if err.kind() == io::ErrorKind::WouldBlock { if read_some { Ok(Some(false)) } else { Ok(None) } - } else if e.kind() == io::ErrorKind::ConnectionReset && read_some { + } else if err.kind() == io::ErrorKind::ConnectionReset && read_some { Ok(Some(true)) } else { - Err(e) + Err(err) } } } @@ -905,13 +940,64 @@ where #[cfg(test)] mod tests { - use actix_service::IntoService; - use futures_util::future::{lazy, ok}; + use std::{marker::PhantomData, str}; + + use actix_service::fn_service; + use futures_util::future::{lazy, ready}; use super::*; - use crate::error::Error; - use crate::h1::{ExpectHandler, UpgradeHandler}; use crate::test::TestBuffer; + use crate::{error::Error, KeepAlive}; + use crate::{ + h1::{ExpectHandler, UpgradeHandler}, + test::TestSeqBuffer, + }; + + fn find_slice(haystack: &[u8], needle: &[u8], from: usize) -> Option { + haystack[from..] + .windows(needle.len()) + .position(|window| window == needle) + } + + fn stabilize_date_header(payload: &mut [u8]) { + let mut from = 0; + + while let Some(pos) = find_slice(&payload, b"date", from) { + payload[(from + pos)..(from + pos + 35)] + .copy_from_slice(b"date: Thu, 01 Jan 1970 12:34:56 UTC"); + from += 35; + } + } + + fn ok_service() -> impl Service + { + fn_service(|_req: Request| ready(Ok::<_, Error>(Response::Ok().finish()))) + } + + fn echo_path_service( + ) -> impl Service { + fn_service(|req: Request| { + let path = req.path().as_bytes(); + ready(Ok::<_, Error>(Response::Ok().body(Body::from_slice(path)))) + }) + } + + fn echo_payload_service( + ) -> impl Service { + fn_service(|mut req: Request| { + Box::pin(async move { + use futures_util::stream::StreamExt as _; + + let mut pl = req.take_payload(); + let mut body = BytesMut::new(); + while let Some(chunk) = pl.next().await { + body.extend_from_slice(chunk.unwrap().bytes()) + } + + Ok::<_, Error>(Response::Ok().body(body)) + }) + }) + } #[actix_rt::test] async fn test_req_parse_err() { @@ -921,14 +1007,14 @@ mod tests { let mut h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new( buf, ServiceConfig::default(), - CloneableService::new( - (|_| ok::<_, Error>(Response::Ok().finish())).into_service(), - ), + CloneableService::new(ok_service()), CloneableService::new(ExpectHandler), None, None, + Extensions::new(), None, ); + match Pin::new(&mut h1).poll(cx) { Poll::Pending => panic!(), Poll::Ready(res) => assert!(res.is_err()), @@ -944,4 +1030,274 @@ mod tests { }) .await; } + + #[actix_rt::test] + async fn test_pipelining() { + lazy(|cx| { + let buf = TestBuffer::new( + "\ + GET /abcd HTTP/1.1\r\n\r\n\ + GET /def HTTP/1.1\r\n\r\n\ + ", + ); + + let cfg = ServiceConfig::new(KeepAlive::Disabled, 1, 1, false, None); + + let mut h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new( + buf, + cfg, + CloneableService::new(echo_path_service()), + CloneableService::new(ExpectHandler), + None, + None, + Extensions::new(), + None, + ); + + assert!(matches!(&h1.inner, DispatcherState::Normal(_))); + + match Pin::new(&mut h1).poll(cx) { + Poll::Pending => panic!("first poll should not be pending"), + Poll::Ready(res) => assert!(res.is_ok()), + } + + // polls: initial => shutdown + assert_eq!(h1.poll_count, 2); + + if let DispatcherState::Normal(ref mut inner) = h1.inner { + let res = &mut inner.io.take().unwrap().write_buf[..]; + stabilize_date_header(res); + + let exp = b"\ + HTTP/1.1 200 OK\r\n\ + content-length: 5\r\n\ + connection: close\r\n\ + date: Thu, 01 Jan 1970 12:34:56 UTC\r\n\r\n\ + /abcd\ + HTTP/1.1 200 OK\r\n\ + content-length: 4\r\n\ + connection: close\r\n\ + date: Thu, 01 Jan 1970 12:34:56 UTC\r\n\r\n\ + /def\ + "; + + assert_eq!(res.to_vec(), exp.to_vec()); + } + }) + .await; + + lazy(|cx| { + let buf = TestBuffer::new( + "\ + GET /abcd HTTP/1.1\r\n\r\n\ + GET /def HTTP/1\r\n\r\n\ + ", + ); + + let cfg = ServiceConfig::new(KeepAlive::Disabled, 1, 1, false, None); + + let mut h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new( + buf, + cfg, + CloneableService::new(echo_path_service()), + CloneableService::new(ExpectHandler), + None, + None, + Extensions::new(), + None, + ); + + assert!(matches!(&h1.inner, DispatcherState::Normal(_))); + + match Pin::new(&mut h1).poll(cx) { + Poll::Pending => panic!("first poll should not be pending"), + Poll::Ready(res) => assert!(res.is_err()), + } + + // polls: initial => shutdown + assert_eq!(h1.poll_count, 1); + + if let DispatcherState::Normal(ref mut inner) = h1.inner { + let res = &mut inner.io.take().unwrap().write_buf[..]; + stabilize_date_header(res); + + let exp = b"\ + HTTP/1.1 200 OK\r\n\ + content-length: 5\r\n\ + connection: close\r\n\ + date: Thu, 01 Jan 1970 12:34:56 UTC\r\n\r\n\ + /abcd\ + HTTP/1.1 400 Bad Request\r\n\ + content-length: 0\r\n\ + connection: close\r\n\ + date: Thu, 01 Jan 1970 12:34:56 UTC\r\n\r\n\ + "; + + assert_eq!(res.to_vec(), exp.to_vec()); + } + }) + .await; + } + + #[actix_rt::test] + async fn test_expect() { + lazy(|cx| { + let mut buf = TestSeqBuffer::empty(); + let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None); + let mut h1 = Dispatcher::<_, _, _, _, UpgradeHandler<_>>::new( + buf.clone(), + cfg, + CloneableService::new(echo_payload_service()), + CloneableService::new(ExpectHandler), + None, + None, + Extensions::new(), + None, + ); + + buf.extend_read_buf( + "\ + POST /upload HTTP/1.1\r\n\ + Content-Length: 5\r\n\ + Expect: 100-continue\r\n\ + \r\n\ + ", + ); + + assert!(Pin::new(&mut h1).poll(cx).is_pending()); + assert!(matches!(&h1.inner, DispatcherState::Normal(_))); + + // polls: manual + assert_eq!(h1.poll_count, 1); + eprintln!("poll count: {}", h1.poll_count); + + if let DispatcherState::Normal(ref inner) = h1.inner { + let io = inner.io.as_ref().unwrap(); + let res = &io.write_buf()[..]; + assert_eq!( + str::from_utf8(res).unwrap(), + "HTTP/1.1 100 Continue\r\n\r\n" + ); + } + + buf.extend_read_buf("12345"); + assert!(Pin::new(&mut h1).poll(cx).is_ready()); + + // polls: manual manual shutdown + assert_eq!(h1.poll_count, 3); + + if let DispatcherState::Normal(ref inner) = h1.inner { + let io = inner.io.as_ref().unwrap(); + let mut res = (&io.write_buf()[..]).to_owned(); + stabilize_date_header(&mut res); + + assert_eq!( + str::from_utf8(&res).unwrap(), + "\ + HTTP/1.1 100 Continue\r\n\ + \r\n\ + HTTP/1.1 200 OK\r\n\ + content-length: 5\r\n\ + connection: close\r\n\ + date: Thu, 01 Jan 1970 12:34:56 UTC\r\n\ + \r\n\ + 12345\ + " + ); + } + }) + .await; + } + + #[actix_rt::test] + async fn test_eager_expect() { + lazy(|cx| { + let mut buf = TestSeqBuffer::empty(); + let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None); + let mut h1 = Dispatcher::<_, _, _, _, UpgradeHandler<_>>::new( + buf.clone(), + cfg, + CloneableService::new(echo_path_service()), + CloneableService::new(ExpectHandler), + None, + None, + Extensions::new(), + None, + ); + + buf.extend_read_buf( + "\ + POST /upload HTTP/1.1\r\n\ + Content-Length: 5\r\n\ + Expect: 100-continue\r\n\ + \r\n\ + ", + ); + + assert!(Pin::new(&mut h1).poll(cx).is_ready()); + assert!(matches!(&h1.inner, DispatcherState::Normal(_))); + + // polls: manual shutdown + assert_eq!(h1.poll_count, 2); + + if let DispatcherState::Normal(ref inner) = h1.inner { + let io = inner.io.as_ref().unwrap(); + let mut res = (&io.write_buf()[..]).to_owned(); + stabilize_date_header(&mut res); + + // Despite the content-length header and even though the request payload has not + // been sent, this test expects a complete service response since the payload + // is not used at all. The service passed to dispatcher is path echo and doesn't + // consume payload bytes. + assert_eq!( + str::from_utf8(&res).unwrap(), + "\ + HTTP/1.1 100 Continue\r\n\ + \r\n\ + HTTP/1.1 200 OK\r\n\ + content-length: 7\r\n\ + connection: close\r\n\ + date: Thu, 01 Jan 1970 12:34:56 UTC\r\n\ + \r\n\ + /upload\ + " + ); + } + }) + .await; + } + + #[actix_rt::test] + async fn test_upgrade() { + lazy(|cx| { + let mut buf = TestSeqBuffer::empty(); + let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None); + let mut h1 = Dispatcher::<_, _, _, _, UpgradeHandler<_>>::new( + buf.clone(), + cfg, + CloneableService::new(ok_service()), + CloneableService::new(ExpectHandler), + Some(CloneableService::new(UpgradeHandler(PhantomData))), + None, + Extensions::new(), + None, + ); + + buf.extend_read_buf( + "\ + GET /ws HTTP/1.1\r\n\ + Connection: Upgrade\r\n\ + Upgrade: websocket\r\n\ + \r\n\ + ", + ); + + assert!(Pin::new(&mut h1).poll(cx).is_ready()); + assert!(matches!(&h1.inner, DispatcherState::Upgrade(_))); + + // polls: manual shutdown + assert_eq!(h1.poll_count, 2); + }) + .await; + } } diff --git a/actix-http/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs index e16b4c3dc..b7648eff1 100644 --- a/actix-http/src/h1/encoder.rs +++ b/actix-http/src/h1/encoder.rs @@ -64,14 +64,17 @@ pub(crate) trait MessageType: Sized { // Content length if let Some(status) = self.status() { match status { - StatusCode::NO_CONTENT - | StatusCode::CONTINUE - | StatusCode::PROCESSING => length = BodySize::None, - StatusCode::SWITCHING_PROTOCOLS => { + StatusCode::CONTINUE + | StatusCode::SWITCHING_PROTOCOLS + | StatusCode::PROCESSING + | StatusCode::NO_CONTENT => { + // skip content-length and transfer-encoding headers + // See https://tools.ietf.org/html/rfc7230#section-3.3.1 + // and https://tools.ietf.org/html/rfc7230#section-3.3.2 skip_len = true; - length = BodySize::Stream; + length = BodySize::None } - _ => (), + _ => {} } } match length { @@ -676,4 +679,28 @@ mod tests { assert!(data.contains("authorization: another authorization\r\n")); assert!(data.contains("date: date\r\n")); } + + #[test] + fn test_no_content_length() { + let mut bytes = BytesMut::with_capacity(2048); + + let mut res: Response<()> = + Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>(); + res.headers_mut() + .insert(DATE, HeaderValue::from_static(&"")); + res.headers_mut() + .insert(CONTENT_LENGTH, HeaderValue::from_static(&"0")); + + let _ = res.encode_headers( + &mut bytes, + Version::HTTP_11, + BodySize::Stream, + ConnectionType::Upgrade, + &ServiceConfig::default(), + ); + let data = + String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap(); + assert!(!data.contains("content-length: 0\r\n")); + assert!(!data.contains("transfer-encoding: chunked\r\n")); + } } diff --git a/actix-http/src/h1/expect.rs b/actix-http/src/h1/expect.rs index 6c08df08e..b89c7ff74 100644 --- a/actix-http/src/h1/expect.rs +++ b/actix-http/src/h1/expect.rs @@ -1,7 +1,7 @@ use std::task::{Context, Poll}; use actix_service::{Service, ServiceFactory}; -use futures_util::future::{ok, Ready}; +use futures_util::future::{ready, Ready}; use crate::error::Error; use crate::request::Request; @@ -17,8 +17,8 @@ impl ServiceFactory for ExpectHandler { type InitError = Error; type Future = Ready>; - fn new_service(&self, _: ()) -> Self::Future { - ok(ExpectHandler) + fn new_service(&self, _: Self::Config) -> Self::Future { + ready(Ok(ExpectHandler)) } } @@ -33,6 +33,8 @@ impl Service for ExpectHandler { } fn call(&mut self, req: Request) -> Self::Future { - ok(req) + ready(Ok(req)) + // TODO: add some way to trigger error + // Err(error::ErrorExpectationFailed("test")) } } diff --git a/actix-http/src/h1/payload.rs b/actix-http/src/h1/payload.rs index 6a348810c..d4cfee146 100644 --- a/actix-http/src/h1/payload.rs +++ b/actix-http/src/h1/payload.rs @@ -182,9 +182,7 @@ impl Inner { self.len += data.len(); self.items.push_back(data); self.need_read = self.len < MAX_BUFFER_SIZE; - if let Some(task) = self.task.take() { - task.wake() - } + self.task.wake(); } #[cfg(test)] diff --git a/actix-http/src/h1/service.rs b/actix-http/src/h1/service.rs index 6aafd4089..5008791c0 100644 --- a/actix-http/src/h1/service.rs +++ b/actix-http/src/h1/service.rs @@ -18,6 +18,7 @@ use crate::error::{DispatchError, Error, ParseError}; use crate::helpers::DataFactory; use crate::request::Request; use crate::response::Response; +use crate::{ConnectCallback, Extensions}; use super::codec::Codec; use super::dispatcher::Dispatcher; @@ -30,6 +31,7 @@ pub struct H1Service> { expect: X, upgrade: Option, on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, B)>, } @@ -52,6 +54,7 @@ where expect: ExpectHandler, upgrade: None, on_connect: None, + on_connect_ext: None, _t: PhantomData, } } @@ -213,6 +216,7 @@ where srv: self.srv, upgrade: self.upgrade, on_connect: self.on_connect, + on_connect_ext: self.on_connect_ext, _t: PhantomData, } } @@ -229,6 +233,7 @@ where srv: self.srv, expect: self.expect, on_connect: self.on_connect, + on_connect_ext: self.on_connect_ext, _t: PhantomData, } } @@ -241,6 +246,12 @@ where self.on_connect = f; self } + + /// Set on connect callback. + pub(crate) fn on_connect_ext(mut self, f: Option>>) -> Self { + self.on_connect_ext = f; + self + } } impl ServiceFactory for H1Service @@ -274,6 +285,7 @@ where expect: None, upgrade: None, on_connect: self.on_connect.clone(), + on_connect_ext: self.on_connect_ext.clone(), cfg: Some(self.cfg.clone()), _t: PhantomData, } @@ -303,6 +315,7 @@ where expect: Option, upgrade: Option, on_connect: Option Box>>, + on_connect_ext: Option>>, cfg: Option, _t: PhantomData<(T, B)>, } @@ -352,23 +365,26 @@ where Poll::Ready(result.map(|service| { let this = self.as_mut().project(); + H1ServiceHandler::new( this.cfg.take().unwrap(), service, this.expect.take().unwrap(), this.upgrade.take(), this.on_connect.clone(), + this.on_connect_ext.clone(), ) })) } } -/// `Service` implementation for HTTP1 transport +/// `Service` implementation for HTTP/1 transport pub struct H1ServiceHandler { srv: CloneableService, expect: CloneableService, upgrade: Option>, on_connect: Option Box>>, + on_connect_ext: Option>>, cfg: ServiceConfig, _t: PhantomData<(T, B)>, } @@ -390,6 +406,7 @@ where expect: X, upgrade: Option, on_connect: Option Box>>, + on_connect_ext: Option>>, ) -> H1ServiceHandler { H1ServiceHandler { srv: CloneableService::new(srv), @@ -397,6 +414,7 @@ where upgrade: upgrade.map(CloneableService::new), cfg, on_connect, + on_connect_ext, _t: PhantomData, } } @@ -462,11 +480,13 @@ where } fn call(&mut self, (io, addr): Self::Request) -> Self::Future { - let on_connect = if let Some(ref on_connect) = self.on_connect { - Some(on_connect(&io)) - } else { - None - }; + let deprecated_on_connect = self.on_connect.as_ref().map(|handler| handler(&io)); + + let mut connect_extensions = Extensions::new(); + if let Some(ref handler) = self.on_connect_ext { + // run on_connect_ext callback, populating connect extensions + handler(&io, &mut connect_extensions); + } Dispatcher::new( io, @@ -474,7 +494,8 @@ where self.srv.clone(), self.expect.clone(), self.upgrade.clone(), - on_connect, + deprecated_on_connect, + connect_extensions, addr, ) } diff --git a/actix-http/src/h1/upgrade.rs b/actix-http/src/h1/upgrade.rs index 22ba99e26..8615f27a8 100644 --- a/actix-http/src/h1/upgrade.rs +++ b/actix-http/src/h1/upgrade.rs @@ -3,13 +3,13 @@ use std::task::{Context, Poll}; use actix_codec::Framed; use actix_service::{Service, ServiceFactory}; -use futures_util::future::Ready; +use futures_util::future::{ready, Ready}; use crate::error::Error; use crate::h1::Codec; use crate::request::Request; -pub struct UpgradeHandler(PhantomData); +pub struct UpgradeHandler(pub(crate) PhantomData); impl ServiceFactory for UpgradeHandler { type Config = (); @@ -36,6 +36,6 @@ impl Service for UpgradeHandler { } fn call(&mut self, _: Self::Request) -> Self::Future { - unimplemented!() + ready(Ok(())) } } diff --git a/actix-http/src/h2/dispatcher.rs b/actix-http/src/h2/dispatcher.rs index daa651f4d..594339121 100644 --- a/actix-http/src/h2/dispatcher.rs +++ b/actix-http/src/h2/dispatcher.rs @@ -24,6 +24,7 @@ use crate::message::ResponseHead; use crate::payload::Payload; use crate::request::Request; use crate::response::Response; +use crate::Extensions; const CHUNK_SIZE: usize = 16_384; @@ -36,6 +37,7 @@ where service: CloneableService, connection: Connection, on_connect: Option>, + on_connect_data: Extensions, config: ServiceConfig, peer_addr: Option, ka_expire: Instant, @@ -56,6 +58,7 @@ where service: CloneableService, connection: Connection, on_connect: Option>, + on_connect_data: Extensions, config: ServiceConfig, timeout: Option, peer_addr: Option, @@ -82,6 +85,7 @@ where peer_addr, connection, on_connect, + on_connect_data, ka_expire, ka_timer, _t: PhantomData, @@ -130,11 +134,15 @@ where head.headers = parts.headers.into(); head.peer_addr = this.peer_addr; + // DEPRECATED // set on_connect data if let Some(ref on_connect) = this.on_connect { on_connect.set(&mut req.extensions_mut()); } + // merge on_connect_ext data into request extensions + req.extensions_mut().drain_from(&mut this.on_connect_data); + actix_rt::spawn(ServiceResponse::< S::Future, S::Response, diff --git a/actix-http/src/h2/service.rs b/actix-http/src/h2/service.rs index 6b5620e02..3103127b4 100644 --- a/actix-http/src/h2/service.rs +++ b/actix-http/src/h2/service.rs @@ -2,7 +2,7 @@ use std::future::Future; use std::marker::PhantomData; use std::pin::Pin; use std::task::{Context, Poll}; -use std::{net, rc}; +use std::{net, rc::Rc}; use actix_codec::{AsyncRead, AsyncWrite}; use actix_rt::net::TcpStream; @@ -23,6 +23,7 @@ use crate::error::{DispatchError, Error}; use crate::helpers::DataFactory; use crate::request::Request; use crate::response::Response; +use crate::{ConnectCallback, Extensions}; use super::dispatcher::Dispatcher; @@ -30,7 +31,8 @@ use super::dispatcher::Dispatcher; pub struct H2Service { srv: S, cfg: ServiceConfig, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, B)>, } @@ -50,19 +52,27 @@ where H2Service { cfg, on_connect: None, + on_connect_ext: None, srv: service.into_factory(), _t: PhantomData, } } /// Set on connect callback. + pub(crate) fn on_connect( mut self, - f: Option Box>>, + f: Option Box>>, ) -> Self { self.on_connect = f; self } + + /// Set on connect callback. + pub(crate) fn on_connect_ext(mut self, f: Option>>) -> Self { + self.on_connect_ext = f; + self + } } impl H2Service @@ -203,6 +213,7 @@ where fut: self.srv.new_service(()), cfg: Some(self.cfg.clone()), on_connect: self.on_connect.clone(), + on_connect_ext: self.on_connect_ext.clone(), _t: PhantomData, } } @@ -214,7 +225,8 @@ pub struct H2ServiceResponse { #[pin] fut: S::Future, cfg: Option, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, B)>, } @@ -237,6 +249,7 @@ where H2ServiceHandler::new( this.cfg.take().unwrap(), this.on_connect.clone(), + this.on_connect_ext.clone(), service, ) })) @@ -247,7 +260,8 @@ where pub struct H2ServiceHandler { srv: CloneableService, cfg: ServiceConfig, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, B)>, } @@ -261,12 +275,14 @@ where { fn new( cfg: ServiceConfig, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, srv: S, ) -> H2ServiceHandler { H2ServiceHandler { cfg, on_connect, + on_connect_ext, srv: CloneableService::new(srv), _t: PhantomData, } @@ -296,18 +312,21 @@ where } fn call(&mut self, (io, addr): Self::Request) -> Self::Future { - let on_connect = if let Some(ref on_connect) = self.on_connect { - Some(on_connect(&io)) - } else { - None - }; + let deprecated_on_connect = self.on_connect.as_ref().map(|handler| handler(&io)); + + let mut connect_extensions = Extensions::new(); + if let Some(ref handler) = self.on_connect_ext { + // run on_connect_ext callback, populating connect extensions + handler(&io, &mut connect_extensions); + } H2ServiceHandlerResponse { state: State::Handshake( Some(self.srv.clone()), Some(self.cfg.clone()), addr, - on_connect, + deprecated_on_connect, + Some(connect_extensions), server::handshake(io), ), } @@ -325,6 +344,7 @@ where Option, Option, Option>, + Option, Handshake, ), } @@ -360,6 +380,7 @@ where ref mut config, ref peer_addr, ref mut on_connect, + ref mut on_connect_data, ref mut handshake, ) => match Pin::new(handshake).poll(cx) { Poll::Ready(Ok(conn)) => { @@ -367,6 +388,7 @@ where srv.take().unwrap(), conn, on_connect.take(), + on_connect_data.take().unwrap(), config.take().unwrap(), None, *peer_addr, diff --git a/actix-http/src/header/common/accept.rs b/actix-http/src/header/common/accept.rs index d52eba241..da26b0261 100644 --- a/actix-http/src/header/common/accept.rs +++ b/actix-http/src/header/common/accept.rs @@ -1,3 +1,5 @@ +use std::cmp::Ordering; + use mime::Mime; use crate::header::{qitem, QualityItem}; @@ -7,7 +9,7 @@ header! { /// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2) /// /// The `Accept` header field can be used by user agents to specify - /// response media types that are acceptable. Accept header fields can + /// response media types that are acceptable. Accept header fields can /// be used to indicate that the request is specifically limited to a /// small set of desired types, as in the case of a request for an /// in-line image @@ -97,14 +99,14 @@ header! { test_header!( test1, vec![b"audio/*; q=0.2, audio/basic"], - Some(HeaderField(vec![ + Some(Accept(vec![ QualityItem::new("audio/*".parse().unwrap(), q(200)), qitem("audio/basic".parse().unwrap()), ]))); test_header!( test2, vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"], - Some(HeaderField(vec![ + Some(Accept(vec![ QualityItem::new(mime::TEXT_PLAIN, q(500)), qitem(mime::TEXT_HTML), QualityItem::new( @@ -138,23 +140,148 @@ header! { } impl Accept { - /// A constructor to easily create `Accept: */*`. + /// Construct `Accept: */*`. pub fn star() -> Accept { Accept(vec![qitem(mime::STAR_STAR)]) } - /// A constructor to easily create `Accept: application/json`. + /// Construct `Accept: application/json`. pub fn json() -> Accept { Accept(vec![qitem(mime::APPLICATION_JSON)]) } - /// A constructor to easily create `Accept: text/*`. + /// Construct `Accept: text/*`. pub fn text() -> Accept { Accept(vec![qitem(mime::TEXT_STAR)]) } - /// A constructor to easily create `Accept: image/*`. + /// Construct `Accept: image/*`. pub fn image() -> Accept { Accept(vec![qitem(mime::IMAGE_STAR)]) } + + /// Construct `Accept: text/html`. + pub fn html() -> Accept { + Accept(vec![qitem(mime::TEXT_HTML)]) + } + + /// Returns a sorted list of mime types from highest to lowest preference, accounting for + /// [q-factor weighting] and specificity. + /// + /// [q-factor weighting]: https://tools.ietf.org/html/rfc7231#section-5.3.2 + pub fn mime_precedence(&self) -> Vec { + let mut types = self.0.clone(); + + // use stable sort so items with equal q-factor and specificity retain listed order + types.sort_by(|a, b| { + // sort by q-factor descending + b.quality.cmp(&a.quality).then_with(|| { + // use specificity rules on mime types with + // same q-factor (eg. text/html > text/* > */*) + + // subtypes are not comparable if main type is star, so return + match (a.item.type_(), b.item.type_()) { + (mime::STAR, mime::STAR) => return Ordering::Equal, + + // a is sorted after b + (mime::STAR, _) => return Ordering::Greater, + + // a is sorted before b + (_, mime::STAR) => return Ordering::Less, + + _ => {} + } + + // in both these match expressions, the returned ordering appears + // inverted because sort is high-to-low ("descending") precedence + match (a.item.subtype(), b.item.subtype()) { + (mime::STAR, mime::STAR) => Ordering::Equal, + + // a is sorted after b + (mime::STAR, _) => Ordering::Greater, + + // a is sorted before b + (_, mime::STAR) => Ordering::Less, + + _ => Ordering::Equal, + } + }) + }); + + types.into_iter().map(|qitem| qitem.item).collect() + } + + /// Extracts the most preferable mime type, accounting for [q-factor weighting]. + /// + /// If no q-factors are provided, the first mime type is chosen. Note that items without + /// q-factors are given the maximum preference value. + /// + /// Returns `None` if contained list is empty. + /// + /// [q-factor weighting]: https://tools.ietf.org/html/rfc7231#section-5.3.2 + pub fn mime_preference(&self) -> Option { + let types = self.mime_precedence(); + types.first().cloned() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::header::q; + + #[test] + fn test_mime_precedence() { + let test = Accept(vec![]); + assert!(test.mime_precedence().is_empty()); + + let test = Accept(vec![qitem(mime::APPLICATION_JSON)]); + assert_eq!(test.mime_precedence(), vec!(mime::APPLICATION_JSON)); + + let test = Accept(vec![ + qitem(mime::TEXT_HTML), + "application/xhtml+xml".parse().unwrap(), + QualityItem::new("application/xml".parse().unwrap(), q(0.9)), + QualityItem::new(mime::STAR_STAR, q(0.8)), + ]); + assert_eq!( + test.mime_precedence(), + vec![ + mime::TEXT_HTML, + "application/xhtml+xml".parse().unwrap(), + "application/xml".parse().unwrap(), + mime::STAR_STAR, + ] + ); + + let test = Accept(vec![ + qitem(mime::STAR_STAR), + qitem(mime::IMAGE_STAR), + qitem(mime::IMAGE_PNG), + ]); + assert_eq!( + test.mime_precedence(), + vec![mime::IMAGE_PNG, mime::IMAGE_STAR, mime::STAR_STAR] + ); + } + + #[test] + fn test_mime_preference() { + let test = Accept(vec![ + qitem(mime::TEXT_HTML), + "application/xhtml+xml".parse().unwrap(), + QualityItem::new("application/xml".parse().unwrap(), q(0.9)), + QualityItem::new(mime::STAR_STAR, q(0.8)), + ]); + assert_eq!(test.mime_preference(), Some(mime::TEXT_HTML)); + + let test = Accept(vec![ + QualityItem::new("video/*".parse().unwrap(), q(0.8)), + qitem(mime::IMAGE_PNG), + QualityItem::new(mime::STAR_STAR, q(0.5)), + qitem(mime::IMAGE_SVG), + QualityItem::new(mime::IMAGE_STAR, q(0.8)), + ]); + assert_eq!(test.mime_preference(), Some(mime::IMAGE_PNG)); + } } diff --git a/actix-http/src/header/common/content_disposition.rs b/actix-http/src/header/common/content_disposition.rs index 37da830ca..826cfef63 100644 --- a/actix-http/src/header/common/content_disposition.rs +++ b/actix-http/src/header/common/content_disposition.rs @@ -550,8 +550,7 @@ impl fmt::Display for ContentDisposition { write!(f, "{}", self.disposition)?; self.parameters .iter() - .map(|param| write!(f, "; {}", param)) - .collect() + .try_for_each(|param| write!(f, "; {}", param)) } } diff --git a/actix-http/src/header/common/mod.rs b/actix-http/src/header/common/mod.rs index 83489b864..c3d18613c 100644 --- a/actix-http/src/header/common/mod.rs +++ b/actix-http/src/header/common/mod.rs @@ -3,7 +3,7 @@ //! ## Mime //! //! Several header fields use MIME values for their contents. Keeping with the -//! strongly-typed theme, the [mime](https://docs.rs/mime) crate +//! strongly-typed theme, the [mime] crate //! is used, such as `ContentType(pub Mime)`. #![cfg_attr(rustfmt, rustfmt_skip)] diff --git a/actix-http/src/header/map.rs b/actix-http/src/header/map.rs index 36c050b8f..6ab3509f7 100644 --- a/actix-http/src/header/map.rs +++ b/actix-http/src/header/map.rs @@ -8,8 +8,6 @@ use http::header::{HeaderName, HeaderValue}; /// A set of HTTP headers /// /// `HeaderMap` is an multi-map of [`HeaderName`] to values. -/// -/// [`HeaderName`]: struct.HeaderName.html #[derive(Debug, Clone)] pub struct HeaderMap { pub(crate) inner: FxHashMap, @@ -141,8 +139,6 @@ impl HeaderMap { /// The returned view does not incur any allocations and allows iterating /// the values associated with the key. See [`GetAll`] for more details. /// Returns `None` if there are no values associated with the key. - /// - /// [`GetAll`]: struct.GetAll.html pub fn get_all(&self, name: N) -> GetAll<'_> { GetAll { idx: 0, diff --git a/actix-http/src/header/mod.rs b/actix-http/src/header/mod.rs index 46fb31a62..0f87516eb 100644 --- a/actix-http/src/header/mod.rs +++ b/actix-http/src/header/mod.rs @@ -370,9 +370,7 @@ impl fmt::Display for ExtendedValue { } /// Percent encode a sequence of bytes with a character set defined in -/// [https://tools.ietf.org/html/rfc5987#section-3.2][url] -/// -/// [url]: https://tools.ietf.org/html/rfc5987#section-3.2 +/// pub fn http_percent_encode(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result { let encoded = percent_encoding::percent_encode(bytes, HTTP_VALUE); fmt::Display::fmt(&encoded, f) diff --git a/actix-http/src/header/shared/charset.rs b/actix-http/src/header/shared/charset.rs index 00e7309d4..36bdbf7e2 100644 --- a/actix-http/src/header/shared/charset.rs +++ b/actix-http/src/header/shared/charset.rs @@ -7,9 +7,7 @@ use self::Charset::*; /// /// The string representation is normalized to upper case. /// -/// See [http://www.iana.org/assignments/character-sets/character-sets.xhtml][url]. -/// -/// [url]: http://www.iana.org/assignments/character-sets/character-sets.xhtml +/// See . #[derive(Clone, Debug, PartialEq)] #[allow(non_camel_case_types)] pub enum Charset { diff --git a/actix-http/src/header/shared/entity.rs b/actix-http/src/header/shared/entity.rs index 3525a19c6..344cfb864 100644 --- a/actix-http/src/header/shared/entity.rs +++ b/actix-http/src/header/shared/entity.rs @@ -7,10 +7,12 @@ use crate::header::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer}; /// 1. `%x21`, or /// 2. in the range `%x23` to `%x7E`, or /// 3. above `%x80` +fn entity_validate_char(c: u8) -> bool { + c == 0x21 || (0x23..=0x7e).contains(&c) || (c >= 0x80) +} + fn check_slice_validity(slice: &str) -> bool { - slice - .bytes() - .all(|c| c == b'\x21' || (c >= b'\x23' && c <= b'\x7e') | (c >= b'\x80')) + slice.bytes().all(entity_validate_char) } /// An entity tag, defined in [RFC7232](https://tools.ietf.org/html/rfc7232#section-2.3) diff --git a/actix-http/src/header/shared/quality_item.rs b/actix-http/src/header/shared/quality_item.rs index 98230dec1..01a3b988a 100644 --- a/actix-http/src/header/shared/quality_item.rs +++ b/actix-http/src/header/shared/quality_item.rs @@ -1,10 +1,17 @@ -use std::{cmp, fmt, str}; +use std::{ + cmp, + convert::{TryFrom, TryInto}, + fmt, str, +}; -use self::internal::IntoQuality; +use derive_more::{Display, Error}; + +const MAX_QUALITY: u16 = 1000; +const MAX_FLOAT_QUALITY: f32 = 1.0; /// Represents a quality used in quality values. /// -/// Can be created with the `q` function. +/// Can be created with the [`q`] function. /// /// # Implementation notes /// @@ -18,12 +25,54 @@ use self::internal::IntoQuality; /// /// [RFC7231 Section 5.3.1](https://tools.ietf.org/html/rfc7231#section-5.3.1) /// gives more information on quality values in HTTP header fields. -#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Quality(u16); +impl Quality { + /// # Panics + /// Panics in debug mode when value is not in the range 0.0 <= n <= 1.0. + fn from_f32(value: f32) -> Self { + // Check that `value` is within range should be done before calling this method. + // Just in case, this debug_assert should catch if we were forgetful. + debug_assert!( + (0.0f32..=1.0f32).contains(&value), + "q value must be between 0.0 and 1.0" + ); + + Quality((value * MAX_QUALITY as f32) as u16) + } +} + impl Default for Quality { fn default() -> Quality { - Quality(1000) + Quality(MAX_QUALITY) + } +} + +#[derive(Debug, Clone, Display, Error)] +pub struct QualityOutOfBounds; + +impl TryFrom for Quality { + type Error = QualityOutOfBounds; + + fn try_from(value: u16) -> Result { + if (0..=MAX_QUALITY).contains(&value) { + Ok(Quality(value)) + } else { + Err(QualityOutOfBounds) + } + } +} + +impl TryFrom for Quality { + type Error = QualityOutOfBounds; + + fn try_from(value: f32) -> Result { + if (0.0..=MAX_FLOAT_QUALITY).contains(&value) { + Ok(Quality::from_f32(value)) + } else { + Err(QualityOutOfBounds) + } } } @@ -55,8 +104,9 @@ impl cmp::PartialOrd for QualityItem { impl fmt::Display for QualityItem { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.item, f)?; + match self.quality.0 { - 1000 => Ok(()), + MAX_QUALITY => Ok(()), 0 => f.write_str("; q=0"), x => write!(f, "; q=0.{}", format!("{:03}", x).trim_end_matches('0')), } @@ -66,105 +116,79 @@ impl fmt::Display for QualityItem { impl str::FromStr for QualityItem { type Err = crate::error::ParseError; - fn from_str(s: &str) -> Result, crate::error::ParseError> { - if !s.is_ascii() { + fn from_str(qitem_str: &str) -> Result, crate::error::ParseError> { + if !qitem_str.is_ascii() { return Err(crate::error::ParseError::Header); } + // Set defaults used if parsing fails. - let mut raw_item = s; + let mut raw_item = qitem_str; let mut quality = 1f32; - let parts: Vec<&str> = s.rsplitn(2, ';').map(|x| x.trim()).collect(); + let parts: Vec<_> = qitem_str.rsplitn(2, ';').map(str::trim).collect(); + if parts.len() == 2 { + // example for item with q-factor: + // + // gzip; q=0.65 + // ^^^^^^ parts[0] + // ^^ start + // ^^^^ q_val + // ^^^^ parts[1] + if parts[0].len() < 2 { + // Can't possibly be an attribute since an attribute needs at least a name followed + // by an equals sign. And bare identifiers are forbidden. return Err(crate::error::ParseError::Header); } + let start = &parts[0][0..2]; + if start == "q=" || start == "Q=" { - let q_part = &parts[0][2..parts[0].len()]; - if q_part.len() > 5 { + let q_val = &parts[0][2..]; + if q_val.len() > 5 { + // longer than 5 indicates an over-precise q-factor return Err(crate::error::ParseError::Header); } - match q_part.parse::() { - Ok(q_value) => { - if 0f32 <= q_value && q_value <= 1f32 { - quality = q_value; - raw_item = parts[1]; - } else { - return Err(crate::error::ParseError::Header); - } - } - Err(_) => return Err(crate::error::ParseError::Header), + + let q_value = q_val + .parse::() + .map_err(|_| crate::error::ParseError::Header)?; + + if (0f32..=1f32).contains(&q_value) { + quality = q_value; + raw_item = parts[1]; + } else { + return Err(crate::error::ParseError::Header); } } } - match raw_item.parse::() { - // we already checked above that the quality is within range - Ok(item) => Ok(QualityItem::new(item, from_f32(quality))), - Err(_) => Err(crate::error::ParseError::Header), - } - } -} -#[inline] -fn from_f32(f: f32) -> Quality { - // this function is only used internally. A check that `f` is within range - // should be done before calling this method. Just in case, this - // debug_assert should catch if we were forgetful - debug_assert!( - f >= 0f32 && f <= 1f32, - "q value must be between 0.0 and 1.0" - ); - Quality((f * 1000f32) as u16) + let item = raw_item + .parse::() + .map_err(|_| crate::error::ParseError::Header)?; + + // we already checked above that the quality is within range + Ok(QualityItem::new(item, Quality::from_f32(quality))) + } } /// Convenience function to wrap a value in a `QualityItem` /// Sets `q` to the default 1.0 pub fn qitem(item: T) -> QualityItem { - QualityItem::new(item, Default::default()) + QualityItem::new(item, Quality::default()) } /// Convenience function to create a `Quality` from a float or integer. /// /// Implemented for `u16` and `f32`. Panics if value is out of range. -pub fn q(val: T) -> Quality { - val.into_quality() -} - -mod internal { - use super::Quality; - - // TryFrom is probably better, but it's not stable. For now, we want to - // keep the functionality of the `q` function, while allowing it to be - // generic over `f32` and `u16`. - // - // `q` would panic before, so keep that behavior. `TryFrom` can be - // introduced later for a non-panicking conversion. - - pub trait IntoQuality: Sealed + Sized { - fn into_quality(self) -> Quality; - } - - impl IntoQuality for f32 { - fn into_quality(self) -> Quality { - assert!( - self >= 0f32 && self <= 1f32, - "float must be between 0.0 and 1.0" - ); - super::from_f32(self) - } - } - - impl IntoQuality for u16 { - fn into_quality(self) -> Quality { - assert!(self <= 1000, "u16 must be between 0 and 1000"); - Quality(self) - } - } - - pub trait Sealed {} - impl Sealed for u16 {} - impl Sealed for f32 {} +pub fn q(val: T) -> Quality +where + T: TryInto, + T::Error: fmt::Debug, +{ + // TODO: on next breaking change, handle unwrap differently + val.try_into().unwrap() } #[cfg(test)] @@ -270,15 +294,13 @@ mod tests { } #[test] - #[should_panic] // FIXME - 32-bit msvc unwinding broken - #[cfg_attr(all(target_arch = "x86", target_env = "msvc"), ignore)] + #[should_panic] fn test_quality_invalid() { q(-1.0); } #[test] - #[should_panic] // FIXME - 32-bit msvc unwinding broken - #[cfg_attr(all(target_arch = "x86", target_env = "msvc"), ignore)] + #[should_panic] fn test_quality_invalid2() { q(2.0); } diff --git a/actix-http/src/helpers.rs b/actix-http/src/helpers.rs index bbf358b66..ac0e0f118 100644 --- a/actix-http/src/helpers.rs +++ b/actix-http/src/helpers.rs @@ -50,6 +50,7 @@ impl<'a> io::Write for Writer<'a> { self.0.extend_from_slice(buf); Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/actix-http/src/httpcodes.rs b/actix-http/src/httpcodes.rs index 0c7f23fc8..f421d8e23 100644 --- a/actix-http/src/httpcodes.rs +++ b/actix-http/src/httpcodes.rs @@ -1,10 +1,12 @@ -//! Basic http responses +//! Status code based HTTP response builders. + #![allow(non_upper_case_globals)] + use http::StatusCode; use crate::response::{Response, ResponseBuilder}; -macro_rules! STATIC_RESP { +macro_rules! static_resp { ($name:ident, $status:expr) => { #[allow(non_snake_case, missing_docs)] pub fn $name() -> ResponseBuilder { @@ -14,63 +16,67 @@ macro_rules! STATIC_RESP { } impl Response { - STATIC_RESP!(Ok, StatusCode::OK); - STATIC_RESP!(Created, StatusCode::CREATED); - STATIC_RESP!(Accepted, StatusCode::ACCEPTED); - STATIC_RESP!( + static_resp!(Continue, StatusCode::CONTINUE); + static_resp!(SwitchingProtocols, StatusCode::SWITCHING_PROTOCOLS); + static_resp!(Processing, StatusCode::PROCESSING); + + static_resp!(Ok, StatusCode::OK); + static_resp!(Created, StatusCode::CREATED); + static_resp!(Accepted, StatusCode::ACCEPTED); + static_resp!( NonAuthoritativeInformation, StatusCode::NON_AUTHORITATIVE_INFORMATION ); - STATIC_RESP!(NoContent, StatusCode::NO_CONTENT); - STATIC_RESP!(ResetContent, StatusCode::RESET_CONTENT); - STATIC_RESP!(PartialContent, StatusCode::PARTIAL_CONTENT); - STATIC_RESP!(MultiStatus, StatusCode::MULTI_STATUS); - STATIC_RESP!(AlreadyReported, StatusCode::ALREADY_REPORTED); + static_resp!(NoContent, StatusCode::NO_CONTENT); + static_resp!(ResetContent, StatusCode::RESET_CONTENT); + static_resp!(PartialContent, StatusCode::PARTIAL_CONTENT); + static_resp!(MultiStatus, StatusCode::MULTI_STATUS); + static_resp!(AlreadyReported, StatusCode::ALREADY_REPORTED); - STATIC_RESP!(MultipleChoices, StatusCode::MULTIPLE_CHOICES); - STATIC_RESP!(MovedPermanently, StatusCode::MOVED_PERMANENTLY); - STATIC_RESP!(Found, StatusCode::FOUND); - STATIC_RESP!(SeeOther, StatusCode::SEE_OTHER); - STATIC_RESP!(NotModified, StatusCode::NOT_MODIFIED); - STATIC_RESP!(UseProxy, StatusCode::USE_PROXY); - STATIC_RESP!(TemporaryRedirect, StatusCode::TEMPORARY_REDIRECT); - STATIC_RESP!(PermanentRedirect, StatusCode::PERMANENT_REDIRECT); + static_resp!(MultipleChoices, StatusCode::MULTIPLE_CHOICES); + static_resp!(MovedPermanently, StatusCode::MOVED_PERMANENTLY); + static_resp!(Found, StatusCode::FOUND); + static_resp!(SeeOther, StatusCode::SEE_OTHER); + static_resp!(NotModified, StatusCode::NOT_MODIFIED); + static_resp!(UseProxy, StatusCode::USE_PROXY); + static_resp!(TemporaryRedirect, StatusCode::TEMPORARY_REDIRECT); + static_resp!(PermanentRedirect, StatusCode::PERMANENT_REDIRECT); - STATIC_RESP!(BadRequest, StatusCode::BAD_REQUEST); - STATIC_RESP!(NotFound, StatusCode::NOT_FOUND); - STATIC_RESP!(Unauthorized, StatusCode::UNAUTHORIZED); - STATIC_RESP!(PaymentRequired, StatusCode::PAYMENT_REQUIRED); - STATIC_RESP!(Forbidden, StatusCode::FORBIDDEN); - STATIC_RESP!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED); - STATIC_RESP!(NotAcceptable, StatusCode::NOT_ACCEPTABLE); - STATIC_RESP!( + static_resp!(BadRequest, StatusCode::BAD_REQUEST); + static_resp!(NotFound, StatusCode::NOT_FOUND); + static_resp!(Unauthorized, StatusCode::UNAUTHORIZED); + static_resp!(PaymentRequired, StatusCode::PAYMENT_REQUIRED); + static_resp!(Forbidden, StatusCode::FORBIDDEN); + static_resp!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED); + static_resp!(NotAcceptable, StatusCode::NOT_ACCEPTABLE); + static_resp!( ProxyAuthenticationRequired, StatusCode::PROXY_AUTHENTICATION_REQUIRED ); - STATIC_RESP!(RequestTimeout, StatusCode::REQUEST_TIMEOUT); - STATIC_RESP!(Conflict, StatusCode::CONFLICT); - STATIC_RESP!(Gone, StatusCode::GONE); - STATIC_RESP!(LengthRequired, StatusCode::LENGTH_REQUIRED); - STATIC_RESP!(PreconditionFailed, StatusCode::PRECONDITION_FAILED); - STATIC_RESP!(PreconditionRequired, StatusCode::PRECONDITION_REQUIRED); - STATIC_RESP!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE); - STATIC_RESP!(UriTooLong, StatusCode::URI_TOO_LONG); - STATIC_RESP!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE); - STATIC_RESP!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE); - STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED); - STATIC_RESP!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY); - STATIC_RESP!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS); + static_resp!(RequestTimeout, StatusCode::REQUEST_TIMEOUT); + static_resp!(Conflict, StatusCode::CONFLICT); + static_resp!(Gone, StatusCode::GONE); + static_resp!(LengthRequired, StatusCode::LENGTH_REQUIRED); + static_resp!(PreconditionFailed, StatusCode::PRECONDITION_FAILED); + static_resp!(PreconditionRequired, StatusCode::PRECONDITION_REQUIRED); + static_resp!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE); + static_resp!(UriTooLong, StatusCode::URI_TOO_LONG); + static_resp!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE); + static_resp!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE); + static_resp!(ExpectationFailed, StatusCode::EXPECTATION_FAILED); + static_resp!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY); + static_resp!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS); - STATIC_RESP!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR); - STATIC_RESP!(NotImplemented, StatusCode::NOT_IMPLEMENTED); - STATIC_RESP!(BadGateway, StatusCode::BAD_GATEWAY); - STATIC_RESP!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE); - STATIC_RESP!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT); - STATIC_RESP!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED); - STATIC_RESP!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES); - STATIC_RESP!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE); - STATIC_RESP!(LoopDetected, StatusCode::LOOP_DETECTED); + static_resp!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR); + static_resp!(NotImplemented, StatusCode::NOT_IMPLEMENTED); + static_resp!(BadGateway, StatusCode::BAD_GATEWAY); + static_resp!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE); + static_resp!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT); + static_resp!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED); + static_resp!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES); + static_resp!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE); + static_resp!(LoopDetected, StatusCode::LOOP_DETECTED); } #[cfg(test)] diff --git a/actix-http/src/lib.rs b/actix-http/src/lib.rs index fab91be2b..89d64fb77 100644 --- a/actix-http/src/lib.rs +++ b/actix-http/src/lib.rs @@ -1,4 +1,4 @@ -//! Basic http primitives for actix-net framework. +//! HTTP primitives for the Actix ecosystem. #![deny(rust_2018_idioms)] #![allow( @@ -8,6 +8,8 @@ clippy::borrow_interior_mutable_const )] #![allow(clippy::manual_strip)] // Allow this to keep MSRV(1.42). +#![doc(html_logo_url = "https://actix.rs/img/logo.png")] +#![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #[macro_use] extern crate log; @@ -78,3 +80,5 @@ pub enum Protocol { Http1, Http2, } + +type ConnectCallback = dyn Fn(&IO, &mut Extensions); diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 2def67168..df2f5be50 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -554,8 +554,9 @@ impl ResponseBuilder { self } - /// This method calls provided closure with builder reference if value is - /// true. + /// This method calls provided closure with builder reference if value is `true`. + #[doc(hidden)] + #[deprecated = "Use an if statement."] pub fn if_true(&mut self, value: bool, f: F) -> &mut Self where F: FnOnce(&mut ResponseBuilder), @@ -566,8 +567,9 @@ impl ResponseBuilder { self } - /// This method calls provided closure with builder reference if value is - /// Some. + /// This method calls provided closure with builder reference if value is `Some`. + #[doc(hidden)] + #[deprecated = "Use an if-let construction."] pub fn if_some(&mut self, value: Option, f: F) -> &mut Self where F: FnOnce(T, &mut ResponseBuilder), diff --git a/actix-http/src/service.rs b/actix-http/src/service.rs index 9ee579702..75745209c 100644 --- a/actix-http/src/service.rs +++ b/actix-http/src/service.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; use std::pin::Pin; use std::task::{Context, Poll}; -use std::{fmt, net, rc}; +use std::{fmt, net, rc::Rc}; use actix_codec::{AsyncRead, AsyncWrite, Framed}; use actix_rt::net::TcpStream; @@ -20,15 +20,17 @@ use crate::error::{DispatchError, Error}; use crate::helpers::DataFactory; use crate::request::Request; use crate::response::Response; -use crate::{h1, h2::Dispatcher, Protocol}; +use crate::{h1, h2::Dispatcher, ConnectCallback, Extensions, Protocol}; -/// `ServiceFactory` HTTP1.1/HTTP2 transport implementation +/// A `ServiceFactory` for HTTP/1.1 or HTTP/2 protocol. pub struct HttpService> { srv: S, cfg: ServiceConfig, expect: X, upgrade: Option, - on_connect: Option Box>>, + // DEPRECATED: in favor of on_connect_ext + on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, B)>, } @@ -66,6 +68,7 @@ where expect: h1::ExpectHandler, upgrade: None, on_connect: None, + on_connect_ext: None, _t: PhantomData, } } @@ -81,6 +84,7 @@ where expect: h1::ExpectHandler, upgrade: None, on_connect: None, + on_connect_ext: None, _t: PhantomData, } } @@ -113,6 +117,7 @@ where srv: self.srv, upgrade: self.upgrade, on_connect: self.on_connect, + on_connect_ext: self.on_connect_ext, _t: PhantomData, } } @@ -138,6 +143,7 @@ where srv: self.srv, expect: self.expect, on_connect: self.on_connect, + on_connect_ext: self.on_connect_ext, _t: PhantomData, } } @@ -145,11 +151,17 @@ where /// Set on connect callback. pub(crate) fn on_connect( mut self, - f: Option Box>>, + f: Option Box>>, ) -> Self { self.on_connect = f; self } + + /// Set connect callback with mutable access to request data container. + pub(crate) fn on_connect_ext(mut self, f: Option>>) -> Self { + self.on_connect_ext = f; + self + } } impl HttpService @@ -355,6 +367,7 @@ where expect: None, upgrade: None, on_connect: self.on_connect.clone(), + on_connect_ext: self.on_connect_ext.clone(), cfg: self.cfg.clone(), _t: PhantomData, } @@ -378,7 +391,8 @@ pub struct HttpServiceResponse< fut_upg: Option, expect: Option, upgrade: Option, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, cfg: ServiceConfig, _t: PhantomData<(T, B)>, } @@ -429,6 +443,7 @@ where .fut .poll(cx) .map_err(|e| log::error!("Init http service error: {:?}", e))); + Poll::Ready(result.map(|service| { let this = self.as_mut().project(); HttpServiceHandler::new( @@ -437,6 +452,7 @@ where this.expect.take().unwrap(), this.upgrade.take(), this.on_connect.clone(), + this.on_connect_ext.clone(), ) })) } @@ -448,7 +464,8 @@ pub struct HttpServiceHandler { expect: CloneableService, upgrade: Option>, cfg: ServiceConfig, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, _t: PhantomData<(T, B, X)>, } @@ -469,11 +486,13 @@ where srv: S, expect: X, upgrade: Option, - on_connect: Option Box>>, + on_connect: Option Box>>, + on_connect_ext: Option>>, ) -> HttpServiceHandler { HttpServiceHandler { cfg, on_connect, + on_connect_ext, srv: CloneableService::new(srv), expect: CloneableService::new(expect), upgrade: upgrade.map(CloneableService::new), @@ -543,11 +562,12 @@ where } fn call(&mut self, (io, proto, peer_addr): Self::Request) -> Self::Future { - let on_connect = if let Some(ref on_connect) = self.on_connect { - Some(on_connect(&io)) - } else { - None - }; + let mut connect_extensions = Extensions::new(); + + let deprecated_on_connect = self.on_connect.as_ref().map(|handler| handler(&io)); + if let Some(ref handler) = self.on_connect_ext { + handler(&io, &mut connect_extensions); + } match proto { Protocol::Http2 => HttpServiceHandlerResponse { @@ -555,10 +575,12 @@ where server::handshake(io), self.cfg.clone(), self.srv.clone(), - on_connect, + deprecated_on_connect, + connect_extensions, peer_addr, ))), }, + Protocol::Http1 => HttpServiceHandlerResponse { state: State::H1(h1::Dispatcher::new( io, @@ -566,7 +588,8 @@ where self.srv.clone(), self.expect.clone(), self.upgrade.clone(), - on_connect, + deprecated_on_connect, + connect_extensions, peer_addr, )), }, @@ -595,6 +618,7 @@ where ServiceConfig, CloneableService, Option>, + Extensions, Option, )>, ), @@ -670,9 +694,16 @@ where } else { panic!() }; - let (_, cfg, srv, on_connect, peer_addr) = data.take().unwrap(); + let (_, cfg, srv, on_connect, on_connect_data, peer_addr) = + data.take().unwrap(); self.set(State::H2(Dispatcher::new( - srv, conn, on_connect, cfg, None, peer_addr, + srv, + conn, + on_connect, + on_connect_data, + cfg, + None, + peer_addr, ))); self.poll(cx) } diff --git a/actix-http/src/test.rs b/actix-http/src/test.rs index b79f5a73c..4512e72c2 100644 --- a/actix-http/src/test.rs +++ b/actix-http/src/test.rs @@ -1,9 +1,14 @@ -//! Test Various helpers for Actix applications to use during testing. -use std::convert::TryFrom; -use std::io::{self, Read, Write}; -use std::pin::Pin; -use std::str::FromStr; -use std::task::{Context, Poll}; +//! Various testing helpers for use in internal and app tests. + +use std::{ + cell::{Ref, RefCell}, + convert::TryFrom, + io::{self, Read, Write}, + pin::Pin, + rc::Rc, + str::FromStr, + task::{Context, Poll}, +}; use actix_codec::{AsyncRead, AsyncWrite}; use bytes::{Bytes, BytesMut}; @@ -183,7 +188,7 @@ fn parts(parts: &mut Option) -> &mut Inner { parts.as_mut().expect("cannot reuse test request builder") } -/// Async io buffer +/// Async I/O test buffer. pub struct TestBuffer { pub read_buf: BytesMut, pub write_buf: BytesMut, @@ -191,24 +196,24 @@ pub struct TestBuffer { } impl TestBuffer { - /// Create new TestBuffer instance - pub fn new(data: T) -> TestBuffer + /// Create new `TestBuffer` instance with initial read buffer. + pub fn new(data: T) -> Self where - BytesMut: From, + T: Into, { - TestBuffer { - read_buf: BytesMut::from(data), + Self { + read_buf: data.into(), write_buf: BytesMut::new(), err: None, } } - /// Create new empty TestBuffer instance - pub fn empty() -> TestBuffer { - TestBuffer::new("") + /// Create new empty `TestBuffer` instance. + pub fn empty() -> Self { + Self::new("") } - /// Add extra data to read buffer. + /// Add data to read buffer. pub fn extend_read_buf>(&mut self, data: T) { self.read_buf.extend_from_slice(data.as_ref()) } @@ -236,6 +241,7 @@ impl io::Write for TestBuffer { self.write_buf.extend(buf); Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } @@ -268,3 +274,113 @@ impl AsyncWrite for TestBuffer { Poll::Ready(Ok(())) } } + +/// Async I/O test buffer with ability to incrementally add to the read buffer. +#[derive(Clone)] +pub struct TestSeqBuffer(Rc>); + +impl TestSeqBuffer { + /// Create new `TestBuffer` instance with initial read buffer. + pub fn new(data: T) -> Self + where + T: Into, + { + Self(Rc::new(RefCell::new(TestSeqInner { + read_buf: data.into(), + write_buf: BytesMut::new(), + err: None, + }))) + } + + /// Create new empty `TestBuffer` instance. + pub fn empty() -> Self { + Self::new("") + } + + pub fn read_buf(&self) -> Ref<'_, BytesMut> { + Ref::map(self.0.borrow(), |inner| &inner.read_buf) + } + + pub fn write_buf(&self) -> Ref<'_, BytesMut> { + Ref::map(self.0.borrow(), |inner| &inner.write_buf) + } + + pub fn err(&self) -> Ref<'_, Option> { + Ref::map(self.0.borrow(), |inner| &inner.err) + } + + /// Add data to read buffer. + pub fn extend_read_buf>(&mut self, data: T) { + self.0 + .borrow_mut() + .read_buf + .extend_from_slice(data.as_ref()) + } +} + +pub struct TestSeqInner { + read_buf: BytesMut, + write_buf: BytesMut, + err: Option, +} + +impl io::Read for TestSeqBuffer { + fn read(&mut self, dst: &mut [u8]) -> Result { + if self.0.borrow().read_buf.is_empty() { + if self.0.borrow().err.is_some() { + Err(self.0.borrow_mut().err.take().unwrap()) + } else { + Err(io::Error::new(io::ErrorKind::WouldBlock, "")) + } + } else { + let size = std::cmp::min(self.0.borrow().read_buf.len(), dst.len()); + let b = self.0.borrow_mut().read_buf.split_to(size); + dst[..size].copy_from_slice(&b); + Ok(size) + } + } +} + +impl io::Write for TestSeqBuffer { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.0.borrow_mut().write_buf.extend(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl AsyncRead for TestSeqBuffer { + fn poll_read( + self: Pin<&mut Self>, + _: &mut Context<'_>, + buf: &mut [u8], + ) -> Poll> { + let r = self.get_mut().read(buf); + match r { + Ok(n) => Poll::Ready(Ok(n)), + Err(err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending, + Err(err) => Poll::Ready(Err(err)), + } + } +} + +impl AsyncWrite for TestSeqBuffer { + fn poll_write( + self: Pin<&mut Self>, + _: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + Poll::Ready(self.get_mut().write(buf)) + } + + fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } +} diff --git a/actix-http/src/ws/mask.rs b/actix-http/src/ws/mask.rs index 726b1a4a1..d37d57eb1 100644 --- a/actix-http/src/ws/mask.rs +++ b/actix-http/src/ws/mask.rs @@ -4,7 +4,9 @@ use std::ptr::copy_nonoverlapping; use std::slice; // Holds a slice guaranteed to be shorter than 8 bytes -struct ShortSlice<'a>(&'a mut [u8]); +struct ShortSlice<'a> { + inner: &'a mut [u8], +} impl<'a> ShortSlice<'a> { /// # Safety @@ -12,10 +14,11 @@ impl<'a> ShortSlice<'a> { unsafe fn new(slice: &'a mut [u8]) -> Self { // Sanity check for debug builds debug_assert!(slice.len() < 8); - ShortSlice(slice) + ShortSlice { inner: slice } } + fn len(&self) -> usize { - self.0.len() + self.inner.len() } } @@ -56,7 +59,7 @@ pub(crate) fn apply_mask(buf: &mut [u8], mask_u32: u32) { fn xor_short(buf: ShortSlice<'_>, mask: u64) { // SAFETY: we know that a `ShortSlice` fits in a u64 unsafe { - let (ptr, len) = (buf.0.as_mut_ptr(), buf.0.len()); + let (ptr, len) = (buf.inner.as_mut_ptr(), buf.len()); let mut b: u64 = 0; #[allow(trivial_casts)] copy_nonoverlapping(ptr, &mut b as *mut _ as *mut u8, len); @@ -96,7 +99,13 @@ fn align_buf(buf: &mut [u8]) -> (ShortSlice<'_>, &mut [u64], ShortSlice<'_>) { // SAFETY: we know the middle section is correctly aligned, and the outer // sections are smaller than 8 bytes - unsafe { (ShortSlice::new(head), cast_slice(mid), ShortSlice(tail)) } + unsafe { + ( + ShortSlice::new(head), + cast_slice(mid), + ShortSlice::new(tail), + ) + } } else { // We didn't cross even one aligned boundary! diff --git a/actix-http/src/ws/mod.rs b/actix-http/src/ws/mod.rs index 6ffdecc35..cd212fb7e 100644 --- a/actix-http/src/ws/mod.rs +++ b/actix-http/src/ws/mod.rs @@ -197,13 +197,13 @@ mod tests { let req = TestRequest::default().method(Method::POST).finish(); assert_eq!( HandshakeError::GetMethodRequired, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default().finish(); assert_eq!( HandshakeError::NoWebsocketUpgrade, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default() @@ -211,7 +211,7 @@ mod tests { .finish(); assert_eq!( HandshakeError::NoWebsocketUpgrade, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default() @@ -222,7 +222,7 @@ mod tests { .finish(); assert_eq!( HandshakeError::NoConnectionUpgrade, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default() @@ -237,7 +237,7 @@ mod tests { .finish(); assert_eq!( HandshakeError::NoVersionHeader, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default() @@ -256,7 +256,7 @@ mod tests { .finish(); assert_eq!( HandshakeError::UnsupportedVersion, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default() @@ -275,7 +275,7 @@ mod tests { .finish(); assert_eq!( HandshakeError::BadWebsocketKey, - verify_handshake(req.head()).err().unwrap() + verify_handshake(req.head()).unwrap_err(), ); let req = TestRequest::default() diff --git a/actix-http/tests/test_openssl.rs b/actix-http/tests/test_openssl.rs index 795deacdc..05f01d240 100644 --- a/actix-http/tests/test_openssl.rs +++ b/actix-http/tests/test_openssl.rs @@ -411,8 +411,10 @@ async fn test_h2_on_connect() { let srv = test_server(move || { HttpService::build() .on_connect(|_| 10usize) + .on_connect_ext(|_, data| data.insert(20isize)) .h2(|req: Request| { assert!(req.extensions().contains::()); + assert!(req.extensions().contains::()); ok::<_, ()>(Response::Ok().finish()) }) .openssl(ssl_acceptor()) diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index 0375b6f66..de6368fda 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -663,8 +663,10 @@ async fn test_h1_on_connect() { let srv = test_server(|| { HttpService::build() .on_connect(|_| 10usize) + .on_connect_ext(|_, data| data.insert(20isize)) .h1(|req: Request| { assert!(req.extensions().contains::()); + assert!(req.extensions().contains::()); future::ok::<_, ()>(Response::Ok().finish()) }) .tcp() diff --git a/actix-multipart/src/server.rs b/actix-multipart/src/server.rs index b9ebf97cc..b476f1791 100644 --- a/actix-multipart/src/server.rs +++ b/actix-multipart/src/server.rs @@ -725,9 +725,7 @@ impl Drop for Safety { if Rc::strong_count(&self.payload) != self.level { self.clean.set(true); } - if let Some(task) = self.task.take() { - task.wake() - } + self.task.wake(); } } diff --git a/actix-web-actors/CHANGES.md b/actix-web-actors/CHANGES.md index 4b9381a33..9df0df159 100644 --- a/actix-web-actors/CHANGES.md +++ b/actix-web-actors/CHANGES.md @@ -1,7 +1,7 @@ # Changes ## Unreleased - 2020-xx-xx - +* Upgrade `pin-project` to `1.0`. ## 3.0.0 - 2020-09-11 * No significant changes from `3.0.0-beta.2`. diff --git a/actix-web-actors/Cargo.toml b/actix-web-actors/Cargo.toml index 2f3c63022..920940c40 100644 --- a/actix-web-actors/Cargo.toml +++ b/actix-web-actors/Cargo.toml @@ -23,7 +23,7 @@ actix-codec = "0.3.0" bytes = "0.5.2" futures-channel = { version = "0.3.5", default-features = false } futures-core = { version = "0.3.5", default-features = false } -pin-project = "0.4.17" +pin-project = "1.0.0" [dev-dependencies] actix-rt = "1.1.1" diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index 3f5972532..8fd03f6a1 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -164,7 +164,6 @@ pub fn handshake_with_protocols( let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS) .upgrade("websocket") - .header(header::TRANSFER_ENCODING, "chunked") .header(header::SEC_WEBSOCKET_ACCEPT, key.as_str()) .take(); @@ -664,10 +663,10 @@ mod tests { ) .to_http_request(); - assert_eq!( - StatusCode::SWITCHING_PROTOCOLS, - handshake(&req).unwrap().finish().status() - ); + let resp = handshake(&req).unwrap().finish(); + assert_eq!(StatusCode::SWITCHING_PROTOCOLS, resp.status()); + assert_eq!(None, resp.headers().get(&header::CONTENT_LENGTH)); + assert_eq!(None, resp.headers().get(&header::TRANSFER_ENCODING)); let req = TestRequest::default() .header( diff --git a/actix-web-codegen/README.md b/actix-web-codegen/README.md index 6eca847b8..283591e86 100644 --- a/actix-web-codegen/README.md +++ b/actix-web-codegen/README.md @@ -3,7 +3,7 @@ > Helper and convenience macros for Actix Web [![crates.io](https://meritbadge.herokuapp.com/actix-web-codegen)](https://crates.io/crates/actix-web-codegen) -[![Documentation](https://docs.rs/actix-web-codegen/badge.svg)](https://docs.rs/actix-web) +[![Documentation](https://docs.rs/actix-web-codegen/badge.svg)](https://docs.rs/actix-web-codegen/0.4.0/actix_web_codegen/) [![Version](https://img.shields.io/badge/rustc-1.42+-ab6000.svg)](https://blog.rust-lang.org/2020/03/12/Rust-1.42.html) [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index af2bc7f18..50e5be712 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -8,7 +8,7 @@ //! are re-exported. //! //! # Runtime Setup -//! Used for setting up the actix async runtime. See [main] macro docs. +//! Used for setting up the actix async runtime. See [macro@main] macro docs. //! //! ```rust //! #[actix_web_codegen::main] // or `#[actix_web::main]` in Actix Web apps @@ -34,7 +34,7 @@ //! //! # Multiple Method Handlers //! Similar to the single method handler macro but takes one or more arguments for the HTTP methods -//! it should respond to. See [route] macro docs. +//! it should respond to. See [macro@route] macro docs. //! //! ```rust //! # use actix_web::HttpResponse; @@ -46,17 +46,15 @@ //! ``` //! //! [actix-web attributes docs]: https://docs.rs/actix-web/*/actix_web/#attributes -//! [main]: attr.main.html -//! [route]: attr.route.html -//! [GET]: attr.get.html -//! [POST]: attr.post.html -//! [PUT]: attr.put.html -//! [DELETE]: attr.delete.html -//! [HEAD]: attr.head.html -//! [CONNECT]: attr.connect.html -//! [OPTIONS]: attr.options.html -//! [TRACE]: attr.trace.html -//! [PATCH]: attr.patch.html +//! [GET]: macro@get +//! [POST]: macro@post +//! [PUT]: macro@put +//! [HEAD]: macro@head +//! [CONNECT]: macro@macro@connect +//! [OPTIONS]: macro@options +//! [TRACE]: macro@trace +//! [PATCH]: macro@patch +//! [DELETE]: macro@delete #![recursion_limit = "512"] diff --git a/actix-web-codegen/tests/trybuild.rs b/actix-web-codegen/tests/trybuild.rs index 1bc2bd25e..6c7c58986 100644 --- a/actix-web-codegen/tests/trybuild.rs +++ b/actix-web-codegen/tests/trybuild.rs @@ -6,9 +6,8 @@ fn compile_macros() { t.compile_fail("tests/trybuild/simple-fail.rs"); t.pass("tests/trybuild/route-ok.rs"); - t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs"); - t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs"); + test_route_duplicate_unexpected_method(&t); test_route_missing_method(&t) } @@ -25,3 +24,13 @@ fn test_route_missing_method(t: &trybuild::TestCases) { #[rustversion::nightly] fn test_route_missing_method(_t: &trybuild::TestCases) {} + +// FIXME: Re-test them on nightly once rust-lang/rust#77993 is fixed. +#[rustversion::not(nightly)] +fn test_route_duplicate_unexpected_method(t: &trybuild::TestCases) { + t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs"); + t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs"); +} + +#[rustversion::nightly] +fn test_route_duplicate_unexpected_method(_t: &trybuild::TestCases) {} diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 07a469746..e4f801bbe 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -1,6 +1,34 @@ # Changes ## Unreleased - 2020-xx-xx +### Changed +* Bumped `rand` to `0.8` + + +## 2.0.3 - 2020-11-29 +### Fixed +* Ensure `actix-http` dependency uses same `serde_urlencoded`. + + +## 2.0.2 - 2020-11-25 +### Changed +* Upgrade `serde_urlencoded` to `0.7`. [#1773] + +[#1773]: https://github.com/actix/actix-web/pull/1773 + + +## 2.0.1 - 2020-10-30 +### Changed +* Upgrade `base64` to `0.13`. [#1744] +* Deprecate `ClientRequest::{if_some, if_true}`. [#1760] + +### Fixed +* Use `Accept-Encoding: identity` instead of `Accept-Encoding: br` when no compression feature + is enabled [#1737] + +[#1737]: https://github.com/actix/actix-web/pull/1737 +[#1760]: https://github.com/actix/actix-web/pull/1760 +[#1744]: https://github.com/actix/actix-web/pull/1744 ## 2.0.0 - 2020-09-11 diff --git a/awc/Cargo.toml b/awc/Cargo.toml index c67b6ba6f..2e92526d2 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "awc" -version = "2.0.0" +version = "2.0.3" authors = ["Nikolay Kim "] -description = "Async HTTP client library that uses the Actix runtime." +description = "Async HTTP and WebSocket client library built on the Actix ecosystem" readme = "README.md" keywords = ["actix", "http", "framework", "async", "web"] homepage = "https://actix.rs" @@ -39,20 +39,21 @@ compress = ["actix-http/compress"] [dependencies] actix-codec = "0.3.0" actix-service = "1.0.6" -actix-http = "2.0.0" +actix-http = "2.2.0" actix-rt = "1.0.0" -base64 = "0.12" +base64 = "0.13" bytes = "0.5.3" +cfg-if = "1.0" derive_more = "0.99.2" futures-core = { version = "0.3.5", default-features = false } log =" 0.4" mime = "0.3" percent-encoding = "2.1" -rand = "0.7" +rand = "0.8" serde = "1.0" serde_json = "1.0" -serde_urlencoded = "0.6.1" +serde_urlencoded = "0.7" open-ssl = { version = "0.10", package = "openssl", optional = true } rust-tls = { version = "0.18.0", package = "rustls", optional = true, features = ["dangerous_configuration"] } diff --git a/awc/README.md b/awc/README.md index 2b6309c1d..b97d4fa00 100644 --- a/awc/README.md +++ b/awc/README.md @@ -1,33 +1,36 @@ -# Actix http client [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/awc)](https://crates.io/crates/awc) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +# awc (Actix Web Client) -An HTTP Client +> Async HTTP and WebSocket client library. -## Documentation & community resources +[![crates.io](https://img.shields.io/crates/v/awc?label=latest)](https://crates.io/crates/awc) +[![Documentation](https://docs.rs/awc/badge.svg?version=2.0.3)](https://docs.rs/awc/2.0.3) +![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/awc) +[![Dependency Status](https://deps.rs/crate/awc/2.0.3/status.svg)](https://deps.rs/crate/awc/2.0.3) +[![Join the chat at https://gitter.im/actix/actix-web](https://badges.gitter.im/actix/actix-web.svg)](https://gitter.im/actix/actix-web?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -* [User Guide](https://actix.rs/docs/) -* [API Documentation](https://docs.rs/awc/) -* [Chat on gitter](https://gitter.im/actix/actix) -* Cargo package: [awc](https://crates.io/crates/awc) -* Minimum supported Rust version: 1.40 or later +## Documentation & Resources + +- [API Documentation](https://docs.rs/awc) +- [Example Project](https://github.com/actix/examples/tree/HEAD/awc_https) +- [Chat on Gitter](https://gitter.im/actix/actix-web) +- Minimum Supported Rust Version (MSRV): 1.42.0 ## Example - ```rust use actix_rt::System; use awc::Client; -use futures::future::{Future, lazy}; fn main() { - System::new("test").block_on(lazy(|| { - let mut client = Client::default(); + System::new("test").block_on(async { + let client = Client::default(); - client.get("http://www.rust-lang.org") // <- Create request builder - .header("User-Agent", "Actix-web") - .send() // <- Send http request - .and_then(|response| { // <- server http response - println!("Response: {:?}", response); - Ok(()) - }) - })); + let res = client + .get("http://www.rust-lang.org") // <- Create request builder + .header("User-Agent", "Actix-web") + .send() // <- Send http request + .await; + + println!("Response: {:?}", res); // <- server http response + }); } ``` diff --git a/awc/src/lib.rs b/awc/src/lib.rs index 45c52092a..fb6ed086a 100644 --- a/awc/src/lib.rs +++ b/awc/src/lib.rs @@ -1,11 +1,4 @@ -#![deny(rust_2018_idioms)] -#![allow( - clippy::type_complexity, - clippy::borrow_interior_mutable_const, - clippy::needless_doctest_main -)] - -//! `awc` is a HTTP and WebSocket client library built using the Actix ecosystem. +//! `awc` is a HTTP and WebSocket client library built on the Actix ecosystem. //! //! ## Making a GET request //! @@ -91,6 +84,15 @@ //! # } //! ``` +#![deny(rust_2018_idioms)] +#![allow( + clippy::type_complexity, + clippy::borrow_interior_mutable_const, + clippy::needless_doctest_main +)] +#![doc(html_logo_url = "https://actix.rs/img/logo.png")] +#![doc(html_favicon_url = "https://actix.rs/favicon.ico")] + use std::cell::RefCell; use std::convert::TryFrom; use std::rc::Rc; diff --git a/awc/src/request.rs b/awc/src/request.rs index dcada2c6d..1e49aae3c 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -21,10 +21,15 @@ use crate::frozen::FrozenClientRequest; use crate::sender::{PrepForSendingError, RequestSender, SendClientRequest}; use crate::ClientConfig; -#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] -const HTTPS_ENCODING: &str = "br, gzip, deflate"; -#[cfg(not(any(feature = "flate2-zlib", feature = "flate2-rust")))] -const HTTPS_ENCODING: &str = "br"; +cfg_if::cfg_if! { + if #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] { + const HTTPS_ENCODING: &str = "br, gzip, deflate"; + } else if #[cfg(feature = "compress")] { + const HTTPS_ENCODING: &str = "br"; + } else { + const HTTPS_ENCODING: &str = "identity"; + } +} /// An HTTP Client request builder /// @@ -349,8 +354,9 @@ impl ClientRequest { self } - /// This method calls provided closure with builder reference if - /// value is `true`. + /// This method calls provided closure with builder reference if value is `true`. + #[doc(hidden)] + #[deprecated = "Use an if statement."] pub fn if_true(self, value: bool, f: F) -> Self where F: FnOnce(ClientRequest) -> ClientRequest, @@ -362,8 +368,9 @@ impl ClientRequest { } } - /// This method calls provided closure with builder reference if - /// value is `Some`. + /// This method calls provided closure with builder reference if value is `Some`. + #[doc(hidden)] + #[deprecated = "Use an if-let construction."] pub fn if_some(self, value: Option, f: F) -> Self where F: FnOnce(T, ClientRequest) -> ClientRequest, @@ -596,20 +603,27 @@ mod tests { #[actix_rt::test] async fn test_basics() { - let mut req = Client::new() + let req = Client::new() .put("/") .version(Version::HTTP_2) .set(header::Date(SystemTime::now().into())) .content_type("plain/text") - .if_true(true, |req| req.header(header::SERVER, "awc")) - .if_true(false, |req| req.header(header::EXPECT, "awc")) - .if_some(Some("server"), |val, req| { - req.header(header::USER_AGENT, val) - }) - .if_some(Option::<&str>::None, |_, req| { - req.header(header::ALLOW, "1") - }) - .content_length(100); + .header(header::SERVER, "awc"); + + let req = if let Some(val) = Some("server") { + req.header(header::USER_AGENT, val) + } else { + req + }; + + let req = if let Some(_val) = Option::<&str>::None { + req.header(header::ALLOW, "1") + } else { + req + }; + + let mut req = req.content_length(100); + assert!(req.headers().contains_key(header::CONTENT_TYPE)); assert!(req.headers().contains_key(header::DATE)); assert!(req.headers().contains_key(header::SERVER)); @@ -617,6 +631,7 @@ mod tests { assert!(!req.headers().contains_key(header::ALLOW)); assert!(!req.headers().contains_key(header::EXPECT)); assert_eq!(req.head.version, Version::HTTP_2); + let _ = req.headers_mut(); let _ = req.send_body(""); } diff --git a/awc/src/ws.rs b/awc/src/ws.rs index 57e80bd46..dd43d08b3 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -1,6 +1,6 @@ //! Websockets client //! -//! Type definitions required to use [`awc::Client`](../struct.Client.html) as a WebSocket client. +//! Type definitions required to use [`awc::Client`](super::Client) as a WebSocket client. //! //! # Example //! @@ -70,9 +70,14 @@ impl WebsocketsRequest { >::Error: Into, { let mut err = None; - let mut head = RequestHead::default(); - head.method = Method::GET; - head.version = Version::HTTP_11; + + #[allow(clippy::field_reassign_with_default)] + let mut head = { + let mut head = RequestHead::default(); + head.method = Method::GET; + head.version = Version::HTTP_11; + head + }; match Uri::try_from(uri) { Ok(uri) => head.uri = uri, diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index a9552d0d5..0024c6652 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -480,6 +480,7 @@ async fn test_client_gzip_encoding_large_random() { let data = rand::thread_rng() .sample_iter(&rand::distributions::Alphanumeric) .take(100_000) + .map(char::from) .collect::(); let srv = test::start(|| { @@ -529,6 +530,7 @@ async fn test_client_brotli_encoding_large_random() { let data = rand::thread_rng() .sample_iter(&rand::distributions::Alphanumeric) .take(70_000) + .map(char::from) .collect::(); let srv = test::start(|| { diff --git a/codecov.yml b/codecov.yml index 90cdfab47..e6bc40203 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,4 +1,15 @@ -ignore: # ignore codecoverage on following paths +comment: false + +coverage: + status: + project: + default: + threshold: 10% # make CI green + patch: + default: + threshold: 10% # make CI green + +ignore: # ignore code coverage on following paths - "**/tests" - "test-server" - "**/benches" diff --git a/docs/graphs/web-focus.dot b/docs/graphs/web-focus.dot index 7abd51268..17228fe62 100644 --- a/docs/graphs/web-focus.dot +++ b/docs/graphs/web-focus.dot @@ -2,29 +2,31 @@ digraph { subgraph cluster_web { label="actix/actix-web" "awc" - "actix-web" - "actix-files" - "actix-http" - "actix-multipart" - "actix-web-actors" - "actix-web-codegen" + "web" + "files" + "http" + "multipart" + "web-actors" + "codegen" + "http-test" } - "actix-web" -> { "actix-codec" "actix-service" "actix-utils" "actix-router" "actix-rt" "actix-server" "actix-testing" "actix-macros" "actix-threadpool" "actix-tls" "actix-web-codegen" "actix-http" "awc" } - "awc" -> { "actix-codec" "actix-service" "actix-http" "actix-rt" } - "actix-web-actors" -> { "actix" "actix-web" "actix-http" "actix-codec" } - "actix-multipart" -> { "actix-web" "actix-service" "actix-utils" } - "actix-http" -> { "actix-service" "actix-codec" "actix-connect" "actix-utils" "actix-rt" "actix-threadpool" } - "actix-http" -> { "actix" "actix-tls" }[color=blue] // optional - "actix-files" -> { "actix-web" "actix-http" } + "web" -> { "codec" "service" "utils" "router" "rt" "server" "testing" "macros" "threadpool" "tls" "codegen" "http" "awc" } + "awc" -> { "codec" "service" "http" "rt" } + "web-actors" -> { "actix" "web" "http" "codec" } + "multipart" -> { "web" "service" "utils" } + "http" -> { "service" "codec" "connect" "utils" "rt" "threadpool" } + "http" -> { "actix" "tls" }[color=blue] // optional + "files" -> { "web" } + "http-test" -> { "service" "codec" "connect" "utils" "rt" "server" "testing" "awc" } // net - "actix-utils" -> { "actix-service" "actix-rt" "actix-codec" } - "actix-tracing" -> { "actix-service" } - "actix-tls" -> { "actix-service" "actix-codec" "actix-utils" } - "actix-testing" -> { "actix-rt" "actix-macros" "actix-server" "actix-service" } - "actix-server" -> { "actix-service" "actix-rt" "actix-codec" "actix-utils" } - "actix-rt" -> { "actix-macros" "actix-threadpool" } - "actix-connect" -> { "actix-service" "actix-codec" "actix-utils" "actix-rt" } + "utils" -> { "service" "rt" "codec" } + "tracing" -> { "service" } + "tls" -> { "service" "codec" "utils" } + "testing" -> { "rt" "macros" "server" "service" } + "server" -> { "service" "rt" "codec" "utils" } + "rt" -> { "macros" "threadpool" } + "connect" -> { "service" "codec" "utils" "rt" } } diff --git a/docs/graphs/web-only.dot b/docs/graphs/web-only.dot index 6e41fdc27..9e1bb2805 100644 --- a/docs/graphs/web-only.dot +++ b/docs/graphs/web-only.dot @@ -8,12 +8,14 @@ digraph { "actix-multipart" "actix-web-actors" "actix-web-codegen" + "actix-http-test" } - "actix-web" -> { "actix-web-codegen" "actix-http" "awc" } + "actix-web" -> { "actix-web-codegen" "actix-http" "awc" } "awc" -> { "actix-http" } "actix-web-actors" -> { "actix" "actix-web" "actix-http" } "actix-multipart" -> { "actix-web" } "actix-http" -> { "actix" }[color=blue] // optional - "actix-files" -> { "actix-web" "actix-http" } + "actix-files" -> { "actix-web" } + "actix-http-test" -> { "awc" } } diff --git a/examples/on_connect.rs b/examples/on_connect.rs new file mode 100644 index 000000000..bdad7e67e --- /dev/null +++ b/examples/on_connect.rs @@ -0,0 +1,51 @@ +//! This example shows how to use `actix_web::HttpServer::on_connect` to access a lower-level socket +//! properties and pass them to a handler through request-local data. +//! +//! For an example of extracting a client TLS certificate, see: +//! + +use std::{any::Any, env, io, net::SocketAddr}; + +use actix_web::{dev::Extensions, rt::net::TcpStream, web, App, HttpServer}; + +#[derive(Debug, Clone)] +struct ConnectionInfo { + bind: SocketAddr, + peer: SocketAddr, + ttl: Option, +} + +async fn route_whoami(conn_info: web::ReqData) -> String { + format!( + "Here is some info about your connection:\n\n{:#?}", + conn_info + ) +} + +fn get_conn_info(connection: &dyn Any, data: &mut Extensions) { + if let Some(sock) = connection.downcast_ref::() { + data.insert(ConnectionInfo { + bind: sock.local_addr().unwrap(), + peer: sock.peer_addr().unwrap(), + ttl: sock.ttl().ok(), + }); + } else { + unreachable!("connection should only be plaintext since no TLS is set up"); + } +} + +#[actix_web::main] +async fn main() -> io::Result<()> { + if env::var("RUST_LOG").is_err() { + env::set_var("RUST_LOG", "info"); + } + + env_logger::init(); + + HttpServer::new(|| App::new().default_service(web::to(route_whoami))) + .on_connect(get_conn_info) + .bind(("127.0.0.1", 8080))? + .workers(1) + .run() + .await +} diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index a50908ca3..000000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -1.42.0 diff --git a/src/app.rs b/src/app.rs index fdedb0a75..8dd86f7ec 100644 --- a/src/app.rs +++ b/src/app.rs @@ -183,6 +183,7 @@ where self.data.extend(cfg.data); self.services.extend(cfg.services); self.external.extend(cfg.external); + self.extensions.extend(cfg.extensions); self } @@ -459,8 +460,8 @@ where { fn into_factory(self) -> AppInit { AppInit { - data: Rc::new(self.data), - data_factories: Rc::new(self.data_factories), + data: self.data.into_boxed_slice().into(), + data_factories: self.data_factories.into_boxed_slice().into(), endpoint: self.endpoint, services: Rc::new(RefCell::new(self.services)), external: RefCell::new(self.external), diff --git a/src/app_service.rs b/src/app_service.rs index 98d8c8a8d..e5f8dd9cf 100644 --- a/src/app_service.rs +++ b/src/app_service.rs @@ -39,8 +39,8 @@ where { pub(crate) endpoint: T, pub(crate) extensions: RefCell>, - pub(crate) data: Rc>>, - pub(crate) data_factories: Rc>, + pub(crate) data: Rc<[Box]>, + pub(crate) data_factories: Rc<[FnDataFactory]>, pub(crate) services: Rc>>>, pub(crate) default: Option>, pub(crate) factory_ref: Rc>>, @@ -88,15 +88,15 @@ where // complete pipeline creation *self.factory_ref.borrow_mut() = Some(AppRoutingFactory { default, - services: Rc::new( - services - .into_iter() - .map(|(mut rdef, srv, guards, nested)| { - rmap.add(&mut rdef, nested); - (rdef, srv, RefCell::new(guards)) - }) - .collect(), - ), + services: services + .into_iter() + .map(|(mut rdef, srv, guards, nested)| { + rmap.add(&mut rdef, nested); + (rdef, srv, RefCell::new(guards)) + }) + .collect::>() + .into_boxed_slice() + .into(), }); // external resources @@ -147,7 +147,7 @@ where rmap: Rc, config: AppConfig, - data: Rc>>, + data: Rc<[Box]>, extensions: Option, _t: PhantomData, @@ -273,7 +273,7 @@ where } pub struct AppRoutingFactory { - services: Rc>)>>, + services: Rc<[(ResourceDef, HttpNewService, RefCell>)]>, default: Rc, } diff --git a/src/config.rs b/src/config.rs index 0f49288ec..01959daa1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -31,7 +31,7 @@ pub struct AppService { Option, Option>, )>, - service_data: Rc>>, + service_data: Rc<[Box]>, } impl AppService { @@ -39,7 +39,7 @@ impl AppService { pub(crate) fn new( config: AppConfig, default: Rc, - service_data: Rc>>, + service_data: Rc<[Box]>, ) -> Self { AppService { config, @@ -141,7 +141,7 @@ impl AppConfig { /// Server host name. /// /// Host name is used by application router as a hostname for url generation. - /// Check [ConnectionInfo](./struct.ConnectionInfo.html#method.host) + /// Check [ConnectionInfo](super::dev::ConnectionInfo::host()) /// documentation for more information. /// /// By default host name is set to a "localhost" value. @@ -178,6 +178,7 @@ pub struct ServiceConfig { pub(crate) services: Vec>, pub(crate) data: Vec>, pub(crate) external: Vec, + pub(crate) extensions: Extensions, } impl ServiceConfig { @@ -186,6 +187,7 @@ impl ServiceConfig { services: Vec::new(), data: Vec::new(), external: Vec::new(), + extensions: Extensions::new(), } } @@ -198,6 +200,14 @@ impl ServiceConfig { self } + /// Set arbitrary data item. + /// + /// This is same as `App::data()` method. + pub fn app_data(&mut self, ext: U) -> &mut Self { + self.extensions.insert(ext); + self + } + /// Configure route for a specific path. /// /// This is same as `App::route()` method. @@ -254,13 +264,16 @@ mod tests { async fn test_data() { let cfg = |cfg: &mut ServiceConfig| { cfg.data(10usize); + cfg.app_data(15u8); }; - let mut srv = - init_service(App::new().configure(cfg).service( - web::resource("/").to(|_: web::Data| HttpResponse::Ok()), - )) - .await; + let mut srv = init_service(App::new().configure(cfg).service( + web::resource("/").to(|_: web::Data, req: HttpRequest| { + assert_eq!(*req.app_data::().unwrap(), 15u8); + HttpResponse::Ok() + }), + )) + .await; let req = TestRequest::default().to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), StatusCode::OK); diff --git a/src/data.rs b/src/data.rs index 6405fd901..19c258ff0 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,3 +1,4 @@ +use std::any::type_name; use std::ops::Deref; use std::sync::Arc; @@ -19,25 +20,20 @@ pub(crate) type FnDataFactory = /// Application data. /// -/// Application data is an arbitrary data attached to the app. -/// Application data is available to all routes and could be added -/// during application configuration process -/// with `App::data()` method. +/// Application level data is a piece of arbitrary data attached to the app, scope, or resource. +/// Application data is available to all routes and can be added during the application +/// configuration process via `App::data()`. /// -/// Application data could be accessed by using `Data` -/// extractor where `T` is data type. +/// Application data can be accessed by using `Data` extractor where `T` is data type. /// -/// **Note**: http server accepts an application factory rather than -/// an application instance. Http server constructs an application -/// instance for each thread, thus application data must be constructed -/// multiple times. If you want to share data between different -/// threads, a shareable object should be used, e.g. `Send + Sync`. Application -/// data does not need to be `Send` or `Sync`. Internally `Data` type -/// uses `Arc`. if your data implements `Send` + `Sync` traits you can -/// use `web::Data::new()` and avoid double `Arc`. +/// **Note**: http server accepts an application factory rather than an application instance. HTTP +/// server constructs an application instance for each thread, thus application data must be +/// constructed multiple times. If you want to share data between different threads, a shareable +/// object should be used, e.g. `Send + Sync`. Application data does not need to be `Send` +/// or `Sync`. Internally `Data` uses `Arc`. /// -/// If route data is not set for a handler, using `Data` extractor would -/// cause *Internal Server Error* response. +/// If route data is not set for a handler, using `Data` extractor would cause *Internal +/// Server Error* response. /// /// ```rust /// use std::sync::Mutex; @@ -47,7 +43,7 @@ pub(crate) type FnDataFactory = /// counter: usize, /// } /// -/// /// Use `Data` extractor to access data in handler. +/// /// Use the `Data` extractor to access data in a handler. /// async fn index(data: web::Data>) -> impl Responder { /// let mut data = data.lock().unwrap(); /// data.counter += 1; @@ -70,10 +66,6 @@ pub struct Data(Arc); impl Data { /// Create new `Data` instance. - /// - /// Internally `Data` type uses `Arc`. if your data implements - /// `Send` + `Sync` traits you can use `web::Data::new()` and - /// avoid double `Arc`. pub fn new(state: T) -> Data { Data(Arc::new(state)) } @@ -121,8 +113,9 @@ impl FromRequest for Data { } else { log::debug!( "Failed to construct App-level Data extractor. \ - Request path: {:?}", - req.path() + Request path: {:?} (type: {})", + req.path(), + type_name::(), ); err(ErrorInternalServerError( "App data is not configured, to configure use App::data()", diff --git a/src/error.rs b/src/error.rs index 659ba05fd..60af8fa11 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,5 @@ //! Error and Result module + pub use actix_http::error::*; use derive_more::{Display, From}; use serde_json::error::Error as JsonError; diff --git a/src/extract.rs b/src/extract.rs index df9c34cb3..5916b1bc5 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -4,7 +4,8 @@ use std::pin::Pin; use std::task::{Context, Poll}; use actix_http::error::Error; -use futures_util::future::{ok, FutureExt, LocalBoxFuture, Ready}; +use futures_util::future::{ready, Ready}; +use futures_util::ready; use crate::dev::Payload; use crate::request::HttpRequest; @@ -95,21 +96,41 @@ where T: FromRequest, T::Future: 'static, { - type Config = T::Config; type Error = Error; - type Future = LocalBoxFuture<'static, Result, Error>>; + type Future = FromRequestOptFuture; + type Config = T::Config; #[inline] fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { - T::from_request(req, payload) - .then(|r| match r { - Ok(v) => ok(Some(v)), - Err(e) => { - log::debug!("Error for Option extractor: {}", e.into()); - ok(None) - } - }) - .boxed_local() + FromRequestOptFuture { + fut: T::from_request(req, payload), + } + } +} + +#[pin_project::pin_project] +pub struct FromRequestOptFuture { + #[pin] + fut: Fut, +} + +impl Future for FromRequestOptFuture +where + Fut: Future>, + E: Into, +{ + type Output = Result, Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.project(); + let res = ready!(this.fut.poll(cx)); + match res { + Ok(t) => Poll::Ready(Ok(Some(t))), + Err(e) => { + log::debug!("Error for Option extractor: {}", e.into()); + Poll::Ready(Ok(None)) + } + } } } @@ -165,29 +186,45 @@ where T::Error: 'static, T::Future: 'static, { - type Config = T::Config; type Error = Error; - type Future = LocalBoxFuture<'static, Result, Error>>; + type Future = FromRequestResFuture; + type Config = T::Config; #[inline] fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { - T::from_request(req, payload) - .then(|res| match res { - Ok(v) => ok(Ok(v)), - Err(e) => ok(Err(e)), - }) - .boxed_local() + FromRequestResFuture { + fut: T::from_request(req, payload), + } + } +} + +#[pin_project::pin_project] +pub struct FromRequestResFuture { + #[pin] + fut: Fut, +} + +impl Future for FromRequestResFuture +where + Fut: Future>, +{ + type Output = Result, Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.project(); + let res = ready!(this.fut.poll(cx)); + Poll::Ready(Ok(res)) } } #[doc(hidden)] impl FromRequest for () { - type Config = (); type Error = Error; type Future = Ready>; + type Config = (); fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future { - ok(()) + ready(Ok(())) } } diff --git a/src/handler.rs b/src/handler.rs index 669512ab3..3d0a2382e 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,4 +1,3 @@ -use std::convert::Infallible; use std::future::Future; use std::marker::PhantomData; use std::pin::Pin; @@ -6,7 +5,7 @@ use std::task::{Context, Poll}; use actix_http::{Error, Response}; use actix_service::{Service, ServiceFactory}; -use futures_util::future::{ok, Ready}; +use futures_util::future::{ready, Ready}; use futures_util::ready; use pin_project::pin_project; @@ -36,9 +35,11 @@ where } #[doc(hidden)] +/// Extract arguments from request, run factory function and make response. pub struct Handler where F: Factory, + T: FromRequest, R: Future, O: Responder, { @@ -49,6 +50,7 @@ where impl Handler where F: Factory, + T: FromRequest, R: Future, O: Responder, { @@ -63,6 +65,7 @@ where impl Clone for Handler where F: Factory, + T: FromRequest, R: Future, O: Responder, { @@ -74,188 +77,103 @@ where } } -impl Service for Handler +impl ServiceFactory for Handler where F: Factory, + T: FromRequest, R: Future, O: Responder, { - type Request = (T, HttpRequest); + type Request = ServiceRequest; type Response = ServiceResponse; - type Error = Infallible; - type Future = HandlerServiceResponse; + type Error = Error; + type Config = (); + type Service = Self; + type InitError = (); + type Future = Ready>; + + fn new_service(&self, _: ()) -> Self::Future { + ready(Ok(self.clone())) + } +} + +// Handler is both it's ServiceFactory and Service Type. +impl Service for Handler +where + F: Factory, + T: FromRequest, + R: Future, + O: Responder, +{ + type Request = ServiceRequest; + type Response = ServiceResponse; + type Error = Error; + type Future = HandlerServiceFuture; fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } - fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future { - HandlerServiceResponse { - fut: self.hnd.call(param), - fut2: None, - req: Some(req), - } + fn call(&mut self, req: Self::Request) -> Self::Future { + let (req, mut payload) = req.into_parts(); + let fut = T::from_request(&req, &mut payload); + HandlerServiceFuture::Extract(fut, Some(req), self.hnd.clone()) } } #[doc(hidden)] -#[pin_project] -pub struct HandlerServiceResponse +#[pin_project(project = HandlerProj)] +pub enum HandlerServiceFuture where - T: Future, - R: Responder, + F: Factory, + T: FromRequest, + R: Future, + O: Responder, { - #[pin] - fut: T, - #[pin] - fut2: Option, - req: Option, + Extract(#[pin] T::Future, Option, F), + Handle(#[pin] R, Option), + Respond(#[pin] O::Future, Option), } -impl Future for HandlerServiceResponse +impl Future for HandlerServiceFuture where - T: Future, - R: Responder, + F: Factory, + T: FromRequest, + R: Future, + O: Responder, { - type Output = Result; + // Error type in this future is a placeholder type. + // all instances of error must be converted to ServiceResponse and return in Ok. + type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.as_mut().project(); - - if let Some(fut) = this.fut2.as_pin_mut() { - return match fut.poll(cx) { - Poll::Ready(Ok(res)) => { - Poll::Ready(Ok(ServiceResponse::new(this.req.take().unwrap(), res))) + loop { + match self.as_mut().project() { + HandlerProj::Extract(fut, req, handle) => { + match ready!(fut.poll(cx)) { + Ok(item) => { + let fut = handle.call(item); + let state = HandlerServiceFuture::Handle(fut, req.take()); + self.as_mut().set(state); + } + Err(e) => { + let res: Response = e.into().into(); + let req = req.take().unwrap(); + return Poll::Ready(Ok(ServiceResponse::new(req, res))); + } + }; } - Poll::Pending => Poll::Pending, - Poll::Ready(Err(e)) => { - let res: Response = e.into().into(); - Poll::Ready(Ok(ServiceResponse::new(this.req.take().unwrap(), res))) + HandlerProj::Handle(fut, req) => { + let res = ready!(fut.poll(cx)); + let fut = res.respond_to(req.as_ref().unwrap()); + let state = HandlerServiceFuture::Respond(fut, req.take()); + self.as_mut().set(state); + } + HandlerProj::Respond(fut, req) => { + let res = ready!(fut.poll(cx)).unwrap_or_else(|e| e.into().into()); + let req = req.take().unwrap(); + return Poll::Ready(Ok(ServiceResponse::new(req, res))); } - }; - } - - match this.fut.poll(cx) { - Poll::Ready(res) => { - let fut = res.respond_to(this.req.as_ref().unwrap()); - self.as_mut().project().fut2.set(Some(fut)); - self.poll(cx) - } - Poll::Pending => Poll::Pending, - } - } -} - -/// Extract arguments from request -pub struct Extract { - service: S, - _t: PhantomData, -} - -impl Extract { - pub fn new(service: S) -> Self { - Extract { - service, - _t: PhantomData, - } - } -} - -impl ServiceFactory for Extract -where - S: Service< - Request = (T, HttpRequest), - Response = ServiceResponse, - Error = Infallible, - > + Clone, -{ - type Config = (); - type Request = ServiceRequest; - type Response = ServiceResponse; - type Error = (Error, ServiceRequest); - type InitError = (); - type Service = ExtractService; - type Future = Ready>; - - fn new_service(&self, _: ()) -> Self::Future { - ok(ExtractService { - _t: PhantomData, - service: self.service.clone(), - }) - } -} - -pub struct ExtractService { - service: S, - _t: PhantomData, -} - -impl Service for ExtractService -where - S: Service< - Request = (T, HttpRequest), - Response = ServiceResponse, - Error = Infallible, - > + Clone, -{ - type Request = ServiceRequest; - type Response = ServiceResponse; - type Error = (Error, ServiceRequest); - type Future = ExtractResponse; - - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, req: ServiceRequest) -> Self::Future { - let (req, mut payload) = req.into_parts(); - let fut = T::from_request(&req, &mut payload); - - ExtractResponse { - fut, - req, - fut_s: None, - service: self.service.clone(), - } - } -} - -#[pin_project] -pub struct ExtractResponse { - req: HttpRequest, - service: S, - #[pin] - fut: T::Future, - #[pin] - fut_s: Option, -} - -impl Future for ExtractResponse -where - S: Service< - Request = (T, HttpRequest), - Response = ServiceResponse, - Error = Infallible, - >, -{ - type Output = Result; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.as_mut().project(); - - if let Some(fut) = this.fut_s.as_pin_mut() { - return fut.poll(cx).map_err(|_| panic!()); - } - - match ready!(this.fut.poll(cx)) { - Err(e) => { - let req = ServiceRequest::new(this.req.clone()); - Poll::Ready(Err((e.into(), req))) - } - Ok(item) => { - let fut = Some(this.service.call((item, this.req.clone()))); - self.as_mut().project().fut_s.set(fut); - self.poll(cx) } } } diff --git a/src/info.rs b/src/info.rs index 1d9b402a7..975604041 100644 --- a/src/info.rs +++ b/src/info.rs @@ -174,7 +174,7 @@ impl ConnectionInfo { /// Do not use this function for security purposes, unless you can ensure the Forwarded and /// X-Forwarded-For headers cannot be spoofed by the client. If you want the client's socket /// address explicitly, use - /// [`HttpRequest::peer_addr()`](../web/struct.HttpRequest.html#method.peer_addr) instead. + /// [`HttpRequest::peer_addr()`](super::web::HttpRequest::peer_addr()) instead. #[inline] pub fn realip_remote_addr(&self) -> Option<&str> { if let Some(ref r) = self.realip_remote_addr { diff --git a/src/lib.rs b/src/lib.rs index 07f988ab1..669b6125a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -//! Actix web is a powerful, pragmatic, and extremely fast web framework for Rust. +//! Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust. //! //! ## Example //! @@ -29,16 +29,16 @@ //! //! To get started navigating the API docs, you may consider looking at the following pages first: //! -//! * [App](struct.App.html): This struct represents an Actix web application and is used to +//! * [App]: This struct represents an Actix web application and is used to //! configure routes and other common application settings. //! -//! * [HttpServer](struct.HttpServer.html): This struct represents an HTTP server instance and is +//! * [HttpServer]: This struct represents an HTTP server instance and is //! used to instantiate and configure servers. //! -//! * [web](web/index.html): This module provides essential types for route registration as well as +//! * [web]: This module provides essential types for route registration as well as //! common utilities for request handlers. //! -//! * [HttpRequest](struct.HttpRequest.html) and [HttpResponse](struct.HttpResponse.html): These +//! * [HttpRequest] and [HttpResponse]: These //! structs represent HTTP requests and responses and expose methods for creating, inspecting, //! and otherwise utilizing them. //! @@ -67,7 +67,6 @@ #![deny(rust_2018_idioms)] #![allow(clippy::needless_doctest_main, clippy::type_complexity)] -#![allow(clippy::rc_buffer)] // FXIME: We should take a closer look for the warnings at some point. #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")] @@ -82,6 +81,7 @@ mod handler; mod info; pub mod middleware; mod request; +mod request_data; mod resource; mod responder; mod rmap; @@ -102,10 +102,11 @@ pub use crate::app::App; pub use crate::extract::FromRequest; pub use crate::request::HttpRequest; pub use crate::resource::Resource; -pub use crate::responder::{Either, Responder}; +pub use crate::responder::Responder; pub use crate::route::Route; pub use crate::scope::Scope; pub use crate::server::HttpServer; +pub use crate::types::{Either, EitherExtractError}; pub mod dev { //! The `actix-web` prelude for library developers diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index fe3ba841c..7575d7455 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -192,10 +192,7 @@ impl AcceptEncoding { }; let quality = match parts.len() { 1 => encoding.quality(), - _ => match f64::from_str(parts[1]) { - Ok(q) => q, - Err(_) => 0.0, - }, + _ => f64::from_str(parts[1]).unwrap_or(0.0), }; Some(AcceptEncoding { encoding, quality }) } diff --git a/src/middleware/condition.rs b/src/middleware/condition.rs index ab1c69746..9061c7458 100644 --- a/src/middleware/condition.rs +++ b/src/middleware/condition.rs @@ -105,6 +105,7 @@ mod tests { use crate::test::{self, TestRequest}; use crate::HttpResponse; + #[allow(clippy::unnecessary_wraps)] fn render_500(mut res: ServiceResponse) -> Result> { res.response_mut() .headers_mut() diff --git a/src/middleware/defaultheaders.rs b/src/middleware/defaultheaders.rs index 6d43aba95..a6f1a4336 100644 --- a/src/middleware/defaultheaders.rs +++ b/src/middleware/defaultheaders.rs @@ -1,10 +1,14 @@ //! Middleware for setting default response headers use std::convert::TryFrom; +use std::future::Future; +use std::marker::PhantomData; +use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll}; use actix_service::{Service, Transform}; -use futures_util::future::{ok, FutureExt, LocalBoxFuture, Ready}; +use futures_util::future::{ready, Ready}; +use futures_util::ready; use crate::http::header::{HeaderName, HeaderValue, CONTENT_TYPE}; use crate::http::{Error as HttpError, HeaderMap}; @@ -97,15 +101,15 @@ where type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; - type InitError = (); type Transform = DefaultHeadersMiddleware; + type InitError = (); type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { - ok(DefaultHeadersMiddleware { + ready(Ok(DefaultHeadersMiddleware { service, inner: self.inner.clone(), - }) + })) } } @@ -122,36 +126,56 @@ where type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; - type Future = LocalBoxFuture<'static, Result>; + type Future = DefaultHeaderFuture; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.service.poll_ready(cx) } - #[allow(clippy::borrow_interior_mutable_const)] fn call(&mut self, req: ServiceRequest) -> Self::Future { let inner = self.inner.clone(); let fut = self.service.call(req); - async move { - let mut res = fut.await?; - - // set response headers - for (key, value) in inner.headers.iter() { - if !res.headers().contains_key(key) { - res.headers_mut().insert(key.clone(), value.clone()); - } - } - // default content-type - if inner.ct && !res.headers().contains_key(&CONTENT_TYPE) { - res.headers_mut().insert( - CONTENT_TYPE, - HeaderValue::from_static("application/octet-stream"), - ); - } - Ok(res) + DefaultHeaderFuture { + fut, + inner, + _body: PhantomData, } - .boxed_local() + } +} + +#[pin_project::pin_project] +pub struct DefaultHeaderFuture { + #[pin] + fut: S::Future, + inner: Rc, + _body: PhantomData, +} + +impl Future for DefaultHeaderFuture +where + S: Service, Error = Error>, +{ + type Output = ::Output; + + #[allow(clippy::borrow_interior_mutable_const)] + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.project(); + let mut res = ready!(this.fut.poll(cx))?; + // set response headers + for (key, value) in this.inner.headers.iter() { + if !res.headers().contains_key(key) { + res.headers_mut().insert(key.clone(), value.clone()); + } + } + // default content-type + if this.inner.ct && !res.headers().contains_key(&CONTENT_TYPE) { + res.headers_mut().insert( + CONTENT_TYPE, + HeaderValue::from_static("application/octet-stream"), + ); + } + Poll::Ready(Ok(res)) } } diff --git a/src/middleware/errhandlers.rs b/src/middleware/errhandlers.rs index 93a5d3f22..c0cb9594e 100644 --- a/src/middleware/errhandlers.rs +++ b/src/middleware/errhandlers.rs @@ -154,6 +154,7 @@ mod tests { use crate::test::{self, TestRequest}; use crate::HttpResponse; + #[allow(clippy::unnecessary_wraps)] fn render_500(mut res: ServiceResponse) -> Result> { res.response_mut() .headers_mut() diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index 51d4722d7..563cb6c32 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -13,7 +13,7 @@ use actix_service::{Service, Transform}; use bytes::Bytes; use futures_util::future::{ok, Ready}; use log::debug; -use regex::Regex; +use regex::{Regex, RegexSet}; use time::OffsetDateTime; use crate::dev::{BodySize, MessageBody, ResponseBody}; @@ -34,21 +34,19 @@ use crate::HttpResponse; /// Default `Logger` could be created with `default` method, it uses the /// default format: /// -/// ```ignore -/// %a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T +/// ```plain +/// %a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T /// ``` +/// /// ```rust -/// use actix_web::middleware::Logger; -/// use actix_web::App; +/// use actix_web::{middleware::Logger, App}; /// -/// fn main() { -/// std::env::set_var("RUST_LOG", "actix_web=info"); -/// env_logger::init(); +/// std::env::set_var("RUST_LOG", "actix_web=info"); +/// env_logger::init(); /// -/// let app = App::new() -/// .wrap(Logger::default()) -/// .wrap(Logger::new("%a %{User-Agent}i")); -/// } +/// let app = App::new() +/// .wrap(Logger::default()) +/// .wrap(Logger::new("%a %{User-Agent}i")); /// ``` /// /// ## Format @@ -80,18 +78,20 @@ use crate::HttpResponse; /// /// `%{FOO}e` os.environ['FOO'] /// +/// `%{FOO}xi` [custom request replacement](Logger::custom_request_replace) labelled "FOO" +/// /// # Security /// **\*** It is calculated using -/// [`ConnectionInfo::realip_remote_addr()`](../dev/struct.ConnectionInfo.html#method.realip_remote_addr) +/// [`ConnectionInfo::realip_remote_addr()`](crate::dev::ConnectionInfo::realip_remote_addr()) /// /// If you use this value ensure that all requests come from trusted hosts, since it is trivial /// for the remote client to simulate being another client. -/// pub struct Logger(Rc); struct Inner { format: Format, exclude: HashSet, + exclude_regex: RegexSet, } impl Logger { @@ -100,6 +100,7 @@ impl Logger { Logger(Rc::new(Inner { format: Format::new(format), exclude: HashSet::new(), + exclude_regex: RegexSet::empty(), })) } @@ -111,18 +112,69 @@ impl Logger { .insert(path.into()); self } + + /// Ignore and do not log access info for paths that match regex + pub fn exclude_regex>(mut self, path: T) -> Self { + let inner = Rc::get_mut(&mut self.0).unwrap(); + let mut patterns = inner.exclude_regex.patterns().to_vec(); + patterns.push(path.into()); + let regex_set = RegexSet::new(patterns).unwrap(); + inner.exclude_regex = regex_set; + self + } + + /// Register a function that receives a ServiceRequest and returns a String for use in the + /// log line. The label passed as the first argument should match a replacement substring in + /// the logger format like `%{label}xi`. + /// + /// It is convention to print "-" to indicate no output instead of an empty string. + /// + /// # Example + /// ```rust + /// # use actix_web::{http::HeaderValue, middleware::Logger}; + /// # fn parse_jwt_id (_req: Option<&HeaderValue>) -> String { "jwt_uid".to_owned() } + /// Logger::new("example %{JWT_ID}xi") + /// .custom_request_replace("JWT_ID", |req| parse_jwt_id(req.headers().get("Authorization"))); + /// ``` + pub fn custom_request_replace( + mut self, + label: &str, + f: impl Fn(&ServiceRequest) -> String + 'static, + ) -> Self { + let inner = Rc::get_mut(&mut self.0).unwrap(); + + let ft = inner.format.0.iter_mut().find(|ft| { + matches!(ft, FormatText::CustomRequest(unit_label, _) if label == unit_label) + }); + + if let Some(FormatText::CustomRequest(_, request_fn)) = ft { + // replace into None or previously registered fn using same label + request_fn.replace(CustomRequestFn { + inner_fn: Rc::new(f), + }); + } else { + // non-printed request replacement function diagnostic + debug!( + "Attempted to register custom request logging function for nonexistent label: {}", + label + ); + } + + self + } } impl Default for Logger { /// Create `Logger` middleware with format: /// - /// ```ignore + /// ```plain /// %a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T /// ``` fn default() -> Logger { Logger(Rc::new(Inner { format: Format::default(), exclude: HashSet::new(), + exclude_regex: RegexSet::empty(), })) } } @@ -140,6 +192,17 @@ where type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { + for unit in &self.0.format.0 { + // missing request replacement function diagnostic + if let FormatText::CustomRequest(label, None) = unit { + debug!( + "No custom request replacement function was registered for label {} in\ + logger format.", + label + ); + } + } + ok(LoggerMiddleware { service, inner: self.0.clone(), @@ -168,7 +231,9 @@ where } fn call(&mut self, req: ServiceRequest) -> Self::Future { - if self.inner.exclude.contains(req.path()) { + if self.inner.exclude.contains(req.path()) + || self.inner.exclude_regex.is_match(req.path()) + { LoggerResponse { fut: self.service.call(req), format: None, @@ -296,7 +361,6 @@ impl MessageBody for StreamLog { /// A formatting style for the `Logger`, consisting of multiple /// `FormatText`s concatenated into one line. #[derive(Clone)] -#[doc(hidden)] struct Format(Vec); impl Default for Format { @@ -312,7 +376,8 @@ impl Format { /// Returns `None` if the format string syntax is incorrect. pub fn new(s: &str) -> Format { log::trace!("Access log format: {}", s); - let fmt = Regex::new(r"%(\{([A-Za-z0-9\-_]+)\}([aioe])|[atPrUsbTD]?)").unwrap(); + let fmt = + Regex::new(r"%(\{([A-Za-z0-9\-_]+)\}([aioe]|xi)|[atPrUsbTD]?)").unwrap(); let mut idx = 0; let mut results = Vec::new(); @@ -340,6 +405,7 @@ impl Format { HeaderName::try_from(key.as_str()).unwrap(), ), "e" => FormatText::EnvironHeader(key.as_str().to_owned()), + "xi" => FormatText::CustomRequest(key.as_str().to_owned(), None), _ => unreachable!(), }) } else { @@ -369,7 +435,9 @@ impl Format { /// A string of text to be logged. This is either one of the data /// fields supported by the `Logger`, or a custom `String`. #[doc(hidden)] +#[non_exhaustive] #[derive(Debug, Clone)] +// TODO: remove pub on next breaking change pub enum FormatText { Str(String), Percent, @@ -385,6 +453,26 @@ pub enum FormatText { RequestHeader(HeaderName), ResponseHeader(HeaderName), EnvironHeader(String), + CustomRequest(String, Option), +} + +// TODO: remove pub on next breaking change +#[doc(hidden)] +#[derive(Clone)] +pub struct CustomRequestFn { + inner_fn: Rc String>, +} + +impl CustomRequestFn { + fn call(&self, req: &ServiceRequest) -> String { + (self.inner_fn)(req) + } +} + +impl fmt::Debug for CustomRequestFn { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("custom_request_fn") + } } impl FormatText { @@ -441,7 +529,7 @@ impl FormatText { } fn render_request(&mut self, now: OffsetDateTime, req: &ServiceRequest) { - match *self { + match &*self { FormatText::RequestLine => { *self = if req.query_string().is_empty() { FormatText::Str(format!( @@ -493,11 +581,20 @@ impl FormatText { }; *self = s; } + FormatText::CustomRequest(_, request_fn) => { + let s = match request_fn { + Some(f) => FormatText::Str(f.call(req)), + None => FormatText::Str("-".to_owned()), + }; + + *self = s; + } _ => (), } } } +/// Converter to get a String from something that writes to a Formatter. pub(crate) struct FormatDisplay<'a>( &'a dyn Fn(&mut Formatter<'_>) -> Result<(), fmt::Error>, ); @@ -515,7 +612,7 @@ mod tests { use super::*; use crate::http::{header, StatusCode}; - use crate::test::TestRequest; + use crate::test::{self, TestRequest}; #[actix_rt::test] async fn test_logger() { @@ -538,6 +635,28 @@ mod tests { let _res = srv.call(req).await; } + #[actix_rt::test] + async fn test_logger_exclude_regex() { + let srv = |req: ServiceRequest| { + ok(req.into_response( + HttpResponse::build(StatusCode::OK) + .header("X-Test", "ttt") + .finish(), + )) + }; + let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test") + .exclude_regex("\\w"); + + let mut srv = logger.new_transform(srv.into_service()).await.unwrap(); + + let req = TestRequest::with_header( + header::USER_AGENT, + header::HeaderValue::from_static("ACTIX-WEB"), + ) + .to_srv_request(); + let _res = srv.call(req).await.unwrap(); + } + #[actix_rt::test] async fn test_url_path() { let mut format = Format::new("%T %U"); @@ -662,4 +781,45 @@ mod tests { println!("{}", s); assert!(s.contains("192.0.2.60")); } + + #[actix_rt::test] + async fn test_custom_closure_log() { + let mut logger = Logger::new("test %{CUSTOM}xi") + .custom_request_replace("CUSTOM", |_req: &ServiceRequest| -> String { + String::from("custom_log") + }); + let mut unit = Rc::get_mut(&mut logger.0).unwrap().format.0[1].clone(); + + let label = match &unit { + FormatText::CustomRequest(label, _) => label, + ft => panic!("expected CustomRequest, found {:?}", ft), + }; + + assert_eq!(label, "CUSTOM"); + + let req = TestRequest::default().to_srv_request(); + let now = OffsetDateTime::now_utc(); + + unit.render_request(now, &req); + + let render = |fmt: &mut Formatter<'_>| unit.render(fmt, 1024, now); + + let log_output = FormatDisplay(&render).to_string(); + assert_eq!(log_output, "custom_log"); + } + + #[actix_rt::test] + async fn test_closure_logger_in_middleware() { + let captured = "custom log replacement"; + + let logger = Logger::new("%{CUSTOM}xi") + .custom_request_replace("CUSTOM", move |_req: &ServiceRequest| -> String { + captured.to_owned() + }); + + let mut srv = logger.new_transform(test::ok_service()).await.unwrap(); + + let req = TestRequest::default().to_srv_request(); + srv.call(req).await.unwrap(); + } } diff --git a/src/middleware/normalize.rs b/src/middleware/normalize.rs index e0ecd90dc..ad9f51079 100644 --- a/src/middleware/normalize.rs +++ b/src/middleware/normalize.rs @@ -1,10 +1,11 @@ -//! `Middleware` to normalize request's URI +//! For middleware documentation, see [`NormalizePath`]. + use std::task::{Context, Poll}; use actix_http::http::{PathAndQuery, Uri}; use actix_service::{Service, Transform}; use bytes::Bytes; -use futures_util::future::{ok, Ready}; +use futures_util::future::{ready, Ready}; use regex::Regex; use crate::service::{ServiceRequest, ServiceResponse}; @@ -17,10 +18,12 @@ pub enum TrailingSlash { /// Always add a trailing slash to the end of the path. /// This will require all routes to end in a trailing slash for them to be accessible. Always, + /// Only merge any present multiple trailing slashes. /// - /// Note: This option provides the best compatibility with the v2 version of this middlware. + /// Note: This option provides the best compatibility with the v2 version of this middleware. MergeOnly, + /// Trim trailing slashes from the end of the path. Trim, } @@ -32,28 +35,53 @@ impl Default for TrailingSlash { } #[derive(Default, Clone, Copy)] -/// `Middleware` to normalize request's URI in place +/// Middleware to normalize a request's path so that routes can be matched less strictly. /// -/// Performs following: -/// -/// - Merges multiple slashes into one. +/// # Normalization Steps +/// - Merges multiple consecutive slashes into one. (For example, `/path//one` always +/// becomes `/path/one`.) /// - Appends a trailing slash if one is not present, removes one if present, or keeps trailing -/// slashes as-is, depending on the supplied `TrailingSlash` variant. +/// slashes as-is, depending on which [`TrailingSlash`] variant is supplied +/// to [`new`](NormalizePath::new()). /// +/// # Default Behavior +/// The default constructor chooses to strip trailing slashes from the end +/// ([`TrailingSlash::Trim`]), the effect is that route definitions should be defined without +/// trailing slashes or else they will be inaccessible. +/// +/// # Example /// ```rust -/// use actix_web::{web, http, middleware, App, HttpResponse}; +/// use actix_web::{web, middleware, App}; /// -/// # fn main() { +/// # #[actix_rt::test] +/// # async fn normalize() { /// let app = App::new() /// .wrap(middleware::NormalizePath::default()) -/// .service( -/// web::resource("/test") -/// .route(web::get().to(|| HttpResponse::Ok())) -/// .route(web::method(http::Method::HEAD).to(|| HttpResponse::MethodNotAllowed())) -/// ); +/// .route("/test", web::get().to(|| async { "test" })) +/// .route("/unmatchable/", web::get().to(|| async { "unmatchable" })); +/// +/// use actix_web::http::StatusCode; +/// use actix_web::test::{call_service, init_service, TestRequest}; +/// +/// let mut app = init_service(app).await; +/// +/// let req = TestRequest::with_uri("/test").to_request(); +/// let res = call_service(&mut app, req).await; +/// assert_eq!(res.status(), StatusCode::OK); +/// +/// let req = TestRequest::with_uri("/test/").to_request(); +/// let res = call_service(&mut app, req).await; +/// assert_eq!(res.status(), StatusCode::OK); +/// +/// let req = TestRequest::with_uri("/unmatchable").to_request(); +/// let res = call_service(&mut app, req).await; +/// assert_eq!(res.status(), StatusCode::NOT_FOUND); +/// +/// let req = TestRequest::with_uri("/unmatchable/").to_request(); +/// let res = call_service(&mut app, req).await; +/// assert_eq!(res.status(), StatusCode::NOT_FOUND); /// # } /// ``` - pub struct NormalizePath(TrailingSlash); impl NormalizePath { @@ -76,11 +104,11 @@ where type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { - ok(NormalizePathNormalization { + ready(Ok(NormalizePathNormalization { service, merge_slash: Regex::new("//+").unwrap(), trailing_slash_behavior: self.0, - }) + })) } } @@ -137,9 +165,9 @@ where // so the change can not be deduced from the length comparison if path != original_path { let mut parts = head.uri.clone().into_parts(); - let pq = parts.path_and_query.as_ref().unwrap(); + let query = parts.path_and_query.as_ref().and_then(|pq| pq.query()); - let path = if let Some(q) = pq.query() { + let path = if let Some(q) = query { Bytes::from(format!("{}?{}", path, q)) } else { Bytes::copy_from_slice(path.as_bytes()) @@ -160,9 +188,11 @@ mod tests { use actix_service::IntoService; use super::*; - use crate::dev::ServiceRequest; - use crate::test::{call_service, init_service, TestRequest}; - use crate::{web, App, HttpResponse}; + use crate::{ + dev::ServiceRequest, + test::{call_service, init_service, TestRequest}, + web, App, HttpResponse, + }; #[actix_rt::test] async fn test_wrap() { @@ -244,7 +274,7 @@ mod tests { } #[actix_rt::test] - async fn keep_trailing_slash_unchange() { + async fn keep_trailing_slash_unchanged() { let mut app = init_service( App::new() .wrap(NormalizePath(TrailingSlash::MergeOnly)) @@ -279,7 +309,7 @@ mod tests { async fn test_in_place_normalization() { let srv = |req: ServiceRequest| { assert_eq!("/v1/something/", req.path()); - ok(req.into_response(HttpResponse::Ok().finish())) + ready(Ok(req.into_response(HttpResponse::Ok().finish()))) }; let mut normalize = NormalizePath::default() @@ -310,7 +340,7 @@ mod tests { let srv = |req: ServiceRequest| { assert_eq!(URI, req.path()); - ok(req.into_response(HttpResponse::Ok().finish())) + ready(Ok(req.into_response(HttpResponse::Ok().finish()))) }; let mut normalize = NormalizePath::default() @@ -324,12 +354,12 @@ mod tests { } #[actix_rt::test] - async fn should_normalize_notrail() { + async fn should_normalize_no_trail() { const URI: &str = "/v1/something"; let srv = |req: ServiceRequest| { assert_eq!(URI.to_string() + "/", req.path()); - ok(req.into_response(HttpResponse::Ok().finish())) + ready(Ok(req.into_response(HttpResponse::Ok().finish()))) }; let mut normalize = NormalizePath::default() diff --git a/src/request.rs b/src/request.rs index a1b42f926..bd4bbbf58 100644 --- a/src/request.rs +++ b/src/request.rs @@ -675,4 +675,40 @@ mod tests { let res = call_service(&mut srv, req).await; assert_eq!(res.status(), StatusCode::OK); } + + #[actix_rt::test] + async fn extract_path_pattern_complex() { + let mut srv = init_service( + App::new() + .service(web::scope("/user").service(web::scope("/{id}").service( + web::resource("").to(move |req: HttpRequest| { + assert_eq!(req.match_pattern(), Some("/user/{id}".to_owned())); + + HttpResponse::Ok().finish() + }), + ))) + .service(web::resource("/").to(move |req: HttpRequest| { + assert_eq!(req.match_pattern(), Some("/".to_owned())); + + HttpResponse::Ok().finish() + })) + .default_service(web::to(move |req: HttpRequest| { + assert!(req.match_pattern().is_none()); + HttpResponse::Ok().finish() + })), + ) + .await; + + let req = TestRequest::get().uri("/user/test").to_request(); + let res = call_service(&mut srv, req).await; + assert_eq!(res.status(), StatusCode::OK); + + let req = TestRequest::get().uri("/").to_request(); + let res = call_service(&mut srv, req).await; + assert_eq!(res.status(), StatusCode::OK); + + let req = TestRequest::get().uri("/not-exist").to_request(); + let res = call_service(&mut srv, req).await; + assert_eq!(res.status(), StatusCode::OK); + } } diff --git a/src/request_data.rs b/src/request_data.rs new file mode 100644 index 000000000..285154884 --- /dev/null +++ b/src/request_data.rs @@ -0,0 +1,175 @@ +use std::{any::type_name, ops::Deref}; + +use actix_http::error::{Error, ErrorInternalServerError}; +use futures_util::future; + +use crate::{dev::Payload, FromRequest, HttpRequest}; + +/// Request-local data extractor. +/// +/// Request-local data is arbitrary data attached to an individual request, usually +/// by middleware. It can be set via `extensions_mut` on [`HttpRequest`][htr_ext_mut] +/// or [`ServiceRequest`][srv_ext_mut]. +/// +/// Unlike app data, request data is dropped when the request has finished processing. This makes it +/// useful as a kind of messaging system between middleware and request handlers. It uses the same +/// types-as-keys storage system as app data. +/// +/// # Mutating Request Data +/// Note that since extractors must output owned data, only types that `impl Clone` can use this +/// extractor. A clone is taken of the required request data and can, therefore, not be directly +/// mutated in-place. To mutate request data, continue to use [`HttpRequest::extensions_mut`] or +/// re-insert the cloned data back into the extensions map. A `DerefMut` impl is intentionally not +/// provided to make this potential foot-gun more obvious. +/// +/// # Example +/// ```rust,no_run +/// # use actix_web::{web, HttpResponse, HttpRequest, Responder}; +/// +/// #[derive(Debug, Clone, PartialEq)] +/// struct FlagFromMiddleware(String); +/// +/// /// Use the `ReqData` extractor to access request data in a handler. +/// async fn handler( +/// req: HttpRequest, +/// opt_flag: Option>, +/// ) -> impl Responder { +/// // use an optional extractor if the middleware is +/// // not guaranteed to add this type of requests data +/// if let Some(flag) = opt_flag { +/// assert_eq!(&flag.into_inner(), req.extensions().get::().unwrap()); +/// } +/// +/// HttpResponse::Ok() +/// } +/// ``` +/// +/// [htr_ext_mut]: crate::HttpRequest::extensions_mut +/// [srv_ext_mut]: crate::dev::ServiceRequest::extensions_mut +#[derive(Debug, Clone)] +pub struct ReqData(T); + +impl ReqData { + /// Consumes the `ReqData`, returning its wrapped data. + pub fn into_inner(self) -> T { + self.0 + } +} + +impl Deref for ReqData { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +impl FromRequest for ReqData { + type Config = (); + type Error = Error; + type Future = future::Ready>; + + fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { + if let Some(st) = req.extensions().get::() { + future::ok(ReqData(st.clone())) + } else { + log::debug!( + "Failed to construct App-level ReqData extractor. \ + Request path: {:?} (type: {})", + req.path(), + type_name::(), + ); + future::err(ErrorInternalServerError( + "Missing expected request extension data", + )) + } + } +} + +#[cfg(test)] +mod tests { + use std::{cell::RefCell, rc::Rc}; + + use futures_util::TryFutureExt as _; + + use super::*; + use crate::{ + dev::Service, + http::{Method, StatusCode}, + test::{init_service, TestRequest}, + web, App, HttpMessage, HttpResponse, + }; + + #[actix_rt::test] + async fn req_data_extractor() { + let mut srv = init_service( + App::new() + .wrap_fn(|req, srv| { + if req.method() == Method::POST { + req.extensions_mut().insert(42u32); + } + + srv.call(req) + }) + .service(web::resource("/test").to( + |req: HttpRequest, data: Option>| { + if req.method() != Method::POST { + assert!(data.is_none()); + } + + if let Some(data) = data { + assert_eq!(*data, 42); + assert_eq!( + Some(data.into_inner()), + req.extensions().get::().copied() + ); + } + + HttpResponse::Ok() + }, + )), + ) + .await; + + let req = TestRequest::get().uri("/test").to_request(); + let resp = srv.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + + let req = TestRequest::post().uri("/test").to_request(); + let resp = srv.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + } + + #[actix_rt::test] + async fn req_data_internal_mutability() { + let mut srv = init_service( + App::new() + .wrap_fn(|req, srv| { + let data_before = Rc::new(RefCell::new(42u32)); + req.extensions_mut().insert(data_before); + + srv.call(req).map_ok(|res| { + { + let ext = res.request().extensions(); + let data_after = ext.get::>>().unwrap(); + assert_eq!(*data_after.borrow(), 53u32); + } + + res + }) + }) + .default_service(web::to(|data: ReqData>>| { + assert_eq!(*data.borrow(), 42); + *data.borrow_mut() += 11; + assert_eq!(*data.borrow(), 53); + + HttpResponse::Ok() + })), + ) + .await; + + let req = TestRequest::get().uri("/test").to_request(); + let resp = srv.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::OK); + } +} diff --git a/src/responder.rs b/src/responder.rs index fc80831b8..d1c22323f 100644 --- a/src/responder.rs +++ b/src/responder.rs @@ -332,82 +332,6 @@ impl Future for CustomResponderFut { } } -/// Combines two different responder types into a single type -/// -/// ```rust -/// use actix_web::{Either, Error, HttpResponse}; -/// -/// type RegisterResult = Either>; -/// -/// fn index() -> RegisterResult { -/// if is_a_variant() { -/// // <- choose left variant -/// Either::A(HttpResponse::BadRequest().body("Bad data")) -/// } else { -/// Either::B( -/// // <- Right variant -/// Ok(HttpResponse::Ok() -/// .content_type("text/html") -/// .body("Hello!")) -/// ) -/// } -/// } -/// # fn is_a_variant() -> bool { true } -/// # fn main() {} -/// ``` -#[derive(Debug, PartialEq)] -pub enum Either { - /// First branch of the type - A(A), - /// Second branch of the type - B(B), -} - -impl Responder for Either -where - A: Responder, - B: Responder, -{ - type Error = Error; - type Future = EitherResponder; - - fn respond_to(self, req: &HttpRequest) -> Self::Future { - match self { - Either::A(a) => EitherResponder::A(a.respond_to(req)), - Either::B(b) => EitherResponder::B(b.respond_to(req)), - } - } -} - -#[pin_project(project = EitherResponderProj)] -pub enum EitherResponder -where - A: Responder, - B: Responder, -{ - A(#[pin] A::Future), - B(#[pin] B::Future), -} - -impl Future for EitherResponder -where - A: Responder, - B: Responder, -{ - type Output = Result; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.project() { - EitherResponderProj::A(fut) => { - Poll::Ready(ready!(fut.poll(cx)).map_err(|e| e.into())) - } - EitherResponderProj::B(fut) => { - Poll::Ready(ready!(fut.poll(cx).map_err(|e| e.into()))) - } - } - } -} - impl Responder for InternalError where T: std::fmt::Debug + std::fmt::Display + 'static, diff --git a/src/rmap.rs b/src/rmap.rs index 05c1f3f15..6827a11b2 100644 --- a/src/rmap.rs +++ b/src/rmap.rs @@ -86,7 +86,7 @@ impl ResourceMap { if let Some(plen) = pattern.is_prefix_match(path) { return rmap.has_resource(&path[plen..]); } - } else if pattern.is_match(path) { + } else if pattern.is_match(path) || pattern.pattern() == "" && path == "/" { return true; } } diff --git a/src/route.rs b/src/route.rs index 129a67332..439ae6c4a 100644 --- a/src/route.rs +++ b/src/route.rs @@ -1,3 +1,5 @@ +#![allow(clippy::rc_buffer)] // inner value is mutated before being shared (`Rc::get_mut`) + use std::future::Future; use std::pin::Pin; use std::rc::Rc; @@ -9,29 +11,29 @@ use futures_util::future::{ready, FutureExt, LocalBoxFuture}; use crate::extract::FromRequest; use crate::guard::{self, Guard}; -use crate::handler::{Extract, Factory, Handler}; +use crate::handler::{Factory, Handler}; use crate::responder::Responder; use crate::service::{ServiceRequest, ServiceResponse}; use crate::HttpResponse; -type BoxedRouteService = Box< +type BoxedRouteService = Box< dyn Service< - Request = Req, - Response = Res, + Request = ServiceRequest, + Response = ServiceResponse, Error = Error, - Future = LocalBoxFuture<'static, Result>, + Future = LocalBoxFuture<'static, Result>, >, >; -type BoxedRouteNewService = Box< +type BoxedRouteNewService = Box< dyn ServiceFactory< Config = (), - Request = Req, - Response = Res, + Request = ServiceRequest, + Response = ServiceResponse, Error = Error, InitError = (), - Service = BoxedRouteService, - Future = LocalBoxFuture<'static, Result, ()>>, + Service = BoxedRouteService, + Future = LocalBoxFuture<'static, Result>, >, >; @@ -40,7 +42,7 @@ type BoxedRouteNewService = Box< /// Route uses builder-like pattern for configuration. /// If handler is not explicitly set, default *404 Not Found* handler is used. pub struct Route { - service: BoxedRouteNewService, + service: BoxedRouteNewService, guards: Rc>>, } @@ -49,9 +51,9 @@ impl Route { #[allow(clippy::new_without_default)] pub fn new() -> Route { Route { - service: Box::new(RouteNewService::new(Extract::new(Handler::new(|| { + service: Box::new(RouteNewService::new(Handler::new(|| { ready(HttpResponse::NotFound()) - })))), + }))), guards: Rc::new(Vec::new()), } } @@ -78,15 +80,8 @@ impl ServiceFactory for Route { } } -type RouteFuture = LocalBoxFuture< - 'static, - Result, ()>, ->; - -#[pin_project::pin_project] pub struct CreateRouteService { - #[pin] - fut: RouteFuture, + fut: LocalBoxFuture<'static, Result>, guards: Rc>>, } @@ -94,9 +89,9 @@ impl Future for CreateRouteService { type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); + let this = self.get_mut(); - match this.fut.poll(cx)? { + match this.fut.as_mut().poll(cx)? { Poll::Ready(service) => Poll::Ready(Ok(RouteService { service, guards: this.guards.clone(), @@ -107,7 +102,7 @@ impl Future for CreateRouteService { } pub struct RouteService { - service: BoxedRouteService, + service: BoxedRouteService, guards: Rc>>, } @@ -133,7 +128,7 @@ impl Service for RouteService { } fn call(&mut self, req: ServiceRequest) -> Self::Future { - self.service.call(req).boxed_local() + self.service.call(req) } } @@ -231,15 +226,14 @@ impl Route { R: Future + 'static, U: Responder + 'static, { - self.service = - Box::new(RouteNewService::new(Extract::new(Handler::new(handler)))); + self.service = Box::new(RouteNewService::new(Handler::new(handler))); self } } struct RouteNewService where - T: ServiceFactory, + T: ServiceFactory, { service: T, } @@ -250,7 +244,7 @@ where Config = (), Request = ServiceRequest, Response = ServiceResponse, - Error = (Error, ServiceRequest), + Error = Error, >, T::Future: 'static, T::Service: 'static, @@ -267,18 +261,18 @@ where Config = (), Request = ServiceRequest, Response = ServiceResponse, - Error = (Error, ServiceRequest), + Error = Error, >, T::Future: 'static, T::Service: 'static, ::Future: 'static, { - type Config = (); type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; + type Config = (); + type Service = BoxedRouteService; type InitError = (); - type Service = BoxedRouteService; type Future = LocalBoxFuture<'static, Result>; fn new_service(&self, _: ()) -> Self::Future { @@ -286,8 +280,7 @@ where .new_service(()) .map(|result| match result { Ok(service) => { - let service: BoxedRouteService<_, _> = - Box::new(RouteServiceWrapper { service }); + let service = Box::new(RouteServiceWrapper { service }) as _; Ok(service) } Err(_) => Err(()), @@ -303,11 +296,7 @@ struct RouteServiceWrapper { impl Service for RouteServiceWrapper where T::Future: 'static, - T: Service< - Request = ServiceRequest, - Response = ServiceResponse, - Error = (Error, ServiceRequest), - >, + T: Service, { type Request = ServiceRequest; type Response = ServiceResponse; @@ -315,27 +304,11 @@ where type Future = LocalBoxFuture<'static, Result>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.service.poll_ready(cx).map_err(|(e, _)| e) + self.service.poll_ready(cx) } fn call(&mut self, req: ServiceRequest) -> Self::Future { - // let mut fut = self.service.call(req); - self.service - .call(req) - .map(|res| match res { - Ok(res) => Ok(res), - Err((err, req)) => Ok(req.error_response(err)), - }) - .boxed_local() - - // match fut.poll() { - // Poll::Ready(Ok(res)) => Either::Left(ok(res)), - // Poll::Ready(Err((e, req))) => Either::Left(ok(req.error_response(e))), - // Poll::Pending => Either::Right(Box::new(fut.then(|res| match res { - // Ok(res) => Ok(res), - // Err((err, req)) => Ok(req.error_response(err)), - // }))), - // } + Box::pin(self.service.call(req)) } } diff --git a/src/scope.rs b/src/scope.rs index 2520fd7ae..681d142be 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -209,6 +209,9 @@ where self.data = Some(data); } + self.data + .get_or_insert_with(Extensions::new) + .extend(cfg.extensions); self } @@ -442,16 +445,17 @@ where *self.factory_ref.borrow_mut() = Some(ScopeFactory { data: self.data.take().map(Rc::new), default: self.default.clone(), - services: Rc::new( - cfg.into_services() - .1 - .into_iter() - .map(|(mut rdef, srv, guards, nested)| { - rmap.add(&mut rdef, nested); - (rdef, srv, RefCell::new(guards)) - }) - .collect(), - ), + services: cfg + .into_services() + .1 + .into_iter() + .map(|(mut rdef, srv, guards, nested)| { + rmap.add(&mut rdef, nested); + (rdef, srv, RefCell::new(guards)) + }) + .collect::>() + .into_boxed_slice() + .into(), }); // get guards @@ -473,7 +477,7 @@ where pub struct ScopeFactory { data: Option>, - services: Rc>)>>, + services: Rc<[(ResourceDef, HttpNewService, RefCell>)]>, default: Rc>>>, } diff --git a/src/server.rs b/src/server.rs index 2b86f7416..be97e8a0d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,8 +1,14 @@ -use std::marker::PhantomData; -use std::sync::{Arc, Mutex}; -use std::{fmt, io, net}; +use std::{ + any::Any, + fmt, io, + marker::PhantomData, + net, + sync::{Arc, Mutex}, +}; -use actix_http::{body::MessageBody, Error, HttpService, KeepAlive, Request, Response}; +use actix_http::{ + body::MessageBody, Error, Extensions, HttpService, KeepAlive, Request, Response, +}; use actix_server::{Server, ServerBuilder}; use actix_service::{map_config, IntoServiceFactory, Service, ServiceFactory}; @@ -64,6 +70,7 @@ where backlog: i32, sockets: Vec, builder: ServerBuilder, + on_connect_fn: Option>, _t: PhantomData<(S, B)>, } @@ -91,6 +98,32 @@ where backlog: 1024, sockets: Vec::new(), builder: ServerBuilder::default(), + on_connect_fn: None, + _t: PhantomData, + } + } + + /// Sets function that will be called once before each connection is handled. + /// It will receive a `&std::any::Any`, which contains underlying connection type and an + /// [Extensions] container so that request-local data can be passed to middleware and handlers. + /// + /// For example: + /// - `actix_tls::openssl::SslStream` when using openssl. + /// - `actix_tls::rustls::TlsStream` when using rustls. + /// - `actix_web::rt::net::TcpStream` when no encryption is used. + /// + /// See `on_connect` example for additional details. + pub fn on_connect(self, f: CB) -> HttpServer + where + CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static, + { + HttpServer { + factory: self.factory, + config: self.config, + backlog: self.backlog, + sockets: self.sockets, + builder: self.builder, + on_connect_fn: Some(Arc::new(f)), _t: PhantomData, } } @@ -180,7 +213,7 @@ where /// Set server host name. /// /// Host name is used by application router as a hostname for url generation. - /// Check [ConnectionInfo](./dev/struct.ConnectionInfo.html#method.host) + /// Check [ConnectionInfo](super::dev::ConnectionInfo::host()) /// documentation for more information. /// /// By default host name is set to a "localhost" value. @@ -240,6 +273,7 @@ where addr, scheme: "http", }); + let on_connect_fn = self.on_connect_fn.clone(); self.builder = self.builder.listen( format!("actix-web-service-{}", addr), @@ -252,11 +286,20 @@ where c.host.clone().unwrap_or_else(|| format!("{}", addr)), ); - HttpService::build() + let svc = HttpService::build() .keep_alive(c.keep_alive) .client_timeout(c.client_timeout) - .local_addr(addr) - .finish(map_config(factory(), move |_| cfg.clone())) + .local_addr(addr); + + let svc = if let Some(handler) = on_connect_fn.clone() { + svc.on_connect_ext(move |io: &_, ext: _| { + (handler)(io as &dyn Any, ext) + }) + } else { + svc + }; + + svc.finish(map_config(factory(), move |_| cfg.clone())) .tcp() }, )?; @@ -289,6 +332,8 @@ where scheme: "https", }); + let on_connect_fn = self.on_connect_fn.clone(); + self.builder = self.builder.listen( format!("actix-web-service-{}", addr), lst, @@ -299,11 +344,21 @@ where addr, c.host.clone().unwrap_or_else(|| format!("{}", addr)), ); - HttpService::build() + + let svc = HttpService::build() .keep_alive(c.keep_alive) .client_timeout(c.client_timeout) - .client_disconnect(c.client_shutdown) - .finish(map_config(factory(), move |_| cfg.clone())) + .client_disconnect(c.client_shutdown); + + let svc = if let Some(handler) = on_connect_fn.clone() { + svc.on_connect_ext(move |io: &_, ext: _| { + (&*handler)(io as &dyn Any, ext) + }) + } else { + svc + }; + + svc.finish(map_config(factory(), move |_| cfg.clone())) .openssl(acceptor.clone()) }, )?; @@ -336,6 +391,8 @@ where scheme: "https", }); + let on_connect_fn = self.on_connect_fn.clone(); + self.builder = self.builder.listen( format!("actix-web-service-{}", addr), lst, @@ -346,11 +403,21 @@ where addr, c.host.clone().unwrap_or_else(|| format!("{}", addr)), ); - HttpService::build() + + let svc = HttpService::build() .keep_alive(c.keep_alive) .client_timeout(c.client_timeout) - .client_disconnect(c.client_shutdown) - .finish(map_config(factory(), move |_| cfg.clone())) + .client_disconnect(c.client_shutdown); + + let svc = if let Some(handler) = on_connect_fn.clone() { + svc.on_connect_ext(move |io: &_, ext: _| { + (handler)(io as &dyn Any, ext) + }) + } else { + svc + }; + + svc.finish(map_config(factory(), move |_| cfg.clone())) .rustls(config.clone()) }, )?; @@ -441,7 +508,7 @@ where } #[cfg(unix)] - /// Start listening for unix domain connections on existing listener. + /// Start listening for unix domain (UDS) connections on existing listener. pub fn listen_uds( mut self, lst: std::os::unix::net::UnixListener, @@ -460,6 +527,7 @@ where }); let addr = format!("actix-web-service-{:?}", lst.local_addr()?); + let on_connect_fn = self.on_connect_fn.clone(); self.builder = self.builder.listen_uds(addr, lst, move || { let c = cfg.lock().unwrap(); @@ -468,11 +536,23 @@ where socket_addr, c.host.clone().unwrap_or_else(|| format!("{}", socket_addr)), ); + pipeline_factory(|io: UnixStream| ok((io, Protocol::Http1, None))).and_then( - HttpService::build() - .keep_alive(c.keep_alive) - .client_timeout(c.client_timeout) - .finish(map_config(factory(), move |_| config.clone())), + { + let svc = HttpService::build() + .keep_alive(c.keep_alive) + .client_timeout(c.client_timeout); + + let svc = if let Some(handler) = on_connect_fn.clone() { + svc.on_connect_ext(move |io: &_, ext: _| { + (&*handler)(io as &dyn Any, ext) + }) + } else { + svc + }; + + svc.finish(map_config(factory(), move |_| config.clone())) + }, ) })?; Ok(self) diff --git a/src/service.rs b/src/service.rs index a861ba38c..189ba5554 100644 --- a/src/service.rs +++ b/src/service.rs @@ -195,13 +195,13 @@ impl ServiceRequest { self.0.match_info() } - /// Counterpart to [`HttpRequest::match_name`](../struct.HttpRequest.html#method.match_name). + /// Counterpart to [`HttpRequest::match_name`](super::HttpRequest::match_name()). #[inline] pub fn match_name(&self) -> Option<&str> { self.0.match_name() } - /// Counterpart to [`HttpRequest::match_pattern`](../struct.HttpRequest.html#method.match_pattern). + /// Counterpart to [`HttpRequest::match_pattern`](super::HttpRequest::match_pattern()). #[inline] pub fn match_pattern(&self) -> Option { self.0.match_pattern() @@ -225,7 +225,7 @@ impl ServiceRequest { self.0.app_config() } - /// Counterpart to [`HttpRequest::app_data`](../struct.HttpRequest.html#method.app_data). + /// Counterpart to [`HttpRequest::app_data`](super::HttpRequest::app_data()). pub fn app_data(&self) -> Option<&T> { for container in (self.0).0.app_data.iter().rev() { if let Some(data) = container.get::() { diff --git a/src/test.rs b/src/test.rs index ee51b71ee..cff6c3e51 100644 --- a/src/test.rs +++ b/src/test.rs @@ -269,8 +269,9 @@ where { let body = read_body(res).await; - serde_json::from_slice(&body) - .unwrap_or_else(|_| panic!("read_response_json failed during deserialization")) + serde_json::from_slice(&body).unwrap_or_else(|e| { + panic!("read_response_json failed during deserialization: {}", e) + }) } pub async fn load_stream(mut stream: S) -> Result diff --git a/src/types/either.rs b/src/types/either.rs new file mode 100644 index 000000000..9f1d81a0b --- /dev/null +++ b/src/types/either.rs @@ -0,0 +1,274 @@ +use std::{ + future::Future, + pin::Pin, + task::{Context, Poll}, +}; + +use actix_http::{Error, Response}; +use bytes::Bytes; +use futures_util::{future::LocalBoxFuture, ready, FutureExt, TryFutureExt}; +use pin_project::pin_project; + +use crate::{dev, request::HttpRequest, FromRequest, Responder}; + +/// Combines two different responder types into a single type +/// +/// ```rust +/// use actix_web::{Either, Error, HttpResponse}; +/// +/// type RegisterResult = Either>; +/// +/// fn index() -> RegisterResult { +/// if is_a_variant() { +/// // <- choose left variant +/// Either::A(HttpResponse::BadRequest().body("Bad data")) +/// } else { +/// Either::B( +/// // <- Right variant +/// Ok(HttpResponse::Ok() +/// .content_type("text/html") +/// .body("Hello!")) +/// ) +/// } +/// } +/// # fn is_a_variant() -> bool { true } +/// # fn main() {} +/// ``` +#[derive(Debug, PartialEq)] +pub enum Either { + /// First branch of the type + A(A), + /// Second branch of the type + B(B), +} + +#[cfg(test)] +impl Either { + pub(self) fn unwrap_left(self) -> A { + match self { + Either::A(data) => data, + Either::B(_) => { + panic!("Cannot unwrap left branch. Either contains a right branch.") + } + } + } + + pub(self) fn unwrap_right(self) -> B { + match self { + Either::A(_) => { + panic!("Cannot unwrap right branch. Either contains a left branch.") + } + Either::B(data) => data, + } + } +} + +impl Responder for Either +where + A: Responder, + B: Responder, +{ + type Error = Error; + type Future = EitherResponder; + + fn respond_to(self, req: &HttpRequest) -> Self::Future { + match self { + Either::A(a) => EitherResponder::A(a.respond_to(req)), + Either::B(b) => EitherResponder::B(b.respond_to(req)), + } + } +} + +#[pin_project(project = EitherResponderProj)] +pub enum EitherResponder +where + A: Responder, + B: Responder, +{ + A(#[pin] A::Future), + B(#[pin] B::Future), +} + +impl Future for EitherResponder +where + A: Responder, + B: Responder, +{ + type Output = Result; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + match self.project() { + EitherResponderProj::A(fut) => { + Poll::Ready(ready!(fut.poll(cx)).map_err(|e| e.into())) + } + EitherResponderProj::B(fut) => { + Poll::Ready(ready!(fut.poll(cx).map_err(|e| e.into()))) + } + } + } +} + +/// A composite error resulting from failure to extract an `Either`. +/// +/// The implementation of `Into` will return the payload buffering error or the +/// error from the primary extractor. To access the fallback error, use a match clause. +#[derive(Debug)] +pub enum EitherExtractError { + /// Error from payload buffering, such as exceeding payload max size limit. + Bytes(Error), + + /// Error from primary extractor. + Extract(A, B), +} + +impl Into for EitherExtractError +where + A: Into, + B: Into, +{ + fn into(self) -> Error { + match self { + EitherExtractError::Bytes(err) => err, + EitherExtractError::Extract(a_err, _b_err) => a_err.into(), + } + } +} + +/// Provides a mechanism for trying two extractors, a primary and a fallback. Useful for +/// "polymorphic payloads" where, for example, a form might be JSON or URL encoded. +/// +/// It is important to note that this extractor, by necessity, buffers the entire request payload +/// as part of its implementation. Though, it does respect a `PayloadConfig`'s maximum size limit. +impl FromRequest for Either +where + A: FromRequest + 'static, + B: FromRequest + 'static, +{ + type Error = EitherExtractError; + type Future = LocalBoxFuture<'static, Result>; + type Config = (); + + fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { + let req2 = req.clone(); + + Bytes::from_request(req, payload) + .map_err(EitherExtractError::Bytes) + .and_then(|bytes| bytes_to_a_or_b(req2, bytes)) + .boxed_local() + } +} + +async fn bytes_to_a_or_b( + req: HttpRequest, + bytes: Bytes, +) -> Result, EitherExtractError> +where + A: FromRequest + 'static, + B: FromRequest + 'static, +{ + let fallback = bytes.clone(); + let a_err; + + let mut pl = payload_from_bytes(bytes); + match A::from_request(&req, &mut pl).await { + Ok(a_data) => return Ok(Either::A(a_data)), + // store A's error for returning if B also fails + Err(err) => a_err = err, + }; + + let mut pl = payload_from_bytes(fallback); + match B::from_request(&req, &mut pl).await { + Ok(b_data) => return Ok(Either::B(b_data)), + Err(b_err) => Err(EitherExtractError::Extract(a_err, b_err)), + } +} + +fn payload_from_bytes(bytes: Bytes) -> dev::Payload { + let (_, mut h1_payload) = actix_http::h1::Payload::create(true); + h1_payload.unread_data(bytes); + dev::Payload::from(h1_payload) +} + +#[cfg(test)] +mod tests { + use serde::{Deserialize, Serialize}; + + use super::*; + use crate::{ + test::TestRequest, + web::{Form, Json}, + }; + + #[derive(Debug, Clone, Serialize, Deserialize)] + struct TestForm { + hello: String, + } + + #[actix_rt::test] + async fn test_either_extract_first_try() { + let (req, mut pl) = TestRequest::default() + .set_form(&TestForm { + hello: "world".to_owned(), + }) + .to_http_parts(); + + let form = Either::, Json>::from_request(&req, &mut pl) + .await + .unwrap() + .unwrap_left() + .into_inner(); + assert_eq!(&form.hello, "world"); + } + + #[actix_rt::test] + async fn test_either_extract_fallback() { + let (req, mut pl) = TestRequest::default() + .set_json(&TestForm { + hello: "world".to_owned(), + }) + .to_http_parts(); + + let form = Either::, Json>::from_request(&req, &mut pl) + .await + .unwrap() + .unwrap_right() + .into_inner(); + assert_eq!(&form.hello, "world"); + } + + #[actix_rt::test] + async fn test_either_extract_recursive_fallback() { + let (req, mut pl) = TestRequest::default() + .set_payload(Bytes::from_static(b"!@$%^&*()")) + .to_http_parts(); + + let payload = + Either::, Json>, Bytes>::from_request( + &req, &mut pl, + ) + .await + .unwrap() + .unwrap_right(); + assert_eq!(&payload.as_ref(), &b"!@$%^&*()"); + } + + #[actix_rt::test] + async fn test_either_extract_recursive_fallback_inner() { + let (req, mut pl) = TestRequest::default() + .set_json(&TestForm { + hello: "world".to_owned(), + }) + .to_http_parts(); + + let form = + Either::, Json>, Bytes>::from_request( + &req, &mut pl, + ) + .await + .unwrap() + .unwrap_left() + .unwrap_right() + .into_inner(); + assert_eq!(&form.hello, "world"); + } +} diff --git a/src/types/form.rs b/src/types/form.rs index 2a7101287..82ea73216 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -35,7 +35,7 @@ use crate::{responder::Responder, web}; /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// -/// [**FormConfig**](struct.FormConfig.html) allows to configure extraction +/// [**FormConfig**](FormConfig) allows to configure extraction /// process. /// /// ### Example diff --git a/src/types/json.rs b/src/types/json.rs index 081a022e8..95613a0ce 100644 --- a/src/types/json.rs +++ b/src/types/json.rs @@ -1,14 +1,16 @@ //! Json extractor/responder use std::future::Future; +use std::marker::PhantomData; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; use std::{fmt, ops}; use bytes::BytesMut; -use futures_util::future::{err, ok, FutureExt, LocalBoxFuture, Ready}; -use futures_util::StreamExt; +use futures_util::future::{ready, Ready}; +use futures_util::ready; +use futures_util::stream::Stream; use serde::de::DeserializeOwned; use serde::Serialize; @@ -31,7 +33,7 @@ use crate::{responder::Responder, web}; /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// -/// [**JsonConfig**](struct.JsonConfig.html) allows to configure extraction +/// [**JsonConfig**](JsonConfig) allows to configure extraction /// process. /// /// ## Example @@ -127,12 +129,12 @@ impl Responder for Json { fn respond_to(self, _: &HttpRequest) -> Self::Future { let body = match serde_json::to_string(&self.0) { Ok(body) => body, - Err(e) => return err(e.into()), + Err(e) => return ready(Err(e.into())), }; - ok(Response::build(StatusCode::OK) + ready(Ok(Response::build(StatusCode::OK) .content_type("application/json") - .body(body)) + .body(body))) } } @@ -142,7 +144,7 @@ impl Responder for Json { /// To extract typed information from request's body, the type `T` must /// implement the `Deserialize` trait from *serde*. /// -/// [**JsonConfig**](struct.JsonConfig.html) allows to configure extraction +/// [**JsonConfig**](JsonConfig) allows to configure extraction /// process. /// /// ## Example @@ -173,37 +175,64 @@ where T: DeserializeOwned + 'static, { type Error = Error; - type Future = LocalBoxFuture<'static, Result>; + type Future = JsonExtractFut; type Config = JsonConfig; #[inline] fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { - let req2 = req.clone(); let config = JsonConfig::from_req(req); let limit = config.limit; let ctype = config.content_type.clone(); let err_handler = config.err_handler.clone(); - JsonBody::new(req, payload, ctype) - .limit(limit) - .map(move |res| match res { - Err(e) => { - log::debug!( - "Failed to deserialize Json from payload. \ - Request path: {}", - req2.path() - ); + JsonExtractFut { + req: Some(req.clone()), + fut: JsonBody::new(req, payload, ctype).limit(limit), + err_handler, + } + } +} - if let Some(err) = err_handler { - Err((*err)(e, &req2)) - } else { - Err(e.into()) - } +type JsonErrorHandler = + Option Error + Send + Sync>>; + +pub struct JsonExtractFut { + req: Option, + fut: JsonBody, + err_handler: JsonErrorHandler, +} + +impl Future for JsonExtractFut +where + T: DeserializeOwned + 'static, +{ + type Output = Result, Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); + + let res = ready!(Pin::new(&mut this.fut).poll(cx)); + + let res = match res { + Err(e) => { + let req = this.req.take().unwrap(); + log::debug!( + "Failed to deserialize Json from payload. \ + Request path: {}", + req.path() + ); + + if let Some(err) = this.err_handler.as_ref() { + Err((*err)(e, &req)) + } else { + Err(e.into()) } - Ok(data) => Ok(Json(data)), - }) - .boxed_local() + } + Ok(data) => Ok(Json(data)), + }; + + Poll::Ready(res) } } @@ -248,8 +277,7 @@ where #[derive(Clone)] pub struct JsonConfig { limit: usize, - err_handler: - Option Error + Send + Sync>>, + err_handler: JsonErrorHandler, content_type: Option bool + Send + Sync>>, } @@ -306,19 +334,24 @@ impl Default for JsonConfig { /// Returns error: /// /// * content type is not `application/json` -/// (unless specified in [`JsonConfig`](struct.JsonConfig.html)) +/// (unless specified in [`JsonConfig`]) /// * content length is greater than 256k -pub struct JsonBody { - limit: usize, - length: Option, - #[cfg(feature = "compress")] - stream: Option>, - #[cfg(not(feature = "compress"))] - stream: Option, - err: Option, - fut: Option>>, +pub enum JsonBody { + Error(Option), + Body { + limit: usize, + length: Option, + #[cfg(feature = "compress")] + payload: Decompress, + #[cfg(not(feature = "compress"))] + payload: Payload, + buf: BytesMut, + _res: PhantomData, + }, } +impl Unpin for JsonBody {} + impl JsonBody where U: DeserializeOwned + 'static, @@ -340,39 +373,58 @@ where }; if !json { - return JsonBody { - limit: 262_144, - length: None, - stream: None, - fut: None, - err: Some(JsonPayloadError::ContentType), - }; + return JsonBody::Error(Some(JsonPayloadError::ContentType)); } - let len = req + let length = req .headers() .get(&CONTENT_LENGTH) .and_then(|l| l.to_str().ok()) .and_then(|s| s.parse::().ok()); + // Notice the content_length is not checked against limit of json config here. + // As the internal usage always call JsonBody::limit after JsonBody::new. + // And limit check to return an error variant of JsonBody happens there. + #[cfg(feature = "compress")] let payload = Decompress::from_headers(payload.take(), req.headers()); #[cfg(not(feature = "compress"))] let payload = payload.take(); - JsonBody { + JsonBody::Body { limit: 262_144, - length: len, - stream: Some(payload), - fut: None, - err: None, + length, + payload, + buf: BytesMut::with_capacity(8192), + _res: PhantomData, } } /// Change max size of payload. By default max size is 256Kb - pub fn limit(mut self, limit: usize) -> Self { - self.limit = limit; - self + pub fn limit(self, limit: usize) -> Self { + match self { + JsonBody::Body { + length, + payload, + buf, + .. + } => { + if let Some(len) = length { + if len > limit { + return JsonBody::Error(Some(JsonPayloadError::Overflow)); + } + } + + JsonBody::Body { + limit, + length, + payload, + buf, + _res: PhantomData, + } + } + JsonBody::Error(e) => JsonBody::Error(e), + } } } @@ -382,41 +434,34 @@ where { type Output = Result; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - if let Some(ref mut fut) = self.fut { - return Pin::new(fut).poll(cx); - } + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); - if let Some(err) = self.err.take() { - return Poll::Ready(Err(err)); - } - - let limit = self.limit; - if let Some(len) = self.length.take() { - if len > limit { - return Poll::Ready(Err(JsonPayloadError::Overflow)); - } - } - let mut stream = self.stream.take().unwrap(); - - self.fut = Some( - async move { - let mut body = BytesMut::with_capacity(8192); - - while let Some(item) = stream.next().await { - let chunk = item?; - if (body.len() + chunk.len()) > limit { - return Err(JsonPayloadError::Overflow); - } else { - body.extend_from_slice(&chunk); + match this { + JsonBody::Body { + limit, + buf, + payload, + .. + } => loop { + let res = ready!(Pin::new(&mut *payload).poll_next(cx)); + match res { + Some(chunk) => { + let chunk = chunk?; + if (buf.len() + chunk.len()) > *limit { + return Poll::Ready(Err(JsonPayloadError::Overflow)); + } else { + buf.extend_from_slice(&chunk); + } + } + None => { + let json = serde_json::from_slice::(&buf)?; + return Poll::Ready(Ok(json)); } } - Ok(serde_json::from_slice::(&body)?) - } - .boxed_local(), - ); - - self.poll(cx) + }, + JsonBody::Error(e) => Poll::Ready(Err(e.take().unwrap())), + } } } diff --git a/src/types/mod.rs b/src/types/mod.rs index b32711e2a..cedf86dd2 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,5 +1,6 @@ //! Helper types +mod either; pub(crate) mod form; pub(crate) mod json; mod path; @@ -7,6 +8,7 @@ pub(crate) mod payload; mod query; pub(crate) mod readlines; +pub use self::either::{Either, EitherExtractError}; pub use self::form::{Form, FormConfig}; pub use self::json::{Json, JsonConfig}; pub use self::path::{Path, PathConfig}; diff --git a/src/types/path.rs b/src/types/path.rs index dbb5f3ee0..640ff4346 100644 --- a/src/types/path.rs +++ b/src/types/path.rs @@ -15,7 +15,7 @@ use crate::FromRequest; #[derive(PartialEq, Eq, PartialOrd, Ord)] /// Extract typed information from the request's path. /// -/// [**PathConfig**](struct.PathConfig.html) allows to configure extraction process. +/// [**PathConfig**](PathConfig) allows to configure extraction process. /// /// ## Example /// diff --git a/src/types/payload.rs b/src/types/payload.rs index 4ff5ef4b4..9228b37aa 100644 --- a/src/types/payload.rs +++ b/src/types/payload.rs @@ -7,10 +7,12 @@ use std::task::{Context, Poll}; use actix_http::error::{Error, ErrorBadRequest, PayloadError}; use actix_http::HttpMessage; use bytes::{Bytes, BytesMut}; -use encoding_rs::UTF_8; +use encoding_rs::{Encoding, UTF_8}; use futures_core::stream::Stream; -use futures_util::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready}; -use futures_util::StreamExt; +use futures_util::{ + future::{err, ok, Either, ErrInto, Ready, TryFutureExt as _}, + ready, +}; use mime::Mime; use crate::extract::FromRequest; @@ -111,7 +113,7 @@ impl FromRequest for Payload { /// /// Loads request's payload and construct Bytes instance. /// -/// [**PayloadConfig**](struct.PayloadConfig.html) allows to configure +/// [**PayloadConfig**](PayloadConfig) allows to configure /// extraction process. /// /// ## Example @@ -135,10 +137,7 @@ impl FromRequest for Payload { impl FromRequest for Bytes { type Config = PayloadConfig; type Error = Error; - type Future = Either< - LocalBoxFuture<'static, Result>, - Ready>, - >; + type Future = Either, Ready>>; #[inline] fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { @@ -151,7 +150,7 @@ impl FromRequest for Bytes { let limit = cfg.limit; let fut = HttpMessageBody::new(req, payload).limit(limit); - Either::Left(async move { Ok(fut.await?) }.boxed_local()) + Either::Left(fut.err_into()) } } @@ -159,7 +158,7 @@ impl FromRequest for Bytes { /// /// Text extractor automatically decode body according to the request's charset. /// -/// [**PayloadConfig**](struct.PayloadConfig.html) allows to configure +/// [**PayloadConfig**](PayloadConfig) allows to configure /// extraction process. /// /// ## Example @@ -185,10 +184,7 @@ impl FromRequest for Bytes { impl FromRequest for String { type Config = PayloadConfig; type Error = Error; - type Future = Either< - LocalBoxFuture<'static, Result>, - Ready>, - >; + type Future = Either>>; #[inline] fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { @@ -205,25 +201,40 @@ impl FromRequest for String { Err(e) => return Either::Right(err(e.into())), }; let limit = cfg.limit; - let fut = HttpMessageBody::new(req, payload).limit(limit); + let body_fut = HttpMessageBody::new(req, payload).limit(limit); - Either::Left( - async move { - let body = fut.await?; + Either::Left(StringExtractFut { body_fut, encoding }) + } +} - if encoding == UTF_8 { - Ok(str::from_utf8(body.as_ref()) - .map_err(|_| ErrorBadRequest("Can not decode body"))? - .to_owned()) - } else { - Ok(encoding - .decode_without_bom_handling_and_without_replacement(&body) - .map(|s| s.into_owned()) - .ok_or_else(|| ErrorBadRequest("Can not decode body"))?) - } - } - .boxed_local(), - ) +pub struct StringExtractFut { + body_fut: HttpMessageBody, + encoding: &'static Encoding, +} + +impl<'a> Future for StringExtractFut { + type Output = Result; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let encoding = self.encoding; + + Pin::new(&mut self.body_fut).poll(cx).map(|out| { + let body = out?; + bytes_to_string(body, encoding) + }) + } +} + +fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result { + if encoding == UTF_8 { + Ok(str::from_utf8(body.as_ref()) + .map_err(|_| ErrorBadRequest("Can not decode body"))? + .to_owned()) + } else { + Ok(encoding + .decode_without_bom_handling_and_without_replacement(&body) + .map(|s| s.into_owned()) + .ok_or_else(|| ErrorBadRequest("Can not decode body"))?) } } @@ -241,9 +252,10 @@ pub struct PayloadConfig { impl PayloadConfig { /// Create `PayloadConfig` instance and set max size of payload. pub fn new(limit: usize) -> Self { - let mut cfg = Self::default(); - cfg.limit = limit; - cfg + Self { + limit, + ..Default::default() + } } /// Change max size of payload. By default max size is 256Kb @@ -290,10 +302,12 @@ impl PayloadConfig { // Allow shared refs to default. const DEFAULT_CONFIG: PayloadConfig = PayloadConfig { - limit: 262_144, // 2^18 bytes (~256kB) + limit: DEFAULT_CONFIG_LIMIT, mimetype: None, }; +const DEFAULT_CONFIG_LIMIT: usize = 262_144; // 2^18 bytes (~256kB) + impl Default for PayloadConfig { fn default() -> Self { DEFAULT_CONFIG.clone() @@ -311,99 +325,83 @@ pub struct HttpMessageBody { limit: usize, length: Option, #[cfg(feature = "compress")] - stream: Option>, + stream: dev::Decompress, #[cfg(not(feature = "compress"))] - stream: Option, + stream: dev::Payload, + buf: BytesMut, err: Option, - fut: Option>>, } impl HttpMessageBody { /// Create `MessageBody` for request. #[allow(clippy::borrow_interior_mutable_const)] pub fn new(req: &HttpRequest, payload: &mut dev::Payload) -> HttpMessageBody { - let mut len = None; + let mut length = None; + let mut err = None; + if let Some(l) = req.headers().get(&header::CONTENT_LENGTH) { - if let Ok(s) = l.to_str() { - if let Ok(l) = s.parse::() { - len = Some(l) - } else { - return Self::err(PayloadError::UnknownLength); - } - } else { - return Self::err(PayloadError::UnknownLength); + match l.to_str() { + Ok(s) => match s.parse::() { + Ok(l) if l > DEFAULT_CONFIG_LIMIT => { + err = Some(PayloadError::Overflow) + } + Ok(l) => length = Some(l), + Err(_) => err = Some(PayloadError::UnknownLength), + }, + Err(_) => err = Some(PayloadError::UnknownLength), } } #[cfg(feature = "compress")] - let stream = Some(dev::Decompress::from_headers(payload.take(), req.headers())); + let stream = dev::Decompress::from_headers(payload.take(), req.headers()); #[cfg(not(feature = "compress"))] - let stream = Some(payload.take()); + let stream = payload.take(); HttpMessageBody { stream, - limit: 262_144, - length: len, - fut: None, - err: None, + limit: DEFAULT_CONFIG_LIMIT, + length, + buf: BytesMut::with_capacity(8192), + err, } } /// Change max size of payload. By default max size is 256Kb pub fn limit(mut self, limit: usize) -> Self { + if let Some(l) = self.length { + if l > limit { + self.err = Some(PayloadError::Overflow); + } + } self.limit = limit; self } - - fn err(e: PayloadError) -> Self { - HttpMessageBody { - stream: None, - limit: 262_144, - fut: None, - err: Some(e), - length: None, - } - } } impl Future for HttpMessageBody { type Output = Result; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - if let Some(ref mut fut) = self.fut { - return Pin::new(fut).poll(cx); + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); + + if let Some(e) = this.err.take() { + return Poll::Ready(Err(e)); } - if let Some(err) = self.err.take() { - return Poll::Ready(Err(err)); - } - - if let Some(len) = self.length.take() { - if len > self.limit { - return Poll::Ready(Err(PayloadError::Overflow)); - } - } - - // future - let limit = self.limit; - let mut stream = self.stream.take().unwrap(); - self.fut = Some( - async move { - let mut body = BytesMut::with_capacity(8192); - - while let Some(item) = stream.next().await { - let chunk = item?; - if body.len() + chunk.len() > limit { - return Err(PayloadError::Overflow); + loop { + let res = ready!(Pin::new(&mut this.stream).poll_next(cx)); + match res { + Some(chunk) => { + let chunk = chunk?; + if this.buf.len() + chunk.len() > this.limit { + return Poll::Ready(Err(PayloadError::Overflow)); } else { - body.extend_from_slice(&chunk); + this.buf.extend_from_slice(&chunk); } } - Ok(body.freeze()) + None => return Poll::Ready(Ok(this.buf.split().freeze())), } - .boxed_local(), - ); - self.poll(cx) + } } } diff --git a/src/types/query.rs b/src/types/query.rs index f9440e1b4..27df220fc 100644 --- a/src/types/query.rs +++ b/src/types/query.rs @@ -18,7 +18,7 @@ use crate::request::HttpRequest; /// be decoded into any type which depends upon data ordering e.g. tuples or tuple-structs. /// Attempts to do so will *fail at runtime*. /// -/// [**QueryConfig**](struct.QueryConfig.html) allows to configure extraction process. +/// [**QueryConfig**](QueryConfig) allows to configure extraction process. /// /// ## Example /// @@ -39,7 +39,7 @@ use crate::request::HttpRequest; /// } /// /// // Use `Query` extractor for query information (and destructure it within the signature). -/// // This handler gets called only if the request's query string contains a `username` field. +/// // This handler gets called only if the request's query string contains `id` and `response_type` fields. /// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`. /// async fn index(web::Query(info): web::Query) -> String { /// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type) @@ -117,7 +117,7 @@ impl fmt::Display for Query { /// } /// /// // Use `Query` extractor for query information. -/// // This handler get called only if request's query contains `username` field +/// // This handler get called only if request's query contains `id` and `response_type` fields. /// // The correct request for this handler would be `/index.html?id=64&response_type=Code"` /// async fn index(info: web::Query) -> String { /// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type) diff --git a/src/web.rs b/src/web.rs index 1d1174f41..bf2158917 100644 --- a/src/web.rs +++ b/src/web.rs @@ -4,7 +4,7 @@ use actix_router::IntoPattern; use std::future::Future; pub use actix_http::Response as HttpResponse; -pub use bytes::{Bytes, BytesMut}; +pub use bytes::{Buf, BufMut, Bytes, BytesMut}; pub use futures_channel::oneshot::Canceled; use crate::error::BlockingError; @@ -19,6 +19,7 @@ use crate::service::WebService; pub use crate::config::ServiceConfig; pub use crate::data::Data; pub use crate::request::HttpRequest; +pub use crate::request_data::ReqData; pub use crate::types::*; /// Create resource for a specific path. diff --git a/test-server/README.md b/test-server/README.md deleted file mode 100644 index db0791db7..000000000 --- a/test-server/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Actix http test server [![Build Status](https://travis-ci.org/actix/actix-web.svg?branch=master)](https://travis-ci.org/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-web) [![crates.io](https://meritbadge.herokuapp.com/actix-http-test)](https://crates.io/crates/actix-http-test) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) - -## Documentation & community resources - -* [User Guide](https://actix.rs/docs/) -* [API Documentation](https://docs.rs/actix-http-test/) -* [Chat on gitter](https://gitter.im/actix/actix) -* Cargo package: [actix-http-test](https://crates.io/crates/actix-http-test) -* Minimum supported Rust version: 1.40 or later diff --git a/tests/test_server.rs b/tests/test_server.rs index f8a9ab86d..c6c316f0d 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -248,6 +248,7 @@ async fn test_body_gzip_large_random() { let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(70_000) + .map(char::from) .collect::(); let srv_data = data.clone(); @@ -529,6 +530,7 @@ async fn test_reading_gzip_encoding_large_random() { let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(60_000) + .map(char::from) .collect::(); let srv = test::start_with(test::config().h1(), || { @@ -614,6 +616,7 @@ async fn test_reading_deflate_encoding_large_random() { let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(160_000) + .map(char::from) .collect::(); let srv = test::start_with(test::config().h1(), || { @@ -672,6 +675,7 @@ async fn test_brotli_encoding_large() { let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(320_000) + .map(char::from) .collect::(); let srv = test::start_with(test::config().h1(), || { @@ -753,6 +757,7 @@ async fn test_reading_deflate_encoding_large_random_rustls() { let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(160_000) + .map(char::from) .collect::(); // load ssl keys