appservice: Improve warp_filter

master
Johannes Becker 2021-06-07 17:24:33 +02:00
parent d6ca3a27bb
commit 4dacef2e2c
1 changed files with 33 additions and 20 deletions

View File

@ -38,12 +38,12 @@ pub async fn run_server(
} }
pub fn warp_filter(appservice: Appservice) -> BoxedFilter<(impl Reply,)> { pub fn warp_filter(appservice: Appservice) -> BoxedFilter<(impl Reply,)> {
// TODO: try to use a struct instead of cloning appservice before `warp::path!` // TODO: try to use a struct instead of needlessly cloning appservice multiple
// matching // times on every request
warp::any() warp::any()
.and(filters::transactions(appservice)) .and(filters::transactions(appservice.clone()))
.or(filters::users()) .or(filters::users(appservice.clone()))
.or(filters::rooms()) .or(filters::rooms(appservice))
.recover(handle_rejection) .recover(handle_rejection)
.boxed() .boxed()
} }
@ -51,7 +51,7 @@ pub fn warp_filter(appservice: Appservice) -> BoxedFilter<(impl Reply,)> {
mod filters { mod filters {
use super::*; use super::*;
pub fn users() -> BoxedFilter<(impl Reply,)> { pub fn users(appservice: Appservice) -> BoxedFilter<(impl Reply,)> {
warp::get() warp::get()
.and( .and(
warp::path!("_matrix" / "app" / "v1" / "users" / String) warp::path!("_matrix" / "app" / "v1" / "users" / String)
@ -59,11 +59,13 @@ mod filters {
.or(warp::path!("users" / String)) .or(warp::path!("users" / String))
.unify(), .unify(),
) )
.and(warp::path::end())
.and(common(appservice))
.and_then(handlers::user) .and_then(handlers::user)
.boxed() .boxed()
} }
pub fn rooms() -> BoxedFilter<(impl Reply,)> { pub fn rooms(appservice: Appservice) -> BoxedFilter<(impl Reply,)> {
warp::get() warp::get()
.and( .and(
warp::path!("_matrix" / "app" / "v1" / "rooms" / String) warp::path!("_matrix" / "app" / "v1" / "rooms" / String)
@ -71,6 +73,8 @@ mod filters {
.or(warp::path!("rooms" / String)) .or(warp::path!("rooms" / String))
.unify(), .unify(),
) )
.and(warp::path::end())
.and(common(appservice))
.and_then(handlers::room) .and_then(handlers::room)
.boxed() .boxed()
} }
@ -83,20 +87,21 @@ mod filters {
.or(warp::path!("transactions" / String)) .or(warp::path!("transactions" / String))
.unify(), .unify(),
) )
.and(filters::valid_access_token(appservice.registration().hs_token.clone())) .and(warp::path::end())
.and(with_appservice(appservice)) .and(common(appservice))
.and(http_request().and_then(|request| async move {
let request = crate::transform_legacy_route(request).map_err(Error::from)?;
Ok::<http::Request<Bytes>, Rejection>(request)
}))
.and_then(handlers::transaction) .and_then(handlers::transaction)
.boxed() .boxed()
} }
pub fn with_appservice( fn common(appservice: Appservice) -> BoxedFilter<(Appservice, http::Request<Bytes>)> {
appservice: Appservice, warp::any()
) -> impl Filter<Extract = (Appservice,), Error = std::convert::Infallible> + Clone { .and(filters::valid_access_token(appservice.registration().hs_token.clone()))
warp::any().map(move || appservice.clone()) .map(move || appservice.clone())
.and(http_request().and_then(|request| async move {
let request = crate::transform_legacy_route(request).map_err(Error::from)?;
Ok::<http::Request<Bytes>, Rejection>(request)
}))
.boxed()
} }
pub fn valid_access_token(token: String) -> BoxedFilter<()> { pub fn valid_access_token(token: String) -> BoxedFilter<()> {
@ -149,16 +154,24 @@ mod filters {
mod handlers { mod handlers {
use super::*; use super::*;
pub async fn user(_: String) -> StdResult<impl warp::Reply, Rejection> { pub async fn user(
_user_id: String,
_appservice: Appservice,
_request: http::Request<Bytes>,
) -> StdResult<impl warp::Reply, Rejection> {
Ok(warp::reply::json(&String::from("{}"))) Ok(warp::reply::json(&String::from("{}")))
} }
pub async fn room(_: String) -> StdResult<impl warp::Reply, Rejection> { pub async fn room(
_room_id: String,
_appservice: Appservice,
_request: http::Request<Bytes>,
) -> StdResult<impl warp::Reply, Rejection> {
Ok(warp::reply::json(&String::from("{}"))) Ok(warp::reply::json(&String::from("{}")))
} }
pub async fn transaction( pub async fn transaction(
_: String, _txn_id: String,
appservice: Appservice, appservice: Appservice,
request: http::Request<Bytes>, request: http::Request<Bytes>,
) -> StdResult<impl warp::Reply, Rejection> { ) -> StdResult<impl warp::Reply, Rejection> {