Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Daniel Stenberg | 277df1c | 2007-01-13 23:32:14 +0000 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al. |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 9 | * |
Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 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. |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 13 | * |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 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 |
Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 16 | * furnished to do so, under the terms of the COPYING file. |
Daniel Stenberg | 24dee48 | 2001-01-03 09:29:33 +0000 | [diff] [blame] | 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | * $Id$ |
Daniel Stenberg | ba4e69b | 2002-09-03 11:52:59 +0000 | [diff] [blame] | 22 | ***************************************************************************/ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 23 | |
| 24 | /*** |
| 25 | |
| 26 | |
| 27 | RECEIVING COOKIE INFORMATION |
| 28 | ============================ |
| 29 | |
| 30 | struct CookieInfo *cookie_init(char *file); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 31 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 32 | Inits a cookie struct to store data in a local file. This is always |
| 33 | called before any cookies are set. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 34 | |
| 35 | int cookies_set(struct CookieInfo *cookie, char *cookie_line); |
| 36 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 37 | The 'cookie_line' parameter is a full "Set-cookie:" line as |
| 38 | received from a server. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 39 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 40 | The function need to replace previously stored lines that this new |
| 41 | line superceeds. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 42 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 43 | It may remove lines that are expired. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 44 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 45 | It should return an indication of success/error. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | SENDING COOKIE INFORMATION |
| 49 | ========================== |
| 50 | |
| 51 | struct Cookies *cookie_getlist(struct CookieInfo *cookie, |
| 52 | char *host, char *path, bool secure); |
| 53 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 54 | For a given host and path, return a linked list of cookies that |
| 55 | the client should send to the server if used now. The secure |
| 56 | boolean informs the cookie if a secure connection is achieved or |
| 57 | not. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 58 | |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 59 | It shall only return cookies that haven't expired. |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 60 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 61 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 62 | Example set of cookies: |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 63 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 64 | Set-cookie: PRODUCTINFO=webxpress; domain=.fidelity.com; path=/; secure |
| 65 | Set-cookie: PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 66 | domain=.fidelity.com; path=/ftgw; secure |
| 67 | Set-cookie: FidHist=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 68 | domain=.fidelity.com; path=/; secure |
| 69 | Set-cookie: FidOrder=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 70 | domain=.fidelity.com; path=/; secure |
| 71 | Set-cookie: DisPend=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 72 | domain=.fidelity.com; path=/; secure |
| 73 | Set-cookie: FidDis=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; |
| 74 | domain=.fidelity.com; path=/; secure |
| 75 | Set-cookie: |
| 76 | Session_Key@6791a9e0-901a-11d0-a1c8-9b012c88aa77=none;expires=Monday, |
| 77 | 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/; secure |
| 78 | ****/ |
| 79 | |
Sterling Hughes | ad6fca2 | 2003-03-31 15:59:17 +0000 | [diff] [blame] | 80 | |
Daniel Stenberg | b6e18f2 | 2000-08-24 14:26:33 +0000 | [diff] [blame] | 81 | #include "setup.h" |
| 82 | |
Daniel Stenberg | ac269a8 | 2004-12-05 23:59:32 +0000 | [diff] [blame] | 83 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) |
Daniel Stenberg | 08ef208 | 2002-06-11 11:13:01 +0000 | [diff] [blame] | 84 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 85 | #include <stdlib.h> |
| 86 | #include <string.h> |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 87 | |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 88 | #define _MPRINTF_REPLACE /* without this on windows OS we get undefined reference to snprintf */ |
| 89 | #include <curl/mprintf.h> |
| 90 | |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 91 | #include "urldata.h" |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 92 | #include "cookie.h" |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 93 | #include "strequal.h" |
Daniel Stenberg | 870bacd | 2001-05-30 11:06:56 +0000 | [diff] [blame] | 94 | #include "strtok.h" |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 95 | #include "sendf.h" |
Daniel Stenberg | bbafb2e | 2004-05-11 11:30:23 +0000 | [diff] [blame] | 96 | #include "memory.h" |
Daniel Stenberg | a676c18 | 2005-08-17 08:55:43 +0000 | [diff] [blame] | 97 | #include "share.h" |
| 98 | #include "strtoofft.h" |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 99 | |
Daniel Stenberg | 0f8facb | 2000-10-09 11:12:34 +0000 | [diff] [blame] | 100 | /* The last #include file should be: */ |
Daniel Stenberg | 2bd71d7 | 2003-06-26 06:50:32 +0000 | [diff] [blame] | 101 | #ifdef CURLDEBUG |
Daniel Stenberg | 0f8facb | 2000-10-09 11:12:34 +0000 | [diff] [blame] | 102 | #include "memdebug.h" |
| 103 | #endif |
| 104 | |
Daniel Stenberg | be7ce43 | 2004-10-03 21:02:01 +0000 | [diff] [blame] | 105 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 106 | static void freecookie(struct Cookie *co) |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 107 | { |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 108 | if(co->expirestr) |
| 109 | free(co->expirestr); |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 110 | if(co->domain) |
| 111 | free(co->domain); |
| 112 | if(co->path) |
| 113 | free(co->path); |
| 114 | if(co->name) |
| 115 | free(co->name); |
| 116 | if(co->value) |
| 117 | free(co->value); |
Daniel Stenberg | f40c9b8 | 2004-10-16 13:54:40 +0000 | [diff] [blame] | 118 | if(co->maxage) |
| 119 | free(co->maxage); |
Daniel Stenberg | 67abd4c | 2004-12-22 22:33:31 +0000 | [diff] [blame] | 120 | if(co->version) |
| 121 | free(co->version); |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 122 | |
| 123 | free(co); |
| 124 | } |
| 125 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 126 | static bool tailmatch(const char *little, const char *bigone) |
| 127 | { |
Daniel Stenberg | d571064 | 2004-02-26 13:40:43 +0000 | [diff] [blame] | 128 | size_t littlelen = strlen(little); |
| 129 | size_t biglen = strlen(bigone); |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 130 | |
| 131 | if(littlelen > biglen) |
| 132 | return FALSE; |
| 133 | |
Daniel Stenberg | 9948250 | 2004-03-10 09:41:37 +0000 | [diff] [blame] | 134 | return (bool)strequal(little, bigone+biglen-littlelen); |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Daniel Stenberg | a676c18 | 2005-08-17 08:55:43 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Load cookies from all given cookie files (CURLOPT_COOKIEFILE). |
| 139 | */ |
| 140 | void Curl_cookie_loadfiles(struct SessionHandle *data) |
| 141 | { |
| 142 | struct curl_slist *list = data->change.cookielist; |
| 143 | if(list) { |
| 144 | Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE); |
| 145 | while(list) { |
| 146 | data->cookies = Curl_cookie_init(data, |
| 147 | list->data, |
| 148 | data->cookies, |
| 149 | data->set.cookiesession); |
| 150 | list = list->next; |
| 151 | } |
| 152 | Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE); |
| 153 | curl_slist_free_all(data->change.cookielist); /* clean up list */ |
| 154 | data->change.cookielist = NULL; /* don't do this again! */ |
| 155 | } |
| 156 | } |
| 157 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 158 | /**************************************************************************** |
| 159 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 160 | * Curl_cookie_add() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 161 | * |
| 162 | * Add a single cookie line to the cookie keeping object. |
| 163 | * |
| 164 | ***************************************************************************/ |
| 165 | |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 166 | struct Cookie * |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 167 | Curl_cookie_add(struct SessionHandle *data, |
| 168 | /* The 'data' pointer here may be NULL at times, and thus |
| 169 | must only be used very carefully for things that can deal |
| 170 | with data being NULL. Such as infof() and similar */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 171 | |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 172 | struct CookieInfo *c, |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 173 | bool httpheader, /* TRUE if HTTP header-style line */ |
Daniel Stenberg | d8b2c81 | 2002-07-29 22:22:49 +0000 | [diff] [blame] | 174 | char *lineptr, /* first character of the line */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 175 | char *domain, /* default domain */ |
| 176 | char *path) /* full path used when this cookie is set, |
| 177 | used to get default path for the cookie |
| 178 | unless set */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 179 | { |
| 180 | struct Cookie *clist; |
Daniel Stenberg | 35558e6 | 2004-06-22 21:15:51 +0000 | [diff] [blame] | 181 | char *what; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 182 | char name[MAX_NAME]; |
| 183 | char *ptr; |
| 184 | char *semiptr; |
| 185 | struct Cookie *co; |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 186 | struct Cookie *lastc=NULL; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 187 | time_t now = time(NULL); |
| 188 | bool replace_old = FALSE; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 189 | bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 190 | |
Yang Tse | f08ac86 | 2007-03-25 02:30:58 +0000 | [diff] [blame] | 191 | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
| 192 | (void)data; |
| 193 | #endif |
| 194 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 195 | /* First, alloc and init a new struct for it */ |
Daniel Stenberg | 7d8cd59 | 2004-02-26 14:52:16 +0000 | [diff] [blame] | 196 | co = (struct Cookie *)calloc(sizeof(struct Cookie), 1); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 197 | if(!co) |
| 198 | return NULL; /* bail out if we're this low on memory */ |
| 199 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 200 | if(httpheader) { |
| 201 | /* This line was read off a HTTP-header */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 202 | char *sep; |
Daniel Stenberg | 35558e6 | 2004-06-22 21:15:51 +0000 | [diff] [blame] | 203 | |
| 204 | what = malloc(MAX_COOKIE_LINE); |
| 205 | if(!what) { |
| 206 | free(co); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 210 | semiptr=strchr(lineptr, ';'); /* first, find a semicolon */ |
Daniel Stenberg | d8b2c81 | 2002-07-29 22:22:49 +0000 | [diff] [blame] | 211 | |
Yang Tse | 6d05a33 | 2007-02-13 17:47:27 +0000 | [diff] [blame] | 212 | while(*lineptr && ISBLANK(*lineptr)) |
Daniel Stenberg | d8b2c81 | 2002-07-29 22:22:49 +0000 | [diff] [blame] | 213 | lineptr++; |
| 214 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 215 | ptr = lineptr; |
Daniel Stenberg | 28ad7dc | 2000-09-25 22:14:42 +0000 | [diff] [blame] | 216 | do { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 217 | /* we have a <what>=<this> pair or a 'secure' word here */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 218 | sep = strchr(ptr, '='); |
| 219 | if(sep && (!semiptr || (semiptr>sep)) ) { |
| 220 | /* |
| 221 | * There is a = sign and if there was a semicolon too, which make sure |
| 222 | * that the semicolon comes _after_ the equal sign. |
| 223 | */ |
| 224 | |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 225 | name[0]=what[0]=0; /* init the buffers */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 226 | if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;=]=%" |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 227 | MAX_COOKIE_LINE_TXT "[^;\r\n]", |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 228 | name, what)) { |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 229 | /* this is a <name>=<what> pair */ |
| 230 | |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 231 | char *whatptr; |
| 232 | |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 233 | /* Strip off trailing whitespace from the 'what' */ |
Daniel Stenberg | d571064 | 2004-02-26 13:40:43 +0000 | [diff] [blame] | 234 | size_t len=strlen(what); |
Yang Tse | 6d05a33 | 2007-02-13 17:47:27 +0000 | [diff] [blame] | 235 | while(len && ISBLANK(what[len-1])) { |
Daniel Stenberg | 3612c37 | 2002-02-27 07:38:04 +0000 | [diff] [blame] | 236 | what[len-1]=0; |
| 237 | len--; |
| 238 | } |
| 239 | |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 240 | /* Skip leading whitespace from the 'what' */ |
| 241 | whatptr=what; |
Yang Tse | 6d05a33 | 2007-02-13 17:47:27 +0000 | [diff] [blame] | 242 | while(*whatptr && ISBLANK(*whatptr)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 243 | whatptr++; |
| 244 | } |
| 245 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 246 | if(strequal("path", name)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 247 | co->path=strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 248 | if(!co->path) { |
| 249 | badcookie = TRUE; /* out of memory bad */ |
| 250 | break; |
| 251 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 252 | } |
| 253 | else if(strequal("domain", name)) { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 254 | /* note that this name may or may not have a preceeding dot, but |
| 255 | we don't care about that, we treat the names the same anyway */ |
| 256 | |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 257 | const char *domptr=whatptr; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 258 | int dotcount=1; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 259 | |
Daniel Stenberg | de6ab3d | 2004-08-04 12:26:27 +0000 | [diff] [blame] | 260 | /* Count the dots, we need to make sure that there are enough |
| 261 | of them. */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 262 | |
| 263 | if('.' == whatptr[0]) |
| 264 | /* don't count the initial dot, assume it */ |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 265 | domptr++; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 266 | |
| 267 | do { |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 268 | domptr = strchr(domptr, '.'); |
| 269 | if(domptr) { |
| 270 | domptr++; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 271 | dotcount++; |
| 272 | } |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 273 | } while(domptr); |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 274 | |
Daniel Stenberg | 98ee12b | 2003-08-04 23:05:57 +0000 | [diff] [blame] | 275 | /* The original Netscape cookie spec defined that this domain name |
| 276 | MUST have three dots (or two if one of the seven holy TLDs), |
| 277 | but it seems that these kinds of cookies are in use "out there" |
| 278 | so we cannot be that strict. I've therefore lowered the check |
| 279 | to not allow less than two dots. */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 280 | |
Daniel Stenberg | 98ee12b | 2003-08-04 23:05:57 +0000 | [diff] [blame] | 281 | if(dotcount < 2) { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 282 | /* Received and skipped a cookie with a domain using too few |
| 283 | dots. */ |
| 284 | badcookie=TRUE; /* mark this as a bad cookie */ |
Daniel Stenberg | 0e03165 | 2004-07-26 15:42:07 +0000 | [diff] [blame] | 285 | infof(data, "skipped cookie with illegal dotcount domain: %s\n", |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 286 | whatptr); |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 287 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 288 | else { |
| 289 | /* Now, we make sure that our host is within the given domain, |
| 290 | or the given domain is not valid and thus cannot be set. */ |
| 291 | |
Daniel Stenberg | 755f98e | 2004-05-21 20:40:15 +0000 | [diff] [blame] | 292 | if('.' == whatptr[0]) |
| 293 | whatptr++; /* ignore preceeding dot */ |
| 294 | |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 295 | if(!domain || tailmatch(whatptr, domain)) { |
Daniel Stenberg | 4d17d68 | 2004-01-29 13:56:45 +0000 | [diff] [blame] | 296 | const char *tailptr=whatptr; |
| 297 | if(tailptr[0] == '.') |
| 298 | tailptr++; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 299 | co->domain=strdup(tailptr); /* don't prefix w/dots |
| 300 | internally */ |
| 301 | if(!co->domain) { |
| 302 | badcookie = TRUE; |
| 303 | break; |
| 304 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 305 | co->tailmatch=TRUE; /* we always do that if the domain name was |
| 306 | given */ |
| 307 | } |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 308 | else { |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 309 | /* we did not get a tailmatch and then the attempted set domain |
| 310 | is not a domain to which the current host belongs. Mark as |
| 311 | bad. */ |
| 312 | badcookie=TRUE; |
Daniel Stenberg | 0e03165 | 2004-07-26 15:42:07 +0000 | [diff] [blame] | 313 | infof(data, "skipped cookie with bad tailmatch domain: %s\n", |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 314 | whatptr); |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 315 | } |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 316 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 317 | } |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 318 | else if(strequal("version", name)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 319 | co->version=strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 320 | if(!co->version) { |
| 321 | badcookie = TRUE; |
| 322 | break; |
| 323 | } |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 324 | } |
| 325 | else if(strequal("max-age", name)) { |
| 326 | /* Defined in RFC2109: |
| 327 | |
| 328 | Optional. The Max-Age attribute defines the lifetime of the |
| 329 | cookie, in seconds. The delta-seconds value is a decimal non- |
| 330 | negative integer. After delta-seconds seconds elapse, the |
| 331 | client should discard the cookie. A value of zero means the |
| 332 | cookie should be discarded immediately. |
| 333 | |
| 334 | */ |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 335 | co->maxage = strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 336 | if(!co->maxage) { |
| 337 | badcookie = TRUE; |
| 338 | break; |
| 339 | } |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 340 | co->expires = |
Daniel Stenberg | 6b1220b | 2005-04-26 13:08:49 +0000 | [diff] [blame] | 341 | atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + (long)now; |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 342 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 343 | else if(strequal("expires", name)) { |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 344 | co->expirestr=strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 345 | if(!co->expirestr) { |
| 346 | badcookie = TRUE; |
| 347 | break; |
| 348 | } |
Daniel Stenberg | 90e1a69 | 2005-08-17 09:11:27 +0000 | [diff] [blame] | 349 | co->expires = curl_getdate(what, &now); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 350 | } |
| 351 | else if(!co->name) { |
| 352 | co->name = strdup(name); |
Daniel Stenberg | 2361aab | 2002-04-14 18:21:17 +0000 | [diff] [blame] | 353 | co->value = strdup(whatptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 354 | if(!co->name || !co->value) { |
| 355 | badcookie = TRUE; |
| 356 | break; |
| 357 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 358 | } |
Daniel Stenberg | c892613 | 2001-08-14 08:17:29 +0000 | [diff] [blame] | 359 | /* |
| 360 | else this is the second (or more) name we don't know |
| 361 | about! */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 362 | } |
| 363 | else { |
| 364 | /* this is an "illegal" <what>=<this> pair */ |
| 365 | } |
| 366 | } |
| 367 | else { |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 368 | if(sscanf(ptr, "%" MAX_COOKIE_LINE_TXT "[^;\r\n]", |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 369 | what)) { |
| 370 | if(strequal("secure", what)) |
| 371 | co->secure = TRUE; |
Daniel Stenberg | c892613 | 2001-08-14 08:17:29 +0000 | [diff] [blame] | 372 | /* else, |
| 373 | unsupported keyword without assign! */ |
| 374 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 377 | if(!semiptr || !*semiptr) { |
| 378 | /* we already know there are no more cookies */ |
| 379 | semiptr = NULL; |
| 380 | continue; |
| 381 | } |
Daniel Stenberg | 28ad7dc | 2000-09-25 22:14:42 +0000 | [diff] [blame] | 382 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 383 | ptr=semiptr+1; |
Yang Tse | 6d05a33 | 2007-02-13 17:47:27 +0000 | [diff] [blame] | 384 | while(ptr && *ptr && ISBLANK(*ptr)) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 385 | ptr++; |
| 386 | semiptr=strchr(ptr, ';'); /* now, find the next semicolon */ |
Daniel Stenberg | 8703713 | 2002-01-07 23:05:36 +0000 | [diff] [blame] | 387 | |
| 388 | if(!semiptr && *ptr) |
| 389 | /* There are no more semicolons, but there's a final name=value pair |
| 390 | coming up */ |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 391 | semiptr=strchr(ptr, '\0'); |
Daniel Stenberg | 28ad7dc | 2000-09-25 22:14:42 +0000 | [diff] [blame] | 392 | } while(semiptr); |
Daniel Stenberg | 598e8df | 2001-09-26 07:08:29 +0000 | [diff] [blame] | 393 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 394 | if(!badcookie && !co->domain) { |
| 395 | if(domain) { |
| 396 | /* no domain was given in the header line, set the default */ |
| 397 | co->domain=strdup(domain); |
| 398 | if(!co->domain) |
| 399 | badcookie = TRUE; |
| 400 | } |
Daniel Stenberg | a23a897 | 2002-02-26 13:07:53 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 403 | if(!badcookie && !co->path && path) { |
| 404 | /* no path was given in the header line, set the default */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 405 | char *endslash = strrchr(path, '/'); |
| 406 | if(endslash) { |
Daniel Stenberg | d571064 | 2004-02-26 13:40:43 +0000 | [diff] [blame] | 407 | size_t pathlen = endslash-path+1; /* include the ending slash */ |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 408 | co->path=malloc(pathlen+1); /* one extra for the zero byte */ |
| 409 | if(co->path) { |
| 410 | memcpy(co->path, path, pathlen); |
| 411 | co->path[pathlen]=0; /* zero terminate */ |
| 412 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 413 | else |
| 414 | badcookie = TRUE; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 417 | |
Daniel Stenberg | 35558e6 | 2004-06-22 21:15:51 +0000 | [diff] [blame] | 418 | free(what); |
| 419 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 420 | if(badcookie || !co->name) { |
| 421 | /* we didn't get a cookie name or a bad one, |
| 422 | this is an illegal line, bail out */ |
| 423 | freecookie(co); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 427 | } |
| 428 | else { |
| 429 | /* This line is NOT a HTTP header style line, we do offer support for |
| 430 | reading the odd netscape cookies-file format here */ |
| 431 | char *firstptr; |
Daniel Stenberg | d567659 | 2001-05-29 19:17:39 +0000 | [diff] [blame] | 432 | char *tok_buf; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 433 | int fields; |
| 434 | |
| 435 | if(lineptr[0]=='#') { |
| 436 | /* don't even try the comments */ |
| 437 | free(co); |
| 438 | return NULL; |
| 439 | } |
| 440 | /* strip off the possible end-of-line characters */ |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 441 | ptr=strchr(lineptr, '\r'); |
| 442 | if(ptr) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 443 | *ptr=0; /* clear it */ |
Daniel Stenberg | 96dde76 | 2000-05-22 14:12:12 +0000 | [diff] [blame] | 444 | ptr=strchr(lineptr, '\n'); |
| 445 | if(ptr) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 446 | *ptr=0; /* clear it */ |
| 447 | |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 448 | firstptr=strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 449 | |
| 450 | /* Here's a quick check to eliminate normal HTTP-headers from this */ |
| 451 | if(!firstptr || strchr(firstptr, ':')) { |
| 452 | free(co); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | /* Now loop through the fields and init the struct we already have |
| 457 | allocated */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 458 | for(ptr=firstptr, fields=0; ptr && !badcookie; |
Daniel Stenberg | efd836d | 2003-04-30 17:03:43 +0000 | [diff] [blame] | 459 | ptr=strtok_r(NULL, "\t", &tok_buf), fields++) { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 460 | switch(fields) { |
| 461 | case 0: |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 462 | if(ptr[0]=='.') /* skip preceeding dots */ |
| 463 | ptr++; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 464 | co->domain = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 465 | if(!co->domain) |
| 466 | badcookie = TRUE; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 467 | break; |
| 468 | case 1: |
Daniel Stenberg | 72dec6c | 2001-05-23 13:04:19 +0000 | [diff] [blame] | 469 | /* This field got its explanation on the 23rd of May 2001 by |
| 470 | Andrés GarcÃa: |
| 471 | |
| 472 | flag: A TRUE/FALSE value indicating if all machines within a given |
| 473 | domain can access the variable. This value is set automatically by |
| 474 | the browser, depending on the value you set for the domain. |
| 475 | |
| 476 | As far as I can see, it is set to true when the cookie says |
| 477 | .domain.com and to false when the domain is complete www.domain.com |
Daniel Stenberg | 72dec6c | 2001-05-23 13:04:19 +0000 | [diff] [blame] | 478 | */ |
Daniel Stenberg | 9948250 | 2004-03-10 09:41:37 +0000 | [diff] [blame] | 479 | co->tailmatch=(bool)strequal(ptr, "TRUE"); /* store information */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 480 | break; |
| 481 | case 2: |
Daniel Stenberg | 8dc9f43 | 2001-05-23 09:26:45 +0000 | [diff] [blame] | 482 | /* It turns out, that sometimes the file format allows the path |
| 483 | field to remain not filled in, we try to detect this and work |
| 484 | around it! Andrés GarcÃa made us aware of this... */ |
| 485 | if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) { |
| 486 | /* only if the path doesn't look like a boolean option! */ |
| 487 | co->path = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 488 | if(!co->path) |
| 489 | badcookie = TRUE; |
Daniel Stenberg | 8dc9f43 | 2001-05-23 09:26:45 +0000 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | /* this doesn't look like a path, make one up! */ |
| 493 | co->path = strdup("/"); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 494 | if(!co->path) |
| 495 | badcookie = TRUE; |
Daniel Stenberg | 8dc9f43 | 2001-05-23 09:26:45 +0000 | [diff] [blame] | 496 | fields++; /* add a field and fall down to secure */ |
| 497 | /* FALLTHROUGH */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 498 | case 3: |
Daniel Stenberg | 9948250 | 2004-03-10 09:41:37 +0000 | [diff] [blame] | 499 | co->secure = (bool)strequal(ptr, "TRUE"); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 500 | break; |
| 501 | case 4: |
Daniel Stenberg | a676c18 | 2005-08-17 08:55:43 +0000 | [diff] [blame] | 502 | co->expires = curlx_strtoofft(ptr, NULL, 10); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 503 | break; |
| 504 | case 5: |
| 505 | co->name = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 506 | if(!co->name) |
| 507 | badcookie = TRUE; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 508 | break; |
| 509 | case 6: |
| 510 | co->value = strdup(ptr); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 511 | if(!co->value) |
| 512 | badcookie = TRUE; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | } |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 516 | if(6 == fields) { |
| 517 | /* we got a cookie with blank contents, fix it */ |
| 518 | co->value = strdup(""); |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 519 | if(!co->value) |
| 520 | badcookie = TRUE; |
| 521 | else |
| 522 | fields++; |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 523 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 524 | |
| 525 | if(!badcookie && (7 != fields)) |
| 526 | /* we did not find the sufficient number of fields */ |
| 527 | badcookie = TRUE; |
| 528 | |
| 529 | if(badcookie) { |
| 530 | freecookie(co); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 533 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 536 | if(!c->running && /* read from a file */ |
| 537 | c->newsession && /* clean session cookies */ |
| 538 | !co->expires) { /* this is a session cookie since it doesn't expire! */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 539 | freecookie(co); |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 540 | return NULL; |
| 541 | } |
| 542 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 543 | co->livecookie = c->running; |
| 544 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 545 | /* now, we have parsed the incoming line, we must now check if this |
| 546 | superceeds an already existing cookie, which it may if the previous have |
| 547 | the same domain and path as this */ |
| 548 | |
| 549 | clist = c->cookies; |
| 550 | replace_old = FALSE; |
| 551 | while(clist) { |
| 552 | if(strequal(clist->name, co->name)) { |
| 553 | /* the names are identical */ |
| 554 | |
| 555 | if(clist->domain && co->domain) { |
Daniel Stenberg | 465de79 | 2003-05-15 22:28:19 +0000 | [diff] [blame] | 556 | if(strequal(clist->domain, co->domain)) |
| 557 | /* The domains are identical */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 558 | replace_old=TRUE; |
| 559 | } |
| 560 | else if(!clist->domain && !co->domain) |
| 561 | replace_old = TRUE; |
| 562 | |
| 563 | if(replace_old) { |
| 564 | /* the domains were identical */ |
| 565 | |
| 566 | if(clist->path && co->path) { |
| 567 | if(strequal(clist->path, co->path)) { |
| 568 | replace_old = TRUE; |
| 569 | } |
| 570 | else |
| 571 | replace_old = FALSE; |
| 572 | } |
| 573 | else if(!clist->path && !co->path) |
| 574 | replace_old = TRUE; |
| 575 | else |
| 576 | replace_old = FALSE; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 577 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 580 | if(replace_old && !co->livecookie && clist->livecookie) { |
| 581 | /* Both cookies matched fine, except that the already present |
| 582 | cookie is "live", which means it was set from a header, while |
| 583 | the new one isn't "live" and thus only read from a file. We let |
| 584 | live cookies stay alive */ |
| 585 | |
| 586 | /* Free the newcomer and get out of here! */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 587 | freecookie(co); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
| 590 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 591 | if(replace_old) { |
| 592 | co->next = clist->next; /* get the next-pointer first */ |
| 593 | |
| 594 | /* then free all the old pointers */ |
| 595 | if(clist->name) |
| 596 | free(clist->name); |
| 597 | if(clist->value) |
| 598 | free(clist->value); |
| 599 | if(clist->domain) |
| 600 | free(clist->domain); |
| 601 | if(clist->path) |
| 602 | free(clist->path); |
| 603 | if(clist->expirestr) |
| 604 | free(clist->expirestr); |
| 605 | |
Daniel Stenberg | c6a8bb3 | 2000-02-01 23:54:51 +0000 | [diff] [blame] | 606 | if(clist->version) |
| 607 | free(clist->version); |
| 608 | if(clist->maxage) |
| 609 | free(clist->maxage); |
| 610 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 611 | *clist = *co; /* then store all the new data */ |
Daniel Stenberg | c6822f5 | 2001-10-24 11:36:55 +0000 | [diff] [blame] | 612 | |
| 613 | free(co); /* free the newly alloced memory */ |
| 614 | co = clist; /* point to the previous struct instead */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 615 | |
Daniel Stenberg | d9a7773 | 2002-01-07 14:56:15 +0000 | [diff] [blame] | 616 | /* We have replaced a cookie, now skip the rest of the list but |
| 617 | make sure the 'lastc' pointer is properly set */ |
| 618 | do { |
| 619 | lastc = clist; |
| 620 | clist = clist->next; |
| 621 | } while(clist); |
| 622 | break; |
| 623 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 624 | } |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 625 | lastc = clist; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 626 | clist = clist->next; |
| 627 | } |
| 628 | |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 629 | if(c->running) |
| 630 | /* Only show this when NOT reading the cookies from a file */ |
| 631 | infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, expire %d\n", |
| 632 | replace_old?"Replaced":"Added", co->name, co->value, |
| 633 | co->domain, co->path, co->expires); |
| 634 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 635 | if(!replace_old) { |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 636 | /* then make the last item point on this new one */ |
| 637 | if(lastc) |
| 638 | lastc->next = co; |
| 639 | else |
| 640 | c->cookies = co; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 641 | } |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 642 | |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 643 | c->numcookies++; /* one more cookie in the jar */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 644 | return co; |
| 645 | } |
| 646 | |
| 647 | /***************************************************************************** |
| 648 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 649 | * Curl_cookie_init() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 650 | * |
| 651 | * Inits a cookie struct to read data from a local file. This is always |
| 652 | * called before any cookies are set. File may be NULL. |
| 653 | * |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 654 | * If 'newsession' is TRUE, discard all "session cookies" on read from file. |
| 655 | * |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 656 | ****************************************************************************/ |
Daniel Stenberg | 168703b | 2003-08-11 09:55:11 +0000 | [diff] [blame] | 657 | struct CookieInfo *Curl_cookie_init(struct SessionHandle *data, |
| 658 | char *file, |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 659 | struct CookieInfo *inc, |
| 660 | bool newsession) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 661 | { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 662 | struct CookieInfo *c; |
| 663 | FILE *fp; |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 664 | bool fromfile=TRUE; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 665 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 666 | if(NULL == inc) { |
| 667 | /* we didn't get a struct, create one */ |
Daniel Stenberg | ce945bd | 2004-06-30 12:05:07 +0000 | [diff] [blame] | 668 | c = (struct CookieInfo *)calloc(1, sizeof(struct CookieInfo)); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 669 | if(!c) |
| 670 | return NULL; /* failed to get memory */ |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 671 | c->filename = strdup(file?file:"none"); /* copy the name just in case */ |
| 672 | } |
| 673 | else { |
| 674 | /* we got an already existing one, use that */ |
| 675 | c = inc; |
| 676 | } |
| 677 | c->running = FALSE; /* this is not running, this is init */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 678 | |
Daniel Stenberg | f2a2596 | 2001-10-10 12:48:32 +0000 | [diff] [blame] | 679 | if(file && strequal(file, "-")) { |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 680 | fp = stdin; |
| 681 | fromfile=FALSE; |
| 682 | } |
Daniel Stenberg | 6f752c6 | 2005-03-04 00:26:50 +0000 | [diff] [blame] | 683 | else if(file && !*file) { |
| 684 | /* points to a "" string */ |
| 685 | fp = NULL; |
| 686 | } |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 687 | else |
| 688 | fp = file?fopen(file, "r"):NULL; |
| 689 | |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 690 | c->newsession = newsession; /* new session? */ |
| 691 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 692 | if(fp) { |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 693 | char *lineptr; |
| 694 | bool headerline; |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 695 | |
Daniel Stenberg | ce945bd | 2004-06-30 12:05:07 +0000 | [diff] [blame] | 696 | char *line = (char *)malloc(MAX_COOKIE_LINE); |
| 697 | if(line) { |
| 698 | while(fgets(line, MAX_COOKIE_LINE, fp)) { |
| 699 | if(checkprefix("Set-Cookie:", line)) { |
| 700 | /* This is a cookie line, get it! */ |
| 701 | lineptr=&line[11]; |
| 702 | headerline=TRUE; |
| 703 | } |
| 704 | else { |
| 705 | lineptr=line; |
| 706 | headerline=FALSE; |
| 707 | } |
Yang Tse | 6d05a33 | 2007-02-13 17:47:27 +0000 | [diff] [blame] | 708 | while(*lineptr && ISBLANK(*lineptr)) |
Daniel Stenberg | ce945bd | 2004-06-30 12:05:07 +0000 | [diff] [blame] | 709 | lineptr++; |
| 710 | |
| 711 | Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL); |
| 712 | } |
| 713 | free(line); /* free the line buffer */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 714 | } |
Daniel Stenberg | 9280c20 | 2000-02-10 23:14:53 +0000 | [diff] [blame] | 715 | if(fromfile) |
| 716 | fclose(fp); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Daniel Stenberg | 980a47b | 2002-05-07 09:58:13 +0000 | [diff] [blame] | 719 | c->running = TRUE; /* now, we're running */ |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 720 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 721 | return c; |
| 722 | } |
| 723 | |
| 724 | /***************************************************************************** |
| 725 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 726 | * Curl_cookie_getlist() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 727 | * |
| 728 | * For a given host and path, return a linked list of cookies that the |
| 729 | * client should send to the server if used now. The secure boolean informs |
| 730 | * the cookie if a secure connection is achieved or not. |
| 731 | * |
| 732 | * It shall only return cookies that haven't expired. |
| 733 | * |
| 734 | ****************************************************************************/ |
| 735 | |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 736 | struct Cookie *Curl_cookie_getlist(struct CookieInfo *c, |
| 737 | char *host, char *path, bool secure) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 738 | { |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 739 | struct Cookie *newco; |
| 740 | struct Cookie *co; |
| 741 | time_t now = time(NULL); |
| 742 | struct Cookie *mainco=NULL; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 743 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 744 | if(!c || !c->cookies) |
| 745 | return NULL; /* no cookie struct or no cookies in the struct */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 746 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 747 | co = c->cookies; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 748 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 749 | while(co) { |
| 750 | /* only process this cookie if it is not expired or had no expire |
| 751 | date AND that if the cookie requires we're secure we must only |
| 752 | continue if we are! */ |
| 753 | if( (co->expires<=0 || (co->expires> now)) && |
| 754 | (co->secure?secure:TRUE) ) { |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 755 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 756 | /* now check if the domain is correct */ |
| 757 | if(!co->domain || |
| 758 | (co->tailmatch && tailmatch(co->domain, host)) || |
| 759 | (!co->tailmatch && strequal(host, co->domain)) ) { |
| 760 | /* the right part of the host matches the domain stuff in the |
| 761 | cookie data */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 762 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 763 | /* now check the left part of the path with the cookies path |
| 764 | requirement */ |
| 765 | if(!co->path || |
Daniel Stenberg | 2861170 | 2006-07-08 18:52:08 +0000 | [diff] [blame] | 766 | /* not using checkprefix() because matching should be |
| 767 | case-sensitive */ |
| 768 | !strncmp(co->path, path, strlen(co->path)) ) { |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 769 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 770 | /* and now, we know this is a match and we should create an |
| 771 | entry for the return-linked-list */ |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 772 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 773 | newco = (struct Cookie *)malloc(sizeof(struct Cookie)); |
| 774 | if(newco) { |
| 775 | /* first, copy the whole source cookie: */ |
| 776 | memcpy(newco, co, sizeof(struct Cookie)); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 777 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 778 | /* then modify our next */ |
| 779 | newco->next = mainco; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 780 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 781 | /* point the main to us */ |
| 782 | mainco = newco; |
| 783 | } |
| 784 | else { |
| 785 | /* failure, clear up the allocated chain and return NULL */ |
| 786 | while(mainco) { |
| 787 | co = mainco->next; |
| 788 | free(mainco); |
| 789 | mainco = co; |
| 790 | } |
Daniel Stenberg | 5dcab07 | 2004-05-10 14:04:06 +0000 | [diff] [blame] | 791 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 792 | return NULL; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | co = co->next; |
| 798 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 799 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 800 | return mainco; /* return the new list */ |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 803 | /***************************************************************************** |
| 804 | * |
| 805 | * Curl_cookie_clearall() |
| 806 | * |
| 807 | * Clear all existing cookies and reset the counter. |
| 808 | * |
| 809 | ****************************************************************************/ |
| 810 | void Curl_cookie_clearall(struct CookieInfo *cookies) |
| 811 | { |
Daniel Stenberg | 277df1c | 2007-01-13 23:32:14 +0000 | [diff] [blame] | 812 | if(cookies) { |
| 813 | Curl_cookie_freelist(cookies->cookies); |
| 814 | cookies->cookies = NULL; |
| 815 | cookies->numcookies = 0; |
| 816 | } |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 817 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 818 | |
| 819 | /***************************************************************************** |
| 820 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 821 | * Curl_cookie_freelist() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 822 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 823 | * Free a list of cookies previously returned by Curl_cookie_getlist(); |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 824 | * |
| 825 | ****************************************************************************/ |
| 826 | |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 827 | void Curl_cookie_freelist(struct Cookie *co) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 828 | { |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 829 | struct Cookie *next; |
| 830 | if(co) { |
| 831 | while(co) { |
| 832 | next = co->next; |
| 833 | free(co); /* we only free the struct since the "members" are all |
Daniel Stenberg | 39af394 | 2004-10-06 07:50:18 +0000 | [diff] [blame] | 834 | just copied! */ |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 835 | co = next; |
| 836 | } |
| 837 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 840 | |
| 841 | /***************************************************************************** |
| 842 | * |
| 843 | * Curl_cookie_clearsess() |
| 844 | * |
| 845 | * Free all session cookies in the cookies list. |
| 846 | * |
| 847 | ****************************************************************************/ |
| 848 | void Curl_cookie_clearsess(struct CookieInfo *cookies) |
| 849 | { |
| 850 | struct Cookie *first, *curr, *next, *prev = NULL; |
| 851 | |
| 852 | if(!cookies->cookies) |
| 853 | return; |
| 854 | |
| 855 | first = curr = prev = cookies->cookies; |
| 856 | |
| 857 | for(; curr; curr = next) { |
| 858 | next = curr->next; |
| 859 | if(!curr->expires) { |
| 860 | if(first == curr) |
| 861 | first = next; |
| 862 | |
| 863 | if(prev == curr) |
| 864 | prev = next; |
| 865 | else |
| 866 | prev->next = next; |
| 867 | |
| 868 | free(curr); |
| 869 | cookies->numcookies--; |
| 870 | } |
| 871 | else |
| 872 | prev = curr; |
| 873 | } |
| 874 | |
| 875 | cookies->cookies = first; |
| 876 | } |
| 877 | |
| 878 | |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 879 | /***************************************************************************** |
| 880 | * |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 881 | * Curl_cookie_cleanup() |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 882 | * |
| 883 | * Free a "cookie object" previous created with cookie_init(). |
| 884 | * |
| 885 | ****************************************************************************/ |
Daniel Stenberg | 4031104 | 2001-01-05 10:11:41 +0000 | [diff] [blame] | 886 | void Curl_cookie_cleanup(struct CookieInfo *c) |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 887 | { |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 888 | struct Cookie *co; |
| 889 | struct Cookie *next; |
| 890 | if(c) { |
| 891 | if(c->filename) |
| 892 | free(c->filename); |
| 893 | co = c->cookies; |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 894 | |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 895 | while(co) { |
| 896 | next = co->next; |
| 897 | freecookie(co); |
| 898 | co = next; |
| 899 | } |
| 900 | free(c); /* free the base struct as well */ |
| 901 | } |
Daniel Stenberg | ae1912c | 1999-12-29 14:20:26 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 904 | /* get_netscape_format() |
| 905 | * |
| 906 | * Formats a string for Netscape output file, w/o a newline at the end. |
| 907 | * |
| 908 | * Function returns a char * to a formatted line. Has to be free()d |
| 909 | */ |
| 910 | static char *get_netscape_format(const struct Cookie *co) |
| 911 | { |
Daniel Stenberg | 606562a | 2006-05-24 22:46:38 +0000 | [diff] [blame] | 912 | return aprintf( |
| 913 | "%s%s\t" /* domain */ |
| 914 | "%s\t" /* tailmatch */ |
| 915 | "%s\t" /* path */ |
| 916 | "%s\t" /* secure */ |
| 917 | "%" FORMAT_OFF_T "\t" /* expires */ |
| 918 | "%s\t" /* name */ |
| 919 | "%s", /* value */ |
| 920 | /* Make sure all domains are prefixed with a dot if they allow |
| 921 | tailmatching. This is Mozilla-style. */ |
| 922 | (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"", |
| 923 | co->domain?co->domain:"unknown", |
| 924 | co->tailmatch?"TRUE":"FALSE", |
| 925 | co->path?co->path:"/", |
| 926 | co->secure?"TRUE":"FALSE", |
| 927 | co->expires, |
| 928 | co->name, |
| 929 | co->value?co->value:""); |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 932 | /* |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 933 | * Curl_cookie_output() |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 934 | * |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 935 | * Writes all internally known cookies to the specified file. Specify |
| 936 | * "-" as file name to write to stdout. |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 937 | * |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 938 | * The function returns non-zero on write failure. |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 939 | */ |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 940 | int Curl_cookie_output(struct CookieInfo *c, char *dumphere) |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 941 | { |
| 942 | struct Cookie *co; |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 943 | FILE *out; |
| 944 | bool use_stdout=FALSE; |
| 945 | |
Daniel Stenberg | 3f5227d | 2001-09-10 07:43:08 +0000 | [diff] [blame] | 946 | if((NULL == c) || (0 == c->numcookies)) |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 947 | /* If there are no known cookies, we don't write or even create any |
| 948 | destination file */ |
| 949 | return 0; |
| 950 | |
| 951 | if(strequal("-", dumphere)) { |
| 952 | /* use stdout */ |
| 953 | out = stdout; |
| 954 | use_stdout=TRUE; |
| 955 | } |
| 956 | else { |
| 957 | out = fopen(dumphere, "w"); |
| 958 | if(!out) |
| 959 | return 1; /* failure */ |
| 960 | } |
| 961 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 962 | if(c) { |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 963 | char *format_ptr; |
| 964 | |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 965 | fputs("# Netscape HTTP Cookie File\n" |
Daniel Stenberg | 68e9f75 | 2006-09-27 21:00:45 +0000 | [diff] [blame] | 966 | "# http://curlm.haxx.se/rfc/cookie_spec.html\n" |
Daniel Stenberg | e719f41 | 2001-10-08 06:43:22 +0000 | [diff] [blame] | 967 | "# This file was generated by libcurl! Edit at your own risk.\n\n", |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 968 | out); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 969 | co = c->cookies; |
Daniel Stenberg | 34e8baa | 2004-05-12 12:04:38 +0000 | [diff] [blame] | 970 | |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 971 | while(co) { |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 972 | format_ptr = get_netscape_format(co); |
| 973 | if (format_ptr == NULL) { |
| 974 | fprintf(out, "#\n# Fatal libcurl error\n"); |
Dan Fandrich | 7e74349 | 2007-04-04 22:49:12 +0000 | [diff] [blame] | 975 | fclose(out); |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 976 | return 1; |
| 977 | } |
| 978 | fprintf(out, "%s\n", format_ptr); |
| 979 | free(format_ptr); |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 980 | co=co->next; |
| 981 | } |
| 982 | } |
Daniel Stenberg | a2b6ef3 | 2001-08-29 09:32:18 +0000 | [diff] [blame] | 983 | |
| 984 | if(!use_stdout) |
| 985 | fclose(out); |
| 986 | |
| 987 | return 0; |
Daniel Stenberg | c9c2115 | 2001-08-23 14:05:25 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 990 | struct curl_slist *Curl_cookie_list(struct SessionHandle *data) |
| 991 | { |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 992 | struct curl_slist *list = NULL; |
| 993 | struct curl_slist *beg; |
| 994 | struct Cookie *c; |
| 995 | char *line; |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 996 | |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 997 | if ((data->cookies == NULL) || |
| 998 | (data->cookies->numcookies == 0)) |
| 999 | return NULL; |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 1000 | |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 1001 | c = data->cookies->cookies; |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 1002 | |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 1003 | beg = list; |
| 1004 | while (c) { |
| 1005 | /* fill the list with _all_ the cookies we know */ |
| 1006 | line = get_netscape_format(c); |
| 1007 | if (line == NULL) { |
Yang Tse | d9e89e1 | 2007-04-07 04:51:35 +0000 | [diff] [blame] | 1008 | curl_slist_free_all(beg); |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 1009 | return NULL; |
| 1010 | } |
| 1011 | list = curl_slist_append(list, line); |
| 1012 | free(line); |
Yang Tse | d9e89e1 | 2007-04-07 04:51:35 +0000 | [diff] [blame] | 1013 | if (list == NULL) { |
| 1014 | curl_slist_free_all(beg); |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | else if (beg == NULL) { |
| 1018 | beg = list; |
| 1019 | } |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 1020 | c = c->next; |
| 1021 | } |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 1022 | |
Daniel Stenberg | 1c388a5 | 2005-07-28 21:49:58 +0000 | [diff] [blame] | 1023 | return list; |
Daniel Stenberg | 2236ba0 | 2005-07-27 22:17:14 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Daniel Stenberg | ac269a8 | 2004-12-05 23:59:32 +0000 | [diff] [blame] | 1026 | #endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */ |