1 module hunt.http.codec.http.stream.Http2Configuration; 2 3 import hunt.http.codec.http.stream.FlowControlStrategy; 4 5 import hunt.net.secure.SecureSessionFactory; 6 import hunt.net.Config; 7 import hunt.http.codec.http.model.HttpVersion; 8 import hunt.net.secure.conscrypt.ConscryptSecureSessionFactory; 9 import hunt.net.secure.conscrypt.AbstractConscryptSSLContextFactory; 10 11 class Http2Configuration { 12 13 // TCP settings 14 private Config tcpConfiguration; 15 16 // SSL/TLS settings 17 private bool _isSecureConnectionEnabled; 18 private SecureSessionFactory secureSessionFactory; 19 // private string _sslCertificate; 20 // private string _sslPrivateKey; 21 22 // HTTP settings 23 private int maxDynamicTableSize = 4096; 24 private int streamIdleTimeout = 10 * 1000; 25 private string flowControlStrategy = "buffer"; 26 private int initialStreamSendWindow = FlowControlStrategy.DEFAULT_WINDOW_SIZE; 27 private int initialSessionRecvWindow = FlowControlStrategy.DEFAULT_WINDOW_SIZE; 28 private int maxConcurrentStreams = -1; 29 private int maxHeaderBlockFragment = 0; 30 private int maxRequestHeadLength = 4 * 1024; 31 private int maxRequestTrailerLength = 4 * 1024; 32 private int maxResponseHeadLength = 4 * 1024; 33 private int maxResponseTrailerLength = 4 * 1024; 34 private string characterEncoding = "UTF-8"; 35 private string protocol; // HTTP/2.0, HTTP/1.1 36 private int http2PingInterval = 10 * 1000; 37 38 // WebSocket settings 39 private int websocketPingInterval = 10 * 1000; 40 41 this() 42 { 43 version(WithTLS) { 44 secureSessionFactory = new ConscryptSecureSessionFactory( 45 new NoCheckConscryptSSLContextFactory(), 46 new DefaultCredentialConscryptSSLContextFactory() 47 ); 48 } 49 tcpConfiguration = new hunt.net.Config.Config(); 50 protocol = HttpVersion.HTTP_1_1.asString(); 51 } 52 53 /** 54 * Get the TCP configuration. 55 * 56 * @return The TCP configuration. 57 */ 58 hunt.net.Config.Config getTcpConfiguration() { 59 return tcpConfiguration; 60 } 61 62 /** 63 * Set the TCP configuration. 64 * 65 * @param tcpConfiguration The TCP configuration. 66 */ 67 void setTcpConfiguration(hunt.net.Config.Config tcpConfiguration) { 68 this.tcpConfiguration = tcpConfiguration; 69 } 70 71 /** 72 * Get the max dynamic table size of HTTP2 protocol. 73 * 74 * @return The max dynamic table size of HTTP2 protocol. 75 */ 76 int getMaxDynamicTableSize() { 77 return maxDynamicTableSize; 78 } 79 80 /** 81 * Set the max dynamic table size of HTTP2 protocol. 82 * 83 * @param maxDynamicTableSize The max dynamic table size of HTTP2 protocol. 84 */ 85 void setMaxDynamicTableSize(int maxDynamicTableSize) { 86 this.maxDynamicTableSize = maxDynamicTableSize; 87 } 88 89 /** 90 * Get the HTTP2 stream idle timeout. The time unit is millisecond. 91 * 92 * @return The HTTP2 stream idle timeout. The time unit is millisecond. 93 */ 94 int getStreamIdleTimeout() { 95 return streamIdleTimeout; 96 } 97 98 /** 99 * Set the HTTP2 stream idle timeout. The time unit is millisecond. 100 * 101 * @param streamIdleTimeout The HTTP2 stream idle timeout. The time unit is millisecond. 102 */ 103 void setStreamIdleTimeout(int streamIdleTimeout) { 104 this.streamIdleTimeout = streamIdleTimeout; 105 } 106 107 /** 108 * Get the HTTP2 flow control strategy. The value is "simple" or "buffer". 109 * If you use the "simple" flow control strategy, once the server or client receives the data, it will send the WindowUpdateFrame. 110 * If you use the "buffer" flow control strategy, the server or client will send WindowUpdateFrame when the consumed data exceed the threshold. 111 * 112 * @return The HTTP2 flow control strategy. The value is "simple" or "buffer". 113 */ 114 string getFlowControlStrategy() { 115 return flowControlStrategy; 116 } 117 118 /** 119 * Set the HTTP2 flow control strategy. The value is "simple" or "buffer". 120 * If you use the "simple" flow control strategy, once the server or client receives the data, it will send the WindowUpdateFrame. 121 * If you use the "buffer" flow control strategy, the server or client will send WindowUpdateFrame when the consumed data exceed the threshold. 122 * 123 * @param flowControlStrategy The HTTP2 flow control strategy. The value is "simple" or "buffer". 124 */ 125 void setFlowControlStrategy(string flowControlStrategy) { 126 this.flowControlStrategy = flowControlStrategy; 127 } 128 129 /** 130 * Get the HTTP2 initial receiving window size. The unit is byte. 131 * 132 * @return the HTTP2 initial receiving window size. The unit is byte. 133 */ 134 int getInitialSessionRecvWindow() { 135 return initialSessionRecvWindow; 136 } 137 138 /** 139 * Set the HTTP2 initial receiving window size. The unit is byte. 140 * 141 * @param initialSessionRecvWindow The HTTP2 initial receiving window size. The unit is byte. 142 */ 143 void setInitialSessionRecvWindow(int initialSessionRecvWindow) { 144 this.initialSessionRecvWindow = initialSessionRecvWindow; 145 } 146 147 /** 148 * Get the HTTP2 initial sending window size. The unit is byte. 149 * 150 * @return The HTTP2 initial sending window size. The unit is byte. 151 */ 152 int getInitialStreamSendWindow() { 153 return initialStreamSendWindow; 154 } 155 156 /** 157 * Set the HTTP2 initial sending window size. The unit is byte. 158 * 159 * @param initialStreamSendWindow the HTTP2 initial sending window size. The unit is byte. 160 */ 161 void setInitialStreamSendWindow(int initialStreamSendWindow) { 162 this.initialStreamSendWindow = initialStreamSendWindow; 163 } 164 165 /** 166 * Get the max concurrent stream size in a HTTP2 session. 167 * 168 * @return the max concurrent stream size in a HTTP2 session. 169 */ 170 int getMaxConcurrentStreams() { 171 return maxConcurrentStreams; 172 } 173 174 /** 175 * Set the max concurrent stream size in a HTTP2 session. 176 * 177 * @param maxConcurrentStreams the max concurrent stream size in a HTTP2 session. 178 */ 179 void setMaxConcurrentStreams(int maxConcurrentStreams) { 180 this.maxConcurrentStreams = maxConcurrentStreams; 181 } 182 183 /** 184 * Set the max HTTP2 header block size. If the header block size more the this value, 185 * the server or client will split the header buffer to many buffers to send. 186 * 187 * @return the max HTTP2 header block size. 188 */ 189 int getMaxHeaderBlockFragment() { 190 return maxHeaderBlockFragment; 191 } 192 193 /** 194 * Get the max HTTP2 header block size. If the header block size more the this value, 195 * the server or client will split the header buffer to many buffers to send. 196 * 197 * @param maxHeaderBlockFragment The max HTTP2 header block size. 198 */ 199 void setMaxHeaderBlockFragment(int maxHeaderBlockFragment) { 200 this.maxHeaderBlockFragment = maxHeaderBlockFragment; 201 } 202 203 /** 204 * Get the max HTTP request header size. 205 * 206 * @return the max HTTP request header size. 207 */ 208 int getMaxRequestHeadLength() { 209 return maxRequestHeadLength; 210 } 211 212 /** 213 * Set the max HTTP request header size. 214 * 215 * @param maxRequestHeadLength the max HTTP request header size. 216 */ 217 void setMaxRequestHeadLength(int maxRequestHeadLength) { 218 this.maxRequestHeadLength = maxRequestHeadLength; 219 } 220 221 /** 222 * Get the max HTTP response header size. 223 * 224 * @return the max HTTP response header size. 225 */ 226 int getMaxResponseHeadLength() { 227 return maxResponseHeadLength; 228 } 229 230 /** 231 * Set the max HTTP response header size. 232 * 233 * @param maxResponseHeadLength the max HTTP response header size. 234 */ 235 void setMaxResponseHeadLength(int maxResponseHeadLength) { 236 this.maxResponseHeadLength = maxResponseHeadLength; 237 } 238 239 /** 240 * Get the max HTTP request trailer size. 241 * 242 * @return the max HTTP request trailer size. 243 */ 244 int getMaxRequestTrailerLength() { 245 return maxRequestTrailerLength; 246 } 247 248 /** 249 * Set the max HTTP request trailer size. 250 * 251 * @param maxRequestTrailerLength the max HTTP request trailer size. 252 */ 253 void setMaxRequestTrailerLength(int maxRequestTrailerLength) { 254 this.maxRequestTrailerLength = maxRequestTrailerLength; 255 } 256 257 /** 258 * Get the max HTTP response trailer size. 259 * 260 * @return the max HTTP response trailer size. 261 */ 262 int getMaxResponseTrailerLength() { 263 return maxResponseTrailerLength; 264 } 265 266 /** 267 * Set the max HTTP response trailer size. 268 * 269 * @param maxResponseTrailerLength the max HTTP response trailer size. 270 */ 271 void setMaxResponseTrailerLength(int maxResponseTrailerLength) { 272 this.maxResponseTrailerLength = maxResponseTrailerLength; 273 } 274 275 /** 276 * Get the charset of the text HTTP body. 277 * 278 * @return the charset of the text HTTP body. 279 */ 280 string getCharacterEncoding() { 281 return characterEncoding; 282 } 283 284 /** 285 * Set the charset of the text HTTP body. 286 * 287 * @param characterEncoding the charset of the text HTTP body. 288 */ 289 void setCharacterEncoding(string characterEncoding) { 290 this.characterEncoding = characterEncoding; 291 } 292 293 /** 294 * If return true, the server or client enable the SSL/TLS connection. 295 * 296 * @return If return true, the server or client enable the SSL/TLS connection. 297 */ 298 bool isSecureConnectionEnabled() { 299 return _isSecureConnectionEnabled; 300 } 301 302 /** 303 * If set true, the server or client enable the SSL/TLS connection. 304 * 305 * @param isSecureConnectionEnabled If set true, the server or client enable the SSL/TLS connection. 306 */ 307 void setSecureConnectionEnabled(bool status) { 308 this._isSecureConnectionEnabled = status; 309 } 310 311 /** 312 * Get the SSL/TLS connection factory. 313 * 314 * @return the SSL/TLS connection factory. 315 */ 316 SecureSessionFactory getSecureSessionFactory() { 317 return secureSessionFactory; 318 } 319 320 /** 321 * Set the SSL/TLS connection factory. 322 * 323 * @param secureSessionFactory the SSL/TLS connection factory. 324 */ 325 void setSecureSessionFactory(SecureSessionFactory secureSessionFactory) { 326 this.secureSessionFactory = secureSessionFactory; 327 } 328 329 // string sslCertificate() { 330 // return _sslCertificate; 331 // } 332 333 // void sslCertificate(string fileName) { 334 // _sslCertificate = fileName; 335 // } 336 337 // string sslPrivateKey() { 338 // return _sslPrivateKey; 339 // } 340 341 // void sslPrivateKey(string fileName) { 342 // _sslPrivateKey = fileName; 343 // } 344 345 346 /** 347 * Get the default HTTP protocol version. The value is "HTTP/2.0" or "HTTP/1.1". If the value is null, 348 * the server or client will negotiate a HTTP protocol version using ALPN. 349 * 350 * @return the default HTTP protocol version. The value is "HTTP/2.0" or "HTTP/1.1". 351 */ 352 string getProtocol() { 353 return protocol; 354 } 355 356 /** 357 * Set the default HTTP protocol version. The value is "HTTP/2.0" or "HTTP/1.1". If the value is null, 358 * the server or client will negotiate a HTTP protocol version using ALPN. 359 * 360 * @param protocol the default HTTP protocol version. The value is "HTTP/2.0" or "HTTP/1.1". 361 */ 362 void setProtocol(string protocol) { 363 this.protocol = protocol; 364 } 365 366 /** 367 * Get the HTTP2 connection sending ping frame interval. The time unit is millisecond. 368 * 369 * @return the sending ping frame interval. The time unit is millisecond. 370 */ 371 int getHttp2PingInterval() { 372 return http2PingInterval; 373 } 374 375 /** 376 * Set the sending ping frame interval. The time unit is millisecond. 377 * 378 * @param http2PingInterval the sending ping frame interval. The time unit is millisecond. 379 */ 380 void setHttp2PingInterval(int http2PingInterval) { 381 this.http2PingInterval = http2PingInterval; 382 } 383 384 /** 385 * Get the WebSocket connection sending ping frame interval. The time unit is millisecond. 386 * 387 * @return the WebSocket connection sending ping frame interval. The time unit is millisecond. 388 */ 389 int getWebsocketPingInterval() { 390 return websocketPingInterval; 391 } 392 393 /** 394 * Set the WebSocket connection sending ping frame interval. The time unit is millisecond. 395 * 396 * @param websocketPingInterval the WebSocket connection sending ping frame interval. The time unit is millisecond. 397 */ 398 void setWebsocketPingInterval(int websocketPingInterval) { 399 this.websocketPingInterval = websocketPingInterval; 400 } 401 }