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
56 gst_uri_handler_get_type (void)
58 static volatile gsize urihandler_type = 0;
60 if (g_once_init_enter (&urihandler_type)) {
62 static const GTypeInfo urihandler_info = {
63 sizeof (GstURIHandlerInterface),
75 _type = g_type_register_static (G_TYPE_INTERFACE,
76 "GstURIHandler", &urihandler_info, 0);
78 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
80 g_once_init_leave (&urihandler_type, _type);
82 return urihandler_type;
85 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
86 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
87 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
88 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
89 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
90 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
91 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
96 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
97 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
98 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
99 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
100 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
101 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
102 } UnsafeCharacterSet;
104 #define HEX_ESCAPE '%'
106 /* Escape undesirable characters using %
107 * -------------------------------------
109 * This function takes a pointer to a string in which
110 * some characters may be unacceptable unescaped.
111 * It returns a string which has these characters
112 * represented by a '%' character followed by two hex digits.
114 * This routine returns a g_malloced string.
117 static const gchar hex[16] = "0123456789ABCDEF";
120 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
122 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
129 UnsafeCharacterSet use_mask;
131 g_return_val_if_fail (mask == UNSAFE_ALL
132 || mask == UNSAFE_ALLOW_PLUS
133 || mask == UNSAFE_PATH
134 || mask == UNSAFE_DOS_PATH
135 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
137 if (string == NULL) {
143 for (p = string; *p != '\0'; p++) {
145 if (!ACCEPTABLE_CHAR (c)) {
148 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
149 /* when escaping a host, if we hit something that needs to be escaped, or we finally
150 * hit a path separator, revert to path mode (the host segment of the url is over).
152 use_mask = UNSAFE_PATH;
156 result = g_malloc (p - string + unacceptable * 2 + 1);
159 for (q = result, p = string; *p != '\0'; p++) {
162 if (!ACCEPTABLE_CHAR (c)) {
163 *q++ = HEX_ESCAPE; /* means hex coming */
169 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
170 use_mask = UNSAFE_PATH;
180 * @string: string to be escaped
182 * Escapes @string, replacing any and all special characters
183 * with equivalent escape sequences.
185 * Return value: a newly allocated string equivalent to @string
186 * but with all special characters escaped
189 escape_string (const gchar * string)
191 return escape_string_internal (string, UNSAFE_ALL);
197 return c >= '0' && c <= '9' ? c - '0'
198 : c >= 'A' && c <= 'F' ? c - 'A' + 10
199 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
203 unescape_character (const char *scanner)
208 first_digit = hex_to_int (*scanner++);
209 if (first_digit < 0) {
213 second_digit = hex_to_int (*scanner);
214 if (second_digit < 0) {
218 return (first_digit << 4) | second_digit;
222 * @escaped_string: an escaped URI, path, or other string
223 * @illegal_characters: a string containing a sequence of characters
224 * considered "illegal", '\0' is automatically in this list.
226 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
227 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
228 * for character 16x+y.
230 * Return value: a newly allocated string with the unescaped equivalents,
231 * or %NULL if @escaped_string contained one of the characters
232 * in @illegal_characters.
235 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
241 if (escaped_string == NULL) {
245 result = g_malloc (strlen (escaped_string) + 1);
248 for (in = escaped_string; *in != '\0'; in++) {
250 if (*in == HEX_ESCAPE) {
251 character = unescape_character (in + 1);
253 /* Check for an illegal character. We consider '\0' illegal here. */
255 || (illegal_characters != NULL
256 && strchr (illegal_characters, (char) character) != NULL)) {
262 *out++ = (char) character;
266 g_assert ((gsize) (out - result) <= strlen (escaped_string));
273 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
275 gchar *check = (gchar *) uri;
277 g_assert (uri != NULL);
278 g_assert (endptr != NULL);
280 if (g_ascii_isalpha (*check)) {
282 while (g_ascii_isalnum (*check) || *check == '+'
283 || *check == '-' || *check == '.')
291 * gst_uri_protocol_is_valid:
292 * @protocol: A string
294 * Tests if the given string is a valid protocol identifier. Protocols
295 * must consist of alphanumeric characters, '+', '-' and '.' and must
296 * start with a alphabetic character. See RFC 3986 Section 3.1.
298 * Returns: TRUE if the string is a valid protocol identifier, FALSE otherwise.
301 gst_uri_protocol_is_valid (const gchar * protocol)
305 g_return_val_if_fail (protocol != NULL, FALSE);
307 gst_uri_protocol_check_internal (protocol, &endptr);
309 return *endptr == '\0' && endptr != protocol;
316 * Tests if the given string is a valid URI identifier. URIs start with a valid
317 * scheme followed by ":" and maybe a string identifying the location.
319 * Returns: TRUE if the string is a valid URI
322 gst_uri_is_valid (const gchar * uri)
326 g_return_val_if_fail (uri != NULL, FALSE);
328 gst_uri_protocol_check_internal (uri, &endptr);
330 return *endptr == ':';
334 * gst_uri_get_protocol:
337 * Extracts the protocol out of a given valid URI. The returned string must be
338 * freed using g_free().
340 * Returns: The protocol for this URI.
343 gst_uri_get_protocol (const gchar * uri)
347 g_return_val_if_fail (uri != NULL, NULL);
348 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
350 colon = strstr (uri, ":");
352 return g_ascii_strdown (uri, colon - uri);
356 * gst_uri_has_protocol:
358 * @protocol: a protocol string (e.g. "http")
360 * Checks if the protocol of a given valid URI matches @protocol.
362 * Returns: %TRUE if the protocol matches.
367 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
371 g_return_val_if_fail (uri != NULL, FALSE);
372 g_return_val_if_fail (protocol != NULL, FALSE);
373 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
375 colon = strstr (uri, ":");
380 return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
384 * gst_uri_get_location:
387 * Extracts the location out of a given valid URI, ie. the protocol and "://"
388 * are stripped from the URI, which means that the location returned includes
389 * the hostname if one is specified. The returned string must be freed using
392 * Free-function: g_free
394 * Returns: (transfer full) (array zero-terminated=1): the location for this
395 * URI. Returns NULL if the URI isn't valid. If the URI does not contain
396 * a location, an empty string is returned.
399 gst_uri_get_location (const gchar * uri)
402 gchar *unescaped = NULL;
404 g_return_val_if_fail (uri != NULL, NULL);
405 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
407 colon = strstr (uri, "://");
411 unescaped = unescape_string (colon + 3, "/");
413 /* On Windows an URI might look like file:///c:/foo/bar.txt or
414 * file:///c|/foo/bar.txt (some Netscape versions) and we want to
415 * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
416 * Can't use g_filename_from_uri() here because it will only handle the
417 * file:// protocol */
419 if (unescaped != NULL && unescaped[0] == '/' &&
420 g_ascii_isalpha (unescaped[1]) &&
421 (unescaped[2] == ':' || unescaped[2] == '|')) {
423 g_memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
427 GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
434 * @protocol: Protocol for URI
435 * @location: (array zero-terminated=1) (transfer none): Location for URI
437 * Constructs a URI for a given valid protocol and location.
439 * Free-function: g_free
441 * Returns: (transfer full) (array zero-terminated=1): a new string for this
442 * URI. Returns NULL if the given URI protocol is not valid, or the given
446 gst_uri_construct (const gchar * protocol, const gchar * location)
448 char *escaped, *proto_lowercase;
451 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
452 g_return_val_if_fail (location != NULL, NULL);
454 proto_lowercase = g_ascii_strdown (protocol, -1);
455 escaped = escape_string (location);
456 retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
458 g_free (proto_lowercase);
466 const gchar *protocol;
471 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
474 GstElementFactory *factory;
475 SearchEntry *entry = (SearchEntry *) search_entry;
477 if (!GST_IS_ELEMENT_FACTORY (feature))
479 factory = GST_ELEMENT_FACTORY_CAST (feature);
481 if (factory->uri_type != entry->type)
484 protocols = gst_element_factory_get_uri_protocols (factory);
486 if (protocols == NULL) {
487 g_warning ("Factory '%s' implements GstUriHandler interface but returned "
488 "no supported protocols!", gst_plugin_feature_get_name (feature));
492 while (*protocols != NULL) {
493 if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
501 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
503 return gst_plugin_feature_get_rank (second) -
504 gst_plugin_feature_get_rank (first);
508 get_element_factories_from_uri_protocol (const GstURIType type,
509 const gchar * protocol)
511 GList *possibilities;
514 g_return_val_if_fail (protocol, NULL);
517 entry.protocol = protocol;
518 possibilities = gst_registry_feature_filter (gst_registry_get_default (),
519 search_by_entry, FALSE, &entry);
521 return possibilities;
525 * gst_uri_protocol_is_supported:
526 * @type: Whether to check for a source or a sink
527 * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
529 * Checks if an element exists that supports the given URI protocol. Note
530 * that a positive return value does not imply that a subsequent call to
531 * gst_element_make_from_uri() is guaranteed to work.
538 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
540 GList *possibilities;
542 g_return_val_if_fail (protocol, FALSE);
544 possibilities = get_element_factories_from_uri_protocol (type, protocol);
547 g_list_free (possibilities);
554 * gst_element_make_from_uri:
555 * @type: Whether to create a source or a sink
556 * @uri: URI to create an element for
557 * @elementname: (allow-none): Name of created element, can be NULL.
559 * Creates an element for handling the given URI.
561 * Returns: (transfer full): a new element or NULL if none could be created
564 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
565 const gchar * elementname)
567 GList *possibilities, *walk;
569 GstElement *ret = NULL;
571 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
572 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
574 protocol = gst_uri_get_protocol (uri);
575 possibilities = get_element_factories_from_uri_protocol (type, protocol);
578 if (!possibilities) {
579 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
584 possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
585 walk = possibilities;
588 gst_element_factory_create (GST_ELEMENT_FACTORY_CAST (walk->data),
589 elementname)) != NULL) {
590 GstURIHandler *handler = GST_URI_HANDLER (ret);
592 if (gst_uri_handler_set_uri (handler, uri))
594 gst_object_unref (ret);
599 gst_plugin_feature_list_free (possibilities);
601 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
602 type == GST_URI_SINK ? "sink" : "source", uri);
607 * gst_uri_handler_get_uri_type:
608 * @handler: A #GstURIHandler.
610 * Gets the type of the given URI handler
612 * Returns: the #GstURIType of the URI handler.
613 * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
616 gst_uri_handler_get_uri_type (GstURIHandler * handler)
618 GstURIHandlerInterface *iface;
621 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
623 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
624 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
625 g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
627 ret = iface->get_type (G_OBJECT_TYPE (handler));
628 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
634 * gst_uri_handler_get_protocols:
635 * @handler: A #GstURIHandler.
637 * Gets the list of protocols supported by @handler. This list may not be
640 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): the
641 * supported protocols. Returns NULL if the @handler isn't implemented
642 * properly, or the @handler doesn't support any protocols.
645 gst_uri_handler_get_protocols (GstURIHandler * handler)
647 GstURIHandlerInterface *iface;
650 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
652 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
653 g_return_val_if_fail (iface != NULL, NULL);
654 g_return_val_if_fail (iface->get_protocols != NULL, NULL);
656 ret = iface->get_protocols (G_OBJECT_TYPE (handler));
657 g_return_val_if_fail (ret != NULL, NULL);
663 * gst_uri_handler_get_uri:
664 * @handler: A #GstURIHandler
666 * Gets the currently handled URI.
668 * Returns: (transfer none): the URI currently handled by the @handler.
669 * Returns NULL if there are no URI currently handled. The
670 * returned string must not be modified or freed.
673 gst_uri_handler_get_uri (GstURIHandler * handler)
675 GstURIHandlerInterface *iface;
678 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
680 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
681 g_return_val_if_fail (iface != NULL, NULL);
682 g_return_val_if_fail (iface->get_uri != NULL, NULL);
683 ret = iface->get_uri (handler);
685 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
691 * gst_uri_handler_set_uri:
692 * @handler: A #GstURIHandler
695 * Tries to set the URI of the given handler.
697 * Returns: TRUE if the URI was set successfully, else FALSE.
700 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
702 GstURIHandlerInterface *iface;
704 gchar *new_uri, *protocol, *location, *colon;
706 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
707 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
709 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
710 g_return_val_if_fail (iface != NULL, FALSE);
711 g_return_val_if_fail (iface->set_uri != NULL, FALSE);
713 protocol = gst_uri_get_protocol (uri);
715 colon = strstr (uri, ":");
716 location = g_strdup (colon);
718 new_uri = g_strdup_printf ("%s%s", protocol, location);
720 ret = iface->set_uri (handler, uri);
730 gst_file_utils_canonicalise_path (const gchar * path)
732 gchar **parts, **p, *clean_path;
736 GST_WARNING ("FIXME: canonicalise win32 path");
737 return g_strdup (path);
741 parts = g_strsplit (path, "/", -1);
745 if (strcmp (*p, ".") == 0) {
746 /* just move all following parts on top of this, incl. NUL terminator */
748 g_memmove (p, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
749 /* re-check the new current part again in the next iteration */
751 } else if (strcmp (*p, "..") == 0 && p > parts) {
752 /* just move all following parts on top of the previous part, incl.
756 g_memmove (p - 1, p + 1, (g_strv_length (p + 1) + 1) * sizeof (gchar *));
757 /* re-check the new current part again in the next iteration */
766 num_parts = g_strv_length (parts) + 1; /* incl. terminator */
767 parts = g_renew (gchar *, parts, num_parts + 1);
768 g_memmove (parts + 1, parts, num_parts * sizeof (gchar *));
769 parts[0] = g_strdup ("/");
772 clean_path = g_build_filenamev (parts);
778 file_path_contains_relatives (const gchar * path)
780 return (strstr (path, "/./") != NULL || strstr (path, "/../") != NULL ||
781 strstr (path, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) != NULL ||
782 strstr (path, G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) != NULL);
786 * gst_filename_to_uri:
787 * @filename: absolute or relative file name path
788 * @error: pointer to error, or NULL
790 * Similar to g_filename_to_uri(), but attempts to handle relative file paths
791 * as well. Before converting @filename into an URI, it will be prefixed by
792 * the current working directory if it is a relative path, and then the path
793 * will be canonicalised so that it doesn't contain any './' or '../' segments.
795 * On Windows #filename should be in UTF-8 encoding.
800 gst_filename_to_uri (const gchar * filename, GError ** error)
802 gchar *abs_location = NULL;
803 gchar *uri, *abs_clean;
805 g_return_val_if_fail (filename != NULL, NULL);
806 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
808 if (g_path_is_absolute (filename)) {
809 if (!file_path_contains_relatives (filename)) {
810 uri = g_filename_to_uri (filename, NULL, error);
814 abs_location = g_strdup (filename);
818 cwd = g_get_current_dir ();
819 abs_location = g_build_filename (cwd, filename, NULL);
822 if (!file_path_contains_relatives (abs_location)) {
823 uri = g_filename_to_uri (abs_location, NULL, error);
828 /* path is now absolute, but contains '.' or '..' */
829 abs_clean = gst_file_utils_canonicalise_path (abs_location);
830 GST_LOG ("'%s' -> '%s' -> '%s'", filename, abs_location, abs_clean);
831 uri = g_filename_to_uri (abs_clean, NULL, error);
836 g_free (abs_location);
837 GST_DEBUG ("'%s' -> '%s'", filename, uri);