Skip to content

Commit

Permalink
Merge pull request luraproject#694 from anivanovic/antonije/fix_h2c_s…
Browse files Browse the repository at this point in the history
…upport

Fix h2c support
  • Loading branch information
dhontecillas authored Oct 26, 2023
2 parents 20a22f6 + c9838bd commit 4d07104
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ type ServiceConfig struct {
// the router layer
TLS *TLS `mapstructure:"tls"`

// UseH2C enables h2c support.
UseH2C bool `json:"use_h2c"`

// run lura in debug mode
Debug bool `mapstructure:"debug_endpoint"`
Echo bool `mapstructure:"echo_endpoint"`
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestConfig_init(t *testing.T) {
t.Error(err.Error())
}

if hash != "FK9W9HSD9jkExuf53M9SUs5bCwlwRde5kRX0cn8rGm4=" {
if hash != "NILRcvZq9y+zinGtRRfG+Zm1ORVE3gI2gjpcgDI3A/Q=" {
t.Errorf("unexpected hash: %s", hash)
}
}
Expand Down
6 changes: 6 additions & 0 deletions transport/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/core"
"github.com/luraproject/lura/v2/logging"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

// ToHTTPError translates an error into a HTTP status code
Expand Down Expand Up @@ -140,6 +142,10 @@ func NewServer(cfg config.ServiceConfig, handler http.Handler) *http.Server {
}

func NewServerWithLogger(cfg config.ServiceConfig, handler http.Handler, logger logging.Logger) *http.Server {
if cfg.UseH2C {
handler = h2c.NewHandler(handler, &http2.Server{})
}

return &http.Server{
Addr: net.JoinHostPort(cfg.Address, fmt.Sprintf("%d", cfg.Port)),
Handler: handler,
Expand Down
51 changes: 51 additions & 0 deletions transport/http/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"html"
"log"
"math/rand"
"net"
"net/http"
"os"
"testing"
"time"

"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"golang.org/x/net/http2"
)

func init() {
Expand Down Expand Up @@ -201,6 +203,43 @@ func TestRunServer_plain(t *testing.T) {
}
}

func TestRunServer_h2c(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

port := newPort()

done := make(chan error)
go func() {
done <- RunServer(
ctx,
config.ServiceConfig{
Port: port,
UseH2C: true,
},
http.HandlerFunc(dummyHandler),
)
}()

<-time.After(100 * time.Millisecond)

client := h2cClient()
resp, err := client.Get(fmt.Sprintf("http://localhost:%d", port))
if err != nil {
t.Error(err)
return
}
if resp.StatusCode != 200 {
t.Errorf("unexpected status code: %d", resp.StatusCode)
return
}
cancel()

if err = <-done; err != nil {
t.Error(err)
}
}

func TestRunServer_disabledTLS(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -412,6 +451,18 @@ func mtlsClient(certPath, keyPath string) (*http.Client, error) {
return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConf}}, nil
}

// h2cClient initializes client which executes cleartext http2 requests
func h2cClient() *http.Client {
return &http.Client{
Transport: &http2.Transport{
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
AllowHTTP: true,
},
}
}

// newPort returns random port numbers to avoid port collisions during the tests
func newPort() int {
return 16666 + rand.Intn(40000)
Expand Down

0 comments on commit 4d07104

Please sign in to comment.