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.util.Args;
33  import org.apache.hc.core5.util.Timeout;
34  
35  /**
36   * HTTP/1.1 protocol parameters.
37   * <p>
38   * Please note that line length is defined in bytes and not characters.
39   * This is only relevant however when using non-standard HTTP charsets
40   * for protocol elements such as UTF-8.
41   * </p>
42   *
43   * @since 4.3
44   */
45  @Contract(threading = ThreadingBehavior.IMMUTABLE)
46  public class Http1Config {
47  
48      public static final Http1Config DEFAULT = new Builder().build();
49  
50      private final int bufferSize;
51      private final int chunkSizeHint;
52      private final Timeout waitForContinueTimeout;
53      private final int maxLineLength;
54      private final int maxHeaderCount;
55      private final int maxEmptyLineCount;
56      private final int initialWindowSize;
57  
58      Http1Config(final int bufferSize, final int chunkSizeHint, final Timeout waitForContinueTimeout,
59                  final int maxLineLength, final int maxHeaderCount, final int maxEmptyLineCount,
60                  final int initialWindowSize) {
61          super();
62          this.bufferSize = bufferSize;
63          this.chunkSizeHint = chunkSizeHint;
64          this.waitForContinueTimeout = waitForContinueTimeout;
65          this.maxLineLength = maxLineLength;
66          this.maxHeaderCount = maxHeaderCount;
67          this.maxEmptyLineCount = maxEmptyLineCount;
68          this.initialWindowSize = initialWindowSize;
69      }
70  
71      public int getBufferSize() {
72          return bufferSize;
73      }
74  
75      public int getChunkSizeHint() {
76          return chunkSizeHint;
77      }
78  
79      public Timeout getWaitForContinueTimeout() {
80          return waitForContinueTimeout;
81      }
82  
83      public int getMaxLineLength() {
84          return maxLineLength;
85      }
86  
87      public int getMaxHeaderCount() {
88          return maxHeaderCount;
89      }
90  
91      public int getMaxEmptyLineCount() {
92          return this.maxEmptyLineCount;
93      }
94  
95      public int getInitialWindowSize() {
96          return initialWindowSize;
97      }
98  
99      @Override
100     public String toString() {
101         final StringBuilder builder = new StringBuilder();
102         builder.append("[bufferSize=").append(bufferSize)
103                 .append(", chunkSizeHint=").append(chunkSizeHint)
104                 .append(", waitForContinueTimeout=").append(waitForContinueTimeout)
105                 .append(", maxLineLength=").append(maxLineLength)
106                 .append(", maxHeaderCount=").append(maxHeaderCount)
107                 .append(", maxEmptyLineCount=").append(maxEmptyLineCount)
108                 .append(", initialWindowSize=").append(initialWindowSize)
109                 .append("]");
110         return builder.toString();
111     }
112 
113     public static Http1Config.Builder custom() {
114         return new Builder();
115     }
116     public static Http1Config.Builder copy(final Http1Config config) {
117         Args.notNull(config, "Config");
118         return new Builder()
119                 .setBufferSize(config.getBufferSize())
120                 .setChunkSizeHint(config.getChunkSizeHint())
121                 .setWaitForContinueTimeout(config.getWaitForContinueTimeout())
122                 .setMaxHeaderCount(config.getMaxHeaderCount())
123                 .setMaxLineLength(config.getMaxLineLength())
124                 .setMaxEmptyLineCount(config.getMaxEmptyLineCount())
125                 .setInitialWindowSize(config.getInitialWindowSize());
126     }
127 
128     private static final int INIT_WINDOW_SIZE = 65535;
129     private static final int INIT_BUF_SIZE = 8192;
130     private static final Timeout INIT_WAIT_FOR_CONTINUE = Timeout.ofSeconds(3);
131     private static final int INIT_BUF_CHUNK = -1;
132     private static final int INIT_MAX_HEADER_COUNT = -1;
133     private static final int INIT_MAX_LINE_LENGTH = -1;
134     private static final int INIT_MAX_EMPTY_LINE_COUNT = 10;
135 
136     public static class Builder {
137 
138         private int bufferSize;
139         private int chunkSizeHint;
140         private Timeout waitForContinueTimeout;
141         private int maxLineLength;
142         private int maxHeaderCount;
143         private int maxEmptyLineCount;
144         private int initialWindowSize;
145 
146         Builder() {
147             this.bufferSize = INIT_BUF_SIZE;
148             this.chunkSizeHint = INIT_BUF_CHUNK;
149             this.waitForContinueTimeout = INIT_WAIT_FOR_CONTINUE;
150             this.maxLineLength = INIT_MAX_LINE_LENGTH;
151             this.maxHeaderCount = INIT_MAX_HEADER_COUNT;
152             this.maxEmptyLineCount = INIT_MAX_EMPTY_LINE_COUNT;
153             this.initialWindowSize = INIT_WINDOW_SIZE;
154         }
155 
156         public Builder setBufferSize(final int bufferSize) {
157             this.bufferSize = bufferSize;
158             return this;
159         }
160 
161         public Builder setChunkSizeHint(final int chunkSizeHint) {
162             this.chunkSizeHint = chunkSizeHint;
163             return this;
164         }
165 
166         public Builder setWaitForContinueTimeout(final Timeout waitForContinueTimeout) {
167             this.waitForContinueTimeout = waitForContinueTimeout;
168             return this;
169         }
170 
171         public Builder setMaxLineLength(final int maxLineLength) {
172             this.maxLineLength = maxLineLength;
173             return this;
174         }
175 
176         public Builder setMaxHeaderCount(final int maxHeaderCount) {
177             this.maxHeaderCount = maxHeaderCount;
178             return this;
179         }
180 
181         public Builder setMaxEmptyLineCount(final int maxEmptyLineCount) {
182             this.maxEmptyLineCount = maxEmptyLineCount;
183             return this;
184         }
185 
186         public Builder setInitialWindowSize(final int initialWindowSize) {
187             Args.positive(initialWindowSize, "Initial window size");
188             this.initialWindowSize = initialWindowSize;
189             return this;
190         }
191 
192         public Http1Config build() {
193             return new Http1Config(
194                     bufferSize,
195                     chunkSizeHint,
196                     waitForContinueTimeout,
197                     maxLineLength,
198                     maxHeaderCount,
199                     maxEmptyLineCount,
200                     initialWindowSize);
201         }
202 
203     }
204 
205 }