2 * Wininet - cookie handling stuff
4 * Copyright 2002 TransGaming Technologies Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "wine/debug.h"
38 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
41 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
44 * Cookies are currently memory only.
45 * Cookies are NOT THREAD SAFE
46 * Cookies could use ALOT OF MEMORY. We need some kind of memory management here!
47 * Cookies should care about the expiry time
50 typedef struct _cookie_domain cookie_domain;
51 typedef struct _cookie cookie;
58 struct _cookie_domain *parent;
62 time_t expiry; /* FIXME: not used */
67 struct _cookie_domain *next;
68 struct _cookie_domain *prev;
75 static cookie_domain *cookieDomainTail;
77 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCSTR name, LPCSTR data);
78 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCSTR lpszCookieName);
79 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
80 static cookie_domain *COOKIE_addDomain(LPCSTR domain, LPCSTR path);
81 static cookie_domain *COOKIE_addDomainFromUrl(LPCSTR lpszUrl);
82 static cookie_domain *COOKIE_findNextDomain(LPCSTR lpszCookieDomain, LPCSTR lpszCookiePath,
83 cookie_domain *prev_domain, BOOL allow_partial);
84 static cookie_domain *COOKIE_findNextDomainFromUrl(LPCSTR lpszUrl, cookie_domain *prev_domain,
86 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
89 /* adds a cookie to the domain */
90 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCSTR name, LPCSTR data)
92 cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));
94 newCookie->next = NULL;
95 newCookie->prev = NULL;
96 newCookie->lpCookieName = NULL;
97 newCookie->lpCookieData = NULL;
101 newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1);
102 strcpy(newCookie->lpCookieName, name);
106 newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, strlen(data) + 1);
107 strcpy(newCookie->lpCookieData, data);
110 TRACE("added cookie %p (data is %s)\n", newCookie, data);
112 newCookie->prev = domain->cookie_tail;
113 newCookie->parent = domain;
114 domain->cookie_tail = newCookie;
119 /* finds a cookie in the domain matching the cookie name */
120 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCSTR lpszCookieName)
122 cookie *searchCookie = domain->cookie_tail;
123 TRACE("(%p, %s)\n", domain, debugstr_a(lpszCookieName));
127 BOOL candidate = TRUE;
128 if (candidate && lpszCookieName)
130 if (candidate && !searchCookie->lpCookieName)
132 if (candidate && strcmp(lpszCookieName, searchCookie->lpCookieName) != 0)
137 searchCookie = searchCookie->prev;
142 /* removes a cookie from the list, if its the last cookie we also remove the domain */
143 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
145 if (deadCookie->lpCookieName)
146 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
147 if (deadCookie->lpCookieData)
148 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
149 if (deadCookie->prev)
150 deadCookie->prev->next = deadCookie->next;
151 if (deadCookie->next)
152 deadCookie->next->prev = deadCookie->prev;
154 if (deadCookie == deadCookie->parent->cookie_tail)
156 /* special case: last cookie, lets remove the domain to save memory */
157 deadCookie->parent->cookie_tail = deadCookie->prev;
158 if (!deadCookie->parent->cookie_tail && deleteDomain)
159 COOKIE_deleteDomain(deadCookie->parent);
163 /* allocates a domain and adds it to the end */
164 static cookie_domain *COOKIE_addDomain(LPCSTR domain, LPCSTR path)
166 cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
168 newDomain->next = NULL;
169 newDomain->prev = NULL;
170 newDomain->cookie_tail = NULL;
171 newDomain->lpCookieDomain = NULL;
172 newDomain->lpCookiePath = NULL;
176 newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, strlen(domain) + 1);
177 strcpy(newDomain->lpCookieDomain, domain);
181 newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, strlen(path) + 1);
182 strcpy(newDomain->lpCookiePath, path);
185 newDomain->prev = cookieDomainTail;
186 cookieDomainTail = newDomain;
187 TRACE("Adding domain: %p\n", newDomain);
191 static cookie_domain *COOKIE_addDomainFromUrl(LPCSTR lpszUrl)
193 char hostName[2048], path[2048];
194 URL_COMPONENTSA UrlComponents;
196 UrlComponents.lpszExtraInfo = NULL;
197 UrlComponents.lpszPassword = NULL;
198 UrlComponents.lpszScheme = NULL;
199 UrlComponents.lpszUrlPath = path;
200 UrlComponents.lpszUserName = NULL;
201 UrlComponents.lpszHostName = hostName;
202 UrlComponents.dwHostNameLength = 2048;
203 UrlComponents.dwUrlPathLength = 2048;
205 InternetCrackUrlA(lpszUrl, 0, 0, &UrlComponents);
207 TRACE("Url cracked. Domain: %s, Path: %s.\n", debugstr_a(UrlComponents.lpszHostName),
208 debugstr_a(UrlComponents.lpszUrlPath));
210 /* hack for now - FIXME - There seems to be a bug in InternetCrackUrl?? */
211 UrlComponents.lpszUrlPath = NULL;
213 return COOKIE_addDomain(UrlComponents.lpszHostName, UrlComponents.lpszUrlPath);
216 /* find a domain. domain must match if its not NULL. path must match if its not NULL */
217 static cookie_domain *COOKIE_findNextDomain(LPCSTR lpszCookieDomain, LPCSTR lpszCookiePath,
218 cookie_domain *prev_domain, BOOL allow_partial)
220 cookie_domain *searchDomain;
224 if(!prev_domain->prev)
226 TRACE("no more domains available, it would seem.\n");
229 searchDomain = prev_domain->prev;
231 else searchDomain = cookieDomainTail;
235 BOOL candidate = TRUE;
236 TRACE("searching on domain %p\n", searchDomain);
237 if (candidate && lpszCookieDomain)
239 if (candidate && !searchDomain->lpCookieDomain)
241 TRACE("candidate! (%p)\n", searchDomain->lpCookieDomain);
242 TRACE("comparing domain %s with %s\n", lpszCookieDomain, searchDomain->lpCookieDomain);
243 if (candidate && allow_partial && !strstr(lpszCookieDomain, searchDomain->lpCookieDomain))
245 else if (candidate && !allow_partial &&
246 strcmp(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
249 if (candidate && lpszCookiePath)
250 { TRACE("comparing paths\n");
251 if (candidate && !searchDomain->lpCookiePath)
253 if (candidate && strcmp(lpszCookiePath, searchDomain->lpCookiePath) != 0)
258 TRACE("returning the domain %p\n", searchDomain);
261 searchDomain = searchDomain->prev;
263 TRACE("found no domain, returning NULL\n");
267 static cookie_domain *COOKIE_findNextDomainFromUrl(LPCSTR lpszUrl, cookie_domain *previous_domain,
270 char hostName[2048], path[2048];
271 URL_COMPONENTSA UrlComponents;
273 UrlComponents.lpszExtraInfo = NULL;
274 UrlComponents.lpszPassword = NULL;
275 UrlComponents.lpszScheme = NULL;
276 UrlComponents.lpszUrlPath = path;
277 UrlComponents.lpszUserName = NULL;
278 UrlComponents.lpszHostName = hostName;
279 UrlComponents.dwHostNameLength = 2048;
280 UrlComponents.dwUrlPathLength = 2048;
282 InternetCrackUrlA(lpszUrl, 0, 0, &UrlComponents);
284 TRACE("Url cracked. Domain: %s, Path: %s.\n", debugstr_a(UrlComponents.lpszHostName),
285 debugstr_a(UrlComponents.lpszUrlPath));
287 /* hack for now - FIXME - There seems to be a bug in InternetCrackUrl?? */
288 UrlComponents.lpszUrlPath = NULL;
290 return COOKIE_findNextDomain(UrlComponents.lpszHostName, UrlComponents.lpszUrlPath,
291 previous_domain, allow_partial);
294 /* remove a domain from the list and delete it */
295 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
297 while (deadDomain->cookie_tail)
298 COOKIE_deleteCookie(deadDomain->cookie_tail, FALSE);
299 if (deadDomain->lpCookieDomain)
300 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
301 if (deadDomain->lpCookiePath)
302 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
303 if (deadDomain->prev)
304 deadDomain->prev->next = deadDomain->next;
305 if (deadDomain->next)
306 deadDomain->next->prev = deadDomain->prev;
308 if (cookieDomainTail == deadDomain)
309 cookieDomainTail = deadDomain->prev;
310 HeapFree(GetProcessHeap(), 0, deadDomain);
313 /***********************************************************************
314 * InternetGetCookieA (WININET.@)
316 * Retrieve cookie from the specified url
318 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
319 * So it won't be implemented here.
326 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
327 LPSTR lpCookieData, LPDWORD lpdwSize)
329 cookie_domain *cookiesDomain = NULL;
331 int cnt = 0, domain_count = 0;
332 /* Ok, this is just ODD!. During my tests, it appears M$ like to send out
333 * a cookie called 'MtrxTracking' to some urls. Also returns it from InternetGetCookie.
334 * I'm not exactly sure what to make of this, so its here for now.
335 * It'd be nice to know what exactly is going on, M$ tracking users? Does this need
336 * to be unique? Should I generate a random number here? etc.
338 char *TrackingString = "MtrxTrackingID=01234567890123456789012345678901";
340 TRACE("(%s, %s, %p, %p)\n", debugstr_a(lpszUrl),debugstr_a(lpszCookieName),
341 lpCookieData, lpdwSize);
344 cnt += snprintf(lpCookieData + cnt, *lpdwSize - cnt, "%s", TrackingString);
346 cnt += strlen(TrackingString);
348 while ((cookiesDomain = COOKIE_findNextDomainFromUrl(lpszUrl, cookiesDomain, TRUE)))
351 TRACE("found domain %p\n", cookiesDomain);
353 thisCookie = cookiesDomain->cookie_tail;
354 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
359 cnt += strlen(thisCookie->lpCookieName);
361 cnt += strlen(thisCookie->lpCookieData);
363 thisCookie = thisCookie->prev;
368 cnt += snprintf(lpCookieData + cnt, *lpdwSize - cnt, "; ");
369 cnt += snprintf(lpCookieData + cnt, *lpdwSize - cnt, "%s=%s", thisCookie->lpCookieName,
370 thisCookie->lpCookieData);
372 thisCookie = thisCookie->prev;
375 if (lpCookieData == NULL)
379 TRACE("returning\n");
388 TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count, lpCookieData);
390 return (cnt ? TRUE : FALSE);
394 /***********************************************************************
395 * InternetGetCookieW (WININET.@)
397 * Retrieve cookie from the specified url
404 BOOL WINAPI InternetGetCookieW(LPCSTR lpszUrl, LPCWSTR lpszCookieName,
405 LPWSTR lpCookieData, LPDWORD lpdwSize)
408 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_w(lpszCookieName),
414 /***********************************************************************
415 * InternetSetCookieA (WININET.@)
417 * Sets cookie for the specified url
424 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
428 cookie_domain *thisCookieDomain;
430 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
431 debugstr_a(lpszCookieName), lpCookieData);
433 if (!lpCookieData || !strlen(lpCookieData))
435 TRACE("no cookie data, not adding\n");
440 /* some apps (or is it us??) try to add a cookie with no cookie name, but
441 * the cookie data in the form of name=data. */
442 /* FIXME, probably a bug here, for now I don't care */
443 char *ourCookieName, *ourCookieData;
444 int ourCookieNameSize;
446 if (!(ourCookieData = strchr(lpCookieData, '=')))
448 TRACE("something terribly wrong with cookie data %s\n", ourCookieData);
451 ourCookieNameSize = ourCookieData - lpCookieData;
453 ourCookieName = HeapAlloc(GetProcessHeap(), 0, ourCookieNameSize + 1);
454 strncpy(ourCookieName, ourCookieData, ourCookieNameSize);
455 ourCookieName[ourCookieNameSize] = '\0';
456 TRACE("setting (hacked) cookie of %s, %s\n", ourCookieName, ourCookieData);
457 ret = InternetSetCookieA(lpszUrl, ourCookieName, ourCookieData);
458 HeapFree(GetProcessHeap(), 0, ourCookieName);
462 if (!(thisCookieDomain = COOKIE_findNextDomainFromUrl(lpszUrl, NULL, FALSE)))
463 thisCookieDomain = COOKIE_addDomainFromUrl(lpszUrl);
465 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
466 COOKIE_deleteCookie(thisCookie, FALSE);
468 thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
473 /***********************************************************************
474 * InternetSetCookieW (WININET.@)
476 * Sets cookie for the specified url
483 BOOL WINAPI InternetSetCookieW(LPCSTR lpszUrl, LPCWSTR lpszCookieName,
484 LPCWSTR lpCookieData)
487 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
488 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));