MCPcopy
hub / github.com/grpc-ecosystem/grpc-gateway / annotateContext

Function annotateContext

runtime/context.go:136–212  ·  view source on GitHub ↗
(ctx context.Context, mux *ServeMux, req *http.Request, rpcMethodName string, options ...AnnotateContextOption)

Source from the content-addressed store, hash-verified

134}
135
136func annotateContext(ctx context.Context, mux *ServeMux, req *http.Request, rpcMethodName string, options ...AnnotateContextOption) (context.Context, metadata.MD, error) {
137 ctx = withRPCMethod(ctx, rpcMethodName)
138 for _, o := range options {
139 ctx = o(ctx)
140 }
141 timeout := DefaultContextTimeout
142 if tm := req.Header.Get(metadataGrpcTimeout); tm != "" {
143 var err error
144 timeout, err = timeoutDecode(tm)
145 if err != nil {
146 return nil, nil, status.Errorf(codes.InvalidArgument, "invalid grpc-timeout: %s", tm)
147 }
148 }
149 var pairs []string
150 for key, vals := range req.Header {
151 key = textproto.CanonicalMIMEHeaderKey(key)
152 switch key {
153 case xForwardedFor, xForwardedHost:
154 // Handled separately below
155 continue
156 }
157
158 for _, val := range vals {
159 // For backwards-compatibility, pass through 'authorization' header with no prefix.
160 if key == "Authorization" {
161 pairs = append(pairs, "authorization", val)
162 }
163 if h, ok := mux.incomingHeaderMatcher(key); ok {
164 if !isValidGRPCMetadataKey(h) {
165 grpclog.Errorf("HTTP header name %q is not valid as gRPC metadata key; skipping", h)
166 continue
167 }
168 // Handles "-bin" metadata in grpc, since grpc will do another base64
169 // encode before sending to server, we need to decode it first.
170 if strings.HasSuffix(key, metadataHeaderBinarySuffix) {
171 b, err := decodeBinHeader(val)
172 if err != nil {
173 return nil, nil, status.Errorf(codes.InvalidArgument, "invalid binary header %s: %s", key, err)
174 }
175
176 val = string(b)
177 } else if !isValidGRPCMetadataTextValue(val) {
178 grpclog.Errorf("Value of HTTP header %q contains non-ASCII value (not valid as gRPC metadata): skipping", h)
179 continue
180 }
181 pairs = append(pairs, h, val)
182 }
183 }
184 }
185 if host := req.Header.Get(xForwardedHost); host != "" {
186 pairs = append(pairs, strings.ToLower(xForwardedHost), host)
187 } else if req.Host != "" {
188 pairs = append(pairs, strings.ToLower(xForwardedHost), req.Host)
189 }
190
191 xff := req.Header.Values(xForwardedFor)
192 if addr := req.RemoteAddr; addr != "" {
193 if remoteIP, _, err := net.SplitHostPort(addr); err == nil {

Callers 2

AnnotateContextFunction · 0.85
AnnotateIncomingContextFunction · 0.85

Calls 5

withRPCMethodFunction · 0.85
timeoutDecodeFunction · 0.85
isValidGRPCMetadataKeyFunction · 0.85
decodeBinHeaderFunction · 0.85

Tested by

no test coverage detected