config.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. StrictMaxConcurrentRequests bool
  28. MaxDecoderHeaderTableSize uint32
  29. MaxEncoderHeaderTableSize uint32
  30. MaxReadFrameSize uint32
  31. MaxUploadBufferPerConnection int32
  32. MaxUploadBufferPerStream int32
  33. SendPingTimeout time.Duration
  34. PingTimeout time.Duration
  35. WriteByteTimeout time.Duration
  36. PermitProhibitedCipherSuites bool
  37. CountError func(errType string)
  38. }
  39. // configFromServer merges configuration settings from
  40. // net/http.Server.HTTP2Config and http2.Server.
  41. func configFromServer(h1 *http.Server, h2 *Server) http2Config {
  42. conf := http2Config{
  43. MaxConcurrentStreams: h2.MaxConcurrentStreams,
  44. MaxEncoderHeaderTableSize: h2.MaxEncoderHeaderTableSize,
  45. MaxDecoderHeaderTableSize: h2.MaxDecoderHeaderTableSize,
  46. MaxReadFrameSize: h2.MaxReadFrameSize,
  47. MaxUploadBufferPerConnection: h2.MaxUploadBufferPerConnection,
  48. MaxUploadBufferPerStream: h2.MaxUploadBufferPerStream,
  49. SendPingTimeout: h2.ReadIdleTimeout,
  50. PingTimeout: h2.PingTimeout,
  51. WriteByteTimeout: h2.WriteByteTimeout,
  52. PermitProhibitedCipherSuites: h2.PermitProhibitedCipherSuites,
  53. CountError: h2.CountError,
  54. }
  55. fillNetHTTPConfig(&conf, h1.HTTP2)
  56. setConfigDefaults(&conf, true)
  57. return conf
  58. }
  59. // configFromTransport merges configuration settings from h2 and h2.t1.HTTP2
  60. // (the net/http Transport).
  61. func configFromTransport(h2 *Transport) http2Config {
  62. conf := http2Config{
  63. StrictMaxConcurrentRequests: h2.StrictMaxConcurrentStreams,
  64. MaxEncoderHeaderTableSize: h2.MaxEncoderHeaderTableSize,
  65. MaxDecoderHeaderTableSize: h2.MaxDecoderHeaderTableSize,
  66. MaxReadFrameSize: h2.MaxReadFrameSize,
  67. SendPingTimeout: h2.ReadIdleTimeout,
  68. PingTimeout: h2.PingTimeout,
  69. WriteByteTimeout: h2.WriteByteTimeout,
  70. }
  71. // Unlike most config fields, where out-of-range values revert to the default,
  72. // Transport.MaxReadFrameSize clips.
  73. if conf.MaxReadFrameSize < minMaxFrameSize {
  74. conf.MaxReadFrameSize = minMaxFrameSize
  75. } else if conf.MaxReadFrameSize > maxFrameSize {
  76. conf.MaxReadFrameSize = maxFrameSize
  77. }
  78. if h2.t1 != nil {
  79. fillNetHTTPConfig(&conf, h2.t1.HTTP2)
  80. }
  81. setConfigDefaults(&conf, false)
  82. return conf
  83. }
  84. func setDefault[T ~int | ~int32 | ~uint32 | ~int64](v *T, minval, maxval, defval T) {
  85. if *v < minval || *v > maxval {
  86. *v = defval
  87. }
  88. }
  89. func setConfigDefaults(conf *http2Config, server bool) {
  90. setDefault(&conf.MaxConcurrentStreams, 1, math.MaxUint32, defaultMaxStreams)
  91. setDefault(&conf.MaxEncoderHeaderTableSize, 1, math.MaxUint32, initialHeaderTableSize)
  92. setDefault(&conf.MaxDecoderHeaderTableSize, 1, math.MaxUint32, initialHeaderTableSize)
  93. if server {
  94. setDefault(&conf.MaxUploadBufferPerConnection, initialWindowSize, math.MaxInt32, 1<<20)
  95. } else {
  96. setDefault(&conf.MaxUploadBufferPerConnection, initialWindowSize, math.MaxInt32, transportDefaultConnFlow)
  97. }
  98. if server {
  99. setDefault(&conf.MaxUploadBufferPerStream, 1, math.MaxInt32, 1<<20)
  100. } else {
  101. setDefault(&conf.MaxUploadBufferPerStream, 1, math.MaxInt32, transportDefaultStreamFlow)
  102. }
  103. setDefault(&conf.MaxReadFrameSize, minMaxFrameSize, maxFrameSize, defaultMaxReadFrameSize)
  104. setDefault(&conf.PingTimeout, 1, math.MaxInt64, 15*time.Second)
  105. }
  106. // adjustHTTP1MaxHeaderSize converts a limit in bytes on the size of an HTTP/1 header
  107. // to an HTTP/2 MAX_HEADER_LIST_SIZE value.
  108. func adjustHTTP1MaxHeaderSize(n int64) int64 {
  109. // http2's count is in a slightly different unit and includes 32 bytes per pair.
  110. // So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
  111. const perFieldOverhead = 32 // per http2 spec
  112. const typicalHeaders = 10 // conservative
  113. return n + typicalHeaders*perFieldOverhead
  114. }
  115. func fillNetHTTPConfig(conf *http2Config, h2 *http.HTTP2Config) {
  116. if h2 == nil {
  117. return
  118. }
  119. if h2.MaxConcurrentStreams != 0 {
  120. conf.MaxConcurrentStreams = uint32(h2.MaxConcurrentStreams)
  121. }
  122. if http2ConfigStrictMaxConcurrentRequests(h2) {
  123. conf.StrictMaxConcurrentRequests = true
  124. }
  125. if h2.MaxEncoderHeaderTableSize != 0 {
  126. conf.MaxEncoderHeaderTableSize = uint32(h2.MaxEncoderHeaderTableSize)
  127. }
  128. if h2.MaxDecoderHeaderTableSize != 0 {
  129. conf.MaxDecoderHeaderTableSize = uint32(h2.MaxDecoderHeaderTableSize)
  130. }
  131. if h2.MaxConcurrentStreams != 0 {
  132. conf.MaxConcurrentStreams = uint32(h2.MaxConcurrentStreams)
  133. }
  134. if h2.MaxReadFrameSize != 0 {
  135. conf.MaxReadFrameSize = uint32(h2.MaxReadFrameSize)
  136. }
  137. if h2.MaxReceiveBufferPerConnection != 0 {
  138. conf.MaxUploadBufferPerConnection = int32(h2.MaxReceiveBufferPerConnection)
  139. }
  140. if h2.MaxReceiveBufferPerStream != 0 {
  141. conf.MaxUploadBufferPerStream = int32(h2.MaxReceiveBufferPerStream)
  142. }
  143. if h2.SendPingTimeout != 0 {
  144. conf.SendPingTimeout = h2.SendPingTimeout
  145. }
  146. if h2.PingTimeout != 0 {
  147. conf.PingTimeout = h2.PingTimeout
  148. }
  149. if h2.WriteByteTimeout != 0 {
  150. conf.WriteByteTimeout = h2.WriteByteTimeout
  151. }
  152. if h2.PermitProhibitedCipherSuites {
  153. conf.PermitProhibitedCipherSuites = true
  154. }
  155. if h2.CountError != nil {
  156. conf.CountError = h2.CountError
  157. }
  158. }