mirror of
https://github.com/actix/actix-web.git
synced 2024-12-30 03:50:42 +00:00
fix Bodyencoding trait usage
This commit is contained in:
parent
3b860ebdc7
commit
1732ae8c79
7 changed files with 24 additions and 21 deletions
|
@ -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!(
|
||||
|
|
|
@ -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| {
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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`
|
||||
|
|
14
src/lib.rs
14
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<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 {
|
||||
fn encoding(&self) -> Option<ContentEncoding> {
|
||||
fn get_encoding(&self) -> Option<ContentEncoding> {
|
||||
if let Some(ref enc) = self.extensions().get::<Enc>() {
|
||||
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<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>() {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue