@cryptotaxi247 / kubo / commits / bb9493ac7

content type on command responses default to text

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Oct 28, 2015 at 08:44 UTC bb9493ac7c9cbf1358b0fba5e4355af53f9773cf
1 file changed +13 -7
commands/http/handler.go
+13 -7
@@ -180,7 +180,11 @@ func guessMimeType(res cmds.Response) (string, error) {
180 return "", errors.New("no encoding option set")
181 }
182
183 - return mimeTypes[enc], nil
183 + if m, ok := mimeTypes[enc]; ok {
184 + return m, nil
185 + }
186 +
187 + return mimeTypes[cmds.JSON], nil
188 }
189
190 func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req cmds.Request) {
@@ -217,10 +221,9 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
221 }
222
223 if _, ok := res.Output().(io.Reader); ok {
220 - // we don't set the Content-Type for streams, so that browsers can MIME-sniff the type themselves
221 - // we set this header so clients have a way to know this is an output stream
222 - // (not marshalled command output)
223 - mime = ""
224 + // set streams output type to text to avoid issues with browsers rendering
225 + // html pages on priveleged api ports
226 + mime = "text/plain"
227 h.Set(streamHeader, "1")
228 }
229
@@ -235,9 +238,12 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
238 h.Set(channelHeader, "1")
239 }
240
238 - if mime != "" {
239 - h.Set(contentTypeHeader, mime)
241 + // catch-all, set to text as default
242 + if mime == "" {
243 + mime = "text/plain"
244 }
245 +
246 + h.Set(contentTypeHeader, mime)
247 h.Set(transferEncodingHeader, "chunked")
248
249 if r.Method == "HEAD" { // after all the headers.