Skip to content

Commit

Permalink
decode CONTENT_DISPOSITION header value with utf-8
Browse files Browse the repository at this point in the history
download file support unicode file name
  • Loading branch information
zuisong committed Jun 21, 2024
1 parent cb44ff5 commit 3f24597
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reqwest::{
};

use crate::decoder::{decompress, get_compression_type};
use crate::utils::{copy_largebuf, test_pretend_term};
use crate::utils::{copy_largebuf, test_pretend_term, HeaderValueExt};

fn get_content_length(headers: &HeaderMap) -> Option<u64> {
headers
Expand All @@ -31,7 +31,11 @@ fn get_file_name(response: &Response, orig_url: &reqwest::Url) -> String {
// Against the spec, but used by e.g. Github's zip downloads
let unquoted = Regex::new("filename=([^;=\"]*)").unwrap();

let header = response.headers().get(CONTENT_DISPOSITION)?.to_str().ok()?;
let header = response
.headers()
.get(CONTENT_DISPOSITION)?
.to_utf8_str()
.ok()?;
let caps = quoted
.captures(header)
.or_else(|| unquoted.captures(header))?;
Expand Down
21 changes: 21 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,27 @@ fn download_supplied_filename() {
);
}

#[test]
fn download_supplied_unicode_filename() {
let dir = tempdir().unwrap();
let server = server::http(|_req| async move {
hyper::Response::builder()
.header("Content-Disposition", r#"attachment; filename="😀.bar""#)
.body("file".into())
.unwrap()
});

get_command()
.args(["--download", &server.base_url()])
.current_dir(&dir)
.assert()
.success();
assert_eq!(
fs::read_to_string(dir.path().join("😀.bar")).unwrap(),
"file"
);
}

#[test]
fn download_supplied_unquoted_filename() {
let dir = tempdir().unwrap();
Expand Down

0 comments on commit 3f24597

Please sign in to comment.