SPB Git

Repository Public

LinkPreview.swift 12.8 KB 236 lines swift

1import Foundation23/// What AURA Peek shows about a link before it is opened.4struct LinkPreview: Equatable, Sendable {5    enum Content: String, Sendable { case page, image, pdf, other }6    var url: URL7    /// Where the link really leads after redirects.8    var finalURL: URL9    var title: String = ""10    var summary: String = ""11    var siteName: String = ""12    var imageURL: URL?13    /// A short excerpt of the page's own text, when the server returned HTML.14    var excerpt: String = ""15    var content: Content = .page1617    var host: String { finalURL.host ?? url.host ?? "" }18    var wasRedirectedToAnotherSite: Bool {19        guard let a = url.host, let b = finalURL.host else { return false }20        return URLInspector.registrableDomain(of: a) != URLInspector.registrableDomain(of: b)21    }22}2324/// Reads title, description, image and a text excerpt out of an HTML document without executing anything.25enum LinkPreviewParser {26    static func parse(html: String, url: URL, finalURL: URL) -> LinkPreview {27        var preview = LinkPreview(url: url, finalURL: finalURL)28        let head = String(html.prefix(200_000))29        var meta: [String: String] = [:]30        for tag in matches(#"<meta\b[^>]*>"#, in: head) {31            let attributes = self.attributes(of: tag)32            guard let content = attributes["content"], !content.isEmpty else { continue }33            if let key = (attributes["property"] ?? attributes["name"] ?? attributes["itemprop"])?.lowercased(), meta[key] == nil { meta[key] = content }34        }35        let titleTag = firstGroup(#"<title\b[^>]*>([\s\S]*?)</title>"#, in: head)36        preview.title = clean(meta["og:title"] ?? meta["twitter:title"] ?? titleTag ?? "", limit: 200)37        preview.summary = clean(meta["og:description"] ?? meta["description"] ?? meta["twitter:description"] ?? "", limit: 400)38        preview.siteName = clean(meta["og:site_name"] ?? meta["application-name"] ?? "", limit: 80)39        if let image = meta["og:image"] ?? meta["og:image:url"] ?? meta["twitter:image"] ?? meta["twitter:image:src"],40           let resolved = URL(string: decodeEntities(image).trimmingCharacters(in: .whitespaces), relativeTo: finalURL)?.absoluteURL,41           let scheme = resolved.scheme?.lowercased(), scheme == "https" || scheme == "http" {42            preview.imageURL = resolved43        }44        preview.excerpt = excerpt(from: html)45        return preview46    }4748    /// The first real paragraphs of the body, as plain text.49    static func excerpt(from html: String, limit: Int = 520) -> String {50        var body = String(html.prefix(600_000))51        if let range = body.range(of: "<body", options: .caseInsensitive) { body = String(body[range.lowerBound...]) }52        for pattern in [#"<script\b[\s\S]*?</script>"#, #"<style\b[\s\S]*?</style>"#, #"<noscript\b[\s\S]*?</noscript>"#, #"<nav\b[\s\S]*?</nav>"#,53                        #"<header\b[\s\S]*?</header>"#, #"<footer\b[\s\S]*?</footer>"#, #"<!--[\s\S]*?-->"#] {54            body = body.replacingOccurrences(of: pattern, with: " ", options: [.regularExpression, .caseInsensitive])55        }56        var parts: [String] = []57        var total = 058        for paragraph in matches(#"<p\b[^>]*>([\s\S]*?)</p>"#, in: body, group: 1) {59            let text = clean(paragraph.replacingOccurrences(of: #"<[^>]+>"#, with: " ", options: .regularExpression), limit: limit)60            guard text.count >= 60 else { continue }61            parts.append(text)62            total += text.count63            if total >= limit || parts.count >= 3 { break }64        }65        let joined = parts.joined(separator: "\n\n")66        return joined.count > limit ? String(joined.prefix(limit)).trimmingCharacters(in: .whitespaces) + "…" : joined67    }6869    static func attributes(of tag: String) -> [String: String] {70        var out: [String: String] = [:]71        guard let regex = try? NSRegularExpression(pattern: #"([a-zA-Z:_-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))"#) else { return out }72        let ns = tag as NSString73        for match in regex.matches(in: tag, range: NSRange(location: 0, length: ns.length)) {74            let name = ns.substring(with: match.range(at: 1)).lowercased()75            for group in 2...4 where match.range(at: group).location != NSNotFound {76                if out[name] == nil { out[name] = ns.substring(with: match.range(at: group)) }77                break78            }79        }80        return out81    }8283    static func clean(_ text: String, limit: Int) -> String {84        let decoded = decodeEntities(text).replacingOccurrences(of: #"\s+"#, with: " ", options: .regularExpression).trimmingCharacters(in: .whitespacesAndNewlines)85        return decoded.count > limit ? String(decoded.prefix(limit)).trimmingCharacters(in: .whitespaces) + "…" : decoded86    }8788    static func decodeEntities(_ text: String) -> String {89        guard text.contains("&") else { return text }90        var out = text91        for (entity, value) in [("&amp;", "&"), ("&lt;", "<"), ("&gt;", ">"), ("&quot;", "\""), ("&#39;", "'"), ("&apos;", "'"), ("&nbsp;", " "),92                                ("&rsquo;", "’"), ("&lsquo;", "‘"), ("&rdquo;", "”"), ("&ldquo;", "“"), ("&hellip;", "…"), ("&mdash;", "—"), ("&ndash;", "–"),93                                ("&eacute;", "é"), ("&egrave;", "è"), ("&agrave;", "à"), ("&ccedil;", "ç"), ("&ecirc;", "ê"), ("&laquo;", "«"), ("&raquo;", "»")] {94            out = out.replacingOccurrences(of: entity, with: value)95        }96        guard out.contains("&#"), let regex = try? NSRegularExpression(pattern: #"&#(x[0-9a-fA-F]{1,6}|[0-9]{1,7});"#) else { return out }97        let ns = out as NSString98        var result = ""99        var last = 0100        for match in regex.matches(in: out, range: NSRange(location: 0, length: ns.length)) {101            result += ns.substring(with: NSRange(location: last, length: match.range.location - last))102            let code = ns.substring(with: match.range(at: 1))103            let value = code.hasPrefix("x") ? UInt32(code.dropFirst(), radix: 16) : UInt32(code)104            if let value, let scalar = Unicode.Scalar(value), value != 0 { result.unicodeScalars.append(scalar) }105            last = match.range.location + match.range.length106        }107        return result + ns.substring(from: last)108    }109110    private static func matches(_ pattern: String, in text: String, group: Int = 0) -> [String] {111        guard let regex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) else { return [] }112        let ns = text as NSString113        return regex.matches(in: text, range: NSRange(location: 0, length: ns.length)).compactMap {114            $0.range(at: group).location == NSNotFound ? nil : ns.substring(with: $0.range(at: group))115        }116    }117118    private static func firstGroup(_ pattern: String, in text: String) -> String? { matches(pattern, in: text, group: 1).first }119}120121enum LinkPreviewError: Error, Equatable { case notAllowed, tooManyRedirects, failed }122123/// Rules for what Peek may fetch.124enum LinkPreviewPolicy {125    /// Only public http(s) addresses are previewed. A page must not be able to make the browser probe the local network,126    /// so loopback, private and link-local hosts are refused unless the page asking is itself local.127    static func isAllowed(_ url: URL, sourceIsLocal: Bool = false) -> Bool {128        guard let scheme = url.scheme?.lowercased(), scheme == "https" || scheme == "http", let host = url.host?.lowercased(), !host.isEmpty,129              url.user == nil, url.password == nil else { return false }130        return sourceIsLocal || !isLocalHost(host)131    }132133    static func isLocalHost(_ host: String) -> Bool {134        let h = host.trimmingCharacters(in: CharacterSet(charactersIn: "[]"))135        if h == "localhost" || h.hasSuffix(".localhost") || h.hasSuffix(".local") || h.hasSuffix(".internal") || h.hasSuffix(".lan") || h.hasSuffix(".home.arpa") { return true }136        if !h.contains(".") && !h.contains(":") { return true }137        if h.contains(":") {138            return h == "::1" || h == "::" || h.hasPrefix("fe80:") || h.hasPrefix("fc") || h.hasPrefix("fd") || h.hasPrefix("::ffff:")139        }140        let parts = h.split(separator: ".").compactMap { Int($0) }141        guard parts.count == 4, h.split(separator: ".").count == 4 else { return false }142        switch (parts[0], parts[1]) {143        case (10, _), (127, _), (0, _), (169, 254), (192, 168): return true144        case (172, 16...31), (100, 64...127): return true145        default: return parts[0] >= 224146        }147    }148}149150/// Fetches previews prudently: one plain GET, no cookies, no credentials, no referrer, no script execution, a size cap and a short timeout.151/// Results are cached in memory only, and never for private browsing.152actor LinkPreviewLoader {153    private var cache: [URL: LinkPreview] = [:]154    private var order: [URL] = []155    private let session: URLSession156    private let redirects = RedirectGuard()157    static let maxBytes = 600_000158159    init(configuration: URLSessionConfiguration? = nil) {160        let config = configuration ?? .ephemeral161        config.httpShouldSetCookies = false162        config.httpCookieAcceptPolicy = .never163        config.httpCookieStorage = nil164        config.urlCredentialStorage = nil165        config.urlCache = nil166        config.timeoutIntervalForRequest = 8167        config.timeoutIntervalForResource = 12168        config.httpAdditionalHeaders = ["Accept": "text/html,application/xhtml+xml;q=0.9,*/*;q=0.5"]169        session = URLSession(configuration: config, delegate: redirects, delegateQueue: nil)170    }171172    func cached(_ url: URL) -> LinkPreview? { cache[url] }173174    func preview(for url: URL, isPrivate: Bool, sourceIsLocal: Bool = false) async throws -> LinkPreview {175        guard LinkPreviewPolicy.isAllowed(url, sourceIsLocal: sourceIsLocal) else { throw LinkPreviewError.notAllowed }176        if !isPrivate, let hit = cache[url] { return hit }177        var request = URLRequest(url: url)178        request.httpShouldHandleCookies = false179        request.setValue("bytes=0-\(Self.maxBytes - 1)", forHTTPHeaderField: "Range")180        redirects.allowLocal = sourceIsLocal181        let preview: LinkPreview182        do {183            let (bytes, response) = try await session.bytes(for: request)184            guard let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode), let final = http.url else { throw LinkPreviewError.failed }185            let type = (http.value(forHTTPHeaderField: "Content-Type") ?? "").lowercased()186            if type.hasPrefix("image/") {187                preview = LinkPreview(url: url, finalURL: final, title: final.lastPathComponent, imageURL: final, content: .image)188            } else if type.contains("pdf") {189                preview = LinkPreview(url: url, finalURL: final, title: final.lastPathComponent, content: .pdf)190            } else if type.contains("html") || type.isEmpty {191                var data = Data()192                data.reserveCapacity(64_000)193                for try await byte in bytes {194                    data.append(byte)195                    if data.count >= Self.maxBytes { break }196                }197                let html = String(data: data, encoding: Self.encoding(from: type)) ?? String(decoding: data, as: UTF8.self)198                preview = LinkPreviewParser.parse(html: html, url: url, finalURL: final)199            } else {200                preview = LinkPreview(url: url, finalURL: final, title: final.lastPathComponent, content: .other)201            }202        } catch let error as LinkPreviewError {203            throw error204        } catch {205            throw LinkPreviewError.failed206        }207        if !isPrivate {208            cache[url] = preview209            order.append(url)210            if order.count > 80 { cache[order.removeFirst()] = nil }211        }212        return preview213    }214215    func clear() { cache = [:]; order = [] }216217    private static func encoding(from contentType: String) -> String.Encoding {218        if contentType.contains("iso-8859-1") || contentType.contains("latin1") { return .isoLatin1 }219        if contentType.contains("windows-1252") { return .windowsCP1252 }220        return .utf8221    }222}223224/// Follows redirects only to addresses the policy allows, at most five times.225private final class RedirectGuard: NSObject, URLSessionTaskDelegate, @unchecked Sendable {226    var allowLocal = false227    func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {228        guard let url = request.url, LinkPreviewPolicy.isAllowed(url, sourceIsLocal: allowLocal), task.countOfBytesReceived >= 0 else { return nil }229        if (task.originalRequest?.url) != nil, (task.currentRequest?.value(forHTTPHeaderField: "X-SPB-Hops").flatMap(Int.init) ?? 0) >= 5 { return nil }230        var next = request231        next.httpShouldHandleCookies = false232        next.setValue(String((task.currentRequest?.value(forHTTPHeaderField: "X-SPB-Hops").flatMap(Int.init) ?? 0) + 1), forHTTPHeaderField: "X-SPB-Hops")233        return next234    }235}236
Files main
Menu

Theme