Repository: cloudwego/hertz
Pull request #1006 (2023-11-21 13:25:41)

optimize(hz): rm_tag has a lower priority than the annotation
#### What type of PR is this?
<!--
Add one of the following kinds:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
optimize: A new optimization
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->
optimize
#### Check the PR title.
<!--
The description of the title will be attached in Release Notes, 
so please describe it from user-oriented, what this PR does / why we need it.
Please check your PR title with the below requirements:
-->
- [ ] This PR title match the format: \<type\>(optional scope): \<description\>
- [ ] The description of this PR title is user-oriented and clear enough for others to understand.
- [ ] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)


#### (Optional) Translate the PR title into Chinese.
让 rm_tag 的优先级低于 IDL 中的 api.XXX 注解

#### (Optional) More detailed description for this PR(en: English/zh: Chinese).
<!--
Provide more detailed info for review(e.g., it's recommended to provide perf data if this is a perf type PR).
-->
en: rm_tag has a lower priority than the annotation
zh(optional): 让 rm_tag 的优先级低于 IDL 中的 api.XXX 注解

#### (Optional) Which issue(s) this PR fixes:
<!--
Automatically closes linked issue when PR is merged.
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

#### (Optional) The PR that updates user documentation:
<!--
If the current PR requires user awareness at the usage level, please submit a PR to update user docs. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)
-->

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/cloudwego/hertz/pull/1006?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`171630c`)](https://app.codecov.io/gh/cloudwego/hertz/commit/171630c2490fa1f1dffa4ed11020ff7fd09ce8de?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) 81.81% compared to head [(`edbee56`)](https://app.codecov.io/gh/cloudwego/hertz/pull/1006?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) 81.74%.
> Report is 6 commits behind head on develop.

> :exclamation: Current head edbee56 differs from pull request most recent head 1367584. Consider uploading reports for the commit 1367584 to get more accurate results


<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop    #1006      +/-   ##
===========================================
- Coverage    81.81%   81.74%   -0.08%     
===========================================
  Files           98       98              
  Lines         9962     9962              
===========================================
- Hits          8150     8143       -7     
- Misses        1319     1326       +7     
  Partials       493      493              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/cloudwego/hertz/pull/1006?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).

welkeyever: ”rm_tag 的优先级低于 IDL 中的 api.XXX 注解”
better to:
“让 rm_tag 的优先级低于 IDL 中的 api.XXX 注解”？

Patch:
diff --git a/cmd/hz/app/app.go b/cmd/hz/app/app.go
index a6c44c0..729b2f2 100644
--- a/cmd/hz/app/app.go
+++ b/cmd/hz/app/app.go
@@ -184,7 +184,7 @@ func Init() *cli.App {
 	unsetOmitemptyFlag := cli.BoolFlag{Name: "unset_omitempty", Usage: "Remove 'omitempty' tag for generated struct.", Destination: &globalArgs.UnsetOmitempty}
 	protoCamelJSONTag := cli.BoolFlag{Name: "pb_camel_json_tag", Usage: "Convert Name style for json tag to camel(Only works protobuf).", Destination: &globalArgs.ProtobufCamelJSONTag}
 	snakeNameFlag := cli.BoolFlag{Name: "snake_tag", Usage: "Use snake_case style naming for tags. (Only works for 'form', 'query', 'json')", Destination: &globalArgs.SnakeName}
-	rmTagFlag := cli.StringSliceFlag{Name: "rm_tag", Usage: "Remove the specified tag"}
+	rmTagFlag := cli.StringSliceFlag{Name: "rm_tag", Usage: "Remove the default tag(json/query/form). If the annotation tag is set explicitly, it will not be removed."}
 	customLayout := cli.StringFlag{Name: "customize_layout", Usage: "Specify the path for layout template.", Destination: &globalArgs.CustomizeLayout}
 	customLayoutData := cli.StringFlag{Name: "customize_layout_data_path", Usage: "Specify the path for layout template render data.", Destination: &globalArgs.CustomizeLayoutData}
 	customPackage := cli.StringFlag{Name: "customize_package", Usage: "Specify the path for package template.", Destination: &globalArgs.CustomizePackage}
diff --git a/cmd/hz/generator/model/model.go b/cmd/hz/generator/model/model.go
index 2bb786b..f402804 100644
--- a/cmd/hz/generator/model/model.go
+++ b/cmd/hz/generator/model/model.go
@@ -377,8 +377,9 @@ type Choice struct {
 type Tags []Tag
 
 type Tag struct {
-	Key   string
-	Value string
+	Key       string
+	Value     string
+	IsDefault bool // default tag
 }
 
 func (ts Tags) String() string {
diff --git a/cmd/hz/generator/package_tpl.go b/cmd/hz/generator/package_tpl.go
index b6f6055..b46aa0e 100644
--- a/cmd/hz/generator/package_tpl.go
+++ b/cmd/hz/generator/package_tpl.go
@@ -324,6 +324,7 @@ type Option struct {
 
 type Options struct {
 	hostUrl               string
+	enumAsInt             bool
 	doer                  client.Doer
 	header                http.Header
 	requestBodyBind       bindRequestBodyFunc
@@ -375,6 +376,13 @@ func WithResponseResultDecider(decider ResponseResultDecider) Option {
 	}}
 }
 
+// WithQueryEnumAsInt is used to set enum as int for query parameters
+func WithQueryEnumAsInt(enable bool) Option {
+	return Option{func(op *Options) {
+		op.enumAsInt = enable
+	}}
+}
+
 func withHostUrl(HostUrl string) Option {
 	return Option{func(op *Options) {
 		op.hostUrl = HostUrl
@@ -384,6 +392,7 @@ func withHostUrl(HostUrl string) Option {
 // underlying client
 type cli struct {
 	hostUrl               string
+	enumAsInt             bool
 	doer                  client.Doer
 	header                http.Header
 	bindRequestBody       bindRequestBodyFunc
@@ -419,6 +428,7 @@ func newClient(opts *Options) (*cli, error) {
 
 	c := &cli{
 		hostUrl:               opts.hostUrl,
+		enumAsInt:             opts.enumAsInt,
 		doer:                  opts.doer,
 		header:                opts.header,
 		bindRequestBody:       opts.requestBodyBind,
@@ -573,6 +583,11 @@ func (r *request) setHeader(header, value string) *request {
 	return r
 }
 
+func (r *request) addHeader(header, value string) *request {
+	r.header.Add(header, value)
+	return r
+}
+
 func (r *request) setQueryParam(param string, value interface{}) *request {
 	v := reflect.ValueOf(value)
 	switch v.Kind() {
@@ -580,6 +595,12 @@ func (r *request) setQueryParam(param string, value interface{}) *request {
 		for index := 0; index < v.Len(); index++ {
 			r.queryParam.Add(param, fmt.Sprint(v.Index(index).Interface()))
 		}
+	case reflect.Int32, reflect.Int64:
+		if r.client.enumAsInt {
+			r.queryParam.Add(param, fmt.Sprintf("%d", v.Interface()))
+		} else {
+			r.queryParam.Add(param, fmt.Sprint(v))
+		}
 	default:
 		r.queryParam.Set(param, fmt.Sprint(v))
 	}
@@ -911,7 +932,7 @@ func (s *{{$.ServiceName}}Client) {{$MethodInfo.Name}}(context context.Context,
 		setPathParams(map[string]string{
 			{{$MethodInfo.PathParamsCode}}
 		}).
-		setHeaders(map[string]string{
+		addHeaders(map[string]string{
 			{{$MethodInfo.HeaderParamsCode}}
 		}).
 		setFormParams(map[string]string{
diff --git a/cmd/hz/protobuf/plugin.go b/cmd/hz/protobuf/plugin.go
index e258038..2603340 100644
--- a/cmd/hz/protobuf/plugin.go
+++ b/cmd/hz/protobuf/plugin.go
@@ -509,7 +509,7 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie
 		}...)
 	}
 
-	err := injectTagsToStructTags(field.Desc, &tags, true)
+	err := injectTagsToStructTags(field.Desc, &tags, true, rmTags)
 	if err != nil {
 		return err
 	}
@@ -518,16 +518,6 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie
 		tags = append(tags, gotrackTags...)
 	}
 
-	if len(rmTags) > 0 {
-		tmp := structTags{}
-		for _, tag := range tags {
-			if !rmTags.Exist(tag[0]) {
-				tmp = append(tmp, tag)
-			}
-		}
-		tags = tmp
-	}
-
 	name := field.GoName
 	if field.Desc.IsWeak() {
 		name = WeakFieldPrefix_goname + name
diff --git a/cmd/hz/protobuf/tags.go b/cmd/hz/protobuf/tags.go
index 1abf231..26c6cc7 100644
--- a/cmd/hz/protobuf/tags.go
+++ b/cmd/hz/protobuf/tags.go
@@ -340,14 +340,18 @@ func defaultBindingStructTags(f protoreflect.FieldDescriptor) []model.Tag {
 		val := getStructJsonValue(f, v.(string))
 		out[0] = tag("json", val)
 	} else {
-		out[0] = reflectJsonTag(f)
+		t := reflectJsonTag(f)
+		t.IsDefault = true
+		out[0] = t
 	}
 	if v := checkFirstOption(api.E_Query, opts); v != nil {
 		val := checkStructRequire(f, v.(string))
 		out[1] = tag(BindingTags[api.E_Query], val)
 	} else {
 		val := checkStructRequire(f, checkSnakeName(string(f.Name())))
-		out[1] = tag(BindingTags[api.E_Query], val)
+		t := tag(BindingTags[api.E_Query], val)
+		t.IsDefault = true
+		out[1] = t
 	}
 	if v := checkFirstOption(api.E_Form, opts); v != nil {
 		val := checkStructRequire(f, v.(string))
@@ -355,16 +359,20 @@ func defaultBindingStructTags(f protoreflect.FieldDescriptor) []model.Tag {
 	} else {
 		if v := checkFirstOption(api.E_FormCompatible, opts); v != nil { // compatible form_compatible
 			val := checkStructRequire(f, v.(string))
-			out[2] = tag(BindingTags[api.E_Form], val)
+			t := tag(BindingTags[api.E_Form], val)
+			t.IsDefault = true
+			out[2] = t
 		} else {
 			val := checkStructRequire(f, checkSnakeName(string(f.Name())))
-			out[2] = tag(BindingTags[api.E_Form], val)
+			t := tag(BindingTags[api.E_Form], val)
+			t.IsDefault = true
+			out[2] = t
 		}
 	}
 	return out
 }
 
-func injectTagsToStructTags(f protoreflect.FieldDescriptor, out *structTags, needDefault bool) error {
+func injectTagsToStructTags(f protoreflect.FieldDescriptor, out *structTags, needDefault bool, rmTags RemoveTags) error {
 	as := f.Options()
 	// binding tags
 	tags := model.Tags(make([]model.Tag, 0, 6))
@@ -424,6 +432,11 @@ func injectTagsToStructTags(f protoreflect.FieldDescriptor, out *structTags, nee
 			disableTag = true
 		}
 	}
+	for _, t := range tags {
+		if t.IsDefault && rmTags.Exist(t.Key) {
+			tags.Remove(t.Key)
+		}
+	}
 	// protobuf tag as first
 	sort.Sort(tags[1:])
 	for _, t := range tags {
diff --git a/cmd/hz/thrift/plugin.go b/cmd/hz/thrift/plugin.go
index 8b840e7..c315c9d 100644
--- a/cmd/hz/thrift/plugin.go
+++ b/cmd/hz/thrift/plugin.go
@@ -418,7 +418,11 @@ func getTagString(f *parser.Field, rmTags []string) (string, error) {
 	}
 
 	for _, rmTag := range rmTags {
-		field.Tags.Remove(rmTag)
+		for _, t := range field.Tags {
+			if t.IsDefault && strings.EqualFold(t.Key, rmTag) {
+				field.Tags.Remove(t.Key)
+			}
+		}
 	}
 
 	var tagString string
diff --git a/cmd/hz/thrift/tags.go b/cmd/hz/thrift/tags.go
index 96c414d..115d79b 100644
--- a/cmd/hz/thrift/tags.go
+++ b/cmd/hz/thrift/tags.go
@@ -205,21 +205,27 @@ func defaultBindingTags(f *parser.Field) []model.Tag {
 		val := getJsonValue(f, v[0])
 		out[0] = tag("json", val)
 	} else {
-		out[0] = jsonTag(f)
+		t := jsonTag(f)
+		t.IsDefault = true
+		out[0] = t
 	}
 	if v := getAnnotation(f.Annotations, AnnotationQuery); len(v) > 0 {
 		val := checkRequire(f, v[0])
 		out[1] = tag(BindingTags[AnnotationQuery], val)
 	} else {
 		val := checkRequire(f, checkSnakeName(f.Name))
-		out[1] = tag(BindingTags[AnnotationQuery], val)
+		t := tag(BindingTags[AnnotationQuery], val)
+		t.IsDefault = true
+		out[1] = t
 	}
 	if v := getAnnotation(f.Annotations, AnnotationForm); len(v) > 0 {
 		val := checkRequire(f, v[0])
 		out[2] = tag(BindingTags[AnnotationForm], val)
 	} else {
 		val := checkRequire(f, checkSnakeName(f.Name))
-		out[2] = tag(BindingTags[AnnotationForm], val)
+		t := tag(BindingTags[AnnotationForm], val)
+		t.IsDefault = true
+		out[2] = t
 	}
 	return out
 }
diff --git a/go.mod b/go.mod
index 3619b83..509c881 100644
--- a/go.mod
+++ b/go.mod
@@ -10,6 +10,7 @@ require (
 	github.com/cloudwego/netpoll v0.5.0
 	github.com/fsnotify/fsnotify v1.5.4
 	github.com/tidwall/gjson v1.14.4
+	golang.org/x/net v0.0.0-20190311183353-d8887717615a
 	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
 	golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
 	google.golang.org/protobuf v1.27.1
diff --git a/go.sum b/go.sum
index 3cf3c2f..26da89b 100644
--- a/go.sum
+++ b/go.sum
@@ -64,6 +64,7 @@ golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5P
 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/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
 golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -71,6 +72,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
 golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
 golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
diff --git a/internal/bytesconv/bytesconv_table.go b/internal/bytesconv/bytesconv_table.go
index 5668f2e..c501d92 100644
--- a/internal/bytesconv/bytesconv_table.go
+++ b/internal/bytesconv/bytesconv_table.go
@@ -51,4 +51,5 @@ const (
 	QuotedArgShouldEscapeTable  = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
 	QuotedPathShouldEscapeTable = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x01\x00\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
 	ValidCookieValueTable       = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+	ValidHeaderFieldValueTable  = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
 )
diff --git a/internal/bytesconv/bytesconv_table_gen.go b/internal/bytesconv/bytesconv_table_gen.go
index 11fdfd9..2e2722d 100644
--- a/internal/bytesconv/bytesconv_table_gen.go
+++ b/internal/bytesconv/bytesconv_table_gen.go
@@ -159,14 +159,36 @@ func main() {
 		return a
 	}()
 
+	validHeaderFieldValueTable := func() [256]byte {
+		// The implementation here is equal to httpguts.ValidHeaderFieldValue
+		var a [256]byte
+		for i := 0; i < 256; i++ {
+			a[i] = 1
+		}
+
+		for i := 0; i < ' '; i++ {
+			a[i] = 0
+		}
+
+		// del CTL
+		a[0x7f] = 0
+		// tab
+		a['\t'] = 1
+
+		return a
+	}()
+
 	w := new(bytes.Buffer)
 	w.WriteString(pre)
-	fmt.Fprintf(w, "const Hex2intTable = %q\n", hex2intTable)
-	fmt.Fprintf(w, "const ToLowerTable = %q\n", toLowerTable)
-	fmt.Fprintf(w, "const ToUpperTable = %q\n", toUpperTable)
-	fmt.Fprintf(w, "const QuotedArgShouldEscapeTable = %q\n", quotedArgShouldEscapeTable)
-	fmt.Fprintf(w, "const QuotedPathShouldEscapeTable = %q\n", quotedPathShouldEscapeTable)
-	fmt.Fprintf(w, "const ValidCookieValueTable = %q\n", validCookieValueTable)
+	fmt.Fprintf(w, "const (\n")
+	fmt.Fprintf(w, "\tHex2intTable = %q\n", hex2intTable)
+	fmt.Fprintf(w, "\tToLowerTable = %q\n", toLowerTable)
+	fmt.Fprintf(w, "\tToUpperTable = %q\n", toUpperTable)
+	fmt.Fprintf(w, "\tQuotedArgShouldEscapeTable = %q\n", quotedArgShouldEscapeTable)
+	fmt.Fprintf(w, "\tQuotedPathShouldEscapeTable = %q\n", quotedPathShouldEscapeTable)
+	fmt.Fprintf(w, "\tValidCookieValueTable = %q\n", validCookieValueTable)
+	fmt.Fprintf(w, "\tValidHeaderFieldValueTable = %q\n", validHeaderFieldValueTable)
+	fmt.Fprintf(w, ")\n")
 
 	if err := ioutil.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil {
 		log.Fatal(err)
diff --git a/pkg/protocol/http1/req/header.go b/pkg/protocol/http1/req/header.go
index 4d571a3..442e344 100644
--- a/pkg/protocol/http1/req/header.go
+++ b/pkg/protocol/http1/req/header.go
@@ -47,6 +47,7 @@ import (
 	"fmt"
 	"io"
 
+	"github.com/cloudwego/hertz/internal/bytesconv"
 	"github.com/cloudwego/hertz/internal/bytestr"
 	errs "github.com/cloudwego/hertz/pkg/common/errors"
 	"github.com/cloudwego/hertz/pkg/common/utils"
@@ -165,6 +166,16 @@ func parseFirstLine(h *protocol.RequestHeader, buf []byte) (int, error) {
 	return len(buf) - len(bNext), nil
 }
 
+// validHeaderFieldValue is equal to httpguts.ValidHeaderFieldValue（shares the same context）
+func validHeaderFieldValue(val []byte) bool {
+	for _, v := range val {
+		if bytesconv.ValidHeaderFieldValueTable[v] == 0 {
+			return false
+		}
+	}
+	return true
+}
+
 func parseHeaders(h *protocol.RequestHeader, buf []byte) (int, error) {
 	h.InitContentLengthWithValue(-2)
 
@@ -178,7 +189,13 @@ func parseHeaders(h *protocol.RequestHeader, buf []byte) (int, error) {
 			// See RFC 7230, Section 3.2.4.
 			if bytes.IndexByte(s.Key, ' ') != -1 || bytes.IndexByte(s.Key, '\t') != -1 {
 				err = fmt.Errorf("invalid header key %q", s.Key)
-				continue
+				return 0, err
+			}
+
+			// Check the invalid chars in header value
+			if !validHeaderFieldValue(s.Value) {
+				err = fmt.Errorf("invalid header value %q", s.Value)
+				return 0, err
 			}
 
 			switch s.Key[0] | 0x20 {


Test patch:
diff --git a/cmd/hz/protobuf/tag_test.go b/cmd/hz/protobuf/tag_test.go
index 7e6528b..2e7a9ca 100644
--- a/cmd/hz/protobuf/tag_test.go
+++ b/cmd/hz/protobuf/tag_test.go
@@ -126,7 +126,7 @@ func TestTagGenerate(t *testing.T) {
 					tags := structTags{
 						{"protobuf", fieldProtobufTagValue(field)},
 					}
-					err = injectTagsToStructTags(field.Desc, &tags, true)
+					err = injectTagsToStructTags(field.Desc, &tags, true, nil)
 					if err != nil {
 						t.Fatal(err)
 					}
diff --git a/internal/bytesconv/bytesconv_test.go b/internal/bytesconv/bytesconv_test.go
index cf7a9a4..4218a82 100644
--- a/internal/bytesconv/bytesconv_test.go
+++ b/internal/bytesconv/bytesconv_test.go
@@ -25,6 +25,7 @@ import (
 	"github.com/cloudwego/hertz/pkg/common/test/assert"
 	"github.com/cloudwego/hertz/pkg/common/test/mock"
 	"github.com/cloudwego/hertz/pkg/network"
+	"golang.org/x/net/http/httpguts"
 )
 
 func TestAppendDate(t *testing.T) {
@@ -183,3 +184,22 @@ func TestParseHTTPDate(t *testing.T) {
 		assert.DeepEqual(t, t1, t2)
 	}
 }
+
+func TestValidHeaderFieldValueTable(t *testing.T) {
+	t.Parallel()
+
+	// Test all characters
+	allBytes := make([]byte, 0)
+	for i := 0; i < 256; i++ {
+		allBytes = append(allBytes, byte(i))
+	}
+	for _, s := range allBytes {
+		ss := []byte{s}
+		expectedS := httpguts.ValidHeaderFieldValue(string(ss))
+		res := func() bool {
+			return ValidHeaderFieldValueTable[s] != 0
+		}()
+
+		assert.DeepEqual(t, expectedS, res)
+	}
+}
diff --git a/internal/bytesconv/bytesconv_timing_test.go b/internal/bytesconv/bytesconv_timing_test.go
new file mode 100644
index 0000000..6fdd28b
--- /dev/null
+++ b/internal/bytesconv/bytesconv_timing_test.go
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2024 CloudWeGo Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package bytesconv
+
+import (
+	"testing"
+
+	"golang.org/x/net/http/httpguts"
+)
+
+func BenchmarkValidHeaderFiledValueTable(b *testing.B) {
+	// Test all characters
+	allBytes := make([]string, 0)
+	for i := 0; i < 256; i++ {
+		allBytes = append(allBytes, string([]byte{byte(i)}))
+	}
+
+	for i := 0; i < b.N; i++ {
+		for _, s := range allBytes {
+			_ = httpguts.ValidHeaderFieldValue(s)
+		}
+	}
+}
+
+func BenchmarkValidHeaderFiledValueTableHertz(b *testing.B) {
+	// Test all characters
+	allBytes := make([]byte, 0)
+	for i := 0; i < 256; i++ {
+		allBytes = append(allBytes, byte(i))
+	}
+
+	for i := 0; i < b.N; i++ {
+		for _, s := range allBytes {
+			_ = func() bool {
+				return ValidHeaderFieldValueTable[s] != 0
+			}()
+		}
+	}
+}