2021-01-19 04:12:31 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-03-23 00:29:27 +03:00
|
|
|
"context"
|
2021-01-19 04:12:31 +03:00
|
|
|
"fmt"
|
2021-03-23 00:29:27 +03:00
|
|
|
"io/ioutil"
|
2021-01-19 04:12:31 +03:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-03-23 00:29:27 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-01-19 04:12:31 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
|
|
|
util "github.com/unistack-org/micro/v3/util/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
templateCache = make(map[string]util.Template)
|
|
|
|
mu sync.RWMutex
|
|
|
|
)
|
|
|
|
|
2021-03-23 00:29:27 +03:00
|
|
|
func newPathRequest(path string, method string, body string, msg interface{}, tags []string) (string, interface{}, error) {
|
2021-01-19 04:12:31 +03:00
|
|
|
// parse via https://github.com/googleapis/googleapis/blob/master/google/api/http.proto definition
|
|
|
|
tpl, err := newTemplate(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:44:53 +03:00
|
|
|
if len(tpl.Fields) > 0 && msg == nil {
|
|
|
|
return "", nil, fmt.Errorf("nil message but path params requested: %v", path)
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
fieldsmap := make(map[string]string, len(tpl.Fields))
|
|
|
|
for _, v := range tpl.Fields {
|
|
|
|
fieldsmap[v] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
nmsg, err := rutil.Zero(msg)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we cant switch on message and use proto helpers, to avoid dependency to protobuf
|
|
|
|
tmsg := reflect.ValueOf(msg)
|
|
|
|
if tmsg.Kind() == reflect.Ptr {
|
|
|
|
tmsg = tmsg.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
tnmsg := reflect.ValueOf(nmsg)
|
|
|
|
if tnmsg.Kind() == reflect.Ptr {
|
|
|
|
tnmsg = tnmsg.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
values := make(map[string]string)
|
|
|
|
// copy cycle
|
|
|
|
for i := 0; i < tmsg.NumField(); i++ {
|
|
|
|
val := tmsg.Field(i)
|
|
|
|
if val.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fld := tmsg.Type().Field(i)
|
2021-03-23 00:29:27 +03:00
|
|
|
|
|
|
|
t := &tag{}
|
|
|
|
for _, tn := range tags {
|
|
|
|
ts, ok := fld.Tag.Lookup(tn)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tp := strings.Split(ts, ",")
|
|
|
|
// special
|
|
|
|
switch tn {
|
|
|
|
case "protobuf": // special
|
|
|
|
t = &tag{key: tn, name: tp[3][5:], opts: append(tp[:3], tp[4:]...)}
|
|
|
|
default:
|
|
|
|
t = &tag{key: tn, name: tp[0], opts: tp[1:]}
|
|
|
|
}
|
|
|
|
if t.name != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.name == "" {
|
|
|
|
// fallback to lowercase
|
|
|
|
t.name = strings.ToLower(fld.Name)
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:55:22 +03:00
|
|
|
if !val.IsValid() || val.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:29:27 +03:00
|
|
|
if _, ok := fieldsmap[t.name]; ok {
|
|
|
|
fieldsmap[t.name] = fmt.Sprintf("%v", val.Interface())
|
|
|
|
} else if (body == "*" || body == t.name) && method != http.MethodGet {
|
2021-01-19 04:12:31 +03:00
|
|
|
tnmsg.Field(i).Set(val)
|
2021-03-19 14:17:53 +03:00
|
|
|
} else {
|
2021-03-23 00:29:27 +03:00
|
|
|
values[t.name] = fmt.Sprintf("%v", val.Interface())
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not filled stuff
|
|
|
|
for k, v := range fieldsmap {
|
|
|
|
if v == "" {
|
2021-03-24 10:44:53 +03:00
|
|
|
return "", nil, fmt.Errorf("path param %s not filled", k)
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
for _, fld := range tpl.Pool {
|
|
|
|
_, _ = b.WriteRune('/')
|
|
|
|
if v, ok := fieldsmap[fld]; ok {
|
|
|
|
_, _ = b.WriteString(v)
|
|
|
|
} else {
|
|
|
|
_, _ = b.WriteString(fld)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:17:53 +03:00
|
|
|
idx := 0
|
|
|
|
for k, v := range values {
|
|
|
|
if idx == 0 {
|
|
|
|
_, _ = b.WriteRune('?')
|
|
|
|
} else {
|
|
|
|
_, _ = b.WriteRune('&')
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
2021-03-19 14:17:53 +03:00
|
|
|
_, _ = b.WriteString(k)
|
|
|
|
_, _ = b.WriteRune('=')
|
|
|
|
_, _ = b.WriteString(v)
|
|
|
|
idx++
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:44:48 +03:00
|
|
|
if rutil.IsZero(nmsg) {
|
|
|
|
return b.String(), nil, nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
return b.String(), nmsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTemplate(path string) (util.Template, error) {
|
|
|
|
mu.RLock()
|
|
|
|
tpl, ok := templateCache[path]
|
|
|
|
if ok {
|
|
|
|
mu.RUnlock()
|
|
|
|
return tpl, nil
|
|
|
|
}
|
|
|
|
mu.RUnlock()
|
|
|
|
|
|
|
|
rule, err := util.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
return tpl, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl = rule.Compile()
|
|
|
|
mu.Lock()
|
|
|
|
templateCache[path] = tpl
|
|
|
|
mu.Unlock()
|
|
|
|
|
|
|
|
return tpl, nil
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
|
|
|
|
func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
b, err := ioutil.ReadAll(hrsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if hrsp.StatusCode < 400 {
|
|
|
|
// unmarshal
|
|
|
|
if err := cf.Unmarshal(b, rsp); err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
|
|
|
if !ok || errmap == nil {
|
|
|
|
// user not provide map of errors
|
|
|
|
// id: req.Service() ??
|
|
|
|
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
|
|
|
err, ok = errmap["default"].(error)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cerr := cf.Unmarshal(b, err); cerr != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type tag struct {
|
|
|
|
key string
|
|
|
|
name string
|
|
|
|
opts []string
|
|
|
|
}
|