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
24 #include "wine/port.h"
39 #include "wine/debug.h"
42 #include "wine/list.h"
44 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
47 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
50 * Cookies are currently memory only.
51 * Cookies are NOT THREAD SAFE
52 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
53 * Cookies should care about the expiry time
56 typedef struct _cookie_domain cookie_domain;
57 typedef struct _cookie cookie;
63 struct _cookie_domain *parent;
67 time_t expiry; /* FIXME: not used */
74 LPWSTR lpCookieDomain;
76 struct list cookie_list;
79 static struct list domain_list = LIST_INIT(domain_list);
81 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data);
82 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
83 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
84 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
85 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
88 /* adds a cookie to the domain */
89 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data)
91 cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));
93 list_init(&newCookie->entry);
94 newCookie->lpCookieName = NULL;
95 newCookie->lpCookieData = NULL;
99 newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1)*sizeof(WCHAR));
100 lstrcpyW(newCookie->lpCookieName, name);
104 newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, (strlenW(data) + 1)*sizeof(WCHAR));
105 lstrcpyW(newCookie->lpCookieData, data);
108 TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
110 list_add_tail(&domain->cookie_list, &newCookie->entry);
111 newCookie->parent = domain;
116 /* finds a cookie in the domain matching the cookie name */
117 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
119 struct list * cursor;
120 TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
122 LIST_FOR_EACH(cursor, &domain->cookie_list)
124 cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
125 BOOL candidate = TRUE;
126 if (candidate && lpszCookieName)
128 if (candidate && !searchCookie->lpCookieName)
130 if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
139 /* removes a cookie from the list, if its the last cookie we also remove the domain */
140 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
142 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
143 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
144 list_remove(&deadCookie->entry);
146 /* special case: last cookie, lets remove the domain to save memory */
147 if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
148 COOKIE_deleteDomain(deadCookie->parent);
149 HeapFree(GetProcessHeap(), 0, deadCookie);
152 /* allocates a domain and adds it to the end */
153 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
155 cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
157 list_init(&newDomain->entry);
158 list_init(&newDomain->cookie_list);
159 newDomain->lpCookieDomain = NULL;
160 newDomain->lpCookiePath = NULL;
164 newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
165 strcpyW(newDomain->lpCookieDomain, domain);
169 newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
170 lstrcpyW(newDomain->lpCookiePath, path);
173 list_add_tail(&domain_list, &newDomain->entry);
175 TRACE("Adding domain: %p\n", newDomain);
179 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
181 URL_COMPONENTSW UrlComponents;
183 UrlComponents.lpszExtraInfo = NULL;
184 UrlComponents.lpszPassword = NULL;
185 UrlComponents.lpszScheme = NULL;
186 UrlComponents.lpszUrlPath = path;
187 UrlComponents.lpszUserName = NULL;
188 UrlComponents.lpszHostName = hostName;
189 UrlComponents.dwHostNameLength = hostNameLen;
190 UrlComponents.dwUrlPathLength = pathLen;
192 InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
195 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
196 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
197 cookie_domain *searchDomain, BOOL allow_partial)
199 TRACE("searching on domain %p\n", searchDomain);
200 if (lpszCookieDomain)
202 if (!searchDomain->lpCookieDomain)
205 TRACE("comparing domain %s with %s\n",
206 debugstr_w(lpszCookieDomain),
207 debugstr_w(searchDomain->lpCookieDomain));
209 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
211 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
216 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
217 if (!searchDomain->lpCookiePath)
219 if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
225 /* remove a domain from the list and delete it */
226 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
228 struct list * cursor;
229 while ((cursor = list_tail(&deadDomain->cookie_list)))
231 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
235 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
236 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
238 list_remove(&deadDomain->entry);
240 HeapFree(GetProcessHeap(), 0, deadDomain);
243 /***********************************************************************
244 * InternetGetCookieW (WININET.@)
246 * Retrieve cookie from the specified url
248 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
249 * So it won't be implemented here.
256 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
257 LPWSTR lpCookieData, LPDWORD lpdwSize)
259 struct list * cursor;
260 int cnt = 0, domain_count = 0;
261 int cookie_count = 0;
262 WCHAR hostName[2048], path[2048];
264 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
265 lpCookieData, lpdwSize);
267 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
269 LIST_FOR_EACH(cursor, &domain_list)
271 cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
272 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, cookiesDomain, TRUE))
274 struct list * cursor;
276 TRACE("found domain %p\n", cookiesDomain);
278 LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
280 cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
281 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
283 if (cookie_count != 0)
285 cnt += strlenW(thisCookie->lpCookieName);
287 cnt += strlenW(thisCookie->lpCookieData);
291 static const WCHAR szsc[] = { ';',' ',0 };
292 static const WCHAR szpseq[] = { '%','s','=','%','s',0 };
293 if (cookie_count != 0)
294 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
295 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szpseq,
296 thisCookie->lpCookieName,
297 thisCookie->lpCookieData);
298 TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie->lpCookieName), debugstr_w(thisCookie->lpCookieData));
307 TRACE("no cookies found for %s\n", debugstr_w(hostName));
308 SetLastError(ERROR_NO_MORE_ITEMS);
312 if (lpCookieData == NULL)
315 *lpdwSize = cnt*sizeof(WCHAR);
316 TRACE("returning\n");
320 *lpdwSize = (cnt + 1)*sizeof(WCHAR);
322 TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count,
323 debugstr_w(lpCookieData));
325 return (cnt ? TRUE : FALSE);
329 /***********************************************************************
330 * InternetGetCookieA (WININET.@)
332 * Retrieve cookie from the specified url
339 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
340 LPSTR lpCookieData, LPDWORD lpdwSize)
343 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
346 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
351 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
352 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
353 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
358 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
359 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
360 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
363 r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
366 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
370 r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
372 *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
373 lpCookieData, *lpdwSize, NULL, NULL );
376 HeapFree( GetProcessHeap(), 0, szCookieData );
377 HeapFree( GetProcessHeap(), 0, szCookieName );
378 HeapFree( GetProcessHeap(), 0, szUrl );
384 /***********************************************************************
385 * InternetSetCookieW (WININET.@)
387 * Sets cookie for the specified url
394 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
395 LPCWSTR lpCookieData)
397 cookie_domain *thisCookieDomain = NULL;
399 WCHAR hostName[2048], path[2048];
400 struct list * cursor;
402 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
403 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
405 if (!lpCookieData || !strlenW(lpCookieData))
407 TRACE("no cookie data, not adding\n");
412 /* some apps (or is it us??) try to add a cookie with no cookie name, but
413 * the cookie data in the form of name=data. */
414 /* FIXME, probably a bug here, for now I don't care */
415 WCHAR *ourCookieName, *ourCookieData;
416 int ourCookieNameSize;
419 if (!(ourCookieData = strchrW(lpCookieData, '=')))
421 TRACE("something terribly wrong with cookie data %s\n",
422 debugstr_w(ourCookieData));
425 ourCookieNameSize = ourCookieData - lpCookieData;
427 ourCookieName = HeapAlloc(GetProcessHeap(), 0,
428 (ourCookieNameSize + 1)*sizeof(WCHAR));
429 memcpy(ourCookieName, lpCookieData, ourCookieNameSize * sizeof(WCHAR));
430 ourCookieName[ourCookieNameSize] = '\0';
431 TRACE("setting (hacked) cookie of %s, %s\n",
432 debugstr_w(ourCookieName), debugstr_w(ourCookieData));
433 ret = InternetSetCookieW(lpszUrl, ourCookieName, ourCookieData);
434 HeapFree(GetProcessHeap(), 0, ourCookieName);
438 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
440 LIST_FOR_EACH(cursor, &domain_list)
442 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
443 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, thisCookieDomain, FALSE))
445 thisCookieDomain = NULL;
447 if (!thisCookieDomain)
448 thisCookieDomain = COOKIE_addDomain(hostName, path);
450 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
451 COOKIE_deleteCookie(thisCookie, FALSE);
453 thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
458 /***********************************************************************
459 * InternetSetCookieA (WININET.@)
461 * Sets cookie for the specified url
468 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
472 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
475 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
476 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
480 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
481 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
482 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
487 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
488 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
489 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
494 len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
495 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
496 MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
499 r = InternetSetCookieW( szUrl, szCookieName, szCookieData );
501 HeapFree( GetProcessHeap(), 0, szCookieData );
502 HeapFree( GetProcessHeap(), 0, szCookieName );
503 HeapFree( GetProcessHeap(), 0, szUrl );
508 /***********************************************************************
509 * InternetSetCookieExA (WININET.@)
511 * See InternetSetCookieExW.
513 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
514 DWORD dwFlags, DWORD_PTR dwReserved)
516 FIXME("(%s, %s, %s, 0x%08lx, 0x%08lx) stub\n",
517 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
518 dwFlags, dwReserved);
522 /***********************************************************************
523 * InternetSetCookieExW (WININET.@)
525 * Sets a cookie for the specified URL.
532 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
533 DWORD dwFlags, DWORD_PTR dwReserved)
535 FIXME("(%s, %s, %s, 0x%08lx, 0x%08lx) stub\n",
536 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
537 dwFlags, dwReserved);
541 /***********************************************************************
542 * InternetGetCookieExA (WININET.@)
544 * See InternetGetCookieExW.
546 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
547 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
549 FIXME("(%s, %s, %s, %p, 0x%08lx, %p) stub\n",
550 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
551 pcchCookieData, dwFlags, lpReserved);
555 /***********************************************************************
556 * InternetGetCookieExW (WININET.@)
558 * Retrieve cookie for the specified URL.
565 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
566 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
568 FIXME("(%s, %s, %s, %p, 0x%08lx, %p) stub\n",
569 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
570 pcchCookieData, dwFlags, lpReserved);
574 /***********************************************************************
575 * InternetClearAllPerSiteCookieDecisions (WININET.@)
577 * Clears all per-site decisions about cookies.
584 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
590 /***********************************************************************
591 * InternetEnumPerSiteCookieDecisionA (WININET.@)
593 * See InternetEnumPerSiteCookieDecisionW.
595 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, unsigned long *pcSiteNameSize,
596 unsigned long *pdwDecision, unsigned long dwIndex )
598 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
599 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
603 /***********************************************************************
604 * InternetEnumPerSiteCookieDecisionW (WININET.@)
606 * Enumerates all per-site decisions about cookies.
613 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, unsigned long *pcSiteNameSize,
614 unsigned long *pdwDecision, unsigned long dwIndex )
616 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
617 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
621 /***********************************************************************
622 * InternetGetPerSiteCookieDecisionA (WININET.@)
624 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, unsigned long *pResult )
626 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
630 /***********************************************************************
631 * InternetGetPerSiteCookieDecisionW (WININET.@)
633 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, unsigned long *pResult )
635 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
639 /***********************************************************************
640 * InternetSetPerSiteCookieDecisionA (WININET.@)
642 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
644 FIXME("(%s, 0x%08lx) stub\n", debugstr_a(pchHostName), dwDecision);
648 /***********************************************************************
649 * InternetSetPerSiteCookieDecisionW (WININET.@)
651 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
653 FIXME("(%s, 0x%08lx) stub\n", debugstr_w(pchHostName), dwDecision);