cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: getting/setting cookies

From: Maurice Barnum <mbarnum_at_tivo.com>
Date: Fri, 17 Dec 2004 22:09:26 -0800

Daniel Stenberg <daniel-curl_at_haxx.se> writes:

> Hi
>
> I have this suggestion on a simple get/set cookies interface for addition to
> libcurl:
>
> curl_easy_getinfo() - CURLINFO_COOKIELIST to get a curl_slist with all known
> cookies (netscape/mozilla cookie file formatted)
>
> curl_easy_setopt() - CURLOPT_COOKIELIST to set a list of cookies (using the
> same format as above or HTTP-header style - both are supported)

in my project, i want to get or set the cookies that matches the
session's host. i hacked up support to do that (fetched the cookie
list with Curl_cookie_getlist, and then formatted using the
HTTP-header style).

using this proposed interface, if i wanted to get or set the cookies
that match only the configured CURLOPT_URL, it looks like i'd need to
replicate the cookie matching code in Curl_cookie_getlist?

it would be very nice if the library would let me retrieve either
"matching" cookies or all of them. the first idea that jumps to
my mind isn't a very good one: if CURLOPT_URL is set, filter the
cookies based upon the url, otherwise return all of them. i'm not
aware of any curl_easy_getinfo or curl_easy_setopt methods that
are "modal" in that way.

> To remove a cookie that libcurl already knows about, you simply set it with no
> contents, or you set it with an expired time.
>
> I'm not sure how to clear all known cookies easily, or even if that is a
> desired feature.

what about passing NULL to curl_easy_setopt/CURLOPT_COOKIELIST?
similarly, removing and modifying cookies could be done by
getting the cookie list and then setting the modified one.

 ...

> This is much simpler than our previous cookie API discussions, but I think
> simplicity is to prefer here.

i like simple. for my project i needed to retrieve the cookies
that match the current url. since i didn't want to worry about
merge issues i didn't define a new CURLINFO_ param. instead i
implemented this:

    /**
     * Return a list of cookies for the configured connection or NULL
     * if there are no cookies that match this request.
     *
     * @param complete if non-zero, format all of the cookie attributes
     * that are known. otherwise, only the name=value
     * pair will be returned.
     */
    struct curl_slist *curl_ext_get_cookies(CURL *, int complete);

the "complete" parameter is silly; i'll remove it if i keep this
function around after upgrading to later versions of curl.
Received on 2004-12-18