1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

fix Bodyencoding trait usage

This commit is contained in:
Nikolay Kim 2019-12-18 09:30:14 +06:00
parent 3b860ebdc7
commit 1732ae8c79
7 changed files with 24 additions and 21 deletions

View File

@ -268,7 +268,7 @@ impl NamedFile {
); );
}); });
if let Some(current_encoding) = self.encoding { if let Some(current_encoding) = self.encoding {
resp.set_encoding(current_encoding); resp.encoding(current_encoding);
} }
let reader = ChunkedReadFile { let reader = ChunkedReadFile {
size: self.md.len(), size: self.md.len(),
@ -335,7 +335,7 @@ impl NamedFile {
}); });
// default compressing // default compressing
if let Some(current_encoding) = self.encoding { if let Some(current_encoding) = self.encoding {
resp.set_encoding(current_encoding); resp.encoding(current_encoding);
} }
resp.if_some(last_modified, |lm, resp| { resp.if_some(last_modified, |lm, resp| {
@ -356,7 +356,7 @@ impl NamedFile {
if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) { if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) {
length = rangesvec[0].length; length = rangesvec[0].length;
offset = rangesvec[0].start; offset = rangesvec[0].start;
resp.set_encoding(ContentEncoding::Identity); resp.encoding(ContentEncoding::Identity);
resp.header( resp.header(
header::CONTENT_RANGE, header::CONTENT_RANGE,
format!( format!(

View File

@ -15,8 +15,9 @@ use rand::Rng;
use actix_http::HttpService; use actix_http::HttpService;
use actix_http_test::test_server; use actix_http_test::test_server;
use actix_service::pipeline_factory; use actix_service::pipeline_factory;
use actix_web::dev::BodyEncoding;
use actix_web::http::Cookie; use actix_web::http::Cookie;
use actix_web::middleware::{BodyEncoding, Compress}; use actix_web::middleware::Compress;
use actix_web::{ use actix_web::{
http::header, test, web, App, Error, HttpMessage, HttpRequest, HttpResponse, http::header, test, web, App, Error, HttpMessage, HttpRequest, HttpResponse,
}; };

View File

@ -6,7 +6,6 @@ use url::ParseError as UrlParseError;
use crate::http::StatusCode; use crate::http::StatusCode;
use crate::HttpResponse; use crate::HttpResponse;
use serde_urlencoded::de;
/// Errors which can occur when attempting to generate resource uri. /// Errors which can occur when attempting to generate resource uri.
#[derive(Debug, PartialEq, Display, From)] #[derive(Debug, PartialEq, Display, From)]
@ -97,7 +96,7 @@ impl ResponseError for JsonPayloadError {
pub enum PathError { pub enum PathError {
/// Deserialize error /// Deserialize error
#[display(fmt = "Path deserialize error: {}", _0)] #[display(fmt = "Path deserialize error: {}", _0)]
Deserialize(de::Error), Deserialize(serde::de::value::Error),
} }
/// Return `BadRequest` for `PathError` /// Return `BadRequest` for `PathError`
@ -112,7 +111,7 @@ impl ResponseError for PathError {
pub enum QueryPayloadError { pub enum QueryPayloadError {
/// Deserialize error /// Deserialize error
#[display(fmt = "Query deserialize error: {}", _0)] #[display(fmt = "Query deserialize error: {}", _0)]
Deserialize(de::Error), Deserialize(serde::de::value::Error),
} }
/// Return `BadRequest` for `QueryPayloadError` /// Return `BadRequest` for `QueryPayloadError`

View File

@ -166,13 +166,15 @@ pub mod dev {
/// Helper trait that allows to set specific encoding for response. /// Helper trait that allows to set specific encoding for response.
pub trait BodyEncoding { pub trait BodyEncoding {
fn encoding(&self) -> Option<ContentEncoding>; /// Get content encoding
fn get_encoding(&self) -> Option<ContentEncoding>;
fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self; /// Set content encoding
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
} }
impl BodyEncoding for ResponseBuilder { impl BodyEncoding for ResponseBuilder {
fn encoding(&self) -> Option<ContentEncoding> { fn get_encoding(&self) -> Option<ContentEncoding> {
if let Some(ref enc) = self.extensions().get::<Enc>() { if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0) Some(enc.0)
} else { } 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.extensions_mut().insert(Enc(encoding));
self self
} }
} }
impl<B> BodyEncoding for Response<B> { impl<B> BodyEncoding for Response<B> {
fn encoding(&self) -> Option<ContentEncoding> { fn get_encoding(&self) -> Option<ContentEncoding> {
if let Some(ref enc) = self.extensions().get::<Enc>() { if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0) Some(enc.0)
} else { } 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.extensions_mut().insert(Enc(encoding));
self self
} }

View File

@ -135,7 +135,7 @@ where
match futures::ready!(this.fut.poll(cx)) { match futures::ready!(this.fut.poll(cx)) {
Ok(resp) => { Ok(resp) => {
let enc = if let Some(enc) = resp.response().encoding() { let enc = if let Some(enc) = resp.response().get_encoding() {
enc enc
} else { } else {
*this.encoding *this.encoding

View File

@ -12,8 +12,9 @@ use flate2::Compression;
use futures::{future::ok, stream::once}; use futures::{future::ok, stream::once};
use rand::{distributions::Alphanumeric, Rng}; use rand::{distributions::Alphanumeric, Rng};
use actix_web::middleware::{BodyEncoding, Compress}; use actix_web::dev::BodyEncoding;
use actix_web::{dev, http, test, web, App, Error, HttpResponse}; 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 \ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
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 || { let srv = test::start_with(test::config().openssl(builder.build()), move || {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| { App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
HttpResponse::Ok() HttpResponse::Ok()
.encoding(http::ContentEncoding::Identity) .encoding(actix_web::http::ContentEncoding::Identity)
.body(bytes) .body(bytes)
}))) })))
}); });
@ -681,7 +682,7 @@ async fn test_brotli_encoding_large_openssl() {
// client request // client request
let mut response = srv let mut response = srv
.post("/") .post("/")
.header(http::header::CONTENT_ENCODING, "br") .header(actix_web::http::header::CONTENT_ENCODING, "br")
.send_body(enc) .send_body(enc)
.await .await
.unwrap(); .unwrap();
@ -716,7 +717,7 @@ async fn test_reading_deflate_encoding_large_random_rustls() {
let srv = test::start_with(test::config().rustls(config), || { let srv = test::start_with(test::config().rustls(config), || {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| { App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
HttpResponse::Ok() HttpResponse::Ok()
.encoding(http::ContentEncoding::Identity) .encoding(actix_web::http::ContentEncoding::Identity)
.body(bytes) .body(bytes)
}))) })))
}); });
@ -729,7 +730,7 @@ async fn test_reading_deflate_encoding_large_random_rustls() {
// client request // client request
let req = srv let req = srv
.post("/") .post("/")
.header(http::header::CONTENT_ENCODING, "deflate") .header(actix_web::http::header::CONTENT_ENCODING, "deflate")
.send_body(enc); .send_body(enc);
let mut response = req.await.unwrap(); let mut response = req.await.unwrap();