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);
122 gst_uri_handler_get_type (void)
124 static volatile gsize urihandler_type = 0;
126 if (g_once_init_enter (&urihandler_type)) {
128 static const GTypeInfo urihandler_info = {
129 sizeof (GstURIHandlerInterface),
141 _type = g_type_register_static (G_TYPE_INTERFACE,
142 "GstURIHandler", &urihandler_info, 0);
144 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
146 g_once_init_leave (&urihandler_type, _type);
148 return urihandler_type;
152 gst_uri_error_quark (void)
154 return g_quark_from_static_string ("gst-uri-error-quark");
157 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
158 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
159 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
160 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
161 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
162 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
163 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
168 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
169 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
170 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
171 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
172 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
173 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
174 } UnsafeCharacterSet;
176 #define HEX_ESCAPE '%'
178 /* Escape undesirable characters using %
179 * -------------------------------------
181 * This function takes a pointer to a string in which
182 * some characters may be unacceptable unescaped.
183 * It returns a string which has these characters
184 * represented by a '%' character followed by two hex digits.
186 * This routine returns a g_malloced string.
189 static const gchar hex[16] = "0123456789ABCDEF";
192 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
194 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
201 UnsafeCharacterSet use_mask;
203 g_return_val_if_fail (mask == UNSAFE_ALL
204 || mask == UNSAFE_ALLOW_PLUS
205 || mask == UNSAFE_PATH
206 || mask == UNSAFE_DOS_PATH
207 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
209 if (string == NULL) {
215 for (p = string; *p != '\0'; p++) {
217 if (!ACCEPTABLE_CHAR (c)) {
220 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
221 /* when escaping a host, if we hit something that needs to be escaped, or we finally
222 * hit a path separator, revert to path mode (the host segment of the url is over).
224 use_mask = UNSAFE_PATH;
228 result = g_malloc (p - string + unacceptable * 2 + 1);
231 for (q = result, p = string; *p != '\0'; p++) {
234 if (!ACCEPTABLE_CHAR (c)) {
235 *q++ = HEX_ESCAPE; /* means hex coming */
241 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
242 use_mask = UNSAFE_PATH;
252 * @string: string to be escaped
254 * Escapes @string, replacing any and all special characters
255 * with equivalent escape sequences.
257 * Return value: a newly allocated string equivalent to @string
258 * but with all special characters escaped
261 escape_string (const gchar * string)
263 return escape_string_internal (string, UNSAFE_ALL);
269 return c >= '0' && c <= '9' ? c - '0'
270 : c >= 'A' && c <= 'F' ? c - 'A' + 10
271 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
275 unescape_character (const char *scanner)
280 first_digit = hex_to_int (*scanner++);
281 if (first_digit < 0) {
285 second_digit = hex_to_int (*scanner);
286 if (second_digit < 0) {
290 return (first_digit << 4) | second_digit;
294 * @escaped_string: an escaped URI, path, or other string
295 * @illegal_characters: a string containing a sequence of characters
296 * considered "illegal", '\0' is automatically in this list.
298 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
299 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
300 * for character 16x+y.
302 * Return value: (nullable): a newly allocated string with the
303 * unescaped equivalents, or %NULL if @escaped_string contained one of
304 * the characters in @illegal_characters.
307 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
313 if (escaped_string == NULL) {
317 result = g_malloc (strlen (escaped_string) + 1);
320 for (in = escaped_string; *in != '\0'; in++) {
322 if (*in == HEX_ESCAPE) {
323 character = unescape_character (in + 1);
325 /* Check for an illegal character. We consider '\0' illegal here. */
327 || (illegal_characters != NULL
328 && strchr (illegal_characters, (char) character) != NULL)) {
334 *out++ = (char) character;
338 g_assert ((gsize) (out - result) <= strlen (escaped_string));
345 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
347 gchar *check = (gchar *) uri;
349 g_assert (uri != NULL);
350 g_assert (endptr != NULL);
352 if (g_ascii_isalpha (*check)) {
354 while (g_ascii_isalnum (*check) || *check == '+'
355 || *check == '-' || *check == '.')
363 * gst_uri_protocol_is_valid:
364 * @protocol: A string
366 * Tests if the given string is a valid protocol identifier. Protocols
367 * must consist of alphanumeric characters, '+', '-' and '.' and must
368 * start with a alphabetic character. See RFC 3986 Section 3.1.
370 * Returns: %TRUE if the string is a valid protocol identifier, %FALSE otherwise.
373 gst_uri_protocol_is_valid (const gchar * protocol)
377 g_return_val_if_fail (protocol != NULL, FALSE);
379 gst_uri_protocol_check_internal (protocol, &endptr);
381 return *endptr == '\0' && ((gsize) (endptr - protocol)) >= 2;
388 * Tests if the given string is a valid URI identifier. URIs start with a valid
389 * scheme followed by ":" and maybe a string identifying the location.
391 * Returns: %TRUE if the string is a valid URI
394 gst_uri_is_valid (const gchar * uri)
398 g_return_val_if_fail (uri != NULL, FALSE);
400 gst_uri_protocol_check_internal (uri, &endptr);
402 return *endptr == ':' && ((gsize) (endptr - uri)) >= 2;
406 * gst_uri_get_protocol:
409 * Extracts the protocol out of a given valid URI. The returned string must be
410 * freed using g_free().
412 * Returns: The protocol for this URI.
415 gst_uri_get_protocol (const gchar * uri)
419 g_return_val_if_fail (uri != NULL, NULL);
420 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
422 colon = strstr (uri, ":");
424 return g_ascii_strdown (uri, colon - uri);
428 * gst_uri_has_protocol:
430 * @protocol: a protocol string (e.g. "http")
432 * Checks if the protocol of a given valid URI matches @protocol.
434 * Returns: %TRUE if the protocol matches.
437 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
441 g_return_val_if_fail (uri != NULL, FALSE);
442 g_return_val_if_fail (protocol != NULL, FALSE);
443 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
445 colon = strstr (uri, ":");
450 return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
454 * gst_uri_get_location:
457 * Extracts the location out of a given valid URI, ie. the protocol and "://"
458 * are stripped from the URI, which means that the location returned includes
459 * the hostname if one is specified. The returned string must be freed using
462 * Free-function: g_free
464 * Returns: (transfer full): the location for this URI. Returns %NULL if the
465 * URI isn't valid. If the URI does not contain a location, an empty
466 * string is returned.
469 gst_uri_get_location (const gchar * uri)
472 gchar *unescaped = NULL;
474 g_return_val_if_fail (uri != NULL, NULL);
475 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
477 colon = strstr (uri, "://");
481 unescaped = unescape_string (colon + 3, "/");
483 /* On Windows an URI might look like file:///c:/foo/bar.txt or
484 * file:///c|/foo/bar.txt (some Netscape versions) and we want to
485 * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
486 * Can't use g_filename_from_uri() here because it will only handle the
487 * file:// protocol */
489 if (unescaped != NULL && unescaped[0] == '/' &&
490 g_ascii_isalpha (unescaped[1]) &&
491 (unescaped[2] == ':' || unescaped[2] == '|')) {
493 memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
497 GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
504 * @protocol: Protocol for URI
505 * @location: (transfer none): Location for URI
507 * Constructs a URI for a given valid protocol and location.
509 * Free-function: g_free
511 * Returns: (transfer full): a new string for this URI. Returns %NULL if the
512 * given URI protocol is not valid, or the given location is %NULL.
515 gst_uri_construct (const gchar * protocol, const gchar * location)
517 char *escaped, *proto_lowercase;
520 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
521 g_return_val_if_fail (location != NULL, NULL);
523 proto_lowercase = g_ascii_strdown (protocol, -1);
524 escaped = escape_string (location);
525 retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
527 g_free (proto_lowercase);
535 const gchar *protocol;
540 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
542 const gchar *const *protocols;
543 GstElementFactory *factory;
544 SearchEntry *entry = (SearchEntry *) search_entry;
546 if (!GST_IS_ELEMENT_FACTORY (feature))
548 factory = GST_ELEMENT_FACTORY_CAST (feature);
550 if (factory->uri_type != entry->type)
553 protocols = gst_element_factory_get_uri_protocols (factory);
555 if (protocols == NULL) {
556 g_warning ("Factory '%s' implements GstUriHandler interface but returned "
557 "no supported protocols!", gst_plugin_feature_get_name (feature));
561 while (*protocols != NULL) {
562 if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
570 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
572 return gst_plugin_feature_get_rank (second) -
573 gst_plugin_feature_get_rank (first);
577 get_element_factories_from_uri_protocol (const GstURIType type,
578 const gchar * protocol)
580 GList *possibilities;
583 g_return_val_if_fail (protocol, NULL);
586 entry.protocol = protocol;
587 possibilities = gst_registry_feature_filter (gst_registry_get (),
588 search_by_entry, FALSE, &entry);
590 return possibilities;
594 * gst_uri_protocol_is_supported:
595 * @type: Whether to check for a source or a sink
596 * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
598 * Checks if an element exists that supports the given URI protocol. Note
599 * that a positive return value does not imply that a subsequent call to
600 * gst_element_make_from_uri() is guaranteed to work.
605 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
607 GList *possibilities;
609 g_return_val_if_fail (protocol, FALSE);
611 possibilities = get_element_factories_from_uri_protocol (type, protocol);
614 g_list_free (possibilities);
621 * gst_element_make_from_uri:
622 * @type: Whether to create a source or a sink
623 * @uri: URI to create an element for
624 * @elementname: (allow-none): Name of created element, can be %NULL.
625 * @error: (allow-none): address where to store error information, or %NULL.
627 * Creates an element for handling the given URI.
629 * Returns: (transfer floating): a new element or %NULL if none could be created
632 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
633 const gchar * elementname, GError ** error)
635 GList *possibilities, *walk;
637 GstElement *ret = NULL;
639 g_return_val_if_fail (gst_is_initialized (), NULL);
640 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
641 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
642 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
644 GST_DEBUG ("type:%d, uri:%s, elementname:%s", type, uri, elementname);
646 protocol = gst_uri_get_protocol (uri);
647 possibilities = get_element_factories_from_uri_protocol (type, protocol);
649 if (!possibilities) {
650 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
652 /* The error message isn't great, but we don't expect applications to
653 * show that error to users, but call the missing plugins functions */
654 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
655 _("No URI handler for the %s protocol found"), protocol);
661 possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
662 walk = possibilities;
664 GstElementFactory *factory = walk->data;
665 GError *uri_err = NULL;
667 ret = gst_element_factory_create (factory, elementname);
669 GstURIHandler *handler = GST_URI_HANDLER (ret);
671 if (gst_uri_handler_set_uri (handler, uri, &uri_err))
674 GST_WARNING ("%s didn't accept URI '%s': %s", GST_OBJECT_NAME (ret), uri,
677 if (error != NULL && *error == NULL)
678 g_propagate_error (error, uri_err);
680 g_error_free (uri_err);
682 gst_object_unref (ret);
687 gst_plugin_feature_list_free (possibilities);
689 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
690 type == GST_URI_SINK ? "sink" : "source", uri);
692 /* if the first handler didn't work, but we found another one that works */
694 g_clear_error (error);
700 * gst_uri_handler_get_uri_type:
701 * @handler: A #GstURIHandler.
703 * Gets the type of the given URI handler
705 * Returns: the #GstURIType of the URI handler.
706 * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
709 gst_uri_handler_get_uri_type (GstURIHandler * handler)
711 GstURIHandlerInterface *iface;
714 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
716 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
717 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
718 g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
720 ret = iface->get_type (G_OBJECT_TYPE (handler));
721 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
727 * gst_uri_handler_get_protocols:
728 * @handler: A #GstURIHandler.
730 * Gets the list of protocols supported by @handler. This list may not be
733 * Returns: (transfer none) (element-type utf8) (nullable): the
734 * supported protocols. Returns %NULL if the @handler isn't
735 * implemented properly, or the @handler doesn't support any
739 gst_uri_handler_get_protocols (GstURIHandler * handler)
741 GstURIHandlerInterface *iface;
742 const gchar *const *ret;
744 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
746 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
747 g_return_val_if_fail (iface != NULL, NULL);
748 g_return_val_if_fail (iface->get_protocols != NULL, NULL);
750 ret = iface->get_protocols (G_OBJECT_TYPE (handler));
751 g_return_val_if_fail (ret != NULL, NULL);
757 * gst_uri_handler_get_uri:
758 * @handler: A #GstURIHandler
760 * Gets the currently handled URI.
762 * Returns: (transfer full) (nullable): the URI currently handled by
763 * the @handler. Returns %NULL if there are no URI currently
764 * handled. The returned string must be freed with g_free() when no
768 gst_uri_handler_get_uri (GstURIHandler * handler)
770 GstURIHandlerInterface *iface;
773 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
775 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
776 g_return_val_if_fail (iface != NULL, NULL);
777 g_return_val_if_fail (iface->get_uri != NULL, NULL);
778 ret = iface->get_uri (handler);
780 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
786 * gst_uri_handler_set_uri:
787 * @handler: A #GstURIHandler
789 * @error: (allow-none): address where to store a #GError in case of
792 * Tries to set the URI of the given handler.
794 * Returns: %TRUE if the URI was set successfully, else %FALSE.
797 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
800 GstURIHandlerInterface *iface;
804 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
805 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
806 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
808 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
809 g_return_val_if_fail (iface != NULL, FALSE);
810 g_return_val_if_fail (iface->set_uri != NULL, FALSE);
812 protocol = gst_uri_get_protocol (uri);
814 if (iface->get_protocols) {
815 const gchar *const *protocols;
816 const gchar *const *p;
817 gboolean found_protocol = FALSE;
819 protocols = iface->get_protocols (G_OBJECT_TYPE (handler));
820 if (protocols != NULL) {
821 for (p = protocols; *p != NULL; ++p) {
822 if (g_ascii_strcasecmp (protocol, *p) == 0) {
823 found_protocol = TRUE;
828 if (!found_protocol) {
829 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
830 _("URI scheme '%s' not supported"), protocol);
837 ret = iface->set_uri (handler, uri, error);
845 gst_file_utils_canonicalise_path (const gchar * path)
847 gchar **parts, **p, *clean_path;
851 GST_WARNING ("FIXME: canonicalise win32 path");
852 return g_strdup (path);
856 parts = g_strsplit (path, "/", -1);
860 if (strcmp (*p, ".") == 0) {
861 /* just move all following parts on top of this, incl. NUL terminator */
863 memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
864 /* re-check the new current part again in the next iteration */
866 } else if (strcmp (*p, "..") == 0 && p > parts) {
867 /* just move all following parts on top of the previous part, incl.
871 memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
872 /* re-check the new current part again in the next iteration */
881 num_parts = g_strv_length (parts) + 1; /* incl. terminator */
882 parts = g_renew (gchar *, parts, num_parts + 1);
883 memmove (parts + 1, parts, num_parts * sizeof (gchar *));
884 parts[0] = g_strdup ("/");
887 clean_path = g_build_filenamev (parts);
893 file_path_contains_relatives (const gchar * path)
895 return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
896 strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
897 strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
901 * gst_filename_to_uri:
902 * @filename: absolute or relative file name path
903 * @error: pointer to error, or %NULL
905 * Similar to g_filename_to_uri(), but attempts to handle relative file paths
906 * as well. Before converting @filename into an URI, it will be prefixed by
907 * the current working directory if it is a relative path, and then the path
908 * will be canonicalised so that it doesn't contain any './' or '../' segments.
910 * On Windows #filename should be in UTF-8 encoding.
913 gst_filename_to_uri (const gchar * filename, GError ** error)
915 gchar *abs_location = NULL;
916 gchar *uri, *abs_clean;
918 g_return_val_if_fail (filename != NULL, NULL);
919 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
921 if (g_path_is_absolute (filename)) {
922 if (!file_path_contains_relatives (filename)) {
923 uri = g_filename_to_uri (filename, NULL, error);
927 abs_location = g_strdup (filename);
931 cwd = g_get_current_dir ();
932 abs_location = g_build_filename (cwd, filename, NULL);
935 if (!file_path_contains_relatives (abs_location)) {
936 uri = g_filename_to_uri (abs_location, NULL, error);
941 /* path is now absolute, but contains '.' or '..' */
942 abs_clean = gst_file_utils_canonicalise_path (abs_location);
943 GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
944 uri = g_filename_to_uri (abs_clean, NULL, error);
949 g_free (abs_location);
950 GST_DEBUG ("'%s' -> '%s'", filename, uri);
954 /****************************************************************************
955 * GstUri - GstMiniObject to parse and merge URIs according to IETF RFC 3986
956 ****************************************************************************/
960 * @short_description: URI parsing and manipulation.
962 * A #GstUri object can be used to parse and split a URI string into its
963 * constituant parts. Two #GstUri objects can be joined to make a new #GstUri
964 * using the algorithm described in RFC3986.
967 /* Definition for GstUri object */
971 GstMiniObject mini_object;
981 GST_DEFINE_MINI_OBJECT_TYPE (GstUri, gst_uri);
983 static GstUri *_gst_uri_copy (const GstUri * uri);
984 static void _gst_uri_free (GstUri * uri);
985 static GstUri *_gst_uri_new (void);
986 static GList *_remove_dot_segments (GList * path);
988 /** private GstUri functions **/
994 uri = GST_URI_CAST (g_slice_new0 (GstUri));
997 gst_mini_object_init (GST_MINI_OBJECT_CAST (uri), 0, gst_uri_get_type (),
998 (GstMiniObjectCopyFunction) _gst_uri_copy, NULL,
999 (GstMiniObjectFreeFunction) _gst_uri_free);
1005 _gst_uri_free (GstUri * uri)
1007 g_return_if_fail (GST_IS_URI (uri));
1009 g_free (uri->scheme);
1010 g_free (uri->userinfo);
1012 g_list_free_full (uri->path, g_free);
1014 g_hash_table_unref (uri->query);
1015 g_free (uri->fragment);
1017 g_slice_free1 (sizeof (*uri), uri);
1021 _gst_uri_copy_query_table (GHashTable * orig)
1023 GHashTable *new = NULL;
1026 GHashTableIter iter;
1027 gpointer key, value;
1028 new = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1029 g_hash_table_iter_init (&iter, orig);
1030 while (g_hash_table_iter_next (&iter, &key, &value)) {
1031 g_hash_table_insert (new, g_strdup (key), g_strdup (value));
1039 _gst_uri_copy (const GstUri * orig_uri)
1043 g_return_val_if_fail (GST_IS_URI (orig_uri), NULL);
1045 new_uri = _gst_uri_new ();
1048 new_uri->scheme = g_strdup (orig_uri->scheme);
1049 new_uri->userinfo = g_strdup (orig_uri->userinfo);
1050 new_uri->host = g_strdup (orig_uri->host);
1051 new_uri->port = orig_uri->port;
1052 new_uri->path = g_list_copy_deep (orig_uri->path, (GCopyFunc) g_strdup,
1054 new_uri->query = _gst_uri_copy_query_table (orig_uri->query);
1055 new_uri->fragment = g_strdup (orig_uri->fragment);
1062 * _gst_uri_compare_lists:
1064 * Compare two lists for equality. This compares the two lists, item for item,
1065 * comparing items in the same position in the two lists. If @first is
1066 * considered less than @second the result will be negative. If @first is
1067 * considered to be more than @second then the result will be positive. If the
1068 * lists are considered to be equal then the result will be 0. If two lists
1069 * have the same items, but one list is shorter than the other, then the
1070 * shorter list is considered to be less than the longer list.
1073 _gst_uri_compare_lists (GList * first, GList * second, GCompareFunc cmp_fn)
1078 for (itr1 = first, itr2 = second;
1079 itr1 != NULL || itr2 != NULL; itr1 = itr1->next, itr2 = itr2->next) {
1084 result = cmp_fn (itr1->data, itr2->data);
1093 _GST_URI_NORMALIZE_LOWERCASE = 1,
1094 _GST_URI_NORMALIZE_UPPERCASE = 2
1095 } _GstUriNormalizations;
1098 * Find the first character that hasn't been normalized according to the @flags.
1101 _gst_uri_first_non_normalized_char (gchar * str, guint flags)
1108 for (pos = str; *pos; pos++) {
1109 if ((flags & _GST_URI_NORMALIZE_UPPERCASE) && g_ascii_islower (*pos))
1111 if ((flags & _GST_URI_NORMALIZE_LOWERCASE) && g_ascii_isupper (*pos))
1118 _gst_uri_normalize_lowercase (gchar * str)
1121 gboolean ret = FALSE;
1123 for (pos = _gst_uri_first_non_normalized_char (str,
1124 _GST_URI_NORMALIZE_LOWERCASE);
1126 pos = _gst_uri_first_non_normalized_char (pos + 1,
1127 _GST_URI_NORMALIZE_LOWERCASE)) {
1128 *pos = g_ascii_tolower (*pos);
1135 #define _gst_uri_normalize_scheme _gst_uri_normalize_lowercase
1136 #define _gst_uri_normalize_hostname _gst_uri_normalize_lowercase
1139 _gst_uri_normalize_path (GList ** path)
1143 new_path = _remove_dot_segments (*path);
1144 if (_gst_uri_compare_lists (new_path, *path, (GCompareFunc) g_strcmp0) != 0) {
1145 g_list_free_full (*path, g_free);
1149 g_list_free_full (new_path, g_free);
1155 _gst_uri_normalize_str_noop (gchar * str)
1161 _gst_uri_normalize_table_noop (GHashTable * table)
1166 #define _gst_uri_normalize_userinfo _gst_uri_normalize_str_noop
1167 #define _gst_uri_normalize_query _gst_uri_normalize_table_noop
1168 #define _gst_uri_normalize_fragment _gst_uri_normalize_str_noop
1170 /** RFC 3986 functions **/
1173 _merge (GList * base, GList * path)
1175 GList *ret, *path_copy, *last;
1177 path_copy = g_list_copy_deep (path, (GCopyFunc) g_strdup, NULL);
1178 /* if base is NULL make path absolute */
1180 if (path_copy != NULL && path_copy->data != NULL) {
1181 path_copy = g_list_prepend (path_copy, NULL);
1186 ret = g_list_copy_deep (base, (GCopyFunc) g_strdup, NULL);
1187 last = g_list_last (ret);
1188 ret = g_list_remove_link (ret, last);
1189 g_list_free_full (last, g_free);
1190 ret = g_list_concat (ret, path_copy);
1196 _remove_dot_segments (GList * path)
1198 GList *out, *elem, *next;
1203 out = g_list_copy_deep (path, (GCopyFunc) g_strdup, NULL);
1205 for (elem = out; elem; elem = next) {
1207 if (elem->data == NULL && elem != out && next != NULL) {
1208 out = g_list_delete_link (out, elem);
1209 } else if (g_strcmp0 (elem->data, ".") == 0) {
1210 g_free (elem->data);
1211 out = g_list_delete_link (out, elem);
1212 } else if (g_strcmp0 (elem->data, "..") == 0) {
1213 GList *prev = g_list_previous (elem);
1214 if (prev && (prev != out || prev->data != NULL)) {
1215 g_free (prev->data);
1216 out = g_list_delete_link (out, prev);
1218 g_free (elem->data);
1219 out = g_list_delete_link (out, elem);
1227 _gst_uri_escape_userinfo (const gchar * userinfo)
1229 return g_uri_escape_string (userinfo,
1230 G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, FALSE);
1234 _gst_uri_escape_host (const gchar * host)
1236 return g_uri_escape_string (host,
1237 G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, FALSE);
1241 _gst_uri_escape_path_segment (const gchar * segment)
1243 return g_uri_escape_string (segment,
1244 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, FALSE);
1248 _gst_uri_escape_http_query_element (const gchar * element)
1252 ret = g_uri_escape_string (element, "!$'()*,;:@/? ", FALSE);
1253 for (c = ret; *c; c++)
1260 _gst_uri_escape_fragment (const gchar * fragment)
1262 return g_uri_escape_string (fragment,
1263 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH "?", FALSE);
1267 _gst_uri_string_to_list (const gchar * str, const gchar * sep, gboolean convert,
1270 GList *new_list = NULL;
1273 guint pct_sep_len = 0;
1277 if (convert && !unescape) {
1278 pct_sep = g_strdup_printf ("%%%2.2X", (guint) (*sep));
1282 split_str = g_strsplit (str, sep, -1);
1285 for (next_elem = split_str; *next_elem; next_elem += 1) {
1286 gchar *elem = *next_elem;
1287 if (*elem == '\0') {
1288 new_list = g_list_append (new_list, NULL);
1290 if (convert && !unescape) {
1292 for (next_sep = strcasestr (elem, pct_sep); next_sep;
1293 next_sep = strcasestr (next_sep + 1, pct_sep)) {
1295 memmove (next_sep + 1, next_sep + pct_sep_len,
1296 strlen (next_sep + pct_sep_len) + 1);
1300 *next_elem = g_uri_unescape_string (elem, NULL);
1304 new_list = g_list_append (new_list, g_strdup (elem));
1308 g_strfreev (split_str);
1309 if (convert && !unescape)
1317 _gst_uri_string_to_table (const gchar * str, const gchar * part_sep,
1318 const gchar * kv_sep, gboolean convert, gboolean unescape)
1320 GHashTable *new_table = NULL;
1323 gchar *pct_part_sep = NULL, *pct_kv_sep = NULL;
1324 gchar **split_parts;
1326 new_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1328 if (convert && !unescape) {
1329 pct_part_sep = g_strdup_printf ("%%%2.2X", (guint) (*part_sep));
1330 pct_kv_sep = g_strdup_printf ("%%%2.2X", (guint) (*kv_sep));
1333 split_parts = g_strsplit (str, part_sep, -1);
1336 for (next_part = split_parts; *next_part; next_part += 1) {
1337 gchar *part = *next_part;
1340 /* if we are converting percent encoded versions of separators then
1341 * substitute the part separator now. */
1342 if (convert && !unescape) {
1344 for (next_sep = strcasestr (part, pct_part_sep); next_sep;
1345 next_sep = strcasestr (next_sep + 1, pct_part_sep)) {
1346 *next_sep = *part_sep;
1347 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1350 /* find the key/value separator within the part */
1351 kv_sep_pos = g_strstr_len (part, -1, kv_sep);
1352 if (kv_sep_pos == NULL) {
1354 key = g_uri_unescape_string (part, NULL);
1356 key = g_strdup (part);
1361 key = g_uri_unescape_segment (part, kv_sep_pos, NULL);
1362 value = g_uri_unescape_string (kv_sep_pos + 1, NULL);
1364 key = g_strndup (part, kv_sep_pos - part);
1365 value = g_strdup (kv_sep_pos + 1);
1368 /* if we are converting percent encoded versions of separators then
1369 * substitute the key/value separator in both key and value now. */
1370 if (convert && !unescape) {
1372 for (next_sep = strcasestr (key, pct_kv_sep); next_sep;
1373 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1374 *next_sep = *kv_sep;
1375 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1378 for (next_sep = strcasestr (value, pct_kv_sep); next_sep;
1379 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1380 *next_sep = *kv_sep;
1381 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1385 /* add value to the table */
1386 g_hash_table_insert (new_table, key, value);
1390 g_strfreev (split_parts);
1391 if (convert && !unescape) {
1392 g_free (pct_part_sep);
1393 g_free (pct_kv_sep);
1402 * Method definitions.
1407 * @scheme: (nullable): The scheme for the new URI.
1408 * @userinfo: (nullable): The user-info for the new URI.
1409 * @host: (nullable): The host name for the new URI.
1410 * @port: The port number for the new URI or %GST_URI_NO_PORT.
1411 * @path: (nullable): The path for the new URI with '/' separating path
1413 * @query: (nullable): The query string for the new URI with '&' separating
1414 * query elements. Elements containing '&' characters
1415 * should encode them as "%26".
1416 * @fragment: (nullable): The fragment name for the new URI.
1418 * Creates a new #GstUri object with the given URI parts. The path and query
1419 * strings will be broken down into their elements. All strings should not be
1420 * escaped except where indicated.
1422 * Returns: (transfer full): A new #GstUri object.
1427 gst_uri_new (const gchar * scheme, const gchar * userinfo, const gchar * host,
1428 guint port, const gchar * path, const gchar * query, const gchar * fragment)
1432 new_uri = _gst_uri_new ();
1434 new_uri->scheme = g_strdup (scheme);
1435 new_uri->userinfo = g_strdup (userinfo);
1436 new_uri->host = g_strdup (host);
1437 new_uri->port = port;
1438 new_uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
1439 new_uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, FALSE);
1440 new_uri->fragment = g_strdup (fragment);
1447 * gst_uri_new_with_base:
1448 * @base: (transfer none)(nullable): The base URI to join the new URI to.
1449 * @scheme: (nullable): The scheme for the new URI.
1450 * @userinfo: (nullable): The user-info for the new URI.
1451 * @host: (nullable): The host name for the new URI.
1452 * @port: The port number for the new URI or %GST_URI_NO_PORT.
1453 * @path: (nullable): The path for the new URI with '/' separating path
1455 * @query: (nullable): The query string for the new URI with '&' separating
1456 * query elements. Elements containing '&' characters
1457 * should encode them as "%26".
1458 * @fragment: (nullable): The fragment name for the new URI.
1460 * Like gst_uri_new(), but joins the new URI onto a base URI.
1462 * Returns: (transfer full): The new URI joined onto @base.
1467 gst_uri_new_with_base (GstUri * base, const gchar * scheme,
1468 const gchar * userinfo, const gchar * host, guint port, const gchar * path,
1469 const gchar * query, const gchar * fragment)
1471 GstUri *new_rel_uri;
1474 g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1476 new_rel_uri = gst_uri_new (scheme, userinfo, host, port, path, query,
1478 new_uri = gst_uri_join (base, new_rel_uri);
1479 gst_uri_unref (new_rel_uri);
1485 * gst_uri_from_string:
1486 * @uri: The URI string to parse.
1488 * Parses a URI string into a new #GstUri object.
1490 * Returns: (transfer full): A new #GstUri object.
1495 gst_uri_from_string (const gchar * uri)
1499 uri_obj = _gst_uri_new ();
1501 if (uri_obj && uri != NULL) {
1503 if (g_ascii_isalpha (uri[i])) {
1504 /* find end of scheme name */
1506 while (g_ascii_isalnum (uri[i]) || uri[i] == '+' || uri[i] == '-' ||
1510 if (i > 0 && uri[i] == ':') {
1512 uri_obj->scheme = g_strndup (uri, i);
1515 if (uri[0] == '/' && uri[1] == '/') {
1516 const gchar *eoa, *eoui, *eoh;
1517 /* get authority [userinfo@]host[:port] */
1519 /* find end of authority */
1520 eoa = strchr (uri, '/');
1522 eoa = uri + strlen (uri);
1523 /* find end of userinfo */
1524 eoui = strchr (uri, '@');
1525 if (eoui != NULL && eoui < eoa) {
1526 uri_obj->userinfo = g_uri_unescape_segment (uri, eoui, NULL);
1529 /* find end of host */
1530 if (uri[0] == '[') {
1531 eoh = strchr (uri, ']');
1532 if (eoh == NULL || eoh >= eoa)
1535 eoh = strchr (uri, ':');
1536 if (eoh == NULL || eoh >= eoa)
1541 uri_obj->host = g_uri_unescape_segment (uri, eoh + 1, NULL);
1544 /* if port number is malformed, do best effort and concat string */
1545 if (uri[0] != ':' || strspn (uri + 1, "0123456789") != eoa - uri - 1) {
1546 gchar *tmp = uri_obj->host;
1547 uri_obj->host = g_malloc (strlen (uri_obj->host) + eoa - uri + 1);
1548 g_strlcpy (g_stpcpy (uri_obj->host, tmp), uri, eoa - uri + 1);
1551 /* otherwise treat port as unsigned decimal number */
1554 uri_obj->port = uri_obj->port * 10 + g_ascii_digit_value (*uri);
1561 if (uri != NULL && uri[0] != '\0') {
1564 len = strcspn (uri, "?#");
1565 if (uri[len] == '\0') {
1566 uri_obj->path = _gst_uri_string_to_list (uri, "/", FALSE, TRUE);
1570 gchar *path_str = g_strndup (uri, len);
1571 uri_obj->path = _gst_uri_string_to_list (path_str, "/", FALSE, TRUE);
1577 if (uri != NULL && uri[0] == '?') {
1580 eoq = strchr (++uri, '#');
1582 uri_obj->query = _gst_uri_string_to_table (uri, "&", "=", TRUE, TRUE);
1586 gchar *query_str = g_strndup (uri, eoq - uri);
1587 uri_obj->query = _gst_uri_string_to_table (query_str, "&", "=", TRUE,
1594 if (uri != NULL && uri[0] == '#') {
1595 uri_obj->fragment = g_uri_unescape_string (uri + 1, NULL);
1603 * gst_uri_from_string_with_base:
1604 * @base: (transfer none)(nullable): The base URI to join the new URI with.
1605 * @uri: The URI string to parse.
1607 * Like gst_uri_from_string() but also joins with a base URI.
1609 * Returns: (transfer full): A new #GstUri object.
1614 gst_uri_from_string_with_base (GstUri * base, const gchar * uri)
1616 GstUri *new_rel_uri;
1619 g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1621 new_rel_uri = gst_uri_from_string (uri);
1622 new_uri = gst_uri_join (base, new_rel_uri);
1623 gst_uri_unref (new_rel_uri);
1630 * @first: First #GstUri to compare.
1631 * @second: Second #GstUri to compare.
1633 * Compares two #GstUri objects to see if they represent the same normalized
1636 * Returns: %TRUE if the normalized versions of the two URI's would be equal.
1641 gst_uri_equal (const GstUri * first, const GstUri * second)
1643 gchar *first_norm = NULL, *second_norm = NULL;
1644 GList *first_norm_list = NULL, *second_norm_list = NULL;
1645 const gchar *first_cmp, *second_cmp;
1646 GHashTableIter table_iter;
1647 gpointer key, value;
1650 g_return_val_if_fail ((first == NULL || GST_IS_URI (first)) &&
1651 (second == NULL || GST_IS_URI (second)), FALSE);
1653 if (first == second)
1656 if (first == NULL || second == NULL)
1659 if (first->port != second->port)
1662 /* work out a version of field value (normalized or not) to compare.
1663 * first_cmp, second_cmp will be the values to compare later.
1664 * first_norm, second_norm will be non-NULL if normalized versions are used,
1665 * and need to be freed later.
1667 #define GST_URI_NORMALIZED_FIELD(pos, field, norm_fn, flags) \
1668 pos##_cmp = pos->field; \
1669 if (_gst_uri_first_non_normalized_char ((gchar*)pos##_cmp, flags) != NULL) { \
1670 pos##_norm = g_strdup (pos##_cmp); \
1671 norm_fn (pos##_norm); \
1672 pos##_cmp = pos##_norm; \
1675 /* compare two string values, normalizing if needed */
1676 #define GST_URI_NORMALIZED_CMP_STR(field, norm_fn, flags) \
1677 GST_URI_NORMALIZED_FIELD (first, field, norm_fn, flags) \
1678 GST_URI_NORMALIZED_FIELD (second, field, norm_fn, flags) \
1679 result = g_strcmp0 (first_cmp, second_cmp); \
1680 g_free (first_norm); \
1681 first_norm = NULL; \
1682 g_free (second_norm); \
1683 second_norm = NULL; \
1684 if (result != 0) return FALSE
1686 /* compare two string values */
1687 #define GST_URI_CMP_STR(field) \
1688 if (g_strcmp0 (first->field, second->field) != 0) return FALSE
1690 /* compare two GLists, normalize lists if needed before comparison */
1691 #define GST_URI_NORMALIZED_CMP_LIST(field, norm_fn, copy_fn, cmp_fn, free_fn) \
1692 first_norm_list = g_list_copy_deep (first->field, (GCopyFunc) copy_fn, NULL); \
1693 norm_fn (&first_norm_list); \
1694 second_norm_list = g_list_copy_deep (second->field, (GCopyFunc) copy_fn, NULL); \
1695 norm_fn (&second_norm_list); \
1696 result = _gst_uri_compare_lists (first_norm_list, second_norm_list, (GCompareFunc) cmp_fn); \
1697 g_list_free_full (first_norm_list, free_fn); \
1698 g_list_free_full (second_norm_list, free_fn); \
1699 if (result != 0) return FALSE
1701 GST_URI_CMP_STR (userinfo);
1703 GST_URI_CMP_STR (fragment);
1705 GST_URI_NORMALIZED_CMP_STR (scheme, _gst_uri_normalize_scheme,
1706 _GST_URI_NORMALIZE_LOWERCASE);
1708 GST_URI_NORMALIZED_CMP_STR (host, _gst_uri_normalize_hostname,
1709 _GST_URI_NORMALIZE_LOWERCASE);
1711 GST_URI_NORMALIZED_CMP_LIST (path, _gst_uri_normalize_path, g_strdup,
1714 if (first->query == NULL && second->query != NULL)
1716 if (first->query != NULL && second->query == NULL)
1718 if (first->query != NULL) {
1719 if (g_hash_table_size (first->query) != g_hash_table_size (second->query))
1722 g_hash_table_iter_init (&table_iter, first->query);
1723 while (g_hash_table_iter_next (&table_iter, &key, &value)) {
1724 if (!g_hash_table_contains (second->query, key))
1726 result = g_strcmp0 (g_hash_table_lookup (second->query, key), value);
1731 #undef GST_URI_NORMALIZED_CMP_STR
1732 #undef GST_URI_CMP_STR
1733 #undef GST_URI_NORMALIZED_CMP_LIST
1734 #undef GST_URI_NORMALIZED_FIELD
1741 * @base_uri: (transfer none)(nullable): The base URI to join another to.
1742 * @ref_uri: (transfer none)(nullable): The reference URI to join onto the
1745 * Join a reference URI onto a base URI using the method from RFC 3986.
1746 * If either URI is %NULL then the other URI will be returned with the ref count
1749 * Returns: (transfer full): A #GstUri which represents the base with the
1750 * reference URI joined on.
1755 gst_uri_join (GstUri * base_uri, GstUri * ref_uri)
1757 const gchar *r_scheme;
1760 g_return_val_if_fail ((base_uri == NULL || GST_IS_URI (base_uri)) &&
1761 (ref_uri == NULL || GST_IS_URI (ref_uri)), NULL);
1763 if (base_uri == NULL && ref_uri == NULL)
1765 if (base_uri == NULL) {
1766 g_return_val_if_fail (GST_IS_URI (ref_uri), NULL);
1767 return gst_uri_ref (ref_uri);
1769 if (ref_uri == NULL) {
1770 g_return_val_if_fail (GST_IS_URI (base_uri), NULL);
1771 return gst_uri_ref (base_uri);
1774 g_return_val_if_fail (GST_IS_URI (base_uri) && GST_IS_URI (ref_uri), NULL);
1776 t = _gst_uri_new ();
1781 /* process according to RFC3986 */
1782 r_scheme = ref_uri->scheme;
1783 if (r_scheme != NULL && g_strcmp0 (base_uri->scheme, r_scheme) == 0) {
1786 if (r_scheme != NULL) {
1787 t->scheme = g_strdup (r_scheme);
1788 t->userinfo = g_strdup (ref_uri->userinfo);
1789 t->host = g_strdup (ref_uri->host);
1790 t->port = ref_uri->port;
1791 t->path = _remove_dot_segments (ref_uri->path);
1792 t->query = _gst_uri_copy_query_table (ref_uri->query);
1794 if (ref_uri->host != NULL) {
1795 t->userinfo = g_strdup (ref_uri->userinfo);
1796 t->host = g_strdup (ref_uri->host);
1797 t->port = ref_uri->port;
1798 t->path = _remove_dot_segments (ref_uri->path);
1799 t->query = _gst_uri_copy_query_table (ref_uri->query);
1801 if (ref_uri->path == NULL) {
1802 t->path = g_list_copy_deep (base_uri->path, (GCopyFunc) g_strdup, NULL);
1803 if (ref_uri->query != NULL)
1804 t->query = _gst_uri_copy_query_table (ref_uri->query);
1806 t->query = _gst_uri_copy_query_table (base_uri->query);
1808 if (ref_uri->path->data == NULL)
1809 t->path = _remove_dot_segments (ref_uri->path);
1811 GList *mrgd = _merge (base_uri->path, ref_uri->path);
1812 t->path = _remove_dot_segments (mrgd);
1813 g_list_free_full (mrgd, g_free);
1815 t->query = _gst_uri_copy_query_table (ref_uri->query);
1817 t->userinfo = g_strdup (base_uri->userinfo);
1818 t->host = g_strdup (base_uri->host);
1819 t->port = base_uri->port;
1821 t->scheme = g_strdup (base_uri->scheme);
1823 t->fragment = g_strdup (ref_uri->fragment);
1829 * gst_uri_join_strings:
1830 * @base_uri: The percent-encoded base URI.
1831 * @ref_uri: The percent-encoded reference URI to join to the @base_uri.
1833 * This is a convenience function to join two URI strings and return the result.
1834 * The returned string should be g_free()'d after use.
1836 * Returns: (transfer full): A string representing the percent-encoded join of
1842 gst_uri_join_strings (const gchar * base_uri, const gchar * ref_uri)
1844 GstUri *base, *result;
1847 base = gst_uri_from_string (base_uri);
1848 result = gst_uri_from_string_with_base (base, ref_uri);
1849 result_uri = gst_uri_to_string (result);
1850 gst_uri_unref (base);
1851 gst_uri_unref (result);
1857 * gst_uri_is_writable:
1858 * @uri: The #GstUri object to test.
1860 * Check if it is safe to write to this #GstUri.
1862 * Check if the refcount of @uri is exactly 1, meaning that no other
1863 * reference exists to the #GstUri and that the #GstUri is therefore writable.
1865 * Modification of a #GstUri should only be done after verifying that it is
1868 * Returns: %TRUE if it is safe to write to the object.
1873 gst_uri_is_writable (const GstUri * uri)
1875 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
1876 return gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (uri));
1880 * gst_uri_make_writable:
1881 * @uri: (transfer full): The #GstUri object to make writable.
1883 * Make the #GstUri writable.
1885 * Checks if @uri is writable, and if so the original object is returned. If
1886 * not, then a writable copy is made and returned. This gives away the
1887 * reference to @uri and returns a reference to the new #GstUri.
1888 * If @uri is %NULL then %NULL is returned.
1890 * Returns: (transfer full): A writable version of @uri.
1895 gst_uri_make_writable (GstUri * uri)
1897 g_return_val_if_fail (GST_IS_URI (uri), NULL);
1899 GST_URI_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (uri)));
1903 * gst_uri_to_string:
1904 * @uri: This #GstUri to convert to a string.
1906 * Convert the URI to a string.
1908 * Returns the URI as held in this object as a gchar* %NUL terminated string.
1909 * The caller should g_free() the string once they are finished with it.
1910 * The string is put together as described in RFC 3986.
1912 * Returns: (transfer full): The string version of the URI.
1917 gst_uri_to_string (const GstUri * uri)
1922 g_return_val_if_fail (GST_IS_URI (uri), NULL);
1924 uri_str = g_string_new (NULL);
1926 if (uri->scheme != NULL)
1927 g_string_append_printf (uri_str, "%s:", uri->scheme);
1929 if (uri->userinfo != NULL || uri->host != NULL ||
1930 uri->port != GST_URI_NO_PORT)
1931 g_string_append (uri_str, "//");
1933 if (uri->userinfo != NULL) {
1934 escaped = _gst_uri_escape_userinfo (uri->userinfo);
1935 g_string_append_printf (uri_str, "%s@", escaped);
1939 if (uri->host != NULL) {
1940 escaped = _gst_uri_escape_host (uri->host);
1941 g_string_append (uri_str, escaped);
1945 if (uri->port != GST_URI_NO_PORT)
1946 g_string_append_printf (uri_str, ":%u", uri->port);
1948 if (uri->path != NULL) {
1949 escaped = gst_uri_get_path_string (uri);
1950 g_string_append (uri_str, escaped);
1955 g_string_append (uri_str, "?");
1956 escaped = gst_uri_get_query_string (uri);
1957 g_string_append (uri_str, escaped);
1961 if (uri->fragment != NULL) {
1962 escaped = _gst_uri_escape_fragment (uri->fragment);
1963 g_string_append_printf (uri_str, "#%s", escaped);
1967 return g_string_free (uri_str, FALSE);
1971 * gst_uri_is_normalized:
1972 * @uri: The #GstUri to test to see if it is normalized.
1974 * Tests the @uri to see if it is normalized. A %NULL @uri is considered to be
1977 * Returns: TRUE if the URI is normalized or is %NULL.
1982 gst_uri_is_normalized (const GstUri * uri)
1990 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
1992 /* check for non-normalized characters in uri parts */
1993 if (_gst_uri_first_non_normalized_char (uri->scheme,
1994 _GST_URI_NORMALIZE_LOWERCASE) != NULL ||
1995 /*_gst_uri_first_non_normalized_char (uri->userinfo,
1996 _GST_URI_NORMALIZE_PERCENTAGES) != NULL || */
1997 _gst_uri_first_non_normalized_char (uri->host,
1998 _GST_URI_NORMALIZE_LOWERCASE /*| _GST_URI_NORMALIZE_PERCENTAGES */ )
2000 /*|| _gst_uri_first_non_normalized_char (uri->path,
2001 _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2002 || _gst_uri_first_non_normalized_char (uri->query,
2003 _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2004 || _gst_uri_first_non_normalized_char (uri->fragment,
2005 _GST_URI_NORMALIZE_PERCENTAGES) != NULL */ )
2008 /* also check path has had dot segments removed */
2009 new_path = _remove_dot_segments (uri->path);
2011 (_gst_uri_compare_lists (new_path, uri->path,
2012 (GCompareFunc) g_strcmp0) == 0);
2013 g_list_free_full (new_path, g_free);
2018 * gst_uri_normalize:
2019 * @uri: (transfer none): The #GstUri to normalize.
2021 * Normalization will remove extra path segments ("." and "..") from the URI. It
2022 * will also convert the scheme and host name to lower case and any
2023 * percent-encoded values to uppercase.
2025 * The #GstUri object must be writable. Check with gst_uri_is_writable() or use
2026 * gst_uri_make_writable() first.
2028 * Returns: TRUE if the URI was modified.
2033 gst_uri_normalize (GstUri * uri)
2035 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2037 return _gst_uri_normalize_scheme (uri->scheme) |
2038 _gst_uri_normalize_userinfo (uri->userinfo) |
2039 _gst_uri_normalize_hostname (uri->host) |
2040 _gst_uri_normalize_path (&uri->path) |
2041 _gst_uri_normalize_query (uri->query) |
2042 _gst_uri_normalize_fragment (uri->fragment);
2046 * gst_uri_get_scheme:
2047 * @uri: (nullable): This #GstUri object.
2049 * Get the scheme name from the URI or %NULL if it doesn't exist.
2050 * If @uri is %NULL then returns %NULL.
2052 * Returns: The scheme from the #GstUri object or %NULL.
2055 gst_uri_get_scheme (const GstUri * uri)
2057 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2058 return (uri ? uri->scheme : NULL);
2062 * gst_uri_set_scheme:
2063 * @uri: (transfer none)(nullable): The #GstUri to modify.
2064 * @scheme: The new scheme to set or %NULL to unset the scheme.
2066 * Set or unset the scheme for the URI.
2068 * Returns: %TRUE if the scheme was set/unset successfully.
2073 gst_uri_set_scheme (GstUri * uri, const gchar * scheme)
2076 return scheme == NULL;
2077 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2079 g_free (uri->scheme);
2080 uri->scheme = g_strdup (scheme);
2086 * gst_uri_get_userinfo:
2087 * @uri: (nullable): This #GstUri object.
2089 * Get the userinfo (usually in the form "username:password") from the URI
2090 * or %NULL if it doesn't exist. If @uri is %NULL then returns %NULL.
2092 * Returns: The userinfo from the #GstUri object or %NULL.
2097 gst_uri_get_userinfo (const GstUri * uri)
2099 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2100 return (uri ? uri->userinfo : NULL);
2104 * gst_uri_set_userinfo:
2105 * @uri: (transfer none)(nullable): The #GstUri to modify.
2106 * @userinfo: The new user-information string to set or %NULL to unset.
2108 * Set or unset the user information for the URI.
2110 * Returns: %TRUE if the user information was set/unset successfully.
2115 gst_uri_set_userinfo (GstUri * uri, const gchar * userinfo)
2118 return userinfo == NULL;
2119 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2121 g_free (uri->userinfo);
2122 uri->userinfo = g_strdup (userinfo);
2129 * @uri: (nullable): This #GstUri object.
2131 * Get the host name from the URI or %NULL if it doesn't exist.
2132 * If @uri is %NULL then returns %NULL.
2134 * Returns: The host name from the #GstUri object or %NULL.
2139 gst_uri_get_host (const GstUri * uri)
2141 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2142 return (uri ? uri->host : NULL);
2147 * @uri: (transfer none)(nullable): The #GstUri to modify.
2148 * @host: The new host string to set or %NULL to unset.
2150 * Set or unset the host for the URI.
2152 * Returns: %TRUE if the host was set/unset successfully.
2157 gst_uri_set_host (GstUri * uri, const gchar * host)
2160 return host == NULL;
2161 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2164 uri->host = g_strdup (host);
2171 * @uri: (nullable): This #GstUri object.
2173 * Get the port number from the URI or %GST_URI_NO_PORT if it doesn't exist.
2174 * If @uri is %NULL then returns %GST_URI_NO_PORT.
2176 * Returns: The port number from the #GstUri object or %GST_URI_NO_PORT.
2181 gst_uri_get_port (const GstUri * uri)
2183 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), GST_URI_NO_PORT);
2184 return (uri ? uri->port : GST_URI_NO_PORT);
2189 * @uri: (transfer none)(nullable): The #GstUri to modify.
2190 * @port: The new port number to set or %GST_URI_NO_PORT to unset.
2192 * Set or unset the port number for the URI.
2194 * Returns: %TRUE if the port number was set/unset successfully.
2199 gst_uri_set_port (GstUri * uri, guint port)
2202 return port == GST_URI_NO_PORT;
2203 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2212 * @uri: The #GstUri to get the path from.
2214 * Extract the path string from the URI object.
2216 * Returns: (transfer full): The path from the URI. Once finished with the
2217 * string should be g_free()'d.
2222 gst_uri_get_path (const GstUri * uri)
2224 GList *path_segment;
2225 const gchar *sep = "";
2230 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2234 ret = g_string_new (NULL);
2236 for (path_segment = uri->path; path_segment;
2237 path_segment = path_segment->next) {
2238 g_string_append (ret, sep);
2239 if (path_segment->data) {
2240 g_string_append (ret, path_segment->data);
2245 return g_string_free (ret, FALSE);
2250 * @uri: (transfer none)(nullable): The #GstUri to modify.
2251 * @path: The new path to set with path segments separated by '/', or use %NULL
2252 * to unset the path.
2254 * Sets or unsets the path in the URI.
2256 * Returns: %TRUE if the path was set successfully.
2261 gst_uri_set_path (GstUri * uri, const gchar * path)
2264 return path == NULL;
2265 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2267 g_list_free_full (uri->path, g_free);
2268 uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
2274 * gst_uri_get_path_string:
2275 * @uri: The #GstUri to get the path from.
2277 * Extract the path string from the URI object as a percent encoded URI path.
2279 * Returns: (transfer full): The path from the URI. Once finished with the
2280 * string should be g_free()'d.
2285 gst_uri_get_path_string (const GstUri * uri)
2287 GList *path_segment;
2288 const gchar *sep = "";
2294 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2298 ret = g_string_new (NULL);
2300 for (path_segment = uri->path; path_segment;
2301 path_segment = path_segment->next) {
2302 g_string_append (ret, sep);
2303 if (path_segment->data) {
2304 escaped = _gst_uri_escape_path_segment (path_segment->data);
2305 g_string_append (ret, escaped);
2311 return g_string_free (ret, FALSE);
2315 * gst_uri_set_path_string:
2316 * @uri: (transfer none)(nullable): The #GstUri to modify.
2317 * @path: The new percent encoded path to set with path segments separated by
2318 * '/', or use %NULL to unset the path.
2320 * Sets or unsets the path in the URI.
2322 * Returns: %TRUE if the path was set successfully.
2327 gst_uri_set_path_string (GstUri * uri, const gchar * path)
2330 return path == NULL;
2331 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2333 g_list_free_full (uri->path, g_free);
2334 uri->path = _gst_uri_string_to_list (path, "/", FALSE, TRUE);
2339 * gst_uri_get_path_segments:
2340 * @uri: (nullable): The #GstUri to get the path from.
2342 * Get a list of path segments from the URI.
2344 * Returns: (transfer full)(element-type gchar*): A #GList of path segment
2345 * strings or %NULL if no path segments are available. Free the list
2346 * when no longer needed with g_list_free_full(list, g_free).
2351 gst_uri_get_path_segments (const GstUri * uri)
2355 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2358 ret = g_list_copy_deep (uri->path, (GCopyFunc) g_strdup, NULL);
2365 * gst_uri_set_path_segments:
2366 * @uri: (transfer none)(nullable): The #GstUri to modify.
2367 * @path_segments: (transfer full)(nullable)(element-type gchar*): The new
2370 * Replace the path segments list in the URI.
2372 * Returns: %TRUE if the path segments were set successfully.
2377 gst_uri_set_path_segments (GstUri * uri, GList * path_segments)
2379 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), FALSE);
2383 g_list_free_full (path_segments, g_free);
2384 return path_segments == NULL;
2387 g_return_val_if_fail (gst_uri_is_writable (uri), FALSE);
2389 g_list_free_full (uri->path, g_free);
2390 uri->path = path_segments;
2395 * gst_uri_append_path:
2396 * @uri: (transfer none)(nullable): The #GstUri to modify.
2397 * @relative_path: Relative path to append to the end of the current path.
2399 * Append a path onto the end of the path in the URI. The path is not
2400 * normalized, call #gst_uri_normalize() to normalize the path.
2402 * Returns: %TRUE if the path was appended successfully.
2407 gst_uri_append_path (GstUri * uri, const gchar * relative_path)
2409 GList *rel_path_list;
2412 return relative_path == NULL;
2413 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2418 GList *last_elem = g_list_last (uri->path);
2419 if (last_elem->data == NULL) {
2420 uri->path = g_list_delete_link (uri->path, last_elem);
2423 rel_path_list = _gst_uri_string_to_list (relative_path, "/", FALSE, FALSE);
2424 /* if path was absolute, make it relative by removing initial NULL element */
2425 if (rel_path_list && rel_path_list->data == NULL) {
2426 rel_path_list = g_list_delete_link (rel_path_list, rel_path_list);
2428 uri->path = g_list_concat (uri->path, rel_path_list);
2433 * gst_uri_append_path_segment:
2434 * @uri: (transfer none)(nullable): The #GstUri to modify.
2435 * @path_segment: The path segment string to append to the URI path.
2437 * Append a single path segment onto the end of the URI path.
2439 * Returns: %TRUE if the path was appended successfully.
2444 gst_uri_append_path_segment (GstUri * uri, const gchar * path_segment)
2447 return path_segment == NULL;
2448 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2452 /* if base path ends in a directory (i.e. last element is NULL), remove it */
2453 if (uri->path && g_list_last (uri->path)->data == NULL) {
2454 uri->path = g_list_delete_link (uri->path, g_list_last (uri->path));
2456 uri->path = g_list_append (uri->path, g_strdup (path_segment));
2461 * gst_uri_get_query_string:
2462 * @uri: (nullable): The #GstUri to get the query string from.
2464 * Get a percent encoded URI query string from the @uri.
2466 * Returns: (transfer full): A percent encoded query string. Use g_free() when
2472 gst_uri_get_query_string (const GstUri * uri)
2474 GHashTableIter iter;
2475 gpointer key, value;
2476 const gchar *sep = "";
2482 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2486 ret = g_string_new (NULL);
2487 g_hash_table_iter_init (&iter, uri->query);
2488 while (g_hash_table_iter_next (&iter, &key, &value)) {
2489 g_string_append (ret, sep);
2490 escaped = _gst_uri_escape_http_query_element (key);
2491 g_string_append (ret, escaped);
2494 escaped = _gst_uri_escape_http_query_element (value);
2495 g_string_append_printf (ret, "=%s", escaped);
2501 return g_string_free (ret, FALSE);
2505 * gst_uri_set_query_string:
2506 * @uri: (transfer none)(nullable): The #GstUri to modify.
2507 * @query: The new percent encoded query string to use to populate the query
2508 * table, or use %NULL to unset the query table.
2510 * Sets or unsets the query table in the URI.
2512 * Returns: %TRUE if the query table was set successfully.
2517 gst_uri_set_query_string (GstUri * uri, const gchar * query)
2520 return query == NULL;
2522 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2525 g_hash_table_unref (uri->query);
2526 uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, TRUE);
2532 * gst_uri_get_query_table:
2533 * @uri: (nullable): The #GstUri to get the query table from.
2535 * Get the query table from the URI. Keys and values in the table are freed
2536 * with g_free when they are deleted. A value may be %NULL to indicate that
2537 * the key should appear in the query string in the URI, but does not have a
2538 * value. Free the returned #GHashTable with #g_hash_table_unref() when it is
2539 * no longer required. Modifying this hash table will modify the query in the
2542 * Returns: (transfer full)(element-type gchar* gchar*): The query hash table
2548 gst_uri_get_query_table (const GstUri * uri)
2552 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2556 return g_hash_table_ref (uri->query);
2560 * gst_uri_set_query_table:
2561 * @uri: (transfer none)(nullable): The #GstUri to modify.
2562 * @query_table: (transfer none)(nullable)(element-type gchar* gchar*): The new
2563 * query table to use.
2565 * Set the query table to use in the URI. The old table is unreferenced and a
2566 * reference to the new one is used instead. A value if %NULL for @query_table
2567 * will remove the query string from the URI.
2569 * Returns: %TRUE if the new table was sucessfully used for the query table.
2574 gst_uri_set_query_table (GstUri * uri, GHashTable * query_table)
2576 GHashTable *old_table = NULL;
2579 return query_table == NULL;
2580 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2582 old_table = uri->query;
2584 uri->query = g_hash_table_ref (query_table);
2588 g_hash_table_unref (old_table);
2594 * gst_uri_set_query_value:
2595 * @uri: (transfer none)(nullable): The #GstUri to modify.
2596 * @query_key: (transfer none): The key for the query entry.
2597 * @query_value: (transfer none)(nullable): The value for the key.
2599 * This inserts or replaces a key in the query table. A @query_value of %NULL
2600 * indicates that the key has no associated value, but will still be present in
2603 * Returns: %TRUE if the query table was sucessfully updated.
2608 gst_uri_set_query_value (GstUri * uri, const gchar * query_key,
2609 const gchar * query_value)
2613 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2616 uri->query = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2619 g_hash_table_insert (uri->query, g_strdup (query_key),
2620 g_strdup (query_value));
2626 * gst_uri_remove_query_key:
2627 * @uri: (transfer none)(nullable): The #GstUri to modify.
2628 * @query_key: The key to remove.
2630 * Remove an entry from the query table by key.
2632 * Returns: %TRUE if the key existed in the table and was removed.
2637 gst_uri_remove_query_key (GstUri * uri, const gchar * query_key)
2643 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2647 result = g_hash_table_remove (uri->query, query_key);
2648 /* if this was the last query entry, remove the query string completely */
2649 if (result && g_hash_table_size (uri->query) == 0) {
2650 g_hash_table_unref (uri->query);
2657 * gst_uri_query_has_key:
2658 * @uri: (nullable): The #GstUri to examine.
2659 * @query_key: The key to lookup.
2661 * Check if there is a query table entry for the @query_key key.
2663 * Returns: %TRUE if @query_key exists in the URI query table.
2668 gst_uri_query_has_key (const GstUri * uri, const gchar * query_key)
2672 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2676 return g_hash_table_contains (uri->query, query_key);
2680 * gst_uri_get_query_value:
2681 * @uri: (nullable): The #GstUri to examine.
2682 * @query_key: The key to lookup.
2684 * Get the value associated with the @query_key key. Will return %NULL if the
2685 * key has no value or if the key does not exist in the URI query table. Because
2686 * %NULL is returned for both missing keys and keys with no value, you should
2687 * use gst_uri_query_has_key() to determine if a key is present in the URI
2690 * Returns: The value for the given key, or %NULL if not found.
2695 gst_uri_get_query_value (const GstUri * uri, const gchar * query_key)
2699 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2703 return g_hash_table_lookup (uri->query, query_key);
2707 * gst_uri_get_query_keys:
2708 * @uri: (nullable): The #GstUri to examine.
2710 * Get a list of the query keys from the URI.
2712 * Returns: (transfer container)(element-type gchar*): A list of keys from
2713 * the URI query. Free the list with g_list_free().
2718 gst_uri_get_query_keys (const GstUri * uri)
2722 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2726 return g_hash_table_get_keys (uri->query);
2730 * gst_uri_get_fragment:
2731 * @uri: (nullable): This #GstUri object.
2733 * Get the fragment name from the URI or %NULL if it doesn't exist.
2734 * If @uri is %NULL then returns %NULL.
2736 * Returns: The host name from the #GstUri object or %NULL.
2741 gst_uri_get_fragment (const GstUri * uri)
2743 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2744 return (uri ? uri->fragment : NULL);
2748 * gst_uri_set_fragment:
2749 * @uri: (transfer none)(nullable): The #GstUri to modify.
2750 * @fragment: (nullable): The fragment string to set.
2752 * Sets the fragment string in the URI. Use a value of %NULL in @fragment to
2753 * unset the fragment string.
2755 * Returns: %TRUE if the fragment was set/unset successfully.
2760 gst_uri_set_fragment (GstUri * uri, const gchar * fragment)
2763 return fragment == NULL;
2764 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2766 g_free (uri->fragment);
2767 uri->fragment = g_strdup (fragment);