Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#6743 from dreamski21/fix/gateway/content…
Browse files Browse the repository at this point in the history
…-type-header

fix #2203: omit the charset attribute when Content-Type is text/html

This commit was moved from ipfs/kubo@c19bc36
  • Loading branch information
Stebalien authored Dec 2, 2019
2 parents 52cdcc7 + 3d40d6b commit 224fb08
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 224fb08

Please sign in to comment.