From fc96bd8a5d8402d7d6bca674a2b376dbc36640e1 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Mon, 1 Jan 2024 01:20:50 +0100 Subject: [PATCH] fix: CSS files now have the correct MIME type --- pages.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pages.go b/pages.go index 443fb51..cde4626 100644 --- a/pages.go +++ b/pages.go @@ -43,9 +43,9 @@ func serveFile(username, reponame, path, giteaUrl string, w http.ResponseWriter) var mimeType string var err error if found { - log.Debugf("Returning %s from cache", path) content = entry.(PageContentCache).Content mimeType = entry.(PageContentCache).mimeType + log.Debugf("Found page in cache with mimeType '%s'", mimeType) } // We have to do the raw request manually because the Gitea SDK does not allow @@ -90,6 +90,13 @@ func serveFile(username, reponame, path, giteaUrl string, w http.ResponseWriter) return } + // Handle 404s early. + // TODO: Maybe allow delivering a 404.html instead? + if resp.StatusCode == 404 { + w.WriteHeader(404) + return + } + content, err = io.ReadAll(resp.Body) if err != nil { log.Errorf("Failed to get file %s/%s/%s (%s)", username, reponame, path, err) @@ -100,6 +107,7 @@ func serveFile(username, reponame, path, giteaUrl string, w http.ResponseWriter) pathParts := strings.Split(path, ".") ext := pathParts[len(pathParts)-1] mimeType = mime.TypeByExtension("." + ext) + log.Debugf("Returning MIME type '%s' for extension '%s", mimeType, ext) now := time.Now() pageCache.Set( @@ -113,7 +121,7 @@ func serveFile(username, reponame, path, giteaUrl string, w http.ResponseWriter) ) log.Debugf("Page %s requested from Gitea and cached in memory at %v", path, now) - w.WriteHeader(200) w.Header().Set("Content-Type", mimeType) + w.WriteHeader(200) w.Write(content) }