1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-15 14:26:15 +02:00

Compare commits

...

6 Commits

Author SHA1 Message Date
b3783b403e Merge branch 'master' of github.com:actix/actix-web 2019-09-17 21:46:45 +06:00
e4503046de Do not override current System 2019-09-17 21:45:06 +06:00
32a1c36597 Make UrlencodedError::Overflow more informative (#1089) 2019-09-17 06:58:04 +06:00
7c9f9afc46 Add ability to use Infallible as HttpResponse error type (#1093)
* Add `std::convert::Infallible` implementantion for `ResponseError`

* Add from `std::convert::Infallible` to `Error`

* Remove `ResponseError` implementantion for `Infallible`

* Remove useless docs

* Better comment

* Update changelog

* Update actix_http::changelog
2019-09-17 06:57:38 +06:00
c1f99e0775 Remove mem::uninitialized() (#1090) 2019-09-16 07:52:23 +09:00
a32573bb58 Allow to re-construct ServiceRequest from HttpRequest and Payload #1088 2019-09-13 11:56:24 +06:00
13 changed files with 148 additions and 65 deletions

View File

@ -1,13 +1,14 @@
# Changes # Changes
## not released yet
## [1.0.8] - 2019-09-xx
### Added ### Added
* Add `middleware::Conditon` that conditionally enables another middleware * Add `middleware::Conditon` that conditionally enables another middleware
### Fixed * Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload`
* h2 will use error response #1080 * Make UrlEncodedError::Overflow more informativve
## [1.0.7] - 2019-08-29 ## [1.0.7] - 2019-08-29

View File

@ -6,8 +6,13 @@
* Add support for sending HTTP requests with `Rc<RequestHead>` in addition to sending HTTP requests with `RequestHead` * Add support for sending HTTP requests with `Rc<RequestHead>` in addition to sending HTTP requests with `RequestHead`
* Allow to use `std::convert::Infallible` as `actix_http::error::Error`
### Fixed ### Fixed
* h2 will use error response #1080
* on_connect result isn't added to request extensions for http2 requests #1009 * on_connect result isn't added to request extensions for http2 requests #1009

View File

@ -212,7 +212,7 @@ where
pub fn finish( pub fn finish(
self, self,
) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectError> ) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectError>
+ Clone { + Clone {
#[cfg(not(any(feature = "ssl", feature = "rust-tls")))] #[cfg(not(any(feature = "ssl", feature = "rust-tls")))]
{ {
let connector = TimeoutService::new( let connector = TimeoutService::new(

View File

@ -132,6 +132,14 @@ impl std::error::Error for Error {
} }
} }
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
// `std::convert::Infallible` indicates an error
// that will never happen
unreachable!()
}
}
/// Convert `Error` to a `Response` instance /// Convert `Error` to a `Response` instance
impl From<Error> for Response { impl From<Error> for Response {
fn from(err: Error) -> Self { fn from(err: Error) -> Self {

View File

@ -1,5 +1,6 @@
use std::io;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::{io, mem}; use std::mem::MaybeUninit;
use actix_codec::Decoder; use actix_codec::Decoder;
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
@ -186,11 +187,12 @@ impl MessageType for Request {
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> { fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
// Unsafe: we read only this data only after httparse parses headers into. // Unsafe: we read only this data only after httparse parses headers into.
// performance bump for pipeline benchmarks. // performance bump for pipeline benchmarks.
let mut headers: [HeaderIndex; MAX_HEADERS] = unsafe { mem::uninitialized() }; let mut headers: [HeaderIndex; MAX_HEADERS] =
unsafe { MaybeUninit::uninit().assume_init() };
let (len, method, uri, ver, h_len) = { let (len, method, uri, ver, h_len) = {
let mut parsed: [httparse::Header; MAX_HEADERS] = let mut parsed: [httparse::Header; MAX_HEADERS] =
unsafe { mem::uninitialized() }; unsafe { MaybeUninit::uninit().assume_init() };
let mut req = httparse::Request::new(&mut parsed); let mut req = httparse::Request::new(&mut parsed);
match req.parse(src)? { match req.parse(src)? {
@ -260,11 +262,12 @@ impl MessageType for ResponseHead {
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> { fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
// Unsafe: we read only this data only after httparse parses headers into. // Unsafe: we read only this data only after httparse parses headers into.
// performance bump for pipeline benchmarks. // performance bump for pipeline benchmarks.
let mut headers: [HeaderIndex; MAX_HEADERS] = unsafe { mem::uninitialized() }; let mut headers: [HeaderIndex; MAX_HEADERS] =
unsafe { MaybeUninit::uninit().assume_init() };
let (len, ver, status, h_len) = { let (len, ver, status, h_len) = {
let mut parsed: [httparse::Header; MAX_HEADERS] = let mut parsed: [httparse::Header; MAX_HEADERS] =
unsafe { mem::uninitialized() }; unsafe { MaybeUninit::uninit().assume_init() };
let mut res = httparse::Response::new(&mut parsed); let mut res = httparse::Response::new(&mut parsed);
match res.parse(src)? { match res.parse(src)? {

View File

@ -115,7 +115,7 @@ pub fn write_content_length(mut n: usize, bytes: &mut BytesMut) {
pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) { pub(crate) fn convert_usize(mut n: usize, bytes: &mut BytesMut) {
let mut curr: isize = 39; let mut curr: isize = 39;
let mut buf: [u8; 41] = unsafe { mem::uninitialized() }; let mut buf: [u8; 41] = unsafe { mem::MaybeUninit::uninit().assume_init() };
buf[39] = b'\r'; buf[39] = b'\r';
buf[40] = b'\n'; buf[40] = b'\n';
let buf_ptr = buf.as_mut_ptr(); let buf_ptr = buf.as_mut_ptr();

View File

@ -150,7 +150,7 @@ impl TestRequest {
/// Complete request creation and generate `Request` instance /// Complete request creation and generate `Request` instance
pub fn finish(&mut self) -> Request { pub fn finish(&mut self) -> Request {
let inner = self.0.take().expect("cannot reuse test request builder");; let inner = self.0.take().expect("cannot reuse test request builder");
let mut req = if let Some(pl) = inner.payload { let mut req = if let Some(pl) = inner.payload {
Request::with_payload(pl) Request::with_payload(pl)

View File

@ -32,8 +32,12 @@ pub enum UrlencodedError {
#[display(fmt = "Can not decode chunked transfer encoding")] #[display(fmt = "Can not decode chunked transfer encoding")]
Chunked, Chunked,
/// Payload size is bigger than allowed. (default: 256kB) /// Payload size is bigger than allowed. (default: 256kB)
#[display(fmt = "Urlencoded payload size is bigger than allowed (default: 256kB)")] #[display(
Overflow, fmt = "Urlencoded payload size is bigger ({} bytes) than allowed (default: {} bytes)",
size,
limit
)]
Overflow { size: usize, limit: usize },
/// Payload size is now known /// Payload size is now known
#[display(fmt = "Payload size is now known")] #[display(fmt = "Payload size is now known")]
UnknownLength, UnknownLength,
@ -52,7 +56,7 @@ pub enum UrlencodedError {
impl ResponseError for UrlencodedError { impl ResponseError for UrlencodedError {
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
match *self { match *self {
UrlencodedError::Overflow => { UrlencodedError::Overflow { .. } => {
HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE) HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE)
} }
UrlencodedError::UnknownLength => { UrlencodedError::UnknownLength => {
@ -164,7 +168,8 @@ mod tests {
#[test] #[test]
fn test_urlencoded_error() { fn test_urlencoded_error() {
let resp: HttpResponse = UrlencodedError::Overflow.error_response(); let resp: HttpResponse =
UrlencodedError::Overflow { size: 0, limit: 0 }.error_response();
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE); assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
let resp: HttpResponse = UrlencodedError::UnknownLength.error_response(); let resp: HttpResponse = UrlencodedError::UnknownLength.error_response();
assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED); assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED);

View File

@ -68,6 +68,34 @@ impl ServiceRequest {
(self.0, pl) (self.0, pl)
} }
/// Construct request from parts.
///
/// `ServiceRequest` can be re-constructed only if `req` hasnt been cloned.
pub fn from_parts(
mut req: HttpRequest,
pl: Payload,
) -> Result<Self, (HttpRequest, Payload)> {
if Rc::strong_count(&req.0) == 1 && Rc::weak_count(&req.0) == 0 {
Rc::get_mut(&mut req.0).unwrap().payload = pl;
Ok(ServiceRequest(req))
} else {
Err((req, pl))
}
}
/// Construct request from request.
///
/// `HttpRequest` implements `Clone` trait via `Rc` type. `ServiceRequest`
/// can be re-constructed only if rc's strong pointers count eq 1 and
/// weak pointers count is 0.
pub fn from_request(req: HttpRequest) -> Result<Self, HttpRequest> {
if Rc::strong_count(&req.0) == 1 && Rc::weak_count(&req.0) == 0 {
Ok(ServiceRequest(req))
} else {
Err(req)
}
}
/// Create service response /// Create service response
#[inline] #[inline]
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> { pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
@ -514,6 +542,27 @@ mod tests {
use crate::test::{call_service, init_service, TestRequest}; use crate::test::{call_service, init_service, TestRequest};
use crate::{guard, http, web, App, HttpResponse}; use crate::{guard, http, web, App, HttpResponse};
#[test]
fn test_service_request() {
let req = TestRequest::default().to_srv_request();
let (r, pl) = req.into_parts();
assert!(ServiceRequest::from_parts(r, pl).is_ok());
let req = TestRequest::default().to_srv_request();
let (r, pl) = req.into_parts();
let _r2 = r.clone();
assert!(ServiceRequest::from_parts(r, pl).is_err());
let req = TestRequest::default().to_srv_request();
let (r, _pl) = req.into_parts();
assert!(ServiceRequest::from_request(r).is_ok());
let req = TestRequest::default().to_srv_request();
let (r, _pl) = req.into_parts();
let _r2 = r.clone();
assert!(ServiceRequest::from_request(r).is_err());
}
#[test] #[test]
fn test_service() { fn test_service() {
let mut srv = init_service( let mut srv = init_service(

View File

@ -318,7 +318,7 @@ where
let limit = self.limit; let limit = self.limit;
if let Some(len) = self.length.take() { if let Some(len) = self.length.take() {
if len > limit { if len > limit {
return Err(UrlencodedError::Overflow); return Err(UrlencodedError::Overflow { size: len, limit });
} }
} }
@ -331,7 +331,10 @@ where
.from_err() .from_err()
.fold(BytesMut::with_capacity(8192), move |mut body, chunk| { .fold(BytesMut::with_capacity(8192), move |mut body, chunk| {
if (body.len() + chunk.len()) > limit { if (body.len() + chunk.len()) > limit {
Err(UrlencodedError::Overflow) Err(UrlencodedError::Overflow {
size: body.len() + chunk.len(),
limit,
})
} else { } else {
body.extend_from_slice(&chunk); body.extend_from_slice(&chunk);
Ok(body) Ok(body)
@ -390,8 +393,8 @@ mod tests {
fn eq(err: UrlencodedError, other: UrlencodedError) -> bool { fn eq(err: UrlencodedError, other: UrlencodedError) -> bool {
match err { match err {
UrlencodedError::Overflow => match other { UrlencodedError::Overflow { .. } => match other {
UrlencodedError::Overflow => true, UrlencodedError::Overflow { .. } => true,
_ => false, _ => false,
}, },
UrlencodedError::UnknownLength => match other { UrlencodedError::UnknownLength => match other {
@ -420,7 +423,10 @@ mod tests {
.header(CONTENT_LENGTH, "1000000") .header(CONTENT_LENGTH, "1000000")
.to_http_parts(); .to_http_parts();
let info = block_on(UrlEncoded::<Info>::new(&req, &mut pl)); let info = block_on(UrlEncoded::<Info>::new(&req, &mut pl));
assert!(eq(info.err().unwrap(), UrlencodedError::Overflow)); assert!(eq(
info.err().unwrap(),
UrlencodedError::Overflow { size: 0, limit: 0 }
));
let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain") let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain")
.header(CONTENT_LENGTH, "10") .header(CONTENT_LENGTH, "10")

View File

@ -1,10 +1,15 @@
# Changes # Changes
## [0.2.5] - 2019-0917
### Changed ### Changed
* Update serde_urlencoded to "0.6.1" * Update serde_urlencoded to "0.6.1"
### Fixed
* Do not override current `System`
## [0.2.4] - 2019-07-18 ## [0.2.4] - 2019-07-18

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-http-test" name = "actix-http-test"
version = "0.2.4" version = "0.2.5"
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"
@ -35,7 +35,7 @@ actix-rt = "0.2.2"
actix-service = "0.4.1" actix-service = "0.4.1"
actix-server = "0.6.0" actix-server = "0.6.0"
actix-utils = "0.4.1" actix-utils = "0.4.1"
awc = "0.2.2" awc = "0.2.6"
actix-connect = "0.2.2" actix-connect = "0.2.2"
base64 = "0.10" base64 = "0.10"
@ -56,5 +56,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" actix-web = "1.0.7"
actix-http = "0.2.4" actix-http = "0.2.9"

View File

@ -103,8 +103,8 @@ pub struct TestServer;
/// Test server controller /// Test server controller
pub struct TestServerRuntime { pub struct TestServerRuntime {
addr: net::SocketAddr, addr: net::SocketAddr,
rt: Runtime,
client: Client, client: Client,
system: System,
} }
impl TestServer { impl TestServer {
@ -130,45 +130,47 @@ impl TestServer {
}); });
let (system, addr) = rx.recv().unwrap(); let (system, addr) = rx.recv().unwrap();
let mut rt = Runtime::new().unwrap();
let client = rt let client = block_on(lazy(move || {
.block_on(lazy(move || { let connector = {
let connector = { #[cfg(feature = "ssl")]
#[cfg(feature = "ssl")] {
{ use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
let mut builder = let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
SslConnector::builder(SslMethod::tls()).unwrap(); builder.set_verify(SslVerifyMode::NONE);
builder.set_verify(SslVerifyMode::NONE); let _ = builder
let _ = builder.set_alpn_protos(b"\x02h2\x08http/1.1").map_err( .set_alpn_protos(b"\x02h2\x08http/1.1")
|e| log::error!("Can not set alpn protocol: {:?}", e), .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
); Connector::new()
Connector::new() .conn_lifetime(time::Duration::from_secs(0))
.conn_lifetime(time::Duration::from_secs(0)) .timeout(time::Duration::from_millis(500))
.timeout(time::Duration::from_millis(500)) .ssl(builder.build())
.ssl(builder.build()) .finish()
.finish() }
} #[cfg(not(feature = "ssl"))]
#[cfg(not(feature = "ssl"))] {
{ Connector::new()
Connector::new() .conn_lifetime(time::Duration::from_secs(0))
.conn_lifetime(time::Duration::from_secs(0)) .timeout(time::Duration::from_millis(500))
.timeout(time::Duration::from_millis(500)) .finish()
.finish() }
} };
};
Ok::<Client, ()>(Client::build().connector(connector).finish()) Ok::<Client, ()>(Client::build().connector(connector).finish())
})) }))
.unwrap(); .unwrap();
rt.block_on(lazy(
block_on(lazy(
|| Ok::<_, ()>(actix_connect::start_default_resolver()), || Ok::<_, ()>(actix_connect::start_default_resolver()),
)) ))
.unwrap(); .unwrap();
System::set_current(system);
TestServerRuntime { addr, rt, client } TestServerRuntime {
addr,
client,
system,
}
} }
/// Get first available unused address /// Get first available unused address
@ -188,7 +190,7 @@ impl TestServerRuntime {
where where
F: Future<Item = I, Error = E>, F: Future<Item = I, Error = E>,
{ {
self.rt.block_on(fut) block_on(fut)
} }
/// Execute future on current core /// Execute future on current core
@ -197,7 +199,7 @@ impl TestServerRuntime {
F: FnOnce() -> R, F: FnOnce() -> R,
R: Future, R: Future,
{ {
self.rt.block_on(lazy(f)) block_on(lazy(f))
} }
/// Execute function on current core /// Execute function on current core
@ -205,7 +207,7 @@ impl TestServerRuntime {
where where
F: FnOnce() -> R, F: FnOnce() -> R,
{ {
self.rt.block_on(lazy(|| Ok::<_, ()>(fut()))).unwrap() block_on(lazy(|| Ok::<_, ()>(fut()))).unwrap()
} }
/// Construct test server url /// Construct test server url
@ -324,8 +326,7 @@ impl TestServerRuntime {
{ {
let url = self.url(path); let url = self.url(path);
let connect = self.client.ws(url).connect(); let connect = self.client.ws(url).connect();
self.rt block_on(lazy(move || connect.map(|(_, framed)| framed)))
.block_on(lazy(move || connect.map(|(_, framed)| framed)))
} }
/// Connect to a websocket server /// Connect to a websocket server
@ -338,7 +339,7 @@ impl TestServerRuntime {
/// Stop http server /// Stop http server
fn stop(&mut self) { fn stop(&mut self) {
System::current().stop(); self.system.stop();
} }
} }