Skip to content

Commit

Permalink
Merge f7c9ecc into 5352ef0
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed May 7, 2021
2 parents 5352ef0 + f7c9ecc commit 3f8413b
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ public List<FileItem> parseRequest(final RequestContext ctx)
boolean successful = false;
try {
final FileItemIterator iter = getItemIterator(ctx);
final FileItemFactory fileItemFactory = Objects.requireNonNull(getFileItemFactory(), "No FileItemFactory has been set.");
final FileItemFactory fileItemFactory = Objects.requireNonNull(getFileItemFactory(),
"No FileItemFactory has been set.");
final byte[] buffer = new byte[Streams.DEFAULT_BUFFER_SIZE];
while (iter.hasNext()) {
final FileItemStream item = iter.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ public List<FileItem> getFileItems() throws FileUploadException, IOException {
final List<FileItem> items = new ArrayList<>();
while (hasNext()) {
final FileItemStream fis = next();
final FileItem fi = fileUploadBase.getFileItemFactory().createItem(fis.getFieldName(), fis.getContentType(), fis.isFormField(), fis.getName());
final FileItem fi = fileUploadBase.getFileItemFactory().createItem(fis.getFieldName(),
fis.getContentType(), fis.isFormField(), fis.getName());
items.add(fi);
}
return items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private Streams() {
* @return Number of bytes, which have been copied.
* @throws IOException An I/O error occurred.
*/
public static long copy(final InputStream inputStream, final OutputStream outputStream, final boolean closeOutputStream)
public static long copy(final InputStream inputStream, final OutputStream outputStream,
final boolean closeOutputStream)
throws IOException {
return copy(inputStream, outputStream, closeOutputStream, new byte[DEFAULT_BUFFER_SIZE]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* Utility class to decode/encode character set on HTTP Header fields based on RFC 2231.
* This implementation adheres to RFC 5987 in particular, which was defined for HTTP headers
*
* RFC 5987 builds on RFC 2231, but has lesser scope like <a href="https://tools.ietf.org/html/rfc5987#section-3.2">mandatory charset definition</a>
* RFC 5987 builds on RFC 2231, but has lesser scope like
* <a href="https://tools.ietf.org/html/rfc5987#section-3.2">mandatory charset definition</a>
* and <a href="https://tools.ietf.org/html/rfc5987#section-4">no parameter continuation</a>
*
* <p>
Expand Down Expand Up @@ -83,7 +84,8 @@ public static String stripDelimiter(final String paramName) {
* <b>Eg 3.</b> {@code UTF-8''%c2%a3%20and%20%e2%82%ac%20rates}
* will be decoded to {@code £ and € rates}.
*
* @param encodedText - Text to be decoded has a format of {@code <charset>'<language>'<encoded_value>} and ASCII only
* @param encodedText - Text to be decoded has a format of {@code <charset>'<language>'<encoded_value>}
* and ASCII only
* @return Decoded text based on charset encoding
* @throws UnsupportedEncodingException The requested character set wasn't found.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class FileItemHeadersTest {
@Test
public void testFileItemHeaders() throws Exception {
final FileItemHeadersImpl aMutableFileItemHeaders = new FileItemHeadersImpl();
aMutableFileItemHeaders.addHeader("Content-Disposition", "form-data; name=\"FileItem\"; filename=\"file1.txt\"");
aMutableFileItemHeaders.addHeader("Content-Disposition",
"form-data; name=\"FileItem\"; filename=\"file1.txt\"");
aMutableFileItemHeaders.addHeader("Content-Type", "text/plain");

aMutableFileItemHeaders.addHeader("TestHeader", "headerValue1");
Expand All @@ -52,7 +53,8 @@ public void testFileItemHeaders() throws Exception {
assertEquals("testheader", headerNameEnumeration.next());
assertFalse(headerNameEnumeration.hasNext());

assertEquals(aMutableFileItemHeaders.getHeader("Content-Disposition"), "form-data; name=\"FileItem\"; filename=\"file1.txt\"");
assertEquals(aMutableFileItemHeaders.getHeader("Content-Disposition"),
"form-data; name=\"FileItem\"; filename=\"file1.txt\"");
assertEquals(aMutableFileItemHeaders.getHeader("Content-Type"), "text/plain");
assertEquals(aMutableFileItemHeaders.getHeader("content-type"), "text/plain");
assertEquals(aMutableFileItemHeaders.getHeader("TestHeader"), "headerValue1");
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/apache/commons/fileupload2/FileUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void testFileUpload(final FileUpload upload)
throws IOException, FileUploadException {
final List<FileItem> fileItems = Util.parseUpload(upload,
"-----1234\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
"Content-Disposition: "
+ "form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
"Content-Type: text/whatever\r\n" +
"\r\n" +
"This is the content of the file\n" +
Expand Down Expand Up @@ -107,7 +108,8 @@ public void testFilenameCaseSensitivity(final FileUpload upload)
throws IOException, FileUploadException {
final List<FileItem> fileItems = Util.parseUpload(upload,
"-----1234\r\n" +
"Content-Disposition: form-data; name=\"FiLe\"; filename=\"FOO.tab\"\r\n" +
"Content-Disposition: form-data; "
+ "name=\"FiLe\"; filename=\"FOO.tab\"\r\n" +
"Content-Type: text/whatever\r\n" +
"\r\n" +
"This is the content of the file\n" +
Expand Down Expand Up @@ -221,7 +223,8 @@ public void testFILEUPLOAD62(final FileUpload upload) throws Exception {
"...contents of file2.gif...\r\n" +
"--BbC04y--\r\n" +
"--AaB03x--";
final List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes(StandardCharsets.US_ASCII), contentType);
final List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes(StandardCharsets.US_ASCII),
contentType);
assertEquals(3, fileItems.size());
final FileItem item0 = fileItems.get(0);
assertEquals("field1", item0.getFieldName());
Expand Down Expand Up @@ -307,7 +310,8 @@ public void testFileUpload130(final FileUpload upload)
};
final List<FileItem> fileItems = Util.parseUpload(upload,
"-----1234\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
"Content-Disposition: form-data; name=\"file\"; "
+ "filename=\"foo.tab\"\r\n" +
"Content-Type: text/whatever\r\n" +
headerNames[0] + ": " + headerValues[0] + "\r\n" +
"\r\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public void testFileUpload139() {
@Test
public void testFileUpload199() {
final ParameterParser parser = new ParameterParser();
final String s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n";
final String s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-"
+ "1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n";
final Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
assertEquals("If you can read this you understand the example.", params.get("filename"));
}
Expand All @@ -127,7 +128,8 @@ public void testFileUpload274() {
final ParameterParser parser = new ParameterParser();

// Should parse a UTF-8 charset
String s = "Content-Disposition: form-data; name=\"file\"; filename*=UTF-8\'\'%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF\r\n";
String s = "Content-Disposition: form-data; "
+ "name=\"file\"; filename*=UTF-8\'\'%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF\r\n";
Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
assertEquals("\u3053\u3093\u306B\u3061\u306F", params.get("filename")); //filename = "こんにちは" in japanese

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public int getContentLength() {
runTest(NUM_ITEMS, contents.length, request);
}

private void runTest(final int NUM_ITEMS, final long pContentLength, final MockHttpServletRequest request) throws FileUploadException, IOException {
private void runTest(final int NUM_ITEMS, final long pContentLength,
final MockHttpServletRequest request) throws FileUploadException, IOException {
final ServletFileUpload upload = new ServletFileUpload();
final ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, NUM_ITEMS);
upload.setProgressListener(listener);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/apache/commons/fileupload2/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static List<FileItem> parseUpload(final FileUpload upload, final byte[] b
return parseUpload(upload, bytes, Constants.CONTENT_TYPE);
}

public static List<FileItem> parseUpload(final FileUpload upload, final byte[] bytes, final String contentType) throws FileUploadException {
public static List<FileItem> parseUpload(final FileUpload upload, final byte[] bytes, final String contentType)
throws FileUploadException {
final HttpServletRequest request = new MockHttpServletRequest(bytes, contentType);
return upload.parseRequest(new ServletRequestContext(request));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public MockPortletActionRequest(final byte[] requestData, final String contentTy
this(new ByteArrayInputStream(requestData), requestData.length, contentType);
}

public MockPortletActionRequest(final ByteArrayInputStream byteArrayInputStream, final int requestLength, final String contentType) {
public MockPortletActionRequest(final ByteArrayInputStream byteArrayInputStream,
final int requestLength, final String contentType) {
this.requestData = byteArrayInputStream;
length = requestLength;
this.contentType = contentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ private static void assertEncoded(final String clearText, final String encoded)
assertArrayEquals(expected, actual);
}

private static void assertIOException(final String messageText, final String encoded) throws UnsupportedEncodingException {
private static void assertIOException(final String messageText, final String encoded)
throws UnsupportedEncodingException {
final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ public void decodeUtf8Base64Encoded() throws Exception {
@Test
public void decodeIso88591Base64Encoded() throws Exception {
assertEncoded("If you can read this you understand the example.",
"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= "
+ "=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
}

@Test
public void decodeIso88591Base64EncodedWithWhiteSpace() throws Exception {
assertEncoded("If you can read this you understand the example.",
"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\t \r\n =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\t \r\n =?ISO-8859-"
+ "2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n");
}

private static void assertEncoded(final String expected, final String encoded) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public void invalidCharDecode() {
*/
@Test
public void softLineBreakDecode() throws Exception {
assertEncoded("If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy.",
"If you believe that truth=3Dbeauty, then surely=20=\r\nmathematics is the most beautiful branch of philosophy.");
assertEncoded("If you believe that truth=beauty, then surely mathematics is the most "
+ "beautiful branch of philosophy.", "If you believe that truth=3Dbeauty, then "
+ "surely=20=\r\nmathematics is the most beautiful branch of philosophy.");
}

@Test
Expand Down Expand Up @@ -110,7 +111,8 @@ private static void assertEncoded(final String clearText, final String encoded)
assertArrayEquals(expected, actual);
}

private static void assertIOException(final String messageText, final String encoded) throws UnsupportedEncodingException {
private static void assertIOException(final String messageText, final String encoded)
throws UnsupportedEncodingException {
final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
/**
* The expected characters are encoded in UTF16, while the actual characters may be encoded in UTF-8/ISO-8859-1
*
* RFC 5987 recommends to support both UTF-8 & ISO 8859-1. Test values are taken from https://tools.ietf.org/html/rfc5987#section-3.2.2
* RFC 5987 recommends to support both UTF-8 & ISO 8859-1. Test values are taken
* from https://tools.ietf.org/html/rfc5987#section-3.2.2
*/
public final class RFC2231UtilityTestCase {

Expand Down Expand Up @@ -66,7 +67,8 @@ public void noNeedToDecode() throws Exception {

@Test
public void decodeUtf8() throws Exception {
assertEncoded("\u00a3 \u0061\u006e\u0064 \u20ac \u0072\u0061\u0074\u0065\u0073", "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates"); //"£ and € rates"
assertEncoded("\u00a3 \u0061\u006e\u0064 \u20ac \u0072\u0061\u0074\u0065\u0073",
"UTF-8''%c2%a3%20and%20%e2%82%ac%20rates"); //"£ and € rates"
}

@Test
Expand Down

0 comments on commit 3f8413b

Please sign in to comment.