From cdd67270492043818411d70c697f7b8c48788b22 Mon Sep 17 00:00:00 2001 From: Moritz Marquardt Date: Wed, 17 Mar 2021 01:16:57 +0100 Subject: [PATCH] Fix error page not rendering & make it more beautiful --- 404.html | 22 +++++++++++++--------- certificates.go | 2 ++ handler.go | 15 ++++++++++----- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/404.html b/404.html index 854781d..3b5265c 100644 --- a/404.html +++ b/404.html @@ -3,16 +3,16 @@ - Not found + %status - + - -

You found a bug!

-

Sorry, this page doesn't exist or is otherwise inaccessible (code %status)

- - - Website powered by Codeberg Pages + +

+ You found a bug! +

+
+ Sorry, this page doesn't exist or is inaccessible for other reasons (%status) +
+ + + Static pages made easy - Codeberg Pages diff --git a/certificates.go b/certificates.go index 78e1110..680fea8 100644 --- a/certificates.go +++ b/certificates.go @@ -14,3 +14,5 @@ var tlsConfig = &tls.Config{ PreferServerCipherSuites: true, // TODO: optimize cipher suites, minimum TLS version, etc. } + +// TODO: HSTS header with includeSubdomains & preload for MainDomainSuffix and RawDomain diff --git a/handler.go b/handler.go index 02a1a73..97c38ad 100644 --- a/handler.go +++ b/handler.go @@ -148,7 +148,8 @@ func handler(ctx *fasthttp.RequestCtx) { // with the provided status code. func returnErrorPage(ctx *fasthttp.RequestCtx, code int) { ctx.Response.SetStatusCode(code) - ctx.Response.SetBody(bytes.ReplaceAll(NotFoundPage, []byte("%status"), []byte(strconv.Itoa(code)))) + ctx.Response.Header.SetContentType("text/html; charset=utf-8") + ctx.Response.SetBody(bytes.ReplaceAll(NotFoundPage, []byte("%status"), []byte(strconv.Itoa(code) + " " + fasthttp.StatusMessage(code)))) } // getBranchTimestamp finds the default branch (if branch is "") and returns the last modification time of the branch @@ -186,10 +187,14 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t // Check if the branch exists and when it was modified if options.BranchTimestamp == (time.Time{}) { targetBranch, options.BranchTimestamp = getBranchTimestamp(targetOwner, targetRepo, targetBranch) - if options.BranchTimestamp == (time.Time{}) { - ctx.Response.SetStatusCode(fasthttp.StatusNotFound) - return false - } + } + + // Handle repositories with no/broken pages setup + if options.BranchTimestamp == (time.Time{}) || targetBranch == "" { + ctx.Response.SetStatusCode(fasthttp.StatusNotFound) + ctx.Response.Header.SetContentType("text/html; charset=utf-8") + ctx.Response.SetBody(bytes.ReplaceAll(NotFoundPage, []byte("%status"), []byte("pages not set up for this repo"))) + return true } // Check if the browser has a cached version