| 1 | //! The renderer's only "network": `data:` URLs decoded in process. Anything |
| 2 | //! else is refused and reported, which is the renderer-side half of invariant 4 |
| 3 | //! (the validator already strips every other URL before an op gets here). |
| 4 | |
| 5 | use blitz_traits::net::{Bytes, NetHandler, NetProvider, Request}; |
| 6 | use std::sync::Mutex; |
| 7 | |
| 8 | pub struct DataOnlyNet { |
| 9 | /// Refused URLs (scheme + first 80 chars), drained into `url-blocked` diagnostics. |
| 10 | pub blocked: Mutex<Vec<String>>, |
| 11 | } |
| 12 | |
| 13 | impl DataOnlyNet { |
| 14 | pub fn new() -> Self { |
| 15 | Self { blocked: Mutex::new(Vec::new()) } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl NetProvider for DataOnlyNet { |
| 20 | fn fetch(&self, _doc_id: usize, request: Request, handler: Box<dyn NetHandler>) { |
| 21 | let url = request.url.as_str().to_string(); |
| 22 | if request.url.scheme() == "data" { |
| 23 | if let Ok(data_url) = data_url::DataUrl::process(&url) { |
| 24 | if let Ok((body, _fragment)) = data_url.decode_to_vec() { |
| 25 | handler.bytes(url, Bytes::from(body)); |
| 26 | } |
| 27 | } |
| 28 | return; |
| 29 | } |
| 30 | let mut blocked = self.blocked.lock().unwrap(); |
| 31 | if blocked.len() < 50 { |
| 32 | let mut shown: String = url.chars().take(80).collect(); |
| 33 | if url.len() > 80 { |
| 34 | shown.push('…'); |
| 35 | } |
| 36 | blocked.push(shown); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |