guri.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright © 2020 Red Hat, Inc.
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General
  17. * Public License along with this library; if not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  22. #error "Only <glib.h> can be included directly."
  23. #endif
  24. #include <glib/gtypes.h>
  25. G_BEGIN_DECLS
  26. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  27. typedef struct _GUri GUri;
  28. GLIB_AVAILABLE_IN_2_66
  29. GUri * g_uri_ref (GUri *uri);
  30. GLIB_AVAILABLE_IN_2_66
  31. void g_uri_unref (GUri *uri);
  32. /**
  33. * GUriFlags:
  34. * @G_URI_FLAGS_NONE: No flags set.
  35. * @G_URI_FLAGS_PARSE_RELAXED: Parse the URI more relaxedly than the
  36. * [RFC 3986](https://tools.ietf.org/html/rfc3986) grammar specifies,
  37. * fixing up or ignoring common mistakes in URIs coming from external
  38. * sources. This is also needed for some obscure URI schemes where `;`
  39. * separates the host from the path. Don’t use this flag unless you need to.
  40. * @G_URI_FLAGS_HAS_PASSWORD: The userinfo field may contain a password,
  41. * which will be separated from the username by `:`.
  42. * @G_URI_FLAGS_HAS_AUTH_PARAMS: The userinfo may contain additional
  43. * authentication-related parameters, which will be separated from
  44. * the username and/or password by `;`.
  45. * @G_URI_FLAGS_NON_DNS: The host component should not be assumed to be a
  46. * DNS hostname or IP address (for example, for `smb` URIs with NetBIOS
  47. * hostnames).
  48. * @G_URI_FLAGS_ENCODED: When parsing a URI, this indicates that `%`-encoded
  49. * characters in the userinfo, path, query, and fragment fields
  50. * should not be decoded. (And likewise the host field if
  51. * %G_URI_FLAGS_NON_DNS is also set.) When building a URI, it indicates
  52. * that you have already `%`-encoded the components, and so #GUri
  53. * should not do any encoding itself.
  54. * @G_URI_FLAGS_ENCODED_QUERY: Same as %G_URI_FLAGS_ENCODED, for the query
  55. * field only.
  56. * @G_URI_FLAGS_ENCODED_PATH: Same as %G_URI_FLAGS_ENCODED, for the path only.
  57. * @G_URI_FLAGS_ENCODED_FRAGMENT: Same as %G_URI_FLAGS_ENCODED, for the
  58. * fragment only.
  59. * @G_URI_FLAGS_SCHEME_NORMALIZE: A scheme-based normalization will be applied.
  60. * For example, when parsing an HTTP URI changing omitted path to `/` and
  61. * omitted port to `80`; and when building a URI, changing empty path to `/`
  62. * and default port `80`). This only supports a subset of known schemes. (Since: 2.68)
  63. *
  64. * Flags that describe a URI.
  65. *
  66. * When parsing a URI, if you need to choose different flags based on
  67. * the type of URI, you can use g_uri_peek_scheme() on the URI string
  68. * to check the scheme first, and use that to decide what flags to
  69. * parse it with.
  70. *
  71. * Since: 2.66
  72. */
  73. GLIB_AVAILABLE_TYPE_IN_2_66
  74. typedef enum {
  75. G_URI_FLAGS_NONE = 0,
  76. G_URI_FLAGS_PARSE_RELAXED = 1 << 0,
  77. G_URI_FLAGS_HAS_PASSWORD = 1 << 1,
  78. G_URI_FLAGS_HAS_AUTH_PARAMS = 1 << 2,
  79. G_URI_FLAGS_ENCODED = 1 << 3,
  80. G_URI_FLAGS_NON_DNS = 1 << 4,
  81. G_URI_FLAGS_ENCODED_QUERY = 1 << 5,
  82. G_URI_FLAGS_ENCODED_PATH = 1 << 6,
  83. G_URI_FLAGS_ENCODED_FRAGMENT = 1 << 7,
  84. G_URI_FLAGS_SCHEME_NORMALIZE GLIB_AVAILABLE_ENUMERATOR_IN_2_68 = 1 << 8,
  85. } GUriFlags;
  86. GLIB_AVAILABLE_IN_2_66
  87. gboolean g_uri_split (const gchar *uri_ref,
  88. GUriFlags flags,
  89. gchar **scheme,
  90. gchar **userinfo,
  91. gchar **host,
  92. gint *port,
  93. gchar **path,
  94. gchar **query,
  95. gchar **fragment,
  96. GError **error);
  97. GLIB_AVAILABLE_IN_2_66
  98. gboolean g_uri_split_with_user (const gchar *uri_ref,
  99. GUriFlags flags,
  100. gchar **scheme,
  101. gchar **user,
  102. gchar **password,
  103. gchar **auth_params,
  104. gchar **host,
  105. gint *port,
  106. gchar **path,
  107. gchar **query,
  108. gchar **fragment,
  109. GError **error);
  110. GLIB_AVAILABLE_IN_2_66
  111. gboolean g_uri_split_network (const gchar *uri_string,
  112. GUriFlags flags,
  113. gchar **scheme,
  114. gchar **host,
  115. gint *port,
  116. GError **error);
  117. GLIB_AVAILABLE_IN_2_66
  118. gboolean g_uri_is_valid (const gchar *uri_string,
  119. GUriFlags flags,
  120. GError **error);
  121. GLIB_AVAILABLE_IN_2_66
  122. gchar * g_uri_join (GUriFlags flags,
  123. const gchar *scheme,
  124. const gchar *userinfo,
  125. const gchar *host,
  126. gint port,
  127. const gchar *path,
  128. const gchar *query,
  129. const gchar *fragment);
  130. GLIB_AVAILABLE_IN_2_66
  131. gchar * g_uri_join_with_user (GUriFlags flags,
  132. const gchar *scheme,
  133. const gchar *user,
  134. const gchar *password,
  135. const gchar *auth_params,
  136. const gchar *host,
  137. gint port,
  138. const gchar *path,
  139. const gchar *query,
  140. const gchar *fragment);
  141. GLIB_AVAILABLE_IN_2_66
  142. GUri * g_uri_parse (const gchar *uri_string,
  143. GUriFlags flags,
  144. GError **error);
  145. GLIB_AVAILABLE_IN_2_66
  146. GUri * g_uri_parse_relative (GUri *base_uri,
  147. const gchar *uri_ref,
  148. GUriFlags flags,
  149. GError **error);
  150. GLIB_AVAILABLE_IN_2_66
  151. gchar * g_uri_resolve_relative (const gchar *base_uri_string,
  152. const gchar *uri_ref,
  153. GUriFlags flags,
  154. GError **error);
  155. GLIB_AVAILABLE_IN_2_66
  156. GUri * g_uri_build (GUriFlags flags,
  157. const gchar *scheme,
  158. const gchar *userinfo,
  159. const gchar *host,
  160. gint port,
  161. const gchar *path,
  162. const gchar *query,
  163. const gchar *fragment);
  164. GLIB_AVAILABLE_IN_2_66
  165. GUri * g_uri_build_with_user (GUriFlags flags,
  166. const gchar *scheme,
  167. const gchar *user,
  168. const gchar *password,
  169. const gchar *auth_params,
  170. const gchar *host,
  171. gint port,
  172. const gchar *path,
  173. const gchar *query,
  174. const gchar *fragment);
  175. /**
  176. * GUriHideFlags:
  177. * @G_URI_HIDE_NONE: No flags set.
  178. * @G_URI_HIDE_USERINFO: Hide the userinfo.
  179. * @G_URI_HIDE_PASSWORD: Hide the password.
  180. * @G_URI_HIDE_AUTH_PARAMS: Hide the auth_params.
  181. * @G_URI_HIDE_QUERY: Hide the query.
  182. * @G_URI_HIDE_FRAGMENT: Hide the fragment.
  183. *
  184. * Flags describing what parts of the URI to hide in
  185. * g_uri_to_string_partial(). Note that %G_URI_HIDE_PASSWORD and
  186. * %G_URI_HIDE_AUTH_PARAMS will only work if the #GUri was parsed with
  187. * the corresponding flags.
  188. *
  189. * Since: 2.66
  190. */
  191. GLIB_AVAILABLE_TYPE_IN_2_66
  192. typedef enum {
  193. G_URI_HIDE_NONE = 0,
  194. G_URI_HIDE_USERINFO = 1 << 0,
  195. G_URI_HIDE_PASSWORD = 1 << 1,
  196. G_URI_HIDE_AUTH_PARAMS = 1 << 2,
  197. G_URI_HIDE_QUERY = 1 << 3,
  198. G_URI_HIDE_FRAGMENT = 1 << 4,
  199. } GUriHideFlags;
  200. GLIB_AVAILABLE_IN_2_66
  201. char * g_uri_to_string (GUri *uri);
  202. GLIB_AVAILABLE_IN_2_66
  203. char * g_uri_to_string_partial (GUri *uri,
  204. GUriHideFlags flags);
  205. GLIB_AVAILABLE_IN_2_66
  206. const gchar *g_uri_get_scheme (GUri *uri);
  207. GLIB_AVAILABLE_IN_2_66
  208. const gchar *g_uri_get_userinfo (GUri *uri);
  209. GLIB_AVAILABLE_IN_2_66
  210. const gchar *g_uri_get_user (GUri *uri);
  211. GLIB_AVAILABLE_IN_2_66
  212. const gchar *g_uri_get_password (GUri *uri);
  213. GLIB_AVAILABLE_IN_2_66
  214. const gchar *g_uri_get_auth_params (GUri *uri);
  215. GLIB_AVAILABLE_IN_2_66
  216. const gchar *g_uri_get_host (GUri *uri);
  217. GLIB_AVAILABLE_IN_2_66
  218. gint g_uri_get_port (GUri *uri);
  219. GLIB_AVAILABLE_IN_2_66
  220. const gchar *g_uri_get_path (GUri *uri);
  221. GLIB_AVAILABLE_IN_2_66
  222. const gchar *g_uri_get_query (GUri *uri);
  223. GLIB_AVAILABLE_IN_2_66
  224. const gchar *g_uri_get_fragment (GUri *uri);
  225. GLIB_AVAILABLE_IN_2_66
  226. GUriFlags g_uri_get_flags (GUri *uri);
  227. /**
  228. * GUriParamsFlags:
  229. * @G_URI_PARAMS_NONE: No flags set.
  230. * @G_URI_PARAMS_CASE_INSENSITIVE: Parameter names are case insensitive.
  231. * @G_URI_PARAMS_WWW_FORM: Replace `+` with space character. Only useful for
  232. * URLs on the web, using the `https` or `http` schemas.
  233. * @G_URI_PARAMS_PARSE_RELAXED: See %G_URI_FLAGS_PARSE_RELAXED.
  234. *
  235. * Flags modifying the way parameters are handled by g_uri_parse_params() and
  236. * #GUriParamsIter.
  237. *
  238. * Since: 2.66
  239. */
  240. GLIB_AVAILABLE_TYPE_IN_2_66
  241. typedef enum {
  242. G_URI_PARAMS_NONE = 0,
  243. G_URI_PARAMS_CASE_INSENSITIVE = 1 << 0,
  244. G_URI_PARAMS_WWW_FORM = 1 << 1,
  245. G_URI_PARAMS_PARSE_RELAXED = 1 << 2,
  246. } GUriParamsFlags;
  247. GLIB_AVAILABLE_IN_2_66
  248. GHashTable *g_uri_parse_params (const gchar *params,
  249. gssize length,
  250. const gchar *separators,
  251. GUriParamsFlags flags,
  252. GError **error);
  253. typedef struct _GUriParamsIter GUriParamsIter;
  254. struct _GUriParamsIter
  255. {
  256. /*< private >*/
  257. gint dummy0;
  258. gpointer dummy1;
  259. gpointer dummy2;
  260. guint8 dummy3[256];
  261. };
  262. GLIB_AVAILABLE_IN_2_66
  263. void g_uri_params_iter_init (GUriParamsIter *iter,
  264. const gchar *params,
  265. gssize length,
  266. const gchar *separators,
  267. GUriParamsFlags flags);
  268. GLIB_AVAILABLE_IN_2_66
  269. gboolean g_uri_params_iter_next (GUriParamsIter *iter,
  270. gchar **attribute,
  271. gchar **value,
  272. GError **error);
  273. /**
  274. * G_URI_ERROR:
  275. *
  276. * Error domain for URI methods. Errors in this domain will be from
  277. * the #GUriError enumeration. See #GError for information on error
  278. * domains.
  279. *
  280. * Since: 2.66
  281. */
  282. #define G_URI_ERROR (g_uri_error_quark ()) GLIB_AVAILABLE_MACRO_IN_2_66
  283. GLIB_AVAILABLE_IN_2_66
  284. GQuark g_uri_error_quark (void);
  285. /**
  286. * GUriError:
  287. * @G_URI_ERROR_FAILED: Generic error if no more specific error is available.
  288. * See the error message for details.
  289. * @G_URI_ERROR_BAD_SCHEME: The scheme of a URI could not be parsed.
  290. * @G_URI_ERROR_BAD_USER: The user/userinfo of a URI could not be parsed.
  291. * @G_URI_ERROR_BAD_PASSWORD: The password of a URI could not be parsed.
  292. * @G_URI_ERROR_BAD_AUTH_PARAMS: The authentication parameters of a URI could not be parsed.
  293. * @G_URI_ERROR_BAD_HOST: The host of a URI could not be parsed.
  294. * @G_URI_ERROR_BAD_PORT: The port of a URI could not be parsed.
  295. * @G_URI_ERROR_BAD_PATH: The path of a URI could not be parsed.
  296. * @G_URI_ERROR_BAD_QUERY: The query of a URI could not be parsed.
  297. * @G_URI_ERROR_BAD_FRAGMENT: The fragment of a URI could not be parsed.
  298. *
  299. * Error codes returned by #GUri methods.
  300. *
  301. * Since: 2.66
  302. */
  303. typedef enum {
  304. G_URI_ERROR_FAILED,
  305. G_URI_ERROR_BAD_SCHEME,
  306. G_URI_ERROR_BAD_USER,
  307. G_URI_ERROR_BAD_PASSWORD,
  308. G_URI_ERROR_BAD_AUTH_PARAMS,
  309. G_URI_ERROR_BAD_HOST,
  310. G_URI_ERROR_BAD_PORT,
  311. G_URI_ERROR_BAD_PATH,
  312. G_URI_ERROR_BAD_QUERY,
  313. G_URI_ERROR_BAD_FRAGMENT,
  314. } GUriError;
  315. /**
  316. * G_URI_RESERVED_CHARS_GENERIC_DELIMITERS:
  317. *
  318. * Generic delimiters characters as defined in
  319. * [RFC 3986](https://tools.ietf.org/html/rfc3986). Includes `:/?#[]@`.
  320. *
  321. * Since: 2.16
  322. **/
  323. #define G_URI_RESERVED_CHARS_GENERIC_DELIMITERS ":/?#[]@"
  324. /**
  325. * G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS:
  326. *
  327. * Subcomponent delimiter characters as defined in
  328. * [RFC 3986](https://tools.ietf.org/html/rfc3986). Includes `!$&'()*+,;=`.
  329. *
  330. * Since: 2.16
  331. **/
  332. #define G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS "!$&'()*+,;="
  333. /**
  334. * G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT:
  335. *
  336. * Allowed characters in path elements. Includes `!$&'()*+,;=:@`.
  337. *
  338. * Since: 2.16
  339. **/
  340. #define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":@"
  341. /**
  342. * G_URI_RESERVED_CHARS_ALLOWED_IN_PATH:
  343. *
  344. * Allowed characters in a path. Includes `!$&'()*+,;=:@/`.
  345. *
  346. * Since: 2.16
  347. **/
  348. #define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/"
  349. /**
  350. * G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO:
  351. *
  352. * Allowed characters in userinfo as defined in
  353. * [RFC 3986](https://tools.ietf.org/html/rfc3986). Includes `!$&'()*+,;=:`.
  354. *
  355. * Since: 2.16
  356. **/
  357. #define G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":"
  358. GLIB_AVAILABLE_IN_ALL
  359. char * g_uri_unescape_string (const char *escaped_string,
  360. const char *illegal_characters);
  361. GLIB_AVAILABLE_IN_ALL
  362. char * g_uri_unescape_segment (const char *escaped_string,
  363. const char *escaped_string_end,
  364. const char *illegal_characters);
  365. GLIB_AVAILABLE_IN_ALL
  366. char * g_uri_parse_scheme (const char *uri);
  367. GLIB_AVAILABLE_IN_2_66
  368. const char *g_uri_peek_scheme (const char *uri);
  369. GLIB_AVAILABLE_IN_ALL
  370. char * g_uri_escape_string (const char *unescaped,
  371. const char *reserved_chars_allowed,
  372. gboolean allow_utf8);
  373. GLIB_AVAILABLE_IN_2_66
  374. GBytes * g_uri_unescape_bytes (const char *escaped_string,
  375. gssize length,
  376. const char *illegal_characters,
  377. GError **error);
  378. GLIB_AVAILABLE_IN_2_66
  379. char * g_uri_escape_bytes (const guint8 *unescaped,
  380. gsize length,
  381. const char *reserved_chars_allowed);
  382. G_GNUC_END_IGNORE_DEPRECATIONS
  383. G_END_DECLS