add vary header to compressed stream

This commit is contained in:
Rob Ede 2022-01-02 11:32:43 +00:00
parent b540c4c9b8
commit fb63ebe5f7
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 7 additions and 4 deletions

View File

@ -27,7 +27,7 @@ use super::Writer;
use crate::{ use crate::{
body::{self, BodySize, MessageBody}, body::{self, BodySize, MessageBody},
error::BlockingError, error::BlockingError,
header::{self, ContentEncoding, CONTENT_ENCODING}, header::{self, ContentEncoding, HeaderValue, CONTENT_ENCODING},
ResponseHead, StatusCode, ResponseHead, StatusCode,
}; };
@ -56,7 +56,7 @@ impl<B: MessageBody> Encoder<B> {
} }
pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self { pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self {
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING) let should_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|| head.status == StatusCode::SWITCHING_PROTOCOLS || head.status == StatusCode::SWITCHING_PROTOCOLS
|| head.status == StatusCode::NO_CONTENT || head.status == StatusCode::NO_CONTENT
|| encoding == ContentEncoding::Identity); || encoding == ContentEncoding::Identity);
@ -71,8 +71,8 @@ impl<B: MessageBody> Encoder<B> {
Err(body) => EncoderBody::Stream { body }, Err(body) => EncoderBody::Stream { body },
}; };
if can_encode { if should_encode {
// Modify response body only if encoder is set // wrap body only if encoder is feature-enabled
if let Some(enc) = ContentEncoder::encoder(encoding) { if let Some(enc) = ContentEncoder::encoder(encoding) {
update_head(encoding, head); update_head(encoding, head);
@ -253,6 +253,8 @@ where
fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) { fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
head.headers_mut() head.headers_mut()
.insert(header::CONTENT_ENCODING, encoding.to_header_value()); .insert(header::CONTENT_ENCODING, encoding.to_header_value());
head.headers_mut()
.insert(header::VARY, HeaderValue::from_static("accept-encoding"));
head.no_chunking(false); head.no_chunking(false);
} }

View File

@ -250,6 +250,7 @@ impl From<ParseError> for Response<BoxBody> {
/// A set of errors that can occur running blocking tasks in thread pool. /// A set of errors that can occur running blocking tasks in thread pool.
#[derive(Debug, Display, Error)] #[derive(Debug, Display, Error)]
#[display(fmt = "Blocking thread pool is gone")] #[display(fmt = "Blocking thread pool is gone")]
// TODO: non-exhaustive
pub struct BlockingError; pub struct BlockingError;
/// A set of errors that can occur during payload parsing. /// A set of errors that can occur during payload parsing.