forked from daltoniam/SwiftHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPTask.swift
More file actions
550 lines (476 loc) · 22.9 KB
/
HTTPTask.swift
File metadata and controls
550 lines (476 loc) · 22.9 KB
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
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// HTTPTask.swift
//
// Created by Dalton Cherry on 6/3/14.
// Copyright (c) 2014 Vluxe. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
import Foundation
/// HTTP Verbs.
///
/// - GET: For GET requests.
/// - POST: For POST requests.
/// - PUT: For PUT requests.
/// - HEAD: For HEAD requests.
/// - DELETE: For DELETE requests.
/// - PATCH: For PATCH requests.
public enum HTTPMethod: String {
case GET = "GET"
case POST = "POST"
case PUT = "PUT"
case HEAD = "HEAD"
case DELETE = "DELETE"
case PATCH = "PATCH"
}
/// Object representation of a HTTP Response.
public class HTTPResponse {
/// The header values in HTTP response.
public var headers: Dictionary<String,String>?
/// The mime type of the HTTP response.
public var mimeType: String?
/// The suggested filename for a downloaded file.
public var suggestedFilename: String?
/// The body or response data of the HTTP response.
public var responseObject: AnyObject?
/// The status code of the HTTP response.
public var statusCode: Int?
/// The URL of the HTTP response.
public var URL: NSURL?
/// The Error of the HTTP response (if there was one).
public var error: NSError?
///Returns the response as a string
public var text: String? {
if let d = self.responseObject as? NSData {
return NSString(data: d, encoding: NSUTF8StringEncoding) as? String
} else if let val: AnyObject = self.responseObject {
return "\(val)"
}
return nil
}
//get the description of the response
public var description: String {
var buffer = ""
if let u = self.URL {
buffer += "URL:\n\(u)\n\n"
}
if let code = self.statusCode {
buffer += "Status Code:\n\(code)\n\n"
}
if let heads = self.headers {
buffer += "Headers:\n"
for (key, value) in heads {
buffer += "\(key): \(value)\n"
}
buffer += "\n"
}
if let s = self.text {
buffer += "Payload:\n\(s)\n"
}
return buffer
}
}
/// Holds the blocks of the background task.
class BackgroundBlocks {
// these 2 only get used for background download/upload since they have to be delegate methods
var completionHandler:((HTTPResponse) -> Void)?
var progress:((Double) -> Void)?
/**
Initializes a new Background Block
:param: completionHandler The closure that is run when a HTTP Request finished.
:param: progress The closure that is run on the progress of a HTTP Upload or Download.
*/
init(_ completionHandler: ((HTTPResponse) -> Void)?,_ progress: ((Double) -> Void)?) {
self.completionHandler = completionHandler
self.progress = progress
}
}
/// Subclass of NSOperation for handling and scheduling HTTPTask on a NSOperationQueue.
public class HTTPOperation : NSOperation {
private var task: NSURLSessionDataTask!
private var running = false
/// Controls if the task is finished or not.
private var done = false
//MARK: Subclassed NSOperation Methods
/// Returns if the task is asynchronous or not. NSURLSessionTask requests are asynchronous.
override public var asynchronous: Bool {
return true
}
/// Returns if the task is current running.
override public var executing: Bool {
return running
}
/// Returns if the task is finished.
override public var finished: Bool {
return done
}
/// Starts the task.
override public func start() {
if cancelled {
self.willChangeValueForKey("isFinished")
done = true
self.didChangeValueForKey("isFinished")
return
}
self.willChangeValueForKey("isExecuting")
self.willChangeValueForKey("isFinished")
running = true
done = false
self.didChangeValueForKey("isExecuting")
self.didChangeValueForKey("isFinished")
task.resume()
}
/// Cancels the running task.
override public func cancel() {
super.cancel()
task.cancel()
}
/// Sets the task to finished.
public func finish() {
self.willChangeValueForKey("isExecuting")
self.willChangeValueForKey("isFinished")
running = false
done = true
self.didChangeValueForKey("isExecuting")
self.didChangeValueForKey("isFinished")
}
}
/// Configures NSURLSession Request for HTTPOperation. Also provides convenience methods for easily running HTTP Request.
public class HTTPTask : NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {
var backgroundTaskMap = Dictionary<String,BackgroundBlocks>()
//var sess: NSURLSession?
public var baseURL: String?
public var requestSerializer = HTTPRequestSerializer()
public var responseSerializer: HTTPResponseSerializer?
//This gets called on auth challenges. If nil, default handling is use.
//Returning nil from this method will cause the request to be rejected and cancelled
public var auth:((NSURLAuthenticationChallenge) -> NSURLCredential?)?
//This is for doing SSL pinning
public var security: HTTPSecurity?
//MARK: Public Methods
/// A newly minted HTTPTask for your enjoyment.
public override init() {
super.init()
}
/**
Creates a HTTPOperation that can be scheduled on a NSOperationQueue. Called by convenience HTTP verb methods below.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
:returns: A freshly constructed HTTPOperation to add to your NSOperationQueue.
*/
public func create(url: String, method: HTTPMethod, parameters: Dictionary<String,AnyObject>!, completionHandler:((HTTPResponse) -> Void)!) -> HTTPOperation? {
var serialResponse = HTTPResponse()
let serialReq = createRequest(url, method: method, parameters: parameters)
if let err = serialReq.error {
if let handler = completionHandler {
serialResponse.error = err
handler(serialResponse)
}
return nil
}
let opt = HTTPOperation()
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithRequest(serialReq.request,
completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if let handler = completionHandler {
if let hresponse = response as? NSHTTPURLResponse {
serialResponse.headers = hresponse.allHeaderFields as? Dictionary<String,String>
serialResponse.mimeType = hresponse.MIMEType
serialResponse.suggestedFilename = hresponse.suggestedFilename
serialResponse.statusCode = hresponse.statusCode
serialResponse.URL = hresponse.URL
}
serialResponse.error = error
if let d = data {
serialResponse.responseObject = d
if let resSerializer = self.responseSerializer, let resp = response {
let resObj = resSerializer.responseObjectFromResponse(resp, data: d)
serialResponse.responseObject = resObj.object
serialResponse.error = resObj.error
}
if let code = serialResponse.statusCode where serialResponse.statusCode > 299 {
serialResponse.error = self.createError(code)
}
}
handler(serialResponse)
}
opt.finish()
})
opt.task = task
return opt
}
/**
Creates a HTTPOperation as a HTTP GET request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func GET(url: String, parameters: Dictionary<String,AnyObject>?, completionHandler:((HTTPResponse) -> Void)!) {
if let opt = self.create(url, method:.GET, parameters: parameters,completionHandler: completionHandler) {
opt.start()
}
}
/**
Creates a HTTPOperation as a HTTP POST request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func POST(url: String, parameters: Dictionary<String,AnyObject>?, completionHandler:((HTTPResponse) -> Void)!) {
if let opt = self.create(url, method:.POST, parameters: parameters,completionHandler: completionHandler) {
opt.start()
}
}
/**
Creates a HTTPOperation as a HTTP PATCH request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func PATCH(url: String, parameters: Dictionary<String,AnyObject>?, completionHandler:((HTTPResponse) -> Void)!) {
if let opt = self.create(url, method:.PATCH, parameters: parameters,completionHandler: completionHandler) {
opt.start()
}
}
/**
Creates a HTTPOperation as a HTTP PUT request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func PUT(url: String, parameters: Dictionary<String,AnyObject>?, completionHandler:((HTTPResponse) -> Void)!) {
if let opt = self.create(url, method:.PUT, parameters: parameters,completionHandler: completionHandler) {
opt.start()
}
}
/**
Creates a HTTPOperation as a HTTP DELETE request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func DELETE(url: String, parameters: Dictionary<String,AnyObject>?, completionHandler:((HTTPResponse) -> Void)!) {
if let opt = self.create(url, method:.DELETE, parameters: parameters,completionHandler: completionHandler) {
opt.start()
}
}
/**
Creates a HTTPOperation as a HTTP HEAD request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func HEAD(url: String, parameters: Dictionary<String,AnyObject>?, completionHandler:((HTTPResponse) -> Void)!) {
if let opt = self.create(url, method:.HEAD, parameters: parameters,completionHandler: completionHandler) {
opt.start()
}
}
/**
Creates and starts a HTTPOperation to download a file in the background.
:param: url The url you would like to make a request to.
:param: method The HTTP method you want to use. Default is GET.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: progress The progress returned in the progress closure is between 0 and 1.
:param: completionHandler The closure that is run when the HTTP Request finishes. The HTTPResponse responseObject object will be a fileURL. You MUST copy the fileURL return in HTTPResponse.responseObject to a new location before using it (e.g. your documents directory).
*/
public func download(url: String, method: HTTPMethod = .GET, parameters: Dictionary<String,AnyObject>?,progress:((Double) -> Void)!, completionHandler:((HTTPResponse) -> Void)!) -> NSURLSessionDownloadTask? {
let serialReq = createRequest(url,method: method, parameters: parameters)
if let err = serialReq.error {
if let handler = completionHandler {
var res = HTTPResponse()
res.error = err
handler(res)
}
return nil
}
let ident = createBackgroundIdent()
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(ident)
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.downloadTaskWithRequest(serialReq.request)
backgroundTaskMap[ident] = BackgroundBlocks(completionHandler,progress)
//this does not have to be queueable as Apple's background dameon *should* handle that.
task.resume()
return task
}
/**
Creates and starts a HTTPOperation to upload a file in the background.
:param: url The url you would like to make a request to.
:param: method The HTTP method you want to use. Default is POST.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: progress The progress returned in the progress closure is between 0 and 1.
:param: completionHandler The closure that is run when a HTTP Request finished.
*/
public func upload(url: String, method: HTTPMethod = .POST, parameters: Dictionary<String,AnyObject>?,progress:((Double) -> Void)!, completionHandler:((HTTPResponse) -> Void)!) -> NSURLSessionTask? {
let serialReq = createRequest(url,method: method, parameters: parameters)
if let err = serialReq.error {
if let handler = completionHandler {
var res = HTTPResponse()
res.error = err
handler(res)
}
return nil
}
let ident = createBackgroundIdent()
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(ident)
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.uploadTaskWithStreamedRequest(serialReq.request)
backgroundTaskMap[ident] = BackgroundBlocks(completionHandler,progress)
task.resume()
return task
}
//MARK: Private Helper Methods
/**
Creates and starts a HTTPOperation to download a file in the background.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:param: parameters The parameters are HTTP parameters you would like to send.
:returns: A NSURLRequest from configured requestSerializer.
*/
private func createRequest(url: String, method: HTTPMethod, parameters: Dictionary<String,AnyObject>!) -> (request: NSURLRequest, error: NSError?) {
var urlVal = url
//probably should change the 'http' to something more generic
if let base = self.baseURL where !url.hasPrefix("http") {
var split = url.hasPrefix("/") ? "" : "/"
urlVal = "\(base)\(split)\(url)"
}
if let encoded = urlVal.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
if let u = NSURL(string: encoded) {
return self.requestSerializer.createRequest(u, method: method, parameters: parameters)
}
}
return (NSURLRequest(),createError(-1001))
}
/**
Creates a random string to use for the identifier of the background download/upload requests.
:returns: Identifier String.
*/
private func createBackgroundIdent() -> String {
let letters = "abcdefghijklmnopqurstuvwxyz"
var str = ""
for var i = 0; i < 14; i++ {
let start = Int(arc4random() % 14)
str.append(letters[advance(letters.startIndex,start)])
}
return "com.vluxe.swifthttp.request.\(str)"
}
/**
Creates a random string to use for the identifier of the background download/upload requests.
:param: code Code for error.
:returns: An NSError.
*/
private func createError(code: Int) -> NSError {
var text: String = HTTPStatusCode(statusCode: code).statusDescription
return NSError(domain: "HTTPTask", code: code, userInfo: [NSLocalizedDescriptionKey: text])
}
/**
Creates a random string to use for the identifier of the background download/upload requests.
:param: identifier The identifier string.
:returns: An NSError.
*/
private func cleanupBackground(identifier: String) {
backgroundTaskMap.removeValueForKey(identifier)
}
//MARK: NSURLSession Delegate Methods
/// Method for authentication challenge.
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
if let sec = security where challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
let space = challenge.protectionSpace
if let trust = space.serverTrust {
if sec.isValid(trust, domain: space.host) {
completionHandler(.UseCredential, NSURLCredential(trust: trust))
return
}
}
completionHandler(.CancelAuthenticationChallenge, nil)
return
} else if let a = auth {
let cred = a(challenge)
if let c = cred {
completionHandler(.UseCredential, c)
return
}
completionHandler(.RejectProtectionSpace, nil)
return
}
completionHandler(.PerformDefaultHandling, nil)
}
//MARK: Methods for background download/upload
///update the download/upload progress closure
func handleProgress(session: NSURLSession, totalBytesExpected: Int64, currentBytes: Int64) {
if session.configuration.valueForKey("identifier") != nil { //temp workaround for radar: 21097168
let increment = 100.0/Double(totalBytesExpected)
var current = (increment*Double(currentBytes))*0.01
if current > 1 {
current = 1;
}
if let blocks = backgroundTaskMap[session.configuration.identifier] {
if blocks.progress != nil {
blocks.progress!(current)
}
}
}
}
//call the completionHandler closure for upload/download requests
func handleFinish(session: NSURLSession, task: NSURLSessionTask, response: AnyObject) {
if session.configuration.valueForKey("identifier") != nil { //temp workaround for radar: 21097168
if let blocks = backgroundTaskMap[session.configuration.identifier] {
if let handler = blocks.completionHandler {
var resp = HTTPResponse()
if let hresponse = task.response as? NSHTTPURLResponse {
resp.headers = hresponse.allHeaderFields as? Dictionary<String,String>
resp.mimeType = hresponse.MIMEType
resp.suggestedFilename = hresponse.suggestedFilename
resp.statusCode = hresponse.statusCode
resp.URL = hresponse.URL
}
resp.responseObject = response
if let code = resp.statusCode where resp.statusCode > 299 {
resp.error = self.createError(code)
}
handler(resp)
}
}
cleanupBackground(session.configuration.identifier)
}
}
/// Called when the background task failed.
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if let err = error {
if session.configuration.valueForKey("identifier") != nil { //temp workaround for radar: 21097168
if let blocks = backgroundTaskMap[session.configuration.identifier] {
if let handler = blocks.completionHandler {
var res = HTTPResponse()
res.error = err
handler(res)
}
}
cleanupBackground(session.configuration.identifier)
}
}
}
/// The background download finished and reports the url the data was saved to.
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL!) {
handleFinish(session, task: downloadTask, response: location)
}
/// Will report progress of background download
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
handleProgress(session, totalBytesExpected: totalBytesExpectedToWrite, currentBytes:totalBytesWritten)
}
/// The background download finished, don't have to really do anything.
public func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
}
/// The background upload finished and reports the response.
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData!) {
handleFinish(session, task: dataTask, response: data)
}
///Will report progress of background upload
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
handleProgress(session, totalBytesExpected: totalBytesExpectedToSend, currentBytes:totalBytesSent)
}
//implement if we want to support partial file upload/download
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
}
}