From 0d830e5a2436d55112755080d1084ed1ff13c1eb Mon Sep 17 00:00:00 2001 From: Matt Bell <mappum@gmail.com> Date: Thu, 23 Oct 2014 16:26:40 -0700 Subject: [PATCH] server/http: Set Content-Type header based on command output --- server/http/http.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/server/http/http.go b/server/http/http.go index dbf8b473b..616b09325 100644 --- a/server/http/http.go +++ b/server/http/http.go @@ -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())) } } -- GitLab