curl*sink: report errors from curl when setting options

https://bugzilla.gnome.org/show_bug.cgi?id=728960
This commit is contained in:
Sebastian Rasmussen 2014-04-16 01:50:16 +02:00 committed by Sebastian Dröge
parent c75c7a9a53
commit 241c3acad5
6 changed files with 324 additions and 87 deletions

View file

@ -607,28 +607,81 @@ static gboolean
gst_curl_base_sink_transfer_set_common_options_unlocked (GstCurlBaseSink * sink)
{
GstCurlBaseSinkClass *klass = GST_CURL_BASE_SINK_GET_CLASS (sink);
CURLcode res;
#ifdef DEBUG
curl_easy_setopt (sink->curl, CURLOPT_VERBOSE, 1);
res = curl_easy_setopt (sink->curl, CURLOPT_VERBOSE, 1);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set verbose: %s",
curl_easy_strerror (res));
return FALSE;
}
#endif
curl_easy_setopt (sink->curl, CURLOPT_URL, sink->url);
res = curl_easy_setopt (sink->curl, CURLOPT_URL, sink->url);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set URL: %s",
curl_easy_strerror (res));
return FALSE;
}
curl_easy_setopt (sink->curl, CURLOPT_CONNECTTIMEOUT, sink->timeout);
res = curl_easy_setopt (sink->curl, CURLOPT_CONNECTTIMEOUT, sink->timeout);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set connection timeout: %s",
curl_easy_strerror (res));
return FALSE;
}
/* using signals in a multi-threaded application is dangerous */
curl_easy_setopt (sink->curl, CURLOPT_NOSIGNAL, 1);
res = curl_easy_setopt (sink->curl, CURLOPT_NOSIGNAL, 1);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set no signalling: %s",
curl_easy_strerror (res));
return FALSE;
}
/* socket settings */
curl_easy_setopt (sink->curl, CURLOPT_SOCKOPTDATA, sink);
curl_easy_setopt (sink->curl, CURLOPT_SOCKOPTFUNCTION,
res = curl_easy_setopt (sink->curl, CURLOPT_SOCKOPTDATA, sink);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set sockopt user data: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (sink->curl, CURLOPT_SOCKOPTFUNCTION,
gst_curl_base_sink_transfer_socket_cb);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set sockopt function: %s",
curl_easy_strerror (res));
return FALSE;
}
curl_easy_setopt (sink->curl, CURLOPT_READFUNCTION, klass->transfer_read_cb);
curl_easy_setopt (sink->curl, CURLOPT_READDATA, sink);
curl_easy_setopt (sink->curl, CURLOPT_WRITEFUNCTION,
res = curl_easy_setopt (sink->curl, CURLOPT_READDATA, sink);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set read user data: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (sink->curl, CURLOPT_READFUNCTION,
klass->transfer_read_cb);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set read function: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (sink->curl, CURLOPT_WRITEDATA, sink);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set write user data: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (sink->curl, CURLOPT_WRITEFUNCTION,
gst_curl_base_sink_transfer_write_cb);
curl_easy_setopt (sink->curl, CURLOPT_WRITEDATA, sink);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set write function: %s",
curl_easy_strerror (res));
return FALSE;
}
return TRUE;
}
@ -636,22 +689,34 @@ gst_curl_base_sink_transfer_set_common_options_unlocked (GstCurlBaseSink * sink)
static gboolean
gst_curl_base_sink_transfer_set_options_unlocked (GstCurlBaseSink * sink)
{
gboolean res = FALSE;
GstCurlBaseSinkClass *klass = GST_CURL_BASE_SINK_GET_CLASS (sink);
CURLcode res;
gst_curl_base_sink_transfer_set_common_options_unlocked (sink);
if (!gst_curl_base_sink_transfer_set_common_options_unlocked (sink)) {
return FALSE;
}
/* authentication settings */
if (sink->user != NULL && strlen (sink->user)) {
curl_easy_setopt (sink->curl, CURLOPT_USERNAME, sink->user);
curl_easy_setopt (sink->curl, CURLOPT_PASSWORD, sink->passwd);
res = curl_easy_setopt (sink->curl, CURLOPT_USERNAME, sink->user);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set user name: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (sink->curl, CURLOPT_PASSWORD, sink->passwd);
if (res != CURLE_OK) {
sink->error = g_strdup_printf ("failed to set password: %s",
curl_easy_strerror (res));
return FALSE;
}
}
if (klass->set_options_unlocked) {
res = klass->set_options_unlocked (sink);
return klass->set_options_unlocked (sink);
} else {
return FALSE;
}
return res;
}
static size_t

View file

@ -185,10 +185,15 @@ static gboolean
set_file_dynamic_options_unlocked (GstCurlBaseSink * basesink)
{
gchar *tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
CURLcode res;
curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
res = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
g_free (tmp);
if (res != CURLE_OK) {
basesink->error = g_strdup_printf ("failed to set URL: %s",
curl_easy_strerror (res));
return FALSE;
}
return TRUE;
}
@ -196,7 +201,14 @@ set_file_dynamic_options_unlocked (GstCurlBaseSink * basesink)
static gboolean
set_file_options_unlocked (GstCurlBaseSink * basesink)
{
curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
CURLcode res;
res = curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
if (res != CURLE_OK) {
basesink->error = g_strdup_printf ("failed to prepare for upload: %s",
curl_easy_strerror (res));
return FALSE;
}
return TRUE;
}

View file

@ -178,6 +178,7 @@ set_ftp_dynamic_options_unlocked (GstCurlBaseSink * basesink)
{
gchar *tmp = NULL;
GstCurlFtpSink *sink = GST_CURL_FTP_SINK (basesink);
CURLcode res;
if (sink->tmpfile_create) {
gchar *rename_from = NULL;
@ -213,10 +214,25 @@ set_ftp_dynamic_options_unlocked (GstCurlBaseSink * basesink)
}
tmp = g_strdup_printf ("%s%s", basesink->url, uploadfile_as);
curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
res = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
if (res != CURLE_OK) {
g_free (tmp);
basesink->error = g_strdup_printf ("failed to set URL: %s",
curl_easy_strerror (res));
return FALSE;
}
sink->headerlist = curl_slist_append (sink->headerlist, rename_from);
sink->headerlist = curl_slist_append (sink->headerlist, rename_to);
curl_easy_setopt (basesink->curl, CURLOPT_POSTQUOTE, sink->headerlist);
res = curl_easy_setopt (basesink->curl, CURLOPT_POSTQUOTE, sink->headerlist);
if (res != CURLE_OK) {
g_free (tmp);
basesink->error = g_strdup_printf ("failed to set post quote: %s",
curl_easy_strerror (res));
return FALSE;
}
g_free (rename_from);
g_free (rename_to);
@ -228,7 +244,13 @@ set_ftp_dynamic_options_unlocked (GstCurlBaseSink * basesink)
}
} else {
tmp = g_strdup_printf ("%s%s", basesink->url, basesink->file_name);
curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
res = curl_easy_setopt (basesink->curl, CURLOPT_URL, tmp);
if (res != CURLE_OK) {
g_free (tmp);
basesink->error = g_strdup_printf ("failed to set URL: %s",
curl_easy_strerror (res));
return FALSE;
}
}
g_free (tmp);
@ -240,12 +262,18 @@ static gboolean
set_ftp_options_unlocked (GstCurlBaseSink * basesink)
{
GstCurlFtpSink *sink = GST_CURL_FTP_SINK (basesink);
CURLcode res;
curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
res = curl_easy_setopt (basesink->curl, CURLOPT_UPLOAD, 1L);
if (res != CURLE_OK) {
basesink->error = g_strdup_printf ("failed to prepare for upload: %s",
curl_easy_strerror (res));
return FALSE;
}
if (sink->ftp_port_arg != NULL && (strlen (sink->ftp_port_arg) > 0)) {
/* Connect data stream actively. */
CURLcode res = curl_easy_setopt (basesink->curl, CURLOPT_FTPPORT,
res = curl_easy_setopt (basesink->curl, CURLOPT_FTPPORT,
sink->ftp_port_arg);
if (res != CURLE_OK) {
@ -253,21 +281,31 @@ set_ftp_options_unlocked (GstCurlBaseSink * basesink)
curl_easy_strerror (res));
return FALSE;
}
goto end;
}
} else {
/* Connect data stream passively.
* libcurl will always attempt to use EPSV before PASV.
*/
if (!sink->epsv_mode) {
/* send only plain PASV command */
curl_easy_setopt (basesink->curl, CURLOPT_FTP_USE_EPSV, 0);
res = curl_easy_setopt (basesink->curl, CURLOPT_FTP_USE_EPSV, 0);
if (res != CURLE_OK) {
basesink->error =
g_strdup_printf ("failed to set extended passive mode: %s",
curl_easy_strerror (res));
return FALSE;
}
}
}
end:
if (sink->create_dirs) {
curl_easy_setopt (basesink->curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
res = curl_easy_setopt (basesink->curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1L);
if (res != CURLE_OK) {
basesink->error =
g_strdup_printf ("failed to set create missing dirs: %s",
curl_easy_strerror (res));
return FALSE;
}
}
return TRUE;

View file

@ -317,6 +317,7 @@ gst_curl_http_sink_set_header_unlocked (GstCurlBaseSink * bcsink)
{
GstCurlHttpSink *sink = GST_CURL_HTTP_SINK (bcsink);
gchar *tmp;
CURLcode res;
if (sink->header_list) {
curl_slist_free_all (sink->header_list);
@ -355,7 +356,12 @@ set_headers:
"\"%s\"", bcsink->file_name);
sink->header_list = curl_slist_append (sink->header_list, tmp);
g_free (tmp);
curl_easy_setopt (bcsink->curl, CURLOPT_HTTPHEADER, sink->header_list);
res = curl_easy_setopt (bcsink->curl, CURLOPT_HTTPHEADER, sink->header_list);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set HTTP headers: %s",
curl_easy_strerror (res));
return FALSE;
}
return TRUE;
}
@ -365,6 +371,7 @@ gst_curl_http_sink_set_options_unlocked (GstCurlBaseSink * bcsink)
{
GstCurlHttpSink *sink = GST_CURL_HTTP_SINK (bcsink);
GstCurlTlsSinkClass *parent_class;
CURLcode res;
/* proxy settings */
if (sink->proxy != NULL) {
@ -373,10 +380,21 @@ gst_curl_http_sink_set_options_unlocked (GstCurlBaseSink * bcsink)
}
}
curl_easy_setopt (bcsink->curl, CURLOPT_POST, 1L);
res = curl_easy_setopt (bcsink->curl, CURLOPT_POST, 1L);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set HTTP POST: %s",
curl_easy_strerror (res));
return FALSE;
}
/* FIXME: check user & passwd */
curl_easy_setopt (bcsink->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
res = curl_easy_setopt (bcsink->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set HTTP authentication methods: %s",
curl_easy_strerror (res));
return FALSE;
}
parent_class = GST_CURL_TLS_SINK_GET_CLASS (sink);
@ -449,30 +467,58 @@ static gboolean
proxy_setup (GstCurlBaseSink * bcsink)
{
GstCurlHttpSink *sink = GST_CURL_HTTP_SINK (bcsink);
CURLcode res;
if (curl_easy_setopt (bcsink->curl, CURLOPT_PROXY, sink->proxy)
!= CURLE_OK) {
res = curl_easy_setopt (bcsink->curl, CURLOPT_PROXY, sink->proxy);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set proxy: %s",
curl_easy_strerror (res));
return FALSE;
}
if (curl_easy_setopt (bcsink->curl, CURLOPT_PROXYPORT, sink->proxy_port)
!= CURLE_OK) {
res = curl_easy_setopt (bcsink->curl, CURLOPT_PROXYPORT, sink->proxy_port);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set proxy port: %s",
curl_easy_strerror (res));
return FALSE;
}
if (sink->proxy_user != NULL &&
strlen (sink->proxy_user) &&
sink->proxy_passwd != NULL && strlen (sink->proxy_passwd)) {
curl_easy_setopt (bcsink->curl, CURLOPT_PROXYUSERNAME, sink->proxy_user);
curl_easy_setopt (bcsink->curl, CURLOPT_PROXYPASSWORD, sink->proxy_passwd);
curl_easy_setopt (bcsink->curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
res = curl_easy_setopt (bcsink->curl, CURLOPT_PROXYUSERNAME,
sink->proxy_user);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set proxy user name: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (bcsink->curl, CURLOPT_PROXYPASSWORD,
sink->proxy_passwd);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set proxy password: %s",
curl_easy_strerror (res));
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;
}
if (g_str_has_prefix (bcsink->url, "https://")) {
/* tunnel all operations through a given HTTP proxy */
if (curl_easy_setopt (bcsink->curl, CURLOPT_HTTPPROXYTUNNEL, 1L)
!= CURLE_OK) {
res = curl_easy_setopt (bcsink->curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set HTTP proxy tunnel: %s",
curl_easy_strerror (res));
return FALSE;
}
}

View file

@ -630,6 +630,7 @@ gst_curl_smtp_sink_set_transfer_options_unlocked (GstCurlBaseSink * bcsink)
gchar *from_header = NULL;
gchar *enc_from;
gint i;
CURLcode res;
g_assert (sink->payload_headers == NULL);
g_assert (sink->mail_rcpt != NULL);
@ -694,7 +695,13 @@ gst_curl_smtp_sink_set_transfer_options_unlocked (GstCurlBaseSink * bcsink)
g_free (from_header);
g_free (request_headers);
curl_easy_setopt (bcsink->curl, CURLOPT_MAIL_FROM, sink->mail_from);
res = curl_easy_setopt (bcsink->curl, CURLOPT_MAIL_FROM, sink->mail_from);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set SMTP sender email address: %s",
curl_easy_strerror (res));
return FALSE;
}
if (sink->curl_recipients != NULL) {
curl_slist_free_all (sink->curl_recipients);
@ -709,7 +716,14 @@ gst_curl_smtp_sink_set_transfer_options_unlocked (GstCurlBaseSink * bcsink)
g_strfreev (tmp_list);
/* note that the CURLOPT_MAIL_RCPT takes a list, not a char array */
curl_easy_setopt (bcsink->curl, CURLOPT_MAIL_RCPT, sink->curl_recipients);
res = curl_easy_setopt (bcsink->curl, CURLOPT_MAIL_RCPT,
sink->curl_recipients);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set SMTP recipient email address: %s",
curl_easy_strerror (res));
return FALSE;
}
parent_class = GST_CURL_TLS_SINK_GET_CLASS (sink);
@ -954,18 +968,33 @@ gst_curl_smtp_sink_prepare_transfer (GstCurlBaseSink * bcsink)
return FALSE;
}
curl_easy_setopt (sink->pop_curl, CURLOPT_URL, sink->pop_location);
res = curl_easy_setopt (sink->pop_curl, CURLOPT_URL, sink->pop_location);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set URL: %s",
curl_easy_strerror (res));
return FALSE;
}
if (sink->pop_user != NULL && strlen (sink->pop_user) &&
sink->pop_passwd != NULL && strlen (sink->pop_passwd)) {
curl_easy_setopt (sink->pop_curl, CURLOPT_USERNAME, sink->pop_user);
curl_easy_setopt (sink->pop_curl, CURLOPT_PASSWORD, sink->pop_passwd);
res = curl_easy_setopt (sink->pop_curl, CURLOPT_USERNAME, sink->pop_user);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set user name: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (sink->pop_curl, CURLOPT_PASSWORD,
sink->pop_passwd);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set user name: %s",
curl_easy_strerror (res));
return FALSE;
}
}
}
if (sink->pop_curl == NULL) {
goto end;
}
if (sink->pop_curl != NULL) {
/* ready to initialize connection to POP server */
res = curl_easy_perform (sink->pop_curl);
if (res != CURLE_OK) {
@ -976,7 +1005,7 @@ gst_curl_smtp_sink_prepare_transfer (GstCurlBaseSink * bcsink)
curl_easy_cleanup (sink->pop_curl);
sink->pop_curl = NULL;
}
end:
return ret;
}

View file

@ -235,21 +235,34 @@ static gboolean
gst_curl_tls_sink_set_options_unlocked (GstCurlBaseSink * bcsink)
{
GstCurlTlsSink *sink = GST_CURL_TLS_SINK (bcsink);
CURLcode res;
if (!g_str_has_prefix (bcsink->url, "http"))
curl_easy_setopt (bcsink->curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
if (!g_str_has_prefix (bcsink->url, "http")) {
res = curl_easy_setopt (bcsink->curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set SSL level: %s",
curl_easy_strerror (res));
return FALSE;
}
}
/* crypto engine */
if ((g_strcmp0 (sink->crypto_engine, "auto") == 0) ||
(sink->crypto_engine == NULL)) {
if (curl_easy_setopt (bcsink->curl, CURLOPT_SSLENGINE_DEFAULT, 1L)
!= CURLE_OK) {
GST_WARNING ("Error setting up default SSL engine.");
res = curl_easy_setopt (bcsink->curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set default crypto engine: %s",
curl_easy_strerror (res));
return FALSE;
}
} else {
if (curl_easy_setopt (bcsink->curl, CURLOPT_SSLENGINE,
sink->crypto_engine) == CURLE_SSL_ENGINE_NOTFOUND) {
GST_WARNING ("Error setting up SSL engine: %s.", sink->crypto_engine);
res = curl_easy_setopt (bcsink->curl, CURLOPT_SSLENGINE,
sink->crypto_engine);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set crypto engine: %s",
curl_easy_strerror (res));
return FALSE;
}
}
@ -258,28 +271,62 @@ gst_curl_tls_sink_set_options_unlocked (GstCurlBaseSink * bcsink)
* certificates. */
if (sink->ca_cert != NULL && strlen (sink->ca_cert)) {
GST_DEBUG ("setting ca cert");
curl_easy_setopt (bcsink->curl, CURLOPT_CAINFO, sink->ca_cert);
res = curl_easy_setopt (bcsink->curl, CURLOPT_CAINFO, sink->ca_cert);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set certificate: %s",
curl_easy_strerror (res));
return FALSE;
}
}
if (sink->ca_path != NULL && strlen (sink->ca_path)) {
GST_DEBUG ("setting ca path");
curl_easy_setopt (bcsink->curl, CURLOPT_CAPATH, sink->ca_path);
res = curl_easy_setopt (bcsink->curl, CURLOPT_CAPATH, sink->ca_path);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set certificate path: %s",
curl_easy_strerror (res));
return FALSE;
}
}
if (!sink->insecure) {
/* identify authenticity of the peer's certificate */
curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYPEER, 1L);
res = curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYPEER, 1L);
if (res != CURLE_OK) {
bcsink->error = g_strdup_printf ("failed to set verification of peer: %s",
curl_easy_strerror (res));
return FALSE;
}
/* when CURLOPT_SSL_VERIFYHOST is 2, the commonName or subjectAltName
* fields are verified */
curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYHOST, 2L);
return TRUE;
res = curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYHOST, 2L);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf
("failed to set verification of server certificate: %s",
curl_easy_strerror (res));
return FALSE;
}
} else {
/* allow "insecure" SSL connections and transfers */
if (sink->insecure) {
curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYPEER, 0L);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf ("failed to set verification of peer: %s",
curl_easy_strerror (res));
return FALSE;
}
res = curl_easy_setopt (bcsink->curl, CURLOPT_SSL_VERIFYHOST, 0L);
if (res != CURLE_OK) {
bcsink->error =
g_strdup_printf
("failed to set verification of server certificate: %s",
curl_easy_strerror (res));
return FALSE;
}
}
}
return TRUE;