@@ -164,10 +164,6 @@ public enum NSURLRequestNetworkServiceType : UInt {
164
164
*/
165
165
public class NSURLRequest : NSObject , NSSecureCoding , NSCopying , NSMutableCopying {
166
166
167
- private var _URL : NSURL ?
168
- private var _mainDocumentURL : NSURL ?
169
- private var _httpHeaderFields : [ String : String ] ?
170
-
171
167
public override func copy( ) -> AnyObject {
172
168
return copyWithZone ( nil )
173
169
}
@@ -235,16 +231,21 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
235
231
*/
236
232
public convenience init ( URL: NSURL ) {
237
233
self . init ( )
238
- _URL = URL
234
+ self . URL = URL
239
235
}
240
236
241
237
/*!
242
238
@method URL
243
239
@abstract Returns the URL of the receiver.
244
240
@result The URL of the receiver.
245
241
*/
246
- /*@NSCopying */public var URL : NSURL ? { return _URL }
242
+ /*@NSCopying */public private ( set ) var URL : NSURL ?
247
243
244
+ public private( set) var cachePolicy : NSURLRequestCachePolicy = . UseProtocolCachePolicy
245
+ public private( set) var timeoutInterval : NSTimeInterval = 60
246
+ public private( set) var networkServiceType : NSURLRequestNetworkServiceType = . NetworkServiceTypeDefault
247
+ public private( set) var allowsCellularAccess : Bool = true
248
+
248
249
/*!
249
250
@method mainDocumentURL
250
251
@abstract The main document URL associated with this load.
@@ -253,14 +254,14 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
253
254
See setMainDocumentURL:
254
255
@result The main document URL.
255
256
*/
256
- /*@NSCopying*/ public var mainDocumentURL : NSURL ? { return _mainDocumentURL }
257
+ /*@NSCopying*/ public private ( set ) var mainDocumentURL : NSURL ?
257
258
258
259
/*!
259
260
@method HTTPMethod
260
261
@abstract Returns the HTTP request method of the receiver.
261
262
@result the HTTP request method of the receiver.
262
263
*/
263
- public var HTTPMethod : String ? { return " GET " }
264
+ public private ( set ) var HTTPMethod : String ? = " GET "
264
265
265
266
/*!
266
267
@method allHTTPHeaderFields
@@ -269,7 +270,7 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
269
270
@result a dictionary containing all the HTTP header fields of the
270
271
receiver.
271
272
*/
272
- public var allHTTPHeaderFields : [ String : String ] ? { return _httpHeaderFields }
273
+ public private ( set ) var allHTTPHeaderFields : [ String : String ] ?
273
274
274
275
/*!
275
276
@method valueForHTTPHeaderField:
@@ -281,8 +282,10 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
281
282
@result the value associated with the given header field, or nil if
282
283
there is no value associated with the given header field.
283
284
*/
284
- public func valueForHTTPHeaderField( field: String ) -> String ? { return _httpHeaderFields ? [ field. lowercased ( ) ] }
285
-
285
+ public func valueForHTTPHeaderField( field: String ) -> String ? {
286
+ guard let f = allHTTPHeaderFields else { return nil }
287
+ return existingHeaderField ( field, inHeaderFields: f) ? . 1
288
+ }
286
289
}
287
290
288
291
/*!
@@ -330,14 +333,10 @@ public class NSMutableURLRequest : NSURLRequest {
330
333
@param URL The new URL for the receiver.
331
334
*/
332
335
/*@NSCopying */ public override var URL : NSURL ? {
333
- get {
334
- return _URL
335
- }
336
- set ( newURL) {
337
- _URL = newURL
338
- }
336
+ get { return super. URL }
337
+ set { super. URL = newValue }
339
338
}
340
-
339
+
341
340
/*!
342
341
@method setMainDocumentURL:
343
342
@abstract Sets the main document URL
@@ -350,11 +349,8 @@ public class NSMutableURLRequest : NSURLRequest {
350
349
other things in the future.
351
350
*/
352
351
/*@NSCopying*/ public override var mainDocumentURL : NSURL ? {
353
- get {
354
- return _mainDocumentURL
355
- } set ( newMainDocumentURL) {
356
- _mainDocumentURL = newMainDocumentURL
357
- }
352
+ get { return super. mainDocumentURL }
353
+ set { super. mainDocumentURL = newValue }
358
354
}
359
355
360
356
@@ -364,11 +360,25 @@ public class NSMutableURLRequest : NSURLRequest {
364
360
@result the HTTP request method of the receiver.
365
361
*/
366
362
public override var HTTPMethod : String ? {
367
- get {
368
- return _HTTPMethod
369
- } set ( newHTTPMethod) {
370
- _HTTPMethod = newHTTPMethod
371
- }
363
+ get { return super. HTTPMethod }
364
+ set { super. HTTPMethod = newValue }
365
+ }
366
+
367
+ public override var cachePolicy : NSURLRequestCachePolicy {
368
+ get { return super. cachePolicy }
369
+ set { super. cachePolicy = newValue }
370
+ }
371
+ public override var timeoutInterval : NSTimeInterval {
372
+ get { return super. timeoutInterval }
373
+ set { super. timeoutInterval = newValue }
374
+ }
375
+ public override var networkServiceType : NSURLRequestNetworkServiceType {
376
+ get { return super. networkServiceType }
377
+ set { super. networkServiceType = newValue }
378
+ }
379
+ public override var allowsCellularAccess : Bool {
380
+ get { return super. allowsCellularAccess }
381
+ set { super. allowsCellularAccess = newValue }
372
382
}
373
383
374
384
/*!
@@ -382,16 +392,12 @@ public class NSMutableURLRequest : NSURLRequest {
382
392
@param field the header field name (case-insensitive).
383
393
*/
384
394
public func setValue( value: String ? , forHTTPHeaderField field: String ) {
385
- if _httpHeaderFields == nil {
386
- _httpHeaderFields = [ : ]
387
- }
388
- if let existingHeader = _httpHeaderFields? . filter ( { ( existingField, _) -> Bool in
389
- return existingField. lowercased ( ) == field. lowercased ( )
390
- } ) . first {
391
- let ( existingField, _) = existingHeader
392
- _httpHeaderFields? . removeValue ( forKey: existingField)
395
+ var f : [ String : String ] = allHTTPHeaderFields ?? [ : ]
396
+ if let old = existingHeaderField ( field, inHeaderFields: f) {
397
+ f. removeValue ( forKey: old. 0 )
393
398
}
394
- _httpHeaderFields ? [ field] = value
399
+ f [ field] = value
400
+ allHTTPHeaderFields = f
395
401
}
396
402
397
403
/*!
@@ -409,18 +415,22 @@ public class NSMutableURLRequest : NSURLRequest {
409
415
@param field the header field name (case-insensitive).
410
416
*/
411
417
public func addValue( value: String , forHTTPHeaderField field: String ) {
412
- if _httpHeaderFields == nil {
413
- _httpHeaderFields = [ : ]
414
- }
415
- if let existingHeader = _httpHeaderFields? . filter ( { ( existingField, _) -> Bool in
416
- return existingField. lowercased ( ) == field. lowercased ( )
417
- } ) . first {
418
- let ( existingField, existingValue) = existingHeader
419
- _httpHeaderFields ? [ existingField] = " \( existingValue) , \( value) "
418
+ var f : [ String : String ] = allHTTPHeaderFields ?? [ : ]
419
+ if let old = existingHeaderField ( field, inHeaderFields: f) {
420
+ f [ old. 0 ] = old. 1 + " , " + value
420
421
} else {
421
- _httpHeaderFields ? [ field] = value
422
+ f [ field] = value
422
423
}
424
+ allHTTPHeaderFields = f
423
425
}
424
426
}
425
427
426
-
428
+ /// Returns an existing key-value pair inside the header fields if it exists.
429
+ private func existingHeaderField( key: String , inHeaderFields fields: [ String : String ] ) -> ( String , String ) ? {
430
+ for (k, v) in fields {
431
+ if k. lowercased ( ) == key. lowercased ( ) {
432
+ return ( k, v)
433
+ }
434
+ }
435
+ return nil
436
+ }
0 commit comments