URI Functions

URI Functions — URI-handling utilities

Functions

Types and Values

Includes

#include <glib.h>

Description

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

Note that GUri 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 GUri 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 g_uri_parse_relative().

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

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

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

g_uri_resolve_relative() and g_uri_parse_relative() allow you to resolve a relative URI relative to a base URI. g_uri_resolve_relative() takes two strings and returns a string, and g_uri_parse_relative() takes a GUri and a string and returns a GUri.

All of the parsing functions take a GUriFlags 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 g_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_FORM when parsing the params for a web URI, so compare the result of g_uri_peek_scheme() against http and https.

Building URIs

g_uri_join() and g_uri_join_with_user() can be used to construct valid URI strings from a set of component strings. They are the inverse of g_uri_split() and g_uri_split_with_user().

Similarly, g_uri_build() and g_uri_build_with_user() can be used to construct a GUri from a set of component strings.

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

Functions

g_uri_ref ()

GUri *
g_uri_ref (GUri *uri);

Increments the reference count of uri by one.

[skip]

Parameters

uri

a GUri

 

Returns

uri

Since: 2.66


g_uri_unref ()

void
g_uri_unref (GUri *uri);

Atomically decrements the reference count of uri by one.

When the reference count reaches zero, the resources allocated by uri are freed

[skip]

Parameters

uri

a GUri

 

Since: 2.66


g_uri_split ()

gboolean
g_uri_split (const gchar *uri_ref,
             GUriFlags flags,
             gchar **scheme,
             gchar **userinfo,
             gchar **host,
             gint *port,
             gchar **path,
             gchar **query,
             gchar **fragment,
             GError **error);

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).

If flags contains G_URI_FLAGS_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 G_URI_FLAGS_ENCODED if they are not.

Note that the G_URI_FLAGS_HAS_PASSWORD and G_URI_FLAGS_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.

Parameters

uri_ref

a string containing a relative or absolute URI

 

flags

flags for parsing uri_ref

 

scheme

on return, contains the scheme (converted to lowercase), or NULL.

[out][nullable][optional][transfer full]

userinfo

on return, contains the userinfo, or NULL.

[out][nullable][optional][transfer full]

host

on return, contains the host, or NULL.

[out][nullable][optional][transfer full]

port

on return, contains the port, or -1.

[out][optional][transfer full]

path

on return, contains the path.

[out][not nullable][optional][transfer full]

query

on return, contains the query, or NULL.

[out][nullable][optional][transfer full]

fragment

on return, contains the fragment, or NULL.

[out][nullable][optional][transfer full]

error

GError for error reporting, or NULL to ignore.

 

Returns

TRUE if uri_ref parsed successfully, FALSE on error.

[skip]

Since: 2.66


g_uri_split_with_user ()

gboolean
g_uri_split_with_user (const gchar *uri_ref,
                       GUriFlags flags,
                       gchar **scheme,
                       gchar **user,
                       gchar **password,
                       gchar **auth_params,
                       gchar **host,
                       gint *port,
                       gchar **path,
                       gchar **query,
                       gchar **fragment,
                       GError **error);

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 GUriFlags, for more information on the effect of flags . Note that password will only be parsed out if flags contains G_URI_FLAGS_HAS_PASSWORD, and auth_params will only be parsed out if flags contains G_URI_FLAGS_HAS_AUTH_PARAMS.

Parameters

uri_ref

a string containing a relative or absolute URI

 

flags

flags for parsing uri_ref

 

scheme

on return, contains the scheme (converted to lowercase), or NULL.

[out][nullable][optional][transfer full]

user

on return, contains the user, or NULL.

[out][nullable][optional][transfer full]

password

on return, contains the password, or NULL.

[out][nullable][optional][transfer full]

auth_params

on return, contains the auth_params, or NULL.

[out][nullable][optional][transfer full]

host

on return, contains the host, or NULL.

[out][nullable][optional][transfer full]

port

on return, contains the port, or -1.

[out][optional][transfer full]

path

on return, contains the path.

[out][not nullable][optional][transfer full]

query

on return, contains the query, or NULL.

[out][nullable][optional][transfer full]

fragment

on return, contains the fragment, or NULL.

[out][nullable][optional][transfer full]

error

GError for error reporting, or NULL to ignore.

 

Returns

TRUE if uri_ref parsed successfully, FALSE on error.

[skip]

Since: 2.66


g_uri_split_network ()

gboolean
g_uri_split_network (const gchar *uri_string,
                     GUriFlags flags,
                     gchar **scheme,
                     gchar **host,
                     gint *port,
                     GError **error);

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.

Parameters

uri_string

a string containing an absolute URI

 

flags

flags for parsing uri_string

 

scheme

on return, contains the scheme (converted to lowercase), or NULL.

[out][nullable][optional][transfer full]

host

on return, contains the host, or NULL.

[out][nullable][optional][transfer full]

port

on return, contains the port, or -1.

[out][optional][transfer full]

error

GError for error reporting, or NULL to ignore.

 

Returns

TRUE if uri_string parsed successfully, FALSE on error.

[skip]

Since: 2.66


g_uri_is_valid ()

gboolean
g_uri_is_valid (const gchar *uri_string,
                GUriFlags flags,
                GError **error);

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 GUriFlags, for more information on the effect of flags .

Parameters

uri_string

a string containing an absolute URI

 

flags

flags for parsing uri_string

 

error

GError for error reporting, or NULL to ignore.

 

Returns

TRUE if uri_string is a valid absolute URI, FALSE on error.

Since: 2.66


g_uri_join ()

gchar *
g_uri_join (GUriFlags flags,
            const gchar *scheme,
            const gchar *userinfo,
            const gchar *host,
            gint port,
            const gchar *path,
            const gchar *query,
            const gchar *fragment);

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).

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.

G_URI_FLAGS_HAS_PASSWORD and G_URI_FLAGS_HAS_AUTH_PARAMS are ignored if set in flags .

Parameters

flags

flags describing how to build the URI string

 

scheme

the URI scheme, or NULL.

[nullable]

userinfo

the userinfo component, or NULL.

[nullable]

host

the host component, or NULL.

[nullable]

port

the port, or -1

 

path

the path component.

[not nullable]

query

the query component, or NULL.

[nullable]

fragment

the fragment, or NULL.

[nullable]

Returns

an absolute URI string.

[not nullable][transfer full]

Since: 2.66


g_uri_join_with_user ()

gchar *
g_uri_join_with_user (GUriFlags flags,
                      const gchar *scheme,
                      const gchar *user,
                      const gchar *password,
                      const gchar *auth_params,
                      const gchar *host,
                      gint port,
                      const gchar *path,
                      const gchar *query,
                      const gchar *fragment);

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.

G_URI_FLAGS_HAS_PASSWORD and G_URI_FLAGS_HAS_AUTH_PARAMS are ignored if set in flags .

Parameters

flags

flags describing how to build the URI string

 

scheme

the URI scheme, or NULL.

[nullable]

user

the user component of the userinfo, or NULL.

[nullable]

password

the password component of the userinfo, or NULL.

[nullable]

auth_params

the auth params of the userinfo, or NULL.

[nullable]

host

the host component, or NULL.

[nullable]

port

the port, or -1

 

path

the path component.

[not nullable]

query

the query component, or NULL.

[nullable]

fragment

the fragment, or NULL.

[nullable]

Returns

an absolute URI string.

[not nullable][transfer full]

Since: 2.66


g_uri_parse ()

GUri *
g_uri_parse (const gchar *uri_string,
             GUriFlags flags,
             GError **error);

Parses uri_string according to flags . If the result is not a valid absolute URI, it will be discarded, and an error returned.

Parameters

uri_string

a string representing an absolute URI

 

flags

flags describing how to parse uri_string

 

error

GError for error reporting, or NULL to ignore.

 

Returns

a new GUri, or NULL on error.

[transfer full]

Since: 2.66


g_uri_parse_relative ()

GUri *
g_uri_parse_relative (GUri *base_uri,
                      const gchar *uri_ref,
                      GUriFlags flags,
                      GError **error);

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.

Parameters

base_uri

a base absolute URI.

[nullable][transfer none]

uri_ref

a string representing a relative or absolute URI

 

flags

flags describing how to parse uri_ref

 

error

GError for error reporting, or NULL to ignore.

 

Returns

a new GUri, or NULL on error.

[transfer full]

Since: 2.66


g_uri_resolve_relative ()

gchar *
g_uri_resolve_relative (const gchar *base_uri_string,
                        const gchar *uri_ref,
                        GUriFlags flags,
                        GError **error);

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.)

Parameters

base_uri_string

a string representing a base URI.

[nullable]

uri_ref

a string representing a relative or absolute URI

 

flags

flags describing how to parse uri_ref

 

error

GError for error reporting, or NULL to ignore.

 

Returns

the resolved URI string, or NULL on error.

[transfer full]

Since: 2.66


g_uri_build ()

GUri *
g_uri_build (GUriFlags flags,
             const gchar *scheme,
             const gchar *userinfo,
             const gchar *host,
             gint port,
             const gchar *path,
             const gchar *query,
             const gchar *fragment);

Creates a new GUri from the given components according to flags .

See also g_uri_build_with_user(), which allows specifying the components of the "userinfo" separately.

Parameters

flags

flags describing how to build the GUri

 

scheme

the URI scheme.

[not nullable]

userinfo

the userinfo component, or NULL.

[nullable]

host

the host component, or NULL.

[nullable]

port

the port, or -1

 

path

the path component.

[not nullable]

query

the query component, or NULL.

[nullable]

fragment

the fragment, or NULL.

[nullable]

Returns

a new GUri.

[not nullable][transfer full]

Since: 2.66


g_uri_build_with_user ()

GUri *
g_uri_build_with_user (GUriFlags flags,
                       const gchar *scheme,
                       const gchar *user,
                       const gchar *password,
                       const gchar *auth_params,
                       const gchar *host,
                       gint port,
                       const gchar *path,
                       const gchar *query,
                       const gchar *fragment);

Creates a new GUri from the given components according to flags (G_URI_FLAGS_HAS_PASSWORD is added unconditionally). The flags must be coherent with the passed values, in particular use %-encoded values with G_URI_FLAGS_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.

Parameters

flags

flags describing how to build the GUri

 

scheme

the URI scheme.

[not nullable]

user

the user component of the userinfo, or NULL.

[nullable]

password

the password component of the userinfo, or NULL.

[nullable]

auth_params

the auth params of the userinfo, or NULL.

[nullable]

host

the host component, or NULL.

[nullable]

port

the port, or -1

 

path

the path component.

[not nullable]

query

the query component, or NULL.

[nullable]

fragment

the fragment, or NULL.

[nullable]

Returns

a new GUri.

[not nullable][transfer full]

Since: 2.66


g_uri_peek_scheme ()

const char *
g_uri_peek_scheme (const char *uri);

Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:

1
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.

Parameters

uri

a valid URI.

 

Returns

The ‘scheme’ component of the URI, or NULL on error. The returned string is normalized to all-lowercase, and interned via g_intern_string(), so it does not need to be freed.

[transfer none][nullable]

Since: 2.66


g_uri_parse_scheme ()

char *
g_uri_parse_scheme (const char *uri);

Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:

1
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

Common schemes include file, https, svn+ssh, etc.

Parameters

uri

a valid URI.

 

Returns

The ‘scheme’ component of the URI, or NULL on error. The returned string should be freed when no longer needed.

[transfer full][nullable]

Since: 2.16


g_uri_to_string ()

char *
g_uri_to_string (GUri *uri);

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 G_URI_FLAGS_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.

Parameters

uri

a GUri

 

Returns

a string representing uri , which the caller must free.

[not nullable][transfer full]

Since: 2.66


g_uri_to_string_partial ()

char *
g_uri_to_string_partial (GUri *uri,
                         GUriHideFlags flags);

Returns a string representing uri , subject to the options in flags . See g_uri_to_string() and GUriHideFlags for more details.

Parameters

uri

a GUri

 

flags

flags describing what parts of uri to hide

 

Returns

a string representing uri , which the caller must free.

[not nullable][transfer full]

Since: 2.66


g_uri_get_scheme ()

const gchar *
g_uri_get_scheme (GUri *uri);

Gets uri 's scheme. Note that this will always be all-lowercase, regardless of the string or strings that uri was created from.

Parameters

uri

a GUri

 

Returns

uri 's scheme.

[not nullable]

Since: 2.66


g_uri_get_userinfo ()

const gchar *
g_uri_get_userinfo (GUri *uri);

Gets uri 's userinfo, which may contain %-encoding, depending on the flags with which uri was created.

Parameters

uri

a GUri

 

Returns

uri 's userinfo.

[nullable]

Since: 2.66


g_uri_get_user ()

const gchar *
g_uri_get_user (GUri *uri);

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 G_URI_FLAGS_HAS_PASSWORD or G_URI_FLAGS_HAS_AUTH_PARAMS, this is the same as g_uri_get_userinfo().

Parameters

uri

a GUri

 

Returns

uri 's user.

[nullable]

Since: 2.66


g_uri_get_password ()

const gchar *
g_uri_get_password (GUri *uri);

Gets uri 's password, which may contain %-encoding, depending on the flags with which uri was created. (If uri was not created with G_URI_FLAGS_HAS_PASSWORD then this will be NULL.)

Parameters

uri

a GUri

 

Returns

uri 's password.

[nullable]

Since: 2.66


g_uri_get_auth_params ()

const gchar *
g_uri_get_auth_params (GUri *uri);

Gets uri 's authentication parameters, which may contain %-encoding, depending on the flags with which uri was created. (If uri was not created with G_URI_FLAGS_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.

Parameters

uri

a GUri

 

Returns

uri 's authentication parameters.

[nullable]

Since: 2.66


g_uri_get_host ()

const gchar *
g_uri_get_host (GUri *uri);

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 G_URI_FLAGS_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).

Parameters

uri

a GUri

 

Returns

uri 's host.

[nullable]

Since: 2.66


g_uri_get_port ()

gint
g_uri_get_port (GUri *uri);

Gets uri 's port.

Parameters

uri

a GUri

 

Returns

uri 's port, or -1 if no port was specified.

Since: 2.66


g_uri_get_path ()

const gchar *
g_uri_get_path (GUri *uri);

Gets uri 's path, which may contain %-encoding, depending on the flags with which uri was created.

Parameters

uri

a GUri

 

Returns

uri 's path.

[not nullable]

Since: 2.66


g_uri_get_query ()

const gchar *
g_uri_get_query (GUri *uri);

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, GUriParamsIter or g_uri_parse_params() may be useful.

Parameters

uri

a GUri

 

Returns

uri 's query.

[nullable]

Since: 2.66


g_uri_get_fragment ()

const gchar *
g_uri_get_fragment (GUri *uri);

Gets uri 's fragment, which may contain %-encoding, depending on the flags with which uri was created.

Parameters

uri

a GUri

 

Returns

uri 's fragment.

[nullable]

Since: 2.66


g_uri_get_flags ()

GUriFlags
g_uri_get_flags (GUri *uri);

Gets uri 's flags set upon construction.

Parameters

uri

a GUri

 

Returns

uri 's flags.

Since: 2.66


g_uri_params_iter_init ()

void
g_uri_params_iter_init (GUriParamsIter *iter,
                        const gchar *params,
                        gssize length,
                        const gchar *separators,
                        GUriParamsFlags flags);

Initializes an attribute/value pair iterator.

The iterator keeps pointers to the params and separators arguments, those variables must thus outlive the iterator and not be modified during the iteration.

If G_URI_PARAMS_WWW_FORM is passed in flags , + characters in the param string will be replaced with spaces in the output. For example, foo=bar+baz will give attribute foo with value bar baz. This is commonly used on the web (the https and http schemes only), but is deprecated in favour of the equivalent of encoding spaces as %20.

Unlike with g_uri_parse_params(), G_URI_PARAMS_CASE_INSENSITIVE has no effect if passed to flags for g_uri_params_iter_init(). The caller is responsible for doing their own case-insensitive comparisons.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GUriParamsIter iter;
GError *error = NULL;
gchar *unowned_attr, *unowned_value;

g_uri_params_iter_init (&iter, "foo=bar&baz=bar&Foo=frob&baz=bar2", -1, "&", G_URI_PARAMS_NONE);
while (g_uri_params_iter_next (&iter, &unowned_attr, &unowned_value, &error))
  {
    g_autofree gchar *attr = g_steal_pointer (&unowned_attr);
    g_autofree gchar *value = g_steal_pointer (&unowned_value);
    // do something with attr and value; this code will be called 4 times
    // for the params string in this example: once with attr=foo and value=bar,
    // then with baz/bar, then Foo/frob, then baz/bar2.
  }
if (error)
  // handle parsing error

Parameters

iter

an uninitialized GUriParamsIter

 

params

a %-encoded string containing attribute=value parameters

 

length

the length of params , or -1 if it is nul-terminated

 

separators

the separator byte character set between parameters. (usually &amp;, but sometimes ; or both &amp;;). 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

flags to modify the way the parameters are handled.

 

Since: 2.66


g_uri_params_iter_next ()

gboolean
g_uri_params_iter_next (GUriParamsIter *iter,
                        gchar **attribute,
                        gchar **value,
                        GError **error);

Advances iter and retrieves the next attribute/value. FALSE is returned if an error has occurred (in which case error is set), or if the end of the iteration is reached (in which case attribute and value are set to NULL and the iterator becomes invalid). If TRUE is returned, g_uri_params_iter_next() may be called again to receive another attribute/value pair.

Note that the same attribute may be returned multiple times, since URIs allow repeated attributes.

Parameters

iter

an initialized GUriParamsIter

 

attribute

on return, contains the attribute, or NULL.

[out][nullable][optional][transfer full]

value

on return, contains the value, or NULL.

[out][nullable][optional][transfer full]

error

GError for error reporting, or NULL to ignore.

 

Returns

FALSE if the end of the parameters has been reached or an error was encountered. TRUE otherwise.

Since: 2.66


g_uri_parse_params ()

GHashTable *
g_uri_parse_params (const gchar *params,
                    gssize length,
                    const gchar *separators,
                    GUriParamsFlags flags,
                    GError **error);

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 GUriParamsIter.

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 G_URI_FLAGS_PARSE_RELAXED rules for g_uri_parse(). (However, if params is the path or query string from a GUri that was parsed without G_URI_FLAGS_PARSE_RELAXED and G_URI_FLAGS_ENCODED, then you already know that it does not contain any invalid encoding.)

G_URI_PARAMS_WWW_FORM is handled as documented for g_uri_params_iter_init().

If G_URI_PARAMS_CASE_INSENSITIVE is passed to flags , attributes will be compared case-insensitively, so a params string attr=123&amp;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.

Parameters

params

a %-encoded string containing attribute=value parameters

 

length

the length of params , or -1 if it is nul-terminated

 

separators

the separator byte character set between parameters. (usually &amp;, but sometimes ; or both &amp;;). 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

flags to modify the way the parameters are handled.

 

error

GError for error reporting, or NULL to ignore.

 

Returns

A hash table of attribute/value pairs, with both names and values fully-decoded; or NULL on error.

[transfer full][element-type utf8 utf8]

Since: 2.66


g_uri_escape_string ()

char *
g_uri_escape_string (const char *unescaped,
                     const char *reserved_chars_allowed,
                     gboolean allow_utf8);

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.

Parameters

unescaped

the unescaped input string.

 

reserved_chars_allowed

a string of reserved characters that are allowed to be used, or NULL.

[nullable]

allow_utf8

TRUE if the result can include UTF-8 characters.

 

Returns

an escaped version of unescaped . The returned string should be freed when no longer needed.

[not nullable]

Since: 2.16


g_uri_unescape_string ()

char *
g_uri_unescape_string (const char *escaped_string,
                       const char *illegal_characters);

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.

Parameters

escaped_string

an escaped string to be unescaped.

 

illegal_characters

a string of illegal characters not to be allowed, or NULL.

[nullable]

Returns

an unescaped version of escaped_string . The returned string should be freed when no longer needed.

[nullable]

Since: 2.16


g_uri_escape_bytes ()

char *
g_uri_escape_bytes (const guint8 *unescaped,
                    gsize length,
                    const char *reserved_chars_allowed);

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.

Parameters

unescaped

the unescaped input data.

[array length=length]

length

the length of unescaped

 

reserved_chars_allowed

a string of reserved characters that are allowed to be used, or NULL.

[nullable]

Returns

an escaped version of unescaped . The returned string should be freed when no longer needed.

[not nullable][transfer full]

Since: 2.66


g_uri_unescape_bytes ()

GBytes *
g_uri_unescape_bytes (const char *escaped_string,
                      gssize length,
                      const char *illegal_characters,
                      GError **error);

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.

Parameters

escaped_string

A URI-escaped string

 

length

the length (in bytes) of escaped_string to escape, or -1 if it is nul-terminated.

 

illegal_characters

a string of illegal characters not to be allowed, or NULL.

[nullable]

error

GError for error reporting, or NULL to ignore.

 

Returns

an unescaped version of escaped_string or NULL on error (if decoding failed, using G_URI_ERROR_FAILED error code). The returned GBytes should be unreffed when no longer needed.

[transfer full]

Since: 2.66


g_uri_unescape_segment ()

char *
g_uri_unescape_segment (const char *escaped_string,
                        const char *escaped_string_end,
                        const char *illegal_characters);

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().

Parameters

escaped_string

A string, may be NULL.

[nullable]

escaped_string_end

Pointer to end of escaped_string , may be NULL.

[nullable]

illegal_characters

An optional string of illegal characters not to be allowed, may be NULL.

[nullable]

Returns

an unescaped version of escaped_string , or NULL on error. The returned string should be freed when no longer needed. As a special case if NULL is given for escaped_string , this function will return NULL.

[nullable]

Since: 2.16


g_uri_list_extract_uris ()

gchar **
g_uri_list_extract_uris (const gchar *uri_list);

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.

Parameters

uri_list

an URI list

 

Returns

a newly allocated NULL-terminated list of strings holding the individual URIs. The array should be freed with g_strfreev().

[transfer full]

Since: 2.6


g_filename_from_uri ()

gchar *
g_filename_from_uri (const gchar *uri,
                     gchar **hostname,
                     GError **error);

Converts an escaped ASCII-encoded URI to a local filename in the encoding used for filenames.

Parameters

uri

a uri describing a filename (escaped, encoded in ASCII).

 

hostname

Location to store hostname for the URI. If there is no hostname in the URI, NULL will be stored in this location.

[out][optional][nullable]

error

location to store the error occurring, or NULL to ignore errors. Any of the errors in GConvertError may occur.

 

Returns

a newly-allocated string holding the resulting filename, or NULL on an error.

[type filename]


g_filename_to_uri ()

gchar *
g_filename_to_uri (const gchar *filename,
                   const gchar *hostname,
                   GError **error);

Converts an absolute filename to an escaped ASCII-encoded URI, with the path component following Section 3.3. of RFC 2396.

Parameters

filename

an absolute filename specified in the GLib file name encoding, which is the on-disk file name bytes on Unix, and UTF-8 on Windows.

[type filename]

hostname

A UTF-8 encoded hostname, or NULL for none.

[nullable]

error

location to store the error occurring, or NULL to ignore errors. Any of the errors in GConvertError may occur.

 

Returns

a newly-allocated string holding the resulting URI, or NULL on an error.

Types and Values

GUri

typedef struct _GUri GUri;

A parsed absolute URI.

Since GUri only represents absolute URIs, all GUris will have a URI scheme, so g_uri_get_scheme() will always return a non-NULL answer. Likewise, by definition, all URIs have a path component, so g_uri_get_path() will always return a non-NULL string (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 GUri will contain a hostname, and possibly a port and ‘userinfo’. Additionally, depending on how the GUri 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 GUri will have all %-encoded characters decoded. However, if you construct/parse a GUri 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:

1
2
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:

1
2
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:

1
2
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 g_uri_parse_params() do the decoding once of the query.

GUri is immutable once constructed, and can safely be accessed from multiple threads. Its reference counting is atomic.

Since: 2.66


enum GUriFlags

Flags that describe a URI.

When parsing a URI, if you need to choose different flags based on the type of URI, you can use g_uri_peek_scheme() on the URI string to check the scheme first, and use that to decide what flags to parse it with.

Members

G_URI_FLAGS_NONE

No flags set.

 

G_URI_FLAGS_PARSE_RELAXED

Parse the URI more relaxedly than the RFC 3986 grammar specifies, fixing up or ignoring common mistakes in URIs coming from external sources. This is also needed for some obscure URI schemes where ; separates the host from the path. Don’t use this flag unless you need to.

 

G_URI_FLAGS_HAS_PASSWORD

The userinfo field may contain a password, which will be separated from the username by :.

 

G_URI_FLAGS_HAS_AUTH_PARAMS

The userinfo may contain additional authentication-related parameters, which will be separated from the username and/or password by ;.

 

G_URI_FLAGS_ENCODED

When parsing a URI, this indicates that %-encoded characters in the userinfo, path, query, and fragment fields should not be decoded. (And likewise the host field if G_URI_FLAGS_NON_DNS is also set.) When building a URI, it indicates that you have already %-encoded the components, and so GUri should not do any encoding itself.

 

G_URI_FLAGS_NON_DNS

The host component should not be assumed to be a DNS hostname or IP address (for example, for smb URIs with NetBIOS hostnames).

 

G_URI_FLAGS_ENCODED_QUERY

Same as G_URI_FLAGS_ENCODED, for the query field only.

 

G_URI_FLAGS_ENCODED_PATH

Same as G_URI_FLAGS_ENCODED, for the path only.

 

G_URI_FLAGS_ENCODED_FRAGMENT

Same as G_URI_FLAGS_ENCODED, for the fragment only.

 

G_URI_FLAGS_SCHEME_NORMALIZE

A scheme-based normalization will be applied. For example, when parsing an HTTP URI changing omitted path to / and omitted port to 80; and when building a URI, changing empty path to / and default port 80). This only supports a subset of known schemes. (Since: 2.68)

 

Since: 2.66


enum GUriHideFlags

Flags describing what parts of the URI to hide in g_uri_to_string_partial(). Note that G_URI_HIDE_PASSWORD and G_URI_HIDE_AUTH_PARAMS will only work if the GUri was parsed with the corresponding flags.

Members

G_URI_HIDE_NONE

No flags set.

 

G_URI_HIDE_USERINFO

Hide the userinfo.

 

G_URI_HIDE_PASSWORD

Hide the password.

 

G_URI_HIDE_AUTH_PARAMS

Hide the auth_params.

 

G_URI_HIDE_QUERY

Hide the query.

 

G_URI_HIDE_FRAGMENT

Hide the fragment.

 

Since: 2.66


struct GUriParamsIter

struct GUriParamsIter {
};

Many URI schemes include one or more attribute/value pairs as part of the URI value. For example scheme://server/path?query=string&amp;is=there has two attributes – query=string and is=there – in its query part.

A GUriParamsIter structure represents an iterator that can be used to iterate over the attribute/value pairs of a URI query string. GUriParamsIter structures are typically allocated on the stack and then initialized with g_uri_params_iter_init(). See the documentation for g_uri_params_iter_init() for a usage example.

Since: 2.66


enum GUriParamsFlags

Flags modifying the way parameters are handled by g_uri_parse_params() and GUriParamsIter.

Members

G_URI_PARAMS_NONE

No flags set.

 

G_URI_PARAMS_CASE_INSENSITIVE

Parameter names are case insensitive.

 

G_URI_PARAMS_WWW_FORM

Replace + with space character. Only useful for URLs on the web, using the https or http schemas.

 

G_URI_PARAMS_PARSE_RELAXED

See G_URI_FLAGS_PARSE_RELAXED.

 

Since: 2.66


G_URI_RESERVED_CHARS_ALLOWED_IN_PATH

#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/"

Allowed characters in a path. Includes !$&amp;'()*+,;=:@/.

Since: 2.16


G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT

#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":@"

Allowed characters in path elements. Includes !$&amp;'()*+,;=:@.

Since: 2.16


G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO

#define G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":"

Allowed characters in userinfo as defined in RFC 3986. Includes !$&amp;'()*+,;=:.

Since: 2.16


G_URI_RESERVED_CHARS_GENERIC_DELIMITERS

#define G_URI_RESERVED_CHARS_GENERIC_DELIMITERS ":/?#[]@"

Generic delimiters characters as defined in RFC 3986. Includes :/?#[]@.

Since: 2.16


G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS

#define G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS "!$&'()*+,;="

Subcomponent delimiter characters as defined in RFC 3986. Includes !$&amp;'()*+,;=.

Since: 2.16


G_URI_ERROR

#define G_URI_ERROR (g_uri_error_quark ()) GLIB_AVAILABLE_MACRO_IN_2_66

Error domain for URI methods. Errors in this domain will be from the GUriError enumeration. See GError for information on error domains.

Since: 2.66


enum GUriError

Error codes returned by GUri methods.

Members

G_URI_ERROR_FAILED

Generic error if no more specific error is available. See the error message for details.

 

G_URI_ERROR_BAD_SCHEME

The scheme of a URI could not be parsed.

 

G_URI_ERROR_BAD_USER

The user/userinfo of a URI could not be parsed.

 

G_URI_ERROR_BAD_PASSWORD

The password of a URI could not be parsed.

 

G_URI_ERROR_BAD_AUTH_PARAMS

The authentication parameters of a URI could not be parsed.

 

G_URI_ERROR_BAD_HOST

The host of a URI could not be parsed.

 

G_URI_ERROR_BAD_PORT

The port of a URI could not be parsed.

 

G_URI_ERROR_BAD_PATH

The path of a URI could not be parsed.

 

G_URI_ERROR_BAD_QUERY

The query of a URI could not be parsed.

 

G_URI_ERROR_BAD_FRAGMENT

The fragment of a URI could not be parsed.

 

Since: 2.66