libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.29k stars 903 forks source link

SSL Context Callback #1021

Open COM8 opened 5 months ago

COM8 commented 5 months ago

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 commented 4 months ago

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

COM8 commented 4 months ago

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...