Skip to content

Commit

Permalink
Issue #9906 Empty path info
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel committed Jun 13, 2023
1 parent d3e88a9 commit eab758f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,6 @@ public void dump(Appendable out, String indent) throws IOException
new DumpableCollection("initparams " + this, getInitParams().entrySet()));
}

/**
* @return the allowNullPathInfo true if /context is not redirected to /context/
*/
@ManagedAttribute("Checks if the /context is not redirected to /context/")
public boolean getAllowNullPathInfo()
{
return _allowNullPathInfo;
}

/**
* @param allowNullPathInfo true if /context is not redirected to /context/
*/
public void setAllowNullPathInfo(boolean allowNullPathInfo)
{
_allowNullPathInfo = allowNullPathInfo;
}

public boolean isUsingSecurityManager()
{
return _usingSecurityManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public boolean handle(Request request, Response response, Callback callback) thr
*/
public MatchedResource<MappedServlet> getMatchedServlet(String target)
{
if (target.startsWith("/"))
if (target.startsWith("/") || target.length() == 0)
{
if (_servletPathMap == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2432,4 +2432,35 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
String setCookieValue = response.get(HttpHeader.SET_COOKIE);
assertThat(setCookieValue, containsString("example=bogus; SameSite=Strict"));
}

@Test
public void testEmptyPathInfo() throws Exception
{
ServletContextHandler context = new ServletContextHandler(null, "/c1", ServletContextHandler.NO_SESSIONS);
context.setAllowNullPathInContext(true);
context.addServlet(new ServletHolder("default-servlet", new HttpServlet()
{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().write("OK2\n");
resp.getWriter().close();
}
}), "/");

_server.setHandler(context);
_server.start();
String rawRequest = """
GET /c1 HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""";

String rawResponse = _connector.getResponse(rawRequest);
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("OK"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public void doHandle(String target, Request baseRequest, HttpServletRequest requ
*/
public MatchedResource<MappedServlet> getMatchedServlet(String target)
{
if (target.startsWith("/"))
if (target.startsWith("/") || target.length() == 0)
{
if (_servletPathMap == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2272,4 +2272,35 @@ public void init() throws ServletException
assertThat(response, containsString("200 OK"));
assertThat(response, containsString("/three"));
}

@Test
public void testEmptyPathInfo() throws Exception
{
ServletContextHandler context = new ServletContextHandler(null, "/c1", ServletContextHandler.NO_SESSIONS);
context.setAllowNullPathInfo(true);
context.addServlet(new ServletHolder("default-servlet", new HttpServlet()
{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().write("OK2\n");
resp.getWriter().close();
}
}), "/");

_server.setHandler(context);
_server.start();
String rawRequest = """
GET /c1 HTTP/1.1\r
Host: localhost\r
Connection: close\r
\r
""";

String rawResponse = _connector.getResponse(rawRequest);
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getContent(), containsString("OK"));
}
}

0 comments on commit eab758f

Please sign in to comment.