Skip to content

Commit

Permalink
serverx: Support html, text, json in DefaultNotFoundHandler (#42)
Browse files Browse the repository at this point in the history
Signed-off-by: aeneasr <aeneas@ory.sh>
  • Loading branch information
aeneasr authored Apr 21, 2019
1 parent 1c744b9 commit ffd7d71
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions serverx/404.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
package serverx

import "net/http"
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 r.Header.Get("Content-type") == "application/json" {
_, _ = w.Write([]byte(`{"error": "The requested route does not exist"}`)) // #nosec
if matchesContentTypes(r, []string{
"text/html",
"application/xhtml+xml",
"application/xml",
}) {
_, _ = w.Write([]byte(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404 - Route not found</title>
<style>*{transition:all .6s}html{height:100%}body{font-family:sans-serif;color:#888;margin:0}#main{display:table;width:100%;height:100vh;text-align:center}.fof{display:table-cell;vertical-align:middle}.fof h1{font-size:50px;display:inline-block;padding-right:12px;margin-bottom:12px;animation:type .5s alternate infinite}@keyframes type{from{box-shadow:inset -3px 0 0 #888}to{box-shadow:inset -3px 0 0 transparent}}</style>
</head>
<body translate="no">
<div id="main">
<div class="fof">
<h1>Error 404</h1>
<p>The requested route does not exist. Make sure you are using the right path, domain, and port.</p>
</div>
</div>
</body>
</html>`)) // #nosec
return
} else if matchesContentTypes(r, []string{
"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.Write([]byte(`The request route does not exist`)) // #nosec
_, _ = w.Write([]byte(`{"error": "Error 404 - The requested route does not exist. Make sure you are using the right path, domain, and port."}`)) // #nosec
})

0 comments on commit ffd7d71

Please sign in to comment.