mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 02:47:48 +08:00
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:
commit
c19bc362f5
@ -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)
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user