Skip to content

Commit 43fb492

Browse files
authored
response simple text message for not html request when 404 (#15229)
* response simple text message for not html request when response 404 Signed-off-by: a1012112796 <[email protected]>
1 parent 9b316a3 commit 43fb492

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

modules/context/context.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ func (ctx *Context) notFoundInternal(title string, err error) {
229229
}
230230
}
231231

232+
// response simple meesage if Accept isn't text/html
233+
reqTypes, has := ctx.Req.Header["Accept"]
234+
if has && len(reqTypes) > 0 {
235+
notHTML := true
236+
for _, part := range reqTypes {
237+
if strings.Contains(part, "text/html") {
238+
notHTML = false
239+
break
240+
}
241+
}
242+
243+
if notHTML {
244+
ctx.PlainText(404, []byte("Not found.\n"))
245+
return
246+
}
247+
}
248+
232249
ctx.Data["IsRepo"] = ctx.Repo.Repository != nil
233250
ctx.Data["Title"] = "Page Not Found"
234251
ctx.HTML(http.StatusNotFound, base.TplName("status/404"))

0 commit comments

Comments
 (0)