camel-endpoint 0.12.0

Endpoint abstractions for rust-camel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
use std::collections::HashMap;

use camel_api::CamelError;

/// Parsed components of a Camel URI.
///
/// Format: `scheme:path?key1=value1&key2=value2`
#[derive(Clone, PartialEq)]
pub struct UriComponents {
    /// The scheme (component name), e.g. "timer", "log".
    pub scheme: String,
    /// The path portion after the scheme, e.g. "tick" in "timer:tick".
    pub path: String,
    /// Query parameters as key-value pairs.
    pub params: HashMap<String, String>,
}

const SENSITIVE_KEYS: &[&str] = &[
    "password",
    "secret",
    "token",
    "credential",
    "apikey",
    "accesskey",
    "privatekey",
];

fn is_sensitive_key(key: &str) -> bool {
    SENSITIVE_KEYS.contains(&key.to_lowercase().as_str())
}

fn unwrap_raw(value: &str) -> &str {
    if value.starts_with("RAW(") && value.ends_with(')') {
        &value[4..value.len() - 1]
    } else {
        value
    }
}

fn is_raw_value(value: &str) -> bool {
    value.starts_with("RAW(") && value.ends_with(')')
}

/// Percent-decode a string per RFC 3986.
///
/// `%XX` sequences are replaced by the byte represented by the hex digits.
/// `+` is NOT treated as space (Camel URIs are not form-encoded).
/// Returns an error for incomplete or invalid `%XX` sequences, or if the
/// resulting bytes are not valid UTF-8.
fn percent_decode(s: &str) -> Result<String, CamelError> {
    let bytes = s.as_bytes();
    let mut result = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' {
            if i + 2 >= bytes.len() {
                return Err(CamelError::InvalidUri(format!(
                    "incomplete percent-encoding at position {i} in '{s}'"
                )));
            }
            let hi = char::from(bytes[i + 1]);
            let lo = char::from(bytes[i + 2]);
            let byte = u8::from_str_radix(&format!("{hi}{lo}"), 16).map_err(|_| {
                CamelError::InvalidUri(format!("invalid percent-encoding '%{hi}{lo}' in '{s}'"))
            })?;
            result.push(byte);
            i += 3;
        } else {
            result.push(bytes[i]);
            i += 1;
        }
    }
    String::from_utf8(result).map_err(|e| {
        CamelError::InvalidUri(format!("percent-decoded bytes are not valid UTF-8: {e}"))
    })
}

impl std::fmt::Debug for UriComponents {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut redacted_params = std::collections::HashMap::new();
        for (k, v) in &self.params {
            if is_sensitive_key(k) {
                redacted_params.insert(k.clone(), "***".to_string());
            } else {
                redacted_params.insert(k.clone(), v.clone());
            }
        }
        f.debug_struct("UriComponents")
            .field("scheme", &self.scheme)
            .field("path", &self.path)
            .field("params", &redacted_params)
            .finish()
    }
}

/// Parse a Camel-style URI into its components.
///
/// Format: `scheme:path?key1=value1&key2=value2`
pub fn parse_uri(uri: &str) -> Result<UriComponents, CamelError> {
    let (scheme, rest) = uri.split_once(':').ok_or_else(|| {
        CamelError::InvalidUri(format!("missing scheme separator ':' in '{uri}'"))
    })?;

    if scheme.is_empty() {
        return Err(CamelError::InvalidUri(format!("empty scheme in '{uri}'")));
    }

    // EP-005: Validate scheme characters — only alphanumeric and hyphens allowed.
    if !scheme
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '-')
    {
        return Err(CamelError::InvalidUri(format!(
            "invalid scheme '{scheme}': must contain only alphanumeric characters and hyphens"
        )));
    }

    let (path, params) = match rest.split_once('?') {
        Some((path, query)) => (path, parse_query(query)?),
        None => (rest, HashMap::new()),
    };

    Ok(UriComponents {
        scheme: scheme.to_string(),
        path: percent_decode(path)?,
        params,
    })
}

fn parse_query(query: &str) -> Result<HashMap<String, String>, CamelError> {
    let mut params = HashMap::new();

    for pair in split_query_pairs(query)
        .into_iter()
        .filter(|s| !s.is_empty())
    {
        let Some((key, value)) = pair.split_once('=') else {
            return Err(CamelError::InvalidUri(format!(
                "query parameter '{}' has no value",
                pair
            )));
        };

        let decoded_key = percent_decode(key)?;

        if params.contains_key(&decoded_key) {
            return Err(CamelError::InvalidUri(format!(
                "duplicate query parameter: {}",
                decoded_key
            )));
        }

        let parsed_value = if is_raw_value(value) {
            // RAW(...) signals "treat this value literally, no further processing".
            // For sensitive keys: unwrap the RAW(...) wrapper so the stored value is
            // the plain secret (consistent with pre-existing sensitive key handling).
            // For non-sensitive keys: preserve the full `RAW(...)` string intact so
            // downstream consumers can detect it and handle it explicitly (e.g., avoid
            // encoding it again). This is intentional, not an oversight.
            if is_sensitive_key(&decoded_key) {
                unwrap_raw(value).to_string()
            } else {
                value.to_string()
            }
        } else if is_sensitive_key(&decoded_key) {
            // Sensitive non-RAW: preserve literally (no decode)
            value.to_string()
        } else {
            // Non-sensitive non-RAW: percent-decode
            percent_decode(value)?
        };

        params.insert(decoded_key, parsed_value);
    }

    Ok(params)
}

fn split_query_pairs(query: &str) -> Vec<&str> {
    let mut pairs = Vec::new();
    let mut start = 0usize;
    let mut i = 0usize;
    let mut raw_depth = 0usize;

    while i < query.len() {
        let rest = &query[i..];

        if rest.starts_with("RAW(") {
            raw_depth += 1;
            i += 4;
            continue;
        }

        let ch = rest.as_bytes()[0] as char;
        match ch {
            ')' if raw_depth > 0 => raw_depth -= 1,
            '&' if raw_depth == 0 => {
                pairs.push(&query[start..i]);
                i += 1;
                start = i;
                continue;
            }
            _ => {}
        }

        i += 1;
    }

    pairs.push(&query[start..]);
    pairs
}

/// Parse a boolean parameter from a string, case-insensitively.
///
/// Accepts: "true"/"True"/"TRUE"/"1"/"yes" as true,
///          "false"/"False"/"FALSE"/"0"/"no" as false.
pub fn parse_bool_param(s: &str) -> Result<bool, String> {
    match s.to_lowercase().as_str() {
        "true" | "1" | "yes" => Ok(true),
        "false" | "0" | "no" => Ok(false),
        _ => Err(format!("invalid boolean value: '{}'", s)),
    }
}

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

    #[test]
    fn test_parse_simple_uri() {
        let result = parse_uri("timer:tick").unwrap();
        assert_eq!(result.scheme, "timer");
        assert_eq!(result.path, "tick");
        assert!(result.params.is_empty());
    }

    #[test]
    fn test_parse_uri_with_params() {
        let result = parse_uri("timer:tick?period=1000&delay=500").unwrap();
        assert_eq!(result.scheme, "timer");
        assert_eq!(result.path, "tick");
        assert_eq!(result.params.get("period"), Some(&"1000".to_string()));
        assert_eq!(result.params.get("delay"), Some(&"500".to_string()));
    }

    #[test]
    fn test_parse_uri_with_single_param() {
        let result = parse_uri("log:info?level=debug").unwrap();
        assert_eq!(result.scheme, "log");
        assert_eq!(result.path, "info");
        assert_eq!(result.params.get("level"), Some(&"debug".to_string()));
    }

    #[test]
    fn test_parse_uri_no_scheme() {
        let result = parse_uri("noscheme");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_uri_empty_scheme() {
        let result = parse_uri(":path");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_direct_uri() {
        let result = parse_uri("direct:myRoute").unwrap();
        assert_eq!(result.scheme, "direct");
        assert_eq!(result.path, "myRoute");
        assert!(result.params.is_empty());
    }

    #[test]
    fn test_parse_mock_uri() {
        let result = parse_uri("mock:result").unwrap();
        assert_eq!(result.scheme, "mock");
        assert_eq!(result.path, "result");
    }

    #[test]
    fn test_parse_http_uri_simple() {
        let result = parse_uri("http://localhost:8080/api/users").unwrap();
        assert_eq!(result.scheme, "http");
        assert_eq!(result.path, "//localhost:8080/api/users");
        assert!(result.params.is_empty());
    }

    #[test]
    fn test_parse_https_uri_with_camel_params() {
        let result = parse_uri(
            "https://api.example.com/v1/data?httpMethod=POST&throwExceptionOnFailure=false",
        )
        .unwrap();
        assert_eq!(result.scheme, "https");
        assert_eq!(result.path, "//api.example.com/v1/data");
        assert_eq!(result.params.get("httpMethod"), Some(&"POST".to_string()));
        assert_eq!(
            result.params.get("throwExceptionOnFailure"),
            Some(&"false".to_string())
        );
    }

    #[test]
    fn test_parse_http_uri_no_path() {
        let result = parse_uri("http://localhost:8080").unwrap();
        assert_eq!(result.scheme, "http");
        assert_eq!(result.path, "//localhost:8080");
        assert!(result.params.is_empty());
    }

    #[test]
    fn test_parse_http_uri_with_port_and_query() {
        let result = parse_uri("http://example.com:3000/api?connectTimeout=5000").unwrap();
        assert_eq!(result.scheme, "http");
        assert_eq!(result.path, "//example.com:3000/api");
        assert_eq!(
            result.params.get("connectTimeout"),
            Some(&"5000".to_string())
        );
    }

    #[test]
    fn test_uri_components_debug_redacts_sensitive_params() {
        let uri = parse_uri("timer:tick?password=secret&token=abc123&name=hello").unwrap();
        let debug_output = format!("{:?}", uri);
        assert!(
            !debug_output.contains("secret"),
            "Debug must not contain password value"
        );
        assert!(
            !debug_output.contains("abc123"),
            "Debug must not contain token value"
        );
        assert!(
            debug_output.contains("hello"),
            "Debug should contain non-sensitive param values"
        );
        assert!(
            debug_output.contains("password"),
            "Debug should show param key 'password'"
        );
    }

    #[test]
    fn test_uri_components_debug_redacts_case_insensitive() {
        let uri = parse_uri("timer:tick?Password=secret&TOKEN=abc123").unwrap();
        let debug_output = format!("{:?}", uri);
        assert!(
            !debug_output.contains("secret"),
            "Debug must redact 'Password' (capitalized)"
        );
        assert!(
            !debug_output.contains("abc123"),
            "Debug must redact 'TOKEN' (uppercase)"
        );
    }

    #[test]
    fn test_parse_bool_param_true_variants() {
        for val in &["true", "True", "TRUE", "1", "yes", "Yes", "YES"] {
            assert_eq!(
                parse_bool_param(val),
                Ok(true),
                "parse_bool_param('{}') should be Ok(true)",
                val
            );
        }
    }

    #[test]
    fn test_parse_bool_param_false_variants() {
        for val in &["false", "False", "FALSE", "0", "no", "No", "NO"] {
            assert_eq!(
                parse_bool_param(val),
                Ok(false),
                "parse_bool_param('{}') should be Ok(false)",
                val
            );
        }
    }

    #[test]
    fn test_parse_bool_param_invalid() {
        for val in &["maybe", "yes ", " true", "2", "-1", ""] {
            assert!(
                parse_bool_param(val).is_err(),
                "parse_bool_param('{}') should be Err",
                val
            );
        }
    }

    #[test]
    fn test_raw_token_extracts_value() {
        assert_eq!(unwrap_raw("RAW(p@ss!)"), "p@ss!");
        assert_eq!(unwrap_raw("RAW(user:pass@host)"), "user:pass@host");
    }

    #[test]
    fn test_non_raw_value_unchanged() {
        assert_eq!(unwrap_raw("plainvalue"), "plainvalue");
        assert_eq!(unwrap_raw("RAW(unclosed"), "RAW(unclosed");
    }

    #[test]
    fn test_uri_with_raw_password_parses_correctly() {
        let result = parse_uri("redis://localhost?password=RAW(p@ss!)").unwrap();
        assert_eq!(result.params.get("password"), Some(&"p@ss!".to_string()));
    }

    #[test]
    fn test_uri_with_raw_password_containing_ampersand_parses_correctly() {
        let result = parse_uri("redis://localhost?password=RAW(a&b)&db=0").unwrap();
        assert_eq!(result.params.get("password"), Some(&"a&b".to_string()));
        assert_eq!(result.params.get("db"), Some(&"0".to_string()));
    }

    #[test]
    fn test_uri_with_non_sensitive_raw_value_is_unchanged() {
        let result = parse_uri("timer:tick?name=RAW(p@ss!)").unwrap();
        assert_eq!(result.params.get("name"), Some(&"RAW(p@ss!)".to_string()));
    }

    #[test]
    fn test_parse_uri_duplicate_query_key_returns_error() {
        let result = parse_uri("foo:bar?key=a&key=b");
        assert!(result.is_err());
        match result {
            Err(CamelError::InvalidUri(msg)) => {
                assert_eq!(msg, "duplicate query parameter: key");
            }
            _ => panic!("Expected InvalidUri for duplicate key"),
        }
    }

    #[test]
    fn test_parse_uri_bare_query_param_returns_error() {
        let result = parse_uri("foo:bar?flag");
        assert!(result.is_err());
        match result {
            Err(CamelError::InvalidUri(msg)) => {
                assert_eq!(msg, "query parameter 'flag' has no value");
            }
            _ => panic!("Expected InvalidUri for bare query parameter"),
        }
    }

    #[test]
    fn test_parse_uri_duplicate_key_with_raw_ampersand_returns_error() {
        let result = parse_uri("foo:bar?password=RAW(a&b)&password=RAW(c&d)");
        assert!(result.is_err());
        match result {
            Err(CamelError::InvalidUri(msg)) => {
                assert_eq!(msg, "duplicate query parameter: password");
            }
            _ => panic!("Expected InvalidUri for duplicate key with RAW value"),
        }
    }

    // EP-005: scheme validation tests

    #[test]
    fn test_valid_scheme_alphanumeric() {
        let result = parse_uri("timer:tick").unwrap();
        assert_eq!(result.scheme, "timer");
    }

    #[test]
    fn test_valid_scheme_with_hyphen() {
        let result = parse_uri("my-component:path").unwrap();
        assert_eq!(result.scheme, "my-component");
    }

    #[test]
    fn test_valid_scheme_alphanumeric_only() {
        let result = parse_uri("opensearchs://host:9200/idx").unwrap();
        assert_eq!(result.scheme, "opensearchs");
    }

    #[test]
    fn test_invalid_scheme_with_space() {
        let result = parse_uri("bad scheme:path");
        assert!(result.is_err());
        match result {
            Err(CamelError::InvalidUri(msg)) => {
                assert!(msg.contains("invalid scheme"), "got: {msg}");
            }
            _ => panic!("Expected InvalidUri for scheme with space"),
        }
    }

    #[test]
    fn test_invalid_scheme_with_dot() {
        let result = parse_uri("bad.scheme:path");
        assert!(result.is_err());
        match result {
            Err(CamelError::InvalidUri(msg)) => {
                assert!(msg.contains("invalid scheme"), "got: {msg}");
            }
            _ => panic!("Expected InvalidUri for scheme with dot"),
        }
    }

    #[test]
    fn test_invalid_scheme_with_underscore() {
        let result = parse_uri("bad_scheme:path");
        assert!(result.is_err());
    }

    // ENDPOINT-002: percent-decoding tests

    #[test]
    fn test_parse_uri_percent_encoded_path() {
        let result = parse_uri("timer:my%20timer").unwrap();
        assert_eq!(result.path, "my timer");
    }

    #[test]
    fn test_parse_uri_percent_encoded_query_value() {
        let result = parse_uri("log:info?description=hello%20world").unwrap();
        assert_eq!(
            result.params.get("description"),
            Some(&"hello world".to_string())
        );
    }

    #[test]
    fn test_parse_uri_percent_encoded_special_chars() {
        // %2F = '/', %3A = ':', %40 = '@'
        let result = parse_uri("http://host/path?user=foo%40bar.com&redirect=%2Fhome").unwrap();
        assert_eq!(result.params.get("user"), Some(&"foo@bar.com".to_string()));
        assert_eq!(result.params.get("redirect"), Some(&"/home".to_string()));
    }

    #[test]
    fn test_parse_uri_percent_encoded_path_with_slash() {
        let result = parse_uri("file:my%2Fpath%2Fhere").unwrap();
        assert_eq!(result.path, "my/path/here");
    }

    #[test]
    fn test_raw_value_not_percent_decoded() {
        // RAW(...) values bypass percent-decoding — they are already raw
        let result = parse_uri("redis://localhost?password=RAW(%40secret)").unwrap();
        assert_eq!(
            result.params.get("password"),
            Some(&"%40secret".to_string())
        );
    }

    #[test]
    fn test_percent_encoded_key_decoded() {
        let result = parse_uri("foo:bar?my%20key=value").unwrap();
        assert_eq!(result.params.get("my key"), Some(&"value".to_string()));
    }

    #[test]
    fn test_invalid_percent_sequence_returns_error() {
        let result = parse_uri("foo:bar?key=%ZZ");
        assert!(
            result.is_err(),
            "Expected error for invalid percent sequence %ZZ"
        );
    }

    #[test]
    fn test_incomplete_percent_sequence_returns_error() {
        let result = parse_uri("foo:bar?key=val%");
        assert!(
            result.is_err(),
            "Expected error for incomplete percent sequence"
        );
        let result2 = parse_uri("foo:bar?key=val%2");
        assert!(
            result2.is_err(),
            "Expected error for truncated percent sequence"
        );
    }

    #[test]
    fn test_percent_encoded_plus_is_not_space() {
        // Camel URIs are NOT form-encoded; '+' is a literal plus, not space
        let result = parse_uri("foo:bar?key=a+b").unwrap();
        assert_eq!(result.params.get("key"), Some(&"a+b".to_string()));
    }

    #[test]
    fn test_percent_encoded_plus_decodes_to_plus() {
        // %2B must decode to literal '+' in both path and query value
        let result = parse_uri("file:a%2Bb?key=c%2Bd").unwrap();
        assert_eq!(result.path, "a+b");
        assert_eq!(result.params.get("key"), Some(&"c+d".to_string()));
    }

    #[test]
    fn test_percent_encoded_multibyte_utf8() {
        // %C3%A9 = U+00E9 LATIN SMALL LETTER E WITH ACUTE ('é')
        let result = parse_uri("file:caf%C3%A9?name=r%C3%A9sum%C3%A9").unwrap();
        assert_eq!(result.path, "café");
        assert_eq!(result.params.get("name"), Some(&"résumé".to_string()));
    }

    #[test]
    fn test_percent_encoded_null_byte_allowed() {
        // %00 decodes to NUL byte — behavior is pinned: decoder allows it, result contains '\0'
        let result = parse_uri("foo:bar?key=val%00end").unwrap();
        assert_eq!(result.params.get("key"), Some(&"val\0end".to_string()));
    }

    #[test]
    fn test_sensitive_key_percent_encoded() {
        // Key is percent-decoded before sensitivity check; sensitive value is NOT percent-decoded
        let result = parse_uri("db:conn?pass%77ord=abc%20def").unwrap();
        // "pass%77ord" decodes key to "password" → sensitive → value stored literal
        assert_eq!(
            result.params.get("password"),
            Some(&"abc%20def".to_string())
        );
    }
}