refactor(media): split media protocol internals

This commit is contained in:
Erwan Leboucher committed 2026-08-07 23:07:43 +02:00
1 parent 2e2f20c010
commit 31666ffba0
4 files changed
+934 -833

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,229 @@
use std::{collections::HashMap, sync::RwLock};
use aes::{
cipher::{KeyIvInit, StreamCipher},
Aes256,
};
use base64::Engine;
use ctr::{Ctr128BE, Ctr64BE};
use sha2::{Digest, Sha256};
use tauri::http::{StatusCode, Uri};
use tauri_plugin_http::reqwest::Url;
use super::response::sniff_image_content_type;
// How the webview spells this protocol: `sable-media://` on iOS/macOS, and
// `http(s)://sable-media.localhost/` on Windows/Android respectively.
const MEDIA_PROTOCOL_PREFIXES: [&str; 3] = [
"sable-media://",
"http://sable-media.localhost/",
"https://sable-media.localhost/",
];
#[derive(Clone)]
struct EncryptionParams {
key: [u8; 32],
iv: [u8; 16],
counter_length: u8,
expected_sha256: Vec<u8>,
content_type: String,
}
#[derive(Default)]
pub(super) struct EncryptionStore {
entries: RwLock<HashMap<String, EncryptionParams>>,
}
impl EncryptionStore {
pub(super) fn register(
&self,
url: &str,
key: &str,
iv: &str,
sha256: &str,
version: &str,
content_type: String,
) -> Result<(), String> {
let key_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
.decode(key)
.map_err(|error| format!("invalid key base64url: {error}"))?;
let key = key_bytes
.try_into()
.map_err(|bytes: Vec<u8>| format!("key must be 32 bytes, got {}", bytes.len()))?;
let iv_bytes = base64::engine::general_purpose::STANDARD_NO_PAD
.decode(iv)
.map_err(|error| format!("invalid iv base64: {error}"))?;
let iv = iv_bytes
.try_into()
.map_err(|bytes: Vec<u8>| format!("iv must be 16 bytes, got {}", bytes.len()))?;
let expected_sha256 = base64::engine::general_purpose::STANDARD_NO_PAD
.decode(sha256)
.map_err(|error| format!("invalid sha256 base64: {error}"))?;
let params = EncryptionParams {
key,
iv,
counter_length: if matches!(version, "v1" | "v2") {
64
} else {
128
},
expected_sha256,
content_type,
};
self.entries
.write()
.map_err(|_| "encryption lock poisoned".to_owned())?
.insert(normalize_key(url), params);
Ok(())
}
pub(super) fn clear(&self) {
if let Ok(mut entries) = self.entries.write() {
entries.clear();
}
}
pub(super) fn contains(&self, url: &str) -> bool {
self.entries
.read()
.map(|entries| entries.contains_key(url))
.unwrap_or(false)
}
/// Decrypt the body if encryption params exist for this URL.
pub(super) fn decrypt(
&self,
url: &str,
ciphertext: Vec<u8>,
upstream_content_type: &str,
) -> Result<(Vec<u8>, String), StatusCode> {
let params = self
.entries
.read()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.get(url)
.cloned();
let Some(params) = params else {
return Ok((ciphertext, upstream_content_type.to_owned()));
};
if Sha256::digest(&ciphertext).as_slice() != params.expected_sha256 {
return Err(StatusCode::BAD_GATEWAY);
}
let mut plaintext = ciphertext;
match params.counter_length {
64 => Ctr64BE::<Aes256>::new((&params.key).into(), (&params.iv).into())
.apply_keystream(&mut plaintext),
128 => Ctr128BE::<Aes256>::new((&params.key).into(), (&params.iv).into())
.apply_keystream(&mut plaintext),
_ => return Err(StatusCode::BAD_REQUEST),
}
let content_type = if params.content_type.is_empty()
|| params.content_type == "application/octet-stream"
{
sniff_image_content_type(&plaintext)
.map(str::to_owned)
.unwrap_or(params.content_type)
} else {
params.content_type
};
Ok((plaintext, content_type))
}
}
pub(super) fn normalize_key(url: &str) -> String {
if MEDIA_PROTOCOL_PREFIXES
.iter()
.any(|prefix| url.starts_with(prefix))
{
if let Ok(uri) = Uri::try_from(url) {
let path = uri.path().trim_start_matches('/');
let decoded = percent_encoding::percent_decode_str(path)
.decode_utf8()
.unwrap_or(std::borrow::Cow::Borrowed(path));
if let Ok(mut parsed) = Url::parse(&decoded) {
// A retry fragment is transport metadata, never part of the params key.
parsed.set_fragment(None);
return parsed.to_string();
}
return decoded.into_owned();
}
}
// Parse bare URLs too, so both sides of the map agree on one canonical form.
Url::parse(url)
.map(|mut parsed| {
parsed.set_fragment(None);
parsed.to_string()
})
.unwrap_or_else(|_| url.to_owned())
}
#[cfg(test)]
mod tests {
use super::normalize_key;
#[test]
fn normalize_key_strips_sable_media_prefix() {
let input = "sable-media://localhost/https%3A%2F%2Fmatrix.example.org%2F_matrix%2Fclient%2Fv1%2Fmedia%2Fdownload%2Fmatrix.org%2Fabc123";
let result = normalize_key(input);
assert_eq!(
result,
"https://matrix.example.org/_matrix/client/v1/media/download/matrix.org/abc123"
);
}
#[test]
fn normalize_key_strips_android_prefix() {
// Android serves the protocol over https, which used to fall through to the bare-URL
// branch: the params were then keyed by the sable-media URL, never matched at fetch
// time, and encrypted media was served as ciphertext.
let input = "https://sable-media.localhost/https%3A%2F%2Fmatrix.example.org%2F_matrix%2Fclient%2Fv1%2Fmedia%2Fdownload%2Fmatrix.org%2Fabc123%3Fallow_redirect%3Dtrue?__sable_media_cache=3&__sable_media_session=%40a%3Aexample.org";
let result = normalize_key(input);
assert_eq!(
result,
"https://matrix.example.org/_matrix/client/v1/media/download/matrix.org/abc123?allow_redirect=true"
);
}
#[test]
fn normalize_key_strips_windows_prefix() {
let input = "http://sable-media.localhost/https%3A%2F%2Fmatrix.example.org%2F_matrix%2Fclient%2Fv1%2Fmedia%2Fthumbnail%2Fmatrix.org%2Fxyz%3Fwidth%3D96%26height%3D96";
let result = normalize_key(input);
assert_eq!(
result,
"https://matrix.example.org/_matrix/client/v1/media/thumbnail/matrix.org/xyz?width=96&height=96"
);
}
#[test]
fn normalize_key_passes_through_bare_url() {
let input = "https://matrix.example.org/_matrix/client/v1/media/download/matrix.org/abc123";
let result = normalize_key(input);
assert_eq!(result, input);
}
#[test]
fn retry_fragment_never_keys_encryption_params() {
let bare = "https://matrix.example.org/_matrix/client/v1/media/download/matrix.org/abc123";
for input in [
format!("{bare}#retry=1"),
format!(
"https://sable-media.localhost/{}?__sable_media_session=%40a%3Aexample.org",
percent_encoding::utf8_percent_encode(
&format!("{bare}#retry=1"),
percent_encoding::NON_ALPHANUMERIC
)
),
] {
assert_eq!(normalize_key(&input), bare);
}
}
}
@@ -0,0 +1,535 @@
use std::{
fs,
io::{Read, Seek, SeekFrom},
path::PathBuf,
};
use tauri::http::{header, response::Builder as ResponseBuilder, Response, StatusCode};
// WRY's Android adapter copies custom-protocol bodies into a JNI byte array, so range
// responses must stay bounded even when WebView asks for the rest of a large video.
pub(super) const MAX_RANGE_CHUNK: u64 = 2 * 1024 * 1024;
enum RangeSelection {
Partial { start: u64, end: u64 },
Ignore,
Unsatisfiable,
}
pub(super) async fn read_full(body_path: PathBuf) -> Result<Vec<u8>, StatusCode> {
tokio::task::spawn_blocking(move || fs::read(&body_path))
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
pub(super) async fn serve_range(
body_path: PathBuf,
content_type: String,
range_header: String,
) -> Result<Response<Vec<u8>>, StatusCode> {
tokio::task::spawn_blocking(move || {
let mut file = fs::File::open(&body_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let total = file
.metadata()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.len();
let (start, end) = match select_range(&range_header, total) {
RangeSelection::Partial { start, end } => (start, end),
RangeSelection::Ignore => {
let mut body = Vec::new();
file.read_to_end(&mut body)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
return Ok(ok_response(body, &content_type));
}
RangeSelection::Unsatisfiable => return Ok(range_not_satisfiable(total)),
};
let length = end - start + 1;
let mut body = vec![0; length as usize];
file.seek(SeekFrom::Start(start))
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
file.read_exact(&mut body)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(partial_response(body, &content_type, start, end, total))
})
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
}
pub(super) fn serve_range_memory(
body: &[u8],
content_type: &str,
range_header: &str,
) -> Response<Vec<u8>> {
let total = body.len() as u64;
let (start, end) = match select_range(range_header, total) {
RangeSelection::Partial { start, end } => (start, end),
RangeSelection::Ignore => return ok_response(body.to_vec(), content_type),
RangeSelection::Unsatisfiable => return range_not_satisfiable(total),
};
partial_response(
body[start as usize..=end as usize].to_vec(),
content_type,
start,
end,
total,
)
}
fn select_range(range_header: &str, total: u64) -> RangeSelection {
if total == 0 {
return RangeSelection::Unsatisfiable;
}
let Some(spec) = range_header.strip_prefix("bytes=") else {
return RangeSelection::Unsatisfiable;
};
if spec.contains(',') {
return RangeSelection::Ignore;
}
let Some((start, end)) = spec.trim().split_once('-') else {
return RangeSelection::Unsatisfiable;
};
let (start, end) = if start.is_empty() {
let Ok(suffix) = end.parse::<u64>() else {
return RangeSelection::Unsatisfiable;
};
if suffix == 0 {
return RangeSelection::Unsatisfiable;
}
let length = suffix.min(total).min(MAX_RANGE_CHUNK);
(total - length, total - 1)
} else {
let Ok(start) = start.parse::<u64>() else {
return RangeSelection::Unsatisfiable;
};
let requested_end = if end.is_empty() {
total - 1
} else {
let Ok(end) = end.parse::<u64>() else {
return RangeSelection::Unsatisfiable;
};
end.min(total - 1)
};
let end = requested_end.min(start.saturating_add(MAX_RANGE_CHUNK - 1));
(start, end)
};
if start <= end && start < total {
RangeSelection::Partial { start, end }
} else {
RangeSelection::Unsatisfiable
}
}
// Shared response headers. Media is content-addressed and the URL is session-scoped, so
// it is safe to let the webview cache it as immutable and advertise byte-range support.
pub(super) fn media_response_builder(status: StatusCode, content_type: &str) -> ResponseBuilder {
let cache_control = if content_type == "application/octet-stream" {
"no-store"
} else {
"private, max-age=31536000, immutable"
};
Response::builder()
.status(status)
.header(header::CONTENT_TYPE, content_type)
.header(header::ACCEPT_RANGES, "bytes")
.header(header::CACHE_CONTROL, cache_control)
.header(header::X_CONTENT_TYPE_OPTIONS, "nosniff")
.header(
header::CONTENT_SECURITY_POLICY,
"sandbox; default-src 'none'; script-src 'none'; object-src 'none'",
)
}
pub(super) fn ok_response(body: Vec<u8>, content_type: &str) -> Response<Vec<u8>> {
let content_length = body.len();
media_response_builder(StatusCode::OK, content_type)
.header(header::CONTENT_LENGTH, content_length)
.body(body)
.expect("failed to build media response")
}
fn partial_response(
body: Vec<u8>,
content_type: &str,
start: u64,
end: u64,
total: u64,
) -> Response<Vec<u8>> {
let content_length = body.len();
media_response_builder(StatusCode::PARTIAL_CONTENT, content_type)
.header(header::CONTENT_LENGTH, content_length)
.header(
header::CONTENT_RANGE,
format!("bytes {start}-{end}/{total}"),
)
.body(body)
.expect("failed to build partial media response")
}
pub(super) fn range_not_satisfiable(total: u64) -> Response<Vec<u8>> {
let mut response = error_response(StatusCode::RANGE_NOT_SATISFIABLE);
let headers = response.headers_mut();
headers.insert(
header::CONTENT_RANGE,
header::HeaderValue::from_str(&format!("bytes */{total}"))
.expect("valid 416 content range"),
);
headers.insert(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("0"),
);
headers.insert(
header::ACCEPT_RANGES,
header::HeaderValue::from_static("bytes"),
);
response
}
/// Sniff the image MIME type from magic bytes of decrypted content.
/// Restricted to an image allowlist and never returns SVG (which can carry scripts).
/// Used as a fallback when the registered content type is missing or octet-stream.
pub(super) fn sniff_image_content_type(bytes: &[u8]) -> Option<&'static str> {
const ALLOWED: [&str; 5] = [
"image/png",
"image/jpeg",
"image/gif",
"image/webp",
"image/avif",
];
infer::get(bytes)
.filter(|kind| ALLOWED.contains(&kind.mime_type()))
.map(|kind| kind.mime_type())
}
pub(super) fn session_unavailable_response() -> Response<Vec<u8>> {
Response::builder()
.status(StatusCode::SERVICE_UNAVAILABLE)
.header(header::RETRY_AFTER, "1")
.header(header::CACHE_CONTROL, "no-store")
.body(Vec::new())
.expect("failed to build 503 media response")
}
pub(super) fn error_response(status: StatusCode) -> Response<Vec<u8>> {
Response::builder()
.status(status)
.header(header::CACHE_CONTROL, "no-store")
.body(Vec::new())
.expect("failed to build media error response")
}
/// The app's own webview origins, which vary by platform and scheme.
fn is_webview_origin(origin: &str) -> bool {
matches!(
origin,
"tauri://localhost" | "http://tauri.localhost" | "https://tauri.localhost"
) || (cfg!(debug_assertions) && origin.starts_with("http://localhost:"))
}
/// Grants CORS only to our own webview. A request with no `Origin` (an `<img>`
/// load) is not a CORS request and needs no grant.
pub(super) fn apply_cors_headers(response: &mut Response<Vec<u8>>, request_origin: Option<&str>) {
let Some(origin) = request_origin.filter(|origin| is_webview_origin(origin)) else {
return;
};
let Ok(value) = header::HeaderValue::from_str(origin) else {
return;
};
let headers = response.headers_mut();
headers.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, value);
// Responses are cached `immutable`, so the cache must key on Origin too.
headers.insert(header::VARY, header::HeaderValue::from_static("Origin"));
}
#[cfg(test)]
mod tests {
use std::{fs, sync::atomic::AtomicU64};
use tauri::http::{header, StatusCode};
use super::*;
static TEST_CACHE_ID: AtomicU64 = AtomicU64::new(0);
fn selected_range(range_header: &str, total: u64) -> Option<(u64, u64)> {
match select_range(range_header, total) {
RangeSelection::Partial { start, end } => Some((start, end)),
_ => None,
}
}
fn is_unsatisfiable(range_header: &str, total: u64) -> bool {
matches!(
select_range(range_header, total),
RangeSelection::Unsatisfiable
)
}
#[test]
fn ok_response_has_content_length_and_cache_headers() {
let response = ok_response(vec![0_u8; 42], "image/png");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get(header::CONTENT_LENGTH).unwrap(),
"42"
);
assert_eq!(
response.headers().get(header::CACHE_CONTROL).unwrap(),
"private, max-age=31536000, immutable"
);
assert_eq!(
response.headers().get(header::ACCEPT_RANGES).unwrap(),
"bytes"
);
}
#[test]
fn select_range_supports_video_metadata_and_seek_requests() {
assert_eq!(selected_range("bytes=0-499", 1000), Some((0, 499)));
assert_eq!(selected_range("bytes=500-", 1000), Some((500, 999)));
assert_eq!(selected_range("bytes=-200", 1000), Some((800, 999)));
assert_eq!(selected_range("bytes=990-5000", 1000), Some((990, 999)));
}
#[test]
fn open_ended_ranges_are_capped_from_the_requested_offset() {
let total = MAX_RANGE_CHUNK * 2 + 100;
assert_eq!(
selected_range("bytes=0-", total),
Some((0, MAX_RANGE_CHUNK - 1))
);
assert_eq!(
selected_range(&format!("bytes={}-", MAX_RANGE_CHUNK), total),
Some((MAX_RANGE_CHUNK, MAX_RANGE_CHUNK * 2 - 1))
);
assert_eq!(
selected_range(&format!("bytes={}-", MAX_RANGE_CHUNK * 2), total),
Some((MAX_RANGE_CHUNK * 2, total - 1))
);
}
#[test]
fn malformed_or_unsatisfiable_range_is_rejected() {
for range in [
"bytes=1000-1001",
"bytes=500-400",
"bytes=abc-def",
"items=0-1",
] {
assert!(is_unsatisfiable(range, 1000), "{range}");
}
assert!(is_unsatisfiable("bytes=0-0", 0));
}
#[test]
fn multi_range_requests_fall_back_to_the_full_response() {
let body = [1_u8, 2, 3];
let response = serve_range_memory(&body, "video/mp4", "bytes=0-0,2-2");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.body(), &body);
}
#[test]
fn in_memory_ranges_are_partial_and_capped() {
let body: Vec<u8> = (0..200u8).collect();
let response = serve_range_memory(&body, "video/mp4", "bytes=10-19");
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
assert_eq!(response.body(), &body[10..=19]);
assert_eq!(
response.headers().get(header::CONTENT_RANGE).unwrap(),
"bytes 10-19/200"
);
let body = vec![7_u8; (MAX_RANGE_CHUNK * 2) as usize];
let response = serve_range_memory(&body, "video/mp4", "bytes=0-");
assert_eq!(response.body().len() as u64, MAX_RANGE_CHUNK);
assert_eq!(
response
.headers()
.get(header::CONTENT_RANGE)
.unwrap()
.to_str()
.unwrap(),
format!("bytes 0-{}/{}", MAX_RANGE_CHUNK - 1, body.len())
);
let requested_suffix = MAX_RANGE_CHUNK + 100;
let response =
serve_range_memory(&body, "video/mp4", &format!("bytes=-{requested_suffix}"));
assert_eq!(response.body().len() as u64, MAX_RANGE_CHUNK);
let expected = format!(
"bytes {}-{}/{}",
body.len() as u64 - MAX_RANGE_CHUNK,
body.len() - 1,
body.len()
);
assert_eq!(
response
.headers()
.get(header::CONTENT_RANGE)
.unwrap()
.to_str()
.unwrap(),
expected
);
}
#[test]
fn unsatisfiable_ranges_are_not_cacheable() {
let response = range_not_satisfiable(1000);
assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
assert_eq!(
response.headers().get(header::CONTENT_RANGE).unwrap(),
"bytes */1000"
);
assert_eq!(
response.headers().get(header::CACHE_CONTROL).unwrap(),
"no-store"
);
assert_eq!(response.headers().get(header::CONTENT_LENGTH).unwrap(), "0");
}
#[test]
fn partial_responses_include_webview_range_metadata() {
let response = serve_range_memory(&[0_u8; 100], "video/mp4", "bytes=10-19");
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
assert_eq!(
response.headers().get(header::CONTENT_TYPE).unwrap(),
"video/mp4"
);
assert_eq!(
response.headers().get(header::CONTENT_LENGTH).unwrap(),
"10"
);
assert_eq!(
response.headers().get(header::CONTENT_RANGE).unwrap(),
"bytes 10-19/100"
);
assert_eq!(
response.headers().get(header::ACCEPT_RANGES).unwrap(),
"bytes"
);
}
#[tokio::test]
async fn disk_range_reads_only_the_requested_cached_bytes() {
let path = std::env::temp_dir().join(format!(
"sable-media-range-test-{}-{}",
std::process::id(),
TEST_CACHE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
));
let body: Vec<u8> = (0..200u8).collect();
fs::write(&path, &body).unwrap();
let response = serve_range(path.clone(), "video/mp4".into(), "bytes=10-19".into())
.await
.unwrap();
fs::remove_file(path).ok();
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
assert_eq!(response.body(), &body[10..=19]);
}
#[test]
fn sniff_detects_png() {
let png_header = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
assert_eq!(sniff_image_content_type(&png_header), Some("image/png"));
}
#[test]
fn sniff_rejects_unknown() {
assert_eq!(sniff_image_content_type(&[0x00, 0x01, 0x02]), None);
}
#[test]
fn sniff_does_not_detect_svg() {
let svg = b"<svg xmlns='http://www.w3.org/2000/svg'>";
assert_eq!(sniff_image_content_type(svg), None);
}
#[test]
fn octet_stream_response_is_not_cached_immutable() {
let response = media_response_builder(StatusCode::OK, "application/octet-stream")
.body(Vec::<u8>::new())
.unwrap();
assert_eq!(
response.headers().get(header::CACHE_CONTROL).unwrap(),
"no-store"
);
}
#[test]
fn cors_is_granted_only_to_the_apps_own_webview_origins() {
for origin in [
"tauri://localhost",
"http://tauri.localhost",
"https://tauri.localhost",
] {
let mut response = ok_response(vec![0_u8; 4], "image/png");
apply_cors_headers(&mut response, Some(origin));
assert_eq!(
response
.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.unwrap(),
origin,
"{origin} should be granted CORS"
);
assert_eq!(response.headers().get(header::VARY).unwrap(), "Origin");
}
}
#[test]
fn cors_is_denied_to_foreign_origins() {
for origin in [
"https://evil.example.org",
"https://tauri.localhost.evil.example.org",
"null",
] {
let mut response = ok_response(vec![0_u8; 4], "image/png");
apply_cors_headers(&mut response, Some(origin));
assert!(
!response
.headers()
.contains_key(header::ACCESS_CONTROL_ALLOW_ORIGIN),
"{origin} must not be granted CORS"
);
}
}
#[test]
fn requests_without_an_origin_get_no_cors_header() {
let mut response = ok_response(vec![0_u8; 4], "image/png");
apply_cors_headers(&mut response, None);
assert!(!response
.headers()
.contains_key(header::ACCESS_CONTROL_ALLOW_ORIGIN));
}
#[test]
fn error_responses_are_no_store() {
for response in [
error_response(StatusCode::UNAUTHORIZED),
error_response(StatusCode::FORBIDDEN),
session_unavailable_response(),
] {
assert_eq!(
response.headers().get(header::CACHE_CONTROL).unwrap(),
"no-store"
);
}
}
#[test]
fn image_response_is_cached_immutable() {
let response = media_response_builder(StatusCode::OK, "image/png")
.body(Vec::<u8>::new())
.unwrap();
assert_eq!(
response.headers().get(header::CACHE_CONTROL).unwrap(),
"private, max-age=31536000, immutable"
);
}
}
@@ -0,0 +1,130 @@
use std::{
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
RwLock,
},
time::Duration,
};
use tokio::{
sync::Notify,
time::{timeout_at, Instant},
};
#[derive(Clone)]
pub(super) struct MediaSession {
pub(super) origin: String,
pub(super) token: String,
// Cache key input. The Matrix user ID, not `token`, which rotates on every OIDC
// refresh and would orphan the whole on-disk cache.
pub(super) scope: String,
pub(super) generation: u64,
}
pub(super) struct SessionStore {
current: RwLock<Option<MediaSession>>,
ready: Notify,
ever_set: AtomicBool,
generation: AtomicU64,
}
impl Default for SessionStore {
fn default() -> Self {
Self {
current: RwLock::new(None),
ready: Notify::new(),
ever_set: AtomicBool::new(false),
generation: AtomicU64::new(0),
}
}
}
impl SessionStore {
pub(super) fn current(&self) -> Option<MediaSession> {
self.current.read().ok().and_then(|session| session.clone())
}
pub(super) async fn wait_initial(&self, wait: Duration) -> Option<MediaSession> {
if let Some(session) = self.current() {
return Some(session);
}
if self.ever_set.load(Ordering::Acquire) {
return None;
}
let deadline = Instant::now() + wait;
loop {
let mut notified = std::pin::pin!(self.ready.notified());
// Register before re-checking, otherwise a session arriving in between is missed.
notified.as_mut().enable();
if let Some(session) = self.current() {
return Some(session);
}
if timeout_at(deadline, notified).await.is_err() {
return None;
}
}
}
pub(super) async fn wait_newer(
&self,
previous: &MediaSession,
wait: Duration,
) -> Option<MediaSession> {
let deadline = Instant::now() + wait;
loop {
let mut notified = std::pin::pin!(self.ready.notified());
// Register before re-checking, otherwise a session arriving in between is missed.
notified.as_mut().enable();
match self.current() {
Some(session) if session.generation > previous.generation => {
if session.origin != previous.origin || session.scope != previous.scope {
return None;
}
if session.token != previous.token {
return Some(session);
}
}
None if self.generation.load(Ordering::Acquire) > previous.generation => {
return None;
}
_ => {}
}
if timeout_at(deadline, notified).await.is_err() {
return None;
}
}
}
// `before_notify` runs before waiters wake, so none observes the new session with stale state.
pub(super) fn set(
&self,
mut session: MediaSession,
before_notify: impl FnOnce(),
) -> Result<(), String> {
let mut current = self
.current
.write()
.map_err(|_| "media session lock poisoned".to_owned())?;
session.generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
*current = Some(session);
drop(current);
before_notify();
self.ever_set.store(true, Ordering::Release);
self.ready.notify_waiters();
Ok(())
}
pub(super) fn clear(&self, before_notify: impl FnOnce()) -> Result<(), String> {
let mut current = self
.current
.write()
.map_err(|_| "media session lock poisoned".to_owned())?;
*current = None;
self.generation.fetch_add(1, Ordering::AcqRel);
drop(current);
before_notify();
self.ready.notify_waiters();
Ok(())
}
}