@@ -37,39 +37,42 @@ extension HTTPCookiePropertyKey {
37
37
38
38
/// Key for cookie value
39
39
public static let value = HTTPCookiePropertyKey ( rawValue: " Value " )
40
-
40
+
41
41
/// Key for cookie origin URL
42
42
public static let originURL = HTTPCookiePropertyKey ( rawValue: " OriginURL " )
43
43
44
44
/// Key for cookie version
45
45
public static let version = HTTPCookiePropertyKey ( rawValue: " Version " )
46
-
46
+
47
47
/// Key for cookie domain
48
48
public static let domain = HTTPCookiePropertyKey ( rawValue: " Domain " )
49
-
49
+
50
50
/// Key for cookie path
51
51
public static let path = HTTPCookiePropertyKey ( rawValue: " Path " )
52
-
52
+
53
53
/// Key for cookie secure flag
54
54
public static let secure = HTTPCookiePropertyKey ( rawValue: " Secure " )
55
-
55
+
56
56
/// Key for cookie expiration date
57
57
public static let expires = HTTPCookiePropertyKey ( rawValue: " Expires " )
58
58
59
59
/// Key for cookie comment text
60
60
public static let comment = HTTPCookiePropertyKey ( rawValue: " Comment " )
61
-
61
+
62
62
/// Key for cookie comment URL
63
63
public static let commentURL = HTTPCookiePropertyKey ( rawValue: " CommentURL " )
64
-
64
+
65
65
/// Key for cookie discard (session-only) flag
66
66
public static let discard = HTTPCookiePropertyKey ( rawValue: " Discard " )
67
-
67
+
68
68
/// Key for cookie maximum age (an alternate way of specifying the expiration)
69
69
public static let maximumAge = HTTPCookiePropertyKey ( rawValue: " Max-Age " )
70
70
71
71
/// Key for cookie ports
72
72
public static let port = HTTPCookiePropertyKey ( rawValue: " Port " )
73
+
74
+ // For Cocoa compatibility
75
+ internal static let created = HTTPCookiePropertyKey ( rawValue: " Created " )
73
76
}
74
77
75
78
/// `NSHTTPCookie` represents an http cookie.
@@ -94,10 +97,10 @@ open class HTTPCookie : NSObject {
94
97
let _version : Int
95
98
var _properties : [ HTTPCookiePropertyKey : Any ]
96
99
97
- static let _attributes : [ HTTPCookiePropertyKey ] = [ . name , . value , . originURL , . version ,
98
- . domain , . path , . secure , . expires ,
99
- . comment , . commentURL , . discard , . maximumAge ,
100
- . port]
100
+ static let _attributes : [ HTTPCookiePropertyKey ]
101
+ = [ . name , . value , . originURL , . version , . domain ,
102
+ . path , . secure , . expires , . comment , . commentURL ,
103
+ . discard , . maximumAge , . port]
101
104
102
105
/// Initialize a NSHTTPCookie object with a dictionary of parameters
103
106
///
@@ -268,7 +271,7 @@ open class HTTPCookie : NSObject {
268
271
version = 0
269
272
}
270
273
_version = version
271
-
274
+
272
275
if let portString = properties [ . port] as? String , _version == 1 {
273
276
_portList = portString. characters
274
277
. split ( separator: " , " )
@@ -300,8 +303,7 @@ open class HTTPCookie : NSObject {
300
303
} else {
301
304
_expiresDate = nil
302
305
}
303
-
304
-
306
+
305
307
if let discardString = properties [ . discard] as? String {
306
308
_sessionOnly = discardString == " TRUE "
307
309
} else {
@@ -321,23 +323,38 @@ open class HTTPCookie : NSObject {
321
323
}
322
324
}
323
325
_HTTPOnly = false
324
- _properties = [ . comment : properties [ . comment] ,
325
- . commentURL : properties [ . commentURL] ,
326
- HTTPCookiePropertyKey ( rawValue: " Created " ) : Date ( ) . timeIntervalSinceReferenceDate, // Cocoa Compatibility
327
- . discard : _sessionOnly,
328
- . domain : _domain,
329
- . expires : _expiresDate,
330
- . maximumAge : properties [ . maximumAge] ,
331
- . name : _name,
332
- . originURL : properties [ . originURL] ,
333
- . path : _path,
334
- . port : _portList,
335
- . secure : _secure,
336
- . value : _value,
337
- . version : _version
326
+
327
+
328
+ _properties = [
329
+ . created : Date ( ) . timeIntervalSinceReferenceDate, // Cocoa Compatibility
330
+ . discard : _sessionOnly,
331
+ . domain : _domain,
332
+ . name : _name,
333
+ . path : _path,
334
+ . secure : _secure,
335
+ . value : _value,
336
+ . version : _version
338
337
]
338
+ if let comment = properties [ . comment] {
339
+ _properties [ . comment] = comment
340
+ }
341
+ if let commentURL = properties [ . commentURL] {
342
+ _properties [ . commentURL] = commentURL
343
+ }
344
+ if let expires = properties [ . expires] {
345
+ _properties [ . expires] = expires
346
+ }
347
+ if let maximumAge = properties [ . maximumAge] {
348
+ _properties [ . maximumAge] = maximumAge
349
+ }
350
+ if let originURL = properties [ . originURL] {
351
+ _properties [ . originURL] = originURL
352
+ }
353
+ if let _portList = _portList {
354
+ _properties [ . port] = _portList
355
+ }
339
356
}
340
-
357
+
341
358
/// Return a dictionary of header fields that can be used to add the
342
359
/// specified cookies to the request.
343
360
///
@@ -355,7 +372,7 @@ open class HTTPCookie : NSObject {
355
372
}
356
373
return [ " Cookie " : cookieString]
357
374
}
358
-
375
+
359
376
/// Return an array of cookies parsed from the specified response header fields and URL.
360
377
///
361
378
/// This method will ignore irrelevant header fields so
@@ -371,7 +388,7 @@ open class HTTPCookie : NSObject {
371
388
// names and values. This implementation takes care of multiple cookies in the same field, however it doesn't
372
389
//support commas and semicolons in names and values(except for dates)
373
390
374
- guard let cookies: String = headerFields [ " Set-Cookie " ] else { return [ ] }
391
+ guard let cookies: String = headerFields [ " Set-Cookie " ] else { return [ ] }
375
392
376
393
let nameValuePairs = cookies. components ( separatedBy: " ; " ) //split the name/value and attribute/value pairs
377
394
. map ( { $0. trim ( ) } ) //trim whitespaces
@@ -383,7 +400,7 @@ open class HTTPCookie : NSObject {
383
400
384
401
//mark cookie boundaries in the name-value array
385
402
var cookieIndices = ( 0 ..< nameValuePairs. count) . filter ( { nameValuePairs [ $0] . hasPrefix ( " Name " ) } )
386
- cookieIndices. append ( nameValuePairs. count)
403
+ cookieIndices. append ( nameValuePairs. count)
387
404
388
405
//bake the cookies
389
406
var httpCookies : [ HTTPCookie ] = [ ]
@@ -392,9 +409,9 @@ open class HTTPCookie : NSObject {
392
409
httpCookies. append ( aCookie)
393
410
}
394
411
}
395
-
412
+
396
413
return httpCookies
397
- }
414
+ }
398
415
399
416
//Bake a cookie
400
417
private class func createHttpCookie( url: URL , pairs: ArraySlice < String > ) -> HTTPCookie ? {
@@ -403,20 +420,20 @@ open class HTTPCookie : NSObject {
403
420
let name = pair. components ( separatedBy: " = " ) [ 0 ]
404
421
var value = pair. components ( separatedBy: " \( name) = " ) [ 1 ] //a value can have an "="
405
422
if canonicalize ( name) == . expires {
406
- value = value. insertComma ( at: 3 ) //re-insert the comma
423
+ value = value. insertComma ( at: 3 ) //re-insert the comma
407
424
}
408
425
properties [ canonicalize ( name) ] = value
409
426
}
410
-
427
+
411
428
//if domain wasn't provided use the URL
412
429
if properties [ . domain] == nil {
413
430
properties [ . domain] = url. absoluteString
414
431
}
415
-
432
+
416
433
//the default Path is "/"
417
434
if properties [ . path] == nil {
418
435
properties [ . path] = " / "
419
- }
436
+ }
420
437
421
438
return HTTPCookie ( properties: properties)
422
439
}
@@ -470,25 +487,25 @@ open class HTTPCookie : NSObject {
470
487
open var properties : [ HTTPCookiePropertyKey : Any ] ? {
471
488
return _properties
472
489
}
473
-
490
+
474
491
/// The version of the receiver.
475
492
///
476
493
/// Version 0 maps to "old-style" Netscape cookies.
477
494
/// Version 1 maps to RFC2965 cookies. There may be future versions.
478
495
open var version : Int {
479
496
return _version
480
497
}
481
-
498
+
482
499
/// The name of the receiver.
483
500
open var name : String {
484
501
return _name
485
502
}
486
-
503
+
487
504
/// The value of the receiver.
488
505
open var value : String {
489
506
return _value
490
507
}
491
-
508
+
492
509
/// Returns The expires date of the receiver.
493
510
///
494
511
/// The expires date is the date when the cookie should be
@@ -497,7 +514,7 @@ open class HTTPCookie : NSObject {
497
514
/*@NSCopying*/ open var expiresDate : Date ? {
498
515
return _expiresDate
499
516
}
500
-
517
+
501
518
/// Whether the receiver is session-only.
502
519
///
503
520
/// `true` if this receiver should be discarded at the end of the
@@ -506,7 +523,7 @@ open class HTTPCookie : NSObject {
506
523
open var isSessionOnly : Bool {
507
524
return _sessionOnly
508
525
}
509
-
526
+
510
527
/// The domain of the receiver.
511
528
///
512
529
/// This value specifies URL domain to which the cookie
@@ -535,7 +552,7 @@ open class HTTPCookie : NSObject {
535
552
open var isSecure : Bool {
536
553
return _secure
537
554
}
538
-
555
+
539
556
/// Whether the receiver should only be sent to HTTP servers per RFC 2965
540
557
///
541
558
/// Cookies may be marked as HTTPOnly by a server (or by a javascript).
@@ -546,7 +563,7 @@ open class HTTPCookie : NSObject {
546
563
open var isHTTPOnly : Bool {
547
564
return _HTTPOnly
548
565
}
549
-
566
+
550
567
/// The comment of the receiver.
551
568
///
552
569
/// This value specifies a string which is suitable for
@@ -555,7 +572,7 @@ open class HTTPCookie : NSObject {
555
572
open var comment : String ? {
556
573
return _comment
557
574
}
558
-
575
+
559
576
/// The comment URL of the receiver.
560
577
///
561
578
/// This value specifies a URL which is suitable for
@@ -564,7 +581,7 @@ open class HTTPCookie : NSObject {
564
581
/*@NSCopying*/ open var commentURL : URL ? {
565
582
return _commentURL
566
583
}
567
-
584
+
568
585
/// The list ports to which the receiver should be sent.
569
586
///
570
587
/// This value specifies an NSArray of NSNumbers
0 commit comments