Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share data between curl easy handles in easy mode #1057

Open
Romantic-LiXuefeng opened this issue May 29, 2024 · 1 comment
Open

Share data between curl easy handles in easy mode #1057

Romantic-LiXuefeng opened this issue May 29, 2024 · 1 comment

Comments

@Romantic-LiXuefeng
Copy link

Is your feature request related to a problem?

Share connection cache and dns cache to improve the http requests in easy mode. (eg: HLS media playback)

Possible Solution

libcurl has a generic "sharing interface", where the application creates a "share object" that then holds data that can be shared by any number of easy handles.

Alternatives

No response

Additional Context

No response

@COM8
Copy link
Member

COM8 commented May 30, 2024

@Romantic-LiXuefeng thanks for the feature request. This sounds like an interesting feature. There is no plan for adding this in the immediate future. But here is a workaround for until we add real support for it:

#include "cpr/curlholder.h"
#include "cpr/session.h"
#include <cassert>
#include <memory>
#include <cpr/cpr.h>
#include <cpr/ssl_options.h>
#include <cpr/threadpool.h>

int main() {
    // Setup curl share
    CURLSH* share = curl_share_init();
    curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
    curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);

    // Create first session
    cpr::Session session1;
    std::shared_ptr<cpr::CurlHolder> holder1 = session1.GetCurlHolder();
    CURL* curl1 = holder1->handle;

    // Create second session
    cpr::Session session2;
    std::shared_ptr<cpr::CurlHolder> holder2 = session2.GetCurlHolder();
    CURL* curl2 = holder2->handle;

    // Apply the share to both session
    curl_easy_setopt(curl1, CURLOPT_SHARE, share);
    curl_easy_setopt(curl2, CURLOPT_SHARE, share);

    // Do further stuff..

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants