Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable h2c support #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading