curlhttpsink: Set auth any for http_proxy and https_proxy

There was different behaviour if the proxy was configured through
properties or environment. For properties libcurl would be configured
with any auth, but for environment libcurl would default to using basic.
Now any auth is set for both configuration methods.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/7935>
This commit is contained in:
Peter Stensson 2024-11-19 10:30:09 +01:00 committed by GStreamer Marge Bot
parent e9d32abf98
commit 4d2e27dcf3

View file

@ -117,7 +117,8 @@ GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (curlhttpsink, "curlhttpsink",
GST_RANK_NONE, GST_TYPE_CURL_HTTP_SINK, curl_element_init (plugin));
/* private functions */
static gboolean proxy_setup (GstCurlBaseSink * bcsink);
static gboolean proxy_setup (GstCurlBaseSink * bcsink, const gchar * http_proxy,
const gchar * https_proxy);
static void
gst_curl_http_sink_class_init (GstCurlHttpSinkClass * klass)
@ -389,16 +390,38 @@ gst_curl_http_sink_set_header_unlocked (GstCurlBaseSink * bcsink)
return TRUE;
}
static gboolean
proxy_enabled (GstCurlHttpSink * sink, gchar ** http_proxy,
gchar ** https_proxy)
{
gboolean res = FALSE;
if (sink->proxy != NULL)
res = TRUE;
*http_proxy = getenv ("http_proxy");
if (*http_proxy != NULL)
res = TRUE;
*https_proxy = getenv ("https_proxy");
if (*https_proxy != NULL)
res = TRUE;
return res;
}
static gboolean
gst_curl_http_sink_set_options_unlocked (GstCurlBaseSink * bcsink)
{
GstCurlHttpSink *sink = GST_CURL_HTTP_SINK (bcsink);
GstCurlTlsSinkClass *parent_class;
CURLcode res;
gchar *http_proxy = NULL;
gchar *https_proxy = NULL;
/* proxy settings */
if (sink->proxy != NULL) {
if (!proxy_setup (bcsink)) {
if (proxy_enabled (sink, &http_proxy, &https_proxy)) {
if (!proxy_setup (bcsink, http_proxy, https_proxy)) {
return FALSE;
}
}
@ -509,7 +532,41 @@ gst_curl_http_sink_stop (GstBaseSink * bsink)
}
static gboolean
proxy_setup (GstCurlBaseSink * bcsink)
url_contains_credentials (const gchar * url)
{
CURLUcode rc;
g_autofree gchar *user = NULL;
g_autofree gchar *pass = NULL;
CURLU *handle = NULL;
if (url == NULL) {
return FALSE;
}
handle = curl_url ();
rc = curl_url_set (handle, CURLUPART_URL, url, 0);
if (rc != CURLUE_OK)
goto error;
rc = curl_url_get (handle, CURLUPART_USER, &user, 0);
if (rc != CURLUE_OK)
goto error;
rc = curl_url_get (handle, CURLUPART_PASSWORD, &pass, 0);
if (rc != CURLUE_OK)
goto error;
curl_url_cleanup (handle);
return TRUE;
error:
curl_url_cleanup (handle);
return FALSE;
}
static gboolean
custom_proxy_setup (GstCurlBaseSink * bcsink)
{
GstCurlHttpSink *sink = GST_CURL_HTTP_SINK (bcsink);
CURLcode res;
@ -547,14 +604,6 @@ proxy_setup (GstCurlBaseSink * bcsink)
return FALSE;
}
res = curl_easy_setopt (bcsink->curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set proxy authentication method: %s",
curl_easy_strerror (res));
return FALSE;
}
sink->proxy_auth = TRUE;
}
@ -567,6 +616,33 @@ proxy_setup (GstCurlBaseSink * bcsink)
return FALSE;
}
}
return TRUE;
}
static gboolean
proxy_setup (GstCurlBaseSink * bcsink, const gchar * http_proxy,
const gchar * https_proxy)
{
GstCurlHttpSink *sink = GST_CURL_HTTP_SINK (bcsink);
CURLcode res;
if (sink->proxy != NULL) {
if (!custom_proxy_setup (bcsink))
return FALSE;
} else {
sink->proxy_auth = url_contains_credentials (http_proxy)
|| url_contains_credentials (https_proxy);
}
if (sink->proxy_auth) {
res = curl_easy_setopt (bcsink->curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set proxy authentication method: %s",
curl_easy_strerror (res));
return FALSE;
}
}
return TRUE;
}