From 416c606e6da7b67338e109f81840809b49bc1161 Mon Sep 17 00:00:00 2001 From: hugmouse Date: Fri, 5 Mar 2021 20:20:51 +0800 Subject: [PATCH] Sending a 404 status if there is no content on the page Probably a hack. I'm not sure how it should be done normally, but I have no other idea how to do it. --- http_readers.go | 21 +++++++++++++++------ util/util.go | 7 +++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/http_readers.go b/http_readers.go index a75b682..807d478 100644 --- a/http_readers.go +++ b/http_readers.go @@ -112,10 +112,19 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) { contents = views.AttachmentHTML(h) + contents } } - util.HTTP200Page(w, - views.BaseHTML( - util.BeautifulName(hyphaName), - views.HyphaHTML(rq, h, contents), - u, - openGraph)) + if contents == "" { + util.HTTP404Page(w, + views.BaseHTML( + util.BeautifulName(hyphaName), + views.HyphaHTML(rq, h, contents), + u, + openGraph)) + } else { + util.HTTP200Page(w, + views.BaseHTML( + util.BeautifulName(hyphaName), + views.HyphaHTML(rq, h, contents), + u, + openGraph)) + } } diff --git a/util/util.go b/util/util.go index 6b7590f..4dbae26 100644 --- a/util/util.go +++ b/util/util.go @@ -53,6 +53,13 @@ func ShorterPath(path string) string { return path } +// HTTP404Page writes a 404 error in the status, needed when no content is found on the page. +func HTTP404Page(w http.ResponseWriter, page string) { + w.Header().Set("Content-Type", "text/html;charset=utf-8") + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(page)) +} + // HTTP200Page wraps some frequently used things for successful 200 responses. func HTTP200Page(w http.ResponseWriter, page string) { w.Header().Set("Content-Type", "text/html;charset=utf-8")