1
0
Fork 0

Implement HTTP2 HPACK table size options

This commit is contained in:
GCHQDeveloper548 2025-10-03 13:24:04 +01:00 committed by GitHub
parent 5d830477b7
commit 0b7f0b4042
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 115 additions and 8 deletions

View file

@ -81,12 +81,16 @@ func (c *HTTPConfig) SetDefaults() {
// HTTP2Config is the HTTP2 configuration of an entry point.
type HTTP2Config struct {
MaxConcurrentStreams int32 `description:"Specifies the number of concurrent streams per connection that each client is allowed to initiate." json:"maxConcurrentStreams,omitempty" toml:"maxConcurrentStreams,omitempty" yaml:"maxConcurrentStreams,omitempty" export:"true"`
MaxConcurrentStreams int32 `description:"Specifies the number of concurrent streams per connection that each client is allowed to initiate." json:"maxConcurrentStreams,omitempty" toml:"maxConcurrentStreams,omitempty" yaml:"maxConcurrentStreams,omitempty" export:"true"`
MaxDecoderHeaderTableSize int32 `description:"Specifies the maximum size of the HTTP2 HPACK header table on the decoding (receiving from client) side." json:"maxDecoderHeaderTableSize,omitempty" toml:"maxDecoderHeaderTableSize,omitempty" yaml:"maxDecoderHeaderTableSize,omitempty" export:"true"`
MaxEncoderHeaderTableSize int32 `description:"Specifies the maximum size of the HTTP2 HPACK header table on the encoding (sending to client) side." json:"maxEncoderHeaderTableSize,omitempty" toml:"maxEncoderHeaderTableSize,omitempty" yaml:"maxEncoderHeaderTableSize,omitempty" export:"true"`
}
// SetDefaults sets the default values.
func (c *HTTP2Config) SetDefaults() {
c.MaxConcurrentStreams = 250 // https://cs.opensource.google/go/x/net/+/cd36cc07:http2/server.go;l=58
c.MaxConcurrentStreams = 250 // https://cs.opensource.google/go/x/net/+/cd36cc07:http2/server.go;l=58
c.MaxDecoderHeaderTableSize = 4096 // https://cs.opensource.google/go/x/net/+/0e478a2a:http2/server.go;l=105
c.MaxEncoderHeaderTableSize = 4096 // https://cs.opensource.google/go/x/net/+/0e478a2a:http2/server.go;l=111
}
// HTTP3Config is the HTTP3 configuration of an entry point.

View file

@ -74,7 +74,9 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
MaxHeaderBytes: 1048576,
},
HTTP2: &HTTP2Config{
MaxConcurrentStreams: 250,
MaxConcurrentStreams: 250,
MaxDecoderHeaderTableSize: 4096,
MaxEncoderHeaderTableSize: 4096,
},
HTTP3: nil,
UDP: &UDPConfig{
@ -120,7 +122,9 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
MaxHeaderBytes: 1048576,
},
HTTP2: &HTTP2Config{
MaxConcurrentStreams: 250,
MaxConcurrentStreams: 250,
MaxDecoderHeaderTableSize: 4096,
MaxEncoderHeaderTableSize: 4096,
},
HTTP3: nil,
UDP: &UDPConfig{
@ -177,7 +181,9 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
MaxHeaderBytes: 1048576,
},
HTTP2: &HTTP2Config{
MaxConcurrentStreams: 250,
MaxConcurrentStreams: 250,
MaxDecoderHeaderTableSize: 4096,
MaxEncoderHeaderTableSize: 4096,
},
HTTP3: nil,
UDP: &UDPConfig{
@ -238,7 +244,9 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
MaxHeaderBytes: 1048576,
},
HTTP2: &HTTP2Config{
MaxConcurrentStreams: 250,
MaxConcurrentStreams: 250,
MaxDecoderHeaderTableSize: 4096,
MaxEncoderHeaderTableSize: 4096,
},
HTTP3: nil,
UDP: &UDPConfig{

View file

@ -631,6 +631,12 @@ func newHTTPServer(ctx context.Context, ln net.Listener, configuration *static.E
if configuration.HTTP2.MaxConcurrentStreams < 0 {
return nil, errors.New("max concurrent streams value must be greater than or equal to zero")
}
if configuration.HTTP2.MaxDecoderHeaderTableSize < 0 {
return nil, errors.New("max decoder header table size value must be greater than or equal to zero")
}
if configuration.HTTP2.MaxEncoderHeaderTableSize < 0 {
return nil, errors.New("max encoder header table size value must be greater than or equal to zero")
}
httpSwitcher := middlewares.NewHandlerSwitcher(http.NotFoundHandler())
@ -688,7 +694,9 @@ func newHTTPServer(ctx context.Context, ln net.Listener, configuration *static.E
IdleTimeout: time.Duration(configuration.Transport.RespondingTimeouts.IdleTimeout),
MaxHeaderBytes: configuration.HTTP.MaxHeaderBytes,
HTTP2: &http.HTTP2Config{
MaxConcurrentStreams: int(configuration.HTTP2.MaxConcurrentStreams),
MaxConcurrentStreams: int(configuration.HTTP2.MaxConcurrentStreams),
MaxDecoderHeaderTableSize: int(configuration.HTTP2.MaxDecoderHeaderTableSize),
MaxEncoderHeaderTableSize: int(configuration.HTTP2.MaxEncoderHeaderTableSize),
},
}
if debugConnection || (configuration.Transport != nil && (configuration.Transport.KeepAliveMaxTime > 0 || configuration.Transport.KeepAliveMaxRequests > 0)) {

View file

@ -648,3 +648,34 @@ func TestPathOperations(t *testing.T) {
})
}
}
func TestHTTP2Config(t *testing.T) {
expectedMaxConcurrentStreams := 42
expectedEncoderTableSize := 128
expectedDecoderTableSize := 256
// Create a listener for the server.
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() {
_ = ln.Close()
})
// Define the server configuration.
configuration := &static.EntryPoint{}
configuration.SetDefaults()
configuration.HTTP2.MaxConcurrentStreams = int32(expectedMaxConcurrentStreams)
configuration.HTTP2.MaxEncoderHeaderTableSize = int32(expectedEncoderTableSize)
configuration.HTTP2.MaxDecoderHeaderTableSize = int32(expectedDecoderTableSize)
// Create the HTTP server using newHTTPServer.
server, err := newHTTPServer(t.Context(), ln, configuration, false, requestdecorator.New(nil))
require.NoError(t, err)
// Get the underlying HTTP Server.
httpServer := server.Server.(*http.Server)
assert.Equal(t, expectedMaxConcurrentStreams, httpServer.HTTP2.MaxConcurrentStreams)
assert.Equal(t, expectedEncoderTableSize, httpServer.HTTP2.MaxEncoderHeaderTableSize)
assert.Equal(t, expectedDecoderTableSize, httpServer.HTTP2.MaxDecoderHeaderTableSize)
}