2021-10-26 22:36:04 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-04-28 21:59:31 +03:00
|
|
|
"go.unistack.org/micro/v4/metadata"
|
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2021-10-26 22:36:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func FillRequest(ctx context.Context, req interface{}, opts ...FillRequestOption) error {
|
|
|
|
var err error
|
|
|
|
options := handlerOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := 0; idx < len(options.headers)/2; idx += 2 {
|
|
|
|
k := http.CanonicalHeaderKey(options.headers[idx])
|
|
|
|
v, ok := md[k]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err = rutil.SetFieldByPath(req, v, k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cookies := strings.Split(md["Cookie"], ";")
|
|
|
|
cmd := make(map[string]string, len(cookies))
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
kv := strings.Split(cookie, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(options.cookies)/2; idx += 2 {
|
|
|
|
k := http.CanonicalHeaderKey(options.cookies[idx])
|
|
|
|
v, ok := cmd[k]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err = rutil.SetFieldByPath(req, v, k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|