diff --git a/gst-libs/gst/rtsp/gstrtspmessage.c b/gst-libs/gst/rtsp/gstrtspmessage.c index a11e12073a..06b6f2353b 100644 --- a/gst-libs/gst/rtsp/gstrtspmessage.c +++ b/gst-libs/gst/rtsp/gstrtspmessage.c @@ -1186,6 +1186,8 @@ parse_auth_credentials (GPtrArray * auth_credentials, const gchar * header, * Parses the credentials given in a WWW-Authenticate or Authorization header. * * Returns: %NULL-terminated array of GstRTSPAuthCredential or %NULL. + * + * Since: 1.12 */ GstRTSPAuthCredential ** gst_rtsp_message_parse_auth_credentials (GstRTSPMessage * msg, @@ -1211,6 +1213,81 @@ gst_rtsp_message_parse_auth_credentials (GstRTSPMessage * msg, return (GstRTSPAuthCredential **) g_ptr_array_free (auth_credentials, FALSE); } +static GstRTSPAuthParam * +gst_rtsp_auth_param_copy (GstRTSPAuthParam * param) +{ + GstRTSPAuthParam *copy; + + if (param == NULL) + return NULL; + + copy = g_new0 (GstRTSPAuthParam, 1); + copy->name = g_strdup (param->name); + copy->value = g_strdup (param->value); + + return copy; +} + +static void +gst_rtsp_auth_param_free (GstRTSPAuthParam * param) +{ + if (param != NULL) { + g_free (param->name); + g_free (param->value); + g_free (param); + } +} + +G_DEFINE_BOXED_TYPE (GstRTSPAuthParam, gst_rtsp_auth_param, + (GBoxedCopyFunc) gst_rtsp_auth_param_copy, + (GBoxedFreeFunc) gst_rtsp_auth_param_free); + +static void +gst_rtsp_auth_credential_free (GstRTSPAuthCredential * credential) +{ + GstRTSPAuthParam **p; + + if (credential == NULL) + return; + + for (p = credential->params; p != NULL && *p != NULL; ++p) + gst_rtsp_auth_param_free (*p); + + g_free (credential->params); + g_free (credential->authorization); + g_free (credential); +} + +static GstRTSPAuthCredential * +gst_rtsp_auth_credential_copy (GstRTSPAuthCredential * cred) +{ + GstRTSPAuthCredential *copy; + + if (cred == NULL) + return NULL; + + copy = g_new0 (GstRTSPAuthCredential, 1); + copy->scheme = cred->scheme; + if (cred->params) { + guint i, n_params = g_strv_length ((gchar **) cred->params); + + copy->params = g_new0 (GstRTSPAuthParam *, n_params + 1); + for (i = 0; i < n_params; ++i) + copy->params[i] = gst_rtsp_auth_param_copy (cred->params[i]); + } + copy->authorization = g_strdup (cred->authorization); + return copy; +} + +/** + * gst_rtsp_auth_credentials_free: + * @credentials: a %NULL-terminated array of #GstRTSPAuthCredential + * + * Free a %NULL-terminated array of credentials returned from + * gst_rtsp_message_parse_auth_credentials(). + * + * Since: 1.12 + */ void gst_rtsp_auth_credentials_free (GstRTSPAuthCredential ** credentials) { @@ -1219,24 +1296,12 @@ gst_rtsp_auth_credentials_free (GstRTSPAuthCredential ** credentials) if (!credentials) return; - p = credentials; - while (*p) { - GstRTSPAuthParam **param = (*p)->params; - - if (param) { - while (*param) { - g_free ((*param)->name); - g_free ((*param)->value); - g_free (*param); - param++; - } - g_free ((*p)->params); - } - if ((*p)->authorization) - g_free ((*p)->authorization); - g_free (*p); - p++; - } + for (p = credentials; p != NULL && *p != NULL; ++p) + gst_rtsp_auth_credential_free (*p); g_free (credentials); } + +G_DEFINE_BOXED_TYPE (GstRTSPAuthCredential, gst_rtsp_auth_credential, + (GBoxedCopyFunc) gst_rtsp_auth_credential_copy, + (GBoxedFreeFunc) gst_rtsp_auth_credential_free); diff --git a/gst-libs/gst/rtsp/gstrtspmessage.h b/gst-libs/gst/rtsp/gstrtspmessage.h index b63abec230..d6c5c67ce3 100644 --- a/gst-libs/gst/rtsp/gstrtspmessage.h +++ b/gst-libs/gst/rtsp/gstrtspmessage.h @@ -201,6 +201,7 @@ GstRTSPResult gst_rtsp_message_steal_body (GstRTSPMessage *msg, typedef struct _GstRTSPAuthCredential GstRTSPAuthCredential; typedef struct _GstRTSPAuthParam GstRTSPAuthParam; + struct _GstRTSPAuthCredential { GstRTSPAuthMethod scheme; @@ -220,6 +221,14 @@ struct _GstRTSPAuthParam { GstRTSPAuthCredential ** gst_rtsp_message_parse_auth_credentials (GstRTSPMessage * msg, GstRTSPHeaderField field); void gst_rtsp_auth_credentials_free (GstRTSPAuthCredential ** credentials); +#define GST_TYPE_RTSP_AUTH_CREDENTIAL gst_rtsp_auth_credential_get_type() + +GType gst_rtsp_auth_credential_get_type (void); + +#define GST_TYPE_RTSP_AUTH_PARAM gst_rtsp_auth_param_get_type() + +GType gst_rtsp_auth_param_get_type (void); + /* debug */ GstRTSPResult gst_rtsp_message_dump (GstRTSPMessage *msg); diff --git a/tests/check/libs/rtsp.c b/tests/check/libs/rtsp.c index 4ce901665b..733d74b969 100644 --- a/tests/check/libs/rtsp.c +++ b/tests/check/libs/rtsp.c @@ -936,6 +936,37 @@ GST_START_TEST (test_rtsp_message_auth_credentials) GST_END_TEST; +GST_START_TEST (test_rtsp_message_auth_credentials_boxed) +{ + GstRTSPAuthCredential **credentials, *credentials2; + GstRTSPAuthParam *param2; + GstRTSPMessage *msg; + GstRTSPResult res; + + res = gst_rtsp_message_new_request (&msg, GST_RTSP_PLAY, + "rtsp://foo.bar:8554/test"); + fail_unless_equals_int (res, GST_RTSP_OK); + res = + gst_rtsp_message_add_header (msg, GST_RTSP_HDR_WWW_AUTHENTICATE, + "Basic foo=\"bar\", baz=foo"); + res = + gst_rtsp_message_add_header (msg, GST_RTSP_HDR_WWW_AUTHENTICATE, + "Basic foo1=\"bar\", baz1=foo"); + credentials = + gst_rtsp_message_parse_auth_credentials (msg, + GST_RTSP_HDR_WWW_AUTHENTICATE); + + credentials2 = g_boxed_copy (GST_TYPE_RTSP_AUTH_CREDENTIAL, credentials[0]); + gst_rtsp_auth_credentials_free (credentials); + gst_rtsp_message_free (msg); + + param2 = g_boxed_copy (GST_TYPE_RTSP_AUTH_PARAM, credentials2->params[0]); + g_boxed_free (GST_TYPE_RTSP_AUTH_CREDENTIAL, credentials2); + g_boxed_free (GST_TYPE_RTSP_AUTH_PARAM, param2); +} + +GST_END_TEST; + static Suite * rtsp_suite (void) { @@ -953,6 +984,7 @@ rtsp_suite (void) tcase_add_test (tc_chain, test_rtsp_range_convert); tcase_add_test (tc_chain, test_rtsp_message); tcase_add_test (tc_chain, test_rtsp_message_auth_credentials); + tcase_add_test (tc_chain, test_rtsp_message_auth_credentials_boxed); return s; } diff --git a/win32/common/libgstrtsp.def b/win32/common/libgstrtsp.def index 9716e08e14..1d0be240f3 100644 --- a/win32/common/libgstrtsp.def +++ b/win32/common/libgstrtsp.def @@ -1,6 +1,8 @@ EXPORTS + gst_rtsp_auth_credential_get_type gst_rtsp_auth_credentials_free gst_rtsp_auth_method_get_type + gst_rtsp_auth_param_get_type gst_rtsp_connection_accept gst_rtsp_connection_clear_auth_params gst_rtsp_connection_close