Merge pull request #6743 from dreamski21/fix/gateway/content-type-header

fix #2203: omit the charset attribute when Content-Type is text/html
This commit is contained in:
Steven Allen 2019-12-02 14:13:57 -05:00 committed by GitHub
commit c19bc362f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"os"
@ -383,6 +384,26 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam
}
}
ctype := mime.TypeByExtension(gopath.Ext(name))
if ctype == "" {
buf := make([]byte, 512)
n, _ := io.ReadFull(content, buf[:])
ctype = http.DetectContentType(buf[:n])
_, err := content.Seek(0, io.SeekStart)
if err != nil {
http.Error(w, "seeker can't seek", http.StatusInternalServerError)
return
}
}
// Strip the encoding from the HTML Content-Type header and let the
// browser figure it out.
//
// Fixes https://github.com/ipfs/go-ipfs/issues/2203
if strings.HasPrefix(ctype, "text/html;") {
ctype = "text/html"
}
w.Header().Set("Content-Type", ctype)
http.ServeContent(w, req, name, modtime, content)
}