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 package httputil
import ( import (
"fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -64,31 +63,25 @@ func NewRouters() Routers {
return r return r
} }
func (r *Routers) configureHTTPErrors() { var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
notAllowedHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte(fmt.Sprintf("405 %s not allowed on this endpoint", r.Method))) w.Header().Set("Content-Type", "application/json")
} _, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell
}))
clientNotFoundHandler := func(w http.ResponseWriter, r *http.Request) { var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell _, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell
} }))
notFoundCORSHandler := WrapHandlerInCORS(http.NotFoundHandler())
notAllowedCORSHandler := WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler))
func (r *Routers) configureHTTPErrors() {
for _, router := range []*mux.Router{ for _, router := range []*mux.Router{
r.Media, r.DendriteAdmin, r.Client, r.Federation, r.Keys,
r.SynapseAdmin, r.WellKnown, r.Media, r.WellKnown, r.Static,
r.Static, r.DendriteAdmin, r.SynapseAdmin,
} { } {
router.NotFoundHandler = notFoundCORSHandler router.NotFoundHandler = NotFoundCORSHandler
router.MethodNotAllowedHandler = notAllowedCORSHandler 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() b.startupLock.Unlock()
externalRouter.NotFoundHandler = httputil.NotFoundCORSHandler
externalRouter.MethodNotAllowedHandler = httputil.NotAllowedHandler
if externalHTTPAddr.Enabled() { if externalHTTPAddr.Enabled() {
go func() { go func() {
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once