The headless browser for AI agents and web scraping
fix(cli): make --user-agent a global flag
Vladimir Babin1949e9bd46aa
diff --git a/crates/obscura-cli/src/main.rs b/crates/obscura-cli/src/main.rsindex 2c9f8a6b..144b26c5 100644--- a/crates/obscura-cli/src/main.rs+++ b/crates/obscura-cli/src/main.rs@@ -41,7 +41,10 @@ struct Args {#[arg(long, global = true)]obey_robots: bool,- #[arg(long)]+ /// Override the User-Agent sent with every request.+ /// Global: applies to fetch, serve, scrape, and mcp. A subcommand-level+ /// --user-agent still wins where one exists.+ #[arg(long, global = true)]user_agent: Option<String>,#[arg(long)]@@ -326,6 +329,13 @@ fn merge_proxy(global_proxy: Option<String>, command_proxy: Option<String>) -> Ocommand_proxy.or(global_proxy)}+fn merge_user_agent(+ global_user_agent: Option<String>,+ command_user_agent: Option<String>,+) -> Option<String> {+ command_user_agent.or(global_user_agent)+}+/// Normalize a raw `--v8-flags` value into the string we'll hand to V8./// Returns `None` when the user didn't pass the flag, passed an empty string,/// or passed only whitespace; in those cases V8 is left untouched.@@ -440,6 +450,7 @@ async fn run_cli() -> anyhow::Result<()> {}let global_proxy = args.proxy.clone();+ let global_user_agent = args.user_agent.clone();let global_storage_dir = args.storage_dir.clone();let stealth = args.stealth;let obey_robots = args.obey_robots;@@ -462,6 +473,7 @@ async fn run_cli() -> anyhow::Result<()> {font_dirs,quiet: _,}) => {+ let user_agent = merge_user_agent(global_user_agent.clone(), user_agent);let policy = pool::RecyclePolicy {max_connections: worker_max_connections,max_rss_mb: worker_max_rss,@@ -556,6 +568,7 @@ async fn run_cli() -> anyhow::Result<()> {concurrency,screenshot,}) => {+ let user_agent = merge_user_agent(global_user_agent.clone(), user_agent);if let Some(file) = file {if url.is_some() {anyhow::bail!("Pass URLs via a positional argument or --file, not both.");@@ -630,6 +643,7 @@ async fn run_cli() -> anyhow::Result<()> {global_proxy,stealth,obey_robots,+ global_user_agent.clone(),).await?;}@@ -641,6 +655,7 @@ async fn run_cli() -> anyhow::Result<()> {proxy,user_agent,}) => {+ let user_agent = merge_user_agent(global_user_agent.clone(), user_agent);configure_font_directories(&font_dirs)?;let mcp_proxy = merge_proxy(global_proxy.clone(), proxy);// `obscura mcp` builds its BrowserContext from the environment, so@@ -2022,6 +2037,7 @@ async fn run_parallel_scrape(proxy: Option<String>,stealth: bool,obey_robots: bool,+ user_agent: Option<String>,) -> anyhow::Result<()> {let total = urls.len();let start = Instant::now();@@ -2064,6 +2080,7 @@ async fn run_parallel_scrape(let eval = eval.clone();let worker_path = worker_path.clone();let proxy = proxy.clone();+ let user_agent = user_agent.clone();async move {let task_start = Instant::now();@@ -2075,6 +2092,7 @@ async fn run_parallel_scrape(.env("OBSCURA_PROXY", proxy.as_deref().unwrap_or("")).env("OBSCURA_STEALTH", if stealth { "1" } else { "" }).env("OBSCURA_OBEY_ROBOTS", if obey_robots { "1" } else { "" })+ .env("OBSCURA_USER_AGENT", user_agent.as_deref().unwrap_or("")).spawn(){Ok(c) => c,@@ -2414,8 +2432,9 @@ mod tests {use super::{configure_fetch_navigation_timeout, effective_v8_flags, extract_assets,extract_readable_text, fetch_original_bytes, is_quiet_command, link_kind_from_rel,- merge_proxy, normalize_v8_flags, read_urls_from_file, resolve_asset_url, select_log_filter,- write_or_print, write_or_print_bytes, Args, Command, DumpFormat, DEFAULT_V8_FLAGS,+ merge_proxy, merge_user_agent, normalize_v8_flags, read_urls_from_file, resolve_asset_url,+ select_log_filter, write_or_print, write_or_print_bytes, Args, Command, DumpFormat,+ DEFAULT_V8_FLAGS,};use clap::Parser;use obscura_dom::parse_html;@@ -2489,6 +2508,79 @@ mod tests {assert!(err.is_err());}+ // Issue #1278 — a top-level --user-agent is global, so it reaches the+ // subcommand whether it appears before or after the subcommand, and a+ // subcommand-level value still wins.+ #[test]+ fn user_agent_before_subcommand_reaches_fetch() {+ let args = Args::try_parse_from([+ "obscura",+ "--user-agent",+ "TestUA/1.0",+ "fetch",+ "https://example.com",+ ])+ .expect("clap should accept a top-level --user-agent before the subcommand");+ let global = args.user_agent.clone();+ match args.command {+ Some(Command::Fetch { user_agent, .. }) => {+ assert_eq!(+ merge_user_agent(global, user_agent),+ Some("TestUA/1.0".to_string())+ );+ }+ _ => panic!("expected Fetch command"),+ }+ }++ #[test]+ fn subcommand_user_agent_wins_over_global() {+ let args = Args::try_parse_from([+ "obscura",+ "--user-agent",+ "Global/1.0",+ "fetch",+ "--user-agent",+ "Sub/2.0",+ "https://example.com",+ ])+ .expect("clap should accept both a global and a subcommand --user-agent");+ let global = args.user_agent.clone();+ match args.command {+ Some(Command::Fetch { user_agent, .. }) => {+ assert_eq!(+ merge_user_agent(global, user_agent),+ Some("Sub/2.0".to_string())+ );+ }+ _ => panic!("expected Fetch command"),+ }+ }++ #[test]+ fn scrape_accepts_global_user_agent() {+ let args = Args::try_parse_from([+ "obscura",+ "scrape",+ "https://example.com",+ "--user-agent",+ "TestUA/1.0",+ ])+ .expect("scrape should accept --user-agent via the global flag");+ assert_eq!(args.user_agent, Some("TestUA/1.0".to_string()));+ }++ #[test]+ fn merge_user_agent_prefers_command_value() {+ assert_eq!(+ merge_user_agent(Some("G".to_string()), Some("C".to_string())),+ Some("C".to_string())+ );+ assert_eq!(merge_user_agent(Some("G".to_string()), None), Some("G".to_string()));+ assert_eq!(merge_user_agent(None, Some("C".to_string())), Some("C".to_string()));+ assert_eq!(merge_user_agent(None, None), None);+ }+#[test]fn read_urls_skips_blanks_and_comments() {let dir = std::env::temp_dir();diff --git a/crates/obscura-cli/src/worker.rs b/crates/obscura-cli/src/worker.rsindex b60d3c0a..06579406 100644--- a/crates/obscura-cli/src/worker.rs+++ b/crates/obscura-cli/src/worker.rs@@ -86,7 +86,12 @@ async fn main() {let obey_robots = std::env::var("OBSCURA_OBEY_ROBOTS").map(|v| matches!(v.trim(), "1" | "true" | "yes" | "on")).unwrap_or(false);- let mut context = BrowserContext::with_options("worker".to_string(), proxy, stealth);+ let user_agent = std::env::var("OBSCURA_USER_AGENT")+ .ok()+ .map(|value| value.trim().to_string())+ .filter(|value| !value.is_empty());+ let mut context =+ BrowserContext::with_full_options("worker".to_string(), proxy, stealth, user_agent);context.obey_robots = obey_robots;let context = Arc::new(context);let mut page = Page::new("page-1".to_string(), context);