From 730c4f74dbf7ebc74e43bc9b34cc83e2a644f78b Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Wed, 11 Jul 2018 15:43:04 +0530 Subject: [PATCH] [federation] Fix state API endpoints (#518) * [federation] Fix state API endpoints Signed-off-by: Anant Prakash * Use parseEventIDParam instead --- .../dendrite/federationapi/routing/routing.go | 8 ++--- .../dendrite/federationapi/routing/state.go | 34 +++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index d52f3e63c..2cabea6dc 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -108,24 +108,24 @@ func Setup( }, )).Methods(http.MethodGet) - v1fedmux.Handle("/state/{roomID}/{eventID}", common.MakeFedAPI( + v1fedmux.Handle("/state/{roomID}", common.MakeFedAPI( "federation_get_event_auth", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { vars := mux.Vars(httpReq) return GetState( httpReq.Context(), request, cfg, query, time.Now(), - keys, vars["roomID"], vars["eventID"], + keys, vars["roomID"], ) }, )).Methods(http.MethodGet) - v1fedmux.Handle("/state_ids/{roomID}/{eventID}", common.MakeFedAPI( + v1fedmux.Handle("/state_ids/{roomID}", common.MakeFedAPI( "federation_get_event_auth", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { vars := mux.Vars(httpReq) return GetStateIDs( httpReq.Context(), request, cfg, query, time.Now(), - keys, vars["roomID"], vars["eventID"], + keys, vars["roomID"], ) }, )).Methods(http.MethodGet) diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/state.go b/src/github.com/matrix-org/dendrite/federationapi/routing/state.go index 03046ece1..40db82b61 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/state.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/state.go @@ -15,8 +15,10 @@ package routing import ( "context" "net/http" + "net/url" "time" + "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" @@ -32,8 +34,12 @@ func GetState( _ time.Time, _ gomatrixserverlib.KeyRing, roomID string, - eventID string, ) util.JSONResponse { + eventID, err := parseEventIDParam(request) + if err != nil { + return *err + } + state, err := getState(ctx, request, query, roomID, eventID) if err != nil { return *err @@ -51,8 +57,12 @@ func GetStateIDs( _ time.Time, _ gomatrixserverlib.KeyRing, roomID string, - eventID string, ) util.JSONResponse { + eventID, err := parseEventIDParam(request) + if err != nil { + return *err + } + state, err := getState(ctx, request, query, roomID, eventID) if err != nil { return *err @@ -68,6 +78,26 @@ func GetStateIDs( } } +func parseEventIDParam( + request *gomatrixserverlib.FederationRequest, +) (eventID string, resErr *util.JSONResponse) { + URL, err := url.Parse(request.RequestURI()) + if err != nil { + *resErr = util.ErrorResponse(err) + return + } + + eventID = URL.Query().Get("event_id") + if eventID == "" { + resErr = &util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.MissingArgument("event_id missing"), + } + } + + return +} + func getState( ctx context.Context, request *gomatrixserverlib.FederationRequest,