commands/http: Don't set Content-Type for stream outputs so browsers can MIME-sniff the actual content type
Matt Bell committed
Nov 8, 2014 at 21:59 UTC
f1c788d71072c24ea491d1c25bb40238a1522ac9
2 files changed
+9
-3
commands/http/client.go
+1
-1
@@ -131,7 +131,7 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
131
contentType := httpRes.Header["Content-Type"][0]
132
contentType = strings.Split(contentType, ";")[0]
133
134
- if contentType == "application/octet-stream" {
134
+ if len(httpRes.Header.Get(streamHeader)) > 0 {
135
res.SetOutput(httpRes.Body)
136
return res, nil
137
}
commands/http/handler.go
+8
-2
@@ -18,6 +18,8 @@ type Handler struct {
18
19
var ErrNotFound = errors.New("404 page not found")
20
21
+const streamHeader = "X-Stream-Output"
22
+
23
var mimeTypes = map[string]string{
24
cmds.JSON: "application/json",
25
cmds.XML: "application/xml",
@@ -48,8 +50,12 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
50
51
// set the Content-Type based on res output
52
if _, ok := res.Output().(io.Reader); ok {
51
- // TODO: set based on actual Content-Type of file
52
- w.Header().Set("Content-Type", "application/octet-stream")
53
+ // we don't set the Content-Type for streams, so that browsers can MIME-sniff the type themselves
54
+ // we set this header so clients have a way to know this is an output stream
55
+ // (not marshalled command output)
56
+ // TODO: set a specific Content-Type if the command response needs it to be a certain type
57
+ w.Header().Set(streamHeader, "1")
58
+
59
} else {
60
enc, _ := req.Option(cmds.EncShort)
61
encStr, ok := enc.(string)