fix: do not expect that all http requests are valid reqwest requests

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
Matthias Ahouansou 2024-04-01 23:22:19 -04:00 committed by June
parent 93a43a0eda
commit c31fb7134a
6 changed files with 30 additions and 41 deletions

View file

@ -10,11 +10,16 @@ use crate::{services, utils, Error, Result};
///
/// 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>>
pub(crate) async fn send_request<T>(registration: Registration, request: T) -> Result<Option<T::IncomingResponse>>
where
T: OutgoingRequest + Debug,
{
let destination = registration.url?;
let destination = match registration.url {
Some(url) => url,
None => {
return Ok(None);
},
};
let hs_token = registration.hs_token.as_str();
@ -42,8 +47,7 @@ where
);
*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");
let mut reqwest_request = reqwest::Request::try_from(http_request)?;
*reqwest_request.timeout_mut() = Some(Duration::from_secs(120));
@ -62,7 +66,7 @@ where
"Could not send request to appservice {} at {}: {}",
registration.id, destination, e
);
return Some(Err(e.into()));
return Err(e.into());
},
};
@ -99,8 +103,8 @@ where
.expect("reqwest body is valid http body"),
);
Some(response.map_err(|_| {
response.map(Some).map_err(|_| {
warn!("Appservice returned invalid response bytes {}\n{}", destination, url);
Error::BadServerResponse("Server returned bad response.")
}))
})
}