diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index c89bf81b73..f9d3812a2a 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -545,7 +545,8 @@ fn read_bitmap(bitmap: &web_sys::ImageBitmap, w: u32, h: u32) -> Result, /// How long a tile or document fetch may go without anything coming in: the answer's headers, /// or the next part of its body. A slow but steady answer is never cut off; one that stalls is /// given up, so it doesn't stay under way forever, holding its server's place in the queue. -/// Downloads the user asked for have none. +/// So is a picture the pop-up fetches. Downloads the user asked for have none +/// (`cache::gives_up_when_stalled`). const STALL_MS: i32 = 30_000; /// What a step of a fetch waits for, or `None` once it has gone [`STALL_MS`] with nothing (the @@ -619,6 +620,7 @@ async fn fetch_reached( &still, || async { let window = web_sys::window().ok_or("no window")?; + let gives_up = cache::gives_up_when_stalled(url, ceiling); // A data download carries its caller's budget (at most MAX_DOWNLOAD); other fetches use 64 MB. let (url, limit) = cache::split_download_limit(url); let max = limit.map(|n| n as u64).unwrap_or(64 << 20); @@ -660,8 +662,8 @@ async fn fetch_reached( (None, None) => None, }; let init = web_sys::RequestInit::new(); - // A tile or document that stalls is given up (see `STALL_MS`). - let abort = if limit.is_none() { web_sys::AbortController::new().ok() } else { None }; + // A tile, document or picture that stalls is given up (see `STALL_MS`). + let abort = if gives_up { web_sys::AbortController::new().ok() } else { None }; if let Some(a) = &abort { init.set_signal(Some(&a.signal())); } diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 883e48dca5..9107b0b705 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -164,6 +164,7 @@ fn lock() -> std::sync::MutexGuard<'static, Cache> { } static GENERATION: AtomicU64 = AtomicU64::new(0); +static CLEARS: AtomicU64 = AtomicU64::new(0); static IN_FLIGHT: AtomicUsize = AtomicUsize::new(0); fn fetcher() -> &'static Mutex>> { @@ -379,6 +380,13 @@ pub fn generation() -> u64 { GENERATION.load(Ordering::Relaxed) } +/// How many times [`clear`] has run. What was read through the downloads before it and is kept +/// elsewhere (the identify pop-up's pictures) goes too: it may have been read with a login that +/// has since ended. +pub fn clears() -> u64 { + CLEARS.load(Ordering::Relaxed) +} + /// Consumes every elapsed deadline together and returns the next one to rearm. Keeping the /// clock as an input lets the ordering contract be tested without scheduler deadlines. fn take_due(alarms: &mut std::collections::BTreeSet, now: u64) -> (bool, Option) { @@ -681,7 +689,10 @@ pub fn clear() { crate::cog::forget(); crate::warp::forget(); rustgis_data::source::remote::forget(); - GENERATION.fetch_add(1, Ordering::Relaxed); + CLEARS.fetch_add(1, Ordering::Relaxed); + // The map draws again (and the pop-up's pictures are forgotten): the waker wakes an idle app, + // as when a fetch ends, so a clear from a worker thread isn't left until the next input. + notify(); } /// The resource a cache key reads: its address without a download budget, a byte range or a @@ -1066,6 +1077,14 @@ pub fn split_download_limit(url: &str) -> (&str, Option) { } } +/// Whether the browser gives up a fetch of `url` that stalls, as an installed fetcher is handed it +/// with `ceiling`. Tiles and documents are given up. So is a download kept to internet addresses +/// ([`download_public_limited`]): its address comes from data, like a picture for the pop-up. +/// A download the user named is not given up. +pub fn gives_up_when_stalled(url: &str, ceiling: crate::address::Reach) -> bool { + split_download_limit(url).1.is_none() || ceiling == crate::address::Reach::Public +} + fn download_address(url: &str, max: usize) -> String { if max >= MAX_DOWNLOAD { format!("{DOWNLOAD}{url}") } else { format!("{LIMITED_DOWNLOAD}{max}:{url}") } } @@ -1238,9 +1257,32 @@ pub fn download_detailed(url: &str, max: usize, done: DoneDetailed) { download_detailed_within(url, max, DOWNLOAD_TIMEOUT, &[], done); } +/// Like [`download_limited`], connecting natively only to internet addresses (as +/// [`download_public_within`], off the frame): a name, or a redirect to one, that resolves to this +/// computer or the local network is refused when it is resolved. For an address that comes from +/// data rather than the user (a picture named in a feature's values), which mustn't be able to +/// reach local services. An installed fetcher (the browser's) is handed the internet-only ceiling. +pub fn download_public_limited(url: &str, max: usize, done: Done) { + download_reaching(url, max, DOWNLOAD_TIMEOUT, &[], Reaching::Public, Box::new(move |r| done(r.map_err(|f| f.message)))); +} + +/// Which addresses a native download may connect to. +#[derive(Clone, Copy)] +enum Reaching { + /// Wherever the address's name resolves (the user named it). + Any, + /// Only internet addresses, judged where names resolve and redirects lead. + Public, +} + /// Like [`download_detailed`], giving up after `timeout` natively; a status in `omit` isn't /// recorded as a failure. fn download_detailed_within(url: &str, max: usize, timeout: std::time::Duration, omit: &'static [u16], done: DoneDetailed) { + download_reaching(url, max, timeout, omit, Reaching::Any, done); +} + +/// [`download_detailed_within`], natively connecting only where `reaching` allows. +fn download_reaching(url: &str, max: usize, timeout: std::time::Duration, omit: &'static [u16], reaching: Reaching, done: DoneDetailed) { if let Err(e) = check_current() { done(Err(Failure::new(url, e))); return; @@ -1264,11 +1306,17 @@ fn download_detailed_within(url: &str, max: usize, timeout: std::time::Duration, match f { Some(f) => f( marked.clone(), - crate::address::Reach::Local, + match reaching { + Reaching::Any => crate::address::Reach::Local, + Reaching::Public => crate::address::Reach::Public, + }, Box::new(move |r| done(r.map(|fetched| fetched.bytes).map_err(|e| Failure::new(&marked, e)))), omit, ), - None => native::download_within(marked, timeout, omit, done), + None => match reaching { + Reaching::Any => native::download_within(marked, timeout, omit, done), + Reaching::Public => native::download_public_within(marked, timeout, done), + }, } } @@ -2393,6 +2441,20 @@ mod native { }); } + /// Like [`download_within`], connecting only to internet addresses ([`public_agent`]). + pub(super) fn download_public_within(url: String, timeout: std::time::Duration, done: DoneDetailed) { + let capture = Capture::current(); + let still = super::current_still(); + std::thread::spawn(move || { + super::with_optional_still(still, || { + let _scope = capture.enter(); + let r = + super::check_current().map_err(Failure::from).and_then(|_| get_detailed_omitting(&public_agent(timeout), &url, &[])); + done(r); + }) + }); + } + /// Dispatches each queued read through the pool matching its inherited IP ceiling. fn work() { let agent = shared_agent(); @@ -2764,6 +2826,10 @@ mod native { done(Err(crate::failure::Failure::new(&url, message))); } + pub(super) fn download_public_within(url: String, timeout: std::time::Duration, done: DoneDetailed) { + download_within(url, timeout, &[], done); + } + pub(super) fn post(url: String, _body: String, done: Done) { done(Err(format!("{url}: this build has no way to send requests"))); } @@ -3335,6 +3401,84 @@ mod tests { assert!(err.contains("resolves to this computer or the local network"), "{err}"); } + /// The same off the frame ([`download_public_limited`] without a fetcher installed, called + /// directly: other tests here install theirs): one that resolves locally is refused there. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn public_downloads_off_the_frame_refuse_names_that_resolve_locally() { + let _turn = turn(); + let (tx, rx) = std::sync::mpsc::channel(); + let url = download_address("http://localhost:9/picture.png", 1024); + native::download_public_within(url, std::time::Duration::from_secs(5), Box::new(move |r| drop(tx.send(r)))); + let err = rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap().unwrap_err().message; + assert!(err.contains("resolves to this computer or the local network"), "{err}"); + } + + /// A public download asked for by a request that has since ended sends nothing, as any + /// download off the frame doesn't. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn public_downloads_off_the_frame_send_nothing_once_their_request_ended() { + let (tx, rx) = std::sync::mpsc::channel(); + let url = download_address("https://example.org/picture.png", 1024); + with_still(Arc::new(|| false), || { + native::download_public_within(url, std::time::Duration::from_secs(5), Box::new(move |r| drop(tx.send(r)))); + }); + let err = rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap().unwrap_err().message; + assert!(err.contains("cancelled before it was sent"), "{err}"); + } + + /// An installed fetcher (the browser's) is handed the internet-only ceiling for a public + /// download, and an answer that came from the local network is refused. + #[test] + fn public_downloads_through_a_fetcher_keep_the_public_ceiling() { + use crate::address::{Origin, Reach}; + let _turn = turn(); + let ceilings = Arc::new(Mutex::new(Vec::new())); + let sent = ceilings.clone(); + set_fetcher_with_origin(move |_, ceiling, done, _| { + sent.lock().unwrap().push(ceiling); + done(Ok(Fetched { bytes: vec![1], origin: Origin { url: "https://pictures.example/a.png".into(), reach: Reach::Local } })); + }); + let (tx, rx) = std::sync::mpsc::channel(); + let public = tx.clone(); + download_public_limited("https://pictures.example/a.png", 1024, Box::new(move |r| drop(public.send(r)))); + let err = rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap().unwrap_err(); + assert!(err.contains("exceeds its request's network reach"), "{err}"); + download_limited("https://pictures.example/a.png", 1024, Box::new(move |r| drop(tx.send(r)))); + assert_eq!(rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap().unwrap(), vec![1]); + assert_eq!(*ceilings.lock().unwrap(), [Reach::Public, Reach::Local]); + } + + /// A picture for the pop-up is given up in the browser when it stalls, as a tile is; a + /// download the user named, with or without a smaller budget, is not. + #[test] + fn only_downloads_the_user_named_wait_out_a_stall() { + let _turn = turn(); + let handed = Arc::new(Mutex::new(Vec::new())); + let sent = handed.clone(); + set_fetcher_with_origin(move |url, ceiling, done, _| { + sent.lock().unwrap().push(gives_up_when_stalled(&url, ceiling)); + done(Err("not fetched".into())); + }); + let (tx, rx) = std::sync::mpsc::channel::<()>(); + let answered = || -> Done { + let tx = tx.clone(); + Box::new(move |_| { + let _ = tx.send(()); + }) + }; + let url = "https://pictures.example/a.png"; + download_public_limited(url, 1024, answered()); + download_limited(url, 1024, answered()); + download(url, answered()); + for _ in 0..3 { + rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + } + assert_eq!(*handed.lock().unwrap(), [true, false, false]); + assert!(gives_up_when_stalled("https://tiles.example/1/2/3.png", crate::address::Reach::Local)); + } + #[test] fn announced_tiff_header_lengths() { assert_eq!(tiff_header_length(" 16384 "), Some(16384)); diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 05394b366e..942d37deb0 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -29,7 +29,7 @@ unicode-normalization = "0.1" sys-locale = { version = "0.3", features = ["js"] } # Pictures kept with features (photo thumbnails), shown in the identify popup. -image = { workspace = true, features = ["jpeg"] } +image = { workspace = true, features = ["jpeg", "webp"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rfd = { workspace = true } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 59102e35e4..07d0413e58 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -66,6 +66,7 @@ mod panes; mod pluginpanel; mod pluginstart; mod pluginsui; +mod popupmedia; mod portalsui; #[cfg(not(target_arch = "wasm32"))] mod readjob; diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index f3888bfb41..332f34103c 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -332,6 +332,8 @@ pub struct MapView { /// The picture an identified feature carries (a photo's thumbnail, a `data:image/…` value): /// a hash of the value, and its texture (or why it couldn't be read). picture: Option<(u64, Result)>, + /// Pictures fetched from web addresses in features' values, by address, for the session. + pictures: crate::popupmedia::Pictures, /// The tile layers' credits, from the last render. attribution: Vec, /// Tile layer errors already reported. @@ -425,6 +427,7 @@ impl Default for MapView { snap: None, split: None, picture: None, + pictures: Default::default(), attribution: Vec::new(), tile_errors: std::collections::HashSet::new(), project: None, @@ -590,6 +593,9 @@ fn now(ui: &Ui) -> f64 { /// Shows the active map workspace, discarding stale node gestures after map-tab changes and /// before rendering their overlays or consuming pointer releases. pub fn show(app: &mut RustGisApp, ui: &mut Ui) { + // Read before the cache was cleared (a sign-out), a picture goes with it: checked on every + // frame, whatever is shown (a view with no map frame, a catalog or a layout included). + app.map.pictures.forget_if_cleared(rustgis_engine::render::rustgis_tiles::cache::clears()); app.map.follow_project(app.session.opened); let t = Tokens::get(ui.ctx()); crate::modelbuilder::follow_view(app); @@ -3588,6 +3594,22 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { // A picture value (a photo's thumbnail) shows as the picture, not as its text. let picture_key = attrs.iter().find(|(_, v)| v.as_str().is_some_and(is_picture)).map(|(k, _)| k.clone()); let picture = picture_key.and_then(|k| attrs.remove(&k)).and_then(|v| v.as_str().map(|s| picture_texture(app, ui.ctx(), s))); + let picture_is_data = picture.is_some(); + // Without one, a picture from a web address in the values (`image_url`, a `.jpg` address…): + // fetched as the pop-up opens only from RustGIS's own asset server, from anywhere else only + // once the user asks; kept for the session. Its address stays in the rows as a link. + let web_picture = if picture.is_none() { + crate::popupmedia::picture_link(attrs.iter().filter_map(|(k, v)| Some((k.as_str(), v.as_str()?)))).map(str::to_string) + } else { + None + }; + let picture = match (picture, &web_picture) { + (Some(Ok(tex)), _) => Some(crate::popupmedia::Shown::Ready(tex)), + (Some(Err(e)), _) => Some(crate::popupmedia::Shown::Failed(crate::popupmedia::Failure::Fetch(e))), + (None, Some(url)) => Some(app.map.pictures.show(ui.ctx(), url, crate::popupmedia::fetch)), + (None, None) => None, + }; + let mut load_picture = false; let title = if waiting { tl!("Asking the map service…").to_string() } else if info["raster"].as_bool() == Some(true) { @@ -3670,17 +3692,65 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { }); }); }); + let mut picture_h = 0.0; if let Some(picture) = &picture { - egui::Frame::NONE.inner_margin(egui::Margin { left: 12, right: 12, top: 8, bottom: 0 }).show(ui, |ui| match picture { - Ok(tex) => { - let size = tex.size_vec2(); - let scale = (ui.available_width() / size.x.max(1.0)).min(1.5); - ui.add(egui::Image::new((tex.id(), size * scale)).corner_radius(CornerRadius::same(6))); - } - Err(e) => { - ui.label(egui::RichText::new(e.as_str()).size(11.0).color(t.text_dim)); - } - }); + let shown = + egui::Frame::NONE.inner_margin(egui::Margin { left: 12, right: 12, top: 8, bottom: 0 }).show( + ui, + |ui| match picture { + crate::popupmedia::Shown::Ready(tex) => { + // As wide as the pop-up, up to half again its own size, and no taller + // than a thumbnail shown that way; one from the web leaves room for + // a few rows under it. + let size = tex.size_vec2(); + // Within the limit too, past the header and the rows' minimum. + let room = (limit.y - header.response.rect.height() - 40.0).max(40.0); + let base: f32 = if picture_is_data { 240.0 } else { 180.0 }; + let tallest = base.min(room); + let scale = (ui.available_width() / size.x.max(1.0)).min(tallest / size.y.max(1.0)).min(1.5); + ui.add( + egui::Image::new((tex.id(), size * scale)) + .corner_radius(CornerRadius::same(6)) + .alt_text(tl!("Picture")), + ); + } + // From another server: fetched only when asked, naming who + // would be asked. + crate::popupmedia::Shown::Offered => { + let host = web_picture.as_deref().and_then(crate::popupmedia::authority).unwrap_or_default(); + ui.horizontal_wrapped(|ui| { + let ask = ui.add(egui::Button::new(egui::RichText::new(tl!("Show Picture")).size(11.5))); + let tip = crate::i18n::fmt( + "Fetches the picture from {host}, which learns your IP address and that it was viewed", + &[("host", host)], + ); + if ask.on_hover_text(tip).clicked() { + load_picture = true; + } + ui.label( + egui::RichText::new(crate::i18n::fmt("from {host}", &[("host", host)])) + .size(11.0) + .color(t.text_dim), + ); + }); + } + crate::popupmedia::Shown::Loading => { + ui.horizontal(|ui| { + ui.spinner(); + ui.label(egui::RichText::new(tl!("Loading picture…")).size(11.0).color(t.text_dim)); + }); + } + // A picture kept in a value says why it can't be shown, as it always has. + crate::popupmedia::Shown::Failed(crate::popupmedia::Failure::Fetch(e)) if picture_is_data => { + ui.label(egui::RichText::new(e.as_str()).size(11.0).color(t.text_dim)); + } + crate::popupmedia::Shown::Failed(f) => { + ui.add(egui::Label::new(egui::RichText::new(f.line()).size(11.0).color(t.text_dim)).wrap()); + } + }, + ); + // The rows below take what the picture leaves of the height limit. + picture_h = shown.response.rect.height(); } // A solid scroll bar in its own gutter, shown when the rows are taller than the // limit (a floating one is invisible until hovered). @@ -3690,7 +3760,7 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { egui::ScrollArea::vertical() .id_salt(("popup_rows", key)) .auto_shrink([false, true]) - .max_height((limit.y - header.response.rect.height()).max(40.0)) + .max_height((limit.y - header.response.rect.height() - picture_h).max(40.0)) .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::VisibleWhenNeeded) .show(ui, |ui| { egui::Frame::NONE.inner_margin(egui::Margin { left: 12, right: 14, top: 6, bottom: 12 }).show(ui, |ui| { @@ -3708,7 +3778,24 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { // A fixed column: long values wrap, and the stripes span it. ui.set_min_width(value_w); ui.set_max_width(value_w); - ui.add(egui::Label::new(egui::RichText::new(v.as_str().unwrap_or("")).size(11.5)).wrap()); + let text = v.as_str().unwrap_or(""); + // A web address is a link that opens in the browser (a new + // tab on the web, so the map stays open). + if let Some(url) = crate::popupmedia::web_link(text) { + ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Wrap); + ui.add( + egui::Hyperlink::from_label_and_url(egui::RichText::new(url).size(11.5), url) + .open_in_new_tab(true), + ) + .context_menu(|ui| { + if ui.button(tl!("Copy Link")).clicked() { + ui.ctx().copy_text(url.to_string()); + ui.close(); + } + }); + } else { + ui.add(egui::Label::new(egui::RichText::new(text).size(11.5)).wrap()); + } }); ui.end_row(); } @@ -3734,6 +3821,10 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { if let Some(l) = new_limit { ui.ctx().data_mut(|d| d.insert_temp(limit_id, l)); } + if let (true, Some(url)) = (load_picture, &web_picture) { + app.map.pictures.accept(url, crate::popupmedia::fetch); + ui.ctx().request_repaint(); + } // Zooms to the feature shown, found by its id, as Zoom To Selection does: the selection and // the undo history are left as they are. if let (true, Some((id, fid))) = (zoom, shown) { @@ -3775,10 +3866,10 @@ fn select_shown(app: &mut RustGisApp) { app.session.revision = app.session.revision.wrapping_add(1); } -/// Whether an attribute value is a picture (a `data:image/png` or `jpeg` address, as a photo's -/// thumbnail is kept). +/// Whether an attribute value is a picture (a `data:image/jpeg`, `png` or `webp` address, as a +/// photo's thumbnail is kept). pub(crate) fn is_picture(v: &str) -> bool { - v.starts_with("data:image/jpeg;base64,") || v.starts_with("data:image/png;base64,") + ["data:image/jpeg;base64,", "data:image/png;base64,", "data:image/webp;base64,"].iter().any(|p| v.starts_with(p)) } /// The texture of a picture value, decoded once and kept while the same value is shown. @@ -3809,17 +3900,29 @@ fn picture_texture(app: &mut RustGisApp, ctx: &egui::Context, value: &str) -> Re decoded } +/// The largest picture the identify popup decodes, pixels a side. +pub(crate) const MAX_PICTURE_SIDE: u32 = 2048; +/// The most memory decoding a picture for the identify popup may take, in bytes. +pub(crate) const MAX_PICTURE_ALLOC: u64 = 64 << 20; + /// A picture kept with a feature, decoded for the identify popup. A project's value is hostile: /// its size is checked before anything is decoded, so a small compressed picture can't unpack /// into a huge one. pub(crate) fn decode_picture(bytes: &[u8]) -> Result { let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| e.to_string())?; let mut limits = image::Limits::default(); - limits.max_image_width = Some(2048); - limits.max_image_height = Some(2048); - limits.max_alloc = Some(64 << 20); + limits.max_image_width = Some(MAX_PICTURE_SIDE); + limits.max_image_height = Some(MAX_PICTURE_SIDE); + limits.max_alloc = Some(MAX_PICTURE_ALLOC); reader.limits(limits); - Ok(reader.decode().map_err(|_| "the picture is too large to show, or isn't one".to_string())?.to_rgba8()) + let bad = || "the picture is too large to show, or isn't one".to_string(); + // The decoder keeps pixels as stored: the picture is turned as its Orientation tag says (a + // phone's photo is often stored sideways). + let mut decoder = reader.into_decoder().map_err(|_| bad())?; + let orientation = image::ImageDecoder::orientation(&mut decoder).map_err(|_| bad())?; + let mut img = image::DynamicImage::from_decoder(decoder).map_err(|_| bad())?; + img.apply_orientation(orientation); + Ok(img.to_rgba8()) } /// The on-screen navigator: a north compass, zoom in and out, and four pan arrows. diff --git a/crates/ui-egui/src/popupmedia.rs b/crates/ui-egui/src/popupmedia.rs new file mode 100644 index 0000000000..8257621c82 --- /dev/null +++ b/crates/ui-egui/src/popupmedia.rs @@ -0,0 +1,829 @@ +//! The identify pop-up's web addresses: values that are links, and a picture fetched from one. +//! +//! A value that is an `http://` or `https://` address is a link. The pop-up shows a picture from +//! the `https://` address in a field named as one (`image`, `image_url`, `photo`, `photo_url`, +//! `picture`, `picture_url`, `thumbnail` or `thumbnail_url`, in any case, with or without the +//! `_`, `-` or space), or else from the first address whose path ends in `.jpg`, `.jpeg`, `.png` +//! or `.webp`, the fields taken in the order of their names. One that would be fetched +//! (`https://`, not on this computer or the local network) comes before one that wouldn't, so a +//! plain `http://` address doesn't hide an `https://` picture. +//! +//! Fetching a picture named by the data tells its server who opened the file and when, so only +//! RustGIS's own asset server ([`AUTO_HOST`], exactly, over `https://`: [`loads_by_itself`]) is +//! fetched as the pop-up opens. A redirect it answers with is handled as for any download: +//! followed natively, refused in the browser, which can't show where it leads. A +//! picture anywhere else shows a **Show Picture** button and nothing is fetched until it is +//! clicked. A plain `http://` picture, or one on this computer or the local network, is never +//! fetched; the pop-up says so. +//! +//! A picture is fetched through the tile cache's downloads (worker threads natively, the +//! browser's fetch on the web, where the browser's own HTTP cache applies), at most +//! [`MAX_PICTURE_BYTES`], and decoded with the limits of [`crate::mapview::decode_picture`], +//! its size checked before decoding. Each address's picture, or why there is none, is kept for +//! the session ([`MAX_PICTURES`] of them, the least recently shown dropped first), so it isn't +//! fetched again when the pop-up opens again, nor a failure every frame; all go when the tile +//! cache is cleared, as by a sign-out ([`Pictures::forget_if_cleared`]). + +use rustgis_engine::render::rustgis_tiles::{address, cache}; + +use crate::Slot; + +/// The longest value shown as a link, in bytes. +pub(crate) const MAX_LINK: usize = 2048; +/// The most bytes a picture fetched for the pop-up may have (as a picture kept in a value). +pub(crate) const MAX_PICTURE_BYTES: usize = 4 << 20; +/// The most pictures (or failures) kept, by address. +pub(crate) const MAX_PICTURES: usize = 16; +/// The longest side a fetched picture is kept at, in pixels: the pop-up shows it smaller. +const MAX_SIDE: u32 = 1024; + +/// Field names (lowercase, without `_`, `-` and spaces) whose web address is a picture. +const PICTURE_FIELDS: [&str; 8] = ["image", "imageurl", "photo", "photourl", "picture", "pictureurl", "thumbnail", "thumbnailurl"]; +/// Address path endings that are a picture, whatever the field is named. +const PICTURE_ENDINGS: [&str; 4] = [".jpg", ".jpeg", ".png", ".webp"]; + +/// `s` without `prefix`, matched in any case. +fn strip_prefix_ignore_case<'a>(s: &'a str, prefix: &str) -> Option<&'a str> { + let head = s.get(..prefix.len())?; + if head.eq_ignore_ascii_case(prefix) { s.get(prefix.len()..) } else { None } +} + +/// The scheme-less rest of an `http://` or `https://` address. +fn after_scheme(v: &str) -> Option<&str> { + strip_prefix_ignore_case(v, "https://").or_else(|| strip_prefix_ignore_case(v, "http://")) +} + +/// Whether an address's authority is a host name or an IP address (letters, digits, dots and +/// hyphens, or an IPv6 address in brackets), with a port of digits if any. +fn valid_authority(authority: &str) -> bool { + let (host_ok, port) = match authority.strip_prefix('[') { + Some(v6) => match v6.split_once(']') { + Some((h, rest)) => (!h.is_empty() && h.chars().all(|c| c.is_ascii_hexdigit() || c == ':' || c == '.'), rest), + None => return false, + }, + None => { + let (h, rest) = authority.find(':').map_or((authority, ""), |i| authority.split_at(i)); + let ok = !h.is_empty() && !h.starts_with(['.', '-']) && h.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-'); + (ok, rest) + } + }; + let port_ok = match port.strip_prefix(':') { + Some(p) => (1..=5).contains(&p.len()) && p.chars().all(|c| c.is_ascii_digit()), + None => port.is_empty(), + }; + host_ok && port_ok +} + +/// `v` as a web address the pop-up links to: `http://` or `https://` (in any case), a host +/// name or IP address, and nothing that can't be in an address (spaces, control characters, +/// quotes, angle brackets, backslashes), at most [`MAX_LINK`] bytes. A user name before the host +/// (`https://bank.example@other.example/`) passes for another site, so it is text too. +pub(crate) fn web_link(v: &str) -> Option<&str> { + if v.len() > MAX_LINK { + return None; + } + let rest = after_scheme(v)?; + if v.chars().any(|c| c.is_whitespace() || c.is_control() || matches!(c, '"' | '<' | '>' | '\\' | '`' | '{' | '}' | '^')) { + return None; + } + let authority = rest.split(['/', '?', '#']).next().unwrap_or_default(); + (!authority.contains('@') && valid_authority(authority)).then_some(v) +} + +/// Whether a field is named as a picture's address (`image_url`, `Photo`, `thumbnail`…); not +/// `image_page_url` or `image_credit`. +pub(crate) fn picture_field(name: &str) -> bool { + if name.len() > 64 { + return false; + } + let plain: String = name.chars().filter(|c| !matches!(c, '_' | '-' | ' ')).flat_map(char::to_lowercase).collect(); + PICTURE_FIELDS.contains(&plain.as_str()) +} + +/// Whether a web address's path (not its host, query or fragment) ends as a picture file does. +pub(crate) fn picture_path(url: &str) -> bool { + let Some(rest) = after_scheme(url) else { return false }; + // The query and fragment go first: a slash in them (`?next=/a.jpg`) doesn't start a path. + let rest = rest.split(['?', '#']).next().unwrap_or_default(); + let Some((_, path)) = rest.split_once('/') else { return false }; + PICTURE_ENDINGS + .iter() + .any(|end| path.len() > end.len() && path.get(path.len() - end.len()..).is_some_and(|t| t.eq_ignore_ascii_case(end))) +} + +/// The web address the pop-up shows a picture from, among a feature's text values (name, +/// value): a link in a picture field first, else the first link whose path is a picture file's; +/// and before either, one that would be fetched ([`refusal`] has nothing against it), so a plain +/// `http://` or local address is chosen (to say why it isn't fetched) only when no other is. +pub(crate) fn picture_link<'a>(attrs: impl Iterator + Clone) -> Option<&'a str> { + // Each candidate: whether its field is named as a picture's, and its address. + let candidates = attrs.filter_map(|(k, v)| Some((picture_field(k), web_link(v)?))).filter(|(named, v)| *named || picture_path(v)); + let first = |named: bool, fetched: bool| candidates.clone().find(|(n, v)| *n == named && (!fetched || refusal(v).is_none())); + first(true, true).or_else(|| first(false, true)).or_else(|| first(true, false)).or_else(|| first(false, false)).map(|(_, v)| v) +} + +/// The one host whose pictures the pop-up fetches without being asked: RustGIS's own asset +/// server, which the project runs and which serves the sample data's pictures. +pub(crate) const AUTO_HOST: &str = "assets.rustgis.app"; + +/// The authority (host and port) of a web address the pop-up links to, as written. +pub(crate) fn authority(url: &str) -> Option<&str> { + let rest = after_scheme(web_link(url)?)?; + rest.split(['/', '?', '#']).next() +} + +/// Whether the pop-up fetches the picture at `url` as soon as it shows it: an `https://` address +/// (the scheme in any case) whose authority is exactly [`AUTO_HOST`], compared without regard to +/// case as host names are. Nothing else passes: no port (not even `:443`), no user name (which +/// [`web_link`] refuses), no subdomain or longer name (`assets.rustgis.app.example.com`), no +/// trailing dot, no percent-encoded or non-ASCII host (neither is a link at all), no `http://`. +pub(crate) fn loads_by_itself(url: &str) -> bool { + strip_prefix_ignore_case(url, "https://").is_some() && authority(url).is_some_and(|a| a.eq_ignore_ascii_case(AUTO_HOST)) +} + +/// The largest picture decoded, pixels a side (as [`crate::mapview::decode_picture`] takes). +const MAX_DECODED_SIDE: u32 = crate::mapview::MAX_PICTURE_SIDE; + +/// Why there is no picture to show. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Failure { + /// The fetch failed: the network, the server's answer, the browser's CORS check, its size + /// in bytes (the fetcher stops reading past [`MAX_PICTURE_BYTES`]). + Fetch(String), + /// What came back isn't a picture that decodes. + NotPicture, + /// A picture in a format that isn't read here (its name, such as `BMP` or `TIFF`). + Unsupported(String), + /// A picture of more pixels than are decoded (width, height), refused before decoding. + TooLarge(u32, u32), + /// Only `https://` pictures are fetched. + NotHttps, + /// Addresses on this computer or the local network aren't fetched for a value. + Local, + /// Link-local and unspecified addresses, and cloud metadata hosts, are never fetched. + Forbidden, +} + +impl Failure { + /// The line the pop-up shows: "Picture unavailable: …". + pub(crate) fn line(&self) -> String { + match self { + Failure::Fetch(e) => { + let cut = e.chars().count() > 200; + let reason: String = e.chars().take(200).chain(cut.then_some('…')).collect(); + crate::i18n::fmt("Picture unavailable: {reason}", &[("reason", &reason)]) + } + Failure::NotPicture => crate::i18n::fmt("Picture unavailable: not a picture", &[]), + Failure::Unsupported(format) => { + crate::i18n::fmt("Picture unavailable: {format} pictures aren't supported", &[("format", format)]) + } + Failure::TooLarge(w, h) => crate::i18n::fmt( + "Picture unavailable: {w} × {h} pixels is larger than {max} × {max}", + &[("w", &w.to_string()), ("h", &h.to_string()), ("max", &MAX_DECODED_SIDE.to_string())], + ), + Failure::NotHttps => crate::i18n::fmt("Picture unavailable: only https:// pictures are fetched", &[]), + Failure::Local => crate::i18n::fmt("Picture unavailable: pictures aren't fetched from this computer or the local network", &[]), + Failure::Forbidden => crate::i18n::fmt("Picture unavailable: that address is never fetched", &[]), + } + } +} + +/// Why the picture at a web address won't be fetched at all, even when asked, if it won't. +fn refusal(url: &str) -> Option { + if strip_prefix_ignore_case(url, "https://").is_none() { + Some(Failure::NotHttps) + } else { + match address::reach(url) { + address::Reach::Public => None, + address::Reach::Local => Some(Failure::Local), + address::Reach::Forbidden => Some(Failure::Forbidden), + } + } +} + +/// Where a fetched picture lands, decoded off the frame (natively). +pub(crate) type Landing = Slot>; + +/// A picture by address: being fetched, shown, or not to be had. +enum Entry { + Loading(Landing), + Ready(egui::TextureHandle), + Failed(Failure), +} + +/// What the pop-up shows for a picture address. +#[derive(Clone)] +pub(crate) enum Shown { + /// Not fetched: a picture from another server, fetched only when the user asks. + Offered, + Loading, + Ready(egui::TextureHandle), + Failed(Failure), +} + +/// The pictures fetched for the pop-up this session, the least recently shown first. +#[derive(Default)] +pub(crate) struct Pictures { + entries: Vec<(String, Entry)>, + /// [`cache::clears`] when [`Self::forget_if_cleared`] last looked. + clears: u64, +} + +impl Pictures { + /// Forgets every picture (those being fetched too) when the tile cache was cleared since this + /// was last called, `clears` being [`cache::clears`]: a sign-out or a login that ends clears + /// it, and a picture may have been read with that login, so it goes with the cache's tiles. + pub(crate) fn forget_if_cleared(&mut self, clears: u64) { + if clears != self.clears { + self.entries.clear(); + self.clears = clears; + } + } + + /// The picture at `url`. The first time it is asked for (or after it was dropped to make + /// room), one from [`AUTO_HOST`] is started with `fetch`; any other is [`Shown::Offered`], + /// not fetched, until [`Self::accept`]. A refused or failed one is kept like a picture, so + /// it isn't fetched again. + pub(crate) fn show(&mut self, ctx: &egui::Context, url: &str, fetch: impl FnOnce(&str, Landing)) -> Shown { + let (url, mut entry) = match self.entries.iter().position(|(u, _)| u == url) { + Some(i) => self.entries.remove(i), + None => match refusal(url) { + Some(f) => self.make_room(url, Entry::Failed(f)), + None if loads_by_itself(url) => self.make_room(url, start(url, fetch)), + None => return Shown::Offered, + }, + }; + let landed = match &entry { + Entry::Loading(landing) => landing.lock().unwrap_or_else(|e| e.into_inner()).take(), + _ => None, + }; + if let Some(r) = landed { + entry = match r { + Ok(img) => { + let size = [img.width() as usize, img.height() as usize]; + let color = egui::ColorImage::from_rgba_unmultiplied(size, img.as_raw()); + Entry::Ready(ctx.load_texture(format!("identify_picture:{url}"), color, egui::TextureOptions::LINEAR)) + } + Err(f) => Entry::Failed(f), + }; + } + let shown = match &entry { + Entry::Loading(_) => { + ctx.request_repaint_after(std::time::Duration::from_millis(150)); + Shown::Loading + } + Entry::Ready(t) => Shown::Ready(t.clone()), + Entry::Failed(f) => Shown::Failed(f.clone()), + }; + self.entries.push((url, entry)); + shown + } + + /// The user asked to see the picture at `url`: it is started with `fetch` unless it is + /// already kept (fetched, being fetched or failed) or is never fetched ([`refusal`]). + pub(crate) fn accept(&mut self, url: &str, fetch: impl FnOnce(&str, Landing)) { + if self.entries.iter().any(|(u, _)| u == url) { + return; + } + let entry = match refusal(url) { + Some(f) => Entry::Failed(f), + None => start(url, fetch), + }; + let kept = self.make_room(url, entry); + self.entries.push(kept); + } + + /// Room for one more address, the least recently shown dropped; the new one, to push. + fn make_room(&mut self, url: &str, entry: Entry) -> (String, Entry) { + if self.entries.len() >= MAX_PICTURES { + self.entries.remove(0); + } + (url.to_string(), entry) + } + + /// How many addresses are kept. + #[cfg(test)] + pub(crate) fn len(&self) -> usize { + self.entries.len() + } +} + +/// Starts fetching the picture at `url` with `fetch`. +fn start(url: &str, fetch: impl FnOnce(&str, Landing)) -> Entry { + let landing = Landing::default(); + fetch(url, landing.clone()); + Entry::Loading(landing) +} + +/// A reader of a fetched picture's header, allowed the memory [`crate::mapview::decode_picture`] +/// allows ([`crate::mapview::MAX_PICTURE_ALLOC`]): reading a PNG's header unpacks its colour +/// profile, which a few megabytes can make hundreds of megabytes. Not its pixel limits, so a +/// picture past them is still measured, and refused saying how large it is. +fn header(bytes: &[u8]) -> Result>, Failure> { + let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|_| Failure::NotPicture)?; + let mut limits = image::Limits::default(); + limits.max_alloc = Some(crate::mapview::MAX_PICTURE_ALLOC); + reader.limits(limits); + Ok(reader) +} + +/// Decodes a fetched picture for the pop-up: refused when it isn't one, or when its header says +/// it is larger than [`crate::mapview::decode_picture`] takes (before anything is decoded, so a +/// small file can't unpack into a huge picture), and kept at most [`MAX_SIDE`] pixels a side. +pub(crate) fn decode(bytes: &[u8]) -> Result { + let header = header(bytes)?; + let Some(format) = header.format() else { return Err(Failure::NotPicture) }; + // A picture whose format has no decoder here says so, rather than that it isn't one. + let (w, h) = header.into_dimensions().map_err(|e| match e { + image::ImageError::Unsupported(u) if matches!(u.kind(), image::error::UnsupportedErrorKind::Format(_)) => { + Failure::Unsupported(format!("{format:?}").to_uppercase()) + } + _ => Failure::NotPicture, + })?; + if w > MAX_DECODED_SIDE || h > MAX_DECODED_SIDE { + return Err(Failure::TooLarge(w, h)); + } + let img = crate::mapview::decode_picture(bytes).map_err(|_| Failure::NotPicture)?; + let (w, h) = (img.width().max(1), img.height().max(1)); + if w.max(h) <= MAX_SIDE { + return Ok(img); + } + let k = MAX_SIDE as f32 / w.max(h) as f32; + let (tw, th) = (((w as f32 * k) as u32).clamp(1, MAX_SIDE), ((h as f32 * k) as u32).clamp(1, MAX_SIDE)); + Ok(image::imageops::thumbnail(&img, tw, th)) +} + +/// Fetches a picture for the pop-up through the tile cache's downloads (the browser's fetch on +/// the web), at most [`MAX_PICTURE_BYTES`], decoding it where it lands (off the frame natively). +/// Natively it connects only to internet addresses: a public-looking name, or a redirect, that +/// resolves to this computer or the local network is refused where it resolves, past what +/// [`refusal`] can tell from the address. +pub(crate) fn fetch(url: &str, landing: Landing) { + // The scheme in lowercase, as the downloads compare it: a redirect from `HTTPS://` down to + // `http://` is refused as one from `https://` is. + let url = match url.split_once("://") { + Some((scheme, rest)) => format!("{}://{rest}", scheme.to_ascii_lowercase()), + None => url.to_string(), + }; + cache::download_public_limited( + &url, + MAX_PICTURE_BYTES, + Box::new(move |r| { + let r = r.map_err(Failure::Fetch).and_then(|bytes| decode(&bytes)); + *landing.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); + }), + ); +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn links_are_http_and_https_addresses_only() { + for good in [ + "https://example.org", + "https://example.org/", + "http://example.org/a/b?c=d&e=f#g", + "HTTPS://EXAMPLE.ORG/PATH", + "HtTp://example.org", + "https://example.org:8443/x", + "https://[2001:db8::1]:443/x", + "https://192.0.2.1/x", + "https://de.wikipedia.org/wiki/Zürich", + ] { + assert_eq!(web_link(good), Some(good), "{good}"); + } + for bad in [ + "", + "example.org", + "www.example.org/page", + "javascript:alert(1)", + "JavaScript://example.org/%0aalert(1)", + "data:text/html,hi", + "data:image/png;base64,iVBORw0KGgo=", + "file:///etc/passwd", + "ftp://example.org/x", + "mailto:someone@example.org", + "https://", + "https:///path", + "https://exa mple.org", + "https://example.org/a b", + " https://example.org", + "https://example.org ", + "https://example.org/\n", + "https://example.org/\u{0}", + "https://example.org/\"onmouseover=\"x", + "https://example.org/