VirtualBox

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

Last change on this file since 69840 was 69105, checked in by vboxsync, 7 years ago

include/iprt/: (C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/** @file
2 * IPRT - Uniform Resource Identifier handling.
3 */
4
5/*
6 * Copyright (C) 2011-2017 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/**
266 * Creates a file URI.
267 *
268 * The returned pointer must be freed using RTStrFree().
269 *
270 * @returns The new URI on success, NULL otherwise. Free With RTStrFree.
271 * @param pszPath The path to create an 'file://' URI for. This is
272 * assumed to be using the default path style of the
273 * system.
274 *
275 * @sa RTUriFileCreateEx, RTUriCreate
276 */
277RTDECL(char *) RTUriFileCreate(const char *pszPath);
278
279/**
280 * Creates an file URL for the given path.
281 *
282 * This API works like RTStrToUtf16Ex with regard to result allocation or
283 * buffering (i.e. it's a bit complicated but very flexible).
284 *
285 * @returns iprt status code.
286 * @param pszPath The path to convert to a file:// URL.
287 * @param fPathStyle The input path style, exactly one of
288 * RTPATH_STR_F_STYLE_HOST, RTPATH_STR_F_STYLE_DOS and
289 * RTPATH_STR_F_STYLE_UNIX. Must include iprt/path.h.
290 * @param ppszUri If cbUri is non-zero, this must either be pointing
291 * to pointer to a buffer of the specified size, or
292 * pointer to a NULL pointer. If *ppszUri is NULL or
293 * cbUri is zero a buffer of at least cbUri chars will
294 * be allocated to hold the URI. If a buffer was
295 * requested it must be freed using RTStrFree().
296 * @param cbUri The buffer size in bytes (includes terminator).
297 * @param pcchUri Where to store the length of the URI string,
298 * excluding the terminator. (Optional)
299 *
300 * This may be set under some error conditions,
301 * however, only for VERR_BUFFER_OVERFLOW and
302 * VERR_NO_STR_MEMORY will it contain a valid string
303 * length that can be used to resize the buffer.
304 * @sa RTUriCreate, RTUriFileCreate
305 */
306RTDECL(int) RTUriFileCreateEx(const char *pszPath, uint32_t fPathStyle, char **ppszUri, size_t cbUri, size_t *pcchUri);
307
308/**
309 * Returns the file path encoded in the file URI.
310 *
311 * This differs a quite a bit from RTUriParsedPath in that it tries to be
312 * compatible with URL produced by older windows version. This API is basically
313 * producing the same results as the PathCreateFromUrl API on Windows.
314 *
315 * @returns The path if the URI contains one, system default path style,
316 * otherwise NULL.
317 * @param pszUri The alleged 'file://' URI to extract the path from.
318 *
319 * @sa RTUriParsedPath, RTUriFilePathEx
320 */
321RTDECL(char *) RTUriFilePath(const char *pszUri);
322
323/**
324 * Queries the file path for the given file URI.
325 *
326 * This API works like RTStrToUtf16Ex with regard to result allocation or
327 * buffering (i.e. it's a bit complicated but very flexible).
328 *
329 * This differs a quite a bit from RTUriParsedPath in that it tries to be
330 * compatible with URL produced by older windows version. This API is basically
331 * producing the same results as the PathCreateFromUrl API on Windows.
332 *
333 * @returns IPRT status code.
334 * @retval VERR_URI_NOT_FILE_SCHEME if not file scheme.
335 *
336 * @param pszUri The alleged file:// URI to extract the path from.
337 * @param fPathStyle The output path style, exactly one of
338 * RTPATH_STR_F_STYLE_HOST, RTPATH_STR_F_STYLE_DOS and
339 * RTPATH_STR_F_STYLE_UNIX. Must include iprt/path.h.
340 * @param ppszPath If cbPath is non-zero, this must either be pointing
341 * to pointer to a buffer of the specified size, or
342 * pointer to a NULL pointer. If *ppszPath is NULL or
343 * cbPath is zero a buffer of at least cbPath chars
344 * will be allocated to hold the path. If a buffer was
345 * requested it must be freed using RTStrFree().
346 * @param cbPath The buffer size in bytes (includes terminator).
347 * @param pcchPath Where to store the length of the path string,
348 * excluding the terminator. (Optional)
349 *
350 * This may be set under some error conditions,
351 * however, only for VERR_BUFFER_OVERFLOW and
352 * VERR_NO_STR_MEMORY will it contain a valid string
353 * length that can be used to resize the buffer.
354 * @sa RTUriParsedPath, RTUriFilePath
355 */
356RTDECL(int) RTUriFilePathEx(const char *pszUri, uint32_t fPathStyle, char **ppszPath, size_t cbPath, size_t *pcchPath);
357
358/** @} */
359
360/** @} */
361
362RT_C_DECLS_END
363
364#endif
365
Note: See TracBrowser for help on using the repository browser.

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