matrix-sdk: Fix the incorrect return value of the HttpSend trait.

The HttpSend trait incorrectly returns a reqwest::Response, we already
have logic to return the response into a http::Response and we need to
do the conversion since there is no other way to build Ruma responses.
master
Damir Jelić 2020-08-26 13:41:15 +02:00
parent 6760f81498
commit ea4befabd9
1 changed files with 58 additions and 40 deletions

View File

@ -36,32 +36,43 @@ pub trait HttpSend: Sync + Send + Debug {
/// ///
/// * `request` - The http request that has been converted from a ruma `Request`. /// * `request` - The http request that has been converted from a ruma `Request`.
/// ///
/// # Returns
///
/// A `reqwest::Response` that will be converted to a ruma `Response` in the `Client`.
///
/// # Examples /// # Examples
/// ///
/// ```ignore /// ```
/// use matrix_sdk::HttpSend; /// use std::convert::TryFrom;
/// use matrix_sdk::{HttpSend, Result};
/// use matrix_sdk_common_macros::async_trait; /// use matrix_sdk_common_macros::async_trait;
/// use reqwest::Response;
/// ///
/// #[derive(Debug)] /// #[derive(Debug)]
/// struct TestSend; /// struct Client(reqwest::Client);
/// ///
/// impl HttpSend for TestSend { /// impl Client {
/// async fn send_request(&self, request: http::Request<Vec<u8>>) -> Result<Response> /// async fn response_to_http_response(
/// // send the request somehow /// &self,
/// let response = send(request, method, homeserver).await?; /// mut response: reqwest::Response,
/// /// ) -> Result<http::Response<Vec<u8>>> {
/// // reqwest can convert to and from `http::Response` types. /// // Convert the reqwest response to a http one.
/// Ok(reqwest::Response::from(response)) /// todo!()
/// } /// }
/// } /// }
/// ///
/// #[async_trait]
/// impl HttpSend for Client {
/// async fn send_request(&self, request: http::Request<Vec<u8>>) -> Result<http::Response<Vec<u8>>> {
/// Ok(self
/// .response_to_http_response(
/// self.0
/// .execute(reqwest::Request::try_from(request)?)
/// .await?,
/// )
/// .await?)
/// }
/// }
/// ``` /// ```
async fn send_request(&self, request: http::Request<Vec<u8>>) -> Result<Response>; async fn send_request(
&self,
request: http::Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>>;
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -76,7 +87,7 @@ impl HttpClient {
&self, &self,
request: Request, request: Request,
session: Arc<RwLock<Option<Session>>>, session: Arc<RwLock<Option<Session>>>,
) -> Result<Response> { ) -> Result<http::Response<Vec<u8>>> {
let mut request = { let mut request = {
let read_guard; let read_guard;
let access_token = if Request::METADATA.requires_authentication { let access_token = if Request::METADATA.requires_authentication {
@ -104,23 +115,6 @@ impl HttpClient {
self.inner.send_request(request).await self.inner.send_request(request).await
} }
async fn response_to_http_response(
&self,
mut response: Response,
) -> Result<http::Response<Vec<u8>>> {
let status = response.status();
let mut http_builder = HttpResponse::builder().status(status);
let headers = http_builder.headers_mut().unwrap();
for (k, v) in response.headers_mut().drain() {
if let Some(key) = k {
headers.insert(key, v);
}
}
let body = response.bytes().await?.as_ref().to_owned();
Ok(http_builder.body(body).unwrap())
}
pub async fn send<Request>(&self, request: Request) -> Result<Request::IncomingResponse> pub async fn send<Request>(&self, request: Request) -> Result<Request::IncomingResponse>
where where
Request: OutgoingRequest, Request: OutgoingRequest,
@ -130,8 +124,6 @@ impl HttpClient {
trace!("Got response: {:?}", response); trace!("Got response: {:?}", response);
let response = self.response_to_http_response(response).await?;
Ok(Request::IncomingResponse::try_from(response)?) Ok(Request::IncomingResponse::try_from(response)?)
} }
} }
@ -187,14 +179,40 @@ impl DefaultHttpClient {
inner: http_client.build()?, inner: http_client.build()?,
}) })
} }
async fn response_to_http_response(
&self,
mut response: Response,
) -> Result<http::Response<Vec<u8>>> {
let status = response.status();
let mut http_builder = HttpResponse::builder().status(status);
let headers = http_builder.headers_mut().unwrap();
for (k, v) in response.headers_mut().drain() {
if let Some(key) = k {
headers.insert(key, v);
}
}
let body = response.bytes().await?.as_ref().to_owned();
Ok(http_builder.body(body).unwrap())
}
} }
#[async_trait] #[async_trait]
impl HttpSend for DefaultHttpClient { impl HttpSend for DefaultHttpClient {
async fn send_request(&self, request: http::Request<Vec<u8>>) -> Result<Response> { async fn send_request(
&self,
request: http::Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>> {
Ok(self Ok(self
.inner .response_to_http_response(
self.inner
.execute(reqwest::Request::try_from(request)?) .execute(reqwest::Request::try_from(request)?)
.await?,
)
.await?) .await?)
} }
} }