-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support RFC 5987 for attribute filename* in HTTP header Content-D…
…isposition (#4647) Add support RFC 5987 for attribute filename* in HTTP header Content-Disposition Signed-off-by: Andrii Serkes <[email protected]>
- Loading branch information
Showing
2 changed files
with
200 additions
and
3 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2014, 2018 Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2014, 2021 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
|
@@ -24,10 +24,12 @@ | |
import org.glassfish.jersey.message.internal.HttpHeaderReader; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.fail; | ||
|
||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
|
@@ -97,6 +99,143 @@ public void testToString() { | |
assertEquals(header, contentDisposition.toString()); | ||
} | ||
|
||
@Test | ||
public void testFileNameExt() { | ||
final String fileName = "test.file"; | ||
String fileNameExt; | ||
String encodedFilename; | ||
try { | ||
//incorrect fileNameExt - does not contain charset'' | ||
try { | ||
fileNameExt = "testExt.file"; | ||
assertFileNameExt(fileName, fileName, fileNameExt); | ||
fail("ParseException was expected to be thrown."); | ||
} catch (ParseException e) { | ||
//expected | ||
} | ||
|
||
//correct fileNameExt | ||
fileNameExt = "ISO-8859-1'language-us'abc%a1abc%a2%b1!#$&+.^_`|~-"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//correct fileNameExt | ||
fileNameExt = "UTF-8'language-us'abc%a1abc%a2%b1!#$&+.^_`|~-"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//correct fileNameExt | ||
fileNameExt = "UTF-8'us'fileName.txt"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//incorrect fileNameExt - too long language tag | ||
try { | ||
fileNameExt = "utf-8'languageTooLong'fileName.txt"; | ||
assertFileNameExt(fileName, fileName, fileNameExt); | ||
fail("ParseException was expected to be thrown."); | ||
} catch (ParseException e) { | ||
//expected | ||
} | ||
|
||
//correct fileNameExt | ||
fileNameExt = "utf-8''a"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//incorrect fileNameExt - language tag does not match to pattern | ||
try { | ||
fileNameExt = "utf-8'lang-'a"; | ||
assertFileNameExt(fileName, fileName, fileNameExt); | ||
fail("ParseException was expected to be thrown."); | ||
} catch (ParseException e) { | ||
//expected | ||
} | ||
|
||
//incorrect fileNameExt - ext-value contains an inappropriate symbol sequence (%z1). Jersey encodes it. | ||
fileNameExt = "utf-8'language-us'a%z1"; | ||
encodedFilename = "utf-8'language-us'a%25z1"; | ||
assertFileNameExt(encodedFilename, fileName, fileNameExt); | ||
|
||
//Incorrect fileNameExt - ext-value contains an inappropriate symbol sequence (%z1). | ||
//Jersey won't encodes it because of the unsupported charset. | ||
try { | ||
fileNameExt = "windows-1251'ru-ru'a%z1"; | ||
assertFileNameExt(fileName, fileName, fileNameExt); | ||
fail("ParseException was expected to be thrown."); | ||
} catch (ParseException e) { | ||
//expected | ||
} | ||
|
||
//correct fileNameExt | ||
fileNameExt = "UTF-8'language-us'abc%a1abc%a2%b1"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//correct fileNameExt - encoded with other charset | ||
fileNameExt = "UTF-16'language-us'abc%a1abc%a2%b1"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//correct fileNameExt - unsupported charset, but fileName contains only valid characters | ||
fileNameExt = "Windows-1251'sr-Latn-RS'abc"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//correct fileNameExt | ||
fileNameExt = "utf-8'sr-Latn-RS'a"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//incorrect fileNameExt - ext-value contains % without two HEXDIG. Jersey encodes it. | ||
fileNameExt = "utf-8'language-us'a%"; | ||
encodedFilename = "utf-8'language-us'a%25"; | ||
assertFileNameExt(encodedFilename, fileName, fileNameExt); | ||
|
||
//correct fileNameExt | ||
fileNameExt = "UTF-8'language-us'abc.TXT"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
//incorrect fileNameExt - no ext-value | ||
try { | ||
fileNameExt = "utf-8'language-us'"; | ||
assertFileNameExt(fileName, fileName, fileNameExt); | ||
fail("ParseException was expected to be thrown."); | ||
} catch (ParseException e) { | ||
//expected | ||
} | ||
|
||
//incorrect fileNameExt - ext-value contains forbidden symbol (\). Jersey encodes it. | ||
fileNameExt = "utf-8'language-us'c:\\\\file.txt"; | ||
encodedFilename = "utf-8'language-us'c%3A%5Cfile.txt"; | ||
assertFileNameExt(encodedFilename, fileName, fileNameExt); | ||
|
||
//incorrect fileNameExt - ext-value contains forbidden symbol (/). Jersey encodes it. | ||
fileNameExt = "utf-8'language-us'home/file.txt"; | ||
encodedFilename = "utf-8'language-us'home%2Ffile.txt"; | ||
assertFileNameExt(encodedFilename, fileName, fileNameExt); | ||
|
||
//incorrect fileNameExt - ext-value contains forbidden symbol (李). Jersey encodes it. | ||
fileNameExt = "utf-8'language-us'李.txt"; | ||
encodedFilename = "utf-8'language-us'%E6%9D%8E.txt"; | ||
assertFileNameExt(encodedFilename, fileName, fileNameExt); | ||
|
||
//correct fileNameExt | ||
fileNameExt = "utf-8'language-us'FILEname.tXt"; | ||
assertFileNameExt(fileNameExt, fileName, fileNameExt); | ||
|
||
} catch (ParseException ex) { | ||
fail(ex.getMessage()); | ||
} | ||
} | ||
|
||
private void assertFileNameExt( | ||
final String expectedFileName, | ||
final String actualFileName, | ||
final String actualFileNameExt | ||
) throws ParseException { | ||
final Date date = new Date(); | ||
final String dateString = HttpDateFormat.getPreferredDateFormat().format(date); | ||
final String prefixHeader = contentDispositionType + ";filename=\"" + actualFileName + "\";" | ||
+ "creation-date=\"" + dateString + "\";modification-date=\"" + dateString + "\";read-date=\"" | ||
+ dateString + "\";size=1222" + ";name=\"testData\";" + "filename*=\""; | ||
final String header = prefixHeader + actualFileNameExt + "\""; | ||
final ContentDisposition contentDisposition = new ContentDisposition(HttpHeaderReader.newInstance(header), true); | ||
assertEquals(expectedFileName, contentDisposition.getFileName()); | ||
} | ||
|
||
protected void assertContentDisposition(final ContentDisposition contentDisposition, Date date) { | ||
assertNotNull(contentDisposition); | ||
assertEquals(contentDispositionType, contentDisposition.getType()); | ||
|