searchwala 8.3.1

Meta-search engine and zero-dependency MCP server in Rust: cross-engine consensus ranking, trafilatura + Readability extraction, domain controls, scholarly sources, and mode-specific synthesis with optional BYOK LLM
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
// ============================================================================
// SearchWala v8.0 — Unified HTTP surface for search engines
//
// WHY THIS EXISTS
// v7.0 built a `wreq::Client` with BoringSSL TLS impersonation, passed it into
// `dispatch_engines` as `_stealth_client`, and never used it — the leading
// underscore was the entire bug. `SearchEngine::search()` only accepted
// `&reqwest::Client`, so the stealth client was unreachable by construction,
// and every WAF-protected engine got plain reqwest and was blocked.
//
// Measured on 2026-10-01, same queries, reqwest vs wreq:
//   brave       429 rate-limited   ->  200, 42 .snippet elements
//   duckduckgo  202 CAPTCHA        ->  200, 10 result__a + 40 uddg= links
//   bing        200 (homepage!)    ->  200, 10 li.b_algo results
//   google      200 enablejs       ->  200 enablejs (needs more than TLS)
//
// This enum unifies both clients behind a 2-method surface that RETURNS THE
// STATUS CODE, which also forces the status check engines previously skipped
// (they called `resp.text()` unconditionally and parsed CAPTCHA pages as
// SERPs).
// ============================================================================

use std::time::Duration;

/// Cap on a single SERP response. Engine result pages are tens of KB; anything
/// past this is a misrouted download or an attack.
const MAX_SERP_BYTES: usize = 3_000_000;

/// Hard per-request ceiling, independent of the client-level timeout.
///
/// Discovered in testing: the Marginalia public API *hangs* on some queries —
/// "cricket world cup" returned in 0.9 s, "cricket world cup 2027" never
/// returned and hit a 25 s timeout. With the client timeout at 16 s and the
/// dispatch deadline at 2.5–6 s, the task was aborted mid-flight, so the
/// health registry recorded an attempt with NO outcome: invisible.
///
/// **This must stay meaningfully BELOW the dispatch deadline.** When it was
/// 6,000 ms against a 6,000 ms auto-mode deadline, a single hanging engine
/// (measured: yacy) held the collector open for the entire window — the stage
/// cost 6,001 ms on every query, warm cache or not, with 16 of 17 tasks long
/// since finished. At 4,000 ms the straggler fails, gets recorded as a
/// transport error, and the dispatch closes as soon as the rest are in.
fn request_timeout() -> Duration {
    Duration::from_millis(
        std::env::var("ENGINE_REQUEST_TIMEOUT_MS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(4_000),
    )
}

/// Insert `extra` into a HeaderMap with REPLACE semantics.
fn set_headers(map: &mut reqwest::header::HeaderMap, extra: &[(&str, &str)]) -> Result<(), String> {
    for (k, v) in extra {
        let name = reqwest::header::HeaderName::from_bytes(k.as_bytes())
            .map_err(|e| format!("bad header name {k}: {e}"))?;
        let value = reqwest::header::HeaderValue::from_str(v)
            .map_err(|e| format!("bad header value for {k}: {e}"))?;
        map.insert(name, value); // insert == replace
    }
    Ok(())
}

#[derive(Clone)]
pub enum Fetcher {
    /// Plain reqwest — fine for indie engines and JSON APIs that do not
    /// fingerprint TLS.
    Plain(reqwest::Client),
    /// wreq + BoringSSL — real browser JA4 + HTTP/2 SETTINGS fingerprint.
    Stealth(wreq::Client),
}

pub struct Fetched {
    pub status: u16,
    pub body: String,
    /// URL after redirects — needed so parsers can resolve relative hrefs and
    /// filter self-links correctly.
    pub final_url: String,
}

impl Fetched {
    /// 200 and not a bot challenge.
    pub fn is_usable(&self) -> bool {
        self.status == 200 && !crate::engines::health::looks_like_challenge(&self.body)
    }
}

impl Fetcher {
    pub fn is_stealth(&self) -> bool {
        matches!(self, Fetcher::Stealth(_))
    }

    pub fn kind(&self) -> &'static str {
        if self.is_stealth() {
            "stealth"
        } else {
            "plain"
        }
    }

    pub async fn get(&self, url: &str, extra: &[(&str, &str)]) -> Result<Fetched, String> {
        // Space requests to the same registrable host. Engine aliases share
        // origins (brave + brave_news, 15 bing_* variants), and firing them
        // simultaneously is what earned the 429s.
        crate::host_gate::acquire(url).await;
        match self {
            Fetcher::Plain(c) => {
                let rb = crate::config::apply_browser_headers(c.get(url), url);
                // Build the Request, then REPLACE (not append) the extras.
                // reqwest's RequestBuilder::header appends, so setting `Accept`
                // twice sends two Accept lines — not what a browser does, and
                // some JSON APIs content-negotiate badly on it.
                let mut req = rb.build().map_err(|e| e.to_string())?;
                set_headers(req.headers_mut(), extra)?;
                let resp = with_timeout(c.execute(req))
                    .await?
                    .map_err(|e| e.to_string())?;
                let status = resp.status().as_u16();
                let final_url = resp.url().to_string();
                let bytes = resp.bytes().await.map_err(|e| e.to_string())?;
                Ok(Fetched {
                    status,
                    body: decode_body(&bytes),
                    final_url,
                })
            }
            Fetcher::Stealth(c) => {
                // IMPORTANT: wreq-util's Emulation already installs a
                // *consistent* User-Agent + Sec-CH-UA suite matched to the TLS
                // and HTTP/2 fingerprint. We deliberately do NOT override them.
                //
                // v7.0's hand-written profiles claimed Chrome 155 while
                // `Emulation::Chrome147` set a Chrome-147 TLS fingerprint. A
                // UA/TLS mismatch is a STRONGER bot signal than no spoofing at
                // all — it is exactly the inconsistency WAFs look for.
                let mut rb = c.get(url).header("Accept-Language", "en-US,en;q=0.9");
                for (k, v) in extra {
                    rb = rb.header(*k, *v);
                }
                let resp = with_timeout(rb.send()).await?.map_err(|e| e.to_string())?;
                let status = resp.status().as_u16();
                // wreq exposes `uri()`, not `url()`.
                let final_url = resp.uri().to_string();
                let bytes = resp.bytes().await.map_err(|e| e.to_string())?;
                Ok(Fetched {
                    status,
                    body: decode_body(&bytes),
                    final_url,
                })
            }
        }
    }

    pub async fn post_form(
        &self,
        url: &str,
        form: &[(String, String)],
        extra: &[(&str, &str)],
    ) -> Result<Fetched, String> {
        crate::host_gate::acquire(url).await;
        match self {
            Fetcher::Plain(c) => {
                let rb = crate::config::apply_browser_headers(c.post(url), url);
                let mut req = rb.form(form).build().map_err(|e| e.to_string())?;
                set_headers(req.headers_mut(), extra)?;
                let resp = with_timeout(c.execute(req))
                    .await?
                    .map_err(|e| e.to_string())?;
                let status = resp.status().as_u16();
                let final_url = resp.url().to_string();
                let bytes = resp.bytes().await.map_err(|e| e.to_string())?;
                Ok(Fetched {
                    status,
                    body: decode_body(&bytes),
                    final_url,
                })
            }
            Fetcher::Stealth(c) => {
                let mut rb = c.post(url).header("Accept-Language", "en-US,en;q=0.9");
                for (k, v) in extra {
                    rb = rb.header(*k, *v);
                }
                let resp = with_timeout(rb.form(form).send())
                    .await?
                    .map_err(|e| e.to_string())?;
                let status = resp.status().as_u16();
                let final_url = resp.uri().to_string();
                let bytes = resp.bytes().await.map_err(|e| e.to_string())?;
                Ok(Fetched {
                    status,
                    body: decode_body(&bytes),
                    final_url,
                })
            }
        }
    }

    /// Fetch and parse JSON. Used by the API-backed engines (Wikipedia, Wiby,
    /// Marginalia) where a status check still matters.
    pub async fn get_json(&self, url: &str) -> Result<(u16, serde_json::Value), String> {
        let page = self
            .get(url, &[("Accept", "application/json,text/plain,*/*")])
            .await?;
        if page.status != 200 {
            return Ok((page.status, serde_json::Value::Null));
        }
        let v = serde_json::from_str(&page.body).map_err(|e| format!("json: {e}"))?;
        Ok((page.status, v))
    }
}

/// Enforce `request_timeout()` around a send future.
async fn with_timeout<T>(fut: impl std::future::Future<Output = T>) -> Result<T, String> {
    tokio::time::timeout(request_timeout(), fut)
        .await
        .map_err(|_| format!("request timed out after {:?}", request_timeout()))
}

// ============================================================================
// Charset-aware body decoding
// ============================================================================

/// Decode a response body, honouring the document's declared charset.
///
/// v7.0 used `String::from_utf8_lossy` unconditionally, which turns every
/// windows-1252 / ISO-8859-1 / GBK / Shift_JIS page into replacement
/// characters. Many regional news sites still serve legacy encodings.
pub fn decode_body(bytes: &[u8]) -> String {
    let slice = &bytes[..bytes.len().min(MAX_SERP_BYTES)];

    // Byte-order marks win outright.
    if let Some((enc, bom_len)) = encoding_rs::Encoding::for_bom(slice) {
        let (cow, _) = enc.decode_without_bom_handling(&slice[bom_len..]);
        return cow.into_owned();
    }

    // Try UTF-8 first — the overwhelming majority case.
    let (cow, _, had_errors) = encoding_rs::UTF_8.decode(slice);
    if !had_errors {
        return cow.into_owned();
    }

    // Malformed as UTF-8: honour the declared charset, else fall back to
    // windows-1252 (the HTML5 spec's default for unlabelled legacy content).
    if let Some(enc) = sniff_meta_charset(slice) {
        let (c2, _) = enc.decode_without_bom_handling(slice);
        return c2.into_owned();
    }
    let (c2, _, _) = encoding_rs::WINDOWS_1252.decode(slice);
    c2.into_owned()
}

/// Scan the document head for `<meta charset=..>` or
/// `<meta http-equiv="content-type" content="..; charset=..">`.
fn sniff_meta_charset(bytes: &[u8]) -> Option<&'static encoding_rs::Encoding> {
    let head = &bytes[..bytes.len().min(4096)];
    // Lossy is safe here: we only need the ASCII charset declaration.
    let ascii = String::from_utf8_lossy(head).to_lowercase();
    let i = ascii.find("charset")? + "charset".len();
    let name: String = ascii[i..]
        .trim_start_matches(['=', '"', '\'', ' '])
        .chars()
        .take_while(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
        .collect();
    if name.is_empty() {
        return None;
    }
    encoding_rs::Encoding::for_label(name.as_bytes())
}

// ============================================================================
// Client builders
// ============================================================================

/// Plain reqwest client.
///
/// NOTE on compression: we enable gzip/brotli/deflate/zstd and let reqwest
/// emit `Accept-Encoding` itself. v7.0 hand-wrote the header with `zstd` in
/// two of three randomized variants while the client had no zstd support, so
/// zstd-serving origins (increasingly common behind Cloudflare) returned
/// bodies reqwest could not decode — silent, intermittent zero-result runs.
pub fn build_plain_client(proxy: Option<&str>) -> reqwest::Client {
    let mut builder = reqwest::Client::builder()
        .connect_timeout(Duration::from_secs(8))
        .timeout(Duration::from_secs(16))
        .redirect(reqwest::redirect::Policy::limited(10))
        .pool_max_idle_per_host(10)
        .cookie_store(true)
        .user_agent(crate::config::random_user_agent())
        .tcp_nodelay(true)
        .gzip(true)
        .brotli(true)
        .deflate(true)
        .zstd(true)
        .https_only(false);

    if let Some(proxy_url) = proxy {
        match reqwest::Proxy::all(proxy_url) {
            Ok(cfg) => builder = builder.proxy(cfg),
            Err(err) => tracing::warn!("Invalid proxy {}: {}", proxy_url, err),
        }
    }

    builder.build().expect("Failed to build plain HTTP client")
}

/// Build both fetchers once per search. Cloning a `Fetcher` is an Arc clone of
/// the inner client, so tasks share connection pools — v7.0 built a brand-new
/// `reqwest::Client` *inside each spawned task* when proxies were enabled,
/// defeating connection reuse entirely.
pub fn build_pair(proxy: Option<&str>) -> (Fetcher, Fetcher) {
    (
        Fetcher::Plain(build_plain_client(proxy)),
        Fetcher::Stealth(crate::config::build_stealth_client(proxy)),
    )
}

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

    #[test]
    fn decodes_plain_utf8() {
        assert_eq!(decode_body("hello".as_bytes()), "hello");
        let hindi = "भारत और पाकिस्तान";
        assert_eq!(decode_body(hindi.as_bytes()), hindi);
    }

    #[test]
    fn decodes_windows_1252_when_utf8_fails() {
        // 0xE9 is 'é' in windows-1252 but invalid standalone UTF-8.
        let bytes = b"<html><head></head><body>caf\xE9</body></html>";
        let out = decode_body(bytes);
        assert!(out.contains("café"), "got {out:?}");
        assert!(
            !out.contains('\u{FFFD}'),
            "must not contain replacement chars"
        );
    }

    #[test]
    fn honours_declared_meta_charset() {
        // ISO-8859-1 declared explicitly; 0xF6 is 'ö'.
        let mut bytes =
            Vec::from(&b"<html><head><meta charset=\"iso-8859-1\"></head><body>sch\xF6n"[..]);
        bytes.extend_from_slice(b"</body></html>");
        let out = decode_body(&bytes);
        assert!(out.contains("schön"), "got {out:?}");
    }

    #[test]
    fn handles_utf8_bom() {
        let mut bytes = vec![0xEF, 0xBB, 0xBF];
        bytes.extend_from_slice("héllo".as_bytes());
        let out = decode_body(&bytes);
        assert_eq!(out, "héllo", "BOM must be stripped");
    }

    #[test]
    fn caps_oversized_bodies_without_panicking() {
        // Multibyte characters straddling the cap must not panic.
        let big: String = "क".repeat(MAX_SERP_BYTES); // 3x the cap in bytes
        let out = decode_body(big.as_bytes());
        assert!(out.len() <= MAX_SERP_BYTES);
        assert!(!out.is_empty());
    }

    #[test]
    fn sniffs_charset_label_variants() {
        assert!(sniff_meta_charset(b"<meta charset=utf-8>").is_some());
        assert!(sniff_meta_charset(b"<meta charset=\"UTF-8\">").is_some());
        assert!(sniff_meta_charset(b"<meta charset='shift_jis'>").is_some());
        assert!(sniff_meta_charset(
            b"<meta http-equiv=content-type content=\"text/html; charset=gbk\">"
        )
        .is_some());
        assert!(sniff_meta_charset(b"<html><body>no declaration").is_none());
    }

    #[test]
    fn is_usable_rejects_non_200_and_challenges() {
        let ok = Fetched {
            status: 200,
            body: "<div class=\"snippet\">real result</div>".into(),
            final_url: "https://x.test/".into(),
        };
        assert!(ok.is_usable());

        let rate_limited = Fetched {
            status: 429,
            body: "<div class=\"snippet\">real result</div>".into(),
            final_url: "https://x.test/".into(),
        };
        assert!(!rate_limited.is_usable(), "429 must not be parsed");

        let soft_block = Fetched {
            status: 200,
            body: "Select all squares containing a duck".into(),
            final_url: "https://x.test/".into(),
        };
        assert!(!soft_block.is_usable(), "200 CAPTCHA must not be parsed");
    }
}