viewpoint_core/network/handler/
constructors.rs

1//! Route handler registry constructors.
2
3use std::sync::Arc;
4
5use viewpoint_cdp::CdpConnection;
6
7use super::RouteHandlerRegistry;
8use crate::network::auth::{AuthHandler, HttpCredentials, ProxyCredentials};
9
10impl RouteHandlerRegistry {
11    /// Create a new route handler registry.
12    pub fn new(connection: Arc<CdpConnection>, session_id: String) -> Self {
13        let auth_handler = AuthHandler::new(connection.clone(), session_id.clone());
14        Self {
15            handlers: tokio::sync::RwLock::new(Vec::new()),
16            connection,
17            session_id,
18            fetch_enabled: tokio::sync::RwLock::new(false),
19            auth_handler,
20            auth_enabled: tokio::sync::RwLock::new(false),
21            context_routes: None,
22        }
23    }
24
25    /// Create a new route handler registry with HTTP credentials.
26    pub fn with_credentials(
27        connection: Arc<CdpConnection>,
28        session_id: String,
29        credentials: HttpCredentials,
30    ) -> Self {
31        let auth_handler =
32            AuthHandler::with_credentials(connection.clone(), session_id.clone(), credentials);
33        Self {
34            handlers: tokio::sync::RwLock::new(Vec::new()),
35            connection,
36            session_id,
37            fetch_enabled: tokio::sync::RwLock::new(false),
38            auth_handler,
39            auth_enabled: tokio::sync::RwLock::new(true),
40            context_routes: None,
41        }
42    }
43
44    /// Create a new route handler registry with context-level routes.
45    ///
46    /// If `http_credentials` is provided, they will be set on the auth handler
47    /// for handling HTTP authentication challenges.
48    pub fn with_context_routes(
49        connection: Arc<CdpConnection>,
50        session_id: String,
51        context_routes: Arc<crate::context::routing::ContextRouteRegistry>,
52        http_credentials: Option<HttpCredentials>,
53    ) -> Self {
54        Self::with_context_routes_and_proxy(
55            connection,
56            session_id,
57            context_routes,
58            http_credentials,
59            None,
60        )
61    }
62
63    /// Create a new route handler registry with context-level routes and optional proxy credentials.
64    ///
65    /// If `http_credentials` is provided, they will be set on the auth handler
66    /// for handling HTTP authentication challenges.
67    /// If `proxy_credentials` is provided, they will be used for proxy authentication.
68    pub fn with_context_routes_and_proxy(
69        connection: Arc<CdpConnection>,
70        session_id: String,
71        context_routes: Arc<crate::context::routing::ContextRouteRegistry>,
72        http_credentials: Option<HttpCredentials>,
73        proxy_credentials: Option<ProxyCredentials>,
74    ) -> Self {
75        let auth_handler = AuthHandler::new(connection.clone(), session_id.clone());
76
77        // Set HTTP credentials if provided
78        if let Some(ref creds) = http_credentials {
79            tracing::debug!(
80                username = %creds.username,
81                has_origin = creds.origin.is_some(),
82                "Setting HTTP credentials on auth handler"
83            );
84            auth_handler.set_credentials_sync(creds.clone());
85        }
86
87        // Set proxy credentials if provided
88        if let Some(ref proxy_creds) = proxy_credentials {
89            tracing::debug!(
90                username = %proxy_creds.username,
91                "Setting proxy credentials on auth handler"
92            );
93            auth_handler.set_proxy_credentials_sync(proxy_creds.clone());
94        }
95
96        // Enable auth if any credentials are provided
97        let auth_enabled = http_credentials.is_some() || proxy_credentials.is_some();
98
99        Self {
100            handlers: tokio::sync::RwLock::new(Vec::new()),
101            connection,
102            session_id,
103            fetch_enabled: tokio::sync::RwLock::new(false),
104            auth_handler,
105            auth_enabled: tokio::sync::RwLock::new(auth_enabled),
106            context_routes: Some(context_routes),
107        }
108    }
109}