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