gsturi: Fixed incorrect escaping of path as a generic string

The gst_uri_construct function was escaping the location string
as a generic uri string. This is incorrect since the slash('/')
characters are reserved for use in this exact case. The patch
changes the escape_string function mode to handle the path correctly.

I have deleted the escape_string function since it is no longer being
used and have created a unit test for the function. I have also
deprecated this function in favour of the GstUri API.

https://bugzilla.gnome.org/show_bug.cgi?id=783787
This commit is contained in:
Dimitrios Katsaros 2017-06-14 17:36:57 +02:00 committed by Sebastian Dröge
parent d4032d9e0f
commit 688d79033f
3 changed files with 55 additions and 19 deletions

View file

@ -155,6 +155,9 @@ gst_uri_error_quark (void)
return g_quark_from_static_string ("gst-uri-error-quark");
}
#define HEX_ESCAPE '%'
#ifndef GST_REMOVE_DEPRECATED
static const guchar acceptable[96] = { /* X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF */
0x00, 0x3F, 0x20, 0x20, 0x20, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x22, 0x20, 0x3F, 0x3F, 0x1C, /* 2X !"#$%&'()*+,-./ */
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20, 0x20, 0x2C, 0x20, 0x2C, /* 3X 0123456789:;<=>? */
@ -174,8 +177,6 @@ typedef enum
UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
} UnsafeCharacterSet;
#define HEX_ESCAPE '%'
/* Escape undesirable characters using %
* -------------------------------------
*
@ -248,21 +249,7 @@ escape_string_internal (const gchar * string, UnsafeCharacterSet mask)
return result;
}
/* escape_string:
* @string: string to be escaped
*
* Escapes @string, replacing any and all special characters
* with equivalent escape sequences.
*
* Return value: a newly allocated string equivalent to @string
* but with all special characters escaped
**/
static gchar *
escape_string (const gchar * string)
{
return escape_string_internal (string, UNSAFE_ALL);
}
#endif
static int
hex_to_int (gchar c)
@ -500,6 +487,10 @@ gst_uri_get_location (const gchar * uri)
return unescaped;
}
#ifdef GST_DISABLE_DEPRECATED
gchar *gst_uri_construct (const gchar * protocol, const gchar * location);
#endif
/**
* gst_uri_construct:
* @protocol: Protocol for URI
@ -512,6 +503,7 @@ gst_uri_get_location (const gchar * uri)
* Returns: (transfer full): a new string for this URI. Returns %NULL if the
* given URI protocol is not valid, or the given location is %NULL.
*/
#ifndef GST_REMOVE_DEPRECATED
gchar *
gst_uri_construct (const gchar * protocol, const gchar * location)
{
@ -522,13 +514,14 @@ gst_uri_construct (const gchar * protocol, const gchar * location)
g_return_val_if_fail (location != NULL, NULL);
proto_lowercase = g_ascii_strdown (protocol, -1);
escaped = escape_string (location);
escaped = escape_string_internal (location, UNSAFE_PATH);
retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
g_free (escaped);
g_free (proto_lowercase);
return retval;
}
#endif
typedef struct
{

View file

@ -147,9 +147,12 @@ gboolean gst_uri_has_protocol (const gchar * uri,
GST_EXPORT
gchar * gst_uri_get_location (const gchar * uri) G_GNUC_MALLOC;
GST_EXPORT
#ifndef GST_DISABLE_DEPRECATED
GST_DEPRECATED_FOR(gst_uri_new)
gchar * gst_uri_construct (const gchar * protocol,
const gchar * location) G_GNUC_MALLOC;
#endif
GST_EXPORT
gchar * gst_filename_to_uri (const gchar * filename,
GError ** error) G_GNUC_MALLOC;

View file

@ -75,6 +75,43 @@ GST_START_TEST (test_uri_get_location)
GST_END_TEST;
#ifndef GST_REMOVE_DEPRECATED
#ifdef GST_DISABLE_DEPRECATED
gchar *gst_uri_construct (const gchar * protocol, const gchar * location);
#endif
GST_START_TEST (test_gst_uri_construct)
{
gchar *l;
/* URI with no protocol or empty protocol should return empty string */
ASSERT_CRITICAL (l = gst_uri_construct (NULL, "/path/to/file"));
fail_unless (l == NULL);
ASSERT_CRITICAL (l = gst_uri_construct ("", "/path/to/file"));
fail_unless (l == NULL);
/* URI with no location should return empty string */
ASSERT_CRITICAL (l = gst_uri_construct ("protocol", NULL));
fail_unless (l == NULL);
/* check the protocol for validity */
l = gst_uri_construct ("protocol1234567890+-.", "somefile");
fail_unless (l != NULL);
fail_unless_equals_string (l, "protocol1234567890+-.://somefile");
g_free (l);
/* check the location for correct handling */
l = gst_uri_construct ("aprotocol",
"/path+ to/some/file%d?akey=aval&key2=val2");
fail_unless (l != NULL);
fail_unless_equals_string (l,
"aprotocol:///path%2B%20to/some/file%25d?akey=aval&key2=val2");
g_free (l);
}
GST_END_TEST;
#endif
#ifdef G_OS_WIN32
GST_START_TEST (test_win32_uri)
@ -1065,6 +1102,9 @@ gst_uri_suite (void)
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_protocol_case);
tcase_add_test (tc_chain, test_uri_get_location);
#ifndef GST_REMOVE_DEPRECATED
tcase_add_test (tc_chain, test_gst_uri_construct);
#endif
tcase_add_test (tc_chain, test_uri_misc);
tcase_add_test (tc_chain, test_element_make_from_uri);
#ifdef G_OS_WIN32