siteone-crawler 2.5.1

Website crawler and QA toolkit in Rust for security, performance, SEO, and accessibility audits, offline cloning, markdown export, sitemap generation, cache warming, and CI/CD gating — one dependency-free binary for all major platforms, 10 tools in one.
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
// SiteOne Crawler - BaseProcessor shared utilities
// (c) Jan Reges <jan.reges@siteone.cz>
//
// Provides shared utility methods used by all content processors.

use std::sync::Arc;

use crate::engine::parsed_url::ParsedUrl;
use crate::export::utils::offline_url_converter::OfflineUrlConverter;
use crate::types::ContentTypeId;

/// Predicate that decides whether a domain is allowed (for static files / crawling).
pub type DomainAllowFn = Arc<dyn Fn(&str) -> bool + Send + Sync>;

/// Configuration extracted from CoreOptions, shared across processors.
/// This avoids each processor needing a reference to the full crawler.
#[derive(Clone)]
pub struct ProcessorConfig {
    pub single_page: bool,
    pub single_foreign_page: bool,
    pub max_depth: i64,
    pub files_enabled: bool,
    pub images_enabled: bool,
    pub scripts_enabled: bool,
    pub styles_enabled: bool,
    pub fonts_enabled: bool,
    pub disable_javascript: bool,
    pub remove_all_anchor_listeners: bool,
    pub ignore_regex: Vec<String>,
    /// Pre-compiled ignore regexes for hot path usage
    pub compiled_ignore_regex: Vec<regex::Regex>,
    pub disable_astro_inline_modules: bool,
    pub offline_export_preserve_urls: bool,
    pub offline_export_no_url_rewriting: bool,
    /// When true, URLs inside HTML comments (`<!-- ... -->`) are stripped before
    /// URL extraction, so commented links are not crawled or reported as broken.
    pub ignore_html_comments: bool,
    pub initial_url: ParsedUrl,
    /// Returns true if the given host is allowed for downloading static files
    /// (`--allowed-domain-for-external-files`). Used by offline export so that
    /// downloaded external/CDN assets are rewritten to their local copies.
    pub is_domain_allowed_for_static_files: Option<DomainAllowFn>,
    /// Returns true if the given host is allowed for whole-domain crawling
    /// (`--allowed-domain-for-crawling`).
    pub is_external_domain_allowed_for_crawling: Option<DomainAllowFn>,
}

impl std::fmt::Debug for ProcessorConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProcessorConfig")
            .field("single_page", &self.single_page)
            .field("single_foreign_page", &self.single_foreign_page)
            .field("max_depth", &self.max_depth)
            .field("files_enabled", &self.files_enabled)
            .field("images_enabled", &self.images_enabled)
            .field("scripts_enabled", &self.scripts_enabled)
            .field("styles_enabled", &self.styles_enabled)
            .field("fonts_enabled", &self.fonts_enabled)
            .field("disable_javascript", &self.disable_javascript)
            .field("remove_all_anchor_listeners", &self.remove_all_anchor_listeners)
            .field("ignore_regex", &self.ignore_regex)
            .field("compiled_ignore_regex", &self.compiled_ignore_regex)
            .field("disable_astro_inline_modules", &self.disable_astro_inline_modules)
            .field("offline_export_preserve_urls", &self.offline_export_preserve_urls)
            .field("offline_export_no_url_rewriting", &self.offline_export_no_url_rewriting)
            .field("ignore_html_comments", &self.ignore_html_comments)
            .field("initial_url", &self.initial_url)
            .field(
                "is_domain_allowed_for_static_files",
                &self.is_domain_allowed_for_static_files.is_some(),
            )
            .field(
                "is_external_domain_allowed_for_crawling",
                &self.is_external_domain_allowed_for_crawling.is_some(),
            )
            .finish()
    }
}

impl ProcessorConfig {
    pub fn new(initial_url: ParsedUrl) -> Self {
        Self {
            single_page: false,
            single_foreign_page: false,
            max_depth: 0,
            files_enabled: true,
            images_enabled: true,
            scripts_enabled: true,
            styles_enabled: true,
            fonts_enabled: true,
            disable_javascript: false,
            remove_all_anchor_listeners: false,
            ignore_regex: Vec::new(),
            compiled_ignore_regex: Vec::new(),
            disable_astro_inline_modules: false,
            offline_export_preserve_urls: false,
            offline_export_no_url_rewriting: false,
            ignore_html_comments: false,
            initial_url,
            is_domain_allowed_for_static_files: None,
            is_external_domain_allowed_for_crawling: None,
        }
    }

    /// Compile ignore_regex patterns into Regex objects for hot path usage.
    /// Call this after setting ignore_regex.
    pub fn compile_ignore_regex(&mut self) {
        self.compiled_ignore_regex = self
            .ignore_regex
            .iter()
            .filter_map(|pattern| regex::Regex::new(pattern).ok())
            .collect();
    }
}

/// Check if a content type is in the list of relevant types
pub fn is_relevant(content_type: ContentTypeId, relevant_types: &[ContentTypeId]) -> bool {
    relevant_types.contains(&content_type)
}

/// Normalize a URL path by resolving `.` and `..` segments.
fn normalize_path(path: &str) -> String {
    let mut segments: Vec<&str> = Vec::new();
    for segment in path.split('/') {
        match segment {
            "." => {}
            ".." => {
                segments.pop();
            }
            s => segments.push(s),
        }
    }
    let result = segments.join("/");
    if result.starts_with('/') {
        result
    } else {
        format!("/{}", result)
    }
}

/// Convert a URL to a relative path for offline use.
/// When `preserve_urls` is true, same-domain links become root-relative and cross-domain links stay absolute.
#[allow(clippy::too_many_arguments)]
pub fn convert_url_to_relative(
    base_url: &ParsedUrl,
    target_url: &str,
    initial_url: &ParsedUrl,
    attribute: Option<&str>,
    preserve_urls: bool,
    no_url_rewriting: bool,
    is_domain_allowed_for_static_files: Option<DomainAllowFn>,
    is_external_domain_allowed_for_crawling: Option<DomainAllowFn>,
) -> String {
    // No URL rewriting → return target URL completely unchanged
    if no_url_rewriting {
        return target_url.to_string();
    }

    // If it's a data URI, anchor, or non-http scheme, return as-is
    if target_url.starts_with("data:")
        || target_url.starts_with("javascript:")
        || target_url.starts_with("mailto:")
        || target_url.starts_with("tel:")
    {
        return target_url.to_string();
    }

    // Normalize HTML entities in URL before parsing so it matches what FoundUrl stored.
    // Only decode entities (not full normalize_url which also trims trailing &, quotes, etc.
    // — those transformations are for discovery, not for offline conversion of already-parsed URLs).
    let normalized = target_url.replace("&#38;", "&").replace("&amp;", "&");
    let parsed_target = ParsedUrl::parse(&normalized, Some(base_url));

    if preserve_urls {
        let target_host = parsed_target.host.as_deref().unwrap_or("");
        let initial_host = initial_url.host.as_deref().unwrap_or("");
        if target_host.is_empty() || target_host == initial_host {
            // Same domain → root-relative (path + query + fragment)
            // Normalize path segments (resolve .. and .)
            let normalized_path = normalize_path(&parsed_target.path);
            let mut result = normalized_path;
            if let Some(ref q) = parsed_target.query {
                result.push('?');
                result.push_str(q);
            }
            if let Some(ref f) = parsed_target.fragment {
                result.push('#');
                result.push_str(f);
            }
            return result;
        } else {
            // Cross domain → full absolute URL
            return parsed_target.get_full_url(true, true);
        }
    }

    let mut converter = OfflineUrlConverter::new(
        initial_url.clone(),
        base_url.clone(),
        parsed_target,
        is_domain_allowed_for_static_files,
        is_external_domain_allowed_for_crawling,
        attribute,
    );

    converter.convert_url_to_relative(true)
}

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

    fn initial_url() -> ParsedUrl {
        ParsedUrl::parse("https://example.com/", None)
    }

    #[test]
    fn decode_amp_entity_before_offline_conversion() {
        let base = ParsedUrl::parse("https://example.com/blog/", None);
        let result = convert_url_to_relative(
            &base,
            "/style.css?v=1&amp;t=2",
            &initial_url(),
            Some("href"),
            false,
            false,
            None,
            None,
        );
        // &amp; must be decoded to & so the query hash matches what FoundUrl stored
        assert!(
            !result.contains("&amp;"),
            "HTML entity &amp; should be decoded before conversion"
        );
    }

    #[test]
    fn decode_numeric_entity_before_offline_conversion() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "/page?a=1&#38;b=2",
            &initial_url(),
            Some("href"),
            false,
            false,
            None,
            None,
        );
        assert!(
            !result.contains("&#38;"),
            "HTML entity &#38; should be decoded before conversion"
        );
    }

    #[test]
    fn preserve_trailing_ampersand() {
        // Trailing & in a query string should NOT be stripped (unlike in FoundUrl discovery)
        let base = ParsedUrl::parse("https://example.com/", None);
        let a = convert_url_to_relative(
            &base,
            "/page?a=1&",
            &initial_url(),
            Some("href"),
            false,
            false,
            None,
            None,
        );
        let b = convert_url_to_relative(
            &base,
            "/page?a=1&b=",
            &initial_url(),
            Some("href"),
            false,
            false,
            None,
            None,
        );
        // Both should produce different results (trailing & matters for hash)
        assert_ne!(a, b, "trailing & should be preserved, not stripped");
    }

    #[test]
    fn skip_data_uri() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "data:image/png;base64,abc",
            &initial_url(),
            None,
            false,
            false,
            None,
            None,
        );
        assert_eq!(result, "data:image/png;base64,abc");
    }

    #[test]
    fn skip_javascript_uri() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "javascript:void(0)",
            &initial_url(),
            None,
            false,
            false,
            None,
            None,
        );
        assert_eq!(result, "javascript:void(0)");
    }

    // --- preserve_urls tests ---

    #[test]
    fn preserve_urls_same_domain_absolute() {
        let base = ParsedUrl::parse("https://example.com/blog/post", None);
        let result = convert_url_to_relative(
            &base,
            "https://example.com/designy/classic",
            &initial_url(),
            Some("href"),
            true,
            false,
            None,
            None,
        );
        assert_eq!(result, "/designy/classic");
    }

    #[test]
    fn preserve_urls_same_domain_root_relative() {
        let base = ParsedUrl::parse("https://example.com/blog/post", None);
        let result = convert_url_to_relative(&base, "/about", &initial_url(), Some("href"), true, false, None, None);
        assert_eq!(result, "/about");
    }

    #[test]
    fn preserve_urls_same_domain_relative() {
        let base = ParsedUrl::parse("https://example.com/blog/post", None);
        let result = convert_url_to_relative(
            &base,
            "../images/logo.png",
            &initial_url(),
            Some("src"),
            true,
            false,
            None,
            None,
        );
        assert_eq!(result, "/images/logo.png");
    }

    #[test]
    fn preserve_urls_cross_domain() {
        let base = ParsedUrl::parse("https://example.com/page", None);
        let result = convert_url_to_relative(
            &base,
            "https://cdn.other.com/style.css",
            &initial_url(),
            Some("href"),
            true,
            false,
            None,
            None,
        );
        assert_eq!(result, "https://cdn.other.com/style.css");
    }

    #[test]
    fn preserve_urls_with_query_and_fragment() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "/page?key=val#section",
            &initial_url(),
            Some("href"),
            true,
            false,
            None,
            None,
        );
        assert_eq!(result, "/page?key=val#section");
    }

    #[test]
    fn preserve_urls_data_uri_unchanged() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "data:image/png;base64,abc",
            &initial_url(),
            None,
            true,
            false,
            None,
            None,
        );
        assert_eq!(result, "data:image/png;base64,abc");
    }

    #[test]
    fn preserve_urls_mailto_unchanged() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "mailto:test@example.com",
            &initial_url(),
            None,
            true,
            false,
            None,
            None,
        );
        assert_eq!(result, "mailto:test@example.com");
    }

    // --- no_url_rewriting tests ---

    #[test]
    fn no_rewriting_absolute_url_unchanged() {
        let base = ParsedUrl::parse("https://example.com/blog/post", None);
        let result = convert_url_to_relative(
            &base,
            "https://example.com/designy/classic",
            &initial_url(),
            Some("href"),
            false,
            true,
            None,
            None,
        );
        assert_eq!(result, "https://example.com/designy/classic");
    }

    #[test]
    fn no_rewriting_relative_url_unchanged() {
        let base = ParsedUrl::parse("https://example.com/blog/post", None);
        let result = convert_url_to_relative(
            &base,
            "../images/logo.png",
            &initial_url(),
            Some("src"),
            false,
            true,
            None,
            None,
        );
        assert_eq!(result, "../images/logo.png");
    }

    #[test]
    fn no_rewriting_root_relative_unchanged() {
        let base = ParsedUrl::parse("https://example.com/blog/post", None);
        let result = convert_url_to_relative(&base, "/about", &initial_url(), Some("href"), false, true, None, None);
        assert_eq!(result, "/about");
    }

    #[test]
    fn no_rewriting_cross_domain_unchanged() {
        let base = ParsedUrl::parse("https://example.com/page", None);
        let result = convert_url_to_relative(
            &base,
            "https://cdn.other.com/style.css",
            &initial_url(),
            Some("href"),
            false,
            true,
            None,
            None,
        );
        assert_eq!(result, "https://cdn.other.com/style.css");
    }

    #[test]
    fn no_rewriting_preserves_html_entities() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "/page?a=1&amp;b=2",
            &initial_url(),
            Some("href"),
            false,
            true,
            None,
            None,
        );
        // With no_url_rewriting, even HTML entities are preserved verbatim
        assert_eq!(result, "/page?a=1&amp;b=2");
    }

    #[test]
    fn no_rewriting_with_query_and_fragment() {
        let base = ParsedUrl::parse("https://example.com/", None);
        let result = convert_url_to_relative(
            &base,
            "/page?key=val#section",
            &initial_url(),
            Some("href"),
            false,
            true,
            None,
            None,
        );
        assert_eq!(result, "/page?key=val#section");
    }
}