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) gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
{ {
GstQuery *query; GstQuery *query;
gchar *uri, *result; gchar *uri, *result, *path, *base_path, *find;
size_t len; GstUri *gst_uri;
gint find;
query = gst_query_new_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); GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
/* find the extension on the uri, this is everything after a '.' */ gst_uri = gst_uri_from_string (uri);
len = strlen (uri); if (gst_uri == NULL)
find = len - 1; goto invalid_uri;
while (find >= 0) { path = gst_uri_get_path (gst_uri);
if (uri[find] == '.') gst_uri_unref (gst_uri);
break;
find--; if (path == NULL)
} goto invalid_uri;
if (find < 0)
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; goto no_extension;
result = g_strdup (&uri[find + 1]); result = g_strdup (find + 1);
GST_DEBUG_OBJECT (typefind, "found extension %s", result); GST_DEBUG_OBJECT (typefind, "found extension %s", result);
gst_query_unref (query); gst_query_unref (query);
g_free (base_path);
g_free (uri); g_free (uri);
return result; return result;
@ -849,11 +856,19 @@ no_uri:
gst_query_unref (query); gst_query_unref (query);
return NULL; 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: no_extension:
{ {
GST_INFO_OBJECT (typefind, "could not find uri extension in %s", uri); GST_INFO_OBJECT (typefind, "could not find uri extension in %s", uri);
gst_query_unref (query); g_free (base_path);
g_free (uri); g_free (uri);
gst_query_unref (query);
return NULL; return NULL;
} }
} }