Files
continuwuity/src/service/sending/appservice.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
2.9 KiB
Rust
Raw Normal View History

use std::{fmt::Debug, mem};
2024-03-05 19:48:54 -05:00
2021-04-23 18:45:06 +02:00
use bytes::BytesMut;
use conduwuit::{
Err, Result, debug_error, err, implement, trace, utils, utils::response::LimitReadExt, warn,
};
use ruma::api::{
IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken, appservice::Registration,
};
2024-03-05 19:48:54 -05:00
/// Sends a request to an appservice
///
2024-04-24 01:00:19 -04:00
/// Only returns Ok(None) if there is no url specified in the appservice
/// registration file
2026-01-05 18:09:18 +00:00
#[implement(super::Service)]
pub async fn send_appservice_request<T>(
&self,
registration: Registration,
request: T,
2024-07-18 06:37:47 +00:00
) -> Result<Option<T::IncomingResponse>>
2020-12-08 10:33:44 +01:00
where
2024-06-09 06:10:21 +00:00
T: OutgoingRequest + Debug + Send,
2020-12-08 10:33:44 +01:00
{
2024-08-31 09:55:11 +00:00
const VERSIONS: [MatrixVersion; 1] = [MatrixVersion::V1_7];
2024-06-02 00:22:48 +00:00
2024-04-24 01:00:19 -04:00
let Some(dest) = registration.url else {
2024-04-01 23:30:57 -04:00
return Ok(None);
};
2020-12-08 10:33:44 +01:00
2025-04-03 12:20:10 -04:00
if dest == *"null" || dest.is_empty() {
return Ok(None);
}
2024-04-24 01:00:19 -04:00
trace!("Appservice URL \"{dest}\", Appservice ID: {}", registration.id);
2024-03-22 19:21:51 -04:00
let hs_token = registration.hs_token.as_str();
let mut http_request = request
.try_into_http_request::<BytesMut>(
&dest,
2026-01-05 18:12:20 +00:00
SendAccessToken::Appservice(hs_token),
&VERSIONS,
)
2025-04-03 12:20:10 -04:00
.map_err(|e| {
err!(BadServerResponse(
warn!(appservice = %registration.id, "Failed to find destination {dest}: {e:?}")
))
})?
2024-03-22 19:21:51 -04:00
.map(BytesMut::freeze);
2024-03-22 19:21:51 -04:00
let mut parts = http_request.uri().clone().into_parts();
let old_path_and_query = parts.path_and_query.unwrap().as_str().to_owned();
let symbol = if old_path_and_query.contains('?') { "&" } else { "?" };
2020-12-08 10:33:44 +01:00
2024-03-25 17:05:11 -04:00
parts.path_and_query = Some(
(old_path_and_query + symbol + "access_token=" + hs_token)
.parse()
.unwrap(),
);
2024-03-22 19:21:51 -04:00
*http_request.uri_mut() = parts.try_into().expect("our manipulation is always valid");
2020-12-08 10:33:44 +01:00
let reqwest_request = reqwest::Request::try_from(http_request)?;
2026-01-05 18:09:18 +00:00
let client = &self.services.client.appservice;
2024-07-18 06:37:47 +00:00
let mut response = client.execute(reqwest_request).await.map_err(|e| {
2025-04-03 12:20:10 -04:00
warn!("Could not send request to appservice \"{}\" at {dest}: {e:?}", registration.id);
2024-07-18 06:37:47 +00:00
e
})?;
2024-03-22 19:21:51 -04:00
// reqwest::Response -> http::Response conversion
let status = response.status();
2024-03-25 17:05:11 -04:00
let mut http_response_builder = http::Response::builder()
.status(status)
.version(response.version());
2024-03-22 19:21:51 -04:00
mem::swap(
response.headers_mut(),
2024-03-25 17:05:11 -04:00
http_response_builder
.headers_mut()
.expect("http::response::Builder is usable"),
2024-03-22 19:21:51 -04:00
);
2020-12-08 10:33:44 +01:00
let body = response
.limit_read(
self.server
.config
.max_request_size
.try_into()
.expect("usize fits into u64"),
)
.await?;
2024-03-22 19:21:51 -04:00
if !status.is_success() {
2024-08-01 10:58:27 +00:00
debug_error!("Appservice response bytes: {:?}", utils::string_from_bytes(&body));
2025-04-03 12:20:10 -04:00
return Err!(BadServerResponse(warn!(
2024-04-24 01:00:19 -04:00
"Appservice \"{}\" returned unsuccessful HTTP response {status} at {dest}",
registration.id
2024-08-01 10:58:27 +00:00
)));
2020-12-08 10:33:44 +01:00
}
2024-03-22 19:21:51 -04:00
let response = T::IncomingResponse::try_from_http_response(
2024-03-25 17:05:11 -04:00
http_response_builder
.body(body)
.expect("reqwest body is valid http body"),
2024-03-22 19:21:51 -04:00
);
2024-04-24 01:00:19 -04:00
response.map(Some).map_err(|e| {
2025-04-03 12:20:10 -04:00
err!(BadServerResponse(warn!(
"Appservice \"{}\" returned invalid/malformed response bytes {dest}: {e}",
2024-08-01 10:58:27 +00:00
registration.id
)))
})
2020-12-08 10:33:44 +01:00
}