2022-10-05 20:34:31 +02:00
|
|
|
use crate::{services, utils, Error, Result};
|
2021-04-23 18:45:06 +02:00
|
|
|
use bytes::BytesMut;
|
2022-02-12 21:04:38 +01:00
|
|
|
use ruma::api::{IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken};
|
2022-02-03 13:24:04 +01:00
|
|
|
use std::{fmt::Debug, mem, time::Duration};
|
2021-07-29 08:36:01 +02:00
|
|
|
use tracing::warn;
|
2020-12-08 10:33:44 +01:00
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
#[tracing::instrument(skip(request))]
|
2021-08-31 19:14:37 +02:00
|
|
|
pub(crate) async fn send_request<T: OutgoingRequest>(
|
2020-12-08 10:33:44 +01:00
|
|
|
registration: serde_yaml::Value,
|
|
|
|
request: T,
|
|
|
|
) -> Result<T::IncomingResponse>
|
|
|
|
where
|
|
|
|
T: Debug,
|
|
|
|
{
|
|
|
|
let destination = registration.get("url").unwrap().as_str().unwrap();
|
|
|
|
let hs_token = registration.get("hs_token").unwrap().as_str().unwrap();
|
|
|
|
|
|
|
|
let mut http_request = request
|
2022-02-12 21:04:38 +01:00
|
|
|
.try_into_http_request::<BytesMut>(
|
|
|
|
destination,
|
|
|
|
SendAccessToken::IfRequired(""),
|
|
|
|
&[MatrixVersion::V1_0],
|
|
|
|
)
|
2021-04-23 18:45:06 +02:00
|
|
|
.unwrap()
|
|
|
|
.map(|body| body.freeze());
|
2020-12-08 10:33:44 +01:00
|
|
|
|
|
|
|
let mut parts = http_request.uri().clone().into_parts();
|
|
|
|
let old_path_and_query = parts.path_and_query.unwrap().as_str().to_owned();
|
2021-01-05 15:21:41 +01:00
|
|
|
let symbol = if old_path_and_query.contains('?') {
|
2020-12-08 10:33:44 +01:00
|
|
|
"&"
|
|
|
|
} else {
|
|
|
|
"?"
|
|
|
|
};
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
let mut reqwest_request = reqwest::Request::try_from(http_request)
|
|
|
|
.expect("all http requests are valid reqwest requests");
|
|
|
|
|
|
|
|
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
|
|
|
|
|
|
|
let url = reqwest_request.url().clone();
|
2022-10-13 10:14:52 +02:00
|
|
|
let mut response = match services()
|
2022-10-05 20:34:31 +02:00
|
|
|
.globals
|
|
|
|
.default_client()
|
|
|
|
.execute(reqwest_request)
|
2022-10-13 10:14:52 +02:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Could not send request to appservice {:?} at {}: {}",
|
2022-10-15 00:28:43 +02:00
|
|
|
registration.get("id"),
|
|
|
|
destination,
|
|
|
|
e
|
2022-10-13 10:14:52 +02:00
|
|
|
);
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
};
|
2020-12-08 10:33:44 +01:00
|
|
|
|
2021-04-29 20:58:05 +02: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"),
|
|
|
|
);
|
2020-12-08 10:33:44 +01:00
|
|
|
|
2021-04-29 20:58:05 +02:00
|
|
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
2021-04-23 18:38:20 +02:00
|
|
|
warn!("server error: {}", e);
|
|
|
|
Vec::new().into()
|
|
|
|
}); // TODO: handle timeout
|
2020-12-08 10:33:44 +01:00
|
|
|
|
2021-04-23 18:38:20 +02:00
|
|
|
if status != 200 {
|
|
|
|
warn!(
|
|
|
|
"Appservice returned bad response {} {}\n{}\n{:?}",
|
|
|
|
destination,
|
|
|
|
status,
|
|
|
|
url,
|
|
|
|
utils::string_from_bytes(&body)
|
|
|
|
);
|
2020-12-08 10:33:44 +01:00
|
|
|
}
|
2021-04-23 18:38:20 +02:00
|
|
|
|
|
|
|
let response = T::IncomingResponse::try_from_http_response(
|
2021-04-29 20:58:05 +02:00
|
|
|
http_response_builder
|
2021-04-23 18:38:20 +02:00
|
|
|
.body(body)
|
|
|
|
.expect("reqwest body is valid http body"),
|
|
|
|
);
|
|
|
|
response.map_err(|_| {
|
|
|
|
warn!(
|
|
|
|
"Appservice returned invalid response bytes {}\n{}",
|
|
|
|
destination, url
|
|
|
|
);
|
|
|
|
Error::BadServerResponse("Server returned bad response.")
|
|
|
|
})
|
2020-12-08 10:33:44 +01:00
|
|
|
}
|