Skip to content

Commit

Permalink
Issue #4383 - check for non multipart content type in constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Jan 22, 2020
1 parent 0ab7751 commit f9f2cca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
Expand Up @@ -94,7 +94,7 @@ private enum State
private volatile boolean _deleteOnExit;
private volatile boolean _writeFilesWithFilenames;
private volatile int _bufferSize = 16 * 1024;
private volatile State state = State.UNPARSED;
private State state = State.UNPARSED;

public class MultiPart implements Part
{
Expand Down Expand Up @@ -356,7 +356,11 @@ public String getContentDispositionFilename()
*/
public MultiPartFormInputStream(InputStream in, String contentType, MultipartConfigElement config, File contextTmpDir)
{
// Must be a multipart request.
_contentType = contentType;
if (_contentType == null || !_contentType.startsWith("multipart/form-data"))
throw new IllegalArgumentException("content type is not multipart/form-data");

_contextTmpDir = (contextTmpDir != null) ? contextTmpDir : new File(System.getProperty("java.io.tmpdir"));
_config = (config != null) ? config : new MultipartConfigElement(_contextTmpDir.getAbsolutePath());

Expand Down Expand Up @@ -520,10 +524,6 @@ protected void parse()
MultiPartParser parser = null;
try
{
// if its not a multipart request, don't parse it
if (_contentType == null || !_contentType.startsWith("multipart/form-data"))
return;

// sort out the location to which to write the files
if (_config.getLocation() == null)
_tmpDir = _contextTmpDir;
Expand Down
Expand Up @@ -206,12 +206,10 @@ public void testNonMultiPartRequest()
throws Exception
{
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
"Content-type: text/plain",
config,
_tmpDir);
mpis.setDeleteOnExit(true);
assertTrue(mpis.getParts().isEmpty());
Throwable t = assertThrows(IllegalArgumentException.class, () ->
new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
"Content-type: text/plain", config, _tmpDir));
assertThat(t.getMessage(), is("content type is not multipart/form-data"));
}

@Test
Expand Down

0 comments on commit f9f2cca

Please sign in to comment.