rdsh 0.5.2

Rust port/shim for dsh: fast launcher, token saver, zero behavior breakage via passthrough
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! `rdsh search-web`: web search through SearXNG (self-hosted default).
//! No API key needed: talks to `$SEARXNG_URL` (default `http://127.0.0.1:8888`)
//! over plain HTTP with a hand-rolled client (no new dependencies). The HTML
//! results page is parsed because many instances (including a default
//! local one) only serve HTML, not the JSON API.

fn default_base() -> String {
    match std::env::var("SEARXNG_URL") {
        Ok(s) => {
            let s = s.trim_end_matches('/').to_string();
            if s.is_empty() {
                "http://127.0.0.1:8888".to_string()
            } else {
                s
            }
        }
        Err(_) => {
            let u = crate::rdsh_config::load().search.searxng_url;
            let u = u.trim().trim_end_matches('/').to_string();
            if u.is_empty() {
                "http://127.0.0.1:8888".to_string()
            } else {
                u
            }
        }
    }
}

fn encode_query(s: &str) -> String {
    use std::fmt::Write as _;
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        if b.is_ascii_alphanumeric() || b"-_.~".contains(&b) {
            out.push(b as char);
        } else if b == b' ' {
            out.push('+');
        } else {
            let _ = write!(out, "%{b:02X}");
        }
    }
    out
}

/// Split an `http://host[:port][/prefix]` base URL.
fn split_base(base: &str, locale: crate::locale::Locale) -> anyhow::Result<(String, u16, String)> {
    // The configured endpoint is a human-selected capability, not a URL from
    // search results. Preserve remote instances, but never interpolate request
    // separators, userinfo, queries, or fragments into the HTTP request.
    anyhow::ensure!(
        !base.chars().any(|c| c.is_control() || c.is_whitespace())
            && !base.contains(['@', '?', '#']),
        "{}",
        crate::locale::native_message(locale, crate::locale::NativeMessage::InvalidSearxUrl)
    );
    let rest = base.strip_prefix("http://").ok_or_else(|| {
        anyhow::anyhow!(
            "{}: {base}",
            crate::locale::native_message(locale, crate::locale::NativeMessage::OnlyHttpSearxUrl)
        )
    })?;
    let (hostport, prefix) = match rest.find('/') {
        Some(i) => (&rest[..i], rest[i..].to_string()),
        None => (rest, String::new()),
    };
    let (host, port_text) = if hostport.starts_with('[') {
        let end = hostport.find(']').ok_or_else(|| {
            anyhow::anyhow!(
                "{}",
                crate::locale::native_message(
                    locale,
                    crate::locale::NativeMessage::InvalidIpv6SearxHost
                )
            )
        })?;
        hostport[1..end]
            .parse::<std::net::Ipv6Addr>()
            .map_err(|_| {
                anyhow::anyhow!(
                    "{}",
                    crate::locale::native_message(
                        locale,
                        crate::locale::NativeMessage::InvalidIpv6SearxHost
                    )
                )
            })?;
        (&hostport[..=end], &hostport[end + 1..])
    } else {
        let end = hostport.find(':').unwrap_or(hostport.len());
        let host = &hostport[..end];
        anyhow::ensure!(
            !host.is_empty()
                && host
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b".-_".contains(&b)),
            "{}",
            crate::locale::native_message(locale, crate::locale::NativeMessage::InvalidSearxHost)
        );
        (host, &hostport[end..])
    };
    let port = if port_text.is_empty() {
        80
    } else {
        port_text
            .strip_prefix(':')
            .and_then(|s| s.parse::<u16>().ok())
            .filter(|port| *port != 0)
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "{}",
                    crate::locale::native_message(
                        locale,
                        crate::locale::NativeMessage::InvalidSearxPort
                    )
                )
            })?
    };
    Ok((host.to_string(), port, prefix))
}

/// Loopback check for the SearXNG warning: 127/8, ::1 and localhost names.
/// Bracketed IPv6 literals (as kept by split_base) are unwrapped first.
fn is_loopback_host(host: &str) -> bool {
    let h = host
        .strip_prefix('[')
        .and_then(|s| s.strip_suffix(']'))
        .unwrap_or(host);
    h == "localhost"
        || h.parse::<std::net::Ipv4Addr>()
            .is_ok_and(|ip| ip.is_loopback())
        || h.parse::<std::net::Ipv6Addr>()
            .is_ok_and(|ip| ip.is_loopback())
}

#[derive(Debug, Clone, PartialEq)]
struct Hit {
    title: String,
    url: String,
    content: String,
}

fn strip_tags(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut tag = false;
    let mut prev_space = true;
    for c in s.chars() {
        match c {
            '<' => tag = true,
            '>' => {
                tag = false;
                prev_space = true;
            }
            _ if tag => {}
            _ if c.is_whitespace() => {
                if !prev_space {
                    out.push(' ');
                    prev_space = true;
                }
            }
            _ => {
                out.push(c);
                prev_space = false;
            }
        }
    }
    out.trim().to_string()
}

fn decode_entities(s: &str) -> String {
    let mut out = s
        .replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&#x27;", "'");
    let mut search = 0;
    while let Some(offset) = out[search..].find("&#") {
        let i = search + offset;
        let rest = &out[i + 2..];
        let Some(end) = rest.find(';') else {
            break;
        };
        let num = &rest[..end];
        let ch = if num.len() > 8 || num.contains('&') {
            None
        } else if let Some(hex) = num.strip_prefix(['x', 'X']) {
            u32::from_str_radix(hex, 16).ok().and_then(char::from_u32)
        } else {
            num.parse::<u32>().ok().and_then(char::from_u32)
        };
        match ch {
            Some(c) => {
                out.replace_range(i..i + 2 + end + 1, &c.to_string());
                search = i + c.len_utf8();
            }
            None => search = i + 2,
        }
    }
    out
}

fn clean(s: &str) -> String {
    decode_entities(&strip_tags(s))
}

/// First `href` + link text inside an `<article>` block whose `class`
/// attribute marks it as a result. Only the start tag's class tokens decide:
/// body text (title/URL/snippet) must never affect classification.
fn parse_html(h: &str, limit: usize) -> Vec<Hit> {
    let mut hits = Vec::new();
    let mut rest = h;
    while let Some(a) = rest.find("<article") {
        let block = &rest[a..];
        let end = block
            .find("</article>")
            .map(|i| i + 10)
            .unwrap_or(block.len());
        let blk = &block[..end];
        rest = &block[end.min(block.len())..];
        // Classify by the article start tag's class tokens only: title, URL
        // and snippet text must never affect the decision (#382).
        let head = &blk[..blk
            .find('>')
            .map(|i| i + 1)
            .unwrap_or(blk.len())
            .min(blk.len())];
        let class = attr(head, "class").unwrap_or_default();
        let tokens: Vec<&str> = class.split_whitespace().collect();
        if !tokens.iter().any(|t| t.contains("result")) {
            continue;
        }
        if tokens.iter().any(|t| t.contains("correction")) {
            continue;
        }
        let href = match find_href(blk) {
            Some(u) => u,
            None => continue,
        };
        let title = match find_link_text(blk) {
            Some(t) => clean(&t),
            None => continue,
        };
        if title.is_empty() || href.is_empty() {
            continue;
        }
        let content = find_snippet(blk).map(|s| clean(&s)).unwrap_or_default();
        hits.push(Hit {
            title,
            url: href,
            content,
        });
        if hits.len() >= limit.max(1) {
            break;
        }
    }
    hits
}

fn find_href(blk: &str) -> Option<String> {
    let mut rest = blk;
    while let Some(a) = rest.find("<a") {
        let tag = &rest[a..];
        let close = tag.find('>')?;
        let head = &tag[..close];
        if let Some(u) = attr(head, "href").filter(|u| !u.is_empty() && !u.starts_with('#')) {
            return Some(decode_entities(&u));
        }
        rest = &tag[close + 1..];
    }
    None
}

/// Text of the same anchor `find_href` returns (first link with a real href).
fn find_link_text(blk: &str) -> Option<String> {
    let mut rest = blk;
    while let Some(a) = rest.find("<a") {
        let tag = &rest[a..];
        let close = tag.find('>')?;
        let head = &tag[..close];
        let ok = attr(head, "href").is_some_and(|u| !u.is_empty() && !u.starts_with('#'));
        let after = &tag[close + 1..];
        if ok {
            let end = after.find("</a>").unwrap_or(after.len());
            return Some(after[..end].to_string());
        }
        rest = after;
    }
    None
}

fn attr(tag_head: &str, name: &str) -> Option<String> {
    // Matches name="..." or name='...' without quote escapes in source.
    const SQ: u8 = b'\'';
    let mut rest = tag_head;
    while let Some(i) = rest.find(name) {
        let after = &rest[i + name.len()..];
        let b = after.as_bytes();
        if b.len() < 2 || b[0] != b'=' || (b[1] != b'"' && b[1] != SQ) {
            let step = after.chars().next().map_or(0, char::len_utf8);
            rest = &after[step..];
            continue;
        }
        let v = &after[2..];
        let end = v.find(b[1] as char).unwrap_or(v.len());
        return Some(v[..end].to_string());
    }
    None
}

fn find_snippet(blk: &str) -> Option<String> {
    let mut rest = blk;
    while let Some(i) = rest.find("<p class=") {
        let tag = &rest[i..];
        let gt = tag.find('>')?;
        if tag[..gt].contains("content") {
            let after = &tag[gt + 1..];
            let end = after.find("</p>").unwrap_or(after.len());
            return Some(after[..end].to_string());
        }
        rest = &tag[gt + 1..];
    }
    None
}

fn fetch(base: &str, query: &str, locale: crate::locale::Locale) -> anyhow::Result<Vec<Hit>> {
    use std::io::{Read, Write};
    use std::net::ToSocketAddrs;
    let (host, port, prefix) = split_base(base, locale)?;
    if !is_loopback_host(&host) {
        // Plain-http to a non-loopback host is observable on the network.
        // Advisory only: remote instances stay usable via an SSH tunnel.
        eprintln!("[rdsh] warning: SearXNG host {host} is not loopback; prefer an SSH tunnel to localhost");
    }
    let target = format!("{prefix}/search?q={}", encode_query(query));
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
    let mut stream = None;
    for address in format!("{host}:{port}").to_socket_addrs()? {
        let remaining = deadline.saturating_duration_since(std::time::Instant::now());
        if remaining.is_zero() {
            break;
        }
        if let Ok(connected) = std::net::TcpStream::connect_timeout(&address, remaining) {
            stream = Some(connected);
            break;
        }
    }
    let mut s = stream.ok_or_else(|| match locale {
        crate::locale::Locale::English => {
            anyhow::anyhow!("cannot reach SearXNG at {base}; is it running? SEARXNG_URL overrides")
        }
        crate::locale::Locale::Chinese => {
            anyhow::anyhow!("无法连接 {base} 的 SearXNG;它是否正在运行?SEARXNG_URL 会覆盖设置")
        }
        crate::locale::Locale::Japanese => {
            anyhow::anyhow!("{base} の SearXNG に接続できません。起動していますか?SEARXNG_URL が設定を上書きします")
        }
    })?;
    let ua = format!("rdsh/{}", env!("CARGO_PKG_VERSION"));
    // Virtual-hosted instances need the port to route; the default port is
    // omitted per convention. Bracketed IPv6 literals pass through as-is.
    let host_header = if port == 80 {
        host.clone()
    } else {
        format!("{host}:{port}")
    };
    let request = format!("GET {target} HTTP/1.0\r\nHost: {host_header}\r\nUser-Agent: {ua}\r\nAccept: text/html\r\nConnection: close\r\n\r\n");
    let mut pending = request.as_bytes();
    while !pending.is_empty() {
        let remaining = deadline.saturating_duration_since(std::time::Instant::now());
        anyhow::ensure!(
            !remaining.is_zero(),
            "{}",
            crate::locale::native_message(
                locale,
                crate::locale::NativeMessage::SearxRequestTimedOut
            )
        );
        s.set_write_timeout(Some(remaining))?;
        let n = s.write(pending)?;
        anyhow::ensure!(
            n != 0,
            "{}",
            crate::locale::native_message(locale, crate::locale::NativeMessage::SearxRequestClosed)
        );
        pending = &pending[n..];
    }
    let mut raw = Vec::new();
    let mut chunk = [0u8; 8192];
    loop {
        let remaining = deadline.saturating_duration_since(std::time::Instant::now());
        if remaining.is_zero() {
            anyhow::bail!(
                "{}",
                crate::locale::native_message(
                    locale,
                    crate::locale::NativeMessage::SearxResponseTimedOut
                )
            );
        }
        s.set_read_timeout(Some(remaining))?;
        let n = s.read(&mut chunk)?;
        if n == 0 {
            break;
        }
        if raw.len().saturating_add(n) > 2 * 1024 * 1024 {
            anyhow::bail!(
                "{}",
                crate::locale::native_message(
                    locale,
                    crate::locale::NativeMessage::SearxResponseTooLarge
                )
            );
        }
        raw.extend_from_slice(&chunk[..n]);
    }
    let text = String::from_utf8_lossy(&raw);
    let status = text
        .lines()
        .next()
        .and_then(|line| {
            let mut parts = line.split_whitespace();
            let protocol = parts.next()?;
            if !matches!(protocol, "HTTP/1.0" | "HTTP/1.1") {
                return None;
            }
            parts.next()?.parse::<u16>().ok()
        })
        .ok_or_else(|| {
            anyhow::anyhow!(
                "{} {base}",
                crate::locale::native_message(
                    locale,
                    crate::locale::NativeMessage::BadSearxResponse
                )
            )
        })?;
    if !(200..300).contains(&status) {
        anyhow::bail!(
            "{} {status} {} {base}",
            crate::locale::native_message(locale, crate::locale::NativeMessage::SearxReturnedHttp),
            match locale {
                crate::locale::Locale::English => "from",
                crate::locale::Locale::Chinese => ",来源",
                crate::locale::Locale::Japanese => "、送信元",
            }
        );
    }
    let body = match text.find("\r\n\r\n") {
        Some(i) => &text[i + 4..],
        None => anyhow::bail!(
            "{} {base}",
            crate::locale::native_message(locale, crate::locale::NativeMessage::BadSearxResponse)
        ),
    };
    ensure_results_page(body, base, locale)?;
    Ok(parse_html(body, 50))
}

/// Accept recognizable empty SearXNG pages; preserve locale on invalid pages.
fn ensure_results_page(
    body: &str,
    base: &str,
    locale: crate::locale::Locale,
) -> anyhow::Result<()> {
    if body.contains("<article") || looks_like_searxng(body) {
        return Ok(());
    }
    anyhow::bail!(
        "{} {base}{}",
        crate::locale::native_message(locale, crate::locale::NativeMessage::NoSearxResultsPage),
        match locale {
            crate::locale::Locale::English => " (is SEARXNG_URL right?)",
            crate::locale::Locale::Chinese => "(SEARXNG_URL 是否正确?)",
            crate::locale::Locale::Japanese => "(SEARXNG_URL は正しいですか?)",
        }
    );
}

fn looks_like_searxng(body: &str) -> bool {
    body.contains("id=\"results\"") || body.contains("content=\"searxng")
}

pub fn cmd_search_web(
    query: &str,
    limit: usize,
    json: bool,
    locale: crate::locale::Locale,
) -> anyhow::Result<()> {
    let base = default_base();
    let hits = fetch(&base, query, locale)?;
    let shown: Vec<&Hit> = hits.iter().take(limit.max(1)).collect();
    if json {
        println!(
            "{}",
            serde_json::json!({
                "query": query,
                "engine": base,
                "results": shown
                    .iter()
                    .map(|r| {
                        serde_json::json!({
                            "title": r.title,
                            "url": r.url,
                            "content": r.content,
                        })
                    })
                    .collect::<Vec<_>>(),
            })
        );
        return Ok(());
    }
    use std::io::Write as _;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for r in &shown {
        let _ = writeln!(out, "- {}\n  {}", r.title, r.url);
        if !r.content.is_empty() {
            let snippet: String = r.content.chars().take(240).collect();
            let _ = writeln!(out, "  {snippet}");
        }
    }
    let _ = out.flush();
    match locale {
        crate::locale::Locale::English => eprintln!("[rdsh] {} web hit(s) via {base}", shown.len()),
        crate::locale::Locale::Chinese => {
            eprintln!("[rdsh] {} 个网页匹配,来源 {base}", shown.len())
        }
        crate::locale::Locale::Japanese => {
            eprintln!("[rdsh] {} 件のWeb一致、経由 {base}", shown.len())
        }
    }
    Ok(())
}

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

    // 0 件でも正しい SearXNG インスタンスなら成功扱いにする(設定ミスと誤診しない)。
    #[test]
    fn empty_searxng_page_is_not_a_configuration_error() {
        let page =
            "<html><body><div id=\"results\"><p>No results were found.</p></div></body></html>";
        assert!(ensure_results_page(
            page,
            "http://127.0.0.1:8888",
            crate::locale::Locale::English
        )
        .is_ok());
        assert!(parse_html(page, 50).is_empty());
    }

    #[test]
    fn searxng_generator_meta_is_accepted() {
        let page = "<html><head><meta name=\"generator\" content=\"searxng/2026\"></head></html>";
        assert!(ensure_results_page(
            page,
            "http://127.0.0.1:8888",
            crate::locale::Locale::English
        )
        .is_ok());
    }

    #[test]
    fn a_plain_page_is_still_reported_as_a_wrong_url() {
        let page = "<html><body><h1>hello</h1></body></html>";
        assert!(ensure_results_page(
            page,
            "http://127.0.0.1:8888",
            crate::locale::Locale::English
        )
        .is_err());
    }

    #[test]
    fn empty_results_and_invalid_pages_retain_the_requested_language() {
        use crate::locale::Locale;
        for (locale, expected) in [
            (Locale::English, "no SearXNG results page"),
            (Locale::Chinese, "以下地址没有 SearXNG 结果页"),
            (Locale::Japanese, "SearXNG の結果ページがありません"),
        ] {
            assert!(ensure_results_page(
                "<div id=\"results\"></div>",
                "http://127.0.0.1:8888",
                locale
            )
            .is_ok());
            let error = ensure_results_page(
                "<html>wrong service</html>",
                "http://127.0.0.1:8888",
                locale,
            )
            .unwrap_err()
            .to_string();
            assert!(error.contains(expected), "{error}");
            assert!(error.contains("http://127.0.0.1:8888"));
        }
    }

    #[test]
    fn query_encoding() {
        assert_eq!(encode_query("a b+c"), "a+b%2Bc");
        assert_eq!(encode_query("日本語"), "%E6%97%A5%E6%9C%AC%E8%AA%9E");
    }

    #[test]
    fn base_splitting() {
        assert_eq!(
            split_base("http://127.0.0.1:8888", crate::locale::Locale::English).unwrap(),
            ("127.0.0.1".to_string(), 8888, String::new())
        );
        assert_eq!(
            split_base("http://h/searx", crate::locale::Locale::English).unwrap(),
            ("h".to_string(), 80, "/searx".to_string())
        );
        assert!(split_base("https://h/", crate::locale::Locale::English).is_err());
        assert_eq!(
            split_base("http://[::1]/searx", crate::locale::Locale::English).unwrap(),
            ("[::1]".to_string(), 80, "/searx".to_string())
        );
        assert_eq!(
            split_base(
                "http://searx.example:8888/prefix",
                crate::locale::Locale::English
            )
            .unwrap(),
            ("searx.example".to_string(), 8888, "/prefix".to_string())
        );
    }

    #[test]
    fn base_rejects_http_request_injection_and_malformed_authorities() {
        for base in [
            "http://127.0.0.1:8888/prefix\r\nInjected: yes",
            "http://127.0.0.1:8888/path HTTP/1.0",
            "http://localhost\t:8888",
            "http://user@localhost:8888",
            "http://localhost:8888/path?other=query",
            "http://localhost:8888/#fragment",
            "http://[::1]extra:8888",
            "http://[bad]:8888",
            "http://::1",
            "http://localhost:0",
            "http://localhost:65536",
            "http://localhost:",
            "http:///path",
        ] {
            assert!(
                split_base(base, crate::locale::Locale::English).is_err(),
                "{base:?}"
            );
        }
        // The query is encoded separately and cannot create a second request.
        assert_eq!(encode_query("\r\nInjected: yes"), "%0D%0AInjected%3A+yes");
    }

    #[test]
    fn fetch_encodes_query_and_preserves_configured_prefix() {
        use std::io::{Read, Write};
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        listener.set_nonblocking(true).unwrap();
        let address = listener.local_addr().unwrap();
        let server = std::thread::spawn(move || {
            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
            let mut stream = loop {
                match listener.accept() {
                    Ok((stream, _)) => break stream,
                    Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                        assert!(
                            std::time::Instant::now() < deadline,
                            "no fixture connection"
                        );
                        std::thread::sleep(std::time::Duration::from_millis(5));
                    }
                    Err(e) => panic!("fixture accept: {e}"),
                }
            };
            // Accepted sockets inherit O_NONBLOCK from the listener on BSD.
            stream.set_nonblocking(false).unwrap();
            stream
                .set_read_timeout(Some(std::time::Duration::from_secs(3)))
                .unwrap();
            let mut request = Vec::new();
            while !request.ends_with(b"\r\n\r\n") {
                let mut byte = [0];
                assert_eq!(stream.read(&mut byte).unwrap(), 1);
                request.push(byte[0]);
                assert!(request.len() < 8192);
            }
            let body = "<article class=\"result\"><a href=\"https://example.invalid/\">DUMMY_RESULT</a></article>";
            write!(
                stream,
                "HTTP/1.0 200 OK\r\nContent-Length: {}\r\n\r\n{body}",
                body.len()
            )
            .unwrap();
            String::from_utf8(request).unwrap()
        });
        let hits = fetch(
            &format!("http://{address}/prefix"),
            "hello\r\nInjected: yes",
            crate::locale::Locale::English,
        );
        let request = server.join().unwrap();
        assert_eq!(hits.unwrap()[0].title, "DUMMY_RESULT");
        assert!(request.starts_with("GET /prefix/search?q=hello%0D%0AInjected%3A+yes HTTP/1.0\r\n"));
        assert!(!request.contains("\r\nInjected:"));
        // Ephemeral port: the Host header must carry it for vhost routing.
        assert!(request.contains(&format!("\r\nHost: {address}\r\n")));
    }

    #[test]
    fn loopback_classification() {
        for good in ["127.0.0.1", "127.1.2.3", "localhost", "::1", "[::1]"] {
            assert!(is_loopback_host(good), "{good:?}");
        }
        for bad in [
            "192.168.1.1",
            "10.0.0.1",
            "example.com",
            "",
            "127.0.0.1:8888",
        ] {
            assert!(!is_loopback_host(bad), "{bad:?}");
        }
    }

    #[test]
    fn html_parsing() {
        let h = "<article class=\"result\"><h3><a href=\"https://e.com/x?a=1&amp;b=2\">A <b>B</b></a></h3><p class=\"content\">one &lt;two&gt;</p></article><article class=\"result result-correction\"><a href=\"https://e.com/y\">no</a></article>";
        let hits = parse_html(h, 10);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].title, "A B");
        assert_eq!(hits[0].url, "https://e.com/x?a=1&b=2");
        assert_eq!(hits[0].content, "one <two>");
    }

    #[test]
    fn html_parsing_keeps_correction_words_in_title_url_snippet() {
        // Regression test for #382: the word "correction" in body text must
        // not exclude a normal result; only the start tag's class tokens decide.
        let h = concat!(
            "<article class=\"result\"><h3><a href=\"https://example.invalid/a\">",
            "Quantum error correction</a></h3><p class=\"content\">Useful paper</p></article>",
            "<article class=\"result\"><h3><a href=\"https://example.invalid/correction-notes\">",
            "Quantum paper</a></h3><p class=\"content\">Useful paper</p></article>",
            "<article class=\"result\"><h3><a href=\"https://example.invalid/b\">",
            "Quantum paper</a></h3><p class=\"content\">About error correction</p></article>",
            "<article class=\"result result-correction\"><h3><a href=\"https://example.invalid/d\">",
            "Did you mean?</a></h3></article>",
        );
        let hits = parse_html(h, 10);
        assert_eq!(hits.len(), 3);
        assert_eq!(hits[0].title, "Quantum error correction");
        assert_eq!(hits[0].url, "https://example.invalid/a");
        assert_eq!(hits[1].url, "https://example.invalid/correction-notes");
        assert_eq!(hits[2].content, "About error correction");
    }

    #[test]
    fn html_parsing_skips_utf8_attribute_name_candidates() {
        for character in ["é", "→", "🦀"] {
            let url = format!("https://example.invalid/{character}");
            let title = format!("結果 {character}");
            let html = format!(
                "<article class=\"result\"><a title=\"href{character}\" href=\"{url}\">{title}</a></article>"
            );

            assert_eq!(
                parse_html(&html, 1),
                vec![Hit {
                    title,
                    url,
                    content: String::new(),
                }],
                "failed to skip a {character:?} candidate"
            );
        }
    }

    #[test]
    fn attribute_name_at_end_does_not_panic() {
        assert_eq!(attr("href", "href"), None);
    }

    #[test]
    fn malformed_numeric_entity_does_not_panic_or_hide_later_entities() {
        assert_eq!(decode_entities("A&#65"), "A&#65");
        assert_eq!(decode_entities("A&#65;B"), "AAB");
        assert_eq!(decode_entities("bad &#xZ; then &#65;"), "bad &#xZ; then A");
    }
}