mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 07:41:23 +01:00
refactor send_request in api/server_server
Seriously, what is going on with the control flow in this codebase?
This commit is contained in:
parent
e13db834ed
commit
cb036593ea
1 changed files with 57 additions and 78 deletions
|
|
@ -258,88 +258,67 @@ where
|
||||||
let response =
|
let response =
|
||||||
services().globals.federation_client().execute(reqwest_request).await;
|
services().globals.federation_client().execute(reqwest_request).await;
|
||||||
|
|
||||||
match response {
|
let mut response = response.inspect_err(|error| {
|
||||||
Ok(mut response) => {
|
if log_error {
|
||||||
// reqwest::Response -> http::Response conversion
|
warn!(%error, "Could not send request");
|
||||||
let status = response.status();
|
}
|
||||||
debug!(status = u16::from(status), "Received response");
|
})?;
|
||||||
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"),
|
|
||||||
);
|
|
||||||
|
|
||||||
debug!("Getting response bytes");
|
// reqwest::Response -> http::Response conversion
|
||||||
// TODO: handle timeout
|
let status = response.status();
|
||||||
let body = response.bytes().await.unwrap_or_else(|e| {
|
debug!(status = u16::from(status), "Received response");
|
||||||
warn!("server error {}", e);
|
let mut http_response_builder =
|
||||||
Vec::new().into()
|
http::Response::builder().status(status).version(response.version());
|
||||||
});
|
mem::swap(
|
||||||
debug!("Got response bytes");
|
response.headers_mut(),
|
||||||
|
http_response_builder
|
||||||
|
.headers_mut()
|
||||||
|
.expect("http::response::Builder is usable"),
|
||||||
|
);
|
||||||
|
|
||||||
if status != 200 {
|
debug!("Getting response bytes");
|
||||||
warn!(
|
// TODO: handle timeout
|
||||||
status = u16::from(status),
|
let body = response.bytes().await.unwrap_or_else(|e| {
|
||||||
response = dbg_truncate_str(
|
warn!("server error {}", e);
|
||||||
String::from_utf8_lossy(&body).as_ref(),
|
Vec::new().into()
|
||||||
100,
|
});
|
||||||
)
|
debug!("Got response bytes");
|
||||||
|
|
||||||
|
if status != 200 {
|
||||||
|
warn!(
|
||||||
|
status = u16::from(status),
|
||||||
|
response =
|
||||||
|
dbg_truncate_str(String::from_utf8_lossy(&body).as_ref(), 100)
|
||||||
.into_owned(),
|
.into_owned(),
|
||||||
"Received error over federation",
|
"Received error over federation",
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
let http_response = http_response_builder
|
|
||||||
.body(body)
|
|
||||||
.expect("reqwest body is valid http body");
|
|
||||||
|
|
||||||
if status == 200 {
|
|
||||||
debug!("Parsing response bytes");
|
|
||||||
let response =
|
|
||||||
T::IncomingResponse::try_from_http_response(http_response);
|
|
||||||
if response.is_ok() && write_destination_to_cache {
|
|
||||||
METRICS.record_lookup(
|
|
||||||
Lookup::FederationDestination,
|
|
||||||
FoundIn::Remote,
|
|
||||||
);
|
|
||||||
services()
|
|
||||||
.globals
|
|
||||||
.actual_destination_cache
|
|
||||||
.write()
|
|
||||||
.await
|
|
||||||
.insert(
|
|
||||||
OwnedServerName::from(destination),
|
|
||||||
(actual_destination, host),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
response.map_err(|e| {
|
|
||||||
warn!(error = %e, "Invalid 200 response",);
|
|
||||||
Error::BadServerResponse(
|
|
||||||
"Server returned bad 200 response.",
|
|
||||||
)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(Error::Federation(
|
|
||||||
destination.to_owned(),
|
|
||||||
RumaError::from_http_response(http_response),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
if log_error {
|
|
||||||
warn!(
|
|
||||||
error = %e,
|
|
||||||
"Could not send request",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Err(e.into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let http_response = http_response_builder
|
||||||
|
.body(body)
|
||||||
|
.expect("reqwest body is valid http body");
|
||||||
|
|
||||||
|
if status != 200 {
|
||||||
|
return Err(Error::Federation(
|
||||||
|
destination.to_owned(),
|
||||||
|
RumaError::from_http_response(http_response),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Parsing response bytes");
|
||||||
|
let response = T::IncomingResponse::try_from_http_response(http_response);
|
||||||
|
if response.is_ok() && write_destination_to_cache {
|
||||||
|
METRICS.record_lookup(Lookup::FederationDestination, FoundIn::Remote);
|
||||||
|
services().globals.actual_destination_cache.write().await.insert(
|
||||||
|
OwnedServerName::from(destination),
|
||||||
|
(actual_destination, host),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.map_err(|e| {
|
||||||
|
warn!(error = %e, "Invalid 200 response");
|
||||||
|
Error::BadServerResponse("Server returned bad 200 response.")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_ip_with_port(destination_str: &str) -> Option<FedDest> {
|
fn get_ip_with_port(destination_str: &str) -> Option<FedDest> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue