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.
23 * SECTION:gsturihandler
24 * @short_description: Plugin feature that handles URI types
25 * @see_also: #GstPluginFeature, #GstUri
27 * The URIHandler is a pluginfeature that can be used to locate elements and the
28 * element property that can handle a given URI.
35 #include "gst_private.h"
38 #include "gstmarshal.h"
39 #include "gstregistry.h"
43 GST_DEBUG_CATEGORY_STATIC (gst_uri_handler_debug);
44 #define GST_CAT_DEFAULT gst_uri_handler_debug
46 static void gst_uri_handler_base_init (gpointer g_class);
49 gst_uri_handler_get_type (void)
51 static GType urihandler_type = 0;
53 if (!urihandler_type) {
54 static const GTypeInfo urihandler_info = {
55 sizeof (GstURIHandlerInterface),
56 gst_uri_handler_base_init,
67 urihandler_type = g_type_register_static (G_TYPE_INTERFACE,
68 "GstURIHandler", &urihandler_info, 0);
70 GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
73 return urihandler_type;
76 gst_uri_handler_base_init (gpointer g_class)
78 static gboolean initialized = FALSE;
81 g_signal_new ("new-uri", GST_TYPE_URI_HANDLER, G_SIGNAL_RUN_LAST,
82 G_STRUCT_OFFSET (GstURIHandlerInterface, new_uri), NULL, NULL,
83 gst_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
88 static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
89 0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
90 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
91 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 4X @ABCDEFGHIJKLMNO */
92 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x3F, /* 5X PQRSTUVWXYZ[\]^_ */
93 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, /* 6X `abcdefghijklmno */
94 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20, 0x20, 0x20, 0x3F, 0x20 /* 7X pqrstuvwxyz{|}~DEL */
99 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
100 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
101 UNSAFE_PATH = 0x4, /* Allows '/' and '?' and '&' and '=' */
102 UNSAFE_DOS_PATH = 0x8, /* Allows '/' and '?' and '&' and '=' and ':' */
103 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
104 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
105 } UnsafeCharacterSet;
107 #define HEX_ESCAPE '%'
109 /* Escape undesirable characters using %
110 * -------------------------------------
112 * This function takes a pointer to a string in which
113 * some characters may be unacceptable unescaped.
114 * It returns a string which has these characters
115 * represented by a '%' character followed by two hex digits.
117 * This routine returns a g_malloced string.
120 static const gchar hex[16] = "0123456789ABCDEF";
123 escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
125 #define ACCEPTABLE_CHAR(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
132 UnsafeCharacterSet use_mask;
134 g_return_val_if_fail (mask == UNSAFE_ALL
135 || mask == UNSAFE_ALLOW_PLUS
136 || mask == UNSAFE_PATH
137 || mask == UNSAFE_DOS_PATH
138 || mask == UNSAFE_HOST || mask == UNSAFE_SLASHES, NULL);
140 if (string == NULL) {
146 for (p = string; *p != '\0'; p++) {
148 if (!ACCEPTABLE_CHAR (c)) {
151 if ((use_mask == UNSAFE_HOST) && (unacceptable || (c == '/'))) {
152 /* when escaping a host, if we hit something that needs to be escaped, or we finally
153 * hit a path separator, revert to path mode (the host segment of the url is over).
155 use_mask = UNSAFE_PATH;
159 result = g_malloc (p - string + unacceptable * 2 + 1);
162 for (q = result, p = string; *p != '\0'; p++) {
165 if (!ACCEPTABLE_CHAR (c)) {
166 *q++ = HEX_ESCAPE; /* means hex coming */
172 if ((use_mask == UNSAFE_HOST) && (!ACCEPTABLE_CHAR (c) || (c == '/'))) {
173 use_mask = UNSAFE_PATH;
184 * @string: string to be escaped
186 * Escapes @string, replacing any and all special characters
187 * with equivalent escape sequences.
189 * Return value: a newly allocated string equivalent to @string
190 * but with all special characters escaped
193 escape_string (const gchar * string)
195 return escape_string_internal (string, UNSAFE_ALL);
201 return c >= '0' && c <= '9' ? c - '0'
202 : c >= 'A' && c <= 'F' ? c - 'A' + 10
203 : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
207 unescape_character (const char *scanner)
212 first_digit = hex_to_int (*scanner++);
213 if (first_digit < 0) {
217 second_digit = hex_to_int (*scanner++);
218 if (second_digit < 0) {
222 return (first_digit << 4) | second_digit;
227 * @escaped_string: an escaped URI, path, or other string
228 * @illegal_characters: a string containing a sequence of characters
229 * considered "illegal", '\0' is automatically in this list.
231 * Decodes escaped characters (i.e. PERCENTxx sequences) in @escaped_string.
232 * Characters are encoded in PERCENTxy form, where xy is the ASCII hex code
233 * for character 16x+y.
235 * Return value: a newly allocated string with the unescaped equivalents,
236 * or %NULL if @escaped_string contained one of the characters
237 * in @illegal_characters.
240 unescape_string (const gchar * escaped_string, const gchar * illegal_characters)
246 if (escaped_string == NULL) {
250 result = g_malloc (strlen (escaped_string) + 1);
253 for (in = escaped_string; *in != '\0'; in++) {
255 if (*in == HEX_ESCAPE) {
256 character = unescape_character (in + 1);
258 /* Check for an illegal character. We consider '\0' illegal here. */
260 || (illegal_characters != NULL
261 && strchr (illegal_characters, (char) character) != NULL)) {
267 *out++ = (char) character;
271 g_assert (out - result <= strlen (escaped_string));
278 gst_uri_protocol_check_internal (const gchar * uri, gchar ** endptr)
280 gchar *check = (gchar *) uri;
282 g_assert (uri != NULL);
283 g_assert (endptr != NULL);
285 if (g_ascii_isalpha (*check)) {
287 while (g_ascii_isalnum (*check))
295 * gst_uri_protocol_is_valid:
296 * @protocol: string to check
298 * Tests if the given string is a valid protocol identifier. Protocols
299 * must consist of alphanumeric characters and not start with a number.
301 * Returns: TRUE if the string is a valid protocol identifier
304 gst_uri_protocol_is_valid (const gchar * protocol)
308 g_return_val_if_fail (protocol != NULL, FALSE);
310 gst_uri_protocol_check_internal (protocol, &endptr);
312 return *endptr == '\0' && endptr != protocol;
317 * @uri: string to check
319 * Tests if the given string is a valid URI identifier. URIs start with a valid
320 * protocol followed by "://" and a string identifying the location.
322 * Returns: TRUE if the string is a valid URI
325 gst_uri_is_valid (const gchar * uri)
329 g_return_val_if_fail (uri != NULL, FALSE);
331 gst_uri_protocol_check_internal (uri, &endptr);
333 return (*endptr == ':' && *(endptr + 1) == '/' && *(endptr + 2) == '/');
337 * gst_uri_get_protocol:
338 * @uri: URI to get protocol from
340 * Extracts the protocol out of a given valid URI. The returned string must be
341 * freed using g_free().
343 * Returns: The protocol for this URI.
346 gst_uri_get_protocol (const gchar * uri)
350 g_return_val_if_fail (uri != NULL, NULL);
351 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
353 colon = strstr (uri, "://");
355 return g_strndup (uri, colon - uri);
359 * gst_uri_get_location:
360 * @uri: URI to get the location from
362 * Extracts the location out of a given valid URI. So the protocol and "://"
363 * are stripped from the URI. The returned string must be freed using
366 * Returns: The location for this URI.
369 gst_uri_get_location (const gchar * uri)
372 gchar *location, *unescaped;
374 g_return_val_if_fail (uri != NULL, NULL);
375 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
377 colon = strstr (uri, "://");
379 location = g_strdup (colon + 3);
381 unescaped = unescape_string (location, "/");
389 * @protocol: protocol for URI
390 * @location: location for URI
392 * Constructs a URI for a given valid protocol and location.
394 * Returns: a new string for this URI
397 gst_uri_construct (const gchar * protocol, const gchar * location)
402 g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
403 g_return_val_if_fail (location != NULL, NULL);
405 escaped = escape_string (location);
406 retval = g_strdup_printf ("%s://%s", protocol, escaped);
420 search_by_entry (GstPluginFeature * feature, gpointer search_entry)
423 GstElementFactory *factory;
424 SearchEntry *entry = (SearchEntry *) search_entry;
426 if (!GST_IS_ELEMENT_FACTORY (feature))
428 factory = GST_ELEMENT_FACTORY (feature);
430 if (gst_element_factory_get_uri_type (factory) != entry->type)
433 protocols = gst_element_factory_get_uri_protocols (factory);
434 /* must be set when uri type is valid */
435 g_assert (protocols);
436 while (*protocols != NULL) {
437 if (strcmp (*protocols, entry->protocol) == 0)
445 sort_by_rank (gconstpointer a, gconstpointer b)
447 GstPluginFeature *first = GST_PLUGIN_FEATURE (a);
448 GstPluginFeature *second = GST_PLUGIN_FEATURE (b);
450 return gst_plugin_feature_get_rank (second) -
451 gst_plugin_feature_get_rank (first);
455 * gst_element_make_from_uri:
456 * @type: wether to create a source or a sink
457 * @uri: URI to create element for
458 * @elementname: optional name of created element
460 * Creates an element for handling the given URI.
462 * Returns: a new element or NULL if none could be created
465 gst_element_make_from_uri (const GstURIType type, const gchar * uri,
466 const gchar * elementname)
468 GList *possibilities, *walk;
470 GstElement *ret = NULL;
472 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (type), NULL);
473 g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
476 entry.protocol = gst_uri_get_protocol (uri);
477 possibilities = gst_registry_feature_filter (gst_registry_get_default (),
478 search_by_entry, FALSE, &entry);
479 g_free (entry.protocol);
481 if (!possibilities) {
482 GST_DEBUG ("No %s for URI '%s'", type == GST_URI_SINK ? "sink" : "source",
487 possibilities = g_list_sort (possibilities, sort_by_rank);
488 walk = possibilities;
490 if ((ret = gst_element_factory_create (GST_ELEMENT_FACTORY (walk->data),
491 elementname)) != NULL) {
492 GstURIHandler *handler = GST_URI_HANDLER (ret);
494 if (gst_uri_handler_set_uri (handler, uri))
496 gst_object_unref (ret);
501 g_list_free (possibilities);
503 GST_LOG_OBJECT (ret, "created %s for URL '%s'",
504 type == GST_URI_SINK ? "sink" : "source", uri);
509 * gst_uri_handler_get_uri_type:
510 * @handler: Handler to query type of
512 * Gets the type of a URI handler
514 * Returns: the type of the URI handler
517 gst_uri_handler_get_uri_type (GstURIHandler * handler)
519 GstURIHandlerInterface *iface;
522 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
524 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
525 g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
526 g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
527 ret = iface->get_type ();
528 g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
534 * gst_uri_handler_get_protocols:
535 * @handler: Handler to get protocols for
537 * Gets the list of supported protocols for this handler. This list may not be
540 * Returns: the supported protocols
543 gst_uri_handler_get_protocols (GstURIHandler * handler)
545 GstURIHandlerInterface *iface;
548 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
550 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
551 g_return_val_if_fail (iface != NULL, NULL);
552 g_return_val_if_fail (iface->get_protocols != NULL, NULL);
553 ret = iface->get_protocols ();
554 g_return_val_if_fail (ret != NULL, NULL);
560 * gst_uri_handler_get_uri:
561 * @handler: handler to query URI of
563 * Gets the currently handled URI of the handler or NULL, if none is set.
567 G_CONST_RETURN gchar *
568 gst_uri_handler_get_uri (GstURIHandler * handler)
570 GstURIHandlerInterface *iface;
573 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), NULL);
575 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
576 g_return_val_if_fail (iface != NULL, NULL);
577 g_return_val_if_fail (iface->get_uri != NULL, NULL);
578 ret = iface->get_uri (handler);
580 g_return_val_if_fail (gst_uri_is_valid (ret), NULL);
586 * gst_uri_handler_set_uri:
587 * @handler: handler to set URI of
590 * Tries to set the URI of the given handler and returns TRUE if it succeeded.
592 * Returns: TRUE, if the URI was set successfully
595 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
597 GstURIHandlerInterface *iface;
599 g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
600 g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
602 iface = GST_URI_HANDLER_GET_INTERFACE (handler);
603 g_return_val_if_fail (iface != NULL, FALSE);
604 g_return_val_if_fail (iface->set_uri != NULL, FALSE);
605 return iface->set_uri (handler, uri);
609 * gst_uri_handler_new_uri:
610 * @handler: handler with a new URI
611 * @uri: new URI or NULL if it was unset
613 * Emits the new-uri event for a given handler, when that handler has a new URI.
614 * This function should only be called by URI handlers themselves.
617 gst_uri_handler_new_uri (GstURIHandler * handler, const gchar * uri)
619 g_return_if_fail (GST_IS_URI_HANDLER (handler));
621 g_signal_emit_by_name (handler, "new-uri", uri);