Skip to content

Commit

Permalink
Issue #6451 - Request.getServletPath returns null for root mapping
Browse files Browse the repository at this point in the history
Issue #6451 - Request.getServletPath not following spec
Force a pathInContext for ROOT servlet matches.
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
Signed-off-by: Greg Wilkins <gregw@webtide.com>
Co-authored-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
joakime committed Jun 21, 2021
1 parent 1a594ce commit 9945225
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Expand Up @@ -1522,9 +1522,11 @@ public static class MappedServlet
switch (pathSpec.getGroup())
{
case EXACT:
case ROOT:
_servletPathMapping = new ServletPathMapping(_pathSpec, _servletHolder.getName(), _pathSpec.getPrefix());
break;
case ROOT:
_servletPathMapping = new ServletPathMapping(_pathSpec, _servletHolder.getName(), "/");
break;
default:
_servletPathMapping = null;
break;
Expand Down
Expand Up @@ -724,6 +724,39 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
assertEquals("getContextPath()=[]", response.getContent(), "response content");
}

/**
* Address spec "3.5. Request Path Elements" with respect to Servlet Path.
*/
@ParameterizedTest
@ValueSource(strings = {"/*", ""})
public void testGetServletPathEmpty(String pathSpec) throws Exception
{
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath("");
contextHandler.addServlet(new ServletHolder(new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.setContentType("text/plain");
resp.setCharacterEncoding("utf-8");
resp.getWriter().printf("getServletPath()=[%s]", req.getServletPath());
}
}), pathSpec);
_server.setHandler(contextHandler);
_server.start();

StringBuilder rawRequest = new StringBuilder();
rawRequest.append("GET / HTTP/1.1\r\n");
rawRequest.append("Host: local\r\n");
rawRequest.append("Connection: close\r\n");
rawRequest.append("\r\n");
String rawResponse = _connector.getResponse(rawRequest.toString());
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertEquals(200, response.getStatus(), "response status");
assertEquals("getServletPath()=[]", response.getContent(), "response content");
}

@Test
public void testGetSetSessionTimeout() throws Exception
{
Expand Down

0 comments on commit 9945225

Please sign in to comment.