VirtualBox

source: vbox/trunk/include/iprt/uri.h@ 58068

Last change on this file since 58068 was 58068, checked in by vboxsync, 10 years ago

IPRT: Added RTUriFilePathEx, removed RTUriFileNPath as we ignored the cchMax param. Fixed special handling of 'file://localhost/C:/Windows/memory.dmp' style URLs as well as teating '|' as an alternative to the drive letter ':' character. Also extended testcases with windows legacy encodings and taught RTUriFilePathEx to handle them. (RTUriFilePath is calling RTUriFilePathEx of course.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/** @file
2 * IPRT - Uniform Resource Identifier handling.
3 */
4
5/*
6 * Copyright (C) 2011-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_uri_h
27#define ___iprt_uri_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31
32RT_C_DECLS_BEGIN
33
34/** @defgroup grp_rt_uri RTUri - Uri parsing and creation
35 *
36 * URI parsing and creation based on RFC-3986.
37 *
38 * @remarks The whole specification isn't implemented and we only provide scheme
39 * specific special APIs for "file://".
40 *
41 * @ingroup grp_rt
42 * @{
43 */
44
45
46/**
47 * Parsed URI.
48 *
49 * @remarks This structure is subject to change.
50 */
51typedef struct RTURIPARSED
52{
53 /** Magic value (for internal use only). */
54 uint32_t u32Magic;
55 /** RTURIPARSED_F_XXX. */
56 uint32_t fFlags;
57
58 /** The length of the scheme. */
59 size_t cchScheme;
60
61 /** The offset into the string of the authority. */
62 size_t offAuthority;
63 /** The authority length.
64 * @remarks The authority component can be zero length, so to check whether
65 * it's there or not consult RTURIPARSED_F_HAVE_AUTHORITY. */
66 size_t cchAuthority;
67
68 /** The offset into the string of the path. */
69 size_t offPath;
70 /** The length of the path. */
71 size_t cchPath;
72
73 /** The offset into the string of the query. */
74 size_t offQuery;
75 /** The length of the query. */
76 size_t cchQuery;
77
78 /** The offset into the string of the fragment. */
79 size_t offFragment;
80 /** The length of the fragment. */
81 size_t cchFragment;
82
83 /** @name Authority subdivisions
84 * @{ */
85 /** If there is a userinfo part, this is the start of it. Otherwise it's the
86 * same as offAuthorityHost. */
87 size_t offAuthorityUsername;
88 /** The length of the username (zero if not present). */
89 size_t cchAuthorityUsername;
90 /** If there is a userinfo part containing a password, this is the start of it.
91 * Otherwise it's the same as offAuthorityHost. */
92 size_t offAuthorityPassword;
93 /** The length of the password (zero if not present). */
94 size_t cchAuthorityPassword;
95 /** The offset of the host part of the authority. */
96 size_t offAuthorityHost;
97 /** The length of the host part of the authority. */
98 size_t cchAuthorityHost;
99 /** The authority port number, UINT32_MAX if not present. */
100 uint32_t uAuthorityPort;
101 /** @} */
102} RTURIPARSED;
103/** Pointer to a parsed URI. */
104typedef RTURIPARSED *PRTURIPARSED;
105/** Pointer to a const parsed URI. */
106typedef RTURIPARSED const *PCRTURIPARSED;
107
108/** @name RTURIPARSED_F_XXX - RTURIPARSED::fFlags
109 * @{ */
110/** Set if the URI contains escaped characters. */
111#define RTURIPARSED_F_CONTAINS_ESCAPED_CHARS UINT32_C(0x00000001)
112/** Set if the URI have an authority component. Necessary since the authority
113 * component can have a zero length. */
114#define RTURIPARSED_F_HAVE_AUTHORITY UINT32_C(0x00000002)
115/** @} */
116
117/**
118 * Parses a URI.
119 *
120 * @returns IPRT status code.
121 * @param pszUri The URI to parse.
122 * @param pParsed Where to return the details. This can be handed
123 * to the RTUriParsed* APIs for retriving
124 * information.
125 */
126RTDECL(int) RTUriParse(const char *pszUri, PRTURIPARSED pParsed);
127
128/**
129 * Extract the scheme out of a parsed URI.
130 *
131 * @returns the scheme if the URI is valid, NULL otherwise.
132 * @param pszUri The URI passed to RTUriParse when producing the
133 * info in @a pParsed.
134 * @param pParsed Pointer to the RTUriParse output.
135 */
136RTDECL(char *) RTUriParsedScheme(const char *pszUri, PCRTURIPARSED pParsed);
137
138/**
139 * Extract the authority out of a parsed URI.
140 *
141 * @returns the authority if the URI contains one, NULL otherwise.
142 * @param pszUri The URI passed to RTUriParse when producing the
143 * info in @a pParsed.
144 * @param pParsed Pointer to the RTUriParse output.
145 * @remarks The authority can have a zero length.
146 */
147RTDECL(char *) RTUriParsedAuthority(const char *pszUri, PCRTURIPARSED pParsed);
148
149/**
150 * Extract the username out of the authority component in a parsed URI.
151 *
152 * @returns The username if the URI contains one, otherwise NULL.
153 * @param pszUri The URI passed to RTUriParse when producing the
154 * info in @a pParsed.
155 * @param pParsed Pointer to the RTUriParse output.
156 *
157 * @todo This may currently be returning NULL when it maybe would be more
158 * appropriate to return an empty string...
159 */
160RTDECL(char *) RTUriParsedAuthorityUsername(const char *pszUri, PCRTURIPARSED pParsed);
161
162/**
163 * Extract the password out of the authority component in a parsed URI.
164 *
165 * @returns The password if the URI contains one, otherwise NULL.
166 * @param pszUri The URI passed to RTUriParse when producing the
167 * info in @a pParsed.
168 * @param pParsed Pointer to the RTUriParse output.
169 *
170 * @todo This may currently be returning NULL when it maybe would be more
171 * appropriate to return an empty string...
172 */
173RTDECL(char *) RTUriParsedAuthorityPassword(const char *pszUri, PCRTURIPARSED pParsed);
174
175/**
176 * Extract the host out of the authority component in a parsed URI.
177 *
178 * @returns The host if the URI contains one, otherwise NULL.
179 * @param pszUri The URI passed to RTUriParse when producing the
180 * info in @a pParsed.
181 * @param pParsed Pointer to the RTUriParse output.
182 *
183 * @todo This may currently be returning NULL when it maybe would be more
184 * appropriate to return an empty string...
185 */
186RTDECL(char *) RTUriParsedAuthorityHost(const char *pszUri, PCRTURIPARSED pParsed);
187
188/**
189 * Extract the port number out of the authority component in a parsed URI.
190 *
191 * @returns The port number if the URI contains one, otherwise UINT32_MAX.
192 * @param pszUri The URI passed to RTUriParse when producing the
193 * info in @a pParsed.
194 * @param pParsed Pointer to the RTUriParse output.
195 */
196RTDECL(uint32_t) RTUriParsedAuthorityPort(const char *pszUri, PCRTURIPARSED pParsed);
197
198/**
199 * Extract the path out of a parsed URI.
200 *
201 * @returns the path if the URI contains one, NULL otherwise.
202 * @param pszUri The URI passed to RTUriParse when producing the
203 * info in @a pParsed.
204 * @param pParsed Pointer to the RTUriParse output.
205 */
206RTDECL(char *) RTUriParsedPath(const char *pszUri, PCRTURIPARSED pParsed);
207
208/**
209 * Extract the query out of a parsed URI.
210 *
211 * @returns the query if the URI contains one, NULL otherwise.
212 * @param pszUri The URI passed to RTUriParse when producing the
213 * info in @a pParsed.
214 * @param pParsed Pointer to the RTUriParse output.
215 */
216RTDECL(char *) RTUriParsedQuery(const char *pszUri, PCRTURIPARSED pParsed);
217
218/**
219 * Extract the fragment out of a parsed URI.
220 *
221 * @returns the fragment if the URI contains one, NULL otherwise.
222 * @param pszUri The URI passed to RTUriParse when producing the
223 * info in @a pParsed.
224 * @param pParsed Pointer to the RTUriParse output.
225 */
226RTDECL(char *) RTUriParsedFragment(const char *pszUri, PCRTURIPARSED pParsed);
227
228
229
230/**
231 * Creates a generic URI.
232 *
233 * The returned pointer must be freed using RTStrFree().
234 *
235 * @returns the new URI on success, NULL otherwise.
236 * @param pszScheme The URI scheme.
237 * @param pszAuthority The authority part of the URI (optional).
238 * @param pszPath The path part of the URI (optional).
239 * @param pszQuery The query part of the URI (optional).
240 * @param pszFragment The fragment part of the URI (optional).
241 */
242RTDECL(char *) RTUriCreate(const char *pszScheme, const char *pszAuthority, const char *pszPath, const char *pszQuery,
243 const char *pszFragment);
244
245/**
246 * Check whether the given scheme matches that of the URI.
247 *
248 * This does not validate the URI, it just compares the scheme, no more, no
249 * less. Thus it's much faster than using RTUriParsedScheme.
250 *
251 * @returns true if the scheme match, false if not.
252 * @param pszUri The URI to check.
253 * @param pszScheme The scheme to compare with.
254 */
255RTDECL(bool) RTUriIsSchemeMatch(const char *pszUri, const char *pszScheme);
256
257/** @defgroup grp_rt_uri_file RTUriFile - Uri file parsing and creation
258 *
259 * Implements basic "file:" scheme support to the generic RTUri interface. This
260 * is partly documented in RFC-1738.
261 *
262 * @{
263 */
264
265/** Return the host format. */
266#define URI_FILE_FORMAT_AUTO UINT32_C(0)
267/** Return a path in UNIX format style. */
268#define URI_FILE_FORMAT_UNIX UINT32_C(1)
269/** Return a path in Windows format style. */
270#define URI_FILE_FORMAT_WIN UINT32_C(2)
271
272/**
273 * Creates a file URI.
274 *
275 * The returned pointer must be freed using RTStrFree().
276 *
277 * @returns The new URI on success, NULL otherwise. Free With RTStrFree.
278 * @param pszPath The path of the URI.
279 *
280 * @sa RTUriFileCreateEx, RTUriCreate
281 */
282RTDECL(char *) RTUriFileCreate(const char *pszPath);
283
284/**
285 * Creates an file URL for the given path.
286 *
287 * This API works like RTStrToUtf16Ex with regard to result allocation or
288 * buffering (i.e. it's a bit complicated but very flexible).
289 *
290 * @returns iprt status code.
291 * @param pszPath The path to convert to a file:// URL.
292 * @param fPathStyle The input path style, exactly one of
293 * RTPATH_STR_F_STYLE_HOST, RTPATH_STR_F_STYLE_DOS and
294 * RTPATH_STR_F_STYLE_UNIX. Must include iprt/path.h.
295 * @param ppszUri If cbUri is non-zero, this must either be pointing
296 * to pointer to a buffer of the specified size, or
297 * pointer to a NULL pointer. If *ppszUri is NULL or
298 * cbUri is zero a buffer of at least cbUri chars will
299 * be allocated to hold the URI. If a buffer was
300 * requested it must be freed using RTStrFree().
301 * @param cbUri The buffer size in bytes (includes terminator).
302 * @param pcchUri Where to store the length of the URI string,
303 * excluding the terminator. (Optional)
304 *
305 * This may be set under some error conditions,
306 * however, only for VERR_BUFFER_OVERFLOW and
307 * VERR_NO_STR_MEMORY will it contain a valid string
308 * length that can be used to resize the buffer.
309 * @sa RTUriCreate, RTUriFileCreate
310 */
311RTDECL(int) RTUriFileCreateEx(const char *pszPath, uint32_t fPathStyle, char **ppszUri, size_t cbUri, size_t *pcchUri);
312
313/**
314 * Returns the file path encoded in the file URI.
315 *
316 * @returns the path if the URI contains one, NULL otherwise.
317 * @param pszUri The URI to extract from.
318 * @param uFormat In which format should the path returned.
319 */
320RTDECL(char *) RTUriFilePath(const char *pszUri, uint32_t uFormat);
321
322/**
323 * Queries the file path for the given file URI.
324 *
325 * This API works like RTStrToUtf16Ex with regard to result allocation or
326 * buffering (i.e. it's a bit complicated but very flexible).
327 *
328 * This differs a quite a bit from RTUriParsedPath in that it tries to be
329 * compatible with URL produced by older windows version. This API is basically
330 * producing the same results as the PathCreateFromUrl API on Windows.
331 *
332 * @returns IPRT status code.
333 * @retval VERR_URI_NOT_FILE_SCHEME if not file scheme.
334 *
335 * @param pszUri The alleged file:// URI.
336 * @param fPathStyle The output path style, exactly one of
337 * RTPATH_STR_F_STYLE_HOST, RTPATH_STR_F_STYLE_DOS and
338 * RTPATH_STR_F_STYLE_UNIX. Must include iprt/path.h.
339 * @param ppszPath If cbPath is non-zero, this must either be pointing
340 * to pointer to a buffer of the specified size, or
341 * pointer to a NULL pointer. If *ppszPath is NULL or
342 * cbPath is zero a buffer of at least cbPath chars
343 * will be allocated to hold the path. If a buffer was
344 * requested it must be freed using RTStrFree().
345 * @param cbPath The buffer size in bytes (includes terminator).
346 * @param pcchPath Where to store the length of the path string,
347 * excluding the terminator. (Optional)
348 *
349 * This may be set under some error conditions,
350 * however, only for VERR_BUFFER_OVERFLOW and
351 * VERR_NO_STR_MEMORY will it contain a valid string
352 * length that can be used to resize the buffer.
353 * @sa RTUriParsedPath, RTUriFilePath
354 */
355RTDECL(int) RTUriFilePathEx(const char *pszUri, uint32_t fPathStyle, char **ppszPath, size_t cbPath, size_t *pcchPath);
356
357/** @} */
358
359/** @} */
360
361RT_C_DECLS_END
362
363#endif
364
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette