File: NSHTTPCookieStorage.m

package info (click to toggle)
gnustep-base 1.28.1%2Breally1.28.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,008 kB
  • sloc: objc: 223,137; ansic: 35,562; sh: 184; makefile: 128; cpp: 122; xml: 32
file content (344 lines) | stat: -rw-r--r-- 9,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
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
/* Implementation for NSHTTPCookieStorage for GNUstep
   Copyright (C) 2006 Software Foundation, Inc.

   Written by:  Richard Frith-Macdonald <rfm@gnu.org>
   Date: 2006
   
   This file is part of the GNUstep Base Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110 USA.
   */ 

#import "common.h"
#define	EXPOSE_NSHTTPCookieStorage_IVARS	1
#import "GSURLPrivate.h"
#import "Foundation/NSSet.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSString.h"
#import "Foundation/NSDistributedNotificationCenter.h"

NSString *objectObserver = @"org.GNUstep.NSHTTPCookieStorage";

// Internal data storage
typedef struct {
  NSHTTPCookieAcceptPolicy	_policy;
  NSMutableArray		*_cookies;
} Internal;
 
#define	this	((Internal*)(self->_NSHTTPCookieStorageInternal))
#define	inst	((Internal*)(o->_NSHTTPCookieStorageInternal))

@interface NSHTTPCookieStorage (Private)
- (void) _updateFromCookieStore;
@end

@implementation NSHTTPCookieStorage

static NSHTTPCookieStorage   *storage = nil;

+ (id) allocWithZone: (NSZone*)z
{
  return RETAIN([self sharedHTTPCookieStorage]);
}

+ (NSHTTPCookieStorage *) sharedHTTPCookieStorage
{
  if (storage == nil)
    {
      [gnustep_global_lock lock];
      if (storage == nil)
        {
	  NSHTTPCookieStorage	*o;

	  o = (NSHTTPCookieStorage*)
	    NSAllocateObject(self, 0, NSDefaultMallocZone());
	  o->_NSHTTPCookieStorageInternal = (Internal*)
	    NSZoneCalloc(NSDefaultMallocZone(), 1, sizeof(Internal));
	  [o init];
	  storage = o;
	}
      [gnustep_global_lock unlock];
    }
  return storage;
}

- init
{
  this->_policy = NSHTTPCookieAcceptPolicyAlways;
  this->_cookies = [NSMutableArray new];
  [[NSDistributedNotificationCenter defaultCenter] 
    addObserver: self
    selector: @selector(cookiesChangedNotification:)
    name: NSHTTPCookieManagerCookiesChangedNotification
    object: objectObserver];
  [[NSDistributedNotificationCenter defaultCenter] 
    addObserver: self
    selector: @selector(acceptPolicyChangeNotification:)
    name: NSHTTPCookieManagerAcceptPolicyChangedNotification
    object: objectObserver];
  [self _updateFromCookieStore];
  return self;
}

- (void) dealloc
{
  if (this != 0)
    {
      [[NSDistributedNotificationCenter defaultCenter] removeObserver: self];
      RELEASE(this->_cookies);
      NSZoneFree([self zone], this);
    }
  [super dealloc];
}

- (NSString *) _cookieStorePath
{
  BOOL isDir;
  NSString *path;
  NSArray *dirs;

  dirs = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, 
  	  NSUserDomainMask, YES);
  path = [[dirs objectAtIndex: 0] stringByAppendingPathComponent: @"Cookies"];
  if ([[NSFileManager defaultManager] 
    fileExistsAtPath: path isDirectory: &isDir] == NO || isDir == NO)
    {
      BOOL ok;

      ok = [[NSFileManager defaultManager] createDirectoryAtPath: path 
                                     withIntermediateDirectories: YES
                                                      attributes: nil
                                                           error: NULL];
      if (ok == NO)
        return nil; 
    }
  path = [path stringByAppendingPathComponent: @"Cookies.plist"];
  return path;
}

/* Remove all cookies that have expired */
/* FIXME: When will we know that the user session expired? */
- (BOOL) _expireCookies: (BOOL)endUserSession
{
  BOOL changed = NO;
  NSDate *now = [NSDate date];
  unsigned count = [this->_cookies count];

  /* FIXME: Handle Max-age */
  while (count-- > 0)
    {
      NSHTTPCookie	*ck = [this->_cookies objectAtIndex: count];
      NSDate *expDate = [ck expiresDate];
      if ((endUserSession && expDate == nil) ||
	  (expDate != nil && [expDate compare: now] != NSOrderedDescending))
        {
          [this->_cookies removeObject: ck];
	  changed = YES;
	}
    }
  return changed;
}

- (void) _updateFromCookieStore
{
  int i;
  NSArray *properties;
  NSString *path = [self _cookieStorePath];

  if (path == nil)
    {
      return;
    }
  properties = nil;
  NS_DURING
    if (YES == [[NSFileManager defaultManager] fileExistsAtPath: path])
      {
        properties = [[NSString stringWithContentsOfFile: path] propertyList];
      }
  NS_HANDLER
    NSLog(@"NSHTTPCookieStorage: Error reading cookies plist");
  NS_ENDHANDLER
  if (nil == properties)
    return;
  for (i = 0; i < [properties count]; i++)
    {
      NSDictionary *props;
      NSHTTPCookie *cookie;

      props = [properties objectAtIndex: i];
      cookie = [NSHTTPCookie cookieWithProperties: props];
      if (NO == [this->_cookies containsObject: cookie])
	{
	  [this->_cookies addObject:cookie];
	}
    }
}

- (void) _updateToCookieStore
{
  int i, count;
  NSMutableArray *properties;
  NSString *path = [self _cookieStorePath];

  if (path == nil)
    {
      return;
    }
  count = [this->_cookies count];
  properties = [NSMutableArray arrayWithCapacity: count];
  for (i = 0; i < count; i++) 
    [properties addObject: [[this->_cookies objectAtIndex: i] properties]];
  [properties writeToFile: path atomically: YES];
}

- (void) _doExpireUpdateAndNotify
{
  [self _expireCookies: NO];
  [self _updateToCookieStore];
  [[NSDistributedNotificationCenter defaultCenter] 
    postNotificationName: NSHTTPCookieManagerCookiesChangedNotification
    object: objectObserver];
}

- (void) cookiesChangedNotification: (NSNotification *)note
{
  if ([note object] == self)
    return;
  [self _updateFromCookieStore];
}

- (void) acceptPolicyChangeNotification: (NSNotification *)note
{
  if ([note object] == self)
    return;
  /* FIXME: Do we need a common place to store the policy? */
}

- (NSHTTPCookieAcceptPolicy) cookieAcceptPolicy
{
  return this->_policy;
}

- (NSArray *) cookies
{
  return [[this->_cookies copy] autorelease];
}

- (NSArray *) cookiesForURL: (NSURL *)URL
{
  NSMutableArray *a = [NSMutableArray array];
  NSEnumerator *ckenum = [this->_cookies objectEnumerator];
  NSHTTPCookie *cookie;
  NSString *receive_domain = [URL host];

  while ((cookie = [ckenum nextObject]))
    {
      if ([receive_domain hasSuffix: [cookie domain]])
        [a addObject: cookie];
    }
  return a;
}

- (void) deleteCookie: (NSHTTPCookie *)cookie
{
  if ([this->_cookies indexOfObject: cookie] != NSNotFound)
    {
      [this->_cookies removeObject: cookie];
      [self _doExpireUpdateAndNotify];
    }
  else
    NSLog(@"NSHTTPCookieStorage: trying to delete a cookie that is not in the storage");
}

- (void) _setCookieNoNotify: (NSHTTPCookie *)cookie
{
  NSEnumerator *ckenum = [this->_cookies objectEnumerator];
  NSHTTPCookie *ck, *remove_ck;
  NSString *name = [cookie name];
  NSString *path = [cookie path];
  NSString *domain = [cookie domain];

  NSAssert([cookie isKindOfClass: [NSHTTPCookie class]] == YES,
    NSInvalidArgumentException);
  
  remove_ck = nil;
  while ((ck = [ckenum nextObject]))
    {
      if ([name isEqual: [ck name]] && [path isEqual: [ck path]])
        {
	  /* The Apple documentation says the domain should match and 
	  RFC 2965 says they should match, though the original Netscape docs 
	  doesn't mention that the domain should match, so here, if the
	  version is explicitely set to 0, we don't require it */
	  id ckv = [[ck properties] objectForKey: NSHTTPCookieVersion];
	  if ((ckv && [ckv intValue] == 0) || [domain isEqual: [ck domain]])
	    {
	      remove_ck = ck;
	      break;
	    }
	}
    }
  if (remove_ck)
    [this->_cookies removeObject: remove_ck];
  
  [this->_cookies addObject: cookie];
}

- (void) setCookie: (NSHTTPCookie *)cookie
{
  if (this->_policy == NSHTTPCookieAcceptPolicyNever)
    return;
  [self _setCookieNoNotify: cookie];
  [self _doExpireUpdateAndNotify];
}

- (void) setCookieAcceptPolicy: (NSHTTPCookieAcceptPolicy)cookieAcceptPolicy
{
  this->_policy = cookieAcceptPolicy;
  [[NSDistributedNotificationCenter defaultCenter] 
    postNotificationName: NSHTTPCookieManagerAcceptPolicyChangedNotification
    object: objectObserver];
}

- (void) setCookies: (NSArray *)cookies
	     forURL: (NSURL *)URL
    mainDocumentURL: (NSURL *)mainDocumentURL
{
  BOOL     changed = NO;
  unsigned count = [cookies count];

  if (count == 0 || this->_policy == NSHTTPCookieAcceptPolicyNever)
    return;

  while (count-- > 0)
    {
      NSHTTPCookie	*ck = [cookies objectAtIndex: count];

      if (this->_policy == NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
          && [[URL host] hasSuffix: [mainDocumentURL host]] == NO)
	continue;

      [self _setCookieNoNotify: ck];
      changed = YES;
    }
  if (changed)
    [self _doExpireUpdateAndNotify];
}

@end