From aefff4865471471deb8afd3a7cb59875e921b936 Mon Sep 17 00:00:00 2001 From: Djalil Dreamski <32184973+dreamski21@users.noreply.github.com> Date: Sat, 2 Nov 2019 21:56:54 +0100 Subject: [PATCH 1/7] fix #2203: omit the charset attribute when Content-Type is text/html License: MIT Signed-off-by: Abdeldjalil Hebal --- core/corehttp/gateway_handler.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 384f6dc45..51f90fe8f 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -383,6 +383,12 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam } } + mime := http.DetectContentType(content) + if strings.HasPrefix(mime, "text/html;") { + mime = "text/html" + } + w.Header().Set("Content-Type", mime) + http.ServeContent(w, req, name, modtime, content) } From 69f81a11dd23b41a4672722ad65c485400cdb14d Mon Sep 17 00:00:00 2001 From: Djalil Dreamski <32184973+dreamski21@users.noreply.github.com> Date: Tue, 5 Nov 2019 17:36:26 +0100 Subject: [PATCH 2/7] Update gateway_handler.go --- core/corehttp/gateway_handler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 51f90fe8f..540b83347 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -383,7 +383,10 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam } } - mime := http.DetectContentType(content) + buf := make([]byte, 512) + _, _ = content.Read(buf) + mime := http.DetectContentType(buf) + if strings.HasPrefix(mime, "text/html;") { mime = "text/html" } From a29a9dbb98207c8c3312c5d0c0eb53abeb7eec05 Mon Sep 17 00:00:00 2001 From: Djalil Dreamski <32184973+dreamski21@users.noreply.github.com> Date: Wed, 6 Nov 2019 01:52:49 +0100 Subject: [PATCH 3/7] gateway: ServeFile: use file extension to determine Content-Type License: MIT Signed-off-by: Abdeldjalil Hebal --- core/corehttp/gateway_handler.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 540b83347..14973b0d7 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -11,7 +11,8 @@ import ( "runtime/debug" "strings" "time" - + "mime" + "github.com/dustin/go-humanize" "github.com/ipfs/go-cid" files "github.com/ipfs/go-ipfs-files" @@ -383,15 +384,23 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam } } - buf := make([]byte, 512) - _, _ = content.Read(buf) - mime := http.DetectContentType(buf) - - if strings.HasPrefix(mime, "text/html;") { - mime = "text/html" + 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 { + Error(w, "seeker can't seek", http.StatusInternalServerError) + return + } } - w.Header().Set("Content-Type", mime) + if strings.HasPrefix(ctype, "text/html;") { + ctype = "text/html" + } + w.Header().Set("Content-Type", ctype) + _, _ = content.Seek(0, io.SeekStart) http.ServeContent(w, req, name, modtime, content) } From ebf2e7da361e170391c2bef1658a856358e04ffe Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 6 Nov 2019 11:44:32 +0000 Subject: [PATCH 4/7] chore(gateway): fix import ordering --- core/corehttp/gateway_handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 14973b0d7..96aaf5a26 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "mime" "net/http" "net/url" "os" @@ -11,8 +12,7 @@ import ( "runtime/debug" "strings" "time" - "mime" - + "github.com/dustin/go-humanize" "github.com/ipfs/go-cid" files "github.com/ipfs/go-ipfs-files" From a12d2e265e665021ca3a94b4b6c1a28e3293516b Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 6 Nov 2019 11:44:56 +0000 Subject: [PATCH 5/7] chore(gateway): fix error call --- core/corehttp/gateway_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 96aaf5a26..96db4400b 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -391,7 +391,7 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam ctype = http.DetectContentType(buf[:n]) _, err := content.Seek(0, io.SeekStart) if err != nil { - Error(w, "seeker can't seek", http.StatusInternalServerError) + http.Error(w, "seeker can't seek", http.StatusInternalServerError) return } } From 69f6e08d9de44a78fa53154aa01870e66f77002f Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 6 Nov 2019 11:45:41 +0000 Subject: [PATCH 6/7] chore(gateway): remove redundant seek --- core/corehttp/gateway_handler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 96db4400b..07d0c59e1 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -400,7 +400,6 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam } w.Header().Set("Content-Type", ctype) - _, _ = content.Seek(0, io.SeekStart) http.ServeContent(w, req, name, modtime, content) } From 7ae6f6fa3eaaa503049caa07508c44adfe33ff60 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 6 Nov 2019 11:47:27 +0000 Subject: [PATCH 7/7] chore(gateway): document encoding fix --- core/corehttp/gateway_handler.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 07d0c59e1..c67b56ad0 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -395,6 +395,10 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam 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" }