Skip to content

Commit

Permalink
prevent NullPointerException if contextPath is null
Browse files Browse the repository at this point in the history
Fixes #1656
Fixes #1655

PiperOrigin-RevId: 524371974
  • Loading branch information
sameb authored and Guice Team committed Apr 14, 2023
1 parent 65c38f8 commit 5b4c057
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,15 @@ public String getPathInfo() {
String servletPath = getServletPath();
int servletPathLength = servletPath.length();
String requestUri = getRequestURI();
pathInfo = requestUri.substring(getContextPath().length()).replaceAll("[/]{2,}", "/");
String contextPath = getContextPath();
// https://github.com/google/guice/issues/1655, contextPath is occasionally null
int contextPathLength = contextPath != null ? contextPath.length() : 0;
pathInfo = requestUri.substring(contextPathLength).replaceAll("[/]{2,}", "/");
// See: https://github.com/google/guice/issues/372
if (pathInfo.startsWith(servletPath)) {
pathInfo = pathInfo.substring(servletPathLength);
// Corner case: when servlet path & request path match exactly (without trailing '/'),
// then pathinfo is null.
// Corner case: when servlet path & request path match exactly
// (without trailing '/'), then pathinfo is null.
if (pathInfo.isEmpty() && servletPathLength > 0) {
pathInfo = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public final void testPathInfoWithServletStyleMatching() throws IOException, Ser
"/path", "/*", "/a file with spaces in name.html", "");
pathInfoWithServletStyleMatching(
"/path/Tam%C3%A1s%20nem%20m%C3%A1s.html", "/path", "/*", "/Tamás nem más.html", "");

// see https://github.com/google/guice/issues/1655
pathInfoWithServletStyleMatching("/index.html", null, "/*", "/index.html", "");
}

private void pathInfoWithServletStyleMatching(
Expand Down

0 comments on commit 5b4c057

Please sign in to comment.