1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-04 18:06:23 +02:00

Compare commits

...

21 Commits

Author SHA1 Message Date
8ff56d7cd5 prepare actix-session release 2019-05-18 11:20:09 -07:00
0843bce7ba prepare actix-multipart 2019-05-18 11:15:58 -07:00
dea0e0a721 update actix-server dep 2019-05-18 11:00:33 -07:00
e857ab1f81 HttpServer::shutdown_timeout u16 to u64 (#849)
Increase maximum graceful shutdown time from 18 hours.

For issue #848.
2019-05-18 10:50:35 -07:00
0dda4b06ea prepare release 2019-05-18 10:49:59 -07:00
cbe0226177 update changes 2019-05-18 10:47:08 -07:00
e8c8626878 update deps 2019-05-18 09:54:23 -07:00
4b215e0839 Support Query<T>::from_query() (#846) 2019-05-17 13:10:46 -07:00
e1ff3bf8fa fix resource match with params #841 2019-05-15 10:31:40 -07:00
80f4ef9aac When using codegen with paths that have parameters then only the first endpoint resolves (#842) 2019-05-15 09:21:07 -07:00
bba90d7f22 Query config (#839)
* add QueryConfig

* expose QueryConfig in web module

* fmt

* use associated type for QueryConfig

* update CHANGES.md
2019-05-14 13:54:30 -07:00
f8af3b86e5 export set_date 2019-05-14 08:48:11 -07:00
6c3d8b8738 Make JsonConfig send (#830)
* replace Rc with Arc

* add Send trait requirement for Fn in JsonConfig error handler

* add Sync trait requirement for Fn in JsonConfig error handler

* use associated type inside JsonConfig

* fix lint: members in the impl has the same order in the trait

* Update CHANGES.md
2019-05-12 20:04:08 -07:00
5a90e33bcc update deps 2019-05-12 12:01:24 -07:00
86b569e320 version 2019-05-12 11:56:01 -07:00
2350a2dc68 Handle cancellation of uploads #834 #736 2019-05-12 11:43:05 -07:00
36d017dcc6 update deps 2019-05-12 11:41:43 -07:00
3bb081852c prep actix-session release 2019-05-12 10:53:21 -07:00
1ca58e876b prepare beta4 release 2019-05-12 10:49:21 -07:00
e9cbcbaf03 update dependencies 2019-05-12 10:18:02 -07:00
07c9eec803 prepare awc release 2019-05-12 10:04:38 -07:00
31 changed files with 381 additions and 75 deletions

View File

@ -1,6 +1,22 @@
# Changes
## [1.0.0-beta.4] - 2019-05-xx
## [1.0.0-rc] - 2019-05-18
### Add
* Add `Query<T>::from_query()` to extract parameters from a query string. #846
* `QueryConfig`, similar to `JsonConfig` for customizing error handling of query extractors.
### Changes
* `JsonConfig` is now `Send + Sync`, this implies that `error_handler` must be `Send + Sync` too.
### Fixed
* Codegen with parameters in the path only resolves the first registered endpoint #841
## [1.0.0-beta.4] - 2019-05-12
### Add
@ -9,6 +25,7 @@
### Changes
* `App::configure` take an `FnOnce` instead of `Fn`
* Upgrade actix-net crates
## [1.0.0-beta.3] - 2019-05-04

View File

@ -1,6 +1,6 @@
[package]
name = "actix-web"
version = "1.0.0-beta.3"
version = "1.0.0-rc"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
readme = "README.md"
@ -41,7 +41,7 @@ members = [
]
[features]
default = ["brotli", "flate2-zlib", "secure-cookies", "client"]
default = ["brotli", "flate2-zlib", "secure-cookies", "client", "fail"]
# http client
client = ["awc"]
@ -58,6 +58,8 @@ flate2-rust = ["actix-http/flate2-rust"]
# sessions feature, session require "ring" crate and c compiler
secure-cookies = ["actix-http/secure-cookies"]
fail = ["actix-http/fail"]
# openssl
ssl = ["openssl", "actix-server/ssl", "awc/ssl"]
@ -67,21 +69,20 @@ rust-tls = ["rustls", "actix-server/rust-tls"]
[dependencies]
actix-codec = "0.1.2"
actix-service = "0.4.0"
actix-utils = "0.4.0"
actix-router = "0.1.3"
actix-utils = "0.4.1"
actix-router = "0.1.5"
actix-rt = "0.2.2"
actix-web-codegen = "0.1.0-beta.1"
actix-http = { version = "0.1.5", features=["fail"] }
actix-server = "0.5.0"
actix-web-codegen = "0.1.0"
actix-http = "0.2.0"
actix-server = "0.5.1"
actix-server-config = "0.1.1"
actix-threadpool = "0.1.0"
actix = { version = "0.8.1", features=["http"], optional = true }
awc = { version = "0.1.1", optional = true }
awc = { version = "0.2.0", optional = true }
bytes = "0.4"
derive_more = "0.14"
encoding = "0.2"
futures = "0.1"
futures = "0.1.25"
hashbrown = "0.3.0"
log = "0.4"
mime = "0.3"
@ -96,12 +97,12 @@ url = { version="1.7", features=["query_encoding"] }
# ssl support
openssl = { version="0.10", optional = true }
rustls = { version = "^0.15", optional = true }
rustls = { version = "0.15", optional = true }
[dev-dependencies]
actix-http = { version = "0.1.5", features=["ssl", "brotli", "flate2-zlib"] }
actix-http-test = { version = "0.1.1", features=["ssl"] }
actix-files = { version = "0.1.0-betsa.1" }
actix-http = { version = "0.2.0", features=["ssl", "brotli", "flate2-zlib"] }
actix-http-test = { version = "0.2.0", features=["ssl"] }
actix-files = { version = "0.1.0-beta.4" }
rand = "0.6"
env_logger = "0.6"
serde_derive = "1.0"
@ -121,8 +122,6 @@ actix-http-test = { path = "test-server" }
actix-web-codegen = { path = "actix-web-codegen" }
actix-web-actors = { path = "actix-web-actors" }
actix-session = { path = "actix-session" }
awc = { path = "awc" }
actix-files = { path = "actix-files" }
actix-framed = { path = "actix-framed" }
actix-multipart = { path = "actix-multipart" }
awc = { path = "awc" }

View File

@ -1,5 +1,9 @@
# Changes
## [0.1.0-beta.4] - 2019-05-12
* Update actix-web to beta.4
## [0.1.0-beta.1] - 2019-04-20
* Update actix-web to beta.1

View File

@ -1,6 +1,6 @@
[package]
name = "actix-files"
version = "0.1.0-betsa.1"
version = "0.1.0-beta.4"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Static files support for actix web."
readme = "README.md"
@ -18,7 +18,7 @@ name = "actix_files"
path = "src/lib.rs"
[dependencies]
actix-web = "1.0.0-beta.1"
actix-web = "1.0.0-beta.5"
actix-service = "0.4.0"
bitflags = "1"
bytes = "0.4"
@ -31,4 +31,4 @@ percent-encoding = "1.0"
v_htmlescape = "0.4"
[dev-dependencies]
actix-web = { version = "1.0.0-beta.1", features=["ssl"] }
actix-web = { version = "1.0.0-beta.5", features=["ssl"] }

View File

@ -1,6 +1,6 @@
[package]
name = "actix-framed"
version = "0.1.0"
version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix framed app server"
readme = "README.md"
@ -25,7 +25,7 @@ actix-service = "0.4.0"
actix-utils = "0.4.0"
actix-router = "0.1.2"
actix-rt = "0.2.2"
actix-http = "0.1.0"
actix-http = "0.2.0"
actix-server-config = "0.1.1"
bytes = "0.4"
@ -33,6 +33,6 @@ futures = "0.1.25"
log = "0.4"
[dev-dependencies]
actix-server = { version = "0.4.3", features=["ssl"] }
actix-connect = { version = "0.1.4", features=["ssl"] }
actix-http-test = { version = "0.1.0", features=["ssl"] }
actix-server = { version = "0.5.0", features=["ssl"] }
actix-connect = { version = "0.2.0", features=["ssl"] }
actix-http-test = { version = "0.2.0", features=["ssl"] }

View File

@ -1,5 +1,10 @@
# Changes
## [0.2.0] - 2019-05-12
* Update dependencies
## [0.1.0] - 2019-04-16
* Update tests

View File

@ -99,7 +99,7 @@ chrono = "0.4.6"
actix-rt = "0.2.2"
actix-server = { version = "0.5.0", features=["ssl"] }
actix-connect = { version = "0.2.0", features=["ssl"] }
actix-http-test = { version = "0.1.0", features=["ssl"] }
actix-http-test = { version = "0.2.0", features=["ssl"] }
env_logger = "0.6"
serde_derive = "1.0"
openssl = { version="0.10" }

View File

@ -158,7 +158,8 @@ impl ServiceConfig {
self.0.timer.now()
}
pub(crate) fn set_date(&self, dst: &mut BytesMut) {
#[doc(hidden)]
pub fn set_date(&self, dst: &mut BytesMut) {
let mut buf: [u8; 39] = [0; 39];
buf[..6].copy_from_slice(b"date: ");
buf[6..35].copy_from_slice(&self.0.timer.date().bytes);

View File

@ -31,7 +31,7 @@ const AVERAGE_HEADER_SIZE: usize = 30;
/// HTTP/1 Codec
pub struct Codec {
pub(crate) config: ServiceConfig,
config: ServiceConfig,
decoder: decoder::MessageDecoder<Request>,
payload: Option<PayloadDecoder>,
version: Version,
@ -104,6 +104,11 @@ impl Codec {
MessageType::Payload
}
}
#[inline]
pub fn config(&self) -> &ServiceConfig {
&self.config
}
}
impl Decoder for Codec {

View File

@ -567,7 +567,7 @@ where
}
if updated && self.ka_timer.is_some() {
if let Some(expire) = self.codec.config.keep_alive_expire() {
if let Some(expire) = self.codec.config().keep_alive_expire() {
self.ka_expire = expire;
}
}
@ -579,7 +579,7 @@ where
if self.ka_timer.is_none() {
// shutdown timeout
if self.flags.contains(Flags::SHUTDOWN) {
if let Some(interval) = self.codec.config.client_disconnect_timer() {
if let Some(interval) = self.codec.config().client_disconnect_timer() {
self.ka_timer = Some(Delay::new(interval));
} else {
self.flags.insert(Flags::READ_DISCONNECT);
@ -607,7 +607,7 @@ where
// start shutdown timer
if let Some(deadline) =
self.codec.config.client_disconnect_timer()
self.codec.config().client_disconnect_timer()
{
if let Some(timer) = self.ka_timer.as_mut() {
timer.reset(deadline);
@ -632,7 +632,8 @@ where
self.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
self.state = State::None;
}
} else if let Some(deadline) = self.codec.config.keep_alive_expire()
} else if let Some(deadline) =
self.codec.config().keep_alive_expire()
{
if let Some(timer) = self.ka_timer.as_mut() {
timer.reset(deadline);

View File

@ -1,5 +1,15 @@
# Changes
## [0.1.0] - 2019-05-18
* Release
## [0.1.0-beta.4] - 2019-05-12
* Handle cancellation of uploads #834 #736
* Upgrade to actix-web 1.0.0-beta.4
## [0.1.0-beta.1] - 2019-04-21
* Do not support nested multipart

View File

@ -1,6 +1,6 @@
[package]
name = "actix-multipart"
version = "0.1.0-beta.1"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Multipart support for actix web framework."
readme = "README.md"
@ -18,7 +18,7 @@ name = "actix_multipart"
path = "src/lib.rs"
[dependencies]
actix-web = "1.0.0-beta.1"
actix-web = "1.0.0-rc"
actix-service = "0.4.0"
bytes = "0.4"
derive_more = "0.14"
@ -31,4 +31,4 @@ twoway = "0.2"
[dev-dependencies]
actix-rt = "0.2.2"
actix-http = "0.1.1"
actix-http = "0.2.0"

View File

@ -28,6 +28,9 @@ pub enum MultipartError {
/// Payload error
#[display(fmt = "{}", _0)]
Payload(PayloadError),
/// Not consumed
#[display(fmt = "Multipart stream is not consumed")]
NotConsumed,
}
/// Return `BadRequest` for `MultipartError`

View File

@ -1,5 +1,5 @@
//! Multipart payload support
use std::cell::{RefCell, UnsafeCell};
use std::cell::{Cell, RefCell, UnsafeCell};
use std::marker::PhantomData;
use std::rc::Rc;
use std::{cmp, fmt};
@ -116,6 +116,8 @@ impl Stream for Multipart {
payload.poll_stream()?;
}
inner.poll(&self.safety)
} else if !self.safety.is_clean() {
Err(MultipartError::NotConsumed)
} else {
Ok(Async::NotReady)
}
@ -415,6 +417,8 @@ impl Stream for Field {
}
inner.poll(&self.safety)
} else if !self.safety.is_clean() {
return Err(MultipartError::NotConsumed);
} else {
Ok(Async::NotReady)
}
@ -655,6 +659,7 @@ struct Safety {
task: Option<Task>,
level: usize,
payload: Rc<PhantomData<bool>>,
clean: Rc<Cell<bool>>,
}
impl Safety {
@ -663,12 +668,17 @@ impl Safety {
Safety {
task: None,
level: Rc::strong_count(&payload),
clean: Rc::new(Cell::new(true)),
payload,
}
}
fn current(&self) -> bool {
Rc::strong_count(&self.payload) == self.level
Rc::strong_count(&self.payload) == self.level && self.clean.get()
}
fn is_clean(&self) -> bool {
self.clean.get()
}
}
@ -678,6 +688,7 @@ impl Clone for Safety {
Safety {
task: Some(current_task()),
level: Rc::strong_count(&payload),
clean: self.clean.clone(),
payload,
}
}
@ -687,7 +698,7 @@ impl Drop for Safety {
fn drop(&mut self) {
// parent task is dead
if Rc::strong_count(&self.payload) != self.level {
panic!("Safety get dropped but it is not from top-most task");
self.clean.set(true);
}
if let Some(task) = self.task.take() {
task.notify()

View File

@ -1,5 +1,13 @@
# Changes
## [0.1.0] - 2019-05-18
* Use actix-web 1.0.0-rc
## [0.1.0-beta.4] - 2019-05-12
* Use actix-web 1.0.0-beta.4
## [0.1.0-beta.2] - 2019-04-28
* Add helper trait `UserSession` which allows to get session for ServiceRequest and HttpRequest

View File

@ -1,6 +1,6 @@
[package]
name = "actix-session"
version = "0.1.0-beta.2"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Session for actix web framework."
readme = "README.md"
@ -24,7 +24,7 @@ default = ["cookie-session"]
cookie-session = ["actix-web/secure-cookies"]
[dependencies]
actix-web = "1.0.0-beta.2"
actix-web = "1.0.0-rc"
actix-service = "0.4.0"
bytes = "0.4"
derive_more = "0.14"

View File

@ -1,6 +1,6 @@
[package]
name = "actix-web-actors"
version = "1.0.0-alpha.3"
version = "1.0.0-beta.4"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix actors support for actix web framework."
readme = "README.md"
@ -18,13 +18,13 @@ name = "actix_web_actors"
path = "src/lib.rs"
[dependencies]
actix = "0.8.0"
actix-web = "1.0.0-beta.1"
actix-http = "0.1.1"
actix = "0.8.2"
actix-web = "1.0.0-beta.5"
actix-http = "0.2.0"
actix-codec = "0.1.2"
bytes = "0.4"
futures = "0.1.25"
[dev-dependencies]
env_logger = "0.6"
actix-http-test = { version = "0.1.0", features=["ssl"] }
actix-http-test = { version = "0.2.0", features=["ssl"] }

View File

@ -1,5 +1,9 @@
# Changes
## [0.1.0] - 2019-05-18
* Release
## [0.1.0-beta.1] - 2019-04-20
* Gen code for actix-web 1.0.0-beta.1

View File

@ -1,6 +1,6 @@
[package]
name = "actix-web-codegen"
version = "0.1.0-beta.1"
version = "0.1.0"
description = "Actix web proc macros"
readme = "README.md"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
@ -16,7 +16,7 @@ quote = "0.6"
syn = { version = "0.15", features = ["full", "parsing"] }
[dev-dependencies]
actix-web = { version = "1.0.0-alpha.6" }
actix-http = { version = "0.1.1", features=["ssl"] }
actix-http-test = { version = "0.1.0", features=["ssl"] }
actix-web = { version = "1.0.0-beta.5" }
actix-http = { version = "0.2.0", features=["ssl"] }
actix-http-test = { version = "0.2.0", features=["ssl"] }
futures = { version = "0.1" }

View File

@ -1,7 +1,7 @@
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_web::{http, App, HttpResponse, Responder};
use actix_web_codegen::{get, post, put};
use actix_web::{http, web::Path, App, HttpResponse, Responder};
use actix_web_codegen::{delete, get, post, put};
use futures::{future, Future};
#[get("/test")]
@ -29,6 +29,45 @@ fn auto_sync() -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
future::ok(HttpResponse::Ok().finish())
}
#[put("/test/{param}")]
fn put_param_test(_: Path<String>) -> impl Responder {
HttpResponse::Created()
}
#[delete("/test/{param}")]
fn delete_param_test(_: Path<String>) -> impl Responder {
HttpResponse::NoContent()
}
#[get("/test/{param}")]
fn get_param_test(_: Path<String>) -> impl Responder {
HttpResponse::Ok()
}
#[test]
fn test_params() {
let mut srv = TestServer::new(|| {
HttpService::new(
App::new()
.service(get_param_test)
.service(put_param_test)
.service(delete_param_test),
)
});
let request = srv.request(http::Method::GET, srv.url("/test/it"));
let response = srv.block_on(request.send()).unwrap();
assert_eq!(response.status(), http::StatusCode::OK);
let request = srv.request(http::Method::PUT, srv.url("/test/it"));
let response = srv.block_on(request.send()).unwrap();
assert_eq!(response.status(), http::StatusCode::CREATED);
let request = srv.request(http::Method::DELETE, srv.url("/test/it"));
let response = srv.block_on(request.send()).unwrap();
assert_eq!(response.status(), http::StatusCode::NO_CONTENT);
}
#[test]
fn test_body() {
let mut srv = TestServer::new(|| {

View File

@ -1,9 +1,15 @@
# Changes
## [0.2.0] - 2019-05-12
### Added
* Allow to send headers in `Camel-Case` form.
### Changed
* Upgrade actix-http dependency.
## [0.1.1] - 2019-04-19

View File

@ -1,6 +1,6 @@
[package]
name = "awc"
version = "0.1.1"
version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http client."
readme = "README.md"
@ -41,7 +41,7 @@ flate2-rust = ["actix-http/flate2-rust"]
[dependencies]
actix-codec = "0.1.2"
actix-service = "0.4.0"
actix-http = "0.1.4"
actix-http = "0.2.0"
base64 = "0.10.1"
bytes = "0.4"
derive_more = "0.14"
@ -58,10 +58,10 @@ openssl = { version="0.10", optional = true }
[dev-dependencies]
actix-rt = "0.2.2"
actix-web = { version = "1.0.0-beta.1", features=["ssl"] }
actix-http = { version = "0.1.4", features=["ssl"] }
actix-http-test = { version = "0.1.1", features=["ssl"] }
actix-utils = "0.3.4"
actix-web = { version = "1.0.0-beta.4", features=["ssl"] }
actix-http = { version = "0.2.0", features=["ssl"] }
actix-http-test = { version = "0.2.0", features=["ssl"] }
actix-utils = "0.4.0"
actix-server = { version = "0.5.0", features=["ssl"] }
brotli2 = { version="0.3.2" }
flate2 = { version="1.0.2" }

View File

@ -129,7 +129,6 @@ impl<T: 'static> DataFactory for Data<T> {
#[cfg(test)]
mod tests {
use actix_service::Service;
use std::sync::Mutex;
use crate::http::StatusCode;
use crate::test::{block_on, init_service, TestRequest};

View File

@ -6,6 +6,7 @@ use url::ParseError as UrlParseError;
use crate::http::StatusCode;
use crate::HttpResponse;
use serde_urlencoded::de;
/// Errors which can occur when attempting to generate resource uri.
#[derive(Debug, PartialEq, Display, From)]
@ -91,6 +92,25 @@ impl ResponseError for JsonPayloadError {
}
}
/// A set of errors that can occur during parsing query strings
#[derive(Debug, Display, From)]
pub enum QueryPayloadError {
/// Deserialize error
#[display(fmt = "Query deserialize error: {}", _0)]
Deserialize(de::Error),
}
/// Return `BadRequest` for `QueryPayloadError`
impl ResponseError for QueryPayloadError {
fn error_response(&self) -> HttpResponse {
match *self {
QueryPayloadError::Deserialize(_) => {
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
}
}
/// Error type returned when reading body as lines.
#[derive(From, Display, Debug)]
pub enum ReadlinesError {
@ -143,6 +163,15 @@ mod tests {
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn test_query_payload_error() {
let resp: HttpResponse = QueryPayloadError::Deserialize(
serde_urlencoded::from_str::<i32>("bad query").unwrap_err(),
)
.error_response();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn test_readlines_error() {
let resp: HttpResponse = ReadlinesError::LimitOverflow.error_response();

View File

@ -606,7 +606,7 @@ mod tests {
use crate::http::{header, HeaderValue, Method, StatusCode};
use crate::service::{ServiceRequest, ServiceResponse};
use crate::test::{call_service, init_service, TestRequest};
use crate::{web, App, Error, HttpResponse};
use crate::{guard, web, App, Error, HttpResponse};
fn md<S, B>(
req: ServiceRequest,
@ -723,4 +723,45 @@ mod tests {
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn test_resource_guards() {
let mut srv = init_service(
App::new()
.service(
web::resource("/test/{p}")
.guard(guard::Get())
.to(|| HttpResponse::Ok()),
)
.service(
web::resource("/test/{p}")
.guard(guard::Put())
.to(|| HttpResponse::Created()),
)
.service(
web::resource("/test/{p}")
.guard(guard::Delete())
.to(|| HttpResponse::NoContent()),
),
);
let req = TestRequest::with_uri("/test/it")
.method(Method::GET)
.to_request();
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::OK);
let req = TestRequest::with_uri("/test/it")
.method(Method::PUT)
.to_request();
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::CREATED);
let req = TestRequest::with_uri("/test/it")
.method(Method::DELETE)
.to_request();
let resp = call_service(&mut srv, req);
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
}
}

View File

@ -207,7 +207,7 @@ where
/// dropped.
///
/// By default shutdown timeout sets to 30 seconds.
pub fn shutdown_timeout(mut self, sec: u16) -> Self {
pub fn shutdown_timeout(mut self, sec: u64) -> Self {
self.builder = Some(self.builder.take().unwrap().shutdown_timeout(sec));
self
}

View File

@ -1,6 +1,6 @@
//! Json extractor/responder
use std::rc::Rc;
use std::sync::Arc;
use std::{fmt, ops};
use bytes::BytesMut;
@ -168,15 +168,15 @@ impl<T> FromRequest for Json<T>
where
T: DeserializeOwned + 'static,
{
type Config = JsonConfig;
type Error = Error;
type Future = Box<Future<Item = Self, Error = Error>>;
type Config = JsonConfig;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let req2 = req.clone();
let (limit, err) = req
.app_data::<JsonConfig>()
.app_data::<Self::Config>()
.map(|c| (c.limit, c.ehandler.clone()))
.unwrap_or((32768, None));
@ -236,7 +236,7 @@ where
#[derive(Clone)]
pub struct JsonConfig {
limit: usize,
ehandler: Option<Rc<Fn(JsonPayloadError, &HttpRequest) -> Error>>,
ehandler: Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>,
}
impl JsonConfig {
@ -249,9 +249,9 @@ impl JsonConfig {
/// Set custom error handler
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(JsonPayloadError, &HttpRequest) -> Error + 'static,
F: Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.ehandler = Some(Rc::new(f));
self.ehandler = Some(Arc::new(f));
self
}
}

View File

@ -11,4 +11,4 @@ pub use self::form::{Form, FormConfig};
pub use self::json::{Json, JsonConfig};
pub use self::path::Path;
pub use self::payload::{Payload, PayloadConfig};
pub use self::query::Query;
pub use self::query::{Query, QueryConfig};

View File

@ -1,5 +1,6 @@
//! Query extractor
use std::sync::Arc;
use std::{fmt, ops};
use actix_http::error::Error;
@ -7,6 +8,7 @@ use serde::de;
use serde_urlencoded;
use crate::dev::Payload;
use crate::error::QueryPayloadError;
use crate::extract::FromRequest;
use crate::request::HttpRequest;
@ -50,6 +52,16 @@ impl<T> Query<T> {
pub fn into_inner(self) -> T {
self.0
}
/// Get query parameters from the path
pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError>
where
T: de::DeserializeOwned,
{
serde_urlencoded::from_str::<T>(query_str)
.map(|val| Ok(Query(val)))
.unwrap_or_else(move |e| Err(QueryPayloadError::Deserialize(e)))
}
}
impl<T> ops::Deref for Query<T> {
@ -115,38 +127,123 @@ impl<T> FromRequest for Query<T>
where
T: de::DeserializeOwned,
{
type Config = ();
type Error = Error;
type Future = Result<Self, Error>;
type Config = QueryConfig;
#[inline]
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);
serde_urlencoded::from_str::<T>(req.query_string())
.map(|val| Ok(Query(val)))
.unwrap_or_else(|e| {
.unwrap_or_else(move |e| {
let e = QueryPayloadError::Deserialize(e);
log::debug!(
"Failed during Query extractor deserialization. \
Request path: {:?}",
req.path()
);
Err(e.into())
let e = if let Some(error_handler) = error_handler {
(error_handler)(e, req)
} else {
e.into()
};
Err(e)
})
}
}
/// Query extractor configuration
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
///
/// #[derive(Deserialize)]
/// struct Info {
/// username: String,
/// }
///
/// /// deserialize `Info` from request's querystring
/// fn index(info: web::Query<Info>) -> String {
/// format!("Welcome {}!", info.username)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html").data(
/// // change query extractor configuration
/// web::Query::<Info>::configure(|cfg| {
/// cfg.error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// }))
/// .route(web::post().to(index))
/// );
/// }
/// ```
#[derive(Clone)]
pub struct QueryConfig {
ehandler: Option<Arc<Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync>>,
}
impl QueryConfig {
/// Set custom error handler
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.ehandler = Some(Arc::new(f));
self
}
}
impl Default for QueryConfig {
fn default() -> Self {
QueryConfig { ehandler: None }
}
}
#[cfg(test)]
mod tests {
use actix_http::http::StatusCode;
use derive_more::Display;
use serde_derive::Deserialize;
use super::*;
use crate::error::InternalError;
use crate::test::TestRequest;
use crate::HttpResponse;
#[derive(Deserialize, Debug, Display)]
struct Id {
id: String,
}
#[test]
fn test_service_request_extract() {
let req = TestRequest::with_uri("/name/user1/").to_srv_request();
assert!(Query::<Id>::from_query(&req.query_string()).is_err());
let req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request();
let mut s = Query::<Id>::from_query(&req.query_string()).unwrap();
assert_eq!(s.id, "test");
assert_eq!(format!("{}, {:?}", s, s), "test, Id { id: \"test\" }");
s.id = "test1".to_string();
let s = s.into_inner();
assert_eq!(s.id, "test1");
}
#[test]
fn test_request_extract() {
let req = TestRequest::with_uri("/name/user1/").to_srv_request();
@ -164,4 +261,27 @@ mod tests {
let s = s.into_inner();
assert_eq!(s.id, "test1");
}
#[test]
fn test_custom_error_responder() {
let req = TestRequest::with_uri("/name/user1/")
.data(QueryConfig::default().error_handler(|e, _| {
let resp = HttpResponse::UnprocessableEntity().finish();
InternalError::from_response(e, resp).into()
}))
.to_srv_request();
let (req, mut pl) = req.into_parts();
let query = Query::<Id>::from_request(&req, &mut pl);
assert!(query.is_err());
assert_eq!(
query
.unwrap_err()
.as_response_error()
.error_response()
.status(),
StatusCode::UNPROCESSABLE_ENTITY
);
}
}

View File

@ -1,5 +1,9 @@
# Changes
## [0.2.0] - 2019-05-12
* Update awc and actix-http deps
## [0.1.1] - 2019-04-24
* Always make new connection for http client

View File

@ -1,6 +1,6 @@
[package]
name = "actix-http-test"
version = "0.1.1"
version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http test server"
readme = "README.md"
@ -35,7 +35,7 @@ actix-rt = "0.2.2"
actix-service = "0.4.0"
actix-server = "0.5.0"
actix-utils = "0.4.0"
awc = "0.1.1"
awc = "0.2.0"
base64 = "0.10"
bytes = "0.4"
@ -55,5 +55,5 @@ tokio-timer = "0.2"
openssl = { version="0.10", optional = true }
[dev-dependencies]
actix-web = "1.0.0-beta.1"
actix-http = "0.1.2"
actix-web = "1.0.0-beta.4"
actix-http = "0.2.0"