1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
/*
URLSession is a replacement API for URLConnection. It provides
options that affect the policy of, and various aspects of the
mechanism by which NSURLRequest objects are retrieved from the
network.
An URLSession may be bound to a delegate object. The delegate is
invoked for certain events during the lifetime of a session, such as
server authentication or determining whether a resource to be loaded
should be converted into a download.
URLSession instances are threadsafe.
The default URLSession uses a system provided delegate and is
appropriate to use in place of existing code that uses
+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
An URLSession creates URLSessionTask objects which represent the
action of a resource being loaded. These are analogous to
NSURLConnection objects but provide for more control and a unified
delegate model.
URLSessionTask objects are always created in a suspended state and
must be sent the -resume message before they will execute.
Subclasses of URLSessionTask are used to syntactically
differentiate between data and file downloads.
An URLSessionDataTask receives the resource as a series of calls to
the URLSession:dataTask:didReceiveData: delegate method. This is type of
task most commonly associated with retrieving objects for immediate parsing
by the consumer.
An URLSessionUploadTask differs from an URLSessionDataTask
in how its instance is constructed. Upload tasks are explicitly created
by referencing a file or data object to upload, or by utilizing the
-URLSession:task:needNewBodyStream: delegate message to supply an upload
body.
An URLSessionDownloadTask will directly write the response data to
a temporary file. When completed, the delegate is sent
URLSession:downloadTask:didFinishDownloadingToURL: and given an opportunity
to move this file to a permanent location in its sandboxed container, or to
otherwise read the file. If canceled, an URLSessionDownloadTask can
produce a data blob that can be used to resume a download at a later
time.
Beginning with iOS 9 and Mac OS X 10.11, URLSessionStream is
available as a task type. This allows for direct TCP/IP connection
to a given host and port with optional secure handshaking and
navigation of proxies. Data tasks may also be upgraded to a
URLSessionStream task via the HTTP Upgrade: header and appropriate
use of the pipelining option of URLSessionConfiguration. See RFC
2817 and RFC 6455 for information about the Upgrade: header, and
comments below on turning data tasks into stream tasks.
*/
/* DataTask objects receive the payload through zero or more delegate messages */
/* UploadTask objects receive periodic progress updates but do not return a body */
/* DownloadTask objects represent an active download to disk. They can provide resume data when canceled. */
/* StreamTask objects may be used to create NSInput and OutputStreams, or used directly in reading and writing. */
/*
URLSession is not available for i386 targets before Mac OS X 10.10.
*/
// -----------------------------------------------------------------------------
/// # URLSession API implementation overview
///
/// ## Design Overview
///
/// This implementation uses libcurl for the HTTP layer implementation. At a
/// high level, the `URLSession` keeps a *multi handle*, and each
/// `URLSessionTask` has an *easy handle*. This way these two APIs somewhat
/// have a 1-to-1 mapping.
///
/// The `URLSessionTask` class is in charge of configuring its *easy handle*
/// and adding it to the owning session’s *multi handle*. Adding / removing
/// the handle effectively resumes / suspends the transfer.
///
/// The `URLSessionTask` class has subclasses, but this design puts all the
/// logic into the parent `URLSessionTask`.
///
/// Both the `URLSession` and `URLSessionTask` extensively use helper
/// types to ease testability, separate responsibilities, and improve
/// readability. These types are nested inside the `URLSession` and
/// `URLSessionTask` to limit their scope. Some of these even have sub-types.
///
/// The session class uses the `URLSession.TaskRegistry` to keep track of its
/// tasks.
///
/// The task class uses an `InternalState` type together with `TransferState` to
/// keep track of its state and each transfer’s state -- note that a single task
/// may do multiple transfers, e.g. as the result of a redirect.
///
/// ## Error Handling
///
/// Most libcurl functions either return a `CURLcode` or `CURLMcode` which
/// are represented in Swift as `CFURLSessionEasyCode` and
/// `CFURLSessionMultiCode` respectively. We turn these functions into throwing
/// functions by appending `.asError()` onto their calls. This turns the error
/// code into `Void` but throws the error if it's not `.OK` / zero.
///
/// This is combined with `try!` is almost all places, because such an error
/// indicates a programming error. Hence the pattern used in this code is
///
/// ```
/// try! someFunction().asError()
/// ```
///
/// where `someFunction()` is a function that returns a `CFURLSessionEasyCode`.
///
/// ## Threading
///
/// The URLSession has a libdispatch ‘work queue’, and all internal work is
/// done on that queue, such that the code doesn't have to deal with thread
/// safety beyond that. All work inside a `URLSessionTask` will run on this
/// work queue, and so will code manipulating the session's *multi handle*.
///
/// Delegate callbacks are, however, done on the passed in
/// `delegateQueue`. And any calls into this API need to switch onto the ‘work
/// queue’ as needed.
///
/// - SeeAlso: https://curl.haxx.se/libcurl/c/threadsafe.html
/// - SeeAlso: URLSession+libcurl.swift
///
/// ## HTTP and RFC 2616
///
/// Most of HTTP is defined in [RFC 2616](https://tools.ietf.org/html/rfc2616).
/// While libcurl handles many of these details, some are handled by this
/// URLSession implementation.
///
/// ## To Do
///
/// - TODO: Is is not clear if using API that takes a URLRequest will override
/// all settings of the URLSessionConfiguration or just those that have not
/// explicitly been set.
/// E.g. creating an URLRequest will cause it to have the default timeoutInterval
/// of 60 seconds, but should this be used in stead of the configuration's
/// timeoutIntervalForRequest even if the request's timeoutInterval has not
/// been set explicitly?
///
/// - TODO: We could re-use EasyHandles once they're complete. That'd be a
/// performance optimization. Not sure how much that'd help. The URLSession
/// would have to keep a pool of unused handles.
///
/// - TODO: Could make `workQueue` concurrent and use a multiple reader / single
/// writer approach if it turns out that there's contention.
// -----------------------------------------------------------------------------
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import SwiftFoundation
#else
import Foundation
#endif
internal import Synchronization
extension URLSession {
public enum DelayedRequestDisposition : Sendable {
case cancel
case continueLoading
case useNewRequest
}
}
fileprivate let sessionCounter = Atomic<Int32>(0)
fileprivate func nextSessionIdentifier() -> Int32 {
let (_, new) = sessionCounter.add(1, ordering: .relaxed)
return new
}
public let NSURLSessionTransferSizeUnknown: Int64 = -1
open class URLSession : NSObject, @unchecked Sendable {
internal let _configuration: _Configuration
fileprivate let multiHandle: _MultiHandle
fileprivate var nextTaskIdentifier = 1
internal let workQueue: DispatchQueue
internal let taskRegistry = URLSession._TaskRegistry()
fileprivate let identifier: Int32
// written to on workQueue, read from workQueue and elsewhere. Inherently somewhat racy, then, because it can change after reading the value asynchronously.
fileprivate var invalidated = false
fileprivate static let registerProtocols: () = {
// TODO: We register all the native protocols here.
_ = URLProtocol.registerClass(_HTTPURLProtocol.self)
_ = URLProtocol.registerClass(_FTPURLProtocol.self)
_ = URLProtocol.registerClass(_DataURLProtocol.self)
_ = URLProtocol.registerClass(_WebSocketURLProtocol.self)
}()
/*
* The shared session uses the currently set global URLCache,
* HTTPCookieStorage and URLCredential.Storage objects.
*/
open class var shared: URLSession {
return _shared
}
fileprivate static let _shared: URLSession = {
var configuration = URLSessionConfiguration.default
configuration.httpCookieStorage = HTTPCookieStorage.shared
configuration.protocolClasses = URLProtocol.getProtocols()
return URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
}()
private static let sharedQueue = DispatchQueue(label: "org.swift.URLSession.SharedQueue")
/*
* Customization of URLSession occurs during creation of a new session.
* If you only need to use the convenience routines with custom
* configuration options it is not necessary to specify a delegate.
* If you do specify a delegate, the delegate will be retained until after
* the delegate has been sent the URLSession:didBecomeInvalidWithError: message.
*/
public /*not inherited*/ init(configuration: URLSessionConfiguration) {
initializeLibcurl()
identifier = nextSessionIdentifier()
self.workQueue = DispatchQueue(label: "URLSession<\(identifier)>", target: Self.sharedQueue)
self.delegateQueue = OperationQueue()
self.delegateQueue.maxConcurrentOperationCount = 1
self.delegate = nil
//TODO: Make sure this one can't be written to?
// Could create a subclass of URLSessionConfiguration that wraps the
// URLSession._Configuration and with fatalError() in all setters.
self.configuration = configuration.copy() as! URLSessionConfiguration
let c = URLSession._Configuration(URLSessionConfiguration: configuration)
self._configuration = c
self.multiHandle = _MultiHandle(configuration: c, workQueue: workQueue)
// registering all the protocol classes with URLProtocol
let _ = URLSession.registerProtocols
}
/*
* A delegate queue should be serial to ensure correct ordering of callbacks.
* However, if user supplies a concurrent delegateQueue it is not converted to serial.
*/
public /*not inherited*/ init(configuration: URLSessionConfiguration, delegate: URLSessionDelegate?, delegateQueue queue: OperationQueue?) {
initializeLibcurl()
identifier = nextSessionIdentifier()
self.workQueue = DispatchQueue(label: "URLSession<\(identifier)>", target: Self.sharedQueue)
if let _queue = queue {
self.delegateQueue = _queue
} else {
self.delegateQueue = OperationQueue()
self.delegateQueue.maxConcurrentOperationCount = 1
}
self.delegate = delegate
//TODO: Make sure this one can't be written to?
// Could create a subclass of URLSessionConfiguration that wraps the
// URLSession._Configuration and with fatalError() in all setters.
self.configuration = configuration.copy() as! URLSessionConfiguration
let c = URLSession._Configuration(URLSessionConfiguration: configuration)
self._configuration = c
self.multiHandle = _MultiHandle(configuration: c, workQueue: workQueue)
// registering all the protocol classes with URLProtocol
let _ = URLSession.registerProtocols
}
open private(set) var delegateQueue: OperationQueue
open private(set) var delegate: URLSessionDelegate?
open private(set) var configuration: URLSessionConfiguration
/*
* The sessionDescription property is available for the developer to
* provide a descriptive label for the session.
*/
open var sessionDescription: String?
/* -finishTasksAndInvalidate returns immediately and existing tasks will be allowed
* to run to completion. New tasks may not be created. The session
* will continue to make delegate callbacks until URLSession:didBecomeInvalidWithError:
* has been issued.
*
* -finishTasksAndInvalidate and -invalidateAndCancel do not
* have any effect on the shared session singleton.
*
* When invalidating a background session, it is not safe to create another background
* session with the same identifier until URLSession:didBecomeInvalidWithError: has
* been issued.
*/
open func finishTasksAndInvalidate() {
//we need to return immediately
workQueue.async {
//don't allow creation of new tasks from this point onwards
self.invalidated = true
let invalidateSessionCallback = { [weak self] in
//invoke the delegate method and break the delegate link
guard let strongSelf = self, let sessionDelegate = strongSelf.delegate else { return }
strongSelf.delegateQueue.addOperation {
sessionDelegate.urlSession(strongSelf, didBecomeInvalidWithError: nil)
strongSelf.delegate = nil
}
}
//wait for running tasks to finish
if !self.taskRegistry.isEmpty {
self.taskRegistry.notify(on: invalidateSessionCallback)
} else {
invalidateSessionCallback()
}
}
}
/* -invalidateAndCancel acts as -finishTasksAndInvalidate, but issues
* -cancel to all outstanding tasks for this session. Note task
* cancellation is subject to the state of the task, and some tasks may
* have already have completed at the time they are sent -cancel.
*/
open func invalidateAndCancel() {
/*
As per documentation,
Calling this method on the session returned by the sharedSession method has no effect.
*/
guard self !== URLSession.shared else { return }
workQueue.sync {
self.invalidated = true
}
for task in taskRegistry.allTasks {
task.cancel()
}
// Don't allow creation of new tasks from this point onwards
workQueue.async {
guard let sessionDelegate = self.delegate else { return }
self.delegateQueue.addOperation {
sessionDelegate.urlSession(self, didBecomeInvalidWithError: nil)
self.delegate = nil
}
}
}
/* empty all cookies, cache and credential stores, removes disk files, issues -flushWithCompletionHandler:. Invokes completionHandler() on the delegate queue. */
open func reset(completionHandler: @Sendable @escaping () -> Void) {
let configuration = self.configuration
DispatchQueue.global(qos: .background).async {
configuration.urlCache?.removeAllCachedResponses()
if let storage = configuration.urlCredentialStorage {
for credentialEntry in storage.allCredentials {
for credential in credentialEntry.value {
storage.remove(credential.value, for: credentialEntry.key)
}
}
}
self.flush(completionHandler: completionHandler)
}
}
/* flush storage to disk and clear transient network caches. Invokes completionHandler() on the delegate queue. */
open func flush(completionHandler: @Sendable @escaping () -> Void) {
// We create new CURL handles every request.
delegateQueue.addOperation {
completionHandler()
}
}
@available(*, unavailable, renamed: "getTasksWithCompletionHandler(_:)")
open func getTasksWithCompletionHandler(completionHandler: @Sendable @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {
getTasksWithCompletionHandler(completionHandler)
}
/* invokes completionHandler with outstanding data, upload and download tasks. */
open func getTasksWithCompletionHandler(_ completionHandler: @Sendable @escaping ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask]) -> Void) {
workQueue.async {
self.delegateQueue.addOperation {
var dataTasks = [URLSessionDataTask]()
var uploadTasks = [URLSessionUploadTask]()
var downloadTasks = [URLSessionDownloadTask]()
for task in self.taskRegistry.allTasks {
guard task.state == .running || task.isSuspendedAfterResume else { continue }
if let uploadTask = task as? URLSessionUploadTask {
uploadTasks.append(uploadTask)
} else if let dataTask = task as? URLSessionDataTask {
dataTasks.append(dataTask)
} else if let downloadTask = task as? URLSessionDownloadTask {
downloadTasks.append(downloadTask)
} else {
// Above three are the only required tasks to be returned from this API, so we can ignore any other types of tasks.
}
}
completionHandler(dataTasks, uploadTasks, downloadTasks)
}
}
}
/* invokes completionHandler with all outstanding tasks. */
open func getAllTasks(completionHandler: @Sendable @escaping ([URLSessionTask]) -> Void) {
workQueue.async {
self.delegateQueue.addOperation {
completionHandler(self.taskRegistry.allTasks.filter { $0.state == .running || $0.isSuspendedAfterResume })
}
}
}
/*
* URLSessionTask objects are always created in a suspended state and
* must be sent the -resume message before they will execute.
*/
/* Creates a data task with the given request. The request may have a body stream. */
open func dataTask(with request: URLRequest) -> URLSessionDataTask {
return dataTask(with: _Request(request), behaviour: .callDelegate)
}
/* Creates a data task to retrieve the contents of the given URL. */
open func dataTask(with url: URL) -> URLSessionDataTask {
return dataTask(with: _Request(url), behaviour: .callDelegate)
}
/*
* data task convenience methods. These methods create tasks that
* bypass the normal delegate calls for response and data delivery,
* and provide a simple cancelable asynchronous interface to receiving
* data. Errors will be returned in the NSURLErrorDomain,
* see <Foundation/NSURLError.h>. The delegate, if any, will still be
* called for authentication challenges.
*/
open func dataTask(with request: URLRequest, completionHandler: @Sendable @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return dataTask(with: _Request(request), behaviour: .dataCompletionHandler(completionHandler))
}
open func dataTask(with url: URL, completionHandler: @Sendable @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return dataTask(with: _Request(url), behaviour: .dataCompletionHandler(completionHandler))
}
/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
open func uploadTask(with request: URLRequest, fromFile fileURL: URL) -> URLSessionUploadTask {
let r = URLSession._Request(request)
return uploadTask(with: r, body: .file(fileURL), behaviour: .callDelegate)
}
/* Creates an upload task with the given request. The body of the request is provided from the bodyData. */
open func uploadTask(with request: URLRequest, from bodyData: Data) -> URLSessionUploadTask {
let r = URLSession._Request(request)
return uploadTask(with: r, body: .data(createDispatchData(bodyData)), behaviour: .callDelegate)
}
/* Creates an upload task with the given request. The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
open func uploadTask(withStreamedRequest request: URLRequest) -> URLSessionUploadTask {
let r = URLSession._Request(request)
return uploadTask(with: r, body: nil, behaviour: .callDelegate)
}
/*
* upload convenience method.
*/
open func uploadTask(with request: URLRequest, fromFile fileURL: URL, completionHandler: @Sendable @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTask {
let r = URLSession._Request(request)
return uploadTask(with: r, body: .file(fileURL), behaviour: .dataCompletionHandler(completionHandler))
}
open func uploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @Sendable @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTask {
return uploadTask(with: _Request(request), body: .data(createDispatchData(bodyData!)), behaviour: .dataCompletionHandler(completionHandler))
}
/* Creates a download task with the given request. */
open func downloadTask(with request: URLRequest) -> URLSessionDownloadTask {
let r = URLSession._Request(request)
return downloadTask(with: r, behavior: .callDelegate)
}
/* Creates a download task to download the contents of the given URL. */
open func downloadTask(with url: URL) -> URLSessionDownloadTask {
return downloadTask(with: _Request(url), behavior: .callDelegate)
}
/* Creates a download task with the resume data. If the download cannot be successfully resumed, URLSession:task:didCompleteWithError: will be called. */
open func downloadTask(withResumeData resumeData: Data) -> URLSessionDownloadTask {
return invalidDownloadTask(behavior: .callDelegate)
}
/*
* download task convenience methods. When a download successfully
* completes, the URL will point to a file that must be read or
* copied during the invocation of the completion routine. The file
* will be removed automatically.
*/
open func downloadTask(with request: URLRequest, completionHandler: @Sendable @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
return downloadTask(with: _Request(request), behavior: .downloadCompletionHandler(completionHandler))
}
open func downloadTask(with url: URL, completionHandler: @Sendable @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
return downloadTask(with: _Request(url), behavior: .downloadCompletionHandler(completionHandler))
}
open func downloadTask(withResumeData resumeData: Data, completionHandler: @Sendable @escaping (URL?, URLResponse?, Error?) -> Void) -> URLSessionDownloadTask {
return invalidDownloadTask(behavior: .downloadCompletionHandler(completionHandler))
}
/* Creates a bidirectional stream task to a given host and port.
*/
@available(*, unavailable, message: "URLSessionStreamTask is not available in swift-corelibs-foundation")
open func streamTask(withHostName hostname: String, port: Int) -> URLSessionStreamTask { NSUnsupported() }
open func webSocketTask(with url: URL) -> URLSessionWebSocketTask {
return webSocketTask(with: _Request(url), behavior: .callDelegate)
}
open func webSocketTask(with url: URL, protocols: [String]) -> URLSessionWebSocketTask {
var request = URLRequest(url: url)
request.setValue(protocols.joined(separator: ", "), forHTTPHeaderField: "Sec-WebSocket-Protocol")
return webSocketTask(with: request)
}
open func webSocketTask(with request: URLRequest) -> URLSessionWebSocketTask {
return webSocketTask(with: _Request(request), behavior: .callDelegate)
}
}
// Helpers
fileprivate extension URLSession {
enum _Request {
case request(URLRequest)
case url(URL)
}
func createConfiguredRequest(from request: URLSession._Request) -> URLRequest {
let r = request.createMutableURLRequest()
return _configuration.configure(request: r)
}
}
extension URLSession._Request {
init(_ url: URL) {
self = .url(url)
}
init(_ request: URLRequest) {
self = .request(request)
}
}
extension URLSession._Request {
func createMutableURLRequest() -> URLRequest {
switch self {
case .url(let url): return URLRequest(url: url)
case .request(let r): return r
}
}
}
fileprivate extension URLSession {
func createNextTaskIdentifier() -> Int {
return workQueue.sync {
let i = nextTaskIdentifier
nextTaskIdentifier += 1
return i
}
}
}
fileprivate extension URLSession {
/// Create a data task.
///
/// All public methods funnel into this one.
func dataTask(with request: _Request, behaviour: _TaskRegistry._Behaviour) -> URLSessionDataTask {
guard !self.invalidated else { fatalError("Session invalidated") }
let r = createConfiguredRequest(from: request)
let i = createNextTaskIdentifier()
let task = URLSessionDataTask(session: self, request: r, taskIdentifier: i)
workQueue.async {
self.taskRegistry.add(task, behaviour: behaviour)
}
return task
}
/// Create an upload task.
///
/// All public methods funnel into this one.
func uploadTask(with request: _Request, body: URLSessionTask._Body?, behaviour: _TaskRegistry._Behaviour) -> URLSessionUploadTask {
guard !self.invalidated else { fatalError("Session invalidated") }
let r = createConfiguredRequest(from: request)
let i = createNextTaskIdentifier()
let task = URLSessionUploadTask(session: self, request: r, taskIdentifier: i, body: body)
workQueue.async {
self.taskRegistry.add(task, behaviour: behaviour)
}
return task
}
/// Create a download task
func downloadTask(with request: _Request, behavior: _TaskRegistry._Behaviour) -> URLSessionDownloadTask {
guard !self.invalidated else { fatalError("Session invalidated") }
let r = createConfiguredRequest(from: request)
let i = createNextTaskIdentifier()
let task = URLSessionDownloadTask(session: self, request: r, taskIdentifier: i)
workQueue.async {
self.taskRegistry.add(task, behaviour: behavior)
}
return task
}
/// Create a web socket task
func webSocketTask(with request: _Request, behavior: _TaskRegistry._Behaviour) -> URLSessionWebSocketTask {
guard !self.invalidated else { fatalError("Session invalidated") }
let r = createConfiguredRequest(from: request)
let i = createNextTaskIdentifier()
let task = URLSessionWebSocketTask(session: self, request: r, taskIdentifier: i, body: URLSessionTask._Body.none)
workQueue.async {
self.taskRegistry.add(task, behaviour: behavior)
}
return task
}
/// Create a download task that is marked invalid.
func invalidDownloadTask(behavior: _TaskRegistry._Behaviour) -> URLSessionDownloadTask {
/* We do not support resume data in swift-corelibs-foundation, so whatever we are passed, we should just behave as Darwin does in the presence of invalid data. */
guard !self.invalidated else { fatalError("Session invalidated") }
let task = URLSessionDownloadTask()
task.createdFromInvalidResumeData = true
task.taskIdentifier = createNextTaskIdentifier()
task.session = self
workQueue.async {
self.taskRegistry.add(task, behaviour: behavior)
}
return task
}
}
internal extension URLSession {
/// The kind of callback / delegate behaviour of a task.
///
/// This is similar to the `URLSession.TaskRegistry.Behaviour`, but it
/// also encodes the kind of delegate that the session has.
enum _TaskBehaviour {
/// The session has no delegate, or just a plain `URLSessionDelegate`.
case noDelegate
/// The session has a delegate of type `URLSessionTaskDelegate`
case taskDelegate(URLSessionTaskDelegate)
/// Default action for all events, except for completion.
/// - SeeAlso: URLSession.TaskRegistry.Behaviour.dataCompletionHandler
case dataCompletionHandler(URLSession._TaskRegistry.DataTaskCompletion)
/// Default action for all asynchronous events.
/// - SeeAlso: URLsession.TaskRegistry.Behaviour.dataCompletionHandlerWithTaskDelegate
case dataCompletionHandlerWithTaskDelegate(URLSession._TaskRegistry.DataTaskCompletion, URLSessionTaskDelegate)
/// Default action for all events, except for completion.
/// - SeeAlso: URLSession.TaskRegistry.Behaviour.downloadCompletionHandler
case downloadCompletionHandler(URLSession._TaskRegistry.DownloadTaskCompletion)
/// Default action for all asynchronous events.
/// - SeeAlso: URLsession.TaskRegistry.Behaviour.downloadCompletionHandlerWithTaskDelegate
case downloadCompletionHandlerWithTaskDelegate(URLSession._TaskRegistry.DownloadTaskCompletion, URLSessionTaskDelegate)
}
func behaviour(for task: URLSessionTask) -> _TaskBehaviour {
switch taskRegistry.behaviour(for: task) {
case .dataCompletionHandler(let c): return .dataCompletionHandler(c)
case .dataCompletionHandlerWithTaskDelegate(let c, let d):
guard let d else {
return .dataCompletionHandler(c)
}
return .dataCompletionHandlerWithTaskDelegate(c, d)
case .downloadCompletionHandler(let c): return .downloadCompletionHandler(c)
case .downloadCompletionHandlerWithTaskDelegate(let c, let d):
guard let d else {
return .downloadCompletionHandler(c)
}
return .downloadCompletionHandlerWithTaskDelegate(c, d)
case .callDelegate:
guard let d = delegate as? URLSessionTaskDelegate else {
return .noDelegate
}
return .taskDelegate(d)
}
}
}
fileprivate extension URLSession {
final class CancelState: Sendable {
struct State {
var isCancelled: Bool
var task: URLSessionTask?
}
let lock = Mutex<State>(State(isCancelled: false, task: nil))
init() {
}
func cancel() {
let task = lock.withLock { state in
state.isCancelled = true
let result = state.task
state.task = nil
return result
}
task?.cancel()
}
func activate(task: URLSessionTask) {
let taskUsed = lock.withLock { state in
if state.task != nil {
fatalError("Cannot activate twice")
}
if state.isCancelled {
return false
} else {
state.isCancelled = false
state.task = task
return true
}
}
if !taskUsed {
task.cancel()
}
}
}
}
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension URLSession {
/// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally.
///
/// - Parameter request: The URLRequest for which to load data.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func data(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) {
let cancelState = CancelState()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let completionHandler: URLSession._TaskRegistry.DataTaskCompletion = { data, response, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: (data!, response!))
}
}
let task = dataTask(with: _Request(request), behaviour: .dataCompletionHandlerWithTaskDelegate(completionHandler, delegate))
task._callCompletionHandlerInline = true
task.resume()
cancelState.activate(task: task)
}
} onCancel: {
cancelState.cancel()
}
}
/// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally.
///
/// - Parameter url: The URL for which to load data.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func data(from url: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) {
let cancelState = CancelState()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let completionHandler: URLSession._TaskRegistry.DataTaskCompletion = { data, response, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: (data!, response!))
}
}
let task = dataTask(with: _Request(url), behaviour: .dataCompletionHandlerWithTaskDelegate(completionHandler, delegate))
task._callCompletionHandlerInline = true
task.resume()
cancelState.activate(task: task)
}
} onCancel: {
cancelState.cancel()
}
}
/// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally.
///
/// - Parameter request: The URLRequest for which to upload data.
/// - Parameter fileURL: File to upload.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func upload(for request: URLRequest, fromFile fileURL: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) {
let cancelState = CancelState()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let completionHandler: URLSession._TaskRegistry.DataTaskCompletion = { data, response, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: (data!, response!))
}
}
let task = uploadTask(with: _Request(request), body: .file(fileURL), behaviour: .dataCompletionHandlerWithTaskDelegate(completionHandler, delegate))
task._callCompletionHandlerInline = true
task.resume()
cancelState.activate(task: task)
}
} onCancel: {
cancelState.cancel()
}
}
/// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally.
///
/// - Parameter request: The URLRequest for which to upload data.
/// - Parameter bodyData: Data to upload.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func upload(for request: URLRequest, from bodyData: Data, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) {
let cancelState = CancelState()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let completionHandler: URLSession._TaskRegistry.DataTaskCompletion = { data, response, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: (data!, response!))
}
}
let task = uploadTask(with: _Request(request), body: .data(createDispatchData(bodyData)), behaviour: .dataCompletionHandlerWithTaskDelegate(completionHandler, delegate))
task._callCompletionHandlerInline = true
task.resume()
cancelState.activate(task: task)
}
} onCancel: {
cancelState.cancel()
}
}
/// Convenience method to download using a URLRequest, creates and resumes a URLSessionDownloadTask internally.
///
/// - Parameter request: The URLRequest for which to download.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Downloaded file URL and response. The file will not be removed automatically.
public func download(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (URL, URLResponse) {
let cancelState = CancelState()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let completionHandler: URLSession._TaskRegistry.DownloadTaskCompletion = { location, response, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: (location!, response!))
}
}
let task = downloadTask(with: _Request(request), behavior: .downloadCompletionHandlerWithTaskDelegate(completionHandler, delegate))
task._callCompletionHandlerInline = true
task.resume()
cancelState.activate(task: task)
}
} onCancel: {
cancelState.cancel()
}
}
/// Convenience method to download using a URL, creates and resumes a URLSessionDownloadTask internally.
///
/// - Parameter url: The URL for which to download.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Downloaded file URL and response. The file will not be removed automatically.
public func download(from url: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (URL, URLResponse) {
let cancelState = CancelState()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let completionHandler: URLSession._TaskRegistry.DownloadTaskCompletion = { location, response, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: (location!, response!))
}
}
let task = downloadTask(with: _Request(url), behavior: .downloadCompletionHandlerWithTaskDelegate(completionHandler, delegate))
task._callCompletionHandlerInline = true
task.resume()
cancelState.activate(task: task)
}
} onCancel: {
cancelState.cancel()
}
}
}
internal protocol URLSessionProtocol: AnyObject {
func add(handle: _EasyHandle)
func remove(handle: _EasyHandle)
func behaviour(for: URLSessionTask) -> URLSession._TaskBehaviour
var configuration: URLSessionConfiguration { get }
var delegate: URLSessionDelegate? { get }
}
extension URLSession: URLSessionProtocol {
func add(handle: _EasyHandle) {
multiHandle.add(handle)
}
func remove(handle: _EasyHandle) {
multiHandle.remove(handle)
}
}
/// This class is only used to allow `URLSessionTask.init()` to work.
///
/// - SeeAlso: URLSessionTask.init()
final internal class _MissingURLSession: URLSessionProtocol {
var delegate: URLSessionDelegate? {
fatalError()
}
var configuration: URLSessionConfiguration {
fatalError()
}
func add(handle: _EasyHandle) {
fatalError()
}
func remove(handle: _EasyHandle) {
fatalError()
}
func behaviour(for: URLSessionTask) -> URLSession._TaskBehaviour {
fatalError()
}
}
|