curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Wrapper function for curl_easy_setopt()

From: Timothe Litt <litt_at_acm.org>
Date: Tue, 24 Oct 2023 07:30:34 -0400

On 24-Oct-23 06:24, Vladimir Levijev via curl-library wrote:
> Hi,
>
> We use curl library and a lot of curl_easy_setopt() calls in our application. When our application is run with curl library compiled without some features we can get the following error message from failed curl_easy_setopt() call:
>
> "An unknown option was passed in to libcurl”
>
> I understand that this is all that cURL can tell because the option that was passed to it was part of the functionality that wasn’t chosen at a compile time.
>
> For example, target library was compiled without support for SMTP protocol. Our application tries to send email using cURL, tries to set CURLOPT_MAIL_FROM and gets an error that this option is unknown. All clear at this point.
>
> Now what we would like is to have the identifier “CURLOPT_MAIL_FROM”, that caused the failure, in the error message that we print to the user. I don’t want to specify the option 2 times in every call (pseudo code):
>
> if (!curl_easy_setopt(... CURLOPT_MAIL_FROM…) ) {
> snprintf(error, “setting CURLOPT_MAIL_FROM failed: …”);
> goto out;
> }
>
> So I tried it with #define but then you deal with static name of the error buffer and the GOTO label (in some parts we use it, but not everythere). So it seems I need a kind of wrapper function that returns an error message together with an option that was attempted to be set. For example:
>
> "cannot set CURLOPT_MAIL_FROM: An unknown option was passed in to libcurl"
>
> This way we would understand from the error message what exactly is missing from the target library.
>
> I see in the source code of the library there is a function Curl_vsetopt(), I thought this is what I could use, but it’s internal. Any idea how I could achieve this?
>
> Or maybe there are plans to improve the error message by saying which option (name) it failed to set?
>
> TIA!
>
> Cheers,
>
> VL

It certainly would be helpful if the error messages were improved to
include the failed parameter name (or even number).

However, you might want to look at calling curl_version_info()
https://curl.se/libcurl/c/curl_version_info.html

Your application can make a high level decision on what's supported by
checking the list returned in the protocols and/or feature_names
elements, rather than checking each curl_easy_setopt.  (For curl <
CURLVERSION_ELEVENTH, check the deprecated features bit mask.)

In the case you mentioned, look for /smtps?/ in protocols.

This is likely to simplify your application.


Timothe Litt
ACM Distinguished Engineer
--------------------------
This communication may not represent the ACM or my employer's views,
if any, on the matters discussed.



-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2023-10-24