Skip to content

Commit

Permalink
Merge pull request #11019 from wangsongyan/master
Browse files Browse the repository at this point in the history
Decode URL encoded filenames from content disposition headers
  • Loading branch information
timvandermeij authored Jul 31, 2019
2 parents 323b2ea + c61205d commit c5d837d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/display/network_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function extractFilenameFromHeader(getResponseHeader) {
const contentDisposition = getResponseHeader('Content-Disposition');
if (contentDisposition) {
let filename = getFilenameFromContentDispositionHeader(contentDisposition);
if (filename.includes('%')) {
try {
filename = decodeURIComponent(filename);
} catch (ex) {}
}
if (/\.pdf$/i.test(filename)) {
return filename;
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit/network_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ describe('network_utils', function() {
}
throw new Error(`Unexpected headerName: ${headerName}`);
})).toEqual('filename.pdf');

expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment; filename="%e4%b8%ad%e6%96%87.pdf"';
}
throw new Error(`Unexpected headerName: ${headerName}`);
})).toEqual('中文.pdf');

expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment; filename="100%.pdf"';
}
throw new Error(`Unexpected headerName: ${headerName}`);
})).toEqual('100%.pdf');
});

it('gets the filename from the response header (RFC 6266)', function() {
Expand Down

0 comments on commit c5d837d

Please sign in to comment.