cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Cookie database query mid-session

From: Daniel Haude <dunno_at_stoptrick.com>
Date: Mon, 31 Jan 2005 15:34:12 +0100

Andy Hobbs wrote:
> Anything will be useful...

/*
        cookie reading/parsing code ro be used with libcurl
        daniel_at_stoptrick.com
        UNTESTED! Don't bother me about bugs.

        The only function you ever need to call from your code is
        www_get_cookie. Some cleanup code omitted.
*/

typedef struct cookie_list_t {
        char *name;
        char *value;
        struct cookie_list_t *next;
} cookie_list;

typedef struct {
        CURL *chandle;
        FILE *log;
        cookie_list *cookies;
} www_handle;

cookie_list *find_cookie(www_handle *conn, const char *name, const char
*newval)
{
        cookie_list *li, *last, *found;
        
        fprintf(stderr, "Cookie \"%s\": ", name);
        found = NULL;
        last = NULL;
        for (li = conn->cookies; li && !found; li = li->next) {
                if (!strcmp(li->name, name)) {
                        if (newval) {
                                free(li->value);
                                li->value = dh_strdup(newval);
                        }
                        found = li;
                        fprintf(stderr, "%s\n", newval ? "replaced" : "found");
                }
                last = li;
        }
        if (!found && newval) {
                found = malloc(sizeof *found);
                found->name = dh_strdup(name);
                found->value = dh_strdup(name);
                found->next = NULL;
                fprintf(stderr, "added\n");
                if (!conn->cookies) {
                        conn->cookies = found;
                } else {
                        last->next = found;
                }
        }
        return found;
}

const char www_get_cookie(www_handle *conn, const char *name)
/* returns value of cookie or NULL if not found */
{
        cookie_list *c;
        c = find_cookie(conn, name, NULL);
        if (c) {
                return c->value;
        } else {
                return NULL;
        }
}

int dump_cookies(www_handle *conn)
{
        cookie_list *li;
        
        for (li = conn->cookies; li; li = li->next) {
                fprintf(stderr, "%s=%s\n", li->name, li->value);
        }
}

static void add_cookie(www_handle *conn, const char *str)
{
        char *p, *q;
        char *name, *value;

        for (p = str; *p && isspace(*p); p++) ;
        for (q = p; *q && *q != '='; q++) ;
        name = dh_strndup(p, q-p);

        for (p = ++q; *p && *p != ';'; p++) ;

        value = dh_strndup(q, p-q);

        find_cookie(conn, name, value);
}

static int curl_debug (CURL *handle, curl_infotype type,
        char *text, size_t size, void *data)
        
        /* callback for
        
{
        int write = 0;
        www_handle *conn = data;
        char *ident;

        (void) handle;
        
        /* trimmed down from a longer switch statement, never compiled
        -- may not work properly! */
        if (type == CURLINFO_HEADER_IN) {
                if (!strncmp(text, "Set-Cookie:", 11)) {
                        add_cookie(conn, text+12);
                }
        }
        return 0;
}

/* Activate cookie parsing like this:

        conn is declared as www_handle *

        curl_easy_setopt(conn->chandle, CURLOPT_DEBUGFUNCTION, curl_debug);
        curl_easy_setopt(conn->chandle, CURLOPT_DEBUGDATA, conn);
*/
Received on 2005-01-31