2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
27 #if !defined(__COREFOUNDATION_CFURL__)
28 #define __COREFOUNDATION_CFURL__ 1
30 #include <CoreFoundation/CFBase.h>
31 #include <CoreFoundation/CFData.h>
32 #include <CoreFoundation/CFString.h>
34 #if defined(__cplusplus)
39 kCFURLPOSIXPathStyle
= 0,
41 kCFURLWindowsPathStyle
44 typedef const struct __CFURL
* CFURLRef
;
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. */
61 CFTypeID
CFURLGetTypeID(void);
63 /* encoding will be used both to interpret the bytes of URLBytes, and to */
64 /* interpret any percent-escapes within the bytes. */
66 CFURLRef
CFURLCreateWithBytes(CFAllocatorRef allocator
, const UInt8
*URLBytes
, CFIndex length
, CFStringEncoding encoding
, CFURLRef baseURL
);
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) */
73 CFDataRef
CFURLCreateData(CFAllocatorRef allocator
, CFURLRef url
, CFStringEncoding encoding
, Boolean escapeWhitespace
);
75 /* Any escape sequences in URLString will be interpreted via UTF-8. */
77 CFURLRef
CFURLCreateWithString(CFAllocatorRef allocator
, CFStringRef URLString
, CFURLRef baseURL
);
79 #if MAC_OS_X_VERSION_10_3 <= MAC_OS_X_VERSION_MAX_ALLOWED
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 */
93 CFURLRef
CFURLCreateAbsoluteURLWithBytes(CFAllocatorRef alloc
, const UInt8
*relativeURLBytes
, CFIndex length
, CFStringEncoding encoding
, CFURLRef baseURL
, Boolean useCompatibilityMode
) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER
;
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 */
103 CFURLRef
CFURLCreateWithFileSystemPath(CFAllocatorRef allocator
, CFStringRef filePath
, CFURLPathStyle pathStyle
, Boolean isDirectory
);
106 CFURLRef
CFURLCreateFromFileSystemRepresentation(CFAllocatorRef allocator
, const UInt8
*buffer
, CFIndex bufLen
, Boolean isDirectory
);
109 CFURLRef
CFURLCreateWithFileSystemPathRelativeToBase(CFAllocatorRef allocator
, CFStringRef filePath
, CFURLPathStyle pathStyle
, Boolean isDirectory
, CFURLRef baseURL
);
112 CFURLRef
CFURLCreateFromFileSystemRepresentationRelativeToBase(CFAllocatorRef allocator
, const UInt8
*buffer
, CFIndex bufLen
, Boolean isDirectory
, CFURLRef baseURL
);
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. */
122 Boolean
CFURLGetFileSystemRepresentation(CFURLRef url
, Boolean resolveAgainstBase
, UInt8
*buffer
, CFIndex maxBufLen
);
124 /* Creates a new URL by resolving the relative portion of relativeURL against its base. */
126 CFURLRef
CFURLCopyAbsoluteURL(CFURLRef relativeURL
);
128 /* Returns the URL's string. */
130 CFStringRef
CFURLGetString(CFURLRef anURL
);
132 /* Returns the base URL if it exists */
134 CFURLRef
CFURLGetBaseURL(CFURLRef anURL
);
137 All URLs can be broken into two pieces - the scheme (preceding the
138 first colon) and the resource specifier (following the first colon).
139 Most URLs are also "standard" URLs conforming to RFC 1808 (available
140 from www.w3c.org). This category includes URLs of the file, http,
141 https, and ftp schemes, to name a few. Standard URLs start the
142 resource specifier with two slashes ("//"), and can be broken into
143 four distinct pieces - the scheme, the net location, the path, and
144 further resource specifiers (typically an optional parameter, query,
145 and/or fragment). The net location appears immediately following
146 the two slashes and goes up to the next slash; it's format is
147 scheme-specific, but is usually composed of some or all of a username,
148 password, host name, and port. The path is a series of path components
149 separated by slashes; if the net location is present, the path always
150 begins with a slash. Standard URLs can be relative to another URL,
151 in which case at least the scheme and possibly other pieces as well
152 come from the base URL (see RFC 1808 for precise details when resolving
153 a relative URL against its base). The full URL is therefore
155 <scheme> "://" <net location> <path, always starting with slash> <add'l resource specifiers>
157 If a given CFURL can be decomposed (that is, conforms to RFC 1808), you
158 can ask for each of the four basic pieces (scheme, net location, path,
159 and resource specifer) separately, as well as for its base URL. The
160 basic pieces are returned with any percent escape sequences still in
161 place (although note that the scheme may not legally include any
162 percent escapes); this is to allow the caller to distinguish between
163 percent sequences that may have syntactic meaning if replaced by the
164 character being escaped (for instance, a '/' in a path component).
165 Since only the individual schemes know which characters are
166 syntactically significant, CFURL cannot safely replace any percent
167 escape sequences. However, you can use
168 CFURLCreateStringByReplacingPercentEscapes() to create a new string with
169 the percent escapes removed; see below.
171 If a given CFURL can not be decomposed, you can ask for its scheme and its
172 resource specifier; asking it for its net location or path will return NULL.
174 To get more refined information about the components of a decomposable
175 CFURL, you may ask for more specific pieces of the URL, expressed with
176 the percent escapes removed. The available functions are CFURLCopyHostName(),
177 CFURLGetPortNumber() (returns an Int32), CFURLCopyUserName(),
178 CFURLCopyPassword(), CFURLCopyQuery(), CFURLCopyParameters(), and
179 CFURLCopyFragment(). Because the parameters, query, and fragment of an
180 URL may contain scheme-specific syntaxes, these methods take a second
181 argument, giving a list of characters which should NOT be replaced if
182 percent escaped. For instance, the ftp parameter syntax gives simple
183 key-value pairs as "<key>=<value>;" Clearly if a key or value includes
184 either '=' or ';', it must be escaped to avoid corrupting the meaning of
185 the parameters, so the caller may request the parameter string as
187 CFStringRef myParams = CFURLCopyParameters(ftpURL, CFSTR("=;%"));
189 requesting that all percent escape sequences be replaced by the represented
190 characters, except for escaped '=', '%' or ';' characters. Pass the empty
191 string (CFSTR("")) to request that all percent escapes be replaced, or NULL
192 to request that none be.
195 /* Returns true if anURL conforms to RFC 1808 */
197 Boolean
CFURLCanBeDecomposed(CFURLRef anURL
);
199 /* The next several methods leave any percent escape sequences intact */
202 CFStringRef
CFURLCopyScheme(CFURLRef anURL
);
204 /* NULL if CFURLCanBeDecomposed(anURL) is false */
206 CFStringRef
CFURLCopyNetLocation(CFURLRef anURL
);
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. */
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. */
220 CFStringRef
CFURLCopyPath(CFURLRef anURL
);
223 CFStringRef
CFURLCopyStrictPath(CFURLRef anURL
, Boolean
*isAbsolute
);
226 CFStringRef
CFURLCopyFileSystemPath(CFURLRef anURL
, CFURLPathStyle pathStyle
);
228 /* Returns whether anURL's path represents a directory */
229 /* (true returned) or a simple file (false returned) */
231 Boolean
CFURLHasDirectoryPath(CFURLRef anURL
);
233 /* Any additional resource specifiers after the path. For URLs */
234 /* that cannot be decomposed, this is everything except the scheme itself. */
236 CFStringRef
CFURLCopyResourceSpecifier(CFURLRef anURL
);
239 CFStringRef
CFURLCopyHostName(CFURLRef anURL
);
242 SInt32
CFURLGetPortNumber(CFURLRef anURL
); /* Returns -1 if no port number is specified */
245 CFStringRef
CFURLCopyUserName(CFURLRef anURL
);
248 CFStringRef
CFURLCopyPassword(CFURLRef anURL
);
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 */
256 CFStringRef
CFURLCopyParameterString(CFURLRef anURL
, CFStringRef charactersToLeaveEscaped
);
259 CFStringRef
CFURLCopyQueryString(CFURLRef anURL
, CFStringRef charactersToLeaveEscaped
);
262 CFStringRef
CFURLCopyFragment(CFURLRef anURL
, CFStringRef charactersToLeaveEscaped
);
265 CFStringRef
CFURLCopyLastPathComponent(CFURLRef url
);
268 CFStringRef
CFURLCopyPathExtension(CFURLRef url
);
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. */
275 CFURLRef
CFURLCreateCopyAppendingPathComponent(CFAllocatorRef allocator
, CFURLRef url
, CFStringRef pathComponent
, Boolean isDirectory
);
278 CFURLRef
CFURLCreateCopyDeletingLastPathComponent(CFAllocatorRef allocator
, CFURLRef url
);
281 CFURLRef
CFURLCreateCopyAppendingPathExtension(CFAllocatorRef allocator
, CFURLRef url
, CFStringRef extension
);
284 CFURLRef
CFURLCreateCopyDeletingPathExtension(CFAllocatorRef allocator
, CFURLRef url
);
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 */
294 CFIndex
CFURLGetBytes(CFURLRef url
, UInt8
*buffer
, CFIndex bufferLength
) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER
;
297 kCFURLComponentScheme
= 1,
298 kCFURLComponentNetLocation
= 2,
299 kCFURLComponentPath
= 3,
300 kCFURLComponentResourceSpecifier
= 4,
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
;
313 Gets the range of the requested component in the bytes of url, as
314 returned by CFURLGetBytes(). This range is only good for use in the
315 bytes returned by CFURLGetBytes!
317 If non-NULL, rangeIncludingSeparators gives the range of component
318 including the sequences that separate component from the previous and
319 next components. If there is no previous or next component, that end of
320 rangeIncludingSeparators will match the range of the component itself.
321 If url does not contain the given component type, (kCFNotFound, 0) is
322 returned, and rangeIncludingSeparators is set to the location where the
323 component would be inserted. Some examples -
325 For the URL http://www.apple.com/hotnews/
327 Component returned range rangeIncludingSeparators
329 net location (7, 13) (4, 16)
331 resource specifier (kCFNotFound, 0) (29, 0)
332 user (kCFNotFound, 0) (7, 0)
333 password (kCFNotFound, 0) (7, 0)
334 user info (kCFNotFound, 0) (7, 0)
336 port (kCFNotFound, 0) (20, 0)
337 parameter (kCFNotFound, 0) (29, 0)
338 query (kCFNotFound, 0) (29, 0)
339 fragment (kCFNotFound, 0) (29, 0)
342 For the URL ./relPath/file.html#fragment
344 Component returned range rangeIncludingSeparators
345 scheme (kCFNotFound, 0) (0, 0)
346 net location (kCFNotFound, 0) (0, 0)
348 resource specifier (20, 8) (19, 9)
349 user (kCFNotFound, 0) (0, 0)
350 password (kCFNotFound, 0) (0, 0)
351 user info (kCFNotFound, 0) (0, 0)
352 host (kCFNotFound, 0) (0, 0)
353 port (kCFNotFound, 0) (0, 0)
354 parameter (kCFNotFound, 0) (19, 0)
355 query (kCFNotFound, 0) (19, 0)
356 fragment (20, 8) (19, 9)
359 For the URL scheme://user:pass@host:1/path/path2/file.html;params?query#fragment
361 Component returned range rangeIncludingSeparators
363 net location (9, 16) (6, 19)
364 path (25, 21) (25, 22)
365 resource specifier (47, 21) (46, 22)
367 password (14, 4) (13, 6)
368 user info (9, 9) (6, 13)
371 parameter (47, 6) (46, 8)
372 query (54, 5) (53, 7)
373 fragment (60, 8) (59, 9)
376 CFRange
CFURLGetByteRangeForComponent(CFURLRef url
, CFURLComponentType component
, CFRange
*rangeIncludingSeparators
) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER
;
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. */
387 CFStringRef
CFURLCreateStringByReplacingPercentEscapes(CFAllocatorRef allocator
, CFStringRef originalString
, CFStringRef charactersToLeaveEscaped
);
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 */
392 CFStringRef
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(CFAllocatorRef allocator
, CFStringRef origString
, CFStringRef charsToLeaveEscaped
, CFStringEncoding encoding
) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER
;
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: */
407 /* newString = CFURLCreateStringByAddingPercentEscapes(NULL, origString, NULL, NULL, kCFStringEncodingUTF8); */
409 CFStringRef
CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator
, CFStringRef originalString
, CFStringRef charactersToLeaveUnescaped
, CFStringRef legalURLCharactersToBeEscaped
, CFStringEncoding encoding
);
412 #if defined(__cplusplus)
416 #endif /* !__COREFOUNDATION_CFURL__ */