Skip to main content

reddb_wire/
conn_string.rs

1//! Connection-string parser shared across `reddb`, `reddb-client`,
2//! `red_client`, and every language driver.
3//!
4//! Pure function over a string; no I/O, no allocation beyond what the
5//! returned [`ConnectionTarget`] needs. The grammar is defined by
6//! `docs/clients/connection-strings.md`; this module is the canonical
7//! parser and is the single source of truth consumed by the rest of
8//! the workspace.
9//!
10//! The parser ports the logic that previously lived in
11//! `drivers/rust/src/connect.rs` (which keeps a thin re-export layer
12//! for backwards compatibility while drivers migrate over). Cluster
13//! URIs (`grpc://primary,replica:port`), default ports per scheme,
14//! and the `?route=primary` override behave identically to the
15//! original.
16
17use std::path::PathBuf;
18
19use url::Url;
20
21/// Stable error code for parser failures.
22///
23/// Mirrors the `ErrorCode` shape used by the language drivers so that
24/// downstream wrappers can map 1:1 without information loss.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum ParseErrorKind {
27    /// The input was empty.
28    Empty,
29    /// `url::Url` rejected the string, or a transport-specific
30    /// invariant (missing host, empty cluster entry, bad port…) was
31    /// violated.
32    InvalidUri,
33    /// The scheme is not in the documented vocabulary.
34    UnsupportedScheme,
35    /// A DoS guardrail in [`ConnStringLimits`] was tripped.
36    /// `message` carries the limit name + the offending value so
37    /// downstream wrappers can surface the structured detail.
38    LimitExceeded,
39}
40
41impl ParseErrorKind {
42    pub fn as_str(self) -> &'static str {
43        match self {
44            ParseErrorKind::Empty => "EMPTY",
45            ParseErrorKind::InvalidUri => "INVALID_URI",
46            ParseErrorKind::UnsupportedScheme => "UNSUPPORTED_SCHEME",
47            ParseErrorKind::LimitExceeded => "LIMIT_EXCEEDED",
48        }
49    }
50}
51
52/// Error returned by [`parse`].
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct ParseError {
55    pub kind: ParseErrorKind,
56    pub message: String,
57}
58
59impl ParseError {
60    pub fn new(kind: ParseErrorKind, message: impl Into<String>) -> Self {
61        Self {
62            kind,
63            message: message.into(),
64        }
65    }
66}
67
68impl std::fmt::Display for ParseError {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(f, "{}: {}", self.kind.as_str(), self.message)
71    }
72}
73
74impl std::error::Error for ParseError {}
75
76/// Default port per documented scheme. Centralised so other crates
77/// (the connector, server-side dispatch) can stay consistent.
78pub const DEFAULT_PORT_RED: u16 = 5050;
79pub const DEFAULT_PORT_GRPC: u16 = 55055;
80pub const DEFAULT_PORT_GRPCS: u16 = 55555;
81/// Default ports for `ws://` / `red+ws://` and `wss://` / `red+wss://` — align with the
82/// standard WS / WSS browser defaults (80 and 443) so a hosted endpoint
83/// like `*.db.reddb.io` works without an explicit port.
84pub const DEFAULT_PORT_WS: u16 = 80;
85pub const DEFAULT_PORT_WSS: u16 = 443;
86
87/// URI schemes accepted by the connection-string parser.
88///
89/// This enum is the connection-layer source for generated agent knowledge:
90/// [`crate::knowledge`] iterates [`SUPPORTED_SCHEMES`] instead of carrying a
91/// separate hand-maintained scheme list.
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum ConnectionScheme {
94    Memory,
95    File,
96    Red,
97    Reds,
98    RedWs,
99    RedWss,
100    Ws,
101    Wss,
102    Grpc,
103    Grpcs,
104    Http,
105    Https,
106}
107
108/// Stable parser-owned list of supported URI schemes.
109pub const SUPPORTED_SCHEMES: &[ConnectionScheme] = &[
110    ConnectionScheme::Red,
111    ConnectionScheme::Reds,
112    ConnectionScheme::Grpc,
113    ConnectionScheme::Grpcs,
114    ConnectionScheme::Http,
115    ConnectionScheme::Https,
116    ConnectionScheme::Memory,
117    ConnectionScheme::File,
118    ConnectionScheme::RedWs,
119    ConnectionScheme::RedWss,
120    ConnectionScheme::Ws,
121    ConnectionScheme::Wss,
122];
123
124impl ConnectionScheme {
125    pub fn from_uri_scheme(scheme: &str) -> Option<Self> {
126        match scheme {
127            "memory" => Some(Self::Memory),
128            "file" => Some(Self::File),
129            "red" => Some(Self::Red),
130            "reds" => Some(Self::Reds),
131            "red+ws" => Some(Self::RedWs),
132            "red+wss" => Some(Self::RedWss),
133            "ws" => Some(Self::Ws),
134            "wss" => Some(Self::Wss),
135            "grpc" => Some(Self::Grpc),
136            "grpcs" => Some(Self::Grpcs),
137            "http" => Some(Self::Http),
138            "https" => Some(Self::Https),
139            _ => None,
140        }
141    }
142
143    pub fn uri_prefix(self) -> &'static str {
144        match self {
145            Self::Memory => "memory://",
146            Self::File => "file://",
147            Self::Red => "red://",
148            Self::Reds => "reds://",
149            Self::RedWs => "red+ws://",
150            Self::RedWss => "red+wss://",
151            Self::Ws => "ws://",
152            Self::Wss => "wss://",
153            Self::Grpc => "grpc://",
154            Self::Grpcs => "grpcs://",
155            Self::Http => "http://",
156            Self::Https => "https://",
157        }
158    }
159
160    pub fn transport(self) -> &'static str {
161        match self {
162            Self::Memory => "embedded in-memory engine",
163            Self::File => "embedded file-backed engine",
164            Self::Red | Self::Reds => "RedWire TCP",
165            Self::RedWs | Self::RedWss | Self::Ws | Self::Wss => "RedWire WebSocket",
166            Self::Grpc | Self::Grpcs => "gRPC",
167            Self::Http | Self::Https => "HTTP REST",
168        }
169    }
170
171    pub fn mode(self) -> &'static str {
172        match self {
173            Self::Memory | Self::File => "embedded",
174            Self::Red
175            | Self::Reds
176            | Self::RedWs
177            | Self::RedWss
178            | Self::Ws
179            | Self::Wss
180            | Self::Grpc
181            | Self::Grpcs
182            | Self::Http
183            | Self::Https => "remote",
184        }
185    }
186
187    pub fn example(self) -> &'static str {
188        match self {
189            Self::Memory => "memory://",
190            Self::File => "file:///var/lib/reddb/app.db",
191            Self::Red => "red://db.example.com:5050",
192            Self::Reds => "reds://db.example.com:5050",
193            Self::RedWs => "red+ws://db.example.com",
194            Self::RedWss => "red+wss://db.example.com",
195            Self::Ws => "ws://db.example.com",
196            Self::Wss => "wss://db.example.com",
197            Self::Grpc => "grpc://db.example.com:55055",
198            Self::Grpcs => "grpcs://db.example.com:55555",
199            Self::Http => "http://db.example.com:80",
200            Self::Https => "https://db.example.com:443",
201        }
202    }
203
204    pub fn notes(self) -> &'static str {
205        match self {
206            Self::Memory => "Zero-config ephemeral engine, commonly used by local MCP hosts.",
207            Self::File => "Embedded durable engine rooted at the URI path.",
208            Self::Red => "Principal RedWire transport without TLS.",
209            Self::Reds => "Principal RedWire transport with TLS.",
210            Self::RedWs => "Browser-native RedWire over WebSocket without TLS.",
211            Self::RedWss => "Browser-native RedWire over WebSocket with TLS.",
212            Self::Ws => "Browser-friendly alias for RedWire over WebSocket without TLS.",
213            Self::Wss => "Browser-friendly alias for RedWire over WebSocket with TLS.",
214            Self::Grpc => "Compatibility transport for existing gRPC clients.",
215            Self::Grpcs => "TLS variant of the gRPC compatibility transport.",
216            Self::Http => "REST/admin transport without TLS.",
217            Self::Https => "REST/admin transport with TLS.",
218        }
219    }
220}
221
222/// DoS guardrails applied by [`parse`] before any URI work happens.
223///
224/// The connection-string parser is the only entry point an attacker
225/// can reach BEFORE auth, so every limit here is enforced eagerly
226/// and surfaces as a structured [`ParseErrorKind::LimitExceeded`]
227/// error rather than a panic, hang, or unbounded allocation.
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub struct ConnStringLimits {
230    /// Maximum length of the input URI in bytes. Default `8 KiB`.
231    pub max_uri_bytes: usize,
232    /// Maximum number of `key=value` query parameters. Default `32`.
233    pub max_query_params: usize,
234    /// Maximum number of comma-separated cluster hosts allowed in a
235    /// `grpc://`/`grpcs://` cluster URI. Default `64`.
236    pub max_cluster_hosts: usize,
237}
238
239impl Default for ConnStringLimits {
240    fn default() -> Self {
241        Self {
242            max_uri_bytes: 8 * 1024,
243            max_query_params: 32,
244            max_cluster_hosts: 64,
245        }
246    }
247}
248
249/// Normalised target produced by [`parse`].
250///
251/// Variants intentionally mirror the public Rust client target shape
252/// so callers can keep a thin compatibility layer without duplicating
253/// parser behavior.
254#[derive(Debug, Clone, PartialEq, Eq)]
255pub enum ConnectionTarget {
256    /// `memory://` — ephemeral, in-memory backend.
257    Memory,
258    /// `file:///abs/path` — embedded engine on disk.
259    File { path: PathBuf },
260    /// Single remote endpoint over `grpc://` or `grpcs://`. Stored as a
261    /// normalised `http://` or `https://` endpoint for tonic.
262    Grpc { endpoint: String },
263    /// Multi-host gRPC URI: primary + read replicas. Writes hit the
264    /// primary; reads round-robin across replicas unless
265    /// `force_primary` is set.
266    GrpcCluster {
267        primary: String,
268        replicas: Vec<String>,
269        force_primary: bool,
270    },
271    /// `http://host:port` / `https://host:port` — REST endpoint.
272    Http { base_url: String },
273    /// `red://host:port` (plain TCP) or `reds://host:port` (TLS).
274    /// RedWire binary frame protocol per ADR 0001. The connector
275    /// speaks framed binary directly; it does NOT route through
276    /// tonic.
277    RedWire { host: String, port: u16, tls: bool },
278    /// `red+ws://host:port` / `ws://host:port` (plain WS) or
279    /// `red+wss://host:port` / `wss://host:port` (WSS).
280    /// Browser-native WebSocket transport (ADR 0047 direct-when-reachable).
281    /// The UI connects directly — no local RedWire-over-TCP bridge needed.
282    WsNative { host: String, port: u16, tls: bool },
283}
284
285/// Authentication material derived from a connection string or adjacent CLI
286/// fallback. `Debug` deliberately redacts every caller-supplied credential.
287#[derive(Clone, PartialEq, Eq)]
288pub enum ConnectionAuth {
289    Anonymous,
290    Bearer(String),
291    Basic { user: String, pass: String },
292    ApiKey(String),
293}
294
295impl std::fmt::Debug for ConnectionAuth {
296    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
297        match self {
298            Self::Anonymous => f.write_str("Anonymous"),
299            Self::Bearer(_) => f.debug_tuple("Bearer").field(&"<redacted>").finish(),
300            Self::Basic { .. } => f
301                .debug_struct("Basic")
302                .field("user", &"<redacted>")
303                .field("pass", &"<redacted>")
304                .finish(),
305            Self::ApiKey(_) => f.debug_tuple("ApiKey").field(&"<redacted>").finish(),
306        }
307    }
308}
309
310impl ConnectionAuth {
311    pub fn bearer(token: impl Into<String>) -> Self {
312        Self::Bearer(token.into())
313    }
314
315    pub fn is_bearer(&self) -> bool {
316        matches!(self, Self::Bearer(_))
317    }
318}
319
320/// Connection target plus URL-derived auth metadata.
321#[derive(Debug, Clone, PartialEq, Eq)]
322pub struct ConnectionSpec {
323    pub target: ConnectionTarget,
324    pub auth: ConnectionAuth,
325    pub redacted_uri: String,
326}
327
328/// Parse a connection URI into a [`ConnectionTarget`] under the
329/// default DoS limits.
330///
331/// Pure function, no side effects. Behaviour matches
332/// `drivers/rust/src/connect.rs::parse` 1:1 with two additions:
333///   - Mixed-case schemes (e.g. `Red://`, `REDS://`) are normalised
334///     to lowercase before dispatch.
335///   - Inputs exceeding [`ConnStringLimits`] return a structured
336///     [`ParseErrorKind::LimitExceeded`] error instead of being
337///     processed.
338pub fn parse(uri: &str) -> Result<ConnectionTarget, ParseError> {
339    parse_with_limits(uri, ConnStringLimits::default())
340}
341
342/// Parse a connection URI and derive auth from RFC 3986 userinfo.
343///
344/// The existing [`parse`] function remains target-only for compatibility.
345/// New client-mode entry points should use this richer shape so credentials
346/// are identified and redacted before transport connectors are built.
347pub fn parse_with_auth(uri: &str) -> Result<ConnectionSpec, ParseError> {
348    let normalised = normalise_scheme(uri);
349    let redacted_uri = redact_connection_uri(&normalised);
350    let target =
351        parse(uri).map_err(|err| redact_parse_error(err, uri, &normalised, &redacted_uri))?;
352    let auth = auth_from_uri_userinfo(&normalised)
353        .map_err(|err| redact_parse_error(err, uri, &normalised, &redacted_uri))?;
354    Ok(ConnectionSpec {
355        target,
356        auth,
357        redacted_uri,
358    })
359}
360
361fn redact_parse_error(
362    mut err: ParseError,
363    raw_uri: &str,
364    normalised_uri: &str,
365    redacted_uri: &str,
366) -> ParseError {
367    err.message = err
368        .message
369        .replace(raw_uri, redacted_uri)
370        .replace(normalised_uri, redacted_uri);
371    err
372}
373
374/// Return true for documented embedded aliases that must not resolve to
375/// a remote transport target.
376///
377/// This is intentionally separate from [`parse`]: legacy clients may need
378/// to reject embedded targets before mapping `red://host` onto a remote
379/// compatibility transport.
380pub fn is_embedded_connection_uri(uri: &str) -> bool {
381    let trimmed = uri.trim();
382    matches!(
383        trimmed,
384        "red://" | "red:" | "red:///" | "red://:memory" | "red://:memory:"
385    ) || trimmed.starts_with("red:///")
386}
387
388/// Same as [`parse`] but with caller-supplied DoS guardrails.
389/// Useful for tests that need tighter limits or for callers (a
390/// future admin tool, an offline validator) that need to relax the
391/// defaults.
392pub fn parse_with_limits(
393    uri: &str,
394    limits: ConnStringLimits,
395) -> Result<ConnectionTarget, ParseError> {
396    if uri.is_empty() {
397        return Err(ParseError::new(
398            ParseErrorKind::Empty,
399            "empty connection string",
400        ));
401    }
402
403    if uri.len() > limits.max_uri_bytes {
404        return Err(ParseError::new(
405            ParseErrorKind::LimitExceeded,
406            format!(
407                "max_uri_bytes exceeded: limit={} actual={}",
408                limits.max_uri_bytes,
409                uri.len(),
410            ),
411        ));
412    }
413
414    // Lowercase the scheme so `Red://Host`, `REDS://Host`, etc.
415    // dispatch identically to the canonical lowercase forms. The
416    // host and path retain original casing — host is downcased by
417    // `url::Url` for IDN per spec, path stays verbatim.
418    let normalised = normalise_scheme(uri);
419    let uri = normalised.as_str();
420
421    if uri == "memory://" || uri == "memory:" {
422        return Ok(ConnectionTarget::Memory);
423    }
424
425    if let Some(rest) = uri.strip_prefix("file://") {
426        if rest.is_empty() {
427            return Err(ParseError::new(
428                ParseErrorKind::InvalidUri,
429                "file:// URI is missing a path",
430            ));
431        }
432        return Ok(ConnectionTarget::File {
433            path: PathBuf::from(rest),
434        });
435    }
436
437    if let Some(cluster) = try_parse_grpc_cluster(uri, &limits)? {
438        return Ok(cluster);
439    }
440
441    // Before `Url::parse`, because a multi-host authority like
442    // `red://a:1,b:2` fails URL parsing with "invalid port number" — a
443    // message that hides the real problem. `red://` no longer folds to
444    // gRPC (#2159), and RedWire has no multi-host form, so a cluster URI
445    // has to name the transport that supports one.
446    if let Some(rest) = uri
447        .strip_prefix("red://")
448        .or_else(|| uri.strip_prefix("reds://"))
449    {
450        let authority = rest.split(['/', '?', '#']).next().unwrap_or(rest);
451        if authority.contains(',') {
452            return Err(ParseError::new(
453                ParseErrorKind::InvalidUri,
454                "RedWire does not support comma-separated cluster hosts; \
455                 use grpc:// or grpcs:// for a cluster URI",
456            ));
457        }
458    }
459
460    let parsed = Url::parse(uri)
461        .map_err(|e| ParseError::new(ParseErrorKind::InvalidUri, format!("{e}: {uri}")))?;
462
463    enforce_query_param_limit(&parsed, &limits)?;
464
465    match ConnectionScheme::from_uri_scheme(parsed.scheme()) {
466        Some(ConnectionScheme::Red | ConnectionScheme::Reds) => {
467            let host = parsed.host_str().ok_or_else(|| {
468                ParseError::new(ParseErrorKind::InvalidUri, "red:// URI is missing a host")
469            })?;
470            if host.contains(',') {
471                return Err(ParseError::new(
472                    ParseErrorKind::InvalidUri,
473                    "RedWire does not support comma-separated cluster hosts",
474                ));
475            }
476            let port = parsed.port().unwrap_or(DEFAULT_PORT_RED);
477            Ok(ConnectionTarget::RedWire {
478                host: host.to_string(),
479                port,
480                tls: parsed.scheme() == "reds",
481            })
482        }
483        Some(
484            ConnectionScheme::RedWs
485            | ConnectionScheme::RedWss
486            | ConnectionScheme::Ws
487            | ConnectionScheme::Wss,
488        ) => {
489            let host = parsed.host_str().ok_or_else(|| {
490                ParseError::new(
491                    ParseErrorKind::InvalidUri,
492                    "RedWire WebSocket URI is missing a host",
493                )
494            })?;
495            let tls = parsed.scheme() == "red+wss" || parsed.scheme() == "wss";
496            let port = parsed.port().unwrap_or(if tls {
497                DEFAULT_PORT_WSS
498            } else {
499                DEFAULT_PORT_WS
500            });
501            Ok(ConnectionTarget::WsNative {
502                host: host.to_string(),
503                port,
504                tls,
505            })
506        }
507        Some(ConnectionScheme::Grpc | ConnectionScheme::Grpcs) => {
508            let host = parsed.host_str().ok_or_else(|| {
509                ParseError::new(ParseErrorKind::InvalidUri, "grpc:// URI is missing a host")
510            })?;
511            let port = parsed.port().unwrap_or_else(|| {
512                if parsed.scheme() == "grpcs" {
513                    DEFAULT_PORT_GRPCS
514                } else {
515                    DEFAULT_PORT_GRPC
516                }
517            });
518            let endpoint_scheme = if parsed.scheme() == "grpcs" {
519                "https"
520            } else {
521                "http"
522            };
523            Ok(ConnectionTarget::Grpc {
524                endpoint: format!("{endpoint_scheme}://{host}:{port}"),
525            })
526        }
527        Some(ConnectionScheme::Http | ConnectionScheme::Https) => {
528            let host = parsed.host_str().ok_or_else(|| {
529                ParseError::new(
530                    ParseErrorKind::InvalidUri,
531                    "http(s):// URI is missing a host",
532                )
533            })?;
534            let scheme = parsed.scheme();
535            let port = parsed
536                .port()
537                .unwrap_or(if scheme == "https" { 443 } else { 80 });
538            Ok(ConnectionTarget::Http {
539                base_url: format!("{scheme}://{host}:{port}"),
540            })
541        }
542        Some(ConnectionScheme::Memory | ConnectionScheme::File) | None => Err(ParseError::new(
543            ParseErrorKind::UnsupportedScheme,
544            format!("unsupported scheme: {}", parsed.scheme()),
545        )),
546    }
547}
548
549/// Lowercase only the scheme portion (everything before the first
550/// `:`), leaving host/path/query untouched. Returns the original
551/// string when no scheme separator is present so the downstream
552/// `Url::parse` path produces the canonical "missing scheme" error
553/// instead of being masked here.
554fn normalise_scheme(uri: &str) -> String {
555    match uri.find(':') {
556        Some(i) => {
557            let scheme = &uri[..i];
558            // Only ASCII alphanumerics + `+ . -` are valid scheme
559            // bytes per RFC 3986. If the prefix violates that we
560            // leave it alone so `Url::parse` can produce the
561            // structured error.
562            if scheme.is_empty()
563                || !scheme
564                    .bytes()
565                    .all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'.' || b == b'-')
566            {
567                return uri.to_string();
568            }
569            let mut out = String::with_capacity(uri.len());
570            out.push_str(&scheme.to_ascii_lowercase());
571            out.push_str(&uri[i..]);
572            out
573        }
574        None => uri.to_string(),
575    }
576}
577
578fn auth_from_uri_userinfo(uri: &str) -> Result<ConnectionAuth, ParseError> {
579    if uri == "memory://" || uri == "memory:" || uri.starts_with("file://") {
580        return Ok(ConnectionAuth::Anonymous);
581    }
582    let parsed = Url::parse(uri)
583        .map_err(|e| ParseError::new(ParseErrorKind::InvalidUri, format!("{e}: {uri}")))?;
584    let username = parsed.username();
585    if username.is_empty() {
586        return Ok(ConnectionAuth::Anonymous);
587    }
588    match parsed.password() {
589        Some(pass) => Ok(ConnectionAuth::Basic {
590            user: username.to_string(),
591            pass: pass.to_string(),
592        }),
593        None => Ok(ConnectionAuth::ApiKey(username.to_string())),
594    }
595}
596
597fn redact_connection_uri(uri: &str) -> String {
598    let uri = redact_uri_userinfo(uri);
599    redact_uri_query_secrets(&uri)
600}
601
602fn redact_uri_userinfo(uri: &str) -> String {
603    let Some(scheme_end) = uri.find("://") else {
604        return uri.to_string();
605    };
606    let authority_start = scheme_end + 3;
607    let authority_end = uri[authority_start..]
608        .find(['/', '?', '#'])
609        .map(|i| authority_start + i)
610        .unwrap_or(uri.len());
611    let authority = &uri[authority_start..authority_end];
612    let Some(at) = authority.rfind('@') else {
613        return uri.to_string();
614    };
615    let userinfo = &authority[..at];
616    let replacement = if userinfo.contains(':') {
617        "<redacted>:<redacted>"
618    } else {
619        "<redacted>"
620    };
621    format!(
622        "{}{}{}",
623        &uri[..authority_start],
624        replacement,
625        &uri[authority_start + at..]
626    )
627}
628
629fn redact_uri_query_secrets(uri: &str) -> String {
630    let Some(query_start) = uri.find('?') else {
631        return uri.to_string();
632    };
633    let query_end = uri[query_start + 1..]
634        .find('#')
635        .map(|offset| query_start + 1 + offset)
636        .unwrap_or(uri.len());
637    let query = &uri[query_start + 1..query_end];
638    let mut redacted = String::with_capacity(uri.len());
639    redacted.push_str(&uri[..query_start + 1]);
640    for (index, parameter) in query.split('&').enumerate() {
641        if index > 0 {
642            redacted.push('&');
643        }
644        let (name, value) = parameter.split_once('=').unwrap_or((parameter, ""));
645        redacted.push_str(name);
646        if parameter.contains('=') {
647            redacted.push('=');
648            if sensitive_query_name(name) {
649                redacted.push_str("<redacted>");
650            } else {
651                redacted.push_str(value);
652            }
653        }
654    }
655    redacted.push_str(&uri[query_end..]);
656    redacted
657}
658
659fn sensitive_query_name(raw: &str) -> bool {
660    let mut decoded = Vec::with_capacity(raw.len());
661    let bytes = raw.as_bytes();
662    let mut index = 0;
663    while index < bytes.len() {
664        match bytes[index] {
665            b'%' if index + 2 < bytes.len() => {
666                let high = (bytes[index + 1] as char).to_digit(16);
667                let low = (bytes[index + 2] as char).to_digit(16);
668                if let (Some(high), Some(low)) = (high, low) {
669                    decoded.push(((high << 4) | low) as u8);
670                    index += 3;
671                    continue;
672                }
673                decoded.push(bytes[index]);
674            }
675            b'+' => decoded.push(b' '),
676            byte => decoded.push(byte),
677        }
678        index += 1;
679    }
680    let normalized: String = String::from_utf8_lossy(&decoded)
681        .chars()
682        .filter(|ch| *ch != '_' && *ch != '-')
683        .flat_map(char::to_lowercase)
684        .collect();
685    matches!(
686        normalized.as_str(),
687        "token" | "password" | "pass" | "passwd" | "secret" | "cert" | "key" | "ca" | "apikey"
688    ) || normalized.ends_with("token")
689        || normalized.ends_with("password")
690        || normalized.ends_with("passwd")
691        || normalized.ends_with("secret")
692        || normalized.ends_with("apikey")
693        || normalized.ends_with("privatekey")
694        || normalized.ends_with("keyfile")
695        || normalized.ends_with("certfile")
696        || normalized.ends_with("cafile")
697        || ((normalized.starts_with("ssl") || normalized.starts_with("tls"))
698            && (normalized.ends_with("key")
699                || normalized.ends_with("cert")
700                || normalized.ends_with("ca")))
701}
702
703fn enforce_query_param_limit(url: &Url, limits: &ConnStringLimits) -> Result<(), ParseError> {
704    let Some(q) = url.query() else {
705        return Ok(());
706    };
707    if q.is_empty() {
708        return Ok(());
709    }
710    let count = q.split('&').count();
711    if count > limits.max_query_params {
712        return Err(ParseError::new(
713            ParseErrorKind::LimitExceeded,
714            format!(
715                "max_query_params exceeded: limit={} actual={}",
716                limits.max_query_params, count,
717            ),
718        ));
719    }
720    Ok(())
721}
722
723/// Try to parse a multi-host gRPC URI. `Ok(None)` means "this is a
724/// single-host URI — fall through to the standard parser".
725fn try_parse_grpc_cluster(
726    uri: &str,
727    limits: &ConnStringLimits,
728) -> Result<Option<ConnectionTarget>, ParseError> {
729    let (rest, default_port, endpoint_scheme) = if let Some(r) = uri.strip_prefix("grpc://") {
730        (r, DEFAULT_PORT_GRPC, "http")
731    } else if let Some(r) = uri.strip_prefix("grpcs://") {
732        (r, DEFAULT_PORT_GRPCS, "https")
733    } else {
734        return Ok(None);
735    };
736
737    let (host_part, query_part) = match rest.find('?') {
738        Some(i) => (&rest[..i], Some(&rest[i + 1..])),
739        None => (rest, None),
740    };
741
742    if !host_part.contains(',') {
743        return Ok(None);
744    }
745
746    let raw_count = host_part.split(',').count();
747    if raw_count > limits.max_cluster_hosts {
748        return Err(ParseError::new(
749            ParseErrorKind::LimitExceeded,
750            format!(
751                "max_cluster_hosts exceeded: limit={} actual={}",
752                limits.max_cluster_hosts, raw_count,
753            ),
754        ));
755    }
756
757    let mut endpoints: Vec<String> = Vec::with_capacity(raw_count);
758    for raw in host_part.split(',') {
759        let raw = raw.trim();
760        if raw.is_empty() {
761            return Err(ParseError::new(
762                ParseErrorKind::InvalidUri,
763                "grpc cluster URI has an empty host entry",
764            ));
765        }
766        // Bracketed IPv6 literal: `[::1]:5050` or `[::1]`.
767        let (host, port) = if let Some(after_bracket) = raw.strip_prefix('[') {
768            let end = after_bracket.find(']').ok_or_else(|| {
769                ParseError::new(
770                    ParseErrorKind::InvalidUri,
771                    format!("unterminated IPv6 bracket in cluster URI: {raw}"),
772                )
773            })?;
774            let host = &after_bracket[..end];
775            let tail = &after_bracket[end + 1..];
776            let port = if tail.is_empty() {
777                default_port
778            } else if let Some(p) = tail.strip_prefix(':') {
779                p.parse::<u16>().map_err(|_| {
780                    ParseError::new(
781                        ParseErrorKind::InvalidUri,
782                        format!("invalid port in cluster URI: {raw}"),
783                    )
784                })?
785            } else {
786                return Err(ParseError::new(
787                    ParseErrorKind::InvalidUri,
788                    format!("trailing junk after IPv6 bracket in cluster URI: {raw}"),
789                ));
790            };
791            (format!("[{host}]"), port)
792        } else {
793            match raw.rsplit_once(':') {
794                Some((h, p)) => {
795                    let port: u16 = p.parse().map_err(|_| {
796                        ParseError::new(
797                            ParseErrorKind::InvalidUri,
798                            format!("invalid port in cluster URI: {raw}"),
799                        )
800                    })?;
801                    (h.to_string(), port)
802                }
803                None => (raw.to_string(), default_port),
804            }
805        };
806        if host.is_empty() || host == "[]" {
807            return Err(ParseError::new(
808                ParseErrorKind::InvalidUri,
809                "grpc cluster URI has an empty host entry",
810            ));
811        }
812        endpoints.push(format!("{endpoint_scheme}://{host}:{port}"));
813    }
814
815    if let Some(q) = query_part {
816        let qcount = if q.is_empty() {
817            0
818        } else {
819            q.split('&').count()
820        };
821        if qcount > limits.max_query_params {
822            return Err(ParseError::new(
823                ParseErrorKind::LimitExceeded,
824                format!(
825                    "max_query_params exceeded: limit={} actual={}",
826                    limits.max_query_params, qcount,
827                ),
828            ));
829        }
830    }
831
832    let force_primary = query_part
833        .map(|q| {
834            q.split('&').any(|kv| {
835                let mut parts = kv.splitn(2, '=');
836                let k = parts.next().unwrap_or("");
837                let v = parts.next().unwrap_or("");
838                k.eq_ignore_ascii_case("route") && v.eq_ignore_ascii_case("primary")
839            })
840        })
841        .unwrap_or(false);
842
843    let mut iter = endpoints.into_iter();
844    let primary = iter.next().expect("split on ',' yields at least one entry");
845    let replicas: Vec<String> = iter.collect();
846
847    Ok(Some(ConnectionTarget::GrpcCluster {
848        primary,
849        replicas,
850        force_primary,
851    }))
852}