server/http: Set Content-Type header based on command output
Matt Bell committed
Oct 23, 2014 at 16:26 UTC
0d830e5a2436d55112755080d1084ed1ff13c1eb
1 file changed
+11
server/http/http.go
+11
@@ -103,6 +103,16 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
103
req := cmds.NewRequest(path, opts, nil, nil)
104
res := commands.Root.Call(req)
105
106
+ // set the Content-Type based on res output
107
+ if _, ok := res.Value().(io.Reader); ok {
108
+ // TODO: set based on actual Content-Type of file
109
+ w.Header().Set("Content-Type", "application/octet-stream")
110
+ } else {
111
+ // TODO: get proper MIME type for encoding from multicodec lib
112
+ enc, _ := req.Option(cmds.EncShort)
113
+ w.Header().Set("Content-Type", "application/"+enc.(string))
114
+ }
115
+
116
// if response contains an error, write an HTTP error status code
117
if e := res.Error(); e != nil {
118
if e.Code == cmds.ErrClient {
@@ -115,6 +125,7 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
125
_, err = io.Copy(w, res)
126
if err != nil {
127
w.WriteHeader(http.StatusInternalServerError)
128
+ w.Header().Set("Content-Type", "text/plain")
129
w.Write([]byte(err.Error()))
130
}
131
}