11package internal
22
33import (
4- "fmt"
54 "io"
65 "net/http"
76
@@ -13,21 +12,15 @@ import (
1312 "google.golang.org/grpc/status"
1413)
1514
16- // MetadataHeaderPrefix is the http prefix that represents custom metadata
17- // parameters to or from a gRPC call.
18- const MetadataHeaderPrefix = "Grpc-Metadata-"
15+ const (
16+ // MetadataHeaderPrefix is the http prefix that represents custom metadata
17+ // parameters to or from a gRPC call.
18+ MetadataHeaderPrefix = "Grpc-Metadata-"
1919
20- // MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to
21- // HTTP headers in a response handled by go-zero gateway
22- const MetadataTrailerPrefix = "Grpc-Trailer-"
23-
24- func defaultOutgoingHeaderMatcher (key string ) (string , bool ) {
25- return fmt .Sprintf ("%s%s" , MetadataHeaderPrefix , key ), true
26- }
27-
28- func defaultOutgoingTrailerMatcher (key string ) (string , bool ) {
29- return fmt .Sprintf ("%s%s" , MetadataTrailerPrefix , key ), true
30- }
20+ // MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to
21+ // HTTP headers in a response handled by go-zero gateway
22+ MetadataTrailerPrefix = "Grpc-Trailer-"
23+ )
3124
3225type EventHandler struct {
3326 Status * status.Status
@@ -49,10 +42,9 @@ func (h *EventHandler) OnReceiveHeaders(md metadata.MD) {
4942 w , ok := h .writer .(http.ResponseWriter )
5043 if ok {
5144 for k , vs := range md {
52- if h , ok := defaultOutgoingHeaderMatcher (k ); ok {
53- for _ , v := range vs {
54- w .Header ().Add (h , v )
55- }
45+ header := defaultOutgoingHeaderMatcher (k )
46+ for _ , v := range vs {
47+ w .Header ().Add (header , v )
5648 }
5749 }
5850 }
@@ -68,10 +60,9 @@ func (h *EventHandler) OnReceiveTrailers(status *status.Status, md metadata.MD)
6860 w , ok := h .writer .(http.ResponseWriter )
6961 if ok {
7062 for k , vs := range md {
71- if h , ok := defaultOutgoingTrailerMatcher (k ); ok {
72- for _ , v := range vs {
73- w .Header ().Add (h , v )
74- }
63+ header := defaultOutgoingTrailerMatcher (k )
64+ for _ , v := range vs {
65+ w .Header ().Add (header , v )
7566 }
7667 }
7768 }
@@ -84,3 +75,11 @@ func (h *EventHandler) OnResolveMethod(_ *desc.MethodDescriptor) {
8475
8576func (h * EventHandler ) OnSendHeaders (_ metadata.MD ) {
8677}
78+
79+ func defaultOutgoingHeaderMatcher (key string ) string {
80+ return MetadataHeaderPrefix + key
81+ }
82+
83+ func defaultOutgoingTrailerMatcher (key string ) string {
84+ return MetadataTrailerPrefix + key
85+ }
0 commit comments