helpers: use $(INSTALL) to ... install the helper.
[platform/upstream/gstreamer.git] / gst / gsturi.c
1 /* GStreamer
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>
7  *
8  * gsturi.c: register URI handlers and IETF RFC 3986 URI manipulations.
9  *
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.
14  *
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.
19  *
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.
24  */
25
26 /**
27  * SECTION:gsturihandler
28  * @short_description: Interface to ease URI handling in plugins.
29  *
30  * The URIHandler is an interface that is implemented by Source and Sink
31  * #GstElement to simplify then handling of URI.
32  *
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()).
36  *
37  * Source and Sink plugins should implement this interface when possible.
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #  include "config.h"
42 #endif
43
44 #include "gst_private.h"
45 #include "gst.h"
46 #include "gsturi.h"
47 #include "gstinfo.h"
48 #include "gstregistry.h"
49
50 #include "gst-i18n-lib.h"
51
52 #include <string.h>
53 #include <glib.h>
54 #include <glib/gprintf.h>
55
56 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
57 #define GST_CAT_DEFAULT gst_uri_handler_debug
58
59 #ifndef HAVE_STRCASESTR
60 #define strcasestr _gst_ascii_strcasestr
61
62 /* From https://github.com/freebsd/freebsd/blob/master/contrib/file/src/strcasestr.c
63  * Updated to use GLib types and GLib string functions
64  *
65  * Copyright (c) 1990, 1993
66  *      The Regents of the University of California.  All rights reserved.
67  *
68  * This code is derived from software contributed to Berkeley by
69  * Chris Torek.
70  *
71  * Redistribution and use in source and binary forms, with or without
72  * modification, are permitted provided that the following conditions
73  * are met:
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.
82  *
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
93  * SUCH DAMAGE.
94  */
95
96 /*
97  * Find the first occurrence of find in s, ignore case.
98  */
99
100 static gchar *
101 _gst_ascii_strcasestr (const gchar * s, const gchar * find)
102 {
103   gchar c, sc;
104   gsize len;
105
106   if ((c = *find++) != 0) {
107     c = g_ascii_tolower (c);
108     len = strlen (find);
109     do {
110       do {
111         if ((sc = *s++) == 0)
112           return (NULL);
113       } while (g_ascii_tolower (sc) != c);
114     } while (g_ascii_strncasecmp (s, find, len) != 0);
115     s--;
116   }
117   return (gchar *) (gintptr) (s);
118 }
119 #endif
120
121 #if !GLIB_CHECK_VERSION (2, 33, 4)
122 #define g_list_copy_deep gst_g_list_copy_deep
123 static GList *
124 gst_g_list_copy_deep (GList * list, GCopyFunc func, gpointer user_data)
125 {
126   list = g_list_copy (list);
127
128   if (func != NULL) {
129     GList *l;
130
131     for (l = list; l != NULL; l = l->next) {
132       l->data = func (l->data, user_data);
133     }
134   }
135
136   return list;
137 }
138 #endif
139
140 GType
141 gst_uri_handler_get_type (void)
142 {
143   static volatile gsize urihandler_type = 0;
144
145   if (g_once_init_enter (&urihandler_type)) {
146     GType _type;
147     static const GTypeInfo urihandler_info = {
148       sizeof (GstURIHandlerInterface),
149       NULL,
150       NULL,
151       NULL,
152       NULL,
153       NULL,
154       0,
155       0,
156       NULL,
157       NULL
158     };
159
160     _type = g_type_register_static (G_TYPE_INTERFACE,
161         "GstURIHandler", &urihandler_info, 0);
162
163     GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
164         "handling of URIs");
165     g_once_init_leave (&urihandler_type, _type);
166   }
167   return urihandler_type;
168 }
169
170 GQuark
171 gst_uri_error_quark (void)
172 {
173   return g_quark_from_static_string ("gst-uri-error-quark");
174 }
175
176 static const guchar acceptable[96] = {  /* X0   X1   X2   X3   X4   X5   X6   X7   X8   X9   XA   XB   XC   XD   XE   XF */
177   0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C,       /* 2X  !"#$%&'()*+,-./   */
178   0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C,       /* 3X 0123456789:;<=>?   */
179   0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       /* 4X @ABCDEFGHIJKLMNO   */
180   0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F,       /* 5X PQRSTUVWXYZ[\]^_   */
181   0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       /* 6X `abcdefghijklmno   */
182   0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20        /* 7X pqrstuvwxyz{|}~DEL */
183 };
184
185 typedef enum
186 {
187   UNSAFE_ALL = 0x1,             /* Escape all unsafe characters   */
188   UNSAFE_ALLOW_PLUS = 0x2,      /* Allows '+'  */
189   UNSAFE_PATH = 0x4,            /* Allows '/' and '?' and '&' and '='  */
190   UNSAFE_DOS_PATH = 0x8,        /* Allows '/' and '?' and '&' and '=' and ':' */
191   UNSAFE_HOST = 0x10,           /* Allows '/' and ':' and '@' */
192   UNSAFE_SLASHES = 0x20         /* Allows all characters except for '/' and '%' */
193 } UnsafeCharacterSet;
194
195 #define HEX_ESCAPE '%'
196
197 /*  Escape undesirable characters using %
198  *  -------------------------------------
199  *
200  * This function takes a pointer to a string in which
201  * some characters may be unacceptable unescaped.
202  * It returns a string which has these characters
203  * represented by a '%' character followed by two hex digits.
204  *
205  * This routine returns a g_malloced string.
206  */
207
208 static const gchar hex[16] = "0123456789ABCDEF";
209
210 static gchar *
211 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
212 {
213 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
214
215   const gchar *p;
216   gchar *q;
217   gchar *result;
218   guchar c;
219   gint unacceptable;
220   UnsafeCharacterSet use_mask;
221
222   g_return_val_if_fail (mask == UNSAFE_ALL
223       || mask == UNSAFE_ALLOW_PLUS
224       || mask == UNSAFE_PATH
225       || mask == UNSAFE_DOS_PATH
226       || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
227
228   if (string == NULL) {
229     return NULL;
230   }
231
232   unacceptable = 0;
233   use_mask = mask;
234   for (p = string; *p != '\0'; p++) {
235     c = *p;
236     if (!ACCEPTABLE_CHAR (c)) {
237       unacceptable++;
238     }
239     if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
240       /* when escaping a host, if we hit something that needs to be escaped, or we finally
241        * hit a path separator, revert to path mode (the host segment of the url is over).
242        */
243       use_mask = UNSAFE_PATH;
244     }
245   }
246
247   result = g_malloc (p - string + unacceptable * 2 + 1);
248
249   use_mask = mask;
250   for (q = result, p = string; *p != '\0'; p++) {
251     c = *p;
252
253     if (!ACCEPTABLE_CHAR (c)) {
254       *q++ = HEX_ESCAPE;        /* means hex coming */
255       *q++ = hex[c >> 4];
256       *q++ = hex[c & 15];
257     } else {
258       *q++ = c;
259     }
260     if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
261       use_mask = UNSAFE_PATH;
262     }
263   }
264
265   *q = '\0';
266
267   return result;
268 }
269
270 /* escape_string:
271  * @string: string to be escaped
272  *
273  * Escapes @string, replacing any and all special characters
274  * with equivalent escape sequences.
275  *
276  * Return value: a newly allocated string equivalent to @string
277  * but with all special characters escaped
278  **/
279 static gchar *
280 escape_string (const gchar * string)
281 {
282   return escape_string_internal (string, UNSAFE_ALL);
283 }
284
285 static int
286 hex_to_int (gchar c)
287 {
288   return c >= '0' && c <= '9' ? c - '0'
289       : c >= 'A' && c <= 'F' ? c - 'A' + 10
290       : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
291 }
292
293 static int
294 unescape_character (const char *scanner)
295 {
296   int first_digit;
297   int second_digit;
298
299   first_digit = hex_to_int (*scanner++);
300   if (first_digit < 0) {
301     return -1;
302   }
303
304   second_digit = hex_to_int (*scanner);
305   if (second_digit < 0) {
306     return -1;
307   }
308
309   return (first_digit << 4) | second_digit;
310 }
311
312 /* unescape_string:
313  * @escaped_string: an escaped URI, path, or other string
314  * @illegal_characters: a string containing a sequence of characters
315  * considered "illegal", '\0' is automatically in this list.
316  *
317  * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
318  * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
319  * for character 16x+y.
320  *
321  * Return value: (nullable): a newly allocated string with the
322  * unescaped equivalents, or %NULL if @escaped_string contained one of
323  * the characters in @illegal_characters.
324  **/
325 static char *
326 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
327 {
328   const gchar *in;
329   gchar *out, *result;
330   gint character;
331
332   if (escaped_string == NULL) {
333     return NULL;
334   }
335
336   result = g_malloc (strlen (escaped_string) + 1);
337
338   out = result;
339   for (in = escaped_string; *in != '\0'; in++) {
340     character = *in;
341     if (*in == HEX_ESCAPE) {
342       character = unescape_character (in + 1);
343
344       /* Check for an illegal character. We consider '\0' illegal here. */
345       if (character <= 0
346           || (illegal_characters != NULL
347               && strchr (illegal_characters, (char) character) != NULL)) {
348         g_free (result);
349         return NULL;
350       }
351       in += 2;
352     }
353     *out++ = (char) character;
354   }
355
356   *out = '\0';
357   g_assert ((gsize) (out - result) <= strlen (escaped_string));
358   return result;
359
360 }
361
362
363 static void
364 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
365 {
366   gchar *check = (gchar *) uri;
367
368   g_assert (uri != NULL);
369   g_assert (endptr != NULL);
370
371   if (g_ascii_isalpha (*check)) {
372     check++;
373     while (g_ascii_isalnum (*check) || *check == '+'
374         || *check == '-' || *check == '.')
375       check++;
376   }
377
378   *endptr = check;
379 }
380
381 /**
382  * gst_uri_protocol_is_valid:
383  * @protocol: A string
384  *
385  * Tests if the given string is a valid protocol identifier. Protocols
386  * must consist of alphanumeric characters, '+', '-' and '.' and must
387  * start with a alphabetic character. See RFC 3986 Section 3.1.
388  *
389  * Returns: %TRUE if the string is a valid protocol identifier, %FALSE otherwise.
390  */
391 gboolean
392 gst_uri_protocol_is_valid (const gchar * protocol)
393 {
394   gchar *endptr;
395
396   g_return_val_if_fail (protocol != NULL, FALSE);
397
398   gst_uri_protocol_check_internal (protocol, &endptr);
399
400   return *endptr == '\0' && ((gsize) (endptr - protocol)) >= 2;
401 }
402
403 /**
404  * gst_uri_is_valid:
405  * @uri: A URI string
406  *
407  * Tests if the given string is a valid URI identifier. URIs start with a valid
408  * scheme followed by ":" and maybe a string identifying the location.
409  *
410  * Returns: %TRUE if the string is a valid URI
411  */
412 gboolean
413 gst_uri_is_valid (const gchar * uri)
414 {
415   gchar *endptr;
416
417   g_return_val_if_fail (uri != NULL, FALSE);
418
419   gst_uri_protocol_check_internal (uri, &endptr);
420
421   return *endptr == ':' && ((gsize) (endptr - uri)) >= 2;
422 }
423
424 /**
425  * gst_uri_get_protocol:
426  * @uri: A URI string
427  *
428  * Extracts the protocol out of a given valid URI. The returned string must be
429  * freed using g_free().
430  *
431  * Returns: The protocol for this URI.
432  */
433 gchar *
434 gst_uri_get_protocol (const gchar * uri)
435 {
436   gchar *colon;
437
438   g_return_val_if_fail (uri != NULL, NULL);
439   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
440
441   colon = strstr (uri, ":");
442
443   return g_ascii_strdown (uri, colon - uri);
444 }
445
446 /**
447  * gst_uri_has_protocol:
448  * @uri: a URI string
449  * @protocol: a protocol string (e.g. "http")
450  *
451  * Checks if the protocol of a given valid URI matches @protocol.
452  *
453  * Returns: %TRUE if the protocol matches.
454  */
455 gboolean
456 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
457 {
458   gchar *colon;
459
460   g_return_val_if_fail (uri != NULL, FALSE);
461   g_return_val_if_fail (protocol != NULL, FALSE);
462   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
463
464   colon = strstr (uri, ":");
465
466   if (colon == NULL)
467     return FALSE;
468
469   return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
470 }
471
472 /**
473  * gst_uri_get_location:
474  * @uri: A URI string
475  *
476  * Extracts the location out of a given valid URI, ie. the protocol and "://"
477  * are stripped from the URI, which means that the location returned includes
478  * the hostname if one is specified. The returned string must be freed using
479  * g_free().
480  *
481  * Free-function: g_free
482  *
483  * Returns: (transfer full): the location for this URI. Returns %NULL if the
484  *     URI isn't valid. If the URI does not contain a location, an empty
485  *     string is returned.
486  */
487 gchar *
488 gst_uri_get_location (const gchar * uri)
489 {
490   const gchar *colon;
491   gchar *unescaped = NULL;
492
493   g_return_val_if_fail (uri != NULL, NULL);
494   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
495
496   colon = strstr (uri, "://");
497   if (!colon)
498     return NULL;
499
500   unescaped = unescape_string (colon + 3, "/");
501
502   /* On Windows an URI might look like file:///c:/foo/bar.txt or
503    * file:///c|/foo/bar.txt (some Netscape versions) and we want to
504    * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
505    * Can't use g_filename_from_uri() here because it will only handle the
506    * file:// protocol */
507 #ifdef G_OS_WIN32
508   if (unescaped != NULL && unescaped[0] == '/' &&
509       g_ascii_isalpha (unescaped[1]) &&
510       (unescaped[2] == ':' || unescaped[2] == '|')) {
511     unescaped[2] = ':';
512     memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
513   }
514 #endif
515
516   GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
517       uri);
518   return unescaped;
519 }
520
521 /**
522  * gst_uri_construct:
523  * @protocol: Protocol for URI
524  * @location: (transfer none): Location for URI
525  *
526  * Constructs a URI for a given valid protocol and location.
527  *
528  * Free-function: g_free
529  *
530  * Returns: (transfer full): a new string for this URI. Returns %NULL if the
531  *     given URI protocol is not valid, or the given location is %NULL.
532  */
533 gchar *
534 gst_uri_construct (const gchar * protocol, const gchar * location)
535 {
536   char *escaped, *proto_lowercase;
537   char *retval;
538
539   g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
540   g_return_val_if_fail (location != NULL, NULL);
541
542   proto_lowercase = g_ascii_strdown (protocol, -1);
543   escaped = escape_string (location);
544   retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
545   g_free (escaped);
546   g_free (proto_lowercase);
547
548   return retval;
549 }
550
551 typedef struct
552 {
553   GstURIType type;
554   const gchar *protocol;
555 }
556 SearchEntry;
557
558 static gboolean
559 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
560 {
561   const gchar *const *protocols;
562   GstElementFactory *factory;
563   SearchEntry *entry = (SearchEntry *) search_entry;
564
565   if (!GST_IS_ELEMENT_FACTORY (feature))
566     return FALSE;
567   factory = GST_ELEMENT_FACTORY_CAST (feature);
568
569   if (factory->uri_type != entry->type)
570     return FALSE;
571
572   protocols = gst_element_factory_get_uri_protocols (factory);
573
574   if (protocols == NULL) {
575     g_warning ("Factory '%s' implements GstUriHandler interface but returned "
576         "no supported protocols!", gst_plugin_feature_get_name (feature));
577     return FALSE;
578   }
579
580   while (*protocols != NULL) {
581     if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
582       return TRUE;
583     protocols++;
584   }
585   return FALSE;
586 }
587
588 static gint
589 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
590 {
591   return gst_plugin_feature_get_rank (second) -
592       gst_plugin_feature_get_rank (first);
593 }
594
595 static GList *
596 get_element_factories_from_uri_protocol (const GstURIType type,
597     const gchar * protocol)
598 {
599   GList *possibilities;
600   SearchEntry entry;
601
602   g_return_val_if_fail (protocol, NULL);
603
604   entry.type = type;
605   entry.protocol = protocol;
606   possibilities = gst_registry_feature_filter (gst_registry_get (),
607       search_by_entry, FALSE, &entry);
608
609   return possibilities;
610 }
611
612 /**
613  * gst_uri_protocol_is_supported:
614  * @type: Whether to check for a source or a sink
615  * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
616  *
617  * Checks if an element exists that supports the given URI protocol. Note
618  * that a positive return value does not imply that a subsequent call to
619  * gst_element_make_from_uri() is guaranteed to work.
620  *
621  * Returns: %TRUE
622 */
623 gboolean
624 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
625 {
626   GList *possibilities;
627
628   g_return_val_if_fail (protocol, FALSE);
629
630   possibilities = get_element_factories_from_uri_protocol (type, protocol);
631
632   if (possibilities) {
633     g_list_free (possibilities);
634     return TRUE;
635   } else
636     return FALSE;
637 }
638
639 /**
640  * gst_element_make_from_uri:
641  * @type: Whether to create a source or a sink
642  * @uri: URI to create an element for
643  * @elementname: (allow-none): Name of created element, can be %NULL.
644  * @error: (allow-none): address where to store error information, or %NULL.
645  *
646  * Creates an element for handling the given URI.
647  *
648  * Returns: (transfer floating): a new element or %NULL if none could be created
649  */
650 GstElement *
651 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
652     const gchar * elementname, GError ** error)
653 {
654   GList *possibilities, *walk;
655   gchar *protocol;
656   GstElement *ret = NULL;
657
658   g_return_val_if_fail (gst_is_initialized (), NULL);
659   g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
660   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
661   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
662
663   GST_DEBUG ("type:%d, uri:%s, elementname:%s", type, uri, elementname);
664
665   protocol = gst_uri_get_protocol (uri);
666   possibilities = get_element_factories_from_uri_protocol (type, protocol);
667
668   if (!possibilities) {
669     GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
670         uri);
671     /* The error message isn't great, but we don't expect applications to
672      * show that error to users, but call the missing plugins functions */
673     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
674         _("No URI handler for the %s protocol found"), protocol);
675     g_free (protocol);
676     return NULL;
677   }
678   g_free (protocol);
679
680   possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
681   walk = possibilities;
682   while (walk) {
683     GstElementFactory *factory = walk->data;
684     GError *uri_err = NULL;
685
686     ret = gst_element_factory_create (factory, elementname);
687     if (ret != NULL) {
688       GstURIHandler *handler = GST_URI_HANDLER (ret);
689
690       if (gst_uri_handler_set_uri (handler, uri, &uri_err))
691         break;
692
693       GST_WARNING ("%s didn't accept URI '%s': %s", GST_OBJECT_NAME (ret), uri,
694           uri_err->message);
695
696       if (error != NULL && *error == NULL)
697         g_propagate_error (error, uri_err);
698       else
699         g_error_free (uri_err);
700
701       gst_object_unref (ret);
702       ret = NULL;
703     }
704     walk = walk->next;
705   }
706   gst_plugin_feature_list_free (possibilities);
707
708   GST_LOG_OBJECT (ret, "created %s for URL '%s'",
709       type == GST_URI_SINK ? "sink" : "source", uri);
710
711   /* if the first handler didn't work, but we found another one that works */
712   if (ret != NULL)
713     g_clear_error (error);
714
715   return ret;
716 }
717
718 /**
719  * gst_uri_handler_get_uri_type:
720  * @handler: A #GstURIHandler.
721  *
722  * Gets the type of the given URI handler
723  *
724  * Returns: the #GstURIType of the URI handler.
725  * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
726  */
727 guint
728 gst_uri_handler_get_uri_type (GstURIHandler * handler)
729 {
730   GstURIHandlerInterface *iface;
731   guint ret;
732
733   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
734
735   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
736   g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
737   g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
738
739   ret = iface->get_type (G_OBJECT_TYPE (handler));
740   g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
741
742   return ret;
743 }
744
745 /**
746  * gst_uri_handler_get_protocols:
747  * @handler: A #GstURIHandler.
748  *
749  * Gets the list of protocols supported by @handler. This list may not be
750  * modified.
751  *
752  * Returns: (transfer none) (element-type utf8) (nullable): the
753  *     supported protocols.  Returns %NULL if the @handler isn't
754  *     implemented properly, or the @handler doesn't support any
755  *     protocols.
756  */
757 const gchar *const *
758 gst_uri_handler_get_protocols (GstURIHandler * handler)
759 {
760   GstURIHandlerInterface *iface;
761   const gchar *const *ret;
762
763   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
764
765   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
766   g_return_val_if_fail (iface != NULL, NULL);
767   g_return_val_if_fail (iface->get_protocols != NULL, NULL);
768
769   ret = iface->get_protocols (G_OBJECT_TYPE (handler));
770   g_return_val_if_fail (ret != NULL, NULL);
771
772   return ret;
773 }
774
775 /**
776  * gst_uri_handler_get_uri:
777  * @handler: A #GstURIHandler
778  *
779  * Gets the currently handled URI.
780  *
781  * Returns: (transfer full) (nullable): the URI currently handled by
782  *   the @handler.  Returns %NULL if there are no URI currently
783  *   handled. The returned string must be freed with g_free() when no
784  *   longer needed.
785  */
786 gchar *
787 gst_uri_handler_get_uri (GstURIHandler * handler)
788 {
789   GstURIHandlerInterface *iface;
790   gchar *ret;
791
792   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
793
794   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
795   g_return_val_if_fail (iface != NULL, NULL);
796   g_return_val_if_fail (iface->get_uri != NULL, NULL);
797   ret = iface->get_uri (handler);
798   if (ret != NULL)
799     g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
800
801   return ret;
802 }
803
804 /**
805  * gst_uri_handler_set_uri:
806  * @handler: A #GstURIHandler
807  * @uri: URI to set
808  * @error: (allow-none): address where to store a #GError in case of
809  *    an error, or %NULL
810  *
811  * Tries to set the URI of the given handler.
812  *
813  * Returns: %TRUE if the URI was set successfully, else %FALSE.
814  */
815 gboolean
816 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
817     GError ** error)
818 {
819   GstURIHandlerInterface *iface;
820   gboolean ret;
821   gchar *protocol;
822
823   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
824   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
825   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
826
827   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
828   g_return_val_if_fail (iface != NULL, FALSE);
829   g_return_val_if_fail (iface->set_uri != NULL, FALSE);
830
831   protocol = gst_uri_get_protocol (uri);
832
833   if (iface->get_protocols) {
834     const gchar *const *protocols;
835     const gchar *const *p;
836     gboolean found_protocol = FALSE;
837
838     protocols = iface->get_protocols (G_OBJECT_TYPE (handler));
839     if (protocols != NULL) {
840       for (p = protocols; *p != NULL; ++p) {
841         if (g_ascii_strcasecmp (protocol, *p) == 0) {
842           found_protocol = TRUE;
843           break;
844         }
845       }
846
847       if (!found_protocol) {
848         g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
849             _("URI scheme '%s' not supported"), protocol);
850         g_free (protocol);
851         return FALSE;
852       }
853     }
854   }
855
856   ret = iface->set_uri (handler, uri, error);
857
858   g_free (protocol);
859
860   return ret;
861 }
862
863 static gchar *
864 gst_file_utils_canonicalise_path (const gchar * path)
865 {
866   gchar **parts, **p, *clean_path;
867
868 #ifdef G_OS_WIN32
869   {
870     GST_WARNING ("FIXME: canonicalise win32 path");
871     return g_strdup (path);
872   }
873 #endif
874
875   parts = g_strsplit (path, "/", -1);
876
877   p = parts;
878   while (*p != NULL) {
879     if (strcmp (*p, ".") == 0) {
880       /* just move all following parts on top of this, incl. NUL terminator */
881       g_free (*p);
882       memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
883       /* re-check the new current part again in the next iteration */
884       continue;
885     } else if (strcmp (*p, "..") == 0 && p > parts) {
886       /* just move all following parts on top of the previous part, incl.
887        * NUL terminator */
888       g_free (*(p - 1));
889       g_free (*p);
890       memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
891       /* re-check the new current part again in the next iteration */
892       --p;
893       continue;
894     }
895     ++p;
896   }
897   if (*path == '/') {
898     guint num_parts;
899
900     num_parts = g_strv_length (parts) + 1;      /* incl. terminator */
901     parts = g_renew (gchar *, parts, num_parts + 1);
902     memmove (parts + 1, parts, num_parts * sizeof (gchar *));
903     parts[0] = g_strdup ("/");
904   }
905
906   clean_path = g_build_filenamev (parts);
907   g_strfreev (parts);
908   return clean_path;
909 }
910
911 static gboolean
912 file_path_contains_relatives (const gchar * path)
913 {
914   return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
915       strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
916       strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
917 }
918
919 /**
920  * gst_filename_to_uri:
921  * @filename: absolute or relative file name path
922  * @error: pointer to error, or %NULL
923  *
924  * Similar to g_filename_to_uri(), but attempts to handle relative file paths
925  * as well. Before converting @filename into an URI, it will be prefixed by
926  * the current working directory if it is a relative path, and then the path
927  * will be canonicalised so that it doesn't contain any './' or '../' segments.
928  *
929  * On Windows #filename should be in UTF-8 encoding.
930  */
931 gchar *
932 gst_filename_to_uri (const gchar * filename, GError ** error)
933 {
934   gchar *abs_location = NULL;
935   gchar *uri, *abs_clean;
936
937   g_return_val_if_fail (filename != NULL, NULL);
938   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
939
940   if (g_path_is_absolute (filename)) {
941     if (!file_path_contains_relatives (filename)) {
942       uri = g_filename_to_uri (filename, NULL, error);
943       goto beach;
944     }
945
946     abs_location = g_strdup (filename);
947   } else {
948     gchar *cwd;
949
950     cwd = g_get_current_dir ();
951     abs_location = g_build_filename (cwd, filename, NULL);
952     g_free (cwd);
953
954     if (!file_path_contains_relatives (abs_location)) {
955       uri = g_filename_to_uri (abs_location, NULL, error);
956       goto beach;
957     }
958   }
959
960   /* path is now absolute, but contains '.' or '..' */
961   abs_clean = gst_file_utils_canonicalise_path (abs_location);
962   GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
963   uri = g_filename_to_uri (abs_clean, NULL, error);
964   g_free (abs_clean);
965
966 beach:
967
968   g_free (abs_location);
969   GST_DEBUG ("'%s' -> '%s'", filename, uri);
970   return uri;
971 }
972
973 /****************************************************************************
974  * GstUri - GstMiniObject to parse and merge URIs according to IETF RFC 3986
975  ****************************************************************************/
976
977 /**
978  * SECTION:gsturi
979  * @short_description: URI parsing and manipulation.
980  *
981  * A #GstUri object can be used to parse and split a URI string into its
982  * constituant parts. Two #GstUri objects can be joined to make a new #GstUri
983  * using the algorithm described in RFC3986.
984  */
985
986 /* Definition for GstUri object */
987 struct _GstUri
988 {
989   /*< private > */
990   GstMiniObject mini_object;
991   gchar *scheme;
992   gchar *userinfo;
993   gchar *host;
994   guint port;
995   GList *path;
996   GHashTable *query;
997   gchar *fragment;
998 };
999
1000 GST_DEFINE_MINI_OBJECT_TYPE (GstUri, gst_uri);
1001
1002 static GstUri *_gst_uri_copy (const GstUri * uri);
1003 static void _gst_uri_free (GstUri * uri);
1004 static GstUri *_gst_uri_new (void);
1005 static GList *_remove_dot_segments (GList * path);
1006
1007 /* private GstUri functions */
1008
1009 static GstUri *
1010 _gst_uri_new (void)
1011 {
1012   GstUri *uri;
1013   uri = GST_URI_CAST (g_slice_new0 (GstUri));
1014
1015   if (uri)
1016     gst_mini_object_init (GST_MINI_OBJECT_CAST (uri), 0, gst_uri_get_type (),
1017         (GstMiniObjectCopyFunction) _gst_uri_copy, NULL,
1018         (GstMiniObjectFreeFunction) _gst_uri_free);
1019
1020   return uri;
1021 }
1022
1023 static void
1024 _gst_uri_free (GstUri * uri)
1025 {
1026   g_return_if_fail (GST_IS_URI (uri));
1027
1028   g_free (uri->scheme);
1029   g_free (uri->userinfo);
1030   g_free (uri->host);
1031   g_list_free_full (uri->path, g_free);
1032   if (uri->query)
1033     g_hash_table_unref (uri->query);
1034   g_free (uri->fragment);
1035
1036   g_slice_free1 (sizeof (*uri), uri);
1037 }
1038
1039 static GHashTable *
1040 _gst_uri_copy_query_table (GHashTable * orig)
1041 {
1042   GHashTable *new = NULL;
1043
1044   if (orig != NULL) {
1045     GHashTableIter iter;
1046     gpointer key, value;
1047     new = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1048     g_hash_table_iter_init (&iter, orig);
1049     while (g_hash_table_iter_next (&iter, &key, &value)) {
1050       g_hash_table_insert (new, g_strdup (key), g_strdup (value));
1051     }
1052   }
1053
1054   return new;
1055 }
1056
1057 static GstUri *
1058 _gst_uri_copy (const GstUri * orig_uri)
1059 {
1060   GstUri *new_uri;
1061
1062   g_return_val_if_fail (GST_IS_URI (orig_uri), NULL);
1063
1064   new_uri = _gst_uri_new ();
1065
1066   if (new_uri) {
1067     new_uri->scheme = g_strdup (orig_uri->scheme);
1068     new_uri->userinfo = g_strdup (orig_uri->userinfo);
1069     new_uri->host = g_strdup (orig_uri->host);
1070     new_uri->port = orig_uri->port;
1071     new_uri->path = g_list_copy_deep (orig_uri->path, (GCopyFunc) g_strdup,
1072         NULL);
1073     new_uri->query = _gst_uri_copy_query_table (orig_uri->query);
1074     new_uri->fragment = g_strdup (orig_uri->fragment);
1075   }
1076
1077   return new_uri;
1078 }
1079
1080 /*
1081  * _gst_uri_compare_lists:
1082  *
1083  * Compare two lists for equality. This compares the two lists, item for item,
1084  * comparing items in the same position in the two lists. If @first is
1085  * considered less than @second the result will be negative. If @first is
1086  * considered to be more than @second then the result will be positive. If the
1087  * lists are considered to be equal then the result will be 0. If two lists
1088  * have the same items, but one list is shorter than the other, then the
1089  * shorter list is considered to be less than the longer list.
1090  */
1091 static gint
1092 _gst_uri_compare_lists (GList * first, GList * second, GCompareFunc cmp_fn)
1093 {
1094   GList *itr1, *itr2;
1095   gint result;
1096
1097   for (itr1 = first, itr2 = second;
1098       itr1 != NULL || itr2 != NULL; itr1 = itr1->next, itr2 = itr2->next) {
1099     if (itr1 == NULL)
1100       return -1;
1101     if (itr2 == NULL)
1102       return 1;
1103     result = cmp_fn (itr1->data, itr2->data);
1104     if (result != 0)
1105       return result;
1106   }
1107   return 0;
1108 }
1109
1110 typedef enum
1111 {
1112   _GST_URI_NORMALIZE_LOWERCASE = 1,
1113   _GST_URI_NORMALIZE_UPPERCASE = 2
1114 } _GstUriNormalizations;
1115
1116 /*
1117  * Find the first character that hasn't been normalized according to the @flags.
1118  */
1119 static gchar *
1120 _gst_uri_first_non_normalized_char (gchar * str, guint flags)
1121 {
1122   gchar *pos;
1123
1124   if (str == NULL)
1125     return NULL;
1126
1127   for (pos = str; *pos; pos++) {
1128     if ((flags & _GST_URI_NORMALIZE_UPPERCASE) && g_ascii_islower (*pos))
1129       return pos;
1130     if ((flags & _GST_URI_NORMALIZE_LOWERCASE) && g_ascii_isupper (*pos))
1131       return pos;
1132   }
1133   return NULL;
1134 }
1135
1136 static gboolean
1137 _gst_uri_normalize_lowercase (gchar * str)
1138 {
1139   gchar *pos;
1140   gboolean ret = FALSE;
1141
1142   for (pos = _gst_uri_first_non_normalized_char (str,
1143           _GST_URI_NORMALIZE_LOWERCASE);
1144       pos != NULL;
1145       pos = _gst_uri_first_non_normalized_char (pos + 1,
1146           _GST_URI_NORMALIZE_LOWERCASE)) {
1147     *pos = g_ascii_tolower (*pos);
1148     ret = TRUE;
1149   }
1150
1151   return ret;
1152 }
1153
1154 #define _gst_uri_normalize_scheme _gst_uri_normalize_lowercase
1155 #define _gst_uri_normalize_hostname _gst_uri_normalize_lowercase
1156
1157 static gboolean
1158 _gst_uri_normalize_path (GList ** path)
1159 {
1160   GList *new_path;
1161
1162   new_path = _remove_dot_segments (*path);
1163   if (_gst_uri_compare_lists (new_path, *path, (GCompareFunc) g_strcmp0) != 0) {
1164     g_list_free_full (*path, g_free);
1165     *path = new_path;
1166     return TRUE;
1167   }
1168   g_list_free_full (new_path, g_free);
1169
1170   return FALSE;
1171 }
1172
1173 static gboolean
1174 _gst_uri_normalize_str_noop (gchar * str)
1175 {
1176   return FALSE;
1177 }
1178
1179 static gboolean
1180 _gst_uri_normalize_table_noop (GHashTable * table)
1181 {
1182   return FALSE;
1183 }
1184
1185 #define _gst_uri_normalize_userinfo _gst_uri_normalize_str_noop
1186 #define _gst_uri_normalize_query _gst_uri_normalize_table_noop
1187 #define _gst_uri_normalize_fragment _gst_uri_normalize_str_noop
1188
1189 /* RFC 3986 functions */
1190
1191 static GList *
1192 _merge (GList * base, GList * path)
1193 {
1194   GList *ret, *path_copy, *last;
1195
1196   path_copy = g_list_copy_deep (path, (GCopyFunc) g_strdup, NULL);
1197   /* if base is NULL make path absolute */
1198   if (base == NULL) {
1199     if (path_copy != NULL && path_copy->data != NULL) {
1200       path_copy = g_list_prepend (path_copy, NULL);
1201     }
1202     return path_copy;
1203   }
1204
1205   ret = g_list_copy_deep (base, (GCopyFunc) g_strdup, NULL);
1206   last = g_list_last (ret);
1207   ret = g_list_remove_link (ret, last);
1208   g_list_free_full (last, g_free);
1209   ret = g_list_concat (ret, path_copy);
1210
1211   return ret;
1212 }
1213
1214 static GList *
1215 _remove_dot_segments (GList * path)
1216 {
1217   GList *out, *elem, *next;
1218
1219   if (path == NULL)
1220     return NULL;
1221
1222   out = g_list_copy_deep (path, (GCopyFunc) g_strdup, NULL);
1223
1224   for (elem = out; elem; elem = next) {
1225     next = elem->next;
1226     if (elem->data == NULL && elem != out && next != NULL) {
1227       out = g_list_delete_link (out, elem);
1228     } else if (g_strcmp0 (elem->data, ".") == 0) {
1229       g_free (elem->data);
1230       out = g_list_delete_link (out, elem);
1231     } else if (g_strcmp0 (elem->data, "..") == 0) {
1232       GList *prev = g_list_previous (elem);
1233       if (prev && (prev != out || prev->data != NULL)) {
1234         g_free (prev->data);
1235         out = g_list_delete_link (out, prev);
1236       }
1237       g_free (elem->data);
1238       out = g_list_delete_link (out, elem);
1239     }
1240   }
1241
1242   return out;
1243 }
1244
1245 static gchar *
1246 _gst_uri_escape_userinfo (const gchar * userinfo)
1247 {
1248   return g_uri_escape_string (userinfo,
1249       G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, FALSE);
1250 }
1251
1252 static gchar *
1253 _gst_uri_escape_host (const gchar * host)
1254 {
1255   return g_uri_escape_string (host,
1256       G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, FALSE);
1257 }
1258
1259 static gchar *
1260 _gst_uri_escape_host_colon (const gchar * host)
1261 {
1262   return g_uri_escape_string (host,
1263       G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":", FALSE);
1264 }
1265
1266 static gchar *
1267 _gst_uri_escape_path_segment (const gchar * segment)
1268 {
1269   return g_uri_escape_string (segment,
1270       G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, FALSE);
1271 }
1272
1273 static gchar *
1274 _gst_uri_escape_http_query_element (const gchar * element)
1275 {
1276   gchar *ret, *c;
1277
1278   ret = g_uri_escape_string (element, "!$'()*,;:@/? ", FALSE);
1279   for (c = ret; *c; c++)
1280     if (*c == ' ')
1281       *c = '+';
1282   return ret;
1283 }
1284
1285 static gchar *
1286 _gst_uri_escape_fragment (const gchar * fragment)
1287 {
1288   return g_uri_escape_string (fragment,
1289       G_URI_RESERVED_CHARS_ALLOWED_IN_PATH "?", FALSE);
1290 }
1291
1292 static GList *
1293 _gst_uri_string_to_list (const gchar * str, const gchar * sep, gboolean convert,
1294     gboolean unescape)
1295 {
1296   GList *new_list = NULL;
1297
1298   if (str) {
1299     guint pct_sep_len = 0;
1300     gchar *pct_sep = NULL;
1301     gchar **split_str;
1302
1303     if (convert && !unescape) {
1304       pct_sep = g_strdup_printf ("%%%2.2X", (guint) (*sep));
1305       pct_sep_len = 3;
1306     }
1307
1308     split_str = g_strsplit (str, sep, -1);
1309     if (split_str) {
1310       gchar **next_elem;
1311       for (next_elem = split_str; *next_elem; next_elem += 1) {
1312         gchar *elem = *next_elem;
1313         if (*elem == '\0') {
1314           new_list = g_list_append (new_list, NULL);
1315         } else {
1316           if (convert && !unescape) {
1317             gchar *next_sep;
1318             for (next_sep = strcasestr (elem, pct_sep); next_sep;
1319                 next_sep = strcasestr (next_sep + 1, pct_sep)) {
1320               *next_sep = *sep;
1321               memmove (next_sep + 1, next_sep + pct_sep_len,
1322                   strlen (next_sep + pct_sep_len) + 1);
1323             }
1324           }
1325           if (unescape) {
1326             *next_elem = g_uri_unescape_string (elem, NULL);
1327             g_free (elem);
1328             elem = *next_elem;
1329           }
1330           new_list = g_list_append (new_list, g_strdup (elem));
1331         }
1332       }
1333     }
1334     g_strfreev (split_str);
1335     if (convert && !unescape)
1336       g_free (pct_sep);
1337   }
1338
1339   return new_list;
1340 }
1341
1342 static GHashTable *
1343 _gst_uri_string_to_table (const gchar * str, const gchar * part_sep,
1344     const gchar * kv_sep, gboolean convert, gboolean unescape)
1345 {
1346   GHashTable *new_table = NULL;
1347
1348   if (str) {
1349     gchar *pct_part_sep = NULL, *pct_kv_sep = NULL;
1350     gchar **split_parts;
1351
1352     new_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1353
1354     if (convert && !unescape) {
1355       pct_part_sep = g_strdup_printf ("%%%2.2X", (guint) (*part_sep));
1356       pct_kv_sep = g_strdup_printf ("%%%2.2X", (guint) (*kv_sep));
1357     }
1358
1359     split_parts = g_strsplit (str, part_sep, -1);
1360     if (split_parts) {
1361       gchar **next_part;
1362       for (next_part = split_parts; *next_part; next_part += 1) {
1363         gchar *part = *next_part;
1364         gchar *kv_sep_pos;
1365         gchar *key, *value;
1366         /* if we are converting percent encoded versions of separators then
1367          *  substitute the part separator now. */
1368         if (convert && !unescape) {
1369           gchar *next_sep;
1370           for (next_sep = strcasestr (part, pct_part_sep); next_sep;
1371               next_sep = strcasestr (next_sep + 1, pct_part_sep)) {
1372             *next_sep = *part_sep;
1373             memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1374           }
1375         }
1376         /* find the key/value separator within the part */
1377         kv_sep_pos = g_strstr_len (part, -1, kv_sep);
1378         if (kv_sep_pos == NULL) {
1379           if (unescape) {
1380             key = g_uri_unescape_string (part, NULL);
1381           } else {
1382             key = g_strdup (part);
1383           }
1384           value = NULL;
1385         } else {
1386           if (unescape) {
1387             key = g_uri_unescape_segment (part, kv_sep_pos, NULL);
1388             value = g_uri_unescape_string (kv_sep_pos + 1, NULL);
1389           } else {
1390             key = g_strndup (part, kv_sep_pos - part);
1391             value = g_strdup (kv_sep_pos + 1);
1392           }
1393         }
1394         /* if we are converting percent encoded versions of separators then
1395          *  substitute the key/value separator in both key and value now. */
1396         if (convert && !unescape) {
1397           gchar *next_sep;
1398           for (next_sep = strcasestr (key, pct_kv_sep); next_sep;
1399               next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1400             *next_sep = *kv_sep;
1401             memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1402           }
1403           if (value) {
1404             for (next_sep = strcasestr (value, pct_kv_sep); next_sep;
1405                 next_sep = strcasestr (next_sep + 1, pct_kv_sep)) {
1406               *next_sep = *kv_sep;
1407               memmove (next_sep + 1, next_sep + 3, strlen (next_sep + 3) + 1);
1408             }
1409           }
1410         }
1411         /* add value to the table */
1412         g_hash_table_insert (new_table, key, value);
1413       }
1414     }
1415     /* tidy up */
1416     g_strfreev (split_parts);
1417     if (convert && !unescape) {
1418       g_free (pct_part_sep);
1419       g_free (pct_kv_sep);
1420     }
1421   }
1422
1423   return new_table;
1424 }
1425
1426
1427 /*
1428  * Method definitions.
1429  */
1430
1431 /**
1432  * gst_uri_new:
1433  * @scheme: (nullable): The scheme for the new URI.
1434  * @userinfo: (nullable): The user-info for the new URI.
1435  * @host: (nullable): The host name for the new URI.
1436  * @port: The port number for the new URI or %GST_URI_NO_PORT.
1437  * @path: (nullable): The path for the new URI with '/' separating path
1438  *                      elements.
1439  * @query: (nullable): The query string for the new URI with '&' separating
1440  *                       query elements. Elements containing '&' characters
1441  *                       should encode them as "%26".
1442  * @fragment: (nullable): The fragment name for the new URI.
1443  *
1444  * Creates a new #GstUri object with the given URI parts. The path and query
1445  * strings will be broken down into their elements. All strings should not be
1446  * escaped except where indicated.
1447  *
1448  * Returns: (transfer full): A new #GstUri object.
1449  *
1450  * Since: 1.6
1451  */
1452 GstUri *
1453 gst_uri_new (const gchar * scheme, const gchar * userinfo, const gchar * host,
1454     guint port, const gchar * path, const gchar * query, const gchar * fragment)
1455 {
1456   GstUri *new_uri;
1457
1458   new_uri = _gst_uri_new ();
1459   if (new_uri) {
1460     new_uri->scheme = g_strdup (scheme);
1461     new_uri->userinfo = g_strdup (userinfo);
1462     new_uri->host = g_strdup (host);
1463     new_uri->port = port;
1464     new_uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
1465     new_uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, FALSE);
1466     new_uri->fragment = g_strdup (fragment);
1467   }
1468
1469   return new_uri;
1470 }
1471
1472 /**
1473  * gst_uri_new_with_base:
1474  * @base: (transfer none)(nullable): The base URI to join the new URI to.
1475  * @scheme: (nullable): The scheme for the new URI.
1476  * @userinfo: (nullable): The user-info for the new URI.
1477  * @host: (nullable): The host name for the new URI.
1478  * @port: The port number for the new URI or %GST_URI_NO_PORT.
1479  * @path: (nullable): The path for the new URI with '/' separating path
1480  *                      elements.
1481  * @query: (nullable): The query string for the new URI with '&' separating
1482  *                       query elements. Elements containing '&' characters
1483  *                       should encode them as "%26".
1484  * @fragment: (nullable): The fragment name for the new URI.
1485  *
1486  * Like gst_uri_new(), but joins the new URI onto a base URI.
1487  *
1488  * Returns: (transfer full): The new URI joined onto @base.
1489  *
1490  * Since: 1.6
1491  */
1492 GstUri *
1493 gst_uri_new_with_base (GstUri * base, const gchar * scheme,
1494     const gchar * userinfo, const gchar * host, guint port, const gchar * path,
1495     const gchar * query, const gchar * fragment)
1496 {
1497   GstUri *new_rel_uri;
1498   GstUri *new_uri;
1499
1500   g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1501
1502   new_rel_uri = gst_uri_new (scheme, userinfo, host, port, path, query,
1503       fragment);
1504   new_uri = gst_uri_join (base, new_rel_uri);
1505   gst_uri_unref (new_rel_uri);
1506
1507   return new_uri;
1508 }
1509
1510 /**
1511  * gst_uri_from_string:
1512  * @uri: The URI string to parse.
1513  *
1514  * Parses a URI string into a new #GstUri object. Will return NULL if the URI
1515  * cannot be parsed.
1516  *
1517  * Returns: (transfer full)(nullable): A new #GstUri object, or NULL.
1518  *
1519  * Since: 1.6
1520  */
1521 GstUri *
1522 gst_uri_from_string (const gchar * uri)
1523 {
1524   const gchar *orig_uri = uri;
1525   GstUri *uri_obj;
1526
1527   uri_obj = _gst_uri_new ();
1528
1529   if (uri_obj && uri != NULL) {
1530     int i = 0;
1531
1532     /* be helpful and skip initial white space */
1533     while (*uri == '\v' || g_ascii_isspace (*uri))
1534       uri++;
1535
1536     if (g_ascii_isalpha (uri[i])) {
1537       /* find end of scheme name */
1538       i++;
1539       while (g_ascii_isalnum (uri[i]) || uri[i] == '+' || uri[i] == '-' ||
1540           uri[i] == '.')
1541         i++;
1542     }
1543     if (i > 0 && uri[i] == ':') {
1544       /* get scheme */
1545       uri_obj->scheme = g_strndup (uri, i);
1546       uri += i + 1;
1547     }
1548     if (uri[0] == '/' && uri[1] == '/') {
1549       const gchar *eoa, *eoui, *eoh, *reoh;
1550       /* get authority [userinfo@]host[:port] */
1551       uri += 2;
1552       /* find end of authority */
1553       eoa = uri + strcspn (uri, "/?#");
1554
1555       /* find end of userinfo */
1556       eoui = strchr (uri, '@');
1557       if (eoui != NULL && eoui < eoa) {
1558         uri_obj->userinfo = g_uri_unescape_segment (uri, eoui, NULL);
1559         uri = eoui + 1;
1560       }
1561       /* find end of host */
1562       if (uri[0] == '[') {
1563         eoh = strchr (uri, ']');
1564         if (eoh == NULL || eoh > eoa) {
1565           GST_DEBUG ("Unable to parse the host part of the URI '%s'.",
1566               orig_uri);
1567           _gst_uri_free (uri_obj);
1568           return NULL;
1569         }
1570         reoh = eoh + 1;
1571         uri++;
1572       } else {
1573         reoh = eoh = strchr (uri, ':');
1574         if (eoh == NULL || eoh > eoa)
1575           reoh = eoh = eoa;
1576       }
1577       /* don't capture empty host strings */
1578       if (eoh != uri)
1579         uri_obj->host = g_uri_unescape_segment (uri, eoh, NULL);
1580
1581       uri = reoh;
1582       if (uri < eoa) {
1583         /* if port number is malformed then we can't parse this */
1584         if (uri[0] != ':' || strspn (uri + 1, "0123456789") != eoa - uri - 1) {
1585           GST_DEBUG ("Unable to parse host/port part of the URI '%s'.",
1586               orig_uri);
1587           _gst_uri_free (uri_obj);
1588           return NULL;
1589         }
1590         /* otherwise treat port as unsigned decimal number */
1591         uri++;
1592         while (uri < eoa) {
1593           uri_obj->port = uri_obj->port * 10 + g_ascii_digit_value (*uri);
1594           uri++;
1595         }
1596       }
1597       uri = eoa;
1598     }
1599     if (uri != NULL && uri[0] != '\0') {
1600       /* get path */
1601       size_t len;
1602       len = strcspn (uri, "?#");
1603       if (uri[len] == '\0') {
1604         uri_obj->path = _gst_uri_string_to_list (uri, "/", FALSE, TRUE);
1605         uri = NULL;
1606       } else {
1607         if (len > 0) {
1608           gchar *path_str = g_strndup (uri, len);
1609           uri_obj->path = _gst_uri_string_to_list (path_str, "/", FALSE, TRUE);
1610           g_free (path_str);
1611         }
1612         uri += len;
1613       }
1614     }
1615     if (uri != NULL && uri[0] == '?') {
1616       /* get query */
1617       gchar *eoq;
1618       eoq = strchr (++uri, '#');
1619       if (eoq == NULL) {
1620         uri_obj->query = _gst_uri_string_to_table (uri, "&", "=", TRUE, TRUE);
1621         uri = NULL;
1622       } else {
1623         if (eoq != uri) {
1624           gchar *query_str = g_strndup (uri, eoq - uri);
1625           uri_obj->query = _gst_uri_string_to_table (query_str, "&", "=", TRUE,
1626               TRUE);
1627           g_free (query_str);
1628         }
1629         uri = eoq;
1630       }
1631     }
1632     if (uri != NULL && uri[0] == '#') {
1633       uri_obj->fragment = g_uri_unescape_string (uri + 1, NULL);
1634     }
1635   }
1636
1637   return uri_obj;
1638 }
1639
1640 /**
1641  * gst_uri_from_string_with_base:
1642  * @base: (transfer none)(nullable): The base URI to join the new URI with.
1643  * @uri: The URI string to parse.
1644  *
1645  * Like gst_uri_from_string() but also joins with a base URI.
1646  *
1647  * Returns: (transfer full): A new #GstUri object.
1648  *
1649  * Since: 1.6
1650  */
1651 GstUri *
1652 gst_uri_from_string_with_base (GstUri * base, const gchar * uri)
1653 {
1654   GstUri *new_rel_uri;
1655   GstUri *new_uri;
1656
1657   g_return_val_if_fail (base == NULL || GST_IS_URI (base), NULL);
1658
1659   new_rel_uri = gst_uri_from_string (uri);
1660   new_uri = gst_uri_join (base, new_rel_uri);
1661   gst_uri_unref (new_rel_uri);
1662
1663   return new_uri;
1664 }
1665
1666 /**
1667  * gst_uri_equal:
1668  * @first: First #GstUri to compare.
1669  * @second: Second #GstUri to compare.
1670  *
1671  * Compares two #GstUri objects to see if they represent the same normalized
1672  * URI.
1673  *
1674  * Returns: %TRUE if the normalized versions of the two URI's would be equal.
1675  *
1676  * Since: 1.6
1677  */
1678 gboolean
1679 gst_uri_equal (const GstUri * first, const GstUri * second)
1680 {
1681   gchar *first_norm = NULL, *second_norm = NULL;
1682   GList *first_norm_list = NULL, *second_norm_list = NULL;
1683   const gchar *first_cmp, *second_cmp;
1684   GHashTableIter table_iter;
1685   gpointer key, value;
1686   int result;
1687
1688   g_return_val_if_fail ((first == NULL || GST_IS_URI (first)) &&
1689       (second == NULL || GST_IS_URI (second)), FALSE);
1690
1691   if (first == second)
1692     return TRUE;
1693
1694   if (first == NULL || second == NULL)
1695     return FALSE;
1696
1697   if (first->port != second->port)
1698     return FALSE;
1699
1700 /* work out a version of field value (normalized or not) to compare.
1701  * first_cmp, second_cmp will be the values to compare later.
1702  * first_norm, second_norm will be non-NULL if normalized versions are used,
1703  *  and need to be freed later.
1704  */
1705 #define GST_URI_NORMALIZED_FIELD(pos, field, norm_fn, flags) \
1706   pos##_cmp = pos->field; \
1707   if (_gst_uri_first_non_normalized_char ((gchar*)pos##_cmp, flags) != NULL) { \
1708     pos##_norm = g_strdup (pos##_cmp); \
1709     norm_fn (pos##_norm); \
1710     pos##_cmp = pos##_norm; \
1711   }
1712
1713 /* compare two string values, normalizing if needed */
1714 #define GST_URI_NORMALIZED_CMP_STR(field, norm_fn, flags) \
1715   GST_URI_NORMALIZED_FIELD (first, field, norm_fn, flags) \
1716   GST_URI_NORMALIZED_FIELD (second, field, norm_fn, flags) \
1717   result = g_strcmp0 (first_cmp, second_cmp); \
1718   g_free (first_norm); \
1719   first_norm = NULL; \
1720   g_free (second_norm); \
1721   second_norm = NULL; \
1722   if (result != 0) return FALSE
1723
1724 /* compare two string values */
1725 #define GST_URI_CMP_STR(field) \
1726   if (g_strcmp0 (first->field, second->field) != 0) return FALSE
1727
1728 /* compare two GLists, normalize lists if needed before comparison */
1729 #define GST_URI_NORMALIZED_CMP_LIST(field, norm_fn, copy_fn, cmp_fn, free_fn) \
1730   first_norm_list = g_list_copy_deep (first->field, (GCopyFunc) copy_fn, NULL); \
1731   norm_fn (&first_norm_list); \
1732   second_norm_list = g_list_copy_deep (second->field, (GCopyFunc) copy_fn, NULL); \
1733   norm_fn (&second_norm_list); \
1734   result = _gst_uri_compare_lists (first_norm_list, second_norm_list, (GCompareFunc) cmp_fn); \
1735   g_list_free_full (first_norm_list, free_fn); \
1736   g_list_free_full (second_norm_list, free_fn); \
1737   if (result != 0) return FALSE
1738
1739   GST_URI_CMP_STR (userinfo);
1740
1741   GST_URI_CMP_STR (fragment);
1742
1743   GST_URI_NORMALIZED_CMP_STR (scheme, _gst_uri_normalize_scheme,
1744       _GST_URI_NORMALIZE_LOWERCASE);
1745
1746   GST_URI_NORMALIZED_CMP_STR (host, _gst_uri_normalize_hostname,
1747       _GST_URI_NORMALIZE_LOWERCASE);
1748
1749   GST_URI_NORMALIZED_CMP_LIST (path, _gst_uri_normalize_path, g_strdup,
1750       g_strcmp0, g_free);
1751
1752   if (first->query == NULL && second->query != NULL)
1753     return FALSE;
1754   if (first->query != NULL && second->query == NULL)
1755     return FALSE;
1756   if (first->query != NULL) {
1757     if (g_hash_table_size (first->query) != g_hash_table_size (second->query))
1758       return FALSE;
1759
1760     g_hash_table_iter_init (&table_iter, first->query);
1761     while (g_hash_table_iter_next (&table_iter, &key, &value)) {
1762       if (!g_hash_table_contains (second->query, key))
1763         return FALSE;
1764       result = g_strcmp0 (g_hash_table_lookup (second->query, key), value);
1765       if (result != 0)
1766         return FALSE;
1767     }
1768   }
1769 #undef GST_URI_NORMALIZED_CMP_STR
1770 #undef GST_URI_CMP_STR
1771 #undef GST_URI_NORMALIZED_CMP_LIST
1772 #undef GST_URI_NORMALIZED_FIELD
1773
1774   return TRUE;
1775 }
1776
1777 /**
1778  * gst_uri_join:
1779  * @base_uri: (transfer none)(nullable): The base URI to join another to.
1780  * @ref_uri: (transfer none)(nullable): The reference URI to join onto the
1781  *                                        base URI.
1782  *
1783  * Join a reference URI onto a base URI using the method from RFC 3986.
1784  * If either URI is %NULL then the other URI will be returned with the ref count
1785  * increased.
1786  *
1787  * Returns: (transfer full): A #GstUri which represents the base with the
1788  *                           reference URI joined on.
1789  *
1790  * Since: 1.6
1791  */
1792 GstUri *
1793 gst_uri_join (GstUri * base_uri, GstUri * ref_uri)
1794 {
1795   const gchar *r_scheme;
1796   GstUri *t;
1797
1798   g_return_val_if_fail ((base_uri == NULL || GST_IS_URI (base_uri)) &&
1799       (ref_uri == NULL || GST_IS_URI (ref_uri)), NULL);
1800
1801   if (base_uri == NULL && ref_uri == NULL)
1802     return NULL;
1803   if (base_uri == NULL) {
1804     g_return_val_if_fail (GST_IS_URI (ref_uri), NULL);
1805     return gst_uri_ref (ref_uri);
1806   }
1807   if (ref_uri == NULL) {
1808     g_return_val_if_fail (GST_IS_URI (base_uri), NULL);
1809     return gst_uri_ref (base_uri);
1810   }
1811
1812   g_return_val_if_fail (GST_IS_URI (base_uri) && GST_IS_URI (ref_uri), NULL);
1813
1814   t = _gst_uri_new ();
1815
1816   if (t == NULL)
1817     return t;
1818
1819   /* process according to RFC3986 */
1820   r_scheme = ref_uri->scheme;
1821   if (r_scheme != NULL && g_strcmp0 (base_uri->scheme, r_scheme) == 0) {
1822     r_scheme = NULL;
1823   }
1824   if (r_scheme != NULL) {
1825     t->scheme = g_strdup (r_scheme);
1826     t->userinfo = g_strdup (ref_uri->userinfo);
1827     t->host = g_strdup (ref_uri->host);
1828     t->port = ref_uri->port;
1829     t->path = _remove_dot_segments (ref_uri->path);
1830     t->query = _gst_uri_copy_query_table (ref_uri->query);
1831   } else {
1832     if (ref_uri->host != NULL) {
1833       t->userinfo = g_strdup (ref_uri->userinfo);
1834       t->host = g_strdup (ref_uri->host);
1835       t->port = ref_uri->port;
1836       t->path = _remove_dot_segments (ref_uri->path);
1837       t->query = _gst_uri_copy_query_table (ref_uri->query);
1838     } else {
1839       if (ref_uri->path == NULL) {
1840         t->path = g_list_copy_deep (base_uri->path, (GCopyFunc) g_strdup, NULL);
1841         if (ref_uri->query != NULL)
1842           t->query = _gst_uri_copy_query_table (ref_uri->query);
1843         else
1844           t->query = _gst_uri_copy_query_table (base_uri->query);
1845       } else {
1846         if (ref_uri->path->data == NULL)
1847           t->path = _remove_dot_segments (ref_uri->path);
1848         else {
1849           GList *mrgd = _merge (base_uri->path, ref_uri->path);
1850           t->path = _remove_dot_segments (mrgd);
1851           g_list_free_full (mrgd, g_free);
1852         }
1853         t->query = _gst_uri_copy_query_table (ref_uri->query);
1854       }
1855       t->userinfo = g_strdup (base_uri->userinfo);
1856       t->host = g_strdup (base_uri->host);
1857       t->port = base_uri->port;
1858     }
1859     t->scheme = g_strdup (base_uri->scheme);
1860   }
1861   t->fragment = g_strdup (ref_uri->fragment);
1862
1863   return t;
1864 }
1865
1866 /**
1867  * gst_uri_join_strings:
1868  * @base_uri: The percent-encoded base URI.
1869  * @ref_uri: The percent-encoded reference URI to join to the @base_uri.
1870  *
1871  * This is a convenience function to join two URI strings and return the result.
1872  * The returned string should be g_free()'d after use.
1873  *
1874  * Returns: (transfer full): A string representing the percent-encoded join of
1875  *          the two URIs.
1876  *
1877  * Since: 1.6
1878  */
1879 gchar *
1880 gst_uri_join_strings (const gchar * base_uri, const gchar * ref_uri)
1881 {
1882   GstUri *base, *result;
1883   gchar *result_uri;
1884
1885   base = gst_uri_from_string (base_uri);
1886   result = gst_uri_from_string_with_base (base, ref_uri);
1887   result_uri = gst_uri_to_string (result);
1888   gst_uri_unref (base);
1889   gst_uri_unref (result);
1890
1891   return result_uri;
1892 }
1893
1894 /**
1895  * gst_uri_is_writable:
1896  * @uri: The #GstUri object to test.
1897  *
1898  * Check if it is safe to write to this #GstUri.
1899  *
1900  * Check if the refcount of @uri is exactly 1, meaning that no other
1901  * reference exists to the #GstUri and that the #GstUri is therefore writable.
1902  *
1903  * Modification of a #GstUri should only be done after verifying that it is
1904  * writable.
1905  *
1906  * Returns: %TRUE if it is safe to write to the object.
1907  *
1908  * Since: 1.6
1909  */
1910 gboolean
1911 gst_uri_is_writable (const GstUri * uri)
1912 {
1913   g_return_val_if_fail (GST_IS_URI (uri), FALSE);
1914   return gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (uri));
1915 }
1916
1917 /**
1918  * gst_uri_make_writable:
1919  * @uri: (transfer full): The #GstUri object to make writable.
1920  *
1921  * Make the #GstUri writable.
1922  *
1923  * Checks if @uri is writable, and if so the original object is returned. If
1924  * not, then a writable copy is made and returned. This gives away the
1925  * reference to @uri and returns a reference to the new #GstUri.
1926  * If @uri is %NULL then %NULL is returned.
1927  *
1928  * Returns: (transfer full): A writable version of @uri.
1929  *
1930  * Since: 1.6
1931  */
1932 GstUri *
1933 gst_uri_make_writable (GstUri * uri)
1934 {
1935   g_return_val_if_fail (GST_IS_URI (uri), NULL);
1936   return
1937       GST_URI_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (uri)));
1938 }
1939
1940 /**
1941  * gst_uri_to_string:
1942  * @uri: This #GstUri to convert to a string.
1943  *
1944  * Convert the URI to a string.
1945  *
1946  * Returns the URI as held in this object as a gchar* %NUL terminated string.
1947  * The caller should g_free() the string once they are finished with it.
1948  * The string is put together as described in RFC 3986.
1949  *
1950  * Returns: (transfer full): The string version of the URI.
1951  *
1952  * Since: 1.6
1953  */
1954 gchar *
1955 gst_uri_to_string (const GstUri * uri)
1956 {
1957   GString *uri_str;
1958   gchar *escaped;
1959
1960   g_return_val_if_fail (GST_IS_URI (uri), NULL);
1961
1962   uri_str = g_string_new (NULL);
1963
1964   if (uri->scheme != NULL)
1965     g_string_append_printf (uri_str, "%s:", uri->scheme);
1966
1967   if (uri->userinfo != NULL || uri->host != NULL ||
1968       uri->port != GST_URI_NO_PORT)
1969     g_string_append (uri_str, "//");
1970
1971   if (uri->userinfo != NULL) {
1972     escaped = _gst_uri_escape_userinfo (uri->userinfo);
1973     g_string_append_printf (uri_str, "%s@", escaped);
1974     g_free (escaped);
1975   }
1976
1977   if (uri->host != NULL) {
1978     if (strchr (uri->host, ':') != NULL) {
1979       escaped = _gst_uri_escape_host_colon (uri->host);
1980       g_string_append_printf (uri_str, "[%s]", escaped);
1981       g_free (escaped);
1982     } else {
1983       escaped = _gst_uri_escape_host (uri->host);
1984       g_string_append (uri_str, escaped);
1985       g_free (escaped);
1986     }
1987   }
1988
1989   if (uri->port != GST_URI_NO_PORT)
1990     g_string_append_printf (uri_str, ":%u", uri->port);
1991
1992   if (uri->path != NULL) {
1993     escaped = gst_uri_get_path_string (uri);
1994     g_string_append (uri_str, escaped);
1995     g_free (escaped);
1996   }
1997
1998   if (uri->query) {
1999     g_string_append (uri_str, "?");
2000     escaped = gst_uri_get_query_string (uri);
2001     g_string_append (uri_str, escaped);
2002     g_free (escaped);
2003   }
2004
2005   if (uri->fragment != NULL) {
2006     escaped = _gst_uri_escape_fragment (uri->fragment);
2007     g_string_append_printf (uri_str, "#%s", escaped);
2008     g_free (escaped);
2009   }
2010
2011   return g_string_free (uri_str, FALSE);
2012 }
2013
2014 /**
2015  * gst_uri_is_normalized:
2016  * @uri: The #GstUri to test to see if it is normalized.
2017  *
2018  * Tests the @uri to see if it is normalized. A %NULL @uri is considered to be
2019  * normalized.
2020  *
2021  * Returns: TRUE if the URI is normalized or is %NULL.
2022  *
2023  * Since: 1.6
2024  */
2025 gboolean
2026 gst_uri_is_normalized (const GstUri * uri)
2027 {
2028   GList *new_path;
2029   gboolean ret;
2030
2031   if (uri == NULL)
2032     return TRUE;
2033
2034   g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2035
2036   /* check for non-normalized characters in uri parts */
2037   if (_gst_uri_first_non_normalized_char (uri->scheme,
2038           _GST_URI_NORMALIZE_LOWERCASE) != NULL ||
2039       /*_gst_uri_first_non_normalized_char (uri->userinfo,
2040           _GST_URI_NORMALIZE_PERCENTAGES) != NULL || */
2041       _gst_uri_first_non_normalized_char (uri->host,
2042           _GST_URI_NORMALIZE_LOWERCASE /*| _GST_URI_NORMALIZE_PERCENTAGES */ )
2043       != NULL
2044       /*|| _gst_uri_first_non_normalized_char (uri->path,
2045          _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2046          || _gst_uri_first_non_normalized_char (uri->query,
2047          _GST_URI_NORMALIZE_PERCENTAGES) != NULL
2048          || _gst_uri_first_non_normalized_char (uri->fragment,
2049          _GST_URI_NORMALIZE_PERCENTAGES) != NULL */ )
2050     return FALSE;
2051
2052   /* also check path has had dot segments removed */
2053   new_path = _remove_dot_segments (uri->path);
2054   ret =
2055       (_gst_uri_compare_lists (new_path, uri->path,
2056           (GCompareFunc) g_strcmp0) == 0);
2057   g_list_free_full (new_path, g_free);
2058   return ret;
2059 }
2060
2061 /**
2062  * gst_uri_normalize:
2063  * @uri: (transfer none): The #GstUri to normalize.
2064  *
2065  * Normalization will remove extra path segments ("." and "..") from the URI. It
2066  * will also convert the scheme and host name to lower case and any
2067  * percent-encoded values to uppercase.
2068  *
2069  * The #GstUri object must be writable. Check with gst_uri_is_writable() or use
2070  * gst_uri_make_writable() first.
2071  *
2072  * Returns: TRUE if the URI was modified.
2073  *
2074  * Since: 1.6
2075  */
2076 gboolean
2077 gst_uri_normalize (GstUri * uri)
2078 {
2079   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2080
2081   return _gst_uri_normalize_scheme (uri->scheme) |
2082       _gst_uri_normalize_userinfo (uri->userinfo) |
2083       _gst_uri_normalize_hostname (uri->host) |
2084       _gst_uri_normalize_path (&uri->path) |
2085       _gst_uri_normalize_query (uri->query) |
2086       _gst_uri_normalize_fragment (uri->fragment);
2087 }
2088
2089 /**
2090  * gst_uri_get_scheme:
2091  * @uri: (nullable): This #GstUri object.
2092  *
2093  * Get the scheme name from the URI or %NULL if it doesn't exist.
2094  * If @uri is %NULL then returns %NULL.
2095  *
2096  * Returns: The scheme from the #GstUri object or %NULL.
2097  */
2098 const gchar *
2099 gst_uri_get_scheme (const GstUri * uri)
2100 {
2101   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2102   return (uri ? uri->scheme : NULL);
2103 }
2104
2105 /**
2106  * gst_uri_set_scheme:
2107  * @uri: (transfer none)(nullable): The #GstUri to modify.
2108  * @scheme: The new scheme to set or %NULL to unset the scheme.
2109  *
2110  * Set or unset the scheme for the URI.
2111  *
2112  * Returns: %TRUE if the scheme was set/unset successfully.
2113  *
2114  * Since: 1.6
2115  */
2116 gboolean
2117 gst_uri_set_scheme (GstUri * uri, const gchar * scheme)
2118 {
2119   if (!uri)
2120     return scheme == NULL;
2121   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2122
2123   g_free (uri->scheme);
2124   uri->scheme = g_strdup (scheme);
2125
2126   return TRUE;
2127 }
2128
2129 /**
2130  * gst_uri_get_userinfo:
2131  * @uri: (nullable): This #GstUri object.
2132  *
2133  * Get the userinfo (usually in the form "username:password") from the URI
2134  * or %NULL if it doesn't exist. If @uri is %NULL then returns %NULL.
2135  *
2136  * Returns: The userinfo from the #GstUri object or %NULL.
2137  *
2138  * Since: 1.6
2139  */
2140 const gchar *
2141 gst_uri_get_userinfo (const GstUri * uri)
2142 {
2143   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2144   return (uri ? uri->userinfo : NULL);
2145 }
2146
2147 /**
2148  * gst_uri_set_userinfo:
2149  * @uri: (transfer none)(nullable): The #GstUri to modify.
2150  * @userinfo: The new user-information string to set or %NULL to unset.
2151  *
2152  * Set or unset the user information for the URI.
2153  *
2154  * Returns: %TRUE if the user information was set/unset successfully.
2155  *
2156  * Since: 1.6
2157  */
2158 gboolean
2159 gst_uri_set_userinfo (GstUri * uri, const gchar * userinfo)
2160 {
2161   if (!uri)
2162     return userinfo == NULL;
2163   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2164
2165   g_free (uri->userinfo);
2166   uri->userinfo = g_strdup (userinfo);
2167
2168   return TRUE;
2169 }
2170
2171 /**
2172  * gst_uri_get_host:
2173  * @uri: (nullable): This #GstUri object.
2174  *
2175  * Get the host name from the URI or %NULL if it doesn't exist.
2176  * If @uri is %NULL then returns %NULL.
2177  *
2178  * Returns: The host name from the #GstUri object or %NULL.
2179  *
2180  * Since: 1.6
2181  */
2182 const gchar *
2183 gst_uri_get_host (const GstUri * uri)
2184 {
2185   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2186   return (uri ? uri->host : NULL);
2187 }
2188
2189 /**
2190  * gst_uri_set_host:
2191  * @uri: (transfer none)(nullable): The #GstUri to modify.
2192  * @host: The new host string to set or %NULL to unset.
2193  *
2194  * Set or unset the host for the URI.
2195  *
2196  * Returns: %TRUE if the host was set/unset successfully.
2197  *
2198  * Since: 1.6
2199  */
2200 gboolean
2201 gst_uri_set_host (GstUri * uri, const gchar * host)
2202 {
2203   if (!uri)
2204     return host == NULL;
2205   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2206
2207   g_free (uri->host);
2208   uri->host = g_strdup (host);
2209
2210   return TRUE;
2211 }
2212
2213 /**
2214  * gst_uri_get_port:
2215  * @uri: (nullable): This #GstUri object.
2216  *
2217  * Get the port number from the URI or %GST_URI_NO_PORT if it doesn't exist.
2218  * If @uri is %NULL then returns %GST_URI_NO_PORT.
2219  *
2220  * Returns: The port number from the #GstUri object or %GST_URI_NO_PORT.
2221  *
2222  * Since: 1.6
2223  */
2224 guint
2225 gst_uri_get_port (const GstUri * uri)
2226 {
2227   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), GST_URI_NO_PORT);
2228   return (uri ? uri->port : GST_URI_NO_PORT);
2229 }
2230
2231 /**
2232  * gst_uri_set_port:
2233  * @uri: (transfer none)(nullable): The #GstUri to modify.
2234  * @port: The new port number to set or %GST_URI_NO_PORT to unset.
2235  *
2236  * Set or unset the port number for the URI.
2237  *
2238  * Returns: %TRUE if the port number was set/unset successfully.
2239  *
2240  * Since: 1.6
2241  */
2242 gboolean
2243 gst_uri_set_port (GstUri * uri, guint port)
2244 {
2245   if (!uri)
2246     return port == GST_URI_NO_PORT;
2247   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2248
2249   uri->port = port;
2250
2251   return TRUE;
2252 }
2253
2254 /**
2255  * gst_uri_get_path:
2256  * @uri: The #GstUri to get the path from.
2257  *
2258  * Extract the path string from the URI object.
2259  *
2260  * Returns: (transfer full): The path from the URI. Once finished with the
2261  *                           string should be g_free()'d.
2262  *
2263  * Since: 1.6
2264  */
2265 gchar *
2266 gst_uri_get_path (const GstUri * uri)
2267 {
2268   GList *path_segment;
2269   const gchar *sep = "";
2270   GString *ret;
2271
2272   if (!uri)
2273     return NULL;
2274   g_return_val_if_fail (GST_IS_URI (uri), NULL);
2275   if (!uri->path)
2276     return NULL;
2277
2278   ret = g_string_new (NULL);
2279
2280   for (path_segment = uri->path; path_segment;
2281       path_segment = path_segment->next) {
2282     g_string_append (ret, sep);
2283     if (path_segment->data) {
2284       g_string_append (ret, path_segment->data);
2285     }
2286     sep = "/";
2287   }
2288
2289   return g_string_free (ret, FALSE);
2290 }
2291
2292 /**
2293  * gst_uri_set_path:
2294  * @uri: (transfer none)(nullable): The #GstUri to modify.
2295  * @path: The new path to set with path segments separated by '/', or use %NULL
2296  *        to unset the path.
2297  *
2298  * Sets or unsets the path in the URI.
2299  *
2300  * Returns: %TRUE if the path was set successfully.
2301  *
2302  * Since: 1.6
2303  */
2304 gboolean
2305 gst_uri_set_path (GstUri * uri, const gchar * path)
2306 {
2307   if (!uri)
2308     return path == NULL;
2309   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2310
2311   g_list_free_full (uri->path, g_free);
2312   uri->path = _gst_uri_string_to_list (path, "/", FALSE, FALSE);
2313
2314   return TRUE;
2315 }
2316
2317 /**
2318  * gst_uri_get_path_string:
2319  * @uri: The #GstUri to get the path from.
2320  *
2321  * Extract the path string from the URI object as a percent encoded URI path.
2322  *
2323  * Returns: (transfer full): The path from the URI. Once finished with the
2324  *                           string should be g_free()'d.
2325  *
2326  * Since: 1.6
2327  */
2328 gchar *
2329 gst_uri_get_path_string (const GstUri * uri)
2330 {
2331   GList *path_segment;
2332   const gchar *sep = "";
2333   GString *ret;
2334   gchar *escaped;
2335
2336   if (!uri)
2337     return NULL;
2338   g_return_val_if_fail (GST_IS_URI (uri), NULL);
2339   if (!uri->path)
2340     return NULL;
2341
2342   ret = g_string_new (NULL);
2343
2344   for (path_segment = uri->path; path_segment;
2345       path_segment = path_segment->next) {
2346     g_string_append (ret, sep);
2347     if (path_segment->data) {
2348       escaped = _gst_uri_escape_path_segment (path_segment->data);
2349       g_string_append (ret, escaped);
2350       g_free (escaped);
2351     }
2352     sep = "/";
2353   }
2354
2355   return g_string_free (ret, FALSE);
2356 }
2357
2358 /**
2359  * gst_uri_set_path_string:
2360  * @uri: (transfer none)(nullable): The #GstUri to modify.
2361  * @path: The new percent encoded path to set with path segments separated by
2362  * '/', or use %NULL to unset the path.
2363  *
2364  * Sets or unsets the path in the URI.
2365  *
2366  * Returns: %TRUE if the path was set successfully.
2367  *
2368  * Since: 1.6
2369  */
2370 gboolean
2371 gst_uri_set_path_string (GstUri * uri, const gchar * path)
2372 {
2373   if (!uri)
2374     return path == NULL;
2375   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2376
2377   g_list_free_full (uri->path, g_free);
2378   uri->path = _gst_uri_string_to_list (path, "/", FALSE, TRUE);
2379   return TRUE;
2380 }
2381
2382 /**
2383  * gst_uri_get_path_segments:
2384  * @uri: (nullable): The #GstUri to get the path from.
2385  *
2386  * Get a list of path segments from the URI.
2387  *
2388  * Returns: (transfer full)(element-type gchar*): A #GList of path segment
2389  *          strings or %NULL if no path segments are available. Free the list
2390  *          when no longer needed with g_list_free_full(list, g_free).
2391  *
2392  * Since: 1.6
2393  */
2394 GList *
2395 gst_uri_get_path_segments (const GstUri * uri)
2396 {
2397   GList *ret = NULL;
2398
2399   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2400
2401   if (uri) {
2402     ret = g_list_copy_deep (uri->path, (GCopyFunc) g_strdup, NULL);
2403   }
2404
2405   return ret;
2406 }
2407
2408 /**
2409  * gst_uri_set_path_segments:
2410  * @uri: (transfer none)(nullable): The #GstUri to modify.
2411  * @path_segments: (transfer full)(nullable)(element-type gchar*): The new
2412  *                 path list to set.
2413  *
2414  * Replace the path segments list in the URI.
2415  *
2416  * Returns: %TRUE if the path segments were set successfully.
2417  *
2418  * Since: 1.6
2419  */
2420 gboolean
2421 gst_uri_set_path_segments (GstUri * uri, GList * path_segments)
2422 {
2423   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), FALSE);
2424
2425   if (!uri) {
2426     if (path_segments)
2427       g_list_free_full (path_segments, g_free);
2428     return path_segments == NULL;
2429   }
2430
2431   g_return_val_if_fail (gst_uri_is_writable (uri), FALSE);
2432
2433   g_list_free_full (uri->path, g_free);
2434   uri->path = path_segments;
2435   return TRUE;
2436 }
2437
2438 /**
2439  * gst_uri_append_path:
2440  * @uri: (transfer none)(nullable): The #GstUri to modify.
2441  * @relative_path: Relative path to append to the end of the current path.
2442  *
2443  * Append a path onto the end of the path in the URI. The path is not
2444  * normalized, call #gst_uri_normalize() to normalize the path.
2445  *
2446  * Returns: %TRUE if the path was appended successfully.
2447  *
2448  * Since: 1.6
2449  */
2450 gboolean
2451 gst_uri_append_path (GstUri * uri, const gchar * relative_path)
2452 {
2453   GList *rel_path_list;
2454
2455   if (!uri)
2456     return relative_path == NULL;
2457   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2458   if (!relative_path)
2459     return TRUE;
2460
2461   if (uri->path) {
2462     GList *last_elem = g_list_last (uri->path);
2463     if (last_elem->data == NULL) {
2464       uri->path = g_list_delete_link (uri->path, last_elem);
2465     }
2466   }
2467   rel_path_list = _gst_uri_string_to_list (relative_path, "/", FALSE, FALSE);
2468   /* if path was absolute, make it relative by removing initial NULL element */
2469   if (rel_path_list && rel_path_list->data == NULL) {
2470     rel_path_list = g_list_delete_link (rel_path_list, rel_path_list);
2471   }
2472   uri->path = g_list_concat (uri->path, rel_path_list);
2473   return TRUE;
2474 }
2475
2476 /**
2477  * gst_uri_append_path_segment:
2478  * @uri: (transfer none)(nullable): The #GstUri to modify.
2479  * @path_segment: The path segment string to append to the URI path.
2480  *
2481  * Append a single path segment onto the end of the URI path.
2482  *
2483  * Returns: %TRUE if the path was appended successfully.
2484  *
2485  * Since: 1.6
2486  */
2487 gboolean
2488 gst_uri_append_path_segment (GstUri * uri, const gchar * path_segment)
2489 {
2490   if (!uri)
2491     return path_segment == NULL;
2492   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2493   if (!path_segment)
2494     return TRUE;
2495
2496   /* if base path ends in a directory (i.e. last element is NULL), remove it */
2497   if (uri->path && g_list_last (uri->path)->data == NULL) {
2498     uri->path = g_list_delete_link (uri->path, g_list_last (uri->path));
2499   }
2500   uri->path = g_list_append (uri->path, g_strdup (path_segment));
2501   return TRUE;
2502 }
2503
2504 /**
2505  * gst_uri_get_query_string:
2506  * @uri: (nullable): The #GstUri to get the query string from.
2507  *
2508  * Get a percent encoded URI query string from the @uri.
2509  *
2510  * Returns: (transfer full): A percent encoded query string. Use g_free() when
2511  *          no longer needed.
2512  *
2513  * Since: 1.6
2514  */
2515 gchar *
2516 gst_uri_get_query_string (const GstUri * uri)
2517 {
2518   GHashTableIter iter;
2519   gpointer key, value;
2520   const gchar *sep = "";
2521   gchar *escaped;
2522   GString *ret;
2523
2524   if (!uri)
2525     return NULL;
2526   g_return_val_if_fail (GST_IS_URI (uri), NULL);
2527   if (!uri->query)
2528     return NULL;
2529
2530   ret = g_string_new (NULL);
2531   g_hash_table_iter_init (&iter, uri->query);
2532   while (g_hash_table_iter_next (&iter, &key, &value)) {
2533     g_string_append (ret, sep);
2534     escaped = _gst_uri_escape_http_query_element (key);
2535     g_string_append (ret, escaped);
2536     g_free (escaped);
2537     if (value) {
2538       escaped = _gst_uri_escape_http_query_element (value);
2539       g_string_append_printf (ret, "=%s", escaped);
2540       g_free (escaped);
2541     }
2542     sep = "&";
2543   }
2544
2545   return g_string_free (ret, FALSE);
2546 }
2547
2548 /**
2549  * gst_uri_set_query_string:
2550  * @uri: (transfer none)(nullable): The #GstUri to modify.
2551  * @query: The new percent encoded query string to use to populate the query
2552  *        table, or use %NULL to unset the query table.
2553  *
2554  * Sets or unsets the query table in the URI.
2555  *
2556  * Returns: %TRUE if the query table was set successfully.
2557  *
2558  * Since: 1.6
2559  */
2560 gboolean
2561 gst_uri_set_query_string (GstUri * uri, const gchar * query)
2562 {
2563   if (!uri)
2564     return query == NULL;
2565
2566   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2567
2568   if (uri->query)
2569     g_hash_table_unref (uri->query);
2570   uri->query = _gst_uri_string_to_table (query, "&", "=", TRUE, TRUE);
2571
2572   return TRUE;
2573 }
2574
2575 /**
2576  * gst_uri_get_query_table:
2577  * @uri: (nullable): The #GstUri to get the query table from.
2578  *
2579  * Get the query table from the URI. Keys and values in the table are freed
2580  * with g_free when they are deleted. A value may be %NULL to indicate that
2581  * the key should appear in the query string in the URI, but does not have a
2582  * value. Free the returned #GHashTable with #g_hash_table_unref() when it is
2583  * no longer required. Modifying this hash table will modify the query in the
2584  * URI.
2585  *
2586  * Returns: (transfer full)(element-type gchar* gchar*): The query hash table
2587  *          from the URI.
2588  *
2589  * Since: 1.6
2590  */
2591 GHashTable *
2592 gst_uri_get_query_table (const GstUri * uri)
2593 {
2594   if (!uri)
2595     return NULL;
2596   g_return_val_if_fail (GST_IS_URI (uri), NULL);
2597   if (!uri->query)
2598     return NULL;
2599
2600   return g_hash_table_ref (uri->query);
2601 }
2602
2603 /**
2604  * gst_uri_set_query_table:
2605  * @uri: (transfer none)(nullable): The #GstUri to modify.
2606  * @query_table: (transfer none)(nullable)(element-type gchar* gchar*): The new
2607  *               query table to use.
2608  *
2609  * Set the query table to use in the URI. The old table is unreferenced and a
2610  * reference to the new one is used instead. A value if %NULL for @query_table
2611  * will remove the query string from the URI.
2612  *
2613  * Returns: %TRUE if the new table was sucessfully used for the query table.
2614  *
2615  * Since: 1.6
2616  */
2617 gboolean
2618 gst_uri_set_query_table (GstUri * uri, GHashTable * query_table)
2619 {
2620   GHashTable *old_table = NULL;
2621
2622   if (!uri)
2623     return query_table == NULL;
2624   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2625
2626   old_table = uri->query;
2627   if (query_table)
2628     uri->query = g_hash_table_ref (query_table);
2629   else
2630     uri->query = NULL;
2631   if (old_table)
2632     g_hash_table_unref (old_table);
2633
2634   return TRUE;
2635 }
2636
2637 /**
2638  * gst_uri_set_query_value:
2639  * @uri: (transfer none)(nullable): The #GstUri to modify.
2640  * @query_key: (transfer none): The key for the query entry.
2641  * @query_value: (transfer none)(nullable): The value for the key.
2642  *
2643  * This inserts or replaces a key in the query table. A @query_value of %NULL
2644  * indicates that the key has no associated value, but will still be present in
2645  * the query string.
2646  *
2647  * Returns: %TRUE if the query table was sucessfully updated.
2648  *
2649  * Since: 1.6
2650  */
2651 gboolean
2652 gst_uri_set_query_value (GstUri * uri, const gchar * query_key,
2653     const gchar * query_value)
2654 {
2655   if (!uri)
2656     return FALSE;
2657   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2658
2659   if (!uri->query) {
2660     uri->query = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2661         g_free);
2662   }
2663   g_hash_table_insert (uri->query, g_strdup (query_key),
2664       g_strdup (query_value));
2665
2666   return TRUE;
2667 }
2668
2669 /**
2670  * gst_uri_remove_query_key:
2671  * @uri: (transfer none)(nullable): The #GstUri to modify.
2672  * @query_key: The key to remove.
2673  *
2674  * Remove an entry from the query table by key.
2675  *
2676  * Returns: %TRUE if the key existed in the table and was removed.
2677  *
2678  * Since: 1.6
2679  */
2680 gboolean
2681 gst_uri_remove_query_key (GstUri * uri, const gchar * query_key)
2682 {
2683   gboolean result;
2684
2685   if (!uri)
2686     return FALSE;
2687   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2688   if (!uri->query)
2689     return FALSE;
2690
2691   result = g_hash_table_remove (uri->query, query_key);
2692   /* if this was the last query entry, remove the query string completely */
2693   if (result && g_hash_table_size (uri->query) == 0) {
2694     g_hash_table_unref (uri->query);
2695     uri->query = NULL;
2696   }
2697   return result;
2698 }
2699
2700 /**
2701  * gst_uri_query_has_key:
2702  * @uri: (nullable): The #GstUri to examine.
2703  * @query_key: The key to lookup.
2704  *
2705  * Check if there is a query table entry for the @query_key key.
2706  *
2707  * Returns: %TRUE if @query_key exists in the URI query table.
2708  *
2709  * Since: 1.6
2710  */
2711 gboolean
2712 gst_uri_query_has_key (const GstUri * uri, const gchar * query_key)
2713 {
2714   if (!uri)
2715     return FALSE;
2716   g_return_val_if_fail (GST_IS_URI (uri), FALSE);
2717   if (!uri->query)
2718     return FALSE;
2719
2720   return g_hash_table_contains (uri->query, query_key);
2721 }
2722
2723 /**
2724  * gst_uri_get_query_value:
2725  * @uri: (nullable): The #GstUri to examine.
2726  * @query_key: The key to lookup.
2727  *
2728  * Get the value associated with the @query_key key. Will return %NULL if the
2729  * key has no value or if the key does not exist in the URI query table. Because
2730  * %NULL is returned for both missing keys and keys with no value, you should
2731  * use gst_uri_query_has_key() to determine if a key is present in the URI
2732  * query.
2733  *
2734  * Returns: The value for the given key, or %NULL if not found.
2735  *
2736  * Since: 1.6
2737  */
2738 const gchar *
2739 gst_uri_get_query_value (const GstUri * uri, const gchar * query_key)
2740 {
2741   if (!uri)
2742     return NULL;
2743   g_return_val_if_fail (GST_IS_URI (uri), NULL);
2744   if (!uri->query)
2745     return NULL;
2746
2747   return g_hash_table_lookup (uri->query, query_key);
2748 }
2749
2750 /**
2751  * gst_uri_get_query_keys:
2752  * @uri: (nullable): The #GstUri to examine.
2753  *
2754  * Get a list of the query keys from the URI.
2755  *
2756  * Returns: (transfer container)(element-type gchar*): A list of keys from
2757  *          the URI query. Free the list with g_list_free().
2758  *
2759  * Since: 1.6
2760  */
2761 GList *
2762 gst_uri_get_query_keys (const GstUri * uri)
2763 {
2764   if (!uri)
2765     return NULL;
2766   g_return_val_if_fail (GST_IS_URI (uri), NULL);
2767   if (!uri->query)
2768     return NULL;
2769
2770   return g_hash_table_get_keys (uri->query);
2771 }
2772
2773 /**
2774  * gst_uri_get_fragment:
2775  * @uri: (nullable): This #GstUri object.
2776  *
2777  * Get the fragment name from the URI or %NULL if it doesn't exist.
2778  * If @uri is %NULL then returns %NULL.
2779  *
2780  * Returns: The host name from the #GstUri object or %NULL.
2781  *
2782  * Since: 1.6
2783  */
2784 const gchar *
2785 gst_uri_get_fragment (const GstUri * uri)
2786 {
2787   g_return_val_if_fail (uri == NULL || GST_IS_URI (uri), NULL);
2788   return (uri ? uri->fragment : NULL);
2789 }
2790
2791 /**
2792  * gst_uri_set_fragment:
2793  * @uri: (transfer none)(nullable): The #GstUri to modify.
2794  * @fragment: (nullable): The fragment string to set.
2795  *
2796  * Sets the fragment string in the URI. Use a value of %NULL in @fragment to
2797  * unset the fragment string.
2798  *
2799  * Returns: %TRUE if the fragment was set/unset successfully.
2800  *
2801  * Since: 1.6
2802  */
2803 gboolean
2804 gst_uri_set_fragment (GstUri * uri, const gchar * fragment)
2805 {
2806   if (!uri)
2807     return fragment == NULL;
2808   g_return_val_if_fail (GST_IS_URI (uri) && gst_uri_is_writable (uri), FALSE);
2809
2810   g_free (uri->fragment);
2811   uri->fragment = g_strdup (fragment);
2812   return TRUE;
2813 }