Handle quoted and hex string type HTTP args for both 'string' and '[]byte' type function args
This commit is contained in:
parent
af1212897c
commit
86506cd4f8
|
@ -121,7 +121,7 @@ func (c *ClientURI) call(method string, params map[string]interface{}, result in
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Info(Fmt("URI request to %v (%v): %v", c.address, method, values))
|
// log.Info(Fmt("URI request to %v (%v): %v", c.address, method, values))
|
||||||
resp, err := c.client.PostForm(c.address+"/"+method, values)
|
resp, err := c.client.PostForm(c.address+"/"+method, values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -178,13 +178,6 @@ func argsToJson(args map[string]interface{}) error {
|
||||||
var n int
|
var n int
|
||||||
var err error
|
var err error
|
||||||
for k, v := range args {
|
for k, v := range args {
|
||||||
// Convert strings to "0x"-prefixed hex
|
|
||||||
str, isString := reflect.ValueOf(v).Interface().(string)
|
|
||||||
if isString {
|
|
||||||
args[k] = fmt.Sprintf("0x%X", str)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert byte slices to "0x"-prefixed hex
|
// Convert byte slices to "0x"-prefixed hex
|
||||||
byteSlice, isByteSlice := reflect.ValueOf(v).Interface().([]byte)
|
byteSlice, isByteSlice := reflect.ValueOf(v).Interface().([]byte)
|
||||||
if isByteSlice {
|
if isByteSlice {
|
||||||
|
|
|
@ -225,36 +225,18 @@ func httpParamsToArgs(rpcFunc *RPCFunc, r *http.Request) ([]reflect.Value, error
|
||||||
argTypes := rpcFunc.args
|
argTypes := rpcFunc.args
|
||||||
argNames := rpcFunc.argNames
|
argNames := rpcFunc.argNames
|
||||||
|
|
||||||
var err error
|
|
||||||
values := make([]reflect.Value, len(argNames))
|
values := make([]reflect.Value, len(argNames))
|
||||||
for i, name := range argNames {
|
for i, name := range argNames {
|
||||||
ty := argTypes[i]
|
ty := argTypes[i]
|
||||||
arg := GetParam(r, name)
|
arg := GetParam(r, name)
|
||||||
// log.Notice("param to arg", "ty", ty, "name", name, "arg", arg)
|
// log.Notice("param to arg", "ty", ty, "name", name, "arg", arg)
|
||||||
|
|
||||||
// Handle quoted strings
|
v, err, ok := nonJsonToArg(ty, arg)
|
||||||
if strings.HasPrefix(arg, "\"") && strings.HasSuffix(arg, "\"") {
|
if err != nil {
|
||||||
data := arg[1 : len(arg)-1]
|
return nil, err
|
||||||
if ty.Kind() == reflect.String {
|
|
||||||
values[i] = reflect.ValueOf(string(data))
|
|
||||||
} else {
|
|
||||||
values[i] = reflect.ValueOf(data)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
if ok {
|
||||||
// Handle hex strings
|
values[i] = v
|
||||||
if strings.HasPrefix(strings.ToLower(arg), "0x") {
|
|
||||||
var value []byte
|
|
||||||
value, err = hex.DecodeString(arg[2:])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if ty.Kind() == reflect.String {
|
|
||||||
values[i] = reflect.ValueOf(string(value))
|
|
||||||
} else {
|
|
||||||
values[i] = reflect.ValueOf(value)
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,6 +260,44 @@ func _jsonStringToArg(ty reflect.Type, arg string) (reflect.Value, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nonJsonToArg(ty reflect.Type, arg string) (reflect.Value, error, bool) {
|
||||||
|
isQuotedString := strings.HasPrefix(arg, `"`) && strings.HasSuffix(arg, `"`)
|
||||||
|
isHexString := strings.HasPrefix(strings.ToLower(arg), "0x")
|
||||||
|
expectingString := ty.Kind() == reflect.String
|
||||||
|
expectingByteSlice := ty.Kind() == reflect.Slice && ty.Elem().Kind() == reflect.Uint8
|
||||||
|
|
||||||
|
if isHexString {
|
||||||
|
if !expectingString && !expectingByteSlice {
|
||||||
|
err := fmt.Errorf("Got a hex string arg, but expected '%s'",
|
||||||
|
ty.Kind().String())
|
||||||
|
return reflect.ValueOf(nil), err, false
|
||||||
|
}
|
||||||
|
|
||||||
|
var value []byte
|
||||||
|
value, err := hex.DecodeString(arg[2:])
|
||||||
|
if err != nil {
|
||||||
|
return reflect.ValueOf(nil), err, false
|
||||||
|
}
|
||||||
|
if ty.Kind() == reflect.String {
|
||||||
|
return reflect.ValueOf(string(value)), nil, true
|
||||||
|
}
|
||||||
|
return reflect.ValueOf([]byte(value)), nil, true
|
||||||
|
}
|
||||||
|
|
||||||
|
if isQuotedString && expectingByteSlice {
|
||||||
|
var err error
|
||||||
|
v := reflect.New(reflect.TypeOf(""))
|
||||||
|
wire.ReadJSONPtr(v.Interface(), []byte(arg), &err)
|
||||||
|
if err != nil {
|
||||||
|
return reflect.ValueOf(nil), err, false
|
||||||
|
}
|
||||||
|
v = v.Elem()
|
||||||
|
return reflect.ValueOf([]byte(v.String())), nil, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.ValueOf(nil), nil, false
|
||||||
|
}
|
||||||
|
|
||||||
// rpc.http
|
// rpc.http
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// rpc.websocket
|
// rpc.websocket
|
||||||
|
|
Loading…
Reference in New Issue