1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-23 17:28:19 +02:00

Compare commits

..

59 Commits

Author SHA1 Message Date
Yuki Okushi
6e8ff5c905 Merge pull request #1495 from JohnTitor/new-web
actix-web: Bump up to 3.0.0-alpha.2
2020-05-08 07:28:18 +09:00
Yuki Okushi
b66c3083a5 Update the actix-web dependency to 3.0.0-alpha.2 2020-05-08 06:46:42 +09:00
Yuki Okushi
b6b3481c6f web: Bump up to 3.0.0-alpha.2 2020-05-08 06:46:13 +09:00
Yuki Okushi
574714d156 Merge pull request #1494 from JohnTitor/new-actors
actors: Bump up to 3.0.0-alpha.1
2020-05-08 06:17:15 +09:00
Yuki Okushi
54abf356d4 actors: Bump up to 3.0.0-alpha.1 2020-05-08 03:33:29 +09:00
Yuki Okushi
9cb3b0ef58 Merge pull request #1493 from JohnTitor/http-next
http: Bump up to 2.0.0-alpha.3
2020-05-08 03:09:52 +09:00
Yuki Okushi
9d0c80b6ce Update actix-http deps 2020-05-08 02:35:45 +09:00
Yuki Okushi
0bc4a5e703 http: Bump up to 2.0.0-alpha.3 2020-05-08 02:35:45 +09:00
Rob Ede
9d94fb91b2 correct spelling of ConnectError::Unresolved (#1487)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-05-08 02:26:48 +09:00
Quentin de Quelen
9164ed1f0c add resource middleware on actix-web-codegen (#1467)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-05-07 18:31:12 +09:00
Rob Ede
b521e9b221 conditional test compilation [range, charset] (#1483)
* conditionally compile range and charset tests

* remove deprecated try macros

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-05-03 22:33:29 +09:00
Rob Ede
f37cb6dd0b refactor h1 status line helper to remove unsafe usage (#1484)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-05-03 17:37:40 +09:00
Mikail Bagishov
d5ceae2074 Replace deprecated now with now_utc (#1481)
* Replace deprecated now with now_utc

* Update doctest
2020-05-02 10:14:50 +01:00
Rob Ede
c27d3fad8e clarify resource/scope app data overriding (#1476)
* relocate FnDataFactory

* clarify app data overriding in Scope and Resource

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-04-30 02:20:47 +09:00
Rob Ede
bb17280f51 simplify data factory future polling (#1473)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-04-29 15:38:53 +09:00
Huston Bokinsky
b047413b39 Small ws codec fix (#1465)
* Small ws codec fix

* Update actix-http/Changes.md

Co-authored-by: Huston Bokinsky <huston@deepgram.com>
2020-04-29 11:13:09 +09:00
Huy
ce24630d31 Fix typos in MIGRATION.md (#1470)
* Fix typos in MIGRATION.md

Those are `crate` not `create`

* Update MIGRATION.md
2020-04-23 03:39:09 +09:00
Yuki Okushi
751253f23e Merge pull request #1463 from actix/fix/doc-typos
fix spelling errors in doc comments
2020-04-21 13:47:03 +09:00
Rob Ede
5b0f7fff69 fix spelling errors in doc comments 2020-04-21 04:09:35 +01:00
Yuki Okushi
54619cb680 actix-http: Remove failure support (#1449) 2020-04-16 06:54:34 +09:00
Yuki Okushi
5b36381cb0 Merge pull request #1452 from actix/fix/default-service-data
set data container on default service calls
2020-04-16 06:01:56 +09:00
Rob Ede
45e2e40140 set data container on default service calls
closes #1450
2020-04-14 02:33:19 +01:00
Yuki Okushi
df3f722589 Merge pull request #1451 from actix/cache
Remove cache config from GHA workflows
2020-04-13 06:06:45 +09:00
Yuki Okushi
e7ba871bbf Remove cache config from GHA workflows 2020-04-13 03:42:44 +09:00
Yuki Okushi
ebc2e67015 Merge pull request #1442 from JohnTitor/workspace-doc
Deploy all the workspace crates' docs
2020-04-09 00:48:08 +09:00
Yuki Okushi
74ddc852c8 Tweak README 2020-04-08 04:48:01 +09:00
Yuki Okushi
dfaa330a94 Deploy all the workspace crates' docs 2020-04-08 04:42:38 +09:00
Tore Pettersen
0ad02ee0e0 Add convenience functions for testing (#1401)
* Add convenience functions for testing

* Fix remarks from PR and add tests

* Add unpin to read_json_body

* Update changelog
2020-04-06 04:12:44 +09:00
Stephen Eckels
aaff68bf05 Change NormalizePath to append trailing slash (#1433)
* Change NormalizePath to append trailing slash

* add tests

* Update CHANGES.md

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-04-05 03:26:40 +09:00
Yuki Okushi
fcb1dec235 Merge pull request #1431 from OSSystems/topic/explicit-features-requirements
Add explicit feature requirements for examples and tests
2020-03-28 10:58:00 +09:00
Otavio Salvador
7b7daa75a4 Add explicit feature requirements for examples and tests
This allow us to build 'actix-web' without default features and run all
tests.

Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-25 23:49:24 -03:00
Ivan Tham
2067331884 Refactor actix-codegen duplicate code (#1423)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-03-20 04:40:42 +09:00
Yuki Okushi
bf630d9475 Merge pull request #1422 from OSSystems/topic/impl-error-more-types
Implement `std::error::Error` for our custom errors
2020-03-19 05:05:57 +09:00
Otavio Salvador
146ae4da18 Implement std::error::Error for our custom errors
For allowing a more ergonomic use and better integration on the
ecosystem, this adds the `std::error::Error` `impl` for our custom
errors.

We intent to drop this hand made code once `derive_more` finishes the
addition of the Error derive support[1]. Until that is available, we
need to live with that.

1. https://github.com/JelteF/derive_more/issues/92

Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-18 00:22:18 -03:00
Yuki Okushi
52c5755d56 Merge pull request #1421 from actix/JohnTitor-patch-1
Upload coverage on PRs
2020-03-18 06:16:41 +09:00
Yuki Okushi
5548c57a09 Upload coverage on PRs 2020-03-18 05:04:30 +09:00
Stig Johan Berggren
0d958fabd7 📝 Improve the code example for JsonConfig (#1418)
* 📝 Improve the code example for JsonConfig

* Remove a redundant comment
2020-03-17 08:23:54 +09:00
Oleg Nosov
c67e4c1fe8 Refactored macros (#1333)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
2020-03-15 07:23:28 +09:00
Yuki Okushi
4875dfbec7 Merge pull request #1416 from JohnTitor/fix-doc
Fix `read_body` doc
2020-03-13 07:06:57 +09:00
Yuki Okushi
d602a7e386 Fix read_body doc 2020-03-13 05:52:58 +09:00
Yuki Okushi
9f196fe5a5 Merge pull request #1413 from OSSystems/topic/fix-warnings
Fix clippy warnings
2020-03-12 16:24:44 +09:00
Yuki Okushi
e4adcd1935 Merge pull request #1411 from JohnTitor/patch
Clean-up metadata
2020-03-12 16:21:34 +09:00
Otavio Salvador
7e0d898d5a Fix clippy warnings
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
2020-03-12 00:52:21 -03:00
Yuki Okushi
51518721e5 Add notes to READMEs 2020-03-12 07:57:38 +09:00
Yuki Okushi
c02d3e205b Clean-up metadata 2020-03-12 07:57:20 +09:00
Yuki Okushi
a253d7d3ce Merge pull request #1410 from JohnTitor/new-web
Release actix-web 3.0.0-alpha.1
2020-03-12 05:12:54 +09:00
Yuki Okushi
0152cedc5d Update changelog 2020-03-12 03:24:15 +09:00
Yuki Okushi
a6a47b95ee Exclude actix-cors 2020-03-12 03:04:31 +09:00
Yuki Okushi
1b28a5d48b Update actix-web dependency to 3.0.0-alpha.1 2020-03-12 03:03:50 +09:00
Yuki Okushi
c147b94832 Bump up to 3.0.0-alpha.1 2020-03-12 03:03:22 +09:00
Yuki Okushi
95f9a12a5e dev-deps: Update env_logger to 0.7 2020-03-12 02:58:22 +09:00
Yuki Okushi
73eeab0e90 Merge pull request #1391 from JohnTitor/new-awc
Release awc v2.0.0-alpha.1
2020-03-11 21:15:48 +09:00
Yuki Okushi
ce1e996b41 Update release date 2020-03-11 20:42:45 +09:00
Yuki Okushi
e718f65121 Update tests 2020-03-08 16:42:45 +09:00
Yuki Okushi
a9a475d555 Make test_server async fn 2020-03-08 16:42:26 +09:00
Yuki Okushi
b93e1555ec Update actix-connect to 2.0.0-alpha.2 2020-03-08 15:27:40 +09:00
Yuki Okushi
6f33b7ea42 Update awc dependency 2020-03-08 15:27:40 +09:00
Yuki Okushi
294523a32f Bump up to 2.0.0-alpha.1 2020-03-08 15:27:39 +09:00
Yuki Okushi
6b626c7d77 dev-deps: Update env_logger to 0.7 2020-03-08 03:07:53 +09:00
84 changed files with 1458 additions and 835 deletions

View File

@@ -16,32 +16,7 @@ jobs:
profile: minimal
override: true
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ matrix.version }}-x86_64-unknown-linux-gnu-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ matrix.version }}-x86_64-unknown-linux-gnu-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ matrix.version }}-x86_64-unknown-linux-gnu-cargo-build-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Check benchmark
uses: actions-rs/cargo@v1
with:
command: bench
- name: Clear the cargo caches
run: |
cargo install cargo-cache --no-default-features --features ci-autoclean
cargo-cache

View File

@@ -25,26 +25,6 @@ jobs:
profile: minimal
override: true
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ matrix.version }}-x86_64-unknown-linux-gnu-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ matrix.version }}-x86_64-unknown-linux-gnu-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ matrix.version }}-x86_64-unknown-linux-gnu-cargo-build-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: check build
uses: actions-rs/cargo@v1
with:
@@ -73,18 +53,12 @@ jobs:
args: --package=awc --no-default-features --features=rustls -- --nocapture
- name: Generate coverage file
if: matrix.version == 'stable' && github.ref == 'refs/heads/master'
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
run: |
cargo install cargo-tarpaulin
cargo tarpaulin --out Xml
- name: Upload to Codecov
if: matrix.version == 'stable' && github.ref == 'refs/heads/master'
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: cobertura.xml
- name: Clear the cargo caches
run: |
cargo install cargo-cache --no-default-features --features ci-autoclean
cargo-cache

View File

@@ -24,26 +24,6 @@ jobs:
profile: minimal
override: true
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ matrix.version }}-x86_64-apple-darwin-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ matrix.version }}-x86_64-apple-darwin-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ matrix.version }}-x86_64-apple-darwin-cargo-build-trimmed-${{ hashFiles('**/Cargo.lock') }}
- name: check build
uses: actions-rs/cargo@v1
with:
@@ -57,8 +37,3 @@ jobs:
args: --all --all-features --no-fail-fast -- --nocapture
--skip=test_h2_content_length
--skip=test_reading_deflate_encoding_large_random_rustls
- name: Clear the cargo caches
run: |
cargo install cargo-cache --no-default-features --features ci-autoclean
cargo-cache

View File

@@ -24,7 +24,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: doc
args: --no-deps --all-features
args: --no-deps --workspace --all-features
- name: Tweak HTML
run: echo "<meta http-equiv=refresh content=0;url=os_balloon/index.html>" > target/doc/index.html
@@ -32,4 +32,4 @@ jobs:
- name: Upload documentation
run: |
git clone https://github.com/davisp/ghp-import.git
./ghp-import/ghp_import.py -n -p -f -m "Documentation upload" -r https://${{ secrets.GITHUB_TOKEN }}@github.com/"${{ github.repository }}.git" target/doc
./ghp-import/ghp_import.py -n -p -f -m "Documentation upload" -r https://${{ secrets.GITHUB_TOKEN }}@github.com/"${{ github.repository }}.git" target/doc

View File

@@ -1,20 +1,34 @@
# Changes
## [3.0.0-alpha.2] - 2020-05-08
## [2.0.NEXT] - 2020-01-xx
### Changed
* `{Resource,Scope}::default_service(f)` handlers now support app data extraction. [#1452]
* Implement `std::error::Error` for our custom errors [#1422]
* NormalizePath middleware now appends trailing / so that routes of form /example/ respond to /example requests.
* Remove the `failure` feature and support.
[#1422]: https://github.com/actix/actix-web/pull/1422
[#1452]: https://github.com/actix/actix-web/pull/1452
## [3.0.0-alpha.1] - 2020-03-11
### Added
* Add helper function for creating routes with `TRACE` method guard `web::trace()`
* Add convenience functions `test::read_body_json()` and `test::TestRequest::send_request()` for testing.
### Changed
* Use `sha-1` crate instead of unmaintained `sha1` crate
* Skip empty chunks when returning response from a `Stream` #1308
* Skip empty chunks when returning response from a `Stream` [#1308]
* Update the `time` dependency to 0.2.7
* Update `actix-tls` dependency to 2.0.0-alpha.1
* Update `rustls` dependency to 0.17
[#1308]: https://github.com/actix/actix-web/pull/1308
## [2.0.0] - 2019-12-25
### Changed

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-web"
version = "2.0.0"
version = "3.0.0-alpha.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
readme = "README.md"
@@ -30,7 +30,7 @@ members = [
".",
"awc",
"actix-http",
"actix-cors",
# "actix-cors",
"actix-files",
"actix-framed",
# "actix-session",
@@ -42,7 +42,7 @@ members = [
]
[features]
default = ["compress", "failure"]
default = ["compress"]
# content-encoding support
compress = ["actix-http/compress", "awc/compress"]
@@ -50,14 +50,24 @@ compress = ["actix-http/compress", "awc/compress"]
# sessions feature, session require "ring" crate and c compiler
secure-cookies = ["actix-http/secure-cookies"]
failure = ["actix-http/failure"]
# openssl
openssl = ["actix-tls/openssl", "awc/openssl", "open-ssl"]
# rustls
rustls = ["actix-tls/rustls", "awc/rustls", "rust-tls"]
[[example]]
name = "basic"
required-features = ["compress"]
[[example]]
name = "uds"
required-features = ["compress"]
[[test]]
name = "test_server"
required-features = ["compress"]
[dependencies]
actix-codec = "0.2.0"
actix-service = "1.0.2"
@@ -71,8 +81,8 @@ actix-threadpool = "0.3.1"
actix-tls = "2.0.0-alpha.1"
actix-web-codegen = "0.2.0"
actix-http = "2.0.0-alpha.2"
awc = { version = "1.0.1", default-features = false }
actix-http = "2.0.0-alpha.3"
awc = { version = "2.0.0-alpha.1", default-features = false }
bytes = "0.5.3"
derive_more = "0.99.2"
@@ -95,7 +105,7 @@ rust-tls = { version = "0.17.0", package = "rustls", optional = true }
[dev-dependencies]
actix = "0.10.0-alpha.1"
rand = "0.7"
env_logger = "0.6"
env_logger = "0.7"
serde_derive = "1.0"
brotli2 = "0.3.2"
flate2 = "1.0.13"
@@ -111,9 +121,6 @@ actix-web = { path = "." }
actix-http = { path = "actix-http" }
actix-http-test = { path = "test-server" }
actix-web-codegen = { path = "actix-web-codegen" }
actix-cors = { path = "actix-cors" }
actix-identity = { path = "actix-identity" }
actix-session = { path = "actix-session" }
actix-files = { path = "actix-files" }
actix-multipart = { path = "actix-multipart" }
awc = { path = "awc" }

View File

@@ -360,7 +360,7 @@
* `actix_web::server` module has been removed. To start http server use `actix_web::HttpServer` type
* StaticFiles and NamedFile has been move to separate create.
* StaticFiles and NamedFile have been moved to a separate crate.
instead of `use actix_web::fs::StaticFile`
@@ -370,7 +370,7 @@
use `use actix_files::NamedFile`
* Multipart has been move to separate create.
* Multipart has been moved to a separate crate.
instead of `use actix_web::multipart::Multipart`

View File

@@ -39,6 +39,12 @@ Actix web is a simple, pragmatic and extremely fast web framework for Rust.
* Includes an asynchronous [HTTP client](https://actix.rs/actix-web/actix_web/client/index.html)
* Supports [Actix actor framework](https://github.com/actix/actix)
## Docs
* [API documentation (master)](https://actix.rs/actix-web/actix_web)
* [API documentation (docs.rs)](https://docs.rs/actix-web)
* [User guide](https://actix.rs)
## Example
Dependencies:
@@ -74,7 +80,7 @@ async fn main() -> std::io::Result<()> {
* [Stateful](https://github.com/actix/examples/tree/master/state/)
* [Multipart streams](https://github.com/actix/examples/tree/master/multipart/)
* [Simple websocket](https://github.com/actix/examples/tree/master/websocket/)
* [Tera](https://github.com/actix/examples/tree/master/template_tera/) /
* [Tera](https://github.com/actix/examples/tree/master/template_tera/)
* [Askama](https://github.com/actix/examples/tree/master/template_askama/) templates
* [Diesel integration](https://github.com/actix/examples/tree/master/diesel/)
* [r2d2](https://github.com/actix/examples/tree/master/r2d2/)

View File

@@ -1,5 +1,7 @@
# Cors Middleware for actix web framework [![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-cors)](https://crates.io/crates/actix-cors) [![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)
**This crate moved to https://github.com/actix/actix-extras.**
## Documentation & community resources
* [User Guide](https://actix.rs/docs/)

View File

@@ -18,8 +18,8 @@ name = "actix_files"
path = "src/lib.rs"
[dependencies]
actix-web = { version = "2.0.0-rc", default-features = false }
actix-http = "2.0.0-alpha.2"
actix-web = { version = "3.0.0-alpha.2", default-features = false }
actix-http = "2.0.0-alpha.3"
actix-service = "1.0.1"
bitflags = "1"
bytes = "0.5.3"
@@ -33,4 +33,4 @@ v_htmlescape = "0.4"
[dev-dependencies]
actix-rt = "1.0.0"
actix-web = { version = "2.0.0-rc", features=["openssl"] }
actix-web = { version = "3.0.0-alpha.2", features = ["openssl"] }

View File

@@ -521,7 +521,7 @@ impl Service for FilesService {
Err(e) => return Either::Left(ok(req.error_response(e))),
};
// full filepath
// full file path
let path = match self.directory.join(&real_path.0).canonicalize() {
Ok(path) => path,
Err(e) => return self.handle_err(e, req),

View File

@@ -23,7 +23,7 @@ actix-codec = "0.2.0"
actix-service = "1.0.1"
actix-router = "0.2.1"
actix-rt = "1.0.0"
actix-http = "2.0.0-alpha.2"
actix-http = "2.0.0-alpha.3"
bytes = "0.5.3"
futures = "0.3.1"
@@ -32,6 +32,6 @@ log = "0.4"
[dev-dependencies]
actix-server = "1.0.0"
actix-connect = { version = "2.0.0-alpha.1", features=["openssl"] }
actix-connect = { version = "2.0.0-alpha.2", features=["openssl"] }
actix-http-test = { version = "1.0.0", features=["openssl"] }
actix-utils = "1.0.3"

View File

@@ -7,7 +7,7 @@ mod service;
mod state;
pub mod test;
// re-export for convinience
// re-export for convenience
pub use actix_http::{http, Error, HttpMessage, Response, ResponseError};
pub use self::app::{FramedApp, FramedAppService};

View File

@@ -42,7 +42,7 @@ impl<Io, S> FramedRequest<Io, S> {
self.req.head()
}
/// This method returns muttable reference to the request head.
/// This method returns mutable reference to the request head.
/// panics if multiple references of http request exists.
#[inline]
pub fn head_mut(&mut self) -> &mut RequestHead {
@@ -131,7 +131,7 @@ mod tests {
use super::*;
#[test]
fn test_reqest() {
fn test_request() {
let buf = TestBuffer::empty();
let framed = Framed::new(buf, Codec::default());
let req = TestRequest::with_uri("/index.html?q=1")

View File

@@ -47,7 +47,8 @@ async fn test_simple() {
)
.finish(|_| future::ok::<_, Error>(Response::NotFound()))
.tcp()
});
})
.await;
assert!(srv.ws_at("/test").await.is_err());
@@ -108,7 +109,8 @@ async fn test_service() {
.map_err(|_| ()),
),
)
});
})
.await;
// non ws request
let res = srv.get("/index.html").send().await.unwrap();

View File

@@ -1,5 +1,22 @@
# Changes
## [2.0.0-alpha.3] - 2020-05-08
### Fixed
* Correct spelling of ConnectError::Unresolved [#1487]
* Fix a mistake in the encoding of websocket continuation messages wherein
Item::FirstText and Item::FirstBinary are each encoded as the other.
### Changed
* Implement `std::error::Error` for our custom errors [#1422]
* Remove `failure` support for `ResponseError` since that crate
will be deprecated in the near future.
[#1422]: https://github.com/actix/actix-web/pull/1422
[#1487]: https://github.com/actix/actix-web/pull/1487
## [2.0.0-alpha.2] - 2020-03-07
### Changed

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-http"
version = "2.0.0-alpha.2"
version = "2.0.0-alpha.3"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http primitives"
readme = "README.md"
@@ -15,7 +15,7 @@ license = "MIT/Apache-2.0"
edition = "2018"
[package.metadata.docs.rs]
features = ["openssl", "rustls", "failure", "compress", "secure-cookies","actors"]
features = ["openssl", "rustls", "compress", "secure-cookies", "actors"]
[lib]
name = "actix_http"
@@ -33,9 +33,6 @@ rustls = ["actix-tls/rustls", "actix-connect/rustls"]
# enable compressison support
compress = ["flate2", "brotli2"]
# failure integration. actix does not use failure anymore
failure = ["fail-ure"]
# support for secure cookies
secure-cookies = ["ring"]
@@ -45,7 +42,7 @@ actors = ["actix"]
[dependencies]
actix-service = "1.0.5"
actix-codec = "0.2.0"
actix-connect = "2.0.0-alpha.1"
actix-connect = "2.0.0-alpha.3"
actix-utils = "1.0.6"
actix-rt = "1.0.0"
actix-threadpool = "0.3.1"
@@ -89,12 +86,9 @@ ring = { version = "0.16.9", optional = true }
brotli2 = { version="0.3.2", optional = true }
flate2 = { version = "1.0.13", optional = true }
# optional deps
fail-ure = { version = "0.1.5", package="failure", optional = true }
[dev-dependencies]
actix-server = "1.0.1"
actix-connect = { version = "2.0.0-alpha.1", features=["openssl"] }
actix-connect = { version = "2.0.0-alpha.2", features=["openssl"] }
actix-http-test = { version = "1.0.0", features=["openssl"] }
actix-tls = { version = "2.0.0-alpha.1", features=["openssl"] }
criterion = "0.3"
@@ -107,3 +101,7 @@ rust-tls = { version="0.17", package = "rustls" }
[[bench]]
name = "content-length"
harness = false
[[bench]]
name = "status-line"
harness = false

View File

@@ -0,0 +1,222 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use bytes::BytesMut;
use http::Version;
const CODES: &[u16] = &[201, 303, 404, 515];
fn bench_write_status_line_11(c: &mut Criterion) {
let mut group = c.benchmark_group("write_status_line v1.1");
let version = Version::HTTP_11;
for i in CODES.iter() {
group.bench_with_input(BenchmarkId::new("Original (unsafe)", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_original::write_status_line(version, i, &mut b);
})
});
group.bench_with_input(BenchmarkId::new("New (safe)", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_new::write_status_line(version, i, &mut b);
})
});
group.bench_with_input(BenchmarkId::new("Naive", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_naive::write_status_line(version, i, &mut b);
})
});
}
group.finish();
}
fn bench_write_status_line_10(c: &mut Criterion) {
let mut group = c.benchmark_group("write_status_line v1.0");
let version = Version::HTTP_10;
for i in CODES.iter() {
group.bench_with_input(BenchmarkId::new("Original (unsafe)", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_original::write_status_line(version, i, &mut b);
})
});
group.bench_with_input(BenchmarkId::new("New (safe)", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_new::write_status_line(version, i, &mut b);
})
});
group.bench_with_input(BenchmarkId::new("Naive", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_naive::write_status_line(version, i, &mut b);
})
});
}
group.finish();
}
fn bench_write_status_line_09(c: &mut Criterion) {
let mut group = c.benchmark_group("write_status_line v0.9");
let version = Version::HTTP_09;
for i in CODES.iter() {
group.bench_with_input(BenchmarkId::new("Original (unsafe)", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_original::write_status_line(version, i, &mut b);
})
});
group.bench_with_input(BenchmarkId::new("New (safe)", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_new::write_status_line(version, i, &mut b);
})
});
group.bench_with_input(BenchmarkId::new("Naive", i), i, |b, &i| {
b.iter(|| {
let mut b = BytesMut::with_capacity(35);
_naive::write_status_line(version, i, &mut b);
})
});
}
group.finish();
}
criterion_group!(
benches,
bench_write_status_line_11,
bench_write_status_line_10,
bench_write_status_line_09
);
criterion_main!(benches);
mod _naive {
use bytes::{BufMut, BytesMut};
use http::Version;
pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut) {
match version {
Version::HTTP_11 => bytes.put_slice(b"HTTP/1.1 "),
Version::HTTP_10 => bytes.put_slice(b"HTTP/1.0 "),
Version::HTTP_09 => bytes.put_slice(b"HTTP/0.9 "),
_ => {
// other HTTP version handlers do not use this method
}
}
bytes.put_slice(n.to_string().as_bytes());
}
}
mod _new {
use bytes::{BufMut, BytesMut};
use http::Version;
const DIGITS_START: u8 = b'0';
pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut) {
match version {
Version::HTTP_11 => bytes.put_slice(b"HTTP/1.1 "),
Version::HTTP_10 => bytes.put_slice(b"HTTP/1.0 "),
Version::HTTP_09 => bytes.put_slice(b"HTTP/0.9 "),
_ => {
// other HTTP version handlers do not use this method
}
}
let d100 = (n / 100) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1);
bytes.put_u8(b' ');
}
}
mod _original {
use std::ptr;
use bytes::{BufMut, BytesMut};
use http::Version;
const DEC_DIGITS_LUT: &[u8] = b"0001020304050607080910111213141516171819\
2021222324252627282930313233343536373839\
4041424344454647484950515253545556575859\
6061626364656667686970717273747576777879\
8081828384858687888990919293949596979899";
pub(crate) const STATUS_LINE_BUF_SIZE: usize = 13;
pub(crate) fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesMut) {
let mut buf: [u8; STATUS_LINE_BUF_SIZE] = *b"HTTP/1.1 ";
match version {
Version::HTTP_2 => buf[5] = b'2',
Version::HTTP_10 => buf[7] = b'0',
Version::HTTP_09 => {
buf[5] = b'0';
buf[7] = b'9';
}
_ => (),
}
let mut curr: isize = 12;
let buf_ptr = buf.as_mut_ptr();
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
let four = n > 999;
// decode 2 more chars, if > 2 chars
let d1 = (n % 100) << 1;
n /= 100;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(
lut_ptr.offset(d1 as isize),
buf_ptr.offset(curr),
2,
);
}
// decode last 1 or 2 chars
if n < 10 {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = (n as u8) + b'0';
}
} else {
let d1 = n << 1;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(
lut_ptr.offset(d1 as isize),
buf_ptr.offset(curr),
2,
);
}
}
bytes.put_slice(&buf);
if four {
bytes.put_u8(b' ');
}
}
}

View File

@@ -48,20 +48,22 @@ pub enum ConnectError {
/// Unresolved host name
#[display(fmt = "Connector received `Connect` method with unresolved host")]
Unresolverd,
Unresolved,
/// Connection io error
#[display(fmt = "{}", _0)]
Io(io::Error),
}
impl std::error::Error for ConnectError {}
impl From<actix_connect::ConnectError> for ConnectError {
fn from(err: actix_connect::ConnectError) -> ConnectError {
match err {
actix_connect::ConnectError::Resolver(e) => ConnectError::Resolver(e),
actix_connect::ConnectError::NoRecords => ConnectError::NoRecords,
actix_connect::ConnectError::InvalidInput => panic!(),
actix_connect::ConnectError::Unresolverd => ConnectError::Unresolverd,
actix_connect::ConnectError::Unresolved => ConnectError::Unresolved,
actix_connect::ConnectError::Io(e) => ConnectError::Io(e),
}
}
@@ -86,6 +88,8 @@ pub enum InvalidUrl {
HttpError(http::Error),
}
impl std::error::Error for InvalidUrl {}
/// A set of errors that can occur during request sending and response reading
#[derive(Debug, Display, From)]
pub enum SendRequestError {
@@ -115,6 +119,8 @@ pub enum SendRequestError {
Body(Error),
}
impl std::error::Error for SendRequestError {}
/// Convert `SendRequestError` to a server `Response`
impl ResponseError for SendRequestError {
fn status_code(&self) -> StatusCode {
@@ -139,6 +145,8 @@ pub enum FreezeRequestError {
Http(HttpError),
}
impl std::error::Error for FreezeRequestError {}
impl From<FreezeRequestError> for SendRequestError {
fn from(e: FreezeRequestError) -> Self {
match e {

View File

@@ -105,7 +105,7 @@ where
let key = if let Some(authority) = req.uri.authority() {
authority.clone().into()
} else {
return Err(ConnectError::Unresolverd);
return Err(ConnectError::Unresolved);
};
// acquire connection
@@ -195,7 +195,7 @@ where
if let Some(i) = self.inner.take() {
let mut inner = i.as_ref().borrow_mut();
inner.release_waiter(&self.key, self.token);
inner.check_availibility();
inner.check_availability();
}
}
}
@@ -232,7 +232,7 @@ where
if let Some(i) = self.inner.take() {
let mut inner = i.as_ref().borrow_mut();
inner.release();
inner.check_availibility();
inner.check_availability();
}
}
}
@@ -359,7 +359,7 @@ where
created,
used: Instant::now(),
});
self.check_availibility();
self.check_availability();
}
fn release_close(&mut self, io: ConnectionType<Io>) {
@@ -369,10 +369,10 @@ where
actix_rt::spawn(CloseConnection::new(io, timeout))
}
}
self.check_availibility();
self.check_availability();
}
fn check_availibility(&self) {
fn check_availability(&self) {
if !self.waiters_queue.is_empty() && self.acquired < self.config.limit {
self.waker.wake();
}
@@ -534,7 +534,7 @@ where
if let Some(inner) = self.project().inner.take() {
let mut inner = inner.as_ref().borrow_mut();
inner.release();
inner.check_availibility();
inner.check_availability();
}
}
}

View File

@@ -114,7 +114,7 @@ impl ServiceConfig {
}
#[inline]
/// Return state of connection keep-alive funcitonality
/// Return state of connection keep-alive functionality
pub fn keep_alive_enabled(&self) -> bool {
self.0.ka_enabled
}
@@ -214,7 +214,7 @@ impl Date {
write!(
self,
"{}",
OffsetDateTime::now().format("%a, %d %b %Y %H:%M:%S GMT")
OffsetDateTime::now_utc().format("%a, %d %b %Y %H:%M:%S GMT")
)
.unwrap();
}

View File

@@ -63,7 +63,7 @@ impl CookieBuilder {
/// use actix_http::cookie::Cookie;
///
/// let c = Cookie::build("foo", "bar")
/// .expires(time::OffsetDateTime::now())
/// .expires(time::OffsetDateTime::now_utc())
/// .finish();
///
/// assert!(c.expires().is_some());

View File

@@ -13,7 +13,7 @@ use super::secure::{Key, PrivateJar, SignedJar};
///
/// A `CookieJar` provides storage for any number of cookies. Any changes made
/// to the jar are tracked; the changes can be retrieved via the
/// [delta](#method.delta) method which returns an interator over the changes.
/// [delta](#method.delta) method which returns an iterator over the changes.
///
/// # Usage
///
@@ -221,7 +221,7 @@ impl CookieJar {
if self.original_cookies.contains(cookie.name()) {
cookie.set_value("");
cookie.set_max_age(Duration::zero());
cookie.set_expires(OffsetDateTime::now() - Duration::days(365));
cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
self.delta_cookies.replace(DeltaCookie::removed(cookie));
} else {
self.delta_cookies.remove(cookie.name());

View File

@@ -103,7 +103,7 @@ enum CookieStr {
impl CookieStr {
/// Retrieves the string `self` corresponds to. If `self` is derived from
/// indexes, the corresponding subslice of `string` is returned. Otherwise,
/// indexes, the corresponding sub-slice of `string` is returned. Otherwise,
/// the concrete string is returned.
///
/// # Panics
@@ -733,7 +733,7 @@ impl<'c> Cookie<'c> {
pub fn make_permanent(&mut self) {
let twenty_years = Duration::days(365 * 20);
self.set_max_age(twenty_years);
self.set_expires(OffsetDateTime::now() + twenty_years);
self.set_expires(OffsetDateTime::now_utc() + twenty_years);
}
fn fmt_parameters(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@@ -84,7 +84,7 @@ impl Key {
}
/// Generates signing/encryption keys from a secure, random source. Keys are
/// generated nondeterministically.
/// generated non-deterministically.
///
/// # Panics
///
@@ -103,7 +103,7 @@ impl Key {
}
/// Attempts to generate signing/encryption keys from a secure, random
/// source. Keys are generated nondeterministically. If randomness cannot be
/// source. Keys are generated non-deterministically. If randomness cannot be
/// retrieved from the underlying operating system, returns `None`.
///
/// # Example

View File

@@ -18,7 +18,7 @@ use serde::de::value::Error as DeError;
use serde_json::error::Error as JsonError;
use serde_urlencoded::ser::Error as FormError;
// re-export for convinience
// re-export for convenience
use crate::body::Body;
pub use crate::cookie::ParseError as CookieParseError;
use crate::helpers::Writer;
@@ -34,7 +34,7 @@ pub type Result<T, E = Error> = result::Result<T, E>;
/// General purpose actix web error.
///
/// An actix web error is used to carry errors from `failure` or `std::error`
/// An actix web error is used to carry errors from `std::error`
/// through actix in a convenient way. It can be created through
/// converting errors with `into()`.
///
@@ -333,6 +333,8 @@ pub enum PayloadError {
Io(io::Error),
}
impl std::error::Error for PayloadError {}
impl From<h2::Error> for PayloadError {
fn from(err: h2::Error) -> Self {
PayloadError::Http2Payload(err)
@@ -430,7 +432,7 @@ pub enum DispatchError {
Unknown,
}
/// A set of error that can occure during parsing content type
/// A set of error that can occur during parsing content type
#[derive(PartialEq, Debug, Display)]
pub enum ContentTypeError {
/// Can not parse content type
@@ -441,6 +443,8 @@ pub enum ContentTypeError {
UnknownEncoding,
}
impl std::error::Error for ContentTypeError {}
/// Return `BadRequest` for `ContentTypeError`
impl ResponseError for ContentTypeError {
fn status_code(&self) -> StatusCode {
@@ -946,10 +950,6 @@ where
InternalError::new(err, StatusCode::NETWORK_AUTHENTICATION_REQUIRED).into()
}
#[cfg(feature = "failure")]
/// Compatibility for `failure::Error`
impl ResponseError for fail_ure::Error {}
#[cfg(feature = "actors")]
/// `InternalServerError` for `actix::MailboxError`
/// This is supported on feature=`actors` only

View File

@@ -6,6 +6,8 @@ use fxhash::FxHashMap;
#[derive(Default)]
/// A type map of request extensions.
pub struct Extensions {
/// Use FxHasher with a std HashMap with for faster
/// lookups on the small `TypeId` (u64 equivalent) keys.
map: FxHashMap<TypeId, Box<dyn Any>>,
}
@@ -67,108 +69,113 @@ impl fmt::Debug for Extensions {
}
}
#[test]
fn test_remove() {
let mut map = Extensions::new();
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_remove() {
let mut map = Extensions::new();
map.insert::<i8>(123);
assert!(map.get::<i8>().is_some());
map.insert::<i8>(123);
assert!(map.get::<i8>().is_some());
map.remove::<i8>();
assert!(map.get::<i8>().is_none());
}
#[test]
fn test_clear() {
let mut map = Extensions::new();
map.insert::<i8>(8);
map.insert::<i16>(16);
map.insert::<i32>(32);
assert!(map.contains::<i8>());
assert!(map.contains::<i16>());
assert!(map.contains::<i32>());
map.clear();
assert!(!map.contains::<i8>());
assert!(!map.contains::<i16>());
assert!(!map.contains::<i32>());
map.insert::<i8>(10);
assert_eq!(*map.get::<i8>().unwrap(), 10);
}
#[test]
fn test_integers() {
let mut map = Extensions::new();
map.insert::<i8>(8);
map.insert::<i16>(16);
map.insert::<i32>(32);
map.insert::<i64>(64);
map.insert::<i128>(128);
map.insert::<u8>(8);
map.insert::<u16>(16);
map.insert::<u32>(32);
map.insert::<u64>(64);
map.insert::<u128>(128);
assert!(map.get::<i8>().is_some());
assert!(map.get::<i16>().is_some());
assert!(map.get::<i32>().is_some());
assert!(map.get::<i64>().is_some());
assert!(map.get::<i128>().is_some());
assert!(map.get::<u8>().is_some());
assert!(map.get::<u16>().is_some());
assert!(map.get::<u32>().is_some());
assert!(map.get::<u64>().is_some());
assert!(map.get::<u128>().is_some());
}
#[test]
fn test_composition() {
struct Magi<T>(pub T);
struct Madoka {
pub god: bool,
map.remove::<i8>();
assert!(map.get::<i8>().is_none());
}
struct Homura {
pub attempts: usize,
#[test]
fn test_clear() {
let mut map = Extensions::new();
map.insert::<i8>(8);
map.insert::<i16>(16);
map.insert::<i32>(32);
assert!(map.contains::<i8>());
assert!(map.contains::<i16>());
assert!(map.contains::<i32>());
map.clear();
assert!(!map.contains::<i8>());
assert!(!map.contains::<i16>());
assert!(!map.contains::<i32>());
map.insert::<i8>(10);
assert_eq!(*map.get::<i8>().unwrap(), 10);
}
struct Mami {
pub guns: usize,
#[test]
fn test_integers() {
let mut map = Extensions::new();
map.insert::<i8>(8);
map.insert::<i16>(16);
map.insert::<i32>(32);
map.insert::<i64>(64);
map.insert::<i128>(128);
map.insert::<u8>(8);
map.insert::<u16>(16);
map.insert::<u32>(32);
map.insert::<u64>(64);
map.insert::<u128>(128);
assert!(map.get::<i8>().is_some());
assert!(map.get::<i16>().is_some());
assert!(map.get::<i32>().is_some());
assert!(map.get::<i64>().is_some());
assert!(map.get::<i128>().is_some());
assert!(map.get::<u8>().is_some());
assert!(map.get::<u16>().is_some());
assert!(map.get::<u32>().is_some());
assert!(map.get::<u64>().is_some());
assert!(map.get::<u128>().is_some());
}
let mut map = Extensions::new();
#[test]
fn test_composition() {
struct Magi<T>(pub T);
map.insert(Magi(Madoka { god: false }));
map.insert(Magi(Homura { attempts: 0 }));
map.insert(Magi(Mami { guns: 999 }));
struct Madoka {
pub god: bool,
}
assert!(!map.get::<Magi<Madoka>>().unwrap().0.god);
assert_eq!(0, map.get::<Magi<Homura>>().unwrap().0.attempts);
assert_eq!(999, map.get::<Magi<Mami>>().unwrap().0.guns);
}
#[test]
fn test_extensions() {
#[derive(Debug, PartialEq)]
struct MyType(i32);
let mut extensions = Extensions::new();
extensions.insert(5i32);
extensions.insert(MyType(10));
assert_eq!(extensions.get(), Some(&5i32));
assert_eq!(extensions.get_mut(), Some(&mut 5i32));
assert_eq!(extensions.remove::<i32>(), Some(5i32));
assert!(extensions.get::<i32>().is_none());
assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));
struct Homura {
pub attempts: usize,
}
struct Mami {
pub guns: usize,
}
let mut map = Extensions::new();
map.insert(Magi(Madoka { god: false }));
map.insert(Magi(Homura { attempts: 0 }));
map.insert(Magi(Mami { guns: 999 }));
assert!(!map.get::<Magi<Madoka>>().unwrap().0.god);
assert_eq!(0, map.get::<Magi<Homura>>().unwrap().0.attempts);
assert_eq!(999, map.get::<Magi<Mami>>().unwrap().0.guns);
}
#[test]
fn test_extensions() {
#[derive(Debug, PartialEq)]
struct MyType(i32);
let mut extensions = Extensions::new();
extensions.insert(5i32);
extensions.insert(MyType(10));
assert_eq!(extensions.get(), Some(&5i32));
assert_eq!(extensions.get_mut(), Some(&mut 5i32));
assert_eq!(extensions.remove::<i32>(), Some(5i32));
assert!(extensions.get::<i32>().is_none());
assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));
}
}

View File

@@ -18,7 +18,7 @@ use crate::request::Request;
const MAX_BUFFER_SIZE: usize = 131_072;
const MAX_HEADERS: usize = 96;
/// Incoming messagd decoder
/// Incoming message decoder
pub(crate) struct MessageDecoder<T: MessageType>(PhantomData<T>);
#[derive(Debug)]

View File

@@ -297,8 +297,8 @@ where
/// Flush stream
///
/// true - got whouldblock
/// false - didnt get whouldblock
/// true - got WouldBlock
/// false - didn't get WouldBlock
#[pin_project::project]
fn poll_flush(
self: Pin<&mut Self>,
@@ -812,7 +812,7 @@ where
return self.poll(cx);
}
// we didnt get WouldBlock from write operation,
// we didn't get WouldBlock from write operation,
// so data get written to kernel completely (OSX)
// and we have to write again otherwise response can get stuck
if inner.as_mut().poll_flush(cx)? || !drain {

View File

@@ -90,40 +90,40 @@ pub enum DispositionParam {
/// [RFC6266](https://tools.ietf.org/html/rfc6266) as *token "=" value*. Recipients should
/// ignore unrecognizable parameters.
Unknown(String, String),
/// An unrecognized extended paramater as defined in
/// An unrecognized extended parameter as defined in
/// [RFC5987](https://tools.ietf.org/html/rfc5987) as *ext-parameter*, in
/// [RFC6266](https://tools.ietf.org/html/rfc6266) as *ext-token "=" ext-value*. The single
/// trailling asterisk is not included. Recipients should ignore unrecognizable parameters.
/// trailing asterisk is not included. Recipients should ignore unrecognizable parameters.
UnknownExt(String, ExtendedValue),
}
impl DispositionParam {
/// Returns `true` if the paramater is [`Name`](DispositionParam::Name).
/// Returns `true` if the parameter is [`Name`](DispositionParam::Name).
#[inline]
pub fn is_name(&self) -> bool {
self.as_name().is_some()
}
/// Returns `true` if the paramater is [`Filename`](DispositionParam::Filename).
/// Returns `true` if the parameter is [`Filename`](DispositionParam::Filename).
#[inline]
pub fn is_filename(&self) -> bool {
self.as_filename().is_some()
}
/// Returns `true` if the paramater is [`FilenameExt`](DispositionParam::FilenameExt).
/// Returns `true` if the parameter is [`FilenameExt`](DispositionParam::FilenameExt).
#[inline]
pub fn is_filename_ext(&self) -> bool {
self.as_filename_ext().is_some()
}
/// Returns `true` if the paramater is [`Unknown`](DispositionParam::Unknown) and the `name`
/// Returns `true` if the parameter is [`Unknown`](DispositionParam::Unknown) and the `name`
#[inline]
/// matches.
pub fn is_unknown<T: AsRef<str>>(&self, name: T) -> bool {
self.as_unknown(name).is_some()
}
/// Returns `true` if the paramater is [`UnknownExt`](DispositionParam::UnknownExt) and the
/// Returns `true` if the parameter is [`UnknownExt`](DispositionParam::UnknownExt) and the
/// `name` matches.
#[inline]
pub fn is_unknown_ext<T: AsRef<str>>(&self, name: T) -> bool {
@@ -373,7 +373,7 @@ impl ContentDisposition {
let param = if param_name.eq_ignore_ascii_case("name") {
DispositionParam::Name(value)
} else if param_name.eq_ignore_ascii_case("filename") {
// See also comments in test_from_raw_uncessary_percent_decode.
// See also comments in test_from_raw_unnecessary_percent_decode.
DispositionParam::Filename(value)
} else {
DispositionParam::Unknown(param_name.to_owned(), value)
@@ -529,7 +529,7 @@ impl fmt::Display for DispositionParam {
// ; to use within parameter values
//
//
// See also comments in test_from_raw_uncessary_percent_decode.
// See also comments in test_from_raw_unnecessary_percent_decode.
lazy_static! {
static ref RE: Regex = Regex::new("[\x00-\x08\x10-\x1F\x7F\"\\\\]").unwrap();
}
@@ -676,7 +676,7 @@ mod tests {
fn test_from_raw_unordered() {
let a = HeaderValue::from_static(
"form-data; dummy=3; filename=\"sample.png\" ; name=upload;",
// Actually, a trailling semolocon is not compliant. But it is fine to accept.
// Actually, a trailing semicolon is not compliant. But it is fine to accept.
);
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let b = ContentDisposition {
@@ -833,7 +833,7 @@ mod tests {
}
#[test]
fn test_from_raw_uncessary_percent_decode() {
fn test_from_raw_unnecessary_percent_decode() {
// In fact, RFC7578 (multipart/form-data) Section 2 and 4.2 suggests that filename with
// non-ASCII characters MAY be percent-encoded.
// On the contrary, RFC6266 or other RFCs related to Content-Disposition response header

View File

@@ -7,7 +7,7 @@ use header::{Header, Raw};
/// `Range` header, defined in [RFC7233](https://tools.ietf.org/html/rfc7233#section-3.1)
///
/// The "Range" header field on a GET request modifies the method
/// semantics to request transfer of only one or more subranges of the
/// semantics to request transfer of only one or more sub-ranges of the
/// selected representation data, rather than the entire selected
/// representation data.
///
@@ -183,13 +183,13 @@ impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Range::Bytes(ref ranges) => {
try!(write!(f, "bytes="));
write!(f, "bytes=")?;
for (i, range) in ranges.iter().enumerate() {
if i != 0 {
try!(f.write_str(","));
f.write_str(",")?;
}
try!(Display::fmt(range, f));
Display::fmt(range, f)?;
}
Ok(())
}
@@ -214,9 +214,9 @@ impl FromStr for Range {
}
Ok(Range::Bytes(ranges))
}
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => Ok(
Range::Unregistered(unit.to_owned(), range_str.to_owned()),
),
(Some(unit), Some(range_str)) if unit != "" && range_str != "" => {
Ok(Range::Unregistered(unit.to_owned(), range_str.to_owned()))
}
_ => Err(::Error::Header),
}
}
@@ -229,7 +229,8 @@ impl FromStr for ByteRangeSpec {
let mut parts = s.splitn(2, '-');
match (parts.next(), parts.next()) {
(Some(""), Some(end)) => end.parse()
(Some(""), Some(end)) => end
.parse()
.or(Err(::Error::Header))
.map(ByteRangeSpec::Last),
(Some(start), Some("")) => start
@@ -272,163 +273,138 @@ impl Header for Range {
}
}
#[test]
fn test_parse_bytes_range_valid() {
let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap();
let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap();
let r3 = Range::bytes(1, 100);
assert_eq!(r, r2);
assert_eq!(r2, r3);
#[cfg(test)]
mod tests {
use super::*;
let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap();
let r2: Range =
Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
let r3 = Range::Bytes(vec![
ByteRangeSpec::FromTo(1, 100),
ByteRangeSpec::AllFrom(200),
]);
assert_eq!(r, r2);
assert_eq!(r2, r3);
#[test]
fn test_parse_bytes_range_valid() {
let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap();
let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap();
let r3 = Range::bytes(1, 100);
assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"bytes=1-100,-100".into()).unwrap();
let r2: Range = Header::parse_header(&"bytes=1-100, ,,-100".into()).unwrap();
let r3 = Range::Bytes(vec![
ByteRangeSpec::FromTo(1, 100),
ByteRangeSpec::Last(100),
]);
assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap();
let r2: Range =
Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
let r3 = Range::Bytes(vec![
ByteRangeSpec::FromTo(1, 100),
ByteRangeSpec::AllFrom(200),
]);
assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2);
}
#[test]
fn test_parse_unregistered_range_valid() {
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2);
let r: Range = Header::parse_header(&"custom=abcd".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned());
assert_eq!(r, r2);
let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
assert_eq!(r, r2);
}
#[test]
fn test_parse_invalid() {
let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=1-2-3".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"abc".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=1-100=".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"custom=".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"=1-100".into());
assert_eq!(r.ok(), None);
}
#[test]
fn test_fmt() {
use header::Headers;
let mut headers = Headers::new();
headers.set(Range::Bytes(vec![
ByteRangeSpec::FromTo(0, 1000),
ByteRangeSpec::AllFrom(2000),
]));
assert_eq!(&headers.to_string(), "Range: bytes=0-1000,2000-\r\n");
headers.clear();
headers.set(Range::Bytes(vec![]));
assert_eq!(&headers.to_string(), "Range: bytes=\r\n");
headers.clear();
headers.set(Range::Unregistered(
"custom".to_owned(),
"1-xxx".to_owned(),
));
assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
}
#[test]
fn test_byte_range_spec_to_satisfiable_range() {
assert_eq!(
Some((0, 0)),
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)
);
assert_eq!(
Some((1, 2)),
ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3)
);
assert_eq!(
Some((1, 2)),
ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0)
);
assert_eq!(
Some((0, 2)),
ByteRangeSpec::AllFrom(0).to_satisfiable_range(3)
);
assert_eq!(
Some((2, 2)),
ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::AllFrom(3).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::AllFrom(5).to_satisfiable_range(3)
);
assert_eq!(
None,
ByteRangeSpec::AllFrom(0).to_satisfiable_range(0)
);
assert_eq!(
Some((1, 2)),
ByteRangeSpec::Last(2).to_satisfiable_range(3)
);
assert_eq!(
Some((2, 2)),
ByteRangeSpec::Last(1).to_satisfiable_range(3)
);
assert_eq!(
Some((0, 2)),
ByteRangeSpec::Last(5).to_satisfiable_range(3)
);
assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0));
let r: Range = Header::parse_header(&"bytes=1-100,-100".into()).unwrap();
let r2: Range = Header::parse_header(&"bytes=1-100, ,,-100".into()).unwrap();
let r3 = Range::Bytes(vec![
ByteRangeSpec::FromTo(1, 100),
ByteRangeSpec::Last(100),
]);
assert_eq!(r, r2);
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2);
}
#[test]
fn test_parse_unregistered_range_valid() {
let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned());
assert_eq!(r, r2);
let r: Range = Header::parse_header(&"custom=abcd".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned());
assert_eq!(r, r2);
let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap();
let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned());
assert_eq!(r, r2);
}
#[test]
fn test_parse_invalid() {
let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=1-2-3".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"abc".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=1-100=".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"bytes=".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"custom=".into());
assert_eq!(r.ok(), None);
let r: ::Result<Range> = Header::parse_header(&"=1-100".into());
assert_eq!(r.ok(), None);
}
#[test]
fn test_fmt() {
use header::Headers;
let mut headers = Headers::new();
headers.set(Range::Bytes(vec![
ByteRangeSpec::FromTo(0, 1000),
ByteRangeSpec::AllFrom(2000),
]));
assert_eq!(&headers.to_string(), "Range: bytes=0-1000,2000-\r\n");
headers.clear();
headers.set(Range::Bytes(vec![]));
assert_eq!(&headers.to_string(), "Range: bytes=\r\n");
headers.clear();
headers.set(Range::Unregistered("custom".to_owned(), "1-xxx".to_owned()));
assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n");
}
#[test]
fn test_byte_range_spec_to_satisfiable_range() {
assert_eq!(
Some((0, 0)),
ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)
);
assert_eq!(
Some((1, 2)),
ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3)
);
assert_eq!(
Some((1, 2)),
ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3)
);
assert_eq!(None, ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0));
assert_eq!(
Some((0, 2)),
ByteRangeSpec::AllFrom(0).to_satisfiable_range(3)
);
assert_eq!(
Some((2, 2)),
ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)
);
assert_eq!(None, ByteRangeSpec::AllFrom(3).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::AllFrom(5).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::AllFrom(0).to_satisfiable_range(0));
assert_eq!(Some((1, 2)), ByteRangeSpec::Last(2).to_satisfiable_range(3));
assert_eq!(Some((2, 2)), ByteRangeSpec::Last(1).to_satisfiable_range(3));
assert_eq!(Some((0, 2)), ByteRangeSpec::Last(5).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3));
assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0));
}
}

View File

@@ -7,7 +7,7 @@ use http::header::{HeaderName, HeaderValue};
/// A set of HTTP headers
///
/// `HeaderMap` is an multimap of [`HeaderName`] to values.
/// `HeaderMap` is an multi-map of [`HeaderName`] to values.
///
/// [`HeaderName`]: struct.HeaderName.html
#[derive(Debug, Clone)]

View File

@@ -137,17 +137,22 @@ impl FromStr for Charset {
}
}
#[test]
fn test_parse() {
assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-ASCII".parse().unwrap());
assert_eq!(Shift_Jis, "Shift-JIS".parse().unwrap());
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
assert_eq!("US-ASCII", format!("{}", Us_Ascii));
assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned())));
#[test]
fn test_parse() {
assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
assert_eq!(Us_Ascii, "US-ASCII".parse().unwrap());
assert_eq!(Shift_Jis, "Shift-JIS".parse().unwrap());
assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
}
#[test]
fn test_display() {
assert_eq!("US-ASCII", format!("{}", Us_Ascii));
assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned())));
}
}

View File

@@ -1,69 +1,34 @@
use std::{io, ptr};
use std::io;
use bytes::{BufMut, BytesMut};
use http::Version;
use crate::extensions::Extensions;
const DEC_DIGITS_LUT: &[u8] = b"0001020304050607080910111213141516171819\
2021222324252627282930313233343536373839\
4041424344454647484950515253545556575859\
6061626364656667686970717273747576777879\
8081828384858687888990919293949596979899";
pub(crate) const STATUS_LINE_BUF_SIZE: usize = 13;
pub(crate) fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesMut) {
let mut buf: [u8; STATUS_LINE_BUF_SIZE] = *b"HTTP/1.1 ";
match version {
Version::HTTP_2 => buf[5] = b'2',
Version::HTTP_10 => buf[7] = b'0',
Version::HTTP_09 => {
buf[5] = b'0';
buf[7] = b'9';
}
_ => (),
}
let mut curr: isize = 12;
let buf_ptr = buf.as_mut_ptr();
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
let four = n > 999;
// decode 2 more chars, if > 2 chars
let d1 = (n % 100) << 1;
n /= 100;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(curr), 2);
}
// decode last 1 or 2 chars
if n < 10 {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = (n as u8) + b'0';
}
} else {
let d1 = n << 1;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(
lut_ptr.offset(d1 as isize),
buf_ptr.offset(curr),
2,
);
}
}
bytes.put_slice(&buf);
if four {
bytes.put_u8(b' ');
}
}
const DIGITS_START: u8 = b'0';
pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut) {
match version {
Version::HTTP_11 => bytes.put_slice(b"HTTP/1.1 "),
Version::HTTP_10 => bytes.put_slice(b"HTTP/1.0 "),
Version::HTTP_09 => bytes.put_slice(b"HTTP/0.9 "),
_ => {
// other HTTP version handlers do not use this method
}
}
let d100 = (n / 100) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1);
// trailing space before reason
bytes.put_u8(b' ');
}
/// NOTE: bytes object has to contain enough space
pub fn write_content_length(n: usize, bytes: &mut BytesMut) {
bytes.put_slice(b"\r\ncontent-length: ");
@@ -189,8 +154,28 @@ impl<T: Clone + 'static> DataFactory for Data<T> {
#[cfg(test)]
mod tests {
use std::str::from_utf8;
use super::*;
#[test]
fn test_status_line() {
let mut bytes = BytesMut::new();
bytes.reserve(50);
write_status_line(Version::HTTP_11, 200, &mut bytes);
assert_eq!(from_utf8(&bytes.split().freeze()).unwrap(), "HTTP/1.1 200 ");
let mut bytes = BytesMut::new();
bytes.reserve(50);
write_status_line(Version::HTTP_09, 404, &mut bytes);
assert_eq!(from_utf8(&bytes.split().freeze()).unwrap(), "HTTP/0.9 404 ");
let mut bytes = BytesMut::new();
bytes.reserve(50);
write_status_line(Version::HTTP_09, 515, &mut bytes);
assert_eq!(from_utf8(&bytes.split().freeze()).unwrap(), "HTTP/0.9 515 ");
}
#[test]
fn test_write_content_length() {
let mut bytes = BytesMut::new();

View File

@@ -99,13 +99,13 @@ impl RequestHead {
}
/// Is to uppercase headers with Camel-Case.
/// Befault is `false`
/// Default is `false`
#[inline]
pub fn camel_case_headers(&self) -> bool {
self.flags.contains(Flags::CAMEL_CASE)
}
/// Set `true` to send headers which are uppercased with Camel-Case.
/// Set `true` to send headers which are formatted as Camel-Case.
#[inline]
pub fn set_camel_case_headers(&mut self, val: bool) {
if val {

View File

@@ -19,7 +19,7 @@ fn try_parse_rfc_850(time: &str) -> Option<PrimitiveDateTime> {
// If the `time` string contains a two-digit year, then as per RFC 2616 § 19.3,
// we consider the year as part of this century if it's within the next 50 years,
// otherwise we consider as part of the previous century.
let now = OffsetDateTime::now();
let now = OffsetDateTime::now_utc();
let century_start_year = (now.year() / 100) * 100;
let mut expanded_year = century_start_year + dt.year();

View File

@@ -137,7 +137,7 @@ impl Encoder for Codec {
Parser::write_message(
dst,
&data[..],
OpCode::Binary,
OpCode::Text,
false,
!self.flags.contains(Flags::SERVER),
)
@@ -151,7 +151,7 @@ impl Encoder for Codec {
Parser::write_message(
dst,
&data[..],
OpCode::Text,
OpCode::Binary,
false,
!self.flags.contains(Flags::SERVER),
)

View File

@@ -58,6 +58,8 @@ pub enum ProtocolError {
Io(io::Error),
}
impl std::error::Error for ProtocolError {}
impl ResponseError for ProtocolError {}
/// Websocket handshake errors
@@ -108,7 +110,7 @@ impl ResponseError for HandshakeError {
}
}
/// Verify `WebSocket` handshake request and create handshake reponse.
/// Verify `WebSocket` handshake request and create handshake response.
// /// `protocols` is a sequence of known protocols. On successful handshake,
// /// the returned response headers contain the first protocol in this list
// /// which the server also knows.
@@ -168,7 +170,7 @@ pub fn verify_handshake(req: &RequestHead) -> Result<(), HandshakeError> {
Ok(())
}
/// Create websocket's handshake response
/// Create websocket handshake response
///
/// This function returns handshake `Response`, ready to send to peer.
pub fn handshake_response(req: &RequestHead) -> ResponseBuilder {

View File

@@ -203,7 +203,7 @@ impl<T: Into<String>> From<(CloseCode, T)> for CloseReason {
static WS_GUID: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
// TODO: hash is always same size, we dont need String
// TODO: hash is always same size, we don't need String
pub fn hash_key(key: &[u8]) -> String {
use sha1::Digest;
let mut hasher = sha1::Sha1::new();

View File

@@ -33,7 +33,8 @@ async fn test_h1_v2() {
HttpService::build()
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -61,7 +62,8 @@ async fn test_connection_close() {
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
.map(|_| ())
});
})
.await;
let response = srv.get("/").force_close().send().await.unwrap();
assert!(response.status().is_success());
@@ -80,7 +82,8 @@ async fn test_with_query_parameter() {
})
.tcp()
.map(|_| ())
});
})
.await;
let request = srv.request(http::Method::GET, srv.url("/?qp=5"));
let response = request.send().await.unwrap();

View File

@@ -67,7 +67,8 @@ async fn test_h2() -> io::Result<()> {
.h2(|_| ok::<_, Error>(Response::Ok().finish()))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -85,7 +86,8 @@ async fn test_h2_1() -> io::Result<()> {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -103,7 +105,8 @@ async fn test_h2_body() -> io::Result<()> {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send_body(data.clone()).await.unwrap();
assert!(response.status().is_success());
@@ -131,7 +134,8 @@ async fn test_h2_content_length() {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let header = HeaderName::from_static("content-length");
let value = HeaderValue::from_static("0");
@@ -192,7 +196,7 @@ async fn test_h2_headers() {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
}).await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -231,7 +235,8 @@ async fn test_h2_body2() {
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -248,7 +253,8 @@ async fn test_h2_head_empty() {
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -273,7 +279,8 @@ async fn test_h2_head_binary() {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -295,7 +302,8 @@ async fn test_h2_head_binary2() {
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -318,7 +326,8 @@ async fn test_h2_body_length() {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -342,7 +351,8 @@ async fn test_h2_body_chunked_explicit() {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -369,7 +379,8 @@ async fn test_h2_response_http_error_handling() {
}))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
@@ -386,7 +397,8 @@ async fn test_h2_service_error() {
.h2(|_| err::<Response, Error>(ErrorBadRequest("error")))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
@@ -407,7 +419,8 @@ async fn test_h2_on_connect() {
})
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());

View File

@@ -45,7 +45,8 @@ async fn test_h1() -> io::Result<()> {
HttpService::build()
.h1(|_| future::ok::<_, Error>(Response::Ok().finish()))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -58,7 +59,8 @@ async fn test_h2() -> io::Result<()> {
HttpService::build()
.h2(|_| future::ok::<_, Error>(Response::Ok().finish()))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -75,7 +77,8 @@ async fn test_h1_1() -> io::Result<()> {
future::ok::<_, Error>(Response::Ok().finish())
})
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -92,7 +95,8 @@ async fn test_h2_1() -> io::Result<()> {
future::ok::<_, Error>(Response::Ok().finish())
})
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -109,7 +113,8 @@ async fn test_h2_body1() -> io::Result<()> {
Ok::<_, Error>(Response::Ok().body(body))
})
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send_body(data.clone()).await.unwrap();
assert!(response.status().is_success());
@@ -136,7 +141,8 @@ async fn test_h2_content_length() {
future::ok::<_, ()>(Response::new(statuses[indx]))
})
.rustls(ssl_acceptor())
});
})
.await;
let header = HeaderName::from_static("content-length");
let value = HeaderValue::from_static("0");
@@ -195,7 +201,7 @@ async fn test_h2_headers() {
future::ok::<_, ()>(config.body(data.clone()))
})
.rustls(ssl_acceptor())
});
}).await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -233,7 +239,8 @@ async fn test_h2_body2() {
HttpService::build()
.h2(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -249,7 +256,8 @@ async fn test_h2_head_empty() {
HttpService::build()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -276,7 +284,8 @@ async fn test_h2_head_binary() {
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
})
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -300,7 +309,8 @@ async fn test_h2_head_binary2() {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -325,7 +335,8 @@ async fn test_h2_body_length() {
)
})
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -348,7 +359,8 @@ async fn test_h2_body_chunked_explicit() {
)
})
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -376,7 +388,8 @@ async fn test_h2_response_http_error_handling() {
}))
}))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
@@ -392,7 +405,8 @@ async fn test_h2_service_error() {
HttpService::build()
.h2(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
@@ -408,7 +422,8 @@ async fn test_h1_service_error() {
HttpService::build()
.h1(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
.rustls(ssl_acceptor())
});
})
.await;
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);

View File

@@ -27,7 +27,8 @@ async fn test_h1() {
future::ok::<_, ()>(Response::Ok().finish())
})
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -46,7 +47,8 @@ async fn test_h1_2() {
future::ok::<_, ()>(Response::Ok().finish())
})
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -65,7 +67,8 @@ async fn test_expect_continue() {
}))
.finish(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test HTTP/1.1\r\nexpect: 100-continue\r\n\r\n");
@@ -95,7 +98,8 @@ async fn test_expect_continue_h1() {
}))
.h1(fn_service(|_| future::ok::<_, ()>(Response::Ok().finish())))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test HTTP/1.1\r\nexpect: 100-continue\r\n\r\n");
@@ -130,7 +134,8 @@ async fn test_chunked_payload() {
})
}))
.tcp()
});
})
.await;
let returned_size = {
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
@@ -172,7 +177,8 @@ async fn test_slow_request() {
.client_timeout(100)
.finish(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n");
@@ -187,7 +193,8 @@ async fn test_http1_malformed_request() {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test/tests/test HTTP1.1\r\n");
@@ -202,7 +209,8 @@ async fn test_http1_keepalive() {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
@@ -223,7 +231,8 @@ async fn test_http1_keepalive_timeout() {
.keep_alive(1)
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
@@ -243,7 +252,8 @@ async fn test_http1_keepalive_close() {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ =
@@ -263,7 +273,8 @@ async fn test_http10_keepalive_default_close() {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.0\r\n\r\n");
@@ -282,7 +293,8 @@ async fn test_http10_keepalive() {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream
@@ -309,7 +321,8 @@ async fn test_http1_keepalive_disabled() {
.keep_alive(KeepAlive::Disabled)
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
});
})
.await;
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
@@ -344,7 +357,8 @@ async fn test_content_length() {
future::ok::<_, ()>(Response::new(statuses[indx]))
})
.tcp()
});
})
.await;
let header = HeaderName::from_static("content-length");
let value = HeaderValue::from_static("0");
@@ -397,7 +411,7 @@ async fn test_h1_headers() {
}
future::ok::<_, ()>(builder.body(data.clone()))
}).tcp()
});
}).await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -435,7 +449,8 @@ async fn test_h1_body() {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -451,7 +466,8 @@ async fn test_h1_head_empty() {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
});
})
.await;
let response = srv.head("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -477,7 +493,8 @@ async fn test_h1_head_binary() {
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
})
.tcp()
});
})
.await;
let response = srv.head("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -501,7 +518,8 @@ async fn test_h1_head_binary2() {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
});
})
.await;
let response = srv.head("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -526,7 +544,8 @@ async fn test_h1_body_length() {
)
})
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -549,7 +568,8 @@ async fn test_h1_body_chunked_explicit() {
)
})
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -579,7 +599,8 @@ async fn test_h1_body_chunked_implicit() {
ok::<_, ()>(Response::Ok().streaming(body))
})
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
@@ -611,7 +632,8 @@ async fn test_h1_response_http_error_handling() {
)
}))
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
@@ -627,7 +649,8 @@ async fn test_h1_service_error() {
HttpService::build()
.h1(|_| future::err::<Response, Error>(error::ErrorBadRequest("error")))
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
@@ -647,7 +670,8 @@ async fn test_h1_on_connect() {
future::ok::<_, ()>(Response::Ok().finish())
})
.tcp()
});
})
.await;
let response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());

View File

@@ -93,7 +93,8 @@ async fn test_simple() {
.finish(|_| future::ok::<_, ()>(Response::NotFound()))
.tcp()
}
});
})
.await;
// client service
let mut framed = srv.ws().await.unwrap();

View File

@@ -1,5 +1,7 @@
# Identity service for actix web framework [![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-identity)](https://crates.io/crates/actix-identity) [![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)
**This crate moved to https://github.com/actix/actix-extras.**
## Documentation & community resources
* [User Guide](https://actix.rs/docs/)

View File

@@ -16,7 +16,7 @@ name = "actix_multipart"
path = "src/lib.rs"
[dependencies]
actix-web = { version = "2.0.0", default-features = false }
actix-web = { version = "3.0.0-alpha.2", default-features = false }
actix-service = "1.0.1"
actix-utils = "1.0.3"
bytes = "0.5.3"
@@ -29,4 +29,4 @@ twoway = "0.2"
[dev-dependencies]
actix-rt = "1.0.0"
actix-http = "2.0.0-alpha.2"
actix-http = "2.0.0-alpha.3"

View File

@@ -1,5 +1,7 @@
# Session for actix web framework [![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-session)](https://crates.io/crates/actix-session) [![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)
**This crate moved to https://github.com/actix/actix-extras.**
## Documentation & community resources
* [User Guide](https://actix.rs/docs/)

View File

@@ -1,5 +1,11 @@
# Changes
# [3.0.0-alpha.1] - 2020-05-08
* Update the actix-web dependency to 3.0.0-alpha.1
* Update the actix dependency to 0.10.0-alpha.2
* Update the actix-http dependency to 2.0.0-alpha.3
## [2.0.0] - 2019-12-20
* Release

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-web-actors"
version = "2.0.0"
version = "3.0.0-alpha.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix actors support for actix web framework."
readme = "README.md"
@@ -16,9 +16,9 @@ name = "actix_web_actors"
path = "src/lib.rs"
[dependencies]
actix = "0.10.0-alpha.1"
actix-web = "2.0.0"
actix-http = "2.0.0-alpha.2"
actix = "0.10.0-alpha.2"
actix-web = "3.0.0-alpha.2"
actix-http = "2.0.0-alpha.3"
actix-codec = "0.2.0"
bytes = "0.5.2"
futures = "0.3.1"
@@ -26,4 +26,4 @@ pin-project = "0.4.6"
[dev-dependencies]
actix-rt = "1.0.0"
env_logger = "0.6"
env_logger = "0.7"

View File

@@ -174,7 +174,7 @@ where
// frames
if let Some(data) = self.fut.ctx().stream.pop_front() {
Poll::Ready(data.map(|b| Ok(b)))
Poll::Ready(data.map(Ok))
} else if self.fut.alive() {
Poll::Pending
} else {

View File

@@ -18,5 +18,5 @@ proc-macro2 = "^1"
[dev-dependencies]
actix-rt = "1.0.0"
actix-web = "2.0.0"
actix-web = "3.0.0-alpha.2"
futures = "0.3.1"

View File

@@ -21,6 +21,7 @@
//!
//! - `"path"` - Raw literal string with path for which to register handle. Mandatory.
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
//! - `wrap="Middleware"` - Registers a resource middleware.
//!
//! ## Notes
//!
@@ -45,7 +46,6 @@ extern crate proc_macro;
mod route;
use proc_macro::TokenStream;
use syn::parse_macro_input;
/// Creates route handler with `GET` method guard.
///
@@ -55,14 +55,10 @@ use syn::parse_macro_input;
///
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory.
/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
/// - `wrap="Middleware"` - Registers a resource middleware.
#[proc_macro_attribute]
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Get) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Get)
}
/// Creates route handler with `POST` method guard.
@@ -72,12 +68,7 @@ pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [get](attr.get.html)
#[proc_macro_attribute]
pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Post) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Post)
}
/// Creates route handler with `PUT` method guard.
@@ -87,12 +78,7 @@ pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [get](attr.get.html)
#[proc_macro_attribute]
pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Put) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Put)
}
/// Creates route handler with `DELETE` method guard.
@@ -102,12 +88,7 @@ pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [get](attr.get.html)
#[proc_macro_attribute]
pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Delete) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Delete)
}
/// Creates route handler with `HEAD` method guard.
@@ -117,12 +98,7 @@ pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [head](attr.head.html)
#[proc_macro_attribute]
pub fn head(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Head) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Head)
}
/// Creates route handler with `CONNECT` method guard.
@@ -132,12 +108,7 @@ pub fn head(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [connect](attr.connect.html)
#[proc_macro_attribute]
pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Connect) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Connect)
}
/// Creates route handler with `OPTIONS` method guard.
@@ -147,12 +118,7 @@ pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [options](attr.options.html)
#[proc_macro_attribute]
pub fn options(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Options) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Options)
}
/// Creates route handler with `TRACE` method guard.
@@ -162,12 +128,7 @@ pub fn options(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [trace](attr.trace.html)
#[proc_macro_attribute]
pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Trace) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Trace)
}
/// Creates route handler with `PATCH` method guard.
@@ -177,10 +138,5 @@ pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
/// Attributes are the same as in [patch](attr.patch.html)
#[proc_macro_attribute]
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
let gen = match route::Route::new(args, input, route::GuardType::Patch) {
Ok(gen) => gen,
Err(err) => return err.to_compile_error().into(),
};
gen.generate()
route::generate(args, input, route::GuardType::Patch)
}

View File

@@ -2,8 +2,8 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{AttributeArgs, Ident, NestedMeta};
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
use syn::{AttributeArgs, Ident, NestedMeta, parse_macro_input};
enum ResourceType {
Async,
@@ -12,11 +12,7 @@ enum ResourceType {
impl ToTokens for ResourceType {
fn to_tokens(&self, stream: &mut TokenStream2) {
let ident = match self {
ResourceType::Async => "to",
ResourceType::Sync => "to",
};
let ident = Ident::new(ident, Span::call_site());
let ident = format_ident!("to");
stream.append(ident);
}
}
@@ -52,8 +48,7 @@ impl GuardType {
impl ToTokens for GuardType {
fn to_tokens(&self, stream: &mut TokenStream2) {
let ident = self.as_str();
let ident = Ident::new(ident, Span::call_site());
let ident = Ident::new(self.as_str(), Span::call_site());
stream.append(ident);
}
}
@@ -61,12 +56,14 @@ impl ToTokens for GuardType {
struct Args {
path: syn::LitStr,
guards: Vec<Ident>,
wrappers: Vec<syn::Type>,
}
impl Args {
fn new(args: AttributeArgs) -> syn::Result<Self> {
let mut path = None;
let mut guards = Vec::new();
let mut wrappers = Vec::new();
for arg in args {
match arg {
NestedMeta::Lit(syn::Lit::Str(lit)) => match path {
@@ -90,21 +87,31 @@ impl Args {
"Attribute guard expects literal string!",
));
}
} else if nv.path.is_ident("wrap") {
if let syn::Lit::Str(lit) = nv.lit {
wrappers.push(lit.parse()?);
} else {
return Err(syn::Error::new_spanned(
nv.lit,
"Attribute wrap expects type",
));
}
} else {
return Err(syn::Error::new_spanned(
nv.path,
"Unknown attribute key is specified. Allowed: guard",
"Unknown attribute key is specified. Allowed: guard and wrap",
));
}
}
arg => {
return Err(syn::Error::new_spanned(arg, "Unknown attribute"));
return Err(syn::Error::new_spanned(arg, "Unknown attribute."));
}
}
}
Ok(Args {
path: path.unwrap(),
guards,
wrappers,
})
}
}
@@ -181,15 +188,18 @@ impl Route {
guard,
})
}
}
pub fn generate(&self) -> TokenStream {
let name = &self.name;
impl ToTokens for Route {
fn to_tokens(&self, output: &mut TokenStream2) {
let Self {
name,
guard,
ast,
args: Args { path, guards, wrappers },
resource_type,
} = self;
let resource_name = name.to_string();
let guard = &self.guard;
let ast = &self.ast;
let path = &self.args.path;
let extra_guards = &self.args.guards;
let resource_type = &self.resource_type;
let stream = quote! {
#[allow(non_camel_case_types, missing_docs)]
pub struct #name;
@@ -200,13 +210,27 @@ impl Route {
let __resource = actix_web::Resource::new(#path)
.name(#resource_name)
.guard(actix_web::guard::#guard())
#(.guard(actix_web::guard::fn_guard(#extra_guards)))*
#(.guard(actix_web::guard::fn_guard(#guards)))*
#(.wrap(#wrappers))*
.#resource_type(#name);
actix_web::dev::HttpServiceFactory::register(__resource, __config)
}
}
};
stream.into()
output.extend(stream);
}
}
pub(crate) fn generate(
args: TokenStream,
input: TokenStream,
guard: GuardType,
) -> TokenStream {
let args = parse_macro_input!(args as syn::AttributeArgs);
match Route::new(args, input, guard) {
Ok(route) => route.into_token_stream().into(),
Err(err) => err.to_compile_error().into(),
}
}

View File

@@ -1,6 +1,11 @@
use actix_web::{http, test, web::Path, App, HttpResponse, Responder};
use std::pin::Pin;
use std::task::{Context, Poll};
use actix_web::{http, test, web::Path, App, HttpResponse, Responder, Error};
use actix_web::dev::{Service, Transform, ServiceRequest, ServiceResponse};
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, trace};
use futures::{future, Future};
use actix_web::http::header::{HeaderName, HeaderValue};
// Make sure that we can name function as 'config'
#[get("/config")]
@@ -73,6 +78,65 @@ async fn get_param_test(_: Path<String>) -> impl Responder {
HttpResponse::Ok()
}
pub struct ChangeStatusCode;
impl<S, B> Transform<S> for ChangeStatusCode
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = ChangeStatusCodeMiddleware<S>;
type Future = future::Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
future::ok(ChangeStatusCodeMiddleware { service })
}
}
pub struct ChangeStatusCodeMiddleware<S> {
service: S,
}
impl<S, B> Service for ChangeStatusCodeMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, req: ServiceRequest) -> Self::Future {
let fut = self.service.call(req);
Box::pin(async move {
let mut res = fut.await?;
let headers = res.headers_mut();
let header_name = HeaderName::from_lowercase(b"custom-header").unwrap();
let header_value = HeaderValue::from_str("hello").unwrap();
headers.insert(header_name, header_value);
Ok(res)
})
}
}
#[get("/test/wrap", wrap = "ChangeStatusCode")]
async fn get_wrap(_: Path<String>) -> impl Responder {
HttpResponse::Ok()
}
#[actix_rt::test]
async fn test_params() {
let srv = test::start(|| {
@@ -155,3 +219,15 @@ async fn test_auto_async() {
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}
#[actix_rt::test]
async fn test_wrap() {
let srv = test::start(|| {
App::new()
.service(get_wrap)
});
let request = srv.request(http::Method::GET, srv.url("/test/wrap"));
let response = request.send().await.unwrap();
assert!(response.headers().contains_key("custom-header"));
}

View File

@@ -1,9 +1,18 @@
# Changes
## [NEXT]
## [Unreleased]
### Changed
* Implement `std::error::Error` for our custom errors [#1422]
[#1422]: https://github.com/actix/actix-web/pull/1422
## [2.0.0-alpha.1] - 2020-03-11
* Update `actix-http` dependency to 2.0.0-alpha.2
* Update `rustls` dependency to 0.17
* ClientBuilder accepts initial_window_size and initial_connection_window_size HTTP2 configuration
* ClientBuilder allowing to set max_http_version to limit HTTP version to be used
## [1.0.1] - 2019-12-15

View File

@@ -1,6 +1,6 @@
[package]
name = "awc"
version = "1.0.1"
version = "2.0.0-alpha.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http client."
readme = "README.md"
@@ -36,7 +36,7 @@ compress = ["actix-http/compress"]
[dependencies]
actix-codec = "0.2.0"
actix-service = "1.0.1"
actix-http = "2.0.0-alpha.2"
actix-http = "2.0.0-alpha.3"
actix-rt = "1.0.0"
base64 = "0.11"
@@ -50,19 +50,19 @@ rand = "0.7"
serde = "1.0"
serde_json = "1.0"
serde_urlencoded = "0.6.1"
open-ssl = { version="0.10", package="openssl", optional = true }
rust-tls = { version = "0.17.0", package="rustls", optional = true, features = ["dangerous_configuration"] }
open-ssl = { version = "0.10", package = "openssl", optional = true }
rust-tls = { version = "0.17.0", package = "rustls", optional = true, features = ["dangerous_configuration"] }
[dev-dependencies]
actix-connect = { version = "2.0.0-alpha.1", features=["openssl"] }
actix-web = { version = "2.0.0", features=["openssl"] }
actix-http = { version = "2.0.0-alpha.2", features=["openssl"] }
actix-http-test = { version = "1.0.0", features=["openssl"] }
actix-connect = { version = "2.0.0-alpha.2", features = ["openssl"] }
actix-web = { version = "3.0.0-alpha.2", features = ["openssl"] }
actix-http = { version = "2.0.0-alpha.3", features = ["openssl"] }
actix-http-test = { version = "1.0.0", features = ["openssl"] }
actix-utils = "1.0.3"
actix-server = "1.0.0"
actix-tls = { version = "2.0.0-alpha.1", features=["openssl", "rustls"] }
actix-tls = { version = "2.0.0-alpha.1", features = ["openssl", "rustls"] }
brotli2 = "0.3.2"
flate2 = "1.0.13"
futures = "0.3.1"
env_logger = "0.6"
env_logger = "0.7"
webpki = "0.21"

View File

@@ -5,10 +5,10 @@ use std::rc::Rc;
use std::time::Duration;
use actix_http::client::{Connect as HttpConnect, ConnectError, Connection, Connector};
use actix_http::http::{header, Error as HttpError, HeaderMap, HeaderName, self};
use actix_http::http::{self, header, Error as HttpError, HeaderMap, HeaderName};
use actix_service::Service;
use crate::connect::{ConnectorWrapper, Connect};
use crate::connect::{Connect, ConnectorWrapper};
use crate::{Client, ClientConfig};
/// An HTTP Client builder
@@ -182,7 +182,9 @@ impl ClientBuilder {
if let Some(val) = self.stream_window_size {
connector = connector.initial_window_size(val)
};
RefCell::new(Box::new(ConnectorWrapper(connector.finish())) as Box<dyn Connect>)
RefCell::new(
Box::new(ConnectorWrapper(connector.finish())) as Box<dyn Connect>
)
};
let config = ClientConfig {
headers: self.headers,

View File

@@ -42,6 +42,8 @@ pub enum WsClientError {
SendRequest(SendRequestError),
}
impl std::error::Error for WsClientError {}
impl From<InvalidUrl> for WsClientError {
fn from(err: InvalidUrl) -> Self {
WsClientError::SendRequest(err.into())
@@ -68,5 +70,7 @@ pub enum JsonPayloadError {
Payload(PayloadError),
}
impl std::error::Error for JsonPayloadError {}
/// Return `InternalServerError` for `JsonPayloadError`
impl ResponseError for JsonPayloadError {}

View File

@@ -9,7 +9,6 @@ use bytes::Bytes;
use derive_more::From;
use futures_core::{Future, Stream};
use serde::Serialize;
use serde_json;
use actix_http::body::{Body, BodyStream};
use actix_http::http::header::{self, IntoHeaderValue};

View File

@@ -107,17 +107,15 @@ async fn test_form() {
#[actix_rt::test]
async fn test_timeout() {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|| {
async {
actix_rt::time::delay_for(Duration::from_millis(200)).await;
Ok::<_, Error>(HttpResponse::Ok().body(STR))
}
App::new().service(web::resource("/").route(web::to(|| async {
actix_rt::time::delay_for(Duration::from_millis(200)).await;
Ok::<_, Error>(HttpResponse::Ok().body(STR))
})))
});
let connector = awc::Connector::new()
.connector(actix_connect::new_connector(
actix_connect::start_default_resolver(),
actix_connect::start_default_resolver().await.unwrap(),
))
.timeout(Duration::from_secs(15))
.finish();
@@ -137,11 +135,9 @@ async fn test_timeout() {
#[actix_rt::test]
async fn test_timeout_override() {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|| {
async {
actix_rt::time::delay_for(Duration::from_millis(200)).await;
Ok::<_, Error>(HttpResponse::Ok().body(STR))
}
App::new().service(web::resource("/").route(web::to(|| async {
actix_rt::time::delay_for(Duration::from_millis(200)).await;
Ok::<_, Error>(HttpResponse::Ok().body(STR))
})))
});
@@ -177,7 +173,8 @@ async fn test_connection_reuse() {
))
.tcp(),
)
});
})
.await;
let client = awc::Client::default();
@@ -214,7 +211,8 @@ async fn test_connection_force_close() {
))
.tcp(),
)
});
})
.await;
let client = awc::Client::default();
@@ -253,7 +251,8 @@ async fn test_connection_server_close() {
))
.tcp(),
)
});
})
.await;
let client = awc::Client::default();
@@ -291,7 +290,8 @@ async fn test_connection_wait_queue() {
))
.tcp(),
)
});
})
.await;
let client = awc::Client::build()
.connector(awc::Connector::new().limit(1).finish())
@@ -339,7 +339,8 @@ async fn test_connection_wait_queue_force_close() {
))
.tcp(),
)
});
})
.await;
let client = awc::Client::build()
.connector(awc::Connector::new().limit(1).finish())

View File

@@ -32,14 +32,14 @@ async fn test_connection_window_size() {
let srv = test_server(move || {
HttpService::build()
.h2(map_config(
App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok())),
),
App::new()
.service(web::resource("/").route(web::to(|| HttpResponse::Ok()))),
|_| AppConfig::default(),
))
.openssl(ssl_acceptor())
.map_err(|_| ())
});
})
.await;
// disable ssl verification
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();

View File

@@ -72,7 +72,8 @@ async fn _test_connection_reuse_h2() {
.openssl(ssl_acceptor())
.map_err(|_| ()),
)
});
})
.await;
// disable ssl verification
let mut config = ClientConfig::new();

View File

@@ -53,7 +53,8 @@ async fn test_connection_reuse_h2() {
.openssl(ssl_acceptor())
.map_err(|_| ()),
)
});
})
.await;
// disable ssl verification
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();

View File

@@ -38,7 +38,8 @@ async fn test_simple() {
})
.finish(|_| ok::<_, Error>(Response::NotFound()))
.tcp()
});
})
.await;
// client service
let mut framed = srv.ws().await.unwrap();

View File

@@ -10,11 +10,11 @@ use actix_service::boxed::{self, BoxServiceFactory};
use actix_service::{
apply, apply_fn_factory, IntoServiceFactory, ServiceFactory, Transform,
};
use futures::future::{FutureExt, LocalBoxFuture};
use futures::future::FutureExt;
use crate::app_service::{AppEntry, AppInit, AppRoutingFactory};
use crate::config::ServiceConfig;
use crate::data::{Data, DataFactory};
use crate::data::{Data, DataFactory, FnDataFactory};
use crate::dev::ResourceDef;
use crate::error::Error;
use crate::resource::Resource;
@@ -25,8 +25,6 @@ use crate::service::{
};
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
type FnDataFactory =
Box<dyn Fn() -> LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>;
/// Application builder - structure that follows the builder pattern
/// for building application instances.
@@ -320,7 +318,7 @@ where
/// Registers middleware, in the form of a middleware component (type),
/// that runs during inbound and/or outbound processing in the request
/// lifecycle (request -> response), modifying request/response as
/// life-cycle (request -> response), modifying request/response as
/// necessary, across all requests managed by the *Application*.
///
/// Use middleware when you need to read or modify *every* request or
@@ -385,7 +383,7 @@ where
}
/// Registers middleware, in the form of a closure, that runs during inbound
/// and/or outbound processing in the request lifecycle (request -> response),
/// and/or outbound processing in the request life-cycle (request -> response),
/// modifying request/response as necessary, across all requests managed by
/// the *Application*.
///
@@ -476,13 +474,13 @@ where
mod tests {
use actix_service::Service;
use bytes::Bytes;
use futures::future::ok;
use futures::future::{ok, err};
use super::*;
use crate::http::{header, HeaderValue, Method, StatusCode};
use crate::middleware::DefaultHeaders;
use crate::service::ServiceRequest;
use crate::test::{call_service, init_service, read_body, TestRequest};
use crate::test::{call_service, init_service, try_init_service, read_body, TestRequest};
use crate::{web, HttpRequest, HttpResponse};
#[actix_rt::test]
@@ -551,6 +549,17 @@ mod tests {
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[actix_rt::test]
async fn test_data_factory_errors() {
let srv =
try_init_service(App::new().data_factory(|| err::<u32, _>(())).service(
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
))
.await;
assert!(srv.is_err());
}
#[actix_rt::test]
async fn test_extension() {
let mut srv = init_service(App::new().app_data(10usize).service(

View File

@@ -9,10 +9,10 @@ use actix_http::{Extensions, Request, Response};
use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url};
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
use actix_service::{fn_service, Service, ServiceFactory};
use futures::future::{ok, FutureExt, LocalBoxFuture};
use futures::future::{join_all, ok, FutureExt, LocalBoxFuture};
use crate::config::{AppConfig, AppService};
use crate::data::DataFactory;
use crate::data::{FnDataFactory, DataFactory};
use crate::error::Error;
use crate::guard::Guard;
use crate::request::{HttpRequest, HttpRequestPool};
@@ -23,8 +23,6 @@ type Guards = Vec<Box<dyn Guard>>;
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
type BoxResponse = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
type FnDataFactory =
Box<dyn Fn() -> LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>;
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
/// It also executes data factories.
@@ -109,12 +107,15 @@ where
let rmap = Rc::new(rmap);
rmap.finish(rmap.clone());
// start all data factory futures
let factory_futs = join_all(self.data_factories.iter().map(|f| f()));
AppInitResult {
endpoint: None,
endpoint_fut: self.endpoint.new_service(()),
data: self.data.clone(),
data_factories: Vec::new(),
data_factories_fut: self.data_factories.iter().map(|f| f()).collect(),
data_factories: None,
data_factories_fut: factory_futs.boxed_local(),
extensions: Some(
self.extensions
.borrow_mut()
@@ -133,15 +134,21 @@ pub struct AppInitResult<T, B>
where
T: ServiceFactory,
{
endpoint: Option<T::Service>,
#[pin]
endpoint_fut: T::Future,
// a Some signals completion of endpoint creation
endpoint: Option<T::Service>,
#[pin]
data_factories_fut: LocalBoxFuture<'static, Vec<Result<Box<dyn DataFactory>, ()>>>,
// a Some signals completion of factory futures
data_factories: Option<Vec<Box<dyn DataFactory>>>,
rmap: Rc<ResourceMap>,
config: AppConfig,
data: Rc<Vec<Box<dyn DataFactory>>>,
data_factories: Vec<Box<dyn DataFactory>>,
data_factories_fut: Vec<LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>,
extensions: Option<Extensions>,
_t: PhantomData<B>,
}
@@ -161,44 +168,46 @@ where
let this = self.project();
// async data factories
let mut idx = 0;
while idx < this.data_factories_fut.len() {
match Pin::new(&mut this.data_factories_fut[idx]).poll(cx)? {
Poll::Ready(f) => {
this.data_factories.push(f);
let _ = this.data_factories_fut.remove(idx);
}
Poll::Pending => idx += 1,
if let Poll::Ready(factories) = this.data_factories_fut.poll(cx) {
let factories: Result<Vec<_>, ()> = factories.into_iter().collect();
if let Ok(factories) = factories {
this.data_factories.replace(factories);
} else {
return Poll::Ready(Err(()));
}
}
// app service and middleware
if this.endpoint.is_none() {
if let Poll::Ready(srv) = this.endpoint_fut.poll(cx)? {
*this.endpoint = Some(srv);
}
}
if this.endpoint.is_some() && this.data_factories_fut.is_empty() {
// not using if let so condition only needs shared ref
if this.endpoint.is_some() && this.data_factories.is_some() {
// create app data container
let mut data = this.extensions.take().unwrap();
for f in this.data.iter() {
f.create(&mut data);
}
for f in this.data_factories.iter() {
for f in this.data_factories.take().unwrap().iter() {
f.create(&mut data);
}
Poll::Ready(Ok(AppInitService {
return Poll::Ready(Ok(AppInitService {
service: this.endpoint.take().unwrap(),
rmap: this.rmap.clone(),
config: this.config.clone(),
data: Rc::new(data),
pool: HttpRequestPool::create(),
}))
} else {
Poll::Pending
}));
}
Poll::Pending
}
}

View File

@@ -50,7 +50,7 @@ impl AppService {
}
}
/// Check if root is beeing configured
/// Check if root is being configured
pub fn is_root(&self) -> bool {
self.root
}
@@ -123,6 +123,7 @@ impl AppService {
}
}
/// Application connection config
#[derive(Clone)]
pub struct AppConfig(Rc<AppConfigInner>);

View File

@@ -3,7 +3,7 @@ use std::sync::Arc;
use actix_http::error::{Error, ErrorInternalServerError};
use actix_http::Extensions;
use futures::future::{err, ok, Ready};
use futures::future::{err, ok, LocalBoxFuture, Ready};
use crate::dev::Payload;
use crate::extract::FromRequest;
@@ -14,6 +14,9 @@ pub(crate) trait DataFactory {
fn create(&self, extensions: &mut Extensions) -> bool;
}
pub(crate) type FnDataFactory =
Box<dyn Fn() -> LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>;
/// Application data.
///
/// Application data is an arbitrary data attached to the app.

View File

@@ -21,6 +21,8 @@ pub enum UrlGenerationError {
ParseError(UrlParseError),
}
impl std::error::Error for UrlGenerationError {}
/// `InternalServerError` for `UrlGeneratorError`
impl ResponseError for UrlGenerationError {}
@@ -51,6 +53,8 @@ pub enum UrlencodedError {
Payload(PayloadError),
}
impl std::error::Error for UrlencodedError {}
/// Return `BadRequest` for `UrlencodedError`
impl ResponseError for UrlencodedError {
fn status_code(&self) -> StatusCode {
@@ -79,6 +83,8 @@ pub enum JsonPayloadError {
Payload(PayloadError),
}
impl std::error::Error for JsonPayloadError {}
/// Return `BadRequest` for `JsonPayloadError`
impl ResponseError for JsonPayloadError {
fn error_response(&self) -> HttpResponse {
@@ -99,6 +105,8 @@ pub enum PathError {
Deserialize(serde::de::value::Error),
}
impl std::error::Error for PathError {}
/// Return `BadRequest` for `PathError`
impl ResponseError for PathError {
fn status_code(&self) -> StatusCode {
@@ -114,6 +122,8 @@ pub enum QueryPayloadError {
Deserialize(serde::de::value::Error),
}
impl std::error::Error for QueryPayloadError {}
/// Return `BadRequest` for `QueryPayloadError`
impl ResponseError for QueryPayloadError {
fn status_code(&self) -> StatusCode {
@@ -139,6 +149,8 @@ pub enum ReadlinesError {
ContentTypeError(ContentTypeError),
}
impl std::error::Error for ReadlinesError {}
/// Return `BadRequest` for `ReadlinesError`
impl ResponseError for ReadlinesError {
fn status_code(&self) -> StatusCode {

View File

@@ -6,7 +6,7 @@
//! It is possible to add guards to *scopes*, *resources*
//! and *routes*. Actix provide several guards by default, like various
//! http methods, header, etc. To become a guard, type must implement `Guard`
//! trait. Simple functions coulds guards as well.
//! trait. Simple functions could be guards as well.
//!
//! Guards can not modify the request object. But it is possible
//! to store extra attributes on a request by using the `Extensions` container.
@@ -100,7 +100,7 @@ pub fn Any<F: Guard + 'static>(guard: F) -> AnyGuard {
AnyGuard(vec![Box::new(guard)])
}
/// Matches if any of supplied guards matche.
/// Matches any of supplied guards.
pub struct AnyGuard(Vec<Box<dyn Guard>>);
impl AnyGuard {

View File

@@ -5,7 +5,7 @@ use actix_service::{Service, Transform};
use futures::future::{ok, Either, FutureExt, LocalBoxFuture};
/// `Middleware` for conditionally enables another middleware.
/// The controled middleware must not change the `Service` interfaces.
/// The controlled middleware must not change the `Service` interfaces.
/// This means you cannot control such middlewares like `Logger` or `Compress`.
///
/// ## Usage

View File

@@ -163,11 +163,11 @@ where
LoggerResponse {
fut: self.service.call(req),
format: None,
time: OffsetDateTime::now(),
time: OffsetDateTime::now_utc(),
_t: PhantomData,
}
} else {
let now = OffsetDateTime::now();
let now = OffsetDateTime::now_utc();
let mut format = self.inner.format.clone();
for unit in &mut format.0 {
@@ -380,12 +380,12 @@ impl FormatText {
FormatText::Percent => "%".fmt(fmt),
FormatText::ResponseSize => size.fmt(fmt),
FormatText::Time => {
let rt = OffsetDateTime::now() - entry_time;
let rt = OffsetDateTime::now_utc() - entry_time;
let rt = rt.as_seconds_f64();
fmt.write_fmt(format_args!("{:.6}", rt))
}
FormatText::TimeMillis => {
let rt = OffsetDateTime::now() - entry_time;
let rt = OffsetDateTime::now_utc() - entry_time;
let rt = (rt.whole_nanoseconds() as f64) / 1_000_000.0;
fmt.write_fmt(format_args!("{:.6}", rt))
}
@@ -520,7 +520,7 @@ mod tests {
.uri("/test/route/yeah")
.to_srv_request();
let now = OffsetDateTime::now();
let now = OffsetDateTime::now_utc();
for unit in &mut format.0 {
unit.render_request(now, &req);
}
@@ -551,7 +551,7 @@ mod tests {
)
.to_srv_request();
let now = OffsetDateTime::now();
let now = OffsetDateTime::now_utc();
for unit in &mut format.0 {
unit.render_request(now, &req);
}
@@ -561,7 +561,7 @@ mod tests {
unit.render_response(&resp);
}
let entry_time = OffsetDateTime::now();
let entry_time = OffsetDateTime::now_utc();
let render = |fmt: &mut Formatter<'_>| {
for unit in &format.0 {
unit.render(fmt, 1024, entry_time)?;
@@ -579,7 +579,7 @@ mod tests {
let mut format = Format::new("%t");
let req = TestRequest::default().to_srv_request();
let now = OffsetDateTime::now();
let now = OffsetDateTime::now_utc();
for unit in &mut format.0 {
unit.render_request(now, &req);
}

View File

@@ -74,9 +74,13 @@ where
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
let head = req.head_mut();
let path = head.uri.path();
// always add trailing slash, might be an extra one
let path = head.uri.path().to_string() + "/";
let original_len = path.len();
let path = self.merge_slash.replace_all(path, "/");
// normalize multiple /'s to one /
let path = self.merge_slash.replace_all(&path, "/");
if original_len != path.len() {
let mut parts = head.uri.clone().into_parts();
@@ -119,6 +123,14 @@ mod tests {
let req = TestRequest::with_uri("/v1//something////").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());
let req2 = TestRequest::with_uri("//v1/something").to_request();
let res2 = call_service(&mut app, req2).await;
assert!(res2.status().is_success());
let req3 = TestRequest::with_uri("//v1//////something").to_request();
let res3 = call_service(&mut app, req3).await;
assert!(res3.status().is_success());
}
#[actix_rt::test]
@@ -136,6 +148,14 @@ mod tests {
let req = TestRequest::with_uri("/v1//something////").to_srv_request();
let res = normalize.call(req).await.unwrap();
assert!(res.status().is_success());
let req2 = TestRequest::with_uri("///v1/something").to_srv_request();
let res2 = normalize.call(req2).await.unwrap();
assert!(res2.status().is_success());
let req3 = TestRequest::with_uri("//v1///something").to_srv_request();
let res3 = normalize.call(req3).await.unwrap();
assert!(res3.status().is_success());
}
#[actix_rt::test]
@@ -147,6 +167,25 @@ mod tests {
ok(req.into_response(HttpResponse::Ok().finish()))
};
let mut normalize = NormalizePath
.new_transform(srv.into_service())
.await
.unwrap();
let req = TestRequest::with_uri(URI).to_srv_request();
let res = normalize.call(req).await.unwrap();
assert!(res.status().is_success());
}
#[actix_rt::test]
async fn should_normalize_nothing_notrail() {
const URI: &str = "/v1/something";
let srv = |req: ServiceRequest| {
assert_eq!(URI, req.path());
ok(req.into_response(HttpResponse::Ok().finish()))
};
let mut normalize = NormalizePath
.new_transform(srv.into_service())
.await

View File

@@ -57,7 +57,7 @@ impl HttpRequest {
&self.0.head
}
/// This method returns muttable reference to the request head.
/// This method returns mutable reference to the request head.
/// panics if multiple references of http request exists.
#[inline]
pub(crate) fn head_mut(&mut self) -> &mut RequestHead {

View File

@@ -46,7 +46,7 @@ type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Err
/// ```
///
/// If no matching route could be found, *405* response code get returned.
/// Default behavior could be overriden with `default_resource()` method.
/// Default behavior could be overridden with `default_resource()` method.
pub struct Resource<T = ResourceEndpoint> {
endpoint: T,
rdef: Vec<String>,
@@ -196,9 +196,11 @@ where
self.app_data(Data::new(data))
}
/// Set or override application data.
/// Add resource data.
///
/// This method overrides data stored with [`App::app_data()`](#method.app_data)
/// If used, this method will create a new data context used for extracting
/// from requests. Data added here is *not* merged with data added on App
/// or containing scopes.
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
if self.data.is_none() {
self.data = Some(Extensions::new());
@@ -393,6 +395,7 @@ where
if let Some(ref mut ext) = self.data {
config.set_service_data(ext);
}
config.register_service(rdef, guards, self, None)
}
}
@@ -542,6 +545,9 @@ impl Service for ResourceService {
}
}
if let Some(ref mut default) = self.default {
if let Some(ref data) = self.data {
req.set_data_container(data.clone());
}
Either::Right(default.call(req))
} else {
let req = req.into_parts().0;
@@ -584,13 +590,14 @@ mod tests {
use actix_rt::time::delay_for;
use actix_service::Service;
use bytes::Bytes;
use futures::future::ok;
use crate::http::{header, HeaderValue, Method, StatusCode};
use crate::middleware::DefaultHeaders;
use crate::service::ServiceRequest;
use crate::test::{call_service, init_service, TestRequest};
use crate::{guard, web, App, Error, HttpResponse};
use crate::test::{call_service, init_service, read_body, TestRequest};
use crate::{guard, web, App, Error, HttpRequest, HttpResponse};
#[actix_rt::test]
async fn test_middleware() {
@@ -616,6 +623,79 @@ mod tests {
);
}
#[actix_rt::test]
async fn test_overwritten_data() {
#[allow(dead_code)]
fn echo_usize(req: HttpRequest) -> HttpResponse {
let num = req.app_data::<usize>().unwrap();
HttpResponse::Ok().body(format!("{}", num))
}
#[allow(dead_code)]
fn echo_u32(req: HttpRequest) -> HttpResponse {
let num = req.app_data::<u32>().unwrap();
HttpResponse::Ok().body(format!("{}", num))
}
#[allow(dead_code)]
fn echo_both(req: HttpRequest) -> HttpResponse {
let num = req.app_data::<usize>().unwrap();
let num2 = req.app_data::<u32>().unwrap();
HttpResponse::Ok().body(format!("{}-{}", num, num2))
}
let mut srv = init_service(
App::new()
.app_data(88usize)
.service(web::resource("/").route(web::get().to(echo_usize)))
.service(
web::resource("/one")
.app_data(1usize)
.route(web::get().to(echo_usize)),
)
.service(
web::resource("/two")
.app_data(2usize)
.route(web::get().to(echo_usize)),
)
.service(
web::resource("/three")
.app_data(3u32)
// this doesnt work because app_data "overrides" the
// entire data field potentially passed down
// .route(web::get().to(echo_both)),
.route(web::get().to(echo_u32)),
)
.service(web::resource("/eight").route(web::get().to(echo_usize))),
)
.await;
let req = TestRequest::get().uri("/").to_request();
let resp = srv.call(req).await.unwrap();
let body = read_body(resp).await;
assert_eq!(body, Bytes::from_static(b"88"));
let req = TestRequest::get().uri("/one").to_request();
let resp = srv.call(req).await.unwrap();
let body = read_body(resp).await;
assert_eq!(body, Bytes::from_static(b"1"));
let req = TestRequest::get().uri("/two").to_request();
let resp = srv.call(req).await.unwrap();
let body = read_body(resp).await;
assert_eq!(body, Bytes::from_static(b"2"));
// let req = TestRequest::get().uri("/three").to_request();
// let resp = srv.call(req).await.unwrap();
// let body = read_body(resp).await;
// assert_eq!(body, Bytes::from_static(b"88-3"));
let req = TestRequest::get().uri("/eight").to_request();
let resp = srv.call(req).await.unwrap();
let body = read_body(resp).await;
assert_eq!(body, Bytes::from_static(b"88"));
}
#[actix_rt::test]
async fn test_middleware_fn() {
let mut srv = init_service(
@@ -649,11 +729,9 @@ mod tests {
#[actix_rt::test]
async fn test_to() {
let mut srv =
init_service(App::new().service(web::resource("/test").to(|| {
async {
delay_for(Duration::from_millis(100)).await;
Ok::<_, Error>(HttpResponse::Ok())
}
init_service(App::new().service(web::resource("/test").to(|| async {
delay_for(Duration::from_millis(100)).await;
Ok::<_, Error>(HttpResponse::Ok())
})))
.await;
let req = TestRequest::with_uri("/test").to_request();
@@ -793,4 +871,23 @@ mod tests {
let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn test_data_default_service() {
let mut srv = init_service(
App::new().data(1usize).service(
web::resource("/test")
.data(10usize)
.default_service(web::to(|data: web::Data<usize>| {
assert_eq!(**data, 10);
HttpResponse::Ok()
})),
),
)
.await;
let req = TestRequest::get().uri("/test").to_request();
let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}
}

View File

@@ -54,7 +54,7 @@ type BoxedResponse = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
/// ```
///
/// In the above example three routes get registered:
/// * /{project_id}/path1 - reponds to all http method
/// * /{project_id}/path1 - responds to all http method
/// * /{project_id}/path2 - `GET` requests
/// * /{project_id}/path3 - `HEAD` requests
///
@@ -151,9 +151,11 @@ where
self.app_data(Data::new(data))
}
/// Set or override application data.
/// Add scope data.
///
/// This method overrides data stored with [`App::app_data()`](#method.app_data)
/// If used, this method will create a new data context used for extracting
/// from requests. Data added here is *not* merged with data added on App
/// or containing scopes.
pub fn app_data<U: 'static>(mut self, data: U) -> Self {
if self.data.is_none() {
self.data = Some(Extensions::new());
@@ -303,7 +305,7 @@ where
/// Registers middleware, in the form of a middleware component (type),
/// that runs during inbound processing in the request
/// lifecycle (request -> response), modifying request as
/// life-cycle (request -> response), modifying request as
/// necessary, across all requests managed by the *Scope*. Scope-level
/// middleware is more limited in what it can modify, relative to Route or
/// Application level middleware, in that Scope-level middleware can not modify
@@ -344,7 +346,7 @@ where
}
/// Registers middleware, in the form of a closure, that runs during inbound
/// processing in the request lifecycle (request -> response), modifying
/// processing in the request life-cycle (request -> response), modifying
/// request as necessary, across all requests managed by the *Scope*.
/// Scope-level middleware is more limited in what it can modify, relative
/// to Route or Application level middleware, in that Scope-level middleware
@@ -626,6 +628,9 @@ impl Service for ScopeService {
}
Either::Left(srv.call(req))
} else if let Some(ref mut default) = self.default {
if let Some(ref data) = self.data {
req.set_data_container(data.clone());
}
Either::Left(default.call(req))
} else {
let req = req.into_parts().0;
@@ -825,11 +830,9 @@ mod tests {
async fn test_scope_variable_segment() {
let mut srv =
init_service(App::new().service(web::scope("/ab-{project}").service(
web::resource("/path1").to(|r: HttpRequest| {
async move {
HttpResponse::Ok()
.body(format!("project: {}", &r.match_info()["project"]))
}
web::resource("/path1").to(|r: HttpRequest| async move {
HttpResponse::Ok()
.body(format!("project: {}", &r.match_info()["project"]))
}),
)))
.await;
@@ -937,11 +940,9 @@ mod tests {
async fn test_nested_scope_with_variable_segment() {
let mut srv = init_service(App::new().service(web::scope("/app").service(
web::scope("/{project_id}").service(web::resource("/path1").to(
|r: HttpRequest| {
async move {
HttpResponse::Created()
.body(format!("project: {}", &r.match_info()["project_id"]))
}
|r: HttpRequest| async move {
HttpResponse::Created()
.body(format!("project: {}", &r.match_info()["project_id"]))
},
)),
)))
@@ -964,14 +965,12 @@ mod tests {
async fn test_nested2_scope_with_variable_segment() {
let mut srv = init_service(App::new().service(web::scope("/app").service(
web::scope("/{project}").service(web::scope("/{id}").service(
web::resource("/path1").to(|r: HttpRequest| {
async move {
HttpResponse::Created().body(format!(
"project: {} - {}",
&r.match_info()["project"],
&r.match_info()["id"],
))
}
web::resource("/path1").to(|r: HttpRequest| async move {
HttpResponse::Created().body(format!(
"project: {} - {}",
&r.match_info()["project"],
&r.match_info()["id"],
))
}),
)),
)))
@@ -1119,6 +1118,23 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn test_override_data_default_service() {
let mut srv = init_service(App::new().data(1usize).service(
web::scope("app").data(10usize).default_service(web::to(
|data: web::Data<usize>| {
assert_eq!(**data, 10);
HttpResponse::Ok()
},
)),
))
.await;
let req = TestRequest::with_uri("/app/t").to_request();
let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn test_override_app_data() {
let mut srv = init_service(App::new().app_data(web::Data::new(1usize)).service(
@@ -1177,15 +1193,11 @@ mod tests {
);
s.route(
"/",
web::get().to(|req: HttpRequest| {
async move {
HttpResponse::Ok().body(format!(
"{}",
req.url_for("youtube", &["xxxxxx"])
.unwrap()
.as_str()
))
}
web::get().to(|req: HttpRequest| async move {
HttpResponse::Ok().body(format!(
"{}",
req.url_for("youtube", &["xxxxxx"]).unwrap().as_str()
))
}),
);
}));
@@ -1203,11 +1215,9 @@ mod tests {
async fn test_url_for_nested() {
let mut srv = init_service(App::new().service(web::scope("/a").service(
web::scope("/b").service(web::resource("/c/{stuff}").name("c").route(
web::get().to(|req: HttpRequest| {
async move {
HttpResponse::Ok()
.body(format!("{}", req.url_for("c", &["12345"]).unwrap()))
}
web::get().to(|req: HttpRequest| async move {
HttpResponse::Ok()
.body(format!("{}", req.url_for("c", &["12345"]).unwrap()))
}),
)),
)))

View File

@@ -69,7 +69,7 @@ impl ServiceRequest {
/// Construct request from parts.
///
/// `ServiceRequest` can be re-constructed only if `req` hasnt been cloned.
/// `ServiceRequest` can be re-constructed only if `req` hasn't been cloned.
pub fn from_parts(
mut req: HttpRequest,
pl: Payload,

View File

@@ -78,6 +78,26 @@ pub fn default_service(
pub async fn init_service<R, S, B, E>(
app: R,
) -> impl Service<Request = Request, Response = ServiceResponse<B>, Error = E>
where
R: IntoServiceFactory<S>,
S: ServiceFactory<
Config = AppConfig,
Request = Request,
Response = ServiceResponse<B>,
Error = E,
>,
S::InitError: std::fmt::Debug,
{
try_init_service(app).await.expect("service initilization failed")
}
/// Fallible version of init_service that allows testing data factory errors.
pub(crate) async fn try_init_service<R, S, B, E>(
app: R,
) -> Result<
impl Service<Request = Request, Response = ServiceResponse<B>, Error = E>,
S::InitError,
>
where
R: IntoServiceFactory<S>,
S: ServiceFactory<
@@ -89,7 +109,7 @@ where
S::InitError: std::fmt::Debug,
{
let srv = app.into_factory();
srv.new_service(AppConfig::default()).await.unwrap()
srv.new_service(AppConfig::default()).await
}
/// Calls service and waits for response future completion.
@@ -187,7 +207,7 @@ where
/// .to_request();
///
/// let resp = test::call_service(&mut app, req).await;
/// let result = test::read_body(resp);
/// let result = test::read_body(resp).await;
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
/// }
/// ```
@@ -203,6 +223,54 @@ where
bytes.freeze()
}
/// Helper function that returns a deserialized response body of a ServiceResponse.
///
/// ```rust
/// use actix_web::{App, test, web, HttpResponse, http::header};
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize)]
/// pub struct Person {
/// id: String,
/// name: String,
/// }
///
/// #[actix_rt::test]
/// async fn test_post_person() {
/// let mut app = test::init_service(
/// App::new().service(
/// web::resource("/people")
/// .route(web::post().to(|person: web::Json<Person>| async {
/// HttpResponse::Ok()
/// .json(person.into_inner())})
/// ))
/// ).await;
///
/// let payload = r#"{"id":"12345","name":"User name"}"#.as_bytes();
///
/// let resp = test::TestRequest::post()
/// .uri("/people")
/// .header(header::CONTENT_TYPE, "application/json")
/// .set_payload(payload)
/// .send_request(&mut app)
/// .await;
///
/// assert!(resp.status().is_success());
///
/// let result: Person = test::read_body_json(resp).await;
/// }
/// ```
pub async fn read_body_json<T, B>(res: ServiceResponse<B>) -> T
where
B: MessageBody + Unpin,
T: DeserializeOwned,
{
let body = read_body(res).await;
serde_json::from_slice(&body)
.unwrap_or_else(|_| panic!("read_response_json failed during deserialization"))
}
pub async fn load_stream<S>(mut stream: S) -> Result<Bytes, Error>
where
S: Stream<Item = Result<Bytes, Error>> + Unpin,
@@ -527,6 +595,16 @@ impl TestRequest {
(req, payload)
}
/// Complete request creation, calls service and waits for response future completion.
pub async fn send_request<S, B, E>(self, app: &mut S) -> S::Response
where
S: Service<Request = Request, Response = ServiceResponse<B>, Error = E>,
E: std::fmt::Debug,
{
let req = self.to_request();
call_service(app, req).await
}
}
/// Start test server with default configuration
@@ -1041,6 +1119,23 @@ mod tests {
assert_eq!(result, Bytes::from_static(b"welcome!"));
}
#[actix_rt::test]
async fn test_send_request() {
let mut app =
init_service(App::new().service(web::resource("/index.html").route(
web::get().to(|| async { HttpResponse::Ok().body("welcome!") }),
)))
.await;
let resp = TestRequest::get()
.uri("/index.html")
.send_request(&mut app)
.await;
let result = read_body(resp).await;
assert_eq!(result, Bytes::from_static(b"welcome!"));
}
#[derive(Serialize, Deserialize)]
pub struct Person {
id: String,
@@ -1050,8 +1145,8 @@ mod tests {
#[actix_rt::test]
async fn test_response_json() {
let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| {
async { HttpResponse::Ok().json(person.into_inner()) }
web::post().to(|person: web::Json<Person>| async {
HttpResponse::Ok().json(person.into_inner())
}),
)))
.await;
@@ -1068,11 +1163,33 @@ mod tests {
assert_eq!(&result.id, "12345");
}
#[actix_rt::test]
async fn test_body_json() {
let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| async {
HttpResponse::Ok().json(person.into_inner())
}),
)))
.await;
let payload = r#"{"id":"12345","name":"User name"}"#.as_bytes();
let resp = TestRequest::post()
.uri("/people")
.header(header::CONTENT_TYPE, "application/json")
.set_payload(payload)
.send_request(&mut app)
.await;
let result: Person = read_body_json(resp).await;
assert_eq!(&result.name, "User name");
}
#[actix_rt::test]
async fn test_request_response_form() {
let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Form<Person>| {
async { HttpResponse::Ok().json(person.into_inner()) }
web::post().to(|person: web::Form<Person>| async {
HttpResponse::Ok().json(person.into_inner())
}),
)))
.await;
@@ -1097,8 +1214,8 @@ mod tests {
#[actix_rt::test]
async fn test_request_response_json() {
let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| {
async { HttpResponse::Ok().json(person.into_inner()) }
web::post().to(|person: web::Json<Person>| async {
HttpResponse::Ok().json(person.into_inner())
}),
)))
.await;
@@ -1162,53 +1279,53 @@ mod tests {
assert!(res.status().is_success());
}
/*
/*
Comment out until actix decoupled of actix-http:
https://github.com/actix/actix/issues/321
Comment out until actix decoupled of actix-http:
https://github.com/actix/actix/issues/321
use futures::FutureExt;
use futures::FutureExt;
#[actix_rt::test]
async fn test_actor() {
use actix::Actor;
#[actix_rt::test]
async fn test_actor() {
use actix::Actor;
struct MyActor;
struct MyActor;
struct Num(usize);
impl actix::Message for Num {
type Result = usize;
}
impl actix::Actor for MyActor {
type Context = actix::Context<Self>;
}
impl actix::Handler<Num> for MyActor {
type Result = usize;
fn handle(&mut self, msg: Num, _: &mut Self::Context) -> Self::Result {
msg.0
struct Num(usize);
impl actix::Message for Num {
type Result = usize;
}
impl actix::Actor for MyActor {
type Context = actix::Context<Self>;
}
impl actix::Handler<Num> for MyActor {
type Result = usize;
fn handle(&mut self, msg: Num, _: &mut Self::Context) -> Self::Result {
msg.0
}
}
}
let mut app = init_service(App::new().service(web::resource("/index.html").to(
move || {
addr.send(Num(1)).map(|res| match res {
Ok(res) => {
if res == 1 {
Ok(HttpResponse::Ok())
} else {
Ok(HttpResponse::BadRequest())
let mut app = init_service(App::new().service(web::resource("/index.html").to(
move || {
addr.send(Num(1)).map(|res| match res {
Ok(res) => {
if res == 1 {
Ok(HttpResponse::Ok())
} else {
Ok(HttpResponse::BadRequest())
}
}
}
Err(err) => Err(err),
})
},
)))
.await;
Err(err) => Err(err),
})
},
)))
.await;
let req = TestRequest::post().uri("/index.html").to_request();
let res = app.call(req).await.unwrap();
assert!(res.status().is_success());
}
*/
let req = TestRequest::post().uri("/index.html").to_request();
let res = app.call(req).await.unwrap();
assert!(res.status().is_success());
}
*/
}

View File

@@ -11,7 +11,6 @@ use futures::future::{err, ok, FutureExt, LocalBoxFuture, Ready};
use futures::StreamExt;
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json;
use actix_http::http::{header::CONTENT_LENGTH, StatusCode};
use actix_http::{HttpMessage, Payload, Response};
@@ -208,8 +207,10 @@ where
/// Json extractor configuration
///
/// # Examples
///
/// ```rust
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
/// use actix_web::{error, web, App, FromRequest, HttpRequest, HttpResponse};
/// use serde_derive::Deserialize;
///
/// #[derive(Deserialize)]
@@ -222,6 +223,19 @@ where
/// format!("Welcome {}!", info.username)
/// }
///
/// /// Return either a 400 or 415, and include the error message from serde
/// /// in the response body
/// fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error::Error {
/// let detail = err.to_string();
/// let response = match &err {
/// error::JsonPayloadError::ContentType => {
/// HttpResponse::UnsupportedMediaType().content_type("text/plain").body(detail)
/// }
/// _ => HttpResponse::BadRequest().content_type("text/plain").body(detail),
/// };
/// error::InternalError::from_response(err, response).into()
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html")
@@ -232,10 +246,7 @@ where
/// .content_type(|mime| { // <- accept text/plain content type
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
/// })
/// .error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// .error_handler(json_error_handler) // Use our custom error response
/// }))
/// .route(web::post().to(index))
/// );

View File

@@ -6,7 +6,6 @@ use std::{fmt, ops};
use actix_http::error::Error;
use futures::future::{err, ok, Ready};
use serde::de;
use serde_urlencoded;
use crate::dev::Payload;
use crate::error::QueryPayloadError;

View File

@@ -3,7 +3,8 @@
## [Unreleased] - 2020-xx-xx
* Update the `time` dependency to 0.2.7
* Update `actix-connect` dependency to 2.0.0-alpha.1
* Update `actix-connect` dependency to 2.0.0-alpha.2
* Make `test_server` `async` fn.
## [1.0.0] - 2019-12-13

View File

@@ -32,12 +32,12 @@ openssl = ["open-ssl", "awc/openssl"]
[dependencies]
actix-service = "1.0.1"
actix-codec = "0.2.0"
actix-connect = "2.0.0-alpha.1"
actix-connect = "2.0.0-alpha.2"
actix-utils = "1.0.3"
actix-rt = "1.0.0"
actix-server = "1.0.0"
actix-testing = "1.0.0"
awc = "1.0.1"
awc = "2.0.0-alpha.1"
base64 = "0.11"
bytes = "0.5.3"
@@ -52,8 +52,8 @@ sha1 = "0.6"
slab = "0.4"
serde_urlencoded = "0.6.1"
time = { version = "0.2.7", default-features = false, features = ["std"] }
open-ssl = { version="0.10", package="openssl", optional = true }
open-ssl = { version = "0.10", package = "openssl", optional = true }
[dev-dependencies]
actix-web = "2.0.0"
actix-http = "2.0.0-alpha.2"
actix-web = "3.0.0-alpha.2"
actix-http = "2.0.0-alpha.3"

View File

@@ -43,7 +43,7 @@ pub use actix_testing::*;
/// assert!(response.status().is_success());
/// }
/// ```
pub fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer {
pub async fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer {
let (tx, rx) = mpsc::channel();
// run server in separate thread
@@ -92,7 +92,7 @@ pub fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer {
Client::build().connector(connector).finish()
};
actix_connect::start_default_resolver();
actix_connect::start_default_resolver().await.unwrap();
TestServer {
addr,

View File

@@ -1,11 +1,11 @@
// Regression test for #/1321
/*
use futures::task::{noop_waker, Context};
use futures::stream::once;
use actix_http::body::{MessageBody, BodyStream};
use bytes::Bytes;
/*
Disable weird poll until actix-web is based on actix-http 2.0.0
#[test]