mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
allow to set/change responses content encoding
This commit is contained in:
parent
2379bcbf39
commit
994d0afd80
@ -4,11 +4,12 @@ version = "0.3.0"
|
|||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix web framework"
|
description = "Actix web framework"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["actor", "http", "web"]
|
keywords = ["actix", "actor", "http", "web", "async", "tokio", "futures", "web"]
|
||||||
homepage = "https://github.com/actix/actix-web"
|
homepage = "https://github.com/actix/actix-web"
|
||||||
repository = "https://github.com/actix/actix-web.git"
|
repository = "https://github.com/actix/actix-web.git"
|
||||||
documentation = "https://docs.rs/actix-web/"
|
documentation = "https://docs.rs/actix-web/"
|
||||||
categories = ["network-programming", "asynchronous"]
|
categories = ["network-programming", "asynchronous",
|
||||||
|
"web-programming::http-server", "web-programming::websocket"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
|
@ -26,15 +26,15 @@ pub enum ConnectionType {
|
|||||||
/// Represents various types of connection
|
/// Represents various types of connection
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub enum ContentEncoding {
|
pub enum ContentEncoding {
|
||||||
/// Auto
|
/// Automatically select encoding based on encoding negotiation
|
||||||
Auto,
|
Auto,
|
||||||
/// Brotli
|
/// A format using the Brotli algorithm
|
||||||
Br,
|
Br,
|
||||||
/// Deflate
|
/// A format using the zlib structure with deflate algorithm
|
||||||
Deflate,
|
Deflate,
|
||||||
/// Gzip
|
/// Gzip algorithm
|
||||||
Gzip,
|
Gzip,
|
||||||
/// Identity
|
/// Indicates the identity function (i.e. no compression, nor modification)
|
||||||
Identity,
|
Identity,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,6 +199,17 @@ impl HttpResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Content encoding
|
||||||
|
pub fn content_encoding(&self) -> &ContentEncoding {
|
||||||
|
&self.encoding
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set content encoding
|
||||||
|
pub fn set_content_encoding(&mut self, enc: ContentEncoding) -> &mut Self {
|
||||||
|
self.encoding = enc;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Get body os this response
|
/// Get body os this response
|
||||||
pub fn body(&self) -> &Body {
|
pub fn body(&self) -> &Body {
|
||||||
&self.body
|
&self.body
|
||||||
@ -320,7 +331,8 @@ impl HttpResponseBuilder {
|
|||||||
/// Set content encoding.
|
/// Set content encoding.
|
||||||
///
|
///
|
||||||
/// By default `ContentEncoding::Auto` is used, which automatically
|
/// By default `ContentEncoding::Auto` is used, which automatically
|
||||||
/// determine content encoding based on request `Accept-Encoding` headers.
|
/// negotiates content encoding based on request's `Accept-Encoding` headers.
|
||||||
|
/// To enforce specific encodnign other `ContentEncoding` could be used.
|
||||||
pub fn content_encoding(&mut self, enc: ContentEncoding) -> &mut Self {
|
pub fn content_encoding(&mut self, enc: ContentEncoding) -> &mut Self {
|
||||||
if let Some(parts) = parts(&mut self.parts, &self.err) {
|
if let Some(parts) = parts(&mut self.parts, &self.err) {
|
||||||
parts.encoding = enc;
|
parts.encoding = enc;
|
||||||
@ -475,4 +487,14 @@ mod tests {
|
|||||||
.content_type("text/plain").body(Body::Empty).unwrap();
|
.content_type("text/plain").body(Body::Empty).unwrap();
|
||||||
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/plain")
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/plain")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_content_encoding() {
|
||||||
|
let resp = HttpResponse::builder(StatusCode::OK).finish().unwrap();
|
||||||
|
assert_eq!(*resp.content_encoding(), ContentEncoding::Auto);
|
||||||
|
|
||||||
|
let resp = HttpResponse::builder(StatusCode::OK)
|
||||||
|
.content_encoding(ContentEncoding::Br).finish().unwrap();
|
||||||
|
assert_eq!(*resp.content_encoding(), ContentEncoding::Br);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user