Use custom handlers, plus one for HTTP 405 too

This commit is contained in:
Neil Alexander 2022-08-01 10:50:52 +01:00
parent d4e1d1aca7
commit 6e9d22bfa2
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -451,9 +451,23 @@ func (b *BaseDendrite) SetupAndServeHTTP(
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux)
externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.PublicWellKnownAPIMux)
notFoundHandler := httputil.WrapHandlerInCORS(http.NotFoundHandler())
internalRouter.NotFoundHandler = notFoundHandler
externalRouter.NotFoundHandler = notFoundHandler
notFoundHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(fmt.Sprintf("not found: %q", r.RequestURI)))
}
notAllowedHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte(fmt.Sprintf("%s not allowed: %q", r.Method, r.RequestURI)))
}
notFoundCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notFoundHandler))
notAllowedCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler))
internalRouter.NotFoundHandler = notFoundCORSHandler
internalRouter.MethodNotAllowedHandler = notAllowedCORSHandler
externalRouter.NotFoundHandler = notFoundCORSHandler
externalRouter.MethodNotAllowedHandler = notAllowedCORSHandler
if internalAddr != NoListener && internalAddr != externalAddr {
go func() {