domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Utility functions for database insert operations.
//!
//! This module provides helper functions for parsing and converting data
//! before insertion into the database.

use chrono::NaiveDateTime;

/// Converts a `NaiveDateTime` to milliseconds since Unix epoch.
pub(crate) fn naive_datetime_to_millis(datetime: Option<&NaiveDateTime>) -> Option<i64> {
    datetime.map(|dt| dt.and_utc().timestamp_millis())
}

/// Parses a JSON array string into a Vec<String>.
/// Returns None if the string is None or empty, or if parsing fails.
pub(crate) fn parse_json_array(json_str: Option<&String>) -> Option<Vec<String>> {
    let json_str = json_str?;
    if json_str.is_empty() {
        return None;
    }
    serde_json::from_str::<Vec<String>>(json_str).ok()
}

/// Detects the type of a TXT record based on its content.
/// Returns "SPF", "DMARC", "VERIFICATION", or "OTHER".
///
/// SPF/DMARC classification shares predicates with [`crate::dns`] extractors
/// so case-sensitivity cannot drift.
pub(crate) fn detect_txt_type(txt: &str) -> &'static str {
    if crate::dns::is_spf_txt(txt) {
        "SPF"
    } else if crate::dns::is_dmarc_txt(txt) {
        "DMARC"
    } else if crate::dns::is_mta_sts_txt(txt) {
        "MTA-STS"
    } else if crate::dns::is_tls_rpt_txt(txt) {
        "TLS-RPT"
    } else if crate::dns::is_bimi_txt(txt) {
        "BIMI"
    } else {
        let txt_lower = txt.to_lowercase();
        if txt_lower.contains("google-site-verification")
            || txt_lower.contains("ms-verify")
            || txt_lower.contains("facebook-domain-verification")
            || txt_lower.contains("atlassian-domain-verification")
        {
            "VERIFICATION"
        } else {
            "OTHER"
        }
    }
}

/// Parses an MX record string into priority and hostname.
/// Expected format: "10 mail.example.com" or just "mail.example.com" (default priority 0)
/// Returns None if parsing fails.
pub(crate) fn parse_mx_record(mx: &str) -> Option<(i32, String)> {
    let parts: Vec<&str> = mx.split_whitespace().collect();
    if parts.is_empty() {
        return None;
    }

    // Try to parse first part as priority
    if let Ok(priority) = parts[0].parse::<i32>() {
        if parts.len() >= 2 {
            Some((priority, parts[1].to_string()))
        } else {
            None
        }
    } else {
        // No priority specified, use default 0
        Some((0, parts[0].to_string()))
    }
}

/// Parses MX records from JSON array format.
/// Expected JSON format: [{"priority": 10, "hostname": "mail.example.com"}, ...]
/// Returns Vec of (priority, hostname) tuples.
pub(crate) fn parse_mx_json_array(json_str: Option<&String>) -> Option<Vec<(i32, String)>> {
    let json_str = json_str?;
    if json_str.is_empty() {
        return None;
    }

    // Try to parse as array of objects first
    if let Ok(mx_objects) = serde_json::from_str::<Vec<serde_json::Value>>(json_str) {
        let mut result = Vec::new();
        for obj in mx_objects {
            // SAFETY: Cast i64 to i32 for MX record priority
            // - MX priority is defined in RFC 5321 as an unsigned 16-bit value (0-65535)
            // - DNS servers enforce this constraint, so values > 65535 are invalid
            // - i32 can hold values up to 2^31-1 (2.1B), which is >> 65535
            // - If JSON contains invalid priority > i32::MAX, cast will truncate/wrap
            // - This is acceptable: invalid priorities are treated as errors during insertion
            #[allow(clippy::cast_possible_truncation)]
            if let (Some(priority), Some(hostname)) = (
                obj.get("priority")
                    .and_then(|v| v.as_i64().map(|p| p as i32)),
                obj.get("hostname").and_then(|v| v.as_str()),
            ) {
                result.push((priority, hostname.to_string()));
            }
        }
        if !result.is_empty() {
            return Some(result);
        }
    }

    // Fallback: try parsing as array of strings
    if let Ok(mx_strings) = serde_json::from_str::<Vec<String>>(json_str) {
        let mut result = Vec::new();
        for mx_str in mx_strings {
            if let Some((priority, hostname)) = parse_mx_record(&mx_str) {
                result.push((priority, hostname));
            }
        }
        if !result.is_empty() {
            return Some(result);
        }
    }

    None
}

/// Builds a batch INSERT SQL query string for satellite tables.
///
/// This helper function reduces code duplication by generating the SQL query
/// string with proper placeholders. Each function still handles its own
/// binding logic since the value types differ.
///
/// # Arguments
///
/// * `table_name` - Name of the table to insert into
/// * `columns` - Column names (e.g., `["url_status_id", "nameserver"]`)
/// * `row_count` - Number of rows to insert
/// * `conflict_clause` - Optional conflict resolution clause (e.g., `"ON CONFLICT(...) DO NOTHING"`)
///
/// # Returns
///
/// The formatted SQL query string ready for binding.
///
/// # Example
///
/// ```rust,ignore
/// let query = build_batch_insert_query(
///     "url_nameservers",
///     &["url_status_id", "nameserver"],
///     2,
///     Some("ON CONFLICT(url_status_id, nameserver) DO NOTHING"),
/// );
/// // Returns: "INSERT INTO url_nameservers (url_status_id, nameserver) VALUES (?, ?), (?, ?) ON CONFLICT(...)"
/// ```
pub(crate) fn build_batch_insert_query(
    table_name: &str,
    columns: &[&str],
    row_count: usize,
    conflict_clause: Option<&str>,
) -> String {
    if row_count == 0 {
        return String::new();
    }

    let num_columns = columns.len();
    let placeholder = format!(
        "({})",
        (0..num_columns).map(|_| "?").collect::<Vec<_>>().join(", ")
    );
    let placeholders: Vec<String> = (0..row_count).map(|_| placeholder.clone()).collect();

    let mut query = format!(
        "INSERT INTO {} ({}) VALUES {}",
        table_name,
        columns.join(", "),
        placeholders.join(", ")
    );

    if let Some(conflict) = conflict_clause {
        query.push(' ');
        query.push_str(conflict);
    }

    query
}

/// Generic batch insert helper for key-value satellite tables.
///
/// This function eliminates code duplication across headers, DNS records, and other
/// satellite tables by providing a single implementation for batch insertion with
/// consistent error handling.
///
/// # Arguments
///
/// * `tx` - Database transaction
/// * `table_name` - Name of the table to insert into
/// * `parent_id_column` - Name of the parent ID column (e.g., "`url_status_id`")
/// * `key_column` - Name of the key column (e.g., "`header_name`", "nameserver")
/// * `value_column` - Name of the value column (e.g., "`header_value`")
/// * `parent_id` - The parent record ID
/// * `data` - Slice of (key, value) tuples to insert
/// * `conflict_clause` - Optional conflict resolution clause
///
/// # Returns
///
/// `Result<(), sqlx::Error>` - Ok on success, Err on database error
///
/// # Example
///
/// ```rust,ignore
/// let headers = vec![("Server", "nginx"), ("X-Powered-By", "PHP")];
/// insert_key_value_batch(
///     &mut tx,
///     "url_http_headers",
///     "url_status_id",
///     "header_name",
///     "header_value",
///     url_status_id,
///     &headers,
///     Some("ON CONFLICT(url_status_id, header_name) DO UPDATE SET header_value=excluded.header_value"),
/// ).await?;
/// ```
#[allow(clippy::too_many_arguments)] // 8 parameters needed for generic flexibility
pub(crate) async fn insert_key_value_batch<K, V>(
    tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
    table_name: &str,
    parent_id_column: &str,
    key_column: &str,
    value_column: &str,
    parent_id: i64,
    data: &[(K, V)],
    conflict_clause: Option<&str>,
) -> Result<(), sqlx::Error>
where
    K: for<'q> sqlx::Encode<'q, sqlx::Sqlite> + sqlx::Type<sqlx::Sqlite> + Clone,
    V: for<'q> sqlx::Encode<'q, sqlx::Sqlite> + sqlx::Type<sqlx::Sqlite> + Clone,
{
    if data.is_empty() {
        return Ok(());
    }

    // Build the SQL query
    let query = build_batch_insert_query(
        table_name,
        &[parent_id_column, key_column, value_column],
        data.len(),
        conflict_clause,
    );

    // Bind parameters
    let mut query_builder = crate::sql::query(query);
    for (key, value) in data {
        query_builder = query_builder
            .bind(parent_id)
            .bind(key.clone())
            .bind(value.clone());
    }

    // Execute
    query_builder.execute(&mut **tx).await?;

    Ok(())
}

/// Generic batch insert helper for single-column satellite tables.
///
/// This function is similar to `insert_key_value_batch` but for tables with
/// only one data column (e.g., nameservers, redirect chains).
///
/// # Arguments
///
/// * `tx` - Database transaction
/// * `table_name` - Name of the table to insert into
/// * `parent_id_column` - Name of the parent ID column (e.g., "`url_status_id`")
/// * `value_column` - Name of the value column (e.g., "nameserver")
/// * `parent_id` - The parent record ID
/// * `data` - Slice of values to insert
/// * `conflict_clause` - Optional conflict resolution clause
///
/// # Returns
///
/// `Result<(), sqlx::Error>` - Ok on success, Err on database error
pub(crate) async fn insert_single_column_batch<V>(
    tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
    table_name: &str,
    parent_id_column: &str,
    value_column: &str,
    parent_id: i64,
    data: &[V],
    conflict_clause: Option<&str>,
) -> Result<(), sqlx::Error>
where
    V: for<'q> sqlx::Encode<'q, sqlx::Sqlite> + sqlx::Type<sqlx::Sqlite> + Clone,
{
    if data.is_empty() {
        return Ok(());
    }

    // Build the SQL query
    let query = build_batch_insert_query(
        table_name,
        &[parent_id_column, value_column],
        data.len(),
        conflict_clause,
    );

    // Bind parameters
    let mut query_builder = crate::sql::query(query);
    for value in data {
        query_builder = query_builder.bind(parent_id).bind(value.clone());
    }

    // Execute
    query_builder.execute(&mut **tx).await?;

    Ok(())
}

/// Deletes child rows for `url_status_id` from compile-time table-name lists.
pub(crate) async fn delete_child_rows(
    tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
    tables: &[&str],
    url_status_id: i64,
) -> Result<(), sqlx::Error> {
    for table in tables {
        let sql = format!("DELETE FROM {table} WHERE url_status_id = ?");
        crate::sql::query(sql)
            .bind(url_status_id)
            .execute(&mut **tx)
            .await?;
    }
    Ok(())
}

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

    #[test]
    fn test_naive_datetime_to_millis_some() {
        let dt = NaiveDate::from_ymd_opt(2024, 1, 1)
            .unwrap()
            .and_hms_opt(12, 0, 0)
            .unwrap();
        let result = naive_datetime_to_millis(Some(&dt));
        assert!(result.is_some());
        // Verify it's a reasonable timestamp (milliseconds since epoch)
        let millis = result.unwrap();
        assert!(millis > 1_700_000_000_000); // Should be > Jan 1, 2024
        assert!(millis < 2_000_000_000_000); // Should be < year 2033
    }

    #[test]
    fn test_naive_datetime_to_millis_none() {
        assert_eq!(naive_datetime_to_millis(None), None);
    }

    #[test]
    fn test_parse_json_array_valid() {
        let json = Some(r#"["item1", "item2", "item3"]"#.to_string());
        let result = parse_json_array(json.as_ref());
        assert_eq!(
            result,
            Some(vec![
                "item1".to_string(),
                "item2".to_string(),
                "item3".to_string()
            ])
        );
    }

    #[test]
    fn test_parse_json_array_empty_string() {
        let json = Some("".to_string());
        assert_eq!(parse_json_array(json.as_ref()), None);
    }

    #[test]
    fn test_parse_json_array_none() {
        assert_eq!(parse_json_array(None), None);
    }

    #[test]
    fn test_parse_json_array_invalid_json() {
        let json = Some("not valid json".to_string());
        assert_eq!(parse_json_array(json.as_ref()), None);
    }

    #[test]
    fn test_parse_json_array_not_array() {
        let json = Some(r#"{"key": "value"}"#.to_string());
        assert_eq!(parse_json_array(json.as_ref()), None);
    }

    #[test]
    fn test_detect_txt_type_spf() {
        assert_eq!(
            detect_txt_type("v=spf1 include:_spf.google.com ~all"),
            "SPF"
        );
        assert_eq!(
            detect_txt_type("V=SPF1 include:_spf.google.com ~all"),
            "SPF"
        ); // Case insensitive
    }

    #[test]
    fn test_detect_txt_type_dmarc() {
        assert_eq!(detect_txt_type("v=dmarc1; p=none"), "DMARC");
        assert_eq!(detect_txt_type("V=DMARC1; p=quarantine"), "DMARC"); // Case insensitive
    }

    #[test]
    fn test_detect_txt_type_verification_google() {
        assert_eq!(
            detect_txt_type("google-site-verification=abc123"),
            "VERIFICATION"
        );
    }

    #[test]
    fn test_detect_txt_type_verification_ms() {
        assert_eq!(detect_txt_type("ms-verify=xyz789"), "VERIFICATION");
    }

    #[test]
    fn test_detect_txt_type_verification_facebook() {
        assert_eq!(
            detect_txt_type("facebook-domain-verification=def456"),
            "VERIFICATION"
        );
    }

    #[test]
    fn test_detect_txt_type_verification_atlassian() {
        assert_eq!(
            detect_txt_type("atlassian-domain-verification=ghi012"),
            "VERIFICATION"
        );
    }

    #[test]
    fn test_detect_txt_type_other() {
        assert_eq!(detect_txt_type("some other text record"), "OTHER");
        // Empty/whitespace TXT is filtered before classify/insert (`is_storable_txt`);
        // it is not a stored `record_type`.
        assert!(!crate::dns::is_storable_txt(""));
        assert!(!crate::dns::is_storable_txt("   "));
    }

    #[test]
    fn test_parse_mx_record_with_priority() {
        assert_eq!(
            parse_mx_record("10 mail.example.com"),
            Some((10, "mail.example.com".to_string()))
        );
        assert_eq!(
            parse_mx_record("0 mail.example.com"),
            Some((0, "mail.example.com".to_string()))
        );
        assert_eq!(
            parse_mx_record("100 smtp.example.com"),
            Some((100, "smtp.example.com".to_string()))
        );
    }

    #[test]
    fn test_parse_mx_record_without_priority() {
        assert_eq!(
            parse_mx_record("mail.example.com"),
            Some((0, "mail.example.com".to_string()))
        );
    }

    #[test]
    fn test_parse_mx_record_multiple_spaces() {
        assert_eq!(
            parse_mx_record("10   mail.example.com"),
            Some((10, "mail.example.com".to_string()))
        );
    }

    #[test]
    fn test_parse_mx_record_invalid() {
        assert_eq!(parse_mx_record(""), None);
        assert_eq!(parse_mx_record("10"), None); // Priority but no hostname
    }

    #[test]
    fn test_parse_mx_json_array_objects_format() {
        let json = Some(r#"[{"priority": 10, "hostname": "mail1.example.com"}, {"priority": 20, "hostname": "mail2.example.com"}]"#.to_string());
        let result = parse_mx_json_array(json.as_ref());
        assert_eq!(
            result,
            Some(vec![
                (10, "mail1.example.com".to_string()),
                (20, "mail2.example.com".to_string())
            ])
        );
    }

    #[test]
    fn test_parse_mx_json_array_strings_format() {
        let json = Some(r#"["10 mail1.example.com", "20 mail2.example.com"]"#.to_string());
        let result = parse_mx_json_array(json.as_ref());
        assert_eq!(
            result,
            Some(vec![
                (10, "mail1.example.com".to_string()),
                (20, "mail2.example.com".to_string())
            ])
        );
    }

    #[test]
    fn test_parse_mx_json_array_strings_without_priority() {
        let json = Some(r#"["mail1.example.com", "mail2.example.com"]"#.to_string());
        let result = parse_mx_json_array(json.as_ref());
        assert_eq!(
            result,
            Some(vec![
                (0, "mail1.example.com".to_string()),
                (0, "mail2.example.com".to_string())
            ])
        );
    }

    #[test]
    fn test_parse_mx_json_array_empty() {
        assert_eq!(parse_mx_json_array(None), None);
        assert_eq!(parse_mx_json_array(Some(&"".to_string())), None);
        assert_eq!(parse_mx_json_array(Some(&"[]".to_string())), None);
    }

    #[test]
    fn test_parse_mx_json_array_invalid_json() {
        assert_eq!(parse_mx_json_array(Some(&"not json".to_string())), None);
    }

    #[test]
    fn test_parse_mx_json_array_mixed_formats() {
        // Objects format takes precedence
        let json = Some(
            r#"[{"priority": 10, "hostname": "mail.example.com"}, "20 mail2.example.com"]"#
                .to_string(),
        );
        let result = parse_mx_json_array(json.as_ref());
        // Should parse objects first, ignore strings if objects found
        assert!(result.is_some());
        assert_eq!(result.unwrap().len(), 1); // Only the object entry
    }

    #[test]
    fn test_parse_mx_json_array_incomplete_object() {
        // Object missing priority or hostname should be skipped
        let json = Some(r#"[{"priority": 10}, {"hostname": "mail.example.com"}]"#.to_string());
        let result = parse_mx_json_array(json.as_ref());
        assert_eq!(result, None); // Empty result after filtering
    }
}