Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/jetty-12.0.x' into jetty-12.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel committed Dec 20, 2024
2 parents 15d500e + 818bd0e commit a2fe8c2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ 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)
return Collections.enumeration(names);
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
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ 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)
return Collections.enumeration(names);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 @@ -144,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 testForwardThenForward() throws Exception
{
Expand Down Expand Up @@ -993,6 +1021,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 +1533,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 a2fe8c2

Please sign in to comment.