提交 0d830e5a 作者: Matt Bell 提交者: Juan Batiz-Benet

server/http: Set Content-Type header based on command output

上级 12b0ebff
......@@ -103,6 +103,16 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req := cmds.NewRequest(path, opts, nil, nil)
res := commands.Root.Call(req)
// set the Content-Type based on res output
if _, ok := res.Value().(io.Reader); ok {
// TODO: set based on actual Content-Type of file
w.Header().Set("Content-Type", "application/octet-stream")
} else {
// TODO: get proper MIME type for encoding from multicodec lib
enc, _ := req.Option(cmds.EncShort)
w.Header().Set("Content-Type", "application/"+enc.(string))
}
// if response contains an error, write an HTTP error status code
if e := res.Error(); e != nil {
if e.Code == cmds.ErrClient {
......@@ -115,6 +125,7 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, err = io.Copy(w, res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(err.Error()))
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论