Skip to main content

camel_component_http/
config.rs

1use serde::Deserialize;
2
3#[derive(Debug, Clone, PartialEq, Deserialize)]
4pub struct HttpConfig {
5    #[serde(default = "default_connect_timeout_ms")]
6    pub connect_timeout_ms: u64,
7    #[serde(default = "default_pool_max_idle_per_host")]
8    pub pool_max_idle_per_host: usize,
9    #[serde(default = "default_pool_idle_timeout_ms")]
10    pub pool_idle_timeout_ms: u64,
11    #[serde(default)]
12    pub follow_redirects: bool,
13    #[serde(default = "default_response_timeout_ms")]
14    pub response_timeout_ms: u64,
15    #[serde(default = "default_max_body_size")]
16    pub max_body_size: usize,
17    #[serde(default = "default_max_request_body")]
18    pub max_request_body: usize,
19    #[serde(default)]
20    pub allow_private_ips: bool,
21    #[serde(default)]
22    pub blocked_hosts: Vec<String>,
23}
24
25fn default_connect_timeout_ms() -> u64 {
26    5_000
27}
28
29fn default_pool_max_idle_per_host() -> usize {
30    100
31}
32
33fn default_pool_idle_timeout_ms() -> u64 {
34    90_000
35}
36
37fn default_response_timeout_ms() -> u64 {
38    30_000
39}
40
41fn default_max_body_size() -> usize {
42    10_485_760
43}
44
45fn default_max_request_body() -> usize {
46    2_097_152
47}
48
49impl Default for HttpConfig {
50    fn default() -> Self {
51        Self {
52            connect_timeout_ms: default_connect_timeout_ms(),
53            pool_max_idle_per_host: default_pool_max_idle_per_host(),
54            pool_idle_timeout_ms: default_pool_idle_timeout_ms(),
55            follow_redirects: false,
56            response_timeout_ms: default_response_timeout_ms(),
57            max_body_size: default_max_body_size(),
58            max_request_body: default_max_request_body(),
59            allow_private_ips: false,
60            blocked_hosts: Vec::new(),
61        }
62    }
63}
64
65impl HttpConfig {
66    pub fn with_connect_timeout_ms(mut self, ms: u64) -> Self {
67        self.connect_timeout_ms = ms;
68        self
69    }
70    pub fn with_pool_max_idle_per_host(mut self, n: usize) -> Self {
71        self.pool_max_idle_per_host = n;
72        self
73    }
74    pub fn with_pool_idle_timeout_ms(mut self, ms: u64) -> Self {
75        self.pool_idle_timeout_ms = ms;
76        self
77    }
78    pub fn with_follow_redirects(mut self, follow: bool) -> Self {
79        self.follow_redirects = follow;
80        self
81    }
82    pub fn with_response_timeout_ms(mut self, ms: u64) -> Self {
83        self.response_timeout_ms = ms;
84        self
85    }
86    pub fn with_max_body_size(mut self, n: usize) -> Self {
87        self.max_body_size = n;
88        self
89    }
90    pub fn with_max_request_body(mut self, n: usize) -> Self {
91        self.max_request_body = n;
92        self
93    }
94    pub fn with_allow_private_ips(mut self, allow: bool) -> Self {
95        self.allow_private_ips = allow;
96        self
97    }
98    pub fn with_blocked_hosts(mut self, hosts: Vec<String>) -> Self {
99        self.blocked_hosts = hosts;
100        self
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_http_config_defaults() {
110        let cfg = HttpConfig::default();
111        assert_eq!(cfg.connect_timeout_ms, 5_000);
112        assert_eq!(cfg.pool_max_idle_per_host, 100);
113        assert_eq!(cfg.pool_idle_timeout_ms, 90_000);
114        assert!(!cfg.follow_redirects);
115        assert_eq!(cfg.response_timeout_ms, 30_000);
116        assert_eq!(cfg.max_body_size, 10_485_760);
117        assert_eq!(cfg.max_request_body, 2_097_152);
118        assert!(!cfg.allow_private_ips);
119        assert!(cfg.blocked_hosts.is_empty());
120    }
121
122    #[test]
123    fn test_http_config_builder() {
124        let cfg = HttpConfig::default()
125            .with_connect_timeout_ms(1_000)
126            .with_pool_max_idle_per_host(50)
127            .with_follow_redirects(true)
128            .with_allow_private_ips(true)
129            .with_blocked_hosts(vec!["evil.com".to_string()]);
130        assert_eq!(cfg.connect_timeout_ms, 1_000);
131        assert_eq!(cfg.pool_max_idle_per_host, 50);
132        assert!(cfg.follow_redirects);
133        assert!(cfg.allow_private_ips);
134        assert_eq!(cfg.blocked_hosts, vec!["evil.com".to_string()]);
135        assert_eq!(cfg.response_timeout_ms, 30_000);
136    }
137}