From f1c788d71072c24ea491d1c25bb40238a1522ac9 Mon Sep 17 00:00:00 2001
From: Matt Bell <mappum@gmail.com>
Date: Sat, 8 Nov 2014 21:59:26 -0800
Subject: [PATCH] commands/http: Don't set Content-Type for stream outputs so
 browsers can MIME-sniff the actual content type

---
 commands/http/client.go  |  2 +-
 commands/http/handler.go | 10 ++++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/commands/http/client.go b/commands/http/client.go
index 748f50365..9e8c1d3a7 100644
--- a/commands/http/client.go
+++ b/commands/http/client.go
@@ -131,7 +131,7 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
 	contentType := httpRes.Header["Content-Type"][0]
 	contentType = strings.Split(contentType, ";")[0]
 
-	if contentType == "application/octet-stream" {
+	if len(httpRes.Header.Get(streamHeader)) > 0 {
 		res.SetOutput(httpRes.Body)
 		return res, nil
 	}
diff --git a/commands/http/handler.go b/commands/http/handler.go
index e5959b4a8..a8fec766e 100644
--- a/commands/http/handler.go
+++ b/commands/http/handler.go
@@ -18,6 +18,8 @@ type Handler struct {
 
 var ErrNotFound = errors.New("404 page not found")
 
+const streamHeader = "X-Stream-Output"
+
 var mimeTypes = map[string]string{
 	cmds.JSON: "application/json",
 	cmds.XML:  "application/xml",
@@ -48,8 +50,12 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 	// set the Content-Type based on res output
 	if _, ok := res.Output().(io.Reader); ok {
-		// TODO: set based on actual Content-Type of file
-		w.Header().Set("Content-Type", "application/octet-stream")
+		// we don't set the Content-Type for streams, so that browsers can MIME-sniff the type themselves
+		// we set this header so clients have a way to know this is an output stream
+		// (not marshalled command output)
+		// TODO: set a specific Content-Type if the command response needs it to be a certain type
+		w.Header().Set(streamHeader, "1")
+
 	} else {
 		enc, _ := req.Option(cmds.EncShort)
 		encStr, ok := enc.(string)
-- 
GitLab