add support for plain h2c

This commit is contained in:
Dmitry Rubtsov 2022-09-19 17:06:32 +06:00
parent 42bd0615c2
commit 95771c7aba
No known key found for this signature in database
GPG Key ID: 4B1E43D13E6D4311
1 changed files with 10 additions and 2 deletions

View File

@ -15,6 +15,8 @@ import (
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/netutil"
"github.com/NYTimes/gziphandler" "github.com/NYTimes/gziphandler"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
) )
// HTTP scheme constants. // HTTP scheme constants.
@ -167,12 +169,15 @@ func (web *Web) Start() {
printHTTPAddresses(schemeHTTP) printHTTPAddresses(schemeHTTP)
errs := make(chan error, 2) errs := make(chan error, 2)
// h2s adds support for plain h2c
h2s := &http2.Server{}
hostStr := web.conf.BindHost.String() hostStr := web.conf.BindHost.String()
// we need to have new instance, because after Shutdown() the Server is not usable // we need to have new instance, because after Shutdown() the Server is not usable
web.httpServer = &http.Server{ web.httpServer = &http.Server{
ErrorLog: log.StdLog("web: plain", log.DEBUG), ErrorLog: log.StdLog("web: plain", log.DEBUG),
Addr: netutil.JoinHostPort(hostStr, web.conf.BindPort), Addr: netutil.JoinHostPort(hostStr, web.conf.BindPort),
Handler: withMiddlewares(Context.mux, limitRequestBody), Handler: h2c.NewHandler(withMiddlewares(Context.mux, limitRequestBody), h2s),
ReadTimeout: web.conf.ReadTimeout, ReadTimeout: web.conf.ReadTimeout,
ReadHeaderTimeout: web.conf.ReadHeaderTimeout, ReadHeaderTimeout: web.conf.ReadHeaderTimeout,
WriteTimeout: web.conf.WriteTimeout, WriteTimeout: web.conf.WriteTimeout,
@ -202,10 +207,13 @@ func (web *Web) startBetaServer(hostStr string) {
return return
} }
// h2s adds support for plain h2c
h2s := &http2.Server{}
web.httpServerBeta = &http.Server{ web.httpServerBeta = &http.Server{
ErrorLog: log.StdLog("web: plain: beta", log.DEBUG), ErrorLog: log.StdLog("web: plain: beta", log.DEBUG),
Addr: netutil.JoinHostPort(hostStr, web.conf.BetaBindPort), Addr: netutil.JoinHostPort(hostStr, web.conf.BetaBindPort),
Handler: withMiddlewares(Context.mux, limitRequestBody, web.wrapIndexBeta), Handler: h2c.NewHandler(withMiddlewares(Context.mux, limitRequestBody, web.wrapIndexBeta), h2s),
ReadTimeout: web.conf.ReadTimeout, ReadTimeout: web.conf.ReadTimeout,
ReadHeaderTimeout: web.conf.ReadHeaderTimeout, ReadHeaderTimeout: web.conf.ReadHeaderTimeout,
WriteTimeout: web.conf.WriteTimeout, WriteTimeout: web.conf.WriteTimeout,