mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-04 09:56:22 +02:00
Compare commits
46 Commits
files-v0.3
...
http-v2.0.
Author | SHA1 | Date | |
---|---|---|---|
cf1c8abe62 | |||
92b5bcd13f | |||
701bdacfa2 | |||
6dc47c4093 | |||
0ec335a39c | |||
f8d5ad6b53 | |||
43c362779d | |||
971ba3eee1 | |||
2fd96c03e5 | |||
ad7c6d2633 | |||
3362a3d61b | |||
769ea6bd5b | |||
1382094c15 | |||
78594a72bd | |||
327e472e44 | |||
e10eb648d9 | |||
a2662b928b | |||
84583799be | |||
08f9a34075 | |||
056803d534 | |||
deab634247 | |||
0b641a2db2 | |||
f2d641b772 | |||
23c8191cca | |||
487f90be5b | |||
fa28175a74 | |||
a70e599ff5 | |||
c11052f826 | |||
73ec01e83b | |||
a7c8533291 | |||
eb0eda69c6 | |||
dc74db1f2f | |||
0ba73fc44c | |||
9af07d66ae | |||
e72ee28232 | |||
4f9a1ac3b7 | |||
6c5c4ea230 | |||
a79450482c | |||
5286b8aed7 | |||
621ebec01a | |||
5e5c8b1c83 | |||
322e7c15d1 | |||
7aa757ad5a | |||
482f74e409 | |||
19967c41cc | |||
8a106c07b4 |
@ -1,41 +0,0 @@
|
|||||||
environment:
|
|
||||||
global:
|
|
||||||
PROJECT_NAME: actix-web
|
|
||||||
matrix:
|
|
||||||
# Stable channel
|
|
||||||
- TARGET: i686-pc-windows-msvc
|
|
||||||
CHANNEL: stable
|
|
||||||
- TARGET: x86_64-pc-windows-gnu
|
|
||||||
CHANNEL: stable
|
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
|
||||||
CHANNEL: stable
|
|
||||||
# Nightly channel
|
|
||||||
- TARGET: i686-pc-windows-msvc
|
|
||||||
CHANNEL: nightly
|
|
||||||
- TARGET: x86_64-pc-windows-gnu
|
|
||||||
CHANNEL: nightly
|
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
|
||||||
CHANNEL: nightly
|
|
||||||
|
|
||||||
# Install Rust and Cargo
|
|
||||||
# (Based on from https://github.com/rust-lang/libc/blob/master/appveyor.yml)
|
|
||||||
install:
|
|
||||||
- ps: >-
|
|
||||||
If ($Env:TARGET -eq 'x86_64-pc-windows-gnu') {
|
|
||||||
$Env:PATH += ';C:\msys64\mingw64\bin'
|
|
||||||
} ElseIf ($Env:TARGET -eq 'i686-pc-windows-gnu') {
|
|
||||||
$Env:PATH += ';C:\MinGW\bin'
|
|
||||||
}
|
|
||||||
- curl -sSf -o rustup-init.exe https://win.rustup.rs
|
|
||||||
- rustup-init.exe --default-host %TARGET% --default-toolchain %CHANNEL% -y
|
|
||||||
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
|
|
||||||
- rustc -Vv
|
|
||||||
- cargo -V
|
|
||||||
|
|
||||||
# 'cargo test' takes care of building for us, so disable Appveyor's build stage.
|
|
||||||
build: false
|
|
||||||
|
|
||||||
# Equivalent to Travis' `script` phase
|
|
||||||
test_script:
|
|
||||||
- cargo clean
|
|
||||||
- cargo test --no-default-features --features="flate2-rust"
|
|
4
.github/ISSUE_TEMPLATE/bug_report.md
vendored
4
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: bug report
|
name: Bug Report
|
||||||
about: create a bug report
|
about: Create a bug report.
|
||||||
---
|
---
|
||||||
|
|
||||||
Your issue may already be reported!
|
Your issue may already be reported!
|
||||||
|
27
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
27
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<!-- Thanks for considering contributing actix! -->
|
||||||
|
<!-- Please fill out the following to make our reviews easy. -->
|
||||||
|
|
||||||
|
## PR Type
|
||||||
|
<!-- What kind of change does this PR make? -->
|
||||||
|
<!-- Bug Fix / Feature / Refactor / Code Style / Other -->
|
||||||
|
INSERT_PR_TYPE
|
||||||
|
|
||||||
|
|
||||||
|
## PR Checklist
|
||||||
|
Check your PR fulfills the following:
|
||||||
|
|
||||||
|
<!-- For draft PRs check the boxes as you complete them. -->
|
||||||
|
|
||||||
|
- [ ] Tests for the changes have been added / updated.
|
||||||
|
- [ ] Documentation comments have been added / updated.
|
||||||
|
- [ ] A changelog entry has been made for the appropriate packages.
|
||||||
|
- [ ] Format code with the latest stable rustfmt
|
||||||
|
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
<!-- Describe the current and new behavior. -->
|
||||||
|
<!-- Emphasize any breaking changes. -->
|
||||||
|
|
||||||
|
|
||||||
|
<!-- If this PR fixes or closes an issue, reference it here. -->
|
||||||
|
<!-- Closes #000 -->
|
7
.github/workflows/bench.yml
vendored
7
.github/workflows/bench.yml
vendored
@ -1,6 +1,11 @@
|
|||||||
name: Benchmark (Linux)
|
name: Benchmark (Linux)
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened]
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check_benchmark:
|
check_benchmark:
|
||||||
|
32
.github/workflows/clippy-fmt.yml
vendored
Normal file
32
.github/workflows/clippy-fmt.yml
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened]
|
||||||
|
|
||||||
|
name: Clippy and rustfmt Check
|
||||||
|
jobs:
|
||||||
|
clippy_check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: rustfmt
|
||||||
|
override: true
|
||||||
|
- name: Check with rustfmt
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: fmt
|
||||||
|
args: --all -- --check
|
||||||
|
|
||||||
|
- uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: nightly
|
||||||
|
components: clippy
|
||||||
|
override: true
|
||||||
|
- name: Check with Clippy
|
||||||
|
uses: actions-rs/clippy-check@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
args: --all-features --all --tests
|
11
.github/workflows/linux.yml
vendored
11
.github/workflows/linux.yml
vendored
@ -1,6 +1,11 @@
|
|||||||
name: CI (Linux)
|
name: CI (Linux)
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened]
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build_and_test:
|
build_and_test:
|
||||||
@ -8,7 +13,7 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
version:
|
version:
|
||||||
- 1.40.0 # MSRV
|
- 1.42.0 # MSRV
|
||||||
- stable
|
- stable
|
||||||
- nightly
|
- nightly
|
||||||
|
|
||||||
@ -55,7 +60,7 @@ jobs:
|
|||||||
- name: Generate coverage file
|
- name: Generate coverage file
|
||||||
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
||||||
run: |
|
run: |
|
||||||
cargo install cargo-tarpaulin
|
cargo install cargo-tarpaulin --vers "^0.13"
|
||||||
cargo tarpaulin --out Xml
|
cargo tarpaulin --out Xml
|
||||||
- name: Upload to Codecov
|
- name: Upload to Codecov
|
||||||
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
if: matrix.version == 'stable' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
|
||||||
|
7
.github/workflows/macos.yml
vendored
7
.github/workflows/macos.yml
vendored
@ -1,6 +1,11 @@
|
|||||||
name: CI (macOS)
|
name: CI (macOS)
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened]
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build_and_test:
|
build_and_test:
|
||||||
|
7
.github/workflows/windows.yml
vendored
7
.github/workflows/windows.yml
vendored
@ -1,6 +1,11 @@
|
|||||||
name: CI (Windows)
|
name: CI (Windows)
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened]
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
env:
|
env:
|
||||||
VCPKGRS_DYNAMIC: 1
|
VCPKGRS_DYNAMIC: 1
|
||||||
|
36
CHANGES.md
36
CHANGES.md
@ -1,19 +1,45 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [3.0.0-alpha.3] - 2020-05-21
|
## Unreleased - 2020-xx-xx
|
||||||
|
### Changed
|
||||||
|
* `PayloadConfig` is now also considered in `Bytes` and `String` extractors when set
|
||||||
|
using `App::data`. [#1610]
|
||||||
|
* `web::Path` now has a public representation: `web::Path(pub T)` that enables
|
||||||
|
destructuring. [#1594]
|
||||||
|
* MSRV is now 1.42.0.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
* Memory leak of app data in pooled requests. [#1609]
|
||||||
|
|
||||||
|
[#1594]: https://github.com/actix/actix-web/pull/1594
|
||||||
|
[#1609]: https://github.com/actix/actix-web/pull/1609
|
||||||
|
[#1610]: https://github.com/actix/actix-web/pull/1610
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-beta.1 - 2020-07-13
|
||||||
### Added
|
### Added
|
||||||
|
* Re-export `actix_rt::main` as `actix_web::main`.
|
||||||
|
* `HttpRequest::match_pattern` and `ServiceRequest::match_pattern` for extracting the matched
|
||||||
|
resource pattern.
|
||||||
|
* `HttpRequest::match_name` and `ServiceRequest::match_name` for extracting matched resource name.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* Fix actix_http::h1::dispatcher so it returns when HW_BUFFER_SIZE is reached. Should reduce peak memory consumption during large uploads. [#1550]
|
||||||
|
* Migrate cookie handling to `cookie` crate. Actix-web no longer requires `ring` dependency.
|
||||||
|
* MSRV is now 1.41.1
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
* `NormalizePath` improved consistency when path needs slashes added _and_ removed.
|
||||||
|
|
||||||
|
|
||||||
|
## 3.0.0-alpha.3 - 2020-05-21
|
||||||
|
### Added
|
||||||
* Add option to create `Data<T>` from `Arc<T>` [#1509]
|
* Add option to create `Data<T>` from `Arc<T>` [#1509]
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* Resources and Scopes can now access non-overridden data types set on App (or containing scopes) when setting their own data. [#1486]
|
* Resources and Scopes can now access non-overridden data types set on App (or containing scopes) when setting their own data. [#1486]
|
||||||
|
|
||||||
* Fix audit issue logging by default peer address [#1485]
|
* Fix audit issue logging by default peer address [#1485]
|
||||||
|
|
||||||
* Bump minimum supported Rust version to 1.40
|
* Bump minimum supported Rust version to 1.40
|
||||||
|
|
||||||
* Replace deprecated `net2` crate with `socket2`
|
* Replace deprecated `net2` crate with `socket2`
|
||||||
|
|
||||||
[#1485]: https://github.com/actix/actix-web/pull/1485
|
[#1485]: https://github.com/actix/actix-web/pull/1485
|
||||||
|
19
Cargo.toml
19
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web"
|
name = "actix-web"
|
||||||
version = "3.0.0-alpha.3"
|
version = "3.0.0-beta.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
|
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -11,7 +11,7 @@ documentation = "https://docs.rs/actix-web/"
|
|||||||
categories = ["network-programming", "asynchronous",
|
categories = ["network-programming", "asynchronous",
|
||||||
"web-programming::http-server",
|
"web-programming::http-server",
|
||||||
"web-programming::websocket"]
|
"web-programming::websocket"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
@ -31,7 +31,6 @@ members = [
|
|||||||
"awc",
|
"awc",
|
||||||
"actix-http",
|
"actix-http",
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-framed",
|
|
||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-web-actors",
|
"actix-web-actors",
|
||||||
"actix-web-codegen",
|
"actix-web-codegen",
|
||||||
@ -44,7 +43,7 @@ default = ["compress"]
|
|||||||
# content-encoding support
|
# content-encoding support
|
||||||
compress = ["actix-http/compress", "awc/compress"]
|
compress = ["actix-http/compress", "awc/compress"]
|
||||||
|
|
||||||
# sessions feature, session require "ring" crate and c compiler
|
# sessions feature
|
||||||
secure-cookies = ["actix-http/secure-cookies"]
|
secure-cookies = ["actix-http/secure-cookies"]
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
@ -70,16 +69,16 @@ actix-codec = "0.2.0"
|
|||||||
actix-service = "1.0.2"
|
actix-service = "1.0.2"
|
||||||
actix-utils = "1.0.6"
|
actix-utils = "1.0.6"
|
||||||
actix-router = "0.2.4"
|
actix-router = "0.2.4"
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.1.1"
|
||||||
actix-server = "1.0.0"
|
actix-server = "1.0.0"
|
||||||
actix-testing = "1.0.0"
|
actix-testing = "1.0.0"
|
||||||
actix-macros = "0.1.0"
|
actix-macros = "0.1.0"
|
||||||
actix-threadpool = "0.3.1"
|
actix-threadpool = "0.3.1"
|
||||||
actix-tls = "2.0.0-alpha.1"
|
actix-tls = "2.0.0-alpha.1"
|
||||||
|
|
||||||
actix-web-codegen = "0.2.2"
|
actix-web-codegen = "0.3.0-beta.1"
|
||||||
actix-http = "2.0.0-alpha.4"
|
actix-http = "2.0.0-alpha.4"
|
||||||
awc = { version = "2.0.0-alpha.2", default-features = false }
|
awc = { version = "2.0.0-beta.1", default-features = false }
|
||||||
|
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
derive_more = "0.99.2"
|
derive_more = "0.99.2"
|
||||||
@ -91,15 +90,15 @@ fxhash = "0.2.1"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
socket2 = "0.3"
|
socket2 = "0.3"
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.17"
|
||||||
regex = "1.3"
|
regex = "1.3"
|
||||||
serde = { version = "1.0", features=["derive"] }
|
serde = { version = "1.0", features=["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_urlencoded = "0.6.1"
|
serde_urlencoded = "0.6.1"
|
||||||
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
open-ssl = { version="0.10", package = "openssl", optional = true }
|
open-ssl = { package = "openssl", version = "0.10", optional = true }
|
||||||
rust-tls = { version = "0.17.0", package = "rustls", optional = true }
|
rust-tls = { package = "rustls", version = "0.17.0", optional = true }
|
||||||
tinyvec = { version = "0.3", features = ["alloc"] }
|
tinyvec = { version = "0.3", features = ["alloc"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
20
MIGRATION.md
20
MIGRATION.md
@ -12,6 +12,26 @@
|
|||||||
* `BodySize::Sized64` variant has been removed. `BodySize::Sized` now receives a
|
* `BodySize::Sized64` variant has been removed. `BodySize::Sized` now receives a
|
||||||
`u64` instead of a `usize`.
|
`u64` instead of a `usize`.
|
||||||
|
|
||||||
|
* Code that was using `path.<index>` to access a `web::Path<(A, B, C)>`s elements now needs to use
|
||||||
|
destructuring or `.into_inner()`. For example:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Previously:
|
||||||
|
async fn some_route(path: web::Path<(String, String)>) -> String {
|
||||||
|
format!("Hello, {} {}", path.0, path.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now (this also worked before):
|
||||||
|
async fn some_route(path: web::Path<(String, String)>) -> String {
|
||||||
|
let (first_name, last_name) = path.into_inner();
|
||||||
|
format!("Hello, {} {}", first_name, last_name)
|
||||||
|
}
|
||||||
|
// Or (this wasn't previously supported):
|
||||||
|
async fn some_route(web::Path((first_name, last_name)): web::Path<(String, String)>) -> String {
|
||||||
|
format!("Hello, {} {}", first_name, last_name)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 2.0.0
|
## 2.0.0
|
||||||
|
|
||||||
* `HttpServer::start()` renamed to `HttpServer::run()`. It also possible to
|
* `HttpServer::start()` renamed to `HttpServer::run()`. It also possible to
|
||||||
|
99
README.md
99
README.md
@ -1,59 +1,58 @@
|
|||||||
<div align="center">
|
<div align="center">
|
||||||
<p><h1>Actix web</h1> </p>
|
<h1>Actix web</h1>
|
||||||
<p><strong>Actix web is a small, pragmatic, and extremely fast rust web framework</strong> </p>
|
<p>
|
||||||
|
<strong>Actix web is a powerful, pragmatic, and extremely fast web framework for Rust</strong>
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
|
[](https://crates.io/crates/actix-web)
|
||||||
|
[](https://docs.rs/actix-web)
|
||||||
|
[](https://blog.rust-lang.org/2020/03/12/Rust-1.42.html)
|
||||||
|

|
||||||
|
<br />
|
||||||
[](https://travis-ci.org/actix/actix-web)
|
[](https://travis-ci.org/actix/actix-web)
|
||||||
[](https://codecov.io/gh/actix/actix-web)
|
[](https://codecov.io/gh/actix/actix-web)
|
||||||
[](https://crates.io/crates/actix-web)
|
|
||||||
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
||||||
[](https://docs.rs/actix-web)
|
|
||||||
[](https://crates.io/crates/actix-web)
|
[](https://crates.io/crates/actix-web)
|
||||||
[](https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html)
|
[](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||

|
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h3>
|
|
||||||
<a href="https://actix.rs">Website</a>
|
|
||||||
<span> | </span>
|
|
||||||
<a href="https://gitter.im/actix/actix">Chat</a>
|
|
||||||
<span> | </span>
|
|
||||||
<a href="https://github.com/actix/examples">Examples</a>
|
|
||||||
</h3>
|
|
||||||
</div>
|
</div>
|
||||||
<br>
|
|
||||||
|
|
||||||
Actix web is a simple, pragmatic and extremely fast web framework for Rust.
|
## Features
|
||||||
|
|
||||||
* Supported *HTTP/1.x* and *HTTP/2.0* protocols
|
* Supports *HTTP/1.x* and *HTTP/2*
|
||||||
* Streaming and pipelining
|
* Streaming and pipelining
|
||||||
* Keep-alive and slow requests handling
|
* Keep-alive and slow requests handling
|
||||||
* Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
* Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
||||||
* Transparent content compression/decompression (br, gzip, deflate)
|
* Transparent content compression/decompression (br, gzip, deflate)
|
||||||
* Configurable [request routing](https://actix.rs/docs/url-dispatch/)
|
* Powerful [request routing](https://actix.rs/docs/url-dispatch/)
|
||||||
* Multipart streams
|
* Multipart streams
|
||||||
* Static assets
|
* Static assets
|
||||||
* SSL support with OpenSSL or Rustls
|
* SSL support using OpenSSL or Rustls
|
||||||
* Middlewares ([Logger, Session, CORS, etc](https://actix.rs/docs/middleware/))
|
* Middlewares ([Logger, Session, CORS, etc](https://actix.rs/docs/middleware/))
|
||||||
* Includes an asynchronous [HTTP client](https://actix.rs/actix-web/actix_web/client/index.html)
|
* Includes an async [HTTP client](https://actix.rs/actix-web/actix_web/client/index.html)
|
||||||
* Supports [Actix actor framework](https://github.com/actix/actix)
|
* Supports [Actix actor framework](https://github.com/actix/actix)
|
||||||
* Supports Rust 1.40+
|
* Runs on stable Rust 1.42+
|
||||||
|
|
||||||
## Docs
|
## Documentation
|
||||||
|
|
||||||
* [API documentation (master)](https://actix.rs/actix-web/actix_web)
|
* [Website & User Guide](https://actix.rs)
|
||||||
* [API documentation (docs.rs)](https://docs.rs/actix-web)
|
* [Examples Repository](https://actix.rs/actix-web/actix_web)
|
||||||
* [User guide](https://actix.rs)
|
* [API Documentation](https://docs.rs/actix-web)
|
||||||
|
* [API Documentation (master branch)](https://actix.rs/actix-web/actix_web)
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
<h2>
|
||||||
|
WARNING: This example is for the master branch which is currently in beta stages for v3. For
|
||||||
|
Actix web v2 see the <a href="https://actix.rs/docs/getting-started/">getting started guide</a>.
|
||||||
|
</h2>
|
||||||
|
|
||||||
Dependencies:
|
Dependencies:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "2"
|
actix-web = "3"
|
||||||
actix-rt = "1"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Code:
|
Code:
|
||||||
@ -62,11 +61,11 @@ Code:
|
|||||||
use actix_web::{get, web, App, HttpServer, Responder};
|
use actix_web::{get, web, App, HttpServer, Responder};
|
||||||
|
|
||||||
#[get("/{id}/{name}/index.html")]
|
#[get("/{id}/{name}/index.html")]
|
||||||
async fn index(info: web::Path<(u32, String)>) -> impl Responder {
|
async fn index(web::Path((id, name)): web::Path<(u32, String)>) -> impl Responder {
|
||||||
format!("Hello {}! id:{}", info.1, info.0)
|
format!("Hello {}! id:{}", name, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| App::new().service(index))
|
HttpServer::new(|| App::new().service(index))
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
@ -77,37 +76,39 @@ async fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
### More examples
|
### More examples
|
||||||
|
|
||||||
* [Basics](https://github.com/actix/examples/tree/master/basics/)
|
* [Basic Setup](https://github.com/actix/examples/tree/master/basics/)
|
||||||
* [Stateful](https://github.com/actix/examples/tree/master/state/)
|
* [Application State](https://github.com/actix/examples/tree/master/state/)
|
||||||
* [Multipart streams](https://github.com/actix/examples/tree/master/multipart/)
|
* [JSON Handling](https://github.com/actix/examples/tree/master/json/)
|
||||||
* [Simple websocket](https://github.com/actix/examples/tree/master/websocket/)
|
* [Multipart Streams](https://github.com/actix/examples/tree/master/multipart/)
|
||||||
* [Tera](https://github.com/actix/examples/tree/master/template_tera/)
|
* [Diesel Integration](https://github.com/actix/examples/tree/master/diesel/)
|
||||||
* [Askama](https://github.com/actix/examples/tree/master/template_askama/) templates
|
* [r2d2 Integration](https://github.com/actix/examples/tree/master/r2d2/)
|
||||||
* [Diesel integration](https://github.com/actix/examples/tree/master/diesel/)
|
* [Simple WebSocket](https://github.com/actix/examples/tree/master/websocket/)
|
||||||
* [r2d2](https://github.com/actix/examples/tree/master/r2d2/)
|
* [Tera Templates](https://github.com/actix/examples/tree/master/template_tera/)
|
||||||
* [OpenSSL](https://github.com/actix/examples/tree/master/openssl/)
|
* [Askama Templates](https://github.com/actix/examples/tree/master/template_askama/)
|
||||||
* [Rustls](https://github.com/actix/examples/tree/master/rustls/)
|
* [HTTPS using Rustls](https://github.com/actix/examples/tree/master/rustls/)
|
||||||
* [Tcp/Websocket chat](https://github.com/actix/examples/tree/master/websocket-chat/)
|
* [HTTPS using OpenSSL](https://github.com/actix/examples/tree/master/openssl/)
|
||||||
* [Json](https://github.com/actix/examples/tree/master/json/)
|
* [WebSocket Chat](https://github.com/actix/examples/tree/master/websocket-chat/)
|
||||||
|
|
||||||
You may consider checking out
|
You may consider checking out
|
||||||
[this directory](https://github.com/actix/examples/tree/master/) for more examples.
|
[this directory](https://github.com/actix/examples/tree/master/) for more examples.
|
||||||
|
|
||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
* [TechEmpower Framework Benchmark](https://www.techempower.com/benchmarks/#section=data-r18)
|
One of the fastest web frameworks available according to the
|
||||||
|
[TechEmpower Framework Benchmark](https://www.techempower.com/benchmarks/#section=data-r19).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This project is licensed under either of
|
This project is licensed under either of
|
||||||
|
|
||||||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))
|
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or [http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT))
|
[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))
|
||||||
|
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
||||||
|
[http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT))
|
||||||
|
|
||||||
at your option.
|
at your option.
|
||||||
|
|
||||||
## Code of Conduct
|
## Code of Conduct
|
||||||
|
|
||||||
Contribution to the actix-web crate is organized under the terms of the
|
Contribution to the actix-web crate is organized under the terms of the Contributor Covenant, the
|
||||||
Contributor Covenant, the maintainer of actix-web, @fafhrd91, promises to
|
maintainers of Actix web, promises to intervene to uphold that code of conduct.
|
||||||
intervene to uphold that code of conduct.
|
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.3.0-alpha.1] - 2020-05-23
|
## [Unreleased] - 2020-xx-xx
|
||||||
|
|
||||||
|
## [0.3.0-beta.1] - 2020-07-15
|
||||||
|
* Update `v_htmlescape` to 0.10
|
||||||
|
* Update `actix-web` and `actix-http` dependencies to beta.1
|
||||||
|
|
||||||
|
## [0.3.0-alpha.1] - 2020-05-23
|
||||||
* Update `actix-web` and `actix-http` dependencies to alpha
|
* Update `actix-web` and `actix-http` dependencies to alpha
|
||||||
* Fix some typos in the docs
|
* Fix some typos in the docs
|
||||||
* Bump minimum supported Rust version to 1.40
|
* Bump minimum supported Rust version to 1.40
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.3.0-alpha.1"
|
version = "0.3.0-beta.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Static files support for actix web."
|
description = "Static files support for actix web."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -9,17 +9,16 @@ homepage = "https://actix.rs"
|
|||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web.git"
|
||||||
documentation = "https://docs.rs/actix-files/"
|
documentation = "https://docs.rs/actix-files/"
|
||||||
categories = ["asynchronous", "web-programming::http-server"]
|
categories = ["asynchronous", "web-programming::http-server"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_files"
|
name = "actix_files"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "3.0.0-alpha.3", default-features = false }
|
actix-web = { version = "3.0.0-beta.1", default-features = false }
|
||||||
actix-http = "2.0.0-alpha.4"
|
actix-http = "2.0.0-beta.1"
|
||||||
actix-service = "1.0.1"
|
actix-service = "1.0.1"
|
||||||
bitflags = "1"
|
bitflags = "1"
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
@ -30,8 +29,8 @@ log = "0.4"
|
|||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
mime_guess = "2.0.1"
|
mime_guess = "2.0.1"
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
v_htmlescape = "0.4"
|
v_htmlescape = "0.10"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
actix-web = { version = "3.0.0-alpha.3", features = ["openssl"] }
|
actix-web = { version = "3.0.0-beta.1", features = ["openssl"] }
|
||||||
|
@ -26,7 +26,6 @@ use actix_web::{web, FromRequest, HttpRequest, HttpResponse};
|
|||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use futures_util::future::{ok, ready, Either, FutureExt, LocalBoxFuture, Ready};
|
use futures_util::future::{ok, ready, Either, FutureExt, LocalBoxFuture, Ready};
|
||||||
use mime;
|
|
||||||
use mime_guess::from_ext;
|
use mime_guess::from_ext;
|
||||||
use percent_encoding::{utf8_percent_encode, CONTROLS};
|
use percent_encoding::{utf8_percent_encode, CONTROLS};
|
||||||
use v_htmlescape::escape as escape_html_entity;
|
use v_htmlescape::escape as escape_html_entity;
|
||||||
@ -250,6 +249,8 @@ pub struct Files {
|
|||||||
renderer: Rc<DirectoryRenderer>,
|
renderer: Rc<DirectoryRenderer>,
|
||||||
mime_override: Option<Rc<MimeOverride>>,
|
mime_override: Option<Rc<MimeOverride>>,
|
||||||
file_flags: named::Flags,
|
file_flags: named::Flags,
|
||||||
|
// FIXME: Should re-visit later.
|
||||||
|
#[allow(clippy::redundant_allocation)]
|
||||||
guards: Option<Rc<Box<dyn Guard>>>,
|
guards: Option<Rc<Box<dyn Guard>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -462,6 +463,8 @@ pub struct FilesService {
|
|||||||
renderer: Rc<DirectoryRenderer>,
|
renderer: Rc<DirectoryRenderer>,
|
||||||
mime_override: Option<Rc<MimeOverride>>,
|
mime_override: Option<Rc<MimeOverride>>,
|
||||||
file_flags: named::Flags,
|
file_flags: named::Flags,
|
||||||
|
// FIXME: Should re-visit later.
|
||||||
|
#[allow(clippy::redundant_allocation)]
|
||||||
guards: Option<Rc<Box<dyn Guard>>>,
|
guards: Option<Rc<Box<dyn Guard>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -501,11 +504,8 @@ impl Service for FilesService {
|
|||||||
// execute user defined guards
|
// execute user defined guards
|
||||||
(**guard).check(req.head())
|
(**guard).check(req.head())
|
||||||
} else {
|
} else {
|
||||||
// default behaviour
|
// default behavior
|
||||||
match *req.method() {
|
matches!(*req.method(), Method::HEAD | Method::GET)
|
||||||
Method::HEAD | Method::GET => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if !is_method_valid {
|
if !is_method_valid {
|
||||||
@ -952,9 +952,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_range_headers() {
|
async fn test_named_file_content_range_headers() {
|
||||||
let srv = test::start(|| {
|
let srv = test::start(|| App::new().service(Files::new("/", ".")));
|
||||||
App::new().service(Files::new("/", "."))
|
|
||||||
});
|
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
let response = srv
|
let response = srv
|
||||||
@ -979,9 +977,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_length_headers() {
|
async fn test_named_file_content_length_headers() {
|
||||||
let srv = test::start(|| {
|
let srv = test::start(|| App::new().service(Files::new("/", ".")));
|
||||||
App::new().service(Files::new("/", "."))
|
|
||||||
});
|
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
let response = srv
|
let response = srv
|
||||||
@ -1020,15 +1016,9 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_head_content_length_headers() {
|
async fn test_head_content_length_headers() {
|
||||||
let srv = test::start(|| {
|
let srv = test::start(|| App::new().service(Files::new("/", ".")));
|
||||||
App::new().service(Files::new("/", "."))
|
|
||||||
});
|
|
||||||
|
|
||||||
let response = srv
|
let response = srv.head("/tests/test.binary").send().await.unwrap();
|
||||||
.head("/tests/test.binary")
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let content_length = response
|
let content_length = response
|
||||||
.headers()
|
.headers()
|
||||||
@ -1097,12 +1087,10 @@ mod tests {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_encoding() {
|
async fn test_named_file_content_encoding() {
|
||||||
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
||||||
web::resource("/").to(|| {
|
web::resource("/").to(|| async {
|
||||||
async {
|
NamedFile::open("Cargo.toml")
|
||||||
NamedFile::open("Cargo.toml")
|
.unwrap()
|
||||||
.unwrap()
|
.set_content_encoding(header::ContentEncoding::Identity)
|
||||||
.set_content_encoding(header::ContentEncoding::Identity)
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
@ -1119,12 +1107,10 @@ mod tests {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_encoding_gzip() {
|
async fn test_named_file_content_encoding_gzip() {
|
||||||
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
||||||
web::resource("/").to(|| {
|
web::resource("/").to(|| async {
|
||||||
async {
|
NamedFile::open("Cargo.toml")
|
||||||
NamedFile::open("Cargo.toml")
|
.unwrap()
|
||||||
.unwrap()
|
.set_content_encoding(header::ContentEncoding::Gzip)
|
||||||
.set_content_encoding(header::ContentEncoding::Gzip)
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
|
@ -8,7 +8,6 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||||||
use std::os::unix::fs::MetadataExt;
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use mime;
|
|
||||||
use mime_guess::from_path;
|
use mime_guess::from_path;
|
||||||
|
|
||||||
use actix_http::body::SizedStream;
|
use actix_http::body::SizedStream;
|
||||||
@ -90,7 +89,7 @@ impl NamedFile {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let ct = from_path(&path).first_or_octet_stream();
|
let ct = from_path(&path).first_or_octet_stream();
|
||||||
let disposition_type = match ct.type_() {
|
let disposition = match ct.type_() {
|
||||||
mime::IMAGE | mime::TEXT | mime::VIDEO => DispositionType::Inline,
|
mime::IMAGE | mime::TEXT | mime::VIDEO => DispositionType::Inline,
|
||||||
_ => DispositionType::Attachment,
|
_ => DispositionType::Attachment,
|
||||||
};
|
};
|
||||||
@ -104,8 +103,8 @@ impl NamedFile {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
let cd = ContentDisposition {
|
let cd = ContentDisposition {
|
||||||
disposition: disposition_type,
|
disposition,
|
||||||
parameters: parameters,
|
parameters,
|
||||||
};
|
};
|
||||||
(ct, cd)
|
(ct, cd)
|
||||||
};
|
};
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "actix-framed"
|
|
||||||
version = "0.3.0"
|
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
|
||||||
description = "Actix framed app server"
|
|
||||||
readme = "README.md"
|
|
||||||
keywords = ["http", "web", "framework", "async", "futures"]
|
|
||||||
homepage = "https://actix.rs"
|
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
|
||||||
documentation = "https://docs.rs/actix-framed/"
|
|
||||||
categories = ["network-programming", "asynchronous",
|
|
||||||
"web-programming::http-server",
|
|
||||||
"web-programming::websocket"]
|
|
||||||
license = "MIT/Apache-2.0"
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "actix_framed"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
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.4"
|
|
||||||
|
|
||||||
bytes = "0.5.3"
|
|
||||||
futures-util = { version = "0.3.5", default-features = false }
|
|
||||||
pin-project = "0.4.6"
|
|
||||||
log = "0.4"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
actix-server = "1.0.0"
|
|
||||||
actix-connect = { version = "2.0.0-alpha.2", features = ["openssl"] }
|
|
||||||
actix-http-test = { version = "2.0.0-alpha.1", features = ["openssl"] }
|
|
||||||
actix-utils = "1.0.3"
|
|
@ -1,201 +0,0 @@
|
|||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright 2017-NOW Nikolay Kim
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
@ -1,25 +0,0 @@
|
|||||||
Copyright (c) 2017 Nikolay Kim
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any
|
|
||||||
person obtaining a copy of this software and associated
|
|
||||||
documentation files (the "Software"), to deal in the
|
|
||||||
Software without restriction, including without
|
|
||||||
limitation the rights to use, copy, modify, merge,
|
|
||||||
publish, distribute, sublicense, and/or sell copies of
|
|
||||||
the Software, and to permit persons to whom the Software
|
|
||||||
is furnished to do so, subject to the following
|
|
||||||
conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice
|
|
||||||
shall be included in all copies or substantial portions
|
|
||||||
of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
||||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
||||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
|
||||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
||||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
|
||||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
DEALINGS IN THE SOFTWARE.
|
|
@ -1,8 +1,3 @@
|
|||||||
# Framed app for actix web [](https://travis-ci.org/actix/actix-web) [](https://codecov.io/gh/actix/actix-web) [](https://crates.io/crates/actix-framed) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
# Framed app for actix web
|
||||||
|
|
||||||
## Documentation & community resources
|
**This crate has been deprecated and removed.**
|
||||||
|
|
||||||
* [API Documentation](https://docs.rs/actix-framed/)
|
|
||||||
* [Chat on gitter](https://gitter.im/actix/actix)
|
|
||||||
* Cargo package: [actix-framed](https://crates.io/crates/actix-framed)
|
|
||||||
* Minimum supported Rust version: 1.40 or later
|
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
# Changes
|
|
||||||
|
|
||||||
## [Unreleased] - 2020-xx-xx
|
|
||||||
|
|
||||||
* Bump minimum supported Rust version to 1.40
|
|
||||||
|
|
||||||
## [0.3.0] - 2019-12-25
|
|
||||||
|
|
||||||
* Migrate to actix-http 1.0
|
|
||||||
|
|
||||||
## [0.2.1] - 2019-07-20
|
|
||||||
|
|
||||||
* Remove unneeded actix-utils dependency
|
|
||||||
|
|
||||||
|
|
||||||
## [0.2.0] - 2019-05-12
|
|
||||||
|
|
||||||
* Update dependencies
|
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0] - 2019-04-16
|
|
||||||
|
|
||||||
* Update tests
|
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0-alpha.1] - 2019-04-12
|
|
||||||
|
|
||||||
* Initial release
|
|
@ -1,221 +0,0 @@
|
|||||||
use std::future::Future;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
||||||
use actix_http::h1::{Codec, SendResponse};
|
|
||||||
use actix_http::{Error, Request, Response};
|
|
||||||
use actix_router::{Path, Router, Url};
|
|
||||||
use actix_service::{IntoServiceFactory, Service, ServiceFactory};
|
|
||||||
use futures_util::future::{ok, FutureExt, LocalBoxFuture};
|
|
||||||
|
|
||||||
use crate::helpers::{BoxedHttpNewService, BoxedHttpService, HttpNewService};
|
|
||||||
use crate::request::FramedRequest;
|
|
||||||
use crate::state::State;
|
|
||||||
|
|
||||||
type BoxedResponse = LocalBoxFuture<'static, Result<(), Error>>;
|
|
||||||
|
|
||||||
pub trait HttpServiceFactory {
|
|
||||||
type Factory: ServiceFactory;
|
|
||||||
|
|
||||||
fn path(&self) -> &str;
|
|
||||||
|
|
||||||
fn create(self) -> Self::Factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Application builder
|
|
||||||
pub struct FramedApp<T, S = ()> {
|
|
||||||
state: State<S>,
|
|
||||||
services: Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: 'static> FramedApp<T, ()> {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
FramedApp {
|
|
||||||
state: State::new(()),
|
|
||||||
services: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: 'static, S: 'static> FramedApp<T, S> {
|
|
||||||
pub fn with(state: S) -> FramedApp<T, S> {
|
|
||||||
FramedApp {
|
|
||||||
services: Vec::new(),
|
|
||||||
state: State::new(state),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn service<U>(mut self, factory: U) -> Self
|
|
||||||
where
|
|
||||||
U: HttpServiceFactory,
|
|
||||||
U::Factory: ServiceFactory<
|
|
||||||
Config = (),
|
|
||||||
Request = FramedRequest<T, S>,
|
|
||||||
Response = (),
|
|
||||||
Error = Error,
|
|
||||||
InitError = (),
|
|
||||||
> + 'static,
|
|
||||||
<U::Factory as ServiceFactory>::Future: 'static,
|
|
||||||
<U::Factory as ServiceFactory>::Service: Service<
|
|
||||||
Request = FramedRequest<T, S>,
|
|
||||||
Response = (),
|
|
||||||
Error = Error,
|
|
||||||
Future = LocalBoxFuture<'static, Result<(), Error>>,
|
|
||||||
>,
|
|
||||||
{
|
|
||||||
let path = factory.path().to_string();
|
|
||||||
self.services
|
|
||||||
.push((path, Box::new(HttpNewService::new(factory.create()))));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S> IntoServiceFactory<FramedAppFactory<T, S>> for FramedApp<T, S>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
S: 'static,
|
|
||||||
{
|
|
||||||
fn into_factory(self) -> FramedAppFactory<T, S> {
|
|
||||||
FramedAppFactory {
|
|
||||||
state: self.state,
|
|
||||||
services: Rc::new(self.services),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct FramedAppFactory<T, S> {
|
|
||||||
state: State<S>,
|
|
||||||
services: Rc<Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S> ServiceFactory for FramedAppFactory<T, S>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
S: 'static,
|
|
||||||
{
|
|
||||||
type Config = ();
|
|
||||||
type Request = (Request, Framed<T, Codec>);
|
|
||||||
type Response = ();
|
|
||||||
type Error = Error;
|
|
||||||
type InitError = ();
|
|
||||||
type Service = FramedAppService<T, S>;
|
|
||||||
type Future = CreateService<T, S>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: ()) -> Self::Future {
|
|
||||||
CreateService {
|
|
||||||
fut: self
|
|
||||||
.services
|
|
||||||
.iter()
|
|
||||||
.map(|(path, service)| {
|
|
||||||
CreateServiceItem::Future(
|
|
||||||
Some(path.clone()),
|
|
||||||
service.new_service(()),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
state: self.state.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub struct CreateService<T, S> {
|
|
||||||
fut: Vec<CreateServiceItem<T, S>>,
|
|
||||||
state: State<S>,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum CreateServiceItem<T, S> {
|
|
||||||
Future(
|
|
||||||
Option<String>,
|
|
||||||
LocalBoxFuture<'static, Result<BoxedHttpService<FramedRequest<T, S>>, ()>>,
|
|
||||||
),
|
|
||||||
Service(String, BoxedHttpService<FramedRequest<T, S>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: 'static, T: 'static> Future for CreateService<T, S>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin,
|
|
||||||
{
|
|
||||||
type Output = Result<FramedAppService<T, S>, ()>;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
||||||
let mut done = true;
|
|
||||||
|
|
||||||
// poll http services
|
|
||||||
for item in &mut self.fut {
|
|
||||||
let res = match item {
|
|
||||||
CreateServiceItem::Future(ref mut path, ref mut fut) => {
|
|
||||||
match Pin::new(fut).poll(cx) {
|
|
||||||
Poll::Ready(Ok(service)) => {
|
|
||||||
Some((path.take().unwrap(), service))
|
|
||||||
}
|
|
||||||
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
|
|
||||||
Poll::Pending => {
|
|
||||||
done = false;
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CreateServiceItem::Service(_, _) => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some((path, service)) = res {
|
|
||||||
*item = CreateServiceItem::Service(path, service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if done {
|
|
||||||
let router = self
|
|
||||||
.fut
|
|
||||||
.drain(..)
|
|
||||||
.fold(Router::build(), |mut router, item| {
|
|
||||||
match item {
|
|
||||||
CreateServiceItem::Service(path, service) => {
|
|
||||||
router.path(&path, service);
|
|
||||||
}
|
|
||||||
CreateServiceItem::Future(_, _) => unreachable!(),
|
|
||||||
}
|
|
||||||
router
|
|
||||||
});
|
|
||||||
Poll::Ready(Ok(FramedAppService {
|
|
||||||
router: router.finish(),
|
|
||||||
state: self.state.clone(),
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FramedAppService<T, S> {
|
|
||||||
state: State<S>,
|
|
||||||
router: Router<BoxedHttpService<FramedRequest<T, S>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: 'static, T: 'static> Service for FramedAppService<T, S>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin,
|
|
||||||
{
|
|
||||||
type Request = (Request, Framed<T, Codec>);
|
|
||||||
type Response = ();
|
|
||||||
type Error = Error;
|
|
||||||
type Future = BoxedResponse;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, (req, framed): (Request, Framed<T, Codec>)) -> Self::Future {
|
|
||||||
let mut path = Path::new(Url::new(req.uri().clone()));
|
|
||||||
|
|
||||||
if let Some((srv, _info)) = self.router.recognize_mut(&mut path) {
|
|
||||||
return srv.call(FramedRequest::new(req, framed, path, self.state.clone()));
|
|
||||||
}
|
|
||||||
SendResponse::new(framed, Response::NotFound().finish())
|
|
||||||
.then(|_| ok(()))
|
|
||||||
.boxed_local()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,98 +0,0 @@
|
|||||||
use std::task::{Context, Poll};
|
|
||||||
|
|
||||||
use actix_http::Error;
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
|
||||||
use futures_util::future::{FutureExt, LocalBoxFuture};
|
|
||||||
|
|
||||||
pub(crate) type BoxedHttpService<Req> = Box<
|
|
||||||
dyn Service<
|
|
||||||
Request = Req,
|
|
||||||
Response = (),
|
|
||||||
Error = Error,
|
|
||||||
Future = LocalBoxFuture<'static, Result<(), Error>>,
|
|
||||||
>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
pub(crate) type BoxedHttpNewService<Req> = Box<
|
|
||||||
dyn ServiceFactory<
|
|
||||||
Config = (),
|
|
||||||
Request = Req,
|
|
||||||
Response = (),
|
|
||||||
Error = Error,
|
|
||||||
InitError = (),
|
|
||||||
Service = BoxedHttpService<Req>,
|
|
||||||
Future = LocalBoxFuture<'static, Result<BoxedHttpService<Req>, ()>>,
|
|
||||||
>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
pub(crate) struct HttpNewService<T: ServiceFactory>(T);
|
|
||||||
|
|
||||||
impl<T> HttpNewService<T>
|
|
||||||
where
|
|
||||||
T: ServiceFactory<Response = (), Error = Error>,
|
|
||||||
T::Response: 'static,
|
|
||||||
T::Future: 'static,
|
|
||||||
T::Service: Service<Future = LocalBoxFuture<'static, Result<(), Error>>> + 'static,
|
|
||||||
<T::Service as Service>::Future: 'static,
|
|
||||||
{
|
|
||||||
pub fn new(service: T) -> Self {
|
|
||||||
HttpNewService(service)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> ServiceFactory for HttpNewService<T>
|
|
||||||
where
|
|
||||||
T: ServiceFactory<Config = (), Response = (), Error = Error>,
|
|
||||||
T::Request: 'static,
|
|
||||||
T::Future: 'static,
|
|
||||||
T::Service: Service<Future = LocalBoxFuture<'static, Result<(), Error>>> + 'static,
|
|
||||||
<T::Service as Service>::Future: 'static,
|
|
||||||
{
|
|
||||||
type Config = ();
|
|
||||||
type Request = T::Request;
|
|
||||||
type Response = ();
|
|
||||||
type Error = Error;
|
|
||||||
type InitError = ();
|
|
||||||
type Service = BoxedHttpService<T::Request>;
|
|
||||||
type Future = LocalBoxFuture<'static, Result<Self::Service, ()>>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: ()) -> Self::Future {
|
|
||||||
let fut = self.0.new_service(());
|
|
||||||
|
|
||||||
async move {
|
|
||||||
fut.await.map_err(|_| ()).map(|service| {
|
|
||||||
let service: BoxedHttpService<_> =
|
|
||||||
Box::new(HttpServiceWrapper { service });
|
|
||||||
service
|
|
||||||
})
|
|
||||||
}
|
|
||||||
.boxed_local()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct HttpServiceWrapper<T: Service> {
|
|
||||||
service: T,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Service for HttpServiceWrapper<T>
|
|
||||||
where
|
|
||||||
T: Service<
|
|
||||||
Response = (),
|
|
||||||
Future = LocalBoxFuture<'static, Result<(), Error>>,
|
|
||||||
Error = Error,
|
|
||||||
>,
|
|
||||||
T::Request: 'static,
|
|
||||||
{
|
|
||||||
type Request = T::Request;
|
|
||||||
type Response = ();
|
|
||||||
type Error = Error;
|
|
||||||
type Future = LocalBoxFuture<'static, Result<(), Error>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
||||||
self.service.poll_ready(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
||||||
self.service.call(req)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
#![allow(clippy::type_complexity, clippy::new_without_default, dead_code)]
|
|
||||||
mod app;
|
|
||||||
mod helpers;
|
|
||||||
mod request;
|
|
||||||
mod route;
|
|
||||||
mod service;
|
|
||||||
mod state;
|
|
||||||
pub mod test;
|
|
||||||
|
|
||||||
// re-export for convenience
|
|
||||||
pub use actix_http::{http, Error, HttpMessage, Response, ResponseError};
|
|
||||||
|
|
||||||
pub use self::app::{FramedApp, FramedAppService};
|
|
||||||
pub use self::request::FramedRequest;
|
|
||||||
pub use self::route::FramedRoute;
|
|
||||||
pub use self::service::{SendError, VerifyWebSockets};
|
|
||||||
pub use self::state::State;
|
|
@ -1,172 +0,0 @@
|
|||||||
use std::cell::{Ref, RefMut};
|
|
||||||
|
|
||||||
use actix_codec::Framed;
|
|
||||||
use actix_http::http::{HeaderMap, Method, Uri, Version};
|
|
||||||
use actix_http::{h1::Codec, Extensions, Request, RequestHead};
|
|
||||||
use actix_router::{Path, Url};
|
|
||||||
|
|
||||||
use crate::state::State;
|
|
||||||
|
|
||||||
pub struct FramedRequest<Io, S = ()> {
|
|
||||||
req: Request,
|
|
||||||
framed: Framed<Io, Codec>,
|
|
||||||
state: State<S>,
|
|
||||||
pub(crate) path: Path<Url>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io, S> FramedRequest<Io, S> {
|
|
||||||
pub fn new(
|
|
||||||
req: Request,
|
|
||||||
framed: Framed<Io, Codec>,
|
|
||||||
path: Path<Url>,
|
|
||||||
state: State<S>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
req,
|
|
||||||
framed,
|
|
||||||
state,
|
|
||||||
path,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io, S> FramedRequest<Io, S> {
|
|
||||||
/// Split request into a parts
|
|
||||||
pub fn into_parts(self) -> (Request, Framed<Io, Codec>, State<S>) {
|
|
||||||
(self.req, self.framed, self.state)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method returns reference to the request head
|
|
||||||
#[inline]
|
|
||||||
pub fn head(&self) -> &RequestHead {
|
|
||||||
self.req.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 {
|
|
||||||
self.req.head_mut()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shared application state
|
|
||||||
#[inline]
|
|
||||||
pub fn state(&self) -> &S {
|
|
||||||
self.state.get_ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Request's uri.
|
|
||||||
#[inline]
|
|
||||||
pub fn uri(&self) -> &Uri {
|
|
||||||
&self.head().uri
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read the Request method.
|
|
||||||
#[inline]
|
|
||||||
pub fn method(&self) -> &Method {
|
|
||||||
&self.head().method
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read the Request Version.
|
|
||||||
#[inline]
|
|
||||||
pub fn version(&self) -> Version {
|
|
||||||
self.head().version
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Returns request's headers.
|
|
||||||
pub fn headers(&self) -> &HeaderMap {
|
|
||||||
&self.head().headers
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The target path of this Request.
|
|
||||||
#[inline]
|
|
||||||
pub fn path(&self) -> &str {
|
|
||||||
self.head().uri.path()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The query string in the URL.
|
|
||||||
///
|
|
||||||
/// E.g., id=10
|
|
||||||
#[inline]
|
|
||||||
pub fn query_string(&self) -> &str {
|
|
||||||
if let Some(query) = self.uri().query().as_ref() {
|
|
||||||
query
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a reference to the Path parameters.
|
|
||||||
///
|
|
||||||
/// Params is a container for url parameters.
|
|
||||||
/// A variable segment is specified in the form `{identifier}`,
|
|
||||||
/// where the identifier can be used later in a request handler to
|
|
||||||
/// access the matched value for that segment.
|
|
||||||
#[inline]
|
|
||||||
pub fn match_info(&self) -> &Path<Url> {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Request extensions
|
|
||||||
#[inline]
|
|
||||||
pub fn extensions(&self) -> Ref<Extensions> {
|
|
||||||
self.head().extensions()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Mutable reference to a the request's extensions
|
|
||||||
#[inline]
|
|
||||||
pub fn extensions_mut(&self) -> RefMut<Extensions> {
|
|
||||||
self.head().extensions_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
|
|
||||||
use actix_http::http::{HeaderName, HeaderValue};
|
|
||||||
use actix_http::test::{TestBuffer, TestRequest};
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_request() {
|
|
||||||
let buf = TestBuffer::empty();
|
|
||||||
let framed = Framed::new(buf, Codec::default());
|
|
||||||
let req = TestRequest::with_uri("/index.html?q=1")
|
|
||||||
.header("content-type", "test")
|
|
||||||
.finish();
|
|
||||||
let path = Path::new(Url::new(req.uri().clone()));
|
|
||||||
|
|
||||||
let mut freq = FramedRequest::new(req, framed, path, State::new(10u8));
|
|
||||||
assert_eq!(*freq.state(), 10);
|
|
||||||
assert_eq!(freq.version(), Version::HTTP_11);
|
|
||||||
assert_eq!(freq.method(), Method::GET);
|
|
||||||
assert_eq!(freq.path(), "/index.html");
|
|
||||||
assert_eq!(freq.query_string(), "q=1");
|
|
||||||
assert_eq!(
|
|
||||||
freq.headers()
|
|
||||||
.get("content-type")
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap(),
|
|
||||||
"test"
|
|
||||||
);
|
|
||||||
|
|
||||||
freq.head_mut().headers.insert(
|
|
||||||
HeaderName::try_from("x-hdr").unwrap(),
|
|
||||||
HeaderValue::from_static("test"),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
freq.headers().get("x-hdr").unwrap().to_str().unwrap(),
|
|
||||||
"test"
|
|
||||||
);
|
|
||||||
|
|
||||||
freq.extensions_mut().insert(100usize);
|
|
||||||
assert_eq!(*freq.extensions().get::<usize>().unwrap(), 100usize);
|
|
||||||
|
|
||||||
let (_, _, state) = freq.into_parts();
|
|
||||||
assert_eq!(*state, 10);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,159 +0,0 @@
|
|||||||
use std::fmt;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
|
||||||
use actix_http::{http::Method, Error};
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
|
||||||
use futures_util::future::{ok, FutureExt, LocalBoxFuture, Ready};
|
|
||||||
use log::error;
|
|
||||||
|
|
||||||
use crate::app::HttpServiceFactory;
|
|
||||||
use crate::request::FramedRequest;
|
|
||||||
|
|
||||||
/// Resource route definition
|
|
||||||
///
|
|
||||||
/// Route uses builder-like pattern for configuration.
|
|
||||||
/// If handler is not explicitly set, default *404 Not Found* handler is used.
|
|
||||||
pub struct FramedRoute<Io, S, F = (), R = (), E = ()> {
|
|
||||||
handler: F,
|
|
||||||
pattern: String,
|
|
||||||
methods: Vec<Method>,
|
|
||||||
state: PhantomData<(Io, S, R, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io, S> FramedRoute<Io, S> {
|
|
||||||
pub fn new(pattern: &str) -> Self {
|
|
||||||
FramedRoute {
|
|
||||||
handler: (),
|
|
||||||
pattern: pattern.to_string(),
|
|
||||||
methods: Vec::new(),
|
|
||||||
state: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(path: &str) -> FramedRoute<Io, S> {
|
|
||||||
FramedRoute::new(path).method(Method::GET)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn post(path: &str) -> FramedRoute<Io, S> {
|
|
||||||
FramedRoute::new(path).method(Method::POST)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put(path: &str) -> FramedRoute<Io, S> {
|
|
||||||
FramedRoute::new(path).method(Method::PUT)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete(path: &str) -> FramedRoute<Io, S> {
|
|
||||||
FramedRoute::new(path).method(Method::DELETE)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn method(mut self, method: Method) -> Self {
|
|
||||||
self.methods.push(method);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to<F, R, E>(self, handler: F) -> FramedRoute<Io, S, F, R, E>
|
|
||||||
where
|
|
||||||
F: FnMut(FramedRequest<Io, S>) -> R,
|
|
||||||
R: Future<Output = Result<(), E>> + 'static,
|
|
||||||
|
|
||||||
E: fmt::Debug,
|
|
||||||
{
|
|
||||||
FramedRoute {
|
|
||||||
handler,
|
|
||||||
pattern: self.pattern,
|
|
||||||
methods: self.methods,
|
|
||||||
state: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io, S, F, R, E> HttpServiceFactory for FramedRoute<Io, S, F, R, E>
|
|
||||||
where
|
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
|
||||||
F: FnMut(FramedRequest<Io, S>) -> R + Clone,
|
|
||||||
R: Future<Output = Result<(), E>> + 'static,
|
|
||||||
E: fmt::Display,
|
|
||||||
{
|
|
||||||
type Factory = FramedRouteFactory<Io, S, F, R, E>;
|
|
||||||
|
|
||||||
fn path(&self) -> &str {
|
|
||||||
&self.pattern
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create(self) -> Self::Factory {
|
|
||||||
FramedRouteFactory {
|
|
||||||
handler: self.handler,
|
|
||||||
methods: self.methods,
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FramedRouteFactory<Io, S, F, R, E> {
|
|
||||||
handler: F,
|
|
||||||
methods: Vec<Method>,
|
|
||||||
_t: PhantomData<(Io, S, R, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io, S, F, R, E> ServiceFactory for FramedRouteFactory<Io, S, F, R, E>
|
|
||||||
where
|
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
|
||||||
F: FnMut(FramedRequest<Io, S>) -> R + Clone,
|
|
||||||
R: Future<Output = Result<(), E>> + 'static,
|
|
||||||
E: fmt::Display,
|
|
||||||
{
|
|
||||||
type Config = ();
|
|
||||||
type Request = FramedRequest<Io, S>;
|
|
||||||
type Response = ();
|
|
||||||
type Error = Error;
|
|
||||||
type InitError = ();
|
|
||||||
type Service = FramedRouteService<Io, S, F, R, E>;
|
|
||||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: ()) -> Self::Future {
|
|
||||||
ok(FramedRouteService {
|
|
||||||
handler: self.handler.clone(),
|
|
||||||
methods: self.methods.clone(),
|
|
||||||
_t: PhantomData,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FramedRouteService<Io, S, F, R, E> {
|
|
||||||
handler: F,
|
|
||||||
methods: Vec<Method>,
|
|
||||||
_t: PhantomData<(Io, S, R, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io, S, F, R, E> Service for FramedRouteService<Io, S, F, R, E>
|
|
||||||
where
|
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
|
||||||
F: FnMut(FramedRequest<Io, S>) -> R + Clone,
|
|
||||||
R: Future<Output = Result<(), E>> + 'static,
|
|
||||||
E: fmt::Display,
|
|
||||||
{
|
|
||||||
type Request = FramedRequest<Io, S>;
|
|
||||||
type Response = ();
|
|
||||||
type Error = Error;
|
|
||||||
type Future = LocalBoxFuture<'static, Result<(), Error>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: FramedRequest<Io, S>) -> Self::Future {
|
|
||||||
let fut = (self.handler)(req);
|
|
||||||
|
|
||||||
async move {
|
|
||||||
let res = fut.await;
|
|
||||||
if let Err(e) = res {
|
|
||||||
error!("Error in request handler: {}", e);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
.boxed_local()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,156 +0,0 @@
|
|||||||
use std::future::Future;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
||||||
use actix_http::body::BodySize;
|
|
||||||
use actix_http::error::ResponseError;
|
|
||||||
use actix_http::h1::{Codec, Message};
|
|
||||||
use actix_http::ws::{verify_handshake, HandshakeError};
|
|
||||||
use actix_http::{Request, Response};
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
|
||||||
use futures_util::future::{err, ok, Either, Ready};
|
|
||||||
|
|
||||||
/// Service that verifies incoming request if it is valid websocket
|
|
||||||
/// upgrade request. In case of error returns `HandshakeError`
|
|
||||||
pub struct VerifyWebSockets<T, C> {
|
|
||||||
_t: PhantomData<(T, C)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, C> Default for VerifyWebSockets<T, C> {
|
|
||||||
fn default() -> Self {
|
|
||||||
VerifyWebSockets { _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, C> ServiceFactory for VerifyWebSockets<T, C> {
|
|
||||||
type Config = C;
|
|
||||||
type Request = (Request, Framed<T, Codec>);
|
|
||||||
type Response = (Request, Framed<T, Codec>);
|
|
||||||
type Error = (HandshakeError, Framed<T, Codec>);
|
|
||||||
type InitError = ();
|
|
||||||
type Service = VerifyWebSockets<T, C>;
|
|
||||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: C) -> Self::Future {
|
|
||||||
ok(VerifyWebSockets { _t: PhantomData })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, C> Service for VerifyWebSockets<T, C> {
|
|
||||||
type Request = (Request, Framed<T, Codec>);
|
|
||||||
type Response = (Request, Framed<T, Codec>);
|
|
||||||
type Error = (HandshakeError, Framed<T, Codec>);
|
|
||||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, (req, framed): (Request, Framed<T, Codec>)) -> Self::Future {
|
|
||||||
match verify_handshake(req.head()) {
|
|
||||||
Err(e) => err((e, framed)),
|
|
||||||
Ok(_) => ok((req, framed)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send http/1 error response
|
|
||||||
pub struct SendError<T, R, E, C>(PhantomData<(T, R, E, C)>);
|
|
||||||
|
|
||||||
impl<T, R, E, C> Default for SendError<T, R, E, C>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite,
|
|
||||||
E: ResponseError,
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
|
||||||
SendError(PhantomData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, R, E, C> ServiceFactory for SendError<T, R, E, C>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
R: 'static,
|
|
||||||
E: ResponseError + 'static,
|
|
||||||
{
|
|
||||||
type Config = C;
|
|
||||||
type Request = Result<R, (E, Framed<T, Codec>)>;
|
|
||||||
type Response = R;
|
|
||||||
type Error = (E, Framed<T, Codec>);
|
|
||||||
type InitError = ();
|
|
||||||
type Service = SendError<T, R, E, C>;
|
|
||||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: C) -> Self::Future {
|
|
||||||
ok(SendError(PhantomData))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, R, E, C> Service for SendError<T, R, E, C>
|
|
||||||
where
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
||||||
R: 'static,
|
|
||||||
E: ResponseError + 'static,
|
|
||||||
{
|
|
||||||
type Request = Result<R, (E, Framed<T, Codec>)>;
|
|
||||||
type Response = R;
|
|
||||||
type Error = (E, Framed<T, Codec>);
|
|
||||||
type Future = Either<Ready<Result<R, (E, Framed<T, Codec>)>>, SendErrorFut<T, R, E>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Result<R, (E, Framed<T, Codec>)>) -> Self::Future {
|
|
||||||
match req {
|
|
||||||
Ok(r) => Either::Left(ok(r)),
|
|
||||||
Err((e, framed)) => {
|
|
||||||
let res = e.error_response().drop_body();
|
|
||||||
Either::Right(SendErrorFut {
|
|
||||||
framed: Some(framed),
|
|
||||||
res: Some((res, BodySize::Empty).into()),
|
|
||||||
err: Some(e),
|
|
||||||
_t: PhantomData,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
|
||||||
pub struct SendErrorFut<T, R, E> {
|
|
||||||
res: Option<Message<(Response<()>, BodySize)>>,
|
|
||||||
framed: Option<Framed<T, Codec>>,
|
|
||||||
err: Option<E>,
|
|
||||||
_t: PhantomData<R>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, R, E> Future for SendErrorFut<T, R, E>
|
|
||||||
where
|
|
||||||
E: ResponseError,
|
|
||||||
T: AsyncRead + AsyncWrite + Unpin,
|
|
||||||
{
|
|
||||||
type Output = Result<R, (E, Framed<T, Codec>)>;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
||||||
if let Some(res) = self.res.take() {
|
|
||||||
if self.framed.as_mut().unwrap().write(res).is_err() {
|
|
||||||
return Poll::Ready(Err((
|
|
||||||
self.err.take().unwrap(),
|
|
||||||
self.framed.take().unwrap(),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match self.framed.as_mut().unwrap().flush(cx) {
|
|
||||||
Poll::Ready(Ok(_)) => {
|
|
||||||
Poll::Ready(Err((self.err.take().unwrap(), self.framed.take().unwrap())))
|
|
||||||
}
|
|
||||||
Poll::Ready(Err(_)) => {
|
|
||||||
Poll::Ready(Err((self.err.take().unwrap(), self.framed.take().unwrap())))
|
|
||||||
}
|
|
||||||
Poll::Pending => Poll::Pending,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
use std::ops::Deref;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
/// Application state
|
|
||||||
pub struct State<S>(Arc<S>);
|
|
||||||
|
|
||||||
impl<S> State<S> {
|
|
||||||
pub fn new(state: S) -> State<S> {
|
|
||||||
State(Arc::new(state))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_ref(&self) -> &S {
|
|
||||||
self.0.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> Deref for State<S> {
|
|
||||||
type Target = S;
|
|
||||||
|
|
||||||
fn deref(&self) -> &S {
|
|
||||||
self.0.as_ref()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> Clone for State<S> {
|
|
||||||
fn clone(&self) -> State<S> {
|
|
||||||
State(self.0.clone())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,155 +0,0 @@
|
|||||||
//! Various helpers for Actix applications to use during testing.
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::future::Future;
|
|
||||||
|
|
||||||
use actix_codec::Framed;
|
|
||||||
use actix_http::h1::Codec;
|
|
||||||
use actix_http::http::header::{Header, HeaderName, IntoHeaderValue};
|
|
||||||
use actix_http::http::{Error as HttpError, Method, Uri, Version};
|
|
||||||
use actix_http::test::{TestBuffer, TestRequest as HttpTestRequest};
|
|
||||||
use actix_router::{Path, Url};
|
|
||||||
|
|
||||||
use crate::{FramedRequest, State};
|
|
||||||
|
|
||||||
/// Test `Request` builder.
|
|
||||||
pub struct TestRequest<S = ()> {
|
|
||||||
req: HttpTestRequest,
|
|
||||||
path: Path<Url>,
|
|
||||||
state: State<S>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for TestRequest<()> {
|
|
||||||
fn default() -> TestRequest {
|
|
||||||
TestRequest {
|
|
||||||
req: HttpTestRequest::default(),
|
|
||||||
path: Path::new(Url::new(Uri::default())),
|
|
||||||
state: State::new(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestRequest<()> {
|
|
||||||
/// Create TestRequest and set request uri
|
|
||||||
pub fn with_uri(path: &str) -> Self {
|
|
||||||
Self::get().uri(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create TestRequest and set header
|
|
||||||
pub fn with_hdr<H: Header>(hdr: H) -> Self {
|
|
||||||
Self::default().set(hdr)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create TestRequest and set header
|
|
||||||
pub fn with_header<K, V>(key: K, value: V) -> Self
|
|
||||||
where
|
|
||||||
HeaderName: TryFrom<K>,
|
|
||||||
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
|
||||||
V: IntoHeaderValue,
|
|
||||||
{
|
|
||||||
Self::default().header(key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create TestRequest and set method to `Method::GET`
|
|
||||||
pub fn get() -> Self {
|
|
||||||
Self::default().method(Method::GET)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create TestRequest and set method to `Method::POST`
|
|
||||||
pub fn post() -> Self {
|
|
||||||
Self::default().method(Method::POST)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> TestRequest<S> {
|
|
||||||
/// Create TestRequest and set request uri
|
|
||||||
pub fn with_state(state: S) -> TestRequest<S> {
|
|
||||||
let req = TestRequest::get();
|
|
||||||
TestRequest {
|
|
||||||
state: State::new(state),
|
|
||||||
req: req.req,
|
|
||||||
path: req.path,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set HTTP version of this request
|
|
||||||
pub fn version(mut self, ver: Version) -> Self {
|
|
||||||
self.req.version(ver);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set HTTP method of this request
|
|
||||||
pub fn method(mut self, meth: Method) -> Self {
|
|
||||||
self.req.method(meth);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set HTTP Uri of this request
|
|
||||||
pub fn uri(mut self, path: &str) -> Self {
|
|
||||||
self.req.uri(path);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set a header
|
|
||||||
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
|
||||||
self.req.set(hdr);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set a header
|
|
||||||
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
|
||||||
where
|
|
||||||
HeaderName: TryFrom<K>,
|
|
||||||
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
|
||||||
V: IntoHeaderValue,
|
|
||||||
{
|
|
||||||
self.req.header(key, value);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set request path pattern parameter
|
|
||||||
pub fn param(mut self, name: &'static str, value: &'static str) -> Self {
|
|
||||||
self.path.add_static(name, value);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Complete request creation and generate `Request` instance
|
|
||||||
pub fn finish(mut self) -> FramedRequest<TestBuffer, S> {
|
|
||||||
let req = self.req.finish();
|
|
||||||
self.path.get_mut().update(req.uri());
|
|
||||||
let framed = Framed::new(TestBuffer::empty(), Codec::default());
|
|
||||||
FramedRequest::new(req, framed, self.path, self.state)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method generates `FramedRequest` instance and executes async handler
|
|
||||||
pub async fn run<F, R, I, E>(self, f: F) -> Result<I, E>
|
|
||||||
where
|
|
||||||
F: FnOnce(FramedRequest<TestBuffer, S>) -> R,
|
|
||||||
R: Future<Output = Result<I, E>>,
|
|
||||||
{
|
|
||||||
f(self.finish()).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test() {
|
|
||||||
let req = TestRequest::with_uri("/index.html")
|
|
||||||
.header("x-test", "test")
|
|
||||||
.param("test", "123")
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
assert_eq!(*req.state(), ());
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
|
||||||
assert_eq!(req.method(), Method::GET);
|
|
||||||
assert_eq!(req.path(), "/index.html");
|
|
||||||
assert_eq!(req.query_string(), "");
|
|
||||||
assert_eq!(
|
|
||||||
req.headers().get("x-test").unwrap().to_str().unwrap(),
|
|
||||||
"test"
|
|
||||||
);
|
|
||||||
assert_eq!(&req.match_info()["test"], "123");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,161 +0,0 @@
|
|||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
|
||||||
use actix_http::{body, http::StatusCode, ws, Error, HttpService, Response};
|
|
||||||
use actix_http_test::test_server;
|
|
||||||
use actix_service::{pipeline_factory, IntoServiceFactory, ServiceFactory};
|
|
||||||
use actix_utils::framed::Dispatcher;
|
|
||||||
use bytes::Bytes;
|
|
||||||
use futures_util::{future, SinkExt, StreamExt};
|
|
||||||
|
|
||||||
use actix_framed::{FramedApp, FramedRequest, FramedRoute, SendError, VerifyWebSockets};
|
|
||||||
|
|
||||||
async fn ws_service<T: AsyncRead + AsyncWrite>(
|
|
||||||
req: FramedRequest<T>,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
let (req, mut framed, _) = req.into_parts();
|
|
||||||
let res = ws::handshake(req.head()).unwrap().message_body(());
|
|
||||||
|
|
||||||
framed
|
|
||||||
.send((res, body::BodySize::None).into())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
Dispatcher::new(framed.into_framed(ws::Codec::new()), service)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
|
|
||||||
let msg = match msg {
|
|
||||||
ws::Frame::Ping(msg) => ws::Message::Pong(msg),
|
|
||||||
ws::Frame::Text(text) => {
|
|
||||||
ws::Message::Text(String::from_utf8_lossy(&text).to_string())
|
|
||||||
}
|
|
||||||
ws::Frame::Binary(bin) => ws::Message::Binary(bin),
|
|
||||||
ws::Frame::Close(reason) => ws::Message::Close(reason),
|
|
||||||
_ => panic!(),
|
|
||||||
};
|
|
||||||
Ok(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn test_simple() {
|
|
||||||
let mut srv = test_server(|| {
|
|
||||||
HttpService::build()
|
|
||||||
.upgrade(
|
|
||||||
FramedApp::new().service(FramedRoute::get("/index.html").to(ws_service)),
|
|
||||||
)
|
|
||||||
.finish(|_| future::ok::<_, Error>(Response::NotFound()))
|
|
||||||
.tcp()
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
|
|
||||||
assert!(srv.ws_at("/test").await.is_err());
|
|
||||||
|
|
||||||
// client service
|
|
||||||
let mut framed = srv.ws_at("/index.html").await.unwrap();
|
|
||||||
framed
|
|
||||||
.send(ws::Message::Text("text".to_string()))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let (item, mut framed) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Text(Bytes::from_static(b"text"))
|
|
||||||
);
|
|
||||||
|
|
||||||
framed
|
|
||||||
.send(ws::Message::Binary("text".into()))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let (item, mut framed) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Binary(Bytes::from_static(b"text"))
|
|
||||||
);
|
|
||||||
|
|
||||||
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
|
||||||
let (item, mut framed) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Pong("text".to_string().into())
|
|
||||||
);
|
|
||||||
|
|
||||||
framed
|
|
||||||
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (item, _) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Close(Some(ws::CloseCode::Normal.into()))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn test_service() {
|
|
||||||
let mut srv = test_server(|| {
|
|
||||||
pipeline_factory(actix_http::h1::OneRequest::new().map_err(|_| ())).and_then(
|
|
||||||
pipeline_factory(
|
|
||||||
pipeline_factory(VerifyWebSockets::default())
|
|
||||||
.then(SendError::default())
|
|
||||||
.map_err(|_| ()),
|
|
||||||
)
|
|
||||||
.and_then(
|
|
||||||
FramedApp::new()
|
|
||||||
.service(FramedRoute::get("/index.html").to(ws_service))
|
|
||||||
.into_factory()
|
|
||||||
.map_err(|_| ()),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
|
|
||||||
// non ws request
|
|
||||||
let res = srv.get("/index.html").send().await.unwrap();
|
|
||||||
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
||||||
|
|
||||||
// not found
|
|
||||||
assert!(srv.ws_at("/test").await.is_err());
|
|
||||||
|
|
||||||
// client service
|
|
||||||
let mut framed = srv.ws_at("/index.html").await.unwrap();
|
|
||||||
framed
|
|
||||||
.send(ws::Message::Text("text".to_string()))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let (item, mut framed) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Text(Bytes::from_static(b"text"))
|
|
||||||
);
|
|
||||||
|
|
||||||
framed
|
|
||||||
.send(ws::Message::Binary("text".into()))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let (item, mut framed) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Binary(Bytes::from_static(b"text"))
|
|
||||||
);
|
|
||||||
|
|
||||||
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
|
||||||
let (item, mut framed) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Pong("text".to_string().into())
|
|
||||||
);
|
|
||||||
|
|
||||||
framed
|
|
||||||
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (item, _) = framed.into_future().await;
|
|
||||||
assert_eq!(
|
|
||||||
item.unwrap().unwrap(),
|
|
||||||
ws::Frame::Close(Some(ws::CloseCode::Normal.into()))
|
|
||||||
);
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
environment:
|
|
||||||
global:
|
|
||||||
PROJECT_NAME: actix-http
|
|
||||||
matrix:
|
|
||||||
# Stable channel
|
|
||||||
- TARGET: i686-pc-windows-msvc
|
|
||||||
CHANNEL: stable
|
|
||||||
- TARGET: x86_64-pc-windows-gnu
|
|
||||||
CHANNEL: stable
|
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
|
||||||
CHANNEL: stable
|
|
||||||
# Nightly channel
|
|
||||||
- TARGET: i686-pc-windows-msvc
|
|
||||||
CHANNEL: nightly
|
|
||||||
- TARGET: x86_64-pc-windows-gnu
|
|
||||||
CHANNEL: nightly
|
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
|
||||||
CHANNEL: nightly
|
|
||||||
|
|
||||||
# Install Rust and Cargo
|
|
||||||
# (Based on from https://github.com/rust-lang/libc/blob/master/appveyor.yml)
|
|
||||||
install:
|
|
||||||
- ps: >-
|
|
||||||
If ($Env:TARGET -eq 'x86_64-pc-windows-gnu') {
|
|
||||||
$Env:PATH += ';C:\msys64\mingw64\bin'
|
|
||||||
} ElseIf ($Env:TARGET -eq 'i686-pc-windows-gnu') {
|
|
||||||
$Env:PATH += ';C:\MinGW\bin'
|
|
||||||
}
|
|
||||||
- curl -sSf -o rustup-init.exe https://win.rustup.rs
|
|
||||||
- rustup-init.exe --default-host %TARGET% --default-toolchain %CHANNEL% -y
|
|
||||||
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
|
|
||||||
- rustc -Vv
|
|
||||||
- cargo -V
|
|
||||||
|
|
||||||
# 'cargo test' takes care of building for us, so disable Appveyor's build stage.
|
|
||||||
build: false
|
|
||||||
|
|
||||||
# Equivalent to Travis' `script` phase
|
|
||||||
test_script:
|
|
||||||
- cargo clean
|
|
||||||
- cargo test
|
|
@ -1,5 +1,32 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## Unreleased - 2020-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 2.0.0-beta.2 - 2020-07-21
|
||||||
|
### Fixed
|
||||||
|
* Potential UB in h1 decoder using uninitialized memory. [#1614]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* Fix illegal chunked encoding. [#1615]
|
||||||
|
|
||||||
|
[#1614]: https://github.com/actix/actix-web/pull/1614
|
||||||
|
[#1615]: https://github.com/actix/actix-web/pull/1615
|
||||||
|
|
||||||
|
|
||||||
|
## [2.0.0-beta.1] - 2020-07-11
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Migrate cookie handling to `cookie` crate. [#1558]
|
||||||
|
* Update `sha-1` to 0.9. [#1586]
|
||||||
|
* Fix leak in client pool. [#1580]
|
||||||
|
* MSRV is now 1.41.1.
|
||||||
|
|
||||||
|
[#1558]: https://github.com/actix/actix-web/pull/1558
|
||||||
|
[#1586]: https://github.com/actix/actix-web/pull/1586
|
||||||
|
[#1580]: https://github.com/actix/actix-web/pull/1580
|
||||||
|
|
||||||
## [2.0.0-alpha.4] - 2020-05-21
|
## [2.0.0-alpha.4] - 2020-05-21
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-http"
|
name = "actix-http"
|
||||||
version = "2.0.0-alpha.4"
|
version = "2.0.0-beta.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix http primitives"
|
description = "Actix HTTP primitives"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["actix", "http", "framework", "async", "futures"]
|
keywords = ["actix", "http", "framework", "async", "futures"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
@ -11,7 +11,7 @@ documentation = "https://docs.rs/actix-http/"
|
|||||||
categories = ["network-programming", "asynchronous",
|
categories = ["network-programming", "asynchronous",
|
||||||
"web-programming::http-server",
|
"web-programming::http-server",
|
||||||
"web-programming::websocket"]
|
"web-programming::websocket"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
@ -34,7 +34,7 @@ rustls = ["actix-tls/rustls", "actix-connect/rustls"]
|
|||||||
compress = ["flate2", "brotli2"]
|
compress = ["flate2", "brotli2"]
|
||||||
|
|
||||||
# support for secure cookies
|
# support for secure cookies
|
||||||
secure-cookies = ["ring"]
|
secure-cookies = ["cookie/secure"]
|
||||||
|
|
||||||
# support for actix Actor messages
|
# support for actix Actor messages
|
||||||
actors = ["actix"]
|
actors = ["actix"]
|
||||||
@ -52,6 +52,7 @@ actix = { version = "0.10.0-alpha.1", optional = true }
|
|||||||
base64 = "0.12"
|
base64 = "0.12"
|
||||||
bitflags = "1.2"
|
bitflags = "1.2"
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
|
cookie = { version = "0.14.1", features = ["percent-encode"] }
|
||||||
copyless = "0.1.4"
|
copyless = "0.1.4"
|
||||||
derive_more = "0.99.2"
|
derive_more = "0.99.2"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
@ -70,19 +71,16 @@ language-tags = "0.2"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.17"
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
regex = "1.3"
|
regex = "1.3"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
sha-1 = "0.8"
|
sha-1 = "0.9"
|
||||||
slab = "0.4"
|
slab = "0.4"
|
||||||
serde_urlencoded = "0.6.1"
|
serde_urlencoded = "0.6.1"
|
||||||
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
||||||
|
|
||||||
# for secure cookie
|
|
||||||
ring = { version = "0.16.9", optional = true }
|
|
||||||
|
|
||||||
# compression
|
# compression
|
||||||
brotli2 = { version="0.3.2", optional = true }
|
brotli2 = { version="0.3.2", optional = true }
|
||||||
flate2 = { version = "1.0.13", optional = true }
|
flate2 = { version = "1.0.13", optional = true }
|
||||||
@ -105,3 +103,7 @@ harness = false
|
|||||||
[[bench]]
|
[[bench]]
|
||||||
name = "status-line"
|
name = "status-line"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "uninit-headers"
|
||||||
|
harness = false
|
||||||
|
137
actix-http/benches/uninit-headers.rs
Normal file
137
actix-http/benches/uninit-headers.rs
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
|
||||||
|
use bytes::BytesMut;
|
||||||
|
|
||||||
|
// A Miri run detects UB, seen on this playground:
|
||||||
|
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f5d9aa166aa48df8dca05fce2b6c3915
|
||||||
|
|
||||||
|
fn bench_header_parsing(c: &mut Criterion) {
|
||||||
|
c.bench_function("Original (Unsound) [short]", |b| {
|
||||||
|
b.iter(|| {
|
||||||
|
let mut buf = BytesMut::from(REQ_SHORT);
|
||||||
|
_original::parse_headers(&mut buf);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
c.bench_function("New (safe) [short]", |b| {
|
||||||
|
b.iter(|| {
|
||||||
|
let mut buf = BytesMut::from(REQ_SHORT);
|
||||||
|
_new::parse_headers(&mut buf);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
c.bench_function("Original (Unsound) [realistic]", |b| {
|
||||||
|
b.iter(|| {
|
||||||
|
let mut buf = BytesMut::from(REQ);
|
||||||
|
_original::parse_headers(&mut buf);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
c.bench_function("New (safe) [realistic]", |b| {
|
||||||
|
b.iter(|| {
|
||||||
|
let mut buf = BytesMut::from(REQ);
|
||||||
|
_new::parse_headers(&mut buf);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(benches, bench_header_parsing);
|
||||||
|
criterion_main!(benches);
|
||||||
|
|
||||||
|
const MAX_HEADERS: usize = 96;
|
||||||
|
|
||||||
|
const EMPTY_HEADER_ARRAY: [httparse::Header<'static>; MAX_HEADERS] =
|
||||||
|
[httparse::EMPTY_HEADER; MAX_HEADERS];
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct HeaderIndex {
|
||||||
|
name: (usize, usize),
|
||||||
|
value: (usize, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
const EMPTY_HEADER_INDEX: HeaderIndex = HeaderIndex {
|
||||||
|
name: (0, 0),
|
||||||
|
value: (0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
const EMPTY_HEADER_INDEX_ARRAY: [HeaderIndex; MAX_HEADERS] =
|
||||||
|
[EMPTY_HEADER_INDEX; MAX_HEADERS];
|
||||||
|
|
||||||
|
impl HeaderIndex {
|
||||||
|
fn record(
|
||||||
|
bytes: &[u8],
|
||||||
|
headers: &[httparse::Header<'_>],
|
||||||
|
indices: &mut [HeaderIndex],
|
||||||
|
) {
|
||||||
|
let bytes_ptr = bytes.as_ptr() as usize;
|
||||||
|
for (header, indices) in headers.iter().zip(indices.iter_mut()) {
|
||||||
|
let name_start = header.name.as_ptr() as usize - bytes_ptr;
|
||||||
|
let name_end = name_start + header.name.len();
|
||||||
|
indices.name = (name_start, name_end);
|
||||||
|
let value_start = header.value.as_ptr() as usize - bytes_ptr;
|
||||||
|
let value_end = value_start + header.value.len();
|
||||||
|
indices.value = (value_start, value_end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test cases taken from:
|
||||||
|
// https://github.com/seanmonstar/httparse/blob/master/benches/parse.rs
|
||||||
|
|
||||||
|
const REQ_SHORT: &'static [u8] = b"\
|
||||||
|
GET / HTTP/1.0\r\n\
|
||||||
|
Host: example.com\r\n\
|
||||||
|
Cookie: session=60; user_id=1\r\n\r\n";
|
||||||
|
|
||||||
|
const REQ: &'static [u8] = b"\
|
||||||
|
GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n\
|
||||||
|
Host: www.kittyhell.com\r\n\
|
||||||
|
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\n\
|
||||||
|
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\
|
||||||
|
Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n\
|
||||||
|
Accept-Encoding: gzip,deflate\r\n\
|
||||||
|
Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n\
|
||||||
|
Keep-Alive: 115\r\n\
|
||||||
|
Connection: keep-alive\r\n\
|
||||||
|
Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; __utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral|padding=under256\r\n\r\n";
|
||||||
|
|
||||||
|
mod _new {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn parse_headers(src: &mut BytesMut) -> usize {
|
||||||
|
let mut headers: [HeaderIndex; MAX_HEADERS] = EMPTY_HEADER_INDEX_ARRAY;
|
||||||
|
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] = EMPTY_HEADER_ARRAY;
|
||||||
|
|
||||||
|
let mut req = httparse::Request::new(&mut parsed);
|
||||||
|
match req.parse(src).unwrap() {
|
||||||
|
httparse::Status::Complete(_len) => {
|
||||||
|
HeaderIndex::record(src, req.headers, &mut headers);
|
||||||
|
req.headers.len()
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod _original {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::mem::MaybeUninit;
|
||||||
|
|
||||||
|
pub fn parse_headers(src: &mut BytesMut) -> usize {
|
||||||
|
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
||||||
|
unsafe { MaybeUninit::uninit().assume_init() };
|
||||||
|
|
||||||
|
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] =
|
||||||
|
unsafe { MaybeUninit::uninit().assume_init() };
|
||||||
|
|
||||||
|
let mut req = httparse::Request::new(&mut parsed);
|
||||||
|
match req.parse(src).unwrap() {
|
||||||
|
httparse::Status::Complete(_len) => {
|
||||||
|
HeaderIndex::record(src, req.headers, &mut headers);
|
||||||
|
req.headers.len()
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,7 @@ use std::{fmt, mem};
|
|||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use futures_util::ready;
|
use futures_util::ready;
|
||||||
use pin_project::{pin_project, project};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
@ -21,12 +21,7 @@ pub enum BodySize {
|
|||||||
|
|
||||||
impl BodySize {
|
impl BodySize {
|
||||||
pub fn is_eof(&self) -> bool {
|
pub fn is_eof(&self) -> bool {
|
||||||
match self {
|
matches!(self, BodySize::None | BodySize::Empty | BodySize::Sized(0))
|
||||||
BodySize::None
|
|
||||||
| BodySize::Empty
|
|
||||||
| BodySize::Sized(0) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +65,7 @@ impl<T: MessageBody + Unpin> MessageBody for Box<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = ResponseBodyProj)]
|
||||||
pub enum ResponseBody<B> {
|
pub enum ResponseBody<B> {
|
||||||
Body(#[pin] B),
|
Body(#[pin] B),
|
||||||
Other(#[pin] Body),
|
Other(#[pin] Body),
|
||||||
@ -109,15 +104,13 @@ impl<B: MessageBody> MessageBody for ResponseBody<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
ResponseBody::Body(body) => body.poll_next(cx),
|
ResponseBodyProj::Body(body) => body.poll_next(cx),
|
||||||
ResponseBody::Other(body) => body.poll_next(cx),
|
ResponseBodyProj::Other(body) => body.poll_next(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,20 +118,18 @@ impl<B: MessageBody> MessageBody for ResponseBody<B> {
|
|||||||
impl<B: MessageBody> Stream for ResponseBody<B> {
|
impl<B: MessageBody> Stream for ResponseBody<B> {
|
||||||
type Item = Result<Bytes, Error>;
|
type Item = Result<Bytes, Error>;
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Self::Item>> {
|
) -> Poll<Option<Self::Item>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
ResponseBody::Body(body) => body.poll_next(cx),
|
ResponseBodyProj::Body(body) => body.poll_next(cx),
|
||||||
ResponseBody::Other(body) => body.poll_next(cx),
|
ResponseBodyProj::Other(body) => body.poll_next(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = BodyProj)]
|
||||||
/// Represents various types of http message body.
|
/// Represents various types of http message body.
|
||||||
pub enum Body {
|
pub enum Body {
|
||||||
/// Empty response. `Content-Length` header is not set.
|
/// Empty response. `Content-Length` header is not set.
|
||||||
@ -173,16 +164,14 @@ impl MessageBody for Body {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
Body::None => Poll::Ready(None),
|
BodyProj::None => Poll::Ready(None),
|
||||||
Body::Empty => Poll::Ready(None),
|
BodyProj::Empty => Poll::Ready(None),
|
||||||
Body::Bytes(ref mut bin) => {
|
BodyProj::Bytes(ref mut bin) => {
|
||||||
let len = bin.len();
|
let len = bin.len();
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
Poll::Ready(None)
|
Poll::Ready(None)
|
||||||
@ -190,7 +179,7 @@ impl MessageBody for Body {
|
|||||||
Poll::Ready(Some(Ok(mem::take(bin))))
|
Poll::Ready(Some(Ok(mem::take(bin))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Body::Message(ref mut body) => Pin::new(body.as_mut()).poll_next(cx),
|
BodyProj::Message(ref mut body) => Pin::new(body.as_mut()).poll_next(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -198,14 +187,8 @@ impl MessageBody for Body {
|
|||||||
impl PartialEq for Body {
|
impl PartialEq for Body {
|
||||||
fn eq(&self, other: &Body) -> bool {
|
fn eq(&self, other: &Body) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Body::None => match *other {
|
Body::None => matches!(*other, Body::None),
|
||||||
Body::None => true,
|
Body::Empty => matches!(*other, Body::Empty),
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
Body::Empty => match *other {
|
|
||||||
Body::Empty => true,
|
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
Body::Bytes(ref b) => match *other {
|
Body::Bytes(ref b) => match *other {
|
||||||
Body::Bytes(ref b2) => b == b2,
|
Body::Bytes(ref b2) => b == b2,
|
||||||
_ => false,
|
_ => false,
|
||||||
@ -482,9 +465,9 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use futures_util::stream;
|
|
||||||
use futures_util::future::poll_fn;
|
use futures_util::future::poll_fn;
|
||||||
use futures_util::pin_mut;
|
use futures_util::pin_mut;
|
||||||
|
use futures_util::stream;
|
||||||
|
|
||||||
impl Body {
|
impl Body {
|
||||||
pub(crate) fn get_ref(&self) -> &[u8] {
|
pub(crate) fn get_ref(&self) -> &[u8] {
|
||||||
@ -618,10 +601,6 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_eq() {
|
async fn test_body_eq() {
|
||||||
assert!(Body::None == Body::None);
|
|
||||||
assert!(Body::None != Body::Empty);
|
|
||||||
assert!(Body::Empty == Body::Empty);
|
|
||||||
assert!(Body::Empty != Body::None);
|
|
||||||
assert!(
|
assert!(
|
||||||
Body::Bytes(Bytes::from_static(b"1"))
|
Body::Bytes(Bytes::from_static(b"1"))
|
||||||
== Body::Bytes(Bytes::from_static(b"1"))
|
== Body::Bytes(Bytes::from_static(b"1"))
|
||||||
@ -633,7 +612,7 @@ mod tests {
|
|||||||
async fn test_body_debug() {
|
async fn test_body_debug() {
|
||||||
assert!(format!("{:?}", Body::None).contains("Body::None"));
|
assert!(format!("{:?}", Body::None).contains("Body::None"));
|
||||||
assert!(format!("{:?}", Body::Empty).contains("Body::Empty"));
|
assert!(format!("{:?}", Body::Empty).contains("Body::Empty"));
|
||||||
assert!(format!("{:?}", Body::Bytes(Bytes::from_static(b"1"))).contains("1"));
|
assert!(format!("{:?}", Body::Bytes(Bytes::from_static(b"1"))).contains('1'));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
@ -7,7 +7,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|||||||
use bytes::{Buf, Bytes};
|
use bytes::{Buf, Bytes};
|
||||||
use futures_util::future::{err, Either, FutureExt, LocalBoxFuture, Ready};
|
use futures_util::future::{err, Either, FutureExt, LocalBoxFuture, Ready};
|
||||||
use h2::client::SendRequest;
|
use h2::client::SendRequest;
|
||||||
use pin_project::{pin_project, project};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::body::MessageBody;
|
use crate::body::MessageBody;
|
||||||
use crate::h1::ClientCodec;
|
use crate::h1::ClientCodec;
|
||||||
@ -205,7 +205,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = EitherIoProj)]
|
||||||
pub enum EitherIo<A, B> {
|
pub enum EitherIo<A, B> {
|
||||||
A(#[pin] A),
|
A(#[pin] A),
|
||||||
B(#[pin] B),
|
B(#[pin] B),
|
||||||
@ -216,16 +216,14 @@ where
|
|||||||
A: AsyncRead,
|
A: AsyncRead,
|
||||||
B: AsyncRead,
|
B: AsyncRead,
|
||||||
{
|
{
|
||||||
#[project]
|
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
EitherIo::A(val) => val.poll_read(cx, buf),
|
EitherIoProj::A(val) => val.poll_read(cx, buf),
|
||||||
EitherIo::B(val) => val.poll_read(cx, buf),
|
EitherIoProj::B(val) => val.poll_read(cx, buf),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,41 +243,34 @@ where
|
|||||||
A: AsyncWrite,
|
A: AsyncWrite,
|
||||||
B: AsyncWrite,
|
B: AsyncWrite,
|
||||||
{
|
{
|
||||||
#[project]
|
|
||||||
fn poll_write(
|
fn poll_write(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
buf: &[u8],
|
buf: &[u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
EitherIo::A(val) => val.poll_write(cx, buf),
|
EitherIoProj::A(val) => val.poll_write(cx, buf),
|
||||||
EitherIo::B(val) => val.poll_write(cx, buf),
|
EitherIoProj::B(val) => val.poll_write(cx, buf),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
EitherIo::A(val) => val.poll_flush(cx),
|
EitherIoProj::A(val) => val.poll_flush(cx),
|
||||||
EitherIo::B(val) => val.poll_flush(cx),
|
EitherIoProj::B(val) => val.poll_flush(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_shutdown(
|
fn poll_shutdown(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<io::Result<()>> {
|
) -> Poll<io::Result<()>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
EitherIo::A(val) => val.poll_shutdown(cx),
|
EitherIoProj::A(val) => val.poll_shutdown(cx),
|
||||||
EitherIo::B(val) => val.poll_shutdown(cx),
|
EitherIoProj::B(val) => val.poll_shutdown(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_write_buf<U: Buf>(
|
fn poll_write_buf<U: Buf>(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
@ -288,10 +279,9 @@ where
|
|||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
EitherIo::A(val) => val.poll_write_buf(cx, buf),
|
EitherIoProj::A(val) => val.poll_write_buf(cx, buf),
|
||||||
EitherIo::B(val) => val.poll_write_buf(cx, buf),
|
EitherIoProj::B(val) => val.poll_write_buf(cx, buf),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ pub enum ConnectError {
|
|||||||
H2(h2::Error),
|
H2(h2::Error),
|
||||||
|
|
||||||
/// Connecting took too long
|
/// Connecting took too long
|
||||||
#[display(fmt = "Timeout out while establishing connection")]
|
#[display(fmt = "Timeout while establishing connection")]
|
||||||
Timeout,
|
Timeout,
|
||||||
|
|
||||||
/// Connector has been disconnected
|
/// Connector has been disconnected
|
||||||
@ -110,7 +110,7 @@ pub enum SendRequestError {
|
|||||||
#[display(fmt = "{}", _0)]
|
#[display(fmt = "{}", _0)]
|
||||||
H2(h2::Error),
|
H2(h2::Error),
|
||||||
/// Response took too long
|
/// Response took too long
|
||||||
#[display(fmt = "Timeout out while waiting for response")]
|
#[display(fmt = "Timeout while waiting for response")]
|
||||||
Timeout,
|
Timeout,
|
||||||
/// Tunnels are not supported for http2 connection
|
/// Tunnels are not supported for http2 connection
|
||||||
#[display(fmt = "Tunnels are not supported for http2 connection")]
|
#[display(fmt = "Tunnels are not supported for http2 connection")]
|
||||||
|
@ -37,10 +37,10 @@ where
|
|||||||
trace!("Sending client request: {:?} {:?}", head, body.size());
|
trace!("Sending client request: {:?} {:?}", head, body.size());
|
||||||
let head_req = head.as_ref().method == Method::HEAD;
|
let head_req = head.as_ref().method == Method::HEAD;
|
||||||
let length = body.size();
|
let length = body.size();
|
||||||
let eof = match length {
|
let eof = matches!(
|
||||||
BodySize::None | BodySize::Empty | BodySize::Sized(0) => true,
|
length,
|
||||||
_ => false,
|
BodySize::None | BodySize::Empty | BodySize::Sized(0)
|
||||||
};
|
);
|
||||||
|
|
||||||
let mut req = Request::new(());
|
let mut req = Request::new(());
|
||||||
*req.uri_mut() = head.as_ref().uri.clone();
|
*req.uri_mut() = head.as_ref().uri.clone();
|
||||||
|
@ -2,7 +2,7 @@ use std::cell::RefCell;
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::{Rc, Weak};
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
@ -53,17 +53,23 @@ where
|
|||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(connector: T, config: ConnectorConfig) -> Self {
|
pub(crate) fn new(connector: T, config: ConnectorConfig) -> Self {
|
||||||
ConnectionPool(
|
let connector_rc = Rc::new(RefCell::new(connector));
|
||||||
Rc::new(RefCell::new(connector)),
|
let inner_rc = Rc::new(RefCell::new(Inner {
|
||||||
Rc::new(RefCell::new(Inner {
|
config,
|
||||||
config,
|
acquired: 0,
|
||||||
acquired: 0,
|
waiters: Slab::new(),
|
||||||
waiters: Slab::new(),
|
waiters_queue: IndexSet::new(),
|
||||||
waiters_queue: IndexSet::new(),
|
available: FxHashMap::default(),
|
||||||
available: FxHashMap::default(),
|
waker: LocalWaker::new(),
|
||||||
waker: LocalWaker::new(),
|
}));
|
||||||
})),
|
|
||||||
)
|
// start support future
|
||||||
|
actix_rt::spawn(ConnectorPoolSupport {
|
||||||
|
connector: connector_rc.clone(),
|
||||||
|
inner: Rc::downgrade(&inner_rc),
|
||||||
|
});
|
||||||
|
|
||||||
|
ConnectionPool(connector_rc, inner_rc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,12 +98,6 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect) -> Self::Future {
|
fn call(&mut self, req: Connect) -> Self::Future {
|
||||||
// start support future
|
|
||||||
actix_rt::spawn(ConnectorPoolSupport {
|
|
||||||
connector: self.0.clone(),
|
|
||||||
inner: self.1.clone(),
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut connector = self.0.clone();
|
let mut connector = self.0.clone();
|
||||||
let inner = self.1.clone();
|
let inner = self.1.clone();
|
||||||
|
|
||||||
@ -421,7 +421,7 @@ where
|
|||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
connector: T,
|
connector: T,
|
||||||
inner: Rc<RefCell<Inner<Io>>>,
|
inner: Weak<RefCell<Inner<Io>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Io> Future for ConnectorPoolSupport<T, Io>
|
impl<T, Io> Future for ConnectorPoolSupport<T, Io>
|
||||||
@ -435,51 +435,55 @@ where
|
|||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.project();
|
let this = self.project();
|
||||||
|
|
||||||
let mut inner = this.inner.as_ref().borrow_mut();
|
if let Some(this_inner) = this.inner.upgrade() {
|
||||||
inner.waker.register(cx.waker());
|
let mut inner = this_inner.as_ref().borrow_mut();
|
||||||
|
inner.waker.register(cx.waker());
|
||||||
|
|
||||||
// check waiters
|
// check waiters
|
||||||
loop {
|
loop {
|
||||||
let (key, token) = {
|
let (key, token) = {
|
||||||
if let Some((key, token)) = inner.waiters_queue.get_index(0) {
|
if let Some((key, token)) = inner.waiters_queue.get_index(0) {
|
||||||
(key.clone(), *token)
|
(key.clone(), *token)
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if inner.waiters.get(token).unwrap().is_none() {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
if inner.waiters.get(token).unwrap().is_none() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
match inner.acquire(&key, cx) {
|
match inner.acquire(&key, cx) {
|
||||||
Acquire::NotAvailable => break,
|
Acquire::NotAvailable => break,
|
||||||
Acquire::Acquired(io, created) => {
|
Acquire::Acquired(io, created) => {
|
||||||
let tx = inner.waiters.get_mut(token).unwrap().take().unwrap().1;
|
let tx = inner.waiters.get_mut(token).unwrap().take().unwrap().1;
|
||||||
if let Err(conn) = tx.send(Ok(IoConnection::new(
|
if let Err(conn) = tx.send(Ok(IoConnection::new(
|
||||||
io,
|
io,
|
||||||
created,
|
created,
|
||||||
Some(Acquired(key.clone(), Some(this.inner.clone()))),
|
Some(Acquired(key.clone(), Some(this_inner.clone()))),
|
||||||
))) {
|
))) {
|
||||||
let (io, created) = conn.unwrap().into_inner();
|
let (io, created) = conn.unwrap().into_inner();
|
||||||
inner.release_conn(&key, io, created);
|
inner.release_conn(&key, io, created);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Acquire::Available => {
|
||||||
|
let (connect, tx) =
|
||||||
|
inner.waiters.get_mut(token).unwrap().take().unwrap();
|
||||||
|
OpenWaitingConnection::spawn(
|
||||||
|
key.clone(),
|
||||||
|
tx,
|
||||||
|
this_inner.clone(),
|
||||||
|
this.connector.call(connect),
|
||||||
|
inner.config.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Acquire::Available => {
|
let _ = inner.waiters_queue.swap_remove_index(0);
|
||||||
let (connect, tx) =
|
|
||||||
inner.waiters.get_mut(token).unwrap().take().unwrap();
|
|
||||||
OpenWaitingConnection::spawn(
|
|
||||||
key.clone(),
|
|
||||||
tx,
|
|
||||||
this.inner.clone(),
|
|
||||||
this.connector.call(connect),
|
|
||||||
inner.config.clone(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let _ = inner.waiters_queue.swap_remove_index(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
|
} else {
|
||||||
|
Poll::Ready(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,252 +0,0 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use time::{Duration, OffsetDateTime};
|
|
||||||
|
|
||||||
use super::{Cookie, SameSite};
|
|
||||||
|
|
||||||
/// Structure that follows the builder pattern for building `Cookie` structs.
|
|
||||||
///
|
|
||||||
/// To construct a cookie:
|
|
||||||
///
|
|
||||||
/// 1. Call [`Cookie::build`](struct.Cookie.html#method.build) to start building.
|
|
||||||
/// 2. Use any of the builder methods to set fields in the cookie.
|
|
||||||
/// 3. Call [finish](#method.finish) to retrieve the built cookie.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let cookie: Cookie = Cookie::build("name", "value")
|
|
||||||
/// .domain("www.rust-lang.org")
|
|
||||||
/// .path("/")
|
|
||||||
/// .secure(true)
|
|
||||||
/// .http_only(true)
|
|
||||||
/// .max_age(84600)
|
|
||||||
/// .finish();
|
|
||||||
/// ```
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct CookieBuilder {
|
|
||||||
/// The cookie being built.
|
|
||||||
cookie: Cookie<'static>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CookieBuilder {
|
|
||||||
/// Creates a new `CookieBuilder` instance from the given name and value.
|
|
||||||
///
|
|
||||||
/// This method is typically called indirectly via
|
|
||||||
/// [Cookie::build](struct.Cookie.html#method.build).
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar").finish();
|
|
||||||
/// assert_eq!(c.name_value(), ("foo", "bar"));
|
|
||||||
/// ```
|
|
||||||
pub fn new<N, V>(name: N, value: V) -> CookieBuilder
|
|
||||||
where
|
|
||||||
N: Into<Cow<'static, str>>,
|
|
||||||
V: Into<Cow<'static, str>>,
|
|
||||||
{
|
|
||||||
CookieBuilder {
|
|
||||||
cookie: Cookie::new(name, value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `expires` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .expires(time::OffsetDateTime::now_utc())
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert!(c.expires().is_some());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn expires(mut self, when: OffsetDateTime) -> CookieBuilder {
|
|
||||||
self.cookie.set_expires(when);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `max_age` field in seconds in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .max_age(1800)
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60)));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn max_age(self, seconds: i64) -> CookieBuilder {
|
|
||||||
self.max_age_time(Duration::seconds(seconds))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `max_age` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .max_age_time(time::Duration::minutes(30))
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60)));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn max_age_time(mut self, value: Duration) -> CookieBuilder {
|
|
||||||
// Truncate any nanoseconds from the Duration, as they aren't represented within `Max-Age`
|
|
||||||
// and would cause two otherwise identical `Cookie` instances to not be equivalent to one another.
|
|
||||||
self.cookie
|
|
||||||
.set_max_age(Duration::seconds(value.whole_seconds()));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `domain` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .domain("www.rust-lang.org")
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.domain(), Some("www.rust-lang.org"));
|
|
||||||
/// ```
|
|
||||||
pub fn domain<D: Into<Cow<'static, str>>>(mut self, value: D) -> CookieBuilder {
|
|
||||||
self.cookie.set_domain(value);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `path` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .path("/")
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.path(), Some("/"));
|
|
||||||
/// ```
|
|
||||||
pub fn path<P: Into<Cow<'static, str>>>(mut self, path: P) -> CookieBuilder {
|
|
||||||
self.cookie.set_path(path);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `secure` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .secure(true)
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.secure(), Some(true));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn secure(mut self, value: bool) -> CookieBuilder {
|
|
||||||
self.cookie.set_secure(value);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `http_only` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .http_only(true)
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.http_only(), Some(true));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn http_only(mut self, value: bool) -> CookieBuilder {
|
|
||||||
self.cookie.set_http_only(value);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the `same_site` field in the cookie being built.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{Cookie, SameSite};
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .same_site(SameSite::Strict)
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.same_site(), Some(SameSite::Strict));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn same_site(mut self, value: SameSite) -> CookieBuilder {
|
|
||||||
self.cookie.set_same_site(value);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Makes the cookie being built 'permanent' by extending its expiration and
|
|
||||||
/// max age 20 years into the future.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
/// use time::Duration;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .permanent()
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
|
|
||||||
/// # assert!(c.expires().is_some());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn permanent(mut self) -> CookieBuilder {
|
|
||||||
self.cookie.make_permanent();
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finishes building and returns the built `Cookie`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Cookie;
|
|
||||||
///
|
|
||||||
/// let c = Cookie::build("foo", "bar")
|
|
||||||
/// .domain("crates.io")
|
|
||||||
/// .path("/")
|
|
||||||
/// .finish();
|
|
||||||
///
|
|
||||||
/// assert_eq!(c.name_value(), ("foo", "bar"));
|
|
||||||
/// assert_eq!(c.domain(), Some("crates.io"));
|
|
||||||
/// assert_eq!(c.path(), Some("/"));
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn finish(self) -> Cookie<'static> {
|
|
||||||
self.cookie
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
use std::borrow::Borrow;
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
|
|
||||||
use super::Cookie;
|
|
||||||
|
|
||||||
/// A `DeltaCookie` is a helper structure used in a cookie jar. It wraps a
|
|
||||||
/// `Cookie` so that it can be hashed and compared purely by name. It further
|
|
||||||
/// records whether the wrapped cookie is a "removal" cookie, that is, a cookie
|
|
||||||
/// that when sent to the client removes the named cookie on the client's
|
|
||||||
/// machine.
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct DeltaCookie {
|
|
||||||
pub cookie: Cookie<'static>,
|
|
||||||
pub removed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DeltaCookie {
|
|
||||||
/// Create a new `DeltaCookie` that is being added to a jar.
|
|
||||||
#[inline]
|
|
||||||
pub fn added(cookie: Cookie<'static>) -> DeltaCookie {
|
|
||||||
DeltaCookie {
|
|
||||||
cookie,
|
|
||||||
removed: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new `DeltaCookie` that is being removed from a jar. The
|
|
||||||
/// `cookie` should be a "removal" cookie.
|
|
||||||
#[inline]
|
|
||||||
pub fn removed(cookie: Cookie<'static>) -> DeltaCookie {
|
|
||||||
DeltaCookie {
|
|
||||||
cookie,
|
|
||||||
removed: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for DeltaCookie {
|
|
||||||
type Target = Cookie<'static>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Cookie<'static> {
|
|
||||||
&self.cookie
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for DeltaCookie {
|
|
||||||
fn deref_mut(&mut self) -> &mut Cookie<'static> {
|
|
||||||
&mut self.cookie
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for DeltaCookie {
|
|
||||||
fn eq(&self, other: &DeltaCookie) -> bool {
|
|
||||||
self.name() == other.name()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for DeltaCookie {}
|
|
||||||
|
|
||||||
impl Hash for DeltaCookie {
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
||||||
self.name().hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Borrow<str> for DeltaCookie {
|
|
||||||
fn borrow(&self) -> &str {
|
|
||||||
self.name()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,106 +0,0 @@
|
|||||||
//! This module contains types that represent cookie properties that are not yet
|
|
||||||
//! standardized. That is, _draft_ features.
|
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
/// The `SameSite` cookie attribute.
|
|
||||||
///
|
|
||||||
/// A cookie with a `SameSite` attribute is imposed restrictions on when it is
|
|
||||||
/// sent to the origin server in a cross-site request. If the `SameSite`
|
|
||||||
/// attribute is "Strict", then the cookie is never sent in cross-site requests.
|
|
||||||
/// If the `SameSite` attribute is "Lax", the cookie is only sent in cross-site
|
|
||||||
/// requests with "safe" HTTP methods, i.e, `GET`, `HEAD`, `OPTIONS`, `TRACE`.
|
|
||||||
/// If the `SameSite` attribute is not present then the cookie will be sent as
|
|
||||||
/// normal. In some browsers, this will implicitly handle the cookie as if "Lax"
|
|
||||||
/// and in others, "None". It's best to explicitly set the `SameSite` attribute
|
|
||||||
/// to avoid inconsistent behavior.
|
|
||||||
///
|
|
||||||
/// **Note:** Depending on browser, the `Secure` attribute may be required for
|
|
||||||
/// `SameSite` "None" cookies to be accepted.
|
|
||||||
///
|
|
||||||
/// **Note:** This cookie attribute is an HTTP draft! Its meaning and definition
|
|
||||||
/// are subject to change.
|
|
||||||
///
|
|
||||||
/// More info about these draft changes can be found in the draft spec:
|
|
||||||
/// - https://tools.ietf.org/html/draft-west-cookie-incrementalism-00
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub enum SameSite {
|
|
||||||
/// The "Strict" `SameSite` attribute.
|
|
||||||
Strict,
|
|
||||||
/// The "Lax" `SameSite` attribute.
|
|
||||||
Lax,
|
|
||||||
/// The "None" `SameSite` attribute.
|
|
||||||
None,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SameSite {
|
|
||||||
/// Returns `true` if `self` is `SameSite::Strict` and `false` otherwise.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::SameSite;
|
|
||||||
///
|
|
||||||
/// let strict = SameSite::Strict;
|
|
||||||
/// assert!(strict.is_strict());
|
|
||||||
/// assert!(!strict.is_lax());
|
|
||||||
/// assert!(!strict.is_none());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn is_strict(self) -> bool {
|
|
||||||
match self {
|
|
||||||
SameSite::Strict => true,
|
|
||||||
SameSite::Lax | SameSite::None => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if `self` is `SameSite::Lax` and `false` otherwise.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::SameSite;
|
|
||||||
///
|
|
||||||
/// let lax = SameSite::Lax;
|
|
||||||
/// assert!(lax.is_lax());
|
|
||||||
/// assert!(!lax.is_strict());
|
|
||||||
/// assert!(!lax.is_none());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn is_lax(self) -> bool {
|
|
||||||
match self {
|
|
||||||
SameSite::Lax => true,
|
|
||||||
SameSite::Strict | SameSite::None => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if `self` is `SameSite::None` and `false` otherwise.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::SameSite;
|
|
||||||
///
|
|
||||||
/// let none = SameSite::None;
|
|
||||||
/// assert!(none.is_none());
|
|
||||||
/// assert!(!none.is_lax());
|
|
||||||
/// assert!(!none.is_strict());
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub fn is_none(self) -> bool {
|
|
||||||
match self {
|
|
||||||
SameSite::None => true,
|
|
||||||
SameSite::Lax | SameSite::Strict => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for SameSite {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match *self {
|
|
||||||
SameSite::Strict => write!(f, "Strict"),
|
|
||||||
SameSite::Lax => write!(f, "Lax"),
|
|
||||||
SameSite::None => write!(f, "None"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,651 +0,0 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
use std::mem;
|
|
||||||
|
|
||||||
use time::{Duration, OffsetDateTime};
|
|
||||||
|
|
||||||
use super::delta::DeltaCookie;
|
|
||||||
use super::Cookie;
|
|
||||||
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
use super::secure::{Key, PrivateJar, SignedJar};
|
|
||||||
|
|
||||||
/// A collection of cookies that tracks its modifications.
|
|
||||||
///
|
|
||||||
/// 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 iterator over the changes.
|
|
||||||
///
|
|
||||||
/// # Usage
|
|
||||||
///
|
|
||||||
/// A jar's life begins via [new](#method.new) and calls to
|
|
||||||
/// [`add_original`](#method.add_original):
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{Cookie, CookieJar};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.add_original(Cookie::new("name", "value"));
|
|
||||||
/// jar.add_original(Cookie::new("second", "another"));
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// Cookies can be added via [add](#method.add) and removed via
|
|
||||||
/// [remove](#method.remove). Finally, cookies can be looked up via
|
|
||||||
/// [get](#method.get):
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// # use actix_http::cookie::{Cookie, CookieJar};
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.add(Cookie::new("a", "one"));
|
|
||||||
/// jar.add(Cookie::new("b", "two"));
|
|
||||||
///
|
|
||||||
/// assert_eq!(jar.get("a").map(|c| c.value()), Some("one"));
|
|
||||||
/// assert_eq!(jar.get("b").map(|c| c.value()), Some("two"));
|
|
||||||
///
|
|
||||||
/// jar.remove(Cookie::named("b"));
|
|
||||||
/// assert!(jar.get("b").is_none());
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// # Deltas
|
|
||||||
///
|
|
||||||
/// A jar keeps track of any modifications made to it over time. The
|
|
||||||
/// modifications are recorded as cookies. The modifications can be retrieved
|
|
||||||
/// via [delta](#method.delta). Any new `Cookie` added to a jar via `add`
|
|
||||||
/// results in the same `Cookie` appearing in the `delta`; cookies added via
|
|
||||||
/// `add_original` do not count towards the delta. Any _original_ cookie that is
|
|
||||||
/// removed from a jar results in a "removal" cookie appearing in the delta. A
|
|
||||||
/// "removal" cookie is a cookie that a server sends so that the cookie is
|
|
||||||
/// removed from the client's machine.
|
|
||||||
///
|
|
||||||
/// Deltas are typically used to create `Set-Cookie` headers corresponding to
|
|
||||||
/// the changes made to a cookie jar over a period of time.
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// # use actix_http::cookie::{Cookie, CookieJar};
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
///
|
|
||||||
/// // original cookies don't affect the delta
|
|
||||||
/// jar.add_original(Cookie::new("original", "value"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 0);
|
|
||||||
///
|
|
||||||
/// // new cookies result in an equivalent `Cookie` in the delta
|
|
||||||
/// jar.add(Cookie::new("a", "one"));
|
|
||||||
/// jar.add(Cookie::new("b", "two"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 2);
|
|
||||||
///
|
|
||||||
/// // removing an original cookie adds a "removal" cookie to the delta
|
|
||||||
/// jar.remove(Cookie::named("original"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 3);
|
|
||||||
///
|
|
||||||
/// // removing a new cookie that was added removes that `Cookie` from the delta
|
|
||||||
/// jar.remove(Cookie::named("a"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 2);
|
|
||||||
/// ```
|
|
||||||
#[derive(Default, Debug, Clone)]
|
|
||||||
pub struct CookieJar {
|
|
||||||
original_cookies: HashSet<DeltaCookie>,
|
|
||||||
delta_cookies: HashSet<DeltaCookie>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CookieJar {
|
|
||||||
/// Creates an empty cookie jar.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::CookieJar;
|
|
||||||
///
|
|
||||||
/// let jar = CookieJar::new();
|
|
||||||
/// assert_eq!(jar.iter().count(), 0);
|
|
||||||
/// ```
|
|
||||||
pub fn new() -> CookieJar {
|
|
||||||
CookieJar::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to the `Cookie` inside this jar with the name
|
|
||||||
/// `name`. If no such cookie exists, returns `None`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// assert!(jar.get("name").is_none());
|
|
||||||
///
|
|
||||||
/// jar.add(Cookie::new("name", "value"));
|
|
||||||
/// assert_eq!(jar.get("name").map(|c| c.value()), Some("value"));
|
|
||||||
/// ```
|
|
||||||
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> {
|
|
||||||
self.delta_cookies
|
|
||||||
.get(name)
|
|
||||||
.or_else(|| self.original_cookies.get(name))
|
|
||||||
.and_then(|c| if !c.removed { Some(&c.cookie) } else { None })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds an "original" `cookie` to this jar. If an original cookie with the
|
|
||||||
/// same name already exists, it is replaced with `cookie`. Cookies added
|
|
||||||
/// with `add` take precedence and are not replaced by this method.
|
|
||||||
///
|
|
||||||
/// Adding an original cookie does not affect the [delta](#method.delta)
|
|
||||||
/// computation. This method is intended to be used to seed the cookie jar
|
|
||||||
/// with cookies received from a client's HTTP message.
|
|
||||||
///
|
|
||||||
/// For accurate `delta` computations, this method should not be called
|
|
||||||
/// after calling `remove`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.add_original(Cookie::new("name", "value"));
|
|
||||||
/// jar.add_original(Cookie::new("second", "two"));
|
|
||||||
///
|
|
||||||
/// assert_eq!(jar.get("name").map(|c| c.value()), Some("value"));
|
|
||||||
/// assert_eq!(jar.get("second").map(|c| c.value()), Some("two"));
|
|
||||||
/// assert_eq!(jar.iter().count(), 2);
|
|
||||||
/// assert_eq!(jar.delta().count(), 0);
|
|
||||||
/// ```
|
|
||||||
pub fn add_original(&mut self, cookie: Cookie<'static>) {
|
|
||||||
self.original_cookies.replace(DeltaCookie::added(cookie));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds `cookie` to this jar. If a cookie with the same name already
|
|
||||||
/// exists, it is replaced with `cookie`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.add(Cookie::new("name", "value"));
|
|
||||||
/// jar.add(Cookie::new("second", "two"));
|
|
||||||
///
|
|
||||||
/// assert_eq!(jar.get("name").map(|c| c.value()), Some("value"));
|
|
||||||
/// assert_eq!(jar.get("second").map(|c| c.value()), Some("two"));
|
|
||||||
/// assert_eq!(jar.iter().count(), 2);
|
|
||||||
/// assert_eq!(jar.delta().count(), 2);
|
|
||||||
/// ```
|
|
||||||
pub fn add(&mut self, cookie: Cookie<'static>) {
|
|
||||||
self.delta_cookies.replace(DeltaCookie::added(cookie));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes `cookie` from this jar. If an _original_ cookie with the same
|
|
||||||
/// name as `cookie` is present in the jar, a _removal_ cookie will be
|
|
||||||
/// present in the `delta` computation. To properly generate the removal
|
|
||||||
/// cookie, `cookie` must contain the same `path` and `domain` as the cookie
|
|
||||||
/// that was initially set.
|
|
||||||
///
|
|
||||||
/// A "removal" cookie is a cookie that has the same name as the original
|
|
||||||
/// cookie but has an empty value, a max-age of 0, and an expiration date
|
|
||||||
/// far in the past.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// Removing an _original_ cookie results in a _removal_ cookie:
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
/// use time::Duration;
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
///
|
|
||||||
/// // Assume this cookie originally had a path of "/" and domain of "a.b".
|
|
||||||
/// jar.add_original(Cookie::new("name", "value"));
|
|
||||||
///
|
|
||||||
/// // If the path and domain were set, they must be provided to `remove`.
|
|
||||||
/// jar.remove(Cookie::build("name", "").path("/").domain("a.b").finish());
|
|
||||||
///
|
|
||||||
/// // The delta will contain the removal cookie.
|
|
||||||
/// let delta: Vec<_> = jar.delta().collect();
|
|
||||||
/// assert_eq!(delta.len(), 1);
|
|
||||||
/// assert_eq!(delta[0].name(), "name");
|
|
||||||
/// assert_eq!(delta[0].max_age(), Some(Duration::zero()));
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// Removing a new cookie does not result in a _removal_ cookie:
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.add(Cookie::new("name", "value"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 1);
|
|
||||||
///
|
|
||||||
/// jar.remove(Cookie::named("name"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 0);
|
|
||||||
/// ```
|
|
||||||
pub fn remove(&mut self, mut cookie: Cookie<'static>) {
|
|
||||||
if self.original_cookies.contains(cookie.name()) {
|
|
||||||
cookie.set_value("");
|
|
||||||
cookie.set_max_age(Duration::zero());
|
|
||||||
cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
|
|
||||||
self.delta_cookies.replace(DeltaCookie::removed(cookie));
|
|
||||||
} else {
|
|
||||||
self.delta_cookies.remove(cookie.name());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes `cookie` from this jar completely. This method differs from
|
|
||||||
/// `remove` in that no delta cookie is created under any condition. Neither
|
|
||||||
/// the `delta` nor `iter` methods will return a cookie that is removed
|
|
||||||
/// using this method.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// Removing an _original_ cookie; no _removal_ cookie is generated:
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
/// use time::Duration;
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
///
|
|
||||||
/// // Add an original cookie and a new cookie.
|
|
||||||
/// jar.add_original(Cookie::new("name", "value"));
|
|
||||||
/// jar.add(Cookie::new("key", "value"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 1);
|
|
||||||
/// assert_eq!(jar.iter().count(), 2);
|
|
||||||
///
|
|
||||||
/// // Now force remove the original cookie.
|
|
||||||
/// jar.force_remove(Cookie::new("name", "value"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 1);
|
|
||||||
/// assert_eq!(jar.iter().count(), 1);
|
|
||||||
///
|
|
||||||
/// // Now force remove the new cookie.
|
|
||||||
/// jar.force_remove(Cookie::new("key", "value"));
|
|
||||||
/// assert_eq!(jar.delta().count(), 0);
|
|
||||||
/// assert_eq!(jar.iter().count(), 0);
|
|
||||||
/// ```
|
|
||||||
pub fn force_remove<'a>(&mut self, cookie: Cookie<'a>) {
|
|
||||||
self.original_cookies.remove(cookie.name());
|
|
||||||
self.delta_cookies.remove(cookie.name());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes all cookies from this cookie jar.
|
|
||||||
#[deprecated(
|
|
||||||
since = "0.7.0",
|
|
||||||
note = "calling this method may not remove \
|
|
||||||
all cookies since the path and domain are not specified; use \
|
|
||||||
`remove` instead"
|
|
||||||
)]
|
|
||||||
pub fn clear(&mut self) {
|
|
||||||
self.delta_cookies.clear();
|
|
||||||
for delta in mem::take(&mut self.original_cookies) {
|
|
||||||
self.remove(delta.cookie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an iterator over cookies that represent the changes to this jar
|
|
||||||
/// over time. These cookies can be rendered directly as `Set-Cookie` header
|
|
||||||
/// values to affect the changes made to this jar on the client.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.add_original(Cookie::new("name", "value"));
|
|
||||||
/// jar.add_original(Cookie::new("second", "two"));
|
|
||||||
///
|
|
||||||
/// // Add new cookies.
|
|
||||||
/// jar.add(Cookie::new("new", "third"));
|
|
||||||
/// jar.add(Cookie::new("another", "fourth"));
|
|
||||||
/// jar.add(Cookie::new("yac", "fifth"));
|
|
||||||
///
|
|
||||||
/// // Remove some cookies.
|
|
||||||
/// jar.remove(Cookie::named("name"));
|
|
||||||
/// jar.remove(Cookie::named("another"));
|
|
||||||
///
|
|
||||||
/// // Delta contains two new cookies ("new", "yac") and a removal ("name").
|
|
||||||
/// assert_eq!(jar.delta().count(), 3);
|
|
||||||
/// ```
|
|
||||||
pub fn delta(&self) -> Delta<'_> {
|
|
||||||
Delta {
|
|
||||||
iter: self.delta_cookies.iter(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an iterator over all of the cookies present in this jar.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie};
|
|
||||||
///
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
///
|
|
||||||
/// jar.add_original(Cookie::new("name", "value"));
|
|
||||||
/// jar.add_original(Cookie::new("second", "two"));
|
|
||||||
///
|
|
||||||
/// jar.add(Cookie::new("new", "third"));
|
|
||||||
/// jar.add(Cookie::new("another", "fourth"));
|
|
||||||
/// jar.add(Cookie::new("yac", "fifth"));
|
|
||||||
///
|
|
||||||
/// jar.remove(Cookie::named("name"));
|
|
||||||
/// jar.remove(Cookie::named("another"));
|
|
||||||
///
|
|
||||||
/// // There are three cookies in the jar: "second", "new", and "yac".
|
|
||||||
/// # assert_eq!(jar.iter().count(), 3);
|
|
||||||
/// for cookie in jar.iter() {
|
|
||||||
/// match cookie.name() {
|
|
||||||
/// "second" => assert_eq!(cookie.value(), "two"),
|
|
||||||
/// "new" => assert_eq!(cookie.value(), "third"),
|
|
||||||
/// "yac" => assert_eq!(cookie.value(), "fifth"),
|
|
||||||
/// _ => unreachable!("there are only three cookies in the jar")
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub fn iter(&self) -> Iter<'_> {
|
|
||||||
Iter {
|
|
||||||
delta_cookies: self
|
|
||||||
.delta_cookies
|
|
||||||
.iter()
|
|
||||||
.chain(self.original_cookies.difference(&self.delta_cookies)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a `PrivateJar` with `self` as its parent jar using the key `key`
|
|
||||||
/// to sign/encrypt and verify/decrypt cookies added/retrieved from the
|
|
||||||
/// child jar.
|
|
||||||
///
|
|
||||||
/// Any modifications to the child jar will be reflected on the parent jar,
|
|
||||||
/// and any retrievals from the child jar will be made from the parent jar.
|
|
||||||
///
|
|
||||||
/// This method is only available when the `secure` feature is enabled.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{Cookie, CookieJar, Key};
|
|
||||||
///
|
|
||||||
/// // Generate a secure key.
|
|
||||||
/// let key = Key::generate();
|
|
||||||
///
|
|
||||||
/// // Add a private (signed + encrypted) cookie.
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.private(&key).add(Cookie::new("private", "text"));
|
|
||||||
///
|
|
||||||
/// // The cookie's contents are encrypted.
|
|
||||||
/// assert_ne!(jar.get("private").unwrap().value(), "text");
|
|
||||||
///
|
|
||||||
/// // They can be decrypted and verified through the child jar.
|
|
||||||
/// assert_eq!(jar.private(&key).get("private").unwrap().value(), "text");
|
|
||||||
///
|
|
||||||
/// // A tampered with cookie does not validate but still exists.
|
|
||||||
/// let mut cookie = jar.get("private").unwrap().clone();
|
|
||||||
/// jar.add(Cookie::new("private", cookie.value().to_string() + "!"));
|
|
||||||
/// assert!(jar.private(&key).get("private").is_none());
|
|
||||||
/// assert!(jar.get("private").is_some());
|
|
||||||
/// ```
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
pub fn private(&mut self, key: &Key) -> PrivateJar<'_> {
|
|
||||||
PrivateJar::new(self, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a `SignedJar` with `self` as its parent jar using the key `key`
|
|
||||||
/// to sign/verify cookies added/retrieved from the child jar.
|
|
||||||
///
|
|
||||||
/// Any modifications to the child jar will be reflected on the parent jar,
|
|
||||||
/// and any retrievals from the child jar will be made from the parent jar.
|
|
||||||
///
|
|
||||||
/// This method is only available when the `secure` feature is enabled.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{Cookie, CookieJar, Key};
|
|
||||||
///
|
|
||||||
/// // Generate a secure key.
|
|
||||||
/// let key = Key::generate();
|
|
||||||
///
|
|
||||||
/// // Add a signed cookie.
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.signed(&key).add(Cookie::new("signed", "text"));
|
|
||||||
///
|
|
||||||
/// // The cookie's contents are signed but still in plaintext.
|
|
||||||
/// assert_ne!(jar.get("signed").unwrap().value(), "text");
|
|
||||||
/// assert!(jar.get("signed").unwrap().value().contains("text"));
|
|
||||||
///
|
|
||||||
/// // They can be verified through the child jar.
|
|
||||||
/// assert_eq!(jar.signed(&key).get("signed").unwrap().value(), "text");
|
|
||||||
///
|
|
||||||
/// // A tampered with cookie does not validate but still exists.
|
|
||||||
/// let mut cookie = jar.get("signed").unwrap().clone();
|
|
||||||
/// jar.add(Cookie::new("signed", cookie.value().to_string() + "!"));
|
|
||||||
/// assert!(jar.signed(&key).get("signed").is_none());
|
|
||||||
/// assert!(jar.get("signed").is_some());
|
|
||||||
/// ```
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
pub fn signed(&mut self, key: &Key) -> SignedJar<'_> {
|
|
||||||
SignedJar::new(self, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use std::collections::hash_set::Iter as HashSetIter;
|
|
||||||
|
|
||||||
/// Iterator over the changes to a cookie jar.
|
|
||||||
pub struct Delta<'a> {
|
|
||||||
iter: HashSetIter<'a, DeltaCookie>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Iterator for Delta<'a> {
|
|
||||||
type Item = &'a Cookie<'static>;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<&'a Cookie<'static>> {
|
|
||||||
self.iter.next().map(|c| &c.cookie)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use std::collections::hash_map::RandomState;
|
|
||||||
use std::collections::hash_set::Difference;
|
|
||||||
use std::iter::Chain;
|
|
||||||
|
|
||||||
/// Iterator over all of the cookies in a jar.
|
|
||||||
pub struct Iter<'a> {
|
|
||||||
delta_cookies:
|
|
||||||
Chain<HashSetIter<'a, DeltaCookie>, Difference<'a, DeltaCookie, RandomState>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Iterator for Iter<'a> {
|
|
||||||
type Item = &'a Cookie<'static>;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<&'a Cookie<'static>> {
|
|
||||||
for cookie in self.delta_cookies.by_ref() {
|
|
||||||
if !cookie.removed {
|
|
||||||
return Some(&*cookie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
use super::Key;
|
|
||||||
use super::{Cookie, CookieJar};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[allow(deprecated)]
|
|
||||||
fn simple() {
|
|
||||||
let mut c = CookieJar::new();
|
|
||||||
|
|
||||||
c.add(Cookie::new("test", ""));
|
|
||||||
c.add(Cookie::new("test2", ""));
|
|
||||||
c.remove(Cookie::named("test"));
|
|
||||||
|
|
||||||
assert!(c.get("test").is_none());
|
|
||||||
assert!(c.get("test2").is_some());
|
|
||||||
|
|
||||||
c.add(Cookie::new("test3", ""));
|
|
||||||
c.clear();
|
|
||||||
|
|
||||||
assert!(c.get("test").is_none());
|
|
||||||
assert!(c.get("test2").is_none());
|
|
||||||
assert!(c.get("test3").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn jar_is_send() {
|
|
||||||
fn is_send<T: Send>(_: T) -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
assert!(is_send(CookieJar::new()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
fn iter() {
|
|
||||||
let key = Key::generate();
|
|
||||||
let mut c = CookieJar::new();
|
|
||||||
|
|
||||||
c.add_original(Cookie::new("original", "original"));
|
|
||||||
|
|
||||||
c.add(Cookie::new("test", "test"));
|
|
||||||
c.add(Cookie::new("test2", "test2"));
|
|
||||||
c.add(Cookie::new("test3", "test3"));
|
|
||||||
assert_eq!(c.iter().count(), 4);
|
|
||||||
|
|
||||||
c.signed(&key).add(Cookie::new("signed", "signed"));
|
|
||||||
c.private(&key).add(Cookie::new("encrypted", "encrypted"));
|
|
||||||
assert_eq!(c.iter().count(), 6);
|
|
||||||
|
|
||||||
c.remove(Cookie::named("test"));
|
|
||||||
assert_eq!(c.iter().count(), 5);
|
|
||||||
|
|
||||||
c.remove(Cookie::named("signed"));
|
|
||||||
c.remove(Cookie::named("test2"));
|
|
||||||
assert_eq!(c.iter().count(), 3);
|
|
||||||
|
|
||||||
c.add(Cookie::new("test2", "test2"));
|
|
||||||
assert_eq!(c.iter().count(), 4);
|
|
||||||
|
|
||||||
c.remove(Cookie::named("test2"));
|
|
||||||
assert_eq!(c.iter().count(), 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
fn delta() {
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use time::Duration;
|
|
||||||
|
|
||||||
let mut c = CookieJar::new();
|
|
||||||
|
|
||||||
c.add_original(Cookie::new("original", "original"));
|
|
||||||
c.add_original(Cookie::new("original1", "original1"));
|
|
||||||
|
|
||||||
c.add(Cookie::new("test", "test"));
|
|
||||||
c.add(Cookie::new("test2", "test2"));
|
|
||||||
c.add(Cookie::new("test3", "test3"));
|
|
||||||
c.add(Cookie::new("test4", "test4"));
|
|
||||||
|
|
||||||
c.remove(Cookie::named("test"));
|
|
||||||
c.remove(Cookie::named("original"));
|
|
||||||
|
|
||||||
assert_eq!(c.delta().count(), 4);
|
|
||||||
|
|
||||||
let names: HashMap<_, _> = c.delta().map(|c| (c.name(), c.max_age())).collect();
|
|
||||||
|
|
||||||
assert!(names.get("test2").unwrap().is_none());
|
|
||||||
assert!(names.get("test3").unwrap().is_none());
|
|
||||||
assert!(names.get("test4").unwrap().is_none());
|
|
||||||
assert_eq!(names.get("original").unwrap(), &Some(Duration::zero()));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn replace_original() {
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
jar.add_original(Cookie::new("original_a", "a"));
|
|
||||||
jar.add_original(Cookie::new("original_b", "b"));
|
|
||||||
assert_eq!(jar.get("original_a").unwrap().value(), "a");
|
|
||||||
|
|
||||||
jar.add(Cookie::new("original_a", "av2"));
|
|
||||||
assert_eq!(jar.get("original_a").unwrap().value(), "av2");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn empty_delta() {
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
jar.add(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().count(), 0);
|
|
||||||
|
|
||||||
jar.add_original(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().count(), 0);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
jar.add(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn add_remove_add() {
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
jar.add_original(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().count(), 0);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.value().is_empty()).count(), 1);
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
// The cookie's been deleted. Another original doesn't change that.
|
|
||||||
jar.add_original(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.value().is_empty()).count(), 1);
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.value().is_empty()).count(), 1);
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
jar.add(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().filter(|c| !c.value().is_empty()).count(), 1);
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.value().is_empty()).count(), 1);
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn replace_remove() {
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
jar.add_original(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().count(), 0);
|
|
||||||
|
|
||||||
jar.add(Cookie::new("name", "val"));
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
assert_eq!(jar.delta().filter(|c| !c.value().is_empty()).count(), 1);
|
|
||||||
|
|
||||||
jar.remove(Cookie::named("name"));
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.value().is_empty()).count(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn remove_with_path() {
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
jar.add_original(Cookie::build("name", "val").finish());
|
|
||||||
assert_eq!(jar.iter().count(), 1);
|
|
||||||
assert_eq!(jar.delta().count(), 0);
|
|
||||||
assert_eq!(jar.iter().filter(|c| c.path().is_none()).count(), 1);
|
|
||||||
|
|
||||||
jar.remove(Cookie::build("name", "").path("/").finish());
|
|
||||||
assert_eq!(jar.iter().count(), 0);
|
|
||||||
assert_eq!(jar.delta().count(), 1);
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.value().is_empty()).count(), 1);
|
|
||||||
assert_eq!(jar.delta().filter(|c| c.path() == Some("/")).count(), 1);
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -1,467 +0,0 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
use std::cmp;
|
|
||||||
use std::convert::From;
|
|
||||||
use std::error::Error;
|
|
||||||
use std::fmt;
|
|
||||||
use std::str::Utf8Error;
|
|
||||||
|
|
||||||
use percent_encoding::percent_decode;
|
|
||||||
use time::Duration;
|
|
||||||
|
|
||||||
use super::{Cookie, CookieStr, SameSite};
|
|
||||||
|
|
||||||
use crate::time_parser;
|
|
||||||
|
|
||||||
/// Enum corresponding to a parsing error.
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
||||||
pub enum ParseError {
|
|
||||||
/// The cookie did not contain a name/value pair.
|
|
||||||
MissingPair,
|
|
||||||
/// The cookie's name was empty.
|
|
||||||
EmptyName,
|
|
||||||
/// Decoding the cookie's name or value resulted in invalid UTF-8.
|
|
||||||
Utf8Error(Utf8Error),
|
|
||||||
/// It is discouraged to exhaustively match on this enum as its variants may
|
|
||||||
/// grow without a breaking-change bump in version numbers.
|
|
||||||
#[doc(hidden)]
|
|
||||||
__Nonexhasutive,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ParseError {
|
|
||||||
/// Returns a description of this error as a string
|
|
||||||
pub fn as_str(&self) -> &'static str {
|
|
||||||
match *self {
|
|
||||||
ParseError::MissingPair => "the cookie is missing a name/value pair",
|
|
||||||
ParseError::EmptyName => "the cookie's name is empty",
|
|
||||||
ParseError::Utf8Error(_) => {
|
|
||||||
"decoding the cookie's name or value resulted in invalid UTF-8"
|
|
||||||
}
|
|
||||||
ParseError::__Nonexhasutive => unreachable!("__Nonexhasutive ParseError"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ParseError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "{}", self.as_str())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Utf8Error> for ParseError {
|
|
||||||
fn from(error: Utf8Error) -> ParseError {
|
|
||||||
ParseError::Utf8Error(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for ParseError {}
|
|
||||||
|
|
||||||
fn indexes_of(needle: &str, haystack: &str) -> Option<(usize, usize)> {
|
|
||||||
let haystack_start = haystack.as_ptr() as usize;
|
|
||||||
let needle_start = needle.as_ptr() as usize;
|
|
||||||
|
|
||||||
if needle_start < haystack_start {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (needle_start + needle.len()) > (haystack_start + haystack.len()) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let start = needle_start - haystack_start;
|
|
||||||
let end = start + needle.len();
|
|
||||||
Some((start, end))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name_val_decoded(
|
|
||||||
name: &str,
|
|
||||||
val: &str,
|
|
||||||
) -> Result<(CookieStr, CookieStr), ParseError> {
|
|
||||||
let decoded_name = percent_decode(name.as_bytes()).decode_utf8()?;
|
|
||||||
let decoded_value = percent_decode(val.as_bytes()).decode_utf8()?;
|
|
||||||
let name = CookieStr::Concrete(Cow::Owned(decoded_name.into_owned()));
|
|
||||||
let val = CookieStr::Concrete(Cow::Owned(decoded_value.into_owned()));
|
|
||||||
|
|
||||||
Ok((name, val))
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function does the real parsing but _does not_ set the `cookie_string` in
|
|
||||||
// the returned cookie object. This only exists so that the borrow to `s` is
|
|
||||||
// returned at the end of the call, allowing the `cookie_string` field to be
|
|
||||||
// set in the outer `parse` function.
|
|
||||||
fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
|
|
||||||
let mut attributes = s.split(';');
|
|
||||||
let key_value = match attributes.next() {
|
|
||||||
Some(s) => s,
|
|
||||||
_ => panic!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Determine the name = val.
|
|
||||||
let (name, value) = match key_value.find('=') {
|
|
||||||
Some(i) => (key_value[..i].trim(), key_value[(i + 1)..].trim()),
|
|
||||||
None => return Err(ParseError::MissingPair),
|
|
||||||
};
|
|
||||||
|
|
||||||
if name.is_empty() {
|
|
||||||
return Err(ParseError::EmptyName);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a cookie with all of the defaults. We'll fill things in while we
|
|
||||||
// iterate through the parameters below.
|
|
||||||
let (name, value) = if decode {
|
|
||||||
name_val_decoded(name, value)?
|
|
||||||
} else {
|
|
||||||
let name_indexes = indexes_of(name, s).expect("name sub");
|
|
||||||
let value_indexes = indexes_of(value, s).expect("value sub");
|
|
||||||
let name = CookieStr::Indexed(name_indexes.0, name_indexes.1);
|
|
||||||
let value = CookieStr::Indexed(value_indexes.0, value_indexes.1);
|
|
||||||
|
|
||||||
(name, value)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut cookie = Cookie {
|
|
||||||
name,
|
|
||||||
value,
|
|
||||||
cookie_string: None,
|
|
||||||
expires: None,
|
|
||||||
max_age: None,
|
|
||||||
domain: None,
|
|
||||||
path: None,
|
|
||||||
secure: None,
|
|
||||||
http_only: None,
|
|
||||||
same_site: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
for attr in attributes {
|
|
||||||
let (key, value) = match attr.find('=') {
|
|
||||||
Some(i) => (attr[..i].trim(), Some(attr[(i + 1)..].trim())),
|
|
||||||
None => (attr.trim(), None),
|
|
||||||
};
|
|
||||||
|
|
||||||
match (&*key.to_ascii_lowercase(), value) {
|
|
||||||
("secure", _) => cookie.secure = Some(true),
|
|
||||||
("httponly", _) => cookie.http_only = Some(true),
|
|
||||||
("max-age", Some(v)) => {
|
|
||||||
// See RFC 6265 Section 5.2.2, negative values indicate that the
|
|
||||||
// earliest possible expiration time should be used, so set the
|
|
||||||
// max age as 0 seconds.
|
|
||||||
cookie.max_age = match v.parse() {
|
|
||||||
Ok(val) if val <= 0 => Some(Duration::zero()),
|
|
||||||
Ok(val) => {
|
|
||||||
// Don't panic if the max age seconds is greater than what's supported by
|
|
||||||
// `Duration`.
|
|
||||||
let val = cmp::min(val, Duration::max_value().whole_seconds());
|
|
||||||
Some(Duration::seconds(val))
|
|
||||||
}
|
|
||||||
Err(_) => continue,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
("domain", Some(mut domain)) if !domain.is_empty() => {
|
|
||||||
if domain.starts_with('.') {
|
|
||||||
domain = &domain[1..];
|
|
||||||
}
|
|
||||||
|
|
||||||
let (i, j) = indexes_of(domain, s).expect("domain sub");
|
|
||||||
cookie.domain = Some(CookieStr::Indexed(i, j));
|
|
||||||
}
|
|
||||||
("path", Some(v)) => {
|
|
||||||
let (i, j) = indexes_of(v, s).expect("path sub");
|
|
||||||
cookie.path = Some(CookieStr::Indexed(i, j));
|
|
||||||
}
|
|
||||||
("samesite", Some(v)) => {
|
|
||||||
if v.eq_ignore_ascii_case("strict") {
|
|
||||||
cookie.same_site = Some(SameSite::Strict);
|
|
||||||
} else if v.eq_ignore_ascii_case("lax") {
|
|
||||||
cookie.same_site = Some(SameSite::Lax);
|
|
||||||
} else if v.eq_ignore_ascii_case("none") {
|
|
||||||
cookie.same_site = Some(SameSite::None);
|
|
||||||
} else {
|
|
||||||
// We do nothing here, for now. When/if the `SameSite`
|
|
||||||
// attribute becomes standard, the spec says that we should
|
|
||||||
// ignore this cookie, i.e, fail to parse it, when an
|
|
||||||
// invalid value is passed in. The draft is at
|
|
||||||
// http://httpwg.org/http-extensions/draft-ietf-httpbis-cookie-same-site.html.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
("expires", Some(v)) => {
|
|
||||||
// Try parsing with three date formats according to
|
|
||||||
// http://tools.ietf.org/html/rfc2616#section-3.3.1. Try
|
|
||||||
// additional ones as encountered in the real world.
|
|
||||||
let tm = time_parser::parse_http_date(v)
|
|
||||||
.or_else(|| time::parse(v, "%a, %d-%b-%Y %H:%M:%S").ok());
|
|
||||||
|
|
||||||
if let Some(time) = tm {
|
|
||||||
cookie.expires = Some(time.assume_utc())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
// We're going to be permissive here. If we have no idea what
|
|
||||||
// this is, then it's something nonstandard. We're not going to
|
|
||||||
// store it (because it's not compliant), but we're also not
|
|
||||||
// going to emit an error.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(cookie)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_cookie<'c, S>(cow: S, decode: bool) -> Result<Cookie<'c>, ParseError>
|
|
||||||
where
|
|
||||||
S: Into<Cow<'c, str>>,
|
|
||||||
{
|
|
||||||
let s = cow.into();
|
|
||||||
let mut cookie = parse_inner(&s, decode)?;
|
|
||||||
cookie.cookie_string = Some(s);
|
|
||||||
Ok(cookie)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::{Cookie, SameSite};
|
|
||||||
use time::{Duration, PrimitiveDateTime};
|
|
||||||
|
|
||||||
macro_rules! assert_eq_parse {
|
|
||||||
($string:expr, $expected:expr) => {
|
|
||||||
let cookie = match Cookie::parse($string) {
|
|
||||||
Ok(cookie) => cookie,
|
|
||||||
Err(e) => panic!("Failed to parse {:?}: {:?}", $string, e),
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(cookie, $expected);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! assert_ne_parse {
|
|
||||||
($string:expr, $expected:expr) => {
|
|
||||||
let cookie = match Cookie::parse($string) {
|
|
||||||
Ok(cookie) => cookie,
|
|
||||||
Err(e) => panic!("Failed to parse {:?}: {:?}", $string, e),
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_ne!(cookie, $expected);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_same_site() {
|
|
||||||
let expected = Cookie::build("foo", "bar")
|
|
||||||
.same_site(SameSite::Lax)
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=Lax", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=lax", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=LAX", expected);
|
|
||||||
assert_eq_parse!("foo=bar; samesite=Lax", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SAMESITE=Lax", expected);
|
|
||||||
|
|
||||||
let expected = Cookie::build("foo", "bar")
|
|
||||||
.same_site(SameSite::Strict)
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=Strict", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSITE=Strict", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=strict", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=STrICT", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=STRICT", expected);
|
|
||||||
|
|
||||||
let expected = Cookie::build("foo", "bar")
|
|
||||||
.same_site(SameSite::None)
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=None", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSITE=None", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=nOne", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=NoNE", expected);
|
|
||||||
assert_eq_parse!("foo=bar; SameSite=NONE", expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse() {
|
|
||||||
assert!(Cookie::parse("bar").is_err());
|
|
||||||
assert!(Cookie::parse("=bar").is_err());
|
|
||||||
assert!(Cookie::parse(" =bar").is_err());
|
|
||||||
assert!(Cookie::parse("foo=").is_ok());
|
|
||||||
|
|
||||||
let expected = Cookie::build("foo", "bar=baz").finish();
|
|
||||||
assert_eq_parse!("foo=bar=baz", expected);
|
|
||||||
|
|
||||||
let mut expected = Cookie::build("foo", "bar").finish();
|
|
||||||
assert_eq_parse!("foo=bar", expected);
|
|
||||||
assert_eq_parse!("foo = bar", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;Domain=", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;Domain= ", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;Ignored", expected);
|
|
||||||
|
|
||||||
let mut unexpected = Cookie::build("foo", "bar").http_only(false).finish();
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar; httponly", unexpected);
|
|
||||||
|
|
||||||
expected.set_http_only(true);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;httponly", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HTTPONLY=whatever", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ; sekure; HTTPONLY", expected);
|
|
||||||
|
|
||||||
expected.set_secure(true);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure=aaaa", expected);
|
|
||||||
|
|
||||||
unexpected.set_http_only(true);
|
|
||||||
unexpected.set_secure(true);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; skeure", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; =secure", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly;", unexpected);
|
|
||||||
|
|
||||||
unexpected.set_secure(false);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; secure", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; secure", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; secure", unexpected);
|
|
||||||
|
|
||||||
expected.set_max_age(Duration::zero());
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age=0", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = 0 ", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age=-1", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = -1 ", expected);
|
|
||||||
|
|
||||||
expected.set_max_age(Duration::minutes(1));
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age=60", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = 60 ", expected);
|
|
||||||
|
|
||||||
expected.set_max_age(Duration::seconds(4));
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age=4", expected);
|
|
||||||
assert_eq_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = 4 ", expected);
|
|
||||||
|
|
||||||
unexpected.set_secure(true);
|
|
||||||
unexpected.set_max_age(Duration::minutes(1));
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; Secure; Max-Age=122", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = 38 ", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; Secure; Max-Age=51", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = -1 ", unexpected);
|
|
||||||
assert_ne_parse!(" foo=bar ;HttpOnly; Secure; Max-Age = 0", unexpected);
|
|
||||||
|
|
||||||
expected.set_path("/");
|
|
||||||
assert_eq_parse!("foo=bar;HttpOnly; Secure; Max-Age=4; Path=/", expected);
|
|
||||||
assert_eq_parse!("foo=bar;HttpOnly; Secure; Max-Age=4;Path=/", expected);
|
|
||||||
|
|
||||||
expected.set_path("/foo");
|
|
||||||
assert_eq_parse!("foo=bar;HttpOnly; Secure; Max-Age=4; Path=/foo", expected);
|
|
||||||
assert_eq_parse!("foo=bar;HttpOnly; Secure; Max-Age=4;Path=/foo", expected);
|
|
||||||
assert_eq_parse!("foo=bar;HttpOnly; Secure; Max-Age=4;path=/foo", expected);
|
|
||||||
assert_eq_parse!("foo=bar;HttpOnly; Secure; Max-Age=4;path = /foo", expected);
|
|
||||||
|
|
||||||
unexpected.set_max_age(Duration::seconds(4));
|
|
||||||
unexpected.set_path("/bar");
|
|
||||||
assert_ne_parse!("foo=bar;HttpOnly; Secure; Max-Age=4; Path=/foo", unexpected);
|
|
||||||
assert_ne_parse!("foo=bar;HttpOnly; Secure; Max-Age=4;Path=/baz", unexpected);
|
|
||||||
|
|
||||||
expected.set_domain("www.foo.com");
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=www.foo.com",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
|
|
||||||
expected.set_domain("foo.com");
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=FOO.COM",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
|
|
||||||
unexpected.set_path("/foo");
|
|
||||||
unexpected.set_domain("bar.com");
|
|
||||||
assert_ne_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com",
|
|
||||||
unexpected
|
|
||||||
);
|
|
||||||
assert_ne_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=FOO.COM",
|
|
||||||
unexpected
|
|
||||||
);
|
|
||||||
|
|
||||||
let time_str = "Wed, 21 Oct 2015 07:28:00 GMT";
|
|
||||||
let expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%M:%S")
|
|
||||||
.unwrap()
|
|
||||||
.assume_utc();
|
|
||||||
expected.set_expires(expires);
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
|
|
||||||
unexpected.set_domain("foo.com");
|
|
||||||
let bad_expires = PrimitiveDateTime::parse(time_str, "%a, %d %b %Y %H:%S:%M")
|
|
||||||
.unwrap()
|
|
||||||
.assume_utc();
|
|
||||||
expected.set_expires(bad_expires);
|
|
||||||
assert_ne_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
|
|
||||||
unexpected
|
|
||||||
);
|
|
||||||
|
|
||||||
expected.set_expires(expires);
|
|
||||||
expected.set_same_site(SameSite::Lax);
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT; \
|
|
||||||
SameSite=Lax",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
expected.set_same_site(SameSite::Strict);
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT; \
|
|
||||||
SameSite=Strict",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
expected.set_same_site(SameSite::None);
|
|
||||||
assert_eq_parse!(
|
|
||||||
" foo=bar ;HttpOnly; Secure; Max-Age=4; Path=/foo; \
|
|
||||||
Domain=foo.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT; \
|
|
||||||
SameSite=None",
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn odd_characters() {
|
|
||||||
let expected = Cookie::new("foo", "b%2Fr");
|
|
||||||
assert_eq_parse!("foo=b%2Fr", expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn odd_characters_encoded() {
|
|
||||||
let expected = Cookie::new("foo", "b/r");
|
|
||||||
let cookie = match Cookie::parse_encoded("foo=b%2Fr") {
|
|
||||||
Ok(cookie) => cookie,
|
|
||||||
Err(e) => panic!("Failed to parse: {:?}", e),
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(cookie, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn do_not_panic_on_large_max_ages() {
|
|
||||||
let max_duration = Duration::max_value();
|
|
||||||
let expected = Cookie::build("foo", "bar")
|
|
||||||
.max_age_time(max_duration)
|
|
||||||
.finish();
|
|
||||||
let overflow_duration = max_duration
|
|
||||||
.checked_add(Duration::nanoseconds(1))
|
|
||||||
.unwrap_or(max_duration);
|
|
||||||
assert_eq_parse!(
|
|
||||||
format!(" foo=bar; Max-Age={:?}", overflow_duration.whole_seconds()),
|
|
||||||
expected
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,190 +0,0 @@
|
|||||||
use ring::hkdf::{Algorithm, KeyType, Prk, HKDF_SHA256};
|
|
||||||
use ring::rand::{SecureRandom, SystemRandom};
|
|
||||||
|
|
||||||
use super::private::KEY_LEN as PRIVATE_KEY_LEN;
|
|
||||||
use super::signed::KEY_LEN as SIGNED_KEY_LEN;
|
|
||||||
|
|
||||||
static HKDF_DIGEST: Algorithm = HKDF_SHA256;
|
|
||||||
const KEYS_INFO: &[&[u8]] = &[b"COOKIE;SIGNED:HMAC-SHA256;PRIVATE:AEAD-AES-256-GCM"];
|
|
||||||
|
|
||||||
/// A cryptographic master key for use with `Signed` and/or `Private` jars.
|
|
||||||
///
|
|
||||||
/// This structure encapsulates secure, cryptographic keys for use with both
|
|
||||||
/// [PrivateJar](struct.PrivateJar.html) and [SignedJar](struct.SignedJar.html).
|
|
||||||
/// It can be derived from a single master key via
|
|
||||||
/// [from_master](#method.from_master) or generated from a secure random source
|
|
||||||
/// via [generate](#method.generate). A single instance of `Key` can be used for
|
|
||||||
/// both a `PrivateJar` and a `SignedJar`.
|
|
||||||
///
|
|
||||||
/// This type is only available when the `secure` feature is enabled.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Key {
|
|
||||||
signing_key: [u8; SIGNED_KEY_LEN],
|
|
||||||
encryption_key: [u8; PRIVATE_KEY_LEN],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl KeyType for &Key {
|
|
||||||
#[inline]
|
|
||||||
fn len(&self) -> usize {
|
|
||||||
SIGNED_KEY_LEN + PRIVATE_KEY_LEN
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Key {
|
|
||||||
/// Derives new signing/encryption keys from a master key.
|
|
||||||
///
|
|
||||||
/// The master key must be at least 256-bits (32 bytes). For security, the
|
|
||||||
/// master key _must_ be cryptographically random. The keys are derived
|
|
||||||
/// deterministically from the master key.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if `key` is less than 32 bytes in length.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Key;
|
|
||||||
///
|
|
||||||
/// # /*
|
|
||||||
/// let master_key = { /* a cryptographically random key >= 32 bytes */ };
|
|
||||||
/// # */
|
|
||||||
/// # let master_key: &Vec<u8> = &(0..32).collect();
|
|
||||||
///
|
|
||||||
/// let key = Key::from_master(master_key);
|
|
||||||
/// ```
|
|
||||||
pub fn from_master(key: &[u8]) -> Key {
|
|
||||||
if key.len() < 32 {
|
|
||||||
panic!(
|
|
||||||
"bad master key length: expected at least 32 bytes, found {}",
|
|
||||||
key.len()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// An empty `Key` structure; will be filled in with HKDF derived keys.
|
|
||||||
let mut output_key = Key {
|
|
||||||
signing_key: [0; SIGNED_KEY_LEN],
|
|
||||||
encryption_key: [0; PRIVATE_KEY_LEN],
|
|
||||||
};
|
|
||||||
|
|
||||||
// Expand the master key into two HKDF generated keys.
|
|
||||||
let mut both_keys = [0; SIGNED_KEY_LEN + PRIVATE_KEY_LEN];
|
|
||||||
let prk = Prk::new_less_safe(HKDF_DIGEST, key);
|
|
||||||
let okm = prk.expand(KEYS_INFO, &output_key).expect("okm expand");
|
|
||||||
okm.fill(&mut both_keys).expect("fill keys");
|
|
||||||
|
|
||||||
// Copy the key parts into their respective fields.
|
|
||||||
output_key
|
|
||||||
.signing_key
|
|
||||||
.copy_from_slice(&both_keys[..SIGNED_KEY_LEN]);
|
|
||||||
output_key
|
|
||||||
.encryption_key
|
|
||||||
.copy_from_slice(&both_keys[SIGNED_KEY_LEN..]);
|
|
||||||
output_key
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates signing/encryption keys from a secure, random source. Keys are
|
|
||||||
/// generated non-deterministically.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if randomness cannot be retrieved from the operating system. See
|
|
||||||
/// [try_generate](#method.try_generate) for a non-panicking version.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Key;
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// ```
|
|
||||||
pub fn generate() -> Key {
|
|
||||||
Self::try_generate().expect("failed to generate `Key` from randomness")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Attempts to generate signing/encryption keys from a secure, random
|
|
||||||
/// source. Keys are generated non-deterministically. If randomness cannot be
|
|
||||||
/// retrieved from the underlying operating system, returns `None`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Key;
|
|
||||||
///
|
|
||||||
/// let key = Key::try_generate();
|
|
||||||
/// ```
|
|
||||||
pub fn try_generate() -> Option<Key> {
|
|
||||||
let mut sign_key = [0; SIGNED_KEY_LEN];
|
|
||||||
let mut enc_key = [0; PRIVATE_KEY_LEN];
|
|
||||||
|
|
||||||
let rng = SystemRandom::new();
|
|
||||||
if rng.fill(&mut sign_key).is_err() || rng.fill(&mut enc_key).is_err() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Key {
|
|
||||||
signing_key: sign_key,
|
|
||||||
encryption_key: enc_key,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the raw bytes of a key suitable for signing cookies.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Key;
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let signing_key = key.signing();
|
|
||||||
/// ```
|
|
||||||
pub fn signing(&self) -> &[u8] {
|
|
||||||
&self.signing_key[..]
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the raw bytes of a key suitable for encrypting cookies.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::Key;
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let encryption_key = key.encryption();
|
|
||||||
/// ```
|
|
||||||
pub fn encryption(&self) -> &[u8] {
|
|
||||||
&self.encryption_key[..]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::Key;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deterministic_from_master() {
|
|
||||||
let master_key: Vec<u8> = (0..32).collect();
|
|
||||||
|
|
||||||
let key_a = Key::from_master(&master_key);
|
|
||||||
let key_b = Key::from_master(&master_key);
|
|
||||||
|
|
||||||
assert_eq!(key_a.signing(), key_b.signing());
|
|
||||||
assert_eq!(key_a.encryption(), key_b.encryption());
|
|
||||||
assert_ne!(key_a.encryption(), key_a.signing());
|
|
||||||
|
|
||||||
let master_key_2: Vec<u8> = (32..64).collect();
|
|
||||||
let key_2 = Key::from_master(&master_key_2);
|
|
||||||
|
|
||||||
assert_ne!(key_2.signing(), key_a.signing());
|
|
||||||
assert_ne!(key_2.encryption(), key_a.encryption());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn non_deterministic_generate() {
|
|
||||||
let key_a = Key::generate();
|
|
||||||
let key_b = Key::generate();
|
|
||||||
|
|
||||||
assert_ne!(key_a.signing(), key_b.signing());
|
|
||||||
assert_ne!(key_a.encryption(), key_b.encryption());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
#[cfg(test)]
|
|
||||||
macro_rules! assert_simple_behaviour {
|
|
||||||
($clear:expr, $secure:expr) => {{
|
|
||||||
assert_eq!($clear.iter().count(), 0);
|
|
||||||
|
|
||||||
$secure.add(Cookie::new("name", "val"));
|
|
||||||
assert_eq!($clear.iter().count(), 1);
|
|
||||||
assert_eq!($secure.get("name").unwrap().value(), "val");
|
|
||||||
assert_ne!($clear.get("name").unwrap().value(), "val");
|
|
||||||
|
|
||||||
$secure.add(Cookie::new("another", "two"));
|
|
||||||
assert_eq!($clear.iter().count(), 2);
|
|
||||||
|
|
||||||
$clear.remove(Cookie::named("another"));
|
|
||||||
assert_eq!($clear.iter().count(), 1);
|
|
||||||
|
|
||||||
$secure.remove(Cookie::named("name"));
|
|
||||||
assert_eq!($clear.iter().count(), 0);
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
macro_rules! assert_secure_behaviour {
|
|
||||||
($clear:expr, $secure:expr) => {{
|
|
||||||
$secure.add(Cookie::new("secure", "secure"));
|
|
||||||
assert!($clear.get("secure").unwrap().value() != "secure");
|
|
||||||
assert!($secure.get("secure").unwrap().value() == "secure");
|
|
||||||
|
|
||||||
let mut cookie = $clear.get("secure").unwrap().clone();
|
|
||||||
let new_val = format!("{}l", cookie.value());
|
|
||||||
cookie.set_value(new_val);
|
|
||||||
$clear.add(cookie);
|
|
||||||
assert!($secure.get("secure").is_none());
|
|
||||||
|
|
||||||
let mut cookie = $clear.get("secure").unwrap().clone();
|
|
||||||
cookie.set_value("foobar");
|
|
||||||
$clear.add(cookie);
|
|
||||||
assert!($secure.get("secure").is_none());
|
|
||||||
}};
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
//! Fork of https://github.com/alexcrichton/cookie-rs
|
|
||||||
#[macro_use]
|
|
||||||
mod macros;
|
|
||||||
mod key;
|
|
||||||
mod private;
|
|
||||||
mod signed;
|
|
||||||
|
|
||||||
pub use self::key::*;
|
|
||||||
pub use self::private::*;
|
|
||||||
pub use self::signed::*;
|
|
@ -1,275 +0,0 @@
|
|||||||
use std::str;
|
|
||||||
|
|
||||||
use log::warn;
|
|
||||||
use ring::aead::{Aad, Algorithm, Nonce, AES_256_GCM};
|
|
||||||
use ring::aead::{LessSafeKey, UnboundKey};
|
|
||||||
use ring::rand::{SecureRandom, SystemRandom};
|
|
||||||
|
|
||||||
use super::Key;
|
|
||||||
use crate::cookie::{Cookie, CookieJar};
|
|
||||||
|
|
||||||
// Keep these in sync, and keep the key len synced with the `private` docs as
|
|
||||||
// well as the `KEYS_INFO` const in secure::Key.
|
|
||||||
static ALGO: &Algorithm = &AES_256_GCM;
|
|
||||||
const NONCE_LEN: usize = 12;
|
|
||||||
pub const KEY_LEN: usize = 32;
|
|
||||||
|
|
||||||
/// A child cookie jar that provides authenticated encryption for its cookies.
|
|
||||||
///
|
|
||||||
/// A _private_ child jar signs and encrypts all the cookies added to it and
|
|
||||||
/// verifies and decrypts cookies retrieved from it. Any cookies stored in a
|
|
||||||
/// `PrivateJar` are simultaneously assured confidentiality, integrity, and
|
|
||||||
/// authenticity. In other words, clients cannot discover nor tamper with the
|
|
||||||
/// contents of a cookie, nor can they fabricate cookie data.
|
|
||||||
///
|
|
||||||
/// This type is only available when the `secure` feature is enabled.
|
|
||||||
pub struct PrivateJar<'a> {
|
|
||||||
parent: &'a mut CookieJar,
|
|
||||||
key: [u8; KEY_LEN],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> PrivateJar<'a> {
|
|
||||||
/// Creates a new child `PrivateJar` with parent `parent` and key `key`.
|
|
||||||
/// This method is typically called indirectly via the `signed` method of
|
|
||||||
/// `CookieJar`.
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub fn new(parent: &'a mut CookieJar, key: &Key) -> PrivateJar<'a> {
|
|
||||||
let mut key_array = [0u8; KEY_LEN];
|
|
||||||
key_array.copy_from_slice(key.encryption());
|
|
||||||
PrivateJar {
|
|
||||||
parent,
|
|
||||||
key: key_array,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Given a sealed value `str` and a key name `name`, where the nonce is
|
|
||||||
/// prepended to the original value and then both are Base64 encoded,
|
|
||||||
/// verifies and decrypts the sealed value and returns it. If there's a
|
|
||||||
/// problem, returns an `Err` with a string describing the issue.
|
|
||||||
fn unseal(&self, name: &str, value: &str) -> Result<String, &'static str> {
|
|
||||||
let mut data = base64::decode(value).map_err(|_| "bad base64 value")?;
|
|
||||||
if data.len() <= NONCE_LEN {
|
|
||||||
return Err("length of decoded data is <= NONCE_LEN");
|
|
||||||
}
|
|
||||||
|
|
||||||
let ad = Aad::from(name.as_bytes());
|
|
||||||
let key = LessSafeKey::new(
|
|
||||||
UnboundKey::new(&ALGO, &self.key).expect("matching key length"),
|
|
||||||
);
|
|
||||||
let (nonce, mut sealed) = data.split_at_mut(NONCE_LEN);
|
|
||||||
let nonce =
|
|
||||||
Nonce::try_assume_unique_for_key(nonce).expect("invalid length of `nonce`");
|
|
||||||
let unsealed = key
|
|
||||||
.open_in_place(nonce, ad, &mut sealed)
|
|
||||||
.map_err(|_| "invalid key/nonce/value: bad seal")?;
|
|
||||||
|
|
||||||
if let Ok(unsealed_utf8) = str::from_utf8(unsealed) {
|
|
||||||
Ok(unsealed_utf8.to_string())
|
|
||||||
} else {
|
|
||||||
warn!(
|
|
||||||
"Private cookie does not have utf8 content!
|
|
||||||
It is likely the secret key used to encrypt them has been leaked.
|
|
||||||
Please change it as soon as possible."
|
|
||||||
);
|
|
||||||
Err("bad unsealed utf8")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to the `Cookie` inside this jar with the name `name`
|
|
||||||
/// and authenticates and decrypts the cookie's value, returning a `Cookie`
|
|
||||||
/// with the decrypted value. If the cookie cannot be found, or the cookie
|
|
||||||
/// fails to authenticate or decrypt, `None` is returned.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// let mut private_jar = jar.private(&key);
|
|
||||||
/// assert!(private_jar.get("name").is_none());
|
|
||||||
///
|
|
||||||
/// private_jar.add(Cookie::new("name", "value"));
|
|
||||||
/// assert_eq!(private_jar.get("name").unwrap().value(), "value");
|
|
||||||
/// ```
|
|
||||||
pub fn get(&self, name: &str) -> Option<Cookie<'static>> {
|
|
||||||
if let Some(cookie_ref) = self.parent.get(name) {
|
|
||||||
let mut cookie = cookie_ref.clone();
|
|
||||||
if let Ok(value) = self.unseal(name, cookie.value()) {
|
|
||||||
cookie.set_value(value);
|
|
||||||
return Some(cookie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds `cookie` to the parent jar. The cookie's value is encrypted with
|
|
||||||
/// authenticated encryption assuring confidentiality, integrity, and
|
|
||||||
/// authenticity.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.private(&key).add(Cookie::new("name", "value"));
|
|
||||||
///
|
|
||||||
/// assert_ne!(jar.get("name").unwrap().value(), "value");
|
|
||||||
/// assert_eq!(jar.private(&key).get("name").unwrap().value(), "value");
|
|
||||||
/// ```
|
|
||||||
pub fn add(&mut self, mut cookie: Cookie<'static>) {
|
|
||||||
self.encrypt_cookie(&mut cookie);
|
|
||||||
|
|
||||||
// Add the sealed cookie to the parent.
|
|
||||||
self.parent.add(cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds an "original" `cookie` to parent jar. The cookie's value is
|
|
||||||
/// encrypted with authenticated encryption assuring confidentiality,
|
|
||||||
/// integrity, and authenticity. Adding an original cookie does not affect
|
|
||||||
/// the [`CookieJar::delta()`](struct.CookieJar.html#method.delta)
|
|
||||||
/// computation. This method is intended to be used to seed the cookie jar
|
|
||||||
/// with cookies received from a client's HTTP message.
|
|
||||||
///
|
|
||||||
/// For accurate `delta` computations, this method should not be called
|
|
||||||
/// after calling `remove`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.private(&key).add_original(Cookie::new("name", "value"));
|
|
||||||
///
|
|
||||||
/// assert_eq!(jar.iter().count(), 1);
|
|
||||||
/// assert_eq!(jar.delta().count(), 0);
|
|
||||||
/// ```
|
|
||||||
pub fn add_original(&mut self, mut cookie: Cookie<'static>) {
|
|
||||||
self.encrypt_cookie(&mut cookie);
|
|
||||||
|
|
||||||
// Add the sealed cookie to the parent.
|
|
||||||
self.parent.add_original(cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Encrypts the cookie's value with
|
|
||||||
/// authenticated encryption assuring confidentiality, integrity, and authenticity.
|
|
||||||
fn encrypt_cookie(&self, cookie: &mut Cookie<'_>) {
|
|
||||||
let name = cookie.name().as_bytes();
|
|
||||||
let value = cookie.value().as_bytes();
|
|
||||||
let data = encrypt_name_value(name, value, &self.key);
|
|
||||||
|
|
||||||
// Base64 encode the nonce and encrypted value.
|
|
||||||
let sealed_value = base64::encode(&data);
|
|
||||||
cookie.set_value(sealed_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes `cookie` from the parent jar.
|
|
||||||
///
|
|
||||||
/// For correct removal, the passed in `cookie` must contain the same `path`
|
|
||||||
/// and `domain` as the cookie that was initially set.
|
|
||||||
///
|
|
||||||
/// See [CookieJar::remove](struct.CookieJar.html#method.remove) for more
|
|
||||||
/// details.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// let mut private_jar = jar.private(&key);
|
|
||||||
///
|
|
||||||
/// private_jar.add(Cookie::new("name", "value"));
|
|
||||||
/// assert!(private_jar.get("name").is_some());
|
|
||||||
///
|
|
||||||
/// private_jar.remove(Cookie::named("name"));
|
|
||||||
/// assert!(private_jar.get("name").is_none());
|
|
||||||
/// ```
|
|
||||||
pub fn remove(&mut self, cookie: Cookie<'static>) {
|
|
||||||
self.parent.remove(cookie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn encrypt_name_value(name: &[u8], value: &[u8], key: &[u8]) -> Vec<u8> {
|
|
||||||
// Create the `SealingKey` structure.
|
|
||||||
let unbound = UnboundKey::new(&ALGO, key).expect("matching key length");
|
|
||||||
let key = LessSafeKey::new(unbound);
|
|
||||||
|
|
||||||
// Create a vec to hold the [nonce | cookie value | overhead].
|
|
||||||
let mut data = vec![0; NONCE_LEN + value.len() + ALGO.tag_len()];
|
|
||||||
|
|
||||||
// Randomly generate the nonce, then copy the cookie value as input.
|
|
||||||
let (nonce, in_out) = data.split_at_mut(NONCE_LEN);
|
|
||||||
let (in_out, tag) = in_out.split_at_mut(value.len());
|
|
||||||
in_out.copy_from_slice(value);
|
|
||||||
|
|
||||||
// Randomly generate the nonce into the nonce piece.
|
|
||||||
SystemRandom::new()
|
|
||||||
.fill(nonce)
|
|
||||||
.expect("couldn't random fill nonce");
|
|
||||||
let nonce = Nonce::try_assume_unique_for_key(nonce).expect("invalid `nonce` length");
|
|
||||||
|
|
||||||
// Use cookie's name as associated data to prevent value swapping.
|
|
||||||
let ad = Aad::from(name);
|
|
||||||
let ad_tag = key
|
|
||||||
.seal_in_place_separate_tag(nonce, ad, in_out)
|
|
||||||
.expect("in-place seal");
|
|
||||||
|
|
||||||
// Copy the tag into the tag piece.
|
|
||||||
tag.copy_from_slice(ad_tag.as_ref());
|
|
||||||
|
|
||||||
// Remove the overhead and return the sealed content.
|
|
||||||
data
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::{encrypt_name_value, Cookie, CookieJar, Key};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn simple() {
|
|
||||||
let key = Key::generate();
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
assert_simple_behaviour!(jar, jar.private(&key));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn private() {
|
|
||||||
let key = Key::generate();
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
assert_secure_behaviour!(jar, jar.private(&key));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn non_utf8() {
|
|
||||||
let key = Key::generate();
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
|
|
||||||
let name = "malicious";
|
|
||||||
let mut assert_non_utf8 = |value: &[u8]| {
|
|
||||||
let sealed = encrypt_name_value(name.as_bytes(), value, &key.encryption());
|
|
||||||
let encoded = base64::encode(&sealed);
|
|
||||||
assert_eq!(
|
|
||||||
jar.private(&key).unseal(name, &encoded),
|
|
||||||
Err("bad unsealed utf8")
|
|
||||||
);
|
|
||||||
jar.add(Cookie::new(name, encoded));
|
|
||||||
assert_eq!(jar.private(&key).get(name), None);
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_non_utf8(&[0x72, 0xfb, 0xdf, 0x74]); // rûst in ISO/IEC 8859-1
|
|
||||||
|
|
||||||
let mut malicious =
|
|
||||||
String::from(r#"{"id":"abc123??%X","admin":true}"#).into_bytes();
|
|
||||||
malicious[8] |= 0b1100_0000;
|
|
||||||
malicious[9] |= 0b1100_0000;
|
|
||||||
assert_non_utf8(&malicious);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,184 +0,0 @@
|
|||||||
use ring::hmac::{self, sign, verify};
|
|
||||||
|
|
||||||
use super::Key;
|
|
||||||
use crate::cookie::{Cookie, CookieJar};
|
|
||||||
|
|
||||||
// Keep these in sync, and keep the key len synced with the `signed` docs as
|
|
||||||
// well as the `KEYS_INFO` const in secure::Key.
|
|
||||||
static HMAC_DIGEST: hmac::Algorithm = hmac::HMAC_SHA256;
|
|
||||||
const BASE64_DIGEST_LEN: usize = 44;
|
|
||||||
pub const KEY_LEN: usize = 32;
|
|
||||||
|
|
||||||
/// A child cookie jar that authenticates its cookies.
|
|
||||||
///
|
|
||||||
/// A _signed_ child jar signs all the cookies added to it and verifies cookies
|
|
||||||
/// retrieved from it. Any cookies stored in a `SignedJar` are assured integrity
|
|
||||||
/// and authenticity. In other words, clients cannot tamper with the contents of
|
|
||||||
/// a cookie nor can they fabricate cookie values, but the data is visible in
|
|
||||||
/// plaintext.
|
|
||||||
///
|
|
||||||
/// This type is only available when the `secure` feature is enabled.
|
|
||||||
pub struct SignedJar<'a> {
|
|
||||||
parent: &'a mut CookieJar,
|
|
||||||
key: hmac::Key,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> SignedJar<'a> {
|
|
||||||
/// Creates a new child `SignedJar` with parent `parent` and key `key`. This
|
|
||||||
/// method is typically called indirectly via the `signed` method of
|
|
||||||
/// `CookieJar`.
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub fn new(parent: &'a mut CookieJar, key: &Key) -> SignedJar<'a> {
|
|
||||||
SignedJar {
|
|
||||||
parent,
|
|
||||||
key: hmac::Key::new(HMAC_DIGEST, key.signing()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Given a signed value `str` where the signature is prepended to `value`,
|
|
||||||
/// verifies the signed value and returns it. If there's a problem, returns
|
|
||||||
/// an `Err` with a string describing the issue.
|
|
||||||
fn verify(&self, cookie_value: &str) -> Result<String, &'static str> {
|
|
||||||
if cookie_value.len() < BASE64_DIGEST_LEN {
|
|
||||||
return Err("length of value is <= BASE64_DIGEST_LEN");
|
|
||||||
}
|
|
||||||
|
|
||||||
let (digest_str, value) = cookie_value.split_at(BASE64_DIGEST_LEN);
|
|
||||||
let sig = base64::decode(digest_str).map_err(|_| "bad base64 digest")?;
|
|
||||||
|
|
||||||
verify(&self.key, value.as_bytes(), &sig)
|
|
||||||
.map(|_| value.to_string())
|
|
||||||
.map_err(|_| "value did not verify")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to the `Cookie` inside this jar with the name `name`
|
|
||||||
/// and verifies the authenticity and integrity of the cookie's value,
|
|
||||||
/// returning a `Cookie` with the authenticated value. If the cookie cannot
|
|
||||||
/// be found, or the cookie fails to verify, `None` is returned.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// let mut signed_jar = jar.signed(&key);
|
|
||||||
/// assert!(signed_jar.get("name").is_none());
|
|
||||||
///
|
|
||||||
/// signed_jar.add(Cookie::new("name", "value"));
|
|
||||||
/// assert_eq!(signed_jar.get("name").unwrap().value(), "value");
|
|
||||||
/// ```
|
|
||||||
pub fn get(&self, name: &str) -> Option<Cookie<'static>> {
|
|
||||||
if let Some(cookie_ref) = self.parent.get(name) {
|
|
||||||
let mut cookie = cookie_ref.clone();
|
|
||||||
if let Ok(value) = self.verify(cookie.value()) {
|
|
||||||
cookie.set_value(value);
|
|
||||||
return Some(cookie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds `cookie` to the parent jar. The cookie's value is signed assuring
|
|
||||||
/// integrity and authenticity.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.signed(&key).add(Cookie::new("name", "value"));
|
|
||||||
///
|
|
||||||
/// assert_ne!(jar.get("name").unwrap().value(), "value");
|
|
||||||
/// assert!(jar.get("name").unwrap().value().contains("value"));
|
|
||||||
/// assert_eq!(jar.signed(&key).get("name").unwrap().value(), "value");
|
|
||||||
/// ```
|
|
||||||
pub fn add(&mut self, mut cookie: Cookie<'static>) {
|
|
||||||
self.sign_cookie(&mut cookie);
|
|
||||||
self.parent.add(cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds an "original" `cookie` to this jar. The cookie's value is signed
|
|
||||||
/// assuring integrity and authenticity. Adding an original cookie does not
|
|
||||||
/// affect the [`CookieJar::delta()`](struct.CookieJar.html#method.delta)
|
|
||||||
/// computation. This method is intended to be used to seed the cookie jar
|
|
||||||
/// with cookies received from a client's HTTP message.
|
|
||||||
///
|
|
||||||
/// For accurate `delta` computations, this method should not be called
|
|
||||||
/// after calling `remove`.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// jar.signed(&key).add_original(Cookie::new("name", "value"));
|
|
||||||
///
|
|
||||||
/// assert_eq!(jar.iter().count(), 1);
|
|
||||||
/// assert_eq!(jar.delta().count(), 0);
|
|
||||||
/// ```
|
|
||||||
pub fn add_original(&mut self, mut cookie: Cookie<'static>) {
|
|
||||||
self.sign_cookie(&mut cookie);
|
|
||||||
self.parent.add_original(cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Signs the cookie's value assuring integrity and authenticity.
|
|
||||||
fn sign_cookie(&self, cookie: &mut Cookie<'_>) {
|
|
||||||
let digest = sign(&self.key, cookie.value().as_bytes());
|
|
||||||
let mut new_value = base64::encode(digest.as_ref());
|
|
||||||
new_value.push_str(cookie.value());
|
|
||||||
cookie.set_value(new_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes `cookie` from the parent jar.
|
|
||||||
///
|
|
||||||
/// For correct removal, the passed in `cookie` must contain the same `path`
|
|
||||||
/// and `domain` as the cookie that was initially set.
|
|
||||||
///
|
|
||||||
/// See [CookieJar::remove](struct.CookieJar.html#method.remove) for more
|
|
||||||
/// details.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use actix_http::cookie::{CookieJar, Cookie, Key};
|
|
||||||
///
|
|
||||||
/// let key = Key::generate();
|
|
||||||
/// let mut jar = CookieJar::new();
|
|
||||||
/// let mut signed_jar = jar.signed(&key);
|
|
||||||
///
|
|
||||||
/// signed_jar.add(Cookie::new("name", "value"));
|
|
||||||
/// assert!(signed_jar.get("name").is_some());
|
|
||||||
///
|
|
||||||
/// signed_jar.remove(Cookie::named("name"));
|
|
||||||
/// assert!(signed_jar.get("name").is_none());
|
|
||||||
/// ```
|
|
||||||
pub fn remove(&mut self, cookie: Cookie<'static>) {
|
|
||||||
self.parent.remove(cookie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::{Cookie, CookieJar, Key};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn simple() {
|
|
||||||
let key = Key::generate();
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
assert_simple_behaviour!(jar, jar.signed(&key));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn private() {
|
|
||||||
let key = Key::generate();
|
|
||||||
let mut jar = CookieJar::new();
|
|
||||||
assert_secure_behaviour!(jar, jar.signed(&key));
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,7 +9,7 @@ use brotli2::write::BrotliEncoder;
|
|||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
use flate2::write::{GzEncoder, ZlibEncoder};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use pin_project::{pin_project, project};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
|
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
|
||||||
use crate::http::header::{ContentEncoding, CONTENT_ENCODING};
|
use crate::http::header::{ContentEncoding, CONTENT_ENCODING};
|
||||||
@ -79,7 +79,7 @@ impl<B: MessageBody> Encoder<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = EncoderBodyProj)]
|
||||||
enum EncoderBody<B> {
|
enum EncoderBody<B> {
|
||||||
Bytes(Bytes),
|
Bytes(Bytes),
|
||||||
Stream(#[pin] B),
|
Stream(#[pin] B),
|
||||||
@ -95,22 +95,22 @@ impl<B: MessageBody> MessageBody for EncoderBody<B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[project]
|
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Option<Result<Bytes, Error>>> {
|
) -> Poll<Option<Result<Bytes, Error>>> {
|
||||||
#[project]
|
|
||||||
match self.project() {
|
match self.project() {
|
||||||
EncoderBody::Bytes(b) => {
|
EncoderBodyProj::Bytes(b) => {
|
||||||
if b.is_empty() {
|
if b.is_empty() {
|
||||||
Poll::Ready(None)
|
Poll::Ready(None)
|
||||||
} else {
|
} else {
|
||||||
Poll::Ready(Some(Ok(std::mem::take(b))))
|
Poll::Ready(Some(Ok(std::mem::take(b))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EncoderBody::Stream(b) => b.poll_next(cx),
|
EncoderBodyProj::Stream(b) => b.poll_next(cx),
|
||||||
EncoderBody::BoxedStream(ref mut b) => Pin::new(b.as_mut()).poll_next(cx),
|
EncoderBodyProj::BoxedStream(ref mut b) => {
|
||||||
|
Pin::new(b.as_mut()).poll_next(cx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -964,7 +964,6 @@ impl ResponseError for actix::actors::resolver::ResolverError {}
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use http::{Error as HttpError, StatusCode};
|
use http::{Error as HttpError, StatusCode};
|
||||||
use httparse;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -72,7 +72,7 @@ impl fmt::Debug for Extensions {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_remove() {
|
fn test_remove() {
|
||||||
let mut map = Extensions::new();
|
let mut map = Extensions::new();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::mem::MaybeUninit;
|
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
|
||||||
use actix_codec::Decoder;
|
use actix_codec::Decoder;
|
||||||
@ -46,7 +45,7 @@ impl<T: MessageType> Decoder for MessageDecoder<T> {
|
|||||||
|
|
||||||
pub(crate) enum PayloadLength {
|
pub(crate) enum PayloadLength {
|
||||||
Payload(PayloadType),
|
Payload(PayloadType),
|
||||||
Upgrade,
|
UpgradeWebSocket,
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
raw_headers: &[HeaderIndex],
|
raw_headers: &[HeaderIndex],
|
||||||
) -> Result<PayloadLength, ParseError> {
|
) -> Result<PayloadLength, ParseError> {
|
||||||
let mut ka = None;
|
let mut ka = None;
|
||||||
let mut has_upgrade = false;
|
let mut has_upgrade_websocket = false;
|
||||||
let mut expect = false;
|
let mut expect = false;
|
||||||
let mut chunked = false;
|
let mut chunked = false;
|
||||||
let mut content_length = None;
|
let mut content_length = None;
|
||||||
@ -77,7 +76,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
let name =
|
let name =
|
||||||
HeaderName::from_bytes(&slice[idx.name.0..idx.name.1]).unwrap();
|
HeaderName::from_bytes(&slice[idx.name.0..idx.name.1]).unwrap();
|
||||||
|
|
||||||
// Unsafe: httparse check header value for valid utf-8
|
// SAFETY: httparse checks header value is valid UTF-8
|
||||||
let value = unsafe {
|
let value = unsafe {
|
||||||
HeaderValue::from_maybe_shared_unchecked(
|
HeaderValue::from_maybe_shared_unchecked(
|
||||||
slice.slice(idx.value.0..idx.value.1),
|
slice.slice(idx.value.0..idx.value.1),
|
||||||
@ -124,12 +123,9 @@ pub(crate) trait MessageType: Sized {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
header::UPGRADE => {
|
header::UPGRADE => {
|
||||||
has_upgrade = true;
|
|
||||||
// check content-length, some clients (dart)
|
|
||||||
// sends "content-length: 0" with websocket upgrade
|
|
||||||
if let Ok(val) = value.to_str().map(|val| val.trim()) {
|
if let Ok(val) = value.to_str().map(|val| val.trim()) {
|
||||||
if val.eq_ignore_ascii_case("websocket") {
|
if val.eq_ignore_ascii_case("websocket") {
|
||||||
content_length = None;
|
has_upgrade_websocket = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,13 +152,13 @@ pub(crate) trait MessageType: Sized {
|
|||||||
Ok(PayloadLength::Payload(PayloadType::Payload(
|
Ok(PayloadLength::Payload(PayloadType::Payload(
|
||||||
PayloadDecoder::chunked(),
|
PayloadDecoder::chunked(),
|
||||||
)))
|
)))
|
||||||
|
} else if has_upgrade_websocket {
|
||||||
|
Ok(PayloadLength::UpgradeWebSocket)
|
||||||
} else if let Some(len) = content_length {
|
} else if let Some(len) = content_length {
|
||||||
// Content-Length
|
// Content-Length
|
||||||
Ok(PayloadLength::Payload(PayloadType::Payload(
|
Ok(PayloadLength::Payload(PayloadType::Payload(
|
||||||
PayloadDecoder::length(len),
|
PayloadDecoder::length(len),
|
||||||
)))
|
)))
|
||||||
} else if has_upgrade {
|
|
||||||
Ok(PayloadLength::Upgrade)
|
|
||||||
} else {
|
} else {
|
||||||
Ok(PayloadLength::None)
|
Ok(PayloadLength::None)
|
||||||
}
|
}
|
||||||
@ -184,16 +180,11 @@ impl MessageType for Request {
|
|||||||
&mut self.head_mut().headers
|
&mut self.head_mut().headers
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::uninit_assumed_init)]
|
|
||||||
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
|
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
|
||||||
// Unsafe: we read only this data only after httparse parses headers into.
|
let mut headers: [HeaderIndex; MAX_HEADERS] = EMPTY_HEADER_INDEX_ARRAY;
|
||||||
// performance bump for pipeline benchmarks.
|
|
||||||
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
|
||||||
unsafe { MaybeUninit::uninit().assume_init() };
|
|
||||||
|
|
||||||
let (len, method, uri, ver, h_len) = {
|
let (len, method, uri, ver, h_len) = {
|
||||||
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] =
|
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] = EMPTY_HEADER_ARRAY;
|
||||||
unsafe { MaybeUninit::uninit().assume_init() };
|
|
||||||
|
|
||||||
let mut req = httparse::Request::new(&mut parsed);
|
let mut req = httparse::Request::new(&mut parsed);
|
||||||
match req.parse(src)? {
|
match req.parse(src)? {
|
||||||
@ -222,7 +213,7 @@ impl MessageType for Request {
|
|||||||
// payload decoder
|
// payload decoder
|
||||||
let decoder = match length {
|
let decoder = match length {
|
||||||
PayloadLength::Payload(pl) => pl,
|
PayloadLength::Payload(pl) => pl,
|
||||||
PayloadLength::Upgrade => {
|
PayloadLength::UpgradeWebSocket => {
|
||||||
// upgrade(websocket)
|
// upgrade(websocket)
|
||||||
PayloadType::Stream(PayloadDecoder::eof())
|
PayloadType::Stream(PayloadDecoder::eof())
|
||||||
}
|
}
|
||||||
@ -260,16 +251,11 @@ impl MessageType for ResponseHead {
|
|||||||
&mut self.headers
|
&mut self.headers
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::uninit_assumed_init)]
|
|
||||||
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
|
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
|
||||||
// Unsafe: we read only this data only after httparse parses headers into.
|
let mut headers: [HeaderIndex; MAX_HEADERS] = EMPTY_HEADER_INDEX_ARRAY;
|
||||||
// performance bump for pipeline benchmarks.
|
|
||||||
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
|
||||||
unsafe { MaybeUninit::uninit().assume_init() };
|
|
||||||
|
|
||||||
let (len, ver, status, h_len) = {
|
let (len, ver, status, h_len) = {
|
||||||
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] =
|
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] = EMPTY_HEADER_ARRAY;
|
||||||
unsafe { MaybeUninit::uninit().assume_init() };
|
|
||||||
|
|
||||||
let mut res = httparse::Response::new(&mut parsed);
|
let mut res = httparse::Response::new(&mut parsed);
|
||||||
match res.parse(src)? {
|
match res.parse(src)? {
|
||||||
@ -324,6 +310,17 @@ pub(crate) struct HeaderIndex {
|
|||||||
pub(crate) value: (usize, usize),
|
pub(crate) value: (usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) const EMPTY_HEADER_INDEX: HeaderIndex = HeaderIndex {
|
||||||
|
name: (0, 0),
|
||||||
|
value: (0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(crate) const EMPTY_HEADER_INDEX_ARRAY: [HeaderIndex; MAX_HEADERS] =
|
||||||
|
[EMPTY_HEADER_INDEX; MAX_HEADERS];
|
||||||
|
|
||||||
|
pub(crate) const EMPTY_HEADER_ARRAY: [httparse::Header<'static>; MAX_HEADERS] =
|
||||||
|
[httparse::EMPTY_HEADER; MAX_HEADERS];
|
||||||
|
|
||||||
impl HeaderIndex {
|
impl HeaderIndex {
|
||||||
pub(crate) fn record(
|
pub(crate) fn record(
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
@ -655,10 +652,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_unhandled(&self) -> bool {
|
fn is_unhandled(&self) -> bool {
|
||||||
match self {
|
matches!(self, PayloadType::Stream(_))
|
||||||
PayloadType::Stream(_) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -670,10 +664,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn eof(&self) -> bool {
|
fn eof(&self) -> bool {
|
||||||
match *self {
|
matches!(*self, PayloadItem::Eof)
|
||||||
PayloadItem::Eof => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -979,7 +970,7 @@ mod tests {
|
|||||||
unreachable!("Error");
|
unreachable!("Error");
|
||||||
}
|
}
|
||||||
|
|
||||||
// type in chunked
|
// intentional typo in "chunked"
|
||||||
let mut buf = BytesMut::from(
|
let mut buf = BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
transfer-encoding: chnked\r\n\r\n",
|
transfer-encoding: chnked\r\n\r\n",
|
||||||
@ -1040,7 +1031,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_upgrade() {
|
fn test_http_request_upgrade_websocket() {
|
||||||
let mut buf = BytesMut::from(
|
let mut buf = BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
connection: upgrade\r\n\
|
connection: upgrade\r\n\
|
||||||
@ -1054,6 +1045,26 @@ mod tests {
|
|||||||
assert!(pl.is_unhandled());
|
assert!(pl.is_unhandled());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_http_request_upgrade_h2c() {
|
||||||
|
let mut buf = BytesMut::from(
|
||||||
|
"GET /test HTTP/1.1\r\n\
|
||||||
|
connection: upgrade, http2-settings\r\n\
|
||||||
|
upgrade: h2c\r\n\
|
||||||
|
http2-settings: dummy\r\n\r\n",
|
||||||
|
);
|
||||||
|
let mut reader = MessageDecoder::<Request>::default();
|
||||||
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
|
// `connection: upgrade, http2-settings` doesn't work properly..
|
||||||
|
// see MessageType::set_headers().
|
||||||
|
//
|
||||||
|
// The line below should be:
|
||||||
|
// assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
||||||
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
||||||
|
assert!(req.upgrade());
|
||||||
|
assert!(!pl.is_unhandled());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_parser_utf8() {
|
fn test_http_request_parser_utf8() {
|
||||||
let mut buf = BytesMut::from(
|
let mut buf = BytesMut::from(
|
||||||
|
@ -58,7 +58,7 @@ where
|
|||||||
inner: DispatcherState<T, S, B, X, U>,
|
inner: DispatcherState<T, S, B, X, U>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = DispatcherStateProj)]
|
||||||
enum DispatcherState<T, S, B, X, U>
|
enum DispatcherState<T, S, B, X, U>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request>,
|
S: Service<Request = Request>,
|
||||||
@ -73,7 +73,7 @@ where
|
|||||||
Upgrade(Pin<Box<U::Future>>),
|
Upgrade(Pin<Box<U::Future>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = InnerDispatcherProj)]
|
||||||
struct InnerDispatcher<T, S, B, X, U>
|
struct InnerDispatcher<T, S, B, X, U>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request>,
|
S: Service<Request = Request>,
|
||||||
@ -112,7 +112,7 @@ enum DispatcherMessage {
|
|||||||
Error(Response<()>),
|
Error(Response<()>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = StateProj)]
|
||||||
enum State<S, B, X>
|
enum State<S, B, X>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request>,
|
S: Service<Request = Request>,
|
||||||
@ -132,19 +132,11 @@ where
|
|||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
fn is_empty(&self) -> bool {
|
fn is_empty(&self) -> bool {
|
||||||
if let State::None = self {
|
matches!(self, State::None)
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_call(&self) -> bool {
|
fn is_call(&self) -> bool {
|
||||||
if let State::ServiceCall(_) = self {
|
matches!(self, State::ServiceCall(_))
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
enum PollResponse {
|
enum PollResponse {
|
||||||
@ -156,14 +148,8 @@ enum PollResponse {
|
|||||||
impl PartialEq for PollResponse {
|
impl PartialEq for PollResponse {
|
||||||
fn eq(&self, other: &PollResponse) -> bool {
|
fn eq(&self, other: &PollResponse) -> bool {
|
||||||
match self {
|
match self {
|
||||||
PollResponse::DrainWriteBuf => match other {
|
PollResponse::DrainWriteBuf => matches!(other, PollResponse::DrainWriteBuf),
|
||||||
PollResponse::DrainWriteBuf => true,
|
PollResponse::DoNothing => matches!(other, PollResponse::DoNothing),
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
PollResponse::DoNothing => match other {
|
|
||||||
PollResponse::DoNothing => true,
|
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -296,7 +282,6 @@ where
|
|||||||
///
|
///
|
||||||
/// true - got WouldBlock
|
/// true - got WouldBlock
|
||||||
/// false - didn't get WouldBlock
|
/// false - didn't get WouldBlock
|
||||||
#[pin_project::project]
|
|
||||||
fn poll_flush(
|
fn poll_flush(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
@ -307,8 +292,7 @@ where
|
|||||||
|
|
||||||
let len = self.write_buf.len();
|
let len = self.write_buf.len();
|
||||||
let mut written = 0;
|
let mut written = 0;
|
||||||
#[project]
|
let InnerDispatcherProj { io, write_buf, .. } = self.project();
|
||||||
let InnerDispatcher { io, write_buf, .. } = self.project();
|
|
||||||
let mut io = Pin::new(io.as_mut().unwrap());
|
let mut io = Pin::new(io.as_mut().unwrap());
|
||||||
while written < len {
|
while written < len {
|
||||||
match io.as_mut().poll_write(cx, &write_buf[written..]) {
|
match io.as_mut().poll_write(cx, &write_buf[written..]) {
|
||||||
@ -366,16 +350,14 @@ where
|
|||||||
.extend_from_slice(b"HTTP/1.1 100 Continue\r\n\r\n");
|
.extend_from_slice(b"HTTP/1.1 100 Continue\r\n\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project::project]
|
|
||||||
fn poll_response(
|
fn poll_response(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Result<PollResponse, DispatchError> {
|
) -> Result<PollResponse, DispatchError> {
|
||||||
loop {
|
loop {
|
||||||
let mut this = self.as_mut().project();
|
let mut this = self.as_mut().project();
|
||||||
#[project]
|
|
||||||
let state = match this.state.project() {
|
let state = match this.state.project() {
|
||||||
State::None => match this.messages.pop_front() {
|
StateProj::None => match this.messages.pop_front() {
|
||||||
Some(DispatcherMessage::Item(req)) => {
|
Some(DispatcherMessage::Item(req)) => {
|
||||||
Some(self.as_mut().handle_request(req, cx)?)
|
Some(self.as_mut().handle_request(req, cx)?)
|
||||||
}
|
}
|
||||||
@ -388,7 +370,7 @@ where
|
|||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
},
|
},
|
||||||
State::ExpectCall(fut) => match fut.as_mut().poll(cx) {
|
StateProj::ExpectCall(fut) => match fut.as_mut().poll(cx) {
|
||||||
Poll::Ready(Ok(req)) => {
|
Poll::Ready(Ok(req)) => {
|
||||||
self.as_mut().send_continue();
|
self.as_mut().send_continue();
|
||||||
this = self.as_mut().project();
|
this = self.as_mut().project();
|
||||||
@ -403,7 +385,7 @@ where
|
|||||||
}
|
}
|
||||||
Poll::Pending => None,
|
Poll::Pending => None,
|
||||||
},
|
},
|
||||||
State::ServiceCall(fut) => match fut.as_mut().poll(cx) {
|
StateProj::ServiceCall(fut) => match fut.as_mut().poll(cx) {
|
||||||
Poll::Ready(Ok(res)) => {
|
Poll::Ready(Ok(res)) => {
|
||||||
let (res, body) = res.into().replace_body(());
|
let (res, body) = res.into().replace_body(());
|
||||||
let state = self.as_mut().send_response(res, body)?;
|
let state = self.as_mut().send_response(res, body)?;
|
||||||
@ -418,7 +400,7 @@ where
|
|||||||
}
|
}
|
||||||
Poll::Pending => None,
|
Poll::Pending => None,
|
||||||
},
|
},
|
||||||
State::SendPayload(mut stream) => {
|
StateProj::SendPayload(mut stream) => {
|
||||||
loop {
|
loop {
|
||||||
if this.write_buf.len() < HW_BUFFER_SIZE {
|
if this.write_buf.len() < HW_BUFFER_SIZE {
|
||||||
match stream.as_mut().poll_next(cx) {
|
match stream.as_mut().poll_next(cx) {
|
||||||
@ -724,13 +706,11 @@ where
|
|||||||
{
|
{
|
||||||
type Output = Result<(), DispatchError>;
|
type Output = Result<(), DispatchError>;
|
||||||
|
|
||||||
#[pin_project::project]
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let this = self.as_mut().project();
|
let this = self.as_mut().project();
|
||||||
#[project]
|
|
||||||
match this.inner.project() {
|
match this.inner.project() {
|
||||||
DispatcherState::Normal(mut inner) => {
|
DispatcherStateProj::Normal(mut inner) => {
|
||||||
inner.as_mut().poll_keepalive(cx)?;
|
inner.as_mut().poll_keepalive(cx)?;
|
||||||
|
|
||||||
if inner.flags.contains(Flags::SHUTDOWN) {
|
if inner.flags.contains(Flags::SHUTDOWN) {
|
||||||
@ -850,7 +830,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DispatcherState::Upgrade(fut) => fut.as_mut().poll(cx).map_err(|e| {
|
DispatcherStateProj::Upgrade(fut) => fut.as_mut().poll(cx).map_err(|e| {
|
||||||
error!("Upgrade handler error: {}", e);
|
error!("Upgrade handler error: {}", e);
|
||||||
DispatchError::Upgrade
|
DispatchError::Upgrade
|
||||||
}),
|
}),
|
||||||
@ -867,7 +847,14 @@ where
|
|||||||
T: AsyncRead + Unpin,
|
T: AsyncRead + Unpin,
|
||||||
{
|
{
|
||||||
let mut read_some = false;
|
let mut read_some = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
// If buf is full return but do not disconnect since
|
||||||
|
// there is more reading to be done
|
||||||
|
if buf.len() >= HW_BUFFER_SIZE {
|
||||||
|
return Ok(Some(false));
|
||||||
|
}
|
||||||
|
|
||||||
let remaining = buf.capacity() - buf.len();
|
let remaining = buf.capacity() - buf.len();
|
||||||
if remaining < LW_BUFFER_SIZE {
|
if remaining < LW_BUFFER_SIZE {
|
||||||
buf.reserve(HW_BUFFER_SIZE - remaining);
|
buf.reserve(HW_BUFFER_SIZE - remaining);
|
||||||
|
@ -165,7 +165,7 @@ struct ServiceResponse<F, I, E, B> {
|
|||||||
_t: PhantomData<(I, E)>,
|
_t: PhantomData<(I, E)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
#[pin_project::pin_project(project = ServiceResponseStateProj)]
|
||||||
enum ServiceResponseState<F, B> {
|
enum ServiceResponseState<F, B> {
|
||||||
ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
|
ServiceCall(#[pin] F, Option<SendResponse<Bytes>>),
|
||||||
SendPayload(SendStream<Bytes>, #[pin] ResponseBody<B>),
|
SendPayload(SendStream<Bytes>, #[pin] ResponseBody<B>),
|
||||||
@ -245,13 +245,11 @@ where
|
|||||||
{
|
{
|
||||||
type Output = ();
|
type Output = ();
|
||||||
|
|
||||||
#[pin_project::project]
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let mut this = self.as_mut().project();
|
let mut this = self.as_mut().project();
|
||||||
|
|
||||||
#[project]
|
|
||||||
match this.state.project() {
|
match this.state.project() {
|
||||||
ServiceResponseState::ServiceCall(call, send) => match call.poll(cx) {
|
ServiceResponseStateProj::ServiceCall(call, send) => match call.poll(cx) {
|
||||||
Poll::Ready(Ok(res)) => {
|
Poll::Ready(Ok(res)) => {
|
||||||
let (res, body) = res.into().replace_body(());
|
let (res, body) = res.into().replace_body(());
|
||||||
|
|
||||||
@ -305,55 +303,59 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ServiceResponseState::SendPayload(ref mut stream, ref mut body) => loop {
|
ServiceResponseStateProj::SendPayload(ref mut stream, ref mut body) => {
|
||||||
loop {
|
loop {
|
||||||
if let Some(ref mut buffer) = this.buffer {
|
loop {
|
||||||
match stream.poll_capacity(cx) {
|
if let Some(ref mut buffer) = this.buffer {
|
||||||
Poll::Pending => return Poll::Pending,
|
match stream.poll_capacity(cx) {
|
||||||
Poll::Ready(None) => return Poll::Ready(()),
|
Poll::Pending => return Poll::Pending,
|
||||||
Poll::Ready(Some(Ok(cap))) => {
|
Poll::Ready(None) => return Poll::Ready(()),
|
||||||
let len = buffer.len();
|
Poll::Ready(Some(Ok(cap))) => {
|
||||||
let bytes = buffer.split_to(std::cmp::min(cap, len));
|
let len = buffer.len();
|
||||||
|
let bytes = buffer.split_to(std::cmp::min(cap, len));
|
||||||
|
|
||||||
if let Err(e) = stream.send_data(bytes, false) {
|
if let Err(e) = stream.send_data(bytes, false) {
|
||||||
|
warn!("{:?}", e);
|
||||||
|
return Poll::Ready(());
|
||||||
|
} else if !buffer.is_empty() {
|
||||||
|
let cap =
|
||||||
|
std::cmp::min(buffer.len(), CHUNK_SIZE);
|
||||||
|
stream.reserve_capacity(cap);
|
||||||
|
} else {
|
||||||
|
this.buffer.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Poll::Ready(Some(Err(e))) => {
|
||||||
warn!("{:?}", e);
|
warn!("{:?}", e);
|
||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
} else if !buffer.is_empty() {
|
|
||||||
let cap = std::cmp::min(buffer.len(), CHUNK_SIZE);
|
|
||||||
stream.reserve_capacity(cap);
|
|
||||||
} else {
|
|
||||||
this.buffer.take();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(Err(e))) => {
|
} else {
|
||||||
warn!("{:?}", e);
|
match body.as_mut().poll_next(cx) {
|
||||||
return Poll::Ready(());
|
Poll::Pending => return Poll::Pending,
|
||||||
}
|
Poll::Ready(None) => {
|
||||||
}
|
if let Err(e) = stream.send_data(Bytes::new(), true)
|
||||||
} else {
|
{
|
||||||
match body.as_mut().poll_next(cx) {
|
warn!("{:?}", e);
|
||||||
Poll::Pending => return Poll::Pending,
|
}
|
||||||
Poll::Ready(None) => {
|
return Poll::Ready(());
|
||||||
if let Err(e) = stream.send_data(Bytes::new(), true) {
|
}
|
||||||
warn!("{:?}", e);
|
Poll::Ready(Some(Ok(chunk))) => {
|
||||||
|
stream.reserve_capacity(std::cmp::min(
|
||||||
|
chunk.len(),
|
||||||
|
CHUNK_SIZE,
|
||||||
|
));
|
||||||
|
*this.buffer = Some(chunk);
|
||||||
|
}
|
||||||
|
Poll::Ready(Some(Err(e))) => {
|
||||||
|
error!("Response payload stream error: {:?}", e);
|
||||||
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
|
||||||
Poll::Ready(Some(Ok(chunk))) => {
|
|
||||||
stream.reserve_capacity(std::cmp::min(
|
|
||||||
chunk.len(),
|
|
||||||
CHUNK_SIZE,
|
|
||||||
));
|
|
||||||
*this.buffer = Some(chunk);
|
|
||||||
}
|
|
||||||
Poll::Ready(Some(Err(e))) => {
|
|
||||||
error!("Response payload stream error: {:?}", e);
|
|
||||||
return Poll::Ready(());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use http::Method;
|
|
||||||
use http::header;
|
use http::header;
|
||||||
|
use http::Method;
|
||||||
|
|
||||||
header! {
|
header! {
|
||||||
/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
|
/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
|
||||||
|
@ -387,26 +387,17 @@ impl ContentDisposition {
|
|||||||
|
|
||||||
/// Returns `true` if it is [`Inline`](DispositionType::Inline).
|
/// Returns `true` if it is [`Inline`](DispositionType::Inline).
|
||||||
pub fn is_inline(&self) -> bool {
|
pub fn is_inline(&self) -> bool {
|
||||||
match self.disposition {
|
matches!(self.disposition, DispositionType::Inline)
|
||||||
DispositionType::Inline => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if it is [`Attachment`](DispositionType::Attachment).
|
/// Returns `true` if it is [`Attachment`](DispositionType::Attachment).
|
||||||
pub fn is_attachment(&self) -> bool {
|
pub fn is_attachment(&self) -> bool {
|
||||||
match self.disposition {
|
matches!(self.disposition, DispositionType::Attachment)
|
||||||
DispositionType::Attachment => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if it is [`FormData`](DispositionType::FormData).
|
/// Returns `true` if it is [`FormData`](DispositionType::FormData).
|
||||||
pub fn is_form_data(&self) -> bool {
|
pub fn is_form_data(&self) -> bool {
|
||||||
match self.disposition {
|
matches!(self.disposition, DispositionType::FormData)
|
||||||
DispositionType::FormData => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if it is [`Ext`](DispositionType::Ext) and the `disp_type` matches.
|
/// Returns `true` if it is [`Ext`](DispositionType::Ext) and the `disp_type` matches.
|
||||||
|
@ -9,11 +9,13 @@
|
|||||||
|
|
||||||
pub use self::accept_charset::AcceptCharset;
|
pub use self::accept_charset::AcceptCharset;
|
||||||
//pub use self::accept_encoding::AcceptEncoding;
|
//pub use self::accept_encoding::AcceptEncoding;
|
||||||
pub use self::accept_language::AcceptLanguage;
|
|
||||||
pub use self::accept::Accept;
|
pub use self::accept::Accept;
|
||||||
|
pub use self::accept_language::AcceptLanguage;
|
||||||
pub use self::allow::Allow;
|
pub use self::allow::Allow;
|
||||||
pub use self::cache_control::{CacheControl, CacheDirective};
|
pub use self::cache_control::{CacheControl, CacheDirective};
|
||||||
pub use self::content_disposition::{ContentDisposition, DispositionType, DispositionParam};
|
pub use self::content_disposition::{
|
||||||
|
ContentDisposition, DispositionParam, DispositionType,
|
||||||
|
};
|
||||||
pub use self::content_language::ContentLanguage;
|
pub use self::content_language::ContentLanguage;
|
||||||
pub use self::content_range::{ContentRange, ContentRangeSpec};
|
pub use self::content_range::{ContentRange, ContentRangeSpec};
|
||||||
pub use self::content_type::ContentType;
|
pub use self::content_type::ContentType;
|
||||||
@ -47,7 +49,7 @@ macro_rules! __hyper__deref {
|
|||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@ -74,8 +76,8 @@ macro_rules! test_header {
|
|||||||
($id:ident, $raw:expr) => {
|
($id:ident, $raw:expr) => {
|
||||||
#[test]
|
#[test]
|
||||||
fn $id() {
|
fn $id() {
|
||||||
use $crate::test;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use $crate::test;
|
||||||
|
|
||||||
let raw = $raw;
|
let raw = $raw;
|
||||||
let a: Vec<Vec<u8>> = raw.iter().map(|x| x.to_vec()).collect();
|
let a: Vec<Vec<u8>> = raw.iter().map(|x| x.to_vec()).collect();
|
||||||
@ -118,7 +120,7 @@ macro_rules! test_header {
|
|||||||
// Test formatting
|
// Test formatting
|
||||||
if typed.is_some() {
|
if typed.is_some() {
|
||||||
let raw = &($raw)[..];
|
let raw = &($raw)[..];
|
||||||
let mut iter = raw.iter().map(|b|str::from_utf8(&b[..]).unwrap());
|
let mut iter = raw.iter().map(|b| str::from_utf8(&b[..]).unwrap());
|
||||||
let mut joined = String::new();
|
let mut joined = String::new();
|
||||||
joined.push_str(iter.next().unwrap());
|
joined.push_str(iter.next().unwrap());
|
||||||
for s in iter {
|
for s in iter {
|
||||||
@ -128,7 +130,7 @@ macro_rules! test_header {
|
|||||||
assert_eq!(format!("{}", typed.unwrap()), joined);
|
assert_eq!(format!("{}", typed.unwrap()), joined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
@ -330,11 +332,10 @@ macro_rules! header {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mod accept_charset;
|
mod accept_charset;
|
||||||
//mod accept_encoding;
|
//mod accept_encoding;
|
||||||
mod accept_language;
|
|
||||||
mod accept;
|
mod accept;
|
||||||
|
mod accept_language;
|
||||||
mod allow;
|
mod allow;
|
||||||
mod cache_control;
|
mod cache_control;
|
||||||
mod content_disposition;
|
mod content_disposition;
|
||||||
|
@ -148,10 +148,7 @@ impl ContentEncoding {
|
|||||||
#[inline]
|
#[inline]
|
||||||
/// Is the content compressed?
|
/// Is the content compressed?
|
||||||
pub fn is_compression(self) -> bool {
|
pub fn is_compression(self) -> bool {
|
||||||
match self {
|
matches!(self, ContentEncoding::Identity | ContentEncoding::Auto)
|
||||||
ContentEncoding::Identity | ContentEncoding::Auto => false,
|
|
||||||
_ => true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -167,7 +167,6 @@ where
|
|||||||
mod tests {
|
mod tests {
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use encoding_rs::ISO_8859_2;
|
use encoding_rs::ISO_8859_2;
|
||||||
use mime;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::TestRequest;
|
use crate::test::TestRequest;
|
||||||
|
@ -32,7 +32,7 @@ mod response;
|
|||||||
mod service;
|
mod service;
|
||||||
mod time_parser;
|
mod time_parser;
|
||||||
|
|
||||||
pub mod cookie;
|
pub use cookie;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod h1;
|
pub mod h1;
|
||||||
pub mod h2;
|
pub mod h2;
|
||||||
|
@ -877,7 +877,7 @@ mod tests {
|
|||||||
.domain("www.rust-lang.org")
|
.domain("www.rust-lang.org")
|
||||||
.path("/test")
|
.path("/test")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.max_age_time(time::Duration::days(1))
|
.max_age(time::Duration::days(1))
|
||||||
.finish(),
|
.finish(),
|
||||||
)
|
)
|
||||||
.del_cookie(&cookies[1])
|
.del_cookie(&cookies[1])
|
||||||
|
@ -10,7 +10,7 @@ use bytes::Bytes;
|
|||||||
use futures_core::{ready, Future};
|
use futures_core::{ready, Future};
|
||||||
use futures_util::future::ok;
|
use futures_util::future::ok;
|
||||||
use h2::server::{self, Handshake};
|
use h2::server::{self, Handshake};
|
||||||
use pin_project::{pin_project, project};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::body::MessageBody;
|
use crate::body::MessageBody;
|
||||||
use crate::builder::HttpServiceBuilder;
|
use crate::builder::HttpServiceBuilder;
|
||||||
@ -574,7 +574,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pin_project]
|
#[pin_project(project = StateProj)]
|
||||||
enum State<T, S, B, X, U>
|
enum State<T, S, B, X, U>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request>,
|
S: Service<Request = Request>,
|
||||||
@ -650,16 +650,14 @@ where
|
|||||||
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()>,
|
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()>,
|
||||||
U::Error: fmt::Display,
|
U::Error: fmt::Display,
|
||||||
{
|
{
|
||||||
#[project]
|
|
||||||
fn poll(
|
fn poll(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), DispatchError>> {
|
) -> Poll<Result<(), DispatchError>> {
|
||||||
#[project]
|
|
||||||
match self.as_mut().project() {
|
match self.as_mut().project() {
|
||||||
State::H1(disp) => disp.poll(cx),
|
StateProj::H1(disp) => disp.poll(cx),
|
||||||
State::H2(disp) => disp.poll(cx),
|
StateProj::H2(disp) => disp.poll(cx),
|
||||||
State::H2Handshake(ref mut data) => {
|
StateProj::H2Handshake(ref mut data) => {
|
||||||
let conn = if let Some(ref mut item) = data {
|
let conn = if let Some(ref mut item) = data {
|
||||||
match Pin::new(&mut item.0).poll(cx) {
|
match Pin::new(&mut item.0).poll(cx) {
|
||||||
Poll::Ready(Ok(conn)) => conn,
|
Poll::Ready(Ok(conn)) => conn,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
//! Test Various helpers for Actix applications to use during testing.
|
//! Test Various helpers for Actix applications to use during testing.
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Write as FmtWrite;
|
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -10,9 +9,8 @@ use actix_codec::{AsyncRead, AsyncWrite};
|
|||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use http::header::{self, HeaderName, HeaderValue};
|
use http::header::{self, HeaderName, HeaderValue};
|
||||||
use http::{Error as HttpError, Method, Uri, Version};
|
use http::{Error as HttpError, Method, Uri, Version};
|
||||||
use percent_encoding::percent_encode;
|
|
||||||
|
|
||||||
use crate::cookie::{Cookie, CookieJar, USERINFO};
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
use crate::header::HeaderMap;
|
use crate::header::HeaderMap;
|
||||||
use crate::header::{Header, IntoHeaderValue};
|
use crate::header::{Header, IntoHeaderValue};
|
||||||
use crate::payload::Payload;
|
use crate::payload::Payload;
|
||||||
@ -163,17 +161,17 @@ impl TestRequest {
|
|||||||
head.version = inner.version;
|
head.version = inner.version;
|
||||||
head.headers = inner.headers;
|
head.headers = inner.headers;
|
||||||
|
|
||||||
let mut cookie = String::new();
|
let cookie: String = inner
|
||||||
for c in inner.cookies.delta() {
|
.cookies
|
||||||
let name = percent_encode(c.name().as_bytes(), USERINFO);
|
.delta()
|
||||||
let value = percent_encode(c.value().as_bytes(), USERINFO);
|
// ensure only name=value is written to cookie header
|
||||||
let _ = write!(&mut cookie, "; {}={}", name, value);
|
.map(|c| Cookie::new(c.name(), c.value()).encoded().to_string())
|
||||||
}
|
.collect::<Vec<_>>()
|
||||||
|
.join("; ");
|
||||||
|
|
||||||
if !cookie.is_empty() {
|
if !cookie.is_empty() {
|
||||||
head.headers.insert(
|
head.headers
|
||||||
header::COOKIE,
|
.insert(header::COOKIE, HeaderValue::from_str(&cookie).unwrap());
|
||||||
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req
|
req
|
||||||
|
@ -229,10 +229,7 @@ mod tests {
|
|||||||
fn is_none(
|
fn is_none(
|
||||||
frm: &Result<Option<(bool, OpCode, Option<BytesMut>)>, ProtocolError>,
|
frm: &Result<Option<(bool, OpCode, Option<BytesMut>)>, ProtocolError>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match *frm {
|
matches!(*frm, Ok(None))
|
||||||
Ok(None) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract(
|
fn extract(
|
||||||
|
@ -139,7 +139,7 @@ mod tests {
|
|||||||
let mut masked = unmasked.clone();
|
let mut masked = unmasked.clone();
|
||||||
apply_mask_fallback(&mut masked[1..], &mask);
|
apply_mask_fallback(&mut masked[1..], &mask);
|
||||||
|
|
||||||
let mut masked_fast = unmasked.clone();
|
let mut masked_fast = unmasked;
|
||||||
apply_mask(&mut masked_fast[1..], mask_u32);
|
apply_mask(&mut masked_fast[1..], mask_u32);
|
||||||
|
|
||||||
assert_eq!(masked, masked_fast);
|
assert_eq!(masked, masked_fast);
|
||||||
|
@ -208,10 +208,10 @@ pub fn hash_key(key: &[u8]) -> String {
|
|||||||
use sha1::Digest;
|
use sha1::Digest;
|
||||||
let mut hasher = sha1::Sha1::new();
|
let mut hasher = sha1::Sha1::new();
|
||||||
|
|
||||||
hasher.input(key);
|
hasher.update(key);
|
||||||
hasher.input(WS_GUID.as_bytes());
|
hasher.update(WS_GUID.as_bytes());
|
||||||
|
|
||||||
base64::encode(hasher.result().as_ref())
|
base64::encode(&hasher.finalize())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -274,9 +274,7 @@ async fn test_h2_head_empty() {
|
|||||||
async fn test_h2_head_binary() {
|
async fn test_h2_head_binary() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| {
|
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||||
ok::<_, ()>(Response::Ok().body(STR))
|
|
||||||
})
|
|
||||||
.openssl(ssl_acceptor())
|
.openssl(ssl_acceptor())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -280,9 +280,7 @@ async fn test_h2_head_empty() {
|
|||||||
async fn test_h2_head_binary() {
|
async fn test_h2_head_binary() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| {
|
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||||
ok::<_, ()>(Response::Ok().body(STR))
|
|
||||||
})
|
|
||||||
.rustls(ssl_acceptor())
|
.rustls(ssl_acceptor())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -489,9 +489,7 @@ async fn test_h1_head_empty() {
|
|||||||
async fn test_h1_head_binary() {
|
async fn test_h1_head_binary() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| {
|
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||||
ok::<_, ()>(Response::Ok().body(STR))
|
|
||||||
})
|
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [Unreleased] - 2020-xx-xx
|
## Unreleased - 2020-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 0.3.0-beta.1 - 2020-07-15
|
||||||
|
* Update `actix-web` to 3.0.0-beta.1
|
||||||
|
|
||||||
|
|
||||||
|
## 0.3.0-alpha.1 - 2020-05-25
|
||||||
|
* Update `actix-web` to 3.0.0-alpha.3
|
||||||
* Bump minimum supported Rust version to 1.40
|
* Bump minimum supported Rust version to 1.40
|
||||||
|
* Minimize `futures` dependencies
|
||||||
## [0.2.1] - 2020-01-xx
|
|
||||||
|
|
||||||
* Remove the unused `time` dependency
|
* Remove the unused `time` dependency
|
||||||
|
|
||||||
* Fix missing `std::error::Error` implement for `MultipartError`.
|
* Fix missing `std::error::Error` implement for `MultipartError`.
|
||||||
|
|
||||||
## [0.2.0] - 2019-12-20
|
## [0.2.0] - 2019-12-20
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-multipart"
|
name = "actix-multipart"
|
||||||
version = "0.2.0"
|
version = "0.3.0-beta.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Multipart support for actix web framework."
|
description = "Multipart support for actix web framework."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -8,7 +8,7 @@ keywords = ["http", "web", "framework", "async", "futures"]
|
|||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web.git"
|
||||||
documentation = "https://docs.rs/actix-multipart/"
|
documentation = "https://docs.rs/actix-multipart/"
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
@ -16,7 +16,7 @@ name = "actix_multipart"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "3.0.0-alpha.3", default-features = false }
|
actix-web = { version = "3.0.0-beta.1", default-features = false }
|
||||||
actix-service = "1.0.1"
|
actix-service = "1.0.1"
|
||||||
actix-utils = "1.0.3"
|
actix-utils = "1.0.3"
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
@ -29,4 +29,4 @@ twoway = "0.2"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
actix-http = "2.0.0-alpha.4"
|
actix-http = "2.0.0-beta.1"
|
||||||
|
@ -9,8 +9,6 @@ use std::{cmp, fmt};
|
|||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures_util::stream::{LocalBoxStream, Stream, StreamExt};
|
use futures_util::stream::{LocalBoxStream, Stream, StreamExt};
|
||||||
use httparse;
|
|
||||||
use mime;
|
|
||||||
|
|
||||||
use actix_utils::task::LocalWaker;
|
use actix_utils::task::LocalWaker;
|
||||||
use actix_web::error::{ParseError, PayloadError};
|
use actix_web::error::{ParseError, PayloadError};
|
||||||
@ -876,11 +874,11 @@ mod tests {
|
|||||||
|
|
||||||
impl SlowStream {
|
impl SlowStream {
|
||||||
fn new(bytes: Bytes) -> SlowStream {
|
fn new(bytes: Bytes) -> SlowStream {
|
||||||
return SlowStream {
|
SlowStream {
|
||||||
bytes: bytes,
|
bytes,
|
||||||
pos: 0,
|
pos: 0,
|
||||||
ready: false,
|
ready: false,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
## [Unreleased] - 2020-xx-xx
|
## [Unreleased] - 2020-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## [3.0.0-beta.1] - 2020-xx-xx
|
||||||
|
* Update `actix-web` & `actix-http` dependencies to beta.1
|
||||||
* Bump minimum supported Rust version to 1.40
|
* Bump minimum supported Rust version to 1.40
|
||||||
|
|
||||||
## [3.0.0-alpha.1] - 2020-05-08
|
|
||||||
|
|
||||||
|
## [3.0.0-alpha.1] - 2020-05-08
|
||||||
* Update the actix-web dependency to 3.0.0-alpha.1
|
* Update the actix-web dependency to 3.0.0-alpha.1
|
||||||
* Update the actix dependency to 0.10.0-alpha.2
|
* Update the actix dependency to 0.10.0-alpha.2
|
||||||
* Update the actix-http dependency to 2.0.0-alpha.3
|
* Update the actix-http dependency to 2.0.0-alpha.3
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web-actors"
|
name = "actix-web-actors"
|
||||||
version = "3.0.0-alpha.1"
|
version = "3.0.0-beta.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix actors support for actix web framework."
|
description = "Actix actors support for actix web framework."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -8,7 +8,7 @@ keywords = ["actix", "http", "web", "framework", "async"]
|
|||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web.git"
|
||||||
documentation = "https://docs.rs/actix-web-actors/"
|
documentation = "https://docs.rs/actix-web-actors/"
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
@ -17,13 +17,13 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.10.0-alpha.2"
|
actix = "0.10.0-alpha.2"
|
||||||
actix-web = "3.0.0-alpha.3"
|
actix-web = { version = "3.0.0-beta.1", default-features = false }
|
||||||
actix-http = "2.0.0-alpha.4"
|
actix-http = "2.0.0-beta.1"
|
||||||
actix-codec = "0.2.0"
|
actix-codec = "0.2.0"
|
||||||
bytes = "0.5.2"
|
bytes = "0.5.2"
|
||||||
futures-channel = { version = "0.3.5", default-features = false }
|
futures-channel = { version = "0.3.5", default-features = false }
|
||||||
futures-core = { version = "0.3.5", default-features = false }
|
futures-core = { version = "0.3.5", default-features = false }
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.17"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
|
@ -30,8 +30,8 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Ws {
|
|||||||
async fn test_simple() {
|
async fn test_simple() {
|
||||||
let mut srv = test::start(|| {
|
let mut srv = test::start(|| {
|
||||||
App::new().service(web::resource("/").to(
|
App::new().service(web::resource("/").to(
|
||||||
|req: HttpRequest, stream: web::Payload| {
|
|req: HttpRequest, stream: web::Payload| async move {
|
||||||
async move { ws::start(Ws, &req, stream) }
|
ws::start(Ws, &req, stream)
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
});
|
});
|
||||||
@ -51,7 +51,7 @@ async fn test_simple() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let item = framed.next().await.unwrap().unwrap();
|
let item = framed.next().await.unwrap().unwrap();
|
||||||
assert_eq!(item, ws::Frame::Binary(Bytes::from_static(b"text").into()));
|
assert_eq!(item, ws::Frame::Binary(Bytes::from_static(b"text")));
|
||||||
|
|
||||||
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
||||||
let item = framed.next().await.unwrap().unwrap();
|
let item = framed.next().await.unwrap().unwrap();
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.2.2] - 2020-05-23
|
## Unreleased - 2020-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 0.3.0-beta.1 - 2020-07-14
|
||||||
|
* Add main entry-point macro that uses re-exported runtime. [#1559]
|
||||||
|
|
||||||
|
[#1559]: https://github.com/actix/actix-web/pull/1559
|
||||||
|
|
||||||
|
|
||||||
|
## [0.2.2] - 2020-05-23
|
||||||
* Add resource middleware on actix-web-codegen [#1467]
|
* Add resource middleware on actix-web-codegen [#1467]
|
||||||
|
|
||||||
[#1467]: https://github.com/actix/actix-web/pull/1467
|
[#1467]: https://github.com/actix/actix-web/pull/1467
|
||||||
|
|
||||||
## [0.2.1] - 2020-02-25
|
## [0.2.1] - 2020-02-25
|
||||||
|
|
||||||
* Add `#[allow(missing_docs)]` attribute to generated structs [#1368]
|
* Add `#[allow(missing_docs)]` attribute to generated structs [#1368]
|
||||||
* Allow the handler function to be named as `config` [#1290]
|
* Allow the handler function to be named as `config` [#1290]
|
||||||
|
|
||||||
@ -21,7 +28,6 @@
|
|||||||
## [0.1.3] - 2019-10-14
|
## [0.1.3] - 2019-10-14
|
||||||
|
|
||||||
* Bump up `syn` & `quote` to 1.0
|
* Bump up `syn` & `quote` to 1.0
|
||||||
|
|
||||||
* Provide better error message
|
* Provide better error message
|
||||||
|
|
||||||
## [0.1.2] - 2019-06-04
|
## [0.1.2] - 2019-06-04
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web-codegen"
|
name = "actix-web-codegen"
|
||||||
version = "0.2.2"
|
version = "0.3.0-beta.1"
|
||||||
description = "Actix web proc macros"
|
description = "Actix web proc macros"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
homepage = "https://actix.rs"
|
||||||
|
repository = "https://github.com/actix/actix-web"
|
||||||
|
documentation = "https://docs.rs/actix-web-codegen"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
@ -18,5 +20,5 @@ proc-macro2 = "1"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
actix-web = "3.0.0-alpha.3"
|
actix-web = "3.0.0-beta.1"
|
||||||
futures-util = { version = "0.3.5", default-features = false }
|
futures-util = { version = "0.3.5", default-features = false }
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Macros for actix-web framework [](https://travis-ci.org/actix/actix-web) [](https://codecov.io/gh/actix/actix-web) [](https://crates.io/crates/actix-web-codegen) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
# Helper and convenience macros for Actix-web. [](https://travis-ci.org/actix/actix-web) [](https://codecov.io/gh/actix/actix-web) [](https://crates.io/crates/actix-web-codegen) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
## Documentation & Resources
|
## Documentation & Resources
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#![recursion_limit = "512"]
|
#![recursion_limit = "512"]
|
||||||
//! Actix-web codegen module
|
|
||||||
|
//! Helper and convenience macros for Actix-web.
|
||||||
//!
|
//!
|
||||||
//! Generators for routes and scopes
|
//! ## Runtime Setup
|
||||||
//!
|
//!
|
||||||
//! ## Route
|
//! - [main](attr.main.html)
|
||||||
//!
|
//!
|
||||||
//! Macros:
|
//! ## Resource Macros:
|
||||||
//!
|
//!
|
||||||
//! - [get](attr.get.html)
|
//! - [get](attr.get.html)
|
||||||
//! - [post](attr.post.html)
|
//! - [post](attr.post.html)
|
||||||
@ -23,12 +24,12 @@
|
|||||||
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||||
//! - `wrap="Middleware"` - Registers a resource middleware.
|
//! - `wrap="Middleware"` - Registers a resource middleware.
|
||||||
//!
|
//!
|
||||||
//! ## Notes
|
//! ### Notes
|
||||||
//!
|
//!
|
||||||
//! Function name can be specified as any expression that is going to be accessible to the generate
|
//! Function name can be specified as any expression that is going to be accessible to the generate
|
||||||
//! code (e.g `my_guard` or `my_module::my_guard`)
|
//! code (e.g `my_guard` or `my_module::my_guard`)
|
||||||
//!
|
//!
|
||||||
//! ## Example:
|
//! ### Example:
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use actix_web::HttpResponse;
|
//! use actix_web::HttpResponse;
|
||||||
@ -139,3 +140,43 @@ pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
|
|||||||
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
|
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
route::generate(args, input, route::GuardType::Patch)
|
route::generate(args, input, route::GuardType::Patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Marks async main function as the actix system entry-point.
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// #[actix_web::main]
|
||||||
|
/// async fn main() {
|
||||||
|
/// async { println!("Hello world"); }.await
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
#[cfg(not(test))] // Work around for rust-lang/rust#62127
|
||||||
|
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
|
||||||
|
let attrs = &input.attrs;
|
||||||
|
let vis = &input.vis;
|
||||||
|
let sig = &mut input.sig;
|
||||||
|
let body = &input.block;
|
||||||
|
let name = &sig.ident;
|
||||||
|
|
||||||
|
if sig.asyncness.is_none() {
|
||||||
|
return syn::Error::new_spanned(sig.fn_token, "only async fn is supported")
|
||||||
|
.to_compile_error()
|
||||||
|
.into();
|
||||||
|
}
|
||||||
|
|
||||||
|
sig.asyncness = None;
|
||||||
|
|
||||||
|
(quote! {
|
||||||
|
#(#attrs)*
|
||||||
|
#vis #sig {
|
||||||
|
actix_web::rt::System::new(stringify!(#name))
|
||||||
|
.block_on(async move { #body })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ extern crate proc_macro;
|
|||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||||
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
|
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
|
||||||
use syn::{AttributeArgs, Ident, NestedMeta, parse_macro_input};
|
use syn::{parse_macro_input, AttributeArgs, Ident, NestedMeta};
|
||||||
|
|
||||||
enum ResourceType {
|
enum ResourceType {
|
||||||
Async,
|
Async,
|
||||||
@ -196,7 +196,12 @@ impl ToTokens for Route {
|
|||||||
name,
|
name,
|
||||||
guard,
|
guard,
|
||||||
ast,
|
ast,
|
||||||
args: Args { path, guards, wrappers },
|
args:
|
||||||
|
Args {
|
||||||
|
path,
|
||||||
|
guards,
|
||||||
|
wrappers,
|
||||||
|
},
|
||||||
resource_type,
|
resource_type,
|
||||||
} = self;
|
} = self;
|
||||||
let resource_name = name.to_string();
|
let resource_name = name.to_string();
|
||||||
|
@ -2,11 +2,11 @@ use std::future::Future;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_web::{http, test, web::Path, App, HttpResponse, Responder, Error};
|
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
use actix_web::dev::{Service, Transform, ServiceRequest, ServiceResponse};
|
use actix_web::http::header::{HeaderName, HeaderValue};
|
||||||
|
use actix_web::{http, test, web::Path, App, Error, HttpResponse, Responder};
|
||||||
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, trace};
|
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, trace};
|
||||||
use futures_util::future;
|
use futures_util::future;
|
||||||
use actix_web::http::header::{HeaderName, HeaderValue};
|
|
||||||
|
|
||||||
// Make sure that we can name function as 'config'
|
// Make sure that we can name function as 'config'
|
||||||
#[get("/config")]
|
#[get("/config")]
|
||||||
@ -112,6 +112,7 @@ where
|
|||||||
type Request = ServiceRequest;
|
type Request = ServiceRequest;
|
||||||
type Response = ServiceResponse<B>;
|
type Response = ServiceResponse<B>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::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>> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
@ -119,7 +120,6 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||||
|
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
@ -223,10 +223,7 @@ async fn test_auto_async() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_wrap() {
|
async fn test_wrap() {
|
||||||
let srv = test::start(|| {
|
let srv = test::start(|| App::new().service(get_wrap));
|
||||||
App::new()
|
|
||||||
.service(get_wrap)
|
|
||||||
});
|
|
||||||
|
|
||||||
let request = srv.request(http::Method::GET, srv.url("/test/wrap"));
|
let request = srv.request(http::Method::GET, srv.url("/test/wrap"));
|
||||||
let response = request.send().await.unwrap();
|
let response = request.send().await.unwrap();
|
||||||
|
@ -1,5 +1,17 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## Unreleased - 2020-xx-xx
|
||||||
|
|
||||||
|
|
||||||
|
## 2.0.0-beta.2 - 2020-07-21
|
||||||
|
### Changed
|
||||||
|
* Update `actix-http` dependency to 2.0.0-beta.2
|
||||||
|
|
||||||
|
|
||||||
|
## [2.0.0-beta.1] - 2020-07-14
|
||||||
|
### Changed
|
||||||
|
* Update `actix-http` dependency to 2.0.0-beta.1
|
||||||
|
|
||||||
## [2.0.0-alpha.2] - 2020-05-21
|
## [2.0.0-alpha.2] - 2020-05-21
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,17 +1,20 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "awc"
|
name = "awc"
|
||||||
version = "2.0.0-alpha.2"
|
version = "2.0.0-beta.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix http client."
|
description = "Async HTTP client library that uses the Actix runtime."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["actix", "http", "framework", "async", "web"]
|
keywords = ["actix", "http", "framework", "async", "web"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web.git"
|
||||||
documentation = "https://docs.rs/awc/"
|
documentation = "https://docs.rs/awc/"
|
||||||
categories = ["network-programming", "asynchronous",
|
categories = [
|
||||||
"web-programming::http-client",
|
"network-programming",
|
||||||
"web-programming::websocket"]
|
"asynchronous",
|
||||||
license = "MIT/Apache-2.0"
|
"web-programming::http-client",
|
||||||
|
"web-programming::websocket",
|
||||||
|
]
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
@ -36,7 +39,7 @@ compress = ["actix-http/compress"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.2.0"
|
actix-codec = "0.2.0"
|
||||||
actix-service = "1.0.1"
|
actix-service = "1.0.1"
|
||||||
actix-http = "2.0.0-alpha.4"
|
actix-http = "2.0.0-beta.2"
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
|
|
||||||
base64 = "0.12"
|
base64 = "0.12"
|
||||||
@ -56,7 +59,7 @@ rust-tls = { version = "0.17.0", package = "rustls", optional = true, features =
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-connect = { version = "2.0.0-alpha.2", features = ["openssl"] }
|
actix-connect = { version = "2.0.0-alpha.2", features = ["openssl"] }
|
||||||
actix-web = { version = "3.0.0-alpha.3", features = ["openssl"] }
|
actix-web = { version = "3.0.0-alpha.3", features = ["openssl"] }
|
||||||
actix-http = { version = "2.0.0-alpha.4", features = ["openssl"] }
|
actix-http = { version = "2.0.0-beta.2", features = ["openssl"] }
|
||||||
actix-http-test = { version = "2.0.0-alpha.1", features = ["openssl"] }
|
actix-http-test = { version = "2.0.0-alpha.1", features = ["openssl"] }
|
||||||
actix-utils = "1.0.3"
|
actix-utils = "1.0.3"
|
||||||
actix-server = "1.0.0"
|
actix-server = "1.0.0"
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Write as FmtWrite;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{fmt, net};
|
use std::{fmt, net};
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use percent_encoding::percent_encode;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use actix_http::body::Body;
|
use actix_http::body::Body;
|
||||||
use actix_http::cookie::{Cookie, CookieJar, USERINFO};
|
use actix_http::cookie::{Cookie, CookieJar};
|
||||||
use actix_http::http::header::{self, Header, IntoHeaderValue};
|
use actix_http::http::header::{self, Header, IntoHeaderValue};
|
||||||
use actix_http::http::{
|
use actix_http::http::{
|
||||||
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderName, HeaderValue, Method,
|
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderName, HeaderValue, Method,
|
||||||
@ -527,16 +525,18 @@ impl ClientRequest {
|
|||||||
|
|
||||||
// set cookies
|
// set cookies
|
||||||
if let Some(ref mut jar) = self.cookies {
|
if let Some(ref mut jar) = self.cookies {
|
||||||
let mut cookie = String::new();
|
let cookie: String = jar
|
||||||
for c in jar.delta() {
|
.delta()
|
||||||
let name = percent_encode(c.name().as_bytes(), USERINFO);
|
// ensure only name=value is written to cookie header
|
||||||
let value = percent_encode(c.value().as_bytes(), USERINFO);
|
.map(|c| Cookie::new(c.name(), c.value()).encoded().to_string())
|
||||||
let _ = write!(&mut cookie, "; {}={}", name, value);
|
.collect::<Vec<_>>()
|
||||||
|
.join("; ");
|
||||||
|
|
||||||
|
if !cookie.is_empty() {
|
||||||
|
self.head
|
||||||
|
.headers
|
||||||
|
.insert(header::COOKIE, HeaderValue::from_str(&cookie).unwrap());
|
||||||
}
|
}
|
||||||
self.head.headers.insert(
|
|
||||||
header::COOKIE,
|
|
||||||
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut slf = self;
|
let mut slf = self;
|
||||||
@ -586,16 +586,16 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::Client;
|
use crate::Client;
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn test_debug() {
|
async fn test_debug() {
|
||||||
let request = Client::new().get("/").header("x-test", "111");
|
let request = Client::new().get("/").header("x-test", "111");
|
||||||
let repr = format!("{:?}", request);
|
let repr = format!("{:?}", request);
|
||||||
assert!(repr.contains("ClientRequest"));
|
assert!(repr.contains("ClientRequest"));
|
||||||
assert!(repr.contains("x-test"));
|
assert!(repr.contains("x-test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn test_basics() {
|
async fn test_basics() {
|
||||||
let mut req = Client::new()
|
let mut req = Client::new()
|
||||||
.put("/")
|
.put("/")
|
||||||
.version(Version::HTTP_2)
|
.version(Version::HTTP_2)
|
||||||
@ -621,8 +621,8 @@ mod tests {
|
|||||||
let _ = req.send_body("");
|
let _ = req.send_body("");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn test_client_header() {
|
async fn test_client_header() {
|
||||||
let req = Client::build()
|
let req = Client::build()
|
||||||
.header(header::CONTENT_TYPE, "111")
|
.header(header::CONTENT_TYPE, "111")
|
||||||
.finish()
|
.finish()
|
||||||
@ -639,8 +639,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn test_client_header_override() {
|
async fn test_client_header_override() {
|
||||||
let req = Client::build()
|
let req = Client::build()
|
||||||
.header(header::CONTENT_TYPE, "111")
|
.header(header::CONTENT_TYPE, "111")
|
||||||
.finish()
|
.finish()
|
||||||
@ -658,8 +658,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn client_basic_auth() {
|
async fn client_basic_auth() {
|
||||||
let req = Client::new()
|
let req = Client::new()
|
||||||
.get("/")
|
.get("/")
|
||||||
.basic_auth("username", Some("password"));
|
.basic_auth("username", Some("password"));
|
||||||
@ -685,8 +685,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn client_bearer_auth() {
|
async fn client_bearer_auth() {
|
||||||
let req = Client::new().get("/").bearer_auth("someS3cr3tAutht0k3n");
|
let req = Client::new().get("/").bearer_auth("someS3cr3tAutht0k3n");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
req.head
|
req.head
|
||||||
@ -699,8 +699,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[actix_rt::test]
|
||||||
fn client_query() {
|
async fn client_query() {
|
||||||
let req = Client::new()
|
let req = Client::new()
|
||||||
.get("/")
|
.get("/")
|
||||||
.query(&[("key1", "val1"), ("key2", "val2")])
|
.query(&[("key1", "val1"), ("key2", "val2")])
|
||||||
|
@ -402,14 +402,12 @@ mod tests {
|
|||||||
|
|
||||||
fn json_eq(err: JsonPayloadError, other: JsonPayloadError) -> bool {
|
fn json_eq(err: JsonPayloadError, other: JsonPayloadError) -> bool {
|
||||||
match err {
|
match err {
|
||||||
JsonPayloadError::Payload(PayloadError::Overflow) => match other {
|
JsonPayloadError::Payload(PayloadError::Overflow) => {
|
||||||
JsonPayloadError::Payload(PayloadError::Overflow) => true,
|
matches!(other, JsonPayloadError::Payload(PayloadError::Overflow))
|
||||||
_ => false,
|
}
|
||||||
},
|
JsonPayloadError::ContentType => {
|
||||||
JsonPayloadError::ContentType => match other {
|
matches!(other, JsonPayloadError::ContentType)
|
||||||
JsonPayloadError::ContentType => true,
|
}
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
//! Test helpers for actix http client to use during testing.
|
//! Test helpers for actix http client to use during testing.
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Write as FmtWrite;
|
|
||||||
|
|
||||||
use actix_http::cookie::{Cookie, CookieJar, USERINFO};
|
use actix_http::cookie::{Cookie, CookieJar};
|
||||||
use actix_http::http::header::{self, Header, HeaderValue, IntoHeaderValue};
|
use actix_http::http::header::{self, Header, HeaderValue, IntoHeaderValue};
|
||||||
use actix_http::http::{Error as HttpError, HeaderName, StatusCode, Version};
|
use actix_http::http::{Error as HttpError, HeaderName, StatusCode, Version};
|
||||||
use actix_http::{h1, Payload, ResponseHead};
|
use actix_http::{h1, Payload, ResponseHead};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use percent_encoding::percent_encode;
|
|
||||||
|
|
||||||
use crate::ClientResponse;
|
use crate::ClientResponse;
|
||||||
|
|
||||||
@ -88,16 +86,10 @@ impl TestResponse {
|
|||||||
pub fn finish(self) -> ClientResponse {
|
pub fn finish(self) -> ClientResponse {
|
||||||
let mut head = self.head;
|
let mut head = self.head;
|
||||||
|
|
||||||
let mut cookie = String::new();
|
for cookie in self.cookies.delta() {
|
||||||
for c in self.cookies.delta() {
|
|
||||||
let name = percent_encode(c.name().as_bytes(), USERINFO);
|
|
||||||
let value = percent_encode(c.value().as_bytes(), USERINFO);
|
|
||||||
let _ = write!(&mut cookie, "; {}={}", name, value);
|
|
||||||
}
|
|
||||||
if !cookie.is_empty() {
|
|
||||||
head.headers.insert(
|
head.headers.insert(
|
||||||
header::SET_COOKIE,
|
header::SET_COOKIE,
|
||||||
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
|
HeaderValue::from_str(&cookie.encoded().to_string()).unwrap(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
//! Websockets client
|
//! Websockets client
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Write as FmtWrite;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
@ -9,9 +8,7 @@ use actix_codec::Framed;
|
|||||||
use actix_http::cookie::{Cookie, CookieJar};
|
use actix_http::cookie::{Cookie, CookieJar};
|
||||||
use actix_http::{ws, Payload, RequestHead};
|
use actix_http::{ws, Payload, RequestHead};
|
||||||
use actix_rt::time::timeout;
|
use actix_rt::time::timeout;
|
||||||
use percent_encoding::percent_encode;
|
|
||||||
|
|
||||||
use actix_http::cookie::USERINFO;
|
|
||||||
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
||||||
|
|
||||||
use crate::connect::BoxedSocket;
|
use crate::connect::BoxedSocket;
|
||||||
@ -246,16 +243,18 @@ impl WebsocketsRequest {
|
|||||||
|
|
||||||
// set cookies
|
// set cookies
|
||||||
if let Some(ref mut jar) = self.cookies {
|
if let Some(ref mut jar) = self.cookies {
|
||||||
let mut cookie = String::new();
|
let cookie: String = jar
|
||||||
for c in jar.delta() {
|
.delta()
|
||||||
let name = percent_encode(c.name().as_bytes(), USERINFO);
|
// ensure only name=value is written to cookie header
|
||||||
let value = percent_encode(c.value().as_bytes(), USERINFO);
|
.map(|c| Cookie::new(c.name(), c.value()).encoded().to_string())
|
||||||
let _ = write!(&mut cookie, "; {}={}", name, value);
|
.collect::<Vec<_>>()
|
||||||
|
.join("; ");
|
||||||
|
|
||||||
|
if !cookie.is_empty() {
|
||||||
|
self.head
|
||||||
|
.headers
|
||||||
|
.insert(header::COOKIE, HeaderValue::from_str(&cookie).unwrap());
|
||||||
}
|
}
|
||||||
self.head.headers.insert(
|
|
||||||
header::COOKIE,
|
|
||||||
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// origin
|
// origin
|
||||||
|
@ -167,8 +167,7 @@ async fn test_connection_reuse() {
|
|||||||
})
|
})
|
||||||
.and_then(
|
.and_then(
|
||||||
HttpService::new(map_config(
|
HttpService::new(map_config(
|
||||||
App::new()
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
||||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok()))),
|
|
||||||
|_| AppConfig::default(),
|
|_| AppConfig::default(),
|
||||||
))
|
))
|
||||||
.tcp(),
|
.tcp(),
|
||||||
@ -205,8 +204,7 @@ async fn test_connection_force_close() {
|
|||||||
})
|
})
|
||||||
.and_then(
|
.and_then(
|
||||||
HttpService::new(map_config(
|
HttpService::new(map_config(
|
||||||
App::new()
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
||||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok()))),
|
|
||||||
|_| AppConfig::default(),
|
|_| AppConfig::default(),
|
||||||
))
|
))
|
||||||
.tcp(),
|
.tcp(),
|
||||||
|
@ -32,8 +32,7 @@ async fn test_connection_window_size() {
|
|||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(map_config(
|
.h2(map_config(
|
||||||
App::new()
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
||||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok()))),
|
|
||||||
|_| AppConfig::default(),
|
|_| AppConfig::default(),
|
||||||
))
|
))
|
||||||
.openssl(ssl_acceptor())
|
.openssl(ssl_acceptor())
|
||||||
|
@ -64,9 +64,8 @@ async fn _test_connection_reuse_h2() {
|
|||||||
.and_then(
|
.and_then(
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(map_config(
|
.h2(map_config(
|
||||||
App::new().service(
|
App::new()
|
||||||
web::resource("/").route(web::to(|| HttpResponse::Ok())),
|
.service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
||||||
),
|
|
||||||
|_| AppConfig::default(),
|
|_| AppConfig::default(),
|
||||||
))
|
))
|
||||||
.openssl(ssl_acceptor())
|
.openssl(ssl_acceptor())
|
||||||
|
@ -45,9 +45,8 @@ async fn test_connection_reuse_h2() {
|
|||||||
.and_then(
|
.and_then(
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(map_config(
|
.h2(map_config(
|
||||||
App::new().service(
|
App::new()
|
||||||
web::resource("/").route(web::to(|| HttpResponse::Ok())),
|
.service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
||||||
),
|
|
||||||
|_| AppConfig::default(),
|
|_| AppConfig::default(),
|
||||||
))
|
))
|
||||||
.openssl(ssl_acceptor())
|
.openssl(ssl_acceptor())
|
||||||
|
@ -27,15 +27,16 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|||||||
|
|
||||||
// benchmark sending all requests at the same time
|
// benchmark sending all requests at the same time
|
||||||
fn bench_async_burst(c: &mut Criterion) {
|
fn bench_async_burst(c: &mut Criterion) {
|
||||||
|
// We are using System here, since Runtime requires preinitialized tokio
|
||||||
|
// Maybe add to actix_rt docs
|
||||||
|
let mut rt = actix_rt::System::new("test");
|
||||||
|
|
||||||
let srv = test::start(|| {
|
let srv = test::start(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||||
});
|
});
|
||||||
|
|
||||||
// We are using System here, since Runtime requires preinitialized tokio
|
|
||||||
// Maybe add to actix_rt docs
|
|
||||||
let url = srv.url("/");
|
let url = srv.url("/");
|
||||||
let mut rt = actix_rt::System::new("test");
|
|
||||||
|
|
||||||
c.bench_function("get_body_async_burst", move |b| {
|
c.bench_function("get_body_async_burst", move |b| {
|
||||||
b.iter_custom(|iters| {
|
b.iter_custom(|iters| {
|
||||||
|
@ -16,7 +16,7 @@ async fn no_params() -> &'static str {
|
|||||||
"Hello world!\r\n"
|
"Hello world!\r\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use actix_http::Error;
|
use actix_http::Error;
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), Error> {
|
async fn main() -> Result<(), Error> {
|
||||||
std::env::set_var("RUST_LOG", "actix_http=trace");
|
std::env::set_var("RUST_LOG", "actix_http=trace");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
@ -20,7 +20,7 @@ async fn no_params() -> &'static str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[actix_rt::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
@ -0,0 +1 @@
|
|||||||
|
1.42.0
|
22
src/app.rs
22
src/app.rs
@ -42,6 +42,7 @@ pub struct App<T, B> {
|
|||||||
|
|
||||||
impl App<AppEntry, Body> {
|
impl App<AppEntry, Body> {
|
||||||
/// Create application builder. Application can be configured with a builder-like pattern.
|
/// Create application builder. Application can be configured with a builder-like pattern.
|
||||||
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let fref = Rc::new(RefCell::new(None));
|
||||||
App {
|
App {
|
||||||
@ -488,7 +489,7 @@ mod tests {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_resource() {
|
async fn test_default_resource() {
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new().service(web::resource("/test").to(|| HttpResponse::Ok())),
|
App::new().service(web::resource("/test").to(HttpResponse::Ok)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
@ -501,13 +502,13 @@ mod tests {
|
|||||||
|
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::resource("/test").to(|| HttpResponse::Ok()))
|
.service(web::resource("/test").to(HttpResponse::Ok))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/test2")
|
web::resource("/test2")
|
||||||
.default_service(|r: ServiceRequest| {
|
.default_service(|r: ServiceRequest| {
|
||||||
ok(r.into_response(HttpResponse::Created()))
|
ok(r.into_response(HttpResponse::Created()))
|
||||||
})
|
})
|
||||||
.route(web::get().to(|| HttpResponse::Ok())),
|
.route(web::get().to(HttpResponse::Ok)),
|
||||||
)
|
)
|
||||||
.default_service(|r: ServiceRequest| {
|
.default_service(|r: ServiceRequest| {
|
||||||
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
||||||
@ -584,7 +585,7 @@ mod tests {
|
|||||||
DefaultHeaders::new()
|
DefaultHeaders::new()
|
||||||
.header(header::CONTENT_TYPE, HeaderValue::from_static("0001")),
|
.header(header::CONTENT_TYPE, HeaderValue::from_static("0001")),
|
||||||
)
|
)
|
||||||
.route("/test", web::get().to(|| HttpResponse::Ok())),
|
.route("/test", web::get().to(HttpResponse::Ok)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
@ -600,7 +601,7 @@ mod tests {
|
|||||||
async fn test_router_wrap() {
|
async fn test_router_wrap() {
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.route("/test", web::get().to(|| HttpResponse::Ok()))
|
.route("/test", web::get().to(HttpResponse::Ok))
|
||||||
.wrap(
|
.wrap(
|
||||||
DefaultHeaders::new()
|
DefaultHeaders::new()
|
||||||
.header(header::CONTENT_TYPE, HeaderValue::from_static("0001")),
|
.header(header::CONTENT_TYPE, HeaderValue::from_static("0001")),
|
||||||
@ -631,7 +632,7 @@ mod tests {
|
|||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.service(web::resource("/test").to(|| HttpResponse::Ok())),
|
.service(web::resource("/test").to(HttpResponse::Ok)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
@ -647,7 +648,7 @@ mod tests {
|
|||||||
async fn test_router_wrap_fn() {
|
async fn test_router_wrap_fn() {
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.route("/test", web::get().to(|| HttpResponse::Ok()))
|
.route("/test", web::get().to(HttpResponse::Ok))
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
let fut = srv.call(req);
|
let fut = srv.call(req);
|
||||||
async {
|
async {
|
||||||
@ -678,10 +679,9 @@ mod tests {
|
|||||||
.route(
|
.route(
|
||||||
"/test",
|
"/test",
|
||||||
web::get().to(|req: HttpRequest| {
|
web::get().to(|req: HttpRequest| {
|
||||||
HttpResponse::Ok().body(format!(
|
HttpResponse::Ok().body(
|
||||||
"{}",
|
req.url_for("youtube", &["12345"]).unwrap().to_string(),
|
||||||
req.url_for("youtube", &["12345"]).unwrap()
|
)
|
||||||
))
|
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -10,6 +10,7 @@ use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url};
|
|||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||||
use actix_service::{fn_service, Service, ServiceFactory};
|
use actix_service::{fn_service, Service, ServiceFactory};
|
||||||
use futures_util::future::{join_all, ok, FutureExt, LocalBoxFuture};
|
use futures_util::future::{join_all, ok, FutureExt, LocalBoxFuture};
|
||||||
|
use tinyvec::tiny_vec;
|
||||||
|
|
||||||
use crate::config::{AppConfig, AppService};
|
use crate::config::{AppConfig, AppService};
|
||||||
use crate::data::{DataFactory, FnDataFactory};
|
use crate::data::{DataFactory, FnDataFactory};
|
||||||
@ -245,7 +246,7 @@ where
|
|||||||
inner.path.reset();
|
inner.path.reset();
|
||||||
inner.head = head;
|
inner.head = head;
|
||||||
inner.payload = payload;
|
inner.payload = payload;
|
||||||
inner.app_data.push(self.data.clone());
|
inner.app_data = tiny_vec![self.data.clone()];
|
||||||
req
|
req
|
||||||
} else {
|
} else {
|
||||||
HttpRequest::new(
|
HttpRequest::new(
|
||||||
@ -474,7 +475,7 @@ mod tests {
|
|||||||
let mut app = init_service(
|
let mut app = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(DropData(data.clone()))
|
.data(DropData(data.clone()))
|
||||||
.service(web::resource("/test").to(|| HttpResponse::Ok())),
|
.service(web::resource("/test").to(HttpResponse::Ok)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user