From c1ca7c9d1aa21cf4f432f1d3198aa2a3b8ac62f5 Mon Sep 17 00:00:00 2001 From: markt Date: Thu, 1 Jul 2010 21:35:19 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49536 Ensure a 404 response for an unmapped request when no ROOT context is deployed. Most of the change is getting the mapper to use the Host object rather than the ObjectName or an empty string git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@959790 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/connector/CoyoteAdapter.java | 10 ++++++++++ .../org/apache/catalina/connector/MapperListener.java | 8 ++++---- java/org/apache/catalina/connector/Request.java | 5 +---- java/org/apache/tomcat/util/http/mapper/Mapper.java | 19 ++++++++++--------- webapps/docs/changelog.xml | 5 +++++ 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/java/org/apache/catalina/connector/CoyoteAdapter.java b/java/org/apache/catalina/connector/CoyoteAdapter.java index a9aaa6d7b..a04315978 100644 --- a/java/org/apache/catalina/connector/CoyoteAdapter.java +++ b/java/org/apache/catalina/connector/CoyoteAdapter.java @@ -630,6 +630,16 @@ public class CoyoteAdapter implements Adapter { return false; } + // If there is no context at this point, it is likely no ROOT context + // has been deployed + if (request.getContext() == null) { + res.setStatus(404); + res.setMessage("Not found"); + // No context, so use host + request.getHost().logAccess(request, response, 0, true); + return false; + } + // Now we have the context, we can parse the session ID from the URL // (if any). Need to do this before we redirect in case we need to // include the session id in the redirect diff --git a/java/org/apache/catalina/connector/MapperListener.java b/java/org/apache/catalina/connector/MapperListener.java index 5f77872a2..e9464988c 100644 --- a/java/org/apache/catalina/connector/MapperListener.java +++ b/java/org/apache/catalina/connector/MapperListener.java @@ -273,7 +273,7 @@ public class MapperListener implements ContainerListener, LifecycleListener { private void registerHost(Host host) { String[] aliases = host.findAliases(); - mapper.addHost(host.getName(), aliases, host.getObjectName()); + mapper.addHost(host.getName(), aliases, host); host.addContainerListener(this); @@ -330,13 +330,13 @@ public class MapperListener implements ContainerListener, LifecycleListener { if ("/".equals(contextName)) { contextName = ""; } - String hostName = context.getParent().getName(); + Container host = context.getParent(); javax.naming.Context resources = context.getResources(); String[] welcomeFiles = context.findWelcomeFiles(); - mapper.addContext(hostName, contextName, context, welcomeFiles, - resources); + mapper.addContext(host.getName(), host, contextName, context, + welcomeFiles, resources); context.addContainerListener(this); diff --git a/java/org/apache/catalina/connector/Request.java b/java/org/apache/catalina/connector/Request.java index 4ed0f6061..5d42d104e 100644 --- a/java/org/apache/catalina/connector/Request.java +++ b/java/org/apache/catalina/connector/Request.java @@ -617,10 +617,7 @@ public class Request * Return the Host within which this Request is being processed. */ public Host getHost() { - if (getContext() == null) - return null; - return (Host) getContext().getParent(); - //return ((Host) mappingData.host); + return ((Host) mappingData.host); } diff --git a/java/org/apache/tomcat/util/http/mapper/Mapper.java b/java/org/apache/tomcat/util/http/mapper/Mapper.java index 9be5703ec..9909ddaec 100644 --- a/java/org/apache/tomcat/util/http/mapper/Mapper.java +++ b/java/org/apache/tomcat/util/http/mapper/Mapper.java @@ -210,33 +210,34 @@ public final class Mapper { * Add a new Context to an existing Host. * * @param hostName Virtual host name this context belongs to + * @param host Host object * @param path Context path * @param context Context object * @param welcomeResources Welcome files defined for this context * @param resources Static resources of the context */ public void addContext - (String hostName, String path, Object context, + (String hostName, Object host, String path, Object context, String[] welcomeResources, javax.naming.Context resources) { Host[] hosts = this.hosts; int pos = find(hosts, hostName); if( pos <0 ) { - addHost(hostName, new String[0], ""); + addHost(hostName, new String[0], host); hosts = this.hosts; pos = find(hosts, hostName); } if (pos < 0) { log.error("No host found: " + hostName); } - Host host = hosts[pos]; - if (host.name.equals(hostName)) { + Host mappedHost = hosts[pos]; + if (mappedHost.name.equals(hostName)) { int slashCount = slashCount(path); - synchronized (host) { - Context[] contexts = host.contextList.contexts; + synchronized (mappedHost) { + Context[] contexts = mappedHost.contextList.contexts; // Update nesting - if (slashCount > host.contextList.nesting) { - host.contextList.nesting = slashCount; + if (slashCount > mappedHost.contextList.nesting) { + mappedHost.contextList.nesting = slashCount; } Context[] newContexts = new Context[contexts.length + 1]; Context newContext = new Context(); @@ -245,7 +246,7 @@ public final class Mapper { newContext.welcomeResources = welcomeResources; newContext.resources = resources; if (insertMap(contexts, newContexts, newContext)) { - host.contextList.contexts = newContexts; + mappedHost.contextList.contexts = newContexts; } } } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 7d6de84c3..4714f9475 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -83,6 +83,11 @@ 49525: Ensure cookies for the ROOT context have a path of / rather than an empty string. (markt) + + 49536: If no ROOT context is deployed, ensure a 404 rather + than a 200 is returned for requests that don't map to any other context. + (markt) + -- 2.11.0