From 1732ae8c79efe46df294e6b64a80ef8ddfbaa719 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 18 Dec 2019 09:30:14 +0600 Subject: [PATCH] fix Bodyencoding trait usage --- actix-files/src/named.rs | 6 +++--- actix-http/src/h1/service.rs | 2 +- awc/tests/test_client.rs | 3 ++- src/error.rs | 5 ++--- src/lib.rs | 14 ++++++++------ src/middleware/compress.rs | 2 +- tests/test_server.rs | 13 +++++++------ 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index 3d9398b4..fdb05599 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -268,7 +268,7 @@ impl NamedFile { ); }); if let Some(current_encoding) = self.encoding { - resp.set_encoding(current_encoding); + resp.encoding(current_encoding); } let reader = ChunkedReadFile { size: self.md.len(), @@ -335,7 +335,7 @@ impl NamedFile { }); // default compressing if let Some(current_encoding) = self.encoding { - resp.set_encoding(current_encoding); + resp.encoding(current_encoding); } resp.if_some(last_modified, |lm, resp| { @@ -356,7 +356,7 @@ impl NamedFile { if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) { length = rangesvec[0].length; offset = rangesvec[0].start; - resp.set_encoding(ContentEncoding::Identity); + resp.encoding(ContentEncoding::Identity); resp.header( header::CONTENT_RANGE, format!( diff --git a/actix-http/src/h1/service.rs b/actix-http/src/h1/service.rs index beb577f9..22f9d03b 100644 --- a/actix-http/src/h1/service.rs +++ b/actix-http/src/h1/service.rs @@ -440,7 +440,7 @@ where })? .is_ready() && ready; - + let ready = if let Some(ref mut upg) = self.upgrade { upg.poll_ready(cx) .map_err(|e| { diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index 73a4696d..48c23cfa 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -15,8 +15,9 @@ use rand::Rng; use actix_http::HttpService; use actix_http_test::test_server; use actix_service::pipeline_factory; +use actix_web::dev::BodyEncoding; use actix_web::http::Cookie; -use actix_web::middleware::{BodyEncoding, Compress}; +use actix_web::middleware::Compress; use actix_web::{ http::header, test, web, App, Error, HttpMessage, HttpRequest, HttpResponse, }; diff --git a/src/error.rs b/src/error.rs index 2eec7c51..31f6b9c5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,7 +6,6 @@ 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)] @@ -97,7 +96,7 @@ impl ResponseError for JsonPayloadError { pub enum PathError { /// Deserialize error #[display(fmt = "Path deserialize error: {}", _0)] - Deserialize(de::Error), + Deserialize(serde::de::value::Error), } /// Return `BadRequest` for `PathError` @@ -112,7 +111,7 @@ impl ResponseError for PathError { pub enum QueryPayloadError { /// Deserialize error #[display(fmt = "Query deserialize error: {}", _0)] - Deserialize(de::Error), + Deserialize(serde::de::value::Error), } /// Return `BadRequest` for `QueryPayloadError` diff --git a/src/lib.rs b/src/lib.rs index d2f501e8..bc89c645 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,13 +166,15 @@ pub mod dev { /// Helper trait that allows to set specific encoding for response. pub trait BodyEncoding { - fn encoding(&self) -> Option; + /// Get content encoding + fn get_encoding(&self) -> Option; - fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self; + /// Set content encoding + fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self; } impl BodyEncoding for ResponseBuilder { - fn encoding(&self) -> Option { + fn get_encoding(&self) -> Option { if let Some(ref enc) = self.extensions().get::() { Some(enc.0) } else { @@ -180,14 +182,14 @@ pub mod dev { } } - fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self { + fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self { self.extensions_mut().insert(Enc(encoding)); self } } impl BodyEncoding for Response { - fn encoding(&self) -> Option { + fn get_encoding(&self) -> Option { if let Some(ref enc) = self.extensions().get::() { Some(enc.0) } else { @@ -195,7 +197,7 @@ pub mod dev { } } - fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self { + fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self { self.extensions_mut().insert(Enc(encoding)); self } diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index c55c741e..70006ab3 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -135,7 +135,7 @@ where match futures::ready!(this.fut.poll(cx)) { Ok(resp) => { - let enc = if let Some(enc) = resp.response().encoding() { + let enc = if let Some(enc) = resp.response().get_encoding() { enc } else { *this.encoding diff --git a/tests/test_server.rs b/tests/test_server.rs index ca4aff63..01137522 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -12,8 +12,9 @@ use flate2::Compression; use futures::{future::ok, stream::once}; use rand::{distributions::Alphanumeric, Rng}; -use actix_web::middleware::{BodyEncoding, Compress}; -use actix_web::{dev, http, test, web, App, Error, HttpResponse}; +use actix_web::dev::BodyEncoding; +use actix_web::middleware::Compress; +use actix_web::{dev, test, web, App, Error, HttpResponse}; const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ @@ -668,7 +669,7 @@ async fn test_brotli_encoding_large_openssl() { let srv = test::start_with(test::config().openssl(builder.build()), move || { App::new().service(web::resource("/").route(web::to(|bytes: Bytes| { HttpResponse::Ok() - .encoding(http::ContentEncoding::Identity) + .encoding(actix_web::http::ContentEncoding::Identity) .body(bytes) }))) }); @@ -681,7 +682,7 @@ async fn test_brotli_encoding_large_openssl() { // client request let mut response = srv .post("/") - .header(http::header::CONTENT_ENCODING, "br") + .header(actix_web::http::header::CONTENT_ENCODING, "br") .send_body(enc) .await .unwrap(); @@ -716,7 +717,7 @@ async fn test_reading_deflate_encoding_large_random_rustls() { let srv = test::start_with(test::config().rustls(config), || { App::new().service(web::resource("/").route(web::to(|bytes: Bytes| { HttpResponse::Ok() - .encoding(http::ContentEncoding::Identity) + .encoding(actix_web::http::ContentEncoding::Identity) .body(bytes) }))) }); @@ -729,7 +730,7 @@ async fn test_reading_deflate_encoding_large_random_rustls() { // client request let req = srv .post("/") - .header(http::header::CONTENT_ENCODING, "deflate") + .header(actix_web::http::header::CONTENT_ENCODING, "deflate") .send_body(enc); let mut response = req.await.unwrap();