-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
AbstractProcessor.java
357 lines (292 loc) · 10.6 KB
/
AbstractProcessor.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.coyote;
import java.io.IOException;
import java.util.concurrent.Executor;
import javax.servlet.RequestDispatcher;
import org.apache.juli.logging.Log;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.parser.Host;
import org.apache.tomcat.util.log.UserDataHelper;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.SocketStatus;
import org.apache.tomcat.util.net.SocketWrapper;
import org.apache.tomcat.util.res.StringManager;
/**
* Provides functionality and attributes common to all supported protocols
* (currently HTTP and AJP).
*/
public abstract class AbstractProcessor<S> implements ActionHook, Processor<S> {
protected static final StringManager sm = StringManager.getManager(Constants.Package);
// Used to avoid useless B2C conversion on the host name.
protected char[] hostNameC = new char[0];
protected Adapter adapter;
protected AsyncStateMachine<S> asyncStateMachine;
protected AbstractEndpoint<S> endpoint;
protected Request request;
protected Response response;
protected SocketWrapper<S> socketWrapper = null;
private int maxCookieCount = 200;
/**
* Error state for the request/response currently being processed.
*/
private ErrorState errorState = ErrorState.NONE;
protected final UserDataHelper userDataHelper;
/**
* Intended for use by the Upgrade sub-classes that have no need to
* initialise the request, response, etc.
*/
protected AbstractProcessor() {
// NOOP
userDataHelper = null;
}
public AbstractProcessor(AbstractEndpoint<S> endpoint) {
this.endpoint = endpoint;
asyncStateMachine = new AsyncStateMachine<S>(this);
request = new Request();
response = new Response();
response.setHook(this);
request.setResponse(response);
userDataHelper = new UserDataHelper(getLog());
}
/**
* Update the current error state to the new error state if the new error
* state is more severe than the current error state.
* @param errorState The error status details
* @param t The error which occurred
*/
protected void setErrorState(ErrorState errorState, Throwable t) {
if (getLog().isDebugEnabled()) {
getLog().debug(sm.getString("abstractProcessor.setErrorState", errorState), t);
}
// Use the return value to avoid processing more than one async error
// in a single async cycle.
boolean setError = response.setError();
boolean blockIo = this.errorState.isIoAllowed() && !errorState.isIoAllowed();
this.errorState = this.errorState.getMostSevere(errorState);
// Don't change the status code for IOException since that is almost
// certainly a client disconnect in which case it is preferable to keep
// the original status code http://markmail.org/message/4cxpwmxhtgnrwh7n
if (response.getStatus() < 400 && !(t instanceof IOException)) {
response.setStatus(500);
}
if (t != null) {
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
}
if (blockIo && isAsync() && setError) {
if (asyncStateMachine.asyncError()) {
getEndpoint().processSocketAsync(socketWrapper, SocketStatus.ERROR);
}
}
}
protected void resetErrorState() {
errorState = ErrorState.NONE;
}
protected ErrorState getErrorState() {
return errorState;
}
/**
* @return The endpoint receiving connections that are handled by this
* processor.
*/
protected AbstractEndpoint<S> getEndpoint() {
return endpoint;
}
/**
* The request associated with this processor.
*/
@Override
public Request getRequest() {
return request;
}
/**
* Set the associated adapter.
*
* @param adapter the new adapter
*/
public void setAdapter(Adapter adapter) {
this.adapter = adapter;
}
/**
* Get the associated adapter.
*
* @return the associated adapter
*/
public Adapter getAdapter() {
return adapter;
}
/**
* Set the socket wrapper being used.
* @param socketWrapper The socket wrapper
*/
protected final void setSocketWrapper(SocketWrapper<S> socketWrapper) {
this.socketWrapper = socketWrapper;
}
/**
* @return the socket wrapper being used.
*/
protected final SocketWrapper<S> getSocketWrapper() {
return socketWrapper;
}
/**
* @return the Executor used by the underlying endpoint.
*/
@Override
public Executor getExecutor() {
return endpoint.getExecutor();
}
@Override
public boolean isAsync() {
return (asyncStateMachine != null && asyncStateMachine.isAsync());
}
@Override
public SocketState asyncPostProcess() {
return asyncStateMachine.asyncPostProcess();
}
@Override
public void errorDispatch() {
getAdapter().errorDispatch(request, response);
}
@Override
public abstract boolean isComet();
protected void parseHost(MessageBytes valueMB) {
if (valueMB == null || valueMB.isNull()) {
populateHost();
populatePort();
return;
} else if (valueMB.getLength() == 0) {
// Empty Host header so set sever name to empty string
request.serverName().setString("");
populatePort();
return;
}
ByteChunk valueBC = valueMB.getByteChunk();
byte[] valueB = valueBC.getBytes();
int valueL = valueBC.getLength();
int valueS = valueBC.getStart();
if (hostNameC.length < valueL) {
hostNameC = new char[valueL];
}
try {
// Validates the host name
int colonPos = Host.parse(valueMB);
// Extract the port information first, if any
if (colonPos != -1) {
int port = 0;
for (int i = colonPos + 1; i < valueL; i++) {
char c = (char) valueB[i + valueS];
if (c < '0' || c > '9') {
response.setStatus(400);
setErrorState(ErrorState.CLOSE_CLEAN, null);
return;
}
port = port * 10 + c - '0';
}
request.setServerPort(port);
// Only need to copy the host name up to the :
valueL = colonPos;
}
// Extract the host name
for (int i = 0; i < valueL; i++) {
hostNameC[i] = (char) valueB[i + valueS];
}
request.serverName().setChars(hostNameC, 0, valueL);
} catch (IllegalArgumentException e) {
// IllegalArgumentException indicates that the host name is invalid
UserDataHelper.Mode logMode = userDataHelper.getNextMode();
if (logMode != null) {
String message = sm.getString("abstractProcessor.hostInvalid", valueMB.toString());
switch (logMode) {
case INFO_THEN_DEBUG:
message += sm.getString("abstractProcessor.fallToDebug");
//$FALL-THROUGH$
case INFO:
getLog().info(message, e);
break;
case DEBUG:
getLog().debug(message, e);
}
}
response.setStatus(400);
setErrorState(ErrorState.CLOSE_CLEAN, e);
}
}
/**
* Called when a host header is not present in the request (e.g. HTTP/1.0).
* It populates the server name with appropriate information. The source is
* expected to vary by protocol.
* <p>
* The default implementation is a NO-OP.
*/
protected void populateHost() {
// NO-OP
}
/**
* Called when a host header is not present or is empty in the request (e.g.
* HTTP/1.0). It populates the server port with appropriate information. The
* source is expected to vary by protocol.
* <p>
* The default implementation is a NO-OP.
*/
protected void populatePort() {
// NO-OP
}
@Override
public abstract boolean isUpgrade();
/**
* Process HTTP requests. All requests are treated as HTTP requests to start
* with although they may change type during processing.
*/
@Override
public abstract SocketState process(SocketWrapper<S> socket) throws IOException;
/**
* Process in-progress Comet requests. These will start as HTTP requests.
*/
@Override
public abstract SocketState event(SocketStatus status) throws IOException;
/**
* Process in-progress Servlet 3.0 Async requests. These will start as HTTP
* requests.
*/
@Override
public abstract SocketState asyncDispatch(SocketStatus status);
/**
* Processes data received on a connection that has been through an HTTP
* upgrade.
*/
@Override
public abstract SocketState upgradeDispatch() throws IOException;
public int getMaxCookieCount() {
return maxCookieCount;
}
public void setMaxCookieCount(int maxCookieCount) {
this.maxCookieCount = maxCookieCount;
}
/**
* @deprecated Will be removed in Tomcat 8.0.x.
*/
@Deprecated
@Override
public abstract org.apache.coyote.http11.upgrade.UpgradeInbound getUpgradeInbound();
protected abstract Log getLog();
@Override
public final AsyncStateMachine<S> getAsyncStateMachine() {
return asyncStateMachine;
}
}