diff --git a/gateway/api_loader.go b/gateway/api_loader.go index 5b4b800aa6f..5ccdb4b795c 100644 --- a/gateway/api_loader.go +++ b/gateway/api_loader.go @@ -119,6 +119,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int, "prefix": "coprocess", }) + if strings.Contains(spec.Proxy.TargetURL, "h2c://") { spec.Proxy.TargetURL = strings.Replace(spec.Proxy.TargetURL, "h2c://", "http://", 1) } diff --git a/gateway/proxy_muxer.go b/gateway/proxy_muxer.go index 1bf92ec0216..c7a80d4b806 100644 --- a/gateway/proxy_muxer.go +++ b/gateway/proxy_muxer.go @@ -12,17 +12,17 @@ import ( "sync/atomic" "time" + "golang.org/x/net/http2/h2c" + "github.com/TykTechnologies/again" "github.com/TykTechnologies/tyk/config" "github.com/TykTechnologies/tyk/tcp" proxyproto "github.com/pires/go-proxyproto" cache "github.com/pmylund/go-cache" - "golang.org/x/net/http2" - "golang.org/x/net/http2/h2c" - "github.com/gorilla/mux" "github.com/sirupsen/logrus" + "golang.org/x/net/http2" ) // handleWrapper's only purpose is to allow router to be dynamically replaced @@ -126,6 +126,7 @@ func (m *proxyMux) router(port int, protocol string) *mux.Router { } func (m *proxyMux) setRouter(port int, protocol string, router *mux.Router) { + if port == 0 { port = config.Global().ListenPort } @@ -401,6 +402,7 @@ func (m *proxyMux) serve() { } var h http.Handler h = &handleWrapper{p.router} + if p.protocol == "h2c" { // wrapping handler in h2c. This ensures all features including tracing work // in h2c services. @@ -410,6 +412,7 @@ func (m *proxyMux) serve() { h: h2c.NewHandler(p.router, h2s), } } + addr := config.Global().ListenAddress + ":" + strconv.Itoa(p.port) p.httpServer = &http.Server{ Addr: addr, @@ -421,11 +424,8 @@ func (m *proxyMux) serve() { if config.Global().CloseConnections { p.httpServer.SetKeepAlivesEnabled(false) } - go p.httpServer.Serve(p.listener) - } - p.started = true } }