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