Skip to content

Commit

Permalink
fix(commands/http/client) cast safely
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Tiger Chow committed Nov 4, 2014
1 parent d1405e4 commit 976a7c1
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions commands/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -12,6 +13,8 @@ import (
cmds "github.com/jbenet/go-ipfs/commands"
)

var castError = errors.New("cast error")

const (
ApiUrlFormat = "http://%s%s/%s?%s"
ApiPath = "/api/v0" // TODO: make configurable
Expand All @@ -33,11 +36,19 @@ func NewClient(address string) Client {
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
var userEncoding string
if enc, found := req.Option(cmds.EncShort); found {
userEncoding = enc.(string)
var ok bool
userEncoding, ok = enc.(string)
if !ok {
return nil, castError
}
req.SetOption(cmds.EncShort, cmds.JSON)
} else {
var ok bool
enc, _ := req.Option(cmds.EncLong)
userEncoding = enc.(string)
userEncoding, ok = enc.(string)
if !ok {
return nil, castError
}
req.SetOption(cmds.EncLong, cmds.JSON)
}

Expand Down Expand Up @@ -73,7 +84,11 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {

query := url.Values{}
for k, v := range req.Options() {
query.Set(k, v.(string))
str, ok := v.(string)
if !ok {
return "", nil, castError
}
query.Set(k, str)
}

args := req.Arguments()
Expand All @@ -86,14 +101,22 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
}

if argDef.Type == cmds.ArgString {
query.Add("arg", arg.(string))
str, ok := arg.(string)
if !ok {
return "", nil, castError
}
query.Add("arg", str)

} else {
// TODO: multipart
if inputStream != nil {
return "", nil, fmt.Errorf("Currently, only one file stream is possible per request")
}
inputStream = arg.(io.Reader)
var ok bool
inputStream, ok = arg.(io.Reader)
if !ok {
return "", nil, castError
}
}
}

Expand Down

1 comment on commit 976a7c1

@jbenet
Copy link
Member

@jbenet jbenet commented on 976a7c1 Nov 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.