]> git.saurik.com Git - apple/cf.git/blame - URL.subproj/CFURL.h
CF-368.28.tar.gz
[apple/cf.git] / URL.subproj / CFURL.h
CommitLineData
9ce05555 1/*
d8925383 2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
9ce05555
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9ce05555
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/* CFURL.h
d8925383 24 Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
9ce05555
A
25*/
26
27#if !defined(__COREFOUNDATION_CFURL__)
28#define __COREFOUNDATION_CFURL__ 1
29
30#include <CoreFoundation/CFBase.h>
31#include <CoreFoundation/CFData.h>
32#include <CoreFoundation/CFString.h>
33
34#if defined(__cplusplus)
35extern "C" {
36#endif
37
38typedef enum {
39 kCFURLPOSIXPathStyle = 0,
40 kCFURLHFSPathStyle,
41 kCFURLWindowsPathStyle
42} CFURLPathStyle;
43
44typedef const struct __CFURL * CFURLRef;
45
46/* CFURLs are composed of two fundamental pieces - their string, and a */
47/* (possibly NULL) base URL. A relative URL is one in which the string */
48/* by itself does not fully specify the URL (for instance "myDir/image.tiff"); */
49/* an absolute URL is one in which the string does fully specify the URL */
50/* ("file://localhost/myDir/image.tiff"). Absolute URLs always have NULL */
51/* base URLs; however, it is possible for a URL to have a NULL base, and still */
52/* not be absolute. Such a URL has only a relative string, and cannot be */
53/* resolved. Two CFURLs are considered equal if and only if their strings */
54/* are equal and their bases are equal. In other words, */
55/* "file://localhost/myDir/image.tiff" is NOT equal to the URL with relative */
56/* string "myDir/image.tiff" and base URL "file://localhost/". Clients that */
57/* need these less strict form of equality should convert all URLs to their */
58/* absolute form via CFURLCopyAbsoluteURL(), then compare the absolute forms. */
59
60CF_EXPORT
61CFTypeID CFURLGetTypeID(void);
62
63/* encoding will be used both to interpret the bytes of URLBytes, and to */
64/* interpret any percent-escapes within the bytes. */
65CF_EXPORT
66CFURLRef CFURLCreateWithBytes(CFAllocatorRef allocator, const UInt8 *URLBytes, CFIndex length, CFStringEncoding encoding, CFURLRef baseURL);
67
68/* Escapes any character that is not 7-bit ASCII with the byte-code */
69/* for the given encoding. If escapeWhitespace is true, whitespace */
70/* characters (' ', '\t', '\r', '\n') will be escaped also (desirable */
71/* if embedding the URL into a larger text stream like HTML) */
72CF_EXPORT
73CFDataRef CFURLCreateData(CFAllocatorRef allocator, CFURLRef url, CFStringEncoding encoding, Boolean escapeWhitespace);
74
75/* Any escape sequences in URLString will be interpreted via UTF-8. */
76CF_EXPORT
77CFURLRef CFURLCreateWithString(CFAllocatorRef allocator, CFStringRef URLString, CFURLRef baseURL);
78
79#if MAC_OS_X_VERSION_10_3 <= MAC_OS_X_VERSION_MAX_ALLOWED
80
81/* Create an absolute URL directly, without requiring the extra step */
82/* of calling CFURLCopyAbsoluteURL(). If useCompatibilityMode is */
83/* true, the rules historically used on the web are used to resolve */
84/* relativeString against baseURL - these rules are generally listed */
85/* in the RFC as optional or alternate interpretations. Otherwise, */
86/* the strict rules from the RFC are used. The major differences are */
87/* that in compatibility mode, we are lenient of the scheme appearing */
88/* in relative portion, leading "../" components are removed from the */
89/* final URL's path, and if the relative portion contains only */
90/* resource specifier pieces (query, parameters, and fragment), then */
91/* the last path component of the base URL will not be deleted */
d8925383 92CF_EXPORT
9ce05555
A
93CFURLRef CFURLCreateAbsoluteURLWithBytes(CFAllocatorRef alloc, const UInt8 *relativeURLBytes, CFIndex length, CFStringEncoding encoding, CFURLRef baseURL, Boolean useCompatibilityMode) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
94#endif
95
96/* filePath should be the URL's path expressed as a path of the type */
97/* fsType. If filePath is not absolute, the resulting URL will be */
98/* considered relative to the current working directory (evaluated */
99/* at creation time). isDirectory determines whether filePath is */
100/* treated as a directory path when resolving against relative path */
101/* components */
102CF_EXPORT
103CFURLRef CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, Boolean isDirectory);
104
105CF_EXPORT
106CFURLRef CFURLCreateFromFileSystemRepresentation(CFAllocatorRef allocator, const UInt8 *buffer, CFIndex bufLen, Boolean isDirectory);
107
108CF_EXPORT
109CFURLRef CFURLCreateWithFileSystemPathRelativeToBase(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, Boolean isDirectory, CFURLRef baseURL);
110
111CF_EXPORT
112CFURLRef CFURLCreateFromFileSystemRepresentationRelativeToBase(CFAllocatorRef allocator, const UInt8 *buffer, CFIndex bufLen, Boolean isDirectory, CFURLRef baseURL);
113
114/* Fills buffer with the file system's native representation of */
115/* url's path. No more than maxBufLen bytes are written to buffer. */
116/* The buffer should be at least the maximum path length for */
117/* the file system in question to avoid failures for insufficiently */
118/* large buffers. If resolveAgainstBase is true, the url's relative */
119/* portion is resolved against its base before the path is computed. */
120/* Returns success or failure. */
121CF_EXPORT
122Boolean CFURLGetFileSystemRepresentation(CFURLRef url, Boolean resolveAgainstBase, UInt8 *buffer, CFIndex maxBufLen);
123
124/* Creates a new URL by resolving the relative portion of relativeURL against its base. */
125CF_EXPORT
126CFURLRef CFURLCopyAbsoluteURL(CFURLRef relativeURL);
127
128/* Returns the URL's string. */
129CF_EXPORT
130CFStringRef CFURLGetString(CFURLRef anURL);
131
132/* Returns the base URL if it exists */
133CF_EXPORT
134CFURLRef CFURLGetBaseURL(CFURLRef anURL);
135
136/*
137All URLs can be broken into two pieces - the scheme (preceding the
138first colon) and the resource specifier (following the first colon).
139Most URLs are also "standard" URLs conforming to RFC 1808 (available
140from www.w3c.org). This category includes URLs of the file, http,
141https, and ftp schemes, to name a few. Standard URLs start the
142resource specifier with two slashes ("//"), and can be broken into
143four distinct pieces - the scheme, the net location, the path, and
144further resource specifiers (typically an optional parameter, query,
145and/or fragment). The net location appears immediately following
146the two slashes and goes up to the next slash; it's format is
147scheme-specific, but is usually composed of some or all of a username,
148password, host name, and port. The path is a series of path components
149separated by slashes; if the net location is present, the path always
150begins with a slash. Standard URLs can be relative to another URL,
151in which case at least the scheme and possibly other pieces as well
152come from the base URL (see RFC 1808 for precise details when resolving
153a relative URL against its base). The full URL is therefore
154
155<scheme> "://" <net location> <path, always starting with slash> <add'l resource specifiers>
156
157If a given CFURL can be decomposed (that is, conforms to RFC 1808), you
158can ask for each of the four basic pieces (scheme, net location, path,
159and resource specifer) separately, as well as for its base URL. The
160basic pieces are returned with any percent escape sequences still in
161place (although note that the scheme may not legally include any
162percent escapes); this is to allow the caller to distinguish between
163percent sequences that may have syntactic meaning if replaced by the
164character being escaped (for instance, a '/' in a path component).
165Since only the individual schemes know which characters are
166syntactically significant, CFURL cannot safely replace any percent
167escape sequences. However, you can use
168CFURLCreateStringByReplacingPercentEscapes() to create a new string with
169the percent escapes removed; see below.
170
171If a given CFURL can not be decomposed, you can ask for its scheme and its
172resource specifier; asking it for its net location or path will return NULL.
173
174To get more refined information about the components of a decomposable
175CFURL, you may ask for more specific pieces of the URL, expressed with
176the percent escapes removed. The available functions are CFURLCopyHostName(),
177CFURLGetPortNumber() (returns an Int32), CFURLCopyUserName(),
178CFURLCopyPassword(), CFURLCopyQuery(), CFURLCopyParameters(), and
179CFURLCopyFragment(). Because the parameters, query, and fragment of an
180URL may contain scheme-specific syntaxes, these methods take a second
181argument, giving a list of characters which should NOT be replaced if
182percent escaped. For instance, the ftp parameter syntax gives simple
183key-value pairs as "<key>=<value>;" Clearly if a key or value includes
184either '=' or ';', it must be escaped to avoid corrupting the meaning of
185the parameters, so the caller may request the parameter string as
186
187CFStringRef myParams = CFURLCopyParameters(ftpURL, CFSTR("=;%"));
188
189requesting that all percent escape sequences be replaced by the represented
190characters, except for escaped '=', '%' or ';' characters. Pass the empty
191string (CFSTR("")) to request that all percent escapes be replaced, or NULL
192to request that none be.
193*/
194
195/* Returns true if anURL conforms to RFC 1808 */
196CF_EXPORT
197Boolean CFURLCanBeDecomposed(CFURLRef anURL);
198
199/* The next several methods leave any percent escape sequences intact */
200
201CF_EXPORT
202CFStringRef CFURLCopyScheme(CFURLRef anURL);
203
204/* NULL if CFURLCanBeDecomposed(anURL) is false */
205CF_EXPORT
206CFStringRef CFURLCopyNetLocation(CFURLRef anURL);
207
208/* NULL if CFURLCanBeDecomposed(anURL) is false; also does not resolve the URL */
209/* against its base. See also CFURLCopyAbsoluteURL(). Note that, strictly */
210/* speaking, any leading '/' is not considered part of the URL's path, although */
211/* its presence or absence determines whether the path is absolute. */
212/* CFURLCopyPath()'s return value includes any leading slash (giving the path */
213/* the normal POSIX appearance); CFURLCopyStrictPath()'s return value omits any */
214/* leading slash, and uses isAbsolute to report whether the URL's path is absolute. */
215
216/* CFURLCopyFileSystemPath() returns the URL's path as a file system path for the */
217/* given path style. All percent escape sequences are replaced. The URL is not */
218/* resolved against its base before computing the path. */
219CF_EXPORT
220CFStringRef CFURLCopyPath(CFURLRef anURL);
221
222CF_EXPORT
223CFStringRef CFURLCopyStrictPath(CFURLRef anURL, Boolean *isAbsolute);
224
225CF_EXPORT
226CFStringRef CFURLCopyFileSystemPath(CFURLRef anURL, CFURLPathStyle pathStyle);
227
228/* Returns whether anURL's path represents a directory */
229/* (true returned) or a simple file (false returned) */
230CF_EXPORT
231Boolean CFURLHasDirectoryPath(CFURLRef anURL);
232
233/* Any additional resource specifiers after the path. For URLs */
234/* that cannot be decomposed, this is everything except the scheme itself. */
235CF_EXPORT
236CFStringRef CFURLCopyResourceSpecifier(CFURLRef anURL);
237
238CF_EXPORT
239CFStringRef CFURLCopyHostName(CFURLRef anURL);
240
241CF_EXPORT
242SInt32 CFURLGetPortNumber(CFURLRef anURL); /* Returns -1 if no port number is specified */
243
244CF_EXPORT
245CFStringRef CFURLCopyUserName(CFURLRef anURL);
246
247CF_EXPORT
248CFStringRef CFURLCopyPassword(CFURLRef anURL);
249
250/* These remove all percent escape sequences except those for */
251/* characters in charactersToLeaveEscaped. If charactersToLeaveEscaped */
252/* is empty (""), all percent escape sequences are replaced by their */
253/* corresponding characters. If charactersToLeaveEscaped is NULL, */
254/* then no escape sequences are removed at all */
255CF_EXPORT
256CFStringRef CFURLCopyParameterString(CFURLRef anURL, CFStringRef charactersToLeaveEscaped);
257
258CF_EXPORT
259CFStringRef CFURLCopyQueryString(CFURLRef anURL, CFStringRef charactersToLeaveEscaped);
260
261CF_EXPORT
262CFStringRef CFURLCopyFragment(CFURLRef anURL, CFStringRef charactersToLeaveEscaped);
263
264CF_EXPORT
265CFStringRef CFURLCopyLastPathComponent(CFURLRef url);
266
267CF_EXPORT
268CFStringRef CFURLCopyPathExtension(CFURLRef url);
269
270/* These functions all treat the base URL of the supplied url as */
271/* invariant. In other words, the URL returned will always have */
272/* the same base as the URL supplied as an argument. */
273
274CF_EXPORT
275CFURLRef CFURLCreateCopyAppendingPathComponent(CFAllocatorRef allocator, CFURLRef url, CFStringRef pathComponent, Boolean isDirectory);
276
277CF_EXPORT
278CFURLRef CFURLCreateCopyDeletingLastPathComponent(CFAllocatorRef allocator, CFURLRef url);
279
280CF_EXPORT
281CFURLRef CFURLCreateCopyAppendingPathExtension(CFAllocatorRef allocator, CFURLRef url, CFStringRef extension);
282
283CF_EXPORT
284CFURLRef CFURLCreateCopyDeletingPathExtension(CFAllocatorRef allocator, CFURLRef url);
285
286#if MAC_OS_X_VERSION_10_3 <= MAC_OS_X_VERSION_MAX_ALLOWED
287/* Fills buffer with the bytes for url, returning the number of bytes */
288/* filled. If buffer is of insufficient size, returns -1 and no bytes */
289/* are placed in buffer. If buffer is NULL, the needed length is */
290/* computed and returned. The returned bytes are the original bytes */
291/* from which the URL was created; if the URL was created from a */
292/* string, the bytes will be the bytes of the string encoded via UTF-8 */
d8925383 293CF_EXPORT
9ce05555
A
294CFIndex CFURLGetBytes(CFURLRef url, UInt8 *buffer, CFIndex bufferLength) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
295
296typedef enum {
297 kCFURLComponentScheme = 1,
298 kCFURLComponentNetLocation = 2,
299 kCFURLComponentPath = 3,
300 kCFURLComponentResourceSpecifier = 4,
301
302 kCFURLComponentUser = 5,
303 kCFURLComponentPassword = 6,
304 kCFURLComponentUserInfo = 7,
305 kCFURLComponentHost = 8,
306 kCFURLComponentPort = 9,
307 kCFURLComponentParameterString = 10,
308 kCFURLComponentQuery = 11,
309 kCFURLComponentFragment = 12
310} CFURLComponentType;
311
312/*
313Gets the range of the requested component in the bytes of url, as
314returned by CFURLGetBytes(). This range is only good for use in the
315bytes returned by CFURLGetBytes!
316
317If non-NULL, rangeIncludingSeparators gives the range of component
318including the sequences that separate component from the previous and
319next components. If there is no previous or next component, that end of
320rangeIncludingSeparators will match the range of the component itself.
321If url does not contain the given component type, (kCFNotFound, 0) is
322returned, and rangeIncludingSeparators is set to the location where the
323component would be inserted. Some examples -
324
325For the URL http://www.apple.com/hotnews/
326
327Component returned range rangeIncludingSeparators
328scheme (0, 4) (0, 7)
329net location (7, 13) (4, 16)
330path (20, 9) (20, 9)
331resource specifier (kCFNotFound, 0) (29, 0)
332user (kCFNotFound, 0) (7, 0)
333password (kCFNotFound, 0) (7, 0)
334user info (kCFNotFound, 0) (7, 0)
335host (7, 13) (4, 16)
336port (kCFNotFound, 0) (20, 0)
337parameter (kCFNotFound, 0) (29, 0)
338query (kCFNotFound, 0) (29, 0)
339fragment (kCFNotFound, 0) (29, 0)
340
341
342For the URL ./relPath/file.html#fragment
343
344Component returned range rangeIncludingSeparators
345scheme (kCFNotFound, 0) (0, 0)
346net location (kCFNotFound, 0) (0, 0)
347path (0, 19) (0, 20)
348resource specifier (20, 8) (19, 9)
349user (kCFNotFound, 0) (0, 0)
350password (kCFNotFound, 0) (0, 0)
351user info (kCFNotFound, 0) (0, 0)
352host (kCFNotFound, 0) (0, 0)
353port (kCFNotFound, 0) (0, 0)
354parameter (kCFNotFound, 0) (19, 0)
355query (kCFNotFound, 0) (19, 0)
356fragment (20, 8) (19, 9)
357
358
359For the URL scheme://user:pass@host:1/path/path2/file.html;params?query#fragment
360
361Component returned range rangeIncludingSeparators
362scheme (0, 6) (0, 9)
363net location (9, 16) (6, 19)
364path (25, 21) (25, 22)
365resource specifier (47, 21) (46, 22)
366user (9, 4) (6, 8)
367password (14, 4) (13, 6)
368user info (9, 9) (6, 13)
369host (19, 4) (18, 6)
370port (24, 1) (23, 2)
371parameter (47, 6) (46, 8)
372query (54, 5) (53, 7)
373fragment (60, 8) (59, 9)
374*/
d8925383 375CF_EXPORT
9ce05555
A
376CFRange CFURLGetByteRangeForComponent(CFURLRef url, CFURLComponentType component, CFRange *rangeIncludingSeparators) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
377#endif
378
379/* Returns a string with any percent escape sequences that do NOT */
380/* correspond to characters in charactersToLeaveEscaped with their */
381/* equivalent. Returns NULL on failure (if an invalid percent sequence */
382/* is encountered), or the original string (retained) if no characters */
383/* need to be replaced. Pass NULL to request that no percent escapes be */
384/* replaced, or the empty string (CFSTR("")) to request that all percent */
385/* escapes be replaced. Uses UTF8 to interpret percent escapes. */
386CF_EXPORT
387CFStringRef CFURLCreateStringByReplacingPercentEscapes(CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveEscaped);
388
389#if MAC_OS_X_VERSION_10_3 <= MAC_OS_X_VERSION_MAX_ALLOWED
390/* As above, but allows you to specify the encoding to use when interpreting percent escapes */
391CF_EXPORT
392CFStringRef CFURLCreateStringByReplacingPercentEscapesUsingEncoding(CFAllocatorRef allocator, CFStringRef origString, CFStringRef charsToLeaveEscaped, CFStringEncoding encoding) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
393#endif
394
395/* Creates a copy or originalString, replacing certain characters with */
396/* the equivalent percent escape sequence based on the encoding specified. */
397/* If the originalString does not need to be modified (no percent escape */
398/* sequences are missing), may retain and return originalString. */
399/* If you are uncertain of the correct encoding, you should use UTF-8, */
400/* which is the encoding designated by RFC 2396 as the correct encoding */
401/* for use in URLs. The characters so escaped are all characters that */
402/* are not legal URL characters (based on RFC 2396), plus any characters */
403/* in legalURLCharactersToBeEscaped, less any characters in */
404/* charactersToLeaveUnescaped. To simply correct any non-URL characters */
405/* in an otherwise correct URL string, do: */
406
407/* newString = CFURLCreateStringByAddingPercentEscapes(NULL, origString, NULL, NULL, kCFStringEncodingUTF8); */
408CF_EXPORT
409CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding encoding);
410
411
412#if defined(__cplusplus)
413}
414#endif
415
416#endif /* !__COREFOUNDATION_CFURL__ */
417