http-handle 0.0.5

A fast and lightweight Rust library for handling HTTP requests and responses.
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
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2023 - 2026 HTTP Handle

//! Multi-tenant configuration isolation and secret-provider helpers.

use crate::error::ServerError;
use std::collections::HashMap;
use std::sync::RwLock;

/// Tenant identifier.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::TenantId;
/// let t = TenantId("acme".into());
/// assert_eq!(t.0, "acme");
/// ```
///
/// # Panics
///
/// This type does not panic.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TenantId(pub String);

/// Per-tenant configuration document.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::TenantConfig;
/// let cfg = TenantConfig::default();
/// assert!(cfg.settings.is_empty());
/// ```
///
/// # Panics
///
/// This type does not panic.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TenantConfig {
    /// Arbitrary tenant settings.
    pub settings: HashMap<String, String>,
}

/// Thread-safe tenant config store with strict tenant keying.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::TenantConfigStore;
/// let _store = TenantConfigStore::default();
/// assert_eq!(1, 1);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[derive(Debug, Default)]
pub struct TenantConfigStore {
    data: RwLock<HashMap<TenantId, TenantConfig>>,
}

impl TenantConfigStore {
    /// Writes tenant config snapshot.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::tenant_isolation::{TenantConfig, TenantConfigStore, TenantId};
    /// let store = TenantConfigStore::default();
    /// let _ = store.set_config(TenantId("acme".into()), TenantConfig::default());
    /// assert_eq!(1, 1);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error when the underlying lock is poisoned.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn set_config(
        &self,
        tenant: TenantId,
        config: TenantConfig,
    ) -> Result<(), ServerError> {
        let mut guard = self.data.write().map_err(|_| {
            ServerError::Custom("tenant store poisoned".into())
        })?;
        let _ = guard.insert(tenant, config);
        Ok(())
    }

    /// Returns a cloned tenant config snapshot.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::tenant_isolation::{TenantConfigStore, TenantId};
    /// let store = TenantConfigStore::default();
    /// let _ = store.get_config(&TenantId("acme".into()));
    /// assert_eq!(1, 1);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error when the underlying lock is poisoned.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn get_config(
        &self,
        tenant: &TenantId,
    ) -> Result<Option<TenantConfig>, ServerError> {
        let guard = self.data.read().map_err(|_| {
            ServerError::Custom("tenant store poisoned".into())
        })?;
        Ok(guard.get(tenant).cloned())
    }
}

/// External secret provider contract for tenant-scoped lookup.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::SecretProvider;
/// # let _ = std::any::TypeId::of::<&dyn SecretProvider>();
/// assert_eq!(1, 1);
/// ```
///
/// # Panics
///
/// Trait usage does not panic by itself.
pub trait SecretProvider: Send + Sync + std::fmt::Debug {
    /// Fetches secret for tenant and key.
    fn get_secret(
        &self,
        tenant: &TenantId,
        key: &str,
    ) -> Result<Option<String>, ServerError>;
}

/// Environment-backed secret provider using strict tenant-key namespace.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::EnvSecretProvider;
/// let _provider = EnvSecretProvider::new("HTTP_HANDLE_SECRET");
/// assert_eq!(1, 1);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[derive(Clone, Debug)]
pub struct EnvSecretProvider {
    prefix: String,
}

impl EnvSecretProvider {
    /// Creates provider with prefix used in env keys.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::tenant_isolation::EnvSecretProvider;
    /// let _provider = EnvSecretProvider::new("HTTP_HANDLE_SECRET");
    /// assert_eq!(1, 1);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
        }
    }

    fn env_key(&self, tenant: &TenantId, key: &str) -> String {
        let tenant_norm =
            tenant.0.replace('-', "_").to_ascii_uppercase();
        let key_norm = key.replace('-', "_").to_ascii_uppercase();
        format!("{}_{}_{}", self.prefix, tenant_norm, key_norm)
    }
}

impl SecretProvider for EnvSecretProvider {
    fn get_secret(
        &self,
        tenant: &TenantId,
        key: &str,
    ) -> Result<Option<String>, ServerError> {
        let env_key = self.env_key(tenant, key);
        Ok(std::env::var(env_key).ok())
    }
}

/// In-memory secret provider useful for local development/testing.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::StaticSecretProvider;
/// let _provider = StaticSecretProvider::default();
/// assert_eq!(1, 1);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[derive(Clone, Debug, Default)]
pub struct StaticSecretProvider {
    data: HashMap<(TenantId, String), String>,
}

impl StaticSecretProvider {
    /// Adds a tenant-scoped secret value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::tenant_isolation::{StaticSecretProvider, TenantId};
    /// let _provider = StaticSecretProvider::default().with_secret(TenantId("acme".into()), "token", "abc");
    /// assert_eq!(1, 1);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn with_secret(
        mut self,
        tenant: TenantId,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        let _ = self.data.insert((tenant, key.into()), value.into());
        self
    }
}

impl SecretProvider for StaticSecretProvider {
    fn get_secret(
        &self,
        tenant: &TenantId,
        key: &str,
    ) -> Result<Option<String>, ServerError> {
        Ok(self.data.get(&(tenant.clone(), key.to_string())).cloned())
    }
}

/// Tenant-scoped secret accessor.
///
/// # Examples
///
/// ```rust
/// use http_handle::tenant_isolation::{StaticSecretProvider, TenantScopedSecrets};
/// let provider = StaticSecretProvider::default();
/// let _secrets = TenantScopedSecrets::new(provider);
/// assert_eq!(1, 1);
/// ```
///
/// # Panics
///
/// This type does not panic.
#[derive(Debug)]
pub struct TenantScopedSecrets<P: SecretProvider> {
    provider: P,
}

impl<P: SecretProvider> TenantScopedSecrets<P> {
    /// Creates a tenant-scoped secret accessor.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::tenant_isolation::{StaticSecretProvider, TenantScopedSecrets};
    /// let _s = TenantScopedSecrets::new(StaticSecretProvider::default());
    /// assert_eq!(1, 1);
    /// ```
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn new(provider: P) -> Self {
        Self { provider }
    }

    /// Reads tenant secret.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_handle::tenant_isolation::{StaticSecretProvider, TenantId, TenantScopedSecrets};
    /// let s = TenantScopedSecrets::new(StaticSecretProvider::default());
    /// let _ = s.read(&TenantId("acme".into()), "token");
    /// assert_eq!(1, 1);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns provider-specific errors for secret lookup failures.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn read(
        &self,
        tenant: &TenantId,
        key: &str,
    ) -> Result<Option<String>, ServerError> {
        self.provider.get_secret(tenant, key)
    }
}

#[cfg(test)]
// Test-only env-var mutations (`std::env::set_var` / `remove_var`) need
// `unsafe` under Rust 2024. Each call site below is a paired write +
// cleanup inside a single test scope and is documented at the use site.
#[allow(unsafe_code)]
mod tests {
    use super::*;

    #[test]
    fn tenant_store_is_isolated() {
        let store = TenantConfigStore::default();
        let tenant_a = TenantId("alpha".into());
        let tenant_b = TenantId("beta".into());
        store
            .set_config(
                tenant_a.clone(),
                TenantConfig {
                    settings: [("mode".into(), "strict".into())]
                        .into_iter()
                        .collect(),
                },
            )
            .expect("set");
        assert_eq!(
            store
                .get_config(&tenant_a)
                .expect("get")
                .expect("cfg")
                .settings
                .get("mode"),
            Some(&"strict".to_string())
        );
        assert!(store.get_config(&tenant_b).expect("get").is_none());
    }

    #[test]
    fn static_secret_provider_is_tenant_scoped() {
        let provider = StaticSecretProvider::default()
            .with_secret(TenantId("alpha".into()), "db_password", "a1")
            .with_secret(TenantId("beta".into()), "db_password", "b1");
        let scoped = TenantScopedSecrets::new(provider);
        assert_eq!(
            scoped
                .read(&TenantId("alpha".into()), "db_password")
                .expect("read"),
            Some("a1".to_string())
        );
        assert_eq!(
            scoped
                .read(&TenantId("beta".into()), "db_password")
                .expect("read"),
            Some("b1".to_string())
        );
    }

    #[test]
    fn env_secret_provider_namespaces_keys() {
        let provider = EnvSecretProvider::new("HTTP_HANDLE_SECRET");
        let tenant = TenantId("alpha-team".into());
        let key = "api_token";
        let env_key = "HTTP_HANDLE_SECRET_ALPHA_TEAM_API_TOKEN";
        let value = "secret-value";
        // Safety: this test writes and removes a single process env var in a
        // short scope and does not spawn threads that concurrently mutate env.
        unsafe { std::env::set_var(env_key, value) };
        let got = provider.get_secret(&tenant, key).expect("read");
        assert_eq!(got, Some(value.to_string()));
        // Safety: paired cleanup for the key set above in the same test scope.
        unsafe { std::env::remove_var(env_key) };
    }

    #[test]
    fn env_secret_provider_returns_none_when_missing() {
        let provider = EnvSecretProvider::new("HTTP_HANDLE_SECRET");
        let got = provider
            .get_secret(&TenantId("missing".into()), "api_token")
            .expect("read");
        assert!(got.is_none());
    }

    #[test]
    fn tenant_store_write_poison_maps_to_error() {
        let store = TenantConfigStore::default();
        let _ = std::panic::catch_unwind(|| {
            let _guard = store.data.write().expect("lock");
            panic!("poison");
        });
        let err = store
            .set_config(TenantId("t1".into()), TenantConfig::default())
            .expect_err("must fail");
        assert!(err.to_string().contains("poisoned"));
    }

    #[test]
    fn tenant_store_read_poison_maps_to_error() {
        let store = TenantConfigStore::default();
        let _ = std::panic::catch_unwind(|| {
            let _guard = store.data.write().expect("lock");
            panic!("poison");
        });
        let err = store
            .get_config(&TenantId("t1".into()))
            .expect_err("must fail");
        assert!(err.to_string().contains("poisoned"));
    }
}