Skip to content

Commit

Permalink
Merge branch 'develop' into feat/add_disable_normalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Jul 11, 2023
2 parents 8126ae7 + 819c222 commit e26d309
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cmd/hz/generator/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,11 @@ func (pkgGen *HttpPackageGenerator) updateMiddlewareReg(router interface{}, midd
}

for _, mw := range middlewareList {
mwName := mw + "Mw"
mwNamePattern := fmt.Sprintf(" %sMw", mw)
if pkgGen.SnakeStyleMiddleware {
mwName = mw + "_mw"
mwNamePattern = fmt.Sprintf(" %s_mw", mw)
}
if bytes.Contains(file, []byte(mwName)) {
if bytes.Contains(file, []byte(mwNamePattern)) {
continue
}
middlewareSingleTpl := pkgGen.tpls[middlewareSingleTplName]
Expand Down
3 changes: 3 additions & 0 deletions cmd/hz/generator/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package generator

import (
"strings"
"text/template"

"github.com/cloudwego/hertz/cmd/hz/util"
Expand All @@ -25,6 +26,8 @@ import (
var funcMap = template.FuncMap{
"GetUniqueHandlerOutDir": getUniqueHandlerOutDir,
"ToSnakeCase": util.ToSnakeCase,
"Split": strings.Split,
"Trim": strings.Trim,
}

// getUniqueHandlerOutDir uses to get unique "api.handler_path"
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/meta/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package meta
import "runtime"

// Version hz version
const Version = "v0.6.4"
const Version = "v0.6.5"

// Mode hz run modes
type Mode int
Expand Down
33 changes: 33 additions & 0 deletions pkg/app/server/hertz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,36 @@ func TestSilentMode(t *testing.T) {
t.Fatalf("unexpected error in log: %s", b.String())
}
}

func TestHertzDisableHeaderNamesNormalizing(t *testing.T) {
h := New(WithHostPorts("localhost:9212"), WithDisableHeaderNamesNormalizing(true))
headerName := "CASE-senSITive-HEAder-NAME"
headerValue := "foobar baz"
succeed := false
h.GET("/test", func(c context.Context, ctx *app.RequestContext) {
ctx.VisitAllHeaders(func(key, value []byte) {
if string(key) == headerName && string(value) == headerValue {
succeed = true
return
}
})
if !succeed {
t.Fatalf("DisableHeaderNamesNormalizing failed")
} else {
ctx.Header(headerName, headerValue)
}
})

go h.Spin()
time.Sleep(100 * time.Millisecond)

cli, _ := c.NewClient(c.WithDisableHeaderNamesNormalizing(true))

r := protocol.NewRequest("GET", "http://localhost:9212/test", nil)
r.Header.DisableNormalizing()
r.Header.Set(headerName, headerValue)
res := protocol.AcquireResponse()
err := cli.Do(context.Background(), r, res)
assert.Nil(t, err)
assert.DeepEqual(t, headerValue, res.Header.Get(headerName))
}
3 changes: 3 additions & 0 deletions pkg/app/server/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestOptions(t *testing.T) {
WithTraceLevel(stats.LevelDisabled),
WithRegistry(nil, info),
WithAutoReloadRender(true, 5*time.Second),
WithDisableHeaderNamesNormalizing(true),
})
assert.DeepEqual(t, opt.ReadTimeout, time.Second)
assert.DeepEqual(t, opt.WriteTimeout, time.Second)
Expand Down Expand Up @@ -92,6 +93,7 @@ func TestOptions(t *testing.T) {
assert.DeepEqual(t, opt.Registry, nil)
assert.DeepEqual(t, opt.AutoReloadRender, true)
assert.DeepEqual(t, opt.AutoReloadInterval, 5*time.Second)
assert.DeepEqual(t, opt.DisableHeaderNamesNormalizing, true)
}

func TestDefaultOptions(t *testing.T) {
Expand Down Expand Up @@ -124,4 +126,5 @@ func TestDefaultOptions(t *testing.T) {
assert.Assert(t, opt.RegistryInfo == nil)
assert.DeepEqual(t, opt.AutoReloadRender, false)
assert.DeepEqual(t, opt.AutoReloadInterval, time.Duration(0))
assert.DeepEqual(t, opt.DisableHeaderNamesNormalizing, false)
}
1 change: 1 addition & 0 deletions pkg/common/config/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestDefaultOptions(t *testing.T) {
assert.DeepEqual(t, []interface{}{}, options.Tracers)
assert.DeepEqual(t, new(interface{}), options.TraceLevel)
assert.DeepEqual(t, registry.NoopRegistry, options.Registry)
assert.DeepEqual(t, false, options.DisableHeaderNamesNormalizing)
}

// TestApplyCustomOptions test apply options with custom values after init
Expand Down
15 changes: 11 additions & 4 deletions pkg/protocol/http1/ext/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,18 @@ func (rs *bodyStream) skipRest() error {
func ReleaseBodyStream(requestReader io.Reader) (err error) {
if rs, ok := requestReader.(*bodyStream); ok {
err = rs.skipRest()
rs.prefetchedBytes = nil
rs.offset = 0
rs.reader = nil
rs.trailer = nil
rs.reset()
bodyStreamPool.Put(rs)
}
return
}

func (rs *bodyStream) reset() {
rs.prefetchedBytes = nil
rs.offset = 0
rs.reader = nil
rs.trailer = nil
rs.chunkEOF = false
rs.chunkLeft = 0
rs.contentLength = 0
}
24 changes: 24 additions & 0 deletions pkg/protocol/http1/ext/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package ext

import (
"bytes"
"fmt"
"io"
"testing"
Expand Down Expand Up @@ -88,3 +89,26 @@ func TestChunkedSkipRest(t *testing.T) {
// big body
testChunkedSkipRestWithBodySize(t, 3*1024*1024)
}

func TestBodyStream_Reset(t *testing.T) {
t.Parallel()
bs := bodyStream{
prefetchedBytes: bytes.NewReader([]byte("aaa")),
reader: mock.NewZeroCopyReader("bbb"),
trailer: &protocol.Trailer{},
offset: 10,
contentLength: 20,
chunkLeft: 50,
chunkEOF: true,
}

bs.reset()

assert.Nil(t, bs.prefetchedBytes)
assert.Nil(t, bs.reader)
assert.Nil(t, bs.trailer)
assert.DeepEqual(t, 0, bs.offset)
assert.DeepEqual(t, 0, bs.contentLength)
assert.DeepEqual(t, 0, bs.chunkLeft)
assert.False(t, bs.chunkEOF)
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ package hertz
// Name and Version info of this framework, used for statistics and debug
const (
Name = "Hertz"
Version = "v0.6.5"
Version = "v0.6.6"
)

0 comments on commit e26d309

Please sign in to comment.