Skip to content

Commit c2f2e8d

Browse files
authored
Support HTTP/2 h2c mode (cleartext) (labstack#1489)
1 parent 712b5e4 commit c2f2e8d

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

echo.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import (
5959
"github.com/labstack/gommon/log"
6060
"golang.org/x/crypto/acme"
6161
"golang.org/x/crypto/acme/autocert"
62+
"golang.org/x/net/http2"
63+
"golang.org/x/net/http2/h2c"
6264
)
6365

6466
type (
@@ -723,6 +725,34 @@ func (e *Echo) StartServer(s *http.Server) (err error) {
723725
return s.Serve(e.TLSListener)
724726
}
725727

728+
// StartH2CServer starts a custom http/2 server with h2c (HTTP/2 Cleartext).
729+
func (e *Echo) StartH2CServer(address string, h2s *http2.Server) (err error) {
730+
// Setup
731+
s := e.Server
732+
s.Addr = address
733+
e.colorer.SetOutput(e.Logger.Output())
734+
s.ErrorLog = e.StdLogger
735+
s.Handler = h2c.NewHandler(e, h2s)
736+
if e.Debug {
737+
e.Logger.SetLevel(log.DEBUG)
738+
}
739+
740+
if !e.HideBanner {
741+
e.colorer.Printf(banner, e.colorer.Red("v"+Version), e.colorer.Blue(website))
742+
}
743+
744+
if e.Listener == nil {
745+
e.Listener, err = newListener(s.Addr)
746+
if err != nil {
747+
return err
748+
}
749+
}
750+
if !e.HidePort {
751+
e.colorer.Printf("⇨ http server started on %s\n", e.colorer.Green(e.Listener.Addr()))
752+
}
753+
return s.Serve(e.Listener)
754+
}
755+
726756
// Close immediately stops the server.
727757
// It internally calls `http.Server#Close()`.
728758
func (e *Echo) Close() error {

echo_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
"golang.org/x/net/http2"
1718
)
1819

1920
type (
@@ -512,6 +513,17 @@ func TestEchoStartAutoTLS(t *testing.T) {
512513
}
513514
}
514515

516+
func TestEchoStartH2CServer(t *testing.T) {
517+
e := New()
518+
e.Debug = true
519+
h2s := &http2.Server{}
520+
521+
go func() {
522+
assert.NoError(t, e.StartH2CServer(":0", h2s))
523+
}()
524+
time.Sleep(200 * time.Millisecond)
525+
}
526+
515527
func testMethod(t *testing.T, method, path string, e *Echo) {
516528
p := reflect.ValueOf(path)
517529
h := reflect.ValueOf(func(c Context) error {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/stretchr/testify v1.4.0
1111
github.com/valyala/fasttemplate v1.1.0
1212
golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876
13-
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
13+
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
1414
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8 // indirect
1515
golang.org/x/text v0.3.2 // indirect
1616
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876 h1:sKJQZMuxjOAR/Uo2LBfU90
2929
golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
3030
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
3131
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
32-
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
33-
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
32+
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
33+
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
3434
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3535
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3636
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)