mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 00:41:02 +00:00
Add some requests 10.6 methods ... untested.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33326 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
813a158585
commit
3ea06f1379
5 changed files with 511 additions and 268 deletions
13
ChangeLog
13
ChangeLog
|
@ -1,3 +1,16 @@
|
|||
2011-06-17 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Headers/GNUstepBase/NSURL+GNUstepBase.h: Add method to build a URL
|
||||
from separate components.
|
||||
* Headers/Foundation/NSURL.h: Add some OSX 10.6 methods for path
|
||||
handling, and incorporate documentation from source file.
|
||||
* Source/NSURL.m: Add some OSX 10.6 methods for path handling, fix
|
||||
minor bug checking resource availability, and move documentation to
|
||||
header.
|
||||
* Source/Additions/NSURL+GNUstepBase.m: Add method to build a URL from
|
||||
components.
|
||||
We NEED testcases for these.
|
||||
|
||||
2011-06-13 Fred Kiefer <FredKiefer@gmx.de>
|
||||
|
||||
* Source/GSAvahiRunLoopIntegration.m: Declare the local methods in
|
||||
|
|
|
@ -42,6 +42,15 @@ extern "C" {
|
|||
*/
|
||||
GS_EXPORT NSString* const NSURLFileScheme;
|
||||
|
||||
/**
|
||||
* This class permits manipulation of URLs and the resources to which they
|
||||
* refer. They can be used to represent absolute URLs or relative URLs
|
||||
* which are based upon an absolute URL. The relevant RFCs describing
|
||||
* how a URL is formatted, and what is legal in a URL are -
|
||||
* 1808, 1738, and 2396.<br />
|
||||
* Handling of the underlying resources is carried out by NSURLHandle
|
||||
* objects, but NSURL provides a simplified API wrapping these objects.
|
||||
*/
|
||||
@interface NSURL: NSObject <NSCoding, NSCopying, NSURLHandleClient>
|
||||
{
|
||||
#if GS_EXPOSE(NSURL)
|
||||
|
@ -52,47 +61,303 @@ GS_EXPORT NSString* const NSURLFileScheme;
|
|||
void *_data;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create and return a file URL with the supplied path.<br />
|
||||
* The value of aPath must be a valid filesystem path.<br />
|
||||
* Calls -initFileURLWithPath: which escapes characters in the
|
||||
* path where necessary.
|
||||
*/
|
||||
+ (id) fileURLWithPath: (NSString*)aPath;
|
||||
|
||||
/**
|
||||
* Create and return a URL with the supplied string, which should
|
||||
* be a string (containing percent escape codes where necessary)
|
||||
* conforming to the description (in RFC2396) of an absolute URL.<br />
|
||||
* Calls -initWithString:
|
||||
*/
|
||||
+ (id) URLWithString: (NSString*)aUrlString;
|
||||
|
||||
/**
|
||||
* Create and return a URL with the supplied string, which should
|
||||
* be a string (containing percent escape codes where necessary)
|
||||
* conforming to the description (in RFC2396) of a relative URL.<br />
|
||||
* Calls -initWithString:relativeToURL:
|
||||
*/
|
||||
+ (id) URLWithString: (NSString*)aUrlString
|
||||
relativeToURL: (NSURL*)aBaseUrl;
|
||||
|
||||
/**
|
||||
* Initialise as a file URL with the specified path (which must
|
||||
* be a valid path on the local filesystem).<br />
|
||||
* Converts relative paths to absolute ones.<br />
|
||||
* Appends a trailing slash to the path when necessary if it
|
||||
* specifies a directory.<br />
|
||||
* Calls -initWithScheme:host:path:
|
||||
*/
|
||||
- (id) initFileURLWithPath: (NSString*)aPath;
|
||||
|
||||
/**
|
||||
* Initialise by building a URL string from the supplied parameters
|
||||
* and calling -initWithString:relativeToURL:<br />
|
||||
* This method adds percent escapes to aPath if it contains characters
|
||||
* which need escaping.<br />
|
||||
* Accepts RFC2732 style IPv6 host addresses either with or without the
|
||||
* enclosing square brackets (MacOS-X at least up to version 10.5 does
|
||||
* not handle these correctly, but GNUstep does).<br />
|
||||
* Permits the 'aHost' part to contain 'username:password@host:port' or
|
||||
* 'host:port' in addition to a simple host name or address.
|
||||
*/
|
||||
- (id) initWithScheme: (NSString*)aScheme
|
||||
host: (NSString*)aHost
|
||||
path: (NSString*)aPath;
|
||||
|
||||
/**
|
||||
* Initialise as an absolute URL.<br />
|
||||
* Calls -initWithString:relativeToURL:
|
||||
*/
|
||||
- (id) initWithString: (NSString*)aUrlString;
|
||||
|
||||
/** <init />
|
||||
* Initialised using aUrlString and aBaseUrl. The value of aBaseUrl
|
||||
* may be nil, but aUrlString must be non-nil.<br />
|
||||
* Accepts RFC2732 style IPv6 host addresses.<br />
|
||||
* Parses a string wihthout a scheme as a simple path.<br />
|
||||
* Parses an empty string as an empty path.<br />
|
||||
* If the string cannot be parsed the method returns nil.
|
||||
*/
|
||||
- (id) initWithString: (NSString*)aUrlString
|
||||
relativeToURL: (NSURL*)aBaseUrl;
|
||||
|
||||
/**
|
||||
* Returns the full string describing the receiver resolved against its base.
|
||||
*/
|
||||
- (NSString*) absoluteString;
|
||||
|
||||
/**
|
||||
* If the receiver is an absolute URL, returns self. Otherwise returns an
|
||||
* absolute URL referring to the same resource as the receiver.
|
||||
*/
|
||||
- (NSURL*) absoluteURL;
|
||||
|
||||
/**
|
||||
* If the receiver is a relative URL, returns its base URL.<br />
|
||||
* Otherwise, returns nil.
|
||||
*/
|
||||
- (NSURL*) baseURL;
|
||||
|
||||
#if OS_API_VERSION(100600,GS_API_LATEST)
|
||||
/** Attempts to load from the specified URL and provides an error
|
||||
* response if the data is unrachable.<br />
|
||||
* Returns YES on success, NO on failure.
|
||||
*/
|
||||
- (BOOL) checkResourceIsReachableAndReturnError: (NSError **)error;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Returns the fragment portion of the receiver or nil if there is no
|
||||
* fragment supplied in the URL.<br />
|
||||
* The fragment is everything in the original URL string after a '#'<br />
|
||||
* File URLs do not have fragments.
|
||||
*/
|
||||
- (NSString*) fragment;
|
||||
|
||||
/**
|
||||
* Returns the host portion of the receiver or nil if there is no
|
||||
* host supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated and the string
|
||||
* treated as UTF8.<br />
|
||||
* Returns IPv6 addresses <em>without</em> the enclosing square brackets
|
||||
* required (by RFC2732) in URL strings.
|
||||
*/
|
||||
- (NSString*) host;
|
||||
|
||||
/**
|
||||
* Returns YES if the receiver is a file URL, NO otherwise.
|
||||
*/
|
||||
- (BOOL) isFileURL;
|
||||
|
||||
#if OS_API_VERSION(100600,GS_API_LATEST)
|
||||
/** Returns the last (rightmost) path component of the receiver.
|
||||
*/
|
||||
- (NSString*) lastPathComponent;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Loads resource data for the specified client.
|
||||
* <p>
|
||||
* If shouldUseCache is YES then an attempt
|
||||
* will be made to locate a cached NSURLHandle to provide the
|
||||
* resource data, otherwise a new handle will be created and
|
||||
* cached.
|
||||
* </p>
|
||||
* <p>
|
||||
* If the handle does not have the data available, it will be
|
||||
* asked to load the data in the background by calling its
|
||||
* loadInBackground method.
|
||||
* </p>
|
||||
* <p>
|
||||
* The specified client (if non-nil) will be set up to receive
|
||||
* notifications of the progress of the background load process.
|
||||
* </p>
|
||||
* <p>
|
||||
* The processes current run loop must be run in order for the
|
||||
* background load operation to operate!
|
||||
* </p>
|
||||
*/
|
||||
- (void) loadResourceDataNotifyingClient: (id)client
|
||||
usingCache: (BOOL)shouldUseCache;
|
||||
|
||||
/**
|
||||
* Returns the parameter portion of the receiver or nil if there is no
|
||||
* parameter supplied in the URL.<br />
|
||||
* The parameters are everything in the original URL string after a ';'
|
||||
* but before the query.<br />
|
||||
* File URLs do not have parameters.
|
||||
*/
|
||||
- (NSString*) parameterString;
|
||||
|
||||
/**
|
||||
* Returns the password portion of the receiver or nil if there is no
|
||||
* password supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated and the string
|
||||
* treated as UTF8 in GNUstep but this appears to be broken in MacOS-X.<br />
|
||||
* NB. because of its security implications it is recommended that you
|
||||
* do not use URLs with users and passwords unless necessary.
|
||||
*/
|
||||
- (NSString*) password;
|
||||
|
||||
/**
|
||||
* Returns the path portion of the receiver.<br />
|
||||
* Replaces percent escapes with unescaped values, interpreting non-ascii
|
||||
* character sequences as UTF8.<br />
|
||||
* NB. This does not conform strictly to the RFCs, in that it includes a
|
||||
* leading slash ('/') character (whereas the path part of a URL strictly
|
||||
* should not) and the interpretation of non-ascii character is (strictly
|
||||
* speaking) undefined.<br />
|
||||
* Also, this breaks strict conformance in that a URL of file scheme is
|
||||
* treated as having a path (contrary to RFCs)
|
||||
*/
|
||||
- (NSString*) path;
|
||||
|
||||
#if OS_API_VERSION(100600,GS_API_LATEST)
|
||||
/** Returns the file extension (text after the rightmost dot in the path)
|
||||
* of the receiver.
|
||||
*/
|
||||
- (NSString*) pathExtension;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Returns the port portion of the receiver or nil if there is no
|
||||
* port supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated in GNUstep
|
||||
* but this appears to be broken in MacOS-X.
|
||||
*/
|
||||
- (NSNumber*) port;
|
||||
|
||||
/**
|
||||
* Asks a URL handle to return the property for the specified key and
|
||||
* returns the result.
|
||||
*/
|
||||
- (id) propertyForKey: (NSString*)propertyKey;
|
||||
|
||||
/**
|
||||
* Returns the query portion of the receiver or nil if there is no
|
||||
* query supplied in the URL.<br />
|
||||
* The query is everything in the original URL string after a '?'
|
||||
* but before the fragment.<br />
|
||||
* File URLs do not have queries.
|
||||
*/
|
||||
- (NSString*) query;
|
||||
|
||||
/**
|
||||
* Returns the path of the receiver, without taking any base URL into account.
|
||||
* If the receiver is an absolute URL, -relativePath is the same as -path.<br />
|
||||
* Returns nil if there is no path specified for the URL.
|
||||
*/
|
||||
- (NSString*) relativePath;
|
||||
|
||||
/**
|
||||
* Returns the relative portion of the URL string. If the receiver is not
|
||||
* a relative URL, this returns the same as absoluteString.
|
||||
*/
|
||||
- (NSString*) relativeString;
|
||||
|
||||
/**
|
||||
* Loads the resource data for the represented URL and returns the result.
|
||||
* The shouldUseCache flag determines whether data previously retrieved by
|
||||
* an existing NSURLHandle can be used to provide the data, or if it should
|
||||
* be refetched.
|
||||
*/
|
||||
- (NSData*) resourceDataUsingCache: (BOOL)shouldUseCache;
|
||||
|
||||
/**
|
||||
* Returns the resource specifier of the URL ... the part which lies
|
||||
* after the scheme.
|
||||
*/
|
||||
- (NSString*) resourceSpecifier;
|
||||
|
||||
/**
|
||||
* Returns the scheme of the receiver.
|
||||
*/
|
||||
- (NSString*) scheme;
|
||||
|
||||
/**
|
||||
* Calls [NSURLHandle-writeProperty:forKey:] to set the named property.
|
||||
*/
|
||||
- (BOOL) setProperty: (id)property
|
||||
forKey: (NSString*)propertyKey;
|
||||
|
||||
/**
|
||||
* Calls [NSURLHandle-writeData:] to write the specified data object
|
||||
* to the resource identified by the receiver URL.<br />
|
||||
* Returns the result.
|
||||
*/
|
||||
- (BOOL) setResourceData: (NSData*)data;
|
||||
|
||||
/**
|
||||
* Returns a URL with '/./' and '/../' sequences resolved etc.
|
||||
*/
|
||||
- (NSURL*) standardizedURL;
|
||||
|
||||
#if OS_API_VERSION(100600,GS_API_LATEST)
|
||||
/** Returns a URL formed by adding a path component to the path of the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSURL*) URLByAppendingPathComponent: (NSString*)pathComponent;
|
||||
|
||||
/** Returns a URL formed by adding a path extension to the path of the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSURL*) URLByAppendingPathExtension: (NSString*)pathExtension;
|
||||
|
||||
/** Returns a URL formed by removing a path component from the path of the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSURL*) URLByDeletingLastPathComponent;
|
||||
|
||||
/** Returns a URL formed by removing an extension from the path of the
|
||||
* receiver.
|
||||
*/
|
||||
- (NSURL*) URLByDeletingPathExtension;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Returns an NSURLHandle instance which may be used to write data to the
|
||||
* resource represented by the receiver URL, or read data from it.<br />
|
||||
* The shouldUseCache flag indicates whether a cached handle may be returned
|
||||
* or a new one should be created.
|
||||
*/
|
||||
- (NSURLHandle*)URLHandleUsingCache: (BOOL)shouldUseCache;
|
||||
|
||||
/**
|
||||
* Returns the user portion of the receiver or nil if there is no
|
||||
* user supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated and
|
||||
* the whole is treated as UTF8 data.<br />
|
||||
* NB. because of its security implications it is recommended that you
|
||||
* do not use URLs with users and passwords unless necessary.
|
||||
*/
|
||||
- (NSString*) user;
|
||||
|
||||
@end
|
||||
|
|
|
@ -36,6 +36,22 @@ extern "C" {
|
|||
|
||||
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
|
||||
|
||||
@interface NSURL (GNUstepBaseAdditions)
|
||||
|
||||
/** Builds a URL from components as returned by the methods of the same names.
|
||||
*/
|
||||
- (id) initWithScheme: (NSString*)scheme
|
||||
user: (NSString*)user
|
||||
password: (NSString*)password
|
||||
host: (NSString*)host
|
||||
port: (NSNumber*)port
|
||||
fullPath: (NSString*)fullPath
|
||||
parameterString: (NSString*)parameterString
|
||||
query: (NSString*)query
|
||||
fragment: (NSString*)fragment;
|
||||
|
||||
@end
|
||||
|
||||
@interface NSURL (GNUstepBase)
|
||||
/** Returns the full path for this URL including any trailing slash.
|
||||
*/
|
||||
|
|
|
@ -23,12 +23,84 @@
|
|||
|
||||
*/
|
||||
|
||||
#ifndef GNUSTEP
|
||||
|
||||
#import "common.h"
|
||||
#import "GNUstepBase/NSURL+GNUstepBase.h"
|
||||
|
||||
@implementation NSURL (GNUstepBaseAdditions)
|
||||
|
||||
- (id) initWithScheme: (NSString*)scheme
|
||||
user: (NSString*)user
|
||||
password: (NSString*)password
|
||||
host: (NSString*)host
|
||||
port: (NSNumber*)port
|
||||
fullPath: (NSString*)fullPath
|
||||
parameterString: (NSString*)parameterString
|
||||
query: (NSString*)query
|
||||
fragment: (NSString*)fragment
|
||||
{
|
||||
NSMutableString *urlString;
|
||||
NSString *s;
|
||||
|
||||
urlString = [scheme mutableCopy];
|
||||
[urlString appendString: @"://"];
|
||||
if ([user length] > 0 || [password length] > 0)
|
||||
{
|
||||
if (nil == (s = user)) s = @"";
|
||||
[urlString appendString:
|
||||
[s stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
||||
[urlString appendString: @":"];
|
||||
if (nil == (s = password)) s = @"";
|
||||
[urlString appendString:
|
||||
[s stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
||||
[urlString appendString: @"@"];
|
||||
}
|
||||
if ([host length] > 0)
|
||||
{
|
||||
[urlString appendString:
|
||||
[s stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
||||
}
|
||||
if ([port intValue] > 0)
|
||||
{
|
||||
[urlString appendString: @":"];
|
||||
[urlString appendFormat: @"%u", [port intValue]];
|
||||
}
|
||||
|
||||
if (nil == (s = fullPath)) s = @"";
|
||||
if ([s hasPrefix: @"/"] == NO)
|
||||
{
|
||||
[urlString appendString: @"/"];
|
||||
}
|
||||
[urlString appendString:
|
||||
[s stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
||||
|
||||
if ([parameterString length] > 0)
|
||||
{
|
||||
[urlString appendString: @";"];
|
||||
[urlString appendString: parameterString];
|
||||
}
|
||||
|
||||
if ([query length] > 0)
|
||||
{
|
||||
[urlString appendString: @"?"];
|
||||
[urlString appendString: query];
|
||||
}
|
||||
|
||||
if ([fragment length] > 0)
|
||||
{
|
||||
[urlString appendString: @"#"];
|
||||
[urlString appendString: fragment];
|
||||
}
|
||||
|
||||
self = [self initWithString: urlString];
|
||||
[urlString release];
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
||||
#ifndef GNUSTEP
|
||||
@implementation NSURL (GNUstepBase)
|
||||
/* For efficiency this is built in to the main library.
|
||||
*/
|
||||
- (NSString*) fullPath
|
||||
{
|
||||
NSRange r;
|
||||
|
@ -50,6 +122,6 @@
|
|||
return s;
|
||||
}
|
||||
@end
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
405
Source/NSURL.m
405
Source/NSURL.m
|
@ -53,6 +53,9 @@ function may be incorrect
|
|||
#import "Foundation/NSURLHandle.h"
|
||||
#import "Foundation/NSValue.h"
|
||||
|
||||
#import "GNUstepBase/NSURL+GNUstepBase.h"
|
||||
|
||||
|
||||
NSString * const NSURLErrorDomain = @"NSURLErrorDomain";
|
||||
NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey";
|
||||
|
||||
|
@ -61,6 +64,7 @@ NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey";
|
|||
@end
|
||||
|
||||
@implementation NSString (NSURLPrivate)
|
||||
|
||||
/* Like the normal percent escape method, but with additional characters
|
||||
* escaped.
|
||||
*/
|
||||
|
@ -121,6 +125,38 @@ NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey";
|
|||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface NSURL (GSPrivate)
|
||||
- (NSURL*) _URLBySettingPath: (NSString*)newPath;
|
||||
@end
|
||||
|
||||
@implementation NSURL (GSPrivate)
|
||||
|
||||
- (NSURL*) _URLBySettingPath: (NSString*)newPath
|
||||
{
|
||||
if ([self isFileURL])
|
||||
{
|
||||
return [NSURL fileURLWithPath: newPath];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSURL *u;
|
||||
|
||||
u = [[NSURL alloc] initWithScheme: [self scheme]
|
||||
user: [self user]
|
||||
password: [self password]
|
||||
host: [self host]
|
||||
port: [self port]
|
||||
fullPath: newPath
|
||||
parameterString: [self parameterString]
|
||||
query: [self query]
|
||||
fragment: [self fragment]];
|
||||
return [u autorelease];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
|
@ -564,25 +600,10 @@ static char *unescape(const char *from, char * to)
|
|||
|
||||
|
||||
|
||||
/**
|
||||
* This class permits manipulation of URLs and the resources to which they
|
||||
* refer. They can be used to represent absolute URLs or relative URLs
|
||||
* which are based upon an absolute URL. The relevant RFCs describing
|
||||
* how a URL is formatted, and what is legal in a URL are -
|
||||
* 1808, 1738, and 2396.<br />
|
||||
* Handling of the underlying resources is carried out by NSURLHandle
|
||||
* objects, but NSURL provides a simplified API wrapping these objects.
|
||||
*/
|
||||
@implementation NSURL
|
||||
|
||||
static NSUInteger urlAlign;
|
||||
|
||||
/**
|
||||
* Create and return a file URL with the supplied path.<br />
|
||||
* The value of aPath must be a valid filesystem path.<br />
|
||||
* Calls -initFileURLWithPath: which escapes characters in the
|
||||
* path where necessary.
|
||||
*/
|
||||
+ (id) fileURLWithPath: (NSString*)aPath
|
||||
{
|
||||
return AUTORELEASE([[NSURL alloc] initFileURLWithPath: aPath]);
|
||||
|
@ -597,23 +618,11 @@ static NSUInteger urlAlign;
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and return a URL with the supplied string, which should
|
||||
* be a string (containing percent escape codes where necessary)
|
||||
* conforming to the description (in RFC2396) of an absolute URL.<br />
|
||||
* Calls -initWithString:
|
||||
*/
|
||||
+ (id) URLWithString: (NSString*)aUrlString
|
||||
{
|
||||
return AUTORELEASE([[NSURL alloc] initWithString: aUrlString]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and return a URL with the supplied string, which should
|
||||
* be a string (containing percent escape codes where necessary)
|
||||
* conforming to the description (in RFC2396) of a relative URL.<br />
|
||||
* Calls -initWithString:relativeToURL:
|
||||
*/
|
||||
+ (id) URLWithString: (NSString*)aUrlString
|
||||
relativeToURL: (NSURL*)aBaseUrl
|
||||
{
|
||||
|
@ -621,17 +630,33 @@ static NSUInteger urlAlign;
|
|||
relativeToURL: aBaseUrl]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise by building a URL string from the supplied parameters
|
||||
* and calling -initWithString:relativeToURL:<br />
|
||||
* This method adds percent escapes to aPath if it contains characters
|
||||
* which need escaping.<br />
|
||||
* Accepts RFC2732 style IPv6 host addresses either with or without the
|
||||
* enclosing square brackets (MacOS-X at least up to version 10.5 does
|
||||
* not handle these correctly, but GNUstep does).<br />
|
||||
* Permits the 'aHost' part to contain 'username:password@host:port' or
|
||||
* 'host:port' in addition to a simple host name or address.
|
||||
*/
|
||||
- (id) initFileURLWithPath: (NSString*)aPath
|
||||
{
|
||||
NSFileManager *mgr = [NSFileManager defaultManager];
|
||||
BOOL flag = NO;
|
||||
|
||||
if ([aPath isAbsolutePath] == NO)
|
||||
{
|
||||
aPath = [[mgr currentDirectoryPath]
|
||||
stringByAppendingPathComponent: aPath];
|
||||
}
|
||||
if ([mgr fileExistsAtPath: aPath isDirectory: &flag] == YES)
|
||||
{
|
||||
if ([aPath isAbsolutePath] == NO)
|
||||
{
|
||||
aPath = [aPath stringByStandardizingPath];
|
||||
}
|
||||
if (flag == YES && [aPath hasSuffix: @"/"] == NO)
|
||||
{
|
||||
aPath = [aPath stringByAppendingString: @"/"];
|
||||
}
|
||||
}
|
||||
self = [self initWithScheme: NSURLFileScheme
|
||||
host: @"localhost"
|
||||
path: aPath];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithScheme: (NSString*)aScheme
|
||||
host: (NSString*)aHost
|
||||
path: (NSString*)aPath
|
||||
|
@ -707,59 +732,12 @@ static NSUInteger urlAlign;
|
|||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise as a file URL with the specified path (which must
|
||||
* be a valid path on the local filesystem).<br />
|
||||
* Converts relative paths to absolute ones.<br />
|
||||
* Appends a trailing slash to the path when necessary if it
|
||||
* specifies a directory.<br />
|
||||
* Calls -initWithScheme:host:path:
|
||||
*/
|
||||
- (id) initFileURLWithPath: (NSString*)aPath
|
||||
{
|
||||
NSFileManager *mgr = [NSFileManager defaultManager];
|
||||
BOOL flag = NO;
|
||||
|
||||
if ([aPath isAbsolutePath] == NO)
|
||||
{
|
||||
aPath = [[mgr currentDirectoryPath]
|
||||
stringByAppendingPathComponent: aPath];
|
||||
}
|
||||
if ([mgr fileExistsAtPath: aPath isDirectory: &flag] == YES)
|
||||
{
|
||||
if ([aPath isAbsolutePath] == NO)
|
||||
{
|
||||
aPath = [aPath stringByStandardizingPath];
|
||||
}
|
||||
if (flag == YES && [aPath hasSuffix: @"/"] == NO)
|
||||
{
|
||||
aPath = [aPath stringByAppendingString: @"/"];
|
||||
}
|
||||
}
|
||||
self = [self initWithScheme: NSURLFileScheme
|
||||
host: @"localhost"
|
||||
path: aPath];
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise as an absolute URL.<br />
|
||||
* Calls -initWithString:relativeToURL:
|
||||
*/
|
||||
- (id) initWithString: (NSString*)aUrlString
|
||||
{
|
||||
self = [self initWithString: aUrlString relativeToURL: nil];
|
||||
return self;
|
||||
}
|
||||
|
||||
/** <init />
|
||||
* Initialised using aUrlString and aBaseUrl. The value of aBaseUrl
|
||||
* may be nil, but aUrlString must be non-nil.<br />
|
||||
* Accepts RFC2732 style IPv6 host addresses.<br />
|
||||
* Parses a string wihthout a scheme as a simple path.<br />
|
||||
* Parses an empty string as an empty path.<br />
|
||||
* If the string cannot be parsed the method returns nil.
|
||||
*/
|
||||
- (id) initWithString: (NSString*)aUrlString
|
||||
relativeToURL: (NSURL*)aBaseUrl
|
||||
{
|
||||
|
@ -1300,9 +1278,6 @@ static NSUInteger urlAlign;
|
|||
return [[self absoluteString] isEqualToString: [other absoluteString]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full string describing the receiver resolved against its base.
|
||||
*/
|
||||
- (NSString*) absoluteString
|
||||
{
|
||||
NSString *absString = myData->absolute;
|
||||
|
@ -1320,10 +1295,6 @@ static NSUInteger urlAlign;
|
|||
return absString;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the receiver is an absolute URL, returns self. Otherwise returns an
|
||||
* absolute URL referring to the same resource as the receiver.
|
||||
*/
|
||||
- (NSURL*) absoluteURL
|
||||
{
|
||||
if (_baseURL == nil)
|
||||
|
@ -1336,10 +1307,6 @@ static NSUInteger urlAlign;
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the receiver is a relative URL, returns its base URL.<br />
|
||||
* Otherwise, returns nil.
|
||||
*/
|
||||
- (NSURL*) baseURL
|
||||
{
|
||||
return _baseURL;
|
||||
|
@ -1373,21 +1340,17 @@ static NSUInteger urlAlign;
|
|||
|
||||
if ((errorStr != nil) && (error != NULL))
|
||||
{
|
||||
NSDictionary *info;
|
||||
|
||||
info = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
errorStr, NSLocalizedDescriptionKey, nil];
|
||||
*error = [NSError errorWithDomain: @"NSURLError"
|
||||
code: 0
|
||||
userInfo: [NSDictionary
|
||||
dictionaryWithObjectsAndKeys: errorStr,
|
||||
NSLocalizedDescriptionKey, nil]];
|
||||
userInfo: info];
|
||||
}
|
||||
return (errorStr != nil);
|
||||
return nil == errorStr ? YES : NO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fragment portion of the receiver or nil if there is no
|
||||
* fragment supplied in the URL.<br />
|
||||
* The fragment is everything in the original URL string after a '#'<br />
|
||||
* File URLs do not have fragments.
|
||||
*/
|
||||
- (NSString*) fragment
|
||||
{
|
||||
NSString *fragment = nil;
|
||||
|
@ -1482,49 +1445,6 @@ static NSUInteger urlAlign;
|
|||
return ptr;
|
||||
}
|
||||
|
||||
- (NSString*) fullPath
|
||||
{
|
||||
NSString *path = nil;
|
||||
unsigned int len = 3;
|
||||
|
||||
if (_baseURL != nil)
|
||||
{
|
||||
if (baseData->path && *baseData->path)
|
||||
{
|
||||
len += strlen(baseData->path);
|
||||
}
|
||||
else if (baseData->hasNoPath == NO)
|
||||
{
|
||||
len++;
|
||||
}
|
||||
}
|
||||
if (myData->path && *myData->path)
|
||||
{
|
||||
len += strlen(myData->path);
|
||||
}
|
||||
else if (myData->hasNoPath == NO)
|
||||
{
|
||||
len++;
|
||||
}
|
||||
if (len > 3)
|
||||
{
|
||||
char buf[len];
|
||||
char *ptr;
|
||||
|
||||
ptr = [self _path: buf];
|
||||
path = [NSString stringWithUTF8String: ptr];
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host portion of the receiver or nil if there is no
|
||||
* host supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated and the string
|
||||
* treated as UTF8.<br />
|
||||
* Returns IPv6 addresses <em>without</em> the enclosing square brackets
|
||||
* required (by RFC2732) in URL strings.
|
||||
*/
|
||||
- (NSString*) host
|
||||
{
|
||||
NSString *host = nil;
|
||||
|
@ -1551,36 +1471,16 @@ static NSUInteger urlAlign;
|
|||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns YES if the receiver is a file URL, NO otherwise.
|
||||
*/
|
||||
- (BOOL) isFileURL
|
||||
{
|
||||
return myData->isFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads resource data for the specified client.
|
||||
* <p>
|
||||
* If shouldUseCache is YES then an attempt
|
||||
* will be made to locate a cached NSURLHandle to provide the
|
||||
* resource data, otherwise a new handle will be created and
|
||||
* cached.
|
||||
* </p>
|
||||
* <p>
|
||||
* If the handle does not have the data available, it will be
|
||||
* asked to load the data in the background by calling its
|
||||
* loadInBackground method.
|
||||
* </p>
|
||||
* <p>
|
||||
* The specified client (if non-nil) will be set up to receive
|
||||
* notifications of the progress of the background load process.
|
||||
* </p>
|
||||
* <p>
|
||||
* The processes current run loop must be run in order for the
|
||||
* background load operation to operate!
|
||||
* </p>
|
||||
*/
|
||||
- (NSString*) lastPathComponent
|
||||
{
|
||||
return [[self path] lastPathComponent];
|
||||
}
|
||||
|
||||
- (void) loadResourceDataNotifyingClient: (id)client
|
||||
usingCache: (BOOL)shouldUseCache
|
||||
{
|
||||
|
@ -1624,13 +1524,6 @@ static NSUInteger urlAlign;
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameter portion of the receiver or nil if there is no
|
||||
* parameter supplied in the URL.<br />
|
||||
* The parameters are everything in the original URL string after a ';'
|
||||
* but before the query.<br />
|
||||
* File URLs do not have parameters.
|
||||
*/
|
||||
- (NSString*) parameterString
|
||||
{
|
||||
NSString *parameters = nil;
|
||||
|
@ -1642,14 +1535,6 @@ static NSUInteger urlAlign;
|
|||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password portion of the receiver or nil if there is no
|
||||
* password supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated and the string
|
||||
* treated as UTF8 in GNUstep but this appears to be broken in MacOS-X.<br />
|
||||
* NB. because of its security implications it is recommended that you
|
||||
* do not use URLs with users and passwords unless necessary.
|
||||
*/
|
||||
- (NSString*) password
|
||||
{
|
||||
NSString *password = nil;
|
||||
|
@ -1664,17 +1549,6 @@ static NSUInteger urlAlign;
|
|||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path portion of the receiver.<br />
|
||||
* Replaces percent escapes with unescaped values, interpreting non-ascii
|
||||
* character sequences as UTF8.<br />
|
||||
* NB. This does not conform strictly to the RFCs, in that it includes a
|
||||
* leading slash ('/') character (whereas the path part of a URL strictly
|
||||
* should not) and the interpretation of non-ascii character is (strictly
|
||||
* speaking) undefined.<br />
|
||||
* Also, this breaks strict conformance in that a URL of file scheme is
|
||||
* treated as having a path (contrary to RFCs)
|
||||
*/
|
||||
- (NSString*) path
|
||||
{
|
||||
NSString *path = nil;
|
||||
|
@ -1720,12 +1594,11 @@ static NSUInteger urlAlign;
|
|||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the port portion of the receiver or nil if there is no
|
||||
* port supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated in GNUstep
|
||||
* but this appears to be broken in MacOS-X.
|
||||
*/
|
||||
- (NSString*) pathExtension
|
||||
{
|
||||
return [[self path] pathExtension];
|
||||
}
|
||||
|
||||
- (NSNumber*) port
|
||||
{
|
||||
NSNumber *port = nil;
|
||||
|
@ -1740,10 +1613,6 @@ static NSUInteger urlAlign;
|
|||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks a URL handle to return the property for the specified key and
|
||||
* returns the result.
|
||||
*/
|
||||
- (id) propertyForKey: (NSString*)propertyKey
|
||||
{
|
||||
NSURLHandle *handle = [self URLHandleUsingCache: YES];
|
||||
|
@ -1751,13 +1620,6 @@ static NSUInteger urlAlign;
|
|||
return [handle propertyForKey: propertyKey];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the query portion of the receiver or nil if there is no
|
||||
* query supplied in the URL.<br />
|
||||
* The query is everything in the original URL string after a '?'
|
||||
* but before the fragment.<br />
|
||||
* File URLs do not have queries.
|
||||
*/
|
||||
- (NSString*) query
|
||||
{
|
||||
NSString *query = nil;
|
||||
|
@ -1769,11 +1631,6 @@ static NSUInteger urlAlign;
|
|||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path of the receiver, without taking any base URL into account.
|
||||
* If the receiver is an absolute URL, -relativePath is the same as -path.<br />
|
||||
* Returns nil if there is no path specified for the URL.
|
||||
*/
|
||||
- (NSString*) relativePath
|
||||
{
|
||||
if (nil == _baseURL)
|
||||
|
@ -1792,10 +1649,6 @@ static NSUInteger urlAlign;
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the relative portion of the URL string. If the receiver is not
|
||||
* a relative URL, this returns the same as absoluteString.
|
||||
*/
|
||||
- (NSString*) relativeString
|
||||
{
|
||||
return _urlString;
|
||||
|
@ -1810,12 +1663,6 @@ static NSUInteger urlAlign;
|
|||
return [super replacementObjectForPortCoder: aCoder];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the resource data for the represented URL and returns the result.
|
||||
* The shouldUseCache flag determines whether data previously retrieved by
|
||||
* an existing NSURLHandle can be used to provide the data, or if it should
|
||||
* be refetched.
|
||||
*/
|
||||
- (NSData*) resourceDataUsingCache: (BOOL)shouldUseCache
|
||||
{
|
||||
NSURLHandle *handle = [self URLHandleUsingCache: YES];
|
||||
|
@ -1829,10 +1676,6 @@ static NSUInteger urlAlign;
|
|||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resource specifier of the URL ... the part which lies
|
||||
* after the scheme.
|
||||
*/
|
||||
- (NSString*) resourceSpecifier
|
||||
{
|
||||
NSRange range = [_urlString rangeOfString: @"://"];
|
||||
|
@ -1872,9 +1715,6 @@ static NSUInteger urlAlign;
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the scheme of the receiver.
|
||||
*/
|
||||
- (NSString*) scheme
|
||||
{
|
||||
NSString *scheme = nil;
|
||||
|
@ -1886,9 +1726,6 @@ static NSUInteger urlAlign;
|
|||
return scheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [NSURLHandle-writeProperty:forKey:] to set the named property.
|
||||
*/
|
||||
- (BOOL) setProperty: (id)property
|
||||
forKey: (NSString*)propertyKey
|
||||
{
|
||||
|
@ -1897,11 +1734,6 @@ static NSUInteger urlAlign;
|
|||
return [handle writeProperty: property forKey: propertyKey];
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [NSURLHandle-writeData:] to write the specified data object
|
||||
* to the resource identified by the receiver URL.<br />
|
||||
* Returns the result.
|
||||
*/
|
||||
- (BOOL) setResourceData: (NSData*)data
|
||||
{
|
||||
NSURLHandle *handle = [self URLHandleUsingCache: YES];
|
||||
|
@ -1921,9 +1753,6 @@ static NSUInteger urlAlign;
|
|||
return YES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URL with '/./' and '/../' sequences resolved etc.
|
||||
*/
|
||||
- (NSURL*) standardizedURL
|
||||
{
|
||||
char *url = buildURL(baseData, myData, YES);
|
||||
|
@ -1939,12 +1768,6 @@ static NSUInteger urlAlign;
|
|||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an NSURLHandle instance which may be used to write data to the
|
||||
* resource represented by the receiver URL, or read data from it.<br />
|
||||
* The shouldUseCache flag indicates whether a cached handle may be returned
|
||||
* or a new one should be created.
|
||||
*/
|
||||
- (NSURLHandle*) URLHandleUsingCache: (BOOL)shouldUseCache
|
||||
{
|
||||
NSURLHandle *handle = nil;
|
||||
|
@ -1966,14 +1789,6 @@ static NSUInteger urlAlign;
|
|||
return handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user portion of the receiver or nil if there is no
|
||||
* user supplied in the URL.<br />
|
||||
* Percent escape sequences in the user string are translated and
|
||||
* the whole is treated as UTF8 data.<br />
|
||||
* NB. because of its security implications it is recommended that you
|
||||
* do not use URLs with users and passwords unless necessary.
|
||||
*/
|
||||
- (NSString*) user
|
||||
{
|
||||
NSString *user = nil;
|
||||
|
@ -1988,6 +1803,30 @@ static NSUInteger urlAlign;
|
|||
return user;
|
||||
}
|
||||
|
||||
- (NSURL*) URLByAppendingPathComponent: (NSString*)pathComponent
|
||||
{
|
||||
return [self _URLBySettingPath:
|
||||
[[self path] stringByAppendingPathComponent:pathComponent]];
|
||||
}
|
||||
|
||||
- (NSURL*) URLByAppendingPathExtension: (NSString*)pathExtension
|
||||
{
|
||||
return [self _URLBySettingPath:
|
||||
[[self path] stringByAppendingPathExtension: pathExtension]];
|
||||
}
|
||||
|
||||
- (NSURL*) URLByDeletingLastPathComponent
|
||||
{
|
||||
return [self _URLBySettingPath:
|
||||
[[self path] stringByDeletingLastPathComponent]];
|
||||
}
|
||||
|
||||
- (NSURL*) URLByDeletingPathExtension
|
||||
{
|
||||
return [self _URLBySettingPath:
|
||||
[[self path] stringByDeletingPathExtension]];
|
||||
}
|
||||
|
||||
- (void) URLHandle: (NSURLHandle*)sender
|
||||
resourceDataDidBecomeAvailable: (NSData*)newData
|
||||
{
|
||||
|
@ -2089,3 +1928,41 @@ static NSUInteger urlAlign;
|
|||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSURL (GNUstepBase)
|
||||
- (NSString*) fullPath
|
||||
{
|
||||
NSString *path = nil;
|
||||
unsigned int len = 3;
|
||||
|
||||
if (_baseURL != nil)
|
||||
{
|
||||
if (baseData->path && *baseData->path)
|
||||
{
|
||||
len += strlen(baseData->path);
|
||||
}
|
||||
else if (baseData->hasNoPath == NO)
|
||||
{
|
||||
len++;
|
||||
}
|
||||
}
|
||||
if (myData->path && *myData->path)
|
||||
{
|
||||
len += strlen(myData->path);
|
||||
}
|
||||
else if (myData->hasNoPath == NO)
|
||||
{
|
||||
len++;
|
||||
}
|
||||
if (len > 3)
|
||||
{
|
||||
char buf[len];
|
||||
char *ptr;
|
||||
|
||||
ptr = [self _path: buf];
|
||||
path = [NSString stringWithUTF8String: ptr];
|
||||
}
|
||||
return path;
|
||||
}
|
||||
@end
|
||||
|
||||
|
|
Loading…
Reference in a new issue