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
Commits
132e7402
Commit
132e7402
authored
Nov 16, 2014
by
Matt Bell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Send req.Files() as multipart
parent
53735263
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
20 deletions
+26
-20
commands/http/client.go
commands/http/client.go
+26
-20
No files found.
commands/http/client.go
View file @
132e7402
...
@@ -10,6 +10,7 @@ import (
...
@@ -10,6 +10,7 @@ import (
"strings"
"strings"
cmds
"github.com/jbenet/go-ipfs/commands"
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/config"
u
"github.com/jbenet/go-ipfs/util"
u
"github.com/jbenet/go-ipfs/util"
)
)
...
@@ -42,16 +43,35 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
...
@@ -42,16 +43,35 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
// override with json to send to server
// override with json to send to server
req
.
SetOption
(
cmds
.
EncShort
,
cmds
.
JSON
)
req
.
SetOption
(
cmds
.
EncShort
,
cmds
.
JSON
)
query
,
inputStream
,
err
:=
getQuery
(
req
)
query
,
err
:=
getQuery
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
var
fileReader
*
MultiFileReader
if
req
.
Files
()
!=
nil
{
fileReader
=
NewMultiFileReader
(
req
.
Files
())
}
path
:=
strings
.
Join
(
req
.
Path
(),
"/"
)
path
:=
strings
.
Join
(
req
.
Path
(),
"/"
)
url
:=
fmt
.
Sprintf
(
ApiUrlFormat
,
c
.
serverAddress
,
ApiPath
,
path
,
query
)
url
:=
fmt
.
Sprintf
(
ApiUrlFormat
,
c
.
serverAddress
,
ApiPath
,
path
,
query
)
// TODO extract string const?
httpReq
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
fileReader
)
httpRes
,
err
:=
http
.
Post
(
url
,
"application/octet-stream"
,
inputStream
)
if
err
!=
nil
{
return
nil
,
err
}
// TODO extract string consts?
if
fileReader
!=
nil
{
httpReq
.
Header
.
Set
(
"Content-Type"
,
"multipart/form-data; boundary="
+
fileReader
.
Boundary
())
httpReq
.
Header
.
Set
(
"Content-Disposition"
,
"form-data: name=
\"
files
\"
"
)
}
else
{
httpReq
.
Header
.
Set
(
"Content-Type"
,
"application/octet-stream"
)
}
version
:=
config
.
CurrentVersionNumber
httpReq
.
Header
.
Set
(
"User-Agent"
,
fmt
.
Sprintf
(
"/go-ipfs/%s/"
,
version
))
httpRes
,
err
:=
http
.
DefaultClient
.
Do
(
httpReq
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -72,10 +92,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
...
@@ -72,10 +92,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
return
res
,
nil
return
res
,
nil
}
}
func
getQuery
(
req
cmds
.
Request
)
(
string
,
io
.
Reader
,
error
)
{
func
getQuery
(
req
cmds
.
Request
)
(
string
,
error
)
{
// TODO: handle multiple files with multipart
var
inputStream
io
.
Reader
query
:=
url
.
Values
{}
query
:=
url
.
Values
{}
for
k
,
v
:=
range
req
.
Options
()
{
for
k
,
v
:=
range
req
.
Options
()
{
str
:=
fmt
.
Sprintf
(
"%v"
,
v
)
str
:=
fmt
.
Sprintf
(
"%v"
,
v
)
...
@@ -94,24 +111,13 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
...
@@ -94,24 +111,13 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
if
argDef
.
Type
==
cmds
.
ArgString
{
if
argDef
.
Type
==
cmds
.
ArgString
{
str
,
ok
:=
arg
.
(
string
)
str
,
ok
:=
arg
.
(
string
)
if
!
ok
{
if
!
ok
{
return
""
,
nil
,
u
.
ErrCast
()
return
""
,
u
.
ErrCast
()
}
}
query
.
Add
(
"arg"
,
str
)
query
.
Add
(
"arg"
,
str
)
}
else
{
// TODO: multipart
if
inputStream
!=
nil
{
return
""
,
nil
,
fmt
.
Errorf
(
"Currently, only one file stream is possible per request"
)
}
var
ok
bool
inputStream
,
ok
=
arg
.
(
io
.
Reader
)
if
!
ok
{
return
""
,
nil
,
u
.
ErrCast
()
}
}
}
}
}
return
query
.
Encode
(),
inputStream
,
nil
return
query
.
Encode
(),
nil
}
}
// getResponse decodes a http.Response to create a cmds.Response
// getResponse decodes a http.Response to create a cmds.Response
...
...
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