// 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 (
	
	
	
)

// http2Config is a package-internal version of net/http.HTTP2Config.
//
// http.HTTP2Config was added in Go 1.24.
// When running with a version of net/http that includes HTTP2Config,
// we merge the configuration with the fields in Transport or Server
// to produce an http2Config.
//
// Zero valued fields in http2Config are interpreted as in the
// net/http.HTTPConfig documentation.
//
// Precedence order for reconciling configurations is:
//
//   - Use the net/http.{Server,Transport}.HTTP2Config value, when non-zero.
//   - Otherwise use the http2.{Server.Transport} value.
//   - If the resulting value is zero or out of range, use a default.
type http2Config struct {
	MaxConcurrentStreams         uint32
	MaxDecoderHeaderTableSize    uint32
	MaxEncoderHeaderTableSize    uint32
	MaxReadFrameSize             uint32
	MaxUploadBufferPerConnection int32
	MaxUploadBufferPerStream     int32
	SendPingTimeout              time.Duration
	PingTimeout                  time.Duration
	WriteByteTimeout             time.Duration
	PermitProhibitedCipherSuites bool
	CountError                   func(errType string)
}

// configFromServer merges configuration settings from
// net/http.Server.HTTP2Config and http2.Server.
func ( *http.Server,  *Server) http2Config {
	 := http2Config{
		MaxConcurrentStreams:         .MaxConcurrentStreams,
		MaxEncoderHeaderTableSize:    .MaxEncoderHeaderTableSize,
		MaxDecoderHeaderTableSize:    .MaxDecoderHeaderTableSize,
		MaxReadFrameSize:             .MaxReadFrameSize,
		MaxUploadBufferPerConnection: .MaxUploadBufferPerConnection,
		MaxUploadBufferPerStream:     .MaxUploadBufferPerStream,
		SendPingTimeout:              .ReadIdleTimeout,
		PingTimeout:                  .PingTimeout,
		WriteByteTimeout:             .WriteByteTimeout,
		PermitProhibitedCipherSuites: .PermitProhibitedCipherSuites,
		CountError:                   .CountError,
	}
	fillNetHTTPServerConfig(&, )
	setConfigDefaults(&, true)
	return 
}

// configFromTransport merges configuration settings from h2 and h2.t1.HTTP2
// (the net/http Transport).
func ( *Transport) http2Config {
	 := http2Config{
		MaxEncoderHeaderTableSize: .MaxEncoderHeaderTableSize,
		MaxDecoderHeaderTableSize: .MaxDecoderHeaderTableSize,
		MaxReadFrameSize:          .MaxReadFrameSize,
		SendPingTimeout:           .ReadIdleTimeout,
		PingTimeout:               .PingTimeout,
		WriteByteTimeout:          .WriteByteTimeout,
	}

	// Unlike most config fields, where out-of-range values revert to the default,
	// Transport.MaxReadFrameSize clips.
	if .MaxReadFrameSize < minMaxFrameSize {
		.MaxReadFrameSize = minMaxFrameSize
	} else if .MaxReadFrameSize > maxFrameSize {
		.MaxReadFrameSize = maxFrameSize
	}

	if .t1 != nil {
		fillNetHTTPTransportConfig(&, .t1)
	}
	setConfigDefaults(&, false)
	return 
}

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

func ( *http2Config,  bool) {
	setDefault(&.MaxConcurrentStreams, 1, math.MaxUint32, defaultMaxStreams)
	setDefault(&.MaxEncoderHeaderTableSize, 1, math.MaxUint32, initialHeaderTableSize)
	setDefault(&.MaxDecoderHeaderTableSize, 1, math.MaxUint32, initialHeaderTableSize)
	if  {
		setDefault(&.MaxUploadBufferPerConnection, initialWindowSize, math.MaxInt32, 1<<20)
	} else {
		setDefault(&.MaxUploadBufferPerConnection, initialWindowSize, math.MaxInt32, transportDefaultConnFlow)
	}
	if  {
		setDefault(&.MaxUploadBufferPerStream, 1, math.MaxInt32, 1<<20)
	} else {
		setDefault(&.MaxUploadBufferPerStream, 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  + *
}