View Javadoc

1   //
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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server;
20  
21  import java.util.List;
22  import java.util.concurrent.CopyOnWriteArrayList;
23  
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;
28  
29  
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.
39   * </p>
40   */
41  @ManagedObject("HTTP Configuration")
42  public class HttpConfiguration
43  {
44      public static final String SERVER_VERSION = "Jetty(" + Jetty.VERSION + ")";
45  
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;
58  
59      /* ------------------------------------------------------------ */
60      /** 
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.
65       * 
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
73       *  </ul>
74       */
75      public interface Customizer
76      {
77          public void customize(Connector connector, HttpConfiguration channelConfig, Request request);
78      }
79      
80      public interface ConnectionFactory
81      {
82          HttpConfiguration getHttpConfiguration();
83      }
84      
85      public HttpConfiguration()
86      {
87      }
88      
89      /* ------------------------------------------------------------ */
90      /** Create a configuration from another.
91       * @param config The configuration to copy.
92       */
93      public HttpConfiguration(HttpConfiguration config)
94      {
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;
105     }
106     
107     /* ------------------------------------------------------------ */
108     /** 
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
114      */
115     public void addCustomizer(Customizer customizer)
116     {
117         _customizers.add(customizer);
118     }
119     
120     /* ------------------------------------------------------------ */
121     public List<Customizer> getCustomizers()
122     {
123         return _customizers;
124     }
125 
126     /* ------------------------------------------------------------ */
127     public <T> T getCustomizer(Class<T> type)
128     {
129         for (Customizer c : _customizers)
130             if (type.isAssignableFrom(c.getClass()))
131                 return (T)c;
132         return null;
133     }
134 
135     /* ------------------------------------------------------------ */
136     @ManagedAttribute("The size in bytes of the output buffer used to aggregate HTTP output")
137     public int getOutputBufferSize()
138     {
139         return _outputBufferSize;
140     }
141 
142     /* ------------------------------------------------------------ */
143     @ManagedAttribute("The maximum size in bytes for HTTP output to be aggregated")
144     public int getOutputAggregationSize()
145     {
146         return _outputAggregationSize;
147     }
148 
149     /* ------------------------------------------------------------ */
150     @ManagedAttribute("The maximum allowed size in bytes for a HTTP request header")
151     public int getRequestHeaderSize()
152     {
153         return _requestHeaderSize;
154     }
155 
156     /* ------------------------------------------------------------ */
157     @ManagedAttribute("The maximum allowed size in bytes for a HTTP response header")
158     public int getResponseHeaderSize()
159     {
160         return _responseHeaderSize;
161     }
162 
163     /* ------------------------------------------------------------ */
164     @ManagedAttribute("The maximum allowed size in bytes for a HTTP header field cache")
165     public int getHeaderCacheSize()
166     {
167         return _headerCacheSize;
168     }
169 
170     /* ------------------------------------------------------------ */
171     @ManagedAttribute("The port to which Integral or Confidential security constraints are redirected")
172     public int getSecurePort()
173     {
174         return _securePort;
175     }
176 
177     /* ------------------------------------------------------------ */
178     @ManagedAttribute("The scheme with which Integral or Confidential security constraints are redirected")
179     public String getSecureScheme()
180     {
181         return _secureScheme;
182     }
183 
184     /* ------------------------------------------------------------ */
185     public void setSendServerVersion (boolean sendServerVersion)
186     {
187         _sendServerVersion = sendServerVersion;
188     }
189 
190     /* ------------------------------------------------------------ */
191     @ManagedAttribute("if true, send the Server header in responses")
192     public boolean getSendServerVersion()
193     {
194         return _sendServerVersion;
195     }
196 
197     /* ------------------------------------------------------------ */
198     public void setSendXPoweredBy (boolean sendXPoweredBy)
199     {
200         _sendXPoweredBy=sendXPoweredBy;
201     }
202 
203     /* ------------------------------------------------------------ */
204     @ManagedAttribute("if true, send the X-Powered-By header in responses")
205     public boolean getSendXPoweredBy()
206     {
207         return _sendXPoweredBy;
208     }
209 
210     /* ------------------------------------------------------------ */
211     public void setSendDateHeader(boolean sendDateHeader)
212     {
213         _sendDateHeader = sendDateHeader;
214     }
215 
216     /* ------------------------------------------------------------ */
217     @ManagedAttribute("if true, include the date in HTTP headers")
218     public boolean getSendDateHeader()
219     {
220         return _sendDateHeader;
221     }
222 
223     /* ------------------------------------------------------------ */
224     /**
225      * @param delay if true, delay the application dispatch until content is available
226      */
227     public void setDelayDispatchUntilContent(boolean delay)
228     {
229         _delayDispatchUntilContent = delay;
230     }
231 
232     /* ------------------------------------------------------------ */
233     @ManagedAttribute("if true, delay the application dispatch until content is available")
234     public boolean isDelayDispatchUntilContent()
235     {
236         return _delayDispatchUntilContent;
237     }
238 
239     /* ------------------------------------------------------------ */
240     /**
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
246      */
247     public void setCustomizers(List<Customizer> customizers)
248     {
249         _customizers.clear();
250         _customizers.addAll(customizers);
251     }
252 
253     /* ------------------------------------------------------------ */
254     /**
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 responseBufferSize buffer size in bytes.
260      */
261     public void setOutputBufferSize(int responseBufferSize)
262     {
263         _outputBufferSize = responseBufferSize;
264     }
265     
266     /* ------------------------------------------------------------ */
267     /**
268      * Set the max size of the response content write that is copied into the aggregate buffer.
269      * Writes that are smaller of this size are copied into the aggregate buffer, while
270      * writes that are larger of this size will cause the aggregate buffer to be flushed
271      * and the write to be executed without being copied.
272      * @param outputAggregationSize the max write size that is aggregated
273      */
274     public void setOutputAggregationSize(int outputAggregationSize)
275     {
276         _outputAggregationSize = outputAggregationSize;
277     }
278 
279     /* ------------------------------------------------------------ */
280     /** Set the maximum size of a request header.
281      * <p>Larger headers will allow for more and/or larger cookies plus larger form content encoded 
282      * in a URL. However, larger headers consume more memory and can make a server more vulnerable to denial of service
283      * attacks.</p>
284      * @param requestHeaderSize Max header size in bytes
285      */
286     public void setRequestHeaderSize(int requestHeaderSize)
287     {
288         _requestHeaderSize = requestHeaderSize;
289     }
290 
291     /* ------------------------------------------------------------ */
292     /** Set the maximum size of a response header.
293      * 
294      * <p>Larger headers will allow for more and/or larger cookies and longer HTTP headers (eg for redirection). 
295      * However, larger headers will also consume more memory.</p>
296      * @param responseHeaderSize Response header size in bytes.
297      */
298     public void setResponseHeaderSize(int responseHeaderSize)
299     {
300         _responseHeaderSize = responseHeaderSize;
301     }
302 
303     /* ------------------------------------------------------------ */
304     /** Set the header field cache size.
305      * @param headerCacheSize The size in bytes of the header field cache.
306      */
307     public void setHeaderCacheSize(int headerCacheSize)
308     {
309         _headerCacheSize = headerCacheSize;
310     }
311 
312     /* ------------------------------------------------------------ */
313     /** Set the TCP/IP port used for CONFIDENTIAL and INTEGRAL redirections.
314      * @param securePort the secure port to redirect to.
315      */
316     public void setSecurePort(int securePort)
317     {
318         _securePort = securePort;
319     }
320 
321     /* ------------------------------------------------------------ */
322     /** Set the  URI scheme used for CONFIDENTIAL and INTEGRAL redirections.
323      * @param secureScheme A scheme string like "https"
324      */
325     public void setSecureScheme(String secureScheme)
326     {
327         _secureScheme = secureScheme;
328     }
329 
330     @Override
331     public String toString()
332     {
333         return String.format("%s@%x{%d/%d,%d/%d,%s://:%d,%s}",
334                 this.getClass().getSimpleName(),
335                 hashCode(),
336                 _outputBufferSize, _outputAggregationSize,
337                 _requestHeaderSize,_responseHeaderSize,
338                 _secureScheme,_securePort,
339                 _customizers);
340     }
341 }