diff options
-rw-r--r-- | service/http/config.go | 8 | ||||
-rw-r--r-- | service/http/h2c_test.go | 57 | ||||
-rw-r--r-- | service/http/service.go | 6 |
3 files changed, 67 insertions, 4 deletions
diff --git a/service/http/config.go b/service/http/config.go index c2b11c8d..ff15e83e 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -49,6 +49,9 @@ type HTTP2Config struct { // Enable or disable HTTP/2 extension, default enable. Enabled bool + // H2C enables HTTP/2 over TCP + H2C bool + // MaxConcurrentStreams defaults to 128. MaxConcurrentStreams uint32 } @@ -91,6 +94,11 @@ func (c *Config) EnableHTTP2() bool { return c.HTTP2.Enabled } +// EnableH2С when HTTP/2 extension must be enabled on TCP. +func (c *Config) EnableH2C() bool { + return c.HTTP2.H2C +} + // EnableFCGI is true when FastCGI server must be enabled. func (c *Config) EnableFCGI() bool { return c.FCGI.Address != "" diff --git a/service/http/h2c_test.go b/service/http/h2c_test.go new file mode 100644 index 00000000..d806e5ff --- /dev/null +++ b/service/http/h2c_test.go @@ -0,0 +1,57 @@ +package http + +import ( + "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus/hooks/test" + "github.com/spiral/roadrunner/service" + "github.com/stretchr/testify/assert" + "net/http" + "testing" + "time" +) + +func Test_Service_H2C(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ + "address": ":6029", + "http2": {"h2c":true}, + "workers":{ + "command": "php ../../tests/http/client.php echo pipes", + "relay": "pipes", + "pool": { + "numWorkers": 1 + } + } + }`})) + + s, st := c.Get(ID) + assert.NotNil(t, s) + assert.Equal(t, service.StatusOK, st) + + // should do nothing + s.(*Service).Stop() + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + req, err := http.NewRequest("PRI", "http://localhost:6029?hello=world", nil) + assert.NoError(t, err) + + req.Header.Add("Upgrade", "h2c") + req.Header.Add("Connection", "HTTP2-Settings") + req.Header.Add("HTTP2-Settings", "") + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + defer r.Body.Close() + + assert.Equal(t, "101 Switching Protocols", r.Status) + + // will fail with h2c notice +} diff --git a/service/http/service.go b/service/http/service.go index 845c4f1f..58038acb 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -100,10 +100,8 @@ func (s *Service) Serve() error { s.handler.Listen(s.throw) if s.cfg.EnableHTTP() { - var h2s *http2.Server - if s.cfg.EnableHTTP2() && !s.cfg.EnableTLS() { - h2s = &http2.Server{} - s.http = &http.Server{Addr: s.cfg.Address, Handler: h2c.NewHandler(s, h2s)} + if s.cfg.EnableH2C() { + s.http = &http.Server{Addr: s.cfg.Address, Handler: h2c.NewHandler(s, &http2.Server{})} } else { s.http = &http.Server{Addr: s.cfg.Address, Handler: s} } |