File: NSURLProtectionSpace.m

package info (click to toggle)
gnustep-base 1.31.1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 26,460 kB
  • sloc: objc: 239,505; ansic: 36,519; sh: 135; cpp: 122; makefile: 100; xml: 32
file content (309 lines) | stat: -rw-r--r-- 7,103 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
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
/* Implementation for NSURLProtectionSpace 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"

#define	EXPOSE_NSURLProtectionSpace_IVARS	1
#import "GSURLPrivate.h"
#import "GNUstepBase/NSURL+GNUstepBase.h"

// Internal data storage
typedef struct {
  NSString	*host;
  int		port;
  NSString	*protocol;
  NSString	*realm;
  NSString	*proxyType;		// Not retained
  NSString	*authenticationMethod;	// Not retained
  BOOL		isProxy;
} Internal;
 
#define	this	((Internal*)(self->_NSURLProtectionSpaceInternal))
#define	inst	((Internal*)(o->_NSURLProtectionSpaceInternal))

@implementation NSURLProtectionSpace

+ (id) allocWithZone: (NSZone*)z
{
  NSURLProtectionSpace	*o = [super allocWithZone: z];

  if (o != nil)
    {
      o->_NSURLProtectionSpaceInternal = NSZoneCalloc(z, 1, sizeof(Internal));
    }
  return o;
}

- (NSString *) authenticationMethod
{
  return this->authenticationMethod;
}

- (id) copyWithZone: (NSZone*)z
{
  if (NSShouldRetainWithZone(self, z) == YES)
    {
      return RETAIN(self);
    }
  else
    {
      NSURLProtectionSpace	*o = [[self class] allocWithZone: z];

      o = [o initWithHost: this->host
		     port: this->port
		 protocol: this->protocol
		    realm: this->realm
     authenticationMethod: this->authenticationMethod];
      if (o != nil)
	{
	  inst->isProxy = this->isProxy;
	  inst->proxyType = this->proxyType;
	}
      return o;
    }
}

- (void) dealloc
{
  if (this != 0)
    {
      RELEASE(this->host);
      RELEASE(this->protocol);
      RELEASE(this->realm);
      NSZoneFree([self zone], this);
    }
  [super dealloc];
}

- (NSUInteger) hash
{
  return [[self host] hash] + [self port]
    + [[self realm] hash] + [[self protocol] hash]
    + (uintptr_t)this->proxyType + (uintptr_t)this->authenticationMethod;
}

- (NSString *) host
{
  return this->host;
}

- (id) initWithHost: (NSString *)host
	       port: (NSInteger)port
	   protocol: (NSString *)protocol
	      realm: (NSString *)realm
authenticationMethod: (NSString *)authenticationMethod
{
  if ((self = [super init]) != nil)
    {
      this->host = [host copy];
      this->protocol = [protocol copy];
      this->realm = [realm copy];
      if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodHTMLForm] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodHTMLForm;
	}
      else if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodHTTPBasic] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodHTTPBasic;
	}
      else if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodHTTPDigest] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodHTTPDigest;
	}
      else if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodNTLM] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodNTLM;
	}
      else if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodNegotiate] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodNegotiate;
	}
      else if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodClientCertificate] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodClientCertificate;
	}
      else if ([authenticationMethod isEqualToString: 
	NSURLAuthenticationMethodServerTrust] == YES)
	{
	  this->authenticationMethod = NSURLAuthenticationMethodServerTrust;
	}
      else
        {
	  this->authenticationMethod = NSURLAuthenticationMethodDefault;
	}
      this->port = port;
      this->proxyType = nil;
      this->isProxy = NO;
    }
  return self;
}

- (id) initWithProxyHost: (NSString *)host
		    port: (NSInteger)port
		    type: (NSString *)type
		   realm: (NSString *)realm
    authenticationMethod: (NSString *)authenticationMethod
{
  self = [self initWithHost: host
		       port: port
		   protocol: nil
		      realm: realm
       authenticationMethod: authenticationMethod];
  if (self != nil)
    {
      this->isProxy = YES;
      if ([type isEqualToString: NSURLProtectionSpaceFTPProxy] == YES)
        {
	  this->proxyType = NSURLProtectionSpaceFTPProxy;
	}
      else if ([type isEqualToString: NSURLProtectionSpaceHTTPProxy] == YES)
        {
	  this->proxyType = NSURLProtectionSpaceHTTPProxy;
	}
      else if ([type isEqualToString: NSURLProtectionSpaceHTTPSProxy] == YES)
        {
	  this->proxyType = NSURLProtectionSpaceHTTPSProxy;
	}
      else if ([type isEqualToString: NSURLProtectionSpaceSOCKSProxy] == YES)
        {
	  this->proxyType = NSURLProtectionSpaceSOCKSProxy;
	}
      else
        {
	  DESTROY(self);	// Bad proxy type.
	}
    }
  return self;
}

- (BOOL) isEqual: (id)other
{
  if ((id)self == other)
    {
      return YES;
    }
  if ([other isKindOfClass: [NSURLProtectionSpace class]] == NO)
    {
      return NO;
    }
  else
    {
      NSURLProtectionSpace	*o = (NSURLProtectionSpace*)other;

      if ([[self host] isEqual: [o host]] == NO)
        {
	  return NO;
	}
      if ([[self realm] isEqual: [o realm]] == NO)
        {
	  return NO;
	}
      if ([self port] != [o port])
        {
	  return NO;
	}
      if ([[self authenticationMethod] isEqual: [o authenticationMethod]] == NO)
        {
	  return NO;
	}
      if ([self isProxy] == YES)
        {
          if ([o isProxy] == NO
	    || [[self proxyType] isEqual: [o proxyType]] == NO)
	    {
	      return NO;
	    }
	}
      else
        {
          if ([o isProxy] == YES
	    || [[self protocol] isEqual: [o protocol]] == NO)
	    {
	      return NO;
	    }
	}
      return YES;
    }
}

- (BOOL) isProxy
{
  return this->isProxy;
}

- (NSInteger) port
{
  return this->port;
}

- (NSString *) protocol
{
  return this->protocol;
}

- (NSString *) proxyType
{
  return this->proxyType;
}

- (NSString *) realm
{
  return this->realm;
}

- (BOOL) receivesCredentialSecurely
{
  if (this->authenticationMethod == NSURLAuthenticationMethodHTTPDigest)
    {
      return YES;
    }
  if (this->isProxy)
    {
      if (this->proxyType == NSURLProtectionSpaceHTTPSProxy)
        {
	  return YES;
	}
    }
  else
    {
      if ([this->protocol isEqual: @"https"] == YES)
        {
	  return YES;
	}
    }
  return NO;
}

- (NSArray *) distinguishedNames
{
  return nil;
}

@end