Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Wilkins2016-02-04 01:00:13 -0500
committerGreg Wilkins2016-02-04 01:00:13 -0500
commitdf79ad689a38592472d3696e15bf0252e617b8e7 (patch)
treec2fedd48685d4f06c044821f01b3a7c12a455b71 /jetty-servlets
parent459ba4ae5aa7f9a29d18b4e08848ff6e1cec0e4a (diff)
parent009fde2400a746b1ce24ba04bd4fcd001378516b (diff)
downloadorg.eclipse.jetty.project-df79ad689a38592472d3696e15bf0252e617b8e7.tar.gz
org.eclipse.jetty.project-df79ad689a38592472d3696e15bf0252e617b8e7.tar.xz
org.eclipse.jetty.project-df79ad689a38592472d3696e15bf0252e617b8e7.zip
Merge remote-tracking branch 'origin/jetty-9.3.x'
Diffstat (limited to 'jetty-servlets')
-rw-r--r--jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java3
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/IncludedGzipTest.java3
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java262
3 files changed, 243 insertions, 25 deletions
diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
index 623a0af6c0..4a5c483586 100644
--- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
+++ b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
@@ -104,6 +104,7 @@ public class MultiPartFilter implements Filter
private boolean _deleteFiles;
private ServletContext _context;
private int _fileOutputBuffer = 0;
+ private boolean _writeFilesWithFilenames = false;
private long _maxFileSize = -1L;
private long _maxRequestSize = -1L;
private int _maxFormKeys = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormKeys", 1000);
@@ -130,6 +131,7 @@ public class MultiPartFilter implements Filter
String mfks = filterConfig.getInitParameter("maxFormKeys");
if (mfks!=null)
_maxFormKeys=Integer.parseInt(mfks);
+ _writeFilesWithFilenames = "true".equalsIgnoreCase(filterConfig.getInitParameter("writeFilesWithFilenames"));
}
/* ------------------------------------------------------------------------------- */
@@ -164,6 +166,7 @@ public class MultiPartFilter implements Filter
MultipartConfigElement config = new MultipartConfigElement(tempdir.getCanonicalPath(), _maxFileSize, _maxRequestSize, _fileOutputBuffer);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(in, content_type, config, tempdir);
mpis.setDeleteOnExit(_deleteFiles);
+ mpis.setWriteFilesWithFilenames(_writeFilesWithFilenames);
request.setAttribute(MULTIPART, mpis);
try
{
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/IncludedGzipTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/IncludedGzipTest.java
index 4e02fccbb8..bea2501e8b 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/IncludedGzipTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/IncludedGzipTest.java
@@ -89,8 +89,7 @@ public class IncludedGzipTest
tester.getContext().addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
GzipHandler gzipHandler = new GzipHandler();
- gzipHandler.setHandler(tester.getContext().getHandler());
- tester.getContext().setHandler(gzipHandler);
+ tester.getContext().insertHandler(gzipHandler);
tester.start();
}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
index 01cdb45637..c2ce682774 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java
@@ -18,9 +18,10 @@
package org.eclipse.jetty.servlets;
-
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -30,10 +31,15 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
+import java.io.Reader;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
import java.util.EnumSet;
+import java.util.Enumeration;
import java.util.Map;
import javax.servlet.DispatcherType;
@@ -45,17 +51,59 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletTester;
+import org.eclipse.jetty.toolchain.test.FS;
+import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
public class MultipartFilterTest
{
private File _dir;
private ServletTester tester;
+ FilterHolder multipartFilter;
+
+ @SuppressWarnings("serial")
+ public static class FilenameServlet extends TestServlet
+ {
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ assertThat(req.getAttribute("fileup"), notNullValue());
+ super.doPost(req, resp);
+ }
+ }
+
+ @SuppressWarnings("serial")
+ public static class ParameterListServlet extends TestServlet
+ {
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ resp.setContentType("text/plain");
+ PrintWriter out = resp.getWriter();
+
+ Enumeration<String> pnames = req.getParameterNames();
+ while (pnames.hasMoreElements())
+ {
+ String pname = pnames.nextElement();
+ Object param = req.getParameter(pname);
+ out.printf("Parameter[%s] = ",pname);
+ if (param == null)
+ {
+ out.println(" <null>");
+ }
+ else
+ {
+ out.printf("(%s) %s%n",param.getClass().getName(),param);
+ }
+ }
+ }
+ }
-
+ @SuppressWarnings("serial")
public static class BoundaryServlet extends TestServlet
{
@Override
@@ -63,9 +111,11 @@ public class MultipartFilterTest
{
//we have configured the multipart filter to always store to disk (file size threshold == 1)
//but fileName has no filename param, so only the attribute should be set
- assertNull(req.getParameter("fileName"));
- assertNotNull(req.getAttribute("fileName"));
+ assertThat("getParameter('fileName')", req.getParameter("fileName"), nullValue());
+ assertThat("getAttribute('fileName')", req.getAttribute("fileName"), notNullValue());
+
File f = (File)req.getAttribute("fileName");
+ assertThat("File exists", f.exists(), is(true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IO.copy(new FileInputStream(f), baos);
assertEquals(getServletContext().getAttribute("fileName"), baos.toString());
@@ -83,37 +133,39 @@ public class MultipartFilterTest
}
}
+ @SuppressWarnings("serial")
public static class TestServlet extends DumpServlet
{
+ @SuppressWarnings("deprecation")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
- assertNotNull(req.getParameter("fileup"));
- System.err.println("Fileup="+req.getParameter("fileup"));
- assertNotNull(req.getParameter("fileup"+MultiPartFilter.CONTENT_TYPE_SUFFIX));
- assertEquals(req.getParameter("fileup"+MultiPartFilter.CONTENT_TYPE_SUFFIX), "application/octet-stream");
+ String fileup = req.getParameter("fileup");
+ assertThat("getParameter('fileup')",fileup,notNullValue());
+
+ System.err.println("Fileup=" + req.getParameter("fileup"));
+
+ String fileupType = req.getParameter("fileup" + MultiPartFilter.CONTENT_TYPE_SUFFIX);
+ assertThat("req.getParameter('fileup'+CONTENT_TYPE_SUFFIX)",fileupType,is("application/octet-stream"));
super.doPost(req, resp);
}
-
}
-
-
+ @SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception
{
- _dir = File.createTempFile("testmultupart",null);
- assertTrue(_dir.delete());
- assertTrue(_dir.mkdir());
- _dir.deleteOnExit();
- assertTrue(_dir.isDirectory());
+ Path tmpDir = MavenTestingUtils.getTargetTestingPath("testmultupart");
+ FS.ensureEmpty(tmpDir);
+
+ _dir = tmpDir.toFile();
tester=new ServletTester("/context");
- tester.getContext().setResourceBase(_dir.getCanonicalPath());
+ tester.getContext().setResourceBase(tmpDir.toString());
tester.getContext().addServlet(TestServlet.class, "/");
tester.getContext().setAttribute("javax.servlet.context.tempdir", _dir);
- FilterHolder multipartFilter = tester.getContext().addFilter(MultiPartFilter.class,"/*", EnumSet.of(DispatcherType.REQUEST));
+ multipartFilter = tester.getContext().addFilter(MultiPartFilter.class,"/*", EnumSet.of(DispatcherType.REQUEST));
multipartFilter.setInitParameter("deleteFiles", "true");
multipartFilter.setInitParameter("fileOutputBuffer", "1"); //write a file if there's more than 1 byte content
tester.start();
@@ -780,6 +832,7 @@ public class MultipartFilterTest
assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatus());
}
+ @SuppressWarnings("serial")
public static class TestServletParameterMap extends DumpServlet
{
@Override
@@ -790,7 +843,7 @@ public class MultipartFilterTest
super.doPost(req, resp);
}
}
-
+
/**
* Validate that the getParameterMap() call is correctly unencoding the parameters in the
* map that it returns.
@@ -820,7 +873,7 @@ public class MultipartFilterTest
"Content-Type: application/octet-stream\r\n\r\n"+
"How now brown cow."+
"\r\n--" + boundary + "\r\n"+
- "Content-Disposition: form-data; name=\"strup\""+
+ "Content-Disposition: form-data; name=\"strup\""+ // FIXME: this is missing a "\r\n"??
"Content-Type: application/octet-stream\r\n\r\n"+
"How now brown cow."+
"\r\n--" + boundary + "--\r\n\r\n";
@@ -831,6 +884,119 @@ public class MultipartFilterTest
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
+
+ /**
+ * Validate that the uploaded file can be accessed on the name it was given
+ * @throws Exception on test failure
+ */
+ @Test
+ @Ignore("Fails for Bug #486394")
+ public void testFileUpload_AccessViaFilename() throws Exception
+ {
+ tester.addServlet(ParameterListServlet.class,"/paramlist");
+
+ multipartFilter.setInitParameter("fileOutputBuffer", "1");
+ multipartFilter.setInitParameter("deleteFiles", "false");
+ multipartFilter.setInitParameter("writeFilesWithFilenames", "true");
+
+ // generated and parsed test
+ HttpTester.Request request = HttpTester.newRequest();
+ HttpTester.Response response;
+
+ // test GET
+ request.setMethod("POST");
+ request.setURI("/context/paramlist");
+ request.setVersion("HTTP/1.1");
+ request.setHeader("Host","tester");
+ request.setHeader("Connection","close");
+ String boundary="XyXyXy";
+ request.setHeader("Content-Type","multipart/form-data; boundary=" + boundary);
+
+ StringBuilder content = new StringBuilder();
+ content.append("--").append(boundary).append("\r\n");
+ content.append("Content-Disposition: form-data; name=\"file\"; filename=\"tiny.dat\"\r\n");
+ content.append("Content-Type: application/octet-stream\r\n");
+ content.append("\r\n");
+ content.append("How now brown cow.\r\n");
+ content.append("--").append(boundary).append("--\r\n");
+ content.append("\r\n");
+
+ request.setContent(content.toString());
+
+ response = HttpTester.parseResponse(tester.getResponses(request.generate()));
+ assertThat("Response status", response.getStatus(), is(HttpServletResponse.SC_OK));
+ assertEquals(HttpServletResponse.SC_OK,response.getStatus());
+
+ String contents = assertUploadedFileExists("tiny.dat");
+ assertThat("contents", contents, containsString("How now brown cow."));
+ }
+
+ /**
+ * Validate that the two upload files, with the same name="" on two different parts,
+ * can be accessed with the filename="" portions.
+ * @throws Exception on test failure
+ */
+ @Test
+ @Ignore("Fails for Bug #486394")
+ public void testTwoFileUploads_AccessViaFilename() throws Exception
+ {
+ tester.addServlet(ParameterListServlet.class,"/paramlist");
+
+ multipartFilter.setInitParameter("fileOutputBuffer", "1");
+ multipartFilter.setInitParameter("deleteFiles", "false");
+ multipartFilter.setInitParameter("writeFilesWithFilenames", "true");
+
+ // generated and parsed test
+ HttpTester.Request request = HttpTester.newRequest();
+ HttpTester.Response response;
+
+ // test GET
+ request.setMethod("POST");
+ request.setURI("/context/paramlist");
+ request.setVersion("HTTP/1.1");
+ request.setHeader("Host","tester");
+ request.setHeader("Connection","close");
+
+ String boundary="XyXyXy";
+ request.setHeader("Content-Type","multipart/form-data; boundary=" + boundary);
+
+ StringBuilder content = new StringBuilder();
+ content.append("--").append(boundary).append("\r\n");
+ content.append("Content-Disposition: form-data; name=\"same\"; filename=\"crocodile.dat\"\r\n");
+ content.append("Content-Type: application/octet-stream\r\n");
+ content.append("\r\n");
+ content.append("See ya later, aligator.\r\n");
+ content.append("--").append(boundary).append("\r\n");
+ content.append("Content-Disposition: form-data; name=\"same\"; filename=\"aligator.dat\"\r\n");
+ content.append("Content-Type: application/octet-stream\r\n");
+ content.append("\r\n");
+ content.append("In a while, crocodile.\r\n");
+ content.append("--").append(boundary).append("--\r\n");
+ content.append("\r\n");
+
+ request.setContent(content.toString());
+
+ response = HttpTester.parseResponse(tester.getResponses(request.generate()));
+ assertThat("Response status", response.getStatus(), is(HttpServletResponse.SC_OK));
+ assertEquals(HttpServletResponse.SC_OK,response.getStatus());
+
+ String contents = assertUploadedFileExists("crocodile.dat");
+ assertThat("contents", contents, containsString("See ya later, aligator."));
+
+ contents = assertUploadedFileExists("aligator.dat");
+ assertThat("contents", contents, containsString("In a while, crocodile."));
+ }
+
+ private String assertUploadedFileExists(String filename) throws IOException
+ {
+ File uploadedFile = new File(_dir,filename);
+ assertThat("Uploaded File[" + uploadedFile + "].exists",uploadedFile.exists(),is(true));
+
+ try (Reader reader = new FileReader(uploadedFile))
+ {
+ return IO.toString(reader);
+ }
+ }
public static class TestServletCharSet extends HttpServlet
{
@@ -885,11 +1051,61 @@ public class MultipartFilterTest
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
}
+
+ @Test
+ public void testFilesWithFilenames ()
+ throws Exception
+ {
+ multipartFilter.setInitParameter("fileOutputBuffer", "0");
+ multipartFilter.setInitParameter("writeFilesWithFilenames", "true");
+
+
+ String boundary="XyXyXy";
+ HttpTester.Request request = HttpTester.newRequest();
+ HttpTester.Response response;
+ tester.addServlet(FilenameServlet.class,"/testf");
+ // test GET
+ request.setMethod("POST");
+ request.setVersion("HTTP/1.0");
+ request.setHeader("Host","tester");
+ request.setURI("/context/testf");
+ request.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
+
+ String content = "--XyXyXy\r"+
+ "Content-Disposition: form-data; name=\"fileName\"\r"+
+ "Content-Type: text/plain; charset=US-ASCII\r"+
+ "Content-Transfer-Encoding: 8bit\r"+
+ "\r"+
+ "abc\r"+
+ "--XyXyXy\r"+
+ "Content-Disposition: form-data; name=\"desc\"\r"+
+ "Content-Type: text/plain; charset=US-ASCII\r"+
+ "Content-Transfer-Encoding: 8bit\r"+
+ "\r"+
+ "123\r"+
+ "--XyXyXy\r"+
+ "Content-Disposition: form-data; name=\"title\"\r"+
+ "Content-Type: text/plain; charset=US-ASCII\r"+
+ "Content-Transfer-Encoding: 8bit\r"+
+ "\r"+
+ "ttt\r"+
+ "--XyXyXy\r"+
+ "Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r"+
+ "Content-Type: application/octet-stream\r"+
+ "Content-Transfer-Encoding: binary\r"+
+ "\r"+
+ "000\r"+
+ "--XyXyXy--\r";
+ request.setContent(content);
+
+ response = HttpTester.parseResponse(tester.getResponses(request.generate()));
+ assertEquals(HttpServletResponse.SC_OK,response.getStatus());
+ assertTrue(response.getContent().indexOf("000")>=0);
+ }
+
+ @SuppressWarnings("serial")
public static class DumpServlet extends HttpServlet
{
- private static final long serialVersionUID = 201012011130L;
-
- /* ------------------------------------------------------------ */
/**
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/

Back to the top