Skip to main content

active_call/call/
refer_headers.rs

1//! Pluggable transformation of the headers an incoming REFER carries onto the
2//! transfer INVITE.
3//!
4//! By default every custom header received on the REFER (`Header::Other` plus
5//! the typed `User-to-User`, with `X-Referred-*` excluded) is forwarded
6//! verbatim onto the transfer INVITE. Embeddings that need different
7//! behaviour — e.g. resolving carrier routing data through an HTTP service
8//! before dialing — install a [`ReferHeaderResolver`] on the
9//! [`AppStateBuilder`](crate::app::AppStateBuilder); with no resolver
10//! configured the pass-through behaviour is unchanged.
11
12use std::collections::HashMap;
13
14use async_trait::async_trait;
15
16/// Input given to a [`ReferHeaderResolver`].
17#[derive(Debug, Clone, Default)]
18pub struct ReferHeaderContext {
19    /// Session id of the call that received the REFER.
20    pub session_id: String,
21    /// Target parsed from the REFER's `Refer-To` header.
22    pub refer_target: String,
23    /// Custom headers carried by the REFER (`Header::Other` + the typed
24    /// `User-to-User`, `X-Referred-*` already excluded). With no resolver
25    /// installed these are forwarded onto the transfer INVITE unchanged.
26    pub headers: HashMap<String, String>,
27}
28
29/// Decides which headers the transfer INVITE carries.
30///
31/// The returned map *replaces* [`ReferHeaderContext::headers`]: implementations
32/// that only rewrite a subset must copy the untouched headers through
33/// themselves. Returning an empty map strips all extra headers from the
34/// transfer INVITE. Errors abort the rewrite and fall back to the
35/// pass-through set (the INVITE still goes out with the original headers).
36#[async_trait]
37pub trait ReferHeaderResolver: Send + Sync {
38    async fn resolve(
39        &self,
40        context: &ReferHeaderContext,
41    ) -> anyhow::Result<HashMap<String, String>>;
42}
43
44/// Default behaviour: pass every REFER header through unchanged.
45#[derive(Debug, Clone, Copy, Default)]
46pub struct PassThroughReferHeaders;
47
48#[async_trait]
49impl ReferHeaderResolver for PassThroughReferHeaders {
50    async fn resolve(
51        &self,
52        context: &ReferHeaderContext,
53    ) -> anyhow::Result<HashMap<String, String>> {
54        Ok(context.headers.clone())
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    /// A resolver that swaps User-to-User for custom routing headers.
63    struct SwapResolver;
64
65    #[async_trait]
66    impl ReferHeaderResolver for SwapResolver {
67        async fn resolve(
68            &self,
69            context: &ReferHeaderContext,
70        ) -> anyhow::Result<HashMap<String, String>> {
71            let mut out = HashMap::new();
72            for (k, v) in &context.headers {
73                if k.eq_ignore_ascii_case("User-to-User") {
74                    out.insert("X-queue".to_string(), "2000".to_string());
75                } else {
76                    out.insert(k.clone(), v.clone());
77                }
78            }
79            Ok(out)
80        }
81    }
82
83    #[tokio::test]
84    async fn pass_through_returns_headers_unchanged() {
85        let mut headers = HashMap::new();
86        headers.insert("User-to-User".to_string(), "abc".to_string());
87        let ctx = ReferHeaderContext {
88            session_id: "s.1".into(),
89            refer_target: "sip:100@pbx".into(),
90            headers: headers.clone(),
91        };
92        let resolved = PassThroughReferHeaders.resolve(&ctx).await.unwrap();
93        assert_eq!(resolved, headers);
94    }
95
96    #[tokio::test]
97    async fn custom_resolver_can_replace_headers() {
98        let mut headers = HashMap::new();
99        headers.insert("User-to-User".to_string(), "abc".to_string());
100        headers.insert("X-ACC-Bizdata".to_string(), "keep".to_string());
101        let ctx = ReferHeaderContext {
102            session_id: "s.1".into(),
103            refer_target: "sip:100@pbx".into(),
104            headers,
105        };
106        let resolved = SwapResolver.resolve(&ctx).await.unwrap();
107        assert_eq!(resolved.get("X-queue").map(String::as_str), Some("2000"));
108        assert_eq!(resolved.get("X-ACC-Bizdata").map(String::as_str), Some("keep"));
109        assert!(resolved.get("User-to-User").is_none());
110    }
111}