Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
40858b43
提交
40858b43
authored
11月 02, 2014
作者:
Matt Bell
提交者:
Juan Batiz-Benet
11月 04, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands/http: Added stream argument handling to client and request parser
上级
39c78fbe
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
45 行增加
和
17 行删除
+45
-17
client.go
commands/http/client.go
+22
-6
parse.go
commands/http/parse.go
+23
-11
没有找到文件。
commands/http/client.go
浏览文件 @
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"
,
nil
)
httpRes
,
err
:=
http
.
Post
(
url
+
query
,
"application/octet-stream"
,
in
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
commands/http/parse.go
浏览文件 @
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
:
]
args
:=
make
([]
interface
{}
,
0
)
stringArgs
:=
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)
args
=
append
(
a
rgs
,
path
[
len
(
path
)
-
1
])
stringArgs
=
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
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论