mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
158 lines
5.6 KiB
Swift
Executable File
158 lines
5.6 KiB
Swift
Executable File
//
|
||
// Downloader.swift
|
||
// RSWeb
|
||
//
|
||
// Created by Brent Simmons on 8/27/16.
|
||
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
import os
|
||
import RSCore
|
||
|
||
public typealias DownloadCallback = @MainActor (DownloadResponse, Error?) -> Swift.Void
|
||
|
||
/// Simple downloader, for a one-shot download like an image
|
||
/// or a web page. For a download-feeds session, see DownloadSession.
|
||
/// Caches response for a short time for GET requests. May return cached response.
|
||
@MainActor public final class Downloader {
|
||
public static let shared = Downloader()
|
||
private let urlSession: URLSession
|
||
private var callbacks = [URL: [(callback: DownloadCallback, fromCache: Bool)]]()
|
||
private let cache = DownloadCache.shared
|
||
|
||
nonisolated private static let logger = Logger(subsystem: Logger.nnwSubsystem, category: "Downloader")
|
||
|
||
private init() {
|
||
let sessionConfiguration = URLSessionConfiguration.ephemeral
|
||
sessionConfiguration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
||
sessionConfiguration.httpShouldSetCookies = false
|
||
sessionConfiguration.httpCookieAcceptPolicy = .never
|
||
sessionConfiguration.httpMaximumConnectionsPerHost = 1
|
||
sessionConfiguration.httpCookieStorage = nil
|
||
|
||
if let userAgentHeaders = UserAgent.headers() {
|
||
sessionConfiguration.httpAdditionalHeaders = userAgentHeaders
|
||
}
|
||
|
||
urlSession = URLSession(configuration: sessionConfiguration)
|
||
}
|
||
|
||
deinit {
|
||
urlSession.invalidateAndCancel()
|
||
}
|
||
|
||
public func download(_ url: URL, userAgentStyle: UserAgentStyle = .feed) async throws -> DownloadResponse {
|
||
try await download(URLRequest(url: url), userAgentStyle: userAgentStyle)
|
||
}
|
||
|
||
public func download(_ urlRequest: URLRequest, userAgentStyle: UserAgentStyle = .feed) async throws -> DownloadResponse {
|
||
try await withCheckedThrowingContinuation { continuation in
|
||
download(urlRequest, userAgentStyle: userAgentStyle) { downloadResponse, error in
|
||
if let error {
|
||
continuation.resume(throwing: error)
|
||
} else {
|
||
continuation.resume(returning: downloadResponse)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public func download(_ url: URL, userAgentStyle: UserAgentStyle = .feed, _ callback: @escaping DownloadCallback) {
|
||
assert(Thread.isMainThread)
|
||
download(URLRequest(url: url), userAgentStyle: userAgentStyle, callback)
|
||
}
|
||
|
||
public func download(_ urlRequest: URLRequest, userAgentStyle: UserAgentStyle = .feed, _ callback: @escaping DownloadCallback) {
|
||
assert(Thread.isMainThread)
|
||
|
||
guard let url = urlRequest.url else {
|
||
Self.logger.fault("Downloader: skipping download for URLRequest without a URL")
|
||
return
|
||
}
|
||
|
||
guard url.isHTTPOrHTTPSURL() else {
|
||
Self.logger.debug("Downloader: skipping download for non-http/https URL: \(url)")
|
||
callback(DownloadResponse(data: nil, response: nil, returnedFromCache: false), nil)
|
||
return
|
||
}
|
||
|
||
let isCacheableRequest = urlRequest.httpMethod == HTTPMethod.get
|
||
|
||
// Return cached record if available.
|
||
if isCacheableRequest {
|
||
if let cachedRecord = cache[url.absoluteString] {
|
||
Self.logger.debug("Downloader: returning cached record for \(url)")
|
||
callback(DownloadResponse(data: cachedRecord.data, response: cachedRecord.response, returnedFromCache: true), nil)
|
||
return
|
||
}
|
||
}
|
||
|
||
// Add callback. If there is already a download in progress for this URL, return early.
|
||
if callbacks[url] == nil {
|
||
Self.logger.debug("Downloader: downloading \(url)")
|
||
callbacks[url] = [(callback, false)]
|
||
} else {
|
||
// A download is already in progress for this URL. Don’t start a separate download.
|
||
// Add the callback to the callbacks array for this URL. This caller is coalesced
|
||
// onto the in-progress download, so it makes no network request of its own.
|
||
Self.logger.debug("Downloader: download in progress for \(url) — adding callback")
|
||
callbacks[url]?.append((callback, true))
|
||
return
|
||
}
|
||
|
||
var urlRequestToUse = urlRequest
|
||
switch userAgentStyle {
|
||
case .feed:
|
||
break // the session's user agent
|
||
case .specialCaseFeed:
|
||
urlRequestToUse.setValue(UserAgent.extendedUserAgent, forHTTPHeaderField: HTTPRequestHeader.userAgent)
|
||
case .browser:
|
||
urlRequestToUse.setValue(UserAgent.browserUserAgent, forHTTPHeaderField: HTTPRequestHeader.userAgent)
|
||
}
|
||
urlRequestToUse.addSpecialCaseUserAgentIfNeeded() // Host requirements win over the requested style.
|
||
|
||
let task = urlSession.dataTask(with: urlRequestToUse) { (data, response, error) in
|
||
|
||
// Don’t cache errors — a retry should hit the network.
|
||
if isCacheableRequest && error == nil {
|
||
Self.logger.debug("Downloader: caching response for \(url)")
|
||
self.cache.add(url.absoluteString, data: data, response: response)
|
||
}
|
||
|
||
Task { @MainActor in
|
||
self.callAndReleaseCallbacks(url, data, response, error)
|
||
}
|
||
}
|
||
task.resume()
|
||
}
|
||
}
|
||
|
||
private extension Downloader {
|
||
|
||
func callAndReleaseCallbacks(_ url: URL, _ data: Data? = nil, _ response: URLResponse? = nil, _ error: Error? = nil) {
|
||
assert(Thread.isMainThread)
|
||
|
||
guard let callbacksForURL = callbacks[url] else {
|
||
assertionFailure("Downloader: downloaded URL \(url) but no callbacks found")
|
||
Self.logger.fault("Downloader: downloaded URL \(url) but no callbacks found")
|
||
return
|
||
}
|
||
|
||
// Release before calling — a callback may start a new download of the same URL.
|
||
callbacks[url] = nil
|
||
|
||
let count = callbacksForURL.count
|
||
if count == 1 {
|
||
Self.logger.debug("Downloader: calling 1 callback for URL \(url)")
|
||
} else {
|
||
Self.logger.debug("Downloader: calling \(count) callbacks for URL \(url)")
|
||
}
|
||
|
||
for entry in callbacksForURL {
|
||
let downloadResponse = DownloadResponse(data: data, response: response, returnedFromCache: entry.fromCache)
|
||
entry.callback(downloadResponse, error)
|
||
}
|
||
}
|
||
}
|