mirror of https://github.com/fafhrd91/actix-web
fix behavior change for audio file
This commit is contained in:
parent
7cee116afd
commit
6c5ef0fada
|
|
@ -714,3 +714,14 @@ impl HttpServiceFactory for NamedFile {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_files_use_inline_content_disposition() {
|
||||||
|
let (_ct, cd) = get_content_type_and_disposition(Path::new("sound.mp3")).unwrap();
|
||||||
|
assert_eq!(cd.disposition, DispositionType::Inline);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -197,9 +197,9 @@ impl Service<ServiceRequest> for FilesService {
|
||||||
if let Some((named_file, encoding)) =
|
if let Some((named_file, encoding)) =
|
||||||
find_compressed(&req, &named_path).await
|
find_compressed(&req, &named_path).await
|
||||||
{
|
{
|
||||||
return Ok(this.serve_named_file_with_encoding(
|
return Ok(
|
||||||
req, named_file, encoding,
|
this.serve_named_file_with_encoding(req, named_file, encoding)
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fallback to the uncompressed version
|
// fallback to the uncompressed version
|
||||||
|
|
@ -246,7 +246,7 @@ async fn find_compressed(
|
||||||
original_path: &Path,
|
original_path: &Path,
|
||||||
) -> Option<(NamedFile, header::ContentEncoding)> {
|
) -> Option<(NamedFile, header::ContentEncoding)> {
|
||||||
use actix_web::HttpMessage;
|
use actix_web::HttpMessage;
|
||||||
use header::{ContentEncoding, Encoding, Preference};
|
use header::{AcceptEncoding, ContentEncoding, Encoding};
|
||||||
|
|
||||||
// Retrieve the content type and content disposition based on the original filename. If we
|
// Retrieve the content type and content disposition based on the original filename. If we
|
||||||
// can't get these successfully, don't even try to find a compressed file.
|
// can't get these successfully, don't even try to find a compressed file.
|
||||||
|
|
@ -256,52 +256,59 @@ async fn find_compressed(
|
||||||
Err(_) => return None,
|
Err(_) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let accept_encoding = req.get_header::<actix_web::http::header::AcceptEncoding>();
|
let accept_encoding = req.get_header::<AcceptEncoding>()?;
|
||||||
let ranked_encodings = accept_encoding
|
|
||||||
.map(|encodings| encodings.ranked())
|
|
||||||
.unwrap_or_else(Vec::new);
|
|
||||||
|
|
||||||
let ranked_encodings_iter = ranked_encodings
|
let mut supported = SUPPORTED_PRECOMPRESSION_ENCODINGS
|
||||||
.into_iter()
|
.iter()
|
||||||
.filter_map(|e| match e {
|
.copied()
|
||||||
Preference::Any | Preference::Specific(Encoding::Unknown(_)) => None,
|
.map(Encoding::Known)
|
||||||
Preference::Specific(Encoding::Known(encoding)) => Some(encoding),
|
.collect::<Vec<_>>();
|
||||||
})
|
|
||||||
.filter_map(|encoding| {
|
// Only move the original content-type/disposition into the chosen compressed file once.
|
||||||
if !SUPPORTED_PRECOMPRESSION_ENCODINGS.contains(&encoding) {
|
let mut content_type = Some(content_type);
|
||||||
return None;
|
let mut content_disposition = Some(content_disposition);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// Select next acceptable encoding (honouring q=0 rejections) from remaining supported set.
|
||||||
|
let chosen = accept_encoding.negotiate(supported.iter())?;
|
||||||
|
|
||||||
|
let encoding = match chosen {
|
||||||
|
Encoding::Known(enc) => enc,
|
||||||
|
// No supported encoding should ever be unknown here.
|
||||||
|
Encoding::Unknown(_) => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Identity indicates there is no acceptable pre-compressed representation.
|
||||||
|
if encoding == ContentEncoding::Identity {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let extension = match encoding {
|
||||||
|
ContentEncoding::Brotli => ".br",
|
||||||
|
ContentEncoding::Gzip => ".gz",
|
||||||
|
ContentEncoding::Zstd => ".zst",
|
||||||
|
ContentEncoding::Identity => unreachable!(),
|
||||||
|
// Only variants in SUPPORTED_PRECOMPRESSION_ENCODINGS can occur here.
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut compressed_path = original_path.to_owned();
|
||||||
|
let filename = match compressed_path.file_name().and_then(|name| name.to_str()) {
|
||||||
|
Some(filename) => filename.to_owned(),
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
compressed_path.set_file_name(filename + extension);
|
||||||
|
|
||||||
|
match NamedFile::open_async(&compressed_path).await {
|
||||||
|
Ok(mut named_file) => {
|
||||||
|
named_file.content_type = content_type.take().unwrap();
|
||||||
|
named_file.content_disposition = content_disposition.take().unwrap();
|
||||||
|
return Some((named_file, encoding));
|
||||||
|
}
|
||||||
|
// Ignore errors while searching disk for a suitable encoding.
|
||||||
|
Err(_) => {
|
||||||
|
supported.retain(|enc| enc != &chosen);
|
||||||
}
|
}
|
||||||
let extension = match encoding {
|
|
||||||
ContentEncoding::Brotli => Some(".br"),
|
|
||||||
ContentEncoding::Gzip => Some(".gz"),
|
|
||||||
ContentEncoding::Zstd => Some(".zst"),
|
|
||||||
ContentEncoding::Identity => None,
|
|
||||||
// Only variants in SUPPORTED_PRECOMPRESSION_ENCODINGS can occur here
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
let path = match extension {
|
|
||||||
Some(extension) => {
|
|
||||||
let mut compressed_path = original_path.to_owned();
|
|
||||||
let filename = compressed_path
|
|
||||||
.file_name()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap()
|
|
||||||
.to_string();
|
|
||||||
compressed_path.set_file_name(filename + extension);
|
|
||||||
compressed_path
|
|
||||||
}
|
|
||||||
None => original_path.to_owned(),
|
|
||||||
};
|
|
||||||
Some((path, encoding))
|
|
||||||
});
|
|
||||||
for (path, encoding) in ranked_encodings_iter {
|
|
||||||
// Ignore errors while searching disk for a suitable encoding
|
|
||||||
if let Ok(mut named_file) = NamedFile::open_async(&path).await {
|
|
||||||
named_file.content_type = content_type;
|
|
||||||
named_file.content_disposition = content_disposition;
|
|
||||||
return Some((named_file, encoding));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,7 @@ async fn test_compression_encodings() {
|
||||||
use actix_web::body::MessageBody;
|
use actix_web::body::MessageBody;
|
||||||
|
|
||||||
let srv =
|
let srv =
|
||||||
test::init_service(App::new().service(Files::new("/", "./tests").try_compressed()))
|
test::init_service(App::new().service(Files::new("/", "./tests").try_compressed())).await;
|
||||||
.await;
|
|
||||||
|
|
||||||
// Select the requested encoding when present
|
// Select the requested encoding when present
|
||||||
let mut req = TestRequest::with_uri("/utf8.txt").to_request();
|
let mut req = TestRequest::with_uri("/utf8.txt").to_request();
|
||||||
|
|
@ -96,6 +95,23 @@ async fn test_compression_encodings() {
|
||||||
Some(&HeaderValue::from_static("text/plain; charset=utf-8")),
|
Some(&HeaderValue::from_static("text/plain; charset=utf-8")),
|
||||||
);
|
);
|
||||||
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None,);
|
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None,);
|
||||||
|
assert_eq!(res.into_body().size(), actix_web::body::BodySize::Sized(44),);
|
||||||
|
|
||||||
|
// Do not select an encoding explicitly refused via q=0
|
||||||
|
let mut req = TestRequest::with_uri("/utf8.txt").to_request();
|
||||||
|
req.headers_mut().insert(
|
||||||
|
header::ACCEPT_ENCODING,
|
||||||
|
header::HeaderValue::from_static("zstd;q=1, gzip;q=0"),
|
||||||
|
);
|
||||||
|
let res = test::call_service(&srv, req).await;
|
||||||
|
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
assert_eq!(
|
||||||
|
res.headers().get(header::CONTENT_TYPE),
|
||||||
|
Some(&HeaderValue::from_static("text/plain; charset=utf-8")),
|
||||||
|
);
|
||||||
|
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None,);
|
||||||
|
assert_eq!(res.into_body().size(), actix_web::body::BodySize::Sized(44),);
|
||||||
|
|
||||||
// Can still request a compressed file directly
|
// Can still request a compressed file directly
|
||||||
let req = TestRequest::with_uri("/utf8.txt.gz").to_request();
|
let req = TestRequest::with_uri("/utf8.txt.gz").to_request();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue