Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
b5e8fd5b
Commit
b5e8fd5b
authored
Nov 04, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(commands/http/client) cast safely
parent
f13e1fed
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
5 deletions
+28
-5
http/client.go
http/client.go
+28
-5
No files found.
http/client.go
View file @
b5e8fd5b
...
...
@@ -3,6 +3,7 @@ package http
import
(
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
...
...
@@ -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
...
...
@@ -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
)
}
...
...
@@ -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
()
...
...
@@ -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
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment