@@ -54,22 +54,20 @@ public let NSHTTPCookiePort: String = "Port"
54
54
/// the various cookie attributes. It has accessors to get the various
55
55
/// attributes of a cookie.
56
56
public class NSHTTPCookie : NSObject {
57
- private struct Cookie {
58
- let comment : String ?
59
- let commentURL : NSURL ?
60
- let domain : String
61
- let expiresDate : NSDate ?
62
- let HTTPOnly : Bool
63
- let secure : Bool
64
- let sessionOnly : Bool
65
- let name : String
66
- let path : String
67
- let portList : [ NSNumber ] ?
68
- let properties : [ String : Any ] ?
69
- let value : String
70
- let version : Int
71
- }
72
- private let cookieRepresentation : Cookie
57
+
58
+ let _comment : String ?
59
+ let _commentURL : NSURL ?
60
+ let _domain : String
61
+ let _expiresDate : NSDate ?
62
+ let _HTTPOnly : Bool
63
+ let _secure : Bool
64
+ let _sessionOnly : Bool
65
+ let _name : String
66
+ let _path : String
67
+ let _portList : [ NSNumber ] ?
68
+ let _value : String
69
+ let _version : Int
70
+ var _properties : [ String : Any ]
73
71
74
72
/// Initialize a NSHTTPCookie object with a dictionary of parameters
75
73
///
@@ -204,7 +202,7 @@ public class NSHTTPCookie : NSObject {
204
202
else {
205
203
return nil
206
204
}
207
-
205
+
208
206
let canonicalDomain : String
209
207
if let domain = properties [ NSHTTPCookieDomain] as? String {
210
208
canonicalDomain = domain
@@ -216,99 +214,100 @@ public class NSHTTPCookie : NSObject {
216
214
} else {
217
215
return nil
218
216
}
219
-
220
- let secure: Bool
217
+
218
+ _path = path
219
+ _name = name
220
+ _value = value
221
+ _do main = canonicalDomain
222
+
221
223
if let
222
224
secureString = properties [ NSHTTPCookieSecure] as? String
223
225
where secureString. characters. count > 0
224
226
{
225
- secure = true
227
+ _secure = true
226
228
} else {
227
- secure = false
229
+ _secure = false
228
230
}
229
231
230
232
let version: Int
231
233
if let
232
- versionString = properties [ NSHTTPCookieSecure ] as? String
234
+ versionString = properties [ NSHTTPCookieVersion ] as? String
233
235
where versionString == " 1 "
234
236
{
235
237
version = 1
236
238
} else {
237
239
version = 0
238
240
}
239
-
240
- let portList: [ NSNumber] ?
241
- if let portString = properties [ NSHTTPCookiePort] as? String {
242
- portList = portString. characters
241
+ _version = version
242
+
243
+ if let portString = properties [ NSHTTPCookiePort] as? String
244
+ where _version == 1 {
245
+ _portList = portString. characters
243
246
. split ( separator: " , " )
244
247
. flatMap { Int ( String ( $0) ) }
245
248
. map { NSNumber ( integer: $0) }
246
249
} else {
247
- portList = nil
250
+ _portList = nil
248
251
}
249
252
250
253
// TODO: factor into a utility function
251
- let expiresDate: NSDate?
252
254
if version == 0 {
253
255
let expiresProperty = properties [ NSHTTPCookieExpires]
254
256
if let date = expiresProperty as? NSDate {
255
- // If the dictionary value is already an NSDate,
256
- // nothing left to do
257
- expiresDate = date
257
+ _expiresDate = date
258
258
} else if let dateString = expiresProperty as? String {
259
- // If the dictionary value is a string, parse it
260
259
let formatter = NSDateFormatter ( )
261
- formatter. dateFormat = " yyyy-MM-dd'T'HH:mm "
262
-
260
+ formatter. dateFormat = " EEE, dd MMM yyyy HH:mm:ss O " // per RFC 6265 '<rfc1123-date, defined in [RFC2616], Section 3.3.1>'
263
261
let timeZone = NSTimeZone ( abbreviation: " GMT " )
264
262
formatter. timeZone = timeZone
265
-
266
- expiresDate = formatter. dateFromString ( dateString)
263
+ _expiresDate = formatter. dateFromString ( dateString)
267
264
} else {
268
- expiresDate = nil
269
- }
270
- } else if version == 1 {
271
- if let
272
- maximumAge = properties [ NSHTTPCookieMaximumAge] as? String ,
273
- secondsFromNow = Double ( maximumAge)
274
- {
275
- expiresDate = NSDate ( timeIntervalSinceNow: secondsFromNow)
276
- } else {
277
- expiresDate = nil
265
+ _expiresDate = nil
278
266
}
267
+ } else if let
268
+ maximumAge = properties [ NSHTTPCookieMaximumAge] as? String ,
269
+ secondsFromNow = Int ( maximumAge)
270
+ where _version == 1 {
271
+ _expiresDate = NSDate ( timeIntervalSinceNow: Double ( secondsFromNow) )
279
272
} else {
280
- expiresDate = nil
273
+ _expiresDate = nil
281
274
}
282
-
283
- var discard = false
275
+
276
+
284
277
if let discardString = properties [ NSHTTPCookieDiscard] as? String {
285
- discard = discardString == " TRUE "
286
- } else if let
287
- _ = properties [ NSHTTPCookieMaximumAge] as? String
288
- where version >= 1
289
- {
290
- discard = false
278
+ _sessionOnly = discardString == " TRUE "
279
+ } else {
280
+ _sessionOnly = properties [ NSHTTPCookieMaximumAge] == nil && version >= 1
291
281
}
292
-
293
- // TODO: commentURL can be a string or NSURL
294
-
295
- self. cookieRepresentation = Cookie (
296
- comment: version == 1 ?
297
- properties [ NSHTTPCookieComment] as? String : nil ,
298
- commentURL: version == 1 ?
299
- properties [ NSHTTPCookieCommentURL] as? NSURL : nil ,
300
- domain: canonicalDomain,
301
- expiresDate: expiresDate,
302
- HTTPOnly: secure,
303
- secure: secure,
304
- sessionOnly: discard,
305
- name: name,
306
- path: path,
307
- portList: version == 1 ? portList : nil ,
308
- properties: properties,
309
- value: value,
310
- version: version
311
- )
282
+ if version == 0 {
283
+ _comment = nil
284
+ _commentURL = nil
285
+ } else {
286
+ _comment = properties [ NSHTTPCookieComment] as? String
287
+ if let commentURL = properties [ NSHTTPCookieCommentURL] as? NSURL {
288
+ _commentURL = commentURL
289
+ } else if let commentURL = properties [ NSHTTPCookieCommentURL] as? String {
290
+ _commentURL = NSURL ( string: commentURL)
291
+ } else {
292
+ _commentURL = nil
293
+ }
294
+ }
295
+ _HTTPOnly = false
296
+ _properties = [ NSHTTPCookieComment : properties [ NSHTTPCookieComment] ,
297
+ NSHTTPCookieCommentURL : properties [ NSHTTPCookieCommentURL] ,
298
+ " Created " : NSDate ( ) . timeIntervalSinceReferenceDate, // Cocoa Compatibility
299
+ NSHTTPCookieDiscard : _sessionOnly,
300
+ NSHTTPCookieDomain : _domain,
301
+ NSHTTPCookieExpires : _expiresDate,
302
+ NSHTTPCookieMaximumAge : properties [ NSHTTPCookieMaximumAge] ,
303
+ NSHTTPCookieName : _name,
304
+ NSHTTPCookieOriginURL : properties [ NSHTTPCookieOriginURL] ,
305
+ NSHTTPCookiePath : _path,
306
+ NSHTTPCookiePort : _portList,
307
+ NSHTTPCookieSecure : _secure,
308
+ NSHTTPCookieValue : _value,
309
+ NSHTTPCookieVersion : _version
310
+ ]
312
311
}
313
312
314
313
/// Return a dictionary of header fields that can be used to add the
@@ -319,7 +318,7 @@ public class NSHTTPCookie : NSObject {
319
318
/// are the corresponding header field values.
320
319
public class func requestHeaderFields( with cookies: [ NSHTTPCookie] ) - > [ String : String] {
321
320
var cookieString = cookies. reduce ( " " ) { ( sum, next) -> String in
322
- return sum + " \( next. cookieRepresentation . name ) = \( next. cookieRepresentation . value ) ; "
321
+ return sum + " \( next. _name ) = \( next. _value ) ; "
323
322
}
324
323
//Remove the final trailing semicolon and whitespace
325
324
if ( cookieString. length > 0 ) {
@@ -351,25 +350,25 @@ public class NSHTTPCookie : NSObject {
351
350
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
352
351
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
353
352
public var properties: [ String : Any] ? {
354
- return self . cookieRepresentation . properties
353
+ return _properties
355
354
}
356
355
357
356
/// The version of the receiver.
358
357
///
359
358
/// Version 0 maps to "old-style" Netscape cookies.
360
359
/// Version 1 maps to RFC2965 cookies. There may be future versions.
361
360
public var version: Int {
362
- return self . cookieRepresentation . version
361
+ return _version
363
362
}
364
363
365
364
/// The name of the receiver.
366
365
public var name: String {
367
- return self . cookieRepresentation . name
366
+ return _name
368
367
}
369
368
370
369
/// The value of the receiver.
371
370
public var value: String {
372
- return self . cookieRepresentation . value
371
+ return _value
373
372
}
374
373
375
374
/// Returns The expires date of the receiver.
@@ -378,7 +377,7 @@ public class NSHTTPCookie : NSObject {
378
377
/// deleted. The result will be nil if there is no specific expires
379
378
/// date. This will be the case only for *session-only* cookies.
380
379
/*@NSCopying*/ public var expiresDate: NSDate? {
381
- return self . cookieRepresentation . expiresDate
380
+ return _expiresDate
382
381
}
383
382
384
383
/// Whether the receiver is session-only.
@@ -387,7 +386,7 @@ public class NSHTTPCookie : NSObject {
387
386
/// session (regardless of expiration date), `false` if receiver need not
388
387
/// be discarded at the end of the session.
389
388
public var isSessionOnly: Bool {
390
- return self . cookieRepresentation . sessionOnly
389
+ return _sessionOnly
391
390
}
392
391
393
392
/// The domain of the receiver.
@@ -397,7 +396,7 @@ public class NSHTTPCookie : NSObject {
397
396
/// should be sent to subdomains as well, assuming certain other
398
397
/// restrictions are valid. See RFC 2965 for more detail.
399
398
public var do main: String {
400
- return self . cookieRepresentation . domain
399
+ return _domain
401
400
}
402
401
403
402
/// The path of the receiver.
@@ -406,7 +405,7 @@ public class NSHTTPCookie : NSObject {
406
405
/// domain for which this cookie should be sent. The cookie will also
407
406
/// be sent for children of that path, so `"/"` is the most general.
408
407
public var path: String {
409
- return self . cookieRepresentation . path
408
+ return _path
410
409
}
411
410
412
411
/// Whether the receiver should be sent only over secure channels
@@ -416,7 +415,7 @@ public class NSHTTPCookie : NSObject {
416
415
/// trusted servers (i.e. via SSL or TLS), and should not be delievered to any
417
416
/// javascript applications to prevent cross-site scripting vulnerabilities.
418
417
public var isSecure: Bool {
419
- return self . cookieRepresentation . secure
418
+ return _secure
420
419
}
421
420
422
421
/// Whether the receiver should only be sent to HTTP servers per RFC 2965
@@ -427,7 +426,7 @@ public class NSHTTPCookie : NSObject {
427
426
/// Specifically these cookies should not be delivered to any javascript
428
427
/// applications to prevent cross-site scripting vulnerabilities.
429
428
public var isHTTPOnly: Bool {
430
- return self . cookieRepresentation . HTTPOnly
429
+ return _HTTPOnly
431
430
}
432
431
433
432
/// The comment of the receiver.
@@ -436,7 +435,7 @@ public class NSHTTPCookie : NSObject {
436
435
/// presentation to the user explaining the contents and purpose of this
437
436
/// cookie. It may be nil.
438
437
public var comment: String? {
439
- return self . cookieRepresentation . comment
438
+ return _comment
440
439
}
441
440
442
441
/// The comment URL of the receiver.
@@ -445,7 +444,7 @@ public class NSHTTPCookie : NSObject {
445
444
/// presentation to the user as a link for further information about
446
445
/// this cookie. It may be nil.
447
446
/*@NSCopying*/ public var commentURL: NSURL? {
448
- return self . cookieRepresentation . commentURL
447
+ return _commentURL
449
448
}
450
449
451
450
/// The list ports to which the receiver should be sent.
@@ -457,6 +456,6 @@ public class NSHTTPCookie : NSObject {
457
456
/// The array may be nil, in which case this cookie can be sent to any
458
457
/// port.
459
458
public var portList: [ NSNumber] ? {
460
- return self . cookieRepresentation . portList
459
+ return _portList
461
460
}
462
461
}
0 commit comments