/
StandardContextValve.java
363 lines (307 loc) · 11.2 KB
/
StandardContextValve.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.core;
import org.apache.catalina.*;
import org.apache.catalina.valves.ValveBase;
import org.glassfish.grizzly.utils.Charsets;
import org.glassfish.web.valve.GlassFishValve;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.glassfish.grizzly.http.util.DataChunk;
// END GlassFish 1343
/**
* Valve that implements the default basic behavior for the
* <code>StandardContext</code> container implementation.
* <p>
* <b>USAGE CONSTRAINT</b>: This implementation is likely to be useful only
* when processing HTTP requests.
*
* @author Craig R. McClanahan
* @version $Revision: 1.19 $ $Date: 2007/05/05 05:31:54 $
*/
final class StandardContextValve
extends ValveBase {
// ----------------------------------------------------- Instance Variables
/**
* The descriptive information related to this implementation.
*/
private static final String info =
"org.apache.catalina.core.StandardContextValve/1.0";
private StandardContext context = null;
// ------------------------------------------------------------- Properties
/**
* Return descriptive information about this Valve implementation.
*/
public String getInfo() {
return (info);
}
// --------------------------------------------------------- Public Methods
/**
* Cast to a StandardContext right away, as it will be needed later.
*
* @see org.apache.catalina.Contained#setContainer(org.apache.catalina.Container)
*/
public void setContainer(Container container) {
super.setContainer(container);
if (container instanceof StandardContext) {
context = (StandardContext) container;
}
}
/**
* Select the appropriate child Wrapper to process this request,
* based on the specified request URI. If no matching Wrapper can
* be found, return an appropriate HTTP error.
*
* @param request Request to be processed
* @param response Response to be produced
* @param valveContext Valve context used to forward to the next Valve
*
* @exception IOException if an input/output error occurred
* @exception ServletException if a servlet error occurred
*/
@Override
public int invoke(Request request, Response response)
throws IOException, ServletException {
Wrapper wrapper = preInvoke(request, response);
if (wrapper == null) {
return END_PIPELINE;
}
/* GlassFish 1343
wrapper.getPipeline().invoke(request, response);
*/
// START GlassFish 1343
if (wrapper.getPipeline().hasNonBasicValves() ||
wrapper.hasCustomPipeline()) {
wrapper.getPipeline().invoke(request, response);
} else {
GlassFishValve basic = wrapper.getPipeline().getBasic();
if (basic != null) {
basic.invoke(request, response);
basic.postInvoke(request, response);
}
}
// END GlassFish 1343
return END_PIPELINE;
}
/**
* Tomcat style invocation.
*/
@Override
public void invoke(org.apache.catalina.connector.Request request,
org.apache.catalina.connector.Response response)
throws IOException, ServletException {
Wrapper wrapper = preInvoke(request, response);
if (wrapper == null) {
return;
}
/* GlassFish 1343
wrapper.getPipeline().invoke(request, response);
*/
// START GlassFish 1343
if (wrapper.getPipeline().hasNonBasicValves() ||
wrapper.hasCustomPipeline()) {
wrapper.getPipeline().invoke(request, response);
} else {
GlassFishValve basic = wrapper.getPipeline().getBasic();
if (basic != null) {
basic.invoke(request, response);
basic.postInvoke(request, response);
}
}
// END GlassFish 1343
postInvoke(request, response);
}
@Override
public void postInvoke(Request request, Response response)
throws IOException, ServletException {
}
/**
* Report a "not found" error for the specified resource. FIXME: We
* should really be using the error reporting settings for this web
* application, but currently that code runs at the wrapper level rather
* than the context level.
*
* @param response The response we are creating
*/
private void notFound(HttpServletResponse response) {
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (IllegalStateException e) {
// Ignore
} catch (IOException e) {
// Ignore
}
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
*
private void log(String message) {
org.apache.catalina.Logger logger = null;
String containerName = null;
if (container != null) {
logger = container.getLogger();
containerName = container.getName();
}
if (logger != null) {
logger.log("StandardContextValve[" + container.getName() + "]: " +
message);
} else {
if (log.isLoggable(Level.INFO)) {
log.info("StandardContextValve[" + containerName + "]: " +
message);
}
}
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
* @param t Associated exception
*
private void log(String message, Throwable t) {
org.apache.catalina.Logger logger = null;
String containerName = null;
if (container != null) {
logger = container.getLogger();
containerName = container.getName();
}
if (logger != null) {
logger.log("StandardContextValve[" + container.getName() + "]: " +
message, t, org.apache.catalina.Logger.WARNING);
} else {
log.log(Level.WARNING, "StandardContextValve[" + containerName +
"]: " + message, t);
}
}
*/
/**
* resolves '.' and '..' elements in the path
* if there are too many, making a path negative, returns null
*
* @param path to be normalized
* @return normalized path or null
*/
static String normalize(String path) {
if (path == null) {
return null;
}
String rv = path;
// starts with a double-slash
if(rv.indexOf("//") == 0) {
rv = rv.replace("//", "/");
}
// starts with dot-slash
if(rv.indexOf("./") == 0) {
rv = rv.replaceFirst("./", "/");
}
// Normalize the slashes and add leading slash if necessary
if (rv.indexOf('\\') >= 0) {
rv = rv.replace('\\', '/');
}
// Resolve occurrences of "/../" in the normalized path
while (true) {
int idx = rv.indexOf("/../");
if (idx < 0) {
break;
}
if (idx == 0) {
return null; // negative relative path
}
int index2 = rv.lastIndexOf('/', idx - 1);
rv = rv.substring(0, index2) + rv.substring(idx + 3);
}
// Resolve occurrences of "/./" example if the path looks like /app/./some/./something/./my.jsp
//then after processing should look like /app/some/something/my.jsp
rv = evaluateNormalizedPathWithSinglePoint(rv);
//if the path don't start with / then include it
if(!rv.startsWith("/")) {
rv = "/" + rv;
}
// Return the normalized path that we have completed
return rv;
}
/**
* this method helps to evaluate the element "/./" on the path
* @param path to be normalized
* @return normalized path
*/
private static String evaluateNormalizedPathWithSinglePoint(String path) {
// Resolve occurrences of "/./" in the normalized path
while (true) {
int idx = path.indexOf("/./");
if (idx < 0) {
break;
}
path = path.substring(0, idx) + path.substring(idx + 2);
}
return path;
}
private Wrapper preInvoke(Request request, Response response) {
// Disallow any direct access to resources under WEB-INF or META-INF
HttpRequest hreq = (HttpRequest) request;
// START CR 6415120
if (request.getCheckRestrictedResources()) {
// END CR 6415120
String requestPath = normalize(hreq.getRequestPathMB().toString(Charsets.UTF8_CHARSET));
if ((requestPath == null)
|| (requestPath.toUpperCase().startsWith("/META-INF/", 0))
|| (requestPath.equalsIgnoreCase("/META-INF"))
|| (requestPath.toUpperCase().startsWith("/WEB-INF/", 0))
|| (requestPath.equalsIgnoreCase("/WEB-INF"))) {
notFound((HttpServletResponse) response.getResponse());
return null;
}
// START CR 6415120
}
// END CR 6415120
// Wait if we are reloading
boolean reloaded = false;
while (((StandardContext) container).getPaused()) {
reloaded = true;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Ignore
}
}
// Reloading will have stopped the old webappclassloader and
// created a new one
if (reloaded &&
context.getLoader() != null &&
context.getLoader().getClassLoader() != null) {
Thread.currentThread().setContextClassLoader(
context.getLoader().getClassLoader());
}
// Select the Wrapper to be used for this Request
Wrapper wrapper = request.getWrapper();
if (wrapper == null) {
notFound((HttpServletResponse) response.getResponse());
return null;
} else if (wrapper.isUnavailable()) {
// May be as a result of a reload, try and find the new wrapper
wrapper = (Wrapper) container.findChild(wrapper.getName());
if (wrapper == null) {
notFound((HttpServletResponse) response.getResponse());
return null;
}
}
return wrapper;
}
}