Make Dendrite spec compliant, fix #2903

This commit is contained in:
Till Faelligen 2023-03-16 12:43:40 +01:00
parent a5aa69add5
commit 59eaec4fd8
No known key found for this signature in database
GPG key ID: ACCDC9606D472758
2 changed files with 20 additions and 24 deletions

View file

@ -15,7 +15,6 @@
package httputil
import (
"fmt"
"net/http"
"net/url"
@ -64,31 +63,25 @@ func NewRouters() Routers {
return r
}
var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell
}))
var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell
}))
func (r *Routers) configureHTTPErrors() {
notAllowedHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte(fmt.Sprintf("405 %s not allowed on this endpoint", r.Method)))
}
clientNotFoundHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell
}
notFoundCORSHandler := WrapHandlerInCORS(http.NotFoundHandler())
notAllowedCORSHandler := WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler))
for _, router := range []*mux.Router{
r.Media, r.DendriteAdmin,
r.SynapseAdmin, r.WellKnown,
r.Static,
r.Client, r.Federation, r.Keys,
r.Media, r.WellKnown, r.Static,
r.DendriteAdmin, r.SynapseAdmin,
} {
router.NotFoundHandler = notFoundCORSHandler
router.MethodNotAllowedHandler = notAllowedCORSHandler
router.NotFoundHandler = NotFoundCORSHandler
router.MethodNotAllowedHandler = NotAllowedHandler
}
// Special case so that we don't upset clients on the CS API.
r.Client.NotFoundHandler = http.HandlerFunc(clientNotFoundHandler)
r.Client.MethodNotAllowedHandler = http.HandlerFunc(clientNotFoundHandler)
}

View file

@ -353,6 +353,9 @@ func (b *BaseDendrite) SetupAndServeHTTP(
b.startupLock.Unlock()
externalRouter.NotFoundHandler = httputil.NotFoundCORSHandler
externalRouter.MethodNotAllowedHandler = httputil.NotAllowedHandler
if externalHTTPAddr.Enabled() {
go func() {
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once