typefind: fix reading file extension from URI

Currently reading extension relies on the fact that everything after the
last"." character is a file extension. Whereas that works fine for most
of the cases, it breaks when the URI contains a query part.

E.g.: `http://url.com/file.mp4?param=value` returns `mp4?param=value`
instead of `mp4`.

In this commit we use URI parser to read the path of the URI (in the example
above, that is `/file.mp4`) and read extension from that path.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1305>
This commit is contained in:
Marcin Kolny 2021-11-04 09:30:31 +00:00
parent a7b376011b
commit a46ab2ced2

View file

@ -800,9 +800,8 @@ static gchar *
gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
{
GstQuery *query;
gchar *uri, *result;
size_t len;
gint find;
gchar *uri, *result, *path, *base_path, *find;
GstUri *gst_uri;
query = gst_query_new_uri ();
@ -816,22 +815,30 @@ gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
/* find the extension on the uri, this is everything after a '.' */
len = strlen (uri);
find = len - 1;
gst_uri = gst_uri_from_string (uri);
if (gst_uri == NULL)
goto invalid_uri;
while (find >= 0) {
if (uri[find] == '.')
break;
find--;
}
if (find < 0)
path = gst_uri_get_path (gst_uri);
gst_uri_unref (gst_uri);
if (path == NULL)
goto invalid_uri;
base_path = g_path_get_basename (path);
g_free (path);
/* find the extension on the path, this is everything after a '.' */
find = strrchr (base_path, '.');
if (find == NULL)
goto no_extension;
result = g_strdup (&uri[find + 1]);
result = g_strdup (find + 1);
GST_DEBUG_OBJECT (typefind, "found extension %s", result);
gst_query_unref (query);
g_free (base_path);
g_free (uri);
return result;
@ -849,11 +856,19 @@ no_uri:
gst_query_unref (query);
return NULL;
}
invalid_uri:
{
GST_INFO_OBJECT (typefind, "failed to extract path from uri %s", uri);
g_free (uri);
gst_query_unref (query);
return NULL;
}
no_extension:
{
GST_INFO_OBJECT (typefind, "could not find uri extension in %s", uri);
gst_query_unref (query);
g_free (base_path);
g_free (uri);
gst_query_unref (query);
return NULL;
}
}