Search in sources :

Example 6 with MultipartConfigElement

use of jakarta.servlet.MultipartConfigElement in project tomcat by apache.

the class TestMultipartConfig method testPartialMultipartConfigMaxFileSize.

@Test
public void testPartialMultipartConfigMaxFileSize() throws Exception {
    MultipartDef multipartDef = new MultipartDef();
    multipartDef.setMaxFileSize("1024");
    StandardWrapper servlet = config(multipartDef);
    MultipartConfigElement mce = servlet.getMultipartConfigElement();
    Assert.assertNotNull(mce);
    Assert.assertEquals("", mce.getLocation());
    Assert.assertEquals(1024, mce.getMaxFileSize());
    Assert.assertEquals(-1, mce.getMaxRequestSize());
    Assert.assertEquals(0, mce.getFileSizeThreshold());
}
Also used : MultipartConfigElement(jakarta.servlet.MultipartConfigElement) MultipartDef(org.apache.tomcat.util.descriptor.web.MultipartDef) StandardWrapper(org.apache.catalina.core.StandardWrapper) Test(org.junit.Test)

Example 7 with MultipartConfigElement

use of jakarta.servlet.MultipartConfigElement in project tomcat by apache.

the class TestMultipartConfig method testDefaultMultipartConfig.

@Test
public void testDefaultMultipartConfig() throws Exception {
    MultipartDef multipartDef = new MultipartDef();
    // Do not set any attributes on multipartDef: expect defaults
    StandardWrapper servlet = config(multipartDef);
    MultipartConfigElement mce = servlet.getMultipartConfigElement();
    Assert.assertNotNull(mce);
    Assert.assertEquals("", mce.getLocation());
    Assert.assertEquals(-1, mce.getMaxFileSize());
    Assert.assertEquals(-1, mce.getMaxRequestSize());
    Assert.assertEquals(0, mce.getFileSizeThreshold());
}
Also used : MultipartConfigElement(jakarta.servlet.MultipartConfigElement) MultipartDef(org.apache.tomcat.util.descriptor.web.MultipartDef) StandardWrapper(org.apache.catalina.core.StandardWrapper) Test(org.junit.Test)

Example 8 with MultipartConfigElement

use of jakarta.servlet.MultipartConfigElement in project tomcat by apache.

the class TestMultipartConfig method testPartialMultipartConfigMaxRequestSize.

@Test
public void testPartialMultipartConfigMaxRequestSize() throws Exception {
    MultipartDef multipartDef = new MultipartDef();
    multipartDef.setMaxRequestSize("10240");
    StandardWrapper servlet = config(multipartDef);
    MultipartConfigElement mce = servlet.getMultipartConfigElement();
    Assert.assertNotNull(mce);
    Assert.assertEquals("", mce.getLocation());
    Assert.assertEquals(-1, mce.getMaxFileSize());
    Assert.assertEquals(10240, mce.getMaxRequestSize());
    Assert.assertEquals(0, mce.getFileSizeThreshold());
}
Also used : MultipartConfigElement(jakarta.servlet.MultipartConfigElement) MultipartDef(org.apache.tomcat.util.descriptor.web.MultipartDef) StandardWrapper(org.apache.catalina.core.StandardWrapper) Test(org.junit.Test)

Example 9 with MultipartConfigElement

use of jakarta.servlet.MultipartConfigElement in project tomcat by apache.

the class TestMultipartConfig method testPartialMultipartConfigFileSizeThreshold.

@Test
public void testPartialMultipartConfigFileSizeThreshold() throws Exception {
    MultipartDef multipartDef = new MultipartDef();
    multipartDef.setFileSizeThreshold("24");
    StandardWrapper servlet = config(multipartDef);
    MultipartConfigElement mce = servlet.getMultipartConfigElement();
    Assert.assertNotNull(mce);
    Assert.assertEquals("", mce.getLocation());
    Assert.assertEquals(-1, mce.getMaxFileSize());
    Assert.assertEquals(-1, mce.getMaxRequestSize());
    Assert.assertEquals(24, mce.getFileSizeThreshold());
}
Also used : MultipartConfigElement(jakarta.servlet.MultipartConfigElement) MultipartDef(org.apache.tomcat.util.descriptor.web.MultipartDef) StandardWrapper(org.apache.catalina.core.StandardWrapper) Test(org.junit.Test)

Example 10 with MultipartConfigElement

use of jakarta.servlet.MultipartConfigElement in project tomcat by apache.

the class Request method parseParts.

private void parseParts(boolean explicit) {
    // Return immediately if the parts have already been parsed
    if (parts != null || partsParseException != null) {
        return;
    }
    Context context = getContext();
    MultipartConfigElement mce = getWrapper().getMultipartConfigElement();
    if (mce == null) {
        if (context.getAllowCasualMultipartParsing()) {
            mce = new MultipartConfigElement(null, connector.getMaxPostSize(), connector.getMaxPostSize(), connector.getMaxPostSize());
        } else {
            if (explicit) {
                partsParseException = new IllegalStateException(sm.getString("coyoteRequest.noMultipartConfig"));
                return;
            } else {
                parts = Collections.emptyList();
                return;
            }
        }
    }
    Parameters parameters = coyoteRequest.getParameters();
    parameters.setLimit(getConnector().getMaxParameterCount());
    boolean success = false;
    try {
        File location;
        String locationStr = mce.getLocation();
        if (locationStr == null || locationStr.length() == 0) {
            location = ((File) context.getServletContext().getAttribute(ServletContext.TEMPDIR));
        } else {
            // If relative, it is relative to TEMPDIR
            location = new File(locationStr);
            if (!location.isAbsolute()) {
                location = new File((File) context.getServletContext().getAttribute(ServletContext.TEMPDIR), locationStr).getAbsoluteFile();
            }
        }
        if (!location.exists() && context.getCreateUploadTargets()) {
            log.warn(sm.getString("coyoteRequest.uploadCreate", location.getAbsolutePath(), getMappingData().wrapper.getName()));
            if (!location.mkdirs()) {
                log.warn(sm.getString("coyoteRequest.uploadCreateFail", location.getAbsolutePath()));
            }
        }
        if (!location.isDirectory()) {
            parameters.setParseFailedReason(FailReason.MULTIPART_CONFIG_INVALID);
            partsParseException = new IOException(sm.getString("coyoteRequest.uploadLocationInvalid", location));
            return;
        }
        // Create a new file upload handler
        DiskFileItemFactory factory = new DiskFileItemFactory();
        try {
            factory.setRepository(location.getCanonicalFile());
        } catch (IOException ioe) {
            parameters.setParseFailedReason(FailReason.IO_ERROR);
            partsParseException = ioe;
            return;
        }
        factory.setSizeThreshold(mce.getFileSizeThreshold());
        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileItemFactory(factory);
        upload.setFileSizeMax(mce.getMaxFileSize());
        upload.setSizeMax(mce.getMaxRequestSize());
        parts = new ArrayList<>();
        try {
            List<FileItem> items = upload.parseRequest(new ServletRequestContext(this));
            int maxPostSize = getConnector().getMaxPostSize();
            int postSize = 0;
            Charset charset = getCharset();
            for (FileItem item : items) {
                ApplicationPart part = new ApplicationPart(item, location);
                parts.add(part);
                if (part.getSubmittedFileName() == null) {
                    String name = part.getName();
                    if (maxPostSize >= 0) {
                        // Have to calculate equivalent size. Not completely
                        // accurate but close enough.
                        postSize += name.getBytes(charset).length;
                        // Equals sign
                        postSize++;
                        // Value length
                        postSize += part.getSize();
                        // Value separator
                        postSize++;
                        if (postSize > maxPostSize) {
                            parameters.setParseFailedReason(FailReason.POST_TOO_LARGE);
                            throw new IllegalStateException(sm.getString("coyoteRequest.maxPostSizeExceeded"));
                        }
                    }
                    String value = null;
                    try {
                        value = part.getString(charset.name());
                    } catch (UnsupportedEncodingException uee) {
                    // Not possible
                    }
                    parameters.addParameter(name, value);
                }
            }
            success = true;
        } catch (InvalidContentTypeException e) {
            parameters.setParseFailedReason(FailReason.INVALID_CONTENT_TYPE);
            partsParseException = new ServletException(e);
        } catch (SizeException e) {
            parameters.setParseFailedReason(FailReason.POST_TOO_LARGE);
            checkSwallowInput();
            partsParseException = new IllegalStateException(e);
        } catch (IOException e) {
            parameters.setParseFailedReason(FailReason.IO_ERROR);
            partsParseException = new IOException(e);
        } catch (IllegalStateException e) {
            // addParameters() will set parseFailedReason
            checkSwallowInput();
            partsParseException = e;
        }
    } finally {
        // respect to changes in the remainder of the method.
        if (partsParseException != null || !success) {
            parameters.setParseFailedReason(FailReason.UNKNOWN);
        }
    }
}
Also used : ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) ServletContext(jakarta.servlet.ServletContext) AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) Parameters(org.apache.tomcat.util.http.Parameters) InvalidContentTypeException(org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException) ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DiskFileItemFactory(org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory) ServletException(jakarta.servlet.ServletException) FileItem(org.apache.tomcat.util.http.fileupload.FileItem) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) ServletFileUpload(org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload) ApplicationPart(org.apache.catalina.core.ApplicationPart) File(java.io.File) SizeException(org.apache.tomcat.util.http.fileupload.impl.SizeException)

Aggregations

MultipartConfigElement (jakarta.servlet.MultipartConfigElement)11 StandardWrapper (org.apache.catalina.core.StandardWrapper)6 MultipartDef (org.apache.tomcat.util.descriptor.web.MultipartDef)6 Test (org.junit.Test)6 ServletException (jakarta.servlet.ServletException)2 Connector (org.eclipse.jetty.server.Connector)2 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)2 Server (org.eclipse.jetty.server.Server)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 AsyncContext (jakarta.servlet.AsyncContext)1 Servlet (jakarta.servlet.Servlet)1 ServletContext (jakarta.servlet.ServletContext)1 SessionCookieConfig (jakarta.servlet.SessionCookieConfig)1 MultipartConfig (jakarta.servlet.annotation.MultipartConfig)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1