|
| 1 | +//! 整页静态快照(v0.20.4 / REQ-305——自研 core 内联,规避 SingleFile AGPL)。 |
| 2 | +//! |
| 3 | +//! @ai-context: 渲染型整页 DOM+子资源内联需要隐藏 wry 窗口(tauri WebView2 |
| 4 | +//! 无 MHTML API——已否决,Foresight §二);本模块提供**静态降级档** |
| 5 | +//! (monolith CC0 同思路自研:纯函数规则内联 <link>/<img>/样式块, |
| 6 | +//! 外链脚本剔除防快照 XSS——快照是可离线查看的文档存档,不执行 |
| 7 | +//! 原文 JS)。渲染档(wry eval 注入)与截图兜底链登记后置。 |
| 8 | +//! @ai-context: 纯函数 + 注入 resolver(测试传假数据;生产=ureq 拉取), |
| 9 | +//! 相对 URL 按页面 base 解析;资源数量/单个体量/总预算护栏。 |
| 10 | +
|
| 11 | +/// 资源解析器:绝对 URL → data URI(None=拉取失败,保留原引用降级)。 |
| 12 | +pub type Resolver<'a> = &'a mut dyn FnMut(&str) -> Option<String>; |
| 13 | + |
| 14 | +/// 相对引用按 base 解析(纯函数;仅 http(s)/同页锚/相对路径)。 |
| 15 | +pub fn resolve_url(base: &str, href: &str) -> Option<String> { |
| 16 | + let href = href.trim(); |
| 17 | + if href.is_empty() { |
| 18 | + return None; |
| 19 | + } |
| 20 | + if href.starts_with("data:") || href.starts_with('#') { |
| 21 | + return Some(href.to_string()); |
| 22 | + } |
| 23 | + if href.starts_with("http://") || href.starts_with("https://") { |
| 24 | + return Some(href.to_string()); |
| 25 | + } |
| 26 | + if href.starts_with("//") { |
| 27 | + return Some(format!("https:{}", href)); // 协议相对 |
| 28 | + } |
| 29 | + let scheme_split = base.find("://")?; |
| 30 | + let scheme_end = scheme_split + 3; |
| 31 | + let rest = &base[scheme_end..]; |
| 32 | + let authority = rest.split('/').next().unwrap_or(""); |
| 33 | + if href.starts_with('/') { |
| 34 | + return Some(format!("{}://{}{}", &base[..scheme_split], authority, href)); |
| 35 | + } |
| 36 | + // 目录相对:取 base 目录(最后一个 / 之前,保留 authority) |
| 37 | + let dir = match rest.rfind('/') { |
| 38 | + Some(i) => rest[..=i].to_string(), |
| 39 | + None => format!("{}/", rest), |
| 40 | + }; |
| 41 | + Some(format!("{}://{}{}", &base[..scheme_split], dir, href)) |
| 42 | +} |
| 43 | + |
| 44 | +/// 内联单资源占位(style/img 空档——防空串替换破坏结构)。 |
| 45 | +fn data_or_keep( |
| 46 | + kind: &str, |
| 47 | + resolved: Option<String>, |
| 48 | + resolver: &mut dyn FnMut(&str) -> Option<String>, |
| 49 | +) -> Option<String> { |
| 50 | + let url = resolved?; |
| 51 | + let data = resolver(&url)?; |
| 52 | + let mime = kind_mime(kind, &url); |
| 53 | + Some(format!("data:{};base64,{}", mime, data)) |
| 54 | +} |
| 55 | + |
| 56 | +fn kind_mime(kind: &str, url: &str) -> String { |
| 57 | + if kind == "style" { |
| 58 | + return "text/css".to_string(); |
| 59 | + } |
| 60 | + let lower = url.to_ascii_lowercase(); |
| 61 | + if lower.contains(".png") || lower.contains(".apng") { |
| 62 | + "image/png".to_string() |
| 63 | + } else if lower.contains(".webp") { |
| 64 | + "image/webp".to_string() |
| 65 | + } else if lower.contains(".svg") { |
| 66 | + "image/svg+xml".to_string() |
| 67 | + } else if lower.contains(".gif") { |
| 68 | + "image/gif".to_string() |
| 69 | + } else { |
| 70 | + "image/jpeg".to_string() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// HTML → 静态内联快照(纯函数;返回内联后 HTML)。 |
| 75 | +/// |
| 76 | +/// @ai-context: 处理 <link rel=stylesheet href>、<img src>、外链 <script src> |
| 77 | +/// (剔除并注释——不执行原文脚本,快照 XSS 面归零); |
| 78 | +/// 行内 <style>/<script> 保持原文(行内 script 属原文内容, |
| 79 | +/// 快照只存不开——由打开方语境保证)。 |
| 80 | +pub fn inline_html(base: &str, html: &str, resolver: Resolver) -> String { |
| 81 | + let mut out = String::new(); |
| 82 | + let mut rest = html; |
| 83 | + while let Some(pos) = rest.find('<') { |
| 84 | + out.push_str(&rest[..pos]); |
| 85 | + rest = &rest[pos..]; |
| 86 | + let end = rest.find('>').map(|e| e + 1).unwrap_or(rest.len()); |
| 87 | + let tag = &rest[..end]; |
| 88 | + rest = &rest[end..]; |
| 89 | + let lower = tag.to_ascii_lowercase(); |
| 90 | + if lower.starts_with("<link") { |
| 91 | + if let Some(href) = extract_attr(tag, "href") { |
| 92 | + if lower.contains("stylesheet") { |
| 93 | + if let Some(data) = data_or_keep("style", resolve_url(base, &href), &mut *resolver) { |
| 94 | + out.push_str(&format!("<style data-inlined=\"{}\">{}</style>", escape_attr(&href), data)); |
| 95 | + continue; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + out.push_str(tag); |
| 100 | + } else if lower.starts_with("<img") { |
| 101 | + if let Some(src) = extract_attr(tag, "src") { |
| 102 | + let data = data_or_keep("img", resolve_url(base, &src), &mut *resolver); |
| 103 | + if let Some(d) = data { |
| 104 | + let replaced = replace_attr(tag, "src", &d); |
| 105 | + out.push_str(&replaced); |
| 106 | + continue; |
| 107 | + } |
| 108 | + } |
| 109 | + out.push_str(tag); |
| 110 | + } else if lower.starts_with("<script") && tag.contains("src=") { |
| 111 | + // 外链脚本剔除(防快照执行第三方 JS——只存档不执行),连同闭合标签 |
| 112 | + out.push_str("<!-- entropy-snapshot: external script removed -->"); |
| 113 | + let lower_rest = rest.to_ascii_lowercase(); |
| 114 | + if let Some(close_idx) = lower_rest.find("</script") { |
| 115 | + if let Some(gt) = rest[close_idx..].find('>') { |
| 116 | + rest = &rest[close_idx + gt + 1..]; |
| 117 | + continue; |
| 118 | + } |
| 119 | + } |
| 120 | + // 无闭合标签的畸形脚本:继续正常扫描 |
| 121 | + } else { |
| 122 | + out.push_str(tag); |
| 123 | + } |
| 124 | + } |
| 125 | + out.push_str(rest); |
| 126 | + out |
| 127 | +} |
| 128 | + |
| 129 | +fn extract_attr(tag: &str, name: &str) -> Option<String> { |
| 130 | + let mut search = tag; |
| 131 | + while let Some(p) = search.find(name) { |
| 132 | + let after = &search[p + name.len()..]; |
| 133 | + let after = after.trim_start(); |
| 134 | + if let Some(after_eq) = after.strip_prefix('=') { |
| 135 | + let v = after_eq.trim_start(); |
| 136 | + let value = if let Some(q) = v.strip_prefix('"') { |
| 137 | + q.split('"').next().unwrap_or("").to_string() |
| 138 | + } else if let Some(q) = v.strip_prefix('\'') { |
| 139 | + q.split('\'').next().unwrap_or("").to_string() |
| 140 | + } else { |
| 141 | + v.split_whitespace().next().unwrap_or("").to_string() |
| 142 | + }; |
| 143 | + return Some(value); |
| 144 | + } |
| 145 | + search = after; |
| 146 | + } |
| 147 | + None |
| 148 | +} |
| 149 | + |
| 150 | +fn replace_attr(tag: &str, name: &str, new_value: &str) -> String { |
| 151 | + if let Some(p) = tag.find(name) { |
| 152 | + let after = &tag[p + name.len()..]; |
| 153 | + let after = after.trim_start(); |
| 154 | + if let Some(after_eq) = after.strip_prefix('=') { |
| 155 | + let v = after_eq.trim_start(); |
| 156 | + if let Some(first) = v.chars().next() { |
| 157 | + if first == '"' || first == '\'' { |
| 158 | + let rest = &v[first.len_utf8()..]; |
| 159 | + if let Some(end) = rest.find(first) { |
| 160 | + let head = format!("{}{}={}", &tag[..p], name, first); |
| 161 | + let tail = &rest[end..]; |
| 162 | + return format!("{}{}{}", head, new_value, tail); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + tag.to_string() |
| 169 | +} |
| 170 | + |
| 171 | +fn escape_attr(s: &str) -> String { |
| 172 | + s.replace('"', """) |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +#[path = "web_snapshot_tests.rs"] |
| 177 | +mod tests; |
0 commit comments