config.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2024 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "math"
  7. "net/http"
  8. "time"
  9. )
  10. // http2Config is a package-internal version of net/http.HTTP2Config.
  11. //
  12. // http.HTTP2Config was added in Go 1.24.
  13. // When running with a version of net/http that includes HTTP2Config,
  14. // we merge the configuration with the fields in Transport or Server
  15. // to produce an http2Config.
  16. //
  17. // Zero valued fields in http2Config are interpreted as in the
  18. // net/http.HTTPConfig documentation.
  19. //
  20. // Precedence order for reconciling configurations is:
  21. //
  22. // - Use the net/http.{Server,Transport}.HTTP2Config value, when non-zero.
  23. // - Otherwise use the http2.{Server.Transport} value.
  24. // - If the resulting value is zero or out of range, use a default.
  25. type http2Config struct {
  26. MaxConcurrentStreams uint32
  27. MaxDecoderHeaderTableSize uint32
  28. MaxEncoderHeaderTableSize uint32
  29. MaxReadFrameSize uint32
  30. MaxUploadBufferPerConnection int32
  31. MaxUploadBufferPerStream int32
  32. SendPingTimeout time.Duration
  33. PingTimeout time.Duration
  34. WriteByteTimeout time.Duration
  35. PermitProhibitedCipherSuites bool
  36. CountError func(errType string)
  37. }
  38. // configFromServer merges configuration settings from
  39. // net/http.Server.HTTP2Config and http2.Server.
  40. func configFromServer(h1 *http.Server, h2 *Server) http2Config {
  41. conf := http2Config{
  42. MaxConcurrentStreams: h2.MaxConcurrentStreams,
  43. MaxEncoderHeaderTableSize: h2.MaxEncoderHeaderTableSize,
  44. MaxDecoderHeaderTableSize: h2.MaxDecoderHeaderTableSize,
  45. MaxReadFrameSize: h2.MaxReadFrameSize,
  46. MaxUploadBufferPerConnection: h2.MaxUploadBufferPerConnection,
  47. MaxUploadBufferPerStream: h2.MaxUploadBufferPerStream,
  48. SendPingTimeout: h2.ReadIdleTimeout,
  49. PingTimeout: h2.PingTimeout,
  50. WriteByteTimeout: h2.WriteByteTimeout,
  51. PermitProhibitedCipherSuites: h2.PermitProhibitedCipherSuites,
  52. CountError: h2.CountError,
  53. }
  54. fillNetHTTPServerConfig(&conf, h1)
  55. setConfigDefaults(&conf, true)
  56. return conf
  57. }
  58. // configFromTransport merges configuration settings from h2 and h2.t1.HTTP2
  59. // (the net/http Transport).
  60. func configFromTransport(h2 *Transport) http2Config {
  61. conf := http2Config{
  62. MaxEncoderHeaderTableSize: h2.MaxEncoderHeaderTableSize,
  63. MaxDecoderHeaderTableSize: h2.MaxDecoderHeaderTableSize,
  64. MaxReadFrameSize: h2.MaxReadFrameSize,
  65. SendPingTimeout: h2.ReadIdleTimeout,
  66. PingTimeout: h2.PingTimeout,
  67. WriteByteTimeout: h2.WriteByteTimeout,
  68. }
  69. // Unlike most config fields, where out-of-range values revert to the default,
  70. // Transport.MaxReadFrameSize clips.
  71. if conf.MaxReadFrameSize < minMaxFrameSize {
  72. conf.MaxReadFrameSize = minMaxFrameSize
  73. } else if conf.MaxReadFrameSize > maxFrameSize {
  74. conf.MaxReadFrameSize = maxFrameSize
  75. }
  76. if h2.t1 != nil {
  77. fillNetHTTPTransportConfig(&conf, h2.t1)
  78. }
  79. setConfigDefaults(&conf, false)
  80. return conf
  81. }
  82. func setDefault[T ~int | ~int32 | ~uint32 | ~int64](v *T, minval, maxval, defval T) {
  83. if *v < minval || *v > maxval {
  84. *v = defval
  85. }
  86. }
  87. func setConfigDefaults(conf *http2Config, server bool) {
  88. setDefault(&conf.MaxConcurrentStreams, 1, math.MaxUint32, defaultMaxStreams)
  89. setDefault(&conf.MaxEncoderHeaderTableSize, 1, math.MaxUint32, initialHeaderTableSize)
  90. setDefault(&conf.MaxDecoderHeaderTableSize, 1, math.MaxUint32, initialHeaderTableSize)
  91. if server {
  92. setDefault(&conf.MaxUploadBufferPerConnection, initialWindowSize, math.MaxInt32, 1<<20)
  93. } else {
  94. setDefault(&conf.MaxUploadBufferPerConnection, initialWindowSize, math.MaxInt32, transportDefaultConnFlow)
  95. }
  96. if server {
  97. setDefault(&conf.MaxUploadBufferPerStream, 1, math.MaxInt32, 1<<20)
  98. } else {
  99. setDefault(&conf.MaxUploadBufferPerStream, 1, math.MaxInt32, transportDefaultStreamFlow)
  100. }
  101. setDefault(&conf.MaxReadFrameSize, minMaxFrameSize, maxFrameSize, defaultMaxReadFrameSize)
  102. setDefault(&conf.PingTimeout, 1, math.MaxInt64, 15*time.Second)
  103. }
  104. // adjustHTTP1MaxHeaderSize converts a limit in bytes on the size of an HTTP/1 header
  105. // to an HTTP/2 MAX_HEADER_LIST_SIZE value.
  106. func adjustHTTP1MaxHeaderSize(n int64) int64 {
  107. // http2's count is in a slightly different unit and includes 32 bytes per pair.
  108. // So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
  109. const perFieldOverhead = 32 // per http2 spec
  110. const typicalHeaders = 10 // conservative
  111. return n + typicalHeaders*perFieldOverhead
  112. }