1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-06 01:50:26 +00:00

Enforce UTF-8 and set correct MIME types for feeds

This commit is contained in:
Umar Getagazov 2021-09-29 22:02:30 +07:00
parent 33650e965c
commit fedb49fef4

View File

@ -61,26 +61,26 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
}
// genericHandlerOfFeeds is a helper function for the web feed handlers.
func genericHandlerOfFeeds(w http.ResponseWriter, rq *http.Request, f func() (string, error), name string) {
func genericHandlerOfFeeds(w http.ResponseWriter, rq *http.Request, f func() (string, error), name string, contentType string) {
if content, err := f(); err != nil {
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "An error while generating "+name+": "+err.Error())
} else {
w.Header().Set("Content-Type", "application/rss+xml")
w.Header().Set("Content-Type", fmt.Sprintf("%s;charset=utf-8", contentType))
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, content)
}
}
func handlerRecentChangesRSS(w http.ResponseWriter, rq *http.Request) {
genericHandlerOfFeeds(w, rq, history.RecentChangesRSS, "RSS")
genericHandlerOfFeeds(w, rq, history.RecentChangesRSS, "RSS", "application/rss+xml")
}
func handlerRecentChangesAtom(w http.ResponseWriter, rq *http.Request) {
genericHandlerOfFeeds(w, rq, history.RecentChangesAtom, "Atom")
genericHandlerOfFeeds(w, rq, history.RecentChangesAtom, "Atom", "application/atom+xml")
}
func handlerRecentChangesJSON(w http.ResponseWriter, rq *http.Request) {
genericHandlerOfFeeds(w, rq, history.RecentChangesJSON, "JSON feed")
genericHandlerOfFeeds(w, rq, history.RecentChangesJSON, "JSON feed", "application/json")
}