cpr icon indicating copy to clipboard operation
cpr copied to clipboard

SSL Context Callback

Open COM8 opened this issue 1 year ago • 2 comments

Implements #1019.

As an extension to #1020 this PR adds a function that gets called by libcurl just before the initialization of an SSL connection after having processed all other SSL related options to give a last chance to an application to modify the behavior of the SSL initialization.

Ref: https://curl.se/libcurl/c/CURLOPT_SSL_CTX_FUNCTION.html

How to use:

    size_t count{0};
    cpr::ssl::SslCtxCallback sslCtxCb{[](const std::shared_ptr<cpr::CurlHolder>& /*curl_holder*/, void* /*ssl_ctx*/, intptr_t userdata) {
                                          // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast)
                                          size_t* count = reinterpret_cast<size_t*>(userdata);
                                          (*count)++;
                                          return CURLE_OK;
                                      },
                                      // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast)
                                      reinterpret_cast<intptr_t>(&count)};

    cpr::SslOptions sslOpts = cpr::Ssl(sslCtxCb);

    cpr::Get(url, sslOpts);
    EXPECT_EQ(count, 1);

This option is incompatible with cpr::ssl::CaBuffer. In case both options are present an instance of std::logic_error gets thrown while performing the request.

COM8 avatar Jan 28 '24 11:01 COM8

Docs: https://github.com/libcpr/docs/pull/44

COM8 avatar Feb 04 '24 17:02 COM8

Currently still WIP since something seams to be broken when using this functionality when cpr is used via fetch content e.g. in our example repo.

Unit tests as well as sanitizers do not find anything yet...

COM8 avatar Feb 04 '24 18:02 COM8