]> source.dussan.org Git - gitea.git/commitdiff
response simple text message for not html request when 404 (#15229)
authora1012112796 <1012112796@qq.com>
Thu, 1 Apr 2021 15:11:42 +0000 (23:11 +0800)
committerGitHub <noreply@github.com>
Thu, 1 Apr 2021 15:11:42 +0000 (17:11 +0200)
* response simple text message for not html request when response 404

Signed-off-by: a1012112796 <1012112796@qq.com>
modules/context/context.go

index eecc81406d0e077673d04e19c6a39babe1623726..a78403260656bf6797a2ae8daa54b59771833625 100644 (file)
@@ -229,6 +229,23 @@ 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 {
+                       if strings.Contains(part, "text/html") {
+                               notHTML = false
+                               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"))