Skip to content

Commit

Permalink
Enable h2c support
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmcconnell committed Mar 7, 2024
1 parent 7db0b60 commit 80db6c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
github.com/klauspost/compress v1.17.4
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.17.0
golang.org/x/net v0.17.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/url"

"github.com/klauspost/compress/gzhttp"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

type HandlerOptions struct {
Expand All @@ -22,6 +24,7 @@ func NewHandler(options HandlerOptions) http.Handler {
handler = NewCacheHandler(options.cache, options.maxCacheableResponseBody, handler)
handler = NewSendfileHandler(options.xSendfileEnabled, handler)
handler = gzhttp.GzipHandler(handler)
handler = h2c.NewHandler(handler, &http2.Server{})

if options.maxRequestBody > 0 {
handler = http.MaxBytesHandler(handler, int64(options.maxRequestBody))
Expand Down
31 changes: 31 additions & 0 deletions internal/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package internal

import (
"bytes"
"context"
"crypto/tls"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
)

func TestHandlerGzipCompression_when_proxying(t *testing.T) {
Expand Down Expand Up @@ -106,6 +111,32 @@ func TestHandlerMaxRequestBody(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
}

func TestHandlerH2C(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer upstream.Close()

options := handlerOptions(upstream.URL)
handler := NewHandler(options)
server := httptest.NewServer(handler)
defer server.Close()

client := http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, network, addr)
},
},
}

resp, err := client.Get(server.URL)

require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "HTTP/2.0", resp.Proto)
}

// Helpers

func handlerOptions(targetUrl string) HandlerOptions {
Expand Down

0 comments on commit 80db6c2

Please sign in to comment.