From 820f9b6b7c8311a39d51689eece6cc37c0d44015 Mon Sep 17 00:00:00 2001 From: Xuran <37136584+Duslia@users.noreply.github.com> Date: Fri, 13 Jan 2023 14:53:54 +0800 Subject: [PATCH] feat: update hertz and adapt content_encoding header (#5) --- .github/workflows/pr-check.yml | 4 +--- factory/http2_test.go | 22 ++++++++++++++++++++++ go.mod | 8 ++++---- go.sum | 16 ++++++++-------- internal/consts/consts.go | 2 ++ write.go | 16 ++++++++++++++-- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 9d397d0..1a51779 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -9,9 +9,7 @@ jobs: - uses: actions/checkout@v3 - name: Check License Header - uses: apache/skywalking-eyes@main - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + uses: apache/skywalking-eyes/header@501a28d2fb4a9b962661987e50cf0219631b32ff - name: typos-action uses: crate-ci/typos@master diff --git a/factory/http2_test.go b/factory/http2_test.go index 263c266..c9e3667 100644 --- a/factory/http2_test.go +++ b/factory/http2_test.go @@ -67,3 +67,25 @@ func TestClientIP(t *testing.T) { req.SetRequestURI("https://127.0.0.1:8888") c.Do(context.Background(), req, rsp) } + +func TestContentEncoding(t *testing.T) { + h := server.New(server.WithHostPorts(":8889"), server.WithH2C(true)) + + // register http2 server factory + h.AddProtocol("h2", NewServerFactory()) + + h.POST("/", func(c context.Context, ctx *app.RequestContext) { + ctx.Response.Header.SetContentEncoding("gzip") + }) + go h.Spin() + time.Sleep(time.Second) + + c, _ := client.NewClient() + c.SetClientFactory(NewClientFactory(config.WithAllowHTTP(true))) + req, rsp := protocol.AcquireRequest(), protocol.AcquireResponse() + req.SetMethod("POST") + req.SetRequestURI("http://127.0.0.1:8889") + c.Do(context.Background(), req, rsp) + assert.DeepEqual(t, "gzip", string(rsp.Header.ContentEncoding())) + assert.DeepEqual(t, "", string(rsp.Header.Server())) +} diff --git a/go.mod b/go.mod index f963fdd..91f3384 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/hertz-contrib/http2 go 1.18 require ( - github.com/cloudwego/hertz v0.4.2 - golang.org/x/net v0.1.0 + github.com/cloudwego/hertz v0.5.0 + golang.org/x/net v0.5.0 ) require ( @@ -24,7 +24,7 @@ require ( github.com/tidwall/pretty v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect - golang.org/x/sys v0.1.0 // indirect - golang.org/x/text v0.4.0 // indirect + golang.org/x/sys v0.4.0 // indirect + golang.org/x/text v0.6.0 // indirect google.golang.org/protobuf v1.27.1 // indirect ) diff --git a/go.sum b/go.sum index 1c3f55b..d794995 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/bytedance/sonic v1.5.0 h1:XWdTi8bwPgxIML+eNV1IwNuTROK6EUrQ65ey8yd6fRQ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06 h1:1sDoSuDPWzhkdzNVxCxtIaKiAe96ESVPv8coGwc1gZ4= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= -github.com/cloudwego/hertz v0.4.2 h1:Ntfs5MdPoKeFSbyStU2drM4CizOkEfYWsB9s1Q3taPY= -github.com/cloudwego/hertz v0.4.2/go.mod h1:K1U0RlU07CDeBINfHNbafH/3j9uSgIW8otbjUys3OPY= +github.com/cloudwego/hertz v0.5.0 h1:xEGRUJIHCOdMtch3jcga7PEiViiXyvN3upfIskcJ+Bk= +github.com/cloudwego/hertz v0.5.0/go.mod h1:K1U0RlU07CDeBINfHNbafH/3j9uSgIW8otbjUys3OPY= github.com/cloudwego/netpoll v0.3.1 h1:xByoORmCLIyKZ8gS+da06WDo3j+jvmhaqS2KeKejtBk= github.com/cloudwego/netpoll v0.3.1/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -47,15 +47,15 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= -golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 47457b0..fd149ab 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -22,3 +22,5 @@ import ( // DefaultPingTimeout is the default ping timeout const DefaultPingTimeout = 15 * time.Second + +const HeaderEncodingLower = "content-encoding" diff --git a/write.go b/write.go index 5cf6394..7458f52 100644 --- a/write.go +++ b/write.go @@ -30,6 +30,7 @@ import ( "github.com/cloudwego/hertz/pkg/protocol" "github.com/cloudwego/hertz/pkg/protocol/consts" "github.com/hertz-contrib/http2/hpack" + consts2 "github.com/hertz-contrib/http2/internal/consts" ) // writeFramer is implemented by any type that is used to write frames. @@ -351,7 +352,6 @@ func (wu writeWindowUpdate) writeFrame(ctx writeContext) error { // is encoded only if k is in keys. func encodeHeaders(enc *hpack.Encoder, h *protocol.ResponseHeader, keys []string) { // did we need sort? - // if keys == nil { // sorter := sorterPool.Get().(*sorter) // // Using defer here, since the returned keys from the @@ -361,7 +361,19 @@ func encodeHeaders(enc *hpack.Encoder, h *protocol.ResponseHeader, keys []string // keys = sorter.Entries(h) // } - encKV(enc, consts.HeaderServerLower, string(h.Server())) + // Add special header that not in `writeResHeaders` + // `writeResHeaders` handles the special headers it considers, + // such as content-length, etc., and the remaining headers are uniformly + // encoded using encodeHeaders. But the special header of hertz is + // different from the special header of writeResHeader. There are + // several additional headers, so special handler here. + if len(h.Server()) > 0 { + encKV(enc, consts.HeaderServerLower, string(h.Server())) + } + if len(h.ContentEncoding()) > 0 { + encKV(enc, consts2.HeaderEncodingLower, string(h.ContentEncoding())) + } + cookies := h.GetCookies() if len(cookies) > 0 { for i := 0; i < len(cookies); i++ {