View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.config;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  import org.apache.hc.core5.http.HttpVersion;
33  import org.apache.hc.core5.util.Args;
34  import org.apache.hc.core5.util.Timeout;
35  
36  /**
37   * HTTP/1.1 protocol parameters.
38   * <p>
39   * Please note that line length is defined in bytes and not characters.
40   * This is only relevant however when using non-standard HTTP charsets
41   * for protocol elements such as UTF-8.
42   * </p>
43   *
44   * @since 4.3
45   */
46  @Contract(threading = ThreadingBehavior.IMMUTABLE)
47  public class Http1Config {
48  
49      public static final Http1Config DEFAULT = new Builder().build();
50  
51      private final HttpVersion version;
52      private final int bufferSize;
53      private final int chunkSizeHint;
54      private final Timeout waitForContinueTimeout;
55      private final int maxLineLength;
56      private final int maxHeaderCount;
57      private final int maxEmptyLineCount;
58      private final int initialWindowSize;
59  
60      Http1Config(final HttpVersion version, final int bufferSize, final int chunkSizeHint,
61                  final Timeout waitForContinueTimeout, final int maxLineLength, final int maxHeaderCount,
62                  final int maxEmptyLineCount, final int initialWindowSize) {
63          super();
64          this.version = version;
65          this.bufferSize = bufferSize;
66          this.chunkSizeHint = chunkSizeHint;
67          this.waitForContinueTimeout = waitForContinueTimeout;
68          this.maxLineLength = maxLineLength;
69          this.maxHeaderCount = maxHeaderCount;
70          this.maxEmptyLineCount = maxEmptyLineCount;
71          this.initialWindowSize = initialWindowSize;
72      }
73  
74      /**
75       * The effective protocol level expressed by the minor version of HTTP/1.x.
76       *
77       * @since 5.3
78       */
79      public HttpVersion getVersion() {
80          return version;
81      }
82  
83      public int getBufferSize() {
84          return bufferSize;
85      }
86  
87      public int getChunkSizeHint() {
88          return chunkSizeHint;
89      }
90  
91      public Timeout getWaitForContinueTimeout() {
92          return waitForContinueTimeout;
93      }
94  
95      public int getMaxLineLength() {
96          return maxLineLength;
97      }
98  
99      public int getMaxHeaderCount() {
100         return maxHeaderCount;
101     }
102 
103     public int getMaxEmptyLineCount() {
104         return this.maxEmptyLineCount;
105     }
106 
107     public int getInitialWindowSize() {
108         return initialWindowSize;
109     }
110 
111     @Override
112     public String toString() {
113         final StringBuilder builder = new StringBuilder();
114         builder.append("[version=").append(version)
115                 .append(", bufferSize=").append(bufferSize)
116                 .append(", chunkSizeHint=").append(chunkSizeHint)
117                 .append(", waitForContinueTimeout=").append(waitForContinueTimeout)
118                 .append(", maxLineLength=").append(maxLineLength)
119                 .append(", maxHeaderCount=").append(maxHeaderCount)
120                 .append(", maxEmptyLineCount=").append(maxEmptyLineCount)
121                 .append(", initialWindowSize=").append(initialWindowSize)
122                 .append("]");
123         return builder.toString();
124     }
125 
126     public static Http1Config.Builder custom() {
127         return new Builder();
128     }
129     public static Http1Config.Builder copy(final Http1Config config) {
130         Args.notNull(config, "Config");
131         return new Builder()
132                 .setVersion(config.getVersion())
133                 .setBufferSize(config.getBufferSize())
134                 .setChunkSizeHint(config.getChunkSizeHint())
135                 .setWaitForContinueTimeout(config.getWaitForContinueTimeout())
136                 .setMaxHeaderCount(config.getMaxHeaderCount())
137                 .setMaxLineLength(config.getMaxLineLength())
138                 .setMaxEmptyLineCount(config.getMaxEmptyLineCount())
139                 .setInitialWindowSize(config.getInitialWindowSize());
140     }
141 
142     private static final int INIT_WINDOW_SIZE = 65535;
143     private static final int INIT_BUF_SIZE = 8192;
144     private static final Timeout INIT_WAIT_FOR_CONTINUE = Timeout.ofSeconds(3);
145     private static final int INIT_BUF_CHUNK = -1;
146     private static final int INIT_MAX_HEADER_COUNT = -1;
147     private static final int INIT_MAX_LINE_LENGTH = -1;
148     private static final int INIT_MAX_EMPTY_LINE_COUNT = 10;
149 
150     public static class Builder {
151 
152         private HttpVersion version;
153         private int bufferSize;
154         private int chunkSizeHint;
155         private Timeout waitForContinueTimeout;
156         private int maxLineLength;
157         private int maxHeaderCount;
158         private int maxEmptyLineCount;
159         private int initialWindowSize;
160 
161         Builder() {
162             this.version = HttpVersion.HTTP_1_1;
163             this.bufferSize = INIT_BUF_SIZE;
164             this.chunkSizeHint = INIT_BUF_CHUNK;
165             this.waitForContinueTimeout = INIT_WAIT_FOR_CONTINUE;
166             this.maxLineLength = INIT_MAX_LINE_LENGTH;
167             this.maxHeaderCount = INIT_MAX_HEADER_COUNT;
168             this.maxEmptyLineCount = INIT_MAX_EMPTY_LINE_COUNT;
169             this.initialWindowSize = INIT_WINDOW_SIZE;
170         }
171 
172         /**
173          * Sets the effective HTTP/1.x protocol level (as expressed by the minor version).
174          * Presently only {@link HttpVersion#HTTP_1_0} and {@link HttpVersion#HTTP_1_1} are
175          * supported.
176          *
177          * @since 5.3
178          */
179         public Builder setVersion(final HttpVersion version) {
180             Args.notNull(version, "HTTP/1 protocol version");
181             Args.check(version.getMajor() == 1, "HTTP/1 protocol version is required");
182             this.version = version;
183             return this;
184         }
185 
186         public Builder setBufferSize(final int bufferSize) {
187             this.bufferSize = bufferSize;
188             return this;
189         }
190 
191         public Builder setChunkSizeHint(final int chunkSizeHint) {
192             this.chunkSizeHint = chunkSizeHint;
193             return this;
194         }
195 
196         public Builder setWaitForContinueTimeout(final Timeout waitForContinueTimeout) {
197             this.waitForContinueTimeout = waitForContinueTimeout;
198             return this;
199         }
200 
201         public Builder setMaxLineLength(final int maxLineLength) {
202             this.maxLineLength = maxLineLength;
203             return this;
204         }
205 
206         public Builder setMaxHeaderCount(final int maxHeaderCount) {
207             this.maxHeaderCount = maxHeaderCount;
208             return this;
209         }
210 
211         public Builder setMaxEmptyLineCount(final int maxEmptyLineCount) {
212             this.maxEmptyLineCount = maxEmptyLineCount;
213             return this;
214         }
215 
216         public Builder setInitialWindowSize(final int initialWindowSize) {
217             Args.positive(initialWindowSize, "Initial window size");
218             this.initialWindowSize = initialWindowSize;
219             return this;
220         }
221 
222         public Http1Config build() {
223             return new Http1Config(
224                     version,
225                     bufferSize,
226                     chunkSizeHint,
227                     waitForContinueTimeout,
228                     maxLineLength,
229                     maxHeaderCount,
230                     maxEmptyLineCount,
231                     initialWindowSize);
232         }
233 
234     }
235 
236 }