2001-12-17 14:31:42 +00:00
|
|
|
|
/** NSUrl.m - Class NSURL
|
1999-02-13 00:50:41 +00:00
|
|
|
|
Copyright (C) 1999 Free Software Foundation, Inc.
|
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
|
Written by: Manuel Guesdon <mguesdon@sbuilders.com>
|
2001-12-17 14:31:42 +00:00
|
|
|
|
Date: Jan 1999
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
Rewrite by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
Date: Jun 2002
|
|
|
|
|
|
1999-02-13 00:50:41 +00:00
|
|
|
|
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 Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
This library is distributed in the hope that it will be useful,
|
1999-02-13 00:50:41 +00:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSURL class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
1999-02-13 00:50:41 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
1999-06-24 19:30:29 +00:00
|
|
|
|
Note from Manuel Guesdon:
|
1999-02-13 00:50:41 +00:00
|
|
|
|
* I've made some test to compare apple NSURL results
|
|
|
|
|
and GNUstep NSURL results but as there this class is not very documented, some
|
|
|
|
|
function may be incorrect
|
|
|
|
|
* I've put 2 functions to make tests. You can add your own tests
|
|
|
|
|
* Some functions are not implemented
|
|
|
|
|
*/
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <base/behavior.h>
|
|
|
|
|
#include <Foundation/NSObject.h>
|
|
|
|
|
#include <Foundation/NSCoder.h>
|
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
|
#include <Foundation/NSDictionary.h>
|
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
|
#include <Foundation/NSException.h>
|
2002-08-24 06:59:30 +00:00
|
|
|
|
#include <Foundation/NSFileManager.h>
|
1999-02-13 00:50:41 +00:00
|
|
|
|
#include <Foundation/NSConcreteNumber.h>
|
2002-06-05 12:39:28 +00:00
|
|
|
|
#include <Foundation/NSLock.h>
|
|
|
|
|
#include <Foundation/NSMapTable.h>
|
1999-02-13 00:50:41 +00:00
|
|
|
|
#include <Foundation/NSURLHandle.h>
|
|
|
|
|
#include <Foundation/NSURL.h>
|
2000-09-22 13:45:58 +00:00
|
|
|
|
#include <Foundation/NSRunLoop.h>
|
2002-06-05 12:39:28 +00:00
|
|
|
|
#include <Foundation/NSZone.h>
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2000-09-22 13:45:58 +00:00
|
|
|
|
NSString *NSURLFileScheme = @"file";
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-17 10:04:53 +00:00
|
|
|
|
/*
|
|
|
|
|
* Structure describing a URL.
|
|
|
|
|
* All the char* fields may be NULL pointers, except path, which
|
|
|
|
|
* is *always* non-null (though it may be an empty string).
|
|
|
|
|
*/
|
2002-06-05 12:39:28 +00:00
|
|
|
|
typedef struct {
|
2002-06-17 10:04:53 +00:00
|
|
|
|
id absolute; // Cache absolute string or nil
|
2002-06-05 12:39:28 +00:00
|
|
|
|
char *scheme;
|
|
|
|
|
char *user;
|
|
|
|
|
char *password;
|
|
|
|
|
char *host;
|
|
|
|
|
char *port;
|
2002-06-17 10:04:53 +00:00
|
|
|
|
char *path; // May never be NULL
|
2002-06-05 12:39:28 +00:00
|
|
|
|
char *parameters;
|
|
|
|
|
char *query;
|
|
|
|
|
char *fragment;
|
|
|
|
|
BOOL pathIsAbsolute;
|
2002-06-18 06:46:05 +00:00
|
|
|
|
BOOL hasNoPath;
|
2002-06-05 15:42:41 +00:00
|
|
|
|
BOOL isGeneric;
|
2002-08-24 06:00:44 +00:00
|
|
|
|
BOOL isFile;
|
2002-06-05 12:39:28 +00:00
|
|
|
|
} parsedURL;
|
|
|
|
|
|
|
|
|
|
#define myData ((parsedURL*)(self->_data))
|
|
|
|
|
#define baseData ((self->_baseURL == 0)?0:((parsedURL*)(self->_baseURL->_data)))
|
|
|
|
|
|
|
|
|
|
static NSLock *clientsLock = nil;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Local utility functions.
|
|
|
|
|
*/
|
|
|
|
|
static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize);
|
|
|
|
|
static id clientForHandle(void *data, NSURLHandle *hdl);
|
|
|
|
|
static char *findUp(char *str);
|
|
|
|
|
static void unescape(const char *from, char * to);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build an absolute URL as a C string
|
|
|
|
|
*/
|
|
|
|
|
static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize)
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
char *buf;
|
|
|
|
|
char *ptr;
|
|
|
|
|
char *tmp;
|
|
|
|
|
unsigned int len = 1;
|
1999-09-16 07:21:34 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (rel->scheme != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->scheme) + 3; // scheme://
|
|
|
|
|
}
|
|
|
|
|
if (rel->user != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->user) + 1; // user...@
|
|
|
|
|
}
|
|
|
|
|
if (rel->password != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->password) + 1; // :password
|
|
|
|
|
}
|
|
|
|
|
if (rel->host != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->host) + 1; // host.../
|
|
|
|
|
}
|
|
|
|
|
if (rel->port != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->port) + 1; // :port
|
|
|
|
|
}
|
|
|
|
|
if (rel->path != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->path) + 1; // path
|
|
|
|
|
}
|
|
|
|
|
if (base != 0 && base->path != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(base->path) + 1; // path
|
|
|
|
|
}
|
|
|
|
|
if (rel->parameters != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->parameters) + 1; // ;parameters
|
|
|
|
|
}
|
|
|
|
|
if (rel->query != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->query) + 1; // ?query
|
|
|
|
|
}
|
|
|
|
|
if (rel->fragment != 0)
|
|
|
|
|
{
|
|
|
|
|
len += strlen(rel->fragment) + 1; // #fragment
|
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
ptr = buf = (char*)NSZoneMalloc(GSAtomicMallocZone(), len);
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (rel->scheme != 0)
|
|
|
|
|
{
|
|
|
|
|
strcpy(ptr, rel->scheme);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
*ptr++ = ':';
|
|
|
|
|
}
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (rel->isGeneric == YES
|
|
|
|
|
|| rel->user != 0 || rel->password != 0 || rel->host != 0 || rel->port != 0)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
|
|
|
|
*ptr++ = '/';
|
|
|
|
|
*ptr++ = '/';
|
|
|
|
|
if (rel->user != 0 || rel->password != 0)
|
|
|
|
|
{
|
|
|
|
|
if (rel->user != 0)
|
|
|
|
|
{
|
|
|
|
|
strcpy(ptr, rel->user);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
|
|
|
|
if (rel->password != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = ':';
|
|
|
|
|
strcpy(ptr, rel->password);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
|
|
|
|
if (rel->host != 0 || rel->port != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '@';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rel->host != 0)
|
|
|
|
|
{
|
|
|
|
|
strcpy(ptr, rel->host);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
|
|
|
|
if (rel->port != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = ':';
|
|
|
|
|
strcpy(ptr, rel->port);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Now build path.
|
|
|
|
|
*/
|
1999-09-16 07:21:34 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
tmp = ptr;
|
|
|
|
|
if (rel->pathIsAbsolute == YES)
|
|
|
|
|
{
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (rel->hasNoPath == NO)
|
|
|
|
|
{
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
strcpy(tmp, rel->path);
|
|
|
|
|
}
|
|
|
|
|
else if (base == 0)
|
|
|
|
|
{
|
|
|
|
|
strcpy(tmp, rel->path);
|
|
|
|
|
}
|
|
|
|
|
else if (rel->path[0] == 0)
|
|
|
|
|
{
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (base->hasNoPath == NO)
|
|
|
|
|
{
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
strcpy(tmp, base->path);
|
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
else
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
|
|
|
|
char *start = base->path;
|
2002-08-24 06:59:30 +00:00
|
|
|
|
char *end = strrchr(start, '/');
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (end != 0)
|
|
|
|
|
{
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
strncpy(tmp, start, end - start);
|
|
|
|
|
tmp += (end - start);
|
|
|
|
|
}
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
strcpy(tmp, rel->path);
|
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (standardize == YES)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Compact '/./' to '/' and strip any trailing '/.'
|
|
|
|
|
*/
|
|
|
|
|
tmp = ptr;
|
|
|
|
|
while (*tmp != '\0')
|
|
|
|
|
{
|
|
|
|
|
if (tmp[0] == '/' && tmp[1] == '.'
|
|
|
|
|
&& (tmp[2] == '/' || tmp[2] == '\0'))
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Ensure we don't remove the leading '/'
|
|
|
|
|
*/
|
|
|
|
|
if (tmp == ptr && tmp[2] == '\0')
|
|
|
|
|
{
|
|
|
|
|
tmp[1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strcpy(tmp, &tmp[2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tmp++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Reduce any sequence of '/' characters to a single '/'
|
|
|
|
|
*/
|
|
|
|
|
tmp = ptr;
|
|
|
|
|
while (*tmp != '\0')
|
|
|
|
|
{
|
|
|
|
|
if (tmp[0] == '/' && tmp[1] == '/')
|
|
|
|
|
{
|
|
|
|
|
strcpy(tmp, &tmp[1]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tmp++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Reduce any '/something/../' sequence to '/' and a trailing
|
|
|
|
|
* "/something/.." to ""
|
|
|
|
|
*/
|
|
|
|
|
tmp = ptr;
|
|
|
|
|
while ((tmp = findUp(tmp)) != 0)
|
|
|
|
|
{
|
|
|
|
|
char *next = &tmp[3];
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
while (tmp > ptr)
|
|
|
|
|
{
|
|
|
|
|
if (*--tmp == '/')
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Ensure we don't remove the leading '/'
|
|
|
|
|
*/
|
|
|
|
|
if (tmp == ptr && *next == '\0')
|
|
|
|
|
{
|
|
|
|
|
tmp[1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strcpy(tmp, next);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-18 06:46:05 +00:00
|
|
|
|
/*
|
|
|
|
|
* if we have an empty path, we standardize to a single slash.
|
|
|
|
|
*/
|
|
|
|
|
tmp = ptr;
|
|
|
|
|
if (*tmp == '\0')
|
|
|
|
|
{
|
|
|
|
|
strcpy(tmp, "/");
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
|
|
|
|
|
if (rel->parameters != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = ';';
|
|
|
|
|
strcpy(ptr, rel->parameters);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
|
|
|
|
if (rel->query != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '?';
|
|
|
|
|
strcpy(ptr, rel->query);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
|
|
|
|
if (rel->fragment != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '#';
|
|
|
|
|
strcpy(ptr, rel->fragment);
|
|
|
|
|
ptr = &ptr[strlen(ptr)];
|
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return buf;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
static id clientForHandle(void *data, NSURLHandle *hdl)
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
id client = nil;
|
|
|
|
|
|
|
|
|
|
if (data != 0)
|
|
|
|
|
{
|
|
|
|
|
[clientsLock lock];
|
|
|
|
|
client = (id)NSMapGet((NSMapTable*)data, hdl);
|
|
|
|
|
[clientsLock unlock];
|
|
|
|
|
}
|
|
|
|
|
return client;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Locate a '/../ or trailing '/..'
|
|
|
|
|
*/
|
|
|
|
|
static char *findUp(char *str)
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
while (*str != '\0')
|
|
|
|
|
{
|
|
|
|
|
if (str[0] == '/' && str[1] == '.' && str[2] == '.'
|
|
|
|
|
&& (str[3] == '/' || str[3] == '\0'))
|
|
|
|
|
{
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-18 06:46:05 +00:00
|
|
|
|
/*
|
|
|
|
|
* Check a string to see if it contains only legal data characters
|
|
|
|
|
* or percent escape sequences.
|
|
|
|
|
*/
|
|
|
|
|
static BOOL legal(const char *str, const char *extras)
|
|
|
|
|
{
|
|
|
|
|
const char *mark = "-_.!~*'()";
|
|
|
|
|
|
|
|
|
|
if (str != 0)
|
|
|
|
|
{
|
|
|
|
|
while (*str != 0)
|
|
|
|
|
{
|
|
|
|
|
if (*str == '%' && isxdigit(str[1]) && isxdigit(str[2]))
|
|
|
|
|
{
|
|
|
|
|
str += 3;
|
|
|
|
|
}
|
|
|
|
|
else if (isalnum(*str))
|
|
|
|
|
{
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else if (strchr(mark, *str) != 0)
|
|
|
|
|
{
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else if (strchr(extras, *str) != 0)
|
|
|
|
|
{
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Convert percent escape sequences to individual characters.
|
|
|
|
|
*/
|
|
|
|
|
static void unescape(const char *from, char * to)
|
2002-05-02 06:10:51 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
while (*from != '\0')
|
|
|
|
|
{
|
|
|
|
|
if (*from == '%')
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
2002-05-02 06:10:51 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
from++;
|
|
|
|
|
if (isxdigit(*from))
|
|
|
|
|
{
|
|
|
|
|
if (*from <= '9')
|
|
|
|
|
{
|
|
|
|
|
c = *from - '0';
|
|
|
|
|
}
|
|
|
|
|
else if (*from <= 'A')
|
|
|
|
|
{
|
|
|
|
|
c = *from - 'A' + 10;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c = *from - 'a' + 10;
|
|
|
|
|
}
|
|
|
|
|
from++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Bad percent escape sequence in URL string"];
|
|
|
|
|
}
|
|
|
|
|
c <<= 4;
|
|
|
|
|
if (isxdigit(*from))
|
|
|
|
|
{
|
|
|
|
|
if (*from <= '9')
|
|
|
|
|
{
|
|
|
|
|
c |= *from - '0';
|
|
|
|
|
}
|
|
|
|
|
else if (*from <= 'A')
|
|
|
|
|
{
|
|
|
|
|
c |= *from - 'A' + 10;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c |= *from - 'a' + 10;
|
|
|
|
|
}
|
|
|
|
|
from++;
|
|
|
|
|
*to++ = c;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Bad percent escape sequence in URL string"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*to++ = *from++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*to = '\0';
|
2002-05-02 06:10:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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 simoplified API wrapping these objects.
|
|
|
|
|
*/
|
|
|
|
|
@implementation NSURL
|
2002-05-02 06:10:51 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Create and return a file URL with the supplied path.<br />
|
|
|
|
|
* The value of aPath must be a valid filesystem path.<br />
|
|
|
|
|
* Calls -initFileURLWithPath:
|
|
|
|
|
*/
|
|
|
|
|
+ (id) fileURLWithPath: (NSString*)aPath
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return AUTORELEASE([[NSURL alloc] initFileURLWithPath: aPath]);
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
+ (void) initialize
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (clientsLock == nil)
|
2002-05-02 06:10:51 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
clientsLock = [NSLock new];
|
2002-05-02 06:10:51 +00:00
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return AUTORELEASE([[NSURL alloc] initWithString: aUrlString]);
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[NSURL alloc] initWithString: aUrlString
|
|
|
|
|
relativeToURL: aBaseUrl]);
|
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialise by building a URL string from the supplied parameters
|
|
|
|
|
* and calling -initWithString:relativeToURL:
|
|
|
|
|
*/
|
|
|
|
|
- (id) initWithScheme: (NSString*)aScheme
|
|
|
|
|
host: (NSString*)aHost
|
|
|
|
|
path: (NSString*)aPath
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSString *aUrlString = [NSString alloc];
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if ([aHost length] > 0)
|
1999-09-16 07:21:34 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if ([aPath length] > 0)
|
|
|
|
|
{
|
|
|
|
|
aUrlString = [aUrlString initWithFormat: @"%@://%@/%@",
|
|
|
|
|
aScheme, aHost, aPath];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
else
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
|
|
|
|
aUrlString = [aUrlString initWithFormat: @"%@://%@/",
|
|
|
|
|
aScheme, aHost];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
else
|
2002-04-21 05:25:54 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if ([aPath length] > 0)
|
|
|
|
|
{
|
|
|
|
|
aUrlString = [aUrlString initWithFormat: @"%@:%@",
|
|
|
|
|
aScheme, aPath];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
aUrlString = [aUrlString initWithFormat: @"%@:",
|
|
|
|
|
aScheme];
|
|
|
|
|
}
|
2002-04-21 05:25:54 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
self = [self initWithString: aUrlString relativeToURL: nil];
|
|
|
|
|
RELEASE(aUrlString);
|
|
|
|
|
return self;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
2002-08-24 06:59:30 +00:00
|
|
|
|
* Initialise as a file URL with the specified path (which must
|
|
|
|
|
* be a valid path on the local filesystem).<br />
|
2002-08-24 09:35:26 +00:00
|
|
|
|
* Converts relative paths to absolute ones.<br />
|
2002-08-24 06:59:30 +00:00
|
|
|
|
* Appends a trailing slash to the path when necessary if it
|
|
|
|
|
* specifies a directory.<br />
|
|
|
|
|
* Calls -initWithScheme:host:path:
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*/
|
|
|
|
|
- (id) initFileURLWithPath: (NSString*)aPath
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-08-24 09:35:26 +00:00
|
|
|
|
BOOL flag = NO;
|
2002-08-24 06:59:30 +00:00
|
|
|
|
|
2002-08-24 09:35:26 +00:00
|
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath: aPath
|
|
|
|
|
isDirectory: &flag] == YES)
|
|
|
|
|
{
|
|
|
|
|
if ([aPath isAbsolutePath] == NO)
|
|
|
|
|
{
|
|
|
|
|
aPath = [aPath stringByStandardizingPath];
|
|
|
|
|
}
|
|
|
|
|
if (flag == YES && [aPath hasSuffix: @"/"] == NO)
|
2002-08-24 06:59:30 +00:00
|
|
|
|
{
|
|
|
|
|
aPath = [aPath stringByAppendingString: @"/"];
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
self = [self initWithScheme: NSURLFileScheme
|
|
|
|
|
host: nil
|
|
|
|
|
path: aPath];
|
|
|
|
|
return self;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialise as an absolute URL.<br />
|
|
|
|
|
* Calls -initWithString:relativeToURL:
|
|
|
|
|
*/
|
|
|
|
|
- (id) initWithString: (NSString*)aUrlString
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
self = [self initWithString: aUrlString relativeToURL: nil];
|
|
|
|
|
return self;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/** <init />
|
2002-08-23 20:57:42 +00:00
|
|
|
|
* Initialised using aUrlString and aBaseUrl. The value of aBaseUrl
|
2002-06-05 12:39:28 +00:00
|
|
|
|
* may be nil, but aUrlString must be non-nil.<br />
|
|
|
|
|
* If the string cannot be parsed the method returns nil.
|
|
|
|
|
*/
|
|
|
|
|
- (id) initWithString: (NSString*)aUrlString
|
|
|
|
|
relativeToURL: (NSURL*)aBaseUrl
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (aUrlString == nil)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ %@] nil string parameter",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
ASSIGNCOPY(_urlString, aUrlString);
|
|
|
|
|
ASSIGN(_baseURL, [aBaseUrl absoluteURL]);
|
|
|
|
|
NS_DURING
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
parsedURL *buf;
|
|
|
|
|
parsedURL *base = baseData;
|
|
|
|
|
unsigned size = [_urlString cStringLength];
|
|
|
|
|
char *end;
|
|
|
|
|
char *start;
|
|
|
|
|
char *ptr;
|
|
|
|
|
BOOL usesFragments = YES;
|
|
|
|
|
BOOL usesParameters = YES;
|
|
|
|
|
BOOL usesQueries = YES;
|
2002-06-05 15:42:41 +00:00
|
|
|
|
BOOL canBeGeneric = YES;
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
|
|
|
|
size += sizeof(parsedURL) + __alignof__(parsedURL) + 1;
|
|
|
|
|
buf = _data = (parsedURL*)NSZoneMalloc(GSAtomicMallocZone(), size);
|
|
|
|
|
memset(buf, '\0', size);
|
|
|
|
|
start = end = ptr = (char*)&buf[1];
|
|
|
|
|
[_urlString getCString: start];
|
|
|
|
|
|
2000-09-22 13:45:58 +00:00
|
|
|
|
/*
|
2002-06-05 12:39:28 +00:00
|
|
|
|
* Parse the scheme if possible.
|
2000-09-22 13:45:58 +00:00
|
|
|
|
*/
|
2002-06-05 12:39:28 +00:00
|
|
|
|
ptr = start;
|
|
|
|
|
if (isalpha(*ptr))
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
ptr++;
|
|
|
|
|
while (isalnum(*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
|
|
|
|
|
{
|
|
|
|
|
ptr++;
|
|
|
|
|
}
|
|
|
|
|
if (*ptr == ':')
|
|
|
|
|
{
|
|
|
|
|
buf->scheme = start; // Got scheme.
|
|
|
|
|
*ptr = '\0'; // Terminate it.
|
|
|
|
|
end = &ptr[1];
|
|
|
|
|
/*
|
2002-08-23 20:57:42 +00:00
|
|
|
|
* Standardise uppercase to lower.
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*/
|
|
|
|
|
while (--ptr > start)
|
|
|
|
|
{
|
|
|
|
|
if (isupper(*ptr))
|
|
|
|
|
{
|
|
|
|
|
*ptr = tolower(*ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-17 10:04:53 +00:00
|
|
|
|
if (base != 0 && base->scheme != 0
|
|
|
|
|
&& strcmp(base->scheme, buf->scheme) != 0)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException format:
|
|
|
|
|
@"scheme of base and relative parts does not match"];
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
start = end;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (buf->scheme == 0 && base != 0)
|
|
|
|
|
{
|
|
|
|
|
buf->scheme = base->scheme;
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
|
2000-09-22 13:45:58 +00:00
|
|
|
|
/*
|
2002-06-05 12:39:28 +00:00
|
|
|
|
* Set up scheme specific parsing options.
|
2000-09-22 13:45:58 +00:00
|
|
|
|
*/
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (buf->scheme != 0)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (strcmp(buf->scheme, "file") == 0)
|
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
usesFragments = NO;
|
|
|
|
|
usesParameters = NO;
|
|
|
|
|
usesQueries = NO;
|
2002-08-24 06:00:44 +00:00
|
|
|
|
buf->isFile = YES;
|
2002-06-05 15:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
else if (strcmp(buf->scheme, "mailto") == 0)
|
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
usesFragments = NO;
|
|
|
|
|
usesParameters = NO;
|
|
|
|
|
usesQueries = NO;
|
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (canBeGeneric == YES)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* Parse the 'authority'
|
|
|
|
|
* //user:password@host:port
|
|
|
|
|
*/
|
|
|
|
|
if (start[0] == '/' && start[1] == '/')
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
buf->isGeneric = YES;
|
|
|
|
|
start = end = &end[2];
|
2002-06-18 06:46:05 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Set 'end' to point to the start of the path, or just past
|
|
|
|
|
* the 'authority' if there is no path.
|
|
|
|
|
*/
|
2002-06-05 12:39:28 +00:00
|
|
|
|
end = strchr(start, '/');
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (end == 0)
|
|
|
|
|
{
|
|
|
|
|
buf->hasNoPath = YES;
|
|
|
|
|
end = &start[strlen(start)];
|
|
|
|
|
}
|
|
|
|
|
else
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
|
|
|
|
*end++ = '\0';
|
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Parser username:password part
|
|
|
|
|
*/
|
|
|
|
|
ptr = strchr(start, '@');
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
buf->user = start;
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
start = ptr;
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (legal(buf->user, ";:&=+$,") == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException format:
|
|
|
|
|
@"illegal character in user/password part"];
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
ptr = strchr(buf->user, ':');
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
buf->password = ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Parse host:port part
|
|
|
|
|
*/
|
|
|
|
|
buf->host = start;
|
|
|
|
|
ptr = strchr(buf->host, ':');
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
2002-06-18 06:46:05 +00:00
|
|
|
|
const char *str;
|
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
buf->port = ptr;
|
2002-06-18 06:46:05 +00:00
|
|
|
|
str = buf->port;
|
|
|
|
|
while (*str != 0)
|
|
|
|
|
{
|
|
|
|
|
if (*str == '%' && isxdigit(str[1]) && isxdigit(str[2]))
|
|
|
|
|
{
|
|
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
|
|
str++;
|
|
|
|
|
if (*str <= '9')
|
|
|
|
|
{
|
|
|
|
|
c = *str - '0';
|
|
|
|
|
}
|
|
|
|
|
else if (*str <= 'A')
|
|
|
|
|
{
|
|
|
|
|
c = *str - 'A' + 10;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c = *str - 'a' + 10;
|
|
|
|
|
}
|
|
|
|
|
c <<= 4;
|
|
|
|
|
str++;
|
|
|
|
|
if (*str <= '9')
|
|
|
|
|
{
|
|
|
|
|
c |= *str - '0';
|
|
|
|
|
}
|
|
|
|
|
else if (*str <= 'A')
|
|
|
|
|
{
|
|
|
|
|
c |= *str - 'A' + 10;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c |= *str - 'a' + 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isdigit(c))
|
|
|
|
|
{
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException format:
|
|
|
|
|
@"illegal port part"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (isdigit(*str))
|
|
|
|
|
{
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException format:
|
|
|
|
|
@"illegal character in port part"];
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
start = end;
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (legal(buf->host, "-") == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException format:
|
|
|
|
|
@"illegal character in user/password part"];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* If we have an authority component,
|
|
|
|
|
* this must be an absolute URL
|
|
|
|
|
*/
|
|
|
|
|
buf->pathIsAbsolute = YES;
|
|
|
|
|
base = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
1999-09-16 07:21:34 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (base != 0)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
buf->isGeneric = base->isGeneric;
|
|
|
|
|
}
|
|
|
|
|
if (*start == '/')
|
|
|
|
|
{
|
|
|
|
|
buf->pathIsAbsolute = YES;
|
|
|
|
|
start++;
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (usesFragments == YES)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* Strip fragment string from end of url.
|
|
|
|
|
*/
|
|
|
|
|
ptr = strchr(start, '#');
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
if (*ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
buf->fragment = ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (buf->fragment == 0 && base != 0)
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
buf->fragment = base->fragment;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
2002-06-05 15:42:41 +00:00
|
|
|
|
|
|
|
|
|
if (usesQueries == YES)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* Strip query string from end of url.
|
|
|
|
|
*/
|
|
|
|
|
ptr = strchr(start, '?');
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
if (*ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
buf->query = ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (buf->query == 0 && base != 0)
|
|
|
|
|
{
|
|
|
|
|
buf->query = base->query;
|
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (usesParameters == YES)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* Strip parameters string from end of url.
|
|
|
|
|
*/
|
|
|
|
|
ptr = strchr(start, ';');
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
if (*ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
buf->parameters = ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (buf->parameters == 0 && base != 0)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
buf->parameters = base->parameters;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
}
|
2002-06-05 15:42:41 +00:00
|
|
|
|
|
2002-08-24 06:16:13 +00:00
|
|
|
|
if (buf->isFile == YES)
|
|
|
|
|
{
|
|
|
|
|
buf->user = 0;
|
|
|
|
|
buf->password = 0;
|
|
|
|
|
buf->host = "localhost";
|
|
|
|
|
buf->port = 0;
|
|
|
|
|
buf->isGeneric = YES;
|
|
|
|
|
}
|
|
|
|
|
else if (base != 0
|
2002-06-05 15:42:41 +00:00
|
|
|
|
&& buf->user == 0 && buf->password == 0
|
|
|
|
|
&& buf->host == 0 && buf->port == 0)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
buf->user = base->user;
|
|
|
|
|
buf->password = base->password;
|
|
|
|
|
buf->host = base->host;
|
|
|
|
|
buf->port = base->port;
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Store the path.
|
|
|
|
|
*/
|
|
|
|
|
buf->path = start;
|
1999-09-16 07:21:34 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NS_HANDLER
|
1999-09-16 07:21:34 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSLog(@"%@", localException);
|
|
|
|
|
DESTROY(self);
|
1999-09-16 07:21:34 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
return self;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (void) dealloc
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (_clients != 0)
|
|
|
|
|
{
|
|
|
|
|
NSFreeMapTable(_clients);
|
|
|
|
|
_clients = 0;
|
|
|
|
|
}
|
|
|
|
|
if (_data != 0)
|
|
|
|
|
{
|
|
|
|
|
DESTROY(myData->absolute);
|
|
|
|
|
NSZoneFree(GSObjCZone(self), _data);
|
|
|
|
|
_data = 0;
|
|
|
|
|
}
|
|
|
|
|
DESTROY(_urlString);
|
|
|
|
|
DESTROY(_baseURL);
|
|
|
|
|
[super dealloc];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (NSShouldRetainWithZone(self, zone) == NO)
|
|
|
|
|
{
|
|
|
|
|
return [[isa allocWithZone: zone] initWithString: _urlString
|
|
|
|
|
relativeToURL: _baseURL];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return RETAIN(self);
|
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (NSString*) description
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSString *dscr = _urlString;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (_baseURL != nil)
|
|
|
|
|
{
|
|
|
|
|
dscr = [dscr stringByAppendingFormat: @" -- %@", _baseURL];
|
|
|
|
|
}
|
|
|
|
|
return dscr;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
[aCoder encodeObject: _urlString];
|
|
|
|
|
[aCoder encodeObject: _baseURL];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (unsigned int) hash
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return [[self absoluteString] hash];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSURL *base;
|
|
|
|
|
NSString *rel;
|
|
|
|
|
|
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(id) at: &rel];
|
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(id) at: &base];
|
|
|
|
|
self = [self initWithString: rel relativeToURL: base];
|
|
|
|
|
RELEASE(rel);
|
|
|
|
|
RELEASE(base);
|
|
|
|
|
return self;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (BOOL) isEqual: (id)other
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (other == nil || [other isKindOfClass: [NSURL class]] == NO)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return [[self absoluteString] isEqualToString: [other absoluteString]];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the full string describing the receiver resiolved against its base.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) absoluteString
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSString *absString = myData->absolute;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (absString == nil)
|
|
|
|
|
{
|
|
|
|
|
char *url = buildURL(baseData, myData, NO);
|
|
|
|
|
unsigned len = strlen(url);
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
absString = [[NSString alloc] initWithCStringNoCopy: url
|
|
|
|
|
length: len
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
myData->absolute = absString;
|
|
|
|
|
}
|
|
|
|
|
return absString;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* If the receiver is an absolute URL, returns self. Otherwise returns an
|
|
|
|
|
* absolute URL referring to the same resource as the receiver.
|
|
|
|
|
*/
|
|
|
|
|
- (NSURL*) absoluteURL
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (_baseURL != nil)
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [NSURL URLWithString: [self absoluteString]];
|
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* If the receiver is a relative URL, returns its base URL.<br />
|
|
|
|
|
* Otherwise, returns nil.
|
|
|
|
|
*/
|
|
|
|
|
- (NSURL*) baseURL
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return _baseURL;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSString *fragment = nil;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (myData->fragment != 0)
|
|
|
|
|
{
|
|
|
|
|
fragment = [NSString stringWithUTF8String: myData->fragment];
|
|
|
|
|
}
|
|
|
|
|
return fragment;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the host portion of the receiver or nil if there is no
|
2002-06-17 16:20:21 +00:00
|
|
|
|
* host supplied in the URL.<br />
|
|
|
|
|
* Percent escape sequences in the user string are translated and the string
|
|
|
|
|
* treated as UTF8.<br />
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*/
|
|
|
|
|
- (NSString*) host
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSString *host = nil;
|
|
|
|
|
|
|
|
|
|
if (myData->host != 0)
|
|
|
|
|
{
|
2002-06-17 16:20:21 +00:00
|
|
|
|
char buf[strlen(myData->host)+1];
|
|
|
|
|
|
|
|
|
|
unescape(myData->host, buf);
|
|
|
|
|
host = [NSString stringWithUTF8String: buf];
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
return host;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns YES if the recevier is a file URL, NO otherwise.
|
|
|
|
|
*/
|
|
|
|
|
- (BOOL) isFileURL
|
|
|
|
|
{
|
2002-08-24 06:00:44 +00:00
|
|
|
|
return myData->isFile;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
2002-06-06 14:02:59 +00:00
|
|
|
|
* 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>
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*/
|
2000-09-22 13:45:58 +00:00
|
|
|
|
- (void) loadResourceDataNotifyingClient: (id)client
|
|
|
|
|
usingCache: (BOOL)shouldUseCache
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2000-09-22 13:45:58 +00:00
|
|
|
|
NSURLHandle *handle = [self URLHandleUsingCache: shouldUseCache];
|
|
|
|
|
NSRunLoop *loop;
|
|
|
|
|
NSDate *future;
|
|
|
|
|
|
|
|
|
|
if (client != nil)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
|
|
|
|
[clientsLock lock];
|
|
|
|
|
if (_clients == 0)
|
|
|
|
|
{
|
|
|
|
|
_clients = NSCreateMapTable (NSNonRetainedObjectMapKeyCallBacks,
|
|
|
|
|
NSNonRetainedObjectMapValueCallBacks, 0);
|
|
|
|
|
}
|
|
|
|
|
NSMapInsert((NSMapTable*)_clients, (void*)handle, (void*)client);
|
|
|
|
|
[clientsLock unlock];
|
2002-06-06 14:02:59 +00:00
|
|
|
|
[handle addClient: self];
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Kick off the load process.
|
|
|
|
|
*/
|
|
|
|
|
[handle loadInBackground];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Keep the runloop going until the load has completed (or failed).
|
|
|
|
|
*/
|
|
|
|
|
loop = [NSRunLoop currentRunLoop];
|
|
|
|
|
future = [NSDate distantFuture];
|
|
|
|
|
while ([handle status] == NSURLHandleLoadInProgress)
|
|
|
|
|
{
|
|
|
|
|
[loop runMode: NSDefaultRunLoopMode beforeDate: future];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (client != nil)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
2002-06-06 14:02:59 +00:00
|
|
|
|
[handle removeClient: self];
|
2002-06-05 12:39:28 +00:00
|
|
|
|
[clientsLock lock];
|
|
|
|
|
NSMapRemove((NSMapTable*)_clients, (void*)handle);
|
|
|
|
|
[clientsLock unlock];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
if (myData->parameters != 0)
|
|
|
|
|
{
|
|
|
|
|
parameters = [NSString stringWithUTF8String: myData->parameters];
|
|
|
|
|
}
|
|
|
|
|
return parameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the password portion of the receiver or nil if there is no
|
|
|
|
|
* password supplied in the URL.<br />
|
2002-06-17 16:20:21 +00:00
|
|
|
|
* 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 />
|
2002-06-05 12:39:28 +00:00
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
if (myData->password != 0)
|
|
|
|
|
{
|
2002-06-17 16:20:21 +00:00
|
|
|
|
char buf[strlen(myData->password)+1];
|
|
|
|
|
|
|
|
|
|
unescape(myData->password, buf);
|
|
|
|
|
password = [NSString stringWithUTF8String: buf];
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
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 (wheras the path part of a URL strictly
|
|
|
|
|
* should not) and the interpretation of non-ascii character is (strictly
|
2002-08-23 20:57:42 +00:00
|
|
|
|
* speaking) undefined.<br />
|
|
|
|
|
* Also, this breaks strict conformance in that a URL of file scheme is
|
|
|
|
|
* treated as having a path (contrary to RFCs)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*/
|
|
|
|
|
- (NSString*) path
|
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
NSString *path = nil;
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* If this scheme is from a URL without generic format, there is no path.
|
|
|
|
|
*/
|
2002-08-24 06:16:13 +00:00
|
|
|
|
if (myData->isGeneric == YES)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
2002-06-05 15:42:41 +00:00
|
|
|
|
unsigned int len = (_baseURL ? strlen(baseData->path) : 0)
|
|
|
|
|
+ strlen(myData->path) + 3;
|
|
|
|
|
char buf[len];
|
|
|
|
|
char *tmp = buf;
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (myData->pathIsAbsolute == YES)
|
2002-06-05 12:39:28 +00:00
|
|
|
|
{
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (myData->hasNoPath == NO)
|
|
|
|
|
{
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
}
|
2002-06-05 15:42:41 +00:00
|
|
|
|
strcpy(tmp, myData->path);
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
2002-06-05 15:42:41 +00:00
|
|
|
|
else if (_baseURL == nil)
|
|
|
|
|
{
|
|
|
|
|
strcpy(tmp, myData->path);
|
|
|
|
|
}
|
|
|
|
|
else if (*myData->path == 0)
|
|
|
|
|
{
|
2002-06-18 06:46:05 +00:00
|
|
|
|
if (baseData->hasNoPath == NO)
|
|
|
|
|
{
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
}
|
2002-06-05 15:42:41 +00:00
|
|
|
|
strcpy(tmp, baseData->path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *start = baseData->path;
|
|
|
|
|
char *end = strrchr(start, '/');
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
if (end != 0)
|
|
|
|
|
{
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
strncpy(tmp, start, end - start);
|
|
|
|
|
tmp += end - start;
|
|
|
|
|
}
|
|
|
|
|
*tmp++ = '/';
|
|
|
|
|
strcpy(tmp, myData->path);
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
2002-06-05 15:42:41 +00:00
|
|
|
|
unescape(buf, buf);
|
|
|
|
|
path = [NSString stringWithUTF8String: buf];
|
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the port portion of the receiver or nil if there is no
|
2002-06-17 16:20:21 +00:00
|
|
|
|
* 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.
|
2002-06-05 12:39:28 +00:00
|
|
|
|
*/
|
|
|
|
|
- (NSNumber*) port
|
|
|
|
|
{
|
|
|
|
|
NSNumber *port = nil;
|
|
|
|
|
|
|
|
|
|
if (myData->port != 0)
|
|
|
|
|
{
|
2002-06-17 16:20:21 +00:00
|
|
|
|
char buf[strlen(myData->port)+1];
|
|
|
|
|
|
|
|
|
|
unescape(myData->port, buf);
|
|
|
|
|
port = [NSNumber numberWithUnsignedShort: atol(buf)];
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (myData->query != 0)
|
|
|
|
|
{
|
|
|
|
|
query = [NSString stringWithUTF8String: myData->query];
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
NSString *path = nil;
|
|
|
|
|
|
|
|
|
|
if (myData->path != 0)
|
|
|
|
|
{
|
|
|
|
|
path = [NSString stringWithUTF8String: myData->path];
|
|
|
|
|
}
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Loads the resource data for the represented URL and returns the result.
|
|
|
|
|
* The shoulduseCache flag determines whether an existing cached NSURLHandle
|
|
|
|
|
* can be used to provide the data.
|
|
|
|
|
*/
|
2000-09-22 13:45:58 +00:00
|
|
|
|
- (NSData*) resourceDataUsingCache: (BOOL)shouldUseCache
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2000-09-22 13:45:58 +00:00
|
|
|
|
NSURLHandle *handle = [self URLHandleUsingCache: shouldUseCache];
|
|
|
|
|
NSData *data;
|
|
|
|
|
|
|
|
|
|
if (shouldUseCache == NO || [handle status] != NSURLHandleLoadSucceeded)
|
|
|
|
|
{
|
|
|
|
|
[self loadResourceDataNotifyingClient: self
|
|
|
|
|
usingCache: shouldUseCache];
|
|
|
|
|
}
|
|
|
|
|
data = [handle resourceData];
|
|
|
|
|
return data;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the resource specifier of the URL ... the part which lies
|
|
|
|
|
* after the scheme.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) resourceSpecifier
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSRange range = [_urlString rangeOfString: @"://"];
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (range.length > 0)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return [_urlString substringFromIndex: range.location + 1];
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
2002-06-05 12:39:28 +00:00
|
|
|
|
else
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Cope with URLs missing net_path info - <scheme>:/<path>...
|
|
|
|
|
*/
|
|
|
|
|
range = [_urlString rangeOfString: @":"];
|
|
|
|
|
if (range.length > 0)
|
2000-09-22 13:45:58 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
return [_urlString substringFromIndex: range.location + 1];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return _urlString;
|
2000-09-22 13:45:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the scheme of the receiver.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) scheme
|
|
|
|
|
{
|
|
|
|
|
NSString *scheme = nil;
|
|
|
|
|
|
|
|
|
|
if (myData->scheme != 0)
|
|
|
|
|
{
|
|
|
|
|
scheme = [NSString stringWithUTF8String: myData->scheme];
|
|
|
|
|
}
|
|
|
|
|
return scheme;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calls [NSURLHandle-writeProperty:forKey:] to set the named property.
|
|
|
|
|
*/
|
|
|
|
|
- (BOOL) setProperty: (id)property
|
|
|
|
|
forKey: (NSString*)propertyKey
|
|
|
|
|
{
|
|
|
|
|
NSURLHandle *handle = [self URLHandleUsingCache: YES];
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
1999-06-24 19:30:29 +00:00
|
|
|
|
- (BOOL) setResourceData: (NSData*)data
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2000-09-22 13:45:58 +00:00
|
|
|
|
NSURLHandle *handle = [self URLHandleUsingCache: YES];
|
|
|
|
|
|
2001-12-12 14:17:03 +00:00
|
|
|
|
if (handle == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
if ([handle writeData: data] == NO)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
[self loadResourceDataNotifyingClient: self
|
|
|
|
|
usingCache: YES];
|
|
|
|
|
if ([handle resourceData] == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a URL with '/./' and '/../' sequences resolved etc.
|
|
|
|
|
*/
|
|
|
|
|
- (NSURL*) standardizedURL
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
char *url = buildURL(baseData, myData, YES);
|
|
|
|
|
unsigned len = strlen(url);
|
|
|
|
|
NSString *str;
|
|
|
|
|
NSURL *tmp;
|
|
|
|
|
|
|
|
|
|
str = [[NSString alloc] initWithCStringNoCopy: url
|
|
|
|
|
length: len
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
tmp = [NSURL URLWithString: str];
|
|
|
|
|
RELEASE(str);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
if (shouldUseCache)
|
|
|
|
|
{
|
|
|
|
|
handle = [NSURLHandle cachedHandleForURL: self];
|
|
|
|
|
}
|
|
|
|
|
if (handle == nil)
|
|
|
|
|
{
|
|
|
|
|
Class c = [NSURLHandle URLHandleClassForURL: self];
|
|
|
|
|
|
|
|
|
|
if (c != 0)
|
|
|
|
|
{
|
|
|
|
|
handle = [[c alloc] initWithURL: self cached: shouldUseCache];
|
|
|
|
|
AUTORELEASE(handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return handle;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the user portion of the receiver or nil if there is no
|
|
|
|
|
* user supplied in the URL.<br />
|
2002-06-17 16:20:21 +00:00
|
|
|
|
* Percent escape sequences in the user string are translated and
|
|
|
|
|
* the whole is treated as UTF8 data.<br />
|
2002-06-05 12:39:28 +00:00
|
|
|
|
* NB. because of its security implications it is recommended that you
|
|
|
|
|
* do not use URLs with users and passwords unless necessary.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) user
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
2002-06-05 12:39:28 +00:00
|
|
|
|
NSString *user = nil;
|
2000-09-22 13:45:58 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
if (myData->user != 0)
|
|
|
|
|
{
|
2002-06-17 16:20:21 +00:00
|
|
|
|
char buf[strlen(myData->user)+1];
|
|
|
|
|
|
|
|
|
|
unescape(myData->user, buf);
|
|
|
|
|
user = [NSString stringWithUTF8String: buf];
|
2002-06-05 12:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) URLHandle: (NSURLHandle*)sender
|
|
|
|
|
resourceDataDidBecomeAvailable: (NSData*)newData
|
|
|
|
|
{
|
|
|
|
|
[clientForHandle(_clients, sender) URL: self
|
|
|
|
|
resourceDataDidBecomeAvailable: newData];
|
|
|
|
|
}
|
|
|
|
|
- (void) URLHandle: (NSURLHandle*)sender
|
|
|
|
|
resourceDidFailLoadingWithReason: (NSString*)reason
|
|
|
|
|
{
|
|
|
|
|
[clientForHandle(_clients, sender) URL: self
|
|
|
|
|
resourceDidFailLoadingWithReason: reason];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) URLHandleResourceDidBeginLoading: (NSURLHandle*)sender
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) URLHandleResourceDidCancelLoading: (NSURLHandle*)sender
|
|
|
|
|
{
|
|
|
|
|
[clientForHandle(_clients, sender) URLResourceDidCancelLoading: self];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) URLHandleResourceDidFinishLoading: (NSURLHandle*)sender
|
|
|
|
|
{
|
|
|
|
|
[clientForHandle(_clients, sender) URLResourceDidFinishLoading: self];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
|
1999-02-13 00:50:41 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 14:30:00 +00:00
|
|
|
|
|
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* An informal protocol to which clients may conform if they wish to be
|
|
|
|
|
* notified of the progress in loading a URL for them. The default
|
|
|
|
|
* implementations of these methods do nothing.
|
|
|
|
|
*/
|
1999-02-13 00:50:41 +00:00
|
|
|
|
@implementation NSObject (NSURLClient)
|
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
|
- (void) URL: (NSURL*)sender
|
2000-09-22 13:45:58 +00:00
|
|
|
|
resourceDataDidBecomeAvailable: (NSData*)newBytes
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (void) URL: (NSURL*)sender
|
|
|
|
|
resourceDidFailLoadingWithReason: (NSString*)reason
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
|
- (void) URLResourceDidCancelLoading: (NSURL*)sender
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-06-05 12:39:28 +00:00
|
|
|
|
- (void) URLResourceDidFinishLoading: (NSURL*)sender
|
1999-02-13 00:50:41 +00:00
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
|
|
|
|
@end
|