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

🔥 Added HTTP2 support to server. #1957

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 30 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"encoding/json"

"github.com/dgrr/http2"
"github.com/gofiber/fiber/v2/internal/colorable"
"github.com/gofiber/fiber/v2/internal/isatty"
"github.com/gofiber/fiber/v2/utils"
Expand Down Expand Up @@ -338,6 +339,12 @@ type Config struct {
// Default: NetworkTCP4
Network string

// Known protocols are "ProtoV1", "ProtoV2"
// WARNING: ProtoV2 (http2) only will work with TLS.
//
// Default: ProtoV1
HttpProtocolVersion int

// If you find yourself behind some sort of proxy, like a load balancer,
// then certain header information may be sent to you using special X-Forwarded-* headers or the Forwarded header.
// For example, the Host HTTP header is usually used to return the requested host.
Expand Down Expand Up @@ -430,6 +437,12 @@ const (
DefaultCompressedFileSuffix = ".fiber.gz"
)

// Values for app configurator to specify http protocol
const (
ProtoV1 = 1
ProtoV2 = 2
)

// DefaultErrorHandler that process return errors from handlers
var DefaultErrorHandler = func(c *Ctx, err error) error {
code := StatusInternalServerError
Expand Down Expand Up @@ -515,6 +528,9 @@ func New(config ...Config) *App {
if app.config.Network == "" {
app.config.Network = NetworkTCP4
}
if app.config.HttpProtocolVersion == 0 {
app.config.HttpProtocolVersion = ProtoV1
}

app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies))
for _, ipAddress := range app.config.TrustedProxies {
Expand Down Expand Up @@ -801,6 +817,11 @@ func (app *App) Listen(addr string) error {
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}
if app.config.HttpProtocolVersion == ProtoV2 {
if !IsChild() {
fmt.Println("[Warning] ProtoV2 should be used with 'ListenTLS' or 'ListenMutualTLS'")
}
}
// Start listening
return app.server.Serve(ln)
}
Expand Down Expand Up @@ -842,6 +863,10 @@ func (app *App) ListenTLS(addr, certFile, keyFile string) error {
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}
// Configuring http protocol
if app.config.HttpProtocolVersion == ProtoV2 {
http2.ConfigureServer(app.server, http2.ServerConfig{})
}
// Start listening
return app.server.ServeTLS(ln, certFile, keyFile)
}
Expand Down Expand Up @@ -900,6 +925,11 @@ func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string)
app.printRoutesMessage()
}

// Configuring http protocol
if app.config.HttpProtocolVersion == ProtoV2 {
http2.ConfigureServer(app.server, http2.ServerConfig{})
}

// Start listening
return app.server.Serve(ln)
}
Expand Down
27 changes: 27 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,33 @@ func Test_App_Listener_TLS_Listener(t *testing.T) {
utils.AssertEqual(t, nil, app.Listener(ln))
}

func Test_App_Http_Proto_Config(t *testing.T) {
app := New(Config{
HttpProtocolVersion: ProtoV1,
})

go func() {
time.Sleep(time.Millisecond * 500)
utils.AssertEqual(t, nil, app.Shutdown())
}()

app.Get("/", func(c *Ctx) error {
return c.Status(200).Send([]byte("Hi there"))
})

if err := app.ListenTLS(":8080", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key"); err != nil {
utils.AssertEqual(t, nil, err)
}

resp, err := app.Test(httptest.NewRequest(MethodGet, "/", nil))
if err != nil {
utils.AssertEqual(t, nil, err)
}
// ProtoV2 test case yet omited, as the cliend yet not configured to support http2
// In order to check HTTP/2 - run the server with ProtoV2 and connect with any modern webbrowser
utils.AssertEqual(t, "HTTP/1.1", resp.Proto, "Protocol name")
}

// go test -v -run=^$ -bench=Benchmark_AcquireCtx -benchmem -count=4
func Benchmark_AcquireCtx(b *testing.B) {
app := New()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gofiber/fiber/v2
go 1.16

require (
github.com/dgrr/http2 v0.3.5
github.com/valyala/fasthttp v1.37.0
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrr/http2 v0.3.5 h1:R54Afxa+yX21j64nbh3+qcj8vhvfuCows0NCxk83c54=
github.com/dgrr/http2 v0.3.5/go.mod h1:ZYb0czp1g5/p7q01JWWKA6qkERz8SScP8KL62ugeqes=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/summerwind/h2spec v2.2.1+incompatible/go.mod h1:eP7IHGVDEe9cbCxRNtmGfII77lBvLgJLNfJjTaKa9sI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.34.0/go.mod h1:epZA5N+7pY6ZaEKRmstzOuYJx9HI8DI1oaCGZpdH4h0=
github.com/valyala/fasthttp v1.37.0 h1:7WHCyI7EAkQMVmrfBhWTCOaeROb1aCBiTopx63LkMbE=
github.com/valyala/fasthttp v1.37.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/fastrand v1.0.0 h1:LUKT9aKer2dVQNUi3waewTbKV+7H17kvWFNKs2ObdkI=
github.com/valyala/fastrand v1.0.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -22,3 +39,5 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=