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

@ -157,20 +157,18 @@ pub(crate) async fn get_alias_helper(room_alias: OwnedRoomAliasId) -> Result<get
None => {
for appservice in services().appservice.read().await.values() {
if appservice.aliases.is_match(room_alias.as_str())
&& if let Some(opt_result) = services()
.sending
.send_appservice_request(
appservice.registration.clone(),
appservice::query::query_room_alias::v1::Request {
room_alias: room_alias.clone(),
},
)
.await
{
opt_result.is_ok()
} else {
false
} {
&& matches!(
services()
.sending
.send_appservice_request(
appservice.registration.clone(),
appservice::query::query_room_alias::v1::Request {
room_alias: room_alias.clone(),
},
)
.await,
Ok(Some(_opt_result))
) {
room_id = Some(
services()
.rooms

View file

@ -53,10 +53,7 @@ pub async fn get_presence_route(body: Ruma<get_presence::v3::Request>) -> Result
{
let room_id = room_id?;
if let Some(presence) = services()
.presence
.get_presence(&room_id, sender_user)?
{
if let Some(presence) = services().presence.get_presence(&room_id, sender_user)? {
presence_event = Some(presence);
break;
}

View file

@ -528,10 +528,7 @@ async fn process_room_presence_updates(
presence_updates: &mut HashMap<OwnedUserId, PresenceEvent>, room_id: &RoomId, since: u64,
) -> Result<()> {
// Take presence updates from this room
for (user_id, _, presence_event) in services()
.presence
.presence_since(room_id, since)
{
for (user_id, _, presence_event) in services().presence.presence_since(room_id, since) {
match presence_updates.entry(user_id) {
Entry::Vacant(slot) => {
slot.insert(presence_event);

View file

@ -143,9 +143,7 @@ fn process_presence_timer(user_id: &OwnedUserId) -> Result<()> {
let mut status_msg = None;
for room_id in services().rooms.state_cache.rooms_joined(user_id) {
let presence_event = services()
.presence
.get_presence(&room_id?, user_id)?;
let presence_event = services().presence.get_presence(&room_id?, user_id)?;
if let Some(presence_event) = presence_event {
presence_state = presence_event.content.presence;

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.")
}))
})
}

View file

@ -250,7 +250,7 @@ impl Service {
/// registration file
pub async fn send_appservice_request<T>(
&self, registration: Registration, request: T,
) -> Option<Result<T::IncomingResponse>>
) -> Result<Option<T::IncomingResponse>>
where
T: OutgoingRequest + Debug,
{
@ -487,10 +487,7 @@ pub fn select_edus_presence(
// Look for presence updates in this room
let mut presence_updates = Vec::new();
for (user_id, count, presence_event) in services()
.presence
.presence_since(room_id, since)
{
for (user_id, count, presence_event) in services().presence.presence_since(room_id, since) {
if count > *max_edu_count {
*max_edu_count = count;
}
@ -655,10 +652,8 @@ async fn handle_events_kind_appservice(
)
.await
{
None => Ok(kind.clone()),
Some(op_resp) => op_resp
.map(|_response| kind.clone())
.map_err(|e| (kind.clone(), e)),
Ok(_) => Ok(kind.clone()),
Err(e) => Err((kind.clone(), e)),
};
drop(permit);