1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00
This commit is contained in:
Nikolay Kim 2017-12-03 20:09:46 -08:00
parent 57c53bd2a0
commit d35be02587
2 changed files with 15 additions and 19 deletions

View File

@ -3,7 +3,7 @@
## Content encoding ## Content encoding
Actix automatically *compress*/*decompress* payload. Actix automatically *compress*/*decompress* payload.
Following encodings are supported: Following codecs are supported:
* Brotli * Brotli
* Gzip * Gzip
@ -13,17 +13,23 @@ Following encodings are supported:
If request headers contains `Content-Encoding` header, request payload get decompressed If request headers contains `Content-Encoding` header, request payload get decompressed
according to header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`. according to header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`.
Response payload get compressed based on `content_encoding` settings. Response payload get compressed based on *content_encoding* parameter.
If `ContentEncoding::Auto` is selected then compression depends on request's By default `ContentEncoding::Auto` is used. If `ContentEncoding::Auto` is selected
`Accept-Encoding` header. `ContentEncoding::Identity` could be used to disable compression. then compression depends on request's `Accept-Encoding` header.
If other content encoding is selected the compression is enforced. `ContentEncoding::Identity` could be used to disable compression.
If other content encoding is selected the compression is enforced for this codec. For example,
to enable `brotli` response's body compression use `ContentEncoding::Br`:
```rust
extern crate actix_web;
use actix_web::*;
```rust,ignore
fn index(req: HttpRequest) -> HttpResponse { fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok() HttpResponse::Ok()
.content_encoding(ContentEncoding::Br) .content_encoding(ContentEncoding::Br)
.body(Body) .body("data").unwrap()
} }
# fn main() {}
``` ```
## JSON Response ## JSON Response

View File

@ -1,5 +1,5 @@
//! Pieces pertaining to the HTTP response. //! Pieces pertaining to the HTTP response.
use std::{io, mem, str, fmt}; use std::{mem, str, fmt};
use std::convert::Into; use std::convert::Into;
use cookie::CookieJar; use cookie::CookieJar;
@ -157,17 +157,6 @@ impl HttpResponse {
self.chunked self.chunked
} }
/// Enables automatic chunked transfer encoding
pub fn enable_chunked_encoding(&mut self) -> Result<(), io::Error> {
if self.headers.contains_key(header::CONTENT_LENGTH) {
Err(io::Error::new(io::ErrorKind::Other,
"You can't enable chunked encoding when a content length is set"))
} else {
self.chunked = true;
Ok(())
}
}
/// Content encoding /// Content encoding
pub fn content_encoding(&self) -> &ContentEncoding { pub fn content_encoding(&self) -> &ContentEncoding {
&self.encoding &self.encoding
@ -597,6 +586,7 @@ mod tests {
fn test_basic_builder() { fn test_basic_builder() {
let resp = HttpResponse::Ok() let resp = HttpResponse::Ok()
.status(StatusCode::NO_CONTENT) .status(StatusCode::NO_CONTENT)
.header("X-TEST", "value")
.version(Version::HTTP_10) .version(Version::HTTP_10)
.finish().unwrap(); .finish().unwrap();
assert_eq!(resp.version(), Some(Version::HTTP_10)); assert_eq!(resp.version(), Some(Version::HTTP_10));