Skip to content

Commit

Permalink
ContentDisposition trims charset in filename
Browse files Browse the repository at this point in the history
Backport of c8bce96

Closes gh-24112
  • Loading branch information
rstoyanchev committed Dec 13, 2019
1 parent 3fbe762 commit 2576aa4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Expand Up @@ -277,7 +277,7 @@ public static ContentDisposition parse(String contentDisposition) {
}
else if (attribute.equals("filename*") ) {
filename = decodeHeaderFieldParam(value);
charset = Charset.forName(value.substring(0, value.indexOf('\'')));
charset = Charset.forName(value.substring(0, value.indexOf('\'')).trim());
Assert.isTrue(UTF_8.equals(charset) || ISO_8859_1.equals(charset),
"Charset should be UTF-8 or ISO-8859-1");
}
Expand Down Expand Up @@ -371,7 +371,7 @@ private static String decodeHeaderFieldParam(String input) {
if (firstQuoteIndex == -1 || secondQuoteIndex == -1) {
return input;
}
Charset charset = Charset.forName(input.substring(0, firstQuoteIndex));
Charset charset = Charset.forName(input.substring(0, firstQuoteIndex).trim());
Assert.isTrue(UTF_8.equals(charset) || ISO_8859_1.equals(charset),
"Charset should be UTF-8 or ISO-8859-1");
byte[] value = input.substring(secondQuoteIndex + 1, input.length()).getBytes(charset);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,6 +71,14 @@ public void parseAndIgnoreEmptyParts() {
.name("foo").filename("foo.txt").size(123L).build(), disposition);
}

@Test // gh-24112
public void parseEncodedFilenameWithPaddedCharset() {
ContentDisposition disposition = ContentDisposition
.parse("attachment; filename*= UTF-8''some-file.zip");
assertEquals(ContentDisposition.builder("attachment")
.filename("some-file.zip", StandardCharsets.UTF_8).build(), disposition);
}

@Test
public void parseEncodedFilename() {
ContentDisposition disposition = ContentDisposition
Expand Down

0 comments on commit 2576aa4

Please sign in to comment.