From f77fc64a5c570f112aa5b3fceacb2c23be4f5f1c Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 3 Jul 2019 10:44:01 +0100 Subject: [PATCH] Make the helper function name slightly more generic --- clientapi/routing/routing.go | 46 +++++++++++++++---------------- common/routing.go | 16 +++++------ federationapi/routing/routing.go | 26 ++++++++--------- go.mod | 1 - mediaapi/routing/routing.go | 2 +- publicroomsapi/routing/routing.go | 4 +-- syncapi/routing/routing.go | 6 ++-- testfile | 2 -- 8 files changed, 50 insertions(+), 53 deletions(-) diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 8ed694776..8135e49af 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -94,7 +94,7 @@ func Setup( ).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/join/{roomIDOrAlias}", common.MakeAuthAPI("join", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -105,7 +105,7 @@ func Setup( ).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/{membership:(?:join|kick|ban|unban|leave|invite)}", common.MakeAuthAPI("membership", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -114,7 +114,7 @@ func Setup( ).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/send/{eventType}", common.MakeAuthAPI("send_message", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -123,7 +123,7 @@ func Setup( ).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}", common.MakeAuthAPI("send_message", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -134,7 +134,7 @@ func Setup( ).Methods(http.MethodPut, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/state/{eventType:[^/]+/?}", common.MakeAuthAPI("send_message", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -149,7 +149,7 @@ func Setup( ).Methods(http.MethodPut, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}", common.MakeAuthAPI("send_message", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -172,7 +172,7 @@ func Setup( r0mux.Handle("/directory/room/{roomAlias}", common.MakeExternalAPI("directory_room", func(req *http.Request) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -182,7 +182,7 @@ func Setup( r0mux.Handle("/directory/room/{roomAlias}", common.MakeAuthAPI("directory_room", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -192,7 +192,7 @@ func Setup( r0mux.Handle("/directory/room/{roomAlias}", common.MakeAuthAPI("directory_room", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -214,7 +214,7 @@ func Setup( r0mux.Handle("/rooms/{roomID}/typing/{userID}", common.MakeAuthAPI("rooms_typing", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -257,7 +257,7 @@ func Setup( r0mux.Handle("/user/{userId}/filter", common.MakeAuthAPI("put_filter", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -267,7 +267,7 @@ func Setup( r0mux.Handle("/user/{userId}/filter/{filterId}", common.MakeAuthAPI("get_filter", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -279,7 +279,7 @@ func Setup( r0mux.Handle("/profile/{userID}", common.MakeExternalAPI("profile", func(req *http.Request) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -289,7 +289,7 @@ func Setup( r0mux.Handle("/profile/{userID}/avatar_url", common.MakeExternalAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -299,7 +299,7 @@ func Setup( r0mux.Handle("/profile/{userID}/avatar_url", common.MakeAuthAPI("profile_avatar_url", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -311,7 +311,7 @@ func Setup( r0mux.Handle("/profile/{userID}/displayname", common.MakeExternalAPI("profile_displayname", func(req *http.Request) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -321,7 +321,7 @@ func Setup( r0mux.Handle("/profile/{userID}/displayname", common.MakeAuthAPI("profile_displayname", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -394,7 +394,7 @@ func Setup( r0mux.Handle("/user/{userID}/account_data/{type}", common.MakeAuthAPI("user_account_data", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -404,7 +404,7 @@ func Setup( r0mux.Handle("/user/{userID}/rooms/{roomID}/account_data/{type}", common.MakeAuthAPI("user_account_data", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -414,7 +414,7 @@ func Setup( r0mux.Handle("/rooms/{roomID}/members", common.MakeAuthAPI("rooms_members", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -424,7 +424,7 @@ func Setup( r0mux.Handle("/rooms/{roomID}/joined_members", common.MakeAuthAPI("rooms_members", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -447,7 +447,7 @@ func Setup( r0mux.Handle("/devices/{deviceID}", common.MakeAuthAPI("get_device", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -457,7 +457,7 @@ func Setup( r0mux.Handle("/devices/{deviceID}", common.MakeAuthAPI("device_data", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } diff --git a/common/routing.go b/common/routing.go index ec56e1f46..330912cde 100644 --- a/common/routing.go +++ b/common/routing.go @@ -18,18 +18,18 @@ import ( "net/url" ) -// URLDecodeVarMap is a function that iterates through each of the items in a -// map, URL decodes the values, and returns a new map with the decoded values +// URLDecodeMapValues is a function that iterates through each of the items in a +// map, URL decodes the value, and returns a new map with the decoded values // under the same key names -func URLDecodeVarMap(vars map[string]string) (map[string]string, error) { - decodedVars := make(map[string]string, len(vars)) - for key, value := range vars { - decoded, err := url.QueryUnescape(value) +func URLDecodeMapValues(vmap map[string]string) (map[string]string, error) { + decoded := make(map[string]string, len(vmap)) + for key, value := range vmap { + decodedVal, err := url.QueryUnescape(value) if err != nil { return make(map[string]string), err } - decodedVars[key] = decoded + decoded[key] = decodedVal } - return decodedVars, nil + return decoded, nil } diff --git a/federationapi/routing/routing.go b/federationapi/routing/routing.go index ff3eaf22f..16704e0b2 100644 --- a/federationapi/routing/routing.go +++ b/federationapi/routing/routing.go @@ -68,7 +68,7 @@ func Setup( v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI( "federation_send", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -82,7 +82,7 @@ func Setup( v1fedmux.Handle("/invite/{roomID}/{eventID}", common.MakeFedAPI( "federation_invite", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -102,7 +102,7 @@ func Setup( v1fedmux.Handle("/exchange_third_party_invite/{roomID}", common.MakeFedAPI( "exchange_third_party_invite", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -115,7 +115,7 @@ func Setup( v1fedmux.Handle("/event/{eventID}", common.MakeFedAPI( "federation_get_event", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -128,7 +128,7 @@ func Setup( v1fedmux.Handle("/state/{roomID}", common.MakeFedAPI( "federation_get_event_auth", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -141,7 +141,7 @@ func Setup( 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, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -172,7 +172,7 @@ func Setup( v1fedmux.Handle("/user/devices/{userID}", common.MakeFedAPI( "federation_user_devices", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -185,7 +185,7 @@ func Setup( v1fedmux.Handle("/make_join/{roomID}/{userID}", common.MakeFedAPI( "federation_make_join", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -200,7 +200,7 @@ func Setup( v1fedmux.Handle("/send_join/{roomID}/{userID}", common.MakeFedAPI( "federation_send_join", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -215,7 +215,7 @@ func Setup( v1fedmux.Handle("/make_leave/{roomID}/{userID}", common.MakeFedAPI( "federation_make_leave", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -230,7 +230,7 @@ func Setup( v1fedmux.Handle("/send_leave/{roomID}/{userID}", common.MakeFedAPI( "federation_send_leave", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -252,7 +252,7 @@ func Setup( v1fedmux.Handle("/get_missing_events/{roomID}", common.MakeFedAPI( "federation_get_missing_events", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } @@ -263,7 +263,7 @@ func Setup( v1fedmux.Handle("/backfill/{roomID}/", common.MakeFedAPI( "federation_backfill", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(httpReq)) + vars, err := common.URLDecodeMapValues(mux.Vars(httpReq)) if err != nil { return util.ErrorResponse(err) } diff --git a/go.mod b/go.mod index 597b94ee8..072d9ef30 100644 --- a/go.mod +++ b/go.mod @@ -63,4 +63,3 @@ require ( gopkg.in/macaroon.v2 v2.1.0 gopkg.in/yaml.v2 v2.2.2 ) - diff --git a/mediaapi/routing/routing.go b/mediaapi/routing/routing.go index 5324dcba4..5bcce1772 100644 --- a/mediaapi/routing/routing.go +++ b/mediaapi/routing/routing.go @@ -91,7 +91,7 @@ func makeDownloadAPI( // Content-Type will be overridden in case of returning file data, else we respond with JSON-formatted errors w.Header().Set("Content-Type", "application/json") - vars, _ := common.URLDecodeVarMap(mux.Vars(req)) + vars, _ := common.URLDecodeMapValues(mux.Vars(req)) Download( w, req, diff --git a/publicroomsapi/routing/routing.go b/publicroomsapi/routing/routing.go index 66aac8f0a..3a1c9eb58 100644 --- a/publicroomsapi/routing/routing.go +++ b/publicroomsapi/routing/routing.go @@ -45,7 +45,7 @@ func Setup(apiMux *mux.Router, deviceDB *devices.Database, publicRoomsDB *storag r0mux.Handle("/directory/list/room/{roomID}", common.MakeExternalAPI("directory_list", func(req *http.Request) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -55,7 +55,7 @@ func Setup(apiMux *mux.Router, deviceDB *devices.Database, publicRoomsDB *storag // TODO: Add AS support r0mux.Handle("/directory/list/room/{roomID}", common.MakeAuthAPI("directory_list", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } diff --git a/syncapi/routing/routing.go b/syncapi/routing/routing.go index 66aa022e9..cbdcfb6bb 100644 --- a/syncapi/routing/routing.go +++ b/syncapi/routing/routing.go @@ -49,7 +49,7 @@ func Setup(apiMux *mux.Router, srp *sync.RequestPool, syncDB *storage.SyncServer })).Methods(http.MethodGet, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/state", common.MakeAuthAPI("room_state", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -57,7 +57,7 @@ func Setup(apiMux *mux.Router, srp *sync.RequestPool, syncDB *storage.SyncServer })).Methods(http.MethodGet, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/state/{type}", common.MakeAuthAPI("room_state", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } @@ -65,7 +65,7 @@ func Setup(apiMux *mux.Router, srp *sync.RequestPool, syncDB *storage.SyncServer })).Methods(http.MethodGet, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/state/{type}/{stateKey}", common.MakeAuthAPI("room_state", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - vars, err := common.URLDecodeVarMap(mux.Vars(req)) + vars, err := common.URLDecodeMapValues(mux.Vars(req)) if err != nil { return util.ErrorResponse(err) } diff --git a/testfile b/testfile index 6e2fb4edf..ea6fc9172 100644 --- a/testfile +++ b/testfile @@ -142,5 +142,3 @@ Trying to get push rules with unknown rule_id fails with 404 Events come down the correct room local user can join room with version 5 User can invite local user to room with version 5 -Should reject keys claiming to belong to a different user -Enabling an unknown default rule fails with 404