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 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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/* Implementation for NSURLConnection 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_NSURLConnection_IVARS 1
#import "Foundation/NSError.h"
#import "Foundation/NSURLError.h"
#import "Foundation/NSRunLoop.h"
#import "GSURLPrivate.h"
@interface _NSURLConnectionDataCollector : NSObject
{
NSURLConnection *_connection; // Not retained
NSMutableData *_data;
NSError *_error;
NSURLResponse *_response;
BOOL _done;
}
- (NSData*) data;
- (BOOL) done;
- (NSError*) error;
- (NSURLResponse*) response;
- (void) setConnection: (NSURLConnection *)c;
@end
@implementation _NSURLConnectionDataCollector
- (void) dealloc
{
[_data release];
[_error release];
[_response release];
[super dealloc];
}
- (BOOL) done
{
return _done;
}
- (NSData*) data
{
return _data;
}
- (id) init
{
if (nil != (self = [super init]))
{
_data = [NSMutableData new]; // Empty data unless we get an error
}
return self;
}
- (NSError*) error
{
return _error;
}
- (NSURLResponse*) response
{
return _response;
}
- (void) setConnection: (NSURLConnection*)c
{
_connection = c; // Not retained ... the connection retains us
}
- (void) connection: (NSURLConnection *)connection
didFailWithError: (NSError *)error
{
ASSIGN(_error, error);
DESTROY(_data); // On error, we make the data nil
_done = YES;
}
- (void) connection: (NSURLConnection *)connection
didReceiveResponse: (NSURLResponse*)response
{
ASSIGN(_response, response);
}
- (void) connectionDidFinishLoading: (NSURLConnection *)connection
{
_done = YES;
}
- (void) connection: (NSURLConnection *)connection
didReceiveData: (NSData *)data
{
[_data appendData: data];
}
@end
typedef struct
{
NSMutableURLRequest *_request;
NSURLProtocol *_protocol;
id _delegate;
BOOL _debug;
} Internal;
#define this ((Internal*)(self->_NSURLConnectionInternal))
#define inst ((Internal*)(o->_NSURLConnectionInternal))
@implementation NSURLConnection
+ (id) allocWithZone: (NSZone*)z
{
NSURLConnection *o = [super allocWithZone: z];
if (o != nil)
{
o->_NSURLConnectionInternal = NSZoneCalloc([self zone],
1, sizeof(Internal));
}
return o;
}
+ (BOOL) canHandleRequest: (NSURLRequest *)request
{
return ([NSURLProtocol _classToHandleRequest: request] != nil);
}
+ (NSURLConnection *) connectionWithRequest: (NSURLRequest *)request
delegate: (id)delegate
{
NSURLConnection *o = [self alloc];
o = [o initWithRequest: request delegate: delegate];
return AUTORELEASE(o);
}
- (void) start
{
[this->_protocol startLoading];
}
- (void) cancel
{
[this->_protocol stopLoading];
DESTROY(this->_protocol);
DESTROY(this->_delegate);
}
- (id) delegate
{
return this->_delegate;
}
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop
forMode: (NSRunLoopMode)mode
{
NSArray *modes = [NSArray arrayWithObject: mode];
[aRunLoop performSelector: @selector(start)
target: self
argument: nil
order: 0
modes: modes];
}
- (void) unscheduleFromRunLoop: (NSRunLoop *)aRunLoop
forMode: (NSRunLoopMode)mode
{
[aRunLoop cancelPerformSelector: @selector(start)
target: self
argument: nil];
}
- (void) dealloc
{
if (this != 0)
{
[self cancel];
DESTROY(this->_request);
DESTROY(this->_delegate);
NSZoneFree([self zone], this);
_NSURLConnectionInternal = 0;
}
[super dealloc];
}
- (void) finalize
{
if (this != 0)
{
[self cancel];
}
}
- (id) initWithRequest: (NSURLRequest *)request
delegate: (id)delegate
startImmediately: (BOOL)startImmediately
{
if ((self = [super init]) != nil)
{
this->_request = [request mutableCopyWithZone: [self zone]];
/* Enrich the request with the appropriate HTTP cookies,
* if desired.
*/
if ([this->_request HTTPShouldHandleCookies] == YES)
{
NSArray *cookies;
cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]
cookiesForURL: [this->_request URL]];
if ([cookies count] > 0)
{
NSDictionary *headers;
NSEnumerator *enumerator;
NSString *header;
headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
enumerator = [headers keyEnumerator];
while (nil != (header = [enumerator nextObject]))
{
[this->_request addValue: [headers valueForKey: header]
forHTTPHeaderField: header];
}
}
}
/* According to bug #35686, Cocoa has a bizarre deviation from the
* convention that delegates are retained here.
* For compatibility we retain the delegate and release it again
* when the operation is over.
*/
this->_delegate = [delegate retain];
this->_protocol = [[NSURLProtocol alloc]
initWithRequest: this->_request
cachedResponse: nil
client: (id<NSURLProtocolClient>)self];
if (startImmediately == YES)
{
[this->_protocol startLoading];
}
this->_debug = GSDebugSet(@"NSURLConnection");
}
return self;
}
- (id) initWithRequest: (NSURLRequest *)request delegate: (id)delegate
{
return [self initWithRequest: request
delegate: delegate
startImmediately: YES];
}
@end
@implementation NSObject (NSURLConnectionDelegate)
- (void) connection: (NSURLConnection *)connection
didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
return;
}
- (void) connection: (NSURLConnection *)connection
didFailWithError: (NSError *)error
{
return;
}
- (void) connectionDidFinishLoading: (NSURLConnection *)connection
{
return;
}
- (void) connection: (NSURLConnection *)connection
didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
if ([challenge proposedCredential] == nil
|| [challenge previousFailureCount] > 0)
{
/* continue without a credential if there is no proposed credential
* at all or if an authentication failure has already happened.
*/
[[challenge sender]
continueWithoutCredentialForAuthenticationChallenge: challenge];
}
}
- (void) connection: (NSURLConnection *)connection
didReceiveData: (NSData *)data
{
return;
}
- (void) connection: (NSURLConnection *)connection
didReceiveResponse: (NSURLResponse *)response
{
return;
}
- (NSCachedURLResponse *) connection: (NSURLConnection *)connection
willCacheResponse: (NSCachedURLResponse *)cachedResponse
{
return cachedResponse;
}
- (NSURLRequest *) connection: (NSURLConnection *)connection
willSendRequest: (NSURLRequest *)request
redirectResponse: (NSURLResponse *)response
{
return request;
}
@end
@implementation NSURLConnection (NSURLConnectionSynchronousLoading)
+ (NSData *) sendSynchronousRequest: (NSURLRequest *)request
returningResponse: (NSURLResponse **)response
error: (NSError **)error
{
NSData *data = nil;
if (0 != response)
{
*response = nil;
}
if (0 != error)
{
*error = nil;
}
if ([self canHandleRequest: request] == YES)
{
_NSURLConnectionDataCollector *collector;
NSURLConnection *conn;
collector = [_NSURLConnectionDataCollector new];
conn = [[self alloc] initWithRequest: request delegate: collector];
if (nil != conn)
{
NSRunLoop *loop;
NSDate *limit;
[collector setConnection: conn];
loop = [NSRunLoop currentRunLoop];
limit = [[NSDate alloc] initWithTimeIntervalSinceNow:
[request timeoutInterval]];
while ([collector done] == NO && [limit timeIntervalSinceNow] > 0.0)
{
[loop runMode: NSDefaultRunLoopMode beforeDate: limit];
}
[limit release];
if (NO == [collector done])
{
data = nil;
if (0 != response)
{
*response = nil;
}
if (0 != error)
{
*error = [NSError errorWithDomain: NSURLErrorDomain
code: NSURLErrorTimedOut
userInfo: nil];
}
}
else
{
data = [[[collector data] retain] autorelease];
if (0 != response)
{
*response = [[[collector response] retain] autorelease];
}
if (0 != error)
{
*error = [[[collector error] retain] autorelease];
}
}
/* Cancel to prevent the NSURLProtocol instance retaining us.
*/
[conn cancel];
[conn release];
}
[collector release];
}
return data;
}
@end
@implementation NSURLConnection (URLProtocolClient)
- (void) URLProtocol: (NSURLProtocol *)protocol
cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse
{
return;
}
- (void) URLProtocol: (NSURLProtocol *)protocol
didFailWithError: (NSError *)error
{
id o = this->_delegate;
this->_delegate = nil;
[o connection: self didFailWithError: error];
DESTROY(o);
[this->_protocol stopLoading];
DESTROY(this->_protocol);
}
- (void) URLProtocol: (NSURLProtocol *)protocol
didLoadData: (NSData *)data
{
[this->_delegate connection: self didReceiveData: data];
}
- (void) URLProtocol: (NSURLProtocol *)protocol
didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
[this->_delegate connection: self
didReceiveAuthenticationChallenge: challenge];
}
- (void) URLProtocol: (NSURLProtocol *)protocol
didReceiveResponse: (NSURLResponse *)response
cacheStoragePolicy: (NSURLCacheStoragePolicy)policy
{
[this->_delegate connection: self didReceiveResponse: response];
if (policy == NSURLCacheStorageAllowed
|| policy == NSURLCacheStorageAllowedInMemoryOnly)
{
// FIXME ... cache response here?
}
}
- (void) URLProtocol: (NSURLProtocol *)protocol
wasRedirectedToRequest: (NSURLRequest *)request
redirectResponse: (NSURLResponse *)redirectResponse
{
if (this->_debug)
{
NSLog(@"%@ tell delegate %@ about redirect to %@ as a result of %@",
self, this->_delegate, request, redirectResponse);
}
request = [this->_delegate connection: self
willSendRequest: request
redirectResponse: redirectResponse];
if (this->_protocol == nil)
{
if (this->_debug)
{
NSLog(@"%@ delegate cancelled request", self);
}
/* Our protocol is nil, so we have been cancelled by the delegate.
*/
return;
}
if (request != nil)
{
if (this->_debug)
{
NSLog(@"%@ delegate allowed redirect to %@", self, request);
}
/* Follow the redirect ... stop the old load and start a new one.
*/
[this->_protocol stopLoading];
DESTROY(this->_protocol);
ASSIGNCOPY(this->_request, request);
this->_protocol = [[NSURLProtocol alloc]
initWithRequest: this->_request
cachedResponse: nil
client: (id<NSURLProtocolClient>)self];
[this->_protocol startLoading];
}
else if (this->_debug)
{
NSLog(@"%@ delegate cancelled redirect", self);
}
}
- (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol
{
id o = this->_delegate;
this->_delegate = nil;
[o connectionDidFinishLoading: self];
DESTROY(o);
[this->_protocol stopLoading];
DESTROY(this->_protocol);
}
- (void) URLProtocol: (NSURLProtocol *)protocol
didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
[this->_delegate connection: self
didCancelAuthenticationChallenge: challenge];
}
@end
|