mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-30 22:03:29 +02:00
gfile: Strip query sections from file: URIs
According to https://url.spec.whatwg.org/#file-state a file URI can have a fragment and query string, so just ignore them and don't raise an invalid URI error. Fixes: #3050
This commit is contained in:
committed by
Philip Withnall
parent
215a6ed80c
commit
b504cc0841
@@ -94,14 +94,24 @@ g_local_vfs_get_file_for_uri (GVfs *vfs,
|
||||
{
|
||||
char *path;
|
||||
GFile *file;
|
||||
char *stripped_uri, *hash;
|
||||
|
||||
char *stripped_uri, *hash, *question_mark;
|
||||
|
||||
/* As per https://url.spec.whatwg.org/#file-state, file: URIs can contain
|
||||
* query and fragment sections. We ignore them in order to get only the file
|
||||
* path. Compliance to this part of the WhatWG spec doesn’t necessarily mean
|
||||
* we comply with the entire spec. */
|
||||
if (strchr (uri, '#') != NULL)
|
||||
{
|
||||
stripped_uri = g_strdup (uri);
|
||||
hash = strchr (stripped_uri, '#');
|
||||
*hash = 0;
|
||||
}
|
||||
else if (strchr (uri, '?') != NULL)
|
||||
{
|
||||
stripped_uri = g_strdup (uri);
|
||||
question_mark = strchr (stripped_uri, '?');
|
||||
*question_mark = 0;
|
||||
}
|
||||
else
|
||||
stripped_uri = (char *)uri;
|
||||
|
||||
|
@@ -3935,6 +3935,38 @@ test_enumerator_cancellation (void)
|
||||
g_object_unref (dir);
|
||||
}
|
||||
|
||||
static void
|
||||
test_from_uri_ignores_fragment (void)
|
||||
{
|
||||
GFile *file;
|
||||
gchar *path;
|
||||
file = g_file_new_for_uri ("file:///tmp/foo#bar");
|
||||
path = g_file_get_path (file);
|
||||
#ifdef G_OS_WIN32
|
||||
g_assert_cmpstr (path, ==, "\\tmp\\foo");
|
||||
#else
|
||||
g_assert_cmpstr (path, ==, "/tmp/foo");
|
||||
#endif
|
||||
g_free (path);
|
||||
g_object_unref (file);
|
||||
}
|
||||
|
||||
static void
|
||||
test_from_uri_ignores_query_string (void)
|
||||
{
|
||||
GFile *file;
|
||||
gchar *path;
|
||||
file = g_file_new_for_uri ("file:///tmp/foo?bar");
|
||||
path = g_file_get_path (file);
|
||||
#ifdef G_OS_WIN32
|
||||
g_assert_cmpstr (path, ==, "\\tmp\\foo");
|
||||
#else
|
||||
g_assert_cmpstr (path, ==, "/tmp/foo");
|
||||
#endif
|
||||
g_free (path);
|
||||
g_object_unref (file);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
@@ -3990,6 +4022,8 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/file/query-default-handler-uri", test_query_default_handler_uri);
|
||||
g_test_add_func ("/file/query-default-handler-uri-async", test_query_default_handler_uri_async);
|
||||
g_test_add_func ("/file/enumerator-cancellation", test_enumerator_cancellation);
|
||||
g_test_add_func ("/file/from-uri/ignores-query-string", test_from_uri_ignores_query_string);
|
||||
g_test_add_func ("/file/from-uri/ignores-fragment", test_from_uri_ignores_fragment);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Reference in New Issue
Block a user