urlsup 2.1.0

CLI to validate URLs in files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    /// Timeout in seconds for HTTP requests
    pub timeout: Option<u64>,

    /// Number of concurrent threads for validation
    pub threads: Option<usize>,

    /// Allow URLs that timeout
    pub allow_timeout: Option<bool>,

    /// File extensions to process
    pub file_types: Option<Vec<String>>,

    /// URL patterns to exclude (regex)
    pub exclude_patterns: Option<Vec<String>>,

    /// URLs to allowlist  
    pub allowlist: Option<Vec<String>>,

    /// HTTP status codes to allow
    pub allowed_status_codes: Option<Vec<u16>>,

    /// Custom User-Agent header
    pub user_agent: Option<String>,

    /// Retry attempts for failed requests
    pub retry_attempts: Option<u8>,

    /// Delay between retries in milliseconds
    pub retry_delay: Option<u64>,

    /// Skip SSL certificate verification
    pub skip_ssl_verification: Option<bool>,

    /// HTTP/HTTPS proxy URL
    pub proxy: Option<String>,

    /// Rate limiting: delay between requests in milliseconds
    pub rate_limit_delay: Option<u64>,

    /// Output format (text, json)
    pub output_format: Option<String>,

    /// Enable verbose logging
    pub verbose: Option<bool>,

    /// Use HEAD requests instead of GET for faster validation (some servers may not support)
    pub use_head_requests: Option<bool>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            timeout: Some(30),
            threads: None, // Will default to CPU core count
            allow_timeout: Some(false),
            file_types: None,
            exclude_patterns: None,
            allowlist: None,
            allowed_status_codes: None,
            user_agent: None,
            retry_attempts: Some(0),
            retry_delay: Some(1000),
            skip_ssl_verification: Some(false),
            proxy: None,
            rate_limit_delay: Some(0),
            output_format: Some("text".to_string()),
            verbose: Some(false),
            use_head_requests: Some(false), // Default to GET for compatibility
        }
    }
}

impl Config {
    /// Load configuration from file, falling back to defaults
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
        let content = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }

    /// Try to find and load a config file in standard locations
    pub fn load_from_standard_locations() -> Self {
        // Check for .urlsup.toml in current directory
        if let Ok(config) = Self::load_from_file(".urlsup.toml") {
            return config;
        }

        // Check for .urlsup.toml in parent directories (up to 3 levels)
        for i in 1..=3 {
            let path = format!("{}.urlsup.toml", "../".repeat(i));
            if let Ok(config) = Self::load_from_file(&path) {
                return config;
            }
        }

        // Fall back to defaults
        Self::default()
    }

    /// Merge this config with CLI arguments (CLI takes precedence)
    pub fn merge_with_cli(&mut self, cli_config: &CliConfig) {
        // Core options
        if let Some(timeout) = cli_config.timeout {
            self.timeout = Some(timeout);
        }

        // Filtering & inclusion
        if let Some(ref file_types) = cli_config.file_types {
            self.file_types = Some(file_types.clone());
        }
        if let Some(ref allowlist) = cli_config.allowlist {
            self.allowlist = Some(allowlist.clone());
        }
        if let Some(ref allowed_status_codes) = cli_config.allowed_status_codes {
            self.allowed_status_codes = Some(allowed_status_codes.clone());
        }
        if let Some(ref exclude_patterns) = cli_config.exclude_patterns {
            self.exclude_patterns = Some(exclude_patterns.clone());
        }

        // Performance & behavior
        if let Some(threads) = cli_config.threads {
            self.threads = Some(threads);
        }
        if let Some(retry_attempts) = cli_config.retry_attempts {
            self.retry_attempts = Some(retry_attempts);
        }
        if let Some(retry_delay) = cli_config.retry_delay {
            self.retry_delay = Some(retry_delay);
        }
        if let Some(rate_limit_delay) = cli_config.rate_limit_delay {
            self.rate_limit_delay = Some(rate_limit_delay);
        }
        if cli_config.allow_timeout {
            self.allow_timeout = Some(true);
        }

        // Output & format
        if cli_config.verbose {
            self.verbose = Some(true);
        }
        if let Some(ref output_format) = cli_config.output_format {
            self.output_format = Some(output_format.clone());
        }

        // Network & security
        if let Some(ref user_agent) = cli_config.user_agent {
            self.user_agent = Some(user_agent.clone());
        }
        if let Some(ref proxy) = cli_config.proxy {
            self.proxy = Some(proxy.clone());
        }
        if cli_config.skip_ssl_verification {
            self.skip_ssl_verification = Some(true);
        }
    }

    /// Compile exclude patterns into regex objects
    pub fn compile_exclude_patterns(&self) -> Result<Vec<Regex>, Box<dyn std::error::Error>> {
        let mut compiled = Vec::new();
        if let Some(ref patterns) = self.exclude_patterns {
            for pattern in patterns {
                compiled.push(Regex::new(pattern)?);
            }
        }
        Ok(compiled)
    }

    /// Convert file_types to HashSet for compatibility
    pub fn file_types_as_set(&self) -> Option<HashSet<String>> {
        self.file_types
            .as_ref()
            .map(|types| types.iter().cloned().collect())
    }

    /// Get timeout as Duration
    pub fn timeout_duration(&self) -> Duration {
        Duration::from_secs(self.timeout.unwrap_or(30))
    }

    /// Get retry delay as Duration
    pub fn retry_delay_duration(&self) -> Duration {
        Duration::from_millis(self.retry_delay.unwrap_or(1000))
    }

    /// Get rate limit delay as Duration
    pub fn rate_limit_delay_duration(&self) -> Duration {
        Duration::from_millis(self.rate_limit_delay.unwrap_or(0))
    }
}

/// Configuration options that can come from CLI
#[derive(Debug, Default)]
pub struct CliConfig {
    // Core options
    pub timeout: Option<u64>,

    // Filtering & inclusion
    pub file_types: Option<Vec<String>>,        // --include
    pub allowlist: Option<Vec<String>>,         // --allowlist
    pub allowed_status_codes: Option<Vec<u16>>, // --allow-status
    pub exclude_patterns: Option<Vec<String>>,  // --exclude-pattern

    // Performance & behavior
    pub threads: Option<usize>,        // --concurrency (was threads)
    pub retry_attempts: Option<u8>,    // --retry
    pub retry_delay: Option<u64>,      // --retry-delay
    pub rate_limit_delay: Option<u64>, // --rate-limit
    pub allow_timeout: bool,           // --allow-timeout

    // Output & format
    pub quiet: bool,                   // --quiet
    pub verbose: bool,                 // --verbose
    pub output_format: Option<String>, // --format
    pub no_progress: bool,             // --no-progress

    // Network & security
    pub user_agent: Option<String>,  // --user-agent
    pub proxy: Option<String>,       // --proxy
    pub skip_ssl_verification: bool, // --insecure

    // Configuration
    pub config_file: Option<String>, // --config
    pub no_config: bool,             // --no-config
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_config_default() {
        let config = Config::default();
        assert_eq!(config.timeout, Some(30));
        assert_eq!(config.allow_timeout, Some(false));
        assert_eq!(config.retry_attempts, Some(0));
        assert_eq!(config.output_format, Some("text".to_string()));
    }

    #[test]
    fn test_config_load_from_file() -> Result<(), Box<dyn std::error::Error>> {
        let mut file = tempfile::NamedTempFile::new()?;
        file.write_all(b"timeout = 60\nallow_timeout = true\nuser_agent = \"test-agent\"")?;

        let config = Config::load_from_file(file.path())?;
        assert_eq!(config.timeout, Some(60));
        assert_eq!(config.allow_timeout, Some(true));
        assert_eq!(config.user_agent, Some("test-agent".to_string()));

        Ok(())
    }

    #[test]
    fn test_config_merge_with_cli() {
        let mut config = Config::default();
        let cli_config = CliConfig {
            timeout: Some(45),
            allow_timeout: true,
            verbose: true,
            ..Default::default()
        };

        config.merge_with_cli(&cli_config);

        assert_eq!(config.timeout, Some(45));
        assert_eq!(config.allow_timeout, Some(true));
        assert_eq!(config.verbose, Some(true));
    }

    #[test]
    fn test_compile_exclude_patterns() -> Result<(), Box<dyn std::error::Error>> {
        let config = Config {
            exclude_patterns: Some(vec![
                r"^https://example\.com/.*".to_string(),
                r".*\.local$".to_string(),
            ]),
            ..Default::default()
        };

        let patterns = config.compile_exclude_patterns()?;
        assert_eq!(patterns.len(), 2);

        assert!(patterns[0].is_match("https://example.com/test"));
        assert!(!patterns[0].is_match("https://other.com/test"));

        assert!(patterns[1].is_match("http://test.local"));
        assert!(!patterns[1].is_match("http://test.com"));

        Ok(())
    }

    #[test]
    fn test_compile_exclude_patterns_empty() -> Result<(), Box<dyn std::error::Error>> {
        let config = Config {
            exclude_patterns: None,
            ..Default::default()
        };

        let patterns = config.compile_exclude_patterns()?;
        assert_eq!(patterns.len(), 0);

        Ok(())
    }

    #[test]
    fn test_compile_exclude_patterns_invalid_regex() {
        let config = Config {
            exclude_patterns: Some(vec![r"[invalid regex".to_string()]),
            ..Default::default()
        };

        assert!(config.compile_exclude_patterns().is_err());
    }

    #[test]
    fn test_file_types_as_set() {
        let config = Config {
            file_types: Some(vec![
                "md".to_string(),
                "txt".to_string(),
                "html".to_string(),
            ]),
            ..Default::default()
        };

        let set = config.file_types_as_set().unwrap();
        assert_eq!(set.len(), 3);
        assert!(set.contains("md"));
        assert!(set.contains("txt"));
        assert!(set.contains("html"));
        assert!(!set.contains("py"));
    }

    #[test]
    fn test_file_types_as_set_none() {
        let config = Config {
            file_types: None,
            ..Default::default()
        };

        assert!(config.file_types_as_set().is_none());
    }

    #[test]
    fn test_timeout_duration() {
        let config = Config {
            timeout: Some(45),
            ..Default::default()
        };

        assert_eq!(config.timeout_duration(), Duration::from_secs(45));

        let default_config = Config {
            timeout: None,
            ..Default::default()
        };

        assert_eq!(default_config.timeout_duration(), Duration::from_secs(30));
    }

    #[test]
    fn test_retry_delay_duration() {
        let config = Config {
            retry_delay: Some(2500),
            ..Default::default()
        };

        assert_eq!(config.retry_delay_duration(), Duration::from_millis(2500));

        let default_config = Config {
            retry_delay: None,
            ..Default::default()
        };

        assert_eq!(
            default_config.retry_delay_duration(),
            Duration::from_millis(1000)
        );
    }

    #[test]
    fn test_rate_limit_delay_duration() {
        let config = Config {
            rate_limit_delay: Some(500),
            ..Default::default()
        };

        assert_eq!(
            config.rate_limit_delay_duration(),
            Duration::from_millis(500)
        );

        let default_config = Config {
            rate_limit_delay: None,
            ..Default::default()
        };

        assert_eq!(
            default_config.rate_limit_delay_duration(),
            Duration::from_millis(0)
        );
    }

    #[test]
    fn test_config_load_from_standard_locations() {
        // This test ensures that the function doesn't panic even if no config file exists
        let config = Config::load_from_standard_locations();
        // Should fall back to defaults
        assert_eq!(config.timeout, Some(30));
        assert_eq!(config.allow_timeout, Some(false));
    }

    #[test]
    fn test_config_merge_with_cli_all_fields() {
        let mut config = Config::default();
        let cli_config = CliConfig {
            timeout: Some(60),
            file_types: Some(vec!["md".to_string(), "html".to_string()]),
            allowlist: Some(vec!["example.com".to_string()]),
            allowed_status_codes: Some(vec![404, 429]),
            exclude_patterns: Some(vec![r".*\.local$".to_string()]),
            threads: Some(8),
            retry_attempts: Some(3),
            retry_delay: Some(2000),
            rate_limit_delay: Some(100),
            allow_timeout: true,
            quiet: true,
            verbose: true,
            output_format: Some("json".to_string()),
            no_progress: true,
            user_agent: Some("test-agent".to_string()),
            proxy: Some("http://proxy.test:8080".to_string()),
            skip_ssl_verification: true,
            config_file: Some("/path/to/config".to_string()),
            no_config: true,
        };

        config.merge_with_cli(&cli_config);

        assert_eq!(config.timeout, Some(60));
        assert_eq!(
            config.file_types,
            Some(vec!["md".to_string(), "html".to_string()])
        );
        assert_eq!(config.allowlist, Some(vec!["example.com".to_string()]));
        assert_eq!(config.allowed_status_codes, Some(vec![404, 429]));
        assert_eq!(
            config.exclude_patterns,
            Some(vec![r".*\.local$".to_string()])
        );
        assert_eq!(config.threads, Some(8));
        assert_eq!(config.retry_attempts, Some(3));
        assert_eq!(config.retry_delay, Some(2000));
        assert_eq!(config.rate_limit_delay, Some(100));
        assert_eq!(config.allow_timeout, Some(true));
        assert_eq!(config.verbose, Some(true));
        assert_eq!(config.output_format, Some("json".to_string()));
        assert_eq!(config.user_agent, Some("test-agent".to_string()));
        assert_eq!(config.proxy, Some("http://proxy.test:8080".to_string()));
        assert_eq!(config.skip_ssl_verification, Some(true));
    }

    #[test]
    fn test_config_load_from_file_invalid_toml() {
        let mut file = tempfile::NamedTempFile::new().unwrap();
        file.write_all(b"invalid toml content [").unwrap();

        let result = Config::load_from_file(file.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_config_load_from_file_nonexistent() {
        let result = Config::load_from_file("/path/that/does/not/exist.toml");
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_config_default() {
        let cli_config = CliConfig::default();
        assert_eq!(cli_config.timeout, None);
        assert_eq!(cli_config.file_types, None);
        assert_eq!(cli_config.allowlist, None);
        assert_eq!(cli_config.allowed_status_codes, None);
        assert_eq!(cli_config.exclude_patterns, None);
        assert_eq!(cli_config.threads, None);
        assert_eq!(cli_config.retry_attempts, None);
        assert_eq!(cli_config.retry_delay, None);
        assert_eq!(cli_config.rate_limit_delay, None);
        assert!(!cli_config.allow_timeout);
        assert!(!cli_config.quiet);
        assert!(!cli_config.verbose);
        assert_eq!(cli_config.output_format, None);
        assert!(!cli_config.no_progress);
        assert_eq!(cli_config.user_agent, None);
        assert_eq!(cli_config.proxy, None);
        assert!(!cli_config.skip_ssl_verification);
        assert_eq!(cli_config.config_file, None);
        assert!(!cli_config.no_config);
    }

    #[test]
    fn test_config_empty_compile_exclude_patterns() -> Result<(), Box<dyn std::error::Error>> {
        let config = Config {
            exclude_patterns: Some(vec![]),
            ..Default::default()
        };

        let patterns = config.compile_exclude_patterns()?;
        assert_eq!(patterns.len(), 0);

        Ok(())
    }
}