Skip to main content

mcp_server_core/
service.rs

1//! Services: the MCP servers this crate hosts, each mounted at `/<name>`.
2//!
3//! A [`Service`] is a name and an rmcp handler. [`mcp_service`] serves any rmcp handler over
4//! Streamable HTTP with the protections this crate adds; the default [`Service::router`] uses it.
5
6use crate::combined::DynHandler;
7use crate::config::{CoreConfig, EnvReader};
8use crate::credential::{Credential, CredentialVerifier};
9use crate::policy::Guarded;
10use crate::server::BODY_LIMIT;
11use axum::extract::{Request, State};
12use axum::http::{Method, StatusCode};
13use axum::middleware::Next;
14use axum::response::{IntoResponse, Response};
15use base64::Engine as _;
16use base64::engine::general_purpose::STANDARD;
17use rmcp::ServerHandler;
18use rmcp::model::Icon;
19use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
20use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
21use std::collections::HashMap;
22use std::sync::{Arc, Mutex};
23use std::time::{Duration, Instant};
24use tokio_util::sync::CancellationToken;
25
26/// Tracing targets kept at warn whatever `LOG_LEVEL` says: rmcp's streamable HTTP server logs
27/// MCP session ids (at info and in error text), and its transport workers carry them in a trace
28/// span name, while session ids should stay out of logs and traces.
29pub const QUIET_LOG_TARGETS: &[&str] = &[
30    "rmcp::transport::streamable_http_server",
31    "rmcp::transport::worker",
32];
33
34/// The environment reader every service factory receives.
35pub type ServiceEnv = EnvReader<Box<dyn Fn(&str) -> Option<String>>>;
36
37/// Reads a service's configuration and builds it. Errors go to `env.errors`, so every problem is
38/// reported at once; `None` means the service cannot start.
39pub type ServiceFactory = fn(&mut ServiceEnv) -> Option<Arc<dyn Service>>;
40
41/// What the MCP HTTP layer needs from the core configuration.
42#[derive(Debug, Clone)]
43pub struct HttpSettings {
44    /// External base URL without a trailing slash, e.g. `https://mcp.example.com`.
45    pub public_url: String,
46    /// Host part of `public_url`.
47    pub public_host: String,
48    /// Extra allowed browser origins (`ALLOWED_ORIGINS`), passed to rmcp as given.
49    pub allowed_origins: Vec<String>,
50}
51
52impl HttpSettings {
53    /// The settings from a loaded [`CoreConfig`].
54    pub fn from_core(core: &CoreConfig) -> Self {
55        Self {
56            public_url: core.public_url.clone(),
57            public_host: core.public_host.clone(),
58            allowed_origins: core.allowed_origins.clone(),
59        }
60    }
61}
62
63/// What a service gets when it is mounted.
64#[derive(Debug, Clone)]
65pub struct ServiceContext {
66    /// Host, origin and URL settings for [`mcp_service`].
67    pub http: HttpSettings,
68    /// Cancelled when the server shuts down.
69    pub shutdown: CancellationToken,
70}
71
72/// One MCP service (say `weather`), mounted by the server at `/<name>`.
73///
74/// Only [`name`](Self::name) and [`handler`](Self::handler) are required:
75///
76/// ```
77/// use mcp_server_core::combined::DynHandler;
78/// use mcp_server_core::rmcp::ServerHandler;
79/// use mcp_server_core::service::Service;
80///
81/// #[derive(Clone)]
82/// struct Tools;
83/// impl ServerHandler for Tools {}
84///
85/// struct Weather;
86///
87/// impl Service for Weather {
88///     fn name(&self) -> &'static str {
89///         "weather"
90///     }
91///     fn handler(&self) -> DynHandler {
92///         DynHandler::new(Tools)
93///     }
94/// }
95/// ```
96pub trait Service: Send + Sync + 'static {
97    /// Path segment, OAuth resource suffix and scope: lowercase letters, digits and `-`, never
98    /// `mcp` (the combined endpoint) and never containing `_` (the combined endpoint's prefix
99    /// separator).
100    fn name(&self) -> &'static str;
101    /// This service's MCP handler. It serves the service's tools and prompts on the combined
102    /// `/mcp` endpoint and, through the default [`router`](Self::router), at `/<name>`. Called once per MCP
103    /// session: keep it cheap, cloning `Arc`s.
104    fn handler(&self) -> DynHandler;
105    /// The Streamable HTTP service mounted at `/<name>`. The default serves
106    /// [`handler`](Self::handler), which carries tools and prompts; override it with
107    /// [`mcp_service`] and your concrete handler to serve resources too, or to do work when the
108    /// service is mounted (logging its settings, warming a cache).
109    fn router(self: Arc<Self>, ctx: &ServiceContext) -> axum::Router {
110        mcp_service(ctx, move || Ok(self.handler()))
111    }
112    /// Tool names, logged at startup. Defaults to none.
113    fn tool_names(&self) -> Vec<String> {
114        Vec::new()
115    }
116    /// Tracing targets (usually dependencies) to cap at info whatever `LOG_LEVEL` says, because
117    /// their debug or trace output can leak secrets.
118    fn log_caps(&self) -> &'static [&'static str] {
119        &[]
120    }
121    /// How people sign in to use this service with their own credential, for
122    /// [`SignIn::ServiceCredential`](crate::app::SignIn::ServiceCredential). Exactly one enabled
123    /// service may return one; more than one is a startup error.
124    fn credential_verifier(&self) -> Option<Arc<dyn CredentialVerifier>> {
125        None
126    }
127}
128
129/// `scheme://host:port` for `public_url`, with the scheme's default port made explicit so rmcp
130/// matches exactly that origin (rmcp treats a port-less entry as "any port").
131fn origin_with_port(public_url: &str) -> String {
132    let (scheme, rest) = public_url
133        .split_once("://")
134        .unwrap_or(("https", public_url));
135    let authority = rest.split('/').next().unwrap_or(rest);
136    let has_port = authority
137        .rsplit_once(':')
138        .is_some_and(|(host, port)| !host.ends_with(':') && port.parse::<u16>().is_ok());
139    if has_port {
140        return format!("{scheme}://{authority}");
141    }
142    let port = if scheme == "http" { 80 } else { 443 };
143    format!("{scheme}://{authority}:{port}")
144}
145
146/// Serves an rmcp handler over Streamable HTTP, with:
147///
148/// - host and origin checks (DNS-rebinding protection): `PUBLIC_URL`'s host and loopback names,
149///   `PUBLIC_URL`'s origin and `ALLOWED_ORIGINS`;
150/// - the request body limit and shutdown wiring;
151/// - the read-only rule and audit log of a [`ToolPolicy`](crate::policy::ToolPolicy) attached to
152///   the router as an extension (none, no change);
153/// - MCP sessions bound to the [`Credential`] that opened them, when the request carries one, so on
154///   a server shared by many people nobody can use or end someone else's session; sessions are
155///   capped per credential and in total. Requests without a credential (the owner's password,
156///   the static token) share one owner.
157///
158/// `factory` runs once per MCP session: only clone `Arc`s in it.
159pub fn mcp_service<S, F>(ctx: &ServiceContext, factory: F) -> axum::Router
160where
161    S: ServerHandler + Send + 'static,
162    F: Fn() -> Result<S, std::io::Error> + Send + Sync + 'static,
163{
164    let http = &ctx.http;
165    let mut hosts = vec![http.public_host.clone()];
166    for h in ["localhost", "127.0.0.1", "::1"] {
167        if !hosts.iter().any(|x| x == h) {
168            hosts.push(h.to_string());
169        }
170    }
171    let mut origins = vec![origin_with_port(&http.public_url)];
172    origins.extend(http.allowed_origins.iter().cloned());
173    let config = StreamableHttpServerConfig::default()
174        .with_allowed_hosts(hosts)
175        .with_allowed_origins(origins)
176        .enforce_origin_validation()
177        .with_max_request_body_bytes(BODY_LIMIT)
178        .with_cancellation_token(ctx.shutdown.clone());
179    let manager = Arc::new(LocalSessionManager::default());
180    let service =
181        StreamableHttpService::new(move || factory().map(Guarded::new), manager.clone(), config);
182    let owners = Arc::new(SessionOwners::new(manager, SessionLimits::DEFAULT));
183    axum::Router::new()
184        .fallback_service(service)
185        .layer(axum::middleware::from_fn_with_state(owners, bind_sessions))
186}
187
188const SESSION_HEADER: &str = "mcp-session-id";
189
190/// Bounds on the session-owner table of one mount.
191#[derive(Debug, Clone, Copy)]
192struct SessionLimits {
193    /// Sessions remembered at once. When full, idle entries and those of sessions rmcp has ended
194    /// are swept first; then the owner holding the most (more than the newcomer's count plus
195    /// one) gives up its least recently used session, or a newcomer holding at least as many as
196    /// anyone else gives up its own. Only when shares are about even are new sessions refused.
197    total: usize,
198    /// Sessions one credential may hold; opening another closes its own least recently used one.
199    per_owner: usize,
200    /// An entry unused for this long is treated as gone: a little longer than rmcp's own idle
201    /// timeout, after which rmcp has ended the session anyway.
202    idle: Duration,
203}
204
205impl SessionLimits {
206    const DEFAULT: SessionLimits = SessionLimits {
207        total: 10_000,
208        per_owner: 50,
209        idle: Duration::from_secs(360),
210    };
211}
212
213/// Which credential opened each MCP session on one mount, by fingerprint. A session id is only
214/// usable with the credential that opened it, so on a server shared by many people one person
215/// cannot read or end another's session even if they learn its id, and cannot push other
216/// people's sessions out by opening many of their own.
217struct SessionOwners {
218    manager: Arc<LocalSessionManager>,
219    limits: SessionLimits,
220    table: Mutex<Table>,
221}
222
223/// The fingerprint of the credential that opened a session (none without a guard).
224type Who = Option<[u8; 32]>;
225
226#[derive(Default)]
227struct Table {
228    /// Session id to its owner and last use.
229    sessions: HashMap<String, (Who, Instant)>,
230    /// Sessions held per owner.
231    counts: HashMap<Who, usize>,
232    /// When the full table was last swept for idle and ended sessions.
233    last_sweep: Option<Instant>,
234}
235
236impl Table {
237    fn insert(&mut self, id: String, who: Who, now: Instant) {
238        if self.sessions.insert(id, (who, now)).is_none() {
239            *self.counts.entry(who).or_default() += 1;
240        }
241    }
242
243    fn remove(&mut self, id: &str) -> bool {
244        let Some((who, _)) = self.sessions.remove(id) else {
245            return false;
246        };
247        if let Some(n) = self.counts.get_mut(&who) {
248            *n -= 1;
249            if *n == 0 {
250                self.counts.remove(&who);
251            }
252        }
253        true
254    }
255
256    fn retain(&mut self, mut keep: impl FnMut(&str, &(Who, Instant)) -> bool) {
257        self.sessions.retain(|k, v| keep(k, v));
258        self.counts.clear();
259        for (who, _) in self.sessions.values() {
260            *self.counts.entry(*who).or_default() += 1;
261        }
262    }
263
264    fn count(&self, who: Who) -> usize {
265        self.counts.get(&who).copied().unwrap_or(0)
266    }
267
268    /// The least recently used session of `who`.
269    fn oldest_of(&self, who: Who) -> Option<String> {
270        self.sessions
271            .iter()
272            .filter(|(_, (owner, _))| *owner == who)
273            .min_by_key(|(_, (_, used))| *used)
274            .map(|(k, _)| k.clone())
275    }
276
277    /// The owner, other than `who`, holding the most sessions, and how many.
278    fn largest_other(&self, who: Who) -> Option<(Who, usize)> {
279        self.counts
280            .iter()
281            .filter(|(owner, _)| **owner != who)
282            .max_by_key(|(_, n)| **n)
283            .map(|(owner, n)| (*owner, *n))
284    }
285
286    /// Whether a new session for `who` could get a slot in a full table by taking one from
287    /// whoever holds the most, so that everyone tends toward an equal share.
288    fn fair_share_room(&self, who: Who) -> Option<Who> {
289        let mine = self.count(who);
290        self.largest_other(who)
291            .filter(|(_, n)| *n > mine + 1)
292            .map(|(owner, _)| owner)
293    }
294
295    /// Whose least recently used session makes room for a new one of `who` in a full table:
296    /// whoever holds clearly more, or else `who` itself when it holds any, so a key that already
297    /// has sessions replaces its own instead of being refused or pushing someone else out.
298    fn room_for(&self, who: Who) -> Option<Who> {
299        self.fair_share_room(who)
300            .or_else(|| (who.is_some() && self.count(who) > 0).then_some(who))
301    }
302}
303
304/// Whether a request may use the session it names.
305#[derive(Debug, PartialEq, Eq)]
306enum Ownership {
307    Owner,
308    NotOwner,
309    /// The owner's entry has not been used for a while; ask rmcp whether the session lives.
310    Stale,
311}
312
313/// What [`SessionOwners::admit`] decided about a new session.
314#[derive(Debug, PartialEq, Eq)]
315struct Admission {
316    admitted: bool,
317    /// Sessions dropped to make room, to be closed in rmcp.
318    dropped: Vec<String>,
319}
320
321impl SessionOwners {
322    fn new(manager: Arc<LocalSessionManager>, limits: SessionLimits) -> Self {
323        SessionOwners {
324            manager,
325            limits,
326            table: Mutex::new(Table::default()),
327        }
328    }
329
330    fn table(&self) -> std::sync::MutexGuard<'_, Table> {
331        self.table.lock().unwrap_or_else(|e| e.into_inner())
332    }
333
334    fn owned_by(&self, id: &str, who: Who) -> Ownership {
335        let now = Instant::now();
336        let mut table = self.table();
337        match table.sessions.get_mut(id) {
338            Some((owner, _)) if *owner != who => Ownership::NotOwner,
339            Some((_, used)) if now.duration_since(*used) < self.limits.idle => {
340                *used = now;
341                Ownership::Owner
342            }
343            Some(_) => Ownership::Stale,
344            None => Ownership::NotOwner,
345        }
346    }
347
348    /// Marks session `id` as used now, if `who` owns it.
349    fn touch(&self, id: &str, who: Who) {
350        if let Some((owner, used)) = self.table().sessions.get_mut(id)
351            && *owner == who
352        {
353            *used = Instant::now();
354        }
355    }
356
357    /// Whether a full table is due a sweep, which needs the sessions rmcp still holds.
358    fn wants_live_sessions(&self) -> bool {
359        let table = self.table();
360        table.sessions.len() >= self.limits.total && self.sweep_due(&table)
361    }
362
363    fn sweep_due(&self, table: &Table) -> bool {
364        table.last_sweep.is_none_or(|t| t.elapsed() >= SWEEP_EVERY)
365    }
366
367    /// Whether a new session for `who` would be refused, judged cheaply before rmcp builds it:
368    /// the table is full, was swept moments ago, and no one holds more than their share.
369    fn refuse_early(&self, who: Who) -> bool {
370        let table = self.table();
371        table.sessions.len() >= self.limits.total
372            && !self.sweep_due(&table)
373            && !(who.is_some() && table.count(who) >= self.limits.per_owner)
374            && table.room_for(who).is_none()
375    }
376
377    /// Records a new session `id` opened by `who`. `live` is the set of sessions rmcp still
378    /// holds, when [`Self::wants_live_sessions`] asked for it.
379    fn admit(
380        &self,
381        id: &str,
382        who: Who,
383        live: Option<&std::collections::HashSet<String>>,
384    ) -> Admission {
385        let now = Instant::now();
386        let limits = self.limits;
387        let mut table = self.table();
388        let mut dropped = Vec::new();
389        // One owner never holds more than its cap: its own oldest session makes room.
390        if who.is_some() {
391            while table.count(who) >= limits.per_owner {
392                let Some(oldest) = table.oldest_of(who) else {
393                    break;
394                };
395                table.remove(&oldest);
396                dropped.push(oldest);
397            }
398        }
399        if table.sessions.len() >= limits.total && self.sweep_due(&table) {
400            table.retain(|_, (_, used)| now.duration_since(*used) < limits.idle);
401            if let Some(live) = live {
402                table.retain(|k, _| live.contains(k));
403            }
404            table.last_sweep = Some(now);
405        }
406        if table.sessions.len() >= limits.total {
407            // Take a slot from whoever holds the most, never from someone with fewer than
408            // the newcomer would have.
409            let Some(owner) = table.room_for(who) else {
410                return Admission {
411                    admitted: false,
412                    dropped,
413                };
414            };
415            if let Some(oldest) = table.oldest_of(owner) {
416                table.remove(&oldest);
417                dropped.push(oldest);
418            }
419        }
420        table.insert(id.to_owned(), who, now);
421        Admission {
422            admitted: true,
423            dropped,
424        }
425    }
426
427    fn remove(&self, id: &str) {
428        self.table().remove(id);
429    }
430
431    async fn live_sessions(&self) -> std::collections::HashSet<String> {
432        self.manager
433            .sessions
434            .read()
435            .await
436            .keys()
437            .map(|k| k.to_string())
438            .collect()
439    }
440
441    async fn is_live(&self, id: &str) -> bool {
442        self.manager.sessions.read().await.contains_key(id)
443    }
444
445    async fn close(&self, id: &str) {
446        use rmcp::transport::streamable_http_server::session::SessionManager as _;
447        let _ = self.manager.close_session(&Arc::from(id)).await;
448    }
449}
450
451/// The full table is swept for idle and ended sessions at most this often.
452const SWEEP_EVERY: Duration = Duration::from_secs(1);
453
454fn too_many_sessions() -> Response {
455    let mut busy = (
456        StatusCode::SERVICE_UNAVAILABLE,
457        "This server has too many open sessions. Try again shortly.",
458    )
459        .into_response();
460    busy.headers_mut().insert(
461        axum::http::header::RETRY_AFTER,
462        axum::http::HeaderValue::from_static("30"),
463    );
464    busy
465}
466
467/// Records the credential that opens a session, and answers 404 (as for an unknown session, so
468/// clients start a new one) when a request names a session opened with another credential.
469async fn bind_sessions(
470    State(owners): State<Arc<SessionOwners>>,
471    req: Request,
472    next: Next,
473) -> Response {
474    let who = req
475        .extensions()
476        .get::<Credential>()
477        .map(Credential::fingerprint);
478    let session = req
479        .headers()
480        .get(SESSION_HEADER)
481        .and_then(|v| v.to_str().ok())
482        .map(str::to_owned);
483    let delete = req.method() == Method::DELETE;
484    match &session {
485        Some(id) => match owners.owned_by(id, who) {
486            Ownership::Owner => {}
487            Ownership::NotOwner => {
488                return (StatusCode::NOT_FOUND, "Session not found").into_response();
489            }
490            // Quiet for a while, as during a long tool call: still the owner's if rmcp has
491            // kept the session.
492            Ownership::Stale => {
493                if owners.is_live(id).await {
494                    owners.touch(id, who);
495                } else {
496                    owners.remove(id);
497                    return (StatusCode::NOT_FOUND, "Session not found").into_response();
498                }
499            }
500        },
501        None if owners.refuse_early(who) => return too_many_sessions(),
502        None => {}
503    }
504    let res = next.run(req).await;
505    match session {
506        None => {
507            let Some(id) = res
508                .headers()
509                .get(SESSION_HEADER)
510                .and_then(|v| v.to_str().ok())
511                .map(str::to_owned)
512            else {
513                return res;
514            };
515            // Decided and cleaned up in a task of its own, so the table and rmcp stay in step
516            // even if the client goes away meanwhile.
517            let task = tokio::spawn({
518                let owners = owners.clone();
519                async move {
520                    let live = if owners.wants_live_sessions() {
521                        Some(owners.live_sessions().await)
522                    } else {
523                        None
524                    };
525                    let admission = owners.admit(&id, who, live.as_ref());
526                    for old in &admission.dropped {
527                        owners.close(old).await;
528                    }
529                    if !admission.admitted {
530                        owners.close(&id).await;
531                    }
532                    admission.admitted
533                }
534            });
535            if task.await.unwrap_or(false) {
536                res
537            } else {
538                too_many_sessions()
539            }
540        }
541        Some(id) if delete && res.status().is_success() => {
542            owners.remove(&id);
543            res
544        }
545        Some(_) => res,
546    }
547}
548
549/// `serverInfo` icons as data URIs: a 128x128 PNG first, then an SVG.
550pub fn icons(png: &[u8], svg: &str) -> Vec<Icon> {
551    vec![
552        Icon::new(format!("data:image/png;base64,{}", STANDARD.encode(png)))
553            .with_mime_type("image/png")
554            .with_sizes(vec!["128x128".into()]),
555        Icon::new(format!(
556            "data:image/svg+xml;base64,{}",
557            STANDARD.encode(svg)
558        ))
559        .with_mime_type("image/svg+xml")
560        .with_sizes(vec!["any".into()]),
561    ]
562}
563
564#[cfg(test)]
565mod tests {
566    use super::*;
567
568    #[test]
569    fn icons_are_png_then_svg_data_uris() {
570        let icons = icons(b"png-bytes", "<svg/>");
571        assert_eq!(icons.len(), 2);
572        assert_eq!(
573            icons[0].src,
574            format!("data:image/png;base64,{}", STANDARD.encode(b"png-bytes"))
575        );
576        assert_eq!(icons[0].mime_type.as_deref(), Some("image/png"));
577        assert_eq!(icons[0].sizes, Some(vec!["128x128".to_string()]));
578        assert_eq!(
579            icons[1].src,
580            format!("data:image/svg+xml;base64,{}", STANDARD.encode("<svg/>"))
581        );
582        assert_eq!(icons[1].mime_type.as_deref(), Some("image/svg+xml"));
583        assert_eq!(icons[1].sizes, Some(vec!["any".to_string()]));
584    }
585
586    fn owners(total: usize, per_owner: usize) -> SessionOwners {
587        SessionOwners::new(
588            Arc::new(LocalSessionManager::default()),
589            SessionLimits {
590                total,
591                per_owner,
592                idle: Duration::from_secs(360),
593            },
594        )
595    }
596
597    fn ok(dropped: &[&str]) -> Admission {
598        Admission {
599            admitted: true,
600            dropped: dropped.iter().map(|s| s.to_string()).collect(),
601        }
602    }
603
604    fn live(ids: &[&str]) -> std::collections::HashSet<String> {
605        ids.iter().map(|s| s.to_string()).collect()
606    }
607
608    #[test]
609    fn one_owner_cannot_hold_more_than_its_share() {
610        let o = owners(100, 3);
611        let (alice, bob) = (Some([1; 32]), Some([2; 32]));
612        assert_eq!(o.admit("a1", alice, None), ok(&[]));
613        for i in 0..3 {
614            assert_eq!(o.admit(&format!("b{i}"), bob, None), ok(&[]));
615        }
616        assert_eq!(
617            o.admit("b3", bob, None),
618            ok(&["b0"]),
619            "Bob's own oldest session makes room"
620        );
621        assert_eq!(
622            o.owned_by("a1", alice),
623            Ownership::Owner,
624            "Alice is untouched"
625        );
626        assert_eq!(o.owned_by("b0", bob), Ownership::NotOwner);
627        assert_eq!(o.owned_by("b3", bob), Ownership::Owner);
628        assert_eq!(o.owned_by("b3", alice), Ownership::NotOwner);
629    }
630
631    #[test]
632    fn a_full_table_frees_ended_sessions_first() {
633        let o = owners(3, 10);
634        for (i, who) in [[1; 32], [2; 32], [3; 32]].into_iter().enumerate() {
635            assert_eq!(o.admit(&format!("s{i}"), Some(who), None), ok(&[]));
636        }
637        assert!(o.wants_live_sessions());
638        assert_eq!(
639            o.admit("new", Some([4; 32]), Some(&live(&["s0", "s2"]))),
640            ok(&[]),
641            "the session rmcp has ended makes room"
642        );
643        assert_eq!(o.owned_by("s1", Some([2; 32])), Ownership::NotOwner);
644        assert_eq!(o.owned_by("new", Some([4; 32])), Ownership::Owner);
645    }
646
647    #[test]
648    fn a_full_table_takes_room_from_whoever_holds_most() {
649        let o = owners(4, 3);
650        let (a, b, c) = (Some([1; 32]), Some([2; 32]), Some([3; 32]));
651        for id in ["a1", "a2", "a3"] {
652            assert_eq!(o.admit(id, a, None), ok(&[]));
653        }
654        assert_eq!(o.admit("b1", b, None), ok(&[]));
655        let all = live(&["a1", "a2", "a3", "b1"]);
656        assert_eq!(
657            o.admit("c1", c, Some(&all)),
658            ok(&["a1"]),
659            "the largest holder gives up its oldest"
660        );
661        assert_eq!(
662            o.owned_by("b1", b),
663            Ownership::Owner,
664            "Bob's only session is kept"
665        );
666        // Now a holds 2, b 1, c 1: c's next takes from a, d's from a, and then shares are even.
667        assert_eq!(o.admit("d1", Some([4; 32]), None), ok(&["a2"]));
668        assert_eq!(
669            o.admit("e1", Some([5; 32]), None),
670            Admission {
671                admitted: false,
672                dropped: vec![]
673            },
674            "everyone holds one: nobody is pushed out for a newcomer"
675        );
676        assert!(
677            o.refuse_early(Some([6; 32])),
678            "and the next is refused cheaply"
679        );
680        // An owner at its cap in a full table can still swap one of its own.
681        let o = owners(2, 1);
682        o.admit("a1", a, None);
683        o.admit("b1", b, None);
684        assert!(!o.admit("c1", c, Some(&live(&["a1", "b1"]))).admitted);
685        assert!(o.refuse_early(c));
686        assert!(!o.refuse_early(a));
687        assert_eq!(o.admit("a2", a, None), ok(&["a1"]));
688    }
689
690    #[test]
691    fn the_largest_holder_below_its_cap_replaces_its_own_sessions() {
692        let o = owners(4, 10);
693        let (a, b) = (Some([1; 32]), Some([2; 32]));
694        for id in ["a1", "a2", "a3"] {
695            assert_eq!(o.admit(id, a, None), ok(&[]));
696        }
697        assert_eq!(o.admit("b1", b, None), ok(&[]));
698        let all = live(&["a1", "a2", "a3", "b1"]);
699        assert!(!o.refuse_early(a), "a holds the most, so it is not refused");
700        assert_eq!(
701            o.admit("a4", a, Some(&all)),
702            ok(&["a1"]),
703            "a full table makes room from a's own oldest"
704        );
705        assert_eq!(o.admit("b2", b, None), ok(&["a2"]), "b still takes from a");
706    }
707
708    #[test]
709    fn a_key_with_one_fewer_than_the_largest_replaces_its_own() {
710        let o = owners(3, 10);
711        let (a, b) = (Some([1; 32]), Some([2; 32]));
712        o.admit("a1", a, None);
713        o.admit("a2", a, None);
714        o.admit("b1", b, None);
715        let all = live(&["a1", "a2", "b1"]);
716        assert!(!o.refuse_early(b));
717        assert_eq!(o.admit("b2", b, Some(&all)), ok(&["b1"]));
718        // A newcomer still takes from whoever holds clearly more, then shares are even.
719        assert_eq!(o.admit("c1", Some([3; 32]), None), ok(&["a1"]));
720        assert!(
721            o.refuse_early(Some([4; 32])),
722            "everyone holds one: a newcomer with none is refused"
723        );
724    }
725
726    #[test]
727    fn a_quiet_session_is_stale_not_gone() {
728        let o = SessionOwners::new(
729            Arc::new(LocalSessionManager::default()),
730            SessionLimits {
731                total: 10,
732                per_owner: 10,
733                idle: Duration::ZERO,
734            },
735        );
736        let who = Some([1; 32]);
737        o.admit("s", who, None);
738        assert_eq!(o.owned_by("s", who), Ownership::Stale);
739        assert_eq!(o.owned_by("s", Some([2; 32])), Ownership::NotOwner);
740    }
741
742    #[test]
743    fn origin_gets_explicit_default_port() {
744        assert_eq!(
745            origin_with_port("https://mail.example.com"),
746            "https://mail.example.com:443"
747        );
748        assert_eq!(
749            origin_with_port("http://localhost:8443"),
750            "http://localhost:8443"
751        );
752        assert_eq!(origin_with_port("http://localhost"), "http://localhost:80");
753        assert_eq!(origin_with_port("https://[::1]"), "https://[::1]:443");
754        assert_eq!(origin_with_port("https://[::1]:9000"), "https://[::1]:9000");
755    }
756}