Fix FSF address
[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  *
6  * gsturi.c: register URI handlers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:gsturihandler
26  * @short_description: Interface to ease URI handling in plugins.
27  *
28  * The URIHandler is an interface that is implemented by Source and Sink
29  * #GstElement to simplify then handling of URI.
30  *
31  * An application can use the following functions to quickly get an element
32  * that handles the given URI for reading or writing
33  * (gst_element_make_from_uri()).
34  *
35  * Source and Sink plugins should implement this interface when possible.
36  *
37  * Last reviewed on 2005-11-09 (0.9.4)
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
54 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
55 #define GST_CAT_DEFAULT gst_uri_handler_debug
56
57 GType
58 gst_uri_handler_get_type (void)
59 {
60   static volatile gsize urihandler_type = 0;
61
62   if (g_once_init_enter (&urihandler_type)) {
63     GType _type;
64     static const GTypeInfo urihandler_info = {
65       sizeof (GstURIHandlerInterface),
66       NULL,
67       NULL,
68       NULL,
69       NULL,
70       NULL,
71       0,
72       0,
73       NULL,
74       NULL
75     };
76
77     _type = g_type_register_static (G_TYPE_INTERFACE,
78         "GstURIHandler", &urihandler_info, 0);
79
80     GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
81         "handling of URIs");
82     g_once_init_leave (&urihandler_type, _type);
83   }
84   return urihandler_type;
85 }
86
87 GQuark
88 gst_uri_error_quark (void)
89 {
90   return g_quark_from_static_string ("gst-uri-error-quark");
91 }
92
93 static const guchar acceptable[96] = {  /* X0   X1   X2   X3   X4   X5   X6   X7   X8   X9   XA   XB   XC   XD   XE   XF */
94   0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C,       /* 2X  !"#$%&'()*+,-./   */
95   0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C,       /* 3X 0123456789:;<=>?   */
96   0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       /* 4X @ABCDEFGHIJKLMNO   */
97   0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F,       /* 5X PQRSTUVWXYZ[\]^_   */
98   0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       /* 6X `abcdefghijklmno   */
99   0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20        /* 7X pqrstuvwxyz{|}~DEL */
100 };
101
102 typedef enum
103 {
104   UNSAFE_ALL = 0x1,             /* Escape all unsafe characters   */
105   UNSAFE_ALLOW_PLUS = 0x2,      /* Allows '+'  */
106   UNSAFE_PATH = 0x4,            /* Allows '/' and '?' and '&' and '='  */
107   UNSAFE_DOS_PATH = 0x8,        /* Allows '/' and '?' and '&' and '=' and ':' */
108   UNSAFE_HOST = 0x10,           /* Allows '/' and ':' and '@' */
109   UNSAFE_SLASHES = 0x20         /* Allows all characters except for '/' and '%' */
110 } UnsafeCharacterSet;
111
112 #define HEX_ESCAPE '%'
113
114 /*  Escape undesirable characters using %
115  *  -------------------------------------
116  *
117  * This function takes a pointer to a string in which
118  * some characters may be unacceptable unescaped.
119  * It returns a string which has these characters
120  * represented by a '%' character followed by two hex digits.
121  *
122  * This routine returns a g_malloced string.
123  */
124
125 static const gchar hex[16] = "0123456789ABCDEF";
126
127 static gchar *
128 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
129 {
130 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
131
132   const gchar *p;
133   gchar *q;
134   gchar *result;
135   guchar c;
136   gint unacceptable;
137   UnsafeCharacterSet use_mask;
138
139   g_return_val_if_fail (mask == UNSAFE_ALL
140       || mask == UNSAFE_ALLOW_PLUS
141       || mask == UNSAFE_PATH
142       || mask == UNSAFE_DOS_PATH
143       || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
144
145   if (string == NULL) {
146     return NULL;
147   }
148
149   unacceptable = 0;
150   use_mask = mask;
151   for (p = string; *p != '\0'; p++) {
152     c = *p;
153     if (!ACCEPTABLE_CHAR (c)) {
154       unacceptable++;
155     }
156     if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
157       /* when escaping a host, if we hit something that needs to be escaped, or we finally
158        * hit a path separator, revert to path mode (the host segment of the url is over).
159        */
160       use_mask = UNSAFE_PATH;
161     }
162   }
163
164   result = g_malloc (p - string + unacceptable * 2 + 1);
165
166   use_mask = mask;
167   for (q = result, p = string; *p != '\0'; p++) {
168     c = *p;
169
170     if (!ACCEPTABLE_CHAR (c)) {
171       *q++ = HEX_ESCAPE;        /* means hex coming */
172       *q++ = hex[c >> 4];
173       *q++ = hex[c & 15];
174     } else {
175       *q++ = c;
176     }
177     if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
178       use_mask = UNSAFE_PATH;
179     }
180   }
181
182   *q = '\0';
183
184   return result;
185 }
186
187 /* escape_string:
188  * @string: string to be escaped
189  *
190  * Escapes @string, replacing any and all special characters
191  * with equivalent escape sequences.
192  *
193  * Return value: a newly allocated string equivalent to @string
194  * but with all special characters escaped
195  **/
196 static gchar *
197 escape_string (const gchar * string)
198 {
199   return escape_string_internal (string, UNSAFE_ALL);
200 }
201
202 static int
203 hex_to_int (gchar c)
204 {
205   return c >= '0' && c <= '9' ? c - '0'
206       : c >= 'A' && c <= 'F' ? c - 'A' + 10
207       : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
208 }
209
210 static int
211 unescape_character (const char *scanner)
212 {
213   int first_digit;
214   int second_digit;
215
216   first_digit = hex_to_int (*scanner++);
217   if (first_digit < 0) {
218     return -1;
219   }
220
221   second_digit = hex_to_int (*scanner);
222   if (second_digit < 0) {
223     return -1;
224   }
225
226   return (first_digit << 4) | second_digit;
227 }
228
229 /* unescape_string:
230  * @escaped_string: an escaped URI, path, or other string
231  * @illegal_characters: a string containing a sequence of characters
232  * considered "illegal", '\0' is automatically in this list.
233  *
234  * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
235  * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
236  * for character 16x+y.
237  *
238  * Return value: a newly allocated string with the unescaped equivalents,
239  * or %NULL if @escaped_string contained one of the characters
240  * in @illegal_characters.
241  **/
242 static char *
243 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
244 {
245   const gchar *in;
246   gchar *out, *result;
247   gint character;
248
249   if (escaped_string == NULL) {
250     return NULL;
251   }
252
253   result = g_malloc (strlen (escaped_string) + 1);
254
255   out = result;
256   for (in = escaped_string; *in != '\0'; in++) {
257     character = *in;
258     if (*in == HEX_ESCAPE) {
259       character = unescape_character (in + 1);
260
261       /* Check for an illegal character. We consider '\0' illegal here. */
262       if (character <= 0
263           || (illegal_characters != NULL
264               && strchr (illegal_characters, (char) character) != NULL)) {
265         g_free (result);
266         return NULL;
267       }
268       in += 2;
269     }
270     *out++ = (char) character;
271   }
272
273   *out = '\0';
274   g_assert ((gsize) (out - result) <= strlen (escaped_string));
275   return result;
276
277 }
278
279
280 static void
281 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
282 {
283   gchar *check = (gchar *) uri;
284
285   g_assert (uri != NULL);
286   g_assert (endptr != NULL);
287
288   if (g_ascii_isalpha (*check)) {
289     check++;
290     while (g_ascii_isalnum (*check) || *check == '+'
291         || *check == '-' || *check == '.')
292       check++;
293   }
294
295   *endptr = check;
296 }
297
298 /**
299  * gst_uri_protocol_is_valid:
300  * @protocol: A string
301  *
302  * Tests if the given string is a valid protocol identifier. Protocols
303  * must consist of alphanumeric characters, '+', '-' and '.' and must
304  * start with a alphabetic character. See RFC 3986 Section 3.1.
305  *
306  * Returns: TRUE if the string is a valid protocol identifier, FALSE otherwise.
307  */
308 gboolean
309 gst_uri_protocol_is_valid (const gchar * protocol)
310 {
311   gchar *endptr;
312
313   g_return_val_if_fail (protocol != NULL, FALSE);
314
315   gst_uri_protocol_check_internal (protocol, &endptr);
316
317   return *endptr == '\0' && ((gsize) (endptr - protocol)) >= 2;
318 }
319
320 /**
321  * gst_uri_is_valid:
322  * @uri: A URI string
323  *
324  * Tests if the given string is a valid URI identifier. URIs start with a valid
325  * scheme followed by ":" and maybe a string identifying the location.
326  *
327  * Returns: TRUE if the string is a valid URI
328  */
329 gboolean
330 gst_uri_is_valid (const gchar * uri)
331 {
332   gchar *endptr;
333
334   g_return_val_if_fail (uri != NULL, FALSE);
335
336   gst_uri_protocol_check_internal (uri, &endptr);
337
338   return *endptr == ':' && ((gsize) (endptr - uri)) >= 2;
339 }
340
341 /**
342  * gst_uri_get_protocol:
343  * @uri: A URI string
344  *
345  * Extracts the protocol out of a given valid URI. The returned string must be
346  * freed using g_free().
347  *
348  * Returns: The protocol for this URI.
349  */
350 gchar *
351 gst_uri_get_protocol (const gchar * uri)
352 {
353   gchar *colon;
354
355   g_return_val_if_fail (uri != NULL, NULL);
356   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
357
358   colon = strstr (uri, ":");
359
360   return g_ascii_strdown (uri, colon - uri);
361 }
362
363 /**
364  * gst_uri_has_protocol:
365  * @uri: a URI string
366  * @protocol: a protocol string (e.g. "http")
367  *
368  * Checks if the protocol of a given valid URI matches @protocol.
369  *
370  * Returns: %TRUE if the protocol matches.
371  */
372 gboolean
373 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
374 {
375   gchar *colon;
376
377   g_return_val_if_fail (uri != NULL, FALSE);
378   g_return_val_if_fail (protocol != NULL, FALSE);
379   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
380
381   colon = strstr (uri, ":");
382
383   if (colon == NULL)
384     return FALSE;
385
386   return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
387 }
388
389 /**
390  * gst_uri_get_location:
391  * @uri: A URI string
392  *
393  * Extracts the location out of a given valid URI, ie. the protocol and "://"
394  * are stripped from the URI, which means that the location returned includes
395  * the hostname if one is specified. The returned string must be freed using
396  * g_free().
397  *
398  * Free-function: g_free
399  *
400  * Returns: (transfer full): the location for this URI. Returns NULL if the
401  *     URI isn't valid. If the URI does not contain a location, an empty
402  *     string is returned.
403  */
404 gchar *
405 gst_uri_get_location (const gchar * uri)
406 {
407   const gchar *colon;
408   gchar *unescaped = NULL;
409
410   g_return_val_if_fail (uri != NULL, NULL);
411   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
412
413   colon = strstr (uri, "://");
414   if (!colon)
415     return NULL;
416
417   unescaped = unescape_string (colon + 3, "/");
418
419   /* On Windows an URI might look like file:///c:/foo/bar.txt or
420    * file:///c|/foo/bar.txt (some Netscape versions) and we want to
421    * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
422    * Can't use g_filename_from_uri() here because it will only handle the
423    * file:// protocol */
424 #ifdef G_OS_WIN32
425   if (unescaped != NULL && unescaped[0] == '/' &&
426       g_ascii_isalpha (unescaped[1]) &&
427       (unescaped[2] == ':' || unescaped[2] == '|')) {
428     unescaped[2] = ':';
429     g_memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
430   }
431 #endif
432
433   GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
434       uri);
435   return unescaped;
436 }
437
438 /**
439  * gst_uri_construct:
440  * @protocol: Protocol for URI
441  * @location: (transfer none): Location for URI
442  *
443  * Constructs a URI for a given valid protocol and location.
444  *
445  * Free-function: g_free
446  *
447  * Returns: (transfer full): a new string for this URI. Returns NULL if the
448  *     given URI protocol is not valid, or the given location is NULL.
449  */
450 gchar *
451 gst_uri_construct (const gchar * protocol, const gchar * location)
452 {
453   char *escaped, *proto_lowercase;
454   char *retval;
455
456   g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
457   g_return_val_if_fail (location != NULL, NULL);
458
459   proto_lowercase = g_ascii_strdown (protocol, -1);
460   escaped = escape_string (location);
461   retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
462   g_free (escaped);
463   g_free (proto_lowercase);
464
465   return retval;
466 }
467
468 typedef struct
469 {
470   GstURIType type;
471   const gchar *protocol;
472 }
473 SearchEntry;
474
475 static gboolean
476 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
477 {
478   const gchar *const *protocols;
479   GstElementFactory *factory;
480   SearchEntry *entry = (SearchEntry *) search_entry;
481
482   if (!GST_IS_ELEMENT_FACTORY (feature))
483     return FALSE;
484   factory = GST_ELEMENT_FACTORY_CAST (feature);
485
486   if (factory->uri_type != entry->type)
487     return FALSE;
488
489   protocols = gst_element_factory_get_uri_protocols (factory);
490
491   if (protocols == NULL) {
492     g_warning ("Factory '%s' implements GstUriHandler interface but returned "
493         "no supported protocols!", gst_plugin_feature_get_name (feature));
494     return FALSE;
495   }
496
497   while (*protocols != NULL) {
498     if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
499       return TRUE;
500     protocols++;
501   }
502   return FALSE;
503 }
504
505 static gint
506 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
507 {
508   return gst_plugin_feature_get_rank (second) -
509       gst_plugin_feature_get_rank (first);
510 }
511
512 static GList *
513 get_element_factories_from_uri_protocol (const GstURIType type,
514     const gchar * protocol)
515 {
516   GList *possibilities;
517   SearchEntry entry;
518
519   g_return_val_if_fail (protocol, NULL);
520
521   entry.type = type;
522   entry.protocol = protocol;
523   possibilities = gst_registry_feature_filter (gst_registry_get (),
524       search_by_entry, FALSE, &entry);
525
526   return possibilities;
527 }
528
529 /**
530  * gst_uri_protocol_is_supported:
531  * @type: Whether to check for a source or a sink
532  * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
533  *
534  * Checks if an element exists that supports the given URI protocol. Note
535  * that a positive return value does not imply that a subsequent call to
536  * gst_element_make_from_uri() is guaranteed to work.
537  *
538  * Returns: TRUE
539 */
540 gboolean
541 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
542 {
543   GList *possibilities;
544
545   g_return_val_if_fail (protocol, FALSE);
546
547   possibilities = get_element_factories_from_uri_protocol (type, protocol);
548
549   if (possibilities) {
550     g_list_free (possibilities);
551     return TRUE;
552   } else
553     return FALSE;
554 }
555
556 /**
557  * gst_element_make_from_uri:
558  * @type: Whether to create a source or a sink
559  * @uri: URI to create an element for
560  * @elementname: (allow-none): Name of created element, can be NULL.
561  * @error: (allow-none): address where to store error information, or NULL.
562  *
563  * Creates an element for handling the given URI.
564  *
565  * Returns: (transfer floating): a new element or NULL if none could be created
566  */
567 GstElement *
568 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
569     const gchar * elementname, GError ** error)
570 {
571   GList *possibilities, *walk;
572   gchar *protocol;
573   GstElement *ret = NULL;
574
575   g_return_val_if_fail (gst_is_initialized (), NULL);
576   g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
577   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
578   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
579
580   GST_DEBUG ("type:%d, uri:%s, elementname:%s", type, uri, elementname);
581
582   protocol = gst_uri_get_protocol (uri);
583   possibilities = get_element_factories_from_uri_protocol (type, protocol);
584
585   if (!possibilities) {
586     GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
587         uri);
588     /* The error message isn't great, but we don't expect applications to
589      * show that error to users, but call the missing plugins functions */
590     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
591         _("No URI handler for the %s protocol found"), protocol);
592     g_free (protocol);
593     return NULL;
594   }
595   g_free (protocol);
596
597   possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
598   walk = possibilities;
599   while (walk) {
600     GstElementFactory *factory = walk->data;
601     GError *uri_err = NULL;
602
603     ret = gst_element_factory_create (factory, elementname);
604     if (ret != NULL) {
605       GstURIHandler *handler = GST_URI_HANDLER (ret);
606
607       if (gst_uri_handler_set_uri (handler, uri, &uri_err))
608         break;
609
610       GST_WARNING ("%s didn't accept URI '%s': %s", GST_OBJECT_NAME (ret), uri,
611           uri_err->message);
612
613       if (error != NULL && *error == NULL)
614         g_propagate_error (error, uri_err);
615       else
616         g_error_free (uri_err);
617
618       gst_object_unref (ret);
619       ret = NULL;
620     }
621     walk = walk->next;
622   }
623   gst_plugin_feature_list_free (possibilities);
624
625   GST_LOG_OBJECT (ret, "created %s for URL '%s'",
626       type == GST_URI_SINK ? "sink" : "source", uri);
627
628   /* if the first handler didn't work, but we found another one that works */
629   if (ret != NULL)
630     g_clear_error (error);
631
632   return ret;
633 }
634
635 /**
636  * gst_uri_handler_get_uri_type:
637  * @handler: A #GstURIHandler.
638  *
639  * Gets the type of the given URI handler
640  *
641  * Returns: the #GstURIType of the URI handler.
642  * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
643  */
644 guint
645 gst_uri_handler_get_uri_type (GstURIHandler * handler)
646 {
647   GstURIHandlerInterface *iface;
648   guint ret;
649
650   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
651
652   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
653   g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
654   g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
655
656   ret = iface->get_type (G_OBJECT_TYPE (handler));
657   g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
658
659   return ret;
660 }
661
662 /**
663  * gst_uri_handler_get_protocols:
664  * @handler: A #GstURIHandler.
665  *
666  * Gets the list of protocols supported by @handler. This list may not be
667  * modified.
668  *
669  * Returns: (transfer none) (element-type utf8): the supported protocols.
670  *     Returns NULL if the @handler isn't implemented properly, or the @handler
671  *     doesn't support any protocols.
672  */
673 const gchar *const *
674 gst_uri_handler_get_protocols (GstURIHandler * handler)
675 {
676   GstURIHandlerInterface *iface;
677   const gchar *const *ret;
678
679   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
680
681   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
682   g_return_val_if_fail (iface != NULL, NULL);
683   g_return_val_if_fail (iface->get_protocols != NULL, NULL);
684
685   ret = iface->get_protocols (G_OBJECT_TYPE (handler));
686   g_return_val_if_fail (ret != NULL, NULL);
687
688   return ret;
689 }
690
691 /**
692  * gst_uri_handler_get_uri:
693  * @handler: A #GstURIHandler
694  *
695  * Gets the currently handled URI.
696  *
697  * Returns: (transfer full): the URI currently handled by the @handler.
698  *   Returns NULL if there are no URI currently handled. The
699  *   returned string must be freed with g_free() when no longer needed.
700  */
701 gchar *
702 gst_uri_handler_get_uri (GstURIHandler * handler)
703 {
704   GstURIHandlerInterface *iface;
705   gchar *ret;
706
707   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
708
709   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
710   g_return_val_if_fail (iface != NULL, NULL);
711   g_return_val_if_fail (iface->get_uri != NULL, NULL);
712   ret = iface->get_uri (handler);
713   if (ret != NULL)
714     g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
715
716   return ret;
717 }
718
719 /**
720  * gst_uri_handler_set_uri:
721  * @handler: A #GstURIHandler
722  * @uri: URI to set
723  * @error: (allow-none): address where to store a #GError in case of
724  *    an error, or NULL
725  *
726  * Tries to set the URI of the given handler.
727  *
728  * Returns: TRUE if the URI was set successfully, else FALSE.
729  */
730 gboolean
731 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
732     GError ** error)
733 {
734   GstURIHandlerInterface *iface;
735   gboolean ret;
736   gchar *new_uri, *protocol, *location, *colon;
737
738   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
739   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
740   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
741
742   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
743   g_return_val_if_fail (iface != NULL, FALSE);
744   g_return_val_if_fail (iface->set_uri != NULL, FALSE);
745
746   protocol = gst_uri_get_protocol (uri);
747
748   if (iface->get_protocols) {
749     const gchar *const *protocols;
750     const gchar *const *p;
751     gboolean found_protocol = FALSE;
752
753     protocols = iface->get_protocols (G_OBJECT_TYPE (handler));
754     if (protocols != NULL) {
755       for (p = protocols; *p != NULL; ++p) {
756         if (g_ascii_strcasecmp (protocol, *p) == 0) {
757           found_protocol = TRUE;
758           break;
759         }
760       }
761
762       if (!found_protocol) {
763         g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_UNSUPPORTED_PROTOCOL,
764             _("URI scheme '%s' not supported"), protocol);
765         g_free (protocol);
766         return FALSE;
767       }
768     }
769   }
770
771   colon = strstr (uri, ":");
772   location = g_strdup (colon);
773
774   new_uri = g_strdup_printf ("%s%s", protocol, location);
775
776   ret = iface->set_uri (handler, uri, error);
777
778   g_free (new_uri);
779   g_free (location);
780   g_free (protocol);
781
782   return ret;
783 }
784
785 static gchar *
786 gst_file_utils_canonicalise_path (const gchar * path)
787 {
788   gchar **parts, **p, *clean_path;
789
790 #ifdef G_OS_WIN32
791   {
792     GST_WARNING ("FIXME: canonicalise win32 path");
793     return g_strdup (path);
794   }
795 #endif
796
797   parts = g_strsplit (path, "/", -1);
798
799   p = parts;
800   while (*p != NULL) {
801     if (strcmp (*p, ".") == 0) {
802       /* just move all following parts on top of this, incl. NUL terminator */
803       g_free (*p);
804       g_memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
805       /* re-check the new current part again in the next iteration */
806       continue;
807     } else if (strcmp (*p, "..") == 0 && p > parts) {
808       /* just move all following parts on top of the previous part, incl.
809        * NUL terminator */
810       g_free (*(p - 1));
811       g_free (*p);
812       g_memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
813       /* re-check the new current part again in the next iteration */
814       --p;
815       continue;
816     }
817     ++p;
818   }
819   if (*path == '/') {
820     guint num_parts;
821
822     num_parts = g_strv_length (parts) + 1;      /* incl. terminator */
823     parts = g_renew (gchar *, parts, num_parts + 1);
824     g_memmove (parts + 1, parts, num_parts * sizeof (gchar *));
825     parts[0] = g_strdup ("/");
826   }
827
828   clean_path = g_build_filenamev (parts);
829   g_strfreev (parts);
830   return clean_path;
831 }
832
833 static gboolean
834 file_path_contains_relatives (const gchar * path)
835 {
836   return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
837       strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
838       strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
839 }
840
841 /**
842  * gst_filename_to_uri:
843  * @filename: absolute or relative file name path
844  * @error: pointer to error, or NULL
845  *
846  * Similar to g_filename_to_uri(), but attempts to handle relative file paths
847  * as well. Before converting @filename into an URI, it will be prefixed by
848  * the current working directory if it is a relative path, and then the path
849  * will be canonicalised so that it doesn't contain any './' or '../' segments.
850  *
851  * On Windows #filename should be in UTF-8 encoding.
852  */
853 gchar *
854 gst_filename_to_uri (const gchar * filename, GError ** error)
855 {
856   gchar *abs_location = NULL;
857   gchar *uri, *abs_clean;
858
859   g_return_val_if_fail (filename != NULL, NULL);
860   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
861
862   if (g_path_is_absolute (filename)) {
863     if (!file_path_contains_relatives (filename)) {
864       uri = g_filename_to_uri (filename, NULL, error);
865       goto beach;
866     }
867
868     abs_location = g_strdup (filename);
869   } else {
870     gchar *cwd;
871
872     cwd = g_get_current_dir ();
873     abs_location = g_build_filename (cwd, filename, NULL);
874     g_free (cwd);
875
876     if (!file_path_contains_relatives (abs_location)) {
877       uri = g_filename_to_uri (abs_location, NULL, error);
878       goto beach;
879     }
880   }
881
882   /* path is now absolute, but contains '.' or '..' */
883   abs_clean = gst_file_utils_canonicalise_path (abs_location);
884   GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
885   uri = g_filename_to_uri (abs_clean, NULL, error);
886   g_free (abs_clean);
887
888 beach:
889
890   g_free (abs_location);
891   GST_DEBUG ("'%s' -> '%s'", filename, uri);
892   return uri;
893 }