Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
23d41e08
提交
23d41e08
authored
7月 27, 2015
作者:
Jeromy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
address comments from CR and fix random failures
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
上级
1ce310be
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
19 行删除
+29
-19
client.go
commands/http/client.go
+24
-18
handler.go
commands/http/handler.go
+5
-1
没有找到文件。
commands/http/client.go
浏览文件 @
23d41e08
...
...
@@ -30,10 +30,18 @@ type Client interface {
type
client
struct
{
serverAddress
string
httpClient
http
.
Client
}
func
NewClient
(
address
string
)
Client
{
return
&
client
{
address
}
return
&
client
{
serverAddress
:
address
,
httpClient
:
http
.
Client
{
Transport
:
&
http
.
Transport
{
DisableKeepAlives
:
true
,
},
},
}
}
func
(
c
*
client
)
Send
(
req
cmds
.
Request
)
(
cmds
.
Response
,
error
)
{
...
...
@@ -85,20 +93,20 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
// 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
\"
"
)
httpReq
.
Header
.
Set
(
contentTypeHeader
,
"multipart/form-data; boundary="
+
fileReader
.
Boundary
())
httpReq
.
Header
.
Set
(
contentDispHeader
,
"form-data: name=
\"
files
\"
"
)
}
else
{
httpReq
.
Header
.
Set
(
"Content-Type"
,
"application/octet-stream"
)
httpReq
.
Header
.
Set
(
contentTypeHeader
,
applicationOctetStream
)
}
version
:=
config
.
CurrentVersionNumber
httpReq
.
Header
.
Set
(
"User-Agent"
,
fmt
.
Sprintf
(
"/go-ipfs/%s/"
,
version
))
httpReq
.
Header
.
Set
(
uaHeader
,
fmt
.
Sprintf
(
"/go-ipfs/%s/"
,
version
))
ec
:=
make
(
chan
error
,
1
)
rc
:=
make
(
chan
cmds
.
Response
,
1
)
dc
:=
req
.
Context
()
.
Done
()
go
func
()
{
httpRes
,
err
:=
http
.
Default
Client
.
Do
(
httpReq
)
httpRes
,
err
:=
c
.
http
Client
.
Do
(
httpReq
)
if
err
!=
nil
{
ec
<-
err
return
...
...
@@ -182,24 +190,25 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
res
.
SetLength
(
length
)
}
res
.
SetCloser
(
httpRes
.
Body
)
rr
:=
&
httpResponseReader
{
httpRes
}
res
.
SetCloser
(
rr
)
if
contentType
!=
"application/json"
{
if
contentType
!=
applicationJson
{
// for all non json output types, just stream back the output
res
.
SetOutput
(
&
httpResponseReader
{
httpRes
}
)
res
.
SetOutput
(
rr
)
return
res
,
nil
}
else
if
len
(
httpRes
.
Header
.
Get
(
channelHeader
))
>
0
{
// if output is coming from a channel, decode each chunk
outChan
:=
make
(
chan
interface
{})
go
readStreamedJson
(
req
,
httpRes
,
outChan
)
go
readStreamedJson
(
req
,
rr
,
outChan
)
res
.
SetOutput
((
<-
chan
interface
{})(
outChan
))
return
res
,
nil
}
dec
:=
json
.
NewDecoder
(
&
httpResponseReader
{
httpRes
}
)
dec
:=
json
.
NewDecoder
(
rr
)
// If we ran into an error
if
httpRes
.
StatusCode
>=
http
.
StatusBadRequest
{
...
...
@@ -211,7 +220,7 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
e
.
Message
=
"Command not found."
e
.
Code
=
cmds
.
ErrClient
case
contentType
==
"text/plain"
:
case
contentType
==
plainText
:
// handle non-marshalled errors
buf
:=
bytes
.
NewBuffer
(
nil
)
io
.
Copy
(
buf
,
httpRes
.
Body
)
...
...
@@ -244,9 +253,9 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
// read json objects off of the given stream, and write the objects out to
// the 'out' channel
func
readStreamedJson
(
req
cmds
.
Request
,
httpRes
*
http
.
Response
,
out
chan
<-
interface
{})
{
func
readStreamedJson
(
req
cmds
.
Request
,
rr
io
.
Reader
,
out
chan
<-
interface
{})
{
defer
close
(
out
)
dec
:=
json
.
NewDecoder
(
&
httpResponseReader
{
httpRes
}
)
dec
:=
json
.
NewDecoder
(
rr
)
outputType
:=
reflect
.
TypeOf
(
req
.
Command
()
.
Type
)
ctx
:=
req
.
Context
()
...
...
@@ -254,9 +263,7 @@ func readStreamedJson(req cmds.Request, httpRes *http.Response, out chan<- inter
for
{
v
,
err
:=
decodeTypedVal
(
outputType
,
dec
)
if
err
!=
nil
{
// since we are just looping reading on the response, the only way to
// know we are 'done' is for the consumer to close the response body.
// doing so doesnt throw an io.EOF, but we want to treat it like one.
// reading on a closed response body is as good as an io.EOF here
if
!
(
strings
.
Contains
(
err
.
Error
(),
"read on closed response body"
)
||
err
==
io
.
EOF
)
{
log
.
Error
(
err
)
}
...
...
@@ -268,7 +275,6 @@ func readStreamedJson(req cmds.Request, httpRes *http.Response, out chan<- inter
return
case
out
<-
v
:
}
}
}
...
...
commands/http/handler.go
浏览文件 @
23d41e08
...
...
@@ -36,10 +36,14 @@ const (
StreamErrHeader
=
"X-Stream-Error"
streamHeader
=
"X-Stream-Output"
channelHeader
=
"X-Chunked-Output"
uaHeader
=
"User-Agent"
contentTypeHeader
=
"Content-Type"
contentLengthHeader
=
"Content-Length"
contentDispHeader
=
"Content-Disposition"
transferEncodingHeader
=
"Transfer-Encoding"
applicationJson
=
"application/json"
applicationOctetStream
=
"application/octet-stream"
plainText
=
"text/plain"
)
var
mimeTypes
=
map
[
string
]
string
{
...
...
@@ -156,7 +160,7 @@ func sendResponse(w http.ResponseWriter, req cmds.Request, res cmds.Response) {
return
}
status
:=
200
status
:=
http
.
StatusOK
// if response contains an error, write an HTTP error status code
if
e
:=
res
.
Error
();
e
!=
nil
{
if
e
.
Code
==
cmds
.
ErrClient
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论