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