use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testFileTooBig.
@Test
public void testFileTooBig() throws Exception {
MultipartConfigElement config = new MultipartConfigElement(_dirname, 40, 1024, 30);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = null;
try {
parts = mpis.getParts();
fail("stuff.txt should have been larger than maxFileSize");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Multipart Mime part"));
}
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testNoBoundaryRequest.
@Test
public void testNoBoundaryRequest() throws Exception {
String str = "--\r\n" + "Content-Disposition: form-data; name=\"fileName\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "abc\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"desc\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "123\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"title\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "ttt\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"datafile5239138112980980385.txt\"; filename=\"datafile5239138112980980385.txt\"\r\n" + "Content-Type: application/octet-stream; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: binary\r\n" + "\r\n" + "000\r\n" + "----\r\n";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), "multipart/form-data", config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(4));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Part fileName = mpis.getPart("fileName");
assertThat(fileName, notNullValue());
assertThat(fileName.getSize(), is(3L));
IO.copy(fileName.getInputStream(), baos);
assertThat(baos.toString("US-ASCII"), is("abc"));
baos = new ByteArrayOutputStream();
Part desc = mpis.getPart("desc");
assertThat(desc, notNullValue());
assertThat(desc.getSize(), is(3L));
IO.copy(desc.getInputStream(), baos);
assertThat(baos.toString("US-ASCII"), is("123"));
baos = new ByteArrayOutputStream();
Part title = mpis.getPart("title");
assertThat(title, notNullValue());
assertThat(title.getSize(), is(3L));
IO.copy(title.getInputStream(), baos);
assertThat(baos.toString("US-ASCII"), is("ttt"));
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testBadlyEncodedFilename.
@Test
public void testBadlyEncodedFilename() throws Exception {
String contents = "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"" + "Taken on Aug 22 \\ 2012.jpg" + "\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "stuff" + "aaa" + "\r\n" + "--AaB03x--\r\n";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(contents.getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(1));
assertThat(((MultiPartInputStreamParser.MultiPart) parts.iterator().next()).getSubmittedFileName(), is("Taken on Aug 22 \\ 2012.jpg"));
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testBufferOverflowNoCRLF.
@Test
public void testBufferOverflowNoCRLF() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("--AaB03x".getBytes());
for (//create content that will overrun default buffer size of BufferedInputStream
int i = 0; //create content that will overrun default buffer size of BufferedInputStream
i < 8500; //create content that will overrun default buffer size of BufferedInputStream
i++) {
baos.write('a');
}
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(baos.toByteArray()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
try {
mpis.getParts();
fail("Multipart buffer overrun");
} catch (IOException e) {
assertTrue(e.getMessage().startsWith("Buffer size exceeded"));
}
}
use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testWriteFilesIfContentDispositionFilename.
@Test
public void testWriteFilesIfContentDispositionFilename() throws Exception {
String s = "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"; filename=\"frooble.txt\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "sss" + "aaa" + "\r\n" + "--AaB03x--\r\n";
//all default values for multipartconfig, ie file size threshold 0
MultipartConfigElement config = new MultipartConfigElement(_dirname);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(s.getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
mpis.setWriteFilesWithFilenames(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(2));
//has a filename, should be written to a file
Part field1 = mpis.getPart("field1");
File f = ((MultiPartInputStreamParser.MultiPart) field1).getFile();
// longer than 100 bytes, should already be a tmp file
assertThat(f, notNullValue());
Part stuff = mpis.getPart("stuff");
//should only be in memory, no filename
f = ((MultiPartInputStreamParser.MultiPart) stuff).getFile();
assertThat(f, nullValue());
}
Aggregations