Commit 0d830e5a authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

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

parent 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 is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment