mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -06:00
Make Dendrite spec compliant, fix #2903
This commit is contained in:
parent
a5aa69add5
commit
59eaec4fd8
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
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{
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue