curl / Mailing Lists / curl-library / Single Mail

curl-library

setting a cookie in libcurl

From: Mckinney, Lee <Lee.Mckinney_at_vencore.com>
Date: Wed, 26 Apr 2017 19:01:38 +0000

According to libcurl online docs and CPR docs (http://whoshuu.github.io/cpr/advanced-usage.html#sending-cookies),
using in-memory cookies in a request should be straightforward with COOKIELIST.

I am trying to set a cookie in a libcurl GET via CPR.
The call chain is:

1) use of CPR Session in my application logic to customize a call
// TEMP

    cpr::Session lSession;

    lSession.SetUrl(cpr::Url{toCprUrl(url)});

    lSession.SetHeader(header);

    lSession.SetSharedContext(*(manager->getSharedContext()));

    lSession.SetCookies(cookies);

    cpr::Response response = lSession.Get();

2) CPR Session instance setup (not my logic, other than TEMP section and commented CURLOPT_COOKIEFILE)

Session::Impl::Impl() {

    curl_ = std::unique_ptr<CurlHolder, std::function<void(CurlHolder*) >>(newHolder(),

                                                                           &Impl::freeHolder);

    auto curl = curl_->handle;

    if (curl) {

        // Set up some sensible defaults

        auto version_info = curl_version_info(CURLVERSION_NOW);

        auto version = std::string{"curl/"} + std::string{version_info->version};

        curl_easy_setopt(curl, CURLOPT_USERAGENT, version.data());

        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);

        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);

        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_->error);

        //curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");

        // TEMP

        curl_global_init(CURL_GLOBAL_ALL);

        curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"cookiefile.txt");
...

3) newHolder used in the above Impl()

CurlHolder* Session::Impl::newHolder() {
    CurlHolder* holder = new CurlHolder();

    // TEMP
    curl_global_init(CURL_GLOBAL_ALL);
    holder->handle = curl_easy_init();
    return holder;
}

4) Finishing the chain from 1) above. Here's the body of Session::Impl::SetCookies,
with some test mods from me

void Session::Impl::SetCookies(const Cookies& cookies) {
    auto curl = curl_->handle;
    if (curl) {

        // TEMP
        std::ofstream myfile;
        myfile.open ("setcookies.txt");
        myfile << "cookies: " << cookies.GetEncoded().data() << "\n";
        myfile.close();

        // clear in-memory cookies.
        curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");

        // TEMP
        ////// Set-Cookie: iPlanetDirectoryPro=asdfasdfasfd;Path=/;Secure
#define SEP "\\t" /* Tab separates the fields */

        char *my_cookie =
          "<server-name>" /* Hostname */
          SEP "FALSE" /* Include subdomains */
          SEP "/" /* Path */
          SEP "FALSE" /* Secure */
          SEP "0" /* Expiry in epoch time format. 0 == Session */
          SEP "iPlanetDirectoryPro" /* Name */
          SEP "cookie-value"; /* Value */
        curl_easy_setopt(curl, CURLOPT_COOKIELIST, my_cookie);

        struct curl_slist* raw_cookies;
        struct curl_slist* nc;
        int i;
        curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &raw_cookies);
        std::ofstream myfile2;
        myfile2.open ("listaftersetcookies.txt");

        myfile2 << "==" << std::endl;
        myfile2 << "Session::Impl::Get cookies:" << std::endl;
        myfile2 << "cookie sent in: " << my_cookie << std::endl;
        nc = raw_cookies, i = 1;
        while(nc) {
            myfile2 << "i : " << i << ", cookie: " << nc->data < "\n";
            nc = nc->next;
            i++;
        }
        if(i == 1) {
            myfile2 << "(none)\n";
        }
        curl_slist_free_all(raw_cookies);
        myfile2 << "COUNT: " << i-1<< "\n";
        myfile2 << "==\n";
        myfile2.close();
        // ENDTEMP
    }
}

From 4), my_cookie should be a valid in-memory cookie at the time of the GET executed at the end of 1).
I am ignoring the cookies arg sent into SetCookies for now.
Want to get the in-memory cookie list working first.

From 4), I do see the "cookie sent in: " line in listaftersetcookies.txt right before the start of in-memory cookie list
looping. Cookie value is right.

The cookie list query

curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &raw_cookies);

comes up empty, though.
Here is the text file:

==
Session::Impl::Get cookies:
cookie sent in: <server-name>\tFALSE\t/\tFALSE\t0\tiPlanetDirectoryPro\tcookie-value
(none)
COUNT: 0

What am I missing regarding the libcurl cookie engine?
I want to add cookie to a request via in-memory cookie list.

R/
Lee

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2017-04-26