package http2
import (
"context"
"crypto/tls"
"errors"
"net"
"net/http"
"slices"
"sync"
"time"
)
type serverInternalState struct {
s1 *http .Server
initOnce sync .Once
serveConnFunc func (context .Context , net .Conn , http .Handler , bool , *http .Request , []byte )
}
func configureServer(s *http .Server , conf *Server ) error {
if s == nil {
panic ("nil *http.Server" )
}
if conf == nil {
conf = new (Server )
}
if conf .state != nil {
panic ("ConfigureServer may be called only once per Server" )
}
if h1 , h2 := s , conf ; h2 .IdleTimeout == 0 {
if h1 .IdleTimeout != 0 {
h2 .IdleTimeout = h1 .IdleTimeout
} else {
h2 .IdleTimeout = h1 .ReadTimeout
}
}
if s .TLSConfig == nil {
s .TLSConfig = new (tls .Config )
}
if !slices .Contains (s .TLSConfig .NextProtos , NextProtoTLS ) {
s .TLSConfig .NextProtos = append (s .TLSConfig .NextProtos , NextProtoTLS )
}
if !slices .Contains (s .TLSConfig .NextProtos , "http/1.1" ) {
s .TLSConfig .NextProtos = append (s .TLSConfig .NextProtos , "http/1.1" )
}
conf .state = &serverInternalState {
s1 : s ,
}
sconfig := &serverConfig {s : conf }
if err := s .Serve (sconfig ); err != nil || sconfig .serveConnFunc == nil {
panic ("http2: net/http does not support this version of x/net/http2" )
}
conf .state .serveConnFunc = sconfig .serveConnFunc
return nil
}
type serverConfig struct {
s *Server
serveConnFunc func (context .Context , net .Conn , http .Handler , bool , *http .Request , []byte )
}
func (*serverConfig ) Accept () (net .Conn , error ) {
return nil , errors .New ("unexpected call to Accept" )
}
func (*serverConfig ) Close () error {
return nil
}
func (*serverConfig ) Addr () net .Addr {
return nil
}
func (s *serverConfig ) ServeConnFunc (f func (context .Context , net .Conn , http .Handler , bool , *http .Request , []byte )) {
s .serveConnFunc = f
}
func (s *serverConfig ) HTTP2Config () http .HTTP2Config {
return http .HTTP2Config {
MaxConcurrentStreams : int (s .s .MaxConcurrentStreams ),
MaxDecoderHeaderTableSize : int (s .s .MaxDecoderHeaderTableSize ),
MaxEncoderHeaderTableSize : int (s .s .MaxEncoderHeaderTableSize ),
MaxReadFrameSize : int (s .s .MaxReadFrameSize ),
PermitProhibitedCipherSuites : s .s .PermitProhibitedCipherSuites ,
MaxReceiveBufferPerConnection : int (s .s .MaxUploadBufferPerConnection ),
MaxReceiveBufferPerStream : int (s .s .MaxUploadBufferPerStream ),
SendPingTimeout : s .s .ReadIdleTimeout ,
PingTimeout : s .s .PingTimeout ,
WriteByteTimeout : s .s .WriteByteTimeout ,
CountError : s .s .CountError ,
}
}
func (s *serverConfig ) IdleTimeout () time .Duration {
return s .s .IdleTimeout
}
type serverConn struct {}
func (s *Server ) serveConn (c net .Conn , opts *ServeConnOpts , _ func (*serverConn )) {
var serveConnFunc func (context .Context , net .Conn , http .Handler , bool , *http .Request , []byte )
switch {
case opts .BaseConfig != nil :
h1 := &http .Server {
TLSConfig : opts .BaseConfig .TLSConfig ,
ReadTimeout : opts .BaseConfig .ReadTimeout ,
ReadHeaderTimeout : opts .BaseConfig .ReadHeaderTimeout ,
WriteTimeout : opts .BaseConfig .WriteTimeout ,
IdleTimeout : opts .BaseConfig .IdleTimeout ,
MaxHeaderBytes : opts .BaseConfig .MaxHeaderBytes ,
ConnState : opts .BaseConfig .ConnState ,
ErrorLog : opts .BaseConfig .ErrorLog ,
BaseContext : opts .BaseConfig .BaseContext ,
ConnContext : opts .BaseConfig .ConnContext ,
HTTP2 : opts .BaseConfig .HTTP2 ,
}
sconfig := &serverConfig {s : s }
if err := h1 .Serve (sconfig ); err != nil || sconfig .serveConnFunc == nil {
panic ("http2: net/http does not support this version of x/net/http2" )
}
serveConnFunc = sconfig .serveConnFunc
case s .state != nil :
serveConnFunc = s .state .serveConnFunc
default :
h1 := &http .Server {}
sconfig := &serverConfig {s : s }
if err := h1 .Serve (sconfig ); err != nil || sconfig .serveConnFunc == nil {
panic ("http2: net/http does not support this version of x/net/http2" )
}
serveConnFunc = sconfig .serveConnFunc
}
ctx , cancel := serverConnBaseContext (c , opts )
defer cancel ()
serveConnFunc (ctx , c , opts .handler (), opts .SawClientPreface , opts .UpgradeRequest , opts .Settings )
}
type FrameWriteRequest struct {
}
func (wr FrameWriteRequest ) StreamID () uint32 {
return 0
}
func (wr FrameWriteRequest ) DataSize () int {
return 0
}
func (wr FrameWriteRequest ) Consume (n int32 ) (FrameWriteRequest , FrameWriteRequest , int ) {
return FrameWriteRequest {}, FrameWriteRequest {}, 0
}
func (wr FrameWriteRequest ) String () string {
return ""
}
func NewPriorityWriteScheduler (cfg *PriorityWriteSchedulerConfig ) WriteScheduler {
return unsupportedWriteScheduler {}
}
func NewRandomWriteScheduler () WriteScheduler {
return unsupportedWriteScheduler {}
}
type unsupportedWriteScheduler struct {}
func (unsupportedWriteScheduler ) OpenStream (streamID uint32 , options OpenStreamOptions ) {}
func (unsupportedWriteScheduler ) CloseStream (streamID uint32 ) {}
func (unsupportedWriteScheduler ) AdjustStream (streamID uint32 , priority PriorityParam ) {}
func (unsupportedWriteScheduler ) Push (wr FrameWriteRequest ) {}
func (unsupportedWriteScheduler ) Pop () (wr FrameWriteRequest , ok bool ) {
return FrameWriteRequest {}, false
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .