cookie.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. /***
  24. RECEIVING COOKIE INFORMATION
  25. ============================
  26. struct CookieInfo *cookie_init(char *file);
  27. Inits a cookie struct to store data in a local file. This is always
  28. called before any cookies are set.
  29. int cookies_set(struct CookieInfo *cookie, char *cookie_line);
  30. The 'cookie_line' parameter is a full "Set-cookie:" line as
  31. received from a server.
  32. The function need to replace previously stored lines that this new
  33. line superceeds.
  34. It may remove lines that are expired.
  35. It should return an indication of success/error.
  36. SENDING COOKIE INFORMATION
  37. ==========================
  38. struct Cookies *cookie_getlist(struct CookieInfo *cookie,
  39. char *host, char *path, bool secure);
  40. For a given host and path, return a linked list of cookies that
  41. the client should send to the server if used now. The secure
  42. boolean informs the cookie if a secure connection is achieved or
  43. not.
  44. It shall only return cookies that haven't expired.
  45. Example set of cookies:
  46. Set-cookie: PRODUCTINFO=webxpress; domain=.fidelity.com; path=/; secure
  47. Set-cookie: PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  48. domain=.fidelity.com; path=/ftgw; secure
  49. Set-cookie: FidHist=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  50. domain=.fidelity.com; path=/; secure
  51. Set-cookie: FidOrder=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  52. domain=.fidelity.com; path=/; secure
  53. Set-cookie: DisPend=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  54. domain=.fidelity.com; path=/; secure
  55. Set-cookie: FidDis=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  56. domain=.fidelity.com; path=/; secure
  57. Set-cookie:
  58. Session_Key@6791a9e0-901a-11d0-a1c8-9b012c88aa77=none;expires=Monday,
  59. 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/; secure
  60. ****/
  61. #include "setup.h"
  62. #ifndef CURL_DISABLE_HTTP
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <ctype.h>
  66. #include "urldata.h"
  67. #include "cookie.h"
  68. #include "getdate.h"
  69. #include "strequal.h"
  70. #include "strtok.h"
  71. #include "sendf.h"
  72. /* The last #include file should be: */
  73. #ifdef CURLDEBUG
  74. #include "memdebug.h"
  75. #endif
  76. static void
  77. free_cookiemess(struct Cookie *co)
  78. {
  79. if(co->domain)
  80. free(co->domain);
  81. if(co->path)
  82. free(co->path);
  83. if(co->name)
  84. free(co->name);
  85. if(co->value)
  86. free(co->value);
  87. free(co);
  88. }
  89. static bool tailmatch(const char *little, const char *bigone)
  90. {
  91. size_t littlelen = strlen(little);
  92. size_t biglen = strlen(bigone);
  93. if(littlelen > biglen)
  94. return FALSE;
  95. return (bool)strequal(little, bigone+biglen-littlelen);
  96. }
  97. /****************************************************************************
  98. *
  99. * Curl_cookie_add()
  100. *
  101. * Add a single cookie line to the cookie keeping object.
  102. *
  103. ***************************************************************************/
  104. struct Cookie *
  105. Curl_cookie_add(struct SessionHandle *data,
  106. /* The 'data' pointer here may be NULL at times, and thus
  107. must only be used very carefully for things that can deal
  108. with data being NULL. Such as infof() and similar */
  109. struct CookieInfo *c,
  110. bool httpheader, /* TRUE if HTTP header-style line */
  111. char *lineptr, /* first character of the line */
  112. char *domain, /* default domain */
  113. char *path) /* full path used when this cookie is set,
  114. used to get default path for the cookie
  115. unless set */
  116. {
  117. struct Cookie *clist;
  118. char what[MAX_COOKIE_LINE];
  119. char name[MAX_NAME];
  120. char *ptr;
  121. char *semiptr;
  122. struct Cookie *co;
  123. struct Cookie *lastc=NULL;
  124. time_t now = time(NULL);
  125. bool replace_old = FALSE;
  126. bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */
  127. /* First, alloc and init a new struct for it */
  128. co = (struct Cookie *)calloc(sizeof(struct Cookie), 1);
  129. if(!co)
  130. return NULL; /* bail out if we're this low on memory */
  131. if(httpheader) {
  132. /* This line was read off a HTTP-header */
  133. char *sep;
  134. semiptr=strchr(lineptr, ';'); /* first, find a semicolon */
  135. while(*lineptr && isspace((int)*lineptr))
  136. lineptr++;
  137. ptr = lineptr;
  138. do {
  139. /* we have a <what>=<this> pair or a 'secure' word here */
  140. sep = strchr(ptr, '=');
  141. if(sep && (!semiptr || (semiptr>sep)) ) {
  142. /*
  143. * There is a = sign and if there was a semicolon too, which make sure
  144. * that the semicolon comes _after_ the equal sign.
  145. */
  146. name[0]=what[0]=0; /* init the buffers */
  147. if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;=]=%"
  148. MAX_COOKIE_LINE_TXT "[^;\r\n]",
  149. name, what)) {
  150. /* this is a <name>=<what> pair */
  151. char *whatptr;
  152. /* Strip off trailing whitespace from the 'what' */
  153. size_t len=strlen(what);
  154. while(len && isspace((int)what[len-1])) {
  155. what[len-1]=0;
  156. len--;
  157. }
  158. /* Skip leading whitespace from the 'what' */
  159. whatptr=what;
  160. while(isspace((int)*whatptr)) {
  161. whatptr++;
  162. }
  163. if(strequal("path", name)) {
  164. co->path=strdup(whatptr);
  165. }
  166. else if(strequal("domain", name)) {
  167. /* note that this name may or may not have a preceeding dot, but
  168. we don't care about that, we treat the names the same anyway */
  169. const char *domptr=whatptr;
  170. int dotcount=1;
  171. unsigned int i;
  172. static const char *seventhree[]= {
  173. "com", "edu", "net", "org", "gov", "mil", "int"
  174. };
  175. /* Count the dots, we need to make sure that there are THREE dots
  176. in the normal domains, or TWO in the seventhree-domains. */
  177. if('.' == whatptr[0])
  178. /* don't count the initial dot, assume it */
  179. domptr++;
  180. do {
  181. domptr = strchr(domptr, '.');
  182. if(domptr) {
  183. domptr++;
  184. dotcount++;
  185. }
  186. } while(domptr);
  187. for(i=0;
  188. i<sizeof(seventhree)/sizeof(seventhree[0]); i++) {
  189. if(tailmatch(seventhree[i], whatptr)) {
  190. dotcount++; /* we allow one dot less for these */
  191. break;
  192. }
  193. }
  194. /* The original Netscape cookie spec defined that this domain name
  195. MUST have three dots (or two if one of the seven holy TLDs),
  196. but it seems that these kinds of cookies are in use "out there"
  197. so we cannot be that strict. I've therefore lowered the check
  198. to not allow less than two dots. */
  199. if(dotcount < 2) {
  200. /* Received and skipped a cookie with a domain using too few
  201. dots. */
  202. badcookie=TRUE; /* mark this as a bad cookie */
  203. infof(data, "skipped cookie with illegal dotcount domain: %s",
  204. whatptr);
  205. }
  206. else {
  207. /* Now, we make sure that our host is within the given domain,
  208. or the given domain is not valid and thus cannot be set. */
  209. if(!domain || tailmatch(whatptr, domain)) {
  210. const char *tailptr=whatptr;
  211. if(tailptr[0] == '.')
  212. tailptr++;
  213. co->domain=strdup(tailptr); /* don't prefix w/dots internally */
  214. co->tailmatch=TRUE; /* we always do that if the domain name was
  215. given */
  216. }
  217. else {
  218. /* we did not get a tailmatch and then the attempted set domain
  219. is not a domain to which the current host belongs. Mark as
  220. bad. */
  221. badcookie=TRUE;
  222. infof(data, "skipped cookie with bad tailmatch domain: %s",
  223. whatptr);
  224. }
  225. }
  226. }
  227. else if(strequal("version", name)) {
  228. co->version=strdup(whatptr);
  229. }
  230. else if(strequal("max-age", name)) {
  231. /* Defined in RFC2109:
  232. Optional. The Max-Age attribute defines the lifetime of the
  233. cookie, in seconds. The delta-seconds value is a decimal non-
  234. negative integer. After delta-seconds seconds elapse, the
  235. client should discard the cookie. A value of zero means the
  236. cookie should be discarded immediately.
  237. */
  238. co->maxage = strdup(whatptr);
  239. co->expires =
  240. atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + now;
  241. }
  242. else if(strequal("expires", name)) {
  243. co->expirestr=strdup(whatptr);
  244. co->expires = curl_getdate(what, &now);
  245. }
  246. else if(!co->name) {
  247. co->name = strdup(name);
  248. co->value = strdup(whatptr);
  249. }
  250. /*
  251. else this is the second (or more) name we don't know
  252. about! */
  253. }
  254. else {
  255. /* this is an "illegal" <what>=<this> pair */
  256. }
  257. }
  258. else {
  259. if(sscanf(ptr, "%" MAX_COOKIE_LINE_TXT "[^;\r\n]",
  260. what)) {
  261. if(strequal("secure", what))
  262. co->secure = TRUE;
  263. /* else,
  264. unsupported keyword without assign! */
  265. }
  266. }
  267. if(!semiptr || !*semiptr) {
  268. /* we already know there are no more cookies */
  269. semiptr = NULL;
  270. continue;
  271. }
  272. ptr=semiptr+1;
  273. while(ptr && *ptr && isspace((int)*ptr))
  274. ptr++;
  275. semiptr=strchr(ptr, ';'); /* now, find the next semicolon */
  276. if(!semiptr && *ptr)
  277. /* There are no more semicolons, but there's a final name=value pair
  278. coming up */
  279. semiptr=strchr(ptr, '\0');
  280. } while(semiptr);
  281. if(badcookie || (NULL == co->name)) {
  282. /* we didn't get a cookie name or a bad one,
  283. this is an illegal line, bail out */
  284. if(co->expirestr)
  285. free(co->expirestr);
  286. if(co->domain)
  287. free(co->domain);
  288. if(co->path)
  289. free(co->path);
  290. if(co->name)
  291. free(co->name);
  292. if(co->value)
  293. free(co->value);
  294. free(co);
  295. return NULL;
  296. }
  297. if(NULL == co->domain)
  298. /* no domain was given in the header line, set the default now */
  299. co->domain=domain?strdup(domain):NULL;
  300. if((NULL == co->path) && path) {
  301. /* no path was given in the header line, set the default now */
  302. char *endslash = strrchr(path, '/');
  303. if(endslash) {
  304. size_t pathlen = endslash-path+1; /* include the ending slash */
  305. co->path=malloc(pathlen+1); /* one extra for the zero byte */
  306. if(co->path) {
  307. memcpy(co->path, path, pathlen);
  308. co->path[pathlen]=0; /* zero terminate */
  309. }
  310. }
  311. }
  312. }
  313. else {
  314. /* This line is NOT a HTTP header style line, we do offer support for
  315. reading the odd netscape cookies-file format here */
  316. char *firstptr;
  317. char *tok_buf;
  318. int fields;
  319. if(lineptr[0]=='#') {
  320. /* don't even try the comments */
  321. free(co);
  322. return NULL;
  323. }
  324. /* strip off the possible end-of-line characters */
  325. ptr=strchr(lineptr, '\r');
  326. if(ptr)
  327. *ptr=0; /* clear it */
  328. ptr=strchr(lineptr, '\n');
  329. if(ptr)
  330. *ptr=0; /* clear it */
  331. firstptr=strtok_r(lineptr, "\t", &tok_buf); /* first tokenize it on the TAB */
  332. /* Here's a quick check to eliminate normal HTTP-headers from this */
  333. if(!firstptr || strchr(firstptr, ':')) {
  334. free(co);
  335. return NULL;
  336. }
  337. /* Now loop through the fields and init the struct we already have
  338. allocated */
  339. for(ptr=firstptr, fields=0; ptr;
  340. ptr=strtok_r(NULL, "\t", &tok_buf), fields++) {
  341. switch(fields) {
  342. case 0:
  343. if(ptr[0]=='.') /* skip preceeding dots */
  344. ptr++;
  345. co->domain = strdup(ptr);
  346. break;
  347. case 1:
  348. /* This field got its explanation on the 23rd of May 2001 by
  349. Andrés García:
  350. flag: A TRUE/FALSE value indicating if all machines within a given
  351. domain can access the variable. This value is set automatically by
  352. the browser, depending on the value you set for the domain.
  353. As far as I can see, it is set to true when the cookie says
  354. .domain.com and to false when the domain is complete www.domain.com
  355. */
  356. co->tailmatch=(bool)strequal(ptr, "TRUE"); /* store information */
  357. break;
  358. case 2:
  359. /* It turns out, that sometimes the file format allows the path
  360. field to remain not filled in, we try to detect this and work
  361. around it! Andrés García made us aware of this... */
  362. if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) {
  363. /* only if the path doesn't look like a boolean option! */
  364. co->path = strdup(ptr);
  365. break;
  366. }
  367. /* this doesn't look like a path, make one up! */
  368. co->path = strdup("/");
  369. fields++; /* add a field and fall down to secure */
  370. /* FALLTHROUGH */
  371. case 3:
  372. co->secure = (bool)strequal(ptr, "TRUE");
  373. break;
  374. case 4:
  375. co->expires = atoi(ptr);
  376. break;
  377. case 5:
  378. co->name = strdup(ptr);
  379. break;
  380. case 6:
  381. co->value = strdup(ptr);
  382. break;
  383. }
  384. }
  385. if(6 == fields) {
  386. /* we got a cookie with blank contents, fix it */
  387. co->value = strdup("");
  388. }
  389. else if(7 != fields) {
  390. /* we did not find the sufficient number of fields to recognize this
  391. as a valid line, abort and go home */
  392. free_cookiemess(co);
  393. return NULL;
  394. }
  395. }
  396. if(!c->running && /* read from a file */
  397. c->newsession && /* clean session cookies */
  398. !co->expires) { /* this is a session cookie since it doesn't expire! */
  399. free_cookiemess(co);
  400. return NULL;
  401. }
  402. co->livecookie = c->running;
  403. /* now, we have parsed the incoming line, we must now check if this
  404. superceeds an already existing cookie, which it may if the previous have
  405. the same domain and path as this */
  406. clist = c->cookies;
  407. replace_old = FALSE;
  408. while(clist) {
  409. if(strequal(clist->name, co->name)) {
  410. /* the names are identical */
  411. if(clist->domain && co->domain) {
  412. if(strequal(clist->domain, co->domain))
  413. /* The domains are identical */
  414. replace_old=TRUE;
  415. }
  416. else if(!clist->domain && !co->domain)
  417. replace_old = TRUE;
  418. if(replace_old) {
  419. /* the domains were identical */
  420. if(clist->path && co->path) {
  421. if(strequal(clist->path, co->path)) {
  422. replace_old = TRUE;
  423. }
  424. else
  425. replace_old = FALSE;
  426. }
  427. else if(!clist->path && !co->path)
  428. replace_old = TRUE;
  429. else
  430. replace_old = FALSE;
  431. }
  432. if(replace_old && !co->livecookie && clist->livecookie) {
  433. /* Both cookies matched fine, except that the already present
  434. cookie is "live", which means it was set from a header, while
  435. the new one isn't "live" and thus only read from a file. We let
  436. live cookies stay alive */
  437. /* Free the newcomer and get out of here! */
  438. if(co->domain)
  439. free(co->domain);
  440. if(co->path)
  441. free(co->path);
  442. if(co->name)
  443. free(co->name);
  444. if(co->value)
  445. free(co->value);
  446. free(co);
  447. return NULL;
  448. }
  449. if(replace_old) {
  450. co->next = clist->next; /* get the next-pointer first */
  451. /* then free all the old pointers */
  452. if(clist->name)
  453. free(clist->name);
  454. if(clist->value)
  455. free(clist->value);
  456. if(clist->domain)
  457. free(clist->domain);
  458. if(clist->path)
  459. free(clist->path);
  460. if(clist->expirestr)
  461. free(clist->expirestr);
  462. if(clist->version)
  463. free(clist->version);
  464. if(clist->maxage)
  465. free(clist->maxage);
  466. *clist = *co; /* then store all the new data */
  467. free(co); /* free the newly alloced memory */
  468. co = clist; /* point to the previous struct instead */
  469. /* We have replaced a cookie, now skip the rest of the list but
  470. make sure the 'lastc' pointer is properly set */
  471. do {
  472. lastc = clist;
  473. clist = clist->next;
  474. } while(clist);
  475. break;
  476. }
  477. }
  478. lastc = clist;
  479. clist = clist->next;
  480. }
  481. if(c->running)
  482. /* Only show this when NOT reading the cookies from a file */
  483. infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, expire %d\n",
  484. replace_old?"Replaced":"Added", co->name, co->value,
  485. co->domain, co->path, co->expires);
  486. if(!replace_old) {
  487. /* then make the last item point on this new one */
  488. if(lastc)
  489. lastc->next = co;
  490. else
  491. c->cookies = co;
  492. }
  493. c->numcookies++; /* one more cookie in the jar */
  494. return co;
  495. }
  496. /*****************************************************************************
  497. *
  498. * Curl_cookie_init()
  499. *
  500. * Inits a cookie struct to read data from a local file. This is always
  501. * called before any cookies are set. File may be NULL.
  502. *
  503. * If 'newsession' is TRUE, discard all "session cookies" on read from file.
  504. *
  505. ****************************************************************************/
  506. struct CookieInfo *Curl_cookie_init(struct SessionHandle *data,
  507. char *file,
  508. struct CookieInfo *inc,
  509. bool newsession)
  510. {
  511. char line[MAX_COOKIE_LINE];
  512. struct CookieInfo *c;
  513. FILE *fp;
  514. bool fromfile=TRUE;
  515. if(NULL == inc) {
  516. /* we didn't get a struct, create one */
  517. c = (struct CookieInfo *)malloc(sizeof(struct CookieInfo));
  518. if(!c)
  519. return NULL; /* failed to get memory */
  520. memset(c, 0, sizeof(struct CookieInfo));
  521. c->filename = strdup(file?file:"none"); /* copy the name just in case */
  522. }
  523. else {
  524. /* we got an already existing one, use that */
  525. c = inc;
  526. }
  527. c->running = FALSE; /* this is not running, this is init */
  528. if(file && strequal(file, "-")) {
  529. fp = stdin;
  530. fromfile=FALSE;
  531. }
  532. else
  533. fp = file?fopen(file, "r"):NULL;
  534. c->newsession = newsession; /* new session? */
  535. if(fp) {
  536. char *lineptr;
  537. bool headerline;
  538. while(fgets(line, MAX_COOKIE_LINE, fp)) {
  539. if(checkprefix("Set-Cookie:", line)) {
  540. /* This is a cookie line, get it! */
  541. lineptr=&line[11];
  542. headerline=TRUE;
  543. }
  544. else {
  545. lineptr=line;
  546. headerline=FALSE;
  547. }
  548. while(*lineptr && isspace((int)*lineptr))
  549. lineptr++;
  550. Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL);
  551. }
  552. if(fromfile)
  553. fclose(fp);
  554. }
  555. c->running = TRUE; /* now, we're running */
  556. return c;
  557. }
  558. /*****************************************************************************
  559. *
  560. * Curl_cookie_getlist()
  561. *
  562. * For a given host and path, return a linked list of cookies that the
  563. * client should send to the server if used now. The secure boolean informs
  564. * the cookie if a secure connection is achieved or not.
  565. *
  566. * It shall only return cookies that haven't expired.
  567. *
  568. ****************************************************************************/
  569. struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
  570. char *host, char *path, bool secure)
  571. {
  572. struct Cookie *newco;
  573. struct Cookie *co;
  574. time_t now = time(NULL);
  575. struct Cookie *mainco=NULL;
  576. if(!c || !c->cookies)
  577. return NULL; /* no cookie struct or no cookies in the struct */
  578. co = c->cookies;
  579. while(co) {
  580. /* only process this cookie if it is not expired or had no expire
  581. date AND that if the cookie requires we're secure we must only
  582. continue if we are! */
  583. if( (co->expires<=0 || (co->expires> now)) &&
  584. (co->secure?secure:TRUE) ) {
  585. /* now check if the domain is correct */
  586. if(!co->domain ||
  587. (co->tailmatch && tailmatch(co->domain, host)) ||
  588. (!co->tailmatch && strequal(host, co->domain)) ) {
  589. /* the right part of the host matches the domain stuff in the
  590. cookie data */
  591. /* now check the left part of the path with the cookies path
  592. requirement */
  593. if(!co->path ||
  594. checkprefix(co->path, path) ) {
  595. /* and now, we know this is a match and we should create an
  596. entry for the return-linked-list */
  597. newco = (struct Cookie *)malloc(sizeof(struct Cookie));
  598. if(newco) {
  599. /* first, copy the whole source cookie: */
  600. memcpy(newco, co, sizeof(struct Cookie));
  601. /* then modify our next */
  602. newco->next = mainco;
  603. /* point the main to us */
  604. mainco = newco;
  605. }
  606. }
  607. }
  608. }
  609. co = co->next;
  610. }
  611. return mainco; /* return the new list */
  612. }
  613. /*****************************************************************************
  614. *
  615. * Curl_cookie_freelist()
  616. *
  617. * Free a list of cookies previously returned by Curl_cookie_getlist();
  618. *
  619. ****************************************************************************/
  620. void Curl_cookie_freelist(struct Cookie *co)
  621. {
  622. struct Cookie *next;
  623. if(co) {
  624. while(co) {
  625. next = co->next;
  626. free(co); /* we only free the struct since the "members" are all
  627. just copied! */
  628. co = next;
  629. }
  630. }
  631. }
  632. /*****************************************************************************
  633. *
  634. * Curl_cookie_cleanup()
  635. *
  636. * Free a "cookie object" previous created with cookie_init().
  637. *
  638. ****************************************************************************/
  639. void Curl_cookie_cleanup(struct CookieInfo *c)
  640. {
  641. struct Cookie *co;
  642. struct Cookie *next;
  643. if(c) {
  644. if(c->filename)
  645. free(c->filename);
  646. co = c->cookies;
  647. while(co) {
  648. if(co->name)
  649. free(co->name);
  650. if(co->value)
  651. free(co->value);
  652. if(co->domain)
  653. free(co->domain);
  654. if(co->path)
  655. free(co->path);
  656. if(co->expirestr)
  657. free(co->expirestr);
  658. if(co->version)
  659. free(co->version);
  660. if(co->maxage)
  661. free(co->maxage);
  662. next = co->next;
  663. free(co);
  664. co = next;
  665. }
  666. free(c); /* free the base struct as well */
  667. }
  668. }
  669. /*
  670. * Curl_cookie_output()
  671. *
  672. * Writes all internally known cookies to the specified file. Specify
  673. * "-" as file name to write to stdout.
  674. *
  675. * The function returns non-zero on write failure.
  676. */
  677. int Curl_cookie_output(struct CookieInfo *c, char *dumphere)
  678. {
  679. struct Cookie *co;
  680. FILE *out;
  681. bool use_stdout=FALSE;
  682. if((NULL == c) || (0 == c->numcookies))
  683. /* If there are no known cookies, we don't write or even create any
  684. destination file */
  685. return 0;
  686. if(strequal("-", dumphere)) {
  687. /* use stdout */
  688. out = stdout;
  689. use_stdout=TRUE;
  690. }
  691. else {
  692. out = fopen(dumphere, "w");
  693. if(!out)
  694. return 1; /* failure */
  695. }
  696. if(c) {
  697. fputs("# Netscape HTTP Cookie File\n"
  698. "# http://www.netscape.com/newsref/std/cookie_spec.html\n"
  699. "# This file was generated by libcurl! Edit at your own risk.\n\n",
  700. out);
  701. co = c->cookies;
  702. while(co) {
  703. fprintf(out,
  704. "%s%s\t" /* domain */
  705. "%s\t" /* tailmatch */
  706. "%s\t" /* path */
  707. "%s\t" /* secure */
  708. "%u\t" /* expires */
  709. "%s\t" /* name */
  710. "%s\n", /* value */
  711. /* Make sure all domains are prefixed with a dot if they allow
  712. tailmatching. This is Mozilla-style. */
  713. (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
  714. co->domain?co->domain:"unknown",
  715. co->tailmatch?"TRUE":"FALSE",
  716. co->path?co->path:"/",
  717. co->secure?"TRUE":"FALSE",
  718. (unsigned int)co->expires,
  719. co->name,
  720. co->value?co->value:"");
  721. co=co->next;
  722. }
  723. }
  724. if(!use_stdout)
  725. fclose(out);
  726. return 0;
  727. }
  728. #endif /* CURL_DISABLE_HTTP */