2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gsturi.c: register URI handlers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:gsturihandler
25 * @short_description: Interface to ease URI handling in plugins.
27 * The URIHandler is an interface that is implemented by Source and Sink
28 * #GstElement to simplify then handling of URI.
30 * An application can use the following functions to quickly get an element
31 * that handles the given URI for reading or writing
32 * (gst_element_make_from_uri()).
34 * Source and Sink plugins should implement this interface when possible.
36 * Last reviewed on 2005-11-09 (0.9.4)
43 #include "gst_private.h"
46 #include "gstmarshal.h"
47 #include "gstregistry.h"
51 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
52 #define GST_CAT_DEFAULT gst_uri_handler_debug
60 static guint gst_uri_handler_signals[LAST_SIGNAL] = { 0 };
62 static void gst_uri_handler_base_init (gpointer g_class);
65 gst_uri_handler_get_type (void)
67 static volatile gsize urihandler_type = 0;
69 if (g_once_init_enter (&urihandler_type)) {
71 static const GTypeInfo urihandler_info = {
72 sizeof (GstURIHandlerInterface),
73 gst_uri_handler_base_init,
84 _type = g_type_register_static (G_TYPE_INTERFACE,
85 "GstURIHandler", &urihandler_info, 0);
87 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
89 g_once_init_leave (&urihandler_type, _type);
91 return urihandler_type;
95 gst_uri_handler_base_init (gpointer g_class)
97 static gboolean initialized = FALSE;
99 if (G_UNLIKELY (!initialized)) {
102 * GstURIHandler::new-uri:
103 * @handler: The #GstURIHandler which emitted the signal
104 * @uri: (transfer none): The new URI, or NULL if the URI was removed
106 * The URI of the given @handler has changed.
109 gst_uri_handler_signals[NEW_URI] =
110 g_signal_new ("new-uri", GST_TYPE_URI_HANDLER, G_SIGNAL_RUN_LAST,
111 G_STRUCT_OFFSET (GstURIHandlerInterface, new_uri), NULL, NULL,
112 gst_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
117 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
118 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
119 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
120 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
121 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
122 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
123 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
128 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
129 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
130 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
131 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
132 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
133 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
134 } UnsafeCharacterSet;
136 #define HEX_ESCAPE '%'
138 /* Escape undesirable characters using %
139 * -------------------------------------
141 * This function takes a pointer to a string in which
142 * some characters may be unacceptable unescaped.
143 * It returns a string which has these characters
144 * represented by a '%' character followed by two hex digits.
146 * This routine returns a g_malloced string.
149 static const gchar hex[16] = "0123456789ABCDEF";
152 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
154 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
161 UnsafeCharacterSet use_mask;
163 g_return_val_if_fail (mask == UNSAFE_ALL
164 || mask == UNSAFE_ALLOW_PLUS
165 || mask == UNSAFE_PATH
166 || mask == UNSAFE_DOS_PATH
167 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
169 if (string == NULL) {
175 for (p = string; *p != '\0'; p++) {
177 if (!ACCEPTABLE_CHAR (c)) {
180 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
181 /* when escaping a host, if we hit something that needs to be escaped, or we finally
182 * hit a path separator, revert to path mode (the host segment of the url is over).
184 use_mask = UNSAFE_PATH;
188 result = g_malloc (p - string + unacceptable * 2 + 1);
191 for (q = result, p = string; *p != '\0'; p++) {
194 if (!ACCEPTABLE_CHAR (c)) {
195 *q++ = HEX_ESCAPE; /* means hex coming */
201 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
202 use_mask = UNSAFE_PATH;
212 * @string: string to be escaped
214 * Escapes @string, replacing any and all special characters
215 * with equivalent escape sequences.
217 * Return value: a newly allocated string equivalent to @string
218 * but with all special characters escaped
221 escape_string (const gchar * string)
223 return escape_string_internal (string, UNSAFE_ALL);
229 return c >= '0' && c <= '9' ? c - '0'
230 : c >= 'A' && c <= 'F' ? c - 'A' + 10
231 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
235 unescape_character (const char *scanner)
240 first_digit = hex_to_int (*scanner++);
241 if (first_digit < 0) {
245 second_digit = hex_to_int (*scanner);
246 if (second_digit < 0) {
250 return (first_digit << 4) | second_digit;
254 * @escaped_string: an escaped URI, path, or other string
255 * @illegal_characters: a string containing a sequence of characters
256 * considered "illegal", '\0' is automatically in this list.
258 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
259 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
260 * for character 16x+y.
262 * Return value: a newly allocated string with the unescaped equivalents,
263 * or %NULL if @escaped_string contained one of the characters
264 * in @illegal_characters.
267 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
273 if (escaped_string == NULL) {
277 result = g_malloc (strlen (escaped_string) + 1);
280 for (in = escaped_string; *in != '\0'; in++) {
282 if (*in == HEX_ESCAPE) {
283 character = unescape_character (in + 1);
285 /* Check for an illegal character. We consider '\0' illegal here. */
287 || (illegal_characters != NULL
288 && strchr (illegal_characters, (char) character) != NULL)) {
294 *out++ = (char) character;
298 g_assert ((gsize) (out - result) <= strlen (escaped_string));
305 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
307 gchar *check = (gchar *) uri;
309 g_assert (uri != NULL);
310 g_assert (endptr != NULL);
312 if (g_ascii_isalpha (*check)) {
314 while (g_ascii_isalnum (*check) || *check == '+'
315 || *check == '-' || *check == '.')
323 * gst_uri_protocol_is_valid:
324 * @protocol: A string
326 * Tests if the given string is a valid protocol identifier. Protocols
327 * must consist of alphanumeric characters, '+', '-' and '.' and must
328 * start with a alphabetic character. See RFC 3986 Section 3.1.
330 * Returns: TRUE if the string is a valid protocol identifier, FALSE otherwise.
333 gst_uri_protocol_is_valid (const gchar * protocol)
337 g_return_val_if_fail (protocol != NULL, FALSE);
339 gst_uri_protocol_check_internal (protocol, &endptr);
341 return *endptr == '\0' && endptr != protocol;
348 * Tests if the given string is a valid URI identifier. URIs start with a valid
349 * scheme followed by ":" and maybe a string identifying the location.
351 * Returns: TRUE if the string is a valid URI
354 gst_uri_is_valid (const gchar * uri)
358 g_return_val_if_fail (uri != NULL, FALSE);
360 gst_uri_protocol_check_internal (uri, &endptr);
362 return *endptr == ':';
366 * gst_uri_get_protocol:
369 * Extracts the protocol out of a given valid URI. The returned string must be
370 * freed using g_free().
372 * Returns: The protocol for this URI.
375 gst_uri_get_protocol (const gchar * uri)
379 g_return_val_if_fail (uri != NULL, NULL);
380 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
382 colon = strstr (uri, ":");
384 return g_ascii_strdown (uri, colon - uri);
388 * gst_uri_has_protocol:
390 * @protocol: a protocol string (e.g. "http")
392 * Checks if the protocol of a given valid URI matches @protocol.
394 * Returns: %TRUE if the protocol matches.
399 gst_uri_has_protocol (const gchar * uri, const gchar * protocol)
403 g_return_val_if_fail (uri != NULL, FALSE);
404 g_return_val_if_fail (protocol != NULL, FALSE);
405 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
407 colon = strstr (uri, ":");
412 return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
416 * gst_uri_get_location:
419 * Extracts the location out of a given valid URI, ie. the protocol and "://"
420 * are stripped from the URI, which means that the location returned includes
421 * the hostname if one is specified. The returned string must be freed using
424 * Free-function: g_free
426 * Returns: (transfer full) (array zero-terminated=1): the location for this
427 * URI. Returns NULL if the URI isn't valid. If the URI does not contain
428 * a location, an empty string is returned.
431 gst_uri_get_location (const gchar * uri)
434 gchar *unescaped = NULL;
436 g_return_val_if_fail (uri != NULL, NULL);
437 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
439 colon = strstr (uri, "://");
443 unescaped = unescape_string (colon + 3, "/");
445 /* On Windows an URI might look like file:///c:/foo/bar.txt or
446 * file:///c|/foo/bar.txt (some Netscape versions) and we want to
447 * return c:/foo/bar.txt as location rather than /c:/foo/bar.txt.
448 * Can't use g_filename_from_uri() here because it will only handle the
449 * file:// protocol */
451 if (unescaped != NULL && unescaped[0] == '/' &&
452 g_ascii_isalpha (unescaped[1]) &&
453 (unescaped[2] == ':' || unescaped[2] == '|')) {
455 g_memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
459 GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
466 * @protocol: Protocol for URI
467 * @location: (array zero-terminated=1) (transfer none): Location for URI
469 * Constructs a URI for a given valid protocol and location.
471 * Free-function: g_free
473 * Returns: (transfer full) (array zero-terminated=1): a new string for this
474 * URI. Returns NULL if the given URI protocol is not valid, or the given
478 gst_uri_construct (const gchar * protocol, const gchar * location)
480 char *escaped, *proto_lowercase;
483 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
484 g_return_val_if_fail (location != NULL, NULL);
486 proto_lowercase = g_ascii_strdown (protocol, -1);
487 escaped = escape_string (location);
488 retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
490 g_free (proto_lowercase);
498 const gchar *protocol;
503 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
506 GstElementFactory *factory;
507 SearchEntry *entry = (SearchEntry *) search_entry;
509 if (!GST_IS_ELEMENT_FACTORY (feature))
511 factory = GST_ELEMENT_FACTORY_CAST (feature);
513 if (factory->uri_type != entry->type)
516 protocols = gst_element_factory_get_uri_protocols (factory);
518 if (protocols == NULL) {
519 g_warning ("Factory '%s' implements GstUriHandler interface but returned "
520 "no supported protocols!", gst_plugin_feature_get_name (feature));
524 while (*protocols != NULL) {
525 if (g_ascii_strcasecmp (*protocols, entry->protocol) == 0)
533 sort_by_rank (GstPluginFeature * first, GstPluginFeature * second)
535 return gst_plugin_feature_get_rank (second) -
536 gst_plugin_feature_get_rank (first);
540 get_element_factories_from_uri_protocol (const GstURIType type,
541 const gchar * protocol)
543 GList *possibilities;
546 g_return_val_if_fail (protocol, NULL);
549 entry.protocol = protocol;
550 possibilities = gst_registry_feature_filter (gst_registry_get_default (),
551 search_by_entry, FALSE, &entry);
553 return possibilities;
557 * gst_uri_protocol_is_supported:
558 * @type: Whether to check for a source or a sink
559 * @protocol: Protocol that should be checked for (e.g. "http" or "smb")
561 * Checks if an element exists that supports the given URI protocol. Note
562 * that a positive return value does not imply that a subsequent call to
563 * gst_element_make_from_uri() is guaranteed to work.
570 gst_uri_protocol_is_supported (const GstURIType type, const gchar * protocol)
572 GList *possibilities;
574 g_return_val_if_fail (protocol, FALSE);
576 possibilities = get_element_factories_from_uri_protocol (type, protocol);
579 g_list_free (possibilities);
586 * gst_element_make_from_uri:
587 * @type: Whether to create a source or a sink
588 * @uri: URI to create an element for
589 * @elementname: (allow-none): Name of created element, can be NULL.
591 * Creates an element for handling the given URI.
593 * Returns: (transfer full): a new element or NULL if none could be created
596 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
597 const gchar * elementname)
599 GList *possibilities, *walk;
601 GstElement *ret = NULL;
603 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
604 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
606 protocol = gst_uri_get_protocol (uri);
607 possibilities = get_element_factories_from_uri_protocol (type, protocol);
610 if (!possibilities) {
611 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
616 possibilities = g_list_sort (possibilities, (GCompareFunc) sort_by_rank);
617 walk = possibilities;
620 gst_element_factory_create (GST_ELEMENT_FACTORY_CAST (walk->data),
621 elementname)) != NULL) {
622 GstURIHandler *handler = GST_URI_HANDLER (ret);
624 if (gst_uri_handler_set_uri (handler, uri))
626 gst_object_unref (ret);
631 gst_plugin_feature_list_free (possibilities);
633 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
634 type == GST_URI_SINK ? "sink" : "source", uri);
639 * gst_uri_handler_get_uri_type:
640 * @handler: A #GstURIHandler.
642 * Gets the type of the given URI handler
644 * Returns: the #GstURIType of the URI handler.
645 * Returns #GST_URI_UNKNOWN if the @handler isn't implemented correctly.
648 gst_uri_handler_get_uri_type (GstURIHandler * handler)
650 GstURIHandlerInterface *iface;
653 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
655 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
656 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
657 g_return_val_if_fail (iface->get_type != NULL
658 || iface->get_type_full != NULL, GST_URI_UNKNOWN);
660 if (iface->get_type != NULL)
661 ret = iface->get_type ();
663 ret = iface->get_type_full (G_OBJECT_TYPE (handler));
664 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
670 * gst_uri_handler_get_protocols:
671 * @handler: A #GstURIHandler.
673 * Gets the list of protocols supported by @handler. This list may not be
676 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): the
677 * supported protocols. Returns NULL if the @handler isn't implemented
678 * properly, or the @handler doesn't support any protocols.
681 gst_uri_handler_get_protocols (GstURIHandler * handler)
683 GstURIHandlerInterface *iface;
686 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
688 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
689 g_return_val_if_fail (iface != NULL, NULL);
690 g_return_val_if_fail (iface->get_protocols != NULL ||
691 iface->get_protocols_full != NULL, NULL);
693 if (iface->get_protocols != NULL) {
694 ret = iface->get_protocols ();
696 ret = iface->get_protocols_full (G_OBJECT_TYPE (handler));
698 g_return_val_if_fail (ret != NULL, NULL);
704 * gst_uri_handler_get_uri:
705 * @handler: A #GstURIHandler
707 * Gets the currently handled URI.
709 * Returns: (transfer none): the URI currently handled by the @handler.
710 * Returns NULL if there are no URI currently handled. The
711 * returned string must not be modified or freed.
713 G_CONST_RETURN gchar *
714 gst_uri_handler_get_uri (GstURIHandler * handler)
716 GstURIHandlerInterface *iface;
719 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
721 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
722 g_return_val_if_fail (iface != NULL, NULL);
723 g_return_val_if_fail (iface->get_uri != NULL, NULL);
724 ret = iface->get_uri (handler);
726 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
732 * gst_uri_handler_set_uri:
733 * @handler: A #GstURIHandler
736 * Tries to set the URI of the given handler.
738 * Returns: TRUE if the URI was set successfully, else FALSE.
741 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
743 GstURIHandlerInterface *iface;
745 gchar *new_uri, *protocol, *location, *colon;
747 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
748 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
750 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
751 g_return_val_if_fail (iface != NULL, FALSE);
752 g_return_val_if_fail (iface->set_uri != NULL, FALSE);
754 protocol = gst_uri_get_protocol (uri);
756 colon = strstr (uri, ":");
757 location = g_strdup (colon);
759 new_uri = g_strdup_printf ("%s%s", protocol, location);
761 ret = iface->set_uri (handler, uri);
771 * gst_uri_handler_new_uri:
772 * @handler: A #GstURIHandler
773 * @uri: new URI or NULL if it was unset
775 * Emits the new-uri signal for a given handler, when that handler has a new URI.
776 * This function should only be called by URI handlers themselves.
779 gst_uri_handler_new_uri (GstURIHandler * handler, const gchar * uri)
781 g_return_if_fail (GST_IS_URI_HANDLER (handler));
783 g_signal_emit (handler, gst_uri_handler_signals[NEW_URI], 0, uri);