2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.util.List;
22 import java.util.concurrent.CopyOnWriteArrayList;
24 import org.eclipse.jetty.http.HttpScheme;
25 import org.eclipse.jetty.util.Jetty;
26 import org.eclipse.jetty.util.annotation.ManagedAttribute;
27 import org.eclipse.jetty.util.annotation.ManagedObject;
30 /* ------------------------------------------------------------ */
31 /** HTTP Configuration.
32 * <p>This class is a holder of HTTP configuration for use by the
33 * {@link HttpChannel} class. Typically a HTTPConfiguration instance
34 * is instantiated and passed to a {@link ConnectionFactory} that can
35 * create HTTP channels (eg HTTP, AJP or SPDY).</p>
36 * <p>The configuration held by this class is not for the wire protocol,
37 * but for the interpretation and handling of HTTP requests that could
38 * be transported by a variety of protocols.
41 @ManagedObject("HTTP Configuration")
42 public class HttpConfiguration
44 public static final String SERVER_VERSION = "Jetty(" + Jetty.VERSION + ")";
46 private List<Customizer> _customizers=new CopyOnWriteArrayList<>();
47 private int _outputBufferSize=32*1024;
48 private int _outputAggregationSize=_outputBufferSize/4;
49 private int _requestHeaderSize=8*1024;
50 private int _responseHeaderSize=8*1024;
51 private int _headerCacheSize=512;
52 private int _securePort;
53 private String _secureScheme = HttpScheme.HTTPS.asString();
54 private boolean _sendServerVersion = true;
55 private boolean _sendXPoweredBy = false;
56 private boolean _sendDateHeader = true;
57 private boolean _delayDispatchUntilContent = false;
59 /* ------------------------------------------------------------ */
61 * <p>An interface that allows a request object to be customized
62 * for a particular HTTP connector configuration. Unlike Filters, customizer are
63 * applied before the request is submitted for processing and can be specific to the
64 * connector on which the request was received.
66 * <p>Typically Customizers perform tasks such as: <ul>
67 * <li>process header fields that may be injected by a proxy or load balancer.
68 * <li>setup attributes that may come from the connection/connector such as SSL Session IDs
69 * <li>Allow a request to be marked as secure or authenticated if those have been offloaded
70 * and communicated by header, cookie or other out-of-band mechanism
71 * <li>Set request attributes/fields that are determined by the connector on which the
72 * request was received
75 public interface Customizer
77 public void customize(Connector connector, HttpConfiguration channelConfig, Request request);
80 public interface ConnectionFactory
82 HttpConfiguration getHttpConfiguration();
85 public HttpConfiguration()
89 /* ------------------------------------------------------------ */
90 /** Create a configuration from another.
91 * @param config The configuration to copy.
93 public HttpConfiguration(HttpConfiguration config)
95 _customizers.addAll(config._customizers);
96 _outputBufferSize=config._outputBufferSize;
97 _outputAggregationSize=config._outputAggregationSize;
98 _requestHeaderSize=config._requestHeaderSize;
99 _responseHeaderSize=config._responseHeaderSize;
100 _securePort=config._securePort;
101 _secureScheme=config._secureScheme;
102 _sendDateHeader=config._sendDateHeader;
103 _sendServerVersion=config._sendServerVersion;
104 _headerCacheSize=config._headerCacheSize;
107 /* ------------------------------------------------------------ */
109 * <p>Add a {@link Customizer} that is invoked for every
110 * request received.</p>
111 * <p>Customiser are often used to interpret optional headers (eg {@link ForwardedRequestCustomizer}) or
112 * optional protocol semantics (eg {@link SecureRequestCustomizer}).
113 * @param customizer A request customizer
115 public void addCustomizer(Customizer customizer)
117 _customizers.add(customizer);
120 /* ------------------------------------------------------------ */
121 public List<Customizer> getCustomizers()
126 /* ------------------------------------------------------------ */
127 public <T> T getCustomizer(Class<T> type)
129 for (Customizer c : _customizers)
130 if (type.isAssignableFrom(c.getClass()))
135 /* ------------------------------------------------------------ */
136 @ManagedAttribute("The size in bytes of the output buffer used to aggregate HTTP output")
137 public int getOutputBufferSize()
139 return _outputBufferSize;
142 /* ------------------------------------------------------------ */
143 @ManagedAttribute("The maximum size in bytes for HTTP output to be aggregated")
144 public int getOutputAggregationSize()
146 return _outputAggregationSize;
149 /* ------------------------------------------------------------ */
150 @ManagedAttribute("The maximum allowed size in bytes for a HTTP request header")
151 public int getRequestHeaderSize()
153 return _requestHeaderSize;
156 /* ------------------------------------------------------------ */
157 @ManagedAttribute("The maximum allowed size in bytes for a HTTP response header")
158 public int getResponseHeaderSize()
160 return _responseHeaderSize;
163 /* ------------------------------------------------------------ */
164 @ManagedAttribute("The maximum allowed size in bytes for a HTTP header field cache")
165 public int getHeaderCacheSize()
167 return _headerCacheSize;
170 /* ------------------------------------------------------------ */
171 @ManagedAttribute("The port to which Integral or Confidential security constraints are redirected")
172 public int getSecurePort()
177 /* ------------------------------------------------------------ */
178 @ManagedAttribute("The scheme with which Integral or Confidential security constraints are redirected")
179 public String getSecureScheme()
181 return _secureScheme;
184 /* ------------------------------------------------------------ */
185 public void setSendServerVersion (boolean sendServerVersion)
187 _sendServerVersion = sendServerVersion;
190 /* ------------------------------------------------------------ */
191 @ManagedAttribute("if true, send the Server header in responses")
192 public boolean getSendServerVersion()
194 return _sendServerVersion;
197 /* ------------------------------------------------------------ */
198 public void setSendXPoweredBy (boolean sendXPoweredBy)
200 _sendXPoweredBy=sendXPoweredBy;
203 /* ------------------------------------------------------------ */
204 @ManagedAttribute("if true, send the X-Powered-By header in responses")
205 public boolean getSendXPoweredBy()
207 return _sendXPoweredBy;
210 /* ------------------------------------------------------------ */
211 public void setSendDateHeader(boolean sendDateHeader)
213 _sendDateHeader = sendDateHeader;
216 /* ------------------------------------------------------------ */
217 @ManagedAttribute("if true, include the date in HTTP headers")
218 public boolean getSendDateHeader()
220 return _sendDateHeader;
223 /* ------------------------------------------------------------ */
225 * @param delay if true, delay the application dispatch until content is available
227 public void setDelayDispatchUntilContent(boolean delay)
229 _delayDispatchUntilContent = delay;
232 /* ------------------------------------------------------------ */
233 @ManagedAttribute("if true, delay the application dispatch until content is available")
234 public boolean isDelayDispatchUntilContent()
236 return _delayDispatchUntilContent;
239 /* ------------------------------------------------------------ */
241 * <p>Set the {@link Customizer}s that are invoked for every
242 * request received.</p>
243 * <p>Customizers are often used to interpret optional headers (eg {@link ForwardedRequestCustomizer}) or
244 * optional protocol semantics (eg {@link SecureRequestCustomizer}).
245 * @param customizers the list of customizers
247 public void setCustomizers(List<Customizer> customizers)
249 _customizers.clear();
250 _customizers.addAll(customizers);
253 /* ------------------------------------------------------------ */
255 * Set the size of the buffer into which response content is aggregated
256 * before being sent to the client. A larger buffer can improve performance by allowing
257 * a content producer to run without blocking, however larger buffers consume more memory and
258 * may induce some latency before a client starts processing the content.
259 * @param outputBufferSize buffer size in bytes.
261 public void setOutputBufferSize(int outputBufferSize)
263 _outputBufferSize = outputBufferSize;
264 setOutputAggregationSize(outputBufferSize / 4);
267 /* ------------------------------------------------------------ */
269 * Set the max size of the response content write that is copied into the aggregate buffer.
270 * Writes that are smaller of this size are copied into the aggregate buffer, while
271 * writes that are larger of this size will cause the aggregate buffer to be flushed
272 * and the write to be executed without being copied.
273 * @param outputAggregationSize the max write size that is aggregated
275 public void setOutputAggregationSize(int outputAggregationSize)
277 _outputAggregationSize = outputAggregationSize;
280 /* ------------------------------------------------------------ */
281 /** Set the maximum size of a request header.
282 * <p>Larger headers will allow for more and/or larger cookies plus larger form content encoded
283 * in a URL. However, larger headers consume more memory and can make a server more vulnerable to denial of service
285 * @param requestHeaderSize Max header size in bytes
287 public void setRequestHeaderSize(int requestHeaderSize)
289 _requestHeaderSize = requestHeaderSize;
292 /* ------------------------------------------------------------ */
293 /** Set the maximum size of a response header.
295 * <p>Larger headers will allow for more and/or larger cookies and longer HTTP headers (eg for redirection).
296 * However, larger headers will also consume more memory.</p>
297 * @param responseHeaderSize Response header size in bytes.
299 public void setResponseHeaderSize(int responseHeaderSize)
301 _responseHeaderSize = responseHeaderSize;
304 /* ------------------------------------------------------------ */
305 /** Set the header field cache size.
306 * @param headerCacheSize The size in bytes of the header field cache.
308 public void setHeaderCacheSize(int headerCacheSize)
310 _headerCacheSize = headerCacheSize;
313 /* ------------------------------------------------------------ */
314 /** Set the TCP/IP port used for CONFIDENTIAL and INTEGRAL redirections.
315 * @param securePort the secure port to redirect to.
317 public void setSecurePort(int securePort)
319 _securePort = securePort;
322 /* ------------------------------------------------------------ */
323 /** Set the URI scheme used for CONFIDENTIAL and INTEGRAL redirections.
324 * @param secureScheme A scheme string like "https"
326 public void setSecureScheme(String secureScheme)
328 _secureScheme = secureScheme;
332 public String toString()
334 return String.format("%s@%x{%d/%d,%d/%d,%s://:%d,%s}",
335 this.getClass().getSimpleName(),
337 _outputBufferSize, _outputAggregationSize,
338 _requestHeaderSize,_responseHeaderSize,
339 _secureScheme,_securePort,