File: NSURLCredentialStorage.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (195 lines) | stat: -rw-r--r-- 6,063 bytes parent folder | download | duplicates (2)
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
/* Implementation for NSURLCredentialStorage 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., 31 Milk Street #960789 Boston, MA 02196 USA.
   */ 

#import "common.h"
#import "GSPThread.h"

#define	EXPOSE_NSURLCredentialStorage_IVARS	1
#import "GSURLPrivate.h"

// Internal data storage
typedef struct {
  NSMutableDictionary	*credentials;
  NSMutableDictionary	*defaults;
} Internal;
 
#define	this	((Internal*)(self->_NSURLCredentialStorageInternal))

@implementation	NSURLCredentialStorage

static NSURLCredentialStorage	*storage = nil;

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

+ (NSURLCredentialStorage *) sharedCredentialStorage
{
  static gs_mutex_t	classLock = GS_MUTEX_INIT_STATIC;

  if (storage == nil)
    {
      GS_MUTEX_LOCK(classLock);
      if (storage == nil)
        {
	  NSURLCredentialStorage	*o;

	  o = (NSURLCredentialStorage*)
	    NSAllocateObject(self, 0, NSDefaultMallocZone());
	  o->_NSURLCredentialStorageInternal = (Internal*)
	    NSZoneCalloc(NSDefaultMallocZone(), 1, sizeof(Internal));
	  ((Internal*)(o->_NSURLCredentialStorageInternal))->credentials
	    = [NSMutableDictionary new];
	  ((Internal*)(o->_NSURLCredentialStorageInternal))->defaults
	    = [NSMutableDictionary new];
	  storage = o;
	}
      GS_MUTEX_UNLOCK(classLock);
    }
  return storage;
}

- (NSDictionary *) allCredentials
{
  NSMutableDictionary	*all;
  NSEnumerator		*enumerator;
  NSURLProtectionSpace	*space;

  all = [NSMutableDictionary dictionaryWithCapacity: [this->credentials count]];
  enumerator = [this->credentials keyEnumerator];
  while ((space = [enumerator nextObject]) != nil)
    {
      NSDictionary	*info = [[this->credentials objectForKey: space] copy];

      [all setObject: info forKey: space];
      RELEASE(info);
    }
  return all;
}

- (NSDictionary *) credentialsForProtectionSpace: (NSURLProtectionSpace *)space
{
  return AUTORELEASE([[this->credentials objectForKey: space] copy]);
}

- (void) dealloc
{
  [NSException raise: NSInternalInconsistencyException
  	      format: @"Attempt to deallocate credential storage"];
  GSNOSUPERDEALLOC;
}

- (NSURLCredential *) defaultCredentialForProtectionSpace:
  (NSURLProtectionSpace *)space
{
  return [this->defaults objectForKey: space];
}

// Should never be called.
- (id) init
{
  DESTROY(self);
  return nil;
}

- (void) removeCredential: (NSURLCredential *)credential
       forProtectionSpace: (NSURLProtectionSpace *)space
{
  if (credential == nil || ![credential isKindOfClass: [NSURLCredential class]])
    {
      [NSException raise: NSInvalidArgumentException
      		  format: @"[%@-%@] nil or bad  class for credential",
		  NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  if (space == nil || ![space isKindOfClass: [NSURLProtectionSpace class]])
    {
      [NSException raise: NSInvalidArgumentException
      		  format: @"[%@-%@] nil or bad  class for space",
		  NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  [[this->credentials objectForKey: space]
    removeObjectForKey: [credential user]];
}

/**
 * Sets credential in the storage for the protection space specified.<br />
 * This replaces any old value with the same username.
 */
- (void) setCredential: (NSURLCredential *)credential
    forProtectionSpace: (NSURLProtectionSpace *)space
{
  NSMutableDictionary	*info;

  if (credential == nil || ![credential isKindOfClass: [NSURLCredential class]])
    {
      [NSException raise: NSInvalidArgumentException
      		  format: @"[%@-%@] nil or bad  class for credential",
		  NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  if (space == nil || ![space isKindOfClass: [NSURLProtectionSpace class]])
    {
      [NSException raise: NSInvalidArgumentException
      		  format: @"[%@-%@] nil or bad  class for space",
		  NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  info = [this->credentials objectForKey: space];
  if (info == nil)
    {
      info = [NSMutableDictionary new];
      [this->credentials setObject: info forKey: space];
      RELEASE(info);
    }
  [info setObject: credential forKey: [credential user]];
}

/**
 * Sets the default credential for the protection space.  Also calls
 * -setCredential:forProtectionSpace: if the credential has not already
 * been set in space.
 */
- (void) setDefaultCredential: (NSURLCredential *)credential
	   forProtectionSpace: (NSURLProtectionSpace *)space
{
  if (credential == nil || ![credential isKindOfClass: [NSURLCredential class]])
    {
      [NSException raise: NSInvalidArgumentException
      		  format: @"[%@-%@] nil or bad  class for credential",
		  NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  if (space == nil || ![space isKindOfClass: [NSURLProtectionSpace class]])
    {
      [NSException raise: NSInvalidArgumentException
      		  format: @"[%@-%@] nil or bad  class for space",
		  NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
    }
  [this->defaults setObject: credential forKey: space];
  if ([[this->credentials objectForKey: space] objectForKey: [credential user]]
    != credential)
    {
      [self setCredential: credential forProtectionSpace: space];
    }
}

@end