use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};
use socketio_next::{
AckSender, Client, ClientBuilder, ConnectionState, Event, ListenerId, Payload,
};
use tauri::ipc::Channel;
use tokio::sync::watch;
use crate::models::{Access, ClientInfo, Dynamic, Message, State};
use crate::{Error, Result};
pub const ACK_TTL: Duration = Duration::from_secs(120);
pub const MAX_PENDING_ACKS_PER_WEBVIEW: usize = 256;
pub const MAX_SUBSCRIPTIONS_PER_WEBVIEW: usize = 64;
const SETTLE: Duration = Duration::from_secs(2);
pub type SubscriptionId = u64;
#[derive(Clone, Default)]
pub struct SocketIo {
inner: Arc<Inner>,
}
#[derive(Default)]
struct Inner {
clients: Mutex<HashMap<String, Entry>>,
reserved: Mutex<HashSet<String>>,
pending: Mutex<Pending>,
generations: Mutex<HashMap<String, u64>>,
allowed: Vec<url::Url>,
next_sub: AtomicU64,
}
struct Entry {
client: Client,
access: Access,
hub: Arc<Hub>,
listeners: Vec<ListenerId>,
dynamic: Option<Arc<Mutex<Dynamic>>>,
}
struct Hub {
state: Mutex<HubState>,
forwarded: watch::Sender<u64>,
}
struct HubState {
subs: HashMap<SubscriptionId, Sub>,
last: Option<State>,
acks: HashMap<u64, PendingAck>,
next_ack: u64,
offer_acks: bool,
closed: bool,
}
struct Sub {
channel: Channel<Message>,
events: Option<HashSet<String>>,
webview: Option<String>,
}
struct PendingAck {
sender: AckSender,
at: Instant,
webviews: Vec<String>,
}
pub(crate) struct SubscribeArgs {
pub events: Option<Vec<String>>,
pub channel: Channel<Message>,
pub webview: Option<String>,
}
impl SubscribeArgs {
fn into_sub(self) -> Sub {
Sub {
channel: self.channel,
events: self.events.map(|e| e.into_iter().collect()),
webview: self.webview,
}
}
}
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
impl fmt::Debug for SocketIo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SocketIo")
.field("clients", &self.ids())
.field("reserved", &lock(&self.inner.reserved).len())
.field("allowed_urls", &self.inner.allowed.len())
.finish()
}
}
impl SocketIo {
pub(crate) fn with_allowed(allowed: Vec<url::Url>) -> Self {
SocketIo {
inner: Arc::new(Inner {
allowed,
..Inner::default()
}),
}
}
pub fn reserve(&self, id: impl Into<String>) {
lock(&self.inner.reserved).insert(id.into());
}
pub async fn connect(&self, id: impl Into<String>, builder: ClientBuilder) -> Result<Client> {
Ok(self.connect_with(id.into(), builder, None, None).await?.0)
}
pub(crate) async fn connect_from_webview(
&self,
id: String,
url: &str,
engine_path: Option<&str>,
builder: ClientBuilder,
dynamic: Arc<Mutex<Dynamic>>,
sub: SubscribeArgs,
) -> Result<(SubscriptionId, State)> {
if lock(&self.inner.reserved).contains(&id) {
return Err(Error::Reserved(id));
}
if !self.url_allowed(url, engine_path) {
return Err(Error::UrlNotAllowed);
}
let (_, sub) = self
.connect_with(id, builder, Some(sub), Some(dynamic))
.await?;
Ok(sub.expect("a subscription was given"))
}
async fn connect_with(
&self,
id: String,
builder: ClientBuilder,
sub: Option<SubscribeArgs>,
dynamic: Option<Arc<Mutex<Dynamic>>>,
) -> Result<(Client, Option<(SubscriptionId, State)>)> {
let webview = sub.as_ref().and_then(|s| s.webview.clone());
let pending = {
let clients = lock(&self.inner.clients);
let mut pending = lock(&self.inner.pending);
if clients.contains_key(&id) || pending.ids.contains(&id) {
return Err(Error::AlreadyExists(id));
}
if let Some(label) = &webview {
let held = pending.slots.get(label).copied().unwrap_or_default();
check_cap(&clients, label, held)?;
*pending.slots.entry(label.clone()).or_default() += 1;
}
pending.ids.insert(id.clone());
PendingGuard {
inner: &self.inner,
id: id.clone(),
webview: webview.clone(),
}
};
let generation = webview.as_deref().map(|l| self.generation(l));
let hub = Hub::new(true);
let sub_id = sub.map(|args| {
let sub_id = self.next_sub();
lock(&hub.state).subs.insert(sub_id, args.into_sub());
sub_id
});
let (on_event, on_state) = (Arc::clone(&hub), Arc::clone(&hub));
let client = builder
.on_any(move |event: Event, _: Client| {
on_event.event(event);
std::future::ready(())
})
.on_state_change(move |state: ConnectionState, _: Client| {
on_state.state(&state);
std::future::ready(())
})
.connect()
.await?;
#[cfg(test)]
let id_for_test = id.clone();
{
let mut clients = lock(&self.inner.clients);
if clients.contains_key(&id) {
return Err(Error::AlreadyExists(id));
}
clients.insert(
id,
Entry {
client: client.clone(),
access: Access::Full,
hub: Arc::clone(&hub),
listeners: Vec::new(),
dynamic,
},
);
drop(pending);
}
#[cfg(test)]
tests::after_insert(&id_for_test).await;
let Some(sub_id) = sub_id else {
return Ok((client, None));
};
if let (Some(label), Some(before)) = (&webview, generation) {
if self.generation(label) != before {
hub.remove_sub(sub_id, None);
}
}
hub.settle(&client).await;
let state = hub.snapshot_to(sub_id, &client);
Ok((client, Some((sub_id, state))))
}
pub fn insert(&self, id: impl Into<String>, client: Client, access: Access) -> Result<()> {
let id = id.into();
let mut clients = lock(&self.inner.clients);
if clients.contains_key(&id) {
return Err(Error::AlreadyExists(id));
}
let hub = Hub::new(access == Access::Full);
let (on_event, on_state) = (Arc::clone(&hub), Arc::clone(&hub));
let listeners = vec![
client.on_any(move |event: Event, _: Client| {
on_event.event(event);
std::future::ready(())
}),
client.on_state_change(move |state: ConnectionState, _: Client| {
on_state.state(&state);
std::future::ready(())
}),
];
clients.insert(
id,
Entry {
client,
access,
hub,
listeners,
dynamic: None,
},
);
Ok(())
}
pub fn get(&self, id: &str) -> Option<Client> {
lock(&self.inner.clients).get(id).map(|e| e.client.clone())
}
pub fn access(&self, id: &str) -> Option<Access> {
lock(&self.inner.clients).get(id).map(|e| e.access)
}
pub fn ids(&self) -> Vec<String> {
let mut ids: Vec<_> = lock(&self.inner.clients).keys().cloned().collect();
ids.sort();
ids
}
pub fn remove(&self, id: &str) -> Option<Client> {
let entry = lock(&self.inner.clients).remove(id)?;
Some(entry.release())
}
pub fn subscribe(
&self,
id: &str,
events: Option<Vec<String>>,
channel: Channel<Message>,
) -> Result<SubscriptionId> {
Ok(self
.subscribe_with(
id,
SubscribeArgs {
events,
channel,
webview: None,
},
)?
.0)
}
pub(crate) fn subscribe_with(
&self,
id: &str,
args: SubscribeArgs,
) -> Result<(SubscriptionId, State)> {
let clients = lock(&self.inner.clients);
if let Some(label) = &args.webview {
let held = lock(&self.inner.pending)
.slots
.get(label)
.copied()
.unwrap_or_default();
check_cap(&clients, label, held)?;
}
let entry = clients
.get(id)
.ok_or_else(|| Error::UnknownClient(id.to_owned()))?;
let sub_id = self.next_sub();
let client = &entry.client;
let state = entry
.hub
.attach(sub_id, args.into_sub(), || State::from(&client.state()));
Ok((sub_id, state))
}
pub fn unsubscribe(&self, id: &str, subscription: SubscriptionId) -> bool {
self.unsubscribe_from(id, subscription, None)
}
pub(crate) fn unsubscribe_from(
&self,
id: &str,
subscription: SubscriptionId,
webview: Option<&str>,
) -> bool {
lock(&self.inner.clients)
.get(id)
.is_some_and(|e| e.hub.remove_sub(subscription, webview))
}
pub(crate) fn drop_webview(&self, label: &str) {
*lock(&self.inner.generations)
.entry(label.to_owned())
.or_default() += 1;
for entry in lock(&self.inner.clients).values() {
entry.hub.drop_webview(label);
}
}
fn generation(&self, label: &str) -> u64 {
lock(&self.inner.generations)
.get(label)
.copied()
.unwrap_or_default()
}
fn next_sub(&self) -> SubscriptionId {
self.inner.next_sub.fetch_add(1, Ordering::Relaxed) + 1
}
pub(crate) fn url_allowed(&self, url: &str, engine_path: Option<&str>) -> bool {
if self.inner.allowed.is_empty() {
return true;
}
if let Some(path) = engine_path {
let lower = path.to_ascii_lowercase();
if path.contains("..")
|| lower.contains("%2e")
|| lower.contains("%25")
|| lower.contains("%u")
|| path.contains('\\')
|| path.chars().any(char::is_control)
{
return false;
}
}
let mut builder = ClientBuilder::new(url);
if let Some(path) = engine_path {
builder = builder.path(path);
}
let Ok(engine) = builder.engine_url() else {
return false;
};
let path = engine.path();
self.inner.allowed.iter().any(|p| {
let base = p.path().trim_end_matches('/');
p.scheme() == engine.scheme()
&& p.host_str() == engine.host_str()
&& p.port_or_known_default() == engine.port_or_known_default()
&& (base.is_empty() || path.strip_prefix(base).is_some_and(|r| r.starts_with('/')))
})
}
pub(crate) fn full(&self, id: &str) -> Result<Client> {
match lock(&self.inner.clients).get(id) {
None => Err(Error::UnknownClient(id.to_owned())),
Some(e) if e.access == Access::ReadOnly => Err(Error::AccessDenied(id.to_owned())),
Some(e) => Ok(e.client.clone()),
}
}
pub(crate) fn any(&self, id: &str) -> Result<Client> {
self.get(id)
.ok_or_else(|| Error::UnknownClient(id.to_owned()))
}
pub(crate) async fn disconnect(&self, id: &str) -> Result<()> {
let entry = {
let mut clients = lock(&self.inner.clients);
match clients.get(id).map(|e| e.access) {
None => return Err(Error::UnknownClient(id.to_owned())),
Some(Access::ReadOnly) => return Err(Error::AccessDenied(id.to_owned())),
Some(Access::Full) => clients.remove(id).expect("checked under the lock"),
}
};
entry.hub.stop_acks();
let result = entry.client.disconnect().await;
entry.release();
Ok(result?)
}
pub(crate) fn configure(&self, id: &str, f: impl FnOnce(&mut Dynamic)) -> Result<()> {
let clients = lock(&self.inner.clients);
let entry = clients
.get(id)
.ok_or_else(|| Error::UnknownClient(id.to_owned()))?;
if entry.access == Access::ReadOnly {
return Err(Error::AccessDenied(id.to_owned()));
}
let dynamic = entry
.dynamic
.as_ref()
.ok_or_else(|| Error::NotConfigurable(id.to_owned()))?;
f(&mut crate::models::lock(dynamic));
Ok(())
}
pub(crate) fn webview_client(&self, id: &str) -> Result<Client> {
let clients = lock(&self.inner.clients);
let entry = clients
.get(id)
.ok_or_else(|| Error::UnknownClient(id.to_owned()))?;
if entry.access == Access::ReadOnly {
return Err(Error::AccessDenied(id.to_owned()));
}
if entry.dynamic.is_none() {
return Err(Error::NotConfigurable(id.to_owned()));
}
Ok(entry.client.clone())
}
pub(crate) fn ack(&self, id: &str, ack_id: u64, webview: &str, payload: Payload) -> Result<()> {
let hub = {
let clients = lock(&self.inner.clients);
match clients.get(id) {
None => return Err(Error::UnknownClient(id.to_owned())),
Some(e) if e.access == Access::ReadOnly => {
return Err(Error::AccessDenied(id.to_owned()));
}
Some(e) => Arc::clone(&e.hub),
}
};
let sender = hub
.take_ack(ack_id, webview)
.ok_or(Error::UnknownAck(ack_id))?;
Ok(sender.send(payload)?)
}
pub(crate) fn list(&self) -> Vec<ClientInfo> {
let clients = lock(&self.inner.clients);
let mut out: Vec<_> = clients
.iter()
.map(|(id, e)| ClientInfo {
id: id.clone(),
access: e.access,
namespace: e.client.namespace().to_owned(),
state: State::from(&e.client.state()),
})
.collect();
out.sort_by(|a, b| a.id.cmp(&b.id));
out
}
#[cfg(test)]
fn hub(&self, id: &str) -> Arc<Hub> {
Arc::clone(&lock(&self.inner.clients)[id].hub)
}
}
#[derive(Default)]
struct Pending {
ids: HashSet<String>,
slots: HashMap<String, usize>,
}
struct PendingGuard<'a> {
inner: &'a Inner,
id: String,
webview: Option<String>,
}
impl Drop for PendingGuard<'_> {
fn drop(&mut self) {
let mut pending = lock(&self.inner.pending);
pending.ids.remove(&self.id);
if let Some(label) = &self.webview {
if let Some(n) = pending.slots.get_mut(label) {
*n = n.saturating_sub(1);
}
}
}
}
fn check_cap(clients: &HashMap<String, Entry>, label: &str, held: usize) -> Result<()> {
let count: usize = clients.values().map(|e| e.hub.count(label)).sum::<usize>() + held;
if count >= MAX_SUBSCRIPTIONS_PER_WEBVIEW {
return Err(Error::TooManySubscriptions(MAX_SUBSCRIPTIONS_PER_WEBVIEW));
}
Ok(())
}
impl Entry {
fn release(self) -> Client {
self.hub.close();
for id in self.listeners {
self.client.off_listener(id);
}
self.client
}
}
impl Hub {
fn new(offer_acks: bool) -> Arc<Hub> {
Arc::new(Hub {
state: Mutex::new(HubState {
subs: HashMap::new(),
last: None,
acks: HashMap::new(),
next_ack: 0,
offer_acks,
closed: false,
}),
forwarded: watch::Sender::new(0),
})
}
fn event(&self, event: Event) {
let mut st = lock(&self.state);
if st.closed || st.subs.is_empty() {
return;
}
let name = event.name().to_owned();
let wanted = |s: &Sub| s.events.as_ref().is_none_or(|e| e.contains(&name));
let mut ack_id = None;
if let Some(sender) = event.ack_sender().filter(|_| st.offer_acks) {
let mut webviews: Vec<String> = st
.subs
.values()
.filter(|s| wanted(s))
.filter_map(|s| s.webview.clone())
.collect();
webviews.sort();
webviews.dedup();
if !webviews.is_empty() {
st.acks.retain(|_, a| a.at.elapsed() < ACK_TTL);
for label in &webviews {
st.make_room(label);
}
st.next_ack += 1;
let id = st.next_ack;
st.acks.insert(
id,
PendingAck {
sender,
at: Instant::now(),
webviews,
},
);
ack_id = Some(id);
}
}
let args: crate::Args = event.into_payload().into();
st.send_all(|s| {
wanted(s).then(|| Message::Event {
event: name.clone(),
args: args.clone(),
ack_id: ack_id.filter(|_| s.webview.is_some()),
})
});
}
fn state(&self, state: &ConnectionState) {
self.forward_state(State::from(state));
}
fn forward_state(&self, state: State) {
let mut st = lock(&self.state);
if st.closed {
return;
}
st.last = Some(state.clone());
st.send_all(|_| {
Some(Message::State {
state: state.clone(),
snapshot: false,
})
});
drop(st);
self.forwarded.send_modify(|n| *n += 1);
}
async fn settle(&self, client: &Client) {
let mut rx = self.forwarded.subscribe();
let settled = |hub: &Hub| {
!matches!(
lock(&hub.state).last,
None | Some(State::Connecting { attempt: 0 })
)
};
let _ = tokio::time::timeout(SETTLE, async {
while !settled(self) && !client.state().is_closed() {
if rx.changed().await.is_err() {
return;
}
}
})
.await;
}
fn snapshot_to(&self, sub: SubscriptionId, client: &Client) -> State {
self.snapshot_or(sub, || State::from(&client.state()))
}
fn snapshot_or(&self, sub: SubscriptionId, fallback: impl FnOnce() -> State) -> State {
Self::snapshot_locked(&mut lock(&self.state), sub, fallback)
}
fn attach(&self, id: SubscriptionId, sub: Sub, fallback: impl FnOnce() -> State) -> State {
let mut st = lock(&self.state);
st.subs.insert(id, sub);
Self::snapshot_locked(&mut st, id, fallback)
}
fn snapshot_locked(
st: &mut HubState,
sub: SubscriptionId,
fallback: impl FnOnce() -> State,
) -> State {
let state = st.last.clone().unwrap_or_else(fallback);
if let Some(s) = st.subs.get(&sub) {
let message = Message::State {
state: state.clone(),
snapshot: true,
};
if s.channel.send(message).is_err() {
st.subs.remove(&sub);
}
}
state
}
fn remove_sub(&self, sub: SubscriptionId, webview: Option<&str>) -> bool {
let mut st = lock(&self.state);
match st.subs.get(&sub) {
Some(s) if webview.is_none_or(|w| s.webview.as_deref() == Some(w)) => {
st.subs.remove(&sub);
true
}
_ => false,
}
}
fn drop_webview(&self, label: &str) {
let mut st = lock(&self.state);
st.subs.retain(|_, s| s.webview.as_deref() != Some(label));
st.acks.retain(|_, a| {
a.webviews.retain(|w| w != label);
!a.webviews.is_empty()
});
}
fn count(&self, label: &str) -> usize {
lock(&self.state)
.subs
.values()
.filter(|s| s.webview.as_deref() == Some(label))
.count()
}
fn take_ack(&self, ack_id: u64, webview: &str) -> Option<AckSender> {
let mut st = lock(&self.state);
let ok = st
.acks
.get(&ack_id)
.is_some_and(|a| a.at.elapsed() < ACK_TTL && a.webviews.iter().any(|w| w == webview));
ok.then(|| st.acks.remove(&ack_id).expect("checked").sender)
}
fn stop_acks(&self) {
let mut st = lock(&self.state);
st.offer_acks = false;
st.acks.clear();
}
fn close(&self) {
let mut st = lock(&self.state);
st.closed = true;
st.subs.clear();
st.acks.clear();
}
}
impl HubState {
fn send_all(&mut self, make: impl Fn(&Sub) -> Option<Message>) {
self.subs.retain(|_, s| match make(s) {
Some(message) => s.channel.send(message).is_ok(),
None => true,
});
}
fn make_room(&mut self, label: &str) {
let mine = || {
self.acks
.iter()
.filter(|(_, a)| a.webviews.iter().any(|w| w == label))
};
if mine().count() < MAX_PENDING_ACKS_PER_WEBVIEW {
return;
}
let Some(oldest) = mine().min_by_key(|(_, a)| a.at).map(|(id, _)| *id) else {
return;
};
let ack = self.acks.get_mut(&oldest).expect("found above");
ack.webviews.retain(|w| w != label);
if ack.webviews.is_empty() {
self.acks.remove(&oldest);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::OnceLock;
use tokio::sync::Notify;
type Pause = (String, Arc<Notify>, Arc<Notify>);
static PAUSE: OnceLock<Mutex<Option<Pause>>> = OnceLock::new();
pub(super) async fn after_insert(id: &str) {
let pause = lock(PAUSE.get_or_init(Mutex::default)).clone();
if let Some((target, reached, go)) = pause {
if target == id {
reached.notify_one();
go.notified().await;
}
}
}
#[tokio::test(flavor = "multi_thread")]
async fn a_registered_connect_holds_no_pending_slot() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let io = SocketIo::default();
let (reached, go) = (Arc::new(Notify::new()), Arc::new(Notify::new()));
*lock(PAUSE.get_or_init(Mutex::default)) =
Some(("p".into(), Arc::clone(&reached), Arc::clone(&go)));
let (ch, _rx) = channel();
let connecting = {
let io = io.clone();
tokio::spawn(async move {
let url = conformance_server();
let builder = ClientBuilder::new(url.clone());
io.connect_from_webview(
"p".into(),
&url,
None,
builder,
Default::default(),
sub(ch, "w"),
)
.await
})
};
reached.notified().await;
*lock(PAUSE.get().unwrap()) = None;
{
let pending = lock(&io.inner.pending);
assert!(pending.ids.is_empty(), "the id is still pending");
assert_eq!(pending.slots.get("w").copied().unwrap_or_default(), 0);
}
go.notify_one();
connecting.await.unwrap().unwrap();
io.remove("p").unwrap().disconnect().await.unwrap();
})
.await
.expect("test `a_registered_connect_holds_no_pending_slot` timed out")
}
use socketio_next_testing::{conformance_server, socket_io_server};
use tauri::ipc::InvokeResponseBody;
fn channel() -> (
Channel<Message>,
std::sync::mpsc::Receiver<serde_json::Value>,
) {
let (tx, rx) = std::sync::mpsc::channel();
let channel = Channel::new(move |body| {
if let InvokeResponseBody::Json(json) = body {
let _ = tx.send(serde_json::from_str(&json).unwrap());
}
Ok(())
});
(channel, rx)
}
fn sub(channel: Channel<Message>, webview: &str) -> SubscribeArgs {
SubscribeArgs {
events: None,
channel,
webview: Some(webview.to_owned()),
}
}
async fn question(client_events: &mut tokio::sync::mpsc::UnboundedReceiver<Event>) -> Event {
loop {
let e = client_events.recv().await.unwrap();
if e.name() == "question" {
return e;
}
}
}
async fn asking_client() -> (Client, tokio::sync::mpsc::UnboundedReceiver<Event>) {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let client = ClientBuilder::new(conformance_server())
.on("question", move |event: Event, _: Client| {
let _ = tx.send(event);
std::future::ready(())
})
.connect()
.await
.unwrap();
(client, rx)
}
#[tokio::test(flavor = "multi_thread")]
async fn no_acks_are_minted_after_stop_acks() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let (client, mut events) = asking_client().await;
let hub = Hub::new(true);
let (ch, rx) = channel();
lock(&hub.state).subs.insert(1, sub(ch, "w").into_sub());
client.emit("ask", ()).unwrap();
let q = question(&mut events).await;
hub.stop_acks();
hub.event(q);
let m = rx.recv_timeout(Duration::from_secs(5)).unwrap();
assert_eq!(m["event"], "question");
assert!(m.get("ackId").is_none(), "{m}");
assert!(lock(&hub.state).acks.is_empty());
client.disconnect().await.unwrap();
})
.await
.expect("test `no_acks_are_minted_after_stop_acks` timed out")
}
#[tokio::test(flavor = "multi_thread")]
async fn ack_budget_is_per_webview() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let (client, mut events) = asking_client().await;
let hub = Hub::new(true);
let (greedy, _g) = channel();
let (polite, polite_rx) = channel();
lock(&hub.state)
.subs
.insert(1, sub(greedy, "greedy").into_sub());
lock(&hub.state)
.subs
.insert(2, sub(polite, "polite").into_sub());
client.emit("ask", ()).unwrap();
hub.event(question(&mut events).await);
let first = polite_rx.recv_timeout(Duration::from_secs(5)).unwrap()["ackId"]
.as_u64()
.unwrap();
hub.remove_sub(2, None);
let q = question_with_ack(&client, &mut events).await;
for _ in 0..MAX_PENDING_ACKS_PER_WEBVIEW + 10 {
hub.event(q.clone());
}
let greedy_count = lock(&hub.state)
.acks
.values()
.filter(|a| a.webviews.iter().any(|w| w == "greedy"))
.count();
assert_eq!(greedy_count, MAX_PENDING_ACKS_PER_WEBVIEW);
assert!(
hub.take_ack(first, "greedy").is_none(),
"greedy's own oldest went"
);
assert!(
hub.take_ack(first, "polite").is_some(),
"the polite webview kept its ack"
);
client.disconnect().await.unwrap();
})
.await
.expect("test `ack_budget_is_per_webview` timed out")
}
async fn question_with_ack(
client: &Client,
events: &mut tokio::sync::mpsc::UnboundedReceiver<Event>,
) -> Event {
client.emit("ask", ()).unwrap();
question(events).await
}
#[tokio::test(flavor = "multi_thread")]
async fn one_ack_id_per_server_ack() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let (client, mut events) = asking_client().await;
let hub = Hub::new(true);
let (a, a_rx) = channel();
let (b, b_rx) = channel();
lock(&hub.state).subs.insert(1, sub(a, "w1").into_sub());
lock(&hub.state).subs.insert(2, sub(b, "w2").into_sub());
hub.event(question_with_ack(&client, &mut events).await);
let ida = a_rx.recv_timeout(Duration::from_secs(5)).unwrap()["ackId"].clone();
let idb = b_rx.recv_timeout(Duration::from_secs(5)).unwrap()["ackId"].clone();
assert_eq!(ida, idb);
let id = ida.as_u64().unwrap();
assert!(hub.take_ack(id, "w3").is_none(), "not offered to w3");
assert!(hub.take_ack(id, "w2").is_some());
assert!(hub.take_ack(id, "w1").is_none(), "answered once");
client.disconnect().await.unwrap();
})
.await
.expect("test `one_ack_id_per_server_ack` timed out")
}
#[test]
fn snapshot_is_ordered_with_transitions() {
let hub = Hub::new(true);
let (ch0, _rx0) = channel();
lock(&hub.state).subs.insert(1, sub(ch0, "w").into_sub());
let writer = {
let hub = Arc::clone(&hub);
std::thread::spawn(move || {
for attempt in 1..=2000 {
hub.forward_state(State::Connecting { attempt });
}
})
};
let mut seqs = Vec::new();
for n in 0..20u64 {
let (ch, rx) = channel();
let snapshot = hub.attach(100 + n, sub(ch, "w").into_sub(), || State::Connecting {
attempt: 0,
});
seqs.push((snapshot, rx));
}
writer.join().unwrap();
for (snapshot, rx) in seqs {
let attempts: Vec<u64> = rx
.try_iter()
.map(|m| m["state"]["attempt"].as_u64().unwrap())
.collect();
let State::Connecting { attempt } = snapshot else {
unreachable!()
};
assert_eq!(attempts[0], u64::from(attempt), "the snapshot comes first");
assert!(
attempts.windows(2).all(|w| w[0] < w[1]),
"ordered: {attempts:?}"
);
}
}
#[tokio::test(flavor = "multi_thread")]
async fn snapshot_uses_the_last_forwarded_state() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let client = ClientBuilder::new(socket_io_server())
.connect()
.await
.unwrap();
let hub = Hub::new(true);
let (ch, rx) = channel();
lock(&hub.state).subs.insert(1, sub(ch, "w").into_sub());
assert!(
matches!(hub.snapshot_to(1, &client), State::Connected { .. }),
"fallback"
);
hub.forward_state(State::Disconnected {
reason: "transport close".into(),
error: None,
});
let (ch2, rx2) = channel();
lock(&hub.state).subs.insert(2, sub(ch2, "w").into_sub());
assert!(matches!(
hub.snapshot_to(2, &client),
State::Disconnected { .. }
));
assert_eq!(rx2.recv().unwrap()["snapshot"], true);
assert_eq!(rx.recv().unwrap()["state"]["status"], "connected");
client.disconnect().await.unwrap();
})
.await
.expect("test `snapshot_uses_the_last_forwarded_state` timed out")
}
#[tokio::test(flavor = "multi_thread")]
async fn reload_during_connect_attaches_nothing() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let io = SocketIo::default();
let (ch, _rx) = channel();
let connecting = {
let io = io.clone();
tokio::spawn(async move {
let url = format!("{}/slow", socket_io_server());
let builder = ClientBuilder::new(url.clone());
io.connect_from_webview(
"slow".into(),
&url,
None,
builder,
Default::default(),
sub(ch, "w"),
)
.await
})
};
tokio::time::sleep(Duration::from_millis(50)).await;
io.drop_webview("w");
connecting.await.unwrap().unwrap();
assert_eq!(io.hub("slow").count("w"), 0);
io.remove("slow").unwrap().disconnect().await.unwrap();
})
.await
.expect("test `reload_during_connect_attaches_nothing` timed out")
}
#[tokio::test(flavor = "multi_thread")]
async fn a_reloaded_webview_loses_its_subscriptions_and_acks() {
tokio::time::timeout(std::time::Duration::from_secs(90), async move {
let io = SocketIo::default();
io.connect("c", ClientBuilder::new(conformance_server()))
.await
.unwrap();
let (a, _) = channel();
let (b, _) = channel();
let (sa, _) = io.subscribe_with("c", sub(a, "a")).unwrap();
let (sb, _) = io.subscribe_with("c", sub(b, "b")).unwrap();
io.drop_webview("a");
assert!(!io.unsubscribe("c", sa));
assert!(io.unsubscribe("c", sb));
io.remove("c").unwrap().disconnect().await.unwrap();
})
.await
.expect("test `a_reloaded_webview_loses_its_subscriptions_and_acks` timed out")
}
#[test]
fn url_allow_list() {
let io = SocketIo::with_allowed(vec![
url::Url::parse("https://api.example.com").unwrap(),
url::Url::parse("wss://rt.example.com/v1/").unwrap(),
]);
let ok = |url: &str, path: Option<&str>| io.url_allowed(url, path);
assert!(ok("https://api.example.com", None));
assert!(ok(
"https://api.example.com:443/admin?x=1",
Some("/anything/")
));
assert!(!ok("https://api.example.com.evil.com", None));
assert!(!ok("https://api.example.com:8443", None));
assert!(!ok("http://api.example.com", None));
assert!(ok("wss://rt.example.com", Some("/v1/socket.io")));
assert!(ok("wss://rt.example.com/v1", Some("v1/")));
assert!(
!ok("wss://rt.example.com/v1", None),
"default /socket.io/ is outside /v1/"
);
assert!(!ok("wss://rt.example.com/v1/chat", Some("/v10/")));
assert!(!ok("not a url", None));
assert!(SocketIo::default().url_allowed("http://anything", Some("/x/")));
for path in [
"/v1/../admin/",
"/v1/%2e%2e/admin/",
"/v1/%2E%2E/admin/",
"/v1/..\\admin/",
"/v1/.%2e/admin/",
"/v1/.\n./admin/",
"/v1/\t../admin/",
"/v1/%252e%252e/admin/",
"/v1/%u002e%u002e/admin/",
] {
assert!(!ok("wss://rt.example.com", Some(path)), "{path:?}");
}
assert!(ok("wss://rt.example.com", Some("/v1/./socket.io/")));
let deep = SocketIo::with_allowed(vec![
url::Url::parse("wss://rt.example.com/v1/socket.io/").unwrap(),
]);
assert!(deep.url_allowed("wss://rt.example.com", Some("/v1/./socket.io/")));
assert!(!deep.url_allowed("wss://rt.example.com", Some("/v1/./other/")));
}
#[test]
fn unknown_ids() {
let io = SocketIo::default();
let channel = Channel::new(|_: InvokeResponseBody| Ok(()));
assert!(matches!(
io.subscribe("x", None, channel),
Err(Error::UnknownClient(_))
));
assert!(matches!(io.full("x"), Err(Error::UnknownClient(_))));
assert!(matches!(
io.ack("x", 1, "w", Payload::empty()),
Err(Error::UnknownClient(_))
));
assert!(!io.unsubscribe("x", 1));
assert!(io.remove("x").is_none());
assert!(io.ids().is_empty());
}
}