Object Hierarchy:

GLib.Uri GLib.Uri GLib.Uri


[ Compact ]
[ CCode ( ref_function = "g_uri_ref" , type_id = "G_TYPE_URI" , unref_function = "g_uri_unref" ) ]
public class Uri

The Uri type and related functions can be used to parse URIs into their components, and build valid URIs from individual components.

Note that Uri scope 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 to help prevent homograph attacks, so Uri is not suitable for formatting URIs for display to the user for making security-sensitive decisions.

Relative and absolute URIs # {relative-absolute-uris}

As defined in [RFC 3986](https://tools.ietf.org/html/rfc3986section-4), 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](https://tools.ietf.org/html/rfc3986section-4.3), 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 parse_relative.

For example, a valid absolute URI is `file:///home/bob` or `https://search.com?query=string`.

A 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 split and split_with_user. These split a URI into its component parts, and return the parts; the difference between the two is that split treats the ‘userinfo’ component of the URI as a single element, while split_with_user can (depending on the UriFlags you pass) treat it as containing a username, password, and authentication parameters. Alternatively, split_network 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).

parse is similar to split, but instead of returning individual strings, it returns a Uri structure (and it requires that the URI be an absolute URI).

resolve_relative and parse_relative allow you to resolve a relative URI relative to a base URI. resolve_relative takes two strings and returns a string, and parse_relative takes a Uri and a string and returns a Uri.

All of the parsing functions take a 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 peek_scheme 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 peek_scheme against `http` and `https`.

Building URIs

join and join_with_user can be used to construct valid URI strings from a set of component strings. They are the inverse of split and split_with_user.

Similarly, build and build_with_user can be used to construct a Uri from a set of component strings.

As with the parsing functions, the building functions take a 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). Uri does not implement these rules. Use from_uri and 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 Uri does not have. Uri can help with normalization if you use the various encoded 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.

Namespace: GLib
Package: glib-2.0



Static methods: