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>
6 * gsturi.c: register URI handlers
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.
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.
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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:gsturihandler
26 * @short_description: Interface to ease URI handling in plugins.
28 * The URIHandler is an interface that is implemented by Source and Sink
29 * #GstElement to simplify then handling of URI.
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()).
35 * Source and Sink plugins should implement this interface when possible.
37 * Last reviewed on 2005-11-09 (0.9.4)
44 #include "gst_private.h"
47 #include "gstmarshal.h"
48 #include "gstregistry.h"
52 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
53 #define GST_CAT_DEFAULT gst_uri_handler_debug
61 static guint gst_uri_handler_signals[LAST_SIGNAL] = { 0 };
63 static void gst_uri_handler_base_init (gpointer g_class);
66 gst_uri_handler_get_type (void)
68 static volatile gsize urihandler_type = 0;
70 if (g_once_init_enter (&urihandler_type)) {
72 static const GTypeInfo urihandler_info = {
73 sizeof (GstURIHandlerInterface),
74 gst_uri_handler_base_init,
85 _type = g_type_register_static (G_TYPE_INTERFACE,
86 "GstURIHandler", &urihandler_info, 0);
88 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
90 g_once_init_leave (&urihandler_type, _type);
92 return urihandler_type;
96 gst_uri_handler_base_init (gpointer g_class)
98 static gboolean initialized = FALSE;
100 if (G_UNLIKELY (!initialized)) {
103 * GstURIHandler::new-uri:
104 * @handler: The #GstURIHandler which emitted the signal
105 * @uri: (transfer none): The new URI, or NULL if the URI was removed
107 * The URI of the given @handler has changed.
110 gst_uri_handler_signals[NEW_URI] =
111 g_signal_new ("new-uri", GST_TYPE_URI_HANDLER, G_SIGNAL_RUN_LAST,
112 G_STRUCT_OFFSET (GstURIHandlerInterface, new_uri), NULL, NULL,
113 gst_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
118 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
119 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
120 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
121 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
122 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
123 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
124 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
129 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
130 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
131 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
132 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
133 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
134 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
135 } UnsafeCharacterSet;
137 #define HEX_ESCAPE '%'
139 /* Escape undesirable characters using %
140 * -------------------------------------
142 * This function takes a pointer to a string in which
143 * some characters may be unacceptable unescaped.
144 * It returns a string which has these characters
145 * represented by a '%' character followed by two hex digits.
147 * This routine returns a g_malloced string.
150 static const gchar hex[16] = "0123456789ABCDEF";
153 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
155 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
162 UnsafeCharacterSet use_mask;
164 g_return_val_if_fail (mask == UNSAFE_ALL
165 || mask == UNSAFE_ALLOW_PLUS
166 || mask == UNSAFE_PATH
167 || mask == UNSAFE_DOS_PATH
168 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
170 if (string == NULL) {
176 for (p = string; *p != '\0'; p++) {
178 if (!ACCEPTABLE_CHAR (c)) {
181 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
182 /* when escaping a host, if we hit something that needs to be escaped, or we finally
183 * hit a path separator, revert to path mode (the host segment of the url is over).
185 use_mask = UNSAFE_PATH;
189 result = g_malloc (p - string + unacceptable * 2 + 1);
192 for (q = result, p = string; *p != '\0'; p++) {
195 if (!ACCEPTABLE_CHAR (c)) {
196 *q++ = HEX_ESCAPE; /* means hex coming */
202 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
203 use_mask = UNSAFE_PATH;
213 * @string: string to be escaped
215 * Escapes @string, replacing any and all special characters
216 * with equivalent escape sequences.
218 * Return value: a newly allocated string equivalent to @string
219 * but with all special characters escaped
222 escape_string (const gchar * string)
224 return escape_string_internal (string, UNSAFE_ALL);
230 return c >= '0' && c <= '9' ? c - '0'
231 : c >= 'A' && c <= 'F' ? c - 'A' + 10
232 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
236 unescape_character (const char *scanner)
241 first_digit = hex_to_int (*scanner++);
242 if (first_digit < 0) {
246 second_digit = hex_to_int (*scanner);
247 if (second_digit < 0) {
251 return (first_digit << 4) | second_digit;
255 * @escaped_string: an escaped URI, path, or other string
256 * @illegal_characters: a string containing a sequence of characters
257 * considered "illegal", '\0' is automatically in this list.
259 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
260 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
261 * for character 16x+y.
263 * Return value: a newly allocated string with the unescaped equivalents,
264 * or %NULL if @escaped_string contained one of the characters
265 * in @illegal_characters.
268 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
274 if (escaped_string == NULL) {
278 result = g_malloc (strlen (escaped_string) + 1);
281 for (in = escaped_string; *in != '\0'; in++) {
283 if (*in == HEX_ESCAPE) {
284 character = unescape_character (in + 1);
286 /* Check for an illegal character. We consider '\0' illegal here. */
288 || (illegal_characters != NULL
289 && strchr (illegal_characters, (char) character) != NULL)) {
295 *out++ = (char) character;
299 g_assert ((gsize) (out - result) <= strlen (escaped_string));
306 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
308 gchar *check = (gchar *) uri;
310 g_assert (uri != NULL);
311 g_assert (endptr != NULL);
313 if (g_ascii_isalpha (*check)) {
315 while (g_ascii_isalnum (*check) || *check == '+'
316 || *check == '-' || *check == '.')
324 * gst_uri_protocol_is_valid:
325 * @protocol: A string
327 * Tests if the given string is a valid protocol identifier. Protocols
328 * must consist of alphanumeric characters, '+', '-' and '.' and must
329 * start with a alphabetic character. See RFC 3986 Section 3.1.
331 * Returns: TRUE if the string is a valid protocol identifier, FALSE otherwise.
334 gst_uri_protocol_is_valid (const gchar * protocol)
338 g_return_val_if_fail (protocol != NULL, FALSE);
340 gst_uri_protocol_check_internal (protocol, &endptr);
342 return *endptr == '\0' && endptr != protocol;
349 * Tests if the given string is a valid URI identifier. URIs start with a valid
350 * scheme followed by ":" and maybe a string identifying the location.
352 * Returns: TRUE if the string is a valid URI
355 gst_uri_is_valid (const gchar * uri)
359 g_return_val_if_fail (uri != NULL, FALSE);
361 gst_uri_protocol_check_internal (uri, &endptr);
363 return *endptr == ':';
367 * gst_uri_get_protocol:
370 * Extracts the protocol out of a given valid URI. The returned string must be
371 * freed using g_free().
373 * Returns: The protocol for this URI.
376 gst_uri_get_protocol (const gchar * uri)
380 g_return_val_if_fail (uri != NULL, NULL);
381 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
383 colon = strstr (uri, ":");
385 return g_ascii_strdown (uri, colon - uri);
389 * gst_uri_has_protocol:
391 * @protocol: a protocol string (e.g. "http")
393 * Checks if the protocol of a given valid URI matches @protocol.
395 * Returns: %TRUE if the protocol matches.
400 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
404 g_return_val_if_fail (uri != NULL, FALSE);
405 g_return_val_if_fail (protocol != NULL, FALSE);
406 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
408 colon = strstr (uri, ":");
413 return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
417 * gst_uri_get_location:
420 * Extracts the location out of a given valid URI, ie. the protocol and "://"
421 * are stripped from the URI, which means that the location returned includes
422 * the hostname if one is specified. The returned string must be freed using
425 * Free-function: g_free
427 * Returns: (transfer full) (array zero-terminated=1): the location for this
428 * URI. Returns NULL if the URI isn't valid. If the URI does not contain
429 * a location, an empty string is returned.
432 gst_uri_get_location (const gchar * uri)
435 gchar *unescaped = NULL;
437 g_return_val_if_fail (uri != NULL, NULL);
438 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
440 colon = strstr (uri, "://");
444 unescaped = unescape_string (colon + 3, "/");
446 /* On Windows an URI might look like file:///c:/foo/bar.txt or
447 * file:///c|/foo/bar.txt (some Netscape versions) and we want to
448 * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
449 * Can't use g_filename_from_uri() here because it will only handle the
450 * file:// protocol */
452 if (unescaped != NULL && unescaped[0] == '/' &&
453 g_ascii_isalpha (unescaped[1]) &&
454 (unescaped[2] == ':' || unescaped[2] == '|')) {
456 g_memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
460 GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
467 * @protocol: Protocol for URI
468 * @location: (array zero-terminated=1) (transfer none): Location for URI
470 * Constructs a URI for a given valid protocol and location.
472 * Free-function: g_free
474 * Returns: (transfer full) (array zero-terminated=1): a new string for this
475 * URI. Returns NULL if the given URI protocol is not valid, or the given
479 gst_uri_construct (const gchar * protocol, const gchar * location)
481 char *escaped, *proto_lowercase;
484 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
485 g_return_val_if_fail (location != NULL, NULL);
487 proto_lowercase = g_ascii_strdown (protocol, -1);
488 escaped = escape_string (location);
489 retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
491 g_free (proto_lowercase);
499 const gchar *protocol;
504 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
507 GstElementFactory *factory;
508 SearchEntry *entry = (SearchEntry *) search_entry;
510 if (!GST_IS_ELEMENT_FACTORY (feature))
512 factory = GST_ELEMENT_FACTORY_CAST (feature);
514 if (factory->uri_type != entry->type)
517 protocols = gst_element_factory_get_uri_protocols (factory);
519 if (protocols == NULL) {
520 g_warning ("Factory '%s' implements GstUriHandler interface but returned "
521 "no supported protocols!", gst_plugin_feature_get_name (feature));
525 while (*protocols != NULL) {
526 if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
534 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
536 return gst_plugin_feature_get_rank (second) -
537 gst_plugin_feature_get_rank (first);
541 get_element_factories_from_uri_protocol (const GstURIType type,
542 const gchar * protocol)
544 GList *possibilities;
547 g_return_val_if_fail (protocol, NULL);
550 entry.protocol = protocol;
551 possibilities = gst_registry_feature_filter (gst_registry_get_default (),
552 search_by_entry, FALSE, &entry);
554 return possibilities;
558 * gst_uri_protocol_is_supported:
559 * @type: Whether to check for a source or a sink
560 * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
562 * Checks if an element exists that supports the given URI protocol. Note
563 * that a positive return value does not imply that a subsequent call to
564 * gst_element_make_from_uri() is guaranteed to work.
571 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
573 GList *possibilities;
575 g_return_val_if_fail (protocol, FALSE);
577 possibilities = get_element_factories_from_uri_protocol (type, protocol);
580 g_list_free (possibilities);
587 * gst_element_make_from_uri:
588 * @type: Whether to create a source or a sink
589 * @uri: URI to create an element for
590 * @elementname: (allow-none): Name of created element, can be NULL.
592 * Creates an element for handling the given URI.
594 * Returns: (transfer full): a new element or NULL if none could be created
597 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
598 const gchar * elementname)
600 GList *possibilities, *walk;
602 GstElement *ret = NULL;
604 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
605 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
607 protocol = gst_uri_get_protocol (uri);
608 possibilities = get_element_factories_from_uri_protocol (type, protocol);
611 if (!possibilities) {
612 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
617 possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
618 walk = possibilities;
621 gst_element_factory_create (GST_ELEMENT_FACTORY_CAST (walk->data),
622 elementname)) != NULL) {
623 GstURIHandler *handler = GST_URI_HANDLER (ret);
625 if (gst_uri_handler_set_uri (handler, uri))
627 gst_object_unref (ret);
632 gst_plugin_feature_list_free (possibilities);
634 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
635 type == GST_URI_SINK ? "sink" : "source", uri);
640 * gst_uri_handler_get_uri_type:
641 * @handler: A #GstURIHandler.
643 * Gets the type of the given URI handler
645 * Returns: the #GstURIType of the URI handler.
646 * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
649 gst_uri_handler_get_uri_type (GstURIHandler * handler)
651 GstURIHandlerInterface *iface;
654 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
656 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
657 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
658 g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
660 ret = iface->get_type (G_OBJECT_TYPE (handler));
661 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
667 * gst_uri_handler_get_protocols:
668 * @handler: A #GstURIHandler.
670 * Gets the list of protocols supported by @handler. This list may not be
673 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): the
674 * supported protocols. Returns NULL if the @handler isn't implemented
675 * properly, or the @handler doesn't support any protocols.
678 gst_uri_handler_get_protocols (GstURIHandler * handler)
680 GstURIHandlerInterface *iface;
683 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
685 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
686 g_return_val_if_fail (iface != NULL, NULL);
687 g_return_val_if_fail (iface->get_protocols != NULL, NULL);
689 ret = iface->get_protocols (G_OBJECT_TYPE (handler));
690 g_return_val_if_fail (ret != NULL, NULL);
696 * gst_uri_handler_get_uri:
697 * @handler: A #GstURIHandler
699 * Gets the currently handled URI.
701 * Returns: (transfer none): the URI currently handled by the @handler.
702 * Returns NULL if there are no URI currently handled. The
703 * returned string must not be modified or freed.
706 gst_uri_handler_get_uri (GstURIHandler * handler)
708 GstURIHandlerInterface *iface;
711 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
713 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
714 g_return_val_if_fail (iface != NULL, NULL);
715 g_return_val_if_fail (iface->get_uri != NULL, NULL);
716 ret = iface->get_uri (handler);
718 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
724 * gst_uri_handler_set_uri:
725 * @handler: A #GstURIHandler
728 * Tries to set the URI of the given handler.
730 * Returns: TRUE if the URI was set successfully, else FALSE.
733 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
735 GstURIHandlerInterface *iface;
737 gchar *new_uri, *protocol, *location, *colon;
739 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
740 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
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);
746 protocol = gst_uri_get_protocol (uri);
748 colon = strstr (uri, ":");
749 location = g_strdup (colon);
751 new_uri = g_strdup_printf ("%s%s", protocol, location);
753 ret = iface->set_uri (handler, uri);
763 * gst_uri_handler_new_uri:
764 * @handler: A #GstURIHandler
765 * @uri: new URI or NULL if it was unset
767 * Emits the new-uri signal for a given handler, when that handler has a new URI.
768 * This function should only be called by URI handlers themselves.
771 gst_uri_handler_new_uri (GstURIHandler * handler, const gchar * uri)
773 g_return_if_fail (GST_IS_URI_HANDLER (handler));
775 g_signal_emit (handler, gst_uri_handler_signals[NEW_URI], 0, uri);
779 gst_file_utils_canonicalise_path (const gchar * path)
781 gchar **parts, **p, *clean_path;
785 GST_WARNING ("FIXME: canonicalise win32 path");
786 return g_strdup (path);
790 parts = g_strsplit (path, "/", -1);
794 if (strcmp (*p, ".") == 0) {
795 /* just move all following parts on top of this, incl. NUL terminator */
797 g_memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
798 /* re-check the new current part again in the next iteration */
800 } else if (strcmp (*p, "..") == 0 && p > parts) {
801 /* just move all following parts on top of the previous part, incl.
805 g_memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
806 /* re-check the new current part again in the next iteration */
815 num_parts = g_strv_length (parts) + 1; /* incl. terminator */
816 parts = g_renew (gchar *, parts, num_parts + 1);
817 g_memmove (parts + 1, parts, num_parts * sizeof (gchar *));
818 parts[0] = g_strdup ("/");
821 clean_path = g_build_filenamev (parts);
827 file_path_contains_relatives (const gchar * path)
829 return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
830 strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
831 strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
835 * gst_filename_to_uri:
836 * @filename: absolute or relative file name path
837 * @error: pointer to error, or NULL
839 * Similar to g_filename_to_uri(), but attempts to handle relative file paths
840 * as well. Before converting @filename into an URI, it will be prefixed by
841 * the current working directory if it is a relative path, and then the path
842 * will be canonicalised so that it doesn't contain any './' or '../' segments.
844 * On Windows #filename should be in UTF-8 encoding.
849 gst_filename_to_uri (const gchar * filename, GError ** error)
851 gchar *abs_location = NULL;
852 gchar *uri, *abs_clean;
854 g_return_val_if_fail (filename != NULL, NULL);
855 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
857 if (g_path_is_absolute (filename)) {
858 if (!file_path_contains_relatives (filename)) {
859 uri = g_filename_to_uri (filename, NULL, error);
863 abs_location = g_strdup (filename);
867 cwd = g_get_current_dir ();
868 abs_location = g_build_filename (cwd, filename, NULL);
871 if (!file_path_contains_relatives (abs_location)) {
872 uri = g_filename_to_uri (abs_location, NULL, error);
877 /* path is now absolute, but contains '.' or '..' */
878 abs_clean = gst_file_utils_canonicalise_path (abs_location);
879 GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
880 uri = g_filename_to_uri (abs_clean, NULL, error);
885 g_free (abs_location);
886 GST_DEBUG ("'%s' -> '%s'", filename, uri);