use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testPartFileNotDeleted.
@Test
public void testPartFileNotDeleted() throws Exception {
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(createMultipartRequestString("tptfd").getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
MultiPart part = (MultiPart) mpis.getPart("stuff");
File stuff = ((MultiPartInputStreamParser.MultiPart) part).getFile();
// longer than 100 bytes, should already be a tmp file
assertThat(stuff, notNullValue());
part.write("tptfd.txt");
File tptfd = new File(_dirname + File.separator + "tptfd.txt");
assertThat(tptfd.exists(), is(true));
//got renamed
assertThat(stuff.exists(), is(false));
part.cleanUp();
//explicitly written file did not get removed after cleanup
assertThat(tptfd.exists(), is(true));
//clean up test
tptfd.deleteOnExit();
}
use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testFinalBoundaryOnly.
@Test
public void testFinalBoundaryOnly() throws Exception {
String delimiter = "\r\n";
final String boundary = "MockMultiPartTestBoundary";
// Malformed multipart request body containing only an arbitrary string of text, followed by the final boundary marker, delimited by empty lines.
String str = delimiter + "Hello world" + // Two delimiter markers, which make an empty line.
delimiter + delimiter + "--" + boundary + "--" + delimiter;
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), "multipart/form-data, boundary=" + boundary, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertTrue(mpis.getParts().isEmpty());
}
use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testMulti.
private void testMulti(String filename) throws IOException, ServletException, InterruptedException {
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(createMultipartRequestString(filename).getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(2));
//field 1 too small to go into tmp file, should be in internal buffer
Part field1 = mpis.getPart("field1");
assertThat(field1, notNullValue());
assertThat(field1.getName(), is("field1"));
InputStream is = field1.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
IO.copy(is, os);
assertEquals("Joe Blow", new String(os.toByteArray()));
assertEquals(8, field1.getSize());
//in internal buffer
assertNotNull(((MultiPartInputStreamParser.MultiPart) field1).getBytes());
field1.write("field1.txt");
//no longer in internal buffer
assertNull(((MultiPartInputStreamParser.MultiPart) field1).getBytes());
File f = new File(_dirname + File.separator + "field1.txt");
assertTrue(f.exists());
//write after having already written
field1.write("another_field1.txt");
File f2 = new File(_dirname + File.separator + "another_field1.txt");
assertTrue(f2.exists());
//should have been renamed
assertFalse(f.exists());
//file should be deleted
field1.delete();
//original file was renamed
assertFalse(f.exists());
//2nd written file was explicitly deleted
assertFalse(f2.exists());
MultiPart stuff = (MultiPart) mpis.getPart("stuff");
assertThat(stuff.getSubmittedFileName(), is(filename));
assertThat(stuff.getContentType(), is("text/plain"));
assertThat(stuff.getHeader("Content-Type"), is("text/plain"));
assertThat(stuff.getHeaders("content-type").size(), is(1));
assertThat(stuff.getHeader("content-disposition"), is("form-data; name=\"stuff\"; filename=\"" + filename + "\""));
assertThat(stuff.getHeaderNames().size(), is(2));
assertThat(stuff.getSize(), is(51L));
File tmpfile = ((MultiPartInputStreamParser.MultiPart) stuff).getFile();
// longer than 100 bytes, should already be a tmp file
assertThat(tmpfile, notNullValue());
//not in an internal buffer
assertThat(((MultiPartInputStreamParser.MultiPart) stuff).getBytes(), nullValue());
assertThat(tmpfile.exists(), is(true));
assertThat(tmpfile.getName(), is(not("stuff with space.txt")));
stuff.write(filename);
f = new File(_dirname + File.separator + filename);
assertThat(f.exists(), is(true));
assertThat(tmpfile.exists(), is(false));
try {
stuff.getInputStream();
} catch (Exception e) {
fail("Part.getInputStream() after file rename operation");
}
//clean up after test
f.deleteOnExit();
}
use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testPartTmpFileDeletion.
@Test
public void testPartTmpFileDeletion() throws Exception {
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(createMultipartRequestString("tptfd").getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
MultiPart part = (MultiPart) mpis.getPart("stuff");
File stuff = ((MultiPartInputStreamParser.MultiPart) part).getFile();
// longer than 100 bytes, should already be a tmp file
assertThat(stuff, notNullValue());
assertThat(stuff.exists(), is(true));
part.cleanUp();
//tmp file was removed after cleanup
assertThat(stuff.exists(), is(false));
}
use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testFileTooBigThrowsErrorOnGetParts.
@Test
public void testFileTooBigThrowsErrorOnGetParts() 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 {
//caused parsing
parts = mpis.getParts();
fail("stuff.txt should have been larger than maxFileSize");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Multipart Mime part"));
}
//test again after the parsing
try {
//caused parsing
parts = mpis.getParts();
fail("stuff.txt should have been larger than maxFileSize");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Multipart Mime part"));
}
}
Aggregations