Jetty 12.0.x 10582 servlethttpwrapper (#10587)

* Issue #10582 fix DispatcherTest use of ServletRequestWrapper
This commit is contained in:
Jan Bartel 2023-10-12 07:46:04 +02:00 committed by GitHub
parent fcc88274a4
commit fffbab015d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 321 additions and 160 deletions

View File

@ -43,188 +43,186 @@ public class ServletRequestHttpWrapper extends ServletRequestWrapper implements
@Override
public String getAuthType()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public Cookie[] getCookies()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public long getDateHeader(String name)
{
return 0;
throw new UnsupportedOperationException();
}
@Override
public String getHeader(String name)
{
return null;
}
@Override
public Enumeration<String> getHeaders(String name)
{
return null;
throw new UnsupportedOperationException();
}
@Override
public Enumeration<String> getHeaderNames()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public Enumeration<String> getHeaders(String name)
{
throw new UnsupportedOperationException();
}
@Override
public int getIntHeader(String name)
{
return 0;
throw new UnsupportedOperationException();
}
@Override
public String getMethod()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getPathInfo()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getPathTranslated()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getContextPath()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getQueryString()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getRemoteUser()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public boolean isUserInRole(String role)
{
return false;
throw new UnsupportedOperationException();
}
@Override
public Principal getUserPrincipal()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getRequestedSessionId()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getRequestURI()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public StringBuffer getRequestURL()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getServletPath()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public HttpSession getSession(boolean create)
{
return null;
throw new UnsupportedOperationException();
}
@Override
public HttpSession getSession()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public boolean isRequestedSessionIdValid()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public boolean isRequestedSessionIdFromCookie()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public boolean isRequestedSessionIdFromURL()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
{
return false;
throw new UnsupportedOperationException();
}
@Override
public Part getPart(String name) throws IOException, ServletException
{
return null;
throw new UnsupportedOperationException();
}
@Override
public Collection<Part> getParts() throws IOException, ServletException
{
return null;
throw new UnsupportedOperationException();
}
@Override
public void login(String username, String password) throws ServletException
{
throw new UnsupportedOperationException();
}
@Override
public void logout() throws ServletException
{
throw new UnsupportedOperationException();
}
@Override
public String changeSessionId()
{
// TODO 3.1 Auto-generated method stub
return null;
throw new UnsupportedOperationException();
}
@Override
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
{
// TODO 3.1 Auto-generated method stub
return null;
throw new UnsupportedOperationException();
}
}

View File

@ -411,6 +411,66 @@ public class DispatcherTest
assertEquals(expected, responses);
}
@Test
public void testServletIncludeWelcome() throws Exception
{
_server.stop();
_contextHandler.setWelcomeFiles(new String[] {"index.x"});
_contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*");
ServletHolder defaultHolder = _contextHandler.addServlet(DefaultServlet.class, "/");
defaultHolder.setInitParameter("welcomeServlets", "true");
_contextHandler.addServlet(RogerThatServlet.class, "*.x");
_server.start();
String rawResponse = _connector.getResponse("""
GET /context/r/ HTTP/1.0\r
Host: localhost\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, rawResponse);
// direct include
rawResponse = _connector.getResponse("""
GET /context/dispatch/test?include=/index.x HTTP/1.0\r
Host: localhost\r
Connection: close\r
\r
""");
expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, rawResponse);
// include through welcome file based on servlet mapping
rawResponse = _connector.getResponse("""
GET /context/dispatch/test?include=/r/ HTTP/1.0\r
Host: localhost\r
Connection: close\r
\r
""");
expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, rawResponse);
}
public static Stream<Arguments> includeTests()
{
return Stream.of(
@ -1142,13 +1202,13 @@ public class DispatcherTest
if (request.getParameter("include") != null)
{
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("include"));
dispatcher.include(new ServletRequestWrapper(request), new ServletResponseWrapper(response));
dispatcher.include(new HttpServletRequestWrapper(request), new HttpServletResponseWrapper(response));
}
else if (request.getParameter("forward") != null)
{
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("forward"));
if (dispatcher != null)
dispatcher.forward(new ServletRequestWrapper(request), new ServletResponseWrapper(response));
dispatcher.forward(new HttpServletRequestWrapper(request), new HttpServletResponseWrapper(response));
else
response.sendError(404);
}

View File

@ -22,7 +22,9 @@ import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletRequestWrapper;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.ServletResponseWrapper;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
@ -43,9 +45,10 @@ public class ServletWrapperTest
{
private Server server;
private LocalConnector localConnector;
ServletContextHandler context;
@BeforeEach
public void startServer() throws Exception
public void initServer() throws Exception
{
server = new Server();
@ -56,16 +59,8 @@ public class ServletWrapperTest
connector.setPort(0);
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context = new ServletContextHandler();
context.setContextPath("/");
ServletHolder servletHolder = context.addServlet(HelloServlet.class, "/hello");
servletHolder.setAsyncSupported(false);
FilterHolder filterHolder = context.addFilter(WrapFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
filterHolder.setAsyncSupported(true);
server.setHandler(context);
server.start();
}
@AfterEach
@ -77,6 +72,14 @@ public class ServletWrapperTest
@Test
public void testWrapper() throws Exception
{
ServletHolder servletHolder = context.addServlet(HelloServlet.class, "/hello");
servletHolder.setAsyncSupported(false);
FilterHolder filterHolder = context.addFilter(NoopRequestWrapperFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
filterHolder.setAsyncSupported(true);
server.setHandler(context);
server.start();
StringBuilder req = new StringBuilder();
req.append("GET /hello HTTP/1.1\r\n");
req.append("Host: local\r\n");
@ -88,6 +91,27 @@ public class ServletWrapperTest
assertThat("Response.status", resp.getStatus(), is(200));
}
@Test
public void testServletRequestHttpWrapper() throws Exception
{
ServletHolder servletHolder = context.addServlet(WrappedRequestServlet.class, "/test");
FilterHolder filterHolder = context.addFilter(ServletRequestHttpWrapperFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
server.setHandler(context);
server.start();
StringBuilder req = new StringBuilder();
req.append("GET /test HTTP/1.1\r\n");
req.append("Host: local\r\n");
req.append("Connection: close\r\n");
req.append("\r\n");
String rawResponse = localConnector.getResponse(req.toString());
HttpTester.Response resp = HttpTester.parseResponse(rawResponse);
assertThat("Response.status", resp.getStatus(), is(200));
assertThat(resp.getContent(), is("Serviced!\n"));
}
public static class HelloServlet extends HttpServlet
{
@Override
@ -99,7 +123,25 @@ public class ServletWrapperTest
}
}
public static class WrapFilter implements Filter
public static class WrappedRequestServlet extends HttpServlet
{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
res.getWriter().println("Serviced!");
}
}
public static class ServletRequestHttpWrapperFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
chain.doFilter(new ServletRequestWrapper(request), new ServletResponseWrapper(response));
}
}
public static class NoopRequestWrapperFilter implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException

View File

@ -0,0 +1 @@
Do Not Delete

View File

@ -40,198 +40,196 @@ public class ServletRequestHttpWrapper extends ServletRequestWrapper implements
super(request);
}
@Override
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
{
throw new UnsupportedOperationException();
}
@Override
public String changeSessionId()
{
throw new UnsupportedOperationException();
}
@Override
public String getAuthType()
{
return null;
}
@Override
public Cookie[] getCookies()
{
return null;
}
@Override
public long getDateHeader(String name)
{
return 0;
}
@Override
public String getHeader(String name)
{
return null;
}
@Override
public Enumeration<String> getHeaders(String name)
{
return null;
}
@Override
public Enumeration<String> getHeaderNames()
{
return null;
}
@Override
public int getIntHeader(String name)
{
return 0;
}
@Override
public String getMethod()
{
return null;
}
@Override
public String getPathInfo()
{
return null;
}
@Override
public String getPathTranslated()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getContextPath()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public Cookie[] getCookies()
{
throw new UnsupportedOperationException();
}
@Override
public long getDateHeader(String name)
{
throw new UnsupportedOperationException();
}
@Override
public String getHeader(String name)
{
throw new UnsupportedOperationException();
}
@Override
public Enumeration<String> getHeaderNames()
{
throw new UnsupportedOperationException();
}
@Override
public Enumeration<String> getHeaders(String name)
{
throw new UnsupportedOperationException();
}
@Override
public int getIntHeader(String name)
{
throw new UnsupportedOperationException();
}
@Override
public String getMethod()
{
throw new UnsupportedOperationException();
}
@Override
public Part getPart(String name) throws IOException, ServletException
{
throw new UnsupportedOperationException();
}
@Override
public Collection<Part> getParts() throws IOException, ServletException
{
throw new UnsupportedOperationException();
}
@Override
public String getPathInfo()
{
throw new UnsupportedOperationException();
}
@Override
public String getPathTranslated()
{
throw new UnsupportedOperationException();
}
@Override
public String getQueryString()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getRemoteUser()
{
return null;
}
@Override
public boolean isUserInRole(String role)
{
return false;
}
@Override
public Principal getUserPrincipal()
{
return null;
}
@Override
public String getRequestedSessionId()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getRequestURI()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public StringBuffer getRequestURL()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public String getRequestedSessionId()
{
throw new UnsupportedOperationException();
}
@Override
public String getServletPath()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public HttpSession getSession(boolean create)
{
return null;
throw new UnsupportedOperationException();
}
@Override
public HttpSession getSession()
{
return null;
throw new UnsupportedOperationException();
}
@Override
public boolean isRequestedSessionIdValid()
public Principal getUserPrincipal()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public boolean isRequestedSessionIdFromCookie()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public boolean isRequestedSessionIdFromURL()
{
return false;
throw new UnsupportedOperationException();
}
@Override
@Deprecated(since = "Servlet API 2.1")
public boolean isRequestedSessionIdFromUrl()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
public boolean isRequestedSessionIdValid()
{
return false;
throw new UnsupportedOperationException();
}
@Override
public Part getPart(String name) throws IOException, ServletException
public boolean isUserInRole(String role)
{
return null;
}
@Override
public Collection<Part> getParts() throws IOException, ServletException
{
return null;
throw new UnsupportedOperationException();
}
@Override
public void login(String username, String password) throws ServletException
{
throw new UnsupportedOperationException();
}
@Override
public void logout() throws ServletException
{
}
@Override
public String changeSessionId()
{
// TODO 3.1 Auto-generated method stub
return null;
throw new UnsupportedOperationException();
}
@Override
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
{
// TODO 3.1 Auto-generated method stub
return null;
throw new UnsupportedOperationException();
}
}

View File

@ -418,6 +418,67 @@ public class DispatcherTest
}
}
@Test
public void testServletIncludeWelcome() throws Exception
{
createDefaultContextHandlerCollection();
_contextHandler.setWelcomeFiles(new String[] {"index.x"});
_contextHandler.addServlet(DispatchServletServlet.class, "/dispatch/*");
ServletHolder defaultHolder = _contextHandler.addServlet(DefaultServlet.class, "/");
defaultHolder.setInitParameter("welcomeServlets", "true");
_contextHandler.addServlet(RogerThatServlet.class, "*.x");
createServer(_contextCollection);
_server.start();
String rawResponse = _connector.getResponse("""
GET /context/r/ HTTP/1.0\r
Host: localhost\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, rawResponse);
// direct include
rawResponse = _connector.getResponse("""
GET /context/dispatch/test?include=/index.x HTTP/1.0\r
Host: localhost\r
Connection: close\r
\r
""");
expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, rawResponse);
// include through welcome file based on servlet mapping
rawResponse = _connector.getResponse("""
GET /context/dispatch/test?include=/r/ HTTP/1.0\r
Host: localhost\r
Connection: close\r
\r
""");
expected = """
HTTP/1.1 200 OK\r
Content-Length: 11\r
\r
Roger That!""";
assertEquals(expected, rawResponse);
}
@Test
public void testInclude() throws Exception
{
@ -1003,13 +1064,13 @@ public class DispatcherTest
if (request.getParameter("include") != null)
{
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("include"));
dispatcher.include(new ServletRequestWrapper(request), new ServletResponseWrapper(response));
dispatcher.include(new HttpServletRequestWrapper(request), new HttpServletResponseWrapper(response));
}
else if (request.getParameter("forward") != null)
{
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("forward"));
if (dispatcher != null)
dispatcher.forward(new ServletRequestWrapper(request), new ServletResponseWrapper(response));
dispatcher.forward(new HttpServletRequestWrapper(request), new HttpServletResponseWrapper(response));
else
response.sendError(404);
}

View File

@ -0,0 +1 @@
Do Not Delete