|
1 | | -use std::{sync::LazyLock, time::Duration}; |
| 1 | +use std::{ |
| 2 | + str::FromStr, |
| 3 | + sync::{Arc, LazyLock, RwLock}, |
| 4 | +}; |
2 | 5 |
|
3 | | -use reqwest::{header::HeaderMap, Client}; |
| 6 | +use reqwest::{ |
| 7 | + header::{HeaderMap, HeaderName, HeaderValue}, |
| 8 | + Client, |
| 9 | +}; |
4 | 10 |
|
5 | | -pub static SHARED_CLIENT: LazyLock<Client> = LazyLock::new(|| { |
6 | | - Client::builder() |
7 | | - .user_agent("pkgforge/soar") |
8 | | - .build() |
9 | | - .expect("failed to build default client") |
| 11 | +struct SharedClient { |
| 12 | + client: Client, |
| 13 | + config: ClientConfig, |
| 14 | +} |
| 15 | + |
| 16 | +static SHARED_CLIENT_STATE: LazyLock<Arc<RwLock<SharedClient>>> = LazyLock::new(|| { |
| 17 | + let config = ClientConfig::default(); |
| 18 | + let client = config.build().expect("failed to build default client"); |
| 19 | + |
| 20 | + Arc::new(RwLock::new(SharedClient { client, config })) |
10 | 21 | }); |
11 | 22 |
|
| 23 | +pub static SHARED_CLIENT: LazyLock<Client> = |
| 24 | + LazyLock::new(|| SHARED_CLIENT_STATE.read().unwrap().client.clone()); |
| 25 | + |
| 26 | +#[derive(Clone, Debug)] |
12 | 27 | pub struct ClientConfig { |
13 | 28 | pub user_agent: Option<String>, |
14 | 29 | pub headers: Option<HeaderMap>, |
15 | 30 | pub proxy: Option<String>, |
16 | | - pub timeout: Option<Duration>, |
| 31 | +} |
| 32 | + |
| 33 | +impl Default for ClientConfig { |
| 34 | + fn default() -> Self { |
| 35 | + Self { |
| 36 | + user_agent: Some("pkgforge/soar".to_string()), |
| 37 | + headers: None, |
| 38 | + proxy: None, |
| 39 | + } |
| 40 | + } |
17 | 41 | } |
18 | 42 |
|
19 | 43 | impl ClientConfig { |
20 | | - pub fn build(self) -> Result<Client, reqwest::Error> { |
| 44 | + pub fn build(&self) -> Result<Client, reqwest::Error> { |
21 | 45 | let mut builder = Client::builder(); |
22 | 46 |
|
23 | | - if let Some(user_agent) = self.user_agent { |
| 47 | + if let Some(user_agent) = &self.user_agent { |
24 | 48 | builder = builder.user_agent(user_agent); |
25 | 49 | } |
26 | 50 |
|
27 | | - if let Some(headers) = self.headers { |
28 | | - builder = builder.default_headers(headers); |
29 | | - } |
30 | | - |
31 | | - if let Some(proxy_url) = self.proxy { |
32 | | - builder = builder.proxy(reqwest::Proxy::all(&proxy_url)?); |
| 51 | + if let Some(headers) = &self.headers { |
| 52 | + builder = builder.default_headers(headers.clone()); |
33 | 53 | } |
34 | 54 |
|
35 | | - if let Some(timeout) = self.timeout { |
36 | | - builder = builder.timeout(timeout); |
| 55 | + if let Some(proxy_url) = &self.proxy { |
| 56 | + builder = builder.proxy(reqwest::Proxy::all(proxy_url)?); |
37 | 57 | } |
38 | 58 |
|
39 | 59 | builder.build() |
40 | 60 | } |
41 | 61 | } |
| 62 | + |
| 63 | +pub fn create_http_header_map(headers: Vec<String>) -> HeaderMap { |
| 64 | + let mut header_map = HeaderMap::new(); |
| 65 | + |
| 66 | + for header in headers { |
| 67 | + let parts: Vec<&str> = header.splitn(2, ':').collect(); |
| 68 | + if parts.len() == 2 { |
| 69 | + let key = parts[0].trim(); |
| 70 | + let value = parts[1].trim(); |
| 71 | + |
| 72 | + if let Ok(header_name) = HeaderName::from_str(key) { |
| 73 | + if let Ok(header_value) = HeaderValue::from_str(value) { |
| 74 | + header_map.insert(header_name, header_value); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + header_map |
| 81 | +} |
| 82 | + |
| 83 | +pub fn configure_http_client<F>(updater: F) -> Result<(), reqwest::Error> |
| 84 | +where |
| 85 | + F: FnOnce(&mut ClientConfig), |
| 86 | +{ |
| 87 | + let mut state = SHARED_CLIENT_STATE.write().unwrap(); |
| 88 | + let mut new_config = state.config.clone(); |
| 89 | + |
| 90 | + updater(&mut new_config); |
| 91 | + |
| 92 | + let new_client = new_config.build()?; |
| 93 | + |
| 94 | + state.client = new_client; |
| 95 | + state.config = new_config; |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments