mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-04 18:06:23 +02:00
Compare commits
39 Commits
codegen-v0
...
files-v0.1
Author | SHA1 | Date | |
---|---|---|---|
a3a78ac6fb | |||
596483ff55 | |||
768859513a | |||
44bb79cd07 | |||
af9fb5d190 | |||
c0c71f82c0 | |||
b948f74b54 | |||
1a24ff8717 | |||
47fab0e393 | |||
313ac48765 | |||
d7780d53c9 | |||
ad0e6f73b3 | |||
546a8a58db | |||
acda1c075a | |||
686e5f1595 | |||
d2b6502c7a | |||
7c0f570845 | |||
eaa371db8b | |||
d293ae2a69 | |||
d7ec241fd0 | |||
cd323f2ff1 | |||
bf48798bce | |||
c8118e8411 | |||
e7ba67e1a8 | |||
13e618b128 | |||
36e6f0cb4b | |||
7450ae37a7 | |||
2ffda29f9b | |||
ff724e239d | |||
ee769832cf | |||
c4b7980b4f | |||
bfbac4f875 | |||
53e2f8090f | |||
e399e01a22 | |||
d9a62c4bbf | |||
a548b69679 | |||
ae64475d98 | |||
a342b1289d | |||
38f04b75a7 |
55
CHANGES.md
55
CHANGES.md
@ -1,8 +1,53 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [1.0.0] - 2019-06-xx
|
## [1.0.3] - unreleased
|
||||||
|
|
||||||
### Add
|
### Added
|
||||||
|
|
||||||
|
* Support asynchronous data factories #850
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Use `encoding_rs` crate instead of unmaintained `encoding` crate
|
||||||
|
|
||||||
|
|
||||||
|
## [1.0.2] - 2019-06-17
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Move cors middleware to `actix-cors` crate.
|
||||||
|
|
||||||
|
* Move identity middleware to `actix-identity` crate.
|
||||||
|
|
||||||
|
|
||||||
|
## [1.0.1] - 2019-06-17
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add support for PathConfig #903
|
||||||
|
|
||||||
|
* Add `middleware::identity::RequestIdentity` trait to `get_identity` from `HttpMessage`.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Move cors middleware to `actix-cors` crate.
|
||||||
|
|
||||||
|
* Move identity middleware to `actix-identity` crate.
|
||||||
|
|
||||||
|
* Disable default feature `secure-cookies`.
|
||||||
|
|
||||||
|
* Allow to test an app that uses async actors #897
|
||||||
|
|
||||||
|
* Re-apply patch from #637 #894
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* HttpRequest::url_for is broken with nested scopes #915
|
||||||
|
|
||||||
|
|
||||||
|
## [1.0.0] - 2019-06-05
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
* Add `Scope::configure()` method.
|
* Add `Scope::configure()` method.
|
||||||
|
|
||||||
@ -13,7 +58,7 @@
|
|||||||
|
|
||||||
* Add macros for head, options, trace, connect and patch http methods
|
* Add macros for head, options, trace, connect and patch http methods
|
||||||
|
|
||||||
### Changes
|
### Changed
|
||||||
|
|
||||||
* Drop an unnecessary `Option<_>` indirection around `ServerBuilder` from `HttpServer`. #863
|
* Drop an unnecessary `Option<_>` indirection around `ServerBuilder` from `HttpServer`. #863
|
||||||
|
|
||||||
@ -31,7 +76,7 @@
|
|||||||
* Add `Query<T>::from_query()` to extract parameters from a query string. #846
|
* Add `Query<T>::from_query()` to extract parameters from a query string. #846
|
||||||
* `QueryConfig`, similar to `JsonConfig` for customizing error handling of query extractors.
|
* `QueryConfig`, similar to `JsonConfig` for customizing error handling of query extractors.
|
||||||
|
|
||||||
### Changes
|
### Changed
|
||||||
|
|
||||||
* `JsonConfig` is now `Send + Sync`, this implies that `error_handler` must be `Send + Sync` too.
|
* `JsonConfig` is now `Send + Sync`, this implies that `error_handler` must be `Send + Sync` too.
|
||||||
|
|
||||||
@ -46,7 +91,7 @@
|
|||||||
|
|
||||||
* Allow to set/override app data on scope level
|
* Allow to set/override app data on scope level
|
||||||
|
|
||||||
### Changes
|
### Changed
|
||||||
|
|
||||||
* `App::configure` take an `FnOnce` instead of `Fn`
|
* `App::configure` take an `FnOnce` instead of `Fn`
|
||||||
* Upgrade actix-net crates
|
* Upgrade actix-net crates
|
||||||
|
28
Cargo.toml
28
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web"
|
name = "actix-web"
|
||||||
version = "1.0.0"
|
version = "1.0.2"
|
||||||
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"
|
||||||
@ -31,9 +31,11 @@ members = [
|
|||||||
".",
|
".",
|
||||||
"awc",
|
"awc",
|
||||||
"actix-http",
|
"actix-http",
|
||||||
|
"actix-cors",
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-framed",
|
"actix-framed",
|
||||||
"actix-session",
|
"actix-session",
|
||||||
|
"actix-identity",
|
||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-web-actors",
|
"actix-web-actors",
|
||||||
"actix-web-codegen",
|
"actix-web-codegen",
|
||||||
@ -41,7 +43,7 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["brotli", "flate2-zlib", "secure-cookies", "client", "fail"]
|
default = ["brotli", "flate2-zlib", "client", "fail"]
|
||||||
|
|
||||||
# http client
|
# http client
|
||||||
client = ["awc"]
|
client = ["awc"]
|
||||||
@ -68,22 +70,22 @@ rust-tls = ["rustls", "actix-server/rust-tls"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.1.2"
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.1"
|
||||||
actix-utils = "0.4.1"
|
actix-utils = "0.4.1"
|
||||||
actix-router = "0.1.5"
|
actix-router = "0.1.5"
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "0.2.2"
|
||||||
actix-web-codegen = "0.1.1"
|
actix-web-codegen = "0.1.2"
|
||||||
actix-http = "0.2.2"
|
actix-http = "0.2.4"
|
||||||
actix-server = "0.5.1"
|
actix-server = "0.5.1"
|
||||||
actix-server-config = "0.1.1"
|
actix-server-config = "0.1.1"
|
||||||
actix-threadpool = "0.1.0"
|
actix-threadpool = "0.1.1"
|
||||||
awc = { version = "0.2.0", optional = true }
|
awc = { version = "0.2.1", optional = true }
|
||||||
|
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
derive_more = "0.14"
|
derive_more = "0.15.0"
|
||||||
encoding = "0.2"
|
encoding_rs = "0.8"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
hashbrown = "0.3.0"
|
hashbrown = "0.5.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
net2 = "0.2.33"
|
net2 = "0.2.33"
|
||||||
@ -100,9 +102,9 @@ openssl = { version="0.10", optional = true }
|
|||||||
rustls = { version = "0.15", optional = true }
|
rustls = { version = "0.15", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-http = { version = "0.2.2", features=["ssl", "brotli", "flate2-zlib"] }
|
actix = { version = "0.8.3" }
|
||||||
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
actix-http = { version = "0.2.4", features=["ssl", "brotli", "flate2-zlib"] }
|
||||||
actix-files = { version = "0.1.1" }
|
actix-http-test = { version = "0.2.2", features=["ssl"] }
|
||||||
rand = "0.6"
|
rand = "0.6"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
91
MIGRATION.md
91
MIGRATION.md
@ -1,4 +1,93 @@
|
|||||||
## 1.0
|
## 1.0.1
|
||||||
|
|
||||||
|
* Cors middleware has been moved to `actix-cors` crate
|
||||||
|
|
||||||
|
instead of
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use actix_web::middleware::cors::Cors;
|
||||||
|
```
|
||||||
|
|
||||||
|
use
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use actix_cors::Cors;
|
||||||
|
```
|
||||||
|
|
||||||
|
* Identity middleware has been moved to `actix-identity` crate
|
||||||
|
|
||||||
|
instead of
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use actix_web::middleware::identity::{Identity, CookieIdentityPolicy, IdentityService};
|
||||||
|
```
|
||||||
|
|
||||||
|
use
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use actix_identity::{Identity, CookieIdentityPolicy, IdentityService};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 1.0.0
|
||||||
|
|
||||||
|
* Extractor configuration. In version 1.0 this is handled with the new `Data` mechanism for both setting and retrieving the configuration
|
||||||
|
|
||||||
|
instead of
|
||||||
|
|
||||||
|
```rust
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ExtractorConfig {
|
||||||
|
config: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRequest for YourExtractor {
|
||||||
|
type Config = ExtractorConfig;
|
||||||
|
type Result = Result<YourExtractor, Error>;
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, cfg: &Self::Config) -> Self::Result {
|
||||||
|
println!("use the config: {:?}", cfg.config);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
App::new().resource("/route_with_config", |r| {
|
||||||
|
r.post().with_config(handler_fn, |cfg| {
|
||||||
|
cfg.0.config = "test".to_string();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
use the HttpRequest to get the configuration like any other `Data` with `req.app_data::<C>()` and set it with the `data()` method on the `resource`
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ExtractorConfig {
|
||||||
|
config: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRequest for YourExtractor {
|
||||||
|
type Error = Error;
|
||||||
|
type Future = Result<Self, Self::Error>;
|
||||||
|
type Config = ExtractorConfig;
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
|
let cfg = req.app_data::<ExtractorConfig>();
|
||||||
|
println!("config data?: {:?}", cfg.unwrap().role);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
App::new().service(
|
||||||
|
resource("/route_with_config")
|
||||||
|
.data(ExtractorConfig {
|
||||||
|
config: "test".to_string(),
|
||||||
|
})
|
||||||
|
.route(post().to(handler_fn)),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
* Resource registration. 1.0 version uses generalized resource
|
* Resource registration. 1.0 version uses generalized resource
|
||||||
registration via `.service()` method.
|
registration via `.service()` method.
|
||||||
|
@ -11,7 +11,7 @@ Actix web is a simple, pragmatic and extremely fast web framework for Rust.
|
|||||||
* Multipart streams
|
* Multipart streams
|
||||||
* Static assets
|
* Static assets
|
||||||
* SSL support with OpenSSL or Rustls
|
* SSL support with OpenSSL or Rustls
|
||||||
* Middlewares ([Logger, Session, CORS, CSRF, 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 asynchronous [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)
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ Actix web is a simple, pragmatic and extremely fast web framework for Rust.
|
|||||||
* [API Documentation (0.7)](https://docs.rs/actix-web/0.7.19/actix_web/)
|
* [API Documentation (0.7)](https://docs.rs/actix-web/0.7.19/actix_web/)
|
||||||
* [Chat on gitter](https://gitter.im/actix/actix)
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
* Cargo package: [actix-web](https://crates.io/crates/actix-web)
|
* Cargo package: [actix-web](https://crates.io/crates/actix-web)
|
||||||
* Minimum supported Rust version: 1.32 or later
|
* Minimum supported Rust version: 1.34 or later
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
9
actix-cors/CHANGES.md
Normal file
9
actix-cors/CHANGES.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.1] - unreleased
|
||||||
|
|
||||||
|
* Bump `derive_more` crate version to 0.15.0
|
||||||
|
|
||||||
|
## [0.1.0] - 2019-06-15
|
||||||
|
|
||||||
|
* Move cors middleware to separate crate
|
23
actix-cors/Cargo.toml
Normal file
23
actix-cors/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-cors"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
description = "Cross-origin resource sharing (CORS) for Actix applications."
|
||||||
|
readme = "README.md"
|
||||||
|
keywords = ["web", "framework"]
|
||||||
|
homepage = "https://actix.rs"
|
||||||
|
repository = "https://github.com/actix/actix-web.git"
|
||||||
|
documentation = "https://docs.rs/actix-cors/"
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
edition = "2018"
|
||||||
|
#workspace = ".."
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "actix_cors"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-web = "1.0.0"
|
||||||
|
actix-service = "0.4.0"
|
||||||
|
derive_more = "0.15.0"
|
||||||
|
futures = "0.1.25"
|
1
actix-cors/LICENSE-APACHE
Symbolic link
1
actix-cors/LICENSE-APACHE
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../LICENSE-APACHE
|
1
actix-cors/LICENSE-MIT
Symbolic link
1
actix-cors/LICENSE-MIT
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../LICENSE-MIT
|
9
actix-cors/README.md
Normal file
9
actix-cors/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Identity service for actix web framework [](https://travis-ci.org/actix/actix-web) [](https://codecov.io/gh/actix/actix-web) [](https://crates.io/crates/actix-identity) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
## Documentation & community resources
|
||||||
|
|
||||||
|
* [User Guide](https://actix.rs/docs/)
|
||||||
|
* [API Documentation](https://docs.rs/actix-identity/)
|
||||||
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
|
* Cargo package: [actix-session](https://crates.io/crates/actix-identity)
|
||||||
|
* Minimum supported Rust version: 1.34 or later
|
@ -7,7 +7,7 @@
|
|||||||
//! # Example
|
//! # Example
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use actix_web::middleware::cors::Cors;
|
//! use actix_cors::Cors;
|
||||||
//! use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer};
|
//! use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer};
|
||||||
//!
|
//!
|
||||||
//! fn index(req: HttpRequest) -> &'static str {
|
//! fn index(req: HttpRequest) -> &'static str {
|
||||||
@ -42,17 +42,15 @@ use std::iter::FromIterator;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_service::{IntoTransform, Service, Transform};
|
use actix_service::{IntoTransform, Service, Transform};
|
||||||
|
use actix_web::dev::{RequestHead, ServiceRequest, ServiceResponse};
|
||||||
|
use actix_web::error::{Error, ResponseError, Result};
|
||||||
|
use actix_web::http::header::{self, HeaderName, HeaderValue};
|
||||||
|
use actix_web::http::{self, HttpTryFrom, Method, StatusCode, Uri};
|
||||||
|
use actix_web::HttpResponse;
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use futures::future::{ok, Either, Future, FutureResult};
|
use futures::future::{ok, Either, Future, FutureResult};
|
||||||
use futures::Poll;
|
use futures::Poll;
|
||||||
|
|
||||||
use crate::dev::RequestHead;
|
|
||||||
use crate::error::{Error, ResponseError, Result};
|
|
||||||
use crate::http::header::{self, HeaderName, HeaderValue};
|
|
||||||
use crate::http::{self, HttpTryFrom, Method, StatusCode, Uri};
|
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
|
||||||
use crate::HttpResponse;
|
|
||||||
|
|
||||||
/// A set of errors that can occur during processing CORS
|
/// A set of errors that can occur during processing CORS
|
||||||
#[derive(Debug, Display)]
|
#[derive(Debug, Display)]
|
||||||
pub enum CorsError {
|
pub enum CorsError {
|
||||||
@ -152,11 +150,11 @@ impl<T> AllOrSome<T> {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
/// use actix_cors::Cors;
|
||||||
/// use actix_web::http::header;
|
/// use actix_web::http::header;
|
||||||
/// use actix_web::middleware::cors;
|
|
||||||
///
|
///
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let cors = cors::Cors::new()
|
/// let cors = Cors::new()
|
||||||
/// .allowed_origin("https://www.rust-lang.org/")
|
/// .allowed_origin("https://www.rust-lang.org/")
|
||||||
/// .allowed_methods(vec!["GET", "POST"])
|
/// .allowed_methods(vec!["GET", "POST"])
|
||||||
/// .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
|
/// .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
|
||||||
@ -806,9 +804,9 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_service::{IntoService, Transform};
|
use actix_service::{IntoService, Transform};
|
||||||
|
use actix_web::test::{self, block_on, TestRequest};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::{self, block_on, TestRequest};
|
|
||||||
|
|
||||||
impl Cors {
|
impl Cors {
|
||||||
fn finish<F, S, B>(self, srv: F) -> CorsMiddleware<S>
|
fn finish<F, S, B>(self, srv: F) -> CorsMiddleware<S>
|
@ -1,5 +1,16 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.3] - 2019-06-28
|
||||||
|
|
||||||
|
* Do not set `Content-Length` header, let actix-http set it #930
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.2] - 2019-06-13
|
||||||
|
|
||||||
|
* Content-Length is 0 for NamedFile HEAD request #914
|
||||||
|
|
||||||
|
* Fix ring dependency from actix-web default features for #741
|
||||||
|
|
||||||
## [0.1.1] - 2019-06-01
|
## [0.1.1] - 2019-06-01
|
||||||
|
|
||||||
* Static files are incorrectly served as both chunked and with length #812
|
* Static files are incorrectly served as both chunked and with length #812
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.1.1"
|
version = "0.1.3"
|
||||||
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"
|
||||||
@ -18,13 +18,13 @@ name = "actix_files"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0.0-rc"
|
actix-web = { version = "1.0.2", default-features = false }
|
||||||
actix-http = "0.2.3"
|
actix-http = "0.2.4"
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.1"
|
||||||
bitflags = "1"
|
bitflags = "1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
derive_more = "0.14"
|
derive_more = "0.15.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
mime_guess = "2.0.0-alpha"
|
mime_guess = "2.0.0-alpha"
|
||||||
@ -32,4 +32,4 @@ percent-encoding = "1.0"
|
|||||||
v_htmlescape = "0.4"
|
v_htmlescape = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = { version = "1.0.0-rc", features=["ssl"] }
|
actix-web = { version = "1.0.2", features=["ssl"] }
|
||||||
|
@ -855,6 +855,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_named_file_content_length_headers() {
|
fn test_named_file_content_length_headers() {
|
||||||
|
use actix_web::body::{MessageBody, ResponseBody};
|
||||||
|
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
||||||
);
|
);
|
||||||
@ -866,14 +868,13 @@ mod tests {
|
|||||||
.to_request();
|
.to_request();
|
||||||
let response = test::call_service(&mut srv, request);
|
let response = test::call_service(&mut srv, request);
|
||||||
|
|
||||||
let contentlength = response
|
// let contentlength = response
|
||||||
.headers()
|
// .headers()
|
||||||
.get(header::CONTENT_LENGTH)
|
// .get(header::CONTENT_LENGTH)
|
||||||
.unwrap()
|
// .unwrap()
|
||||||
.to_str()
|
// .to_str()
|
||||||
.unwrap();
|
// .unwrap();
|
||||||
|
// assert_eq!(contentlength, "11");
|
||||||
assert_eq!(contentlength, "11");
|
|
||||||
|
|
||||||
// Invalid range header
|
// Invalid range header
|
||||||
let request = TestRequest::get()
|
let request = TestRequest::get()
|
||||||
@ -890,14 +891,13 @@ mod tests {
|
|||||||
.to_request();
|
.to_request();
|
||||||
let response = test::call_service(&mut srv, request);
|
let response = test::call_service(&mut srv, request);
|
||||||
|
|
||||||
let contentlength = response
|
// let contentlength = response
|
||||||
.headers()
|
// .headers()
|
||||||
.get(header::CONTENT_LENGTH)
|
// .get(header::CONTENT_LENGTH)
|
||||||
.unwrap()
|
// .unwrap()
|
||||||
.to_str()
|
// .to_str()
|
||||||
.unwrap();
|
// .unwrap();
|
||||||
|
// assert_eq!(contentlength, "100");
|
||||||
assert_eq!(contentlength, "100");
|
|
||||||
|
|
||||||
// chunked
|
// chunked
|
||||||
let request = TestRequest::get()
|
let request = TestRequest::get()
|
||||||
@ -926,6 +926,29 @@ mod tests {
|
|||||||
assert_eq!(bytes.freeze(), data);
|
assert_eq!(bytes.freeze(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_head_content_length_headers() {
|
||||||
|
let mut srv = test::init_service(
|
||||||
|
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Valid range header
|
||||||
|
let request = TestRequest::default()
|
||||||
|
.method(Method::HEAD)
|
||||||
|
.uri("/t%65st/tests/test.binary")
|
||||||
|
.to_request();
|
||||||
|
let response = test::call_service(&mut srv, request);
|
||||||
|
|
||||||
|
// TODO: fix check
|
||||||
|
// let contentlength = response
|
||||||
|
// .headers()
|
||||||
|
// .get(header::CONTENT_LENGTH)
|
||||||
|
// .unwrap()
|
||||||
|
// .to_str()
|
||||||
|
// .unwrap();
|
||||||
|
// assert_eq!(contentlength, "100");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_files_with_spaces() {
|
fn test_static_files_with_spaces() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
|
@ -414,28 +414,22 @@ impl Responder for NamedFile {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
resp.header(header::CONTENT_LENGTH, format!("{}", length));
|
|
||||||
|
|
||||||
if precondition_failed {
|
if precondition_failed {
|
||||||
return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish());
|
return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish());
|
||||||
} else if not_modified {
|
} else if not_modified {
|
||||||
return Ok(resp.status(StatusCode::NOT_MODIFIED).finish());
|
return Ok(resp.status(StatusCode::NOT_MODIFIED).finish());
|
||||||
}
|
}
|
||||||
|
|
||||||
if *req.method() == Method::HEAD {
|
let reader = ChunkedReadFile {
|
||||||
Ok(resp.finish())
|
offset,
|
||||||
} else {
|
size: length,
|
||||||
let reader = ChunkedReadFile {
|
file: Some(self.file),
|
||||||
offset,
|
fut: None,
|
||||||
size: length,
|
counter: 0,
|
||||||
file: Some(self.file),
|
};
|
||||||
fut: None,
|
if offset != 0 || length != self.md.len() {
|
||||||
counter: 0,
|
return Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader));
|
||||||
};
|
};
|
||||||
if offset != 0 || length != self.md.len() {
|
Ok(resp.body(SizedStream::new(length, reader)))
|
||||||
return Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader));
|
|
||||||
};
|
|
||||||
Ok(resp.body(SizedStream::new(length, reader)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,18 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.5] - unreleased
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Use `encoding_rs` crate instead of unmaintained `encoding` crate
|
||||||
|
|
||||||
|
## [0.2.4] - 2019-06-16
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Do not compress NoContent (204) responses #918
|
||||||
|
|
||||||
|
|
||||||
## [0.2.3] - 2019-06-02
|
## [0.2.3] - 2019-06-02
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
@ -76,7 +89,7 @@
|
|||||||
|
|
||||||
## [0.1.1] - 2019-04-19
|
## [0.1.1] - 2019-04-19
|
||||||
|
|
||||||
### Changes
|
### Changed
|
||||||
|
|
||||||
* Cookie::max_age() accepts value in seconds
|
* Cookie::max_age() accepts value in seconds
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-http"
|
name = "actix-http"
|
||||||
version = "0.2.3"
|
version = "0.2.4"
|
||||||
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"
|
||||||
@ -56,11 +56,11 @@ bitflags = "1.0"
|
|||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
byteorder = "1.2"
|
byteorder = "1.2"
|
||||||
copyless = "0.1.2"
|
copyless = "0.1.2"
|
||||||
derive_more = "0.14"
|
derive_more = "0.15.0"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
encoding = "0.2"
|
encoding_rs = "0.8"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
hashbrown = "0.3.0"
|
hashbrown = "0.5.0"
|
||||||
h2 = "0.1.16"
|
h2 = "0.1.16"
|
||||||
http = "0.1.17"
|
http = "0.1.17"
|
||||||
httparse = "1.3"
|
httparse = "1.3"
|
||||||
|
@ -33,6 +33,7 @@ impl<B: MessageBody> Encoder<B> {
|
|||||||
) -> ResponseBody<Encoder<B>> {
|
) -> ResponseBody<Encoder<B>> {
|
||||||
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|
||||||
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
||||||
|
|| head.status == StatusCode::NO_CONTENT
|
||||||
|| encoding == ContentEncoding::Identity
|
|| encoding == ContentEncoding::Identity
|
||||||
|| encoding == ContentEncoding::Auto);
|
|| encoding == ContentEncoding::Auto);
|
||||||
|
|
||||||
|
@ -693,11 +693,12 @@ where
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// read socket into a buf
|
// read socket into a buf
|
||||||
let should_disconnect = if !inner.flags.contains(Flags::READ_DISCONNECT) {
|
let should_disconnect =
|
||||||
read_available(&mut inner.io, &mut inner.read_buf)?
|
if !inner.flags.contains(Flags::READ_DISCONNECT) {
|
||||||
} else {
|
read_available(&mut inner.io, &mut inner.read_buf)?
|
||||||
None
|
} else {
|
||||||
};
|
None
|
||||||
|
};
|
||||||
|
|
||||||
inner.poll_request()?;
|
inner.poll_request()?;
|
||||||
if let Some(true) = should_disconnect {
|
if let Some(true) = should_disconnect {
|
||||||
|
@ -61,6 +61,7 @@ impl Response {
|
|||||||
STATIC_RESP!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
|
STATIC_RESP!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
|
||||||
STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
|
STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
|
||||||
STATIC_RESP!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
|
STATIC_RESP!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
|
||||||
|
STATIC_RESP!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS);
|
||||||
|
|
||||||
STATIC_RESP!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
|
STATIC_RESP!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
STATIC_RESP!(NotImplemented, StatusCode::NOT_IMPLEMENTED);
|
STATIC_RESP!(NotImplemented, StatusCode::NOT_IMPLEMENTED);
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
use std::cell::{Ref, RefMut};
|
use std::cell::{Ref, RefMut};
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use encoding::label::encoding_from_whatwg_label;
|
|
||||||
use encoding::EncodingRef;
|
|
||||||
use http::header;
|
use http::header;
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
|
||||||
@ -59,10 +57,12 @@ pub trait HttpMessage: Sized {
|
|||||||
/// Get content type encoding
|
/// Get content type encoding
|
||||||
///
|
///
|
||||||
/// UTF-8 is used by default, If request charset is not set.
|
/// UTF-8 is used by default, If request charset is not set.
|
||||||
fn encoding(&self) -> Result<EncodingRef, ContentTypeError> {
|
fn encoding(&self) -> Result<&'static Encoding, ContentTypeError> {
|
||||||
if let Some(mime_type) = self.mime_type()? {
|
if let Some(mime_type) = self.mime_type()? {
|
||||||
if let Some(charset) = mime_type.get_param("charset") {
|
if let Some(charset) = mime_type.get_param("charset") {
|
||||||
if let Some(enc) = encoding_from_whatwg_label(charset.as_str()) {
|
if let Some(enc) =
|
||||||
|
Encoding::for_label_no_replacement(charset.as_str().as_bytes())
|
||||||
|
{
|
||||||
Ok(enc)
|
Ok(enc)
|
||||||
} else {
|
} else {
|
||||||
Err(ContentTypeError::UnknownEncoding)
|
Err(ContentTypeError::UnknownEncoding)
|
||||||
@ -166,8 +166,7 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use encoding::all::ISO_8859_2;
|
use encoding_rs::ISO_8859_2;
|
||||||
use encoding::Encoding;
|
|
||||||
use mime;
|
use mime;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -223,7 +222,7 @@ mod tests {
|
|||||||
"application/json; charset=ISO-8859-2",
|
"application/json; charset=ISO-8859-2",
|
||||||
)
|
)
|
||||||
.finish();
|
.finish();
|
||||||
assert_eq!(ISO_8859_2.name(), req.encoding().unwrap().name());
|
assert_eq!(ISO_8859_2, req.encoding().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -37,7 +37,7 @@ pub enum Frame {
|
|||||||
Close(Option<CloseReason>),
|
Close(Option<CloseReason>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
/// WebSockets protocol codec
|
/// WebSockets protocol codec
|
||||||
pub struct Codec {
|
pub struct Codec {
|
||||||
max_size: usize,
|
max_size: usize,
|
||||||
|
5
actix-identity/CHANGES.md
Normal file
5
actix-identity/CHANGES.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.0] - 2019-06-xx
|
||||||
|
|
||||||
|
* Move identity middleware to separate crate
|
30
actix-identity/Cargo.toml
Normal file
30
actix-identity/Cargo.toml
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-identity"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
description = "Identity service for actix web framework."
|
||||||
|
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-identity/"
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "actix_identity"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-web = { version = "1.0.0", default-features = false, features = ["secure-cookies"] }
|
||||||
|
actix-service = "0.4.0"
|
||||||
|
futures = "0.1.25"
|
||||||
|
serde = "1.0"
|
||||||
|
serde_json = "1.0"
|
||||||
|
time = "0.1.42"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
actix-rt = "0.2.2"
|
||||||
|
actix-http = "0.2.3"
|
||||||
|
bytes = "0.4"
|
1
actix-identity/LICENSE-APACHE
Symbolic link
1
actix-identity/LICENSE-APACHE
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../LICENSE-APACHE
|
1
actix-identity/LICENSE-MIT
Symbolic link
1
actix-identity/LICENSE-MIT
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../LICENSE-MIT
|
9
actix-identity/README.md
Normal file
9
actix-identity/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Identity service for actix web framework [](https://travis-ci.org/actix/actix-web) [](https://codecov.io/gh/actix/actix-web) [](https://crates.io/crates/actix-identity) [](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
|
## Documentation & community resources
|
||||||
|
|
||||||
|
* [User Guide](https://actix.rs/docs/)
|
||||||
|
* [API Documentation](https://docs.rs/actix-identity/)
|
||||||
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
|
* Cargo package: [actix-session](https://crates.io/crates/actix-identity)
|
||||||
|
* Minimum supported Rust version: 1.34 or later
|
@ -10,12 +10,11 @@
|
|||||||
//! uses cookies as identity storage.
|
//! uses cookies as identity storage.
|
||||||
//!
|
//!
|
||||||
//! To access current request identity
|
//! To access current request identity
|
||||||
//! [**Identity**](trait.Identity.html) extractor should be used.
|
//! [**Identity**](struct.Identity.html) extractor should be used.
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use actix_web::middleware::identity::Identity;
|
|
||||||
//! use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
|
||||||
//! use actix_web::*;
|
//! use actix_web::*;
|
||||||
|
//! use actix_identity::{Identity, CookieIdentityPolicy, IdentityService};
|
||||||
//!
|
//!
|
||||||
//! fn index(id: Identity) -> String {
|
//! fn index(id: Identity) -> String {
|
||||||
//! // access request identity
|
//! // access request identity
|
||||||
@ -39,7 +38,7 @@
|
|||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! let app = App::new().wrap(IdentityService::new(
|
//! let app = App::new().wrap(IdentityService::new(
|
||||||
//! // <- create identity middleware
|
//! // <- create identity middleware
|
||||||
//! CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
|
//! CookieIdentityPolicy::new(&[0; 32]) // <- create cookie identity policy
|
||||||
//! .name("auth-cookie")
|
//! .name("auth-cookie")
|
||||||
//! .secure(false)))
|
//! .secure(false)))
|
||||||
//! .service(web::resource("/index.html").to(index))
|
//! .service(web::resource("/index.html").to(index))
|
||||||
@ -57,17 +56,17 @@ use futures::{Future, IntoFuture, Poll};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use time::Duration;
|
use time::Duration;
|
||||||
|
|
||||||
use crate::cookie::{Cookie, CookieJar, Key, SameSite};
|
use actix_web::cookie::{Cookie, CookieJar, Key, SameSite};
|
||||||
use crate::error::{Error, Result};
|
use actix_web::dev::{Extensions, Payload, ServiceRequest, ServiceResponse};
|
||||||
use crate::http::header::{self, HeaderValue};
|
use actix_web::error::{Error, Result};
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
use actix_web::http::header::{self, HeaderValue};
|
||||||
use crate::{dev::Payload, FromRequest, HttpMessage, HttpRequest};
|
use actix_web::{FromRequest, HttpMessage, HttpRequest};
|
||||||
|
|
||||||
/// The extractor type to obtain your identity from a request.
|
/// The extractor type to obtain your identity from a request.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_web::*;
|
/// use actix_web::*;
|
||||||
/// use actix_web::middleware::identity::Identity;
|
/// use actix_identity::Identity;
|
||||||
///
|
///
|
||||||
/// fn index(id: Identity) -> Result<String> {
|
/// fn index(id: Identity) -> Result<String> {
|
||||||
/// // access request identity
|
/// // access request identity
|
||||||
@ -96,11 +95,7 @@ impl Identity {
|
|||||||
/// Return the claimed identity of the user associated request or
|
/// Return the claimed identity of the user associated request or
|
||||||
/// ``None`` if no identity can be found associated with the request.
|
/// ``None`` if no identity can be found associated with the request.
|
||||||
pub fn identity(&self) -> Option<String> {
|
pub fn identity(&self) -> Option<String> {
|
||||||
if let Some(id) = self.0.extensions().get::<IdentityItem>() {
|
Identity::get_identity(&self.0.extensions())
|
||||||
id.id.clone()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remember identity.
|
/// Remember identity.
|
||||||
@ -119,6 +114,14 @@ impl Identity {
|
|||||||
id.changed = true;
|
id.changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_identity(extensions: &Extensions) -> Option<String> {
|
||||||
|
if let Some(id) = extensions.get::<IdentityItem>() {
|
||||||
|
id.id.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IdentityItem {
|
struct IdentityItem {
|
||||||
@ -126,11 +129,28 @@ struct IdentityItem {
|
|||||||
changed: bool,
|
changed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper trait that allows to get Identity.
|
||||||
|
///
|
||||||
|
/// It could be used in middleware but identity policy must be set before any other middleware that needs identity
|
||||||
|
/// RequestIdentity is implemented both for `ServiceRequest` and `HttpRequest`.
|
||||||
|
pub trait RequestIdentity {
|
||||||
|
fn get_identity(&self) -> Option<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> RequestIdentity for T
|
||||||
|
where
|
||||||
|
T: HttpMessage,
|
||||||
|
{
|
||||||
|
fn get_identity(&self) -> Option<String> {
|
||||||
|
Identity::get_identity(&self.extensions())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Extractor implementation for Identity type.
|
/// Extractor implementation for Identity type.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use actix_web::*;
|
/// # use actix_web::*;
|
||||||
/// use actix_web::middleware::identity::Identity;
|
/// use actix_identity::Identity;
|
||||||
///
|
///
|
||||||
/// fn index(id: Identity) -> String {
|
/// fn index(id: Identity) -> String {
|
||||||
/// // access request identity
|
/// // access request identity
|
||||||
@ -177,7 +197,7 @@ pub trait IdentityPolicy: Sized + 'static {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_web::App;
|
/// use actix_web::App;
|
||||||
/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
/// use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new().wrap(IdentityService::new(
|
/// let app = App::new().wrap(IdentityService::new(
|
||||||
@ -442,9 +462,8 @@ impl CookieIdentityInner {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
|
||||||
/// use actix_web::App;
|
/// use actix_web::App;
|
||||||
|
/// use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new().wrap(IdentityService::new(
|
/// let app = App::new().wrap(IdentityService::new(
|
||||||
@ -590,13 +609,13 @@ impl IdentityPolicy for CookieIdentityPolicy {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
use crate::http::StatusCode;
|
|
||||||
use crate::test::{self, TestRequest};
|
|
||||||
use crate::{web, App, HttpResponse};
|
|
||||||
|
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use actix_web::http::StatusCode;
|
||||||
|
use actix_web::test::{self, TestRequest};
|
||||||
|
use actix_web::{web, App, Error, HttpResponse};
|
||||||
|
|
||||||
const COOKIE_KEY_MASTER: [u8; 32] = [0; 32];
|
const COOKIE_KEY_MASTER: [u8; 32] = [0; 32];
|
||||||
const COOKIE_NAME: &'static str = "actix_auth";
|
const COOKIE_NAME: &'static str = "actix_auth";
|
||||||
const COOKIE_LOGIN: &'static str = "test";
|
const COOKIE_LOGIN: &'static str = "test";
|
||||||
@ -717,8 +736,8 @@ mod tests {
|
|||||||
f: F,
|
f: F,
|
||||||
) -> impl actix_service::Service<
|
) -> impl actix_service::Service<
|
||||||
Request = actix_http::Request,
|
Request = actix_http::Request,
|
||||||
Response = ServiceResponse<actix_http::body::Body>,
|
Response = ServiceResponse<actix_web::body::Body>,
|
||||||
Error = actix_http::Error,
|
Error = Error,
|
||||||
> {
|
> {
|
||||||
test::init_service(
|
test::init_service(
|
||||||
App::new()
|
App::new()
|
@ -1,5 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.3] - 2019-06-06
|
||||||
|
|
||||||
|
* Fix ring dependency from actix-web default features for #741.
|
||||||
|
|
||||||
## [0.1.2] - 2019-06-02
|
## [0.1.2] - 2019-06-02
|
||||||
|
|
||||||
* Fix boundary parsing #876
|
* Fix boundary parsing #876
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-multipart"
|
name = "actix-multipart"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
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"
|
||||||
@ -18,10 +18,10 @@ name = "actix_multipart"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0.0-rc"
|
actix-web = { version = "1.0.0", default-features = false }
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
derive_more = "0.14"
|
derive_more = "0.15.0"
|
||||||
httparse = "1.3"
|
httparse = "1.3"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
@ -31,4 +31,4 @@ twoway = "0.2"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "0.2.2"
|
||||||
actix-http = "0.2.2"
|
actix-http = "0.2.4"
|
@ -24,12 +24,12 @@ default = ["cookie-session"]
|
|||||||
cookie-session = ["actix-web/secure-cookies"]
|
cookie-session = ["actix-web/secure-cookies"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0.0-rc"
|
actix-web = "1.0.0"
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
derive_more = "0.14"
|
derive_more = "0.15.0"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
hashbrown = "0.3.0"
|
hashbrown = "0.5.0"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
time = "0.1.42"
|
time = "0.1.42"
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [1.0.1] - 2019-06-28
|
||||||
|
|
||||||
|
* Allow to use custom ws codec with `WebsocketContext` #925
|
||||||
|
|
||||||
## [1.0.0] - 2019-05-29
|
## [1.0.0] - 2019-05-29
|
||||||
|
|
||||||
* Update actix-http and actix-web
|
* Update actix-http and actix-web
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web-actors"
|
name = "actix-web-actors"
|
||||||
version = "1.0.0"
|
version = "1.0.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"
|
||||||
@ -19,8 +19,8 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.8.3"
|
actix = "0.8.3"
|
||||||
actix-web = "1.0.0-rc"
|
actix-web = "1.0.2"
|
||||||
actix-http = "0.2.2"
|
actix-http = "0.2.4"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.1.2"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
|
@ -177,9 +177,30 @@ where
|
|||||||
inner: ContextParts::new(mb.sender_producer()),
|
inner: ContextParts::new(mb.sender_producer()),
|
||||||
messages: VecDeque::new(),
|
messages: VecDeque::new(),
|
||||||
};
|
};
|
||||||
ctx.add_stream(WsStream::new(stream));
|
ctx.add_stream(WsStream::new(stream, Codec::new()));
|
||||||
|
|
||||||
WebsocketContextFut::new(ctx, actor, mb)
|
WebsocketContextFut::new(ctx, actor, mb, Codec::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Create a new Websocket context from a request, an actor, and a codec
|
||||||
|
pub fn with_codec<S>(
|
||||||
|
actor: A,
|
||||||
|
stream: S,
|
||||||
|
codec: Codec,
|
||||||
|
) -> impl Stream<Item = Bytes, Error = Error>
|
||||||
|
where
|
||||||
|
A: StreamHandler<Message, ProtocolError>,
|
||||||
|
S: Stream<Item = Bytes, Error = PayloadError> + 'static,
|
||||||
|
{
|
||||||
|
let mb = Mailbox::default();
|
||||||
|
let mut ctx = WebsocketContext {
|
||||||
|
inner: ContextParts::new(mb.sender_producer()),
|
||||||
|
messages: VecDeque::new(),
|
||||||
|
};
|
||||||
|
ctx.add_stream(WsStream::new(stream, codec));
|
||||||
|
|
||||||
|
WebsocketContextFut::new(ctx, actor, mb, codec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new Websocket context
|
/// Create a new Websocket context
|
||||||
@ -197,11 +218,11 @@ where
|
|||||||
inner: ContextParts::new(mb.sender_producer()),
|
inner: ContextParts::new(mb.sender_producer()),
|
||||||
messages: VecDeque::new(),
|
messages: VecDeque::new(),
|
||||||
};
|
};
|
||||||
ctx.add_stream(WsStream::new(stream));
|
ctx.add_stream(WsStream::new(stream, Codec::new()));
|
||||||
|
|
||||||
let act = f(&mut ctx);
|
let act = f(&mut ctx);
|
||||||
|
|
||||||
WebsocketContextFut::new(ctx, act, mb)
|
WebsocketContextFut::new(ctx, act, mb, Codec::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,11 +309,11 @@ impl<A> WebsocketContextFut<A>
|
|||||||
where
|
where
|
||||||
A: Actor<Context = WebsocketContext<A>>,
|
A: Actor<Context = WebsocketContext<A>>,
|
||||||
{
|
{
|
||||||
fn new(ctx: WebsocketContext<A>, act: A, mailbox: Mailbox<A>) -> Self {
|
fn new(ctx: WebsocketContext<A>, act: A, mailbox: Mailbox<A>, codec: Codec) -> Self {
|
||||||
let fut = ContextFut::new(ctx, act, mailbox);
|
let fut = ContextFut::new(ctx, act, mailbox);
|
||||||
WebsocketContextFut {
|
WebsocketContextFut {
|
||||||
fut,
|
fut,
|
||||||
encoder: Codec::new(),
|
encoder: codec,
|
||||||
buf: BytesMut::new(),
|
buf: BytesMut::new(),
|
||||||
closed: false,
|
closed: false,
|
||||||
}
|
}
|
||||||
@ -353,10 +374,10 @@ impl<S> WsStream<S>
|
|||||||
where
|
where
|
||||||
S: Stream<Item = Bytes, Error = PayloadError>,
|
S: Stream<Item = Bytes, Error = PayloadError>,
|
||||||
{
|
{
|
||||||
fn new(stream: S) -> Self {
|
fn new(stream: S, codec: Codec) -> Self {
|
||||||
Self {
|
Self {
|
||||||
stream,
|
stream,
|
||||||
decoder: Codec::new(),
|
decoder: codec,
|
||||||
buf: BytesMut::new(),
|
buf: BytesMut::new(),
|
||||||
closed: false,
|
closed: false,
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ quote = "0.6.12"
|
|||||||
syn = { version = "0.15.34", features = ["full", "parsing", "extra-traits"] }
|
syn = { version = "0.15.34", features = ["full", "parsing", "extra-traits"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = { version = "1.0.0-rc" }
|
actix-web = { version = "1.0.0" }
|
||||||
actix-http = { version = "0.2.2", features=["ssl"] }
|
actix-http = { version = "0.2.4", features=["ssl"] }
|
||||||
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
||||||
futures = { version = "0.1" }
|
futures = { version = "0.1" }
|
||||||
|
@ -29,7 +29,7 @@ pub enum GuardType {
|
|||||||
Connect,
|
Connect,
|
||||||
Options,
|
Options,
|
||||||
Trace,
|
Trace,
|
||||||
Patch
|
Patch,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for GuardType {
|
impl fmt::Display for GuardType {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use actix_http::HttpService;
|
use actix_http::HttpService;
|
||||||
use actix_http_test::TestServer;
|
use actix_http_test::TestServer;
|
||||||
use actix_web::{http, web::Path, App, HttpResponse, Responder};
|
use actix_web::{http, web::Path, App, HttpResponse, Responder};
|
||||||
use actix_web_codegen::{delete, get, post, put, patch, head, connect, options, trace};
|
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, trace};
|
||||||
use futures::{future, Future};
|
use futures::{future, Future};
|
||||||
|
|
||||||
#[get("/test")]
|
#[get("/test")]
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.1] - 2019-06-05
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add license files
|
||||||
|
|
||||||
## [0.2.0] - 2019-05-12
|
## [0.2.0] - 2019-05-12
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "awc"
|
name = "awc"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix http client."
|
description = "Actix http client."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -40,11 +40,11 @@ flate2-rust = ["actix-http/flate2-rust"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.1.2"
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.1"
|
||||||
actix-http = "0.2.0"
|
actix-http = "0.2.4"
|
||||||
base64 = "0.10.1"
|
base64 = "0.10.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
derive_more = "0.14"
|
derive_more = "0.15.0"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
log =" 0.4"
|
log =" 0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
@ -58,11 +58,11 @@ openssl = { version="0.10", optional = true }
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "0.2.2"
|
||||||
actix-web = { version = "1.0.0-beta.4", features=["ssl"] }
|
actix-web = { version = "1.0.0", features=["ssl"] }
|
||||||
actix-http = { version = "0.2.0", features=["ssl"] }
|
actix-http = { version = "0.2.4", features=["ssl"] }
|
||||||
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
actix-http-test = { version = "0.2.0", features=["ssl"] }
|
||||||
actix-utils = "0.4.0"
|
actix-utils = "0.4.1"
|
||||||
actix-server = { version = "0.5.0", features=["ssl"] }
|
actix-server = { version = "0.5.1", features=["ssl"] }
|
||||||
brotli2 = { version="0.3.2" }
|
brotli2 = { version="0.3.2" }
|
||||||
flate2 = { version="1.0.2" }
|
flate2 = { version="1.0.2" }
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
|
@ -38,6 +38,7 @@ pub mod test;
|
|||||||
pub mod ws;
|
pub mod ws;
|
||||||
|
|
||||||
pub use self::builder::ClientBuilder;
|
pub use self::builder::ClientBuilder;
|
||||||
|
pub use self::connect::BoxedSocket;
|
||||||
pub use self::request::ClientRequest;
|
pub use self::request::ClientRequest;
|
||||||
pub use self::response::{ClientResponse, JsonBody, MessageBody};
|
pub use self::response::{ClientResponse, JsonBody, MessageBody};
|
||||||
|
|
||||||
|
76
src/app.rs
76
src/app.rs
@ -8,7 +8,7 @@ use actix_service::boxed::{self, BoxedNewService};
|
|||||||
use actix_service::{
|
use actix_service::{
|
||||||
apply_transform, IntoNewService, IntoTransform, NewService, Transform,
|
apply_transform, IntoNewService, IntoTransform, NewService, Transform,
|
||||||
};
|
};
|
||||||
use futures::IntoFuture;
|
use futures::{Future, IntoFuture};
|
||||||
|
|
||||||
use crate::app_service::{AppEntry, AppInit, AppRoutingFactory};
|
use crate::app_service::{AppEntry, AppInit, AppRoutingFactory};
|
||||||
use crate::config::{AppConfig, AppConfigInner, ServiceConfig};
|
use crate::config::{AppConfig, AppConfigInner, ServiceConfig};
|
||||||
@ -23,6 +23,7 @@ use crate::service::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
type HttpNewService = BoxedNewService<(), ServiceRequest, ServiceResponse, Error, ()>;
|
type HttpNewService = BoxedNewService<(), ServiceRequest, ServiceResponse, Error, ()>;
|
||||||
|
type FnDataFactory = Box<Fn() -> Box<dyn Future<Item = Box<DataFactory>, Error = ()>>>;
|
||||||
|
|
||||||
/// Application builder - structure that follows the builder pattern
|
/// Application builder - structure that follows the builder pattern
|
||||||
/// for building application instances.
|
/// for building application instances.
|
||||||
@ -32,6 +33,7 @@ pub struct App<T, B> {
|
|||||||
default: Option<Rc<HttpNewService>>,
|
default: Option<Rc<HttpNewService>>,
|
||||||
factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
||||||
data: Vec<Box<DataFactory>>,
|
data: Vec<Box<DataFactory>>,
|
||||||
|
data_factories: Vec<FnDataFactory>,
|
||||||
config: AppConfigInner,
|
config: AppConfigInner,
|
||||||
external: Vec<ResourceDef>,
|
external: Vec<ResourceDef>,
|
||||||
_t: PhantomData<(B)>,
|
_t: PhantomData<(B)>,
|
||||||
@ -44,6 +46,7 @@ impl App<AppEntry, Body> {
|
|||||||
App {
|
App {
|
||||||
endpoint: AppEntry::new(fref.clone()),
|
endpoint: AppEntry::new(fref.clone()),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
|
data_factories: Vec::new(),
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
default: None,
|
default: None,
|
||||||
factory_ref: fref,
|
factory_ref: fref,
|
||||||
@ -100,6 +103,38 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set application data factory. This function is
|
||||||
|
/// similar to `.data()` but it accepts data factory. Data object get
|
||||||
|
/// constructed asynchronously during application initialization.
|
||||||
|
pub fn data_factory<F, Out>(mut self, data: F) -> Self
|
||||||
|
where
|
||||||
|
F: Fn() -> Out + 'static,
|
||||||
|
Out: IntoFuture + 'static,
|
||||||
|
Out::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
self.data_factories.push(Box::new(move || {
|
||||||
|
Box::new(
|
||||||
|
data()
|
||||||
|
.into_future()
|
||||||
|
.map_err(|e| {
|
||||||
|
log::error!("Can not construct data instance: {:?}", e);
|
||||||
|
})
|
||||||
|
.map(|data| {
|
||||||
|
let data: Box<dyn DataFactory> = Box::new(Data::new(data));
|
||||||
|
data
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set application data. Application data could be accessed
|
||||||
|
/// by using `Data<T>` extractor where `T` is data type.
|
||||||
|
pub fn register_data<U: 'static>(mut self, data: Data<U>) -> Self {
|
||||||
|
self.data.push(Box::new(data));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Run external configuration as part of the application building
|
/// Run external configuration as part of the application building
|
||||||
/// process
|
/// process
|
||||||
///
|
///
|
||||||
@ -218,7 +253,6 @@ where
|
|||||||
/// It is also possible to use static files as default service.
|
/// It is also possible to use static files as default service.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_files::Files;
|
|
||||||
/// use actix_web::{web, App, HttpResponse};
|
/// use actix_web::{web, App, HttpResponse};
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
@ -226,7 +260,7 @@ where
|
|||||||
/// .service(
|
/// .service(
|
||||||
/// web::resource("/index.html").to(|| HttpResponse::Ok()))
|
/// web::resource("/index.html").to(|| HttpResponse::Ok()))
|
||||||
/// .default_service(
|
/// .default_service(
|
||||||
/// Files::new("", "./static")
|
/// web::to(|| HttpResponse::NotFound())
|
||||||
/// );
|
/// );
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -343,6 +377,7 @@ where
|
|||||||
App {
|
App {
|
||||||
endpoint,
|
endpoint,
|
||||||
data: self.data,
|
data: self.data,
|
||||||
|
data_factories: self.data_factories,
|
||||||
services: self.services,
|
services: self.services,
|
||||||
default: self.default,
|
default: self.default,
|
||||||
factory_ref: self.factory_ref,
|
factory_ref: self.factory_ref,
|
||||||
@ -417,6 +452,7 @@ where
|
|||||||
fn into_new_service(self) -> AppInit<T, B> {
|
fn into_new_service(self) -> AppInit<T, B> {
|
||||||
AppInit {
|
AppInit {
|
||||||
data: Rc::new(self.data),
|
data: Rc::new(self.data),
|
||||||
|
data_factories: Rc::new(self.data_factories),
|
||||||
endpoint: self.endpoint,
|
endpoint: self.endpoint,
|
||||||
services: Rc::new(RefCell::new(self.services)),
|
services: Rc::new(RefCell::new(self.services)),
|
||||||
external: RefCell::new(self.external),
|
external: RefCell::new(self.external),
|
||||||
@ -484,24 +520,24 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn test_data_factory() {
|
fn test_data_factory() {
|
||||||
// let mut srv =
|
let mut srv =
|
||||||
// init_service(App::new().data_factory(|| Ok::<_, ()>(10usize)).service(
|
init_service(App::new().data_factory(|| Ok::<_, ()>(10usize)).service(
|
||||||
// web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
// ));
|
));
|
||||||
// let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
// let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
// assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
// let mut srv =
|
let mut srv =
|
||||||
// init_service(App::new().data_factory(|| Ok::<_, ()>(10u32)).service(
|
init_service(App::new().data_factory(|| Ok::<_, ()>(10u32)).service(
|
||||||
// web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
// ));
|
));
|
||||||
// let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
// let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
// assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
// }
|
}
|
||||||
|
|
||||||
fn md<S, B>(
|
fn md<S, B>(
|
||||||
req: ServiceRequest,
|
req: ServiceRequest,
|
||||||
|
@ -25,6 +25,7 @@ type BoxedResponse = Either<
|
|||||||
FutureResult<ServiceResponse, Error>,
|
FutureResult<ServiceResponse, Error>,
|
||||||
Box<Future<Item = ServiceResponse, Error = Error>>,
|
Box<Future<Item = ServiceResponse, Error = Error>>,
|
||||||
>;
|
>;
|
||||||
|
type FnDataFactory = Box<Fn() -> Box<dyn Future<Item = Box<DataFactory>, Error = ()>>>;
|
||||||
|
|
||||||
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
||||||
/// It also executes data factories.
|
/// It also executes data factories.
|
||||||
@ -40,6 +41,7 @@ where
|
|||||||
{
|
{
|
||||||
pub(crate) endpoint: T,
|
pub(crate) endpoint: T,
|
||||||
pub(crate) data: Rc<Vec<Box<DataFactory>>>,
|
pub(crate) data: Rc<Vec<Box<DataFactory>>>,
|
||||||
|
pub(crate) data_factories: Rc<Vec<FnDataFactory>>,
|
||||||
pub(crate) config: RefCell<AppConfig>,
|
pub(crate) config: RefCell<AppConfig>,
|
||||||
pub(crate) services: Rc<RefCell<Vec<Box<ServiceFactory>>>>,
|
pub(crate) services: Rc<RefCell<Vec<Box<ServiceFactory>>>>,
|
||||||
pub(crate) default: Option<Rc<HttpNewService>>,
|
pub(crate) default: Option<Rc<HttpNewService>>,
|
||||||
@ -119,16 +121,12 @@ where
|
|||||||
let rmap = Rc::new(rmap);
|
let rmap = Rc::new(rmap);
|
||||||
rmap.finish(rmap.clone());
|
rmap.finish(rmap.clone());
|
||||||
|
|
||||||
// create app data container
|
|
||||||
let mut data = Extensions::new();
|
|
||||||
for f in self.data.iter() {
|
|
||||||
f.create(&mut data);
|
|
||||||
}
|
|
||||||
|
|
||||||
AppInitResult {
|
AppInitResult {
|
||||||
endpoint: None,
|
endpoint: None,
|
||||||
endpoint_fut: self.endpoint.new_service(&()),
|
endpoint_fut: self.endpoint.new_service(&()),
|
||||||
data: Rc::new(data),
|
data: self.data.clone(),
|
||||||
|
data_factories: Vec::new(),
|
||||||
|
data_factories_fut: self.data_factories.iter().map(|f| f()).collect(),
|
||||||
config,
|
config,
|
||||||
rmap,
|
rmap,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
@ -144,7 +142,9 @@ where
|
|||||||
endpoint_fut: T::Future,
|
endpoint_fut: T::Future,
|
||||||
rmap: Rc<ResourceMap>,
|
rmap: Rc<ResourceMap>,
|
||||||
config: AppConfig,
|
config: AppConfig,
|
||||||
data: Rc<Extensions>,
|
data: Rc<Vec<Box<DataFactory>>>,
|
||||||
|
data_factories: Vec<Box<DataFactory>>,
|
||||||
|
data_factories_fut: Vec<Box<dyn Future<Item = Box<DataFactory>, Error = ()>>>,
|
||||||
_t: PhantomData<B>,
|
_t: PhantomData<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,21 +159,43 @@ where
|
|||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
type Item = AppInitService<T::Service, B>;
|
type Item = AppInitService<T::Service, B>;
|
||||||
type Error = T::InitError;
|
type Error = ();
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
// async data factories
|
||||||
|
let mut idx = 0;
|
||||||
|
while idx < self.data_factories_fut.len() {
|
||||||
|
match self.data_factories_fut[idx].poll()? {
|
||||||
|
Async::Ready(f) => {
|
||||||
|
self.data_factories.push(f);
|
||||||
|
self.data_factories_fut.remove(idx);
|
||||||
|
}
|
||||||
|
Async::NotReady => idx += 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if self.endpoint.is_none() {
|
if self.endpoint.is_none() {
|
||||||
if let Async::Ready(srv) = self.endpoint_fut.poll()? {
|
if let Async::Ready(srv) = self.endpoint_fut.poll()? {
|
||||||
self.endpoint = Some(srv);
|
self.endpoint = Some(srv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.endpoint.is_some() {
|
if self.endpoint.is_some() && self.data_factories_fut.is_empty() {
|
||||||
|
// create app data container
|
||||||
|
let mut data = Extensions::new();
|
||||||
|
for f in self.data.iter() {
|
||||||
|
f.create(&mut data);
|
||||||
|
}
|
||||||
|
|
||||||
|
for f in &self.data_factories {
|
||||||
|
f.create(&mut data);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Async::Ready(AppInitService {
|
Ok(Async::Ready(AppInitService {
|
||||||
service: self.endpoint.take().unwrap(),
|
service: self.endpoint.take().unwrap(),
|
||||||
rmap: self.rmap.clone(),
|
rmap: self.rmap.clone(),
|
||||||
config: self.config.clone(),
|
config: self.config.clone(),
|
||||||
data: self.data.clone(),
|
data: Rc::new(data),
|
||||||
pool: HttpRequestPool::create(),
|
pool: HttpRequestPool::create(),
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
|
25
src/data.rs
25
src/data.rs
@ -54,7 +54,7 @@ pub(crate) trait DataFactory {
|
|||||||
///
|
///
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// // Store `MyData` in application storage.
|
/// // Store `MyData` in application storage.
|
||||||
/// .data(data.clone())
|
/// .register_data(data.clone())
|
||||||
/// .service(
|
/// .service(
|
||||||
/// web::resource("/index.html").route(
|
/// web::resource("/index.html").route(
|
||||||
/// web::get().to(index)));
|
/// web::get().to(index)));
|
||||||
@ -73,7 +73,7 @@ impl<T> Data<T> {
|
|||||||
Data(Arc::new(state))
|
Data(Arc::new(state))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get referecnce to inner app data.
|
/// Get reference to inner app data.
|
||||||
pub fn get_ref(&self) -> &T {
|
pub fn get_ref(&self) -> &T {
|
||||||
self.0.as_ref()
|
self.0.as_ref()
|
||||||
}
|
}
|
||||||
@ -130,6 +130,7 @@ impl<T: 'static> DataFactory for Data<T> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
use crate::http::StatusCode;
|
use crate::http::StatusCode;
|
||||||
use crate::test::{block_on, init_service, TestRequest};
|
use crate::test::{block_on, init_service, TestRequest};
|
||||||
use crate::{web, App, HttpResponse};
|
use crate::{web, App, HttpResponse};
|
||||||
@ -154,6 +155,26 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_register_data_extractor() {
|
||||||
|
let mut srv =
|
||||||
|
init_service(App::new().register_data(Data::new(10usize)).service(
|
||||||
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
|
));
|
||||||
|
|
||||||
|
let req = TestRequest::default().to_request();
|
||||||
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let mut srv =
|
||||||
|
init_service(App::new().register_data(Data::new(10u32)).service(
|
||||||
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
|
));
|
||||||
|
let req = TestRequest::default().to_request();
|
||||||
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_route_data_extractor() {
|
fn test_route_data_extractor() {
|
||||||
let mut srv =
|
let mut srv =
|
||||||
|
17
src/error.rs
17
src/error.rs
@ -92,6 +92,23 @@ impl ResponseError for JsonPayloadError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A set of errors that can occur during parsing request paths
|
||||||
|
#[derive(Debug, Display, From)]
|
||||||
|
pub enum PathError {
|
||||||
|
/// Deserialize error
|
||||||
|
#[display(fmt = "Path deserialize error: {}", _0)]
|
||||||
|
Deserialize(de::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return `BadRequest` for `PathError`
|
||||||
|
impl ResponseError for PathError {
|
||||||
|
fn error_response(&self) -> HttpResponse {
|
||||||
|
match *self {
|
||||||
|
PathError::Deserialize(_) => HttpResponse::new(StatusCode::BAD_REQUEST),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing query strings
|
/// A set of errors that can occur during parsing query strings
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
pub enum QueryPayloadError {
|
pub enum QueryPayloadError {
|
||||||
|
@ -66,15 +66,15 @@
|
|||||||
//!
|
//!
|
||||||
//! ## Package feature
|
//! ## Package feature
|
||||||
//!
|
//!
|
||||||
//! * `client` - enables http client
|
//! * `client` - enables http client (default enabled)
|
||||||
//! * `ssl` - enables ssl support via `openssl` crate, supports `http/2`
|
//! * `ssl` - enables ssl support via `openssl` crate, supports `http/2`
|
||||||
//! * `rust-tls` - enables ssl support via `rustls` crate, supports `http/2`
|
//! * `rust-tls` - enables ssl support via `rustls` crate, supports `http/2`
|
||||||
//! * `secure-cookies` - enables secure cookies support, includes `ring` crate as
|
//! * `secure-cookies` - enables secure cookies support, includes `ring` crate as
|
||||||
//! dependency
|
//! dependency
|
||||||
//! * `brotli` - enables `brotli` compression support, requires `c`
|
//! * `brotli` - enables `brotli` compression support, requires `c`
|
||||||
//! compiler
|
//! compiler (default enabled)
|
||||||
//! * `flate2-zlib` - enables `gzip`, `deflate` compression support, requires
|
//! * `flate2-zlib` - enables `gzip`, `deflate` compression support, requires
|
||||||
//! `c` compiler
|
//! `c` compiler (default enabled)
|
||||||
//! * `flate2-rust` - experimental rust based implementation for
|
//! * `flate2-rust` - experimental rust based implementation for
|
||||||
//! `gzip`, `deflate` compression.
|
//! `gzip`, `deflate` compression.
|
||||||
//!
|
//!
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
mod compress;
|
mod compress;
|
||||||
pub use self::compress::{BodyEncoding, Compress};
|
pub use self::compress::{BodyEncoding, Compress};
|
||||||
|
|
||||||
pub mod cors;
|
|
||||||
mod defaultheaders;
|
mod defaultheaders;
|
||||||
pub mod errhandlers;
|
pub mod errhandlers;
|
||||||
mod logger;
|
mod logger;
|
||||||
@ -11,6 +10,3 @@ mod normalize;
|
|||||||
pub use self::defaultheaders::DefaultHeaders;
|
pub use self::defaultheaders::DefaultHeaders;
|
||||||
pub use self::logger::Logger;
|
pub use self::logger::Logger;
|
||||||
pub use self::normalize::NormalizePath;
|
pub use self::normalize::NormalizePath;
|
||||||
|
|
||||||
#[cfg(feature = "secure-cookies")]
|
|
||||||
pub mod identity;
|
|
||||||
|
@ -27,7 +27,7 @@ type HttpNewService = BoxedNewService<(), ServiceRequest, ServiceResponse, Error
|
|||||||
/// Resource in turn has at least one route.
|
/// Resource in turn has at least one route.
|
||||||
/// Route consists of an handlers objects and list of guards
|
/// Route consists of an handlers objects and list of guards
|
||||||
/// (objects that implement `Guard` trait).
|
/// (objects that implement `Guard` trait).
|
||||||
/// Resources and rouets uses builder-like pattern for configuration.
|
/// Resources and routes uses builder-like pattern for configuration.
|
||||||
/// During request handling, resource object iterate through all routes
|
/// During request handling, resource object iterate through all routes
|
||||||
/// and check guards for specific route, if request matches all
|
/// and check guards for specific route, if request matches all
|
||||||
/// guards, route considered matched and route handler get called.
|
/// guards, route considered matched and route handler get called.
|
||||||
|
@ -38,7 +38,8 @@ impl ResourceMap {
|
|||||||
pub(crate) fn finish(&self, current: Rc<ResourceMap>) {
|
pub(crate) fn finish(&self, current: Rc<ResourceMap>) {
|
||||||
for (_, nested) in &self.patterns {
|
for (_, nested) in &self.patterns {
|
||||||
if let Some(ref nested) = nested {
|
if let Some(ref nested) = nested {
|
||||||
*nested.parent.borrow_mut() = Some(current.clone())
|
*nested.parent.borrow_mut() = Some(current.clone());
|
||||||
|
nested.finish(nested.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
src/scope.rs
20
src/scope.rs
@ -1135,4 +1135,24 @@ mod tests {
|
|||||||
let body = read_body(resp);
|
let body = read_body(resp);
|
||||||
assert_eq!(body, &b"https://youtube.com/watch/xxxxxx"[..]);
|
assert_eq!(body, &b"https://youtube.com/watch/xxxxxx"[..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_url_for_nested() {
|
||||||
|
let mut srv = init_service(App::new().service(web::scope("/a").service(
|
||||||
|
web::scope("/b").service(web::resource("/c/{stuff}").name("c").route(
|
||||||
|
web::get().to(|req: HttpRequest| {
|
||||||
|
HttpResponse::Ok()
|
||||||
|
.body(format!("{}", req.url_for("c", &["12345"]).unwrap()))
|
||||||
|
}),
|
||||||
|
)),
|
||||||
|
)));
|
||||||
|
let req = TestRequest::with_uri("/a/b/c/test").to_request();
|
||||||
|
let resp = call_service(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
let body = read_body(resp);
|
||||||
|
assert_eq!(
|
||||||
|
body,
|
||||||
|
Bytes::from_static(b"http://localhost:8080/a/b/c/12345")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
46
src/test.rs
46
src/test.rs
@ -7,7 +7,7 @@ use actix_http::http::{HttpTryFrom, Method, StatusCode, Uri, Version};
|
|||||||
use actix_http::test::TestRequest as HttpTestRequest;
|
use actix_http::test::TestRequest as HttpTestRequest;
|
||||||
use actix_http::{cookie::Cookie, Extensions, Request};
|
use actix_http::{cookie::Cookie, Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
use actix_rt::Runtime;
|
use actix_rt::{System, SystemRunner};
|
||||||
use actix_server_config::ServerConfig;
|
use actix_server_config::ServerConfig;
|
||||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
@ -29,14 +29,14 @@ use crate::{Error, HttpRequest, HttpResponse};
|
|||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static RT: RefCell<Inner> = {
|
static RT: RefCell<Inner> = {
|
||||||
RefCell::new(Inner(Some(Runtime::new().unwrap())))
|
RefCell::new(Inner(Some(System::builder().build())))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Inner(Option<Runtime>);
|
struct Inner(Option<SystemRunner>);
|
||||||
|
|
||||||
impl Inner {
|
impl Inner {
|
||||||
fn get_mut(&mut self) -> &mut Runtime {
|
fn get_mut(&mut self) -> &mut SystemRunner {
|
||||||
self.0.as_mut().unwrap()
|
self.0.as_mut().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -714,4 +714,42 @@ mod tests {
|
|||||||
let res = block_fn(|| app.call(req)).unwrap();
|
let res = block_fn(|| app.call(req)).unwrap();
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_actor() {
|
||||||
|
use actix::Actor;
|
||||||
|
|
||||||
|
struct MyActor;
|
||||||
|
|
||||||
|
struct Num(usize);
|
||||||
|
impl actix::Message for Num {
|
||||||
|
type Result = usize;
|
||||||
|
}
|
||||||
|
impl actix::Actor for MyActor {
|
||||||
|
type Context = actix::Context<Self>;
|
||||||
|
}
|
||||||
|
impl actix::Handler<Num> for MyActor {
|
||||||
|
type Result = usize;
|
||||||
|
fn handle(&mut self, msg: Num, _: &mut Self::Context) -> Self::Result {
|
||||||
|
msg.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let addr = run_on(|| MyActor.start());
|
||||||
|
let mut app = init_service(App::new().service(
|
||||||
|
web::resource("/index.html").to_async(move || {
|
||||||
|
addr.send(Num(1)).from_err().and_then(|res| {
|
||||||
|
if res == 1 {
|
||||||
|
HttpResponse::Ok()
|
||||||
|
} else {
|
||||||
|
HttpResponse::BadRequest()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
let req = TestRequest::post().uri("/index.html").to_request();
|
||||||
|
let res = block_fn(|| app.call(req)).unwrap();
|
||||||
|
assert!(res.status().is_success());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,7 @@ use std::{fmt, ops};
|
|||||||
|
|
||||||
use actix_http::{Error, HttpMessage, Payload};
|
use actix_http::{Error, HttpMessage, Payload};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use encoding::types::{DecoderTrap, Encoding};
|
|
||||||
use encoding::EncodingRef;
|
|
||||||
use futures::{Future, Poll, Stream};
|
use futures::{Future, Poll, Stream};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
@ -187,7 +185,7 @@ pub struct UrlEncoded<U> {
|
|||||||
stream: Option<Decompress<Payload>>,
|
stream: Option<Decompress<Payload>>,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
length: Option<usize>,
|
length: Option<usize>,
|
||||||
encoding: EncodingRef,
|
encoding: &'static Encoding,
|
||||||
err: Option<UrlencodedError>,
|
err: Option<UrlencodedError>,
|
||||||
fut: Option<Box<Future<Item = U, Error = UrlencodedError>>>,
|
fut: Option<Box<Future<Item = U, Error = UrlencodedError>>>,
|
||||||
}
|
}
|
||||||
@ -286,13 +284,14 @@ where
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.and_then(move |body| {
|
.and_then(move |body| {
|
||||||
if (encoding as *const Encoding) == UTF_8 {
|
if encoding == UTF_8 {
|
||||||
serde_urlencoded::from_bytes::<U>(&body)
|
serde_urlencoded::from_bytes::<U>(&body)
|
||||||
.map_err(|_| UrlencodedError::Parse)
|
.map_err(|_| UrlencodedError::Parse)
|
||||||
} else {
|
} else {
|
||||||
let body = encoding
|
let body = encoding
|
||||||
.decode(&body, DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map_err(|_| UrlencodedError::Parse)?;
|
.map(|s| s.into_owned())
|
||||||
|
.ok_or(UrlencodedError::Parse)?;
|
||||||
serde_urlencoded::from_str::<U>(&body)
|
serde_urlencoded::from_str::<U>(&body)
|
||||||
.map_err(|_| UrlencodedError::Parse)
|
.map_err(|_| UrlencodedError::Parse)
|
||||||
}
|
}
|
||||||
|
@ -175,21 +175,19 @@ where
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
let req2 = req.clone();
|
let req2 = req.clone();
|
||||||
let (limit, err) = req
|
let (limit, err, ctype) = req
|
||||||
.app_data::<Self::Config>()
|
.app_data::<Self::Config>()
|
||||||
.map(|c| (c.limit, c.ehandler.clone()))
|
.map(|c| (c.limit, c.ehandler.clone(), c.content_type.clone()))
|
||||||
.unwrap_or((32768, None));
|
.unwrap_or((32768, None, None));
|
||||||
|
|
||||||
let path = req.path().to_string();
|
|
||||||
|
|
||||||
Box::new(
|
Box::new(
|
||||||
JsonBody::new(req, payload)
|
JsonBody::new(req, payload, ctype)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.map_err(move |e| {
|
.map_err(move |e| {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Failed to deserialize Json from payload. \
|
"Failed to deserialize Json from payload. \
|
||||||
Request path: {:?}",
|
Request path: {}",
|
||||||
path
|
req2.path()
|
||||||
);
|
);
|
||||||
if let Some(err) = err {
|
if let Some(err) = err {
|
||||||
(*err)(e, &req2)
|
(*err)(e, &req2)
|
||||||
@ -224,6 +222,9 @@ where
|
|||||||
/// // change json extractor configuration
|
/// // change json extractor configuration
|
||||||
/// web::Json::<Info>::configure(|cfg| {
|
/// web::Json::<Info>::configure(|cfg| {
|
||||||
/// cfg.limit(4096)
|
/// cfg.limit(4096)
|
||||||
|
/// .content_type(|mime| { // <- accept text/plain content type
|
||||||
|
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||||
|
/// })
|
||||||
/// .error_handler(|err, req| { // <- create custom error response
|
/// .error_handler(|err, req| { // <- create custom error response
|
||||||
/// error::InternalError::from_response(
|
/// error::InternalError::from_response(
|
||||||
/// err, HttpResponse::Conflict().finish()).into()
|
/// err, HttpResponse::Conflict().finish()).into()
|
||||||
@ -237,6 +238,7 @@ where
|
|||||||
pub struct JsonConfig {
|
pub struct JsonConfig {
|
||||||
limit: usize,
|
limit: usize,
|
||||||
ehandler: Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>,
|
ehandler: Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>,
|
||||||
|
content_type: Option<Arc<dyn Fn(mime::Mime) -> bool + Send + Sync>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsonConfig {
|
impl JsonConfig {
|
||||||
@ -254,6 +256,15 @@ impl JsonConfig {
|
|||||||
self.ehandler = Some(Arc::new(f));
|
self.ehandler = Some(Arc::new(f));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set predicate for allowed content types
|
||||||
|
pub fn content_type<F>(mut self, predicate: F) -> Self
|
||||||
|
where
|
||||||
|
F: Fn(mime::Mime) -> bool + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
self.content_type = Some(Arc::new(predicate));
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for JsonConfig {
|
impl Default for JsonConfig {
|
||||||
@ -261,6 +272,7 @@ impl Default for JsonConfig {
|
|||||||
JsonConfig {
|
JsonConfig {
|
||||||
limit: 32768,
|
limit: 32768,
|
||||||
ehandler: None,
|
ehandler: None,
|
||||||
|
content_type: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -271,6 +283,7 @@ impl Default for JsonConfig {
|
|||||||
/// Returns error:
|
/// Returns error:
|
||||||
///
|
///
|
||||||
/// * content type is not `application/json`
|
/// * content type is not `application/json`
|
||||||
|
/// (unless specified in [`JsonConfig`](struct.JsonConfig.html))
|
||||||
/// * content length is greater than 256k
|
/// * content length is greater than 256k
|
||||||
pub struct JsonBody<U> {
|
pub struct JsonBody<U> {
|
||||||
limit: usize,
|
limit: usize,
|
||||||
@ -285,13 +298,20 @@ where
|
|||||||
U: DeserializeOwned + 'static,
|
U: DeserializeOwned + 'static,
|
||||||
{
|
{
|
||||||
/// Create `JsonBody` for request.
|
/// Create `JsonBody` for request.
|
||||||
pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
|
pub fn new(
|
||||||
|
req: &HttpRequest,
|
||||||
|
payload: &mut Payload,
|
||||||
|
ctype: Option<Arc<dyn Fn(mime::Mime) -> bool + Send + Sync>>,
|
||||||
|
) -> Self {
|
||||||
// check content-type
|
// check content-type
|
||||||
let json = if let Ok(Some(mime)) = req.mime_type() {
|
let json = if let Ok(Some(mime)) = req.mime_type() {
|
||||||
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
|
mime.subtype() == mime::JSON
|
||||||
|
|| mime.suffix() == Some(mime::JSON)
|
||||||
|
|| ctype.as_ref().map_or(false, |predicate| predicate(mime))
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
if !json {
|
if !json {
|
||||||
return JsonBody {
|
return JsonBody {
|
||||||
limit: 262_144,
|
limit: 262_144,
|
||||||
@ -302,14 +322,11 @@ where
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut len = None;
|
let len = req
|
||||||
if let Some(l) = req.headers().get(&CONTENT_LENGTH) {
|
.headers()
|
||||||
if let Ok(s) = l.to_str() {
|
.get(&CONTENT_LENGTH)
|
||||||
if let Ok(l) = s.parse::<usize>() {
|
.and_then(|l| l.to_str().ok())
|
||||||
len = Some(l)
|
.and_then(|s| s.parse::<usize>().ok());
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let payload = Decompress::from_headers(payload.take(), req.headers());
|
let payload = Decompress::from_headers(payload.take(), req.headers());
|
||||||
|
|
||||||
JsonBody {
|
JsonBody {
|
||||||
@ -512,7 +529,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_json_body() {
|
fn test_json_body() {
|
||||||
let (req, mut pl) = TestRequest::default().to_http_parts();
|
let (req, mut pl) = TestRequest::default().to_http_parts();
|
||||||
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl));
|
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl, None));
|
||||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||||
|
|
||||||
let (req, mut pl) = TestRequest::default()
|
let (req, mut pl) = TestRequest::default()
|
||||||
@ -521,7 +538,7 @@ mod tests {
|
|||||||
header::HeaderValue::from_static("application/text"),
|
header::HeaderValue::from_static("application/text"),
|
||||||
)
|
)
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl));
|
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl, None));
|
||||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||||
|
|
||||||
let (req, mut pl) = TestRequest::default()
|
let (req, mut pl) = TestRequest::default()
|
||||||
@ -535,7 +552,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
||||||
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl).limit(100));
|
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl, None).limit(100));
|
||||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::Overflow));
|
assert!(json_eq(json.err().unwrap(), JsonPayloadError::Overflow));
|
||||||
|
|
||||||
let (req, mut pl) = TestRequest::default()
|
let (req, mut pl) = TestRequest::default()
|
||||||
@ -550,7 +567,7 @@ mod tests {
|
|||||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
||||||
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl));
|
let json = block_on(JsonBody::<MyObject>::new(&req, &mut pl, None));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
json.ok().unwrap(),
|
json.ok().unwrap(),
|
||||||
MyObject {
|
MyObject {
|
||||||
@ -558,4 +575,62 @@ mod tests {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_json_and_bad_content_type() {
|
||||||
|
let (req, mut pl) = TestRequest::with_header(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static("text/plain"),
|
||||||
|
)
|
||||||
|
.header(
|
||||||
|
header::CONTENT_LENGTH,
|
||||||
|
header::HeaderValue::from_static("16"),
|
||||||
|
)
|
||||||
|
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||||
|
.data(JsonConfig::default().limit(4096))
|
||||||
|
.to_http_parts();
|
||||||
|
|
||||||
|
let s = block_on(Json::<MyObject>::from_request(&req, &mut pl));
|
||||||
|
assert!(s.is_err())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_json_and_good_custom_content_type() {
|
||||||
|
let (req, mut pl) = TestRequest::with_header(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static("text/plain"),
|
||||||
|
)
|
||||||
|
.header(
|
||||||
|
header::CONTENT_LENGTH,
|
||||||
|
header::HeaderValue::from_static("16"),
|
||||||
|
)
|
||||||
|
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||||
|
.data(JsonConfig::default().content_type(|mime: mime::Mime| {
|
||||||
|
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||||
|
}))
|
||||||
|
.to_http_parts();
|
||||||
|
|
||||||
|
let s = block_on(Json::<MyObject>::from_request(&req, &mut pl));
|
||||||
|
assert!(s.is_ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_json_and_bad_custom_content_type() {
|
||||||
|
let (req, mut pl) = TestRequest::with_header(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
header::HeaderValue::from_static("text/html"),
|
||||||
|
)
|
||||||
|
.header(
|
||||||
|
header::CONTENT_LENGTH,
|
||||||
|
header::HeaderValue::from_static("16"),
|
||||||
|
)
|
||||||
|
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||||
|
.data(JsonConfig::default().content_type(|mime: mime::Mime| {
|
||||||
|
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||||
|
}))
|
||||||
|
.to_http_parts();
|
||||||
|
|
||||||
|
let s = block_on(Json::<MyObject>::from_request(&req, &mut pl));
|
||||||
|
assert!(s.is_err())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,6 @@ pub(crate) mod readlines;
|
|||||||
|
|
||||||
pub use self::form::{Form, FormConfig};
|
pub use self::form::{Form, FormConfig};
|
||||||
pub use self::json::{Json, JsonConfig};
|
pub use self::json::{Json, JsonConfig};
|
||||||
pub use self::path::Path;
|
pub use self::path::{Path, PathConfig};
|
||||||
pub use self::payload::{Payload, PayloadConfig};
|
pub use self::payload::{Payload, PayloadConfig};
|
||||||
pub use self::query::{Query, QueryConfig};
|
pub use self::query::{Query, QueryConfig};
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
//! Path extractor
|
//! Path extractor
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
use std::{fmt, ops};
|
use std::{fmt, ops};
|
||||||
|
|
||||||
use actix_http::error::{Error, ErrorNotFound};
|
use actix_http::error::{Error, ErrorNotFound};
|
||||||
@ -7,6 +8,7 @@ use actix_router::PathDeserializer;
|
|||||||
use serde::de;
|
use serde::de;
|
||||||
|
|
||||||
use crate::dev::Payload;
|
use crate::dev::Payload;
|
||||||
|
use crate::error::PathError;
|
||||||
use crate::request::HttpRequest;
|
use crate::request::HttpRequest;
|
||||||
use crate::FromRequest;
|
use crate::FromRequest;
|
||||||
|
|
||||||
@ -156,15 +158,89 @@ impl<T> FromRequest for Path<T>
|
|||||||
where
|
where
|
||||||
T: de::DeserializeOwned,
|
T: de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
type Config = ();
|
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Self, Error>;
|
type Future = Result<Self, Error>;
|
||||||
|
type Config = PathConfig;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
let error_handler = req
|
||||||
|
.app_data::<Self::Config>()
|
||||||
|
.map(|c| c.ehandler.clone())
|
||||||
|
.unwrap_or(None);
|
||||||
|
|
||||||
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
||||||
.map(|inner| Path { inner })
|
.map(|inner| Path { inner })
|
||||||
.map_err(ErrorNotFound)
|
.map_err(move |e| {
|
||||||
|
log::debug!(
|
||||||
|
"Failed during Path extractor deserialization. \
|
||||||
|
Request path: {:?}",
|
||||||
|
req.path()
|
||||||
|
);
|
||||||
|
if let Some(error_handler) = error_handler {
|
||||||
|
let e = PathError::Deserialize(e);
|
||||||
|
(error_handler)(e, req)
|
||||||
|
} else {
|
||||||
|
ErrorNotFound(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Path extractor configuration
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// use actix_web::web::PathConfig;
|
||||||
|
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
|
||||||
|
///
|
||||||
|
/// #[derive(Deserialize, Debug)]
|
||||||
|
/// enum Folder {
|
||||||
|
/// #[serde(rename = "inbox")]
|
||||||
|
/// Inbox,
|
||||||
|
/// #[serde(rename = "outbox")]
|
||||||
|
/// Outbox,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // deserialize `Info` from request's path
|
||||||
|
/// fn index(folder: web::Path<Folder>) -> String {
|
||||||
|
/// format!("Selected folder: {:?}!", folder)
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let app = App::new().service(
|
||||||
|
/// web::resource("/messages/{folder}")
|
||||||
|
/// .data(PathConfig::default().error_handler(|err, req| {
|
||||||
|
/// error::InternalError::from_response(
|
||||||
|
/// err,
|
||||||
|
/// HttpResponse::Conflict().finish(),
|
||||||
|
/// )
|
||||||
|
/// .into()
|
||||||
|
/// }))
|
||||||
|
/// .route(web::post().to(index)),
|
||||||
|
/// );
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct PathConfig {
|
||||||
|
ehandler: Option<Arc<Fn(PathError, &HttpRequest) -> Error + Send + Sync>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PathConfig {
|
||||||
|
/// Set custom error handler
|
||||||
|
pub fn error_handler<F>(mut self, f: F) -> Self
|
||||||
|
where
|
||||||
|
F: Fn(PathError, &HttpRequest) -> Error + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
self.ehandler = Some(Arc::new(f));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PathConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
PathConfig { ehandler: None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,6 +252,7 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::{block_on, TestRequest};
|
use crate::test::{block_on, TestRequest};
|
||||||
|
use crate::{error, http, HttpResponse};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Display)]
|
#[derive(Deserialize, Debug, Display)]
|
||||||
#[display(fmt = "MyStruct({}, {})", key, value)]
|
#[display(fmt = "MyStruct({}, {})", key, value)]
|
||||||
@ -271,4 +348,21 @@ mod tests {
|
|||||||
assert_eq!(res[1], "32".to_owned());
|
assert_eq!(res[1], "32".to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_custom_err_handler() {
|
||||||
|
let (req, mut pl) = TestRequest::with_uri("/name/user1/")
|
||||||
|
.data(PathConfig::default().error_handler(|err, _| {
|
||||||
|
error::InternalError::from_response(
|
||||||
|
err,
|
||||||
|
HttpResponse::Conflict().finish(),
|
||||||
|
)
|
||||||
|
.into()
|
||||||
|
}))
|
||||||
|
.to_http_parts();
|
||||||
|
|
||||||
|
let s = block_on(Path::<(usize,)>::from_request(&req, &mut pl)).unwrap_err();
|
||||||
|
let res: HttpResponse = s.into();
|
||||||
|
|
||||||
|
assert_eq!(res.status(), http::StatusCode::CONFLICT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ use std::str;
|
|||||||
use actix_http::error::{Error, ErrorBadRequest, PayloadError};
|
use actix_http::error::{Error, ErrorBadRequest, PayloadError};
|
||||||
use actix_http::HttpMessage;
|
use actix_http::HttpMessage;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::UTF_8;
|
||||||
use encoding::types::{DecoderTrap, Encoding};
|
|
||||||
use futures::future::{err, Either, FutureResult};
|
use futures::future::{err, Either, FutureResult};
|
||||||
use futures::{Future, Poll, Stream};
|
use futures::{Future, Poll, Stream};
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
@ -208,15 +207,15 @@ impl FromRequest for String {
|
|||||||
.limit(limit)
|
.limit(limit)
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(move |body| {
|
.and_then(move |body| {
|
||||||
let enc: *const Encoding = encoding as *const Encoding;
|
if encoding == UTF_8 {
|
||||||
if enc == UTF_8 {
|
|
||||||
Ok(str::from_utf8(body.as_ref())
|
Ok(str::from_utf8(body.as_ref())
|
||||||
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
||||||
.to_owned())
|
.to_owned())
|
||||||
} else {
|
} else {
|
||||||
Ok(encoding
|
Ok(encoding
|
||||||
.decode(&body, DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map_err(|_| ErrorBadRequest("Can not decode body"))?)
|
.map(|s| s.into_owned())
|
||||||
|
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
))
|
))
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use encoding::types::{DecoderTrap, Encoding};
|
|
||||||
use encoding::EncodingRef;
|
|
||||||
use futures::{Async, Poll, Stream};
|
use futures::{Async, Poll, Stream};
|
||||||
|
|
||||||
use crate::dev::Payload;
|
use crate::dev::Payload;
|
||||||
@ -16,7 +15,7 @@ pub struct Readlines<T: HttpMessage> {
|
|||||||
buff: BytesMut,
|
buff: BytesMut,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
checked_buff: bool,
|
checked_buff: bool,
|
||||||
encoding: EncodingRef,
|
encoding: &'static Encoding,
|
||||||
err: Option<ReadlinesError>,
|
err: Option<ReadlinesError>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,15 +86,17 @@ where
|
|||||||
if ind + 1 > self.limit {
|
if ind + 1 > self.limit {
|
||||||
return Err(ReadlinesError::LimitOverflow);
|
return Err(ReadlinesError::LimitOverflow);
|
||||||
}
|
}
|
||||||
let enc: *const Encoding = self.encoding as *const Encoding;
|
let line = if self.encoding == UTF_8 {
|
||||||
let line = if enc == UTF_8 {
|
|
||||||
str::from_utf8(&self.buff.split_to(ind + 1))
|
str::from_utf8(&self.buff.split_to(ind + 1))
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map_err(|_| ReadlinesError::EncodingError)?
|
||||||
.to_owned()
|
.to_owned()
|
||||||
} else {
|
} else {
|
||||||
self.encoding
|
self.encoding
|
||||||
.decode(&self.buff.split_to(ind + 1), DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
&self.buff.split_to(ind + 1),
|
||||||
|
)
|
||||||
|
.map(Cow::into_owned)
|
||||||
|
.ok_or(ReadlinesError::EncodingError)?
|
||||||
};
|
};
|
||||||
return Ok(Async::Ready(Some(line)));
|
return Ok(Async::Ready(Some(line)));
|
||||||
}
|
}
|
||||||
@ -117,15 +118,17 @@ where
|
|||||||
if ind + 1 > self.limit {
|
if ind + 1 > self.limit {
|
||||||
return Err(ReadlinesError::LimitOverflow);
|
return Err(ReadlinesError::LimitOverflow);
|
||||||
}
|
}
|
||||||
let enc: *const Encoding = self.encoding as *const Encoding;
|
let line = if self.encoding == UTF_8 {
|
||||||
let line = if enc == UTF_8 {
|
|
||||||
str::from_utf8(&bytes.split_to(ind + 1))
|
str::from_utf8(&bytes.split_to(ind + 1))
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map_err(|_| ReadlinesError::EncodingError)?
|
||||||
.to_owned()
|
.to_owned()
|
||||||
} else {
|
} else {
|
||||||
self.encoding
|
self.encoding
|
||||||
.decode(&bytes.split_to(ind + 1), DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
&bytes.split_to(ind + 1),
|
||||||
|
)
|
||||||
|
.map(Cow::into_owned)
|
||||||
|
.ok_or(ReadlinesError::EncodingError)?
|
||||||
};
|
};
|
||||||
// extend buffer with rest of the bytes;
|
// extend buffer with rest of the bytes;
|
||||||
self.buff.extend_from_slice(&bytes);
|
self.buff.extend_from_slice(&bytes);
|
||||||
@ -143,15 +146,15 @@ where
|
|||||||
if self.buff.len() > self.limit {
|
if self.buff.len() > self.limit {
|
||||||
return Err(ReadlinesError::LimitOverflow);
|
return Err(ReadlinesError::LimitOverflow);
|
||||||
}
|
}
|
||||||
let enc: *const Encoding = self.encoding as *const Encoding;
|
let line = if self.encoding == UTF_8 {
|
||||||
let line = if enc == UTF_8 {
|
|
||||||
str::from_utf8(&self.buff)
|
str::from_utf8(&self.buff)
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map_err(|_| ReadlinesError::EncodingError)?
|
||||||
.to_owned()
|
.to_owned()
|
||||||
} else {
|
} else {
|
||||||
self.encoding
|
self.encoding
|
||||||
.decode(&self.buff, DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(&self.buff)
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map(Cow::into_owned)
|
||||||
|
.ok_or(ReadlinesError::EncodingError)?
|
||||||
};
|
};
|
||||||
self.buff.clear();
|
self.buff.clear();
|
||||||
Ok(Async::Ready(Some(line)))
|
Ok(Async::Ready(Some(line)))
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.2] - 2019-06-16
|
||||||
|
|
||||||
|
* Add .put() and .sput() methods
|
||||||
|
|
||||||
|
## [0.2.1] - 2019-06-05
|
||||||
|
|
||||||
|
* Add license files
|
||||||
|
|
||||||
## [0.2.0] - 2019-05-12
|
## [0.2.0] - 2019-05-12
|
||||||
|
|
||||||
* Update awc and actix-http deps
|
* Update awc and actix-http deps
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-http-test"
|
name = "actix-http-test"
|
||||||
version = "0.2.0"
|
version = "0.2.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix http test server"
|
description = "Actix http test server"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -32,10 +32,10 @@ ssl = ["openssl", "actix-server/ssl", "awc/ssl"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.1.2"
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "0.2.2"
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.1"
|
||||||
actix-server = "0.5.0"
|
actix-server = "0.5.1"
|
||||||
actix-utils = "0.4.0"
|
actix-utils = "0.4.1"
|
||||||
awc = "0.2.0"
|
awc = "0.2.1"
|
||||||
|
|
||||||
base64 = "0.10"
|
base64 = "0.10"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
@ -55,5 +55,5 @@ tokio-timer = "0.2"
|
|||||||
openssl = { version="0.10", optional = true }
|
openssl = { version="0.10", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = "1.0.0-beta.4"
|
actix-web = "1.0.0"
|
||||||
actix-http = "0.2.0"
|
actix-http = "0.2.4"
|
||||||
|
@ -255,6 +255,16 @@ impl TestServerRuntime {
|
|||||||
self.client.head(self.surl(path.as_ref()).as_str())
|
self.client.head(self.surl(path.as_ref()).as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create `PUT` request
|
||||||
|
pub fn put<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.put(self.url(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create https `PUT` request
|
||||||
|
pub fn sput<S: AsRef<str>>(&self, path: S) -> ClientRequest {
|
||||||
|
self.client.put(self.surl(path.as_ref()).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
/// Connect to test http server
|
/// Connect to test http server
|
||||||
pub fn request<S: AsRef<str>>(&self, method: Method, path: S) -> ClientRequest {
|
pub fn request<S: AsRef<str>>(&self, method: Method, path: S) -> ClientRequest {
|
||||||
self.client.request(method, path.as_ref())
|
self.client.request(method, path.as_ref())
|
||||||
|
Reference in New Issue
Block a user