fill metadata early

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-30 00:40:08 +03:00
parent 11020aa6e5
commit b18465083b

View File

@ -81,7 +81,15 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
ctx := metadata.NewIncomingContext(r.Context(), nil) ctx := context.WithValue(r.Context(), rspCodeKey{}, &rspCodeVal{})
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.New(len(r.Header))
}
for k, v := range r.Header {
md.Set(k, strings.Join(v, ", "))
}
ctx = metadata.NewIncomingContext(ctx, md)
defer r.Body.Close() defer r.Body.Close()
@ -136,15 +144,6 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.New(0)
}
for k, v := range r.Header {
md.Set(k, strings.Join(v, ", "))
}
// get fields from url values // get fields from url values
if len(r.URL.RawQuery) > 0 { if len(r.URL.RawQuery) > 0 {
umd, cerr := rflutil.URLMap(r.URL.RawQuery) umd, cerr := rflutil.URLMap(r.URL.RawQuery)
@ -203,13 +202,12 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name), method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
body: b, body: b,
payload: argv.Interface(), payload: argv.Interface(),
header: md,
} }
var scode int var scode int
// define the handler func // define the handler func
fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) { fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) {
fctx = context.WithValue(fctx, rspCodeKey{}, &rspCodeVal{})
fctx = metadata.NewIncomingContext(fctx, md)
returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)}) returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
scode = GetRspCode(fctx) scode = GetRspCode(fctx)