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 = 5055;
80/// Default ports for `red+ws://` and `red+wss://` — align with the
81/// standard WS / WSS browser defaults (80 and 443) so a hosted endpoint
82/// like `*.db.reddb.io` works without an explicit port.
83pub const DEFAULT_PORT_WS: u16 = 80;
84pub const DEFAULT_PORT_WSS: u16 = 443;
85
86/// DoS guardrails applied by [`parse`] before any URI work happens.
87///
88/// The connection-string parser is the only entry point an attacker
89/// can reach BEFORE auth, so every limit here is enforced eagerly
90/// and surfaces as a structured [`ParseErrorKind::LimitExceeded`]
91/// error rather than a panic, hang, or unbounded allocation.
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub struct ConnStringLimits {
94    /// Maximum length of the input URI in bytes. Default `8 KiB`.
95    pub max_uri_bytes: usize,
96    /// Maximum number of `key=value` query parameters. Default `32`.
97    pub max_query_params: usize,
98    /// Maximum number of comma-separated cluster hosts allowed in a
99    /// `red://`/`reds://`/`grpc://` cluster URI. Default `64`.
100    pub max_cluster_hosts: usize,
101}
102
103impl Default for ConnStringLimits {
104    fn default() -> Self {
105        Self {
106            max_uri_bytes: 8 * 1024,
107            max_query_params: 32,
108            max_cluster_hosts: 64,
109        }
110    }
111}
112
113/// Normalised target produced by [`parse`].
114///
115/// Variants intentionally mirror the public Rust client target shape
116/// so callers can keep a thin compatibility layer without duplicating
117/// parser behavior.
118#[derive(Debug, Clone, PartialEq, Eq)]
119pub enum ConnectionTarget {
120    /// `memory://` — ephemeral, in-memory backend.
121    Memory,
122    /// `file:///abs/path` — embedded engine on disk.
123    File { path: PathBuf },
124    /// Single remote endpoint over `grpc://` or `grpcs://`. Stored
125    /// as a normalised `http://host:port` string because tonic's
126    /// `Endpoint` consumes that form.
127    Grpc { endpoint: String },
128    /// Multi-host gRPC URI: primary + read replicas. Writes hit the
129    /// primary; reads round-robin across replicas unless
130    /// `force_primary` is set.
131    GrpcCluster {
132        primary: String,
133        replicas: Vec<String>,
134        force_primary: bool,
135    },
136    /// `http://host:port` / `https://host:port` — REST endpoint.
137    Http { base_url: String },
138    /// `red://host:port` (plain TCP) or `reds://host:port` (TLS).
139    /// RedWire binary frame protocol per ADR 0001. The connector
140    /// speaks framed binary directly; it does NOT route through
141    /// tonic.
142    RedWire { host: String, port: u16, tls: bool },
143    /// `red+ws://host:port` (plain WS) or `red+wss://host:port` (WSS).
144    /// Browser-native WebSocket transport (ADR 0047 direct-when-reachable).
145    /// The UI connects directly — no local RedWire-over-TCP bridge needed.
146    WsNative { host: String, port: u16, tls: bool },
147}
148
149/// Parse a connection URI into a [`ConnectionTarget`] under the
150/// default DoS limits.
151///
152/// Pure function, no side effects. Behaviour matches
153/// `drivers/rust/src/connect.rs::parse` 1:1 with two additions:
154///   - Mixed-case schemes (e.g. `Red://`, `REDS://`) are normalised
155///     to lowercase before dispatch.
156///   - Inputs exceeding [`ConnStringLimits`] return a structured
157///     [`ParseErrorKind::LimitExceeded`] error instead of being
158///     processed.
159pub fn parse(uri: &str) -> Result<ConnectionTarget, ParseError> {
160    parse_with_limits(uri, ConnStringLimits::default())
161}
162
163/// Return true for documented embedded aliases that must not resolve to
164/// a remote transport target.
165///
166/// This is intentionally separate from [`parse`]: legacy clients may need
167/// to reject embedded targets before mapping `red://host` onto a remote
168/// compatibility transport.
169pub fn is_embedded_connection_uri(uri: &str) -> bool {
170    let trimmed = uri.trim();
171    matches!(
172        trimmed,
173        "red://" | "red:" | "red:///" | "red://:memory" | "red://:memory:"
174    ) || trimmed.starts_with("red:///")
175}
176
177/// Same as [`parse`] but with caller-supplied DoS guardrails.
178/// Useful for tests that need tighter limits or for callers (a
179/// future admin tool, an offline validator) that need to relax the
180/// defaults.
181pub fn parse_with_limits(
182    uri: &str,
183    limits: ConnStringLimits,
184) -> Result<ConnectionTarget, ParseError> {
185    if uri.is_empty() {
186        return Err(ParseError::new(
187            ParseErrorKind::Empty,
188            "empty connection string",
189        ));
190    }
191
192    if uri.len() > limits.max_uri_bytes {
193        return Err(ParseError::new(
194            ParseErrorKind::LimitExceeded,
195            format!(
196                "max_uri_bytes exceeded: limit={} actual={}",
197                limits.max_uri_bytes,
198                uri.len(),
199            ),
200        ));
201    }
202
203    // Lowercase the scheme so `Red://Host`, `REDS://Host`, etc.
204    // dispatch identically to the canonical lowercase forms. The
205    // host and path retain original casing — host is downcased by
206    // `url::Url` for IDN per spec, path stays verbatim.
207    let normalised = normalise_scheme(uri);
208    let uri = normalised.as_str();
209
210    if uri == "memory://" || uri == "memory:" {
211        return Ok(ConnectionTarget::Memory);
212    }
213
214    if let Some(rest) = uri.strip_prefix("file://") {
215        if rest.is_empty() {
216            return Err(ParseError::new(
217                ParseErrorKind::InvalidUri,
218                "file:// URI is missing a path",
219            ));
220        }
221        return Ok(ConnectionTarget::File {
222            path: PathBuf::from(rest),
223        });
224    }
225
226    if let Some(cluster) = try_parse_grpc_cluster(uri, &limits)? {
227        return Ok(cluster);
228    }
229
230    let parsed = Url::parse(uri)
231        .map_err(|e| ParseError::new(ParseErrorKind::InvalidUri, format!("{e}: {uri}")))?;
232
233    enforce_query_param_limit(&parsed, &limits)?;
234
235    match parsed.scheme() {
236        "red" | "reds" => {
237            let host = parsed.host_str().ok_or_else(|| {
238                ParseError::new(ParseErrorKind::InvalidUri, "red:// URI is missing a host")
239            })?;
240            let port = parsed.port().unwrap_or(DEFAULT_PORT_RED);
241            Ok(ConnectionTarget::RedWire {
242                host: host.to_string(),
243                port,
244                tls: parsed.scheme() == "reds",
245            })
246        }
247        "red+ws" | "red+wss" => {
248            let host = parsed.host_str().ok_or_else(|| {
249                ParseError::new(
250                    ParseErrorKind::InvalidUri,
251                    "red+ws(s):// URI is missing a host",
252                )
253            })?;
254            let tls = parsed.scheme() == "red+wss";
255            let port = parsed.port().unwrap_or(if tls {
256                DEFAULT_PORT_WSS
257            } else {
258                DEFAULT_PORT_WS
259            });
260            Ok(ConnectionTarget::WsNative {
261                host: host.to_string(),
262                port,
263                tls,
264            })
265        }
266        "grpc" | "grpcs" => {
267            let host = parsed.host_str().ok_or_else(|| {
268                ParseError::new(ParseErrorKind::InvalidUri, "grpc:// URI is missing a host")
269            })?;
270            let port = parsed.port().unwrap_or(DEFAULT_PORT_GRPC);
271            Ok(ConnectionTarget::Grpc {
272                endpoint: format!("http://{host}:{port}"),
273            })
274        }
275        "http" | "https" => {
276            let host = parsed.host_str().ok_or_else(|| {
277                ParseError::new(
278                    ParseErrorKind::InvalidUri,
279                    "http(s):// URI is missing a host",
280                )
281            })?;
282            let scheme = parsed.scheme();
283            let port = parsed
284                .port()
285                .unwrap_or(if scheme == "https" { 443 } else { 80 });
286            Ok(ConnectionTarget::Http {
287                base_url: format!("{scheme}://{host}:{port}"),
288            })
289        }
290        other => Err(ParseError::new(
291            ParseErrorKind::UnsupportedScheme,
292            format!("unsupported scheme: {other}"),
293        )),
294    }
295}
296
297/// Lowercase only the scheme portion (everything before the first
298/// `:`), leaving host/path/query untouched. Returns the original
299/// string when no scheme separator is present so the downstream
300/// `Url::parse` path produces the canonical "missing scheme" error
301/// instead of being masked here.
302fn normalise_scheme(uri: &str) -> String {
303    match uri.find(':') {
304        Some(i) => {
305            let scheme = &uri[..i];
306            // Only ASCII alphanumerics + `+ . -` are valid scheme
307            // bytes per RFC 3986. If the prefix violates that we
308            // leave it alone so `Url::parse` can produce the
309            // structured error.
310            if scheme.is_empty()
311                || !scheme
312                    .bytes()
313                    .all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'.' || b == b'-')
314            {
315                return uri.to_string();
316            }
317            let mut out = String::with_capacity(uri.len());
318            out.push_str(&scheme.to_ascii_lowercase());
319            out.push_str(&uri[i..]);
320            out
321        }
322        None => uri.to_string(),
323    }
324}
325
326fn enforce_query_param_limit(url: &Url, limits: &ConnStringLimits) -> Result<(), ParseError> {
327    let Some(q) = url.query() else {
328        return Ok(());
329    };
330    if q.is_empty() {
331        return Ok(());
332    }
333    let count = q.split('&').count();
334    if count > limits.max_query_params {
335        return Err(ParseError::new(
336            ParseErrorKind::LimitExceeded,
337            format!(
338                "max_query_params exceeded: limit={} actual={}",
339                limits.max_query_params, count,
340            ),
341        ));
342    }
343    Ok(())
344}
345
346/// Try to parse a multi-host gRPC URI. `Ok(None)` means "this is a
347/// single-host URI — fall through to the standard parser".
348fn try_parse_grpc_cluster(
349    uri: &str,
350    limits: &ConnStringLimits,
351) -> Result<Option<ConnectionTarget>, ParseError> {
352    let (rest, default_port) = if let Some(r) = uri.strip_prefix("grpc://") {
353        (r, DEFAULT_PORT_GRPC)
354    } else if let Some(r) = uri.strip_prefix("grpcs://") {
355        (r, DEFAULT_PORT_GRPC)
356    } else if let Some(r) = uri
357        .strip_prefix("red://")
358        .or_else(|| uri.strip_prefix("reds://"))
359    {
360        (r, DEFAULT_PORT_RED)
361    } else {
362        return Ok(None);
363    };
364
365    let (host_part, query_part) = match rest.find('?') {
366        Some(i) => (&rest[..i], Some(&rest[i + 1..])),
367        None => (rest, None),
368    };
369
370    if !host_part.contains(',') {
371        return Ok(None);
372    }
373
374    let raw_count = host_part.split(',').count();
375    if raw_count > limits.max_cluster_hosts {
376        return Err(ParseError::new(
377            ParseErrorKind::LimitExceeded,
378            format!(
379                "max_cluster_hosts exceeded: limit={} actual={}",
380                limits.max_cluster_hosts, raw_count,
381            ),
382        ));
383    }
384
385    let mut endpoints: Vec<String> = Vec::with_capacity(raw_count);
386    for raw in host_part.split(',') {
387        let raw = raw.trim();
388        if raw.is_empty() {
389            return Err(ParseError::new(
390                ParseErrorKind::InvalidUri,
391                "grpc cluster URI has an empty host entry",
392            ));
393        }
394        // Bracketed IPv6 literal: `[::1]:5050` or `[::1]`.
395        let (host, port) = if let Some(after_bracket) = raw.strip_prefix('[') {
396            let end = after_bracket.find(']').ok_or_else(|| {
397                ParseError::new(
398                    ParseErrorKind::InvalidUri,
399                    format!("unterminated IPv6 bracket in cluster URI: {raw}"),
400                )
401            })?;
402            let host = &after_bracket[..end];
403            let tail = &after_bracket[end + 1..];
404            let port = if tail.is_empty() {
405                default_port
406            } else if let Some(p) = tail.strip_prefix(':') {
407                p.parse::<u16>().map_err(|_| {
408                    ParseError::new(
409                        ParseErrorKind::InvalidUri,
410                        format!("invalid port in cluster URI: {raw}"),
411                    )
412                })?
413            } else {
414                return Err(ParseError::new(
415                    ParseErrorKind::InvalidUri,
416                    format!("trailing junk after IPv6 bracket in cluster URI: {raw}"),
417                ));
418            };
419            (format!("[{host}]"), port)
420        } else {
421            match raw.rsplit_once(':') {
422                Some((h, p)) => {
423                    let port: u16 = p.parse().map_err(|_| {
424                        ParseError::new(
425                            ParseErrorKind::InvalidUri,
426                            format!("invalid port in cluster URI: {raw}"),
427                        )
428                    })?;
429                    (h.to_string(), port)
430                }
431                None => (raw.to_string(), default_port),
432            }
433        };
434        if host.is_empty() || host == "[]" {
435            return Err(ParseError::new(
436                ParseErrorKind::InvalidUri,
437                "grpc cluster URI has an empty host entry",
438            ));
439        }
440        endpoints.push(format!("http://{host}:{port}"));
441    }
442
443    if let Some(q) = query_part {
444        let qcount = if q.is_empty() {
445            0
446        } else {
447            q.split('&').count()
448        };
449        if qcount > limits.max_query_params {
450            return Err(ParseError::new(
451                ParseErrorKind::LimitExceeded,
452                format!(
453                    "max_query_params exceeded: limit={} actual={}",
454                    limits.max_query_params, qcount,
455                ),
456            ));
457        }
458    }
459
460    let force_primary = query_part
461        .map(|q| {
462            q.split('&').any(|kv| {
463                let mut parts = kv.splitn(2, '=');
464                let k = parts.next().unwrap_or("");
465                let v = parts.next().unwrap_or("");
466                k.eq_ignore_ascii_case("route") && v.eq_ignore_ascii_case("primary")
467            })
468        })
469        .unwrap_or(false);
470
471    let mut iter = endpoints.into_iter();
472    let primary = iter.next().expect("split on ',' yields at least one entry");
473    let replicas: Vec<String> = iter.collect();
474
475    Ok(Some(ConnectionTarget::GrpcCluster {
476        primary,
477        replicas,
478        force_primary,
479    }))
480}