Skip to content

Commit

Permalink
Issue #12650 Fix multipart attribute for forwards. (#12656)
Browse files Browse the repository at this point in the history
* Issue #12650 Fix multipart attribute for forwards.

Only return the org.eclipse.jetty.multipartConfig attribute name on
getAttributeNames iff the servlet that is the target of the forward
supports multipart.
  • Loading branch information
janbartel authored Dec 20, 2024
1 parent aa65102 commit 818bd0e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public Object getAttribute(String name)
public Enumeration<String> getAttributeNames()
{
ArrayList<String> names = new ArrayList<>(Collections.list(super.getAttributeNames()));

//only return the multipart attribute name if this servlet mapping has multipart config
if (names.contains(ServletContextRequest.MULTIPART_CONFIG_ELEMENT) && _mappedServlet.getServletHolder().getMultipartConfigElement() == null)
names.remove(ServletContextRequest.MULTIPART_CONFIG_ELEMENT);

//Servlet Spec 9.4.2 no forward attributes if a named dispatcher
if (_named != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
Expand All @@ -30,6 +31,7 @@
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.GenericServlet;
import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContext;
Expand Down Expand Up @@ -143,6 +145,33 @@ public void testForward() throws Exception

assertEquals(expected, rawResponse);
}

@Test
public void testMultiPartForwardAttribute() throws Exception
{
ServletHolder forwardServlet = new ServletHolder(new ForwardServlet());
forwardServlet.getRegistration().setMultipartConfig(new MultipartConfigElement("/tmp"));
_contextHandler.addServlet(forwardServlet, "/ForwardServlet/*");
_contextHandler.addServlet(AssertMultiPartForwardServlet.class, "/AssertMultiPartForwardServlet/*");

String rawResponse = _connector.getResponse("""
GET /context/ForwardServlet?do=assertmultipart&do=more&test=1 HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");

String expected = """
HTTP/1.1 200 OK\r
Content-Type: text/html\r
Content-Length: 42\r
Connection: close\r
\r
org.eclipse.jetty.multipartConfig = null\r
""";

assertEquals(expected, rawResponse);
}

@Test
public void testFowardThenForward() throws Exception
Expand Down Expand Up @@ -993,6 +1022,8 @@ else if (request.getParameter("do").equals("req.echo"))
dispatcher = request.getRequestDispatcher(request.getParameter("uri"));
else if (request.getParameter("do").equals("always"))
dispatcher = request.getRequestDispatcher("/AlwaysForwardServlet");
else if (request.getParameter("do").equals("assertmultipart"))
dispatcher = getServletContext().getRequestDispatcher("/AssertMultiPartForwardServlet?do=end&do=the");
assert dispatcher != null;
dispatcher.forward(request, response);
}
Expand Down Expand Up @@ -1503,6 +1534,17 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
}
}

public static class AssertMultiPartForwardServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getOutputStream().println("org.eclipse.jetty.multipartConfig = " + request.getAttribute("org.eclipse.jetty.multipartConfig"));
}
}

public static class AssertNonUTF8ForwardServlet extends HttpServlet implements Servlet
{
@Override
Expand Down

0 comments on commit 818bd0e

Please sign in to comment.