Skip to content

Commit 816552a

Browse files
committed
With mapperContextRootRedirectEnabled ste to false, the redirect needs to be handled elsewhere. - Ensure the Mapper does not add the '/' - 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/trunk@1716882 13f79535-47bb-0310-9956-ffa450edef68
1 parent 24c8d8c commit 816552a

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,20 @@ public boolean authenticate(Request request, HttpServletResponse response)
219219

220220
// No -- Save this request and redirect to the form login page
221221
if (!loginAction) {
222+
// If this request was to the root of the context without a trailing
223+
// '/', need to redirect to add it else the submit of the login form
224+
// may not go to the correct web application
225+
if (request.getServletPath().length() == 0 && request.getPathInfo() == null) {
226+
StringBuilder location = new StringBuilder(requestURI);
227+
location.append('/');
228+
if (request.getQueryString() != null) {
229+
location.append('?');
230+
location.append(request.getQueryString());
231+
}
232+
response.sendRedirect(response.encodeRedirectURL(location.toString()));
233+
return false;
234+
}
235+
222236
session = request.getSessionInternal(true);
223237
if (log.isDebugEnabled()) {
224238
log.debug("Save request in session '" + session.getIdInternal() + "'");

java/org/apache/catalina/mapper/Mapper.java

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

836836
int pathOffset = path.getOffset();
837837
int pathEnd = path.getEnd();
838-
int servletPath = pathOffset;
839838
boolean noServletPath = false;
840839

841840
int length = contextVersion.path.length();
842-
if (length != (pathEnd - pathOffset)) {
843-
servletPath = pathOffset + length;
844-
} else {
841+
if (length == (pathEnd - pathOffset)) {
845842
noServletPath = true;
846-
path.append('/');
847-
pathOffset = path.getOffset();
848-
pathEnd = path.getEnd();
849-
servletPath = pathOffset+length;
850843
}
851-
844+
int servletPath = pathOffset + length;
852845
path.setOffset(servletPath);
853846

854847
// Rule 1 -- Exact Match
@@ -1002,7 +995,13 @@ private final void internalMapWrapper(ContextVersion contextVersion,
1002995
char[] buf = path.getBuffer();
1003996
if (contextVersion.resources != null && buf[pathEnd -1 ] != '/') {
1004997
String pathStr = path.toString();
1005-
WebResource file = contextVersion.resources.getResource(pathStr);
998+
WebResource file;
999+
// Handle context root
1000+
if (pathStr.length() == 0) {
1001+
file = contextVersion.resources.getResource("/");
1002+
} else {
1003+
file = contextVersion.resources.getResource(pathStr);
1004+
}
10061005
if (file != null && file.isDirectory() &&
10071006
mappingData.context.getMapperDirectoryRedirectEnabled()) {
10081007
// Note: this mutates the path: do not do any processing

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ public void init() throws ServletException {
331331
* @param request The servlet request we are processing
332332
*/
333333
protected String getRelativePath(HttpServletRequest request) {
334+
return getRelativePath(request, false);
335+
}
336+
337+
protected String getRelativePath(HttpServletRequest request, boolean allowEmptyPath) {
334338
// IMPORTANT: DefaultServlet can be mapped to '/' or '/path/*' but always
335339
// serves resources from the web app root with context rooted paths.
336340
// i.e. it cannot be used to mount the web app root under a sub-path
@@ -356,7 +360,7 @@ protected String getRelativePath(HttpServletRequest request) {
356360
if (pathInfo != null) {
357361
result.append(pathInfo);
358362
}
359-
if (result.length() == 0) {
363+
if (result.length() == 0 && !allowEmptyPath) {
360364
result.append('/');
361365
}
362366

@@ -686,7 +690,8 @@ protected void serveResource(HttpServletRequest request,
686690
boolean serveContent = content;
687691

688692
// Identify the requested resource path
689-
String path = getRelativePath(request);
693+
String path = getRelativePath(request, true);
694+
690695
if (debug > 0) {
691696
if (serveContent)
692697
log("DefaultServlet.serveResource: Serving resource '" +
@@ -696,6 +701,12 @@ protected void serveResource(HttpServletRequest request,
696701
path + "' headers only");
697702
}
698703

704+
if (path.length() == 0) {
705+
// Context root redirect
706+
doDirectoryRedirect(request, response);
707+
return;
708+
}
709+
699710
WebResource resource = resources.getResource(path);
700711

701712
if (!resource.exists()) {
@@ -811,13 +822,7 @@ protected void serveResource(HttpServletRequest request,
811822

812823
if (resource.isDirectory()) {
813824
if (!path.endsWith("/")) {
814-
StringBuilder location = new StringBuilder(request.getRequestURI());
815-
location.append('/');
816-
if (request.getQueryString() != null) {
817-
location.append('?');
818-
location.append(request.getQueryString());
819-
}
820-
response.sendRedirect(response.encodeRedirectURL(location.toString()));
825+
doDirectoryRedirect(request, response);
821826
return;
822827
}
823828

@@ -1026,6 +1031,16 @@ protected void serveResource(HttpServletRequest request,
10261031
}
10271032
}
10281033

1034+
private void doDirectoryRedirect(HttpServletRequest request, HttpServletResponse response)
1035+
throws IOException {
1036+
StringBuilder location = new StringBuilder(request.getRequestURI());
1037+
location.append('/');
1038+
if (request.getQueryString() != null) {
1039+
location.append('?');
1040+
location.append(request.getQueryString());
1041+
}
1042+
response.sendRedirect(response.encodeRedirectURL(location.toString()));
1043+
}
10291044

10301045
/**
10311046
* Parse the content-range header.

0 commit comments

Comments
 (0)