MCPcopy Create free account
hub / github.com/apache/tomcat / getContextPath

Method getContextPath

java/org/apache/catalina/connector/Request.java:1953–2022  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1951
1952
1953 @Override
1954 public String getContextPath() {
1955 int lastSlash = mappingData.contextSlashCount;
1956 // Special case handling for the root context
1957 if (lastSlash == 0) {
1958 return "";
1959 }
1960
1961 String canonicalContextPath = getServletContext().getContextPath();
1962
1963 String uri = getRequestURI();
1964 int pos = 0;
1965 if (!getContext().getAllowMultipleLeadingForwardSlashInPath()) {
1966 // Ensure that the returned value only starts with a single '/'.
1967 // This prevents the value being misinterpreted as a protocol-relative
1968 // URI if used with sendRedirect().
1969 do {
1970 pos++;
1971 } while (pos < uri.length() && uri.charAt(pos) == '/');
1972 pos--;
1973 uri = uri.substring(pos);
1974 }
1975
1976 char[] uriChars = uri.toCharArray();
1977 // Need at least the number of slashes in the context path
1978 while (lastSlash > 0) {
1979 pos = nextSlash(uriChars, pos + 1);
1980 if (pos == -1) {
1981 break;
1982 }
1983 lastSlash--;
1984 }
1985 // Now allow for path parameters, normalization and/or encoding.
1986 // Essentially, keep extending the candidate path up to the next slash
1987 // until the decoded and normalized candidate path (with the path
1988 // parameters removed) is the same as the canonical path.
1989 String candidate;
1990 if (pos == -1) {
1991 candidate = uri;
1992 } else {
1993 candidate = uri.substring(0, pos);
1994 }
1995 candidate = removePathParameters(candidate);
1996 candidate = UDecoder.URLDecode(candidate, connector.getURICharset());
1997 candidate = org.apache.tomcat.util.http.RequestUtil.normalize(candidate);
1998 boolean match = canonicalContextPath.equals(candidate);
1999 while (!match && pos != -1) {
2000 pos = nextSlash(uriChars, pos + 1);
2001 if (pos == -1) {
2002 candidate = uri;
2003 } else {
2004 candidate = uri.substring(0, pos);
2005 }
2006 candidate = removePathParameters(candidate);
2007 candidate = UDecoder.URLDecode(candidate, connector.getURICharset());
2008 candidate = org.apache.tomcat.util.http.RequestUtil.normalize(candidate);
2009 match = canonicalContextPath.equals(candidate);
2010 }

Callers 1

dispatchMethod · 0.95

Calls 15

getServletContextMethod · 0.95
getRequestURIMethod · 0.95
getContextMethod · 0.95
nextSlashMethod · 0.95
removePathParametersMethod · 0.95
URLDecodeMethod · 0.95
lengthMethod · 0.80
charAtMethod · 0.80
toCharArrayMethod · 0.80
getURICharsetMethod · 0.80
getContextPathMethod · 0.65

Tested by

no test coverage detected