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
40858b43
Commit
40858b43
authored
10 years ago
by
Matt Bell
Committed by
Juan Batiz-Benet
10 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Added stream argument handling to client and request parser
parent
39c78fbe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
17 deletions
+45
-17
commands/http/client.go
commands/http/client.go
+22
-6
commands/http/parse.go
commands/http/parse.go
+23
-11
No files found.
commands/http/client.go
View file @
40858b43
...
...
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
...
...
@@ -14,7 +15,7 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
)
const
ApiPath
=
"/api/v0"
const
ApiPath
=
"/api/v0"
// TODO: make configurable
func
Send
(
req
cmds
.
Request
)
(
cmds
.
Response
,
error
)
{
addr
,
err
:=
ma
.
NewMultiaddr
(
req
.
Context
()
.
Config
.
Addresses
.
API
)
...
...
@@ -40,18 +41,33 @@ func Send(req cmds.Request) (cmds.Response, error) {
req
.
SetOption
(
cmds
.
EncLong
,
cmds
.
JSON
)
}
// TODO: handle multiple files with multipart
var
in
io
.
Reader
query
:=
"?"
for
k
,
v
:=
range
req
.
Options
()
{
query
+=
"&"
+
k
+
"="
+
v
.
(
string
)
}
for
_
,
arg
:=
range
req
.
Arguments
()
{
s
,
ok
:=
arg
.
(
string
)
if
ok
{
query
+=
"&arg="
+
s
args
:=
req
.
Arguments
()
for
i
,
arg
:=
range
args
{
if
req
.
Command
()
.
Arguments
[
i
]
.
Type
==
cmds
.
ArgString
{
query
+=
"&arg="
+
arg
.
(
string
)
}
else
{
// TODO: multipart
if
in
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Currently, only one file stream is possible per request"
)
}
in
,
err
=
os
.
Open
(
arg
.
(
string
))
if
err
!=
nil
{
return
nil
,
err
}
args
[
i
]
=
in
}
}
httpRes
,
err
:=
http
.
Post
(
url
+
query
,
"application/octet-stream"
,
n
il
)
httpRes
,
err
:=
http
.
Post
(
url
+
query
,
"application/octet-stream"
,
i
n
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
This diff is collapsed.
Click to expand it.
commands/http/parse.go
View file @
40858b43
...
...
@@ -10,7 +10,7 @@ import (
// Parse parses the data in a http.Request and returns a command Request object
func
Parse
(
r
*
http
.
Request
,
root
*
cmds
.
Command
)
(
cmds
.
Request
,
error
)
{
path
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)[
3
:
]
a
rgs
:=
make
([]
interface
{}
,
0
)
stringA
rgs
:=
make
([]
string
,
0
)
cmd
,
err
:=
root
.
Get
(
path
[
:
len
(
path
)
-
1
])
if
err
!=
nil
{
...
...
@@ -24,36 +24,48 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
// if the last string in the path isn't a subcommand, use it as an argument
// e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
a
rgs
=
append
(
a
rgs
,
path
[
len
(
path
)
-
1
])
stringA
rgs
=
append
(
stringA
rgs
,
path
[
len
(
path
)
-
1
])
path
=
path
[
:
len
(
path
)
-
1
]
}
else
{
cmd
=
sub
}
opts
,
args2
:=
parseOptions
(
r
)
args
=
append
(
args
,
args2
...
)
opts
,
stringArgs2
:=
parseOptions
(
r
)
stringArgs
=
append
(
stringArgs
,
stringArgs2
...
)
// Note that the argument handling here is dumb, it does not do any error-checking.
// (Arguments are further processed when the request is passed to the command to run)
args
:=
make
([]
interface
{},
len
(
cmd
.
Arguments
))
for
i
,
arg
:=
range
cmd
.
Arguments
{
if
arg
.
Type
==
cmds
.
ArgString
{
if
len
(
stringArgs
)
>
0
{
args
[
i
]
=
stringArgs
[
0
]
stringArgs
=
stringArgs
[
1
:
]
}
}
else
{
// TODO: create multipart streams for file args
args
[
i
]
=
r
.
Body
}
}
return
cmds
.
NewRequest
(
path
,
opts
,
args
,
cmd
),
nil
}
func
parseOptions
(
r
*
http
.
Request
)
(
map
[
string
]
interface
{},
[]
interface
{}
)
{
func
parseOptions
(
r
*
http
.
Request
)
(
map
[
string
]
interface
{},
[]
string
)
{
opts
:=
make
(
map
[
string
]
interface
{})
args
:=
make
([]
interface
{},
0
)
var
args
[]
string
query
:=
r
.
URL
.
Query
()
for
k
,
v
:=
range
query
{
if
k
==
"arg"
{
for
_
,
s
:=
range
v
{
args
=
append
(
args
,
interface
{}(
s
))
}
args
=
v
}
else
{
opts
[
k
]
=
v
[
0
]
}
}
// TODO: create multipart streams for file args
// default to setting encoding to JSON
_
,
short
:=
opts
[
cmds
.
EncShort
]
_
,
long
:=
opts
[
cmds
.
EncLong
]
...
...
This diff is collapsed.
Click to expand it.
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