Add timeouts to HTTP requests (#3908)

This commit is contained in:
iequidoo
2023-01-11 11:55:42 -03:00
committed by iequidoo
parent 6642083f52
commit 6d9d31cad1
6 changed files with 25 additions and 4 deletions

View File

@@ -16,7 +16,7 @@ pub async fn read_url(context: &Context, url: &str) -> anyhow::Result<String> {
}
pub async fn read_url_inner(context: &Context, url: &str) -> anyhow::Result<String> {
let client = reqwest::Client::new();
let client = crate::http::get_client()?;
let mut url = url.to_string();
// Follow up to 10 http-redirects

12
src/http.rs Normal file
View File

@@ -0,0 +1,12 @@
//! # HTTP module.
use anyhow::Result;
use std::time::Duration;
const HTTP_TIMEOUT: Duration = Duration::from_secs(30);
pub(crate) fn get_client() -> Result<reqwest::Client> {
Ok(reqwest::ClientBuilder::new()
.timeout(HTTP_TIMEOUT)
.build()?)
}

View File

@@ -66,6 +66,7 @@ mod decrypt;
pub mod download;
mod e2ee;
pub mod ephemeral;
mod http;
mod imap;
pub mod imex;
mod scheduler;

View File

@@ -158,7 +158,7 @@ pub async fn get_oauth2_access_token(
}
// ... and POST
let client = reqwest::Client::new();
let client = crate::http::get_client()?;
let response: Response = match client.post(post_url).form(&post_param).send().await {
Ok(resp) => match resp.json().await {
@@ -284,7 +284,14 @@ impl Oauth2 {
// "verified_email": true,
// "picture": "https://lh4.googleusercontent.com/-Gj5jh_9R0BY/AAAAAAAAAAI/AAAAAAAAAAA/IAjtjfjtjNA/photo.jpg"
// }
let response = match reqwest::get(userinfo_url).await {
let client = match crate::http::get_client() {
Ok(cl) => cl,
Err(err) => {
warn!(context, "failed to get HTTP client: {}", err);
return None;
}
};
let response = match client.get(userinfo_url).send().await {
Ok(response) => response,
Err(err) => {
warn!(context, "failed to get userinfo: {}", err);

View File

@@ -381,7 +381,7 @@ struct CreateAccountErrorResponse {
#[allow(clippy::indexing_slicing)]
async fn set_account_from_qr(context: &Context, qr: &str) -> Result<()> {
let url_str = &qr[DCACCOUNT_SCHEME.len()..];
let response = reqwest::Client::new().post(url_str).send().await?;
let response = crate::http::get_client()?.post(url_str).send().await?;
let response_status = response.status();
let response_text = response.text().await.with_context(|| {
format!(