appservice: Rely on cfg-toggle in send_request

master
Johannes Becker 2021-05-10 07:56:00 +02:00
parent eece920953
commit 3b24d33822
2 changed files with 77 additions and 55 deletions

View File

@ -405,6 +405,7 @@ pub struct RequestConfig {
pub(crate) retry_limit: Option<u64>, pub(crate) retry_limit: Option<u64>,
pub(crate) retry_timeout: Option<Duration>, pub(crate) retry_timeout: Option<Duration>,
pub(crate) force_auth: bool, pub(crate) force_auth: bool,
#[cfg(feature = "appservice")]
pub(crate) assert_identity: bool, pub(crate) assert_identity: bool,
} }
@ -427,6 +428,7 @@ impl Default for RequestConfig {
retry_limit: Default::default(), retry_limit: Default::default(),
retry_timeout: Default::default(), retry_timeout: Default::default(),
force_auth: false, force_auth: false,
#[cfg(feature = "appservice")]
assert_identity: false, assert_identity: false,
} }
} }

View File

@ -115,7 +115,26 @@ impl HttpClient {
None => self.request_config, None => self.request_config,
}; };
#[cfg(not(feature = "appservice"))]
let request = self.try_into_http_request(request, session, config).await?;
#[cfg(feature = "appservice")]
let request = if !self.request_config.assert_identity { let request = if !self.request_config.assert_identity {
self.try_into_http_request(request, session, config).await?
} else {
self.try_into_http_request_with_identy_assertion(request, session, config)
.await?
};
self.inner.send_request(request, config).await
}
async fn try_into_http_request<Request: OutgoingRequest>(
&self,
request: Request,
session: Arc<RwLock<Option<Session>>>,
config: RequestConfig,
) -> Result<http::Request<Bytes>, HttpError> {
let read_guard; let read_guard;
let access_token = if config.force_auth { let access_token = if config.force_auth {
read_guard = session.read().await; read_guard = session.read().await;
@ -140,17 +159,20 @@ impl HttpClient {
} }
}; };
request let http_request = request
.try_into_http_request::<BytesMut>(&self.homeserver.to_string(), access_token)? .try_into_http_request::<BytesMut>(&self.homeserver.to_string(), access_token)?
.map(|body| body.freeze()) .map(|body| body.freeze());
} else {
// this should be unreachable since assert_identity on request_config can only be set Ok(http_request)
// with the appservice feature active }
#[cfg(not(feature = "appservice"))]
return Err(HttpError::NeedsAppserviceFeature);
#[cfg(feature = "appservice")] #[cfg(feature = "appservice")]
{ async fn try_into_http_request_with_identy_assertion<Request: OutgoingRequest>(
&self,
request: Request,
session: Arc<RwLock<Option<Session>>>,
_: RequestConfig,
) -> Result<http::Request<Bytes>, HttpError> {
let read_guard = session.read().await; let read_guard = session.read().await;
let access_token = if let Some(session) = read_guard.as_ref() { let access_token = if let Some(session) = read_guard.as_ref() {
SendAccessToken::Always(session.access_token.as_str()) SendAccessToken::Always(session.access_token.as_str())
@ -164,17 +186,15 @@ impl HttpClient {
return Err(HttpError::UserIdRequired); return Err(HttpError::UserIdRequired);
}; };
request let http_request = request
.try_into_http_request_with_user_id::<BytesMut>( .try_into_http_request_with_user_id::<BytesMut>(
&self.homeserver.to_string(), &self.homeserver.to_string(),
access_token, access_token,
user_id, user_id,
)? )?
.map(|body| body.freeze()) .map(|body| body.freeze());
}
};
self.inner.send_request(request, config).await Ok(http_request)
} }
pub async fn upload( pub async fn upload(