glib.uri
Module for [Uri] class
Types 2
The [glib.uri.Uri] type and related functions can be used to parse URIs into their components, and build valid URIs from individual components.
Since [glib.uri.Uri] only represents absolute URIs, all [glib.uri.Uri]s will have a URI scheme, so [glib.uri.Uri.getScheme] will always return a non-NULL answer. Likewise, by definition, all URIs have a path component, so [glib.uri.Uri.getPath] will always return a non-NULL string (which may be empty).
If the URI string has an
‘authority’ component (thatis, if the scheme is followed by `://` rather than just `:`), then the [glib.uri.Uri] will contain a hostname, and possibly a port and ‘userinfo’. Additionally, depending on how the [glib.uri.Uri] was constructed/parsed (for example, using the G_URI_FLAGS_HAS_PASSWORD and G_URI_FLAGS_HAS_AUTH_PARAMS flags), the userinfo may be split out into a username, password, and additional authorization-related parameters.
Normally, the components of a [glib.uri.Uri] will have all `%`-encoded characters decoded. However, if you construct/parse a [glib.uri.Uri] with G_URI_FLAGS_ENCODED, then the `%`-encoding will be preserved instead in the userinfo, path, and query fields (and in the host field if also created with G_URI_FLAGS_NON_DNS). In particular, this is necessary if the URI may contain binary data or non-UTF-8 text, or if decoding the components might change the interpretation of the URI.
For example, with the encoded flag:
g_autoptr(GUri) uri = g_uri_parse ("http://host/path?query=http%3A%2F%2Fhost%2Fpath%3Fparam%3Dvalue", G_URI_FLAGS_ENCODED, &err);
g_assert_cmpstr (g_uri_get_query (uri), ==, "query=http%3A%2F%2Fhost%2Fpath%3Fparam%3Dvalue");While the default `%`-decoding behaviour would give:
g_autoptr(GUri) uri = g_uri_parse ("http://host/path?query=http%3A%2F%2Fhost%2Fpath%3Fparam%3Dvalue", G_URI_FLAGS_NONE, &err);
g_assert_cmpstr (g_uri_get_query (uri), ==, "query=http://host/path?param=value");During decoding, if an invalid UTF-8 string is encountered, parsing will fail with an error indicating the bad string location:
g_autoptr(GUri) uri = g_uri_parse ("http://host/path?query=http%3A%2F%2Fhost%2Fpath%3Fbad%3D%00alue", G_URI_FLAGS_NONE, &err);
g_assert_error (err, G_URI_ERROR, G_URI_ERROR_BAD_QUERY);You should pass G_URI_FLAGS_ENCODED or G_URI_FLAGS_ENCODED_QUERY if you need to handle that case manually. In particular, if the query string contains `=` characters that are `%`-encoded, you should let [glib.uri.Uri.parseParams] do the decoding once of the query.
[glib.uri.Uri] is immutable once constructed, and can safely be accessed from multiple threads. Its reference counting is atomic.
Note that the scope of [glib.uri.Uri] is to help manipulate URIs in various applications, following RFC 3986. In particular, it doesn't intend to cover web browser needs, and doesn’t implement the
WHATWG URL standard. No APIs are provided tohelp prevent
homograph attacks, so[glib.uri.Uri] is not suitable for formatting URIs for display to the user for making security-sensitive decisions.
Relative and absolute URIs
As defined in RFC 3986, the hierarchical nature of URIs means that they can either be ‘relative references’ (sometimes referred to as ‘relative URIs’) or ‘URIs’ (for clarity, ‘URIs’ are referred to in this documentation as ‘absolute URIs’ — although
in constrast to RFC 3986,fragment identifiers are always allowed).
Relative references have one or more components of the URI missing. In particular, they have no scheme. Any other component, such as hostname, query, etc. may be missing, apart from a path, which has to be specified (but may be empty). The path may be relative, starting with `./` rather than `/`.
For example, a valid relative reference is ./path?query, /?query#fragment or //example.com.
Absolute URIs have a scheme specified. Any other components of the URI which are missing are specified as explicitly unset in the URI, rather than being resolved relative to a base URI using [glib.uri.Uri.parseRelative].
For example, a valid absolute URI is file:///home/bob or https://search.com?query=string.
A [glib.uri.Uri] instance is always an absolute URI. A string may be an absolute URI or a relative reference; see the documentation for individual functions as to what forms they accept.
Parsing URIs
The most minimalist APIs for parsing URIs are [glib.uri.Uri.split] and [glib.uri.Uri.splitWithUser]. These split a URI into its component parts, and return the parts; the difference between the two is that [glib.uri.Uri.split] treats the ‘userinfo’ component of the URI as a single element, while [glib.uri.Uri.splitWithUser] can (depending on the [glib.types.UriFlags] you pass) treat it as containing a username, password, and authentication parameters. Alternatively, [glib.uri.Uri.splitNetwork] can be used when you are only interested in the components that are needed to initiate a network connection to the service (scheme, host, and port).
[glib.uri.Uri.parse] is similar to [glib.uri.Uri.split], but instead of returning individual strings, it returns a [glib.uri.Uri] structure (and it requires that the URI be an absolute URI).
[glib.uri.Uri.resolveRelative] and [glib.uri.Uri.parseRelative] allow you to resolve a relative URI relative to a base URI. [glib.uri.Uri.resolveRelative] takes two strings and returns a string, and [glib.uri.Uri.parseRelative] takes a [glib.uri.Uri] and a string and returns a [glib.uri.Uri].
All of the parsing functions take a [glib.types.UriFlags] argument describing exactly how to parse the URI; see the documentation for that type for more details on the specific flags that you can pass. If you need to choose different flags based on the type of URI, you can use [glib.uri.Uri.peekScheme] on the URI string to check the scheme first, and use that to decide what flags to parse it with.
For example, you might want to use G_URI_PARAMS_WWW_FORM when parsing the params for a web URI, so compare the result of [glib.uri.Uri.peekScheme] against http and https.
Building URIs
[glib.uri.Uri.join] and [glib.uri.Uri.joinWithUser] can be used to construct valid URI strings from a set of component strings. They are the inverse of [glib.uri.Uri.split] and [glib.uri.Uri.splitWithUser].
Similarly, [glib.uri.Uri.build] and [glib.uri.Uri.buildWithUser] can be used to construct a [glib.uri.Uri] from a set of component strings.
As with the parsing functions, the building functions take a [glib.types.UriFlags] argument. In particular, it is important to keep in mind whether the URI components you are using are already `%`-encoded. If so, you must pass the G_URI_FLAGS_ENCODED flag.
file:// URIs
Note that Windows and Unix both define special rules for parsing file:// URIs (involving non-UTF-8 character sets on Unix, and the interpretation of path separators on Windows). [glib.uri.Uri] does not implement these rules. Use func@GLib.filename_from_uri and func@GLib.filename_to_uri if you want to properly convert between file:// URIs and local filenames.
URI Equality
Note that there is no g_uri_equal () function, because comparing URIs usefully requires scheme-specific knowledge that [glib.uri.Uri] does not have. [glib.uri.Uri] can help with normalization if you use the various encoded [glib.types.UriFlags] as well as G_URI_FLAGS_SCHEME_NORMALIZE however it is not comprehensive. For example, data:,foo and data:;base64,Zm9v resolve to the same thing according to the data: URI specification which GLib does not handle.
string getAuthParams()Gets uri's authentication parameters, which may contain `%`-encoding, depending on the flags with which uri was created. (If uri was not created with `GURIFLAGSHASAUTH_PARAMS` then this will be null.)glib.types.UriFlags getFlags()Gets uri's flags set upon construction. Returns: uri's flags.string getFragment()Gets uri's fragment, which may contain `%`-encoding, depending on the flags with which uri was created. Returns: uri's fragment.string getHost()Gets uri's host. This will never have `%`-encoded characters, unless it is non-UTF-8 (which can only be the case if uri was created with `GURIFLAGSNONDNS`).string getPassword()Gets uri's password, which may contain `%`-encoding, depending on the flags with which uri was created. (If uri was not created with `GURIFLAGSHASPASSWORD` then this will be null.) Returns: uri's p...string getPath()Gets uri's path, which may contain `%`-encoding, depending on the flags with which uri was created. Returns: uri's path.int getPort()Gets uri's port. Returns: uri's port, or `-1` if no port was specified.string getQuery()Gets uri's query, which may contain `%`-encoding, depending on the flags with which uri was created.string getScheme()Gets uri's scheme. Note that this will always be all-lowercase, regardless of the string or strings that uri was created from. Returns: uri's scheme.string getUser()Gets the ‘username’ component of uri's userinfo, which may contain `%`-encoding, depending on the flags with which uri was created. If uri was not created with `GURIFLAGSHASPASSWORD` or `GURIFL...string getUserinfo()Gets uri's userinfo, which may contain `%`-encoding, depending on the flags with which uri was created. Returns: uri's userinfo.glib.uri.Uri parseRelative(string uriRef, glib.types.UriFlags flags)Parses uriref according to flags and, if it is a [relative URI](#relative-and-absolute-uris), resolves it relative to baseuri. If the result is not a valid absolute URI, it will be discarded, and a...string toString_()Returns a string representing uri.string toStringPartial(glib.types.UriHideFlags flags)Returns a string representing uri, subject to the options in flags. See [glib.uri.Uri.toString_] and #GUriHideFlags for more details.glib.uri.Uri build(glib.types.UriFlags flags, string scheme, string userinfo, string host, int port, string path, string query = null, string fragment = null)Creates a new #GUri from the given components according to flags.glib.uri.Uri buildWithUser(glib.types.UriFlags flags, string scheme, string user, string password, string authParams, string host, int port, string path, string query = null, string fragment = null)Creates a new #GUri from the given components according to flags (`GURIFLAGSHASPASSWORD` is added unconditionally). The flags must be coherent with the passed values, in particular use `%`-encoded ...string escapeBytes(ubyte[] unescaped, string reservedCharsAllowed = null)Escapes arbitrary data for use in a URI.string escapeString(string unescaped, string reservedCharsAllowed, bool allowUtf8)Escapes a string for use in a URI.bool isValid(string uriString, glib.types.UriFlags flags)Parses uri_string according to flags, to determine whether it is a valid [absolute URI](#relative-and-absolute-uris), i.e. it does not need to be resolved relative to another URI using [glib.uri.Ur...string join(glib.types.UriFlags flags, string scheme, string userinfo, string host, int port, string path, string query = null, string fragment = null)Joins the given components together according to flags to create an absolute URI string. path may not be null (though it may be the empty string).string joinWithUser(glib.types.UriFlags flags, string scheme, string user, string password, string authParams, string host, int port, string path, string query = null, string fragment = null)Joins the given components together according to flags to create an absolute URI string. path may not be null (though it may be the empty string).string[] listExtractUris(string uriList)Splits an URI list conforming to the text/uri-list mime type defined in RFC 2483 into individual URIs, discarding any comments. The URIs are not validated.glib.uri.Uri parse(string uriString, glib.types.UriFlags flags)Parses uri_string according to flags. If the result is not a valid [absolute URI](#relative-and-absolute-uris), it will be discarded, and an error returned.string[string] parseParams(string params, string separators, glib.types.UriParamsFlags flags)Many URI schemes include one or more attribute/value pairs as part of the URI value. This method can be used to parse them into a hash table. When an attribute has multiple occurrences, the last va...string parseScheme(string uri)Gets the scheme portion of a URI string. [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3) decodes the scheme as: ``` URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] ``` Common sc...string peekScheme(string uri)Gets the scheme portion of a URI string. [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3) decodes the scheme as: ``` URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] ``` Common sc...string resolveRelative(string baseUriString, string uriRef, glib.types.UriFlags flags)Parses uriref according to flags and, if it is a [relative URI](#relative-and-absolute-uris), resolves it relative to baseuri_string. If the result is not a valid absolute URI, it will be discarded...bool split(string uriRef, glib.types.UriFlags flags, out string scheme, out string userinfo, out string host, out int port, out string path, out string query, out string fragment)Parses uriref (which can be an [absolute or relative URI](#relative-and-absolute-uris)) according to flags, and returns the pieces. Any component that doesn't appear in uriref will be returned as n...bool splitNetwork(string uriString, glib.types.UriFlags flags, out string scheme, out string host, out int port)Parses uristring (which must be an [absolute URI](#relative-and-absolute-uris)) according to flags, and returns the pieces relevant to connecting to a host. See the documentation for [glib.uri.Uri....bool splitWithUser(string uriRef, glib.types.UriFlags flags, out string scheme, out string user, out string password, out string authParams, out string host, out int port, out string path, out string query, out string fragment)Parses uriref (which can be an [absolute or relative URI](#relative-and-absolute-uris)) according to flags, and returns the pieces. Any component that doesn't appear in uriref will be returned as n...glib.bytes.Bytes unescapeBytes(string escapedString, string illegalCharacters = null)Unescapes a segment of an escaped string as binary data.string unescapeSegment(string escapedString = null, string escapedStringEnd = null, string illegalCharacters = null)Unescapes a segment of an escaped string.string unescapeString(string escapedString, string illegalCharacters = null)Unescapes a whole escaped string.