From 86bde5ec11c26b58d4428c7c57941d85679a07b1 Mon Sep 17 00:00:00 2001 From: makeworld Date: Thu, 4 Feb 2021 18:06:56 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Handle=20empty=20META=20-=20fixe?= =?UTF-8?q?s=20#176?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + display/handlers.go | 3 ++- renderer/page.go | 19 +++++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e2f706..156e56c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix downloading of pages that are too large or timed out - `about:` URLs can be typed into the bottom bar (#167) - Bookmarks modal closes on ESC like the others (#173) +- Handle empty META string (#176) ## [1.7.2] - 2020-12-21 diff --git a/display/handlers.go b/display/handlers.go index 21e3af3..f4d6f1f 100644 --- a/display/handlers.go +++ b/display/handlers.go @@ -136,7 +136,8 @@ func handleFavicon(t *tab, host, old string) { cache.AddFavicon(host, cache.KnownNoFavicon) return } - if !strings.HasPrefix(res.Meta, "text/") { + if !strings.HasPrefix(res.Meta, "text/") && res.Meta != "" { + // Not a textual page cache.AddFavicon(host, cache.KnownNoFavicon) return } diff --git a/renderer/page.go b/renderer/page.go index 0c43306..8f321ed 100644 --- a/renderer/page.go +++ b/renderer/page.go @@ -24,14 +24,25 @@ var ErrBadMediatype = errors.New("displayable mediatype is not handled in the co // isUTF8 returns true for charsets that are compatible with UTF-8 and don't need to be decoded. func isUTF8(charset string) bool { utfCharsets := []string{"", "utf-8", "us-ascii"} - for i := range utfCharsets { - if strings.ToLower(charset) == utfCharsets[i] { + for _, s := range utfCharsets { + if charset == s || strings.ToLower(charset) == s { return true } } return false } +// getMetaInfo returns the output of mime.ParseMediaType, but handles the empty +// META which is equal to "text/gemini; charset=utf-8" according to the spec. +func decodeMeta(meta string) (string, map[string]string, error) { + if meta == "" { + params := make(map[string]string) + params["charset"] = "utf-8" + return "text/gemini", params, nil + } + return mime.ParseMediaType(meta) +} + // CanDisplay returns true if the response is supported by Amfora // for displaying on the screen. // It also doubles as a function to detect whether something can be stored in a Page struct. @@ -40,7 +51,7 @@ func CanDisplay(res *gemini.Response) bool { // No content return false } - mediatype, params, err := mime.ParseMediaType(res.Meta) + mediatype, params, err := decodeMeta(res.Meta) if err != nil { return false } @@ -82,7 +93,7 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int, proxied b } // Otherwise, the error is EOF, which is what we want. - mediatype, params, _ := mime.ParseMediaType(res.Meta) + mediatype, params, _ := decodeMeta(res.Meta) // Convert content first var utfText string