Fix rooms v3 url paths for good - with tests (#1130)

* Fix rooms v3 url paths for good - with tests

- Add a test rig around `federationapi` to test routing.
- Use `JSONVerifier` over `KeyRing` so we can stub things out more easily.
- Add `test.NopJSONVerifier` which verifies nothing.
- Add `base.BaseMux` which is the original `mux.Router` used to spawn public/internal routers.
- Listen on `base.BaseMux` and not the default serve mux as it cleans paths which we don't want.
- Factor out `ListenAndServe` to `test.ListenAndServe` and add flag for listening on TLS.

* Fix comments

* Linting
This commit is contained in:
Kegsay 2020-06-15 16:57:59 +01:00 committed by GitHub
parent 1aac317341
commit 7c36fb78a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 228 additions and 76 deletions

View file

@ -47,6 +47,7 @@ var (
userID = flag.String("user-id", "@userid:$SERVER_NAME", "The user ID to use as the event sender")
messageCount = flag.Int("message-count", 10, "The number of m.room.messsage events to generate")
format = flag.String("Format", "InputRoomEvent", "The output format to use for the messages: InputRoomEvent or Event")
ver = flag.String("version", string(gomatrixserverlib.RoomVersionV1), "Room version to generate events as")
)
// By default we use a private key of 0.
@ -109,7 +110,7 @@ func buildAndOutput() gomatrixserverlib.EventReference {
event, err := b.Build(
now, name, key, privateKey,
gomatrixserverlib.RoomVersionV1,
gomatrixserverlib.RoomVersion(*ver),
)
if err != nil {
panic(err)
@ -127,7 +128,7 @@ func writeEvent(event gomatrixserverlib.Event) {
if *format == "InputRoomEvent" {
var ire api.InputRoomEvent
ire.Kind = api.KindNew
ire.Event = event.Headered(gomatrixserverlib.RoomVersionV1)
ire.Event = event.Headered(gomatrixserverlib.RoomVersion(*ver))
authEventIDs := []string{}
for _, ref := range b.AuthEvents.([]gomatrixserverlib.EventReference) {
authEventIDs = append(authEventIDs, ref.EventID)

View file

@ -173,7 +173,7 @@ func main() {
monolith.AddAllPublicRoutes(base.Base.PublicAPIMux)
httputil.SetupHTTPAPI(
http.DefaultServeMux,
base.Base.BaseMux,
base.Base.PublicAPIMux,
base.Base.InternalAPIMux,
&cfg,
@ -184,7 +184,7 @@ func main() {
go func() {
httpBindAddr := fmt.Sprintf(":%d", *instancePort)
logrus.Info("Listening on ", httpBindAddr)
logrus.Fatal(http.ListenAndServe(httpBindAddr, nil))
logrus.Fatal(http.ListenAndServe(httpBindAddr, base.Base.BaseMux))
}()
// Expose the matrix APIs also via libp2p
if base.LibP2P != nil {
@ -197,7 +197,7 @@ func main() {
defer func() {
logrus.Fatal(listener.Close())
}()
logrus.Fatal(http.Serve(listener, nil))
logrus.Fatal(http.Serve(listener, base.Base.BaseMux))
}()
}

View file

@ -94,18 +94,6 @@ func createFederationClient(
func main() {
flag.Parse()
// Build both ends of a HTTP multiplex.
httpServer := &http.Server{
Addr: ":0",
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
ReadTimeout: 15 * time.Second,
WriteTimeout: 45 * time.Second,
IdleTimeout: 60 * time.Second,
BaseContext: func(_ net.Listener) context.Context {
return context.Background()
},
}
ygg, err := yggconn.Setup(*instanceName, *instancePeer)
if err != nil {
panic(err)
@ -188,13 +176,26 @@ func main() {
monolith.AddAllPublicRoutes(base.PublicAPIMux)
httputil.SetupHTTPAPI(
http.DefaultServeMux,
base.BaseMux,
base.PublicAPIMux,
base.InternalAPIMux,
cfg,
base.UseHTTPAPIs,
)
// Build both ends of a HTTP multiplex.
httpServer := &http.Server{
Addr: ":0",
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
ReadTimeout: 15 * time.Second,
WriteTimeout: 45 * time.Second,
IdleTimeout: 60 * time.Second,
BaseContext: func(_ net.Listener) context.Context {
return context.Background()
},
Handler: base.BaseMux,
}
go func() {
logrus.Info("Listening on ", ygg.DerivedServerName())
logrus.Fatal(httpServer.Serve(ygg))
@ -202,7 +203,7 @@ func main() {
go func() {
httpBindAddr := fmt.Sprintf("localhost:%d", *instancePort)
logrus.Info("Listening on ", httpBindAddr)
logrus.Fatal(http.ListenAndServe(httpBindAddr, nil))
logrus.Fatal(http.ListenAndServe(httpBindAddr, base.BaseMux))
}()
select {}

View file

@ -135,7 +135,7 @@ func main() {
monolith.AddAllPublicRoutes(base.PublicAPIMux)
httputil.SetupHTTPAPI(
http.DefaultServeMux,
base.BaseMux,
base.PublicAPIMux,
base.InternalAPIMux,
cfg,
@ -147,6 +147,7 @@ func main() {
serv := http.Server{
Addr: *httpBindAddr,
WriteTimeout: setup.HTTPServerTimeout,
Handler: base.BaseMux,
}
logrus.Info("Listening on ", serv.Addr)
@ -158,6 +159,7 @@ func main() {
serv := http.Server{
Addr: *httpsBindAddr,
WriteTimeout: setup.HTTPServerTimeout,
Handler: base.BaseMux,
}
logrus.Info("Listening on ", serv.Addr)

View file

@ -28,7 +28,7 @@ import (
// JSServer exposes an HTTP-like server interface which allows JS to 'send' requests to it.
type JSServer struct {
// The router which will service requests
Mux *http.ServeMux
Mux http.Handler
}
// OnRequestFromJS is the function that JS will invoke when there is a new request.

View file

@ -19,7 +19,6 @@ package main
import (
"crypto/ed25519"
"fmt"
"net/http"
"syscall/js"
"github.com/matrix-org/dendrite/appservice"
@ -233,7 +232,7 @@ func main() {
monolith.AddAllPublicRoutes(base.PublicAPIMux)
httputil.SetupHTTPAPI(
http.DefaultServeMux,
base.BaseMux,
base.PublicAPIMux,
base.InternalAPIMux,
cfg,
@ -245,7 +244,7 @@ func main() {
go func() {
logrus.Info("Listening on libp2p-js host ID ", node.Id)
s := JSServer{
Mux: http.DefaultServeMux,
Mux: base.BaseMux,
}
s.ListenAndServe("p2p")
}()
@ -255,7 +254,7 @@ func main() {
go func() {
logrus.Info("Listening for service-worker fetch traffic")
s := JSServer{
Mux: http.DefaultServeMux,
Mux: base.BaseMux,
}
s.ListenAndServe("fetch")
}()