Skip to content

Commit

Permalink
feat: update hertz and adapt content_encoding header (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia authored Jan 13, 2023
1 parent f85451b commit 820f9b6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions factory/http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ import (

// DefaultPingTimeout is the default ping timeout
const DefaultPingTimeout = 15 * time.Second

const HeaderEncodingLower = "content-encoding"
16 changes: 14 additions & 2 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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++ {
Expand Down

0 comments on commit 820f9b6

Please sign in to comment.