VirtualBox

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

Last change on this file since 76421 was 74424, checked in by vboxsync, 6 years ago

IPRT/uri: Better handling of empty port specifiers. bugref:9249

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 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 or empty. */
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 has an authority component. Necessary since the authority
113 * component can have a zero length. */
114#define RTURIPARSED_F_HAS_AUTHORITY UINT32_C(0x00000002)
115/** Set if there is a port component. */
116#define RTURIPARSED_F_HAS_PORT UINT32_C(0x00000004)
117/** @} */
118
119/**
120 * Parses a URI.
121 *
122 * @returns IPRT status code.
123 * @param pszUri The URI to parse.
124 * @param pParsed Where to return the details. This can be handed
125 * to the RTUriParsed* APIs for retriving
126 * information.
127 */
128RTDECL(int) RTUriParse(const char *pszUri, PRTURIPARSED pParsed);
129
130/**
131 * Extract the scheme out of a parsed URI.
132 *
133 * @returns the scheme if the URI is valid, NULL otherwise.
134 * @param pszUri The URI passed to RTUriParse when producing the
135 * info in @a pParsed.
136 * @param pParsed Pointer to the RTUriParse output.
137 */
138RTDECL(char *) RTUriParsedScheme(const char *pszUri, PCRTURIPARSED pParsed);
139
140/**
141 * Extract the authority out of a parsed URI.
142 *
143 * @returns the authority if the URI contains one, NULL otherwise.
144 * @param pszUri The URI passed to RTUriParse when producing the
145 * info in @a pParsed.
146 * @param pParsed Pointer to the RTUriParse output.
147 * @remarks The authority can have a zero length.
148 */
149RTDECL(char *) RTUriParsedAuthority(const char *pszUri, PCRTURIPARSED pParsed);
150
151/**
152 * Extract the username out of the authority component in a parsed URI.
153 *
154 * @returns The username if the URI contains one, otherwise NULL.
155 * @param pszUri The URI passed to RTUriParse when producing the
156 * info in @a pParsed.
157 * @param pParsed Pointer to the RTUriParse output.
158 *
159 * @todo This may currently be returning NULL when it maybe would be more
160 * appropriate to return an empty string...
161 */
162RTDECL(char *) RTUriParsedAuthorityUsername(const char *pszUri, PCRTURIPARSED pParsed);
163
164/**
165 * Extract the password out of the authority component in a parsed URI.
166 *
167 * @returns The password if the URI contains one, otherwise NULL.
168 * @param pszUri The URI passed to RTUriParse when producing the
169 * info in @a pParsed.
170 * @param pParsed Pointer to the RTUriParse output.
171 *
172 * @todo This may currently be returning NULL when it maybe would be more
173 * appropriate to return an empty string...
174 */
175RTDECL(char *) RTUriParsedAuthorityPassword(const char *pszUri, PCRTURIPARSED pParsed);
176
177/**
178 * Extract the host out of the authority component in a parsed URI.
179 *
180 * @returns The host if the URI contains one, otherwise NULL.
181 * @param pszUri The URI passed to RTUriParse when producing the
182 * info in @a pParsed.
183 * @param pParsed Pointer to the RTUriParse output.
184 *
185 * @todo This may currently be returning NULL when it maybe would be more
186 * appropriate to return an empty string...
187 */
188RTDECL(char *) RTUriParsedAuthorityHost(const char *pszUri, PCRTURIPARSED pParsed);
189
190/**
191 * Extract the port number out of the authority component in a parsed URI.
192 *
193 * @returns The port number if the URI contains one, otherwise UINT32_MAX.
194 * @param pszUri The URI passed to RTUriParse when producing the
195 * info in @a pParsed.
196 * @param pParsed Pointer to the RTUriParse output.
197 */
198RTDECL(uint32_t) RTUriParsedAuthorityPort(const char *pszUri, PCRTURIPARSED pParsed);
199
200/**
201 * Extract the path out of a parsed URI.
202 *
203 * @returns the path if the URI contains one, NULL otherwise.
204 * @param pszUri The URI passed to RTUriParse when producing the
205 * info in @a pParsed.
206 * @param pParsed Pointer to the RTUriParse output.
207 */
208RTDECL(char *) RTUriParsedPath(const char *pszUri, PCRTURIPARSED pParsed);
209
210/**
211 * Extract the query out of a parsed URI.
212 *
213 * @returns the query if the URI contains one, NULL otherwise.
214 * @param pszUri The URI passed to RTUriParse when producing the
215 * info in @a pParsed.
216 * @param pParsed Pointer to the RTUriParse output.
217 */
218RTDECL(char *) RTUriParsedQuery(const char *pszUri, PCRTURIPARSED pParsed);
219
220/**
221 * Extract the fragment out of a parsed URI.
222 *
223 * @returns the fragment if the URI contains one, NULL otherwise.
224 * @param pszUri The URI passed to RTUriParse when producing the
225 * info in @a pParsed.
226 * @param pParsed Pointer to the RTUriParse output.
227 */
228RTDECL(char *) RTUriParsedFragment(const char *pszUri, PCRTURIPARSED pParsed);
229
230
231
232/**
233 * Creates a generic URI.
234 *
235 * The returned pointer must be freed using RTStrFree().
236 *
237 * @returns the new URI on success, NULL otherwise.
238 * @param pszScheme The URI scheme.
239 * @param pszAuthority The authority part of the URI (optional).
240 * @param pszPath The path part of the URI (optional).
241 * @param pszQuery The query part of the URI (optional).
242 * @param pszFragment The fragment part of the URI (optional).
243 */
244RTDECL(char *) RTUriCreate(const char *pszScheme, const char *pszAuthority, const char *pszPath, const char *pszQuery,
245 const char *pszFragment);
246
247/**
248 * Check whether the given scheme matches that of the URI.
249 *
250 * This does not validate the URI, it just compares the scheme, no more, no
251 * less. Thus it's much faster than using RTUriParsedScheme.
252 *
253 * @returns true if the scheme match, false if not.
254 * @param pszUri The URI to check.
255 * @param pszScheme The scheme to compare with.
256 */
257RTDECL(bool) RTUriIsSchemeMatch(const char *pszUri, const char *pszScheme);
258
259/** @defgroup grp_rt_uri_file RTUriFile - Uri file parsing and creation
260 *
261 * Implements basic "file:" scheme support to the generic RTUri interface. This
262 * is partly documented in RFC-1738.
263 *
264 * @{
265 */
266
267/**
268 * Creates a file URI.
269 *
270 * The returned pointer must be freed using RTStrFree().
271 *
272 * @returns The new URI on success, NULL otherwise. Free With RTStrFree.
273 * @param pszPath The path to create an 'file://' URI for. This is
274 * assumed to be using the default path style of the
275 * system.
276 *
277 * @sa RTUriFileCreateEx, RTUriCreate
278 */
279RTDECL(char *) RTUriFileCreate(const char *pszPath);
280
281/**
282 * Creates an file URL for the given path.
283 *
284 * This API works like RTStrToUtf16Ex with regard to result allocation or
285 * buffering (i.e. it's a bit complicated but very flexible).
286 *
287 * @returns iprt status code.
288 * @param pszPath The path to convert to a file:// URL.
289 * @param fPathStyle The input path style, exactly one of
290 * RTPATH_STR_F_STYLE_HOST, RTPATH_STR_F_STYLE_DOS and
291 * RTPATH_STR_F_STYLE_UNIX. Must include iprt/path.h.
292 * @param ppszUri If cbUri is non-zero, this must either be pointing
293 * to pointer to a buffer of the specified size, or
294 * pointer to a NULL pointer. If *ppszUri is NULL or
295 * cbUri is zero a buffer of at least cbUri chars will
296 * be allocated to hold the URI. If a buffer was
297 * requested it must be freed using RTStrFree().
298 * @param cbUri The buffer size in bytes (includes terminator).
299 * @param pcchUri Where to store the length of the URI string,
300 * excluding the terminator. (Optional)
301 *
302 * This may be set under some error conditions,
303 * however, only for VERR_BUFFER_OVERFLOW and
304 * VERR_NO_STR_MEMORY will it contain a valid string
305 * length that can be used to resize the buffer.
306 * @sa RTUriCreate, RTUriFileCreate
307 */
308RTDECL(int) RTUriFileCreateEx(const char *pszPath, uint32_t fPathStyle, char **ppszUri, size_t cbUri, size_t *pcchUri);
309
310/**
311 * Returns the file path encoded in the file URI.
312 *
313 * This differs a quite a bit from RTUriParsedPath in that it tries to be
314 * compatible with URL produced by older windows version. This API is basically
315 * producing the same results as the PathCreateFromUrl API on Windows.
316 *
317 * @returns The path if the URI contains one, system default path style,
318 * otherwise NULL.
319 * @param pszUri The alleged 'file://' URI to extract the path from.
320 *
321 * @sa RTUriParsedPath, RTUriFilePathEx
322 */
323RTDECL(char *) RTUriFilePath(const char *pszUri);
324
325/**
326 * Queries the file path for the given file URI.
327 *
328 * This API works like RTStrToUtf16Ex with regard to result allocation or
329 * buffering (i.e. it's a bit complicated but very flexible).
330 *
331 * This differs a quite a bit from RTUriParsedPath in that it tries to be
332 * compatible with URL produced by older windows version. This API is basically
333 * producing the same results as the PathCreateFromUrl API on Windows.
334 *
335 * @returns IPRT status code.
336 * @retval VERR_URI_NOT_FILE_SCHEME if not file scheme.
337 *
338 * @param pszUri The alleged file:// URI to extract the path from.
339 * @param fPathStyle The output path style, exactly one of
340 * RTPATH_STR_F_STYLE_HOST, RTPATH_STR_F_STYLE_DOS and
341 * RTPATH_STR_F_STYLE_UNIX. Must include iprt/path.h.
342 * @param ppszPath If cbPath is non-zero, this must either be pointing
343 * to pointer to a buffer of the specified size, or
344 * pointer to a NULL pointer. If *ppszPath is NULL or
345 * cbPath is zero a buffer of at least cbPath chars
346 * will be allocated to hold the path. If a buffer was
347 * requested it must be freed using RTStrFree().
348 * @param cbPath The buffer size in bytes (includes terminator).
349 * @param pcchPath Where to store the length of the path string,
350 * excluding the terminator. (Optional)
351 *
352 * This may be set under some error conditions,
353 * however, only for VERR_BUFFER_OVERFLOW and
354 * VERR_NO_STR_MEMORY will it contain a valid string
355 * length that can be used to resize the buffer.
356 * @sa RTUriParsedPath, RTUriFilePath
357 */
358RTDECL(int) RTUriFilePathEx(const char *pszUri, uint32_t fPathStyle, char **ppszPath, size_t cbPath, size_t *pcchPath);
359
360/** @} */
361
362/** @} */
363
364RT_C_DECLS_END
365
366#endif
367
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