Skip to content

Commit

Permalink
response simple text message for not html request when 404 (#15229)
Browse files Browse the repository at this point in the history
* response simple text message for not html request when response 404

Signed-off-by: a1012112796 <1012112796@qq.com>
  • Loading branch information
a1012112796 authored Apr 1, 2021
1 parent 9b316a3 commit 43fb492
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 43fb492

Please sign in to comment.