Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Bartel2013-06-11 04:44:39 +0000
committerJan Bartel2013-06-11 04:45:41 +0000
commitd967ee2c3ae4ec417cda1a000b5c50ea0cf40c3a (patch)
tree6cd135896a258e17a439b249f3879852ebdc4fbb
parent9b8a78392ca2fa3efaf72f59136526384075f21c (diff)
downloadorg.eclipse.jetty.project-d967ee2c3ae4ec417cda1a000b5c50ea0cf40c3a.tar.gz
org.eclipse.jetty.project-d967ee2c3ae4ec417cda1a000b5c50ea0cf40c3a.tar.xz
org.eclipse.jetty.project-d967ee2c3ae4ec417cda1a000b5c50ea0cf40c3a.zip
408806 getParameter returns null on Multipart request if called before request.getPart()/getParts()
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/Request.java84
-rw-r--r--jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java4
2 files changed, 50 insertions, 38 deletions
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java
index 1b6387bd3d..b873977d95 100644
--- a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java
@@ -19,6 +19,7 @@
package org.eclipse.jetty.server;
import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -81,6 +82,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandler.Context;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.AttributesMap;
+import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.MultiMap;
@@ -363,6 +365,7 @@ public class Request implements HttpServletRequest
}
}
}
+
}
if (_parameters == null)
@@ -380,6 +383,28 @@ public class Request implements HttpServletRequest
_parameters.add(name,LazyList.get(values,i));
}
}
+
+ if (content_type != null && content_type.length()>0 && content_type.startsWith("multipart/form-data") && getAttribute(__MULTIPART_CONFIG_ELEMENT)!=null)
+ {
+ try
+ {
+ getParts();
+ }
+ catch (IOException e)
+ {
+ if (LOG.isDebugEnabled())
+ LOG.warn(e);
+ else
+ LOG.warn(e.toString());
+ }
+ catch (ServletException e)
+ {
+ if (LOG.isDebugEnabled())
+ LOG.warn(e);
+ else
+ LOG.warn(e.toString());
+ }
+ }
}
finally
{
@@ -2014,39 +2039,8 @@ public class Request implements HttpServletRequest
/* ------------------------------------------------------------ */
public Part getPart(String name) throws IOException, ServletException
- {
- if (getContentType() == null || !getContentType().startsWith("multipart/form-data"))
- throw new ServletException("Content-Type != multipart/form-data");
-
- if (_multiPartInputStream == null)
- {
- MultipartConfigElement config = (MultipartConfigElement)getAttribute(__MULTIPART_CONFIG_ELEMENT);
-
- if (config == null)
- throw new IllegalStateException("No multipart config for servlet");
-
- _multiPartInputStream = new MultiPartInputStream(getInputStream(),
- getContentType(),config,
- (_context != null?(File)_context.getAttribute("javax.servlet.context.tempdir"):null));
- setAttribute(__MULTIPART_INPUT_STREAM, _multiPartInputStream);
- setAttribute(__MULTIPART_CONTEXT, _context);
- Collection<Part> parts = _multiPartInputStream.getParts(); //causes parsing
- for (Part p:parts)
- {
- MultiPartInputStream.MultiPart mp = (MultiPartInputStream.MultiPart)p;
- if (mp.getContentDispositionFilename() == null && mp.getFile() == null)
- {
- //Servlet Spec 3.0 pg 23, parts without filenames must be put into init params
- String charset = null;
- if (mp.getContentType() != null)
- charset = MimeTypes.getCharsetFromContentType(new ByteArrayBuffer(mp.getContentType()));
-
- String content=new String(mp.getBytes(),charset==null?StringUtil.__UTF8:charset);
- getParameter(""); //cause params to be evaluated
- getParameters().add(mp.getName(), content);
- }
- }
- }
+ {
+ getParts();
return _multiPartInputStream.getPart(name);
}
@@ -2057,6 +2051,9 @@ public class Request implements HttpServletRequest
throw new ServletException("Content-Type != multipart/form-data");
if (_multiPartInputStream == null)
+ _multiPartInputStream = (MultiPartInputStream)getAttribute(__MULTIPART_INPUT_STREAM);
+
+ if (_multiPartInputStream == null)
{
MultipartConfigElement config = (MultipartConfigElement)getAttribute(__MULTIPART_CONFIG_ELEMENT);
@@ -2073,19 +2070,32 @@ public class Request implements HttpServletRequest
for (Part p:parts)
{
MultiPartInputStream.MultiPart mp = (MultiPartInputStream.MultiPart)p;
- if (mp.getContentDispositionFilename() == null && mp.getFile() == null)
+ if (mp.getContentDispositionFilename() == null)
{
//Servlet Spec 3.0 pg 23, parts without filenames must be put into init params
String charset = null;
if (mp.getContentType() != null)
charset = MimeTypes.getCharsetFromContentType(new ByteArrayBuffer(mp.getContentType()));
- String content=new String(mp.getBytes(),charset==null?StringUtil.__UTF8:charset);
- getParameter(""); //cause params to be evaluated
- getParameters().add(mp.getName(), content);
+ ByteArrayOutputStream os = null;
+ InputStream is = mp.getInputStream(); //get the bytes regardless of being in memory or in temp file
+ try
+ {
+ os = new ByteArrayOutputStream();
+ IO.copy(is, os);
+ String content=new String(os.toByteArray(),charset==null?StringUtil.__UTF8:charset);
+ getParameter(""); //cause params to be evaluated
+ getParameters().add(mp.getName(), content);
+ }
+ finally
+ {
+ IO.close(os);
+ IO.close(is);
+ }
}
}
}
+
return _multiPartInputStream.getParts();
}
diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java
index 3d82e1bbfc..51f12d32af 100644
--- a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java
+++ b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java
@@ -998,10 +998,12 @@ public class RequestTest
MultipartConfigElement mpce = new MultipartConfigElement(tmpDir.getAbsolutePath(),-1, -1, 2);
request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce);
+ String field1 = request.getParameter("field1");
+ assertNotNull(field1);
+
Part foo = request.getPart("stuff");
assertNotNull(foo);
assertTrue(foo.getSize() > 0);
-
response.setStatus(200);
}
catch (IllegalStateException e)

Back to the top