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 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
|
/** NSURLHandle.m - Class NSURLHandle
Copyright (C) 1999 Free Software Foundation, Inc.
Written by: Manuel Guesdon <mguesdon@sbuilders.com>
Date: Jan 1999
Rewrite by: Richard Frith-Macdonald <rfm@gnu.org>
Date: Sep 2000, June 2002
This file is part of the GNUstep 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.
<title>NSURLHandle class reference</title>
$Date$ $Revision$
*/
#import "common.h"
#define EXPOSE_NSURLHandle_IVARS 1
#import "GSURLPrivate.h"
#import "Foundation/NSURLHandle.h"
#import "Foundation/NSRunLoop.h"
#import "Foundation/NSFileManager.h"
@class GSFTPURLHandle;
@interface GSFTPURLHandle : NSObject // Help the compiler
@end
@class GSHTTPURLHandle;
@interface GSHTTPURLHandle : NSObject // Help the compiler
@end
@interface GSFileURLHandle : NSURLHandle
{
NSString *_path;
NSMutableDictionary *_attributes;
}
@end
/**
* <p>
* An NSURLHandle instance is used to manage the resource data
* corresponding to an NSURL object. A single NSURLHandle can
* be used to manage multiple NSURL objects as long as those
* objects all refer to the same resource.
* </p>
* <p>
* Different NSURLHandle subclasses are used to manage different
* types of URL (usually based on the scheme of the URL), and you
* can register new subclasses to extend (or replace) the
* standard ones.
* </p>
* <p>
* GNUstep comes with private subclasses to handle the common
* URL schemes -
* </p>
* <list>
* <item>
* <code>file:</code> (local file I/O)
* </item>
* <item>
* <code>http:</code> and <code>https:</code> (webserver) access.
* </item>
* <item>
* <code>ftp:</code> (FTP server) access.
* </item>
* </list>
*/
@implementation NSURLHandle
static NSLock *registryLock = nil;
static NSMutableArray *registry = nil;
static Class NSURLHandleClass = 0;
/**
* Return a handle for the specified URL from the cache if possible.
* If the cache does not contain a matching handle, returns nil.
*/
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)url
{
/*
* Each subclass is supposed to do its own caching, so we must
* find the correct subclass and ask it for its cached handle.
*/
if (self == NSURLHandleClass)
{
Class c = [self URLHandleClassForURL: url];
if (c == self || c == 0)
{
return nil;
}
else
{
return [c cachedHandleForURL: url];
}
}
else
{
[self subclassResponsibility: _cmd];
return nil;
}
}
/** <override-subclass />
* Implemented by subclasses to say which URLs they can handle.
* This method is used to determine which subclasses can be used
* to handle a particular URL.
*/
+ (BOOL) canInitWithURL: (NSURL*)url
{
/*
* The semi-abstract base class can't handle ANY scheme
*/
return NO;
}
+ (void) initialize
{
if (self == [NSURLHandle class])
{
NSURLHandleClass = self;
registry = [NSMutableArray new];
[[NSObject leakAt: ®istry] release];
registryLock = [NSLock new];
[[NSObject leakAt: ®istryLock] release];
[self registerURLHandleClass: [GSFileURLHandle class]];
[self registerURLHandleClass: [GSFTPURLHandle class]];
[self registerURLHandleClass: [GSHTTPURLHandle class]];
}
}
/**
* Used to register a subclass as being available to handle URLs.
*/
+ (void) registerURLHandleClass: (Class)urlHandleSubclass
{
/*
* Maintain a registry of classes that handle various schemes
* Re-adding a class moves it to the end of the registry - so it will
* be used in preference to any class added earlier.
*/
[registryLock lock];
NS_DURING
{
[registry removeObjectIdenticalTo: urlHandleSubclass];
[registry addObject: urlHandleSubclass];
}
NS_HANDLER
{
[registryLock unlock];
[localException raise];
}
NS_ENDHANDLER
[registryLock unlock];
}
/**
* Returns the most recently registered NSURLHandle subclass that
* responds to +canInitWithURL: with YES.
* If there is no such subclass, returns nil.
*/
+ (Class) URLHandleClassForURL: (NSURL*)url
{
unsigned count;
Class c = 0;
[registryLock lock];
NS_DURING
{
count = [registry count];
/*
* Find a class to handle the URL, try most recently registered first.
*/
while (count-- > 0)
{
id found = [registry objectAtIndex: count];
if ([found canInitWithURL: url] == YES)
{
c = (Class)found;
break; // Found it.
}
}
}
NS_HANDLER
{
[registryLock unlock];
[localException raise];
}
NS_ENDHANDLER
[registryLock unlock];
return c;
}
/**
* Add a client object, making sure that it doesn't occur more than once.<br />
* The client object will receive messages notifying it of events on the handle.
*/
- (void) addClient: (id <NSURLHandleClient>)client
{
id o = client;
IF_NO_ARC([o retain];)
[_clients removeObjectIdenticalTo: o];
[_clients addObject: o];
IF_NO_ARC([o release];)
}
/**
* Returns the resource data that is currently available for the
* handle. This may be a partially loaded resource or may be
* empty if no data has been loaded yet or the last load failed.
*/
- (NSData*) availableResourceData
{
return AUTORELEASE([_data copy]);
}
/**
* This method should be called when a background load fails.<br />
* The method passes the failure notification to the clients of
* the handle - so subclasses should call super's implementation
* at the end of their implementation of this method.
*/
- (void) backgroundLoadDidFailWithReason: (NSString*)reason
{
NSEnumerator *enumerator = [_clients objectEnumerator];
id <NSURLHandleClient> client;
_status = NSURLHandleLoadFailed;
DESTROY(_data);
ASSIGNCOPY(_failure, reason);
while ((client = [enumerator nextObject]) != nil)
{
[client URLHandle: self resourceDidFailLoadingWithReason: _failure];
}
}
/**
* This method is called by when a background load begins.
* Subclasses should call super's implementation at
* the end of their implementation of this method.
*/
- (void) beginLoadInBackground
{
_status = NSURLHandleLoadInProgress;
DESTROY(_data);
_data = [NSMutableData new];
[_clients makeObjectsPerformSelector:
@selector(URLHandleResourceDidBeginLoading:)
withObject: self];
}
/**
* This method should be called to cancel a load currently in
* progress. The method calls -endLoadInBackground
* Subclasses should call super's implementation at
* the end of their implementation of this method.
*/
- (void) cancelLoadInBackground
{
IF_NO_ARC([self retain];)
[_clients makeObjectsPerformSelector:
@selector(URLHandleResourceDidCancelLoading:)
withObject: self];
[self endLoadInBackground];
IF_NO_ARC(RELEASE(self);)
}
- (void) dealloc
{
RELEASE(_data);
RELEASE(_failure);
RELEASE(_clients);
[super dealloc];
}
/**
* Method called by subclasses during process of loading a resource.
* The base class maintains a copy of the data being read in and
* accumulates separate parts of the data.
*/
- (void) didLoadBytes: (NSData*)newData
loadComplete: (BOOL)loadComplete
{
NSEnumerator *enumerator;
id <NSURLHandleClient> client;
/*
* Let clients know we are starting loading (unless this has already been
* done).
*/
if (_status != NSURLHandleLoadInProgress)
{
_status = NSURLHandleLoadInProgress;
DESTROY(_data);
_data = [NSMutableData new];
[_clients makeObjectsPerformSelector:
@selector(URLHandleResourceDidBeginLoading:)
withObject: self];
}
/*
* If we have been given nil data, there must have been a failure!
*/
if (newData == nil)
{
[self backgroundLoadDidFailWithReason: @"nil data"];
return;
}
/*
* Let clients know we have read some data.
*/
enumerator = [_clients objectEnumerator];
while ((client = [enumerator nextObject]) != nil)
{
[client URLHandle: self resourceDataDidBecomeAvailable: newData];
}
/*
* Accumulate data in cache.
*/
[_data appendData: newData];
if (loadComplete == YES)
{
id tmp = _data;
_data = [tmp copy];
RELEASE(tmp);
/*
* Let clients know we have finished loading.
*/
_status = NSURLHandleLoadSucceeded;
[_clients makeObjectsPerformSelector:
@selector(URLHandleResourceDidFinishLoading:)
withObject: self];
}
}
/**
* This method is called to stop any background loading process.
* -cancelLoadInBackground uses this method to cancel loading.
* Subclasses should call super's implementation at
* the end of their implementation of this method.
*/
- (void) endLoadInBackground
{
_status = NSURLHandleNotLoaded;
DESTROY(_data);
}
/**
* Returns the failure reason for the last failure to load
* the resource data.
*/
- (NSString*) failureReason
{
if (_status == NSURLHandleLoadFailed)
return _failure;
else
return nil;
}
/**
* Flushes any cached resource data.
*/
- (void) flushCachedData
{
DESTROY(_data);
}
- (id) init
{
return [self initWithURL: nil cached: NO];
}
/** <init />
* Initialises a handle with the specified URL.<br />
* The flag determines whether the handle will cache resource data
* and respond to requests from equivalent URLs for the cached data.
*/
- (id) initWithURL: (NSURL*)url
cached: (BOOL)cached
{
_status = NSURLHandleNotLoaded;
_clients = [NSMutableArray new];
return self;
}
/**
* Starts (or queues) loading of the handle's resource data
* in the background (asynchronously).<br />
* The default implementation uses loadInForeground -
* if this method is not overridden, loadInForeground MUST be.
*/
- (void) loadInBackground
{
NSData *d;
[self beginLoadInBackground];
d = [self loadInForeground];
if (d == nil)
{
[self backgroundLoadDidFailWithReason: @"foreground load returned nil"];
}
else
{
[self didLoadBytes: d loadComplete: YES];
}
}
/**
* Loads the handle's resource data in the foreground (synchronously).<br />
* The default implementation starts a background load and waits for
* it to complete -
* if this method is not overridden, loadInBackground MUST be.
*/
- (NSData*) loadInForeground
{
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[self loadInBackground];
while ([self status] == NSURLHandleLoadInProgress)
{
NSDate *limit;
limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 1.0];
[loop runMode: NSDefaultRunLoopMode beforeDate: limit];
RELEASE(limit);
}
return _data;
}
/** <override-subclass />
* Returns the property for the specified key, or nil if the
* key does not exist.
*/
- (id) propertyForKey: (NSString*)propertyKey
{
[self subclassResponsibility: _cmd];
return nil;
}
/** <override-subclass />
* Returns the property for the specified key, but only if the
* handle does not need to do any work to retrieve it.
*/
- (id) propertyForKeyIfAvailable: (NSString*)propertyKey
{
[self subclassResponsibility: _cmd];
return nil;
}
/**
* Removes an object from them list of clients notified of
* resource loading events by the URL handle.
*/
- (void) removeClient: (id <NSURLHandleClient>)client
{
[_clients removeObjectIdenticalTo: client];
}
/**
* Returns the resource data belonging to the handle.
* Calls -loadInForeground if necessary.
* <p>
* The GNUstep implementation treats an <em>ftp:</em> request for a
* directory as a request to list the names of the directory contents.
* </p>
*/
- (NSData*) resourceData
{
NSData *d = nil;
if (NSURLHandleLoadSucceeded == _status)
{
d = [self availableResourceData];
}
if (nil == d
&& _status != NSURLHandleLoadSucceeded
&& _status != NSURLHandleLoadFailed)
{
if (_status == NSURLHandleLoadInProgress)
{
return nil;
}
else
{
d = [self loadInForeground];
if (d != nil)
{
ASSIGNCOPY(_data, d);
}
}
}
return d;
}
/* Private method ... subclasses override this to enable debug to be
* turned off and on.
*/
- (int) setDebug: (int)aFlag
{
return 0;
}
- (void) setReturnAll: (BOOL)flag
{
return;
}
- (void) setURL: (NSURL*)newUrl
{
return;
}
/**
* Returns the current status of the handle.
*/
- (NSURLHandleStatus) status
{
return _status;
}
/**
* <p>
* Writes resource data to the handle. Returns YES on success,
* NO on failure.
* </p>
* <p>
* The GNUstep implementation for <em>file:</em> writes the data
* directly to the local filesystem, and the return status reflects
* the result of that write operation.
* </p>
* <p>
* The GNUstep implementation for <em>http:</em> and <em>https:</em>
* sets the specified data as information to be POSTed to the URL next
* time it is loaded - so the method always returns YES.
* </p>
* <p>
* The GNUstep implementation for <em>ftp:</em> sets the specified data
* as information to be written to the URL next time it is loaded - so
* the method always returns YES.
* </p>
*/
- (BOOL) writeData: (NSData*)data
{
[self subclassResponsibility: _cmd];
return NO;
}
/**
* <p>
* Sets a property for handle.
* Returns YES on success, NO on failure.
* </p>
* <p>
* The GNUstep implementation sets the property as a header
* to be sent the next time the URL is loaded, and recognizes
* some special property keys which control the behavior of
* the next load.
* </p>
*/
- (BOOL) writeProperty: (id)propertyValue
forKey: (NSString*)propertyKey
{
[self subclassResponsibility: _cmd];
return NO;
}
@end
/**
* <p>
* This is a <em>PRIVATE</em> subclass of NSURLHandle.
* It is documented here in order to give you information about the
* default behavior of an NSURLHandle created to deal with a URL
* that has the FILE scheme. The name and/or other
* implementation details of this class may be changed at any time.
* </p>
* <p>
* A GSFileURLHandle instance is used to manage files on the local
* file-system of your machine.
* </p>
*/
@implementation GSFileURLHandle
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)url
{
return nil;
}
+ (BOOL) canInitWithURL: (NSURL*)url
{
if ([url isFileURL] == YES)
{
return YES;
}
return NO;
}
- (NSData*) availableResourceData
{
if (_data != nil)
{
NSDictionary *dict;
dict = [[NSFileManager defaultManager] fileAttributesAtPath: _path
traverseLink: YES];
if (dict == nil)
{
// File no longer exists.
DESTROY(_data);
DESTROY(_attributes);
}
else
{
NSDate *original;
NSDate *latest;
original = [_attributes fileModificationDate];
latest = [dict fileModificationDate];
if ([latest earlierDate: original] != latest)
{
// File has been modified
DESTROY(_data);
DESTROY(_attributes);
_status = NSURLHandleNotLoaded;
}
}
}
return [super availableResourceData];
}
- (void) dealloc
{
RELEASE(_path);
RELEASE(_attributes);
[super dealloc];
}
- (id) initWithURL: (NSURL*)url
cached: (BOOL)cached
{
NSString *path;
if ([url isFileURL] == NO)
{
NSLog(@"Attempt to init GSFileURLHandle with bad URL");
DESTROY(self);
return nil;
}
path = [url path];
path = [path stringByStandardizingPath];
if ((self = [super initWithURL: url cached: cached]) != nil)
{
_path = [path copy];
}
return self;
}
- (NSData*) loadInForeground
{
NSData *d = [NSData dataWithContentsOfFile: _path];
NSDictionary *dict;
dict = [[NSFileManager defaultManager] fileAttributesAtPath: _path
traverseLink: YES];
RELEASE(_attributes);
_attributes = [dict mutableCopy];
[self didLoadBytes: d loadComplete: YES];
return d;
}
/**
* Gets file attribute information for the file represented by
* the handle, using the same dictionary keys as the
* <ref class="NSFileManager">NSFileManager</ref> class.
*/
- (id) propertyForKey: (NSString*)propertyKey
{
NSDictionary *dict;
dict = [[NSFileManager defaultManager] fileAttributesAtPath: _path
traverseLink: YES];
RELEASE(_attributes);
_attributes = [dict mutableCopy];
return [_attributes objectForKey: propertyKey];
}
- (id) propertyForKeyIfAvailable: (NSString*)propertyKey
{
return [_attributes objectForKey: propertyKey];
}
- (int) setDebug: (int)flag
{
return 0;
}
- (void) setReturnAll: (BOOL)flag
{
return;
}
- (void) setURL: (NSURL*)newUrl
{
return;
}
/**
* Writes the specified data as the contents of the file
* represented by the handle.
*/
- (BOOL) writeData: (NSData*)data
{
if ([data writeToFile: _path atomically: YES] == YES)
{
ASSIGNCOPY(_data, data);
return YES;
}
return NO;
}
/**
* Changes the attributes of the file represented by this handle.
* This method uses the same dictionary keys as the
* <ref class="NSFileManager">NSFileManger</ref> class.
*/
- (BOOL) writeProperty: (id)propertyValue
forKey: (NSString*)propertyKey
{
if ([self propertyForKey: propertyKey] == nil)
{
return NO; /* Not a valid file property key. */
}
[_attributes setObject: propertyValue forKey: propertyKey];
return [[NSFileManager defaultManager] changeFileAttributes: _attributes
atPath: _path];
}
@end
|