Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Search iOS9// File : Core/Sources/SPSearchKit/SearchLink.swift10// Description : Search addresses: the web results page (universal link)11// read into a search, and written from one.12// Module : SPSearchKit13// Repository : https://git.spboucher.ai/sp-search-ios14// Project Website : https://search.spboucher.ai15// Created : 2026-12-0116// Last Modified : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : None specified (no license file in this repository)83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192/// A search as an address. The web results page is `https://search.spboucher.ai/search?q=…&lang=…&site=…&from=…&to=…`93/// (web/search/src/types.ts, `readQuery`); the V2 parameters `vertical`, `kind` and `fresh` are read the same way when94/// present. `region` is never part of a page address (it is the reader's own setting) and `cursor` is ignored:95/// a link always opens the first page.96public struct SearchLink: Sendable, Hashable {97 public var query: String98 public var filters: SearchFilters99 public var vertical: String100101 public init(query: String, filters: SearchFilters = SearchFilters(), vertical: String = SearchRequest.defaultVertical) {102 self.query = query; self.filters = filters; self.vertical = vertical103 }104105 public static let hosts: Set<String> = ["search.spboucher.ai"]106 /// Scheme of the links the app's own widget builds; it is not registered with the system.107 public static let scheme = "spsearch"108109 /// What an incoming address asks for.110 public enum Target: Sendable, Hashable {111 case home112 /// The app's own "new search" link (widget): home with the cursor in the field.113 case newSearch114 case search(SearchLink)115 }116117 /// nil: not an address of ours (another host, another path) — the caller leaves it alone.118 public static func parse(_ url: URL) -> Target? {119 guard var c = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return nil }120 // The web page writes its address with URLSearchParams, where a space is a `+`.121 if let q = c.percentEncodedQuery, q.contains("+") { c.percentEncodedQuery = q.replacingOccurrences(of: "+", with: "%20") }122 let scheme = (c.scheme ?? "").lowercased()123 let path: String124 if scheme == Self.scheme {125 // spsearch://search?q=… or spsearch:///search?q=…126 path = "/" + ((c.host ?? "") + c.path).trimmingCharacters(in: CharacterSet(charactersIn: "/"))127 } else if scheme == "https" || scheme == "http", let host = c.host?.lowercased(), hosts.contains(host) {128 path = "/" + c.path.trimmingCharacters(in: CharacterSet(charactersIn: "/"))129 } else {130 return nil131 }132 func value(_ name: String, max: Int) -> String? {133 guard let v = c.queryItems?.first(where: { $0.name == name })?.value?.trimmingCharacters(in: .whitespacesAndNewlines), !v.isEmpty else { return nil }134 return String(v.prefix(max))135 }136 switch path {137 case "/new" where scheme == Self.scheme:138 return .newSearch139 case "/":140 // The home page with a query is a search too (`/?q=`), as search boxes of browsers build it.141 guard let q = value("q", max: SearchRequest.maxQueryLength) else { return .home }142 return .search(SearchLink(query: SearchRequest.normalise(q)))143 case "/search":144 guard let raw = value("q", max: 2_000) else { return .home }145 let q = SearchRequest.normalise(raw)146 guard !q.isEmpty else { return .home }147 var f = SearchFilters()148 f.language = value("lang", max: 16)?.lowercased()149 f.site = value("site", max: 253)?.lowercased()150 f.kind = value("kind", max: 32)?.lowercased()151 if let fresh = value("fresh", max: 16)?.lowercased() {152 f.fresh = fresh153 } else {154 f.from = value("from", max: 10).flatMap { SearchRequest.date(day: $0) }155 f.to = value("to", max: 10).flatMap { SearchRequest.date(day: $0) }156 }157 return .search(SearchLink(query: q, filters: f, vertical: value("vertical", max: 32)?.lowercased() ?? SearchRequest.defaultVertical))158 default:159 return nil160 }161 }162163 public static let newSearchURL = URL(string: "\(scheme)://new")!164165 /// The address of this search on the web site: what "share this search" hands out, and what the widget opens.166 public var webURL: URL {167 var items = [URLQueryItem(name: "q", value: query)]168 if vertical != SearchRequest.defaultVertical { items.append(URLQueryItem(name: "vertical", value: vertical)) }169 if let l = filters.language { items.append(URLQueryItem(name: "lang", value: l)) }170 if let s = filters.site { items.append(URLQueryItem(name: "site", value: s)) }171 if let k = filters.kind { items.append(URLQueryItem(name: "kind", value: k)) }172 if let f = filters.fresh {173 items.append(URLQueryItem(name: "fresh", value: f))174 } else {175 if let d = filters.from { items.append(URLQueryItem(name: "from", value: SearchRequest.day(d))) }176 if let d = filters.to { items.append(URLQueryItem(name: "to", value: SearchRequest.day(d))) }177 }178 return Endpoint.url(path: "/search", items: items)179 }180}181