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