YarBor commented on code in PR #2648:
URL: https://github.com/apache/dubbo-go/pull/2648#discussion_r1564649916


##########
protocol/triple/triple_protocol/header.go:
##########
@@ -88,61 +90,109 @@ func addHeaderCanonical(h http.Header, key, value string) {
        h[key] = append(h[key], value)
 }
 
-type headerIncomingKey struct{}
-type headerOutgoingKey struct{}
+type extraDataKey struct{}
+
+const headerIncomingKey string = "headerIncomingKey"
+const headerOutgoingKey string = "headerOutgoingKey"
+
 type handlerOutgoingKey struct{}
 
 func newIncomingContext(ctx context.Context, header http.Header) 
context.Context {
-       return context.WithValue(ctx, headerIncomingKey{}, header)
+       extraData, ok := ctx.Value(extraDataKey{}).(map[string]http.Header)
+       if !ok {
+               extraData = map[string]http.Header{}
+       }
+       if header == nil {
+               header = make(http.Header)
+       }
+       extraData[headerIncomingKey] = header
+       return context.WithValue(ctx, extraDataKey{}, extraData)
 }
 
 // NewOutgoingContext sets headers entirely. If there are existing headers, 
they would be replaced.
 // It is used for passing headers to server-side.
 // It is like grpc.NewOutgoingContext.
 // Please refer to 
https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md#sending-metadata.
-func NewOutgoingContext(ctx context.Context, header http.Header) 
context.Context {
-       return context.WithValue(ctx, headerOutgoingKey{}, header)
+func NewOutgoingContext(ctx context.Context, data interface{}) 
(context.Context, error) {
+       header := make(http.Header)
+       if inputData, ok := data.(map[string]string); ok {
+               for k, v := range inputData {
+                       header.Add(k, v)
+               }
+       } else if inputData, ok := data.(map[string][]string); ok {
+               header = inputData
+       } else if inputData, ok := data.(http.Header); ok {
+               header = inputData
+       } else if inputData, ok := data.(map[string]interface{}); ok {
+               for k, v := range inputData {
+                       if val, ok := v.(string); ok {
+                               header[k] = []string{val}
+                       } else if val, ok := v.([]string); ok {
+                               header[k] = val
+                       } else {
+                               return ctx, errors.New("IncomingContext data 
must be map[string]string or map[string][]string")
+                       }
+               }
+       } else {
+               return ctx, errors.New("IncomingContext data must be 
map[string]string or map[string][]string")
+       }
+
+       extraData, ok := ctx.Value(extraDataKey{}).(map[string]http.Header)
+       if !ok {
+               extraData = map[string]http.Header{}
+       }
+       extraData[headerOutgoingKey] = header
+       return context.WithValue(ctx, extraDataKey{}, extraData), nil
 }
 
 // AppendToOutgoingContext merges kv pairs from user and existing headers.
 // It is used for passing headers to server-side.
 // It is like grpc.AppendToOutgoingContext.
 // Please refer to 
https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md#sending-metadata.
-func AppendToOutgoingContext(ctx context.Context, kv ...string) 
context.Context {
+func AppendToOutgoingContext(ctx context.Context, kv ...string) 
(context.Context, error) {

Review Comment:
   I think this needs to be discussed. Should there be constraints on the 
attachment key? When transmitting and returning within the framework, the first 
letter of the attachment key will be changed to uppercase. On the receiving 
side, we cannot restore the original key.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to