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-04-18 15:45:42 +03:00
|
|
|
"io"
|
2021-01-19 04:12:31 +03:00
|
|
|
"net/http"
|
2021-04-19 02:10:24 +03:00
|
|
|
"net/url"
|
2021-01-19 04:12:31 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
2023-07-11 00:47:36 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2021-10-25 19:59:37 +03:00
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2021-01-19 04:12:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-07-30 23:02:50 +03:00
|
|
|
templateCache = make(map[string][]string)
|
2021-01-19 04:12:31 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
)
|
|
|
|
|
2021-07-05 16:02:35 +03:00
|
|
|
// Error struct holds error
|
|
|
|
type Error struct {
|
|
|
|
err interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error func for error interface
|
|
|
|
func (err *Error) Error() string {
|
|
|
|
return fmt.Sprintf("%v", err.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetError(err error) interface{} {
|
|
|
|
if rerr, ok := err.(*Error); ok {
|
|
|
|
return rerr.err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
func newPathRequest(path string, method string, body string, msg interface{}, tags []string, parameters map[string]map[string]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-07-30 23:02:50 +03:00
|
|
|
if len(tpl) > 0 && msg == nil {
|
2021-03-24 10:44:53 +03:00
|
|
|
return "", nil, fmt.Errorf("nil message but path params requested: %v", path)
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:10:24 +03:00
|
|
|
fieldsmapskip := make(map[string]struct{})
|
2021-07-30 23:02:50 +03:00
|
|
|
fieldsmap := make(map[string]string, len(tpl))
|
|
|
|
for _, v := range tpl {
|
2021-09-01 02:06:01 +03:00
|
|
|
var vs, ve int
|
|
|
|
for i := 0; i < len(v); i++ {
|
|
|
|
switch v[i] {
|
|
|
|
case '{':
|
|
|
|
vs = i + 1
|
|
|
|
case '}':
|
|
|
|
ve = i
|
|
|
|
}
|
|
|
|
if ve != 0 {
|
|
|
|
fieldsmap[v[vs:ve]] = ""
|
|
|
|
vs = 0
|
|
|
|
ve = 0
|
|
|
|
}
|
2021-07-30 23:02:50 +03:00
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:10:24 +03:00
|
|
|
values := url.Values{}
|
2021-01-19 04:12:31 +03:00
|
|
|
// copy cycle
|
2024-05-02 14:40:12 +03:00
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
for i := 0; i < tmsg.NumField(); i++ {
|
|
|
|
val := tmsg.Field(i)
|
|
|
|
if val.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fld := tmsg.Type().Field(i)
|
2022-03-03 17:47:12 +03:00
|
|
|
// Skip unexported fields.
|
|
|
|
if fld.PkgPath != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
/* check for empty PkgPath can be replaced with new method IsExported
|
2022-03-02 17:51:29 +03:00
|
|
|
if !fld.IsExported() {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-03 17:47:12 +03:00
|
|
|
*/
|
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
|
2021-04-19 10:08:28 +03:00
|
|
|
for _, p := range tp {
|
|
|
|
if idx := strings.Index(p, "name="); idx > 0 {
|
|
|
|
t = &tag{key: tn, name: p[idx:]}
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
default:
|
2021-04-19 10:08:28 +03:00
|
|
|
t = &tag{key: tn, name: tp[0]}
|
2021-03-23 00:29:27 +03:00
|
|
|
}
|
2024-05-02 14:40:12 +03:00
|
|
|
|
2021-03-23 00:29:27 +03:00
|
|
|
if t.name != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
cname := t.name
|
|
|
|
if cname == "" {
|
|
|
|
cname = fld.Name
|
2021-03-23 00:29:27 +03:00
|
|
|
// fallback to lowercase
|
|
|
|
t.name = strings.ToLower(fld.Name)
|
|
|
|
}
|
2021-10-25 19:59:37 +03:00
|
|
|
if _, ok := parameters["header"][cname]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := parameters["cookie"][cname]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
|
2021-04-03 11:55:22 +03:00
|
|
|
if !val.IsValid() || val.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-09 23:10:48 +03:00
|
|
|
// nolint: gocritic, nestif
|
2021-03-23 00:29:27 +03:00
|
|
|
if _, ok := fieldsmap[t.name]; ok {
|
2021-04-25 16:26:36 +03:00
|
|
|
switch val.Type().Kind() {
|
|
|
|
case reflect.Slice:
|
2021-04-19 02:10:24 +03:00
|
|
|
for idx := 0; idx < val.Len(); idx++ {
|
2022-03-10 12:27:37 +03:00
|
|
|
values.Add(t.name, getParam(val.Index(idx)))
|
2021-04-19 02:10:24 +03:00
|
|
|
}
|
|
|
|
fieldsmapskip[t.name] = struct{}{}
|
2021-04-25 16:26:36 +03:00
|
|
|
default:
|
2022-03-10 12:27:37 +03:00
|
|
|
fieldsmap[t.name] = getParam(val)
|
2021-04-19 02:10:24 +03:00
|
|
|
}
|
2021-07-09 18:22:40 +03:00
|
|
|
} else if (body == "*" || body == t.name) && method != http.MethodGet {
|
|
|
|
if tnmsg.Field(i).CanSet() {
|
|
|
|
tnmsg.Field(i).Set(val)
|
|
|
|
}
|
2021-03-19 14:17:53 +03:00
|
|
|
} else {
|
2024-05-02 14:40:12 +03:00
|
|
|
isSet := false
|
|
|
|
for k, v := range fieldsmap {
|
|
|
|
if v != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fld := msg
|
|
|
|
|
|
|
|
parts := strings.Split(k, ".")
|
2024-05-04 13:41:59 +03:00
|
|
|
|
2024-05-02 14:40:12 +03:00
|
|
|
for idx := 0; idx < len(parts); idx++ {
|
2024-05-04 13:41:59 +03:00
|
|
|
var nfld interface{}
|
|
|
|
if tags == nil {
|
|
|
|
tags = []string{"json"}
|
|
|
|
}
|
|
|
|
tagsloop:
|
|
|
|
for i := 0; i < len(tags); i++ {
|
|
|
|
nfld, err = rutil.StructFieldByTag(fld, tags[i], parts[idx])
|
|
|
|
if err == nil {
|
|
|
|
break tagsloop
|
|
|
|
}
|
2024-05-02 14:40:12 +03:00
|
|
|
}
|
2024-05-04 13:41:59 +03:00
|
|
|
if err == nil {
|
|
|
|
fld = nfld
|
|
|
|
if len(parts)-1 == idx {
|
|
|
|
isSet = true
|
|
|
|
fieldsmap[k] = fmt.Sprintf("%v", fld)
|
|
|
|
}
|
2024-05-02 14:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isSet {
|
|
|
|
if val.Type().Kind() == reflect.Slice {
|
|
|
|
for idx := 0; idx < val.Len(); idx++ {
|
|
|
|
values.Add(t.name, getParam(val.Index(idx)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
values.Add(t.name, getParam(val))
|
2021-04-19 02:10:24 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check not filled stuff
|
|
|
|
for k, v := range fieldsmap {
|
2021-04-19 02:10:24 +03:00
|
|
|
_, ok := fieldsmapskip[k]
|
|
|
|
if !ok && 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
|
2021-09-01 02:06:01 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
for _, fld := range tpl {
|
2021-01-19 04:12:31 +03:00
|
|
|
_, _ = b.WriteRune('/')
|
2021-07-31 15:01:44 +03:00
|
|
|
// nolint: nestif
|
2021-09-01 02:06:01 +03:00
|
|
|
var vs, ve, vf int
|
|
|
|
var pholder bool
|
|
|
|
for i := 0; i < len(fld); i++ {
|
|
|
|
switch fld[i] {
|
|
|
|
case '{':
|
|
|
|
vs = i + 1
|
|
|
|
case '}':
|
|
|
|
ve = i
|
|
|
|
}
|
|
|
|
// nolint: nestif
|
|
|
|
if vs > 0 && ve != 0 {
|
|
|
|
if vm, ok := fieldsmap[fld[vs:ve]]; ok {
|
|
|
|
if vm != "" {
|
|
|
|
_, _ = b.WriteString(fld[vf : vs-1])
|
|
|
|
_, _ = b.WriteString(vm)
|
|
|
|
vf = ve + 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, _ = b.WriteString(fld)
|
2021-07-30 23:02:50 +03:00
|
|
|
}
|
2021-09-01 02:06:01 +03:00
|
|
|
vs = 0
|
|
|
|
ve = 0
|
|
|
|
pholder = true
|
2021-04-19 02:10:24 +03:00
|
|
|
}
|
2021-09-01 02:06:01 +03:00
|
|
|
}
|
|
|
|
if !pholder {
|
2021-01-19 04:12:31 +03:00
|
|
|
_, _ = b.WriteString(fld)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:10:24 +03:00
|
|
|
if len(values) > 0 {
|
|
|
|
_, _ = b.WriteRune('?')
|
|
|
|
_, _ = b.WriteString(values.Encode())
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2024-03-26 09:09:38 +03:00
|
|
|
if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) {
|
2021-03-21 17:44:48 +03:00
|
|
|
return b.String(), nil, nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
return b.String(), nmsg, nil
|
|
|
|
}
|
|
|
|
|
2024-03-26 09:09:38 +03:00
|
|
|
func isEmptyStruct(v interface{}) bool {
|
|
|
|
val := reflect.ValueOf(v)
|
|
|
|
if val.Kind() == reflect.Ptr {
|
|
|
|
val = val.Elem()
|
|
|
|
}
|
|
|
|
return val.Kind() == reflect.Struct && val.NumField() == 0
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
func newTemplate(path string) ([]string, error) {
|
|
|
|
if len(path) == 0 || path[0] != '/' {
|
|
|
|
return nil, fmt.Errorf("path must starts with /")
|
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
mu.RLock()
|
|
|
|
tpl, ok := templateCache[path]
|
|
|
|
if ok {
|
|
|
|
mu.RUnlock()
|
|
|
|
return tpl, nil
|
|
|
|
}
|
|
|
|
mu.RUnlock()
|
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
tpl = strings.Split(path[1:], "/")
|
2021-01-19 04:12:31 +03:00
|
|
|
mu.Lock()
|
|
|
|
templateCache[path] = tpl
|
|
|
|
mu.Unlock()
|
|
|
|
|
|
|
|
return tpl, nil
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
|
2021-04-18 15:45:42 +03:00
|
|
|
func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp interface{}, opts client.CallOptions) error {
|
2021-04-25 16:26:36 +03:00
|
|
|
var err error
|
2023-03-16 19:16:35 +03:00
|
|
|
var buf []byte
|
|
|
|
|
|
|
|
// fast path return
|
|
|
|
if hrsp.StatusCode == http.StatusNoContent {
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-25 16:26:36 +03:00
|
|
|
|
2023-07-11 00:47:36 +03:00
|
|
|
if opts.ResponseMetadata != nil {
|
|
|
|
*opts.ResponseMetadata = metadata.New(len(hrsp.Header))
|
|
|
|
for k, v := range hrsp.Header {
|
|
|
|
opts.ResponseMetadata.Set(k, strings.Join(v, ","))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = ctx.Err()
|
|
|
|
default:
|
|
|
|
ct := DefaultContentType
|
2021-04-18 15:45:42 +03:00
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
if htype := hrsp.Header.Get("Content-Type"); htype != "" {
|
|
|
|
ct = htype
|
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
|
2023-03-16 19:16:35 +03:00
|
|
|
if hrsp.Body != nil {
|
|
|
|
buf, err = io.ReadAll(hrsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if h.opts.Logger.V(logger.ErrorLevel) {
|
2024-04-09 23:46:36 +03:00
|
|
|
h.opts.Logger.Error(ctx, "failed to read body", err)
|
2021-07-14 17:05:33 +03:00
|
|
|
}
|
2023-03-16 19:16:35 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", string(buf))
|
2021-07-14 17:05:33 +03:00
|
|
|
}
|
2023-03-16 19:16:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cf, cerr := h.newCodec(ct)
|
|
|
|
if cerr != nil {
|
2023-03-15 22:26:14 +03:00
|
|
|
if h.opts.Logger.V(logger.DebugLevel) {
|
2024-04-09 23:46:36 +03:00
|
|
|
h.opts.Logger.Debug(ctx, fmt.Sprintf("response with %v unknown content-type %s %s", hrsp.Header, ct, buf))
|
2023-03-15 22:26:14 +03:00
|
|
|
}
|
2021-04-25 16:26:36 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
|
2023-03-16 19:16:35 +03:00
|
|
|
if h.opts.Logger.V(logger.DebugLevel) {
|
2024-04-09 23:46:36 +03:00
|
|
|
h.opts.Logger.Debug(ctx, fmt.Sprintf("response %s with %v", buf, hrsp.Header))
|
2023-03-16 19:16:35 +03:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:02:35 +03:00
|
|
|
// succeseful response
|
2021-04-25 16:26:36 +03:00
|
|
|
if hrsp.StatusCode < 400 {
|
2023-03-16 19:16:35 +03:00
|
|
|
if err = cf.Unmarshal(buf, rsp); err != nil {
|
2021-04-25 16:26:36 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2021-03-23 00:29:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:02:35 +03:00
|
|
|
// response with error
|
|
|
|
var rerr interface{}
|
2021-04-25 16:26:36 +03:00
|
|
|
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
|
|
|
if ok && errmap != nil {
|
2021-07-05 16:02:35 +03:00
|
|
|
rerr, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)]
|
|
|
|
if !ok {
|
|
|
|
rerr, ok = errmap["default"]
|
2021-04-25 16:26:36 +03:00
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
}
|
2021-07-05 16:02:35 +03:00
|
|
|
|
|
|
|
if !ok || rerr == nil {
|
2021-04-25 16:26:36 +03:00
|
|
|
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
2021-04-18 15:45:42 +03:00
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
|
2023-03-16 19:16:35 +03:00
|
|
|
if cerr := cf.Unmarshal(buf, rerr); cerr != nil {
|
2021-07-05 16:10:38 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
2021-04-25 16:26:36 +03:00
|
|
|
}
|
2021-07-05 16:02:35 +03:00
|
|
|
|
|
|
|
if err, ok = rerr.(error); !ok {
|
|
|
|
err = &Error{rerr}
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:29:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type tag struct {
|
|
|
|
key string
|
|
|
|
name string
|
|
|
|
}
|
2022-03-10 12:27:37 +03:00
|
|
|
|
|
|
|
func getParam(val reflect.Value) string {
|
|
|
|
var v string
|
|
|
|
switch val.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
switch reflect.Indirect(val).Type().String() {
|
|
|
|
case
|
|
|
|
"wrapperspb.BoolValue",
|
|
|
|
"wrapperspb.BytesValue",
|
|
|
|
"wrapperspb.DoubleValue",
|
|
|
|
"wrapperspb.FloatValue",
|
|
|
|
"wrapperspb.Int32Value", "wrapperspb.Int64Value",
|
|
|
|
"wrapperspb.StringValue",
|
|
|
|
"wrapperspb.UInt32Value", "wrapperspb.UInt64Value":
|
|
|
|
if eva := reflect.Indirect(val).FieldByName("Value"); eva.IsValid() {
|
|
|
|
v = getParam(eva)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
v = fmt.Sprintf("%v", val.Interface())
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|