fix api metadata extract from context (#1452)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-03-31 22:36:51 +03:00 committed by GitHub
parent d6bef84de0
commit 18061723bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -118,6 +118,17 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// create context // create context
cx := ctx.FromRequest(r) cx := ctx.FromRequest(r)
// get context from http handler wrappers
md, ok := r.Context().Value(metadata.MetadataKey{}).(metadata.Metadata)
if !ok {
md = make(metadata.Metadata)
}
// merge context with overwrite
cx = metadata.MergeContext(cx, md, true)
// set merged context to request
*r = *r.WithContext(cx)
// if stream we currently only support json // if stream we currently only support json
if isStream(r, service) { if isStream(r, service) {
@ -287,6 +298,7 @@ func requestPayload(r *http.Request) ([]byte, error) {
// allocate maximum // allocate maximum
matches := make(map[string]string, len(md)) matches := make(map[string]string, len(md))
for k, v := range md { for k, v := range md {
// filter own keys
if strings.HasPrefix(k, "x-api-field-") { if strings.HasPrefix(k, "x-api-field-") {
matches[strings.TrimPrefix(k, "x-api-field-")] = v matches[strings.TrimPrefix(k, "x-api-field-")] = v
delete(md, k) delete(md, k)
@ -294,8 +306,8 @@ func requestPayload(r *http.Request) ([]byte, error) {
} }
// restore context without fields // restore context without fields
ctx = metadata.NewContext(ctx, md) *r = *r.WithContext(metadata.NewContext(ctx, md))
*r = *r.WithContext(ctx)
req := make(map[string]interface{}, len(md)) req := make(map[string]interface{}, len(md))
for k, v := range matches { for k, v := range matches {
ps := strings.Split(k, ".") ps := strings.Split(k, ".")

View File

@ -255,7 +255,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
} }
pMatch = true pMatch = true
ctx := req.Context() ctx := req.Context()
md, ok := metadata.FromContext(ctx) md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata)
if !ok { if !ok {
md = make(metadata.Metadata) md = make(metadata.Metadata)
} }