-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
AbstractHttp11Protocol.java
495 lines (407 loc) · 17.2 KB
/
AbstractHttp11Protocol.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* 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.http11;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpUpgradeHandler;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.Processor;
import org.apache.coyote.UpgradeProtocol;
import org.apache.coyote.http11.upgrade.InternalHttpUpgradeHandler;
import org.apache.coyote.http11.upgrade.UpgradeProcessorExternal;
import org.apache.coyote.http11.upgrade.UpgradeProcessorInternal;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.SocketWrapperBase;
public abstract class AbstractHttp11Protocol<S> extends AbstractProtocol<S> {
public AbstractHttp11Protocol(AbstractEndpoint<S> endpoint) {
super(endpoint);
setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
// TODO: Make this configurable via nested UpgradeProtocol elements in
// the Connector.
// This is disabled by default otherwise it will break the
// APR/native connector with clients that support h2 with ALPN
// (because the Http2Protocol is only stubbed out)
//addUpgradeProtocol(new Http2Protocol());
}
@Override
protected String getProtocolName() {
return "Http";
}
/**
* {@inheritDoc}
* <p>
* Over-ridden here to make the method visible to nested classes.
*/
@Override
protected AbstractEndpoint<S> getEndpoint() {
return super.getEndpoint();
}
// ------------------------------------------------ HTTP specific properties
// ------------------------------------------ managed in the ProtocolHandler
/**
* Maximum size of the post which will be saved when processing certain
* requests, such as a POST.
*/
private int maxSavePostSize = 4 * 1024;
public int getMaxSavePostSize() { return maxSavePostSize; }
public void setMaxSavePostSize(int valueI) { maxSavePostSize = valueI; }
/**
* Maximum size of the HTTP message header.
*/
private int maxHttpHeaderSize = 8 * 1024;
public int getMaxHttpHeaderSize() { return maxHttpHeaderSize; }
public void setMaxHttpHeaderSize(int valueI) { maxHttpHeaderSize = valueI; }
/**
* Specifies a different (usually longer) connection timeout during data
* upload.
*/
private int connectionUploadTimeout = 300000;
public int getConnectionUploadTimeout() { return connectionUploadTimeout; }
public void setConnectionUploadTimeout(int i) {
connectionUploadTimeout = i;
}
/**
* If true, the connectionUploadTimeout will be ignored and the regular
* socket timeout will be used for the full duration of the connection.
*/
private boolean disableUploadTimeout = true;
public boolean getDisableUploadTimeout() { return disableUploadTimeout; }
public void setDisableUploadTimeout(boolean isDisabled) {
disableUploadTimeout = isDisabled;
}
/**
* Integrated compression support.
*/
private String compression = "off";
public String getCompression() { return compression; }
public void setCompression(String valueS) { compression = valueS; }
private String noCompressionUserAgents = null;
public String getNoCompressionUserAgents() {
return noCompressionUserAgents;
}
public void setNoCompressionUserAgents(String valueS) {
noCompressionUserAgents = valueS;
}
private String compressableMimeType = "text/html,text/xml,text/plain,text/css";
private String[] compressableMimeTypes = null;
public String getCompressableMimeType() { return compressableMimeType; }
public void setCompressableMimeType(String valueS) {
compressableMimeType = valueS;
compressableMimeTypes = null;
}
public String[] getCompressableMimeTypes() {
String[] result = compressableMimeTypes;
if (result != null) {
return result;
}
List<String> values = new ArrayList<>();
StringTokenizer tokens = new StringTokenizer(compressableMimeType, ",");
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken().trim();
if (token.length() > 0) {
values.add(token);
}
}
result = values.toArray(new String[values.size()]);
compressableMimeTypes = result;
return result;
}
private int compressionMinSize = 2048;
public int getCompressionMinSize() { return compressionMinSize; }
public void setCompressionMinSize(int valueI) {
compressionMinSize = valueI;
}
/**
* Regular expression that defines the User agents which should be
* restricted to HTTP/1.0 support.
*/
private String restrictedUserAgents = null;
public String getRestrictedUserAgents() { return restrictedUserAgents; }
public void setRestrictedUserAgents(String valueS) {
restrictedUserAgents = valueS;
}
/**
* Server header.
*/
private String server;
public String getServer() { return server; }
public void setServer( String server ) {
this.server = server;
}
/**
* Maximum size of trailing headers in bytes
*/
private int maxTrailerSize = 8192;
public int getMaxTrailerSize() { return maxTrailerSize; }
public void setMaxTrailerSize(int maxTrailerSize) {
this.maxTrailerSize = maxTrailerSize;
}
/**
* Maximum size of extension information in chunked encoding
*/
private int maxExtensionSize = 8192;
public int getMaxExtensionSize() { return maxExtensionSize; }
public void setMaxExtensionSize(int maxExtensionSize) {
this.maxExtensionSize = maxExtensionSize;
}
/**
* Maximum amount of request body to swallow.
*/
private int maxSwallowSize = 2 * 1024 * 1024;
public int getMaxSwallowSize() { return maxSwallowSize; }
public void setMaxSwallowSize(int maxSwallowSize) {
this.maxSwallowSize = maxSwallowSize;
}
/**
* This field indicates if the protocol is treated as if it is secure. This
* normally means https is being used but can be used to fake https e.g
* behind a reverse proxy.
*/
private boolean secure;
public boolean getSecure() { return secure; }
public void setSecure(boolean b) {
secure = b;
}
/**
* The names of headers that are allowed to be sent via a trailer when using
* chunked encoding. They are stored in lower case.
*/
private Set<String> allowedTrailerHeaders =
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
public void setAllowedTrailerHeaders(String commaSeparatedHeaders) {
// Jump through some hoops so we don't end up with an empty set while
// doing updates.
Set<String> toRemove = new HashSet<>();
toRemove.addAll(allowedTrailerHeaders);
if (commaSeparatedHeaders != null) {
String[] headers = commaSeparatedHeaders.split(",");
for (String header : headers) {
String trimmedHeader = header.trim().toLowerCase(Locale.ENGLISH);
if (toRemove.contains(trimmedHeader)) {
toRemove.remove(trimmedHeader);
} else {
allowedTrailerHeaders.add(trimmedHeader);
}
}
allowedTrailerHeaders.removeAll(toRemove);
}
}
public String getAllowedTrailerHeaders() {
// Chances of a size change between these lines are small enough that a
// sync is unnecessary.
List<String> copy = new ArrayList<>(allowedTrailerHeaders.size());
copy.addAll(allowedTrailerHeaders);
StringBuilder result = new StringBuilder();
boolean first = true;
for (String header : copy) {
if (first) {
first = false;
} else {
result.append(',');
}
result.append(header);
}
return result.toString();
}
public void addAllowedTrailerHeader(String header) {
if (header != null) {
allowedTrailerHeaders.add(header.trim().toLowerCase(Locale.ENGLISH));
}
}
public void removeAllowedTrailerHeader(String header) {
if (header != null) {
allowedTrailerHeaders.remove(header.trim().toLowerCase(Locale.ENGLISH));
}
}
/**
* The protocols that are available via internal Tomcat support for access
* via HTTP upgrade.
*/
private final Map<String,UpgradeProtocol> httpUpgradeProtocols = new HashMap<>();
/**
* The protocols that are available via internal Tomcat support for access
* via ALPN negotiation.
*/
private final Map<String,UpgradeProtocol> negotiatedProtocols = new HashMap<>();
public void addUpgradeProtocol(UpgradeProtocol upgradeProtocol) {
httpUpgradeProtocols.put(upgradeProtocol.getHttpUpgradeName(), upgradeProtocol);
negotiatedProtocols.put(upgradeProtocol.getAlpnName(), upgradeProtocol);
getEndpoint().addNegotiatedProtocol(upgradeProtocol.getAlpnName());
}
@Override
public UpgradeProtocol getNegotiatedProtocol(String negotiatedName) {
return negotiatedProtocols.get(negotiatedName);
}
// ------------------------------------------------ HTTP specific properties
// ------------------------------------------ passed through to the EndPoint
public boolean isSSLEnabled() { return getEndpoint().isSSLEnabled();}
public void setSSLEnabled(boolean SSLEnabled) {
getEndpoint().setSSLEnabled(SSLEnabled);
}
public boolean getUseSendfile() { return getEndpoint().getUseSendfile(); }
public void setUseSendfile(boolean useSendfile) { getEndpoint().setUseSendfile(useSendfile); }
/**
* @return The maximum number of requests which can be performed over a
* keep-alive connection. The default is the same as for Apache HTTP
* Server (100).
*/
public int getMaxKeepAliveRequests() {
return getEndpoint().getMaxKeepAliveRequests();
}
public void setMaxKeepAliveRequests(int mkar) {
getEndpoint().setMaxKeepAliveRequests(mkar);
}
// ----------------------------------------------- HTTPS specific properties
// -------------------------------------------- Handled via an SSLHostConfig
private String defaultSSLHostConfigName = SSLHostConfig.DEFAULT_SSL_HOST_NAME;
public String getDefaultSSLHostConfigName() {
return defaultSSLHostConfigName;
}
public void setDefaultSSLHostConfigName(String defaultSSLHostConfigName) {
this.defaultSSLHostConfigName = defaultSSLHostConfigName;
}
@Override
public void addSslHostConfig(SSLHostConfig sslHostConfig) {
getEndpoint().addSslHostConfig(sslHostConfig);
}
private SSLHostConfig defaultSSLHostConfig = null;
private void registerDefaultSSLHostConfig() {
if (defaultSSLHostConfig == null) {
defaultSSLHostConfig = new SSLHostConfig();
defaultSSLHostConfig.setHostName(getDefaultSSLHostConfigName());
getEndpoint().addSslHostConfig(defaultSSLHostConfig);
}
}
// TODO: All of these SSL setters can be removed once it is no longer
// necessary to support the old configuration attributes (Tomcat 10?).
public void setSslEnabledProtocols(String enabledProtocols) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setProtocols(enabledProtocols);
}
public void setSSLProtocol(String sslProtocol) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setProtocols(sslProtocol);
}
public void setKeystoreFile(String keystoreFile) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setKeystoreFile(keystoreFile);
}
public void setSSLCertificateFile(String certificateFile) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCertificateFile(certificateFile);
}
public void setSSLCertificateKeyFile(String certificateKeyFile) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCertificateKeyFile(certificateKeyFile);
}
public void setAlgorithm(String keyManagerAlgorithm) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setKeyManagerAlgorithm(keyManagerAlgorithm);
}
public void setClientAuth(String certificateVerification) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCertificateVerification(certificateVerification);
}
public void setSSLVerifyClient(String certificateVerification) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCertificateVerification(certificateVerification);
}
public void setTrustMaxCertLength(int certificateVerificationDepth){
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCertificateVerificationDepth(certificateVerificationDepth);
}
public void setSSLVerifyDepth(int certificateVerificationDepth) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCertificateVerificationDepth(certificateVerificationDepth);
}
public void setUseServerCipherSuitesOrder(boolean honorCipherOrder) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setHonorCipherOrder(honorCipherOrder);
}
public void setSSLHonorCipherOrder(boolean honorCipherOrder) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setHonorCipherOrder(honorCipherOrder);
}
public void setCiphers(String ciphers) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCiphers(ciphers);
}
public void setSSLCipherSuite(String ciphers) {
registerDefaultSSLHostConfig();
defaultSSLHostConfig.setCiphers(ciphers);
}
// ------------------------------------------------------------- Common code
// Common configuration required for all new HTTP11 processors
protected void configureProcessor(Http11Processor processor) {
processor.setAdapter(getAdapter());
processor.setMaxKeepAliveRequests(getMaxKeepAliveRequests());
processor.setConnectionUploadTimeout(getConnectionUploadTimeout());
processor.setDisableUploadTimeout(getDisableUploadTimeout());
processor.setCompressionMinSize(getCompressionMinSize());
processor.setCompression(getCompression());
processor.setNoCompressionUserAgents(getNoCompressionUserAgents());
processor.setCompressableMimeTypes(getCompressableMimeTypes());
processor.setRestrictedUserAgents(getRestrictedUserAgents());
processor.setMaxSavePostSize(getMaxSavePostSize());
processor.setServer(getServer());
processor.setClientCertProvider(getClientCertProvider());
}
protected abstract static class AbstractHttp11ConnectionHandler<S>
extends AbstractConnectionHandler<S,Http11Processor> {
private final AbstractHttp11Protocol<S> proto;
protected AbstractHttp11ConnectionHandler(AbstractHttp11Protocol<S> proto) {
this.proto = proto;
}
@Override
protected AbstractHttp11Protocol<S> getProtocol() {
return proto;
}
@Override
public Http11Processor createProcessor() {
Http11Processor processor = new Http11Processor(
proto.getMaxHttpHeaderSize(), proto.getEndpoint(), proto.getMaxTrailerSize(),
proto.allowedTrailerHeaders, proto.getMaxExtensionSize(),
proto.getMaxSwallowSize());
proto.configureProcessor(processor);
register(processor);
return processor;
}
@Override
protected Processor createUpgradeProcessor(
SocketWrapperBase<?> socket, ByteBuffer leftoverInput,
HttpUpgradeHandler httpUpgradeHandler)
throws IOException {
if (httpUpgradeHandler instanceof InternalHttpUpgradeHandler) {
return new UpgradeProcessorInternal(socket, leftoverInput,
(InternalHttpUpgradeHandler) httpUpgradeHandler);
} else {
return new UpgradeProcessorExternal(socket, leftoverInput, httpUpgradeHandler);
}
}
}
}