Merge remote-tracking branch 'origin/jetty-7' into jetty-8

Conflicts:
	jetty-servlets/src/main/java/org/eclipse/jetty/servlets/MultiPartFilter.java
This commit is contained in:
Jan Bartel 2012-12-22 14:05:29 +11:00
commit 3a236c6735
3 changed files with 174 additions and 3 deletions

View File

@ -656,7 +656,73 @@ public class MultipartFilterTest
assertTrue(response.getReason().startsWith("Missing initial"));
}
@Test
public void testLeadingWhitespaceBodyWithCRLF()
throws Exception
{
String boundary = "AaB03x";
String body = " \n\n\n\r\n\r\n\r\n\r\n"+
"--AaB03x\r\n"+
"content-disposition: form-data; name=\"field1\"\r\n"+
"\r\n"+
"Joe Blow\r\n"+
"--AaB03x\r\n"+
"Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r\n"+
"Content-Type: application/octet-stream\r\n"+
"\r\n" +
"aaaa,bbbbb"+"\r\n" +
"--AaB03x--\r\n";
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
request.setMethod("POST");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setURI("/context/dump");
request.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
request.setContent(body);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertTrue(response.getContent().contains("aaaa,bbbbb"));
}
@Test
public void testLeadingWhitespaceBodyWithoutCRLF()
throws Exception
{
String boundary = "AaB03x";
String body = " "+
"--AaB03x\r\n"+
"content-disposition: form-data; name=\"field1\"\r\n"+
"\r\n"+
"Joe Blow\r\n"+
"--AaB03x\r\n"+
"Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r\n"+
"Content-Type: application/octet-stream\r\n"+
"\r\n" +
"aaaa,bbbbb"+"\r\n" +
"--AaB03x--\r\n";
// generated and parsed test
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
request.setMethod("POST");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setURI("/context/dump");
request.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
request.setContent(body);
response.parse(tester.getResponses(request.generate()));
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertTrue(response.getContent().contains("aaaa,bbbbb"));
}
/*

View File

@ -45,6 +45,9 @@ import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.http.Part;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
@ -54,6 +57,8 @@ import javax.servlet.http.Part;
*/
public class MultiPartInputStream
{
private static final Logger LOG = Log.getLogger(MultiPartInputStream.class);
public static final MultipartConfigElement __DEFAULT_MULTIPART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
protected InputStream _in;
protected MultipartConfigElement _config;
@ -476,11 +481,24 @@ public class MultiPartInputStream
// Get first boundary
String line=((ReadLineInputStream)_in).readLine();
if (line == null || line.length() == 0)
throw new IOException("Missing content for multipart request");
if (!line.equals(boundary))
boolean badFormatLogged = false;
line=line.trim();
while (line != null && !line.equals(boundary))
{
if (!badFormatLogged)
{
LOG.warn("Badly formatted multipart request");
badFormatLogged = true;
}
line=((ReadLineInputStream)_in).readLine();
line=(line==null?line:line.trim());
}
if (line == null || line.length() == 0)
throw new IOException("Missing initial multi part boundary");

View File

@ -41,6 +41,7 @@ import javax.servlet.http.Part;
import junit.framework.TestCase;
import org.eclipse.jetty.util.MultiPartInputStream.MultiPart;
import org.hamcrest.core.IsNot;
/**
* MultiPartInputStreamTest
@ -215,6 +216,92 @@ public class MultiPartInputStreamTest extends TestCase
}
}
public void testLeadingWhitespaceBodyWithCRLF()
throws Exception
{
String body = " \n\n\n\r\n\r\n\r\n\r\n"+
"--AaB03x\r\n"+
"content-disposition: form-data; name=\"field1\"\r\n"+
"\r\n"+
"Joe Blow\r\n"+
"--AaB03x\r\n"+
"content-disposition: form-data; name=\"stuff\"; filename=\"" + "foo.txt" + "\"\r\n"+
"Content-Type: text/plain\r\n"+
"\r\n"+"aaaa"+
"bbbbb"+"\r\n" +
"--AaB03x--\r\n";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStream mpis = new MultiPartInputStream(new ByteArrayInputStream(body.getBytes()),
_contentType,
config,
_tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts, notNullValue());
assertThat(parts.size(), is(2));
Part field1 = mpis.getPart("field1");
assertThat(field1, notNullValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IO.copy(field1.getInputStream(), baos);
assertThat(baos.toString("US-ASCII"), is("Joe Blow"));
Part stuff = mpis.getPart("stuff");
assertThat(stuff, notNullValue());
baos = new ByteArrayOutputStream();
IO.copy(stuff.getInputStream(), baos);
assertTrue(baos.toString("US-ASCII").contains("aaaa"));
}
public void testLeadingWhitespaceBodyWithoutCRLF()
throws Exception
{
String body = " "+
"--AaB03x\r\n"+
"content-disposition: form-data; name=\"field1\"\r\n"+
"\r\n"+
"Joe Blow\r\n"+
"--AaB03x\r\n"+
"content-disposition: form-data; name=\"stuff\"; filename=\"" + "foo.txt" + "\"\r\n"+
"Content-Type: text/plain\r\n"+
"\r\n"+"aaaa"+
"bbbbb"+"\r\n" +
"--AaB03x--\r\n";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStream mpis = new MultiPartInputStream(new ByteArrayInputStream(body.getBytes()),
_contentType,
config,
_tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts, notNullValue());
assertThat(parts.size(), is(2));
Part field1 = mpis.getPart("field1");
assertThat(field1, notNullValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IO.copy(field1.getInputStream(), baos);
assertThat(baos.toString("US-ASCII"), is("Joe Blow"));
Part stuff = mpis.getPart("stuff");
assertThat(stuff, notNullValue());
baos = new ByteArrayOutputStream();
IO.copy(stuff.getInputStream(), baos);
assertTrue(baos.toString("US-ASCII").contains("bbbbb"));
}
public void testNonMultiPartRequest()
throws Exception
{