2002-06-16 11:52:53 +00:00
|
|
|
|
/** GSFTPURLHandle.m - Class GSFTPURLHandle
|
|
|
|
|
Copyright (C) 2002 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
Date: June 2002
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
This file is part of the GNUstep Library.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2002-06-16 11:52:53 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
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
|
|
|
|
|
Library General Public License for more details.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2002-06-16 11:52:53 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-10-09 14:00:01 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02111 USA.
|
2002-06-16 11:52:53 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
|
#import "common.h"
|
2007-11-29 20:53:26 +00:00
|
|
|
|
#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"
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
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;
|
2003-01-03 20:14:47 +00:00
|
|
|
|
int old;
|
|
|
|
|
int len;
|
2002-06-16 11:52:53 +00:00
|
|
|
|
int i; // May be negative.
|
|
|
|
|
|
|
|
|
|
if (d != nil)
|
|
|
|
|
{
|
2002-06-16 14:54:26 +00:00
|
|
|
|
// NSLog(@"Read - '%@'", d);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[ibuf appendData: d];
|
|
|
|
|
}
|
2003-01-03 20:14:47 +00:00
|
|
|
|
old = len = (int)[ibuf length];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Command: %d %d", ptr[1], ptr[2]);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
len -= 3;
|
|
|
|
|
if (len - i > 0)
|
|
|
|
|
{
|
2018-02-03 10:11:30 +00:00
|
|
|
|
memmove(ptr, &ptr[3], len - i);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
i--; // Try again.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
i--;
|
|
|
|
|
break; // Need more data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (c == IAC) // Escaped IAC
|
|
|
|
|
{
|
|
|
|
|
len--;
|
|
|
|
|
if (len - i > 0)
|
|
|
|
|
{
|
2018-02-03 10:11:30 +00:00
|
|
|
|
memmove(ptr, &ptr[1], len - i);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Command: %d", ptr[1]);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
/*
|
|
|
|
|
* Strip unimplemented escape sequence.
|
|
|
|
|
*/
|
|
|
|
|
len -= 2;
|
|
|
|
|
if (len - i > 0)
|
|
|
|
|
{
|
2018-02-03 10:11:30 +00:00
|
|
|
|
memmove(ptr, &ptr[2], len - i);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
i--; // Try again from here.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
i--;
|
|
|
|
|
break; // Need more data
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-01-03 20:14:47 +00:00
|
|
|
|
else if (c == '\r' && (int)i < len - 1 && ptr[i+1] == '\n')
|
2002-06-16 11:52:53 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-02-03 10:11:30 +00:00
|
|
|
|
memmove(ptr, &ptr[s], len);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ibuf setLength: len];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Send telnet protocol negotion info if necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (toWrite != nil)
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Write - '%@'", toWrite);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[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;
|
2009-04-26 13:45:47 +00:00
|
|
|
|
NSString *term;
|
2002-06-16 11:52:53 +00:00
|
|
|
|
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
|
2002-06-16 14:54:26 +00:00
|
|
|
|
list, // listing directory
|
2002-06-16 11:52:53 +00:00
|
|
|
|
} 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];
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Lookup for handle for '%@'", page);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[urlLock lock];
|
|
|
|
|
obj = [urlCache objectForKey: page];
|
2009-01-12 12:48:46 +00:00
|
|
|
|
IF_NO_GC([[obj retain] autorelease];)
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[urlLock unlock];
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Found handle %@", obj);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [GSFTPURLHandle class])
|
|
|
|
|
{
|
|
|
|
|
urlCache = [NSMutableDictionary new];
|
2013-08-22 15:44:54 +00:00
|
|
|
|
[[NSObject leakAt: &urlCache] release];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
urlLock = [NSLock new];
|
2013-08-22 15:44:54 +00:00
|
|
|
|
[[NSObject leakAt: &urlLock] release];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (state != idle)
|
|
|
|
|
{
|
|
|
|
|
[self endLoadInBackground];
|
|
|
|
|
}
|
|
|
|
|
RELEASE(url);
|
|
|
|
|
RELEASE(wData);
|
2009-04-26 13:45:47 +00:00
|
|
|
|
RELEASE(term);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[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];
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Cache handle %@ for '%@'", self, page);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
2002-06-16 13:31:26 +00:00
|
|
|
|
NSString *line;
|
2002-06-16 11:52:53 +00:00
|
|
|
|
|
|
|
|
|
if (e == nil)
|
|
|
|
|
{
|
2002-06-17 16:07:06 +00:00
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
|
|
2002-06-16 13:31:26 +00:00
|
|
|
|
text = [info objectForKey: GSTelnetTextKey];
|
|
|
|
|
// NSLog(@"Ctl: %@", text);
|
2009-04-26 13:45:47 +00:00
|
|
|
|
/* Find first reply line which is not a continuation of another.
|
2002-06-17 16:07:06 +00:00
|
|
|
|
*/
|
|
|
|
|
enumerator = [text objectEnumerator];
|
|
|
|
|
while ((line = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
2009-04-26 13:45:47 +00:00
|
|
|
|
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)
|
2002-06-17 16:07:06 +00:00
|
|
|
|
{
|
2009-04-26 13:45:47 +00:00
|
|
|
|
/* Found end of a multiline response.
|
|
|
|
|
*/
|
|
|
|
|
DESTROY(term);
|
2002-06-17 16:07:06 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (line == nil)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-06-16 13:31:26 +00:00
|
|
|
|
if (state == cConnect)
|
2002-06-16 11:52:53 +00:00
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
if ([line hasPrefix: @"2"] == YES)
|
|
|
|
|
{
|
|
|
|
|
NSString *user = [url user];
|
|
|
|
|
|
|
|
|
|
if (user == nil)
|
|
|
|
|
{
|
|
|
|
|
user = @"anonymous";
|
|
|
|
|
}
|
|
|
|
|
[cHandle putTelnetLine: [@"USER " stringByAppendingString: user]];
|
|
|
|
|
state = sentUser;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e = line;
|
|
|
|
|
}
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
2002-06-16 13:31:26 +00:00
|
|
|
|
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];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
|
2002-06-16 13:31:26 +00:00
|
|
|
|
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)
|
2002-06-16 11:52:53 +00:00
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
if ([line hasPrefix: @"2"] == YES)
|
2002-06-16 11:52:53 +00:00
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
[cHandle putTelnetLine: @"TYPE I"];
|
|
|
|
|
state = sentType;
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
e = line;
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-16 13:31:26 +00:00
|
|
|
|
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"];
|
2009-01-12 12:48:46 +00:00
|
|
|
|
IF_NO_GC([dHandle retain];)
|
2002-06-16 13:31:26 +00:00
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[nc addObserver: self
|
|
|
|
|
selector: @selector(_data:)
|
|
|
|
|
name: GSFileHandleConnectCompletionNotification
|
|
|
|
|
object: dHandle];
|
|
|
|
|
state = data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e = line;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (state == data)
|
|
|
|
|
{
|
2002-06-16 14:54:26 +00:00
|
|
|
|
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;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
}
|
2002-06-16 14:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
else if (state == list)
|
|
|
|
|
{
|
2002-06-17 17:02:49 +00:00
|
|
|
|
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)
|
2002-06-16 14:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
e = line;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
}
|
2002-06-16 13:31:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e = @"Message in unknown state";
|
|
|
|
|
}
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
2002-06-16 13:31:26 +00:00
|
|
|
|
if (e != nil)
|
2002-06-16 11:52:53 +00:00
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
/*
|
|
|
|
|
* Tell superclass that the load failed - let it do housekeeping.
|
|
|
|
|
*/
|
|
|
|
|
[self endLoadInBackground];
|
|
|
|
|
[self backgroundLoadDidFailWithReason: e];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) _data: (NSNotification*)n
|
|
|
|
|
{
|
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
NSString *name = [n name];
|
|
|
|
|
NSDictionary *info = [n userInfo];
|
|
|
|
|
NSString *e = [info objectForKey: GSFileHandleNotificationError];
|
|
|
|
|
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"_data: %@", n);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[nc removeObserver: self name: name object: dHandle];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* See if the connection attempt caused an error.
|
|
|
|
|
*/
|
|
|
|
|
if (e != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([name isEqualToString: GSFileHandleConnectCompletionNotification])
|
|
|
|
|
{
|
2004-09-08 09:13:50 +00:00
|
|
|
|
NSLog(@"Unable to connect to %@:%@ via socket ... %@",
|
|
|
|
|
[dHandle socketAddress], [dHandle socketService], e);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
2002-06-16 13:31:26 +00:00
|
|
|
|
// NSLog(@"Fail - %@", e);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
/*
|
|
|
|
|
* Tell superclass that the load failed - let it do housekeeping.
|
|
|
|
|
*/
|
|
|
|
|
[self endLoadInBackground];
|
|
|
|
|
[self backgroundLoadDidFailWithReason: e];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ([name isEqualToString: GSFileHandleConnectCompletionNotification])
|
|
|
|
|
{
|
|
|
|
|
if (wData == nil)
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
[cHandle putTelnetLine:
|
|
|
|
|
[NSString stringWithFormat: @"RETR %@", [url path]]];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[nc addObserver: self
|
|
|
|
|
selector: @selector(_data:)
|
|
|
|
|
name: NSFileHandleReadCompletionNotification
|
|
|
|
|
object: dHandle];
|
|
|
|
|
[dHandle readInBackgroundAndNotify];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
[cHandle putTelnetLine:
|
|
|
|
|
[NSString stringWithFormat: @"STOR %@", [url path]]];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
[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
|
|
|
|
|
{
|
2002-06-16 13:31:26 +00:00
|
|
|
|
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];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-06-16 14:54:26 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
/*
|
|
|
|
|
* Tell superclass that we have successfully loaded the data.
|
|
|
|
|
*/
|
2002-06-16 14:54:26 +00:00
|
|
|
|
tmp = wData;
|
|
|
|
|
wData = nil;
|
|
|
|
|
[self didLoadBytes: tmp loadComplete: YES];
|
|
|
|
|
DESTROY(tmp);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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:
|
2006-10-09 14:00:01 +00:00
|
|
|
|
@"Unable to connect to %@:%@ ... %@",
|
2006-10-20 10:56:27 +00:00
|
|
|
|
host, port, [NSError _last]]];
|
2002-06-16 11:52:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cHandle = [[GSTelnetHandle alloc] initWithHandle: sock isConnected: NO];
|
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[nc addObserver: self
|
|
|
|
|
selector: @selector(_control:)
|
|
|
|
|
name: GSTelnetNotification
|
|
|
|
|
object: cHandle];
|
|
|
|
|
state = cConnect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2002-06-16 14:54:26 +00:00
|
|
|
|
* 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.
|
2002-06-16 11:52:53 +00:00
|
|
|
|
*/
|
2002-06-16 14:54:26 +00:00
|
|
|
|
- (BOOL) writeData: (NSData*)data
|
2002-06-16 11:52:53 +00:00
|
|
|
|
{
|
2002-06-16 14:54:26 +00:00
|
|
|
|
ASSIGN(wData, data);
|
2002-06-16 11:52:53 +00:00
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-16 14:54:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* We cannot get/set any properties for FTP
|
|
|
|
|
*/
|
|
|
|
|
- (BOOL) writeProperty: (id)propertyValue
|
|
|
|
|
forKey: (NSString*)propertyKey
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-16 11:52:53 +00:00
|
|
|
|
@end
|
|
|
|
|
|