Gets uri's authentication parameters, which may contain
%-encoding, depending on the flags with which uri was created.
(If uri was not created with GLib.UriFlags.HAS_AUTH_PARAMS then this will
be null.)
Depending on the URI scheme, g_uri_parse_params() may be useful for
further parsing this information.
uri's authentication parameters.
Gets uri's fragment, which may contain %-encoding, depending on
the flags with which uri was created.
uri's fragment.
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 GLib.UriFlags.NON_DNS).
If uri contained an IPv6 address literal, this value will be just
that address, without the brackets around it that are necessary in
the string form of the URI. Note that in this case there may also
be a scope ID attached to the address. Eg, fe80::1234%``em1 (or
fe80::1234%``25em1 if the string is still encoded).
uri's host.
Gets uri's password, which may contain %-encoding, depending on
the flags with which uri was created. (If uri was not created
with GLib.UriFlags.HAS_PASSWORD then this will be null.)
uri's password.
Gets uri's path, which may contain %-encoding, depending on the
flags with which uri was created.
uri's path.
Gets uri's port.
uri's port, or -1 if no port was specified.
Gets uri's query, which may contain %-encoding, depending on the
flags with which uri was created.
For queries consisting of a series of name=value parameters,
GLib.UriParamsIter or g_uri_parse_params() may be useful.
uri's query.
Gets uri's scheme. Note that this will always be all-lowercase,
regardless of the string or strings that uri was created from.
uri's scheme.
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 GLib.UriFlags.HAS_PASSWORD or
GLib.UriFlags.HAS_AUTH_PARAMS, this is the same as g_uri_get_userinfo().
uri's user.
Gets uri's userinfo, which may contain %-encoding, depending on
the flags with which uri was created.
uri's userinfo.
Parses uri_ref according to flags and, if it is a
relative URI, resolves it relative to base_uri.
If the result is not a valid absolute URI, it will be discarded, and an error
returned.
a new GLib.Uri, or NULL on error.
Returns a string representing uri.
This is not guaranteed to return a string which is identical to the
string that uri was parsed from. However, if the source URI was
syntactically correct (according to RFC 3986), and it was parsed
with GLib.UriFlags.ENCODED, then g_uri_to_string() is guaranteed to return
a string which is at least semantically equivalent to the source
URI (according to RFC 3986).
If uri might contain sensitive details, such as authentication parameters,
or private data in its query string, and the returned string is going to be
logged, then consider using g_uri_to_string_partial() to redact parts.
a string representing uri, which the caller must free.
Returns a string representing uri, subject to the options in
flags. See g_uri_to_string() and GLib.UriHideFlags for more details.
flags describing what parts of uri to hide
a string representing uri, which the caller must free.
StaticbuildCreates a new GLib.Uri from the given components according to flags.
See also g_uri_build_with_user(), which allows specifying the
components of the "userinfo" separately.
flags describing how to build the GLib.Uri
the URI scheme
the userinfo component, or null
the host component, or null
the port, or -1
the path component
Optionalquery: stringthe query component, or null
Optionalfragment: stringthe fragment, or null
Staticbuild_Creates a new GLib.Uri from the given components according to flags
(GLib.UriFlags.HAS_PASSWORD is added unconditionally). The flags must be
coherent with the passed values, in particular use %-encoded values with
GLib.UriFlags.ENCODED.
In contrast to g_uri_build(), this allows specifying the components
of the ‘userinfo’ field separately. Note that user must be non-null
if either password or auth_params is non-null.
flags describing how to build the GLib.Uri
the URI scheme
the user component of the userinfo, or null
the password component of the userinfo, or null
the auth params of the userinfo, or null
the host component, or null
the port, or -1
the path component
Optionalquery: stringthe query component, or null
Optionalfragment: stringthe fragment, or null
Staticerror_Staticescape_Escapes arbitrary data for use in a URI.
Normally all characters that are not ‘unreserved’ (i.e. ASCII
alphanumerical characters plus dash, dot, underscore and tilde) are
escaped. But if you specify characters in reserved_chars_allowed
they are not escaped. This is useful for the ‘reserved’ characters
in the URI specification, since those are allowed unescaped in some
portions of a URI.
Though technically incorrect, this will also allow escaping nul
bytes as %``00.
the unescaped input data.
Optionalreserved_chars_allowed: stringa string of reserved characters that are allowed to be used, or null.
Staticescape_Escapes a string for use in a URI.
Normally all characters that are not "unreserved" (i.e. ASCII
alphanumerical characters plus dash, dot, underscore and tilde) are
escaped. But if you specify characters in reserved_chars_allowed
they are not escaped. This is useful for the "reserved" characters
in the URI specification, since those are allowed unescaped in some
portions of a URI.
the unescaped input string.
a string of reserved characters that are allowed to be used, or null.
true if the result can include UTF-8 characters.
Staticis_Parses uri_string according to flags, to determine whether it is a valid
absolute URI, i.e. it does not need to be resolved
relative to another URI using g_uri_parse_relative().
If it’s not a valid URI, an error is returned explaining how it’s invalid.
See g_uri_split(), and the definition of GLib.UriFlags, for more
information on the effect of flags.
StaticjoinJoins 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).
When host is present, path must either be empty or begin with a slash (/)
character. When host is not present, path cannot begin with two slash
characters (//). See
RFC 3986, section 3.
See also g_uri_join_with_user(), which allows specifying the
components of the ‘userinfo’ separately.
GLib.UriFlags.HAS_PASSWORD and GLib.UriFlags.HAS_AUTH_PARAMS are ignored if set
in flags.
flags describing how to build the URI string
the URI scheme, or null
the userinfo component, or null
the host component, or null
the port, or -1
the path component
Optionalquery: stringthe query component, or null
Optionalfragment: stringthe fragment, or null
Staticjoin_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).
In contrast to g_uri_join(), this allows specifying the components
of the ‘userinfo’ separately. It otherwise behaves the same.
GLib.UriFlags.HAS_PASSWORD and GLib.UriFlags.HAS_AUTH_PARAMS are ignored if set
in flags.
flags describing how to build the URI string
the URI scheme, or null
the user component of the userinfo, or null
the password component of the userinfo, or null
the auth params of the userinfo, or null
the host component, or null
the port, or -1
the path component
Optionalquery: stringthe query component, or null
Optionalfragment: stringthe fragment, or null
Staticlist_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.
an URI list
StaticparseParses uri_string according to flags. If the result is not a
valid absolute URI, it will be discarded, and an
error returned.
Staticparse_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 value is the final returned value. If you need to handle repeated attributes differently, use GLib.UriParamsIter.
The params string is assumed to still be %-encoded, but the returned
values will be fully decoded. (Thus it is possible that the returned values
may contain = or separators, if the value was encoded in the input.)
Invalid %-encoding is treated as with the GLib.UriFlags.PARSE_RELAXED
rules for g_uri_parse(). (However, if params is the path or query string
from a GLib.Uri that was parsed without GLib.UriFlags.PARSE_RELAXED and
GLib.UriFlags.ENCODED, then you already know that it does not contain any
invalid encoding.)
GLib.UriParamsFlags.WWW_FORM is handled as documented for g_uri_params_iter_init().
If GLib.UriParamsFlags.CASE_INSENSITIVE is passed to flags, attributes will be
compared case-insensitively, so a params string attr=123&Attr=456 will only
return a single attribute–value pair, Attr=456. Case will be preserved in
the returned attributes.
If params cannot be parsed (for example, it contains two separators
characters in a row), then error is set and null is returned.
a %-encoded string containing attribute=value parameters
the length of params, or -1 if it is nul-terminated
the separator byte character set between parameters. (usually &, but sometimes ; or both &;). Note that this function works on bytes not characters, so it can't be used to delimit UTF-8 strings for anything but ASCII characters. You may pass an empty set, in which case no splitting will occur.
flags to modify the way the parameters are handled.
Staticparse_Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
Common schemes include file, https, svn+ssh, etc.
a valid URI.
Staticpeek_Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
Common schemes include file, https, svn+ssh, etc.
Unlike g_uri_parse_scheme(), the returned scheme is normalized to
all-lowercase and does not need to be freed.
a valid URI.
Staticresolve_Parses uri_ref according to flags and, if it is a
relative URI, resolves it relative to
base_uri_string. If the result is not a valid absolute URI, it will be
discarded, and an error returned.
(If base_uri_string is null, this just returns uri_ref, or
null if uri_ref is invalid or not absolute.)
StaticsplitParses uri_ref (which can be an
absolute or relative URI) according to flags, and
returns the pieces. Any component that doesn't appear in uri_ref will be
returned as null (but note that all URIs always have a path component,
though it may be the empty string).
If flags contains GLib.UriFlags.ENCODED, then %-encoded characters in
uri_ref will remain encoded in the output strings. (If not,
then all such characters will be decoded.) Note that decoding will
only work if the URI components are ASCII or UTF-8, so you will
need to use GLib.UriFlags.ENCODED if they are not.
Note that the GLib.UriFlags.HAS_PASSWORD and
GLib.UriFlags.HAS_AUTH_PARAMS flags are ignored by g_uri_split(),
since it always returns only the full userinfo; use
g_uri_split_with_user() if you want it split up.
Staticsplit_Parses uri_string (which must be an absolute URI)
according to flags, and returns the pieces relevant to connecting to a host.
See the documentation for g_uri_split() for more details; this is
mostly a wrapper around that function with simpler arguments.
However, it will return an error if uri_string is a relative URI,
or does not contain a hostname component.
Staticsplit_Parses uri_ref (which can be an
absolute or relative URI) according to flags, and
returns the pieces. Any component that doesn't appear in uri_ref will be
returned as null (but note that all URIs always have a path component,
though it may be the empty string).
See g_uri_split(), and the definition of GLib.UriFlags, for more
information on the effect of flags. Note that password will only
be parsed out if flags contains GLib.UriFlags.HAS_PASSWORD, and
auth_params will only be parsed out if flags contains
GLib.UriFlags.HAS_AUTH_PARAMS.
Staticunescape_Unescapes a segment of an escaped string as binary data.
Note that in contrast to g_uri_unescape_string(), this does allow
nul bytes to appear in the output.
If any of the characters in illegal_characters appears as an escaped
character in escaped_string, then that is an error and null will be
returned. This is useful if you want to avoid for instance having a slash
being expanded in an escaped path element, which might confuse pathname
handling.
A URI-escaped string
the length (in bytes) of escaped_string to escape, or -1 if it is nul-terminated.
Optionalillegal_characters: stringa string of illegal characters not to be allowed, or null.
Staticunescape_Unescapes a segment of an escaped string.
If any of the characters in illegal_characters or the NUL
character appears as an escaped character in escaped_string, then
that is an error and null will be returned. This is useful if you
want to avoid for instance having a slash being expanded in an
escaped path element, which might confuse pathname handling.
Note: NUL byte is not accepted in the output, in contrast to
g_uri_unescape_bytes().
Optionalescaped_string: stringA string, may be null
Optionalescaped_string_end: stringPointer to end of escaped_string, may be null
Optionalillegal_characters: stringAn optional string of illegal characters not to be allowed, may be null
Staticunescape_Unescapes a whole escaped string.
If any of the characters in illegal_characters or the NUL
character appears as an escaped character in escaped_string, then
that is an error and null will be returned. This is useful if you
want to avoid for instance having a slash being expanded in an
escaped path element, which might confuse pathname handling.
an escaped string to be unescaped.
Optionalillegal_characters: stringa string of illegal characters not to be allowed, or null.
The GLib.Uri type and related functions can be used to parse URIs into their components, and build valid URIs from individual components.
Since GLib.Uri only represents absolute URIs, all GLib.Uris will have a URI scheme, so GLib.Uri.get_scheme will always return a non-
NULLanswer. Likewise, by definition, all URIs have a path component, so GLib.Uri.get_path will always return a non-NULLstring (which may be empty).If the URI string has an ‘authority’ component (that is, if the scheme is followed by
://rather than just:), then the GLib.Uri will contain a hostname, and possibly a port and ‘userinfo’. Additionally, depending on how the GLib.Uri was constructed/parsed (for example, using theG_URI_FLAGS_HAS_PASSWORDandG_URI_FLAGS_HAS_AUTH_PARAMSflags), the userinfo may be split out into a username, password, and additional authorization-related parameters.Normally, the components of a GLib.Uri will have all
%-encoded characters decoded. However, if you construct/parse a GLib.Uri withG_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 withG_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:
While the default
%-decoding behaviour would give:During decoding, if an invalid UTF-8 string is encountered, parsing will fail with an error indicating the bad string location:
You should pass
G_URI_FLAGS_ENCODEDorG_URI_FLAGS_ENCODED_QUERYif you need to handle that case manually. In particular, if the query string contains=characters that are%-encoded, you should let GLib.Uri.parse_params do the decoding once of the query.GLib.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 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 GLib.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 contrast 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#fragmentor//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.parse_relative.
For example, a valid absolute URI is
file:///home/boborhttps://search.com?query=string.A GLib.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.split and GLib.Uri.split_with_user. These split a URI into its component parts, and return the parts; the difference between the two is that GLib.Uri.split treats the ‘userinfo’ component of the URI as a single element, while GLib.Uri.split_with_user can (depending on the GLib.UriFlags you pass) treat it as containing a username, password, and authentication parameters. Alternatively, GLib.Uri.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).
GLib.Uri.parse is similar to GLib.Uri.split, but instead of returning individual strings, it returns a GLib.Uri structure (and it requires that the URI be an absolute URI).
GLib.Uri.resolve_relative and GLib.Uri.parse_relative allow you to resolve a relative URI relative to a base URI. GLib.Uri.resolve_relative takes two strings and returns a string, and GLib.Uri.parse_relative takes a GLib.Uri and a string and returns a GLib.Uri.
All of the parsing functions take a GLib.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.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_FORMwhen parsing the params for a web URI, so compare the result of GLib.Uri.peek_scheme againsthttpandhttps.Building URIs
GLib.Uri.join and GLib.Uri.join_with_user can be used to construct valid URI strings from a set of component strings. They are the inverse of GLib.Uri.split and GLib.Uri.split_with_user.
Similarly, GLib.Uri.build and GLib.Uri.build_with_user can be used to construct a GLib.Uri from a set of component strings.
As with the parsing functions, the building functions take a GLib.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 theG_URI_FLAGS_ENCODEDflag.file://URIsNote 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 does not implement these rules. Use GLib.filename_from_uri and GLib.filename_to_uri if you want to properly convert betweenfile://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 does not have. GLib.Uri can help with normalization if you use the various encoded GLib.UriFlags as well asG_URI_FLAGS_SCHEME_NORMALIZEhowever it is not comprehensive. For example,data:,fooanddata:;base64,Zm9vresolve to the same thing according to thedata:URI specification which GLib does not handle.Since
2.66