int main(int argc, char **argv) { struct CookieInfo *c=NULL; if(argc>1) { c = Curl_cookie_init(argv[1], c); Curl_cookie_add(c, TRUE, "PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/ftgw; secure"); Curl_cookie_add(c, TRUE, "foobar=yes; domain=.haxx.se; path=/looser;"); c = Curl_cookie_init(argv[1], c); Curl_cookie_output(c); Curl_cookie_cleanup(c); return 0; } return 1; }
struct Cookie* curl_cookie_add(struct SessionHandle *data, const char* value, const char* domain, const char* path) { if(!data->cookies) data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE); return Curl_cookie_add(data, data->cookies, TRUE, (char*)value, domain, path); }
/***************************************************************************** * * Curl_cookie_init() * * Inits a cookie struct to read data from a local file. This is always * called before any cookies are set. File may be NULL. * * If 'newsession' is TRUE, discard all "session cookies" on read from file. * ****************************************************************************/ struct CookieInfo *Curl_cookie_init(struct SessionHandle *data, char *file, struct CookieInfo *inc, bool newsession) { char line[MAX_COOKIE_LINE]; struct CookieInfo *c; FILE *fp; bool fromfile=TRUE; if(NULL == inc) { /* we didn't get a struct, create one */ c = (struct CookieInfo *)malloc(sizeof(struct CookieInfo)); if(!c) return NULL; /* failed to get memory */ memset(c, 0, sizeof(struct CookieInfo)); c->filename = strdup(file?file:"none"); /* copy the name just in case */ } else { /* we got an already existing one, use that */ c = inc; } c->running = FALSE; /* this is not running, this is init */ if(file && strequal(file, "-")) { fp = stdin; fromfile=FALSE; } else fp = file?fopen(file, "r"):NULL; c->newsession = newsession; /* new session? */ if(fp) { char *lineptr; bool headerline; while(fgets(line, MAX_COOKIE_LINE, fp)) { if(checkprefix("Set-Cookie:", line)) { /* This is a cookie line, get it! */ lineptr=&line[11]; headerline=TRUE; } else { lineptr=line; headerline=FALSE; } while(*lineptr && isspace((int)*lineptr)) lineptr++; Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL); } if(fromfile) fclose(fp); } c->running = TRUE; /* now, we're running */ return c; }
/***************************************************************************** * * Curl_cookie_init() * * Inits a cookie struct to read data from a local file. This is always * called before any cookies are set. File may be NULL. * * If 'newsession' is TRUE, discard all "session cookies" on read from file. * * Returns NULL on out of memory. Invalid cookies are ignored. ****************************************************************************/ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data, const char *file, struct CookieInfo *inc, bool newsession) { struct CookieInfo *c; FILE *fp = NULL; bool fromfile = TRUE; char *line = NULL; if(NULL == inc) { /* we didn't get a struct, create one */ c = calloc(1, sizeof(struct CookieInfo)); if(!c) return NULL; /* failed to get memory */ c->filename = strdup(file?file:"none"); /* copy the name just in case */ if(!c->filename) goto fail; /* failed to get memory */ } else { /* we got an already existing one, use that */ c = inc; } c->running = FALSE; /* this is not running, this is init */ if(file && !strcmp(file, "-")) { fp = stdin; fromfile = FALSE; } else if(file && !*file) { /* points to a "" string */ fp = NULL; } else fp = file?fopen(file, FOPEN_READTEXT):NULL; c->newsession = newsession; /* new session? */ if(fp) { char *lineptr; bool headerline; line = malloc(MAX_COOKIE_LINE); if(!line) goto fail; while(get_line(line, MAX_COOKIE_LINE, fp)) { if(checkprefix("Set-Cookie:", line)) { /* This is a cookie line, get it! */ lineptr = &line[11]; headerline = TRUE; } else { lineptr = line; headerline = FALSE; } while(*lineptr && ISBLANK(*lineptr)) lineptr++; Curl_cookie_add(data, c, headerline, TRUE, lineptr, NULL, NULL); } free(line); /* free the line buffer */ remove_expired(c); /* run this once, not on every cookie */ if(fromfile) fclose(fp); } c->running = TRUE; /* now, we're running */ return c; fail: free(line); if(!inc) /* Only clean up if we allocated it here, as the original could still be in * use by a share handle */ Curl_cookie_cleanup(c); if(fromfile && fp) fclose(fp); return NULL; /* out of memory */ }