-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Widen/fix/DAM-8293-utf8-attachment-filenames
fix: add proper rfc5987 UTF-8 attachment filenames
- Loading branch information
Showing
8 changed files
with
77 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.widen.util; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.text.Normalizer; | ||
|
||
public class HttpUtils | ||
{ | ||
public static String createContentDispositionHeader(String type, String filename) | ||
{ | ||
// Most browsers don't normally support UTF-8 in HTTP headers (HTTP officially supports only ISO-8859-1). In | ||
// practice, S3 only supports ASCII characters, so strip everything from the filename out not in the ASCII range. | ||
String asciiFilename = Normalizer | ||
.normalize(filename, Normalizer.Form.NFD) | ||
.replaceAll("[^\\x20-\\x7E]", ""); | ||
|
||
// Create the base header value. | ||
String header = String.format("%s; filename=\"%s\"", type, asciiFilename); | ||
|
||
// For clients that support RFC 5987, we can URL encode the UTF-8 encoded filename and pass it in as an | ||
// additional parameter. If the ASCII filename differs from the given filename, add the additional parameter. | ||
if (!asciiFilename.equals(filename)) | ||
{ | ||
try | ||
{ | ||
String utf8EncodedFilename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20"); | ||
header += String.format("; filename*=UTF-8''%s", utf8EncodedFilename); | ||
} | ||
catch (UnsupportedEncodingException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
return header; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.widen.util; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class HttpUtilsTest | ||
{ | ||
@Test | ||
public void testCreateContentDispositionHeader() | ||
{ | ||
assertEquals( | ||
"inline; filename=\"foo.jpg\"", | ||
HttpUtils.createContentDispositionHeader("inline", "foo.jpg") | ||
); | ||
assertEquals( | ||
"attachment; filename=\"+oo.jpg\"; filename*=UTF-8''%2B%C6%92oo.jpg", | ||
HttpUtils.createContentDispositionHeader("attachment", "+ƒoo.jpg") | ||
); | ||
assertEquals( | ||
"attachment; filename=\"foo.jpg\"; filename*=UTF-8''%F0%A2%83%87%F0%A2%9E%B5%F0%A2%AB%95foo%F0%A2%AD%83%F0%A2%AF%8A%F0%A2%B1%91%F0%A2%B1%95.jpg", | ||
HttpUtils.createContentDispositionHeader("attachment", "𢃇𢞵𢫕foo𢭃𢯊𢱑𢱕.jpg") | ||
); | ||
assertEquals( | ||
"attachment; filename=\"helloworld.jpg\"; filename*=UTF-8''hello%0Aworld.jpg", | ||
HttpUtils.createContentDispositionHeader("attachment", "hello\nworld.jpg") | ||
); | ||
assertEquals( | ||
"attachment; filename=\"resume.pdf\"; filename*=UTF-8''r%C3%A9sum%C3%A9.pdf", | ||
HttpUtils.createContentDispositionHeader("attachment", "résumé.pdf") | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters