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