Specify HTTP methods for public APIs (#226)

* Specify HTTP methods for the client API

* Specify HTTP methods for the federation API

* Specify HTTP methods for the media API

* Specify HTTP methods for the sync API
main
Brendan Abolivier 2017-09-13 11:08:11 +01:00 committed by Mark Haines
parent 029e71828a
commit 4fbafc35fe
4 changed files with 34 additions and 34 deletions

View File

@ -64,7 +64,7 @@ func Setup(
}}, }},
} }
}), }),
) ).Methods("GET")
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter() r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
unstableMux := apiMux.PathPrefix(pathPrefixUnstable).Subrouter() unstableMux := apiMux.PathPrefix(pathPrefixUnstable).Subrouter()
@ -73,7 +73,7 @@ func Setup(
common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
return writers.CreateRoom(req, device, cfg, producer, accountDB) return writers.CreateRoom(req, device, cfg, producer, accountDB)
}), }),
) ).Methods("POST", "OPTIONS")
r0mux.Handle("/join/{roomIDOrAlias}", r0mux.Handle("/join/{roomIDOrAlias}",
common.MakeAuthAPI("join", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("join", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
@ -81,7 +81,7 @@ func Setup(
req, device, vars["roomIDOrAlias"], cfg, federation, producer, queryAPI, aliasAPI, keyRing, accountDB, req, device, vars["roomIDOrAlias"], cfg, federation, producer, queryAPI, aliasAPI, keyRing, accountDB,
) )
}), }),
) ).Methods("POST", "OPTIONS")
r0mux.Handle("/rooms/{roomID}/{membership:(?:join|kick|ban|unban|leave|invite)}", r0mux.Handle("/rooms/{roomID}/{membership:(?:join|kick|ban|unban|leave|invite)}",
common.MakeAuthAPI("membership", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("membership", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
@ -93,7 +93,7 @@ func Setup(
vars := mux.Vars(req) vars := mux.Vars(req)
return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], vars["txnID"], nil, cfg, queryAPI, producer) return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], vars["txnID"], nil, cfg, queryAPI, producer)
}), }),
) ).Methods("PUT", "OPTIONS")
r0mux.Handle("/rooms/{roomID}/state/{eventType:[^/]+/?}", r0mux.Handle("/rooms/{roomID}/state/{eventType:[^/]+/?}",
common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
@ -105,18 +105,18 @@ func Setup(
} }
return writers.SendEvent(req, device, vars["roomID"], eventType, "", &emptyString, cfg, queryAPI, producer) return writers.SendEvent(req, device, vars["roomID"], eventType, "", &emptyString, cfg, queryAPI, producer)
}), }),
) ).Methods("PUT", "OPTIONS")
r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}", r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}",
common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
stateKey := vars["stateKey"] stateKey := vars["stateKey"]
return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], "", &stateKey, cfg, queryAPI, producer) return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], "", &stateKey, cfg, queryAPI, producer)
}), }),
) ).Methods("PUT", "OPTIONS")
r0mux.Handle("/register", common.MakeAPI("register", func(req *http.Request) util.JSONResponse { r0mux.Handle("/register", common.MakeAPI("register", func(req *http.Request) util.JSONResponse {
return writers.Register(req, accountDB, deviceDB) return writers.Register(req, accountDB, deviceDB)
})) })).Methods("POST", "OPTIONS")
r0mux.Handle("/directory/room/{roomAlias}", r0mux.Handle("/directory/room/{roomAlias}",
common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
@ -143,7 +143,7 @@ func Setup(
common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
return readers.Logout(req, deviceDB, device) return readers.Logout(req, deviceDB, device)
}), }),
) ).Methods("POST", "OPTIONS")
// Stub endpoints required by Riot // Stub endpoints required by Riot
@ -151,7 +151,7 @@ func Setup(
common.MakeAPI("login", func(req *http.Request) util.JSONResponse { common.MakeAPI("login", func(req *http.Request) util.JSONResponse {
return readers.Login(req, accountDB, deviceDB, cfg) return readers.Login(req, accountDB, deviceDB, cfg)
}), }),
) ).Methods("POST", "OPTIONS")
r0mux.Handle("/pushrules/", r0mux.Handle("/pushrules/",
common.MakeAPI("push_rules", func(req *http.Request) util.JSONResponse { common.MakeAPI("push_rules", func(req *http.Request) util.JSONResponse {
@ -170,7 +170,7 @@ func Setup(
JSON: &res, JSON: &res,
} }
}), }),
) ).Methods("GET")
r0mux.Handle("/user/{userID}/filter", r0mux.Handle("/user/{userID}/filter",
common.MakeAPI("make_filter", func(req *http.Request) util.JSONResponse { common.MakeAPI("make_filter", func(req *http.Request) util.JSONResponse {
@ -180,7 +180,7 @@ func Setup(
JSON: struct{}{}, JSON: struct{}{},
} }
}), }),
) ).Methods("POST", "OPTIONS")
r0mux.Handle("/user/{userID}/filter/{filterID}", r0mux.Handle("/user/{userID}/filter/{filterID}",
common.MakeAPI("filter", func(req *http.Request) util.JSONResponse { common.MakeAPI("filter", func(req *http.Request) util.JSONResponse {
@ -190,7 +190,7 @@ func Setup(
JSON: struct{}{}, JSON: struct{}{},
} }
}), }),
) ).Methods("GET")
// Riot user settings // Riot user settings
@ -199,7 +199,7 @@ func Setup(
vars := mux.Vars(req) vars := mux.Vars(req)
return readers.GetProfile(req, accountDB, vars["userID"]) return readers.GetProfile(req, accountDB, vars["userID"])
}), }),
) ).Methods("GET")
r0mux.Handle("/profile/{userID}/avatar_url", r0mux.Handle("/profile/{userID}/avatar_url",
common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse { common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse {
@ -266,7 +266,7 @@ func Setup(
JSON: struct{}{}, JSON: struct{}{},
} }
}), }),
) ).Methods("PUT", "OPTIONS")
r0mux.Handle("/voip/turnServer", r0mux.Handle("/voip/turnServer",
common.MakeAPI("turn_server", func(req *http.Request) util.JSONResponse { common.MakeAPI("turn_server", func(req *http.Request) util.JSONResponse {
@ -276,7 +276,7 @@ func Setup(
JSON: struct{}{}, JSON: struct{}{},
} }
}), }),
) ).Methods("GET")
unstableMux.Handle("/thirdparty/protocols", unstableMux.Handle("/thirdparty/protocols",
common.MakeAPI("thirdparty_protocols", func(req *http.Request) util.JSONResponse { common.MakeAPI("thirdparty_protocols", func(req *http.Request) util.JSONResponse {
@ -286,7 +286,7 @@ func Setup(
JSON: struct{}{}, JSON: struct{}{},
} }
}), }),
) ).Methods("GET")
r0mux.Handle("/rooms/{roomID}/initialSync", r0mux.Handle("/rooms/{roomID}/initialSync",
common.MakeAPI("rooms_initial_sync", func(req *http.Request) util.JSONResponse { common.MakeAPI("rooms_initial_sync", func(req *http.Request) util.JSONResponse {
@ -296,47 +296,47 @@ func Setup(
JSON: jsonerror.GuestAccessForbidden("Guest access not implemented"), JSON: jsonerror.GuestAccessForbidden("Guest access not implemented"),
} }
}), }),
) ).Methods("GET")
r0mux.Handle("/user/{userID}/account_data/{type}", r0mux.Handle("/user/{userID}/account_data/{type}",
common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
return readers.SaveAccountData(req, accountDB, device, vars["userID"], "", vars["type"], syncProducer) return readers.SaveAccountData(req, accountDB, device, vars["userID"], "", vars["type"], syncProducer)
}), }),
) ).Methods("PUT", "OPTIONS")
r0mux.Handle("/user/{userID}/rooms/{roomID}/account_data/{type}", r0mux.Handle("/user/{userID}/rooms/{roomID}/account_data/{type}",
common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
return readers.SaveAccountData(req, accountDB, device, vars["userID"], vars["roomID"], vars["type"], syncProducer) return readers.SaveAccountData(req, accountDB, device, vars["userID"], vars["roomID"], vars["type"], syncProducer)
}), }),
) ).Methods("PUT", "OPTIONS")
r0mux.Handle("/rooms/{roomID}/members", r0mux.Handle("/rooms/{roomID}/members",
common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
return readers.GetMemberships(req, device, vars["roomID"], false, cfg, queryAPI) return readers.GetMemberships(req, device, vars["roomID"], false, cfg, queryAPI)
}), }),
) ).Methods("GET")
r0mux.Handle("/rooms/{roomID}/joined_members", r0mux.Handle("/rooms/{roomID}/joined_members",
common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
vars := mux.Vars(req) vars := mux.Vars(req)
return readers.GetMemberships(req, device, vars["roomID"], true, cfg, queryAPI) return readers.GetMemberships(req, device, vars["roomID"], true, cfg, queryAPI)
}), }),
) ).Methods("GET")
r0mux.Handle("/rooms/{roomID}/read_markers", r0mux.Handle("/rooms/{roomID}/read_markers",
common.MakeAPI("rooms_read_markers", func(req *http.Request) util.JSONResponse { common.MakeAPI("rooms_read_markers", func(req *http.Request) util.JSONResponse {
// TODO: return the read_markers. // TODO: return the read_markers.
return util.JSONResponse{Code: 200, JSON: struct{}{}} return util.JSONResponse{Code: 200, JSON: struct{}{}}
}), }),
) ).Methods("POST", "OPTIONS")
r0mux.Handle("/rooms/{roomID}/typing/{userID}", r0mux.Handle("/rooms/{roomID}/typing/{userID}",
common.MakeAPI("rooms_typing", func(req *http.Request) util.JSONResponse { common.MakeAPI("rooms_typing", func(req *http.Request) util.JSONResponse {
// TODO: handling typing // TODO: handling typing
return util.JSONResponse{Code: 200, JSON: struct{}{}} return util.JSONResponse{Code: 200, JSON: struct{}{}}
}), }),
) ).Methods("PUT", "OPTIONS")
} }

View File

@ -54,8 +54,8 @@ func Setup(
// return that key. // return that key.
// Even if we had more than one server key, we would probably still ignore the // Even if we had more than one server key, we would probably still ignore the
// {keyID} argument and always return a response containing all of the keys. // {keyID} argument and always return a response containing all of the keys.
v2keysmux.Handle("/server/{keyID}", localKeys) v2keysmux.Handle("/server/{keyID}", localKeys).Methods("GET")
v2keysmux.Handle("/server/", localKeys) v2keysmux.Handle("/server/", localKeys).Methods("GET")
v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI( v1fedmux.Handle("/send/{txnID}/", common.MakeFedAPI(
"federation_send", cfg.Matrix.ServerName, keys, "federation_send", cfg.Matrix.ServerName, keys,
@ -66,7 +66,7 @@ func Setup(
cfg, query, producer, keys, federation, cfg, query, producer, keys, federation,
) )
}, },
)) )).Methods("PUT", "OPTIONS")
v1fedmux.Handle("/invite/{roomID}/{eventID}", common.MakeFedAPI( v1fedmux.Handle("/invite/{roomID}/{eventID}", common.MakeFedAPI(
"federation_invite", cfg.Matrix.ServerName, keys, "federation_invite", cfg.Matrix.ServerName, keys,
@ -77,13 +77,13 @@ func Setup(
cfg, producer, keys, cfg, producer, keys,
) )
}, },
)) )).Methods("PUT", "OPTIONS")
v1fedmux.Handle("/3pid/onbind", common.MakeAPI("3pid_onbind", v1fedmux.Handle("/3pid/onbind", common.MakeAPI("3pid_onbind",
func(req *http.Request) util.JSONResponse { func(req *http.Request) util.JSONResponse {
return writers.CreateInvitesFrom3PIDInvites(req, query, cfg, producer, federation) return writers.CreateInvitesFrom3PIDInvites(req, query, cfg, producer, federation)
}, },
)) )).Methods("POST", "OPTIONS")
v1fedmux.Handle("/exchange_third_party_invite/{roomID}", common.MakeFedAPI( v1fedmux.Handle("/exchange_third_party_invite/{roomID}", common.MakeFedAPI(
"exchange_third_party_invite", cfg.Matrix.ServerName, keys, "exchange_third_party_invite", cfg.Matrix.ServerName, keys,
@ -93,7 +93,7 @@ func Setup(
httpReq, request, vars["roomID"], query, cfg, federation, producer, httpReq, request, vars["roomID"], query, cfg, federation, producer,
) )
}, },
)) )).Methods("PUT", "OPTIONS")
v1fedmux.Handle("/event/{eventID}", common.MakeFedAPI( v1fedmux.Handle("/event/{eventID}", common.MakeFedAPI(
"federation_get_event", cfg.Matrix.ServerName, keys, "federation_get_event", cfg.Matrix.ServerName, keys,
@ -103,5 +103,5 @@ func Setup(
request, cfg, query, time.Now(), keys, vars["eventID"], request, cfg, query, time.Now(), keys, vars["eventID"],
) )
}, },
)) )).Methods("GET")
} }

View File

@ -41,17 +41,17 @@ func Setup(apiMux *mux.Router, cfg *config.Dendrite, db *storage.Database) {
// FIXME: /upload should use common.MakeAuthAPI() // FIXME: /upload should use common.MakeAuthAPI()
r0mux.Handle("/upload", common.MakeAPI("upload", func(req *http.Request) util.JSONResponse { r0mux.Handle("/upload", common.MakeAPI("upload", func(req *http.Request) util.JSONResponse {
return writers.Upload(req, cfg, db, activeThumbnailGeneration) return writers.Upload(req, cfg, db, activeThumbnailGeneration)
})) })).Methods("POST", "OPTIONS")
activeRemoteRequests := &types.ActiveRemoteRequests{ activeRemoteRequests := &types.ActiveRemoteRequests{
MXCToResult: map[string]*types.RemoteRequestResult{}, MXCToResult: map[string]*types.RemoteRequestResult{},
} }
r0mux.Handle("/download/{serverName}/{mediaId}", r0mux.Handle("/download/{serverName}/{mediaId}",
makeDownloadAPI("download", cfg, db, activeRemoteRequests, activeThumbnailGeneration), makeDownloadAPI("download", cfg, db, activeRemoteRequests, activeThumbnailGeneration),
) ).Methods("GET")
r0mux.Handle("/thumbnail/{serverName}/{mediaId}", r0mux.Handle("/thumbnail/{serverName}/{mediaId}",
makeDownloadAPI("thumbnail", cfg, db, activeRemoteRequests, activeThumbnailGeneration), makeDownloadAPI("thumbnail", cfg, db, activeRemoteRequests, activeThumbnailGeneration),
) ).Methods("GET")
} }
func makeDownloadAPI(name string, cfg *config.Dendrite, db *storage.Database, activeRemoteRequests *types.ActiveRemoteRequests, activeThumbnailGeneration *types.ActiveThumbnailGeneration) http.HandlerFunc { func makeDownloadAPI(name string, cfg *config.Dendrite, db *storage.Database, activeRemoteRequests *types.ActiveRemoteRequests, activeThumbnailGeneration *types.ActiveThumbnailGeneration) http.HandlerFunc {

View File

@ -32,5 +32,5 @@ func Setup(apiMux *mux.Router, srp *sync.RequestPool, deviceDB *devices.Database
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter() r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
r0mux.Handle("/sync", common.MakeAuthAPI("sync", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { r0mux.Handle("/sync", common.MakeAuthAPI("sync", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
return srp.OnIncomingSyncRequest(req, device) return srp.OnIncomingSyncRequest(req, device)
})) })).Methods("GET")
} }