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 * @title: GstUriHandler
29 * @short_description: Interface to ease URI handling in plugins.
31 * The #GstURIHandler is an interface that is implemented by Source and Sink
32 * #GstElement to unify handling of URI.
34 * An application can use the following functions to quickly get an element
35 * that handles the given URI for reading or writing
36 * (gst_element_make_from_uri()).
38 * Source and Sink plugins should implement this interface when possible.
45 #include "gst_private.h"
49 #include "gstregistry.h"
51 #include "gst-i18n-lib.h"
55 #include <glib/gprintf.h>
57 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
58 #define GST_CAT_DEFAULT gst_uri_handler_debug
60 #ifndef HAVE_STRCASESTR
61 #define strcasestr _gst_ascii_strcasestr
63 /* From https://github.com/freebsd/freebsd/blob/master/contrib/file/src/strcasestr.c
64 * Updated to use GLib types and GLib string functions
66 * Copyright (c) 1990, 1993
67 * The Regents of the University of California. All rights reserved.
69 * This code is derived from software contributed to Berkeley by
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the University nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 * Find the first occurrence of find in s, ignore case.
102 _gst_ascii_strcasestr (const gchar * s, const gchar * find)
107 if ((c = *find++) != 0) {
108 c = g_ascii_tolower (c);
112 if ((sc = *s++) == 0)
114 } while (g_ascii_tolower (sc) != c);
115 } while (g_ascii_strncasecmp (s, find, len) != 0);
118 return (gchar *) (gintptr) (s);
123 gst_uri_handler_get_type (void)
125 static volatile gsize urihandler_type = 0;
127 if (g_once_init_enter (&urihandler_type)) {
129 static const GTypeInfo urihandler_info = {
130 sizeof (GstURIHandlerInterface),
142 _type = g_type_register_static (G_TYPE_INTERFACE,
143 "GstURIHandler", &urihandler_info, 0);
145 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
147 g_once_init_leave (&urihandler_type, _type);
149 return urihandler_type;
153 gst_uri_error_quark (void)
155 return g_quark_from_static_string ("gst-uri-error-quark");
158 #define HEX_ESCAPE '%'
160 #ifndef GST_REMOVE_DEPRECATED
161 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
162 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
163 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
164 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
165 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
166 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
167 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
172 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
173 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
174 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
175 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
176 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
177 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
178 } UnsafeCharacterSet;
180 /* Escape undesirable characters using %
181 * -------------------------------------
183 * This function takes a pointer to a string in which
184 * some characters may be unacceptable unescaped.
185 * It returns a string which has these characters
186 * represented by a '%' character followed by two hex digits.
188 * This routine returns a g_malloced string.
191 static const gchar hex[16] = "0123456789ABCDEF";
194 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
196 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
203 UnsafeCharacterSet use_mask;
205 g_return_val_if_fail (mask == UNSAFE_ALL
206 || mask == UNSAFE_ALLOW_PLUS
207 || mask == UNSAFE_PATH
208 || mask == UNSAFE_DOS_PATH
209 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
211 if (string == NULL) {
217 for (p = string; *p != '\0'; p++) {
219 if (!ACCEPTABLE_CHAR (c)) {
222 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
223 /* when escaping a host, if we hit something that needs to be escaped, or we finally
224 * hit a path separator, revert to path mode (the host segment of the url is over).
226 use_mask = UNSAFE_PATH;
230 result = g_malloc (p - string + unacceptable * 2 + 1);
233 for (q = result, p = string; *p != '\0'; p++) {
236 if (!ACCEPTABLE_CHAR (c)) {
237 *q++ = HEX_ESCAPE; /* means hex coming */
243 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
244 use_mask = UNSAFE_PATH;
257 return c >= '0' && c <= '9' ? c - '0'
258 : c >= 'A' && c <= 'F' ? c - 'A' + 10
259 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
263 unescape_character (const char *scanner)
268 first_digit = hex_to_int (*scanner++);
269 if (first_digit < 0) {
273 second_digit = hex_to_int (*scanner);
274 if (second_digit < 0) {
278 return (first_digit << 4) | second_digit;
282 * @escaped_string: an escaped URI, path, or other string
283 * @illegal_characters: a string containing a sequence of characters
284 * considered "illegal", '\0' is automatically in this list.
286 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
287 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
288 * for character 16x+y.
290 * Return value: (nullable): a newly allocated string with the
291 * unescaped equivalents, or %NULL if @escaped_string contained one of
292 * the characters in @illegal_characters.
295 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
301 if (escaped_string == NULL) {
305 result = g_malloc (strlen (escaped_string) + 1);
308 for (in = escaped_string; *in != '\0'; in++) {
310 if (*in == HEX_ESCAPE) {
311 character = unescape_character (in + 1);
313 /* Check for an illegal character. We consider '\0' illegal here. */
315 || (illegal_characters != NULL
316 && strchr (illegal_characters, (char) character) != NULL)) {
322 *out++ = (char) character;
326 g_assert ((gsize) (out - result) <= strlen (escaped_string));
333 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
335 gchar *check = (gchar *) uri;
337 g_assert (uri != NULL);
338 g_assert (endptr != NULL);
340 if (g_ascii_isalpha (*check)) {
342 while (g_ascii_isalnum (*check) || *check == '+'
343 || *check == '-' || *check == '.')
351 * gst_uri_protocol_is_valid:
352 * @protocol: A string
354 * Tests if the given string is a valid protocol identifier. Protocols
355 * must consist of alphanumeric characters, '+', '-' and '.' and must
356 * start with a alphabetic character. See RFC 3986 Section 3.1.
358 * Returns: %TRUE if the string is a valid protocol identifier, %FALSE otherwise.
361 gst_uri_protocol_is_valid (const gchar * protocol)
365 g_return_val_if_fail (protocol != NULL, FALSE);
367 gst_uri_protocol_check_internal (protocol, &endptr);
369 return *endptr == '\0' && ((gsize) (endptr - protocol)) >= 2;
376 * Tests if the given string is a valid URI identifier. URIs start with a valid
377 * scheme followed by ":" and maybe a string identifying the location.
379 * Returns: %TRUE if the string is a valid URI
382 gst_uri_is_valid (const gchar * uri)
386 g_return_val_if_fail (uri != NULL, FALSE);
388 gst_uri_protocol_check_internal (uri, &endptr);
390 return *endptr == ':' && ((gsize) (endptr - uri)) >= 2;
394 * gst_uri_get_protocol:
397 * Extracts the protocol out of a given valid URI. The returned string must be
398 * freed using g_free().
400 * Returns: (nullable): The protocol for this URI.
403 gst_uri_get_protocol (const gchar * uri)
407 g_return_val_if_fail (uri != NULL, NULL);
408 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
410 colon = strstr (uri, ":");
412 return g_ascii_strdown (uri, colon - uri);
416 * gst_uri_has_protocol:
418 * @protocol: a protocol string (e.g. "http")
420 * Checks if the protocol of a given valid URI matches @protocol.
422 * Returns: %TRUE if the protocol matches.
425 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
429 g_return_val_if_fail (uri != NULL, FALSE);
430 g_return_val_if_fail (protocol != NULL, FALSE);
431 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
433 colon = strstr (uri, ":");
438 return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
442 * gst_uri_get_location:
445 * Extracts the location out of a given valid URI, ie. the protocol and "://"
446 * are stripped from the URI, which means that the location returned includes
447 * the hostname if one is specified. The returned string must be freed using
450 * Free-function: g_free
452 * Returns: (transfer full) (nullable): the location for this URI. Returns
453 * %NULL if the URI isn't valid. If the URI does not contain a location, an
454 * empty string is returned.
457 gst_uri_get_location (const gchar * uri)
460 gchar *unescaped = NULL;
462 g_return_val_if_fail (uri != NULL, NULL);
463 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
465 colon = strstr (uri, "://");
469 unescaped = unescape_string (colon + 3, "/");
471 /* On Windows an URI might look like file:///c:/foo/bar.txt or
472 * file:///c|/foo/bar.txt (some Netscape versions) and we want to
473 * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
474 * Can't use g_filename_from_uri() here because it will only handle the
475 * file:// protocol */
477 if (unescaped != NULL && unescaped[0] == '/' &&
478 g_ascii_isalpha (unescaped[1]) &&
479 (unescaped[2] == ':' || unescaped[2] == '|')) {
481 memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
485 GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
492 * @protocol: Protocol for URI
493 * @location: (transfer none): Location for URI
495 * Constructs a URI for a given valid protocol and location.
497 * Free-function: g_free
499 * Returns: (transfer full): a new string for this URI. Returns %NULL if the
500 * given URI protocol is not valid, or the given location is %NULL.
502 * Deprecated: Use GstURI instead.
504 #ifndef GST_REMOVE_DEPRECATED
506 gst_uri_construct (const gchar * protocol, const gchar * location)
508 char *escaped, *proto_lowercase;
511 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
512 g_return_val_if_fail (location != NULL, NULL);
514 proto_lowercase = g_ascii_strdown (protocol, -1);
515 escaped = escape_string_internal (location, UNSAFE_PATH);
516 retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
518 g_free (proto_lowercase);
527 const gchar *protocol;
532 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
534 const gchar *const *protocols;
535 GstElementFactory *factory;
536 SearchEntry *entry = (SearchEntry *) search_entry;
538 if (!GST_IS_ELEMENT_FACTORY (feature))
540 factory = GST_ELEMENT_FACTORY_CAST (feature);
542 if (factory->uri_type != entry->type)
545 protocols = gst_element_factory_get_uri_protocols (factory);
547 if (protocols == NULL) {
548 g_warning ("Factory '%s' implements GstUriHandler interface but returned "
549 "no supported protocols!", gst_plugin_feature_get_name (feature));
553 while (*protocols != NULL) {
554 if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
562 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
564 return gst_plugin_feature_get_rank (second) -
565 gst_plugin_feature_get_rank (first);
569 get_element_factories_from_uri_protocol (const GstURIType type,
570 const gchar * protocol)
572 GList *possibilities;
575 g_return_val_if_fail (protocol, NULL);
578 entry.protocol = protocol;
579 possibilities = gst_registry_feature_filter (gst_registry_get (),
580 search_by_entry, FALSE, &entry);
582 return possibilities;
586 * gst_uri_protocol_is_supported:
587 * @type: Whether to check for a source or a sink
588 * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
590 * Checks if an element exists that supports the given URI protocol. Note
591 * that a positive return value does not imply that a subsequent call to
592 * gst_element_make_from_uri() is guaranteed to work.
597 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
599 GList *possibilities;
601 g_return_val_if_fail (protocol, FALSE);
603 possibilities = get_element_factories_from_uri_protocol (type, protocol);
606 g_list_free (possibilities);
613 * gst_element_make_from_uri:
614 * @type: Whether to create a source or a sink
615 * @uri: URI to create an element for
616 * @elementname: (allow-none): Name of created element, can be %NULL.
617 * @error: (allow-none): address where to store error information, or %NULL.
619 * Creates an element for handling the given URI.
621 * Returns: (transfer floating) (nullable): a new element or %NULL if none
625 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
626 const gchar * elementname, GError ** error)
628 GList *possibilities, *walk;
630 GstElement *ret = NULL;
632 g_return_val_if_fail (gst_is_initialized (), NULL);
633 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
634 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
635 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
637 GST_DEBUG ("type:%d, uri:%s, elementname:%s", type, uri, elementname);
639 protocol = gst_uri_get_protocol (uri);
640 possibilities = get_element_factories_from_uri_protocol (type, protocol);
642 if (!possibilities) {
643 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
645 /* The error message isn't great, but we don't expect applications to
646 * show that error to users, but call the missing plugins functions */
647 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
648 _("No URI handler for the %s protocol found"), protocol);
654 possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
655 walk = possibilities;
657 GstElementFactory *factory = walk->data;
658 GError *uri_err = NULL;
660 ret = gst_element_factory_create (factory, elementname);
662 GstURIHandler *handler = GST_URI_HANDLER (ret);
664 if (gst_uri_handler_set_uri (handler, uri, &uri_err))
667 GST_WARNING ("%s didn't accept URI '%s': %s", GST_OBJECT_NAME (ret), uri,
670 if (error != NULL && *error == NULL)
671 g_propagate_error (error, uri_err);
673 g_error_free (uri_err);
675 gst_object_unref (ret);
680 gst_plugin_feature_list_free (possibilities);
682 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
683 type == GST_URI_SINK ? "sink" : "source", uri);
685 /* if the first handler didn't work, but we found another one that works */
687 g_clear_error (error);
693 * gst_uri_handler_get_uri_type:
694 * @handler: A #GstURIHandler.
696 * Gets the type of the given URI handler
698 * Returns: the #GstURIType of the URI handler.
699 * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
702 gst_uri_handler_get_uri_type (GstURIHandler * handler)
704 GstURIHandlerInterface *iface;
707 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
709 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
710 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
711 g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
713 ret = iface->get_type (G_OBJECT_TYPE (handler));
714 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
720 * gst_uri_handler_get_protocols:
721 * @handler: A #GstURIHandler.
723 * Gets the list of protocols supported by @handler. This list may not be
726 * Returns: (transfer none) (element-type utf8) (nullable): the
727 * supported protocols. Returns %NULL if the @handler isn't
728 * implemented properly, or the @handler doesn't support any
732 gst_uri_handler_get_protocols (GstURIHandler * handler)
734 GstURIHandlerInterface *iface;
735 const gchar *const *ret;
737 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
739 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
740 g_return_val_if_fail (iface != NULL, NULL);
741 g_return_val_if_fail (iface->get_protocols != NULL, NULL);
743 ret = iface->get_protocols (G_OBJECT_TYPE (handler));
744 g_return_val_if_fail (ret != NULL, NULL);
750 * gst_uri_handler_get_uri:
751 * @handler: A #GstURIHandler
753 * Gets the currently handled URI.
755 * Returns: (transfer full) (nullable): the URI currently handled by
756 * the @handler. Returns %NULL if there are no URI currently
757 * handled. The returned string must be freed with g_free() when no
761 gst_uri_handler_get_uri (GstURIHandler * handler)
763 GstURIHandlerInterface *iface;
766 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
768 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
769 g_return_val_if_fail (iface != NULL, NULL);
770 g_return_val_if_fail (iface->get_uri != NULL, NULL);
771 ret = iface->get_uri (handler);
773 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
779 * gst_uri_handler_set_uri:
780 * @handler: A #GstURIHandler
782 * @error: (allow-none): address where to store a #GError in case of
785 * Tries to set the URI of the given handler.
787 * Returns: %TRUE if the URI was set successfully, else %FALSE.
790 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
793 GstURIHandlerInterface *iface;
797 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
798 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
799 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
801 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
802 g_return_val_if_fail (iface != NULL, FALSE);
803 g_return_val_if_fail (iface->set_uri != NULL, FALSE);
805 protocol = gst_uri_get_protocol (uri);
807 if (iface->get_protocols) {
808 const gchar *const *protocols;
809 const gchar *const *p;
810 gboolean found_protocol = FALSE;
812 protocols = iface->get_protocols (G_OBJECT_TYPE (handler));
813 if (protocols != NULL) {
814 for (p = protocols; *p != NULL; ++p) {
815 if (g_ascii_strcasecmp (protocol, *p) == 0) {
816 found_protocol = TRUE;
821 if (!found_protocol) {
822 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
823 _("URI scheme '%s' not supported"), protocol);
830 ret = iface->set_uri (handler, uri, error);
838 gst_file_utils_canonicalise_path (const gchar * path)
840 gchar **parts, **p, *clean_path;
844 GST_WARNING ("FIXME: canonicalise win32 path");
845 return g_strdup (path);
849 parts = g_strsplit (path, "/", -1);
853 if (strcmp (*p, ".") == 0) {
854 /* just move all following parts on top of this, incl. NUL terminator */
856 memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
857 /* re-check the new current part again in the next iteration */
859 } else if (strcmp (*p, "..") == 0 && p > parts) {
860 /* just move all following parts on top of the previous part, incl.
864 memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
865 /* re-check the new current part again in the next iteration */
874 num_parts = g_strv_length (parts) + 1; /* incl. terminator */
875 parts = g_renew (gchar *, parts, num_parts + 1);
876 memmove (parts + 1, parts, num_parts * sizeof (gchar *));
877 parts[0] = g_strdup ("/");
880 clean_path = g_build_filenamev (parts);
886 file_path_contains_relatives (const gchar * path)
888 return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
889 strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
890 strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
894 * gst_filename_to_uri:
895 * @filename: (type filename): absolute or relative file name path
896 * @error: pointer to error, or %NULL
898 * Similar to g_filename_to_uri(), but attempts to handle relative file paths
899 * as well. Before converting @filename into an URI, it will be prefixed by
900 * the current working directory if it is a relative path, and then the path
901 * will be canonicalised so that it doesn't contain any './' or '../' segments.
903 * On Windows #filename should be in UTF-8 encoding.
905 * Returns: newly-allocated URI string, or NULL on error. The caller must
906 * free the URI string with g_free() when no longer needed.
909 gst_filename_to_uri (const gchar * filename, GError ** error)
911 gchar *abs_location = NULL;
912 gchar *uri, *abs_clean;
914 g_return_val_if_fail (filename != NULL, NULL);
915 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
917 if (g_path_is_absolute (filename)) {
918 if (!file_path_contains_relatives (filename)) {
919 uri = g_filename_to_uri (filename, NULL, error);
923 abs_location = g_strdup (filename);
927 cwd = g_get_current_dir ();
928 abs_location = g_build_filename (cwd, filename, NULL);
931 if (!file_path_contains_relatives (abs_location)) {
932 uri = g_filename_to_uri (abs_location, NULL, error);
937 /* path is now absolute, but contains '.' or '..' */
938 abs_clean = gst_file_utils_canonicalise_path (abs_location);
939 GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
940 uri = g_filename_to_uri (abs_clean, NULL, error);
945 g_free (abs_location);
946 GST_DEBUG ("'%s' -> '%s'", filename, uri);
950 /****************************************************************************
951 * GstUri - GstMiniObject to parse and merge URIs according to IETF RFC 3986
952 ****************************************************************************/
957 * @short_description: URI parsing and manipulation.
959 * A #GstUri object can be used to parse and split a URI string into its
960 * constituant parts. Two #GstUri objects can be joined to make a new #GstUri
961 * using the algorithm described in RFC3986.
964 /* Definition for GstUri object */
968 GstMiniObject mini_object;
978 GST_DEFINE_MINI_OBJECT_TYPE (GstUri, gst_uri);
980 static GstUri *_gst_uri_copy (const GstUri * uri);
981 static void _gst_uri_free (GstUri * uri);
982 static GstUri *_gst_uri_new (void);
983 static GList *_remove_dot_segments (GList * path);
985 /* private GstUri functions */
992 g_return_val_if_fail (gst_is_initialized (), NULL);
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);
1220 out = g_list_delete_link (out, elem);
1222 /* path ends in '/..' We need to keep the last '/' */
1232 _gst_uri_escape_userinfo (const gchar * userinfo)
1234 return g_uri_escape_string (userinfo,
1235 G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, FALSE);
1239 _gst_uri_escape_host (const gchar * host)
1241 return g_uri_escape_string (host,
1242 G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, FALSE);
1246 _gst_uri_escape_host_colon (const gchar * host)
1248 return g_uri_escape_string (host,
1249 G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":", FALSE);
1253 _gst_uri_escape_path_segment (const gchar * segment)
1255 return g_uri_escape_string (segment,
1256 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, FALSE);
1260 _gst_uri_escape_http_query_element (const gchar * element)
1264 ret = g_uri_escape_string (element, "!$'()*,;:@/? ", FALSE);
1265 for (c = ret; *c; c++)
1272 _gst_uri_escape_fragment (const gchar * fragment)
1274 return g_uri_escape_string (fragment,
1275 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH "?", FALSE);
1279 _gst_uri_string_to_list (const gchar * str, const gchar * sep, gboolean convert,
1282 GList *new_list = NULL;
1285 guint pct_sep_len = 0;
1286 gchar *pct_sep = NULL;
1289 if (convert && !unescape) {
1290 pct_sep = g_strdup_printf ("%%%2.2X", (guint) (*sep));
1294 split_str = g_strsplit (str, sep, -1);
1297 for (next_elem = split_str; *next_elem; next_elem += 1) {
1298 gchar *elem = *next_elem;
1299 if (*elem == '\0') {
1300 new_list = g_list_append (new_list, NULL);
1302 if (convert && !unescape) {
1304 for (next_sep = strcasestr (elem, pct_sep); next_sep;
1305 next_sep = strcasestr (next_sep + 1, pct_sep)) {
1307 memmove (next_sep + 1, next_sep + pct_sep_len,
1308 strlen (next_sep + pct_sep_len) + 1);
1312 *next_elem = g_uri_unescape_string (elem, NULL);
1316 new_list = g_list_append (new_list, g_strdup (elem));
1320 g_strfreev (split_str);
1321 if (convert && !unescape)
1329 _gst_uri_string_to_table (const gchar * str, const gchar * part_sep,
1330 const gchar * kv_sep, gboolean convert, gboolean unescape)
1332 GHashTable *new_table = NULL;
1335 gchar *pct_part_sep = NULL, *pct_kv_sep = NULL;
1336 gchar **split_parts;
1338 new_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1340 if (convert && !unescape) {
1341 pct_part_sep = g_strdup_printf ("%%%2.2X", (guint) (*part_sep));
1342 pct_kv_sep = g_strdup_printf ("%%%2.2X", (guint) (*kv_sep));
1345 split_parts = g_strsplit (str, part_sep, -1);
1348 for (next_part = split_parts; *next_part; next_part += 1) {
1349 gchar *part = *next_part;
1352 /* if we are converting percent encoded versions of separators then
1353 * substitute the part separator now. */
1354 if (convert && !unescape) {
1356 for (next_sep = strcasestr (part, pct_part_sep); next_sep;
1357 next_sep = strcasestr (next_sep + 1, pct_part_sep)) {
1358 *next_sep = *part_sep;
1359 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1362 /* find the key/value separator within the part */
1363 kv_sep_pos = g_strstr_len (part, -1, kv_sep);
1364 if (kv_sep_pos == NULL) {
1366 key = g_uri_unescape_string (part, NULL);
1368 key = g_strdup (part);
1373 key = g_uri_unescape_segment (part, kv_sep_pos, NULL);
1374 value = g_uri_unescape_string (kv_sep_pos + 1, NULL);
1376 key = g_strndup (part, kv_sep_pos - part);
1377 value = g_strdup (kv_sep_pos + 1);
1380 /* if we are converting percent encoded versions of separators then
1381 * substitute the key/value separator in both key and value now. */
1382 if (convert && !unescape) {
1384 for (next_sep = strcasestr (key, pct_kv_sep); next_sep;
1385 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1386 *next_sep = *kv_sep;
1387 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1390 for (next_sep = strcasestr (value, pct_kv_sep); next_sep;
1391 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1392 *next_sep = *kv_sep;
1393 memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1397 /* add value to the table */
1398 g_hash_table_insert (new_table, key, value);
1402 g_strfreev (split_parts);
1403 if (convert && !unescape) {
1404 g_free (pct_part_sep);
1405 g_free (pct_kv_sep);
1414 * Method definitions.
1419 * @scheme: (nullable): The scheme for the new URI.
1420 * @userinfo: (nullable): The user-info for the new URI.
1421 * @host: (nullable): The host name for the new URI.
1422 * @port: The port number for the new URI or %GST_URI_NO_PORT.
1423 * @path: (nullable): The path for the new URI with '/' separating path
1425 * @query: (nullable): The query string for the new URI with '&' separating
1426 * query elements. Elements containing '&' characters
1427 * should encode them as "%26".
1428 * @fragment: (nullable): The fragment name for the new URI.
1430 * Creates a new #GstUri object with the given URI parts. The path and query
1431 * strings will be broken down into their elements. All strings should not be
1432 * escaped except where indicated.
1434 * Returns: (transfer full): A new #GstUri object.
1439 gst_uri_new (const gchar * scheme, const gchar * userinfo, const gchar * host,
1440 guint port, const gchar * path, const gchar * query, const gchar * fragment)
1444 new_uri = _gst_uri_new ();
1446 new_uri->scheme = g_strdup (scheme);
1447 new_uri->userinfo = g_strdup (userinfo);
1448 new_uri->host = g_strdup (host);
1449 new_uri->port = port;
1450 new_uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
1451 new_uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, FALSE);
1452 new_uri->fragment = g_strdup (fragment);
1459 * gst_uri_new_with_base:
1460 * @base: (transfer none)(nullable): The base URI to join the new URI to.
1461 * @scheme: (nullable): The scheme for the new URI.
1462 * @userinfo: (nullable): The user-info for the new URI.
1463 * @host: (nullable): The host name for the new URI.
1464 * @port: The port number for the new URI or %GST_URI_NO_PORT.
1465 * @path: (nullable): The path for the new URI with '/' separating path
1467 * @query: (nullable): The query string for the new URI with '&' separating
1468 * query elements. Elements containing '&' characters
1469 * should encode them as "%26".
1470 * @fragment: (nullable): The fragment name for the new URI.
1472 * Like gst_uri_new(), but joins the new URI onto a base URI.
1474 * Returns: (transfer full): The new URI joined onto @base.
1479 gst_uri_new_with_base (GstUri * base, const gchar * scheme,
1480 const gchar * userinfo, const gchar * host, guint port, const gchar * path,
1481 const gchar * query, const gchar * fragment)
1483 GstUri *new_rel_uri;
1486 g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1488 new_rel_uri = gst_uri_new (scheme, userinfo, host, port, path, query,
1490 new_uri = gst_uri_join (base, new_rel_uri);
1491 gst_uri_unref (new_rel_uri);
1497 * gst_uri_from_string:
1498 * @uri: The URI string to parse.
1500 * Parses a URI string into a new #GstUri object. Will return NULL if the URI
1503 * Returns: (transfer full) (nullable): A new #GstUri object, or NULL.
1508 gst_uri_from_string (const gchar * uri)
1510 const gchar *orig_uri = uri;
1513 uri_obj = _gst_uri_new ();
1515 if (uri_obj && uri != NULL) {
1518 /* be helpful and skip initial white space */
1519 while (*uri == '\v' || g_ascii_isspace (*uri))
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, *reoh;
1536 /* get authority [userinfo@]host[:port] */
1538 /* find end of authority */
1539 eoa = uri + strcspn (uri, "/?#");
1541 /* find end of userinfo */
1542 eoui = strchr (uri, '@');
1543 if (eoui != NULL && eoui < eoa) {
1544 uri_obj->userinfo = g_uri_unescape_segment (uri, eoui, NULL);
1547 /* find end of host */
1548 if (uri[0] == '[') {
1549 eoh = strchr (uri, ']');
1550 if (eoh == NULL || eoh > eoa) {
1551 GST_DEBUG ("Unable to parse the host part of the URI '%s'.",
1553 gst_uri_unref (uri_obj);
1559 reoh = eoh = strchr (uri, ':');
1560 if (eoh == NULL || eoh > eoa)
1563 /* don't capture empty host strings */
1565 uri_obj->host = g_uri_unescape_segment (uri, eoh, NULL);
1569 /* if port number is malformed then we can't parse this */
1570 if (uri[0] != ':' || strspn (uri + 1, "0123456789") != eoa - uri - 1) {
1571 GST_DEBUG ("Unable to parse host/port part of the URI '%s'.",
1573 gst_uri_unref (uri_obj);
1576 /* otherwise treat port as unsigned decimal number */
1579 uri_obj->port = uri_obj->port * 10 + g_ascii_digit_value (*uri);
1585 if (uri != NULL && uri[0] != '\0') {
1588 len = strcspn (uri, "?#");
1589 if (uri[len] == '\0') {
1590 uri_obj->path = _gst_uri_string_to_list (uri, "/", FALSE, TRUE);
1594 gchar *path_str = g_strndup (uri, len);
1595 uri_obj->path = _gst_uri_string_to_list (path_str, "/", FALSE, TRUE);
1601 if (uri != NULL && uri[0] == '?') {
1604 eoq = strchr (++uri, '#');
1606 uri_obj->query = _gst_uri_string_to_table (uri, "&", "=", TRUE, TRUE);
1610 gchar *query_str = g_strndup (uri, eoq - uri);
1611 uri_obj->query = _gst_uri_string_to_table (query_str, "&", "=", TRUE,
1618 if (uri != NULL && uri[0] == '#') {
1619 uri_obj->fragment = g_uri_unescape_string (uri + 1, NULL);
1627 * gst_uri_from_string_with_base:
1628 * @base: (transfer none)(nullable): The base URI to join the new URI with.
1629 * @uri: The URI string to parse.
1631 * Like gst_uri_from_string() but also joins with a base URI.
1633 * Returns: (transfer full): A new #GstUri object.
1638 gst_uri_from_string_with_base (GstUri * base, const gchar * uri)
1640 GstUri *new_rel_uri;
1643 g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1645 new_rel_uri = gst_uri_from_string (uri);
1646 new_uri = gst_uri_join (base, new_rel_uri);
1647 gst_uri_unref (new_rel_uri);
1654 * @first: First #GstUri to compare.
1655 * @second: Second #GstUri to compare.
1657 * Compares two #GstUri objects to see if they represent the same normalized
1660 * Returns: %TRUE if the normalized versions of the two URI's would be equal.
1665 gst_uri_equal (const GstUri * first, const GstUri * second)
1667 gchar *first_norm = NULL, *second_norm = NULL;
1668 GList *first_norm_list = NULL, *second_norm_list = NULL;
1669 const gchar *first_cmp, *second_cmp;
1670 GHashTableIter table_iter;
1671 gpointer key, value;
1674 g_return_val_if_fail ((first == NULL || GST_IS_URI (first)) &&
1675 (second == NULL || GST_IS_URI (second)), FALSE);
1677 if (first == second)
1680 if (first == NULL || second == NULL)
1683 if (first->port != second->port)
1686 /* work out a version of field value (normalized or not) to compare.
1687 * first_cmp, second_cmp will be the values to compare later.
1688 * first_norm, second_norm will be non-NULL if normalized versions are used,
1689 * and need to be freed later.
1691 #define GST_URI_NORMALIZED_FIELD(pos, field, norm_fn, flags) \
1692 pos##_cmp = pos->field; \
1693 if (_gst_uri_first_non_normalized_char ((gchar*)pos##_cmp, flags) != NULL) { \
1694 pos##_norm = g_strdup (pos##_cmp); \
1695 norm_fn (pos##_norm); \
1696 pos##_cmp = pos##_norm; \
1699 /* compare two string values, normalizing if needed */
1700 #define GST_URI_NORMALIZED_CMP_STR(field, norm_fn, flags) \
1701 GST_URI_NORMALIZED_FIELD (first, field, norm_fn, flags) \
1702 GST_URI_NORMALIZED_FIELD (second, field, norm_fn, flags) \
1703 result = g_strcmp0 (first_cmp, second_cmp); \
1704 g_free (first_norm); \
1705 first_norm = NULL; \
1706 g_free (second_norm); \
1707 second_norm = NULL; \
1708 if (result != 0) return FALSE
1710 /* compare two string values */
1711 #define GST_URI_CMP_STR(field) \
1712 if (g_strcmp0 (first->field, second->field) != 0) return FALSE
1714 /* compare two GLists, normalize lists if needed before comparison */
1715 #define GST_URI_NORMALIZED_CMP_LIST(field, norm_fn, copy_fn, cmp_fn, free_fn) \
1716 first_norm_list = g_list_copy_deep (first->field, (GCopyFunc) copy_fn, NULL); \
1717 norm_fn (&first_norm_list); \
1718 second_norm_list = g_list_copy_deep (second->field, (GCopyFunc) copy_fn, NULL); \
1719 norm_fn (&second_norm_list); \
1720 result = _gst_uri_compare_lists (first_norm_list, second_norm_list, (GCompareFunc) cmp_fn); \
1721 g_list_free_full (first_norm_list, free_fn); \
1722 g_list_free_full (second_norm_list, free_fn); \
1723 if (result != 0) return FALSE
1725 GST_URI_CMP_STR (userinfo);
1727 GST_URI_CMP_STR (fragment);
1729 GST_URI_NORMALIZED_CMP_STR (scheme, _gst_uri_normalize_scheme,
1730 _GST_URI_NORMALIZE_LOWERCASE);
1732 GST_URI_NORMALIZED_CMP_STR (host, _gst_uri_normalize_hostname,
1733 _GST_URI_NORMALIZE_LOWERCASE);
1735 GST_URI_NORMALIZED_CMP_LIST (path, _gst_uri_normalize_path, g_strdup,
1738 if (first->query == NULL && second->query != NULL)
1740 if (first->query != NULL && second->query == NULL)
1742 if (first->query != NULL) {
1743 if (g_hash_table_size (first->query) != g_hash_table_size (second->query))
1746 g_hash_table_iter_init (&table_iter, first->query);
1747 while (g_hash_table_iter_next (&table_iter, &key, &value)) {
1748 if (!g_hash_table_contains (second->query, key))
1750 result = g_strcmp0 (g_hash_table_lookup (second->query, key), value);
1755 #undef GST_URI_NORMALIZED_CMP_STR
1756 #undef GST_URI_CMP_STR
1757 #undef GST_URI_NORMALIZED_CMP_LIST
1758 #undef GST_URI_NORMALIZED_FIELD
1765 * @base_uri: (transfer none) (nullable): The base URI to join another to.
1766 * @ref_uri: (transfer none) (nullable): The reference URI to join onto the
1769 * Join a reference URI onto a base URI using the method from RFC 3986.
1770 * If either URI is %NULL then the other URI will be returned with the ref count
1773 * Returns: (transfer full) (nullable): A #GstUri which represents the base
1774 * with the reference URI joined on.
1779 gst_uri_join (GstUri * base_uri, GstUri * ref_uri)
1781 const gchar *r_scheme;
1784 g_return_val_if_fail ((base_uri == NULL || GST_IS_URI (base_uri)) &&
1785 (ref_uri == NULL || GST_IS_URI (ref_uri)), NULL);
1787 if (base_uri == NULL && ref_uri == NULL)
1789 if (base_uri == NULL) {
1790 g_return_val_if_fail (GST_IS_URI (ref_uri), NULL);
1791 return gst_uri_ref (ref_uri);
1793 if (ref_uri == NULL) {
1794 g_return_val_if_fail (GST_IS_URI (base_uri), NULL);
1795 return gst_uri_ref (base_uri);
1798 g_return_val_if_fail (GST_IS_URI (base_uri) && GST_IS_URI (ref_uri), NULL);
1800 t = _gst_uri_new ();
1805 /* process according to RFC3986 */
1806 r_scheme = ref_uri->scheme;
1807 if (r_scheme != NULL && g_strcmp0 (base_uri->scheme, r_scheme) == 0) {
1810 if (r_scheme != NULL) {
1811 t->scheme = g_strdup (r_scheme);
1812 t->userinfo = g_strdup (ref_uri->userinfo);
1813 t->host = g_strdup (ref_uri->host);
1814 t->port = ref_uri->port;
1815 t->path = _remove_dot_segments (ref_uri->path);
1816 t->query = _gst_uri_copy_query_table (ref_uri->query);
1818 if (ref_uri->host != NULL) {
1819 t->userinfo = g_strdup (ref_uri->userinfo);
1820 t->host = g_strdup (ref_uri->host);
1821 t->port = ref_uri->port;
1822 t->path = _remove_dot_segments (ref_uri->path);
1823 t->query = _gst_uri_copy_query_table (ref_uri->query);
1825 if (ref_uri->path == NULL) {
1826 t->path = g_list_copy_deep (base_uri->path, (GCopyFunc) g_strdup, NULL);
1827 if (ref_uri->query != NULL)
1828 t->query = _gst_uri_copy_query_table (ref_uri->query);
1830 t->query = _gst_uri_copy_query_table (base_uri->query);
1832 if (ref_uri->path->data == NULL)
1833 t->path = _remove_dot_segments (ref_uri->path);
1835 GList *mrgd = _merge (base_uri->path, ref_uri->path);
1836 t->path = _remove_dot_segments (mrgd);
1837 g_list_free_full (mrgd, g_free);
1839 t->query = _gst_uri_copy_query_table (ref_uri->query);
1841 t->userinfo = g_strdup (base_uri->userinfo);
1842 t->host = g_strdup (base_uri->host);
1843 t->port = base_uri->port;
1845 t->scheme = g_strdup (base_uri->scheme);
1847 t->fragment = g_strdup (ref_uri->fragment);
1853 * gst_uri_join_strings:
1854 * @base_uri: The percent-encoded base URI.
1855 * @ref_uri: The percent-encoded reference URI to join to the @base_uri.
1857 * This is a convenience function to join two URI strings and return the result.
1858 * The returned string should be g_free()'d after use.
1860 * Returns: (transfer full): A string representing the percent-encoded join of
1866 gst_uri_join_strings (const gchar * base_uri, const gchar * ref_uri)
1868 GstUri *base, *result;
1871 base = gst_uri_from_string (base_uri);
1872 result = gst_uri_from_string_with_base (base, ref_uri);
1873 result_uri = gst_uri_to_string (result);
1874 gst_uri_unref (base);
1875 gst_uri_unref (result);
1881 * gst_uri_is_writable:
1882 * @uri: The #GstUri object to test.
1884 * Check if it is safe to write to this #GstUri.
1886 * Check if the refcount of @uri is exactly 1, meaning that no other
1887 * reference exists to the #GstUri and that the #GstUri is therefore writable.
1889 * Modification of a #GstUri should only be done after verifying that it is
1892 * Returns: %TRUE if it is safe to write to the object.
1897 gst_uri_is_writable (const GstUri * uri)
1899 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
1900 return gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (uri));
1904 * gst_uri_make_writable:
1905 * @uri: (transfer full): The #GstUri object to make writable.
1907 * Make the #GstUri writable.
1909 * Checks if @uri is writable, and if so the original object is returned. If
1910 * not, then a writable copy is made and returned. This gives away the
1911 * reference to @uri and returns a reference to the new #GstUri.
1912 * If @uri is %NULL then %NULL is returned.
1914 * Returns: (transfer full): A writable version of @uri.
1919 gst_uri_make_writable (GstUri * uri)
1921 g_return_val_if_fail (GST_IS_URI (uri), NULL);
1923 GST_URI_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (uri)));
1927 * gst_uri_to_string:
1928 * @uri: This #GstUri to convert to a string.
1930 * Convert the URI to a string.
1932 * Returns the URI as held in this object as a #gchar* nul-terminated string.
1933 * The caller should g_free() the string once they are finished with it.
1934 * The string is put together as described in RFC 3986.
1936 * Returns: (transfer full): The string version of the URI.
1941 gst_uri_to_string (const GstUri * uri)
1946 g_return_val_if_fail (GST_IS_URI (uri), NULL);
1948 uri_str = g_string_new (NULL);
1950 if (uri->scheme != NULL)
1951 g_string_append_printf (uri_str, "%s:", uri->scheme);
1953 if (uri->userinfo != NULL || uri->host != NULL ||
1954 uri->port != GST_URI_NO_PORT)
1955 g_string_append (uri_str, "//");
1957 if (uri->userinfo != NULL) {
1958 escaped = _gst_uri_escape_userinfo (uri->userinfo);
1959 g_string_append_printf (uri_str, "%s@", escaped);
1963 if (uri->host != NULL) {
1964 if (strchr (uri->host, ':') != NULL) {
1965 escaped = _gst_uri_escape_host_colon (uri->host);
1966 g_string_append_printf (uri_str, "[%s]", escaped);
1969 escaped = _gst_uri_escape_host (uri->host);
1970 g_string_append (uri_str, escaped);
1975 if (uri->port != GST_URI_NO_PORT)
1976 g_string_append_printf (uri_str, ":%u", uri->port);
1978 if (uri->path != NULL) {
1979 escaped = gst_uri_get_path_string (uri);
1980 g_string_append (uri_str, escaped);
1985 g_string_append (uri_str, "?");
1986 escaped = gst_uri_get_query_string (uri);
1987 g_string_append (uri_str, escaped);
1991 if (uri->fragment != NULL) {
1992 escaped = _gst_uri_escape_fragment (uri->fragment);
1993 g_string_append_printf (uri_str, "#%s", escaped);
1997 return g_string_free (uri_str, FALSE);
2001 * gst_uri_is_normalized:
2002 * @uri: The #GstUri to test to see if it is normalized.
2004 * Tests the @uri to see if it is normalized. A %NULL @uri is considered to be
2007 * Returns: TRUE if the URI is normalized or is %NULL.
2012 gst_uri_is_normalized (const GstUri * uri)
2020 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2022 /* check for non-normalized characters in uri parts */
2023 if (_gst_uri_first_non_normalized_char (uri->scheme,
2024 _GST_URI_NORMALIZE_LOWERCASE) != NULL ||
2025 /*_gst_uri_first_non_normalized_char (uri->userinfo,
2026 _GST_URI_NORMALIZE_PERCENTAGES) != NULL || */
2027 _gst_uri_first_non_normalized_char (uri->host,
2028 _GST_URI_NORMALIZE_LOWERCASE /*| _GST_URI_NORMALIZE_PERCENTAGES */ )
2030 /*|| _gst_uri_first_non_normalized_char (uri->path,
2031 _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2032 || _gst_uri_first_non_normalized_char (uri->query,
2033 _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2034 || _gst_uri_first_non_normalized_char (uri->fragment,
2035 _GST_URI_NORMALIZE_PERCENTAGES) != NULL */ )
2038 /* also check path has had dot segments removed */
2039 new_path = _remove_dot_segments (uri->path);
2041 (_gst_uri_compare_lists (new_path, uri->path,
2042 (GCompareFunc) g_strcmp0) == 0);
2043 g_list_free_full (new_path, g_free);
2048 * gst_uri_normalize:
2049 * @uri: (transfer none): The #GstUri to normalize.
2051 * Normalization will remove extra path segments ("." and "..") from the URI. It
2052 * will also convert the scheme and host name to lower case and any
2053 * percent-encoded values to uppercase.
2055 * The #GstUri object must be writable. Check with gst_uri_is_writable() or use
2056 * gst_uri_make_writable() first.
2058 * Returns: TRUE if the URI was modified.
2063 gst_uri_normalize (GstUri * uri)
2065 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2067 return _gst_uri_normalize_scheme (uri->scheme) |
2068 _gst_uri_normalize_userinfo (uri->userinfo) |
2069 _gst_uri_normalize_hostname (uri->host) |
2070 _gst_uri_normalize_path (&uri->path) |
2071 _gst_uri_normalize_query (uri->query) |
2072 _gst_uri_normalize_fragment (uri->fragment);
2076 * gst_uri_get_scheme:
2077 * @uri: (nullable): This #GstUri object.
2079 * Get the scheme name from the URI or %NULL if it doesn't exist.
2080 * If @uri is %NULL then returns %NULL.
2082 * Returns: (nullable): The scheme from the #GstUri object or %NULL.
2085 gst_uri_get_scheme (const GstUri * uri)
2087 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2088 return (uri ? uri->scheme : NULL);
2092 * gst_uri_set_scheme:
2093 * @uri: (transfer none)(nullable): The #GstUri to modify.
2094 * @scheme: The new scheme to set or %NULL to unset the scheme.
2096 * Set or unset the scheme for the URI.
2098 * Returns: %TRUE if the scheme was set/unset successfully.
2103 gst_uri_set_scheme (GstUri * uri, const gchar * scheme)
2106 return scheme == NULL;
2107 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2109 g_free (uri->scheme);
2110 uri->scheme = g_strdup (scheme);
2116 * gst_uri_get_userinfo:
2117 * @uri: (nullable): This #GstUri object.
2119 * Get the userinfo (usually in the form "username:password") from the URI
2120 * or %NULL if it doesn't exist. If @uri is %NULL then returns %NULL.
2122 * Returns: (nullable): The userinfo from the #GstUri object or %NULL.
2127 gst_uri_get_userinfo (const GstUri * uri)
2129 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2130 return (uri ? uri->userinfo : NULL);
2134 * gst_uri_set_userinfo:
2135 * @uri: (transfer none)(nullable): The #GstUri to modify.
2136 * @userinfo: The new user-information string to set or %NULL to unset.
2138 * Set or unset the user information for the URI.
2140 * Returns: %TRUE if the user information was set/unset successfully.
2145 gst_uri_set_userinfo (GstUri * uri, const gchar * userinfo)
2148 return userinfo == NULL;
2149 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2151 g_free (uri->userinfo);
2152 uri->userinfo = g_strdup (userinfo);
2159 * @uri: (nullable): This #GstUri object.
2161 * Get the host name from the URI or %NULL if it doesn't exist.
2162 * If @uri is %NULL then returns %NULL.
2164 * Returns: (nullable): The host name from the #GstUri object or %NULL.
2169 gst_uri_get_host (const GstUri * uri)
2171 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2172 return (uri ? uri->host : NULL);
2177 * @uri: (transfer none)(nullable): The #GstUri to modify.
2178 * @host: The new host string to set or %NULL to unset.
2180 * Set or unset the host for the URI.
2182 * Returns: %TRUE if the host was set/unset successfully.
2187 gst_uri_set_host (GstUri * uri, const gchar * host)
2190 return host == NULL;
2191 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2194 uri->host = g_strdup (host);
2201 * @uri: (nullable): This #GstUri object.
2203 * Get the port number from the URI or %GST_URI_NO_PORT if it doesn't exist.
2204 * If @uri is %NULL then returns %GST_URI_NO_PORT.
2206 * Returns: The port number from the #GstUri object or %GST_URI_NO_PORT.
2211 gst_uri_get_port (const GstUri * uri)
2213 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), GST_URI_NO_PORT);
2214 return (uri ? uri->port : GST_URI_NO_PORT);
2219 * @uri: (transfer none)(nullable): The #GstUri to modify.
2220 * @port: The new port number to set or %GST_URI_NO_PORT to unset.
2222 * Set or unset the port number for the URI.
2224 * Returns: %TRUE if the port number was set/unset successfully.
2229 gst_uri_set_port (GstUri * uri, guint port)
2232 return port == GST_URI_NO_PORT;
2233 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2242 * @uri: The #GstUri to get the path from.
2244 * Extract the path string from the URI object.
2246 * Returns: (transfer full): (nullable): The path from the URI. Once finished
2247 * with the string should be g_free()'d.
2252 gst_uri_get_path (const GstUri * uri)
2254 GList *path_segment;
2255 const gchar *sep = "";
2260 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2264 ret = g_string_new (NULL);
2266 for (path_segment = uri->path; path_segment;
2267 path_segment = path_segment->next) {
2268 g_string_append (ret, sep);
2269 if (path_segment->data) {
2270 g_string_append (ret, path_segment->data);
2275 return g_string_free (ret, FALSE);
2280 * @uri: (transfer none) (nullable): The #GstUri to modify.
2281 * @path: The new path to set with path segments separated by '/', or use %NULL
2282 * to unset the path.
2284 * Sets or unsets the path in the URI.
2286 * Returns: %TRUE if the path was set successfully.
2291 gst_uri_set_path (GstUri * uri, const gchar * path)
2294 return path == NULL;
2295 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2297 g_list_free_full (uri->path, g_free);
2298 uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
2304 * gst_uri_get_path_string:
2305 * @uri: The #GstUri to get the path from.
2307 * Extract the path string from the URI object as a percent encoded URI path.
2309 * Returns: (transfer full) (nullable): The path from the URI. Once finished
2310 * with the string should be g_free()'d.
2315 gst_uri_get_path_string (const GstUri * uri)
2317 GList *path_segment;
2318 const gchar *sep = "";
2324 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2328 ret = g_string_new (NULL);
2330 for (path_segment = uri->path; path_segment;
2331 path_segment = path_segment->next) {
2332 g_string_append (ret, sep);
2333 if (path_segment->data) {
2334 escaped = _gst_uri_escape_path_segment (path_segment->data);
2335 g_string_append (ret, escaped);
2341 return g_string_free (ret, FALSE);
2345 * gst_uri_set_path_string:
2346 * @uri: (transfer none)(nullable): The #GstUri to modify.
2347 * @path: The new percent encoded path to set with path segments separated by
2348 * '/', or use %NULL to unset the path.
2350 * Sets or unsets the path in the URI.
2352 * Returns: %TRUE if the path was set successfully.
2357 gst_uri_set_path_string (GstUri * uri, const gchar * path)
2360 return path == NULL;
2361 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2363 g_list_free_full (uri->path, g_free);
2364 uri->path = _gst_uri_string_to_list (path, "/", FALSE, TRUE);
2369 * gst_uri_get_path_segments:
2370 * @uri: (nullable): The #GstUri to get the path from.
2372 * Get a list of path segments from the URI.
2374 * Returns: (transfer full) (element-type gchar*): A #GList of path segment
2375 * strings or %NULL if no path segments are available. Free the list
2376 * when no longer needed with g_list_free_full(list, g_free).
2381 gst_uri_get_path_segments (const GstUri * uri)
2385 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2388 ret = g_list_copy_deep (uri->path, (GCopyFunc) g_strdup, NULL);
2395 * gst_uri_set_path_segments:
2396 * @uri: (transfer none)(nullable): The #GstUri to modify.
2397 * @path_segments: (transfer full)(nullable)(element-type gchar*): The new
2400 * Replace the path segments list in the URI.
2402 * Returns: %TRUE if the path segments were set successfully.
2407 gst_uri_set_path_segments (GstUri * uri, GList * path_segments)
2409 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), FALSE);
2413 g_list_free_full (path_segments, g_free);
2414 return path_segments == NULL;
2417 g_return_val_if_fail (gst_uri_is_writable (uri), FALSE);
2419 g_list_free_full (uri->path, g_free);
2420 uri->path = path_segments;
2425 * gst_uri_append_path:
2426 * @uri: (transfer none)(nullable): The #GstUri to modify.
2427 * @relative_path: Relative path to append to the end of the current path.
2429 * Append a path onto the end of the path in the URI. The path is not
2430 * normalized, call #gst_uri_normalize() to normalize the path.
2432 * Returns: %TRUE if the path was appended successfully.
2437 gst_uri_append_path (GstUri * uri, const gchar * relative_path)
2439 GList *rel_path_list;
2442 return relative_path == NULL;
2443 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2448 GList *last_elem = g_list_last (uri->path);
2449 if (last_elem->data == NULL) {
2450 uri->path = g_list_delete_link (uri->path, last_elem);
2453 rel_path_list = _gst_uri_string_to_list (relative_path, "/", FALSE, FALSE);
2454 /* if path was absolute, make it relative by removing initial NULL element */
2455 if (rel_path_list && rel_path_list->data == NULL) {
2456 rel_path_list = g_list_delete_link (rel_path_list, rel_path_list);
2458 uri->path = g_list_concat (uri->path, rel_path_list);
2463 * gst_uri_append_path_segment:
2464 * @uri: (transfer none)(nullable): The #GstUri to modify.
2465 * @path_segment: The path segment string to append to the URI path.
2467 * Append a single path segment onto the end of the URI path.
2469 * Returns: %TRUE if the path was appended successfully.
2474 gst_uri_append_path_segment (GstUri * uri, const gchar * path_segment)
2477 return path_segment == NULL;
2478 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2482 /* if base path ends in a directory (i.e. last element is NULL), remove it */
2483 if (uri->path && g_list_last (uri->path)->data == NULL) {
2484 uri->path = g_list_delete_link (uri->path, g_list_last (uri->path));
2486 uri->path = g_list_append (uri->path, g_strdup (path_segment));
2491 * gst_uri_get_query_string:
2492 * @uri: (nullable): The #GstUri to get the query string from.
2494 * Get a percent encoded URI query string from the @uri.
2496 * Returns: (transfer full) (nullable): A percent encoded query string. Use
2497 * g_free() when no longer needed.
2502 gst_uri_get_query_string (const GstUri * uri)
2504 GHashTableIter iter;
2505 gpointer key, value;
2506 const gchar *sep = "";
2512 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2516 ret = g_string_new (NULL);
2517 g_hash_table_iter_init (&iter, uri->query);
2518 while (g_hash_table_iter_next (&iter, &key, &value)) {
2519 g_string_append (ret, sep);
2520 escaped = _gst_uri_escape_http_query_element (key);
2521 g_string_append (ret, escaped);
2524 escaped = _gst_uri_escape_http_query_element (value);
2525 g_string_append_printf (ret, "=%s", escaped);
2531 return g_string_free (ret, FALSE);
2535 * gst_uri_set_query_string:
2536 * @uri: (transfer none)(nullable): The #GstUri to modify.
2537 * @query: The new percent encoded query string to use to populate the query
2538 * table, or use %NULL to unset the query table.
2540 * Sets or unsets the query table in the URI.
2542 * Returns: %TRUE if the query table was set successfully.
2547 gst_uri_set_query_string (GstUri * uri, const gchar * query)
2550 return query == NULL;
2552 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2555 g_hash_table_unref (uri->query);
2556 uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, TRUE);
2562 * gst_uri_get_query_table:
2563 * @uri: (nullable): The #GstUri to get the query table from.
2565 * Get the query table from the URI. Keys and values in the table are freed
2566 * with g_free when they are deleted. A value may be %NULL to indicate that
2567 * the key should appear in the query string in the URI, but does not have a
2568 * value. Free the returned #GHashTable with #g_hash_table_unref() when it is
2569 * no longer required. Modifying this hash table will modify the query in the
2572 * Returns: (transfer full) (element-type gchar* gchar*) (nullable): The query
2573 * hash table from the URI.
2578 gst_uri_get_query_table (const GstUri * uri)
2582 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2586 return g_hash_table_ref (uri->query);
2590 * gst_uri_set_query_table:
2591 * @uri: (transfer none)(nullable): The #GstUri to modify.
2592 * @query_table: (transfer none)(nullable)(element-type gchar* gchar*): The new
2593 * query table to use.
2595 * Set the query table to use in the URI. The old table is unreferenced and a
2596 * reference to the new one is used instead. A value if %NULL for @query_table
2597 * will remove the query string from the URI.
2599 * Returns: %TRUE if the new table was sucessfully used for the query table.
2604 gst_uri_set_query_table (GstUri * uri, GHashTable * query_table)
2606 GHashTable *old_table = NULL;
2609 return query_table == NULL;
2610 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2612 old_table = uri->query;
2614 uri->query = g_hash_table_ref (query_table);
2618 g_hash_table_unref (old_table);
2624 * gst_uri_set_query_value:
2625 * @uri: (transfer none)(nullable): The #GstUri to modify.
2626 * @query_key: (transfer none): The key for the query entry.
2627 * @query_value: (transfer none)(nullable): The value for the key.
2629 * This inserts or replaces a key in the query table. A @query_value of %NULL
2630 * indicates that the key has no associated value, but will still be present in
2633 * Returns: %TRUE if the query table was sucessfully updated.
2638 gst_uri_set_query_value (GstUri * uri, const gchar * query_key,
2639 const gchar * query_value)
2643 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2646 uri->query = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2649 g_hash_table_insert (uri->query, g_strdup (query_key),
2650 g_strdup (query_value));
2656 * gst_uri_remove_query_key:
2657 * @uri: (transfer none)(nullable): The #GstUri to modify.
2658 * @query_key: The key to remove.
2660 * Remove an entry from the query table by key.
2662 * Returns: %TRUE if the key existed in the table and was removed.
2667 gst_uri_remove_query_key (GstUri * uri, const gchar * query_key)
2673 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2677 result = g_hash_table_remove (uri->query, query_key);
2678 /* if this was the last query entry, remove the query string completely */
2679 if (result && g_hash_table_size (uri->query) == 0) {
2680 g_hash_table_unref (uri->query);
2687 * gst_uri_query_has_key:
2688 * @uri: (nullable): The #GstUri to examine.
2689 * @query_key: The key to lookup.
2691 * Check if there is a query table entry for the @query_key key.
2693 * Returns: %TRUE if @query_key exists in the URI query table.
2698 gst_uri_query_has_key (const GstUri * uri, const gchar * query_key)
2702 g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2706 return g_hash_table_contains (uri->query, query_key);
2710 * gst_uri_get_query_value:
2711 * @uri: (nullable): The #GstUri to examine.
2712 * @query_key: The key to lookup.
2714 * Get the value associated with the @query_key key. Will return %NULL if the
2715 * key has no value or if the key does not exist in the URI query table. Because
2716 * %NULL is returned for both missing keys and keys with no value, you should
2717 * use gst_uri_query_has_key() to determine if a key is present in the URI
2720 * Returns: (nullable): The value for the given key, or %NULL if not found.
2725 gst_uri_get_query_value (const GstUri * uri, const gchar * query_key)
2729 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2733 return g_hash_table_lookup (uri->query, query_key);
2737 * gst_uri_get_query_keys:
2738 * @uri: (nullable): The #GstUri to examine.
2740 * Get a list of the query keys from the URI.
2742 * Returns: (transfer container) (element-type gchar*): A list of keys from
2743 * the URI query. Free the list with g_list_free().
2748 gst_uri_get_query_keys (const GstUri * uri)
2752 g_return_val_if_fail (GST_IS_URI (uri), NULL);
2756 return g_hash_table_get_keys (uri->query);
2760 * gst_uri_get_fragment:
2761 * @uri: (nullable): This #GstUri object.
2763 * Get the fragment name from the URI or %NULL if it doesn't exist.
2764 * If @uri is %NULL then returns %NULL.
2766 * Returns: (nullable): The host name from the #GstUri object or %NULL.
2771 gst_uri_get_fragment (const GstUri * uri)
2773 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2774 return (uri ? uri->fragment : NULL);
2778 * gst_uri_set_fragment:
2779 * @uri: (transfer none)(nullable): The #GstUri to modify.
2780 * @fragment: (nullable): The fragment string to set.
2782 * Sets the fragment string in the URI. Use a value of %NULL in @fragment to
2783 * unset the fragment string.
2785 * Returns: %TRUE if the fragment was set/unset successfully.
2790 gst_uri_set_fragment (GstUri * uri, const gchar * fragment)
2793 return fragment == NULL;
2794 g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2796 g_free (uri->fragment);
2797 uri->fragment = g_strdup (fragment);
2802 * gst_uri_get_media_fragment_table:
2803 * @uri: (nullable): The #GstUri to get the fragment table from.
2805 * Get the media fragment table from the URI, as defined by "Media Fragments URI 1.0".
2806 * Hash table returned by this API is a list of "key-value" pairs, and the each
2807 * pair is generated by splitting "URI fragment" per "&" sub-delims, then "key"
2808 * and "value" are splitted by "=" sub-delims. The "key" returned by this API may
2809 * be undefined keyword by standard.
2810 * A value may be %NULL to indicate that the key should appear in the fragment
2811 * string in the URI, but does not have a value. Free the returned #GHashTable
2812 * with #g_hash_table_unref() when it is no longer required.
2813 * Modifying this hash table does not affect the fragment in the URI.
2815 * See more about Media Fragments URI 1.0 (W3C) at https://www.w3.org/TR/media-frags/
2817 * Returns: (transfer full) (element-type gchar* gchar*) (nullable): The
2818 * fragment hash table from the URI.
2823 gst_uri_get_media_fragment_table (const GstUri * uri)
2825 g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2829 return _gst_uri_string_to_table (uri->fragment, "&", "=", TRUE, TRUE);