Skip to content

Commit 7288bc7

Browse files
committed
With mapperContextRootRedirectEnabled ste to false, the redirect needs to be handled elsewhere. - Ensure the Mapper does not add the '/' handling the redirect - Handle the redirect in the DefaultServlet - Add a redirect to FORM auth if auth is occurring at the context root else the login page could be submitted to the wrong web application git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk@1717212 13f79535-47bb-0310-9956-ffa450edef68
1 parent fb08113 commit 7288bc7

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

java/org/apache/catalina/authenticator/FormAuthenticator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ public boolean authenticate(Request request,
236236

237237
// No -- Save this request and redirect to the form login page
238238
if (!loginAction) {
239+
// If this request was to the root of the context without a trailing
240+
// '/', need to redirect to add it else the submit of the login form
241+
// may not go to the correct web application
242+
if (request.getServletPath().length() == 0 && request.getPathInfo() == null) {
243+
StringBuilder location = new StringBuilder(requestURI);
244+
location.append('/');
245+
if (request.getQueryString() != null) {
246+
location.append('?');
247+
location.append(request.getQueryString());
248+
}
249+
response.sendRedirect(response.encodeRedirectURL(location.toString()));
250+
return false;
251+
}
252+
239253
session = request.getSessionInternal(true);
240254
if (log.isDebugEnabled()) {
241255
log.debug("Save request in session '" + session.getIdInternal() + "'");

java/org/apache/catalina/servlets/DefaultServlet.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ public void init() throws ServletException {
375375
* @param request The servlet request we are processing
376376
*/
377377
protected String getRelativePath(HttpServletRequest request) {
378+
return getRelativePath(request, false);
379+
}
380+
381+
protected String getRelativePath(HttpServletRequest request, boolean allowEmptyPath) {
378382
// IMPORTANT: DefaultServlet can be mapped to '/' or '/path/*' but always
379383
// serves resources from the web app root with context rooted paths.
380384
// i.e. it can not be used to mount the web app root under a sub-path
@@ -400,7 +404,7 @@ protected String getRelativePath(HttpServletRequest request) {
400404
if (pathInfo != null) {
401405
result.append(pathInfo);
402406
}
403-
if (result.length() == 0) {
407+
if (result.length() == 0 && !allowEmptyPath) {
404408
result.append('/');
405409
}
406410

@@ -778,7 +782,8 @@ protected void serveResource(HttpServletRequest request,
778782
boolean serveContent = content;
779783

780784
// Identify the requested resource path
781-
String path = getRelativePath(request);
785+
String path = getRelativePath(request, true);
786+
782787
if (debug > 0) {
783788
if (serveContent)
784789
log("DefaultServlet.serveResource: Serving resource '" +
@@ -788,6 +793,12 @@ protected void serveResource(HttpServletRequest request,
788793
path + "' headers only");
789794
}
790795

796+
if (path.length() == 0) {
797+
// Context root redirect
798+
doDirectoryRedirect(request, response);
799+
return;
800+
}
801+
791802
CacheEntry cacheEntry = resources.lookupCache(path);
792803

793804
if (!cacheEntry.exists) {
@@ -857,13 +868,7 @@ protected void serveResource(HttpServletRequest request,
857868
if (cacheEntry.context != null) {
858869

859870
if (!path.endsWith("/")) {
860-
StringBuilder location = new StringBuilder(request.getRequestURI());
861-
location.append('/');
862-
if (request.getQueryString() != null) {
863-
location.append('?');
864-
location.append(request.getQueryString());
865-
}
866-
response.sendRedirect(response.encodeRedirectURL(location.toString()));
871+
doDirectoryRedirect(request, response);
867872
return;
868873
}
869874

@@ -1074,6 +1079,16 @@ protected void serveResource(HttpServletRequest request,
10741079

10751080
}
10761081

1082+
private void doDirectoryRedirect(HttpServletRequest request, HttpServletResponse response)
1083+
throws IOException {
1084+
StringBuilder location = new StringBuilder(request.getRequestURI());
1085+
location.append('/');
1086+
if (request.getQueryString() != null) {
1087+
location.append('?');
1088+
location.append(request.getQueryString());
1089+
}
1090+
response.sendRedirect(response.encodeRedirectURL(location.toString()));
1091+
}
10771092

10781093
/**
10791094
* Parse the content-range header.

java/org/apache/catalina/servlets/WebdavServlet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ protected boolean checkIfHeaders(HttpServletRequest request,
430430
*/
431431
@Override
432432
protected String getRelativePath(HttpServletRequest request) {
433+
return getRelativePath(request, false);
434+
}
435+
436+
@Override
437+
protected String getRelativePath(HttpServletRequest request, boolean allowEmptyPath) {
433438
String pathInfo;
434439

435440
if (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null) {

java/org/apache/tomcat/util/http/mapper/Mapper.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -887,20 +887,13 @@ private final void internalMapWrapper(ContextVersion contextVersion,
887887

888888
int pathOffset = path.getOffset();
889889
int pathEnd = path.getEnd();
890-
int servletPath = pathOffset;
891890
boolean noServletPath = false;
892891

893892
int length = contextVersion.path.length();
894-
if (length != (pathEnd - pathOffset)) {
895-
servletPath = pathOffset + length;
896-
} else {
893+
if (length == (pathEnd - pathOffset)) {
897894
noServletPath = true;
898-
path.append('/');
899-
pathOffset = path.getOffset();
900-
pathEnd = path.getEnd();
901-
servletPath = pathOffset+length;
902895
}
903-
896+
int servletPath = pathOffset + length;
904897
path.setOffset(servletPath);
905898

906899
// Rule 1 -- Exact Match
@@ -938,8 +931,10 @@ private final void internalMapWrapper(ContextVersion contextVersion,
938931
if(mappingData.wrapper == null && noServletPath &&
939932
contextVersion.mapperContextRootRedirectEnabled) {
940933
// The path is empty, redirect to "/"
934+
path.append('/');
935+
pathEnd = path.getEnd();
941936
mappingData.redirectPath.setChars
942-
(path.getBuffer(), pathOffset, pathEnd-pathOffset);
937+
(path.getBuffer(), pathOffset, pathEnd - pathOffset);
943938
path.setEnd(pathEnd - 1);
944939
return;
945940
}
@@ -1060,7 +1055,11 @@ private final void internalMapWrapper(ContextVersion contextVersion,
10601055
Object file = null;
10611056
String pathStr = path.toString();
10621057
try {
1063-
file = contextVersion.resources.lookup(pathStr);
1058+
if (pathStr.length() == 0) {
1059+
file = contextVersion.resources.lookup("/");
1060+
} else {
1061+
file = contextVersion.resources.lookup(pathStr);
1062+
}
10641063
} catch(NamingException nex) {
10651064
// Swallow, since someone else handles the 404
10661065
}

webapps/docs/changelog.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
issues do not "pop up" wrt. others).
5959
-->
6060
<section name="Tomcat 7.0.67 (violetagg)">
61+
<subsection name="Catalina">
62+
<changelog>
63+
<fix>
64+
<bug>58660</bug>: Correct a regression in 8.0.29 caused by the change
65+
that moved the redirection for context roots from the Mapper to the
66+
Default Servlet. (markt)
67+
</fix>
68+
</changelog>
69+
</subsection>
6170
<subsection name="WebSocket">
6271
<changelog>
6372
<fix>

0 commit comments

Comments
 (0)