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.annotation.ManagedAttribute;
26  import org.eclipse.jetty.util.annotation.ManagedObject;
27  
28  
29  /* ------------------------------------------------------------ */
30  /** HTTP Configuration.
31   * <p>This class is a holder of HTTP configuration for use by the 
32   * {@link HttpChannel} class.  Typically a HTTPConfiguration instance
33   * is instantiated and passed to a {@link ConnectionFactory} that can 
34   * create HTTP channels (eg HTTP, AJP or SPDY).</p>
35   * <p>The configuration held by this class is not for the wire protocol,
36   * but for the interpretation and handling of HTTP requests that could
37   * be transported by a variety of protocols.
38   * </p>
39   */
40  @ManagedObject("HTTP Configuration")
41  public class HttpConfiguration
42  {
43      private List<Customizer> _customizers=new CopyOnWriteArrayList<>();
44      private int _outputBufferSize=32*1024;
45      private int _requestHeaderSize=8*1024;
46      private int _responseHeaderSize=8*1024;
47      private int _securePort;
48      private String _secureScheme = HttpScheme.HTTPS.asString();
49      private boolean _sendServerVersion = true; //send Server: header
50      private boolean _sendDateHeader = false; //send Date: header
51      
52  
53      public interface Customizer
54      {
55          public void customize(Connector connector, HttpConfiguration channelConfig, Request request);
56      }
57      
58      public interface ConnectionFactory
59      {
60          HttpConfiguration getHttpConfiguration();
61      }
62      
63      public HttpConfiguration()
64      {
65      }
66      
67      /* ------------------------------------------------------------ */
68      /** Create a configuration from another.
69       * @param config The configuration to copy.
70       */
71      public HttpConfiguration(HttpConfiguration config)
72      {
73          _customizers.addAll(config._customizers);
74          _outputBufferSize=config._outputBufferSize;
75          _requestHeaderSize=config._requestHeaderSize;
76          _responseHeaderSize=config._responseHeaderSize;
77          _securePort=config._securePort;
78          _secureScheme=config._secureScheme;
79          _sendDateHeader=config._sendDateHeader;
80          _sendServerVersion=config._sendServerVersion;
81      }
82      
83      /* ------------------------------------------------------------ */
84      /** 
85       * <p>Add a {@link Customizer} that is invoked for every 
86       * request received.</p>
87       * <p>Customiser are often used to interpret optional headers (eg {@link ForwardedRequestCustomizer}) or 
88       * optional protocol semantics (eg {@link SecureRequestCustomizer}). 
89       * @param customizer A request customizer
90       */
91      public void addCustomizer(Customizer customizer)
92      {
93          _customizers.add(customizer);
94      }
95      
96      /* ------------------------------------------------------------ */
97      public List<Customizer> getCustomizers()
98      {
99          return _customizers;
100     }
101     
102     public <T> T getCustomizer(Class<T> type)
103     {
104         for (Customizer c : _customizers)
105             if (type.isAssignableFrom(c.getClass()))
106                 return (T)c;
107         return null;
108     }
109 
110     @ManagedAttribute("The size in bytes of the output buffer used to aggregate HTTP output")
111     public int getOutputBufferSize()
112     {
113         return _outputBufferSize;
114     }
115     
116     @ManagedAttribute("The maximum allowed size in bytes for a HTTP request header")
117     public int getRequestHeaderSize()
118     {
119         return _requestHeaderSize;
120     }
121     
122     @ManagedAttribute("The maximum allowed size in bytes for a HTTP response header")
123     public int getResponseHeaderSize()
124     {
125         return _responseHeaderSize;
126     }
127     
128     @ManagedAttribute("The port to which Integral or Confidential security constraints are redirected")
129     public int getSecurePort()
130     {
131         return _securePort;
132     }
133     
134     @ManagedAttribute("The scheme with which Integral or Confidential security constraints are redirected")
135     public String getSecureScheme()
136     {
137         return _secureScheme;
138     }
139 
140     public void setSendServerVersion (boolean sendServerVersion)
141     {
142         _sendServerVersion = sendServerVersion;
143     }
144 
145     @ManagedAttribute("if true, include the server version in HTTP headers")
146     public boolean getSendServerVersion()
147     {
148         return _sendServerVersion;
149     }
150 
151     public void setSendDateHeader(boolean sendDateHeader)
152     {
153         _sendDateHeader = sendDateHeader;
154     }
155 
156     @ManagedAttribute("if true, include the date in HTTP headers")
157     public boolean getSendDateHeader()
158     {
159         return _sendDateHeader;
160     }
161     
162     /* ------------------------------------------------------------ */
163     /**
164      * <p>Set the {@link Customizer}s that are invoked for every 
165      * request received.</p>
166      * <p>Customisers are often used to interpret optional headers (eg {@link ForwardedRequestCustomizer}) or 
167      * optional protocol semantics (eg {@link SecureRequestCustomizer}). 
168      * @param customizers
169      */
170     public void setCustomizers(List<Customizer> customizers)
171     {
172         _customizers.clear();
173         _customizers.addAll(customizers);
174     }
175 
176     /* ------------------------------------------------------------ */
177     /**
178      * Set the size of the buffer into which response content is aggregated
179      * before being sent to the client.  A larger buffer can improve performance by allowing
180      * a content producer to run without blocking, however larger buffers consume more memory and
181      * may induce some latency before a client starts processing the content.
182      * @param responseBufferSize buffer size in bytes.
183      */
184     public void setOutputBufferSize(int responseBufferSize)
185     {
186         _outputBufferSize = responseBufferSize;
187     }
188     
189     /* ------------------------------------------------------------ */
190     /** Set the maximum size of a request header.
191      * <p>Larger headers will allow for more and/or larger cookies plus larger form content encoded 
192      * in a URL. However, larger headers consume more memory and can make a server more vulnerable to denial of service
193      * attacks.</p>
194      * @param requestHeaderSize Max header size in bytes
195      */
196     public void setRequestHeaderSize(int requestHeaderSize)
197     {
198         _requestHeaderSize = requestHeaderSize;
199     }
200 
201     /* ------------------------------------------------------------ */
202     /** Set the maximum size of a response header.
203      * 
204      * <p>Larger headers will allow for more and/or larger cookies and longer HTTP headers (eg for redirection). 
205      * However, larger headers will also consume more memory.</p>
206      * @param responseHeaderSize Response header size in bytes.
207      */
208     public void setResponseHeaderSize(int responseHeaderSize)
209     {
210         _responseHeaderSize = responseHeaderSize;
211     }
212 
213     /* ------------------------------------------------------------ */
214     /** Set the TCP/IP port used for CONFIDENTIAL and INTEGRAL 
215      * redirections.
216      * @param confidentialPort
217      */
218     public void setSecurePort(int confidentialPort)
219     {
220         _securePort = confidentialPort;
221     }
222 
223     /* ------------------------------------------------------------ */
224     /** Set the  URI scheme used for CONFIDENTIAL and INTEGRAL 
225      * redirections.
226      * @param confidentialScheme A string like"https"
227      */
228     public void setSecureScheme(String confidentialScheme)
229     {
230         _secureScheme = confidentialScheme;
231     }
232 
233     @Override
234     public String toString()
235     {
236         return String.format("%s@%x{%d,%d/%d,%s://:%d,%s}",this.getClass().getSimpleName(),hashCode(),_outputBufferSize,_requestHeaderSize,_responseHeaderSize,_secureScheme,_securePort,_customizers);
237     }
238 }