package serverx import ( "mime" "net/http" "strings" "github.com/ory/x/stringslice" "github.com/ory/x/stringsx" ) func matchesContentTypes(r *http.Request, mimetypes []string) bool { contentType := stringsx.Coalesce(r.Header.Get("Content-type"), "application/octet-stream") for _, v := range strings.Split(contentType, ",") { t, _, err := mime.ParseMediaType(v) if err != nil { break } if stringslice.Has(mimetypes, t) { return true } } return false } // DefaultNotFoundHandler is a default handler for handling 404 errors. var DefaultNotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) if matchesContentTypes(r, []string{ "text/html", "application/xhtml+xml", "application/xml", }) { w.Header().Set("Content-Type", "text/html") _, _ = w.Write([]byte(` 404 - Route not found

Error 404

The requested route does not exist. Make sure you are using the right path, domain, and port.

`)) // #nosec return } else if matchesContentTypes(r, []string{ "text/plain", }) { w.Header().Set("Content-Type", "text/plain") _, _ = w.Write([]byte(`Error 404 - The requested route does not exist. Make sure you are using the right path, domain, and port.`)) // #nosec return } w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"error": "Error 404 - The requested route does not exist. Make sure you are using the right path, domain, and port."}`)) // #nosec })