1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.core;
18
19 import java.io.IOException;
20
21 import javax.servlet.RequestDispatcher;
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.catalina.Wrapper;
26 import org.apache.catalina.connector.Request;
27 import org.apache.catalina.connector.Response;
28 import org.apache.catalina.valves.ValveBase;
29 import org.apache.tomcat.util.buf.MessageBytes;
30 import org.apache.tomcat.util.res.StringManager;
31
32 /**
33  * Valve that implements the default basic behavior for the
34  * <code>StandardContext</code> container implementation.
35  * <p>
36  * <b>USAGE CONSTRAINT</b>:  This implementation is likely to be useful only
37  * when processing HTTP requests.
38  *
39  * @author Craig R. McClanahan
40  */

41 final class StandardContextValve extends ValveBase {
42
43     private static final StringManager sm = StringManager.getManager(StandardContextValve.class);
44
45     public StandardContextValve() {
46         super(true);
47     }
48
49
50     /**
51      * Select the appropriate child Wrapper to process this request,
52      * based on the specified request URI.  If no matching Wrapper can
53      * be found, return an appropriate HTTP error.
54      *
55      * @param request Request to be processed
56      * @param response Response to be produced
57      *
58      * @exception IOException if an input/output error occurred
59      * @exception ServletException if a servlet error occurred
60      */

61     @Override
62     public final void invoke(Request request, Response response)
63         throws IOException, ServletException {
64
65         // Disallow any direct access to resources under WEB-INF or META-INF
66         MessageBytes requestPathMB = request.getRequestPathMB();
67         if ((requestPathMB.startsWithIgnoreCase("/META-INF/", 0))
68                 || (requestPathMB.equalsIgnoreCase("/META-INF"))
69                 || (requestPathMB.startsWithIgnoreCase("/WEB-INF/", 0))
70                 || (requestPathMB.equalsIgnoreCase("/WEB-INF"))) {
71             response.sendError(HttpServletResponse.SC_NOT_FOUND);
72             return;
73         }
74
75         // Select the Wrapper to be used for this Request
76         Wrapper wrapper = request.getWrapper();
77         if (wrapper == null || wrapper.isUnavailable()) {
78             response.sendError(HttpServletResponse.SC_NOT_FOUND);
79             return;
80         }
81
82         // Acknowledge the request
83         try {
84             response.sendAcknowledgement();
85         } catch (IOException ioe) {
86             container.getLogger().error(sm.getString(
87                     "standardContextValve.acknowledgeException"), ioe);
88             request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, ioe);
89             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
90             return;
91         }
92
93         if (request.isAsyncSupported()) {
94             request.setAsyncSupported(wrapper.getPipeline().isAsyncSupported());
95         }
96         wrapper.getPipeline().getFirst().invoke(request, response);
97     }
98 }
99