-
Notifications
You must be signed in to change notification settings - Fork 0
feat(settings): align settings, registry and docs with real capabilities (MAT-136) #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0902b9a
feat(settings): align settings, registry and docs with real capabilities
mpiton a774adf
fix(settings): refresh MEGA plugin.toml checksum after manifest descr…
mpiton 59d96a8
fix(settings): use released MEGA v1.1.0 wasm checksum
mpiton 21d6c4b
fix(settings): tighten registry test, proxy logging and deps hook aft…
mpiton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| //! Shared HTTP client built from the persisted network settings (MAT-136). | ||
| //! | ||
| //! Applied at startup only — changing these settings requires an app | ||
| //! restart. `dns_over_https` is not consumed yet (planned). | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| use tracing::warn; | ||
|
|
||
| use crate::domain::model::config::AppConfig; | ||
|
|
||
| /// Build the app-wide reqwest client from user network settings. | ||
| /// | ||
| /// Invalid proxy values fall back to a direct connection instead of | ||
| /// failing startup: the app must stay usable so the user can fix the | ||
| /// setting. | ||
| pub fn client_from_config(config: &AppConfig) -> Result<reqwest::Client, reqwest::Error> { | ||
| let mut builder = reqwest::Client::builder() | ||
| .user_agent(effective_user_agent(config)) | ||
| .connect_timeout(Duration::from_secs(effective_connect_timeout_secs(config))); | ||
| if let Some(proxy_url) = configured_proxy_uri(config) { | ||
| match reqwest::Proxy::all(&proxy_url) { | ||
| Ok(proxy) => builder = builder.proxy(proxy), | ||
| // Never log the raw URL: proxy URIs can embed `user:password@`. | ||
| Err(err) => warn!(error = %err, "invalid proxy setting ignored"), | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| builder.build() | ||
| } | ||
|
|
||
| fn effective_user_agent(config: &AppConfig) -> String { | ||
| let trimmed = config.user_agent.trim(); | ||
| if trimmed.is_empty() { | ||
| AppConfig::default().user_agent | ||
| } else if reqwest::header::HeaderValue::from_str(trimmed).is_err() { | ||
| warn!(user_agent = trimmed, "invalid user_agent setting ignored"); | ||
| AppConfig::default().user_agent | ||
| } else { | ||
| trimmed.to_string() | ||
| } | ||
| } | ||
|
|
||
| fn effective_connect_timeout_secs(config: &AppConfig) -> u64 { | ||
| if config.connection_timeout_seconds == 0 { | ||
| u64::from(AppConfig::default().connection_timeout_seconds) | ||
| } else { | ||
| u64::from(config.connection_timeout_seconds) | ||
| } | ||
| } | ||
|
|
||
| /// Proxy URI to use, or `None` for a direct connection. Prepends the | ||
| /// selected proxy type as scheme when the user typed a bare `host:port`. | ||
| fn configured_proxy_uri(config: &AppConfig) -> Option<String> { | ||
| if config.proxy_type == "none" { | ||
| return None; | ||
| } | ||
| let url = config.proxy_url.as_deref().map(str::trim)?; | ||
| if url.is_empty() { | ||
| return None; | ||
| } | ||
| if url.contains("://") { | ||
| Some(url.to_string()) | ||
| } else { | ||
| Some(format!("{}://{}", config.proxy_type, url)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use wiremock::matchers::{header, method, path}; | ||
| use wiremock::{Mock, MockServer, ResponseTemplate}; | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn test_client_from_config_sends_configured_user_agent() { | ||
| let mock_server = MockServer::start().await; | ||
|
|
||
| Mock::given(method("GET")) | ||
| .and(path("/probe")) | ||
| .and(header("user-agent", "TestAgent/9")) | ||
| .respond_with(ResponseTemplate::new(200)) | ||
| .mount(&mock_server) | ||
| .await; | ||
|
|
||
| let config = AppConfig { | ||
| user_agent: "TestAgent/9".to_string(), | ||
| ..AppConfig::default() | ||
| }; | ||
| let client = client_from_config(&config).expect("client builds"); | ||
| let response = client | ||
| .get(format!("{}/probe", mock_server.uri())) | ||
| .send() | ||
| .await | ||
| .expect("request succeeds"); | ||
|
|
||
| assert_eq!(response.status(), 200); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_client_from_config_with_invalid_proxy_url_falls_back_to_direct() { | ||
| let config = AppConfig { | ||
| proxy_type: "http".to_string(), | ||
| proxy_url: Some("::not a proxy url::".to_string()), | ||
| ..AppConfig::default() | ||
| }; | ||
|
|
||
| assert!(client_from_config(&config).is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_client_from_config_with_control_chars_in_user_agent_still_builds() { | ||
| // A hand-edited config.toml can hold a value that is not a valid | ||
| // HTTP header; startup must not fail on it. | ||
| let config = AppConfig { | ||
| user_agent: "Bad\nAgent".to_string(), | ||
| ..AppConfig::default() | ||
| }; | ||
|
|
||
| assert!(client_from_config(&config).is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_effective_user_agent_with_blank_value_uses_default() { | ||
| let config = AppConfig { | ||
| user_agent: " ".to_string(), | ||
| ..AppConfig::default() | ||
| }; | ||
|
|
||
| assert_eq!(effective_user_agent(&config), "Vortex/1.0"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_effective_connect_timeout_with_zero_uses_default() { | ||
| let config = AppConfig { | ||
| connection_timeout_seconds: 0, | ||
| ..AppConfig::default() | ||
| }; | ||
|
|
||
| assert_eq!(effective_connect_timeout_secs(&config), 30); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_configured_proxy_uri_with_none_type_returns_none() { | ||
| assert_eq!(configured_proxy_uri(&AppConfig::default()), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_configured_proxy_uri_with_bare_host_prepends_scheme() { | ||
| let config = AppConfig { | ||
| proxy_type: "socks5".to_string(), | ||
| proxy_url: Some("127.0.0.1:1080".to_string()), | ||
| ..AppConfig::default() | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| configured_proxy_uri(&config), | ||
| Some("socks5://127.0.0.1:1080".to_string()) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_configured_proxy_uri_with_explicit_scheme_keeps_url() { | ||
| let config = AppConfig { | ||
| proxy_type: "http".to_string(), | ||
| proxy_url: Some("http://proxy.local:3128".to_string()), | ||
| ..AppConfig::default() | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| configured_proxy_uri(&config), | ||
| Some("http://proxy.local:3128".to_string()) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.