jpbarrette / curlpp

C++ wrapper around libcURL
http://www.curlpp.org
1.68k stars 360 forks source link

Some culpp options should be defined as this #151

Open z16166 opened 2 years ago

z16166 commented 2 years ago

current definitions of curlpp options:

typedef curlpp::OptionTrait<std::string, CURLOPT_CAINFO> CaInfo;
typedef curlpp::OptionTrait<std::string, CURLOPT_CAPATH> CaPath;
typedef curlpp::OptionTrait<curlpp::types::SslCtxFunctionFunctor, CURLOPT_SSL_CTX_FUNCTION> SslCtxFunction;

But the above is not working if we want to use our own certificates in memory. That is, we want to do the following: (please refer to https://curl.se/libcurl/c/cacertinmem.html and https://curl.se/libcurl/c/CURLOPT_SSL_CTX_FUNCTION.html)

curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);

Why it's not woking? Because NULL pointer is different with empty string, libcurl will report error if you pass it with CaInfo("") and CaPath(""). It just expects NULL pointer, not empty string.

I used the following to do it(be sure all pointers are valid during the life time of curlpp easy handle!):

typedef curlpp::OptionTrait<void *, CURLOPT_SSL_CTX_FUNCTION> SslCtxFunctionOpt;
typedef curlpp::OptionTrait<void *, CURLOPT_CAINFO> CaInfoOpt;
typedef curlpp::OptionTrait<void *, CURLOPT_CAPATH> CaPathOpt;
sgallou commented 2 years ago

Hi,

this seems to make sense, as seen by this related curl usage example. But the solution you suggested changes Curlpp interface, and will break builds of other users. (For CURLOPT_SSL_CTX_FUNCTION, I'm not sure that curlpp option need modification)

I suggest to add an NullableOptionTrait class and to add 2 news options, like :

typedef curlpp::NullableOptionTrait<std::string, CURLOPT_CAINFO> NullableCaInfoOpt;
typedef curlpp::NullableOptionTrait<std::string, CURLOPT_CAPATH> NullableCaPathOpt;

(declare CaInfoOpt and CaPathOpt as obsoletes)

NullableOptionTrait should be able to manage a null value (add en empty ctor and a reset/clear function).

Il you're OK to achieve this, please create a PR.

Thanks for your help,