View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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 _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 = false; //send Date: header
56  
57      public interface Customizer
58      {
59          public void customize(Connector connector, HttpConfiguration channelConfig, Request request);
60      }
61      
62      public interface ConnectionFactory
63      {
64          HttpConfiguration getHttpConfiguration();
65      }
66      
67      public HttpConfiguration()
68      {
69      }
70      
71      /* ------------------------------------------------------------ */
72      /** Create a configuration from another.
73       * @param config The configuration to copy.
74       */
75      public HttpConfiguration(HttpConfiguration config)
76      {
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;
86      }
87      
88      /* ------------------------------------------------------------ */
89      /** 
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
95       */
96      public void addCustomizer(Customizer customizer)
97      {
98          _customizers.add(customizer);
99      }
100     
101     /* ------------------------------------------------------------ */
102     public List<Customizer> getCustomizers()
103     {
104         return _customizers;
105     }
106     
107     public <T> T getCustomizer(Class<T> type)
108     {
109         for (Customizer c : _customizers)
110             if (type.isAssignableFrom(c.getClass()))
111                 return (T)c;
112         return null;
113     }
114 
115     @ManagedAttribute("The size in bytes of the output buffer used to aggregate HTTP output")
116     public int getOutputBufferSize()
117     {
118         return _outputBufferSize;
119     }
120     
121     @ManagedAttribute("The maximum allowed size in bytes for a HTTP request header")
122     public int getRequestHeaderSize()
123     {
124         return _requestHeaderSize;
125     }
126     
127     @ManagedAttribute("The maximum allowed size in bytes for a HTTP response header")
128     public int getResponseHeaderSize()
129     {
130         return _responseHeaderSize;
131     }
132 
133     @ManagedAttribute("The maximum allowed size in bytes for a HTTP header field cache")
134     public int getHeaderCacheSize()
135     {
136         return _headerCacheSize;
137     }
138 
139     @ManagedAttribute("The port to which Integral or Confidential security constraints are redirected")
140     public int getSecurePort()
141     {
142         return _securePort;
143     }
144     
145     @ManagedAttribute("The scheme with which Integral or Confidential security constraints are redirected")
146     public String getSecureScheme()
147     {
148         return _secureScheme;
149     }
150 
151     public void setSendServerVersion (boolean sendServerVersion)
152     {
153         _sendServerVersion = sendServerVersion;
154     }
155 
156     @ManagedAttribute("if true, send the Server header in responses")
157     public boolean getSendServerVersion()
158     {
159         return _sendServerVersion;
160     }
161     
162     public void setSendXPoweredBy (boolean sendXPoweredBy)
163     {
164         _sendXPoweredBy=sendXPoweredBy;
165     }
166 
167     @ManagedAttribute("if true, send the X-Powered-By header in responses")
168     public boolean getSendXPoweredBy()
169     {
170         return _sendXPoweredBy;
171     }
172 
173     public void setSendDateHeader(boolean sendDateHeader)
174     {
175         _sendDateHeader = sendDateHeader;
176     }
177 
178     @ManagedAttribute("if true, include the date in HTTP headers")
179     public boolean getSendDateHeader()
180     {
181         return _sendDateHeader;
182     }
183     
184     /* ------------------------------------------------------------ */
185     /**
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}). 
190      * @param customizers
191      */
192     public void setCustomizers(List<Customizer> customizers)
193     {
194         _customizers.clear();
195         _customizers.addAll(customizers);
196     }
197 
198     /* ------------------------------------------------------------ */
199     /**
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.
205      */
206     public void setOutputBufferSize(int responseBufferSize)
207     {
208         _outputBufferSize = responseBufferSize;
209     }
210     
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
215      * attacks.</p>
216      * @param requestHeaderSize Max header size in bytes
217      */
218     public void setRequestHeaderSize(int requestHeaderSize)
219     {
220         _requestHeaderSize = requestHeaderSize;
221     }
222 
223     /* ------------------------------------------------------------ */
224     /** Set the maximum size of a response header.
225      * 
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.
229      */
230     public void setResponseHeaderSize(int responseHeaderSize)
231     {
232         _responseHeaderSize = responseHeaderSize;
233     }
234 
235     /* ------------------------------------------------------------ */
236     /** Set the header field cache size.
237      * @param headerCacheSize The size in bytes of the header field cache.
238      */
239     public void setHeaderCacheSize(int headerCacheSize)
240     {
241         _headerCacheSize = headerCacheSize;
242     }
243 
244     /* ------------------------------------------------------------ */
245     /** Set the TCP/IP port used for CONFIDENTIAL and INTEGRAL 
246      * redirections.
247      * @param confidentialPort
248      */
249     public void setSecurePort(int confidentialPort)
250     {
251         _securePort = confidentialPort;
252     }
253 
254     /* ------------------------------------------------------------ */
255     /** Set the  URI scheme used for CONFIDENTIAL and INTEGRAL 
256      * redirections.
257      * @param confidentialScheme A string like"https"
258      */
259     public void setSecureScheme(String confidentialScheme)
260     {
261         _secureScheme = confidentialScheme;
262     }
263 
264     @Override
265     public String toString()
266     {
267         return String.format("%s@%x{%d,%d/%d,%s://:%d,%s}",this.getClass().getSimpleName(),hashCode(),_outputBufferSize,_requestHeaderSize,_responseHeaderSize,_secureScheme,_securePort,_customizers);
268     }
269 }