Overview
Comment: | OFHTTPCookie: Make domain and path mandatory
This requires passing a domain and defaults the path to /. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
f36f9bed028df7d2e1ab8c074256187f |
User & Date: | js on 2017-05-15 21:35:12 |
Other Links: | manifest | tags |
Context
2017-05-15
| ||
21:41 | Remove cookies from OFHTTP{Request,Response} check-in: fedbbb2344 user: js tags: trunk | |
21:35 | OFHTTPCookie: Make domain and path mandatory check-in: f36f9bed02 user: js tags: trunk | |
2017-05-14
| ||
23:37 | OFDataArray+MessagePackValue: Use memcpy() check-in: fe4ee8b458 user: js tags: trunk | |
Changes
Modified src/OFHTTPCookie.h from [a664df5bcf] to [a02e34311d].
︙ | ︙ | |||
12 13 14 15 16 17 18 | * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #import "OFString.h" | < < > > > > > > | < < < < < < | > > > > > | | 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 | * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #import "OFString.h" OF_ASSUME_NONNULL_BEGIN @class OFArray OF_GENERIC(ObjectType); @class OFDate; @class OFDictionary OF_GENERIC(KeyType, ObjectType); @class OFMutableArray OF_GENERIC(ObjectType); @class OFURL; /*! * @class OFHTTPCookie OFHTTPCookie.h ObjFW/OFHTTPCookie.h * * @brief A class for storing and manipulating HTTP cookies. */ @interface OFHTTPCookie: OFObject <OFCopying> { OFString *_name, *_value, *_domain, *_path; OFDate *_expires; bool _secure, _HTTPOnly; OFMutableArray OF_GENERIC(OFString *) *_extensions; } /*! * The name of the cookie. */ @property (nonatomic, copy) OFString *name; /*! * The value of the cookie. */ @property (nonatomic, copy) OFString *value; /*! * The domain for the cookie. */ @property (nonatomic, copy) OFString *domain; /*! * The path for the cookie. */ @property (nonatomic, copy) OFString *path; /*! * The date when the cookie expires. */ @property OF_NULLABLE_PROPERTY (nonatomic, copy) OFDate *expires; /*! * Whether the cookie is only to be used with HTTPS. */ @property (nonatomic, getter=isSecure) bool secure; /*! |
︙ | ︙ | |||
77 78 79 80 81 82 83 84 85 86 | OFMutableArray OF_GENERIC(OFString *) *extensions; /*! * @brief Create a new cookie with the specified name and value. * * @param name The name of the cookie * @param value The value of the cookie * @return A new, autoreleased OFHTTPCookie */ + (instancetype)cookieWithName: (OFString *)name | > | > | > | > > > | > | 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 | OFMutableArray OF_GENERIC(OFString *) *extensions; /*! * @brief Create a new cookie with the specified name and value. * * @param name The name of the cookie * @param value The value of the cookie * @param domain The domain for the cookie * @return A new, autoreleased OFHTTPCookie */ + (instancetype)cookieWithName: (OFString *)name value: (OFString *)value domain: (OFString *)domain; /*! * @brief Parses the specified string and returns an array of cookies. * * @param headers The headers to parse * @param URL The URL for the cookies to parse * @return An array of cookies */ + (OFArray OF_GENERIC(OFHTTPCookie *) *)cookiesFromHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers forURL: (OFURL *)URL; - init OF_UNAVAILABLE; /*! * @brief Initializes an already allocated new cookie with the specified name * and value. * * @param name The name of the cookie * @param value The value of the cookie * @param domain The domain for the cookie * @return An initialized OFHTTPCookie */ - initWithName: (OFString *)name value: (OFString *)value domain: (OFString *)domain OF_DESIGNATED_INITIALIZER; @end OF_ASSUME_NONNULL_END |
Modified src/OFHTTPCookie.m from [7df1adeb38] to [fe9eb6c85d].
︙ | ︙ | |||
13 14 15 16 17 18 19 20 21 22 23 24 25 26 | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFHTTPCookie.h" #import "OFInvalidFormatException.h" static void handleAttribute(OFHTTPCookie *cookie, OFString *name, OFString *value) { OFString *lowerName = [name lowercaseString]; | > > > > | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFHTTPCookie.h" #import "OFArray.h" #import "OFDate.h" #import "OFDictionary.h" #import "OFURL.h" #import "OFInvalidFormatException.h" static void handleAttribute(OFHTTPCookie *cookie, OFString *name, OFString *value) { OFString *lowerName = [name lowercaseString]; |
︙ | ︙ | |||
49 50 51 52 53 54 55 | [cookie setHTTPOnly: true]; else if ([name length] > 0) [[cookie extensions] addObject: name]; } } @implementation OFHTTPCookie | | | > | > | > > > > | 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 | [cookie setHTTPOnly: true]; else if ([name length] > 0) [[cookie extensions] addObject: name]; } } @implementation OFHTTPCookie @synthesize name = _name, value = _value, domain = _domain, path = _path; @synthesize expires = _expires, secure = _secure, HTTPOnly = _HTTPOnly; @synthesize extensions = _extensions; + (instancetype)cookieWithName: (OFString *)name value: (OFString *)value domain: (OFString *)domain { return [[[self alloc] initWithName: name value: value domain: domain] autorelease]; } + (OFArray OF_GENERIC(OFHTTPCookie *) *)cookiesFromHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers forURL: (OFURL *)URL { OFMutableArray OF_GENERIC(OFHTTPCookie *) *ret = [OFMutableArray array]; void *pool = objc_autoreleasePoolPush(); OFString *string = [headers objectForKey: @"Set-Cookie"]; OFString *domain = [URL host]; const of_unichar_t *characters = [string characters]; size_t length = [string length], last = 0; enum { STATE_PRE_NAME, STATE_NAME, STATE_EXPECT_VALUE, STATE_VALUE, |
︙ | ︙ | |||
113 114 115 116 117 118 119 | case STATE_VALUE: if (characters[i] == ';' || characters[i] == ',') { value = [string substringWithRange: of_range(last, i - last)]; [ret addObject: [OFHTTPCookie cookieWithName: name | | > | > | 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 | case STATE_VALUE: if (characters[i] == ';' || characters[i] == ',') { value = [string substringWithRange: of_range(last, i - last)]; [ret addObject: [OFHTTPCookie cookieWithName: name value: value domain: domain]]; state = (characters[i] == ';' ? STATE_PRE_ATTR_NAME : STATE_PRE_NAME); } break; case STATE_QUOTED_VALUE: if (characters[i] == '"') { value = [string substringWithRange: of_range(last, i - last)]; [ret addObject: [OFHTTPCookie cookieWithName: name value: value domain: domain]]; state = STATE_POST_QUOTED_VALUE; } break; case STATE_POST_QUOTED_VALUE: if (characters[i] == ';') state = STATE_PRE_ATTR_NAME; |
︙ | ︙ | |||
211 212 213 214 215 216 217 | case STATE_QUOTED_VALUE: @throw [OFInvalidFormatException exception]; break; case STATE_VALUE: value = [string substringWithRange: of_range(last, length - last)]; [ret addObject: [OFHTTPCookie cookieWithName: name | | > | > | 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | case STATE_QUOTED_VALUE: @throw [OFInvalidFormatException exception]; break; case STATE_VALUE: value = [string substringWithRange: of_range(last, length - last)]; [ret addObject: [OFHTTPCookie cookieWithName: name value: value domain: domain]]; break; /* We end up here if the cookie is just foo= */ case STATE_EXPECT_VALUE: [ret addObject: [OFHTTPCookie cookieWithName: name value: @"" domain: domain]]; break; case STATE_ATTR_NAME: if (last != length) { name = [string substringWithRange: of_range(last, length - last)]; handleAttribute([ret lastObject], name, nil); |
︙ | ︙ | |||
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 | - init { OF_INVALID_INIT_METHOD } - initWithName: (OFString *)name value: (OFString *)value { self = [super init]; @try { _name = [name copy]; _value = [value copy]; _extensions = [[OFMutableArray alloc] init]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_name release]; [_value release]; | > > > < > < < > > | | | | > < < > > > < < < < < < | 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 | - init { OF_INVALID_INIT_METHOD } - initWithName: (OFString *)name value: (OFString *)value domain: (OFString *)domain { self = [super init]; @try { _name = [name copy]; _value = [value copy]; _domain = [domain copy]; _path = @"/"; _extensions = [[OFMutableArray alloc] init]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_name release]; [_value release]; [_domain release]; [_path release]; [_expires release]; [_extensions release]; [super dealloc]; } - (bool)isEqual: (id)otherObject { OFHTTPCookie *other; if (![otherObject isKindOfClass: [OFHTTPCookie class]]) return false; other = otherObject; if (![_name isEqual: other->_name]) return false; if (![_value isEqual: other->_value]) return false; if (_domain != other->_domain && ![_domain isEqual: other->_domain]) return false; if (_path != other->_path && ![_path isEqual: other->_path]) return false; if (_expires != other->_expires && ![_expires isEqual: other->_expires]) return false; if (_secure != other->_secure) return false; if (_HTTPOnly != other->_HTTPOnly) return false; if (_extensions != other->_extensions && ![_extensions isEqual: other->_extensions]) return false; return true; } - (uint32_t)hash { uint32_t hash; OF_HASH_INIT(hash); OF_HASH_ADD_HASH(hash, [_name hash]); OF_HASH_ADD_HASH(hash, [_value hash]); OF_HASH_ADD_HASH(hash, [_domain hash]); OF_HASH_ADD_HASH(hash, [_path hash]); OF_HASH_ADD_HASH(hash, [_expires hash]); OF_HASH_ADD(hash, _secure); OF_HASH_ADD(hash, _HTTPOnly); OF_HASH_ADD_HASH(hash, [_extensions hash]); OF_HASH_FINALIZE(hash); return hash; } - copy { OFHTTPCookie *copy = [[OFHTTPCookie alloc] initWithName: _name value: _value domain: _domain]; @try { copy->_path = [_path copy]; copy->_expires = [_expires copy]; copy->_secure = _secure; copy->_HTTPOnly = _HTTPOnly; [copy->_extensions addObjectsFromArray: _extensions]; } @catch (id e) { [copy release]; @throw e; } return copy; } - (OFString *)description { OFMutableString *ret = [OFMutableString stringWithFormat: @"%@=%@", _name, _value]; void *pool = objc_autoreleasePoolPush(); [ret appendFormat: @"; Domain=%@; Path=%@", _domain, _path]; if (_expires != nil) [ret appendString: [_expires dateStringWithFormat: @"; Expires=%a, %d %b %Y " @"%H:%M:%S +0000"]]; if (_secure) [ret appendString: @"; Secure"]; if (_HTTPOnly) [ret appendString: @"; HTTPOnly"]; if ([_extensions count] > 0) |
︙ | ︙ |
Modified src/OFHTTPCookieManager.m from [eb2ac21f4b] to [7266c82110].
︙ | ︙ | |||
14 15 16 17 18 19 20 21 22 23 24 25 26 27 | * file. */ #include "config.h" #import "OFHTTPCookieManager.h" #import "OFArray.h" #import "OFHTTPCookie.h" #import "OFURL.h" @implementation OFHTTPCookieManager + (instancetype)manager { return [[[self alloc] init] autorelease]; | > | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | * file. */ #include "config.h" #import "OFHTTPCookieManager.h" #import "OFArray.h" #import "OFDate.h" #import "OFHTTPCookie.h" #import "OFURL.h" @implementation OFHTTPCookieManager + (instancetype)manager { return [[[self alloc] init] autorelease]; |
︙ | ︙ | |||
56 57 58 59 60 61 62 | - (void)addCookie: (OFHTTPCookie *)cookie forURL: (OFURL *)URL { void *pool = objc_autoreleasePoolPush(); OFString *cookieDomain, *URLHost; size_t i; | < < < | | 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | - (void)addCookie: (OFHTTPCookie *)cookie forURL: (OFURL *)URL { void *pool = objc_autoreleasePoolPush(); OFString *cookieDomain, *URLHost; size_t i; if (![[cookie path] hasPrefix: @"/"]) [cookie setPath: @"/"]; if ([cookie isSecure] && ![[URL scheme] isEqual: @"https"]) { objc_autoreleasePoolPop(pool); return; } |
︙ | ︙ |
Modified tests/OFHTTPCookieManagerTests.m from [db6e110ccc] to [0cc34f9e4e].
︙ | ︙ | |||
13 14 15 16 17 18 19 20 21 22 23 24 25 26 | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFHTTPCookieManager.h" #import "OFHTTPCookie.h" #import "OFURL.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h" static OFString *module = @"OFHTTPCookieManager"; | > > | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFHTTPCookieManager.h" #import "OFArray.h" #import "OFDate.h" #import "OFHTTPCookie.h" #import "OFURL.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h" static OFString *module = @"OFHTTPCookieManager"; |
︙ | ︙ | |||
35 36 37 38 39 40 41 | URL[0] = [OFURL URLWithString: @"http://heap.zone/foo"]; URL[1] = [OFURL URLWithString: @"https://heap.zone/foo/bar"]; URL[2] = [OFURL URLWithString: @"https://test.heap.zone/foo/bar"]; URL[3] = [OFURL URLWithString: @"http://webkeks.org/foo/bar"]; cookie[0] = [OFHTTPCookie cookieWithName: @"test" | | > | | | > | > | | | 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 | URL[0] = [OFURL URLWithString: @"http://heap.zone/foo"]; URL[1] = [OFURL URLWithString: @"https://heap.zone/foo/bar"]; URL[2] = [OFURL URLWithString: @"https://test.heap.zone/foo/bar"]; URL[3] = [OFURL URLWithString: @"http://webkeks.org/foo/bar"]; cookie[0] = [OFHTTPCookie cookieWithName: @"test" value: @"1" domain: @"heap.zone"]; TEST(@"-[addCookie:forURL:] #1", R([manager addCookie: cookie[0] forURL: URL[0]])) TEST(@"-[cookiesForURL:] #1", [[manager cookiesForURL: URL[0]] isEqual: [OFArray arrayWithObject: cookie[0]]]) cookie[1] = [OFHTTPCookie cookieWithName: @"test" value: @"2" domain: @"webkeks.org"]; TEST(@"-[addCookie:forURL:] #2", R([manager addCookie: cookie[1] forURL: URL[0]])) TEST(@"-[cookiesForURL:] #2", [[manager cookiesForURL: URL[0]] isEqual: [OFArray arrayWithObject: cookie[0]]] && [[manager cookiesForURL: URL[3]] isEqual: [OFArray array]]) cookie[2] = [OFHTTPCookie cookieWithName: @"test" value: @"3" domain: @"heap.zone"]; [cookie[2] setSecure: true]; TEST(@"-[addCookie:forURL:] #3", R([manager addCookie: cookie[2] forURL: URL[1]])) TEST(@"-[cookiesForURL:] #3", [[manager cookiesForURL: URL[1]] isEqual: [OFArray arrayWithObject: cookie[2]]] && [[manager cookiesForURL: URL[0]] isEqual: [OFArray array]]) [cookie[2] setExpires: [OFDate dateWithTimeIntervalSinceNow: -1]]; cookie[3] = [OFHTTPCookie cookieWithName: @"test" value: @"4" domain: @"heap.zone"]; [cookie[3] setDomain: @".heap.zone"]; TEST(@"-[addCookie:forURL:] #4", R([manager addCookie: cookie[3] forURL: URL[1]])) TEST(@"-[cookiesForURL:] #4", [[manager cookiesForURL: URL[1]] isEqual: [OFArray arrayWithObject: cookie[3]]] && [[manager cookiesForURL: URL[2]] isEqual: [OFArray arrayWithObject: cookie[3]]]) cookie[4] = [OFHTTPCookie cookieWithName: @"bar" value: @"5" domain: @"test.heap.zone"]; TEST(@"-[addCookie:forURL:] #5", R([manager addCookie: cookie[4] forURL: URL[0]])) TEST(@"-[cookiesForURL:] #5", [[manager cookiesForURL: URL[0]] isEqual: [OFArray arrayWithObject: cookie[3]]] && [[manager cookiesForURL: URL[2]] isEqual: |
︙ | ︙ |
Modified tests/OFHTTPCookieTests.m from [2e9348ac5f] to [c2cad19355].
︙ | ︙ | |||
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFHTTPCookie.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h" static OFString *module = @"OFHTTPCookie"; @implementation TestsAppDelegate (OFHTTPCookieTests) - (void)HTTPCookieTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFHTTPCookie *cookie[2]; cookie[0] = [OFHTTPCookie cookieWithName: @"foo" | > > > > > | > | > > | | > > | > | | | > | > | | 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 | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFHTTPCookie.h" #import "OFArray.h" #import "OFDate.h" #import "OFDictionary.h" #import "OFURL.h" #import "OFAutoreleasePool.h" #import "TestsAppDelegate.h" static OFString *module = @"OFHTTPCookie"; @implementation TestsAppDelegate (OFHTTPCookieTests) - (void)HTTPCookieTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFURL *URL = [OFURL URLWithString: @"http://heap.zone"]; OFHTTPCookie *cookie[2]; cookie[0] = [OFHTTPCookie cookieWithName: @"foo" value: @"bar" domain: @"heap.zone"]; TEST(@"+[cookiesForString:] #1", [[OFHTTPCookie cookiesFromHeaders: [OFDictionary dictionaryWithObject: @"foo=bar" forKey: @"Set-Cookie"] forURL: URL] isEqual: [OFArray arrayWithObject: cookie[0]]]) cookie[1] = [OFHTTPCookie cookieWithName: @"qux" value: @"cookie" domain: @"heap.zone"]; TEST(@"+[cookiesForString:] #2", [[OFHTTPCookie cookiesFromHeaders: [OFDictionary dictionaryWithObject: @"foo=bar,qux=cookie" forKey: @"Set-Cookie"] forURL: URL] isEqual: [OFArray arrayWithObjects: cookie[0], cookie[1], nil]]) [cookie[0] setExpires: [OFDate dateWithTimeIntervalSince1970: 1234567890]]; [cookie[1] setExpires: [OFDate dateWithTimeIntervalSince1970: 1234567890]]; [cookie[0] setPath: @"/x"]; [cookie[1] setDomain: @"webkeks.org"]; [cookie[1] setPath: @"/objfw"]; [cookie[1] setSecure: true]; [cookie[1] setHTTPOnly: true]; [[cookie[1] extensions] addObject: @"foo"]; [[cookie[1] extensions] addObject: @"bar"]; TEST(@"+[cookiesForString:] #3", [[OFHTTPCookie cookiesFromHeaders: [OFDictionary dictionaryWithObject: @"foo=bar; Expires=Fri, 13 Feb 2009 23:31:30 GMT; Path=/x," @"qux=cookie; Expires=Fri, 13 Feb 2009 23:31:30 GMT; " @"Domain=webkeks.org; Path=/objfw; Secure; HTTPOnly; foo; bar" forKey: @"Set-Cookie"] forURL: URL] isEqual: [OFArray arrayWithObjects: cookie[0], cookie[1], nil]]) [pool drain]; } @end |