mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
commands/http: Don't set Content-Type for stream outputs so browsers can MIME-sniff the actual content type
This commit is contained in:

committed by
Juan Batiz-Benet

parent
35983b480a
commit
f1c788d710
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user