@@ -95,23 +95,20 @@ public let NSHTTPCookiePort: String = "Port"
95
95
attributes of a cookie.
96
96
*/
97
97
public class NSHTTPCookie : NSObject {
98
- private struct Cookie {
99
- let comment : String ?
100
- let commentURL : NSURL ?
101
- let domain : String
102
- let expiresDate : NSDate ?
103
- let HTTPOnly : Bool
104
- let secure : Bool
105
- let sessionOnly : Bool
106
- let name : String
107
- let path : String
108
- let portList : [ NSNumber ] ?
109
- let properties : [ String : Any ] ?
110
- let value : String
111
- let version : Int
112
- }
113
- private let cookieRepresentation : Cookie
114
-
98
+ let _comment : String ?
99
+ let _commentURL : NSURL ?
100
+ let _domain : String
101
+ let _expiresDate : NSDate ?
102
+ let _HTTPOnly : Bool
103
+ let _secure : Bool
104
+ let _sessionOnly : Bool
105
+ let _name : String
106
+ let _path : String
107
+ let _portList : [ NSNumber ] ?
108
+ let _value : String
109
+ let _version : Int
110
+ var _properties : [ String : Any ]
111
+
115
112
/*!
116
113
@method initWithProperties:
117
114
@abstract Initialize a NSHTTPCookie object with a dictionary of
@@ -245,7 +242,7 @@ public class NSHTTPCookie : NSObject {
245
242
else {
246
243
return nil
247
244
}
248
-
245
+
249
246
let canonicalDomain : String
250
247
if let domain = properties [ NSHTTPCookieDomain] as? String {
251
248
canonicalDomain = domain
@@ -257,99 +254,100 @@ public class NSHTTPCookie : NSObject {
257
254
} else {
258
255
return nil
259
256
}
260
-
261
- let secure: Bool
257
+
258
+ _path = path
259
+ _name = name
260
+ _value = value
261
+ _do main = canonicalDomain
262
+
262
263
if let
263
264
secureString = properties [ NSHTTPCookieSecure] as? String
264
265
where secureString. characters. count > 0
265
266
{
266
- secure = true
267
+ _secure = true
267
268
} else {
268
- secure = false
269
+ _secure = false
269
270
}
270
271
271
272
let version: Int
272
273
if let
273
- versionString = properties [ NSHTTPCookieSecure ] as? String
274
+ versionString = properties [ NSHTTPCookieVersion ] as? String
274
275
where versionString == " 1 "
275
276
{
276
277
version = 1
277
278
} else {
278
279
version = 0
279
280
}
280
-
281
- let portList: [ NSNumber] ?
282
- if let portString = properties [ NSHTTPCookiePort] as? String {
283
- portList = portString. characters
281
+ _version = version
282
+
283
+ if let portString = properties [ NSHTTPCookiePort] as? String
284
+ where _version == 1 {
285
+ _portList = portString. characters
284
286
. split ( " , " )
285
287
. flatMap { Int ( String ( $0) ) }
286
288
. map { NSNumber ( integer: $0) }
287
289
} else {
288
- portList = nil
290
+ _portList = nil
289
291
}
290
292
291
293
// TODO: factor into a utility function
292
- let expiresDate: NSDate?
293
294
if version == 0 {
294
295
let expiresProperty = properties [ NSHTTPCookieExpires]
295
296
if let date = expiresProperty as? NSDate {
296
- // If the dictionary value is already an NSDate,
297
- // nothing left to do
298
- expiresDate = date
297
+ _expiresDate = date
299
298
} else if let dateString = expiresProperty as? String {
300
- // If the dictionary value is a string, parse it
301
299
let formatter = NSDateFormatter ( )
302
- formatter. dateFormat = " yyyy-MM-dd'T'HH:mm "
303
-
300
+ formatter. dateFormat = " EEE, dd MMM yyyy HH:mm:ss O " // per RFC 6265 '<rfc1123-date, defined in [RFC2616], Section 3.3.1>'
304
301
let timeZone = NSTimeZone ( abbreviation: " GMT " )
305
302
formatter. timeZone = timeZone
306
-
307
- expiresDate = formatter. dateFromString ( dateString)
303
+ _expiresDate = formatter. dateFromString ( dateString)
308
304
} else {
309
- expiresDate = nil
310
- }
311
- } else if version == 1 {
312
- if let
313
- maximumAge = properties [ NSHTTPCookieMaximumAge] as? String ,
314
- secondsFromNow = Double ( maximumAge)
315
- {
316
- expiresDate = NSDate ( timeIntervalSinceNow: secondsFromNow)
317
- } else {
318
- expiresDate = nil
305
+ _expiresDate = nil
319
306
}
307
+ } else if let
308
+ maximumAge = properties [ NSHTTPCookieMaximumAge] as? String ,
309
+ secondsFromNow = Int ( maximumAge)
310
+ where _version == 1 {
311
+ _expiresDate = NSDate ( timeIntervalSinceNow: Double ( secondsFromNow) )
320
312
} else {
321
- expiresDate = nil
313
+ _expiresDate = nil
322
314
}
323
-
324
- var discard = false
315
+
316
+
325
317
if let discardString = properties [ NSHTTPCookieDiscard] as? String {
326
- discard = discardString == " TRUE "
327
- } else if let
328
- _ = properties [ NSHTTPCookieMaximumAge] as? String
329
- where version >= 1
330
- {
331
- discard = false
318
+ _sessionOnly = discardString == " TRUE "
319
+ } else {
320
+ _sessionOnly = properties [ NSHTTPCookieMaximumAge] == nil && version >= 1
332
321
}
333
-
334
- // TODO: commentURL can be a string or NSURL
335
-
336
- self. cookieRepresentation = Cookie (
337
- comment: version == 1 ?
338
- properties [ NSHTTPCookieComment] as? String : nil ,
339
- commentURL: version == 1 ?
340
- properties [ NSHTTPCookieCommentURL] as? NSURL : nil ,
341
- domain: canonicalDomain,
342
- expiresDate: expiresDate,
343
- HTTPOnly: secure,
344
- secure: secure,
345
- sessionOnly: discard,
346
- name: name,
347
- path: path,
348
- portList: version == 1 ? portList : nil ,
349
- properties: properties,
350
- value: value,
351
- version: version
352
- )
322
+ if version == 0 {
323
+ _comment = nil
324
+ _commentURL = nil
325
+ } else {
326
+ _comment = properties [ NSHTTPCookieComment] as? String
327
+ if let commentURL = properties [ NSHTTPCookieCommentURL] as? NSURL {
328
+ _commentURL = commentURL
329
+ } else if let commentURL = properties [ NSHTTPCookieCommentURL] as? String {
330
+ _commentURL = NSURL ( string: commentURL)
331
+ } else {
332
+ _commentURL = nil
333
+ }
334
+ }
335
+ _HTTPOnly = false
336
+ _properties = [ NSHTTPCookieComment : properties [ NSHTTPCookieComment] ,
337
+ NSHTTPCookieCommentURL : properties [ NSHTTPCookieCommentURL] ,
338
+ " Created " : NSDate ( ) . timeIntervalSinceReferenceDate, // Cocoa Compatibility
339
+ NSHTTPCookieDiscard : _sessionOnly,
340
+ NSHTTPCookieDomain : _domain,
341
+ NSHTTPCookieExpires : _expiresDate,
342
+ NSHTTPCookieMaximumAge : properties [ NSHTTPCookieMaximumAge] ,
343
+ NSHTTPCookieName : _name,
344
+ NSHTTPCookieOriginURL : properties [ NSHTTPCookieOriginURL] ,
345
+ NSHTTPCookiePath : _path,
346
+ NSHTTPCookiePort : _portList,
347
+ NSHTTPCookieSecure : _secure,
348
+ NSHTTPCookieValue : _value,
349
+ NSHTTPCookieVersion : _version
350
+ ]
353
351
}
354
352
355
353
/*!
@@ -376,7 +374,7 @@ public class NSHTTPCookie : NSObject {
376
374
*/
377
375
public class func requestHeaderFieldsWithCookies( cookies: [ NSHTTPCookie] ) - > [ String : String] {
378
376
var cookieString = cookies. reduce ( " " ) { ( sum, next) -> String in
379
- return sum + " \( next. cookieRepresentation . name ) = \( next. cookieRepresentation . value ) ; "
377
+ return sum + " \( next. _name ) = \( next. _value ) ; "
380
378
}
381
379
//Remove the final trailing semicolon and whitespace
382
380
if ( cookieString. length > 0 ) {
@@ -412,7 +410,7 @@ public class NSHTTPCookie : NSObject {
412
410
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
413
411
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
414
412
public var properties: [ String : Any] ? {
415
- return self . cookieRepresentation . properties
413
+ return _properties
416
414
}
417
415
418
416
/*!
@@ -423,7 +421,7 @@ public class NSHTTPCookie : NSObject {
423
421
@result the version of the receiver.
424
422
*/
425
423
public var version: Int {
426
- return self . cookieRepresentation . version
424
+ return _version
427
425
}
428
426
429
427
/*!
@@ -432,7 +430,7 @@ public class NSHTTPCookie : NSObject {
432
430
@result the name of the receiver.
433
431
*/
434
432
public var name: String {
435
- return self . cookieRepresentation . name
433
+ return _name
436
434
}
437
435
438
436
/*!
@@ -441,7 +439,7 @@ public class NSHTTPCookie : NSObject {
441
439
@result the value of the receiver.
442
440
*/
443
441
public var value: String {
444
- return self . cookieRepresentation . value
442
+ return _value
445
443
}
446
444
447
445
/*!
@@ -454,7 +452,7 @@ public class NSHTTPCookie : NSObject {
454
452
@result The expires date of the receiver.
455
453
*/
456
454
/*@NSCopying*/ public var expiresDate: NSDate? {
457
- return self . cookieRepresentation . expiresDate
455
+ return _expiresDate
458
456
}
459
457
460
458
/*!
@@ -465,7 +463,7 @@ public class NSHTTPCookie : NSObject {
465
463
be discarded at the end of the session.
466
464
*/
467
465
public var sessionOnly: Bool {
468
- return self . cookieRepresentation . sessionOnly
466
+ return _sessionOnly
469
467
}
470
468
471
469
/*!
@@ -478,7 +476,7 @@ public class NSHTTPCookie : NSObject {
478
476
@result The domain of the receiver.
479
477
*/
480
478
public var do main: String {
481
- return self . cookieRepresentation . domain
479
+ return _domain
482
480
}
483
481
484
482
/*!
@@ -490,7 +488,7 @@ public class NSHTTPCookie : NSObject {
490
488
@result The path of the receiver.
491
489
*/
492
490
public var path: String {
493
- return self . cookieRepresentation . path
491
+ return _path
494
492
}
495
493
496
494
/*!
@@ -505,7 +503,7 @@ public class NSHTTPCookie : NSObject {
505
503
NO otherwise.
506
504
*/
507
505
public var secure: Bool {
508
- return self . cookieRepresentation . secure
506
+ return _secure
509
507
}
510
508
511
509
/*!
@@ -521,7 +519,7 @@ public class NSHTTPCookie : NSObject {
521
519
NO otherwise.
522
520
*/
523
521
public var HTTPOnly: Bool {
524
- return self . cookieRepresentation . HTTPOnly
522
+ return _HTTPOnly
525
523
}
526
524
527
525
/*!
@@ -534,7 +532,7 @@ public class NSHTTPCookie : NSObject {
534
532
comment.
535
533
*/
536
534
public var comment: String? {
537
- return self . cookieRepresentation . comment
535
+ return _comment
538
536
}
539
537
540
538
/*!
@@ -547,7 +545,7 @@ public class NSHTTPCookie : NSObject {
547
545
has no comment URL.
548
546
*/
549
547
/*@NSCopying*/ public var commentURL: NSURL? {
550
- return self . cookieRepresentation . commentURL
548
+ return _commentURL
551
549
}
552
550
553
551
/*!
@@ -562,7 +560,7 @@ public class NSHTTPCookie : NSObject {
562
560
port.
563
561
*/
564
562
public var portList: [ NSNumber] ? {
565
- return self . cookieRepresentation . portList
563
+ return _portList
566
564
}
567
565
}
568
566
0 commit comments