fix: serve files with names containing newline char (#328)

This commit is contained in:
sigoden
2023-12-23 15:40:41 +08:00
committed by GitHub
parent 77f86a4c60
commit 006e03ed30
3 changed files with 31 additions and 2 deletions

View File

@@ -1547,13 +1547,23 @@ fn status_no_content(res: &mut Response) {
fn set_content_diposition(res: &mut Response, inline: bool, filename: &str) -> Result<()> {
let kind = if inline { "inline" } else { "attachment" };
let filename: String = filename
.chars()
.map(|ch| {
if ch.is_ascii_control() && ch != '\t' {
' '
} else {
ch
}
})
.collect();
let value = if filename.is_ascii() {
HeaderValue::from_str(&format!("{kind}; filename=\"{}\"", filename,))?
} else {
HeaderValue::from_str(&format!(
"{kind}; filename=\"{}\"; filename*=UTF-8''{}",
filename,
encode_uri(filename),
encode_uri(&filename),
))?
};
res.headers_mut().insert(CONTENT_DISPOSITION, value);