fix(commands/http/client) cast safely
Brian Tiger Chow committed
Nov 4, 2014 at 02:09 UTC
d3f9aadd3eb7b756f560d2cc598221884831a52d
1 file changed
+28
-5
commands/http/client.go
+28
-5
@@ -3,6 +3,7 @@ package http
3
import (
4
"bytes"
5
"encoding/json"
6
+ "errors"
7
"fmt"
8
"io"
9
"net/http"
@@ -12,6 +13,8 @@ import (
13
cmds "github.com/jbenet/go-ipfs/commands"
14
)
15
16
+var castError = errors.New("cast error")
17
+
18
const (
19
ApiUrlFormat = "http://%s%s/%s?%s"
20
ApiPath = "/api/v0" // TODO: make configurable
@@ -33,11 +36,19 @@ func NewClient(address string) Client {
36
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
37
var userEncoding string
38
if enc, found := req.Option(cmds.EncShort); found {
36
- userEncoding = enc.(string)
39
+ var ok bool
40
+ userEncoding, ok = enc.(string)
41
+ if !ok {
42
+ return nil, castError
43
+ }
44
req.SetOption(cmds.EncShort, cmds.JSON)
45
} else {
46
+ var ok bool
47
enc, _ := req.Option(cmds.EncLong)
40
- userEncoding = enc.(string)
48
+ userEncoding, ok = enc.(string)
49
+ if !ok {
50
+ return nil, castError
51
+ }
52
req.SetOption(cmds.EncLong, cmds.JSON)
53
}
54
@@ -73,7 +84,11 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
84
85
query := url.Values{}
86
for k, v := range req.Options() {
76
- query.Set(k, v.(string))
87
+ str, ok := v.(string)
88
+ if !ok {
89
+ return "", nil, castError
90
+ }
91
+ query.Set(k, str)
92
}
93
94
args := req.Arguments()
@@ -86,14 +101,22 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
101
}
102
103
if argDef.Type == cmds.ArgString {
89
- query.Add("arg", arg.(string))
104
+ str, ok := arg.(string)
105
+ if !ok {
106
+ return "", nil, castError
107
+ }
108
+ query.Add("arg", str)
109
110
} else {
111
// TODO: multipart
112
if inputStream != nil {
113
return "", nil, fmt.Errorf("Currently, only one file stream is possible per request")
114
}
96
- inputStream = arg.(io.Reader)
115
+ var ok bool
116
+ inputStream, ok = arg.(io.Reader)
117
+ if !ok {
118
+ return "", nil, castError
119
+ }
120
}
121
}
122