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 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
|
/** GSFTPURLHandle.m - Class GSFTPURLHandle
Copyright (C) 2002 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: 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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSException.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSData.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSURLHandle.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSRunLoop.h"
#import "Foundation/NSByteOrder.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSFileHandle.h"
#import "GNUstepBase/GSMime.h"
#import "GSPrivate.h"
GS_EXPORT NSString * const GSTelnetNotification;
GS_EXPORT NSString * const GSTelnetErrorKey;
GS_EXPORT NSString * const GSTelnetTextKey;
@interface GSTelnetHandle : NSObject
{
NSStringEncoding enc;
NSFileHandle *remote;
NSMutableData *ibuf;
unsigned pos;
BOOL lineMode;
BOOL connected;
}
- (id) initWithHandle: (NSFileHandle*)handle isConnected: (BOOL)flag;
- (void) putTelnetLine: (NSString*)s;
- (void) putTelnetText: (NSString*)s;
- (void) setEncoding: (NSStringEncoding)e;
- (void) setLineMode: (BOOL)flag;
@end
@interface GSTelnetHandle (Private)
- (void) _didConnect: (NSNotification*)notification;
- (void) _didRead: (NSNotification*)notification;
- (void) _didWrite: (NSNotification*)notification;
@end
NSString * const GSTelnetNotification = @"GSTelnetNotification";
NSString * const GSTelnetErrorKey = @"GSTelnetErrorKey";
NSString * const GSTelnetTextKey = @"GSTelnetTextKey";
@implementation GSTelnetHandle
#define WILL 251
#define WONT 252
#define DO 253
#define DONT 254
#define IAC 255
- (void) dealloc
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver: self];
RELEASE(remote);
RELEASE(ibuf);
[super dealloc];
}
- (id) init
{
return [self initWithHandle: nil isConnected: NO];
}
- (id) initWithHandle: (NSFileHandle*)handle isConnected: (BOOL)flag
{
if (handle == nil)
{
DESTROY(self);
}
else
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
connected = flag;
enc = NSUTF8StringEncoding;
ibuf = [NSMutableData new];
remote = RETAIN(handle);
if (connected == YES)
{
[nc addObserver: self
selector: @selector(_didRead:)
name: NSFileHandleReadCompletionNotification
object: remote];
[nc addObserver: self
selector: @selector(_didWrite:)
name: GSFileHandleWriteCompletionNotification
object: remote];
[remote readInBackgroundAndNotify];
}
else
{
[nc addObserver: self
selector: @selector(_didConnect:)
name: GSFileHandleConnectCompletionNotification
object: remote];
}
}
return self;
}
- (void) putTelnetLine: (NSString*)s
{
if ([s hasSuffix: @"\n"] == NO)
{
s = [s stringByAppendingString: @"\r\n"];
}
[self putTelnetText: s];
}
- (void) putTelnetText: (NSString*)s
{
NSMutableData *md;
unsigned char *to;
NSData *d = [s dataUsingEncoding: enc];
unsigned char *from = (unsigned char *)[d bytes];
unsigned int len = [d length];
unsigned int i = 0;
unsigned int count = 0;
for (i = 0; i < len; i++)
{
if (from[i] == IAC)
{
count++;
}
}
md = [[NSMutableData alloc] initWithLength: len + count];
to = [md mutableBytes];
for (i = 0; i < len; i++)
{
if (*from == IAC)
{
*to++ = IAC;
}
*to++ = *from++;
}
//NSLog(@"Write - '%*.*s'", [md length], [md length], [md bytes]);
[remote writeInBackgroundAndNotify: md];
DESTROY(md);
}
/**
* Set the string encoding used to convert strings to be sent to the
* remote system into raw data, and to convert incoming data from that
* system inot input text.
*/
- (void) setEncoding: (NSStringEncoding)e
{
enc = e;
}
/**
* Sets a flag to say whether observers are to be notified of incoming
* data after every chunk read, or only when one or more entire lines
* are read. <br />
* When switching out of line mode, this will cause a notification to be
* generated if there is any buffered data available for use.
*/
- (void) setLineMode: (BOOL)flag
{
if (lineMode != flag)
{
lineMode = flag;
if (lineMode == NO)
{
[self _didRead: nil];
}
}
}
@end
@implementation GSTelnetHandle (Private)
- (void) _didConnect: (NSNotification*)notification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *info = [notification userInfo];
NSString *e;
e = [info objectForKey: GSFileHandleNotificationError];
if (e == nil)
{
[nc removeObserver: self
name: GSFileHandleConnectCompletionNotification
object: [notification object]];
[nc addObserver: self
selector: @selector(_didRead:)
name: NSFileHandleReadCompletionNotification
object: remote];
[nc addObserver: self
selector: @selector(_didWrite:)
name: GSFileHandleWriteCompletionNotification
object: remote];
[remote readInBackgroundAndNotify];
}
else
{
info = [NSDictionary dictionaryWithObject: e
forKey: GSTelnetErrorKey];
[nc postNotificationName: GSTelnetNotification
object: self
userInfo: info];
}
}
- (void) _didRead: (NSNotification*)notification
{
NSDictionary *userInfo = [notification userInfo];
NSMutableArray *text = nil;
NSData *d;
d = [userInfo objectForKey: NSFileHandleNotificationDataItem];
/*
* If the notification is nil, this method has been called to flush
* any buffered data out.
*/
if (notification != nil && (d == nil || [d length] == 0))
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *info;
info = [NSDictionary dictionaryWithObject: @"EOF"
forKey: GSTelnetErrorKey];
[nc postNotificationName: GSTelnetNotification
object: self
userInfo: info];
}
else
{
NSMutableData *toWrite = nil;
unsigned char *ptr;
unsigned char c;
unsigned int s = 0;
int old;
int len;
int i; // May be negative.
if (d != nil)
{
// NSLog(@"Read - '%@'", d);
[ibuf appendData: d];
}
old = len = (int)[ibuf length];
ptr = [ibuf mutableBytes];
for (i = pos; i < len; i++)
{
NSData *line = nil;
c = ptr[i];
if (c == IAC)
{
if (i < len - 1)
{
c = ptr[i+1];
if (c == WILL || c == WONT || c == DO || c == DONT)
{
/*
* refuse any negotiation attempts.
*/
if (c == WILL || c == DO)
{
unsigned char opt[3];
if (toWrite == nil)
{
toWrite = [NSMutableData alloc];
toWrite = [toWrite initWithCapacity: 12];
}
opt[0] = IAC;
if (c == DO)
{
opt[1] = WONT;
}
else
{
opt[1] = DONT;
}
opt[2] = ptr[i+2];
[toWrite appendBytes: opt length: 3];
}
if (i < len - 2)
{
// NSLog(@"Command: %d %d", ptr[1], ptr[2]);
len -= 3;
if (len - i > 0)
{
memmove(ptr, &ptr[3], len - i);
}
i--; // Try again.
}
else
{
i--;
break; // Need more data
}
}
else if (c == IAC) // Escaped IAC
{
len--;
if (len - i > 0)
{
memmove(ptr, &ptr[1], len - i);
}
}
else
{
// NSLog(@"Command: %d", ptr[1]);
/*
* Strip unimplemented escape sequence.
*/
len -= 2;
if (len - i > 0)
{
memmove(ptr, &ptr[2], len - i);
}
i--; // Try again from here.
}
}
else
{
i--;
break; // Need more data
}
}
else if (c == '\r' && (int)i < len - 1 && ptr[i+1] == '\n')
{
line = [[NSData alloc] initWithBytes: &ptr[s] length: i-s+2];
i++;
s = i + 1;
}
else if (c == '\n')
{
line = [[NSData alloc] initWithBytes: &ptr[s] length: i-s+1];
s = i + 1;
}
if (line != nil)
{
NSString *lineString;
lineString = [[NSString alloc] initWithData: line encoding: enc];
DESTROY(line);
if (text == nil)
{
text = [[NSMutableArray alloc] initWithCapacity: 4];
}
[text addObject: lineString];
DESTROY(lineString);
}
}
pos = i;
/*
* If not in line mode, we can add the remainder of the data to
* the array of strings for notification.
*/
if (lineMode == NO && s != pos)
{
NSString *lineString;
NSData *line;
line = [[NSData alloc] initWithBytes: &ptr[s] length: pos - s];
s = pos;
lineString = [[NSString alloc] initWithData: line encoding: enc];
DESTROY(line);
if (text == nil)
{
text = [[NSMutableArray alloc] initWithCapacity: 4];
}
[text addObject: lineString];
DESTROY(lineString);
}
/*
* Adjust size of data remaining in buffer if necessary.
*/
if (old != len || s > 0)
{
if (s > 0)
{
len -= s;
pos -= s;
if (len > 0)
{
memmove(ptr, &ptr[s], len);
}
}
[ibuf setLength: len];
}
/*
* Send telnet protocol negotion info if necessary.
*/
if (toWrite != nil)
{
// NSLog(@"Write - '%@'", toWrite);
[remote writeInBackgroundAndNotify: toWrite];
DESTROY(toWrite);
}
/*
* Send notification for text read as necessary.
*/
if (text != nil)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSNotification *n;
NSDictionary *info;
info = [NSDictionary dictionaryWithObject: text
forKey: GSTelnetTextKey];
DESTROY(text);
n = [NSNotification notificationWithName: GSTelnetNotification
object: self
userInfo: info];
[nc postNotification: n];
}
[remote readInBackgroundAndNotify];
}
}
- (void) _didWrite: (NSNotification*)notification
{
NSDictionary *userInfo = [notification userInfo];
NSString *e;
e = [userInfo objectForKey: GSFileHandleNotificationError];
if (e)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSDictionary *info;
info = [NSDictionary dictionaryWithObject: e
forKey: GSTelnetErrorKey];
[nc postNotificationName: GSTelnetNotification
object: self
userInfo: info];
}
}
@end
@interface GSFTPURLHandle : NSURLHandle
{
GSTelnetHandle *cHandle;
NSFileHandle *dHandle;
NSURL *url;
NSData *wData;
NSString *term;
enum {
idle,
cConnect, // Establishing control connection
sentUser, // Sent username
sentPass, // Sent password
sentType, // Sent data type
sentPasv, // Requesting host/port information for data link
data, // Establishing or using data connection
list, // listing directory
} state;
}
- (void) _control: (NSNotification*)n;
- (void) _data: (NSNotification*)n;
@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 <code>ftp</code> scheme.
* The name and/or other implementation details of this class
* may be changed at any time.
* </p>
* <p>
* A GSFTPURLHandle instance is used to manage connections to
* <code>ftp</code> URLs.
* </p>
*/
@implementation GSFTPURLHandle
static NSMutableDictionary *urlCache = nil;
static NSLock *urlLock = nil;
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl
{
NSURLHandle *obj = nil;
if ([[newUrl scheme] caseInsensitiveCompare: @"ftp"] == NSOrderedSame)
{
NSString *page = [newUrl absoluteString];
// NSLog(@"Lookup for handle for '%@'", page);
[urlLock lock];
obj = [urlCache objectForKey: page];
IF_NO_GC([[obj retain] autorelease];)
[urlLock unlock];
// NSLog(@"Found handle %@", obj);
}
return obj;
}
+ (void) initialize
{
if (self == [GSFTPURLHandle class])
{
urlCache = [NSMutableDictionary new];
[[NSObject leakAt: &urlCache] release];
urlLock = [NSLock new];
[[NSObject leakAt: &urlLock] release];
}
}
- (void) dealloc
{
if (state != idle)
{
[self endLoadInBackground];
}
RELEASE(url);
RELEASE(wData);
RELEASE(term);
[super dealloc];
}
- (id) initWithURL: (NSURL*)newUrl
cached: (BOOL)cached
{
if ((self = [super initWithURL: newUrl cached: cached]) != nil)
{
ASSIGN(url, newUrl);
state = idle;
if (cached == YES)
{
NSString *page = [newUrl absoluteString];
[urlLock lock];
[urlCache setObject: self forKey: page];
[urlLock unlock];
// NSLog(@"Cache handle %@ for '%@'", self, page);
}
}
return self;
}
+ (BOOL) canInitWithURL: (NSURL*)newUrl
{
if ([[newUrl scheme] isEqualToString: @"ftp"] == YES)
{
return YES;
}
return NO;
}
- (void) _control: (NSNotification*)n
{
NSDictionary *info = [n userInfo];
NSString *e = [info objectForKey: GSTelnetErrorKey];
NSArray *text;
NSString *line;
if (e == nil)
{
NSEnumerator *enumerator;
text = [info objectForKey: GSTelnetTextKey];
// NSLog(@"Ctl: %@", text);
/* Find first reply line which is not a continuation of another.
*/
enumerator = [text objectEnumerator];
while ((line = [enumerator nextObject]) != nil)
{
if (term == nil)
{
if ([line length] > 4)
{
char buf[4];
buf[0] = (char)[line characterAtIndex: 0];
buf[1] = (char)[line characterAtIndex: 1];
buf[2] = (char)[line characterAtIndex: 2];
buf[3] = (char)[line characterAtIndex: 3];
if (isdigit(buf[0]) && isdigit(buf[1]) && isdigit(buf[2]))
{
if (buf[3] == '-')
{
/* Got start of a multiline block ...
* set the terminator we need to look for.
*/
buf[3] = ' ';
term = [[NSString alloc]
initWithCString: buf length: 4];
}
else if (buf[3] == ' ')
{
/* Found single line response.
*/
break;
}
}
}
}
else if ([line hasPrefix: term] == YES)
{
/* Found end of a multiline response.
*/
DESTROY(term);
break;
}
}
if (line == nil)
{
return;
}
if (state == cConnect)
{
if ([line hasPrefix: @"2"] == YES)
{
NSString *user = [url user];
if (user == nil)
{
user = @"anonymous";
}
[cHandle putTelnetLine: [@"USER " stringByAppendingString: user]];
state = sentUser;
}
else
{
e = line;
}
}
else if (state == sentUser)
{
if ([line hasPrefix: @"230"] == YES) // No password required
{
[cHandle putTelnetLine: @"TYPE I"];
state = sentType;
}
else if ([line hasPrefix: @"331"] == YES)
{
NSString *pass = [url password];
if (pass == nil)
{
pass = [url user];
if (pass == nil)
{
pass = @"GNUstep@here";
}
else
{
pass = @"";
}
}
[cHandle putTelnetLine: [@"PASS " stringByAppendingString: pass]];
state = sentPass;
}
else
{
e = line;
}
}
else if (state == sentPass)
{
if ([line hasPrefix: @"2"] == YES)
{
[cHandle putTelnetLine: @"TYPE I"];
state = sentType;
}
else
{
e = line;
}
}
else if (state == sentType)
{
if ([line hasPrefix: @"2"] == YES)
{
[cHandle putTelnetLine: @"PASV"];
state = sentPasv;
}
else
{
e = line;
}
}
else if (state == sentPasv)
{
if ([line hasPrefix: @"227"] == YES)
{
NSRange r = [line rangeOfString: @"("];
NSString *h = nil;
NSString *p = nil;
if (r.length > 0)
{
unsigned pos = NSMaxRange(r);
r = [line rangeOfString: @")"];
if (r.length > 0 && r.location > pos)
{
NSArray *a;
r = NSMakeRange(pos, r.location - pos);
line = [line substringWithRange: r];
a = [line componentsSeparatedByString: @","];
if ([a count] == 6)
{
h = [NSString stringWithFormat: @"%@.%@.%@.%@",
[a objectAtIndex: 0], [a objectAtIndex: 1],
[a objectAtIndex: 2], [a objectAtIndex: 3]];
p = [NSString stringWithFormat: @"%d",
[[a objectAtIndex: 4] intValue] * 256
+ [[a objectAtIndex: 5] intValue]];
}
}
}
if (h == nil)
{
e = @"Invalid host/port information for pasv command";
}
else
{
NSNotificationCenter *nc;
dHandle = [NSFileHandle
fileHandleAsClientInBackgroundAtAddress: h service: p
protocol: @"tcp"];
IF_NO_GC([dHandle retain];)
nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self
selector: @selector(_data:)
name: GSFileHandleConnectCompletionNotification
object: dHandle];
state = data;
}
}
else
{
e = line;
}
}
else if (state == data)
{
if ([line hasPrefix: @"550"] == YES && wData == nil)
{
state = list;
[cHandle putTelnetLine:
[NSString stringWithFormat: @"NLST %@", [url path]]];
}
else if ([line hasPrefix: @"1"] == NO && [line hasPrefix: @"2"] == NO)
{
e = line;
}
}
else if (state == list)
{
if ([line hasPrefix: @"550"] == YES)
{
NSRange r = [line rangeOfString: @"not a plain file"];
/*
* Some servers may return an error on listing even though
* the path was a valid directory. We try to catch some of
* those cases and produce an empty listing instead.
*/
if (r.location > 0)
{
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
if (dHandle != nil)
{
[nc removeObserver: self name: nil object: dHandle];
[dHandle closeFile];
DESTROY(dHandle);
}
[nc removeObserver: self
name: GSTelnetNotification
object: cHandle];
DESTROY(cHandle);
state = idle;
[self didLoadBytes: [NSData data] loadComplete: YES];
}
else
{
e = line;
}
}
else if ([line hasPrefix: @"1"] == NO && [line hasPrefix: @"2"] == NO)
{
e = line;
}
}
else
{
e = @"Message in unknown state";
}
}
if (e != nil)
{
/*
* Tell superclass that the load failed - let it do housekeeping.
*/
[self endLoadInBackground];
[self backgroundLoadDidFailWithReason: e];
}
}
- (void) _data: (NSNotification*)n
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSString *name = [n name];
NSDictionary *info = [n userInfo];
NSString *e = [info objectForKey: GSFileHandleNotificationError];
// NSLog(@"_data: %@", n);
[nc removeObserver: self name: name object: dHandle];
/*
* See if the connection attempt caused an error.
*/
if (e != nil)
{
if ([name isEqualToString: GSFileHandleConnectCompletionNotification])
{
NSLog(@"Unable to connect to %@:%@ via socket ... %@",
[dHandle socketAddress], [dHandle socketService], e);
}
// NSLog(@"Fail - %@", e);
/*
* Tell superclass that the load failed - let it do housekeeping.
*/
[self endLoadInBackground];
[self backgroundLoadDidFailWithReason: e];
return;
}
if ([name isEqualToString: GSFileHandleConnectCompletionNotification])
{
if (wData == nil)
{
[cHandle putTelnetLine:
[NSString stringWithFormat: @"RETR %@", [url path]]];
[nc addObserver: self
selector: @selector(_data:)
name: NSFileHandleReadCompletionNotification
object: dHandle];
[dHandle readInBackgroundAndNotify];
}
else
{
[cHandle putTelnetLine:
[NSString stringWithFormat: @"STOR %@", [url path]]];
[nc addObserver: self
selector: @selector(_data:)
name: GSFileHandleWriteCompletionNotification
object: dHandle];
[dHandle writeInBackgroundAndNotify: wData];
}
}
else
{
if (wData == nil)
{
NSData *d;
d = [info objectForKey: NSFileHandleNotificationDataItem];
if ([d length] > 0)
{
[self didLoadBytes: d loadComplete: NO];
[nc addObserver: self
selector: @selector(_data:)
name: NSFileHandleReadCompletionNotification
object: dHandle];
[dHandle readInBackgroundAndNotify];
}
else
{
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
if (dHandle != nil)
{
[nc removeObserver: self name: nil object: dHandle];
[dHandle closeFile];
DESTROY(dHandle);
}
[nc removeObserver: self
name: GSTelnetNotification
object: cHandle];
DESTROY(cHandle);
state = idle;
[self didLoadBytes: d loadComplete: YES];
}
}
else
{
NSNotificationCenter *nc;
NSData *tmp;
nc = [NSNotificationCenter defaultCenter];
if (dHandle != nil)
{
[nc removeObserver: self name: nil object: dHandle];
[dHandle closeFile];
DESTROY(dHandle);
}
[nc removeObserver: self
name: GSTelnetNotification
object: cHandle];
DESTROY(cHandle);
state = idle;
/*
* Tell superclass that we have successfully loaded the data.
*/
tmp = wData;
wData = nil;
[self didLoadBytes: tmp loadComplete: YES];
DESTROY(tmp);
}
}
}
- (void) endLoadInBackground
{
if (state != idle)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (dHandle != nil)
{
[nc removeObserver: self name: nil object: dHandle];
[dHandle closeFile];
DESTROY(dHandle);
}
[nc removeObserver: self name: GSTelnetNotification object: cHandle];
DESTROY(cHandle);
state = idle;
}
[super endLoadInBackground];
}
- (void) loadInBackground
{
NSNotificationCenter *nc;
NSString *host = nil;
NSString *port = nil;
NSNumber *p;
NSFileHandle *sock;
/*
* Don't start a load if one is in progress.
*/
if (state != idle)
{
NSLog(@"Attempt to load an ftp handle which is not idle ... ignored");
return;
}
[self beginLoadInBackground];
host = [url host];
p = [url port];
if (p != nil)
{
port = [NSString stringWithFormat: @"%u", [p unsignedIntValue]];
}
else
{
port = [url scheme];
}
sock = [NSFileHandle fileHandleAsClientInBackgroundAtAddress: host
service: port
protocol: @"tcp"];
if (sock == nil)
{
/*
* Tell superclass that the load failed - let it do housekeeping.
*/
[self backgroundLoadDidFailWithReason: [NSString stringWithFormat:
@"Unable to connect to %@:%@ ... %@",
host, port, [NSError _last]]];
return;
}
cHandle = [[GSTelnetHandle alloc] initWithHandle: sock isConnected: NO];
nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self
selector: @selector(_control:)
name: GSTelnetNotification
object: cHandle];
state = cConnect;
}
/**
* We cannot get/set any properties for FTP
*/
- (id) propertyForKey: (NSString*)propertyKey
{
return nil;
}
/**
* We cannot get/set any properties for FTP
*/
- (id) propertyForKeyIfAvailable: (NSString*)propertyKey
{
return nil;
}
/**
* Sets the specified data to be written to the URL on the next load.
*/
- (BOOL) writeData: (NSData*)data
{
ASSIGN(wData, data);
return YES;
}
/**
* We cannot get/set any properties for FTP
*/
- (BOOL) writeProperty: (id)propertyValue
forKey: (NSString*)propertyKey
{
return NO;
}
@end
|