Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49536
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 1 Jul 2010 21:35:19 +0000 (21:35 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 1 Jul 2010 21:35:19 +0000 (21:35 +0000)
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
java/org/apache/catalina/connector/MapperListener.java
java/org/apache/catalina/connector/Request.java
java/org/apache/tomcat/util/http/mapper/Mapper.java
webapps/docs/changelog.xml

index a9aaa6d..a043159 100644 (file)
@@ -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
index 5f77872..e946498 100644 (file)
@@ -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);
        
index 4ed0f60..5d42d10 100644 (file)
@@ -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);
     }
 
 
index 9be5703..9909dda 100644 (file)
@@ -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;
                 }
             }
         }
index 7d6de84..4714f94 100644 (file)
         <bug>49525</bug>: Ensure cookies for the ROOT context have a path of /
         rather than an empty string. (markt)
       </fix>
+      <fix>
+        <bug>49536</bug>: 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)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">