Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
8286abaf
提交
8286abaf
authored
7月 22, 2015
作者:
Jeromy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
attempt at properly closing http response bodies
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
上级
bd809e66
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
35 行增加
和
5 行删除
+35
-5
client.go
commands/http/client.go
+12
-1
handler.go
commands/http/handler.go
+7
-4
response.go
commands/response.go
+16
-0
没有找到文件。
commands/http/client.go
浏览文件 @
8286abaf
...
...
@@ -102,12 +102,14 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
ec
<-
err
return
}
// using the overridden JSON encoding in request
res
,
err
:=
getResponse
(
httpRes
,
req
)
if
err
!=
nil
{
ec
<-
err
return
}
rc
<-
res
}()
...
...
@@ -179,6 +181,8 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
res
.
SetLength
(
length
)
}
res
.
SetCloser
(
httpRes
.
Body
)
if
len
(
httpRes
.
Header
.
Get
(
streamHeader
))
>
0
{
// if output is a stream, we can just use the body reader
res
.
SetOutput
(
httpRes
.
Body
)
...
...
@@ -202,8 +206,15 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
}
else
{
err
=
dec
.
Decode
(
&
v
)
}
// 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.
if
err
!=
nil
&&
strings
.
Contains
(
err
.
Error
(),
"read on closed response body"
)
{
err
=
io
.
EOF
}
if
err
!=
nil
&&
err
!=
io
.
EOF
{
fmt
.
Println
(
err
.
Error
()
)
log
.
Error
(
err
)
return
}
...
...
commands/http/handler.go
浏览文件 @
8286abaf
...
...
@@ -171,12 +171,15 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(200)
err
=
copyChunks
(
applicationJson
,
w
,
out
)
if
err
!=
nil
{
log
.
Debug
(
err
)
log
.
Debug
(
"copy chunks error: "
,
err
)
}
return
}
flushCopy
(
w
,
out
)
err
=
flushCopy
(
w
,
out
)
if
err
!=
nil
{
log
.
Debug
(
"Flush copy returned an error: "
,
err
)
}
}
func
(
i
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -191,8 +194,8 @@ func flushCopy(w http.ResponseWriter, out io.Reader) error {
return
copyChunks
(
""
,
w
,
out
)
}
io
.
Copy
(
&
flushResponse
{
w
},
out
)
return
nil
_
,
err
:=
io
.
Copy
(
&
flushResponse
{
w
},
out
)
return
err
}
// Copies from an io.Reader to a http.ResponseWriter.
...
...
commands/response.go
浏览文件 @
8286abaf
...
...
@@ -101,6 +101,10 @@ type Response interface {
SetLength
(
uint64
)
Length
()
uint64
// underlying http connections need to be cleaned up, this is for that
Close
()
error
SetCloser
(
io
.
Closer
)
// Marshal marshals out the response into a buffer. It uses the EncodingType
// on the Request to chose a Marshaler (Codec).
Marshal
()
(
io
.
Reader
,
error
)
...
...
@@ -121,6 +125,7 @@ type response struct {
length
uint64
stdout
io
.
Writer
stderr
io
.
Writer
closer
io
.
Closer
}
func
(
r
*
response
)
Request
()
Request
{
...
...
@@ -214,6 +219,17 @@ func (r *response) Reader() (io.Reader, error) {
return
r
.
out
,
nil
}
func
(
r
*
response
)
Close
()
error
{
if
r
.
closer
!=
nil
{
return
r
.
closer
.
Close
()
}
return
nil
}
func
(
r
*
response
)
SetCloser
(
c
io
.
Closer
)
{
r
.
closer
=
c
}
func
(
r
*
response
)
Stdout
()
io
.
Writer
{
return
r
.
stdout
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论