2 // ========================================================================
3 // Copyright (c) 1995-2014 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 _requestHeaderSize=8*1024;
49 private int _responseHeaderSize=8*1024;
50 private int _headerCacheSize=512;
51 private int _securePort;
52 private String _secureScheme = HttpScheme.HTTPS.asString();
53 private boolean _sendServerVersion = true; //send Server: header
54 private boolean _sendXPoweredBy = false; //send X-Powered-By: header
55 private boolean _sendDateHeader = true; //send Date: header
57 public interface Customizer
59 public void customize(Connector connector, HttpConfiguration channelConfig, Request request);
62 public interface ConnectionFactory
64 HttpConfiguration getHttpConfiguration();
67 public HttpConfiguration()
71 /* ------------------------------------------------------------ */
72 /** Create a configuration from another.
73 * @param config The configuration to copy.
75 public HttpConfiguration(HttpConfiguration config)
77 _customizers.addAll(config._customizers);
78 _outputBufferSize=config._outputBufferSize;
79 _requestHeaderSize=config._requestHeaderSize;
80 _responseHeaderSize=config._responseHeaderSize;
81 _securePort=config._securePort;
82 _secureScheme=config._secureScheme;
83 _sendDateHeader=config._sendDateHeader;
84 _sendServerVersion=config._sendServerVersion;
85 _headerCacheSize=config._headerCacheSize;
88 /* ------------------------------------------------------------ */
90 * <p>Add a {@link Customizer} that is invoked for every
91 * request received.</p>
92 * <p>Customiser are often used to interpret optional headers (eg {@link ForwardedRequestCustomizer}) or
93 * optional protocol semantics (eg {@link SecureRequestCustomizer}).
94 * @param customizer A request customizer
96 public void addCustomizer(Customizer customizer)
98 _customizers.add(customizer);
101 /* ------------------------------------------------------------ */
102 public List<Customizer> getCustomizers()
107 public <T> T getCustomizer(Class<T> type)
109 for (Customizer c : _customizers)
110 if (type.isAssignableFrom(c.getClass()))
115 @ManagedAttribute("The size in bytes of the output buffer used to aggregate HTTP output")
116 public int getOutputBufferSize()
118 return _outputBufferSize;
121 @ManagedAttribute("The maximum allowed size in bytes for a HTTP request header")
122 public int getRequestHeaderSize()
124 return _requestHeaderSize;
127 @ManagedAttribute("The maximum allowed size in bytes for a HTTP response header")
128 public int getResponseHeaderSize()
130 return _responseHeaderSize;
133 @ManagedAttribute("The maximum allowed size in bytes for a HTTP header field cache")
134 public int getHeaderCacheSize()
136 return _headerCacheSize;
139 @ManagedAttribute("The port to which Integral or Confidential security constraints are redirected")
140 public int getSecurePort()
145 @ManagedAttribute("The scheme with which Integral or Confidential security constraints are redirected")
146 public String getSecureScheme()
148 return _secureScheme;
151 public void setSendServerVersion (boolean sendServerVersion)
153 _sendServerVersion = sendServerVersion;
156 @ManagedAttribute("if true, send the Server header in responses")
157 public boolean getSendServerVersion()
159 return _sendServerVersion;
162 public void setSendXPoweredBy (boolean sendXPoweredBy)
164 _sendXPoweredBy=sendXPoweredBy;
167 @ManagedAttribute("if true, send the X-Powered-By header in responses")
168 public boolean getSendXPoweredBy()
170 return _sendXPoweredBy;
173 public void setSendDateHeader(boolean sendDateHeader)
175 _sendDateHeader = sendDateHeader;
178 @ManagedAttribute("if true, include the date in HTTP headers")
179 public boolean getSendDateHeader()
181 return _sendDateHeader;
184 /* ------------------------------------------------------------ */
186 * <p>Set the {@link Customizer}s that are invoked for every
187 * request received.</p>
188 * <p>Customisers are often used to interpret optional headers (eg {@link ForwardedRequestCustomizer}) or
189 * optional protocol semantics (eg {@link SecureRequestCustomizer}).
192 public void setCustomizers(List<Customizer> customizers)
194 _customizers.clear();
195 _customizers.addAll(customizers);
198 /* ------------------------------------------------------------ */
200 * Set the size of the buffer into which response content is aggregated
201 * before being sent to the client. A larger buffer can improve performance by allowing
202 * a content producer to run without blocking, however larger buffers consume more memory and
203 * may induce some latency before a client starts processing the content.
204 * @param responseBufferSize buffer size in bytes.
206 public void setOutputBufferSize(int responseBufferSize)
208 _outputBufferSize = responseBufferSize;
211 /* ------------------------------------------------------------ */
212 /** Set the maximum size of a request header.
213 * <p>Larger headers will allow for more and/or larger cookies plus larger form content encoded
214 * in a URL. However, larger headers consume more memory and can make a server more vulnerable to denial of service
216 * @param requestHeaderSize Max header size in bytes
218 public void setRequestHeaderSize(int requestHeaderSize)
220 _requestHeaderSize = requestHeaderSize;
223 /* ------------------------------------------------------------ */
224 /** Set the maximum size of a response header.
226 * <p>Larger headers will allow for more and/or larger cookies and longer HTTP headers (eg for redirection).
227 * However, larger headers will also consume more memory.</p>
228 * @param responseHeaderSize Response header size in bytes.
230 public void setResponseHeaderSize(int responseHeaderSize)
232 _responseHeaderSize = responseHeaderSize;
235 /* ------------------------------------------------------------ */
236 /** Set the header field cache size.
237 * @param headerCacheSize The size in bytes of the header field cache.
239 public void setHeaderCacheSize(int headerCacheSize)
241 _headerCacheSize = headerCacheSize;
244 /* ------------------------------------------------------------ */
245 /** Set the TCP/IP port used for CONFIDENTIAL and INTEGRAL
247 * @param confidentialPort
249 public void setSecurePort(int confidentialPort)
251 _securePort = confidentialPort;
254 /* ------------------------------------------------------------ */
255 /** Set the URI scheme used for CONFIDENTIAL and INTEGRAL
257 * @param confidentialScheme A string like"https"
259 public void setSecureScheme(String confidentialScheme)
261 _secureScheme = confidentialScheme;
265 public String toString()
267 return String.format("%s@%x{%d,%d/%d,%s://:%d,%s}",this.getClass().getSimpleName(),hashCode(),_outputBufferSize,_requestHeaderSize,_responseHeaderSize,_secureScheme,_securePort,_customizers);