From 34734779deb48416ee24aebdac2584b8b18b796d Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Thu, 1 Apr 2021 10:53:04 +0800 Subject: [PATCH 1/2] response simple text message for not html request when response 404 Signed-off-by: a1012112796 <1012112796@qq.com> --- modules/context/context.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/modules/context/context.go b/modules/context/context.go index eecc81406d0e0..d732487019f27 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -229,6 +229,29 @@ func (ctx *Context) notFoundInternal(title string, err error) { } } + // response simple meesage if Accept isn't text/html + reqTypes, has := ctx.Req.Header["Accept"] + if has && len(reqTypes) > 0 { + notHTML := true + for _, part := range reqTypes { + for _, typ := range strings.Split(part, ",") { + if typ == "text/html" { + notHTML = false + break + } + } + + if !notHTML { + break + } + } + + if notHTML { + ctx.PlainText(404, []byte("Not found.\n")) + return + } + } + ctx.Data["IsRepo"] = ctx.Repo.Repository != nil ctx.Data["Title"] = "Page Not Found" ctx.HTML(http.StatusNotFound, base.TplName("status/404")) From 595457db73132a49f45e5bb088d6a80c37524386 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Thu, 1 Apr 2021 22:29:29 +0800 Subject: [PATCH 2/2] simple logic --- modules/context/context.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/modules/context/context.go b/modules/context/context.go index d732487019f27..a78403260656b 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -234,14 +234,8 @@ func (ctx *Context) notFoundInternal(title string, err error) { if has && len(reqTypes) > 0 { notHTML := true for _, part := range reqTypes { - for _, typ := range strings.Split(part, ",") { - if typ == "text/html" { - notHTML = false - break - } - } - - if !notHTML { + if strings.Contains(part, "text/html") { + notHTML = false break } }