2022-02-03 12:24:04 +00:00
|
|
|
use std::{fmt::Debug, mem, time::Duration};
|
2024-03-06 00:48:54 +00:00
|
|
|
|
|
|
|
use bytes::BytesMut;
|
|
|
|
use ruma::api::{appservice::Registration, IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken};
|
2021-07-29 06:36:01 +00:00
|
|
|
use tracing::warn;
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
use crate::{services, utils, Error, Result};
|
|
|
|
|
2024-02-07 22:48:01 +00:00
|
|
|
/// Sends a request to an appservice
|
|
|
|
///
|
2024-03-06 00:48:54 +00:00
|
|
|
/// Only returns None if there is no url specified in the appservice
|
|
|
|
/// registration file
|
|
|
|
pub(crate) async fn send_request<T>(registration: Registration, request: T) -> Option<Result<T::IncomingResponse>>
|
2020-12-08 09:33:44 +00:00
|
|
|
where
|
2024-03-06 00:48:54 +00:00
|
|
|
T: OutgoingRequest + Debug,
|
2020-12-08 09:33:44 +00:00
|
|
|
{
|
2024-03-06 00:48:54 +00:00
|
|
|
if let Some(destination) = registration.url {
|
|
|
|
let hs_token = registration.hs_token.as_str();
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
let mut http_request = request
|
|
|
|
.try_into_http_request::<BytesMut>(
|
|
|
|
&destination,
|
|
|
|
SendAccessToken::IfRequired(hs_token),
|
|
|
|
&[MatrixVersion::V1_0],
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
warn!("Failed to find destination {}: {}", destination, e);
|
|
|
|
Error::BadServerResponse("Invalid destination")
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
.map(bytes::BytesMut::freeze);
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00: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 {
|
|
|
|
"?"
|
|
|
|
};
|
2024-02-07 22:48:01 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
parts.path_and_query = Some((old_path_and_query + symbol + "access_token=" + hs_token).parse().unwrap());
|
|
|
|
*http_request.uri_mut() = parts.try_into().expect("our manipulation is always valid");
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
let mut reqwest_request =
|
|
|
|
reqwest::Request::try_from(http_request).expect("all http requests are valid reqwest requests");
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
*reqwest_request.timeout_mut() = Some(Duration::from_secs(120));
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
let url = reqwest_request.url().clone();
|
|
|
|
let mut response = match services().globals.default_client().execute(reqwest_request).await {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Could not send request to appservice {} at {}: {}",
|
|
|
|
registration.id, destination, e
|
|
|
|
);
|
|
|
|
return Some(Err(e.into()));
|
|
|
|
},
|
|
|
|
};
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
// reqwest::Response -> http::Response conversion
|
|
|
|
let status = response.status();
|
|
|
|
let mut http_response_builder = http::Response::builder().status(status).version(response.version());
|
|
|
|
mem::swap(
|
|
|
|
response.headers_mut(),
|
|
|
|
http_response_builder.headers_mut().expect("http::response::Builder is usable"),
|
|
|
|
);
|
2024-02-07 22:48:01 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
|
|
|
warn!("server error: {}", e);
|
|
|
|
Vec::new().into()
|
|
|
|
}); // TODO: handle timeout
|
2024-02-07 22:48:01 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
if !status.is_success() {
|
|
|
|
warn!(
|
|
|
|
"Appservice returned bad response {} {}\n{}\n{:?}",
|
|
|
|
destination,
|
|
|
|
status,
|
|
|
|
url,
|
|
|
|
utils::string_from_bytes(&body)
|
|
|
|
);
|
|
|
|
}
|
2020-12-08 09:33:44 +00:00
|
|
|
|
2024-03-06 00:48:54 +00:00
|
|
|
let response = T::IncomingResponse::try_from_http_response(
|
|
|
|
http_response_builder.body(body).expect("reqwest body is valid http body"),
|
|
|
|
);
|
|
|
|
Some(response.map_err(|_| {
|
|
|
|
warn!("Appservice returned invalid response bytes {}\n{}", destination, url);
|
|
|
|
Error::BadServerResponse("Server returned bad response.")
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-12-08 09:33:44 +00:00
|
|
|
}
|