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.

97 lines
2.8 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 ruma::api::{appservice::Registration, IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken};
2024-04-24 01:00:19 -04:00
use tracing::{trace, warn};
2020-12-08 10:33:44 +01:00
2024-04-24 01:00:19 -04:00
use crate::{debug_error, services, utils, Error, Result};
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
pub(crate) async fn send_request<T>(registration: Registration, request: T) -> 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-06-02 00:22:48 +00:00
const VERSIONS: [MatrixVersion; 1] = [MatrixVersion::V1_0];
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
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
2024-04-24 01:00:19 -04:00
.try_into_http_request::<BytesMut>(&dest, SendAccessToken::IfRequired(hs_token), &VERSIONS)
2024-03-22 19:21:51 -04:00
.map_err(|e| {
2024-04-24 01:00:19 -04:00
warn!("Failed to find destination {dest}: {e}");
Error::BadServerResponse("Invalid appservice destination")
})?
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)?;
2024-04-12 19:50:30 -04:00
let mut response = services()
2024-03-25 17:05:11 -04:00
.globals
.client
.appservice
.execute(reqwest_request)
.await
2024-04-12 19:50:30 -04:00
.map_err(|e| {
2024-04-24 01:00:19 -04:00
warn!("Could not send request to appservice \"{}\" at {dest}: {e}", registration.id);
2024-04-12 19:50:30 -04: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
2024-04-24 01:00:19 -04:00
let body = response.bytes().await?; // TODO: handle timeout
2024-03-22 19:21:51 -04:00
if !status.is_success() {
warn!(
2024-04-24 01:00:19 -04:00
"Appservice \"{}\" returned unsuccessful HTTP response {status} at {dest}",
registration.id
2021-04-23 18:38:20 +02:00
);
2024-04-24 01:00:19 -04:00
debug_error!("Appservice response bytes: {:?}", utils::string_from_bytes(&body));
return Err(Error::BadServerResponse("Appservice returned unsuccessful HTTP response"));
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| {
warn!("Appservice \"{}\" returned invalid response bytes {dest}: {e}", registration.id);
Error::BadServerResponse("Appservice returned bad/invalid response")
})
2020-12-08 10:33:44 +01:00
}