// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package http2

import (
	
	
)

// Config must be kept in sync with net/http.HTTP2Config.
type Config struct {
	MaxConcurrentStreams          int
	StrictMaxConcurrentRequests   bool
	MaxDecoderHeaderTableSize     int
	MaxEncoderHeaderTableSize     int
	MaxReadFrameSize              int
	MaxReceiveBufferPerConnection int
	MaxReceiveBufferPerStream     int
	SendPingTimeout               time.Duration
	PingTimeout                   time.Duration
	WriteByteTimeout              time.Duration
	PermitProhibitedCipherSuites  bool
	CountError                    func(errType string)
}

func ( ServerConfig) Config {
	 := Config{}
	fillNetHTTPConfig(&, .HTTP2Config())
	setConfigDefaults(&, true)
	return 
}

func ( *Transport) Config {
	 := Config{}
	if .t1 != nil {
		fillNetHTTPConfig(&, .t1.HTTP2Config())
	}
	setConfigDefaults(&, false)
	return 
}

func [ ~int | ~int32 | ~uint32 | ~int64]( *, , ,  ) {
	if * <  || * >  {
		* = 
	}
}

func ( *Config,  bool) {
	setDefault(&.MaxConcurrentStreams, 1, math.MaxInt32, defaultMaxStreams)
	setDefault(&.MaxEncoderHeaderTableSize, 1, math.MaxInt32, initialHeaderTableSize)
	setDefault(&.MaxDecoderHeaderTableSize, 1, math.MaxInt32, initialHeaderTableSize)
	if  {
		setDefault(&.MaxReceiveBufferPerConnection, initialWindowSize, math.MaxInt32, 1<<20)
	} else {
		setDefault(&.MaxReceiveBufferPerConnection, initialWindowSize, math.MaxInt32, transportDefaultConnFlow)
	}
	if  {
		setDefault(&.MaxReceiveBufferPerStream, 1, math.MaxInt32, 1<<20)
	} else {
		setDefault(&.MaxReceiveBufferPerStream, 1, math.MaxInt32, transportDefaultStreamFlow)
	}
	setDefault(&.MaxReadFrameSize, minMaxFrameSize, maxFrameSize, defaultMaxReadFrameSize)
	setDefault(&.PingTimeout, 1, math.MaxInt64, 15*time.Second)
}

// adjustHTTP1MaxHeaderSize converts a limit in bytes on the size of an HTTP/1 header
// to an HTTP/2 MAX_HEADER_LIST_SIZE value.
func ( int64) int64 {
	// http2's count is in a slightly different unit and includes 32 bytes per pair.
	// So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
	const  = 32 // per http2 spec
	const  = 10   // conservative
	return  + *
}

func ( *Config,  Config) {
	if .MaxConcurrentStreams != 0 {
		.MaxConcurrentStreams = .MaxConcurrentStreams
	}
	if .StrictMaxConcurrentRequests {
		.StrictMaxConcurrentRequests = true
	}
	if .MaxEncoderHeaderTableSize != 0 {
		.MaxEncoderHeaderTableSize = .MaxEncoderHeaderTableSize
	}
	if .MaxDecoderHeaderTableSize != 0 {
		.MaxDecoderHeaderTableSize = .MaxDecoderHeaderTableSize
	}
	if .MaxConcurrentStreams != 0 {
		.MaxConcurrentStreams = .MaxConcurrentStreams
	}
	if .MaxReadFrameSize != 0 {
		.MaxReadFrameSize = .MaxReadFrameSize
	}
	if .MaxReceiveBufferPerConnection != 0 {
		.MaxReceiveBufferPerConnection = .MaxReceiveBufferPerConnection
	}
	if .MaxReceiveBufferPerStream != 0 {
		.MaxReceiveBufferPerStream = .MaxReceiveBufferPerStream
	}
	if .SendPingTimeout != 0 {
		.SendPingTimeout = .SendPingTimeout
	}
	if .PingTimeout != 0 {
		.PingTimeout = .PingTimeout
	}
	if .WriteByteTimeout != 0 {
		.WriteByteTimeout = .WriteByteTimeout
	}
	if .PermitProhibitedCipherSuites {
		.PermitProhibitedCipherSuites = true
	}
	if .CountError != nil {
		.CountError = .CountError
	}
}