2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * Copyright (C) 2011 Tim-Philipp Müller <tim centricular net>
5 * Copyright (C) 2014 David Waring, British Broadcasting Corporation
6 * <david.waring@rd.bbc.co.uk>
8 * gsturi.c: register URI handlers and IETF RFC 3986 URI manipulations.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:gsturihandler
28 * @short_description: Interface to ease URI handling in plugins.
30 * The URIHandler is an interface that is implemented by Source and Sink
31 * #GstElement to simplify then handling of URI.
33 * An application can use the following functions to quickly get an element
34 * that handles the given URI for reading or writing
35 * (gst_element_make_from_uri()).
37 * Source and Sink plugins should implement this interface when possible.
44 #include "gst_private.h"
48 #include "gstregistry.h"
50 #include "gst-i18n-lib.h"
54 #include <glib/gprintf.h>
56 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
57 #define GST_CAT_DEFAULT gst_uri_handler_debug
59 #ifndef HAVE_STRCASESTR
60 #define strcasestr _gst_ascii_strcasestr
62 /* From https://github.com/freebsd/freebsd/blob/master/contrib/file/src/strcasestr.c
63 * Updated to use GLib types and GLib string functions
65 * Copyright (c) 1990, 1993
66 * The Regents of the University of California. All rights reserved.
68 * This code is derived from software contributed to Berkeley by
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 * notice, this list of conditions and the following disclaimer in the
78 * documentation and/or other materials provided with the distribution.
79 * 3. Neither the name of the University nor the names of its contributors
80 * may be used to endorse or promote products derived from this software
81 * without specific prior written permission.
83 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 * Find the first occurrence of find in s, ignore case.
101 _gst_ascii_strcasestr (const gchar * s, const gchar * find)
106 if ((c = *find++) != 0) {
107 c = g_ascii_tolower (c);
111 if ((sc = *s++) == 0)
113 } while (g_ascii_tolower (sc) != c);
114 } while (g_ascii_strncasecmp (s, find, len) != 0);
117 return (gchar *) (gintptr) (s);
121 #if !GLIB_CHECK_VERSION (2, 33, 4)
122 #define g_list_copy_deep gst_g_list_copy_deep
124 gst_g_list_copy_deep (GList * list, GCopyFunc func, gpointer user_data)
126 list = g_list_copy (list);
131 for (l = list; l != NULL; l = l->next) {
132 l->data = func (l->data, user_data);
141 gst_uri_handler_get_type (void)
143 static volatile gsize urihandler_type = 0;
145 if (g_once_init_enter (&urihandler_type)) {
147 static const GTypeInfo urihandler_info = {
148 sizeof (GstURIHandlerInterface),
160 _type = g_type_register_static (G_TYPE_INTERFACE,
161 "GstURIHandler", &urihandler_info, 0);
163 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
165 g_once_init_leave (&urihandler_type, _type);
167 return urihandler_type;
171 gst_uri_error_quark (void)
173 return g_quark_from_static_string ("gst-uri-error-quark");
176 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
177 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
178 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
179 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
180 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
181 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
182 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
187 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
188 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
189 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
190 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
191 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
192 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
193 } UnsafeCharacterSet;
195 #define HEX_ESCAPE '%'
197 /* Escape undesirable characters using %
198 * -------------------------------------
200 * This function takes a pointer to a string in which
201 * some characters may be unacceptable unescaped.
202 * It returns a string which has these characters
203 * represented by a '%' character followed by two hex digits.
205 * This routine returns a g_malloced string.
208 static const gchar hex[16] = "0123456789ABCDEF";
211 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
213 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
220 UnsafeCharacterSet use_mask;
222 g_return_val_if_fail (mask == UNSAFE_ALL
223 || mask == UNSAFE_ALLOW_PLUS
224 || mask == UNSAFE_PATH
225 || mask == UNSAFE_DOS_PATH
226 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
228 if (string == NULL) {
234 for (p = string; *p != '\0'; p++) {
236 if (!ACCEPTABLE_CHAR (c)) {
239 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
240 /* when escaping a host, if we hit something that needs to be escaped, or we finally
241 * hit a path separator, revert to path mode (the host segment of the url is over).
243 use_mask = UNSAFE_PATH;
247 result = g_malloc (p - string + unacceptable * 2 + 1);
250 for (q = result, p = string; *p != '\0'; p++) {
253 if (!ACCEPTABLE_CHAR (c)) {
254 *q++ = HEX_ESCAPE; /* means hex coming */
260 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
261 use_mask = UNSAFE_PATH;
271 * @string: string to be escaped
273 * Escapes @string, replacing any and all special characters
274 * with equivalent escape sequences.
276 * Return value: a newly allocated string equivalent to @string
277 * but with all special characters escaped
280 escape_string (const gchar * string)
282 return escape_string_internal (string, UNSAFE_ALL);
288 return c >= '0' && c <= '9' ? c - '0'
289 : c >= 'A' && c <= 'F' ? c - 'A' + 10
290 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
294 unescape_character (const char *scanner)
299 first_digit = hex_to_int (*scanner++);
300 if (first_digit < 0) {
304 second_digit = hex_to_int (*scanner);
305 if (second_digit < 0) {
309 return (first_digit << 4) | second_digit;
313 * @escaped_string: an escaped URI, path, or other string
314 * @illegal_characters: a string containing a sequence of characters
315 * considered "illegal", '\0' is automatically in this list.
317 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
318 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
319 * for character 16x+y.
321 * Return value: (nullable): a newly allocated string with the
322 * unescaped equivalents, or %NULL if @escaped_string contained one of
323 * the characters in @illegal_characters.
326 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
332 if (escaped_string == NULL) {
336 result = g_malloc (strlen (escaped_string) + 1);
339 for (in = escaped_string; *in != '\0'; in++) {
341 if (*in == HEX_ESCAPE) {
342 character = unescape_character (in + 1);
344 /* Check for an illegal character. We consider '\0' illegal here. */
346 || (illegal_characters != NULL
347 && strchr (illegal_characters, (char) character) != NULL)) {
353 *out++ = (char) character;
357 g_assert ((gsize) (out - result) <= strlen (escaped_string));
364 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
366 gchar *check = (gchar *) uri;
368 g_assert (uri != NULL);
369 g_assert (endptr != NULL);
371 if (g_ascii_isalpha (*check)) {
373 while (g_ascii_isalnum (*check) || *check == '+'
374 || *check == '-' || *check == '.')
382 * gst_uri_protocol_is_valid:
383 * @protocol: A string
385 * Tests if the given string is a valid protocol identifier. Protocols
386 * must consist of alphanumeric characters, '+', '-' and '.' and must
387 * start with a alphabetic character. See RFC 3986 Section 3.1.
389 * Returns: %TRUE if the string is a valid protocol identifier, %FALSE otherwise.
392 gst_uri_protocol_is_valid (const gchar * protocol)
396 g_return_val_if_fail (protocol != NULL, FALSE);
398 gst_uri_protocol_check_internal (protocol, &endptr);
400 return *endptr == '\0' && ((gsize) (endptr - protocol)) >= 2;
407 * Tests if the given string is a valid URI identifier. URIs start with a valid
408 * scheme followed by ":" and maybe a string identifying the location.
410 * Returns: %TRUE if the string is a valid URI
413 gst_uri_is_valid (const gchar * uri)
417 g_return_val_if_fail (uri != NULL, FALSE);
419 gst_uri_protocol_check_internal (uri, &endptr);
421 return *endptr == ':' && ((gsize) (endptr - uri)) >= 2;
425 * gst_uri_get_protocol:
428 * Extracts the protocol out of a given valid URI. The returned string must be
429 * freed using g_free().
431 * Returns: The protocol for this URI.
434 gst_uri_get_protocol (const gchar * uri)
438 g_return_val_if_fail (uri != NULL, NULL);
439 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
441 colon = strstr (uri, ":");
443 return g_ascii_strdown (uri, colon - uri);
447 * gst_uri_has_protocol:
449 * @protocol: a protocol string (e.g. "http")
451 * Checks if the protocol of a given valid URI matches @protocol.
453 * Returns: %TRUE if the protocol matches.
456 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
460 g_return_val_if_fail (uri != NULL, FALSE);
461 g_return_val_if_fail (protocol != NULL, FALSE);
462 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
464 colon = strstr (uri, ":");
469 return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
473 * gst_uri_get_location:
476 * Extracts the location out of a given valid URI, ie. the protocol and "://"
477 * are stripped from the URI, which means that the location returned includes
478 * the hostname if one is specified. The returned string must be freed using
481 * Free-function: g_free
483 * Returns: (transfer full): the location for this URI. Returns %NULL if the
484 * URI isn't valid. If the URI does not contain a location, an empty
485 * string is returned.
488 gst_uri_get_location (const gchar * uri)
491 gchar *unescaped = NULL;
493 g_return_val_if_fail (uri != NULL, NULL);
494 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
496 colon = strstr (uri, "://");
500 unescaped = unescape_string (colon + 3, "/");
502 /* On Windows an URI might look like file:///c:/foo/bar.txt or
503 * file:///c|/foo/bar.txt (some Netscape versions) and we want to
504 * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
505 * Can't use g_filename_from_uri() here because it will only handle the
506 * file:// protocol */
508 if (unescaped != NULL && unescaped[0] == '/' &&
509 g_ascii_isalpha (unescaped[1]) &&
510 (unescaped[2] == ':' || unescaped[2] == '|')) {
512 memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
516 GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
523 * @protocol: Protocol for URI
524 * @location: (transfer none): Location for URI
526 * Constructs a URI for a given valid protocol and location.
528 * Free-function: g_free
530 * Returns: (transfer full): a new string for this URI. Returns %NULL if the
531 * given URI protocol is not valid, or the given location is %NULL.
534 gst_uri_construct (const gchar * protocol, const gchar * location)
536 char *escaped, *proto_lowercase;
539 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
540 g_return_val_if_fail (location != NULL, NULL);
542 proto_lowercase = g_ascii_strdown (protocol, -1);
543 escaped = escape_string (location);
544 retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
546 g_free (proto_lowercase);
554 const gchar *protocol;
559 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
561 const gchar *const *protocols;
562 GstElementFactory *factory;
563 SearchEntry *entry = (SearchEntry *) search_entry;
565 if (!GST_IS_ELEMENT_FACTORY (feature))
567 factory = GST_ELEMENT_FACTORY_CAST (feature);
569 if (factory->uri_type != entry->type)
572 protocols = gst_element_factory_get_uri_protocols (factory);
574 if (protocols == NULL) {
575 g_warning ("Factory '%s' implements GstUriHandler interface but returned "
576 "no supported protocols!", gst_plugin_feature_get_name (feature));
580 while (*protocols != NULL) {
581 if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
589 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
591 return gst_plugin_feature_get_rank (second) -
592 gst_plugin_feature_get_rank (first);
596 get_element_factories_from_uri_protocol (const GstURIType type,
597 const gchar * protocol)
599 GList *possibilities;
602 g_return_val_if_fail (protocol, NULL);
605 entry.protocol = protocol;
606 possibilities = gst_registry_feature_filter (gst_registry_get (),
607 search_by_entry, FALSE, &entry);
609 return possibilities;
613 * gst_uri_protocol_is_supported:
614 * @type: Whether to check for a source or a sink
615 * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
617 * Checks if an element exists that supports the given URI protocol. Note
618 * that a positive return value does not imply that a subsequent call to
619 * gst_element_make_from_uri() is guaranteed to work.
624 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
626 GList *possibilities;
628 g_return_val_if_fail (protocol, FALSE);
630 possibilities = get_element_factories_from_uri_protocol (type, protocol);
633 g_list_free (possibilities);
640 * gst_element_make_from_uri:
641 * @type: Whether to create a source or a sink
642 * @uri: URI to create an element for
643 * @elementname: (allow-none): Name of created element, can be %NULL.
644 * @error: (allow-none): address where to store error information, or %NULL.
646 * Creates an element for handling the given URI.
648 * Returns: (transfer floating): a new element or %NULL if none could be created
651 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
652 const gchar * elementname, GError ** error)
654 GList *possibilities, *walk;
656 GstElement *ret = NULL;
658 g_return_val_if_fail (gst_is_initialized (), NULL);
659 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
660 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
661 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
663 GST_DEBUG ("type:%d, uri:%s, elementname:%s", type, uri, elementname);
665 protocol = gst_uri_get_protocol (uri);
666 possibilities = get_element_factories_from_uri_protocol (type, protocol);
668 if (!possibilities) {
669 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
671 /* The error message isn't great, but we don't expect applications to
672 * show that error to users, but call the missing plugins functions */
673 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
674 _("No URI handler for the %s protocol found"), protocol);
680 possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
681 walk = possibilities;
683 GstElementFactory *factory = walk->data;
684 GError *uri_err = NULL;
686 ret = gst_element_factory_create (factory, elementname);
688 GstURIHandler *handler = GST_URI_HANDLER (ret);
690 if (gst_uri_handler_set_uri (handler, uri, &uri_err))
693 GST_WARNING ("%s didn't accept URI '%s': %s", GST_OBJECT_NAME (ret), uri,
696 if (error != NULL && *error == NULL)
697 g_propagate_error (error, uri_err);
699 g_error_free (uri_err);
701 gst_object_unref (ret);
706 gst_plugin_feature_list_free (possibilities);
708 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
709 type == GST_URI_SINK ? "sink" : "source", uri);
711 /* if the first handler didn't work, but we found another one that works */
713 g_clear_error (error);
719 * gst_uri_handler_get_uri_type:
720 * @handler: A #GstURIHandler.
722 * Gets the type of the given URI handler
724 * Returns: the #GstURIType of the URI handler.
725 * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
728 gst_uri_handler_get_uri_type (GstURIHandler * handler)
730 GstURIHandlerInterface *iface;
733 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
735 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
736 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
737 g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
739 ret = iface->get_type (G_OBJECT_TYPE (handler));
740 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
746 * gst_uri_handler_get_protocols:
747 * @handler: A #GstURIHandler.
749 * Gets the list of protocols supported by @handler. This list may not be
752 * Returns: (transfer none) (element-type utf8) (nullable): the
753 * supported protocols. Returns %NULL if the @handler isn't
754 * implemented properly, or the @handler doesn't support any
758 gst_uri_handler_get_protocols (GstURIHandler * handler)
760 GstURIHandlerInterface *iface;
761 const gchar *const *ret;
763 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
765 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
766 g_return_val_if_fail (iface != NULL, NULL);
767 g_return_val_if_fail (iface->get_protocols != NULL, NULL);
769 ret = iface->get_protocols (G_OBJECT_TYPE (handler));
770 g_return_val_if_fail (ret != NULL, NULL);
776 * gst_uri_handler_get_uri:
777 * @handler: A #GstURIHandler
779 * Gets the currently handled URI.
781 * Returns: (transfer full) (nullable): the URI currently handled by
782 * the @handler. Returns %NULL if there are no URI currently
783 * handled. The returned string must be freed with g_free() when no
787 gst_uri_handler_get_uri (GstURIHandler * handler)
789 GstURIHandlerInterface *iface;
792 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
794 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
795 g_return_val_if_fail (iface != NULL, NULL);
796 g_return_val_if_fail (iface->get_uri != NULL, NULL);
797 ret = iface->get_uri (handler);
799 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
805 * gst_uri_handler_set_uri:
806 * @handler: A #GstURIHandler
808 * @error: (allow-none): address where to store a #GError in case of
811 * Tries to set the URI of the given handler.
813 * Returns: %TRUE if the URI was set successfully, else %FALSE.
816 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
819 GstURIHandlerInterface *iface;
823 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
824 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
825 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
827 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
828 g_return_val_if_fail (iface != NULL, FALSE);
829 g_return_val_if_fail (iface->set_uri != NULL, FALSE);
831 protocol = gst_uri_get_protocol (uri);
833 if (iface->get_protocols) {
834 const gchar *const *protocols;
835 const gchar *const *p;
836 gboolean found_protocol = FALSE;
838 protocols = iface->get_protocols (G_OBJECT_TYPE (handler));
839 if (protocols != NULL) {
840 for (p = protocols; *p != NULL; ++p) {
841 if (g_ascii_strcasecmp (protocol, *p) == 0) {
842 found_protocol = TRUE;
847 if (!found_protocol) {
848 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
849 _("URI scheme '%s' not supported"), protocol);
856 ret = iface->set_uri (handler, uri, error);
864 gst_file_utils_canonicalise_path (const gchar * path)
866 gchar **parts, **p, *clean_path;
870 GST_WARNING ("FIXME: canonicalise win32 path");
871 return g_strdup (path);
875 parts = g_strsplit (path, "/", -1);
879 if (strcmp (*p, ".") == 0) {
880 /* just move all following parts on top of this, incl. NUL terminator */
882 memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
883 /* re-check the new current part again in the next iteration */
885 } else if (strcmp (*p, "..") == 0 && p > parts) {
886 /* just move all following parts on top of the previous part, incl.
890 memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
891 /* re-check the new current part again in the next iteration */
900 num_parts = g_strv_length (parts) + 1; /* incl. terminator */
901 parts = g_renew (gchar *, parts, num_parts + 1);
902 memmove (parts + 1, parts, num_parts * sizeof (gchar *));
903 parts[0] = g_strdup ("/");
906 clean_path = g_build_filenamev (parts);
912 file_path_contains_relatives (const gchar * path)
914 return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
915 strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
916 strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
920 * gst_filename_to_uri:
921 * @filename: absolute or relative file name path
922 * @error: pointer to error, or %NULL
924 * Similar to g_filename_to_uri(), but attempts to handle relative file paths
925 * as well. Before converting @filename into an URI, it will be prefixed by
926 * the current working directory if it is a relative path, and then the path
927 * will be canonicalised so that it doesn't contain any './' or '../' segments.
929 * On Windows #filename should be in UTF-8 encoding.
932 gst_filename_to_uri (const gchar * filename, GError ** error)
934 gchar *abs_location = NULL;
935 gchar *uri, *abs_clean;
937 g_return_val_if_fail (filename != NULL, NULL);
938 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
940 if (g_path_is_absolute (filename)) {
941 if (!file_path_contains_relatives (filename)) {
942 uri = g_filename_to_uri (filename, NULL, error);
946 abs_location = g_strdup (filename);
950 cwd = g_get_current_dir ();
951 abs_location = g_build_filename (cwd, filename, NULL);
954 if (!file_path_contains_relatives (abs_location)) {
955 uri = g_filename_to_uri (abs_location, NULL, error);
960 /* path is now absolute, but contains '.' or '..' */
961 abs_clean = gst_file_utils_canonicalise_path (abs_location);
962 GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
963 uri = g_filename_to_uri (abs_clean, NULL, error);
968 g_free (abs_location);
969 GST_DEBUG ("'%s' -> '%s'", filename, uri);
973 /****************************************************************************
974 * GstUri - GstMiniObject to parse and merge URIs according to IETF RFC 3986
975 ****************************************************************************/
979 * @short_description: URI parsing and manipulation.
981 * A #GstUri object can be used to parse and split a URI string into its
982 * constituant parts. Two #GstUri objects can be joined to make a new #GstUri
983 * using the algorithm described in RFC3986.
986 /* Definition for GstUri object */
990 GstMiniObject mini_object;
1000 GST_DEFINE_MINI_OBJECT_TYPE (GstUri, gst_uri);
1002 static GstUri *_gst_uri_copy (const GstUri * uri);
1003 static void _gst_uri_free (GstUri * uri);
1004 static GstUri *_gst_uri_new (void);
1005 static GList *_remove_dot_segments (GList * path);
1007 /** private GstUri functions **/
1013 uri = GST_URI_CAST (g_slice_new0 (GstUri));
1016 gst_mini_object_init (GST_MINI_OBJECT_CAST (uri), 0, gst_uri_get_type (),
1017 (GstMiniObjectCopyFunction) _gst_uri_copy, NULL,
1018 (GstMiniObjectFreeFunction) _gst_uri_free);
1024 _gst_uri_free (GstUri * uri)
1026 g_return_if_fail (GST_IS_URI (uri));
1028 g_free (uri->scheme);
1029 g_free (uri->userinfo);
1031 g_list_free_full (uri->path, g_free);
1033 g_hash_table_unref (uri->query);
1034 g_free (uri->fragment);
1036 g_slice_free1 (sizeof (*uri), uri);
1040 _gst_uri_copy_query_table (GHashTable * orig)
1042 GHashTable *new = NULL;
1045 GHashTableIter iter;
1046 gpointer key, value;
1047 new = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1048 g_hash_table_iter_init (&iter, orig);
1049 while (g_hash_table_iter_next (&iter, &key, &value)) {
1050 g_hash_table_insert (new, g_strdup (key), g_strdup (value));
1058 _gst_uri_copy (const GstUri * orig_uri)
1062 g_return_val_if_fail (GST_IS_URI (orig_uri), NULL);
1064 new_uri = _gst_uri_new ();
1067 new_uri->scheme = g_strdup (orig_uri->scheme);
1068 new_uri->userinfo = g_strdup (orig_uri->userinfo);
1069 new_uri->host = g_strdup (orig_uri->host);
1070 new_uri->port = orig_uri->port;
1071 new_uri->path = g_list_copy_deep (orig_uri->path, (GCopyFunc) g_strdup,
1073 new_uri->query = _gst_uri_copy_query_table (orig_uri->query);
1074 new_uri->fragment = g_strdup (orig_uri->fragment);
1081 * _gst_uri_compare_lists:
1083 * Compare two lists for equality. This compares the two lists, item for item,
1084 * comparing items in the same position in the two lists. If @first is
1085 * considered less than @second the result will be negative. If @first is
1086 * considered to be more than @second then the result will be positive. If the
1087 * lists are considered to be equal then the result will be 0. If two lists
1088 * have the same items, but one list is shorter than the other, then the
1089 * shorter list is considered to be less than the longer list.
1092 _gst_uri_compare_lists (GList * first, GList * second, GCompareFunc cmp_fn)
1097 for (itr1 = first, itr2 = second;
1098 itr1 != NULL || itr2 != NULL; itr1 = itr1->next, itr2 = itr2->next) {
1103 result = cmp_fn (itr1->data, itr2->data);
1112 _GST_URI_NORMALIZE_LOWERCASE = 1,
1113 _GST_URI_NORMALIZE_UPPERCASE = 2
1114 } _GstUriNormalizations;
1117 * Find the first character that hasn't been normalized according to the @flags.
1120 _gst_uri_first_non_normalized_char (gchar * str, guint flags)
1127 for (pos = str; *pos; pos++) {
1128 if ((flags & _GST_URI_NORMALIZE_UPPERCASE) && g_ascii_islower (*pos))
1130 if ((flags & _GST_URI_NORMALIZE_LOWERCASE) && g_ascii_isupper (*pos))
1137 _gst_uri_normalize_lowercase (gchar * str)
1140 gboolean ret = FALSE;
1142 for (pos = _gst_uri_first_non_normalized_char (str,
1143 _GST_URI_NORMALIZE_LOWERCASE);
1145 pos = _gst_uri_first_non_normalized_char (pos + 1,
1146 _GST_URI_NORMALIZE_LOWERCASE)) {
1147 *pos = g_ascii_tolower (*pos);
1154 #define _gst_uri_normalize_scheme _gst_uri_normalize_lowercase
1155 #define _gst_uri_normalize_hostname _gst_uri_normalize_lowercase
1158 _gst_uri_normalize_path (GList ** path)
1162 new_path = _remove_dot_segments (*path);
1163 if (_gst_uri_compare_lists (new_path, *path, (GCompareFunc) g_strcmp0) != 0) {
1164 g_list_free_full (*path, g_free);
1168 g_list_free_full (new_path, g_free);
1174 _gst_uri_normalize_str_noop (gchar * str)
1180 _gst_uri_normalize_table_noop (GHashTable * table)
1185 #define _gst_uri_normalize_userinfo _gst_uri_normalize_str_noop
1186 #define _gst_uri_normalize_query _gst_uri_normalize_table_noop
1187 #define _gst_uri_normalize_fragment _gst_uri_normalize_str_noop
1189 /** RFC 3986 functions **/
1192 _merge (GList * base, GList * path)
1194 GList *ret, *path_copy, *last;
1196 path_copy = g_list_copy_deep (path, (GCopyFunc) g_strdup, NULL);
1197 /* if base is NULL make path absolute */
1199 if (path_copy != NULL && path_copy->data != NULL) {
1200 path_copy = g_list_prepend (path_copy, NULL);
1205 ret = g_list_copy_deep (base, (GCopyFunc) g_strdup, NULL);
1206 last = g_list_last (ret);
1207 ret = g_list_remove_link (ret, last);
1208 g_list_free_full (last, g_free);
1209 ret = g_list_concat (ret, path_copy);
1215 _remove_dot_segments (GList * path)
1217 GList *out, *elem, *next;
1222 out = g_list_copy_deep (path, (GCopyFunc) g_strdup, NULL);
1224 for (elem = out; elem; elem = next) {
1226 if (elem->data == NULL && elem != out && next != NULL) {
1227 out = g_list_delete_link (out, elem);
1228 } else if (g_strcmp0 (elem->data, ".") == 0) {
1229 g_free (elem->data);
1230 out = g_list_delete_link (out, elem);
1231 } else if (g_strcmp0 (elem->data, "..") == 0) {
1232 GList *prev = g_list_previous (elem);
1233 if (prev && (prev != out || prev->data != NULL)) {
1234 g_free (prev->data);
1235 out = g_list_delete_link (out, prev);
1237 g_free (elem->data);
1238 out = g_list_delete_link (out, elem);
1246 _gst_uri_escape_userinfo (const gchar * userinfo)
1248 return g_uri_escape_string (userinfo,
1249 G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, FALSE);
1253 _gst_uri_escape_host (const gchar * host)
1255 return g_uri_escape_string (host,
1256 G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, FALSE);
1260 _gst_uri_escape_path_segment (const gchar * segment)
1262 return g_uri_escape_string (segment,
1263 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, FALSE);
1267 _gst_uri_escape_http_query_element (const gchar * element)
1271 ret = g_uri_escape_string (element, "!$'()*,;:@/? ", FALSE);
1272 for (c = ret; *c; c++)
1279 _gst_uri_escape_fragment (const gchar * fragment)
1281 return g_uri_escape_string (fragment,
1282 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH "?", FALSE);
1286 _gst_uri_string_to_list (const gchar * str, const gchar * sep, gboolean convert,
1289 GList *new_list = NULL;
1292 guint pct_sep_len = 0;
1296 if (convert && !unescape) {
1297 pct_sep = g_strdup_printf ("%%%2.2X", (guint) (*sep));
1301 split_str = g_strsplit (str, sep, -1);
1304 for (next_elem = split_str; *next_elem; next_elem += 1) {
1305 gchar *elem = *next_elem;
1306 if (*elem == '\0') {
1307 new_list = g_list_append (new_list, NULL);
1309 if (convert && !unescape) {
1311 for (next_sep = strcasestr (elem, pct_sep); next_sep;
1312 next_sep = strcasestr (next_sep + 1, pct_sep)) {
1314 memmove (next_sep + 1, next_sep + pct_sep_len,
1315 strlen (next_sep + pct_sep_len) + 1);
1319 *next_elem = g_uri_unescape_string (elem, NULL);
1323 new_list = g_list_append (new_list, g_strdup (elem));
1327 g_strfreev (split_str);
1328 if (convert && !unescape)
1336 _gst_uri_string_to_table (const gchar * str, const gchar * part_sep,
1337 const gchar * kv_sep, gboolean convert, gboolean unescape)
1339 GHashTable *new_table = NULL;
1342 gchar *pct_part_sep = NULL, *pct_kv_sep = NULL;
1343 gchar **split_parts;
1345 new_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1347 if (convert && !unescape) {
1348 pct_part_sep = g_strdup_printf ("%%%2.2X", (guint) (*part_sep));
1349 pct_kv_sep = g_strdup_printf ("%%%2.2X", (guint) (*kv_sep));
1352 split_parts = g_strsplit (str, part_sep, -1);
1355 for (next_part = split_parts; *next_part; next_part += 1) {
1356 gchar *part = *next_part;
1359 /* if we are converting percent encoded versions of separators then
1360 * substitute the part separator now. */
1361 if (convert && !unescape) {
1363 for (next_sep = strcasestr (part, pct_part_sep); next_sep;
1364 next_sep = strcasestr (next_sep + 1, pct_part_sep)) {
1365 *next_sep = *part_sep;
1366 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1369 /* find the key/value separator within the part */
1370 kv_sep_pos = g_strstr_len (part, -1, kv_sep);
1371 if (kv_sep_pos == NULL) {
1373 key = g_uri_unescape_string (part, NULL);
1375 key = g_strdup (part);
1380 key = g_uri_unescape_segment (part, kv_sep_pos, NULL);
1381 value = g_uri_unescape_string (kv_sep_pos + 1, NULL);
1383 key = g_strndup (part, kv_sep_pos - part);
1384 value = g_strdup (kv_sep_pos + 1);
1387 /* if we are converting percent encoded versions of separators then
1388 * substitute the key/value separator in both key and value now. */
1389 if (convert && !unescape) {
1391 for (next_sep = strcasestr (key, pct_kv_sep); next_sep;
1392 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1393 *next_sep = *kv_sep;
1394 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1397 for (next_sep = strcasestr (value, pct_kv_sep); next_sep;
1398 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1399 *next_sep = *kv_sep;
1400 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1404 /* add value to the table */
1405 g_hash_table_insert (new_table, key, value);
1409 g_strfreev (split_parts);
1410 if (convert && !unescape) {
1411 g_free (pct_part_sep);
1412 g_free (pct_kv_sep);
1421 * Method definitions.
1426 * @scheme: (nullable): The scheme for the new URI.
1427 * @userinfo: (nullable): The user-info for the new URI.
1428 * @host: (nullable): The host name for the new URI.
1429 * @port: The port number for the new URI or %GST_URI_NO_PORT.
1430 * @path: (nullable): The path for the new URI with '/' separating path
1432 * @query: (nullable): The query string for the new URI with '&' separating
1433 * query elements. Elements containing '&' characters
1434 * should encode them as "%26".
1435 * @fragment: (nullable): The fragment name for the new URI.
1437 * Creates a new #GstUri object with the given URI parts. The path and query
1438 * strings will be broken down into their elements. All strings should not be
1439 * escaped except where indicated.
1441 * Returns: (transfer full): A new #GstUri object.
1446 gst_uri_new (const gchar * scheme, const gchar * userinfo, const gchar * host,
1447 guint port, const gchar * path, const gchar * query, const gchar * fragment)
1451 new_uri = _gst_uri_new ();
1453 new_uri->scheme = g_strdup (scheme);
1454 new_uri->userinfo = g_strdup (userinfo);
1455 new_uri->host = g_strdup (host);
1456 new_uri->port = port;
1457 new_uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
1458 new_uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, FALSE);
1459 new_uri->fragment = g_strdup (fragment);
1466 * gst_uri_new_with_base:
1467 * @base: (transfer none)(nullable): The base URI to join the new URI to.
1468 * @scheme: (nullable): The scheme for the new URI.
1469 * @userinfo: (nullable): The user-info for the new URI.
1470 * @host: (nullable): The host name for the new URI.
1471 * @port: The port number for the new URI or %GST_URI_NO_PORT.
1472 * @path: (nullable): The path for the new URI with '/' separating path
1474 * @query: (nullable): The query string for the new URI with '&' separating
1475 * query elements. Elements containing '&' characters
1476 * should encode them as "%26".
1477 * @fragment: (nullable): The fragment name for the new URI.
1479 * Like gst_uri_new(), but joins the new URI onto a base URI.
1481 * Returns: (transfer full): The new URI joined onto @base.
1486 gst_uri_new_with_base (GstUri * base, const gchar * scheme,
1487 const gchar * userinfo, const gchar * host, guint port, const gchar * path,
1488 const gchar * query, const gchar * fragment)
1490 GstUri *new_rel_uri;
1493 g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1495 new_rel_uri = gst_uri_new (scheme, userinfo, host, port, path, query,
1497 new_uri = gst_uri_join (base, new_rel_uri);
1498 gst_uri_unref (new_rel_uri);
1504 * gst_uri_from_string:
1505 * @uri: The URI string to parse.
1507 * Parses a URI string into a new #GstUri object.
1509 * Returns: (transfer full): A new #GstUri object.
1514 gst_uri_from_string (const gchar * uri)
1518 uri_obj = _gst_uri_new ();
1520 if (uri_obj && uri != NULL) {
1522 if (g_ascii_isalpha (uri[i])) {
1523 /* find end of scheme name */
1525 while (g_ascii_isalnum (uri[i]) || uri[i] == '+' || uri[i] == '-' ||
1529 if (i > 0 && uri[i] == ':') {
1531 uri_obj->scheme = g_strndup (uri, i);
1534 if (uri[0] == '/' && uri[1] == '/') {
1535 const gchar *eoa, *eoui, *eoh;
1536 /* get authority [userinfo@]host[:port] */
1538 /* find end of authority */
1539 eoa = strchr (uri, '/');
1541 eoa = uri + strlen (uri);
1542 /* find end of userinfo */
1543 eoui = strchr (uri, '@');
1544 if (eoui != NULL && eoui < eoa) {
1545 uri_obj->userinfo = g_uri_unescape_segment (uri, eoui, NULL);
1548 /* find end of host */
1549 if (uri[0] == '[') {
1550 eoh = strchr (uri, ']');
1551 if (eoh == NULL || eoh >= eoa)
1554 eoh = strchr (uri, ':');
1555 if (eoh == NULL || eoh >= eoa)
1560 uri_obj->host = g_uri_unescape_segment (uri, eoh + 1, NULL);
1563 /* if port number is malformed, do best effort and concat string */
1564 if (uri[0] != ':' || strspn (uri + 1, "0123456789") != eoa - uri - 1) {
1565 gchar *tmp = uri_obj->host;
1566 uri_obj->host = g_malloc (strlen (uri_obj->host) + eoa - uri + 1);
1567 g_strlcpy (g_stpcpy (uri_obj->host, tmp), uri, eoa - uri + 1);
1570 /* otherwise treat port as unsigned decimal number */
1573 uri_obj->port = uri_obj->port * 10 + g_ascii_digit_value (*uri);
1580 if (uri != NULL && uri[0] != '\0') {
1583 len = strcspn (uri, "?#");
1584 if (uri[len] == '\0') {
1585 uri_obj->path = _gst_uri_string_to_list (uri, "/", FALSE, TRUE);
1589 gchar *path_str = g_strndup (uri, len);
1590 uri_obj->path = _gst_uri_string_to_list (path_str, "/", FALSE, TRUE);
1596 if (uri != NULL && uri[0] == '?') {
1599 eoq = strchr (++uri, '#');
1601 uri_obj->query = _gst_uri_string_to_table (uri, "&", "=", TRUE, TRUE);
1605 gchar *query_str = g_strndup (uri, eoq - uri);
1606 uri_obj->query = _gst_uri_string_to_table (query_str, "&", "=", TRUE,
1613 if (uri != NULL && uri[0] == '#') {
1614 uri_obj->fragment = g_uri_unescape_string (uri + 1, NULL);
1622 * gst_uri_from_string_with_base:
1623 * @base: (transfer none)(nullable): The base URI to join the new URI with.
1624 * @uri: The URI string to parse.
1626 * Like gst_uri_from_string() but also joins with a base URI.
1628 * Returns: (transfer full): A new #GstUri object.
1633 gst_uri_from_string_with_base (GstUri * base, const gchar * uri)
1635 GstUri *new_rel_uri;
1638 g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1640 new_rel_uri = gst_uri_from_string (uri);
1641 new_uri = gst_uri_join (base, new_rel_uri);
1642 gst_uri_unref (new_rel_uri);
1649 * @first: First #GstUri to compare.
1650 * @second: Second #GstUri to compare.
1652 * Compares two #GstUri objects to see if they represent the same normalized
1655 * Returns: %TRUE if the normalized versions of the two URI's would be equal.
1660 gst_uri_equal (const GstUri * first, const GstUri * second)
1662 gchar *first_norm = NULL, *second_norm = NULL;
1663 GList *first_norm_list = NULL, *second_norm_list = NULL;
1664 const gchar *first_cmp, *second_cmp;
1665 GHashTableIter table_iter;
1666 gpointer key, value;
1669 g_return_val_if_fail ((first == NULL || GST_IS_URI (first)) &&
1670 (second == NULL || GST_IS_URI (second)), FALSE);
1672 if (first == second)
1675 if (first == NULL || second == NULL)
1678 if (first->port != second->port)
1681 /* work out a version of field value (normalized or not) to compare.
1682 * first_cmp, second_cmp will be the values to compare later.
1683 * first_norm, second_norm will be non-NULL if normalized versions are used,
1684 * and need to be freed later.
1686 #define GST_URI_NORMALIZED_FIELD(pos, field, norm_fn, flags) \
1687 pos##_cmp = pos->field; \
1688 if (_gst_uri_first_non_normalized_char ((gchar*)pos##_cmp, flags) != NULL) { \
1689 pos##_norm = g_strdup (pos##_cmp); \
1690 norm_fn (pos##_norm); \
1691 pos##_cmp = pos##_norm; \
1694 /* compare two string values, normalizing if needed */
1695 #define GST_URI_NORMALIZED_CMP_STR(field, norm_fn, flags) \
1696 GST_URI_NORMALIZED_FIELD (first, field, norm_fn, flags) \
1697 GST_URI_NORMALIZED_FIELD (second, field, norm_fn, flags) \
1698 result = g_strcmp0 (first_cmp, second_cmp); \
1699 g_free (first_norm); \
1700 first_norm = NULL; \
1701 g_free (second_norm); \
1702 second_norm = NULL; \
1703 if (result != 0) return FALSE
1705 /* compare two string values */
1706 #define GST_URI_CMP_STR(field) \
1707 if (g_strcmp0 (first->field, second->field) != 0) return FALSE
1709 /* compare two GLists, normalize lists if needed before comparison */
1710 #define GST_URI_NORMALIZED_CMP_LIST(field, norm_fn, copy_fn, cmp_fn, free_fn) \
1711 first_norm_list = g_list_copy_deep (first->field, (GCopyFunc) copy_fn, NULL); \
1712 norm_fn (&first_norm_list); \
1713 second_norm_list = g_list_copy_deep (second->field, (GCopyFunc) copy_fn, NULL); \
1714 norm_fn (&second_norm_list); \
1715 result = _gst_uri_compare_lists (first_norm_list, second_norm_list, (GCompareFunc) cmp_fn); \
1716 g_list_free_full (first_norm_list, free_fn); \
1717 g_list_free_full (second_norm_list, free_fn); \
1718 if (result != 0) return FALSE
1720 GST_URI_CMP_STR (userinfo);
1722 GST_URI_CMP_STR (fragment);
1724 GST_URI_NORMALIZED_CMP_STR (scheme, _gst_uri_normalize_scheme,
1725 _GST_URI_NORMALIZE_LOWERCASE);
1727 GST_URI_NORMALIZED_CMP_STR (host, _gst_uri_normalize_hostname,
1728 _GST_URI_NORMALIZE_LOWERCASE);
1730 GST_URI_NORMALIZED_CMP_LIST (path, _gst_uri_normalize_path, g_strdup,
1733 if (first->query == NULL && second->query != NULL)
1735 if (first->query != NULL && second->query == NULL)
1737 if (first->query != NULL) {
1738 if (g_hash_table_size (first->query) != g_hash_table_size (second->query))
1741 g_hash_table_iter_init (&table_iter, first->query);
1742 while (g_hash_table_iter_next (&table_iter, &key, &value)) {
1743 if (!g_hash_table_contains (second->query, key))
1745 result = g_strcmp0 (g_hash_table_lookup (second->query, key), value);
1750 #undef GST_URI_NORMALIZED_CMP_STR
1751 #undef GST_URI_CMP_STR
1752 #undef GST_URI_NORMALIZED_CMP_LIST
1753 #undef GST_URI_NORMALIZED_FIELD
1760 * @base_uri: (transfer none)(nullable): The base URI to join another to.
1761 * @ref_uri: (transfer none)(nullable): The reference URI to join onto the
1764 * Join a reference URI onto a base URI using the method from RFC 3986.
1765 * If either URI is %NULL then the other URI will be returned with the ref count
1768 * Returns: (transfer full): A #GstUri which represents the base with the
1769 * reference URI joined on.
1774 gst_uri_join (GstUri * base_uri, GstUri * ref_uri)
1776 const gchar *r_scheme;
1779 g_return_val_if_fail ((base_uri == NULL || GST_IS_URI (base_uri)) &&
1780 (ref_uri == NULL || GST_IS_URI (ref_uri)), NULL);
1782 if (base_uri == NULL && ref_uri == NULL)
1784 if (base_uri == NULL) {
1785 g_return_val_if_fail (GST_IS_URI (ref_uri), NULL);
1786 return gst_uri_ref (ref_uri);
1788 if (ref_uri == NULL) {
1789 g_return_val_if_fail (GST_IS_URI (base_uri), NULL);
1790 return gst_uri_ref (base_uri);
1793 g_return_val_if_fail (GST_IS_URI (base_uri) && GST_IS_URI (ref_uri), NULL);
1795 t = _gst_uri_new ();
1800 /* process according to RFC3986 */
1801 r_scheme = ref_uri->scheme;
1802 if (r_scheme != NULL && g_strcmp0 (base_uri->scheme, r_scheme) == 0) {
1805 if (r_scheme != NULL) {
1806 t->scheme = g_strdup (r_scheme);
1807 t->userinfo = g_strdup (ref_uri->userinfo);
1808 t->host = g_strdup (ref_uri->host);
1809 t->port = ref_uri->port;
1810 t->path = _remove_dot_segments (ref_uri->path);
1811 t->query = _gst_uri_copy_query_table (ref_uri->query);
1813 if (ref_uri->host != NULL) {
1814 t->userinfo = g_strdup (ref_uri->userinfo);
1815 t->host = g_strdup (ref_uri->host);
1816 t->port = ref_uri->port;
1817 t->path = _remove_dot_segments (ref_uri->path);
1818 t->query = _gst_uri_copy_query_table (ref_uri->query);
1820 if (ref_uri->path == NULL) {
1821 t->path = g_list_copy_deep (base_uri->path, (GCopyFunc) g_strdup, NULL);
1822 if (ref_uri->query != NULL)
1823 t->query = _gst_uri_copy_query_table (ref_uri->query);
1825 t->query = _gst_uri_copy_query_table (base_uri->query);
1827 if (ref_uri->path->data == NULL)
1828 t->path = _remove_dot_segments (ref_uri->path);
1830 GList *mrgd = _merge (base_uri->path, ref_uri->path);
1831 t->path = _remove_dot_segments (mrgd);
1832 g_list_free_full (mrgd, g_free);
1834 t->query = _gst_uri_copy_query_table (ref_uri->query);
1836 t->userinfo = g_strdup (base_uri->userinfo);
1837 t->host = g_strdup (base_uri->host);
1838 t->port = base_uri->port;
1840 t->scheme = g_strdup (base_uri->scheme);
1842 t->fragment = g_strdup (ref_uri->fragment);
1848 * gst_uri_join_strings:
1849 * @base_uri: The percent-encoded base URI.
1850 * @ref_uri: The percent-encoded reference URI to join to the @base_uri.
1852 * This is a convenience function to join two URI strings and return the result.
1853 * The returned string should be g_free()'d after use.
1855 * Returns: (transfer full): A string representing the percent-encoded join of
1861 gst_uri_join_strings (const gchar * base_uri, const gchar * ref_uri)
1863 GstUri *base, *result;
1866 base = gst_uri_from_string (base_uri);
1867 result = gst_uri_from_string_with_base (base, ref_uri);
1868 result_uri = gst_uri_to_string (result);
1869 gst_uri_unref (base);
1870 gst_uri_unref (result);
1876 * gst_uri_is_writable:
1877 * @uri: The #GstUri object to test.
1879 * Check if it is safe to write to this #GstUri.
1881 * Check if the refcount of @uri is exactly 1, meaning that no other
1882 * reference exists to the #GstUri and that the #GstUri is therefore writable.
1884 * Modification of a #GstUri should only be done after verifying that it is
1887 * Returns: %TRUE if it is safe to write to the object.
1892 gst_uri_is_writable (const GstUri * uri)
1894 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
1895 return gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (uri));
1899 * gst_uri_make_writable:
1900 * @uri: (transfer full): The #GstUri object to make writable.
1902 * Make the #GstUri writable.
1904 * Checks if @uri is writable, and if so the original object is returned. If
1905 * not, then a writable copy is made and returned. This gives away the
1906 * reference to @uri and returns a reference to the new #GstUri.
1907 * If @uri is %NULL then %NULL is returned.
1909 * Returns: (transfer full): A writable version of @uri.
1914 gst_uri_make_writable (GstUri * uri)
1916 g_return_val_if_fail (GST_IS_URI (uri), NULL);
1918 GST_URI_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (uri)));
1922 * gst_uri_to_string:
1923 * @uri: This #GstUri to convert to a string.
1925 * Convert the URI to a string.
1927 * Returns the URI as held in this object as a gchar* %NUL terminated string.
1928 * The caller should g_free() the string once they are finished with it.
1929 * The string is put together as described in RFC 3986.
1931 * Returns: (transfer full): The string version of the URI.
1936 gst_uri_to_string (const GstUri * uri)
1941 g_return_val_if_fail (GST_IS_URI (uri), NULL);
1943 uri_str = g_string_new (NULL);
1945 if (uri->scheme != NULL)
1946 g_string_append_printf (uri_str, "%s:", uri->scheme);
1948 if (uri->userinfo != NULL || uri->host != NULL ||
1949 uri->port != GST_URI_NO_PORT)
1950 g_string_append (uri_str, "//");
1952 if (uri->userinfo != NULL) {
1953 escaped = _gst_uri_escape_userinfo (uri->userinfo);
1954 g_string_append_printf (uri_str, "%s@", escaped);
1958 if (uri->host != NULL) {
1959 escaped = _gst_uri_escape_host (uri->host);
1960 g_string_append (uri_str, escaped);
1964 if (uri->port != GST_URI_NO_PORT)
1965 g_string_append_printf (uri_str, ":%u", uri->port);
1967 if (uri->path != NULL) {
1968 escaped = gst_uri_get_path_string (uri);
1969 g_string_append (uri_str, escaped);
1974 g_string_append (uri_str, "?");
1975 escaped = gst_uri_get_query_string (uri);
1976 g_string_append (uri_str, escaped);
1980 if (uri->fragment != NULL) {
1981 escaped = _gst_uri_escape_fragment (uri->fragment);
1982 g_string_append_printf (uri_str, "#%s", escaped);
1986 return g_string_free (uri_str, FALSE);
1990 * gst_uri_is_normalized:
1991 * @uri: The #GstUri to test to see if it is normalized.
1993 * Tests the @uri to see if it is normalized. A %NULL @uri is considered to be
1996 * Returns: TRUE if the URI is normalized or is %NULL.
2001 gst_uri_is_normalized (const GstUri * uri)
2009 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2011 /* check for non-normalized characters in uri parts */
2012 if (_gst_uri_first_non_normalized_char (uri->scheme,
2013 _GST_URI_NORMALIZE_LOWERCASE) != NULL ||
2014 /*_gst_uri_first_non_normalized_char (uri->userinfo,
2015 _GST_URI_NORMALIZE_PERCENTAGES) != NULL || */
2016 _gst_uri_first_non_normalized_char (uri->host,
2017 _GST_URI_NORMALIZE_LOWERCASE /*| _GST_URI_NORMALIZE_PERCENTAGES */ )
2019 /*|| _gst_uri_first_non_normalized_char (uri->path,
2020 _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2021 || _gst_uri_first_non_normalized_char (uri->query,
2022 _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2023 || _gst_uri_first_non_normalized_char (uri->fragment,
2024 _GST_URI_NORMALIZE_PERCENTAGES) != NULL */ )
2027 /* also check path has had dot segments removed */
2028 new_path = _remove_dot_segments (uri->path);
2030 (_gst_uri_compare_lists (new_path, uri->path,
2031 (GCompareFunc) g_strcmp0) == 0);
2032 g_list_free_full (new_path, g_free);
2037 * gst_uri_normalize:
2038 * @uri: (transfer none): The #GstUri to normalize.
2040 * Normalization will remove extra path segments ("." and "..") from the URI. It
2041 * will also convert the scheme and host name to lower case and any
2042 * percent-encoded values to uppercase.
2044 * The #GstUri object must be writable. Check with gst_uri_is_writable() or use
2045 * gst_uri_make_writable() first.
2047 * Returns: TRUE if the URI was modified.
2052 gst_uri_normalize (GstUri * uri)
2054 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2056 return _gst_uri_normalize_scheme (uri->scheme) |
2057 _gst_uri_normalize_userinfo (uri->userinfo) |
2058 _gst_uri_normalize_hostname (uri->host) |
2059 _gst_uri_normalize_path (&uri->path) |
2060 _gst_uri_normalize_query (uri->query) |
2061 _gst_uri_normalize_fragment (uri->fragment);
2065 * gst_uri_get_scheme:
2066 * @uri: (nullable): This #GstUri object.
2068 * Get the scheme name from the URI or %NULL if it doesn't exist.
2069 * If @uri is %NULL then returns %NULL.
2071 * Returns: The scheme from the #GstUri object or %NULL.
2074 gst_uri_get_scheme (const GstUri * uri)
2076 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2077 return (uri ? uri->scheme : NULL);
2081 * gst_uri_set_scheme:
2082 * @uri: (transfer none)(nullable): The #GstUri to modify.
2083 * @scheme: The new scheme to set or %NULL to unset the scheme.
2085 * Set or unset the scheme for the URI.
2087 * Returns: %TRUE if the scheme was set/unset successfully.
2092 gst_uri_set_scheme (GstUri * uri, const gchar * scheme)
2095 return scheme == NULL;
2096 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2098 g_free (uri->scheme);
2099 uri->scheme = g_strdup (scheme);
2105 * gst_uri_get_userinfo:
2106 * @uri: (nullable): This #GstUri object.
2108 * Get the userinfo (usually in the form "username:password") from the URI
2109 * or %NULL if it doesn't exist. If @uri is %NULL then returns %NULL.
2111 * Returns: The userinfo from the #GstUri object or %NULL.
2116 gst_uri_get_userinfo (const GstUri * uri)
2118 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2119 return (uri ? uri->userinfo : NULL);
2123 * gst_uri_set_userinfo:
2124 * @uri: (transfer none)(nullable): The #GstUri to modify.
2125 * @userinfo: The new user-information string to set or %NULL to unset.
2127 * Set or unset the user information for the URI.
2129 * Returns: %TRUE if the user information was set/unset successfully.
2134 gst_uri_set_userinfo (GstUri * uri, const gchar * userinfo)
2137 return userinfo == NULL;
2138 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2140 g_free (uri->userinfo);
2141 uri->userinfo = g_strdup (userinfo);
2148 * @uri: (nullable): This #GstUri object.
2150 * Get the host name from the URI or %NULL if it doesn't exist.
2151 * If @uri is %NULL then returns %NULL.
2153 * Returns: The host name from the #GstUri object or %NULL.
2158 gst_uri_get_host (const GstUri * uri)
2160 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2161 return (uri ? uri->host : NULL);
2166 * @uri: (transfer none)(nullable): The #GstUri to modify.
2167 * @host: The new host string to set or %NULL to unset.
2169 * Set or unset the host for the URI.
2171 * Returns: %TRUE if the host was set/unset successfully.
2176 gst_uri_set_host (GstUri * uri, const gchar * host)
2179 return host == NULL;
2180 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2183 uri->host = g_strdup (host);
2190 * @uri: (nullable): This #GstUri object.
2192 * Get the port number from the URI or %GST_URI_NO_PORT if it doesn't exist.
2193 * If @uri is %NULL then returns %GST_URI_NO_PORT.
2195 * Returns: The port number from the #GstUri object or %GST_URI_NO_PORT.
2200 gst_uri_get_port (const GstUri * uri)
2202 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), GST_URI_NO_PORT);
2203 return (uri ? uri->port : GST_URI_NO_PORT);
2208 * @uri: (transfer none)(nullable): The #GstUri to modify.
2209 * @port: The new port number to set or %GST_URI_NO_PORT to unset.
2211 * Set or unset the port number for the URI.
2213 * Returns: %TRUE if the port number was set/unset successfully.
2218 gst_uri_set_port (GstUri * uri, guint port)
2221 return port == GST_URI_NO_PORT;
2222 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2231 * @uri: The #GstUri to get the path from.
2233 * Extract the path string from the URI object.
2235 * Returns: (transfer full): The path from the URI. Once finished with the
2236 * string should be g_free()'d.
2241 gst_uri_get_path (const GstUri * uri)
2243 GList *path_segment;
2244 const gchar *sep = "";
2249 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2253 ret = g_string_new (NULL);
2255 for (path_segment = uri->path; path_segment;
2256 path_segment = path_segment->next) {
2257 g_string_append (ret, sep);
2258 if (path_segment->data) {
2259 g_string_append (ret, path_segment->data);
2264 return g_string_free (ret, FALSE);
2269 * @uri: (transfer none)(nullable): The #GstUri to modify.
2270 * @path: The new path to set with path segments separated by '/', or use %NULL
2271 * to unset the path.
2273 * Sets or unsets the path in the URI.
2275 * Returns: %TRUE if the path was set successfully.
2280 gst_uri_set_path (GstUri * uri, const gchar * path)
2283 return path == NULL;
2284 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2286 g_list_free_full (uri->path, g_free);
2287 uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
2293 * gst_uri_get_path_string:
2294 * @uri: The #GstUri to get the path from.
2296 * Extract the path string from the URI object as a percent encoded URI path.
2298 * Returns: (transfer full): The path from the URI. Once finished with the
2299 * string should be g_free()'d.
2304 gst_uri_get_path_string (const GstUri * uri)
2306 GList *path_segment;
2307 const gchar *sep = "";
2313 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2317 ret = g_string_new (NULL);
2319 for (path_segment = uri->path; path_segment;
2320 path_segment = path_segment->next) {
2321 g_string_append (ret, sep);
2322 if (path_segment->data) {
2323 escaped = _gst_uri_escape_path_segment (path_segment->data);
2324 g_string_append (ret, escaped);
2330 return g_string_free (ret, FALSE);
2334 * gst_uri_set_path_string:
2335 * @uri: (transfer none)(nullable): The #GstUri to modify.
2336 * @path: The new percent encoded path to set with path segments separated by
2337 * '/', or use %NULL to unset the path.
2339 * Sets or unsets the path in the URI.
2341 * Returns: %TRUE if the path was set successfully.
2346 gst_uri_set_path_string (GstUri * uri, const gchar * path)
2349 return path == NULL;
2350 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2352 g_list_free_full (uri->path, g_free);
2353 uri->path = _gst_uri_string_to_list (path, "/", FALSE, TRUE);
2358 * gst_uri_get_path_segments:
2359 * @uri: (nullable): The #GstUri to get the path from.
2361 * Get a list of path segments from the URI.
2363 * Returns: (transfer full)(element-type gchar*): A #GList of path segment
2364 * strings or %NULL if no path segments are available. Free the list
2365 * when no longer needed with g_list_free_full(list, g_free).
2370 gst_uri_get_path_segments (const GstUri * uri)
2374 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2377 ret = g_list_copy_deep (uri->path, (GCopyFunc) g_strdup, NULL);
2384 * gst_uri_set_path_segments:
2385 * @uri: (transfer none)(nullable): The #GstUri to modify.
2386 * @path_segments: (transfer full)(nullable)(element-type gchar*): The new
2389 * Replace the path segments list in the URI.
2391 * Returns: %TRUE if the path segments were set successfully.
2396 gst_uri_set_path_segments (GstUri * uri, GList * path_segments)
2398 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), FALSE);
2402 g_list_free_full (path_segments, g_free);
2403 return path_segments == NULL;
2406 g_return_val_if_fail (gst_uri_is_writable (uri), FALSE);
2408 g_list_free_full (uri->path, g_free);
2409 uri->path = path_segments;
2414 * gst_uri_append_path:
2415 * @uri: (transfer none)(nullable): The #GstUri to modify.
2416 * @relative_path: Relative path to append to the end of the current path.
2418 * Append a path onto the end of the path in the URI. The path is not
2419 * normalized, call #gst_uri_normalize() to normalize the path.
2421 * Returns: %TRUE if the path was appended successfully.
2426 gst_uri_append_path (GstUri * uri, const gchar * relative_path)
2428 GList *rel_path_list;
2431 return relative_path == NULL;
2432 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2437 GList *last_elem = g_list_last (uri->path);
2438 if (last_elem->data == NULL) {
2439 uri->path = g_list_delete_link (uri->path, last_elem);
2442 rel_path_list = _gst_uri_string_to_list (relative_path, "/", FALSE, FALSE);
2443 /* if path was absolute, make it relative by removing initial NULL element */
2444 if (rel_path_list && rel_path_list->data == NULL) {
2445 rel_path_list = g_list_delete_link (rel_path_list, rel_path_list);
2447 uri->path = g_list_concat (uri->path, rel_path_list);
2452 * gst_uri_append_path_segment:
2453 * @uri: (transfer none)(nullable): The #GstUri to modify.
2454 * @path_segment: The path segment string to append to the URI path.
2456 * Append a single path segment onto the end of the URI path.
2458 * Returns: %TRUE if the path was appended successfully.
2463 gst_uri_append_path_segment (GstUri * uri, const gchar * path_segment)
2466 return path_segment == NULL;
2467 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2471 /* if base path ends in a directory (i.e. last element is NULL), remove it */
2472 if (uri->path && g_list_last (uri->path)->data == NULL) {
2473 uri->path = g_list_delete_link (uri->path, g_list_last (uri->path));
2475 uri->path = g_list_append (uri->path, g_strdup (path_segment));
2480 * gst_uri_get_query_string:
2481 * @uri: (nullable): The #GstUri to get the query string from.
2483 * Get a percent encoded URI query string from the @uri.
2485 * Returns: (transfer full): A percent encoded query string. Use g_free() when
2491 gst_uri_get_query_string (const GstUri * uri)
2493 GHashTableIter iter;
2494 gpointer key, value;
2495 const gchar *sep = "";
2501 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2505 ret = g_string_new (NULL);
2506 g_hash_table_iter_init (&iter, uri->query);
2507 while (g_hash_table_iter_next (&iter, &key, &value)) {
2508 g_string_append (ret, sep);
2509 escaped = _gst_uri_escape_http_query_element (key);
2510 g_string_append (ret, escaped);
2513 escaped = _gst_uri_escape_http_query_element (value);
2514 g_string_append_printf (ret, "=%s", escaped);
2520 return g_string_free (ret, FALSE);
2524 * gst_uri_set_query_string:
2525 * @uri: (transfer none)(nullable): The #GstUri to modify.
2526 * @query: The new percent encoded query string to use to populate the query
2527 * table, or use %NULL to unset the query table.
2529 * Sets or unsets the query table in the URI.
2531 * Returns: %TRUE if the query table was set successfully.
2536 gst_uri_set_query_string (GstUri * uri, const gchar * query)
2539 return query == NULL;
2541 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2544 g_hash_table_unref (uri->query);
2545 uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, TRUE);
2551 * gst_uri_get_query_table:
2552 * @uri: (nullable): The #GstUri to get the query table from.
2554 * Get the query table from the URI. Keys and values in the table are freed
2555 * with g_free when they are deleted. A value may be %NULL to indicate that
2556 * the key should appear in the query string in the URI, but does not have a
2557 * value. Free the returned #GHashTable with #g_hash_table_unref() when it is
2558 * no longer required. Modifying this hash table will modify the query in the
2561 * Returns: (transfer full)(element-type gchar* gchar*): The query hash table
2567 gst_uri_get_query_table (const GstUri * uri)
2571 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2575 return g_hash_table_ref (uri->query);
2579 * gst_uri_set_query_table:
2580 * @uri: (transfer none)(nullable): The #GstUri to modify.
2581 * @query_table: (transfer none)(nullable)(element-type gchar* gchar*): The new
2582 * query table to use.
2584 * Set the query table to use in the URI. The old table is unreferenced and a
2585 * reference to the new one is used instead. A value if %NULL for @query_table
2586 * will remove the query string from the URI.
2588 * Returns: %TRUE if the new table was sucessfully used for the query table.
2593 gst_uri_set_query_table (GstUri * uri, GHashTable * query_table)
2595 GHashTable *old_table = NULL;
2598 return query_table == NULL;
2599 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2601 old_table = uri->query;
2603 uri->query = g_hash_table_ref (query_table);
2607 g_hash_table_unref (old_table);
2613 * gst_uri_set_query_value:
2614 * @uri: (transfer none)(nullable): The #GstUri to modify.
2615 * @query_key: (transfer none): The key for the query entry.
2616 * @query_value: (transfer none)(nullable): The value for the key.
2618 * This inserts or replaces a key in the query table. A @query_value of %NULL
2619 * indicates that the key has no associated value, but will still be present in
2622 * Returns: %TRUE if the query table was sucessfully updated.
2627 gst_uri_set_query_value (GstUri * uri, const gchar * query_key,
2628 const gchar * query_value)
2632 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2635 uri->query = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2638 g_hash_table_insert (uri->query, g_strdup (query_key),
2639 g_strdup (query_value));
2645 * gst_uri_remove_query_key:
2646 * @uri: (transfer none)(nullable): The #GstUri to modify.
2647 * @query_key: The key to remove.
2649 * Remove an entry from the query table by key.
2651 * Returns: %TRUE if the key existed in the table and was removed.
2656 gst_uri_remove_query_key (GstUri * uri, const gchar * query_key)
2662 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2666 result = g_hash_table_remove (uri->query, query_key);
2667 /* if this was the last query entry, remove the query string completely */
2668 if (result && g_hash_table_size (uri->query) == 0) {
2669 g_hash_table_unref (uri->query);
2676 * gst_uri_query_has_key:
2677 * @uri: (nullable): The #GstUri to examine.
2678 * @query_key: The key to lookup.
2680 * Check if there is a query table entry for the @query_key key.
2682 * Returns: %TRUE if @query_key exists in the URI query table.
2687 gst_uri_query_has_key (const GstUri * uri, const gchar * query_key)
2691 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2695 return g_hash_table_contains (uri->query, query_key);
2699 * gst_uri_get_query_value:
2700 * @uri: (nullable): The #GstUri to examine.
2701 * @query_key: The key to lookup.
2703 * Get the value associated with the @query_key key. Will return %NULL if the
2704 * key has no value or if the key does not exist in the URI query table. Because
2705 * %NULL is returned for both missing keys and keys with no value, you should
2706 * use gst_uri_query_has_key() to determine if a key is present in the URI
2709 * Returns: The value for the given key, or %NULL if not found.
2714 gst_uri_get_query_value (const GstUri * uri, const gchar * query_key)
2718 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2722 return g_hash_table_lookup (uri->query, query_key);
2726 * gst_uri_get_query_keys:
2727 * @uri: (nullable): The #GstUri to examine.
2729 * Get a list of the query keys from the URI.
2731 * Returns: (transfer container)(element-type gchar*): A list of keys from
2732 * the URI query. Free the list with g_list_free().
2737 gst_uri_get_query_keys (const GstUri * uri)
2741 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2745 return g_hash_table_get_keys (uri->query);
2749 * gst_uri_get_fragment:
2750 * @uri: (nullable): This #GstUri object.
2752 * Get the fragment name from the URI or %NULL if it doesn't exist.
2753 * If @uri is %NULL then returns %NULL.
2755 * Returns: The host name from the #GstUri object or %NULL.
2760 gst_uri_get_fragment (const GstUri * uri)
2762 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2763 return (uri ? uri->fragment : NULL);
2767 * gst_uri_set_fragment:
2768 * @uri: (transfer none)(nullable): The #GstUri to modify.
2769 * @fragment: (nullable): The fragment string to set.
2771 * Sets the fragment string in the URI. Use a value of %NULL in @fragment to
2772 * unset the fragment string.
2774 * Returns: %TRUE if the fragment was set/unset successfully.
2779 gst_uri_set_fragment (GstUri * uri, const gchar * fragment)
2782 return fragment == NULL;
2783 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2785 g_free (uri->fragment);
2786 uri->fragment = g_strdup (fragment);