camel-component-ws 0.11.0

WebSocket component 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
use std::time::Duration;

use camel_component_api::CamelError;

#[derive(Debug, Clone, Default, serde::Deserialize)]
pub struct WsConfig {
    pub max_connections: Option<u32>,
    pub max_message_size: Option<u32>,
    pub heartbeat_interval_ms: Option<u64>,
    pub idle_timeout_ms: Option<u64>,
    pub connect_timeout_ms: Option<u64>,
    pub response_timeout_ms: Option<u64>,
    pub send_timeout_ms: Option<u64>,
    pub binary_payload: Option<bool>,
    pub subprotocols: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct WsEndpointConfig {
    pub scheme: String,
    pub host: String,
    pub port: u16,
    pub path: String,
    pub max_connections: u32,
    pub max_message_size: u32,
    pub send_to_all: bool,
    pub heartbeat_interval: Duration,
    pub idle_timeout: Duration,
    pub connect_timeout: Duration,
    pub response_timeout: Duration,
    pub allow_origin: String,
    pub tls_cert: Option<String>,
    pub tls_key: Option<String>,
    pub reconnect: bool,
    pub reconnect_max_attempts: u32,
    pub reconnect_delay_ms: u64,
    pub send_timeout: Duration,
    pub binary_payload: bool,
    pub subprotocols: Vec<String>,
}

impl Default for WsEndpointConfig {
    fn default() -> Self {
        Self {
            scheme: "ws".into(),
            host: "0.0.0.0".into(),
            port: 8080,
            path: "/".into(),
            max_connections: 100,
            max_message_size: 65536,
            send_to_all: false,
            heartbeat_interval: Duration::ZERO,
            idle_timeout: Duration::ZERO,
            connect_timeout: Duration::from_secs(10),
            response_timeout: Duration::from_secs(30),
            allow_origin: "*".into(),
            tls_cert: None,
            tls_key: None,
            reconnect: true,
            reconnect_max_attempts: 5,
            reconnect_delay_ms: 1000,
            send_timeout: Duration::from_secs(30),
            binary_payload: false,
            subprotocols: Vec::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct WsServerConfig {
    pub inner: WsEndpointConfig,
}

#[derive(Debug, Clone)]
pub struct WsClientConfig {
    pub inner: WsEndpointConfig,
}

impl WsConfig {
    /// Validate configuration values.
    ///
    /// Returns an error if any explicitly-set value is invalid (e.g. zero).
    /// `None` values are valid — they mean "use the default / unlimited".
    pub fn validate(&self) -> Result<(), CamelError> {
        if let Some(0) = self.max_connections {
            return Err(CamelError::Config(
                "maxConnections must be >= 1 when specified".into(),
            ));
        }
        if let Some(0) = self.max_message_size {
            return Err(CamelError::Config(
                "maxMessageSize must be >= 1 when specified".into(),
            ));
        }
        Ok(())
    }
}

impl WsEndpointConfig {
    pub fn from_uri(uri: &str) -> Result<Self, CamelError> {
        let parsed = camel_component_api::parse_uri(uri)
            .map_err(|e| CamelError::EndpointCreationFailed(e.to_string()))?;

        let scheme = parsed.scheme;
        if scheme != "ws" && scheme != "wss" {
            return Err(CamelError::EndpointCreationFailed(format!(
                "Invalid WebSocket scheme: {scheme}"
            )));
        }

        let host_port_path = parsed.path;
        let host_port_path = host_port_path.strip_prefix("//").unwrap_or(&host_port_path);
        let (host_port, path) = match host_port_path.split_once('/') {
            Some((hp, p)) => (hp, format!("/{p}")),
            None => (host_port_path, "/".to_string()),
        };

        let (host, port) = match host_port.rsplit_once(':') {
            Some((h, p)) if p.parse::<u16>().is_ok() => {
                let parsed_port = p.parse::<u16>().unwrap(); // allow-unwrap
                (h.to_string(), parsed_port)
            }
            _ => (
                host_port.to_string(),
                if scheme == "wss" { 443 } else { 80 },
            ),
        };

        let mut cfg = Self {
            scheme,
            host: if host.is_empty() {
                "0.0.0.0".to_string()
            } else {
                host
            },
            port,
            path,
            ..Self::default()
        };

        let params = parsed.params;
        // Validate maxConnections >= 1 (WS-015)
        if let Some(raw) = params.get("maxConnections") {
            let v = raw.parse::<u32>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "maxConnections must be an unsigned integer, got '{raw}'"
                ))
            })?;
            if v == 0 {
                return Err(CamelError::InvalidUri("maxConnections must be >= 1".into()));
            }
            cfg.max_connections = v;
        }
        // Validate maxMessageSize > 0 (WS-019)
        if let Some(raw) = params.get("maxMessageSize") {
            let v = raw.parse::<u32>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "maxMessageSize must be an unsigned integer, got '{raw}'"
                ))
            })?;
            if v == 0 {
                return Err(CamelError::InvalidUri("maxMessageSize must be > 0".into()));
            }
            cfg.max_message_size = v;
        }
        if let Some(raw) = params.get("sendToAll") {
            let v = raw.parse::<bool>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "sendToAll must be a boolean ('true' or 'false'), got '{raw}'"
                ))
            })?;
            cfg.send_to_all = v;
        }
        if let Some(raw) = params.get("heartbeatIntervalMs") {
            let v = raw.parse::<u64>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "heartbeatIntervalMs must be an unsigned integer, got '{raw}'"
                ))
            })?;
            cfg.heartbeat_interval = Duration::from_millis(v);
        }
        if let Some(raw) = params.get("idleTimeoutMs") {
            let v = raw.parse::<u64>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "idleTimeoutMs must be an unsigned integer, got '{raw}'"
                ))
            })?;
            cfg.idle_timeout = Duration::from_millis(v);
        }
        if let Some(raw) = params.get("connectTimeoutMs") {
            let v = raw.parse::<u64>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "connectTimeoutMs must be an unsigned integer, got '{raw}'"
                ))
            })?;
            cfg.connect_timeout = Duration::from_millis(v);
        }
        if let Some(raw) = params.get("responseTimeoutMs") {
            let v = raw.parse::<u64>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "responseTimeoutMs must be an unsigned integer, got '{raw}'"
                ))
            })?;
            cfg.response_timeout = Duration::from_millis(v);
        }
        if let Some(v) = params.get("allowOrigin") {
            if v.is_empty() {
                return Err(CamelError::InvalidUri(
                    "allowOrigin must not be empty when specified".into(),
                ));
            }
            cfg.allow_origin = v.to_string();
        }
        if let Some(v) = params.get("tlsCert") {
            cfg.tls_cert = Some(v.to_string());
        }
        if let Some(v) = params.get("tlsKey") {
            cfg.tls_key = Some(v.to_string());
        }
        if let Some(raw) = params.get("reconnect") {
            cfg.reconnect = raw.parse::<bool>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "reconnect must be a boolean ('true' or 'false'), got '{raw}'"
                ))
            })?;
        }
        if let Some(raw) = params.get("reconnectMaxAttempts") {
            cfg.reconnect_max_attempts = raw.parse::<u32>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "reconnectMaxAttempts must be an unsigned integer, got '{raw}'"
                ))
            })?;
        }
        if let Some(raw) = params.get("reconnectDelayMs") {
            cfg.reconnect_delay_ms = raw.parse::<u64>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "reconnectDelayMs must be an unsigned integer, got '{raw}'"
                ))
            })?;
        }
        if let Some(raw) = params.get("sendTimeoutMs") {
            let v = raw.parse::<u64>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "sendTimeoutMs must be an unsigned integer, got '{raw}'"
                ))
            })?;
            cfg.send_timeout = Duration::from_millis(v);
        }
        if let Some(raw) = params.get("binaryPayload") {
            cfg.binary_payload = raw.parse::<bool>().map_err(|_| {
                CamelError::InvalidUri(format!(
                    "binaryPayload must be a boolean ('true' or 'false'), got '{raw}'"
                ))
            })?;
        }
        if let Some(raw) = params.get("subprotocols") {
            cfg.subprotocols = raw
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
        }

        Ok(cfg)
    }

    pub fn server_config(&self) -> WsServerConfig {
        WsServerConfig {
            inner: self.clone(),
        }
    }

    pub fn client_config(&self) -> WsClientConfig {
        WsClientConfig {
            inner: self.clone(),
        }
    }

    pub fn canonical_host(&self) -> String {
        match self.host.as_str() {
            "0.0.0.0" | "localhost" => "127.0.0.1".to_string(),
            h => h.to_string(),
        }
    }
}

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

    #[test]
    fn test_rejects_zero_max_connections() {
        let cfg = WsConfig {
            max_connections: Some(0),
            ..WsConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_rejects_zero_max_message_size() {
        let cfg = WsConfig {
            max_message_size: Some(0),
            ..WsConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_accepts_valid_config() {
        let cfg = WsConfig::default();
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_accepts_nonzero_max_connections() {
        let cfg = WsConfig {
            max_connections: Some(50),
            ..WsConfig::default()
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_accepts_nonzero_max_message_size() {
        let cfg = WsConfig {
            max_message_size: Some(1024),
            ..WsConfig::default()
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_from_uri_rejects_invalid_send_to_all() {
        let err = WsEndpointConfig::from_uri("ws://localhost:8080?sendToAll=yes").unwrap_err();
        assert!(err.to_string().contains("sendToAll"));
    }

    #[test]
    fn test_from_uri_rejects_invalid_max_connections_numeric() {
        let err = WsEndpointConfig::from_uri("ws://localhost:8080?maxConnections=abc").unwrap_err();
        assert!(err.to_string().contains("maxConnections"));
    }

    #[test]
    fn test_from_uri_rejects_invalid_max_message_size_numeric() {
        let err = WsEndpointConfig::from_uri("ws://localhost:8080?maxMessageSize=abc").unwrap_err();
        assert!(err.to_string().contains("maxMessageSize"));
    }

    #[test]
    fn test_from_uri_rejects_invalid_heartbeat_interval_numeric() {
        let err =
            WsEndpointConfig::from_uri("ws://localhost:8080?heartbeatIntervalMs=abc").unwrap_err();
        assert!(err.to_string().contains("heartbeatIntervalMs"));
    }

    #[test]
    fn test_from_uri_rejects_invalid_idle_timeout_numeric() {
        let err = WsEndpointConfig::from_uri("ws://localhost:8080?idleTimeoutMs=abc").unwrap_err();
        assert!(err.to_string().contains("idleTimeoutMs"));
    }

    #[test]
    fn test_from_uri_rejects_invalid_connect_timeout_numeric() {
        let err =
            WsEndpointConfig::from_uri("ws://localhost:8080?connectTimeoutMs=abc").unwrap_err();
        assert!(err.to_string().contains("connectTimeoutMs"));
    }

    #[test]
    fn test_from_uri_rejects_invalid_response_timeout_numeric() {
        let err =
            WsEndpointConfig::from_uri("ws://localhost:8080?responseTimeoutMs=abc").unwrap_err();
        assert!(err.to_string().contains("responseTimeoutMs"));
    }

    // WS-017: sendTimeoutMs parsing
    #[test]
    fn test_from_uri_parses_send_timeout_ms() {
        let cfg = WsEndpointConfig::from_uri("ws://localhost:8080?sendTimeoutMs=7500").unwrap();
        assert_eq!(cfg.send_timeout, Duration::from_millis(7500));
    }

    #[test]
    fn test_from_uri_rejects_invalid_send_timeout_ms() {
        let err = WsEndpointConfig::from_uri("ws://localhost:8080?sendTimeoutMs=xyz").unwrap_err();
        assert!(err.to_string().contains("sendTimeoutMs"));
    }

    // WS-018: binaryPayload parsing
    #[test]
    fn test_from_uri_parses_binary_payload_true() {
        let cfg = WsEndpointConfig::from_uri("ws://localhost:8080?binaryPayload=true").unwrap();
        assert!(cfg.binary_payload);
    }

    #[test]
    fn test_from_uri_parses_binary_payload_false() {
        let cfg = WsEndpointConfig::from_uri("ws://localhost:8080?binaryPayload=false").unwrap();
        assert!(!cfg.binary_payload);
    }

    #[test]
    fn test_from_uri_rejects_invalid_binary_payload() {
        let err = WsEndpointConfig::from_uri("ws://localhost:8080?binaryPayload=sure").unwrap_err();
        assert!(err.to_string().contains("binaryPayload"));
    }

    // WS-007: subprotocols parsing
    #[test]
    fn test_from_uri_parses_subprotocols() {
        let cfg =
            WsEndpointConfig::from_uri("ws://localhost:8080?subprotocols=json,protobuf").unwrap();
        assert_eq!(cfg.subprotocols, vec!["json", "protobuf"]);
    }

    #[test]
    fn test_from_uri_subprotocols_trims_whitespace() {
        let cfg = WsEndpointConfig::from_uri("ws://localhost:8080?subprotocols=a, b").unwrap();
        assert_eq!(cfg.subprotocols, vec!["a", "b"]);
    }

    #[test]
    fn test_from_uri_subprotocols_empty_when_not_specified() {
        let cfg = WsEndpointConfig::from_uri("ws://localhost:8080").unwrap();
        assert!(cfg.subprotocols.is_empty());
    }
}