Skip to content

Commit 937cbaa

Browse files
authored
[rust] Reuse common http client in Selenium Manager (#11449)
* [rust] Reuse common http client in Selenium Manager * [rust] Remove non necessary condition in fallback logic for chromedriver * [rust] Avoid checking negative browser version for chromedriver fallback
1 parent 3930688 commit 937cbaa

File tree

6 files changed

+86
-34
lines changed

6 files changed

+86
-34
lines changed

rust/src/chrome.rs

+29-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use crate::config::ManagerConfig;
19+
use reqwest::Client;
1920
use std::collections::HashMap;
2021
use std::error::Error;
2122
use std::path::PathBuf;
@@ -28,8 +29,9 @@ use crate::metadata::{
2829
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
2930
};
3031
use crate::{
31-
SeleniumManager, BETA, DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA, ENV_PROGRAM_FILES,
32-
ENV_PROGRAM_FILES_X86, FALLBACK_RETRIES, NIGHTLY, REG_QUERY, STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
32+
create_default_http_client, SeleniumManager, BETA, DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA,
33+
ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, FALLBACK_RETRIES, NIGHTLY, REG_QUERY, STABLE,
34+
WMIC_COMMAND, WMIC_COMMAND_ENV,
3335
};
3436

3537
const BROWSER_NAME: &str = "chrome";
@@ -41,6 +43,7 @@ pub struct ChromeManager {
4143
pub browser_name: &'static str,
4244
pub driver_name: &'static str,
4345
pub config: ManagerConfig,
46+
pub http_client: Client,
4447
}
4548

4649
impl ChromeManager {
@@ -49,6 +52,7 @@ impl ChromeManager {
4952
browser_name: BROWSER_NAME,
5053
driver_name: DRIVER_NAME,
5154
config: ManagerConfig::default(),
55+
http_client: create_default_http_client(),
5256
})
5357
}
5458
}
@@ -58,6 +62,10 @@ impl SeleniumManager for ChromeManager {
5862
self.browser_name
5963
}
6064

65+
fn get_http_client(&self) -> &Client {
66+
&self.http_client
67+
}
68+
6169
fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str> {
6270
HashMap::from([
6371
(
@@ -156,31 +164,32 @@ impl SeleniumManager for ChromeManager {
156164
_ => {
157165
let mut driver_version = "".to_string();
158166
let mut browser_version_int = browser_version.parse::<i32>().unwrap_or_default();
159-
if browser_version_int > 0 {
160-
for i in 0..FALLBACK_RETRIES {
161-
let driver_url = if browser_version.is_empty() {
162-
format!("{}{}", DRIVER_URL, LATEST_RELEASE)
163-
} else {
164-
format!("{}{}_{}", DRIVER_URL, LATEST_RELEASE, browser_version_int)
165-
};
166-
log::debug!("Reading {} version from {}", &self.driver_name, driver_url);
167-
let content = read_content_from_link(driver_url);
168-
match content {
169-
Ok(version) => {
170-
driver_version = version;
171-
break;
172-
}
173-
_ => {
174-
log::warn!(
167+
for i in 0..FALLBACK_RETRIES {
168+
let driver_url = if browser_version.is_empty() {
169+
format!("{}{}", DRIVER_URL, LATEST_RELEASE)
170+
} else {
171+
format!("{}{}_{}", DRIVER_URL, LATEST_RELEASE, browser_version_int)
172+
};
173+
if !browser_version.is_empty() && browser_version_int <= 0 {
174+
break;
175+
}
176+
log::debug!("Reading {} version from {}", &self.driver_name, driver_url);
177+
let content = read_content_from_link(self.get_http_client(), driver_url);
178+
match content {
179+
Ok(version) => {
180+
driver_version = version;
181+
break;
182+
}
183+
_ => {
184+
log::warn!(
175185
"Error getting version of {} {}. Retrying with {} {} (attempt {}/{})",
176186
&self.driver_name,
177187
browser_version_int,
178188
&self.driver_name,
179189
browser_version_int - 1,
180190
i + 1, FALLBACK_RETRIES
181191
);
182-
browser_version_int -= 1;
183-
}
192+
browser_version_int -= 1;
184193
}
185194
}
186195
}

rust/src/downloads.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use reqwest::Client;
1819
use std::error::Error;
1920
use std::fs::File;
2021
use std::io::copy;
@@ -26,6 +27,7 @@ use crate::files::parse_version;
2627

2728
#[tokio::main]
2829
pub async fn download_driver_to_tmp_folder(
30+
http_client: &Client,
2931
url: String,
3032
) -> Result<(TempDir, String), Box<dyn Error>> {
3133
let tmp_dir = Builder::new().prefix("selenium-manager").tempdir()?;
@@ -35,7 +37,7 @@ pub async fn download_driver_to_tmp_folder(
3537
tmp_dir.path()
3638
);
3739

38-
let response = reqwest::get(url).await?;
40+
let response = http_client.get(url).send().await?;
3941
let target_path;
4042
let mut tmp_file = {
4143
let target_name = response
@@ -59,11 +61,17 @@ pub async fn download_driver_to_tmp_folder(
5961
}
6062

6163
#[tokio::main]
62-
pub async fn read_content_from_link(url: String) -> Result<String, Box<dyn Error>> {
63-
parse_version(reqwest::get(url).await?.text().await?)
64+
pub async fn read_content_from_link(
65+
http_client: &Client,
66+
url: String,
67+
) -> Result<String, Box<dyn Error>> {
68+
parse_version(http_client.get(url).send().await?.text().await?)
6469
}
6570

6671
#[tokio::main]
67-
pub async fn read_redirect_from_link(url: String) -> Result<String, Box<dyn Error>> {
68-
parse_version(reqwest::get(&url).await?.url().path().to_string())
72+
pub async fn read_redirect_from_link(
73+
http_client: &Client,
74+
url: String,
75+
) -> Result<String, Box<dyn Error>> {
76+
parse_version(http_client.get(&url).send().await?.url().path().to_string())
6977
}

rust/src/edge.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use crate::config::ManagerConfig;
19+
use reqwest::Client;
1920
use std::collections::HashMap;
2021
use std::error::Error;
2122
use std::path::PathBuf;
@@ -28,8 +29,9 @@ use crate::metadata::{
2829
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
2930
};
3031
use crate::{
31-
SeleniumManager, BETA, DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA, ENV_PROGRAM_FILES,
32-
ENV_PROGRAM_FILES_X86, NIGHTLY, REG_QUERY, STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
32+
create_default_http_client, SeleniumManager, BETA, DASH_DASH_VERSION, DEV, ENV_LOCALAPPDATA,
33+
ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, NIGHTLY, REG_QUERY, STABLE, WMIC_COMMAND,
34+
WMIC_COMMAND_ENV,
3335
};
3436

3537
const BROWSER_NAME: &str = "edge";
@@ -42,6 +44,7 @@ pub struct EdgeManager {
4244
pub browser_name: &'static str,
4345
pub driver_name: &'static str,
4446
pub config: ManagerConfig,
47+
pub http_client: Client,
4548
}
4649

4750
impl EdgeManager {
@@ -50,6 +53,7 @@ impl EdgeManager {
5053
browser_name: BROWSER_NAME,
5154
driver_name: DRIVER_NAME,
5255
config: ManagerConfig::default(),
56+
http_client: create_default_http_client(),
5357
})
5458
}
5559
}
@@ -59,6 +63,10 @@ impl SeleniumManager for EdgeManager {
5963
self.browser_name
6064
}
6165

66+
fn get_http_client(&self) -> &Client {
67+
&self.http_client
68+
}
69+
6270
fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str> {
6371
HashMap::from([
6472
(
@@ -167,7 +175,7 @@ impl SeleniumManager for EdgeManager {
167175
)
168176
};
169177
log::debug!("Reading {} version from {}", &self.driver_name, driver_url);
170-
let driver_version = read_content_from_link(driver_url)?;
178+
let driver_version = read_content_from_link(self.get_http_client(), driver_url)?;
171179

172180
if !browser_version.is_empty() {
173181
metadata.drivers.push(create_driver_metadata(

rust/src/firefox.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use crate::config::ManagerConfig;
19+
use reqwest::Client;
1920
use std::collections::HashMap;
2021
use std::error::Error;
2122
use std::path::PathBuf;
@@ -28,8 +29,8 @@ use crate::metadata::{
2829
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
2930
};
3031
use crate::{
31-
SeleniumManager, BETA, DASH_VERSION, DEV, ENV_PROGRAM_FILES, ENV_PROGRAM_FILES_X86, NIGHTLY,
32-
STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
32+
create_default_http_client, SeleniumManager, BETA, DASH_VERSION, DEV, ENV_PROGRAM_FILES,
33+
ENV_PROGRAM_FILES_X86, NIGHTLY, STABLE, WMIC_COMMAND, WMIC_COMMAND_ENV,
3334
};
3435

3536
const BROWSER_NAME: &str = "firefox";
@@ -41,6 +42,7 @@ pub struct FirefoxManager {
4142
pub browser_name: &'static str,
4243
pub driver_name: &'static str,
4344
pub config: ManagerConfig,
45+
pub http_client: Client,
4446
}
4547

4648
impl FirefoxManager {
@@ -49,6 +51,7 @@ impl FirefoxManager {
4951
browser_name: BROWSER_NAME,
5052
driver_name: DRIVER_NAME,
5153
config: ManagerConfig::default(),
54+
http_client: create_default_http_client(),
5255
})
5356
}
5457
}
@@ -58,6 +61,10 @@ impl SeleniumManager for FirefoxManager {
5861
self.browser_name
5962
}
6063

64+
fn get_http_client(&self) -> &Client {
65+
&self.http_client
66+
}
67+
6168
fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str> {
6269
HashMap::from([
6370
(
@@ -147,7 +154,7 @@ impl SeleniumManager for FirefoxManager {
147154
}
148155
_ => {
149156
let latest_url = format!("{}{}", DRIVER_URL, LATEST_RELEASE);
150-
let driver_version = read_redirect_from_link(latest_url)?;
157+
let driver_version = read_redirect_from_link(self.get_http_client(), latest_url)?;
151158

152159
if !browser_version.is_empty() {
153160
metadata.drivers.push(create_driver_metadata(

rust/src/iexplorer.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
// under the License.
1717

1818
use crate::config::ManagerConfig;
19+
use reqwest::Client;
1920
use std::collections::HashMap;
2021
use std::error::Error;
2122
use std::path::PathBuf;
2223

2324
use crate::downloads::read_redirect_from_link;
2425
use crate::files::{compose_driver_path_in_cache, BrowserPath};
2526

26-
use crate::SeleniumManager;
27+
use crate::{create_default_http_client, SeleniumManager};
2728

2829
use crate::metadata::{
2930
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
@@ -38,6 +39,7 @@ pub struct IExplorerManager {
3839
pub browser_name: &'static str,
3940
pub driver_name: &'static str,
4041
pub config: ManagerConfig,
42+
pub http_client: Client,
4143
}
4244

4345
impl IExplorerManager {
@@ -46,6 +48,7 @@ impl IExplorerManager {
4648
browser_name: BROWSER_NAME,
4749
driver_name: DRIVER_NAME,
4850
config: ManagerConfig::default(),
51+
http_client: create_default_http_client(),
4952
})
5053
}
5154
}
@@ -55,6 +58,10 @@ impl SeleniumManager for IExplorerManager {
5558
self.browser_name
5659
}
5760

61+
fn get_http_client(&self) -> &Client {
62+
&self.http_client
63+
}
64+
5865
fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str> {
5966
HashMap::new()
6067
}
@@ -82,7 +89,7 @@ impl SeleniumManager for IExplorerManager {
8289
}
8390
_ => {
8491
let latest_url = format!("{}{}", DRIVER_URL, LATEST_RELEASE);
85-
let driver_version = read_redirect_from_link(latest_url)?;
92+
let driver_version = read_redirect_from_link(self.get_http_client(), latest_url)?;
8693

8794
if !browser_version.is_empty() {
8895
metadata.drivers.push(create_driver_metadata(

rust/src/lib.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::iexplorer::IExplorerManager;
2323
use std::fs;
2424

2525
use crate::config::{str_to_os, ManagerConfig};
26+
use reqwest::Client;
2627
use std::collections::HashMap;
2728
use std::error::Error;
2829
use std::path::PathBuf;
@@ -65,6 +66,8 @@ pub trait SeleniumManager {
6566

6667
fn get_browser_name(&self) -> &str;
6768

69+
fn get_http_client(&self) -> &Client;
70+
6871
fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str>;
6972

7073
fn discover_browser_version(&self) -> Option<String>;
@@ -87,7 +90,9 @@ pub trait SeleniumManager {
8790

8891
fn download_driver(&self) -> Result<(), Box<dyn Error>> {
8992
let driver_url = Self::get_driver_url(self)?;
90-
let (_tmp_folder, driver_zip_file) = download_driver_to_tmp_folder(driver_url)?;
93+
log::debug!("Driver URL: {}", driver_url);
94+
let (_tmp_folder, driver_zip_file) =
95+
download_driver_to_tmp_folder(self.get_http_client(), driver_url)?;
9196
let driver_path_in_cache = Self::get_driver_path_in_cache(self);
9297
uncompress(&driver_zip_file, driver_path_in_cache)
9398
}
@@ -350,6 +355,14 @@ pub fn clear_cache() {
350355
}
351356
}
352357

358+
pub fn create_default_http_client() -> Client {
359+
Client::builder()
360+
.danger_accept_invalid_certs(true)
361+
.use_rustls_tls()
362+
.build()
363+
.unwrap_or_default()
364+
}
365+
353366
// ----------------------------------------------------------
354367
// Private functions
355368
// ----------------------------------------------------------

0 commit comments

Comments
 (0)