| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- // Manager.swift
- //
- // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- import Foundation
- /**
- Responsible for creating and managing `Request` objects, as well as their underlying `NSURLSession`.
- */
- public class Manager {
- // MARK: - Properties
- /**
- A shared instance of `Manager`, used by top-level Alamofire request methods, and suitable for use directly
- for any ad hoc requests.
- */
- public static let sharedInstance: Manager = {
- let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
- configuration.HTTPAdditionalHeaders = Manager.defaultHTTPHeaders
- return Manager(configuration: configuration)
- }()
- /**
- Creates default values for the "Accept-Encoding", "Accept-Language" and "User-Agent" headers.
- */
- public static let defaultHTTPHeaders: [String: String] = {
- // Accept-Encoding HTTP Header; see https://tools.ietf.org/html/rfc7230#section-4.2.3
- let acceptEncoding: String = "gzip;q=1.0,compress;q=0.5"
- // Accept-Language HTTP Header; see https://tools.ietf.org/html/rfc7231#section-5.3.5
- let acceptLanguage: String = {
- var components: [String] = []
- for (index, languageCode) in (NSLocale.preferredLanguages() as [String]).enumerate() {
- let q = 1.0 - (Double(index) * 0.1)
- components.append("\(languageCode);q=\(q)")
- if q <= 0.5 {
- break
- }
- }
- return ",".join(components)
- }()
- // User-Agent Header; see https://tools.ietf.org/html/rfc7231#section-5.5.3
- let userAgent: String = {
- if let info = NSBundle.mainBundle().infoDictionary {
- let executable: AnyObject = info[kCFBundleExecutableKey as String] ?? "Unknown"
- let bundle: AnyObject = info[kCFBundleIdentifierKey as String] ?? "Unknown"
- let version: AnyObject = info[kCFBundleVersionKey as String] ?? "Unknown"
- let os: AnyObject = NSProcessInfo.processInfo().operatingSystemVersionString ?? "Unknown"
- var mutableUserAgent = NSMutableString(string: "\(executable)/\(bundle) (\(version); OS \(os))") as CFMutableString
- let transform = NSString(string: "Any-Latin; Latin-ASCII; [:^ASCII:] Remove") as CFString
- if CFStringTransform(mutableUserAgent, UnsafeMutablePointer<CFRange>(nil), transform, false) {
- return mutableUserAgent as String
- }
- }
- return "Alamofire"
- }()
- return [
- "Accept-Encoding": acceptEncoding,
- "Accept-Language": acceptLanguage,
- "User-Agent": userAgent
- ]
- }()
- let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL)
- /// The underlying session.
- public let session: NSURLSession
- /// The session delegate handling all the task and session delegate callbacks.
- public let delegate: SessionDelegate
- /// Whether to start requests immediately after being constructed. `true` by default.
- public var startRequestsImmediately: Bool = true
- /**
- The background completion handler closure provided by the UIApplicationDelegate
- `application:handleEventsForBackgroundURLSession:completionHandler:` method. By setting the background
- completion handler, the SessionDelegate `sessionDidFinishEventsForBackgroundURLSession` closure implementation
- will automatically call the handler.
-
- If you need to handle your own events before the handler is called, then you need to override the
- SessionDelegate `sessionDidFinishEventsForBackgroundURLSession` and manually call the handler when finished.
-
- `nil` by default.
- */
- public var backgroundCompletionHandler: (() -> Void)?
- // MARK: - Lifecycle
- /**
- Initializes the `Manager` instance with the given configuration and server trust policy.
- - parameter configuration: The configuration used to construct the managed session.
- `NSURLSessionConfiguration.defaultSessionConfiguration()` by default.
- - parameter serverTrustPolicyManager: The server trust policy manager to use for evaluating all server trust
- challenges. `nil` by default.
- - returns: The new `Manager` instance.
- */
- required public init(
- configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration(),
- serverTrustPolicyManager: ServerTrustPolicyManager? = nil)
- {
- self.delegate = SessionDelegate()
- self.session = NSURLSession(configuration: configuration, delegate: self.delegate, delegateQueue: nil)
- self.session.serverTrustPolicyManager = serverTrustPolicyManager
- self.delegate.sessionDidFinishEventsForBackgroundURLSession = { [weak self] session in
- if let strongSelf = self {
- strongSelf.backgroundCompletionHandler?()
- }
- }
- }
- deinit {
- session.invalidateAndCancel()
- }
- // MARK: - Request
- /**
- Creates a request for the specified method, URL string, parameters, and parameter encoding.
- - parameter method: The HTTP method.
- - parameter URLString: The URL string.
- - parameter parameters: The parameters. `nil` by default.
- - parameter encoding: The parameter encoding. `.URL` by default.
- - parameter headers: The HTTP headers. `nil` by default.
- - returns: The created request.
- */
- public func request(
- method: Method,
- _ URLString: URLStringConvertible,
- parameters: [String: AnyObject]? = nil,
- encoding: ParameterEncoding = .URL,
- headers: [String: String]? = nil)
- -> Request
- {
- let mutableURLRequest = URLRequest(method, URLString, headers: headers)
- let encodedURLRequest = encoding.encode(mutableURLRequest, parameters: parameters).0
- return request(encodedURLRequest)
- }
- /**
- Creates a request for the specified URL request.
- If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
- - parameter URLRequest: The URL request
- - returns: The created request.
- */
- public func request(URLRequest: URLRequestConvertible) -> Request {
- var dataTask: NSURLSessionDataTask!
- dispatch_sync(queue) {
- dataTask = self.session.dataTaskWithRequest(URLRequest.URLRequest)
- }
- let request = Request(session: session, task: dataTask)
- delegate[request.delegate.task] = request.delegate
- if startRequestsImmediately {
- request.resume()
- }
- return request
- }
- // MARK: - SessionDelegate
- /**
- Responsible for handling all delegate callbacks for the underlying session.
- */
- public final class SessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate {
- private var subdelegates: [Int: Request.TaskDelegate] = [:]
- private let subdelegateQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)
- subscript(task: NSURLSessionTask) -> Request.TaskDelegate? {
- get {
- var subdelegate: Request.TaskDelegate?
- dispatch_sync(subdelegateQueue) {
- subdelegate = self.subdelegates[task.taskIdentifier]
- }
- return subdelegate
- }
- set {
- dispatch_barrier_async(subdelegateQueue) {
- self.subdelegates[task.taskIdentifier] = newValue
- }
- }
- }
- // MARK: - NSURLSessionDelegate
- // MARK: Override Closures
- /// NSURLSessionDelegate override closure for `URLSession:didBecomeInvalidWithError:` method.
- public var sessionDidBecomeInvalidWithError: ((NSURLSession, NSError?) -> Void)?
- /// NSURLSessionDelegate override closure for `URLSession:didReceiveChallenge:completionHandler:` method.
- public var sessionDidReceiveChallenge: ((NSURLSession, NSURLAuthenticationChallenge) -> (NSURLSessionAuthChallengeDisposition, NSURLCredential?))?
- /// NSURLSessionDelegate override closure for `URLSessionDidFinishEventsForBackgroundURLSession:` method.
- public var sessionDidFinishEventsForBackgroundURLSession: ((NSURLSession) -> Void)?
- // MARK: Delegate Methods
- public func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
- sessionDidBecomeInvalidWithError?(session, error)
- }
- public func URLSession(
- session: NSURLSession,
- didReceiveChallenge challenge: NSURLAuthenticationChallenge,
- completionHandler: ((NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void))
- {
- var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
- var credential: NSURLCredential?
- if let sessionDidReceiveChallenge = sessionDidReceiveChallenge {
- (disposition, credential) = sessionDidReceiveChallenge(session, challenge)
- } else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
- let host = challenge.protectionSpace.host
- if let
- serverTrustPolicy = session.serverTrustPolicyManager?.serverTrustPolicyForHost(host),
- serverTrust = challenge.protectionSpace.serverTrust
- {
- if serverTrustPolicy.evaluateServerTrust(serverTrust, isValidForHost: host) {
- disposition = .UseCredential
- credential = NSURLCredential(forTrust: serverTrust)
- } else {
- disposition = .CancelAuthenticationChallenge
- }
- }
- }
- completionHandler(disposition, credential)
- }
- public func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
- sessionDidFinishEventsForBackgroundURLSession?(session)
- }
- // MARK: - NSURLSessionTaskDelegate
- // MARK: Override Closures
- /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:`.
- public var taskWillPerformHTTPRedirection: ((NSURLSession, NSURLSessionTask, NSHTTPURLResponse, NSURLRequest) -> NSURLRequest?)?
- /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:task:didReceiveChallenge:completionHandler:`.
- public var taskDidReceiveChallenge: ((NSURLSession, NSURLSessionTask, NSURLAuthenticationChallenge) -> (NSURLSessionAuthChallengeDisposition, NSURLCredential?))?
- /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:session:task:needNewBodyStream:`.
- public var taskNeedNewBodyStream: ((NSURLSession, NSURLSessionTask) -> NSInputStream!)?
- /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:`.
- public var taskDidSendBodyData: ((NSURLSession, NSURLSessionTask, Int64, Int64, Int64) -> Void)?
- /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:task:didCompleteWithError:`.
- public var taskDidComplete: ((NSURLSession, NSURLSessionTask, NSError?) -> Void)?
- // MARK: Delegate Methods
- public func URLSession(
- session: NSURLSession,
- task: NSURLSessionTask,
- willPerformHTTPRedirection response: NSHTTPURLResponse,
- newRequest request: NSURLRequest,
- completionHandler: ((NSURLRequest?) -> Void))
- {
- var redirectRequest: NSURLRequest? = request
- if let taskWillPerformHTTPRedirection = taskWillPerformHTTPRedirection {
- redirectRequest = taskWillPerformHTTPRedirection(session, task, response, request)
- }
- completionHandler(redirectRequest)
- }
- public func URLSession(
- session: NSURLSession,
- task: NSURLSessionTask,
- didReceiveChallenge challenge: NSURLAuthenticationChallenge,
- completionHandler: ((NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void))
- {
- if let taskDidReceiveChallenge = taskDidReceiveChallenge {
- completionHandler(taskDidReceiveChallenge(session, task, challenge))
- } else if let delegate = self[task] {
- delegate.URLSession(
- session,
- task: task,
- didReceiveChallenge: challenge,
- completionHandler: completionHandler
- )
- } else {
- URLSession(session, didReceiveChallenge: challenge, completionHandler: completionHandler)
- }
- }
- public func URLSession(
- session: NSURLSession,
- task: NSURLSessionTask,
- needNewBodyStream completionHandler: ((NSInputStream?) -> Void))
- {
- if let taskNeedNewBodyStream = taskNeedNewBodyStream {
- completionHandler(taskNeedNewBodyStream(session, task))
- } else if let delegate = self[task] {
- delegate.URLSession(session, task: task, needNewBodyStream: completionHandler)
- }
- }
- public func URLSession(
- session: NSURLSession,
- task: NSURLSessionTask,
- didSendBodyData bytesSent: Int64,
- totalBytesSent: Int64,
- totalBytesExpectedToSend: Int64)
- {
- if let taskDidSendBodyData = taskDidSendBodyData {
- taskDidSendBodyData(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
- } else if let delegate = self[task] as? Request.UploadTaskDelegate {
- delegate.URLSession(
- session,
- task: task,
- didSendBodyData: bytesSent,
- totalBytesSent: totalBytesSent,
- totalBytesExpectedToSend: totalBytesExpectedToSend
- )
- }
- }
- public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
- if let taskDidComplete = taskDidComplete {
- taskDidComplete(session, task, error)
- } else if let delegate = self[task] {
- delegate.URLSession(session, task: task, didCompleteWithError: error)
- }
- self[task] = nil
- }
- // MARK: - NSURLSessionDataDelegate
- // MARK: Override Closures
- /// Overrides default behavior for NSURLSessionDataDelegate method `URLSession:dataTask:didReceiveResponse:completionHandler:`.
- public var dataTaskDidReceiveResponse: ((NSURLSession, NSURLSessionDataTask, NSURLResponse) -> NSURLSessionResponseDisposition)?
- /// Overrides default behavior for NSURLSessionDataDelegate method `URLSession:dataTask:didBecomeDownloadTask:`.
- public var dataTaskDidBecomeDownloadTask: ((NSURLSession, NSURLSessionDataTask, NSURLSessionDownloadTask) -> Void)?
- /// Overrides default behavior for NSURLSessionDataDelegate method `URLSession:dataTask:didReceiveData:`.
- public var dataTaskDidReceiveData: ((NSURLSession, NSURLSessionDataTask, NSData) -> Void)?
- /// Overrides default behavior for NSURLSessionDataDelegate method `URLSession:dataTask:willCacheResponse:completionHandler:`.
- public var dataTaskWillCacheResponse: ((NSURLSession, NSURLSessionDataTask, NSCachedURLResponse) -> NSCachedURLResponse!)?
- // MARK: Delegate Methods
- public func URLSession(
- session: NSURLSession,
- dataTask: NSURLSessionDataTask,
- didReceiveResponse response: NSURLResponse,
- completionHandler: ((NSURLSessionResponseDisposition) -> Void))
- {
- var disposition: NSURLSessionResponseDisposition = .Allow
- if let dataTaskDidReceiveResponse = dataTaskDidReceiveResponse {
- disposition = dataTaskDidReceiveResponse(session, dataTask, response)
- }
- completionHandler(disposition)
- }
- public func URLSession(
- session: NSURLSession,
- dataTask: NSURLSessionDataTask,
- didBecomeDownloadTask downloadTask: NSURLSessionDownloadTask)
- {
- if let dataTaskDidBecomeDownloadTask = dataTaskDidBecomeDownloadTask {
- dataTaskDidBecomeDownloadTask(session, dataTask, downloadTask)
- } else {
- let downloadDelegate = Request.DownloadTaskDelegate(task: downloadTask)
- self[downloadTask] = downloadDelegate
- }
- }
- public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
- if let dataTaskDidReceiveData = dataTaskDidReceiveData {
- dataTaskDidReceiveData(session, dataTask, data)
- } else if let delegate = self[dataTask] as? Request.DataTaskDelegate {
- delegate.URLSession(session, dataTask: dataTask, didReceiveData: data)
- }
- }
- public func URLSession(
- session: NSURLSession,
- dataTask: NSURLSessionDataTask,
- willCacheResponse proposedResponse: NSCachedURLResponse,
- completionHandler: ((NSCachedURLResponse?) -> Void))
- {
- if let dataTaskWillCacheResponse = dataTaskWillCacheResponse {
- completionHandler(dataTaskWillCacheResponse(session, dataTask, proposedResponse))
- } else if let delegate = self[dataTask] as? Request.DataTaskDelegate {
- delegate.URLSession(
- session,
- dataTask: dataTask,
- willCacheResponse: proposedResponse,
- completionHandler: completionHandler
- )
- } else {
- completionHandler(proposedResponse)
- }
- }
- // MARK: - NSURLSessionDownloadDelegate
- // MARK: Override Closures
- /// Overrides default behavior for NSURLSessionDownloadDelegate method `URLSession:downloadTask:didFinishDownloadingToURL:`.
- public var downloadTaskDidFinishDownloadingToURL: ((NSURLSession, NSURLSessionDownloadTask, NSURL) -> Void)?
- /// Overrides default behavior for NSURLSessionDownloadDelegate method `URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:`.
- public var downloadTaskDidWriteData: ((NSURLSession, NSURLSessionDownloadTask, Int64, Int64, Int64) -> Void)?
- /// Overrides default behavior for NSURLSessionDownloadDelegate method `URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:`.
- public var downloadTaskDidResumeAtOffset: ((NSURLSession, NSURLSessionDownloadTask, Int64, Int64) -> Void)?
- // MARK: Delegate Methods
- public func URLSession(
- session: NSURLSession,
- downloadTask: NSURLSessionDownloadTask,
- didFinishDownloadingToURL location: NSURL)
- {
- if let downloadTaskDidFinishDownloadingToURL = downloadTaskDidFinishDownloadingToURL {
- downloadTaskDidFinishDownloadingToURL(session, downloadTask, location)
- } else if let delegate = self[downloadTask] as? Request.DownloadTaskDelegate {
- delegate.URLSession(session, downloadTask: downloadTask, didFinishDownloadingToURL: location)
- }
- }
- public func URLSession(
- session: NSURLSession,
- downloadTask: NSURLSessionDownloadTask,
- didWriteData bytesWritten: Int64,
- totalBytesWritten: Int64,
- totalBytesExpectedToWrite: Int64)
- {
- if let downloadTaskDidWriteData = downloadTaskDidWriteData {
- downloadTaskDidWriteData(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
- } else if let delegate = self[downloadTask] as? Request.DownloadTaskDelegate {
- delegate.URLSession(
- session,
- downloadTask: downloadTask,
- didWriteData: bytesWritten,
- totalBytesWritten: totalBytesWritten,
- totalBytesExpectedToWrite: totalBytesExpectedToWrite
- )
- }
- }
- public func URLSession(
- session: NSURLSession,
- downloadTask: NSURLSessionDownloadTask,
- didResumeAtOffset fileOffset: Int64,
- expectedTotalBytes: Int64)
- {
- if let downloadTaskDidResumeAtOffset = downloadTaskDidResumeAtOffset {
- downloadTaskDidResumeAtOffset(session, downloadTask, fileOffset, expectedTotalBytes)
- } else if let delegate = self[downloadTask] as? Request.DownloadTaskDelegate {
- delegate.URLSession(
- session,
- downloadTask: downloadTask,
- didResumeAtOffset: fileOffset,
- expectedTotalBytes: expectedTotalBytes
- )
- }
- }
- }
- }
|