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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #if defined(__MINGW32__) || defined (_MSC_VER)
43 #include "wine/debug.h"
46 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
49 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
52 * Cookies are currently memory only.
53 * Cookies are NOT THREAD SAFE
54 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
55 * Cookies should care about the expiry time
58 typedef struct _cookie_domain cookie_domain;
59 typedef struct _cookie cookie;
65 struct _cookie_domain *parent;
69 time_t expiry; /* FIXME: not used */
76 LPWSTR lpCookieDomain;
78 struct list cookie_list;
81 static struct list domain_list = LIST_INIT(domain_list);
83 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data);
84 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
85 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
86 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
87 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
90 /* adds a cookie to the domain */
91 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data)
93 cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));
95 list_init(&newCookie->entry);
96 newCookie->lpCookieName = NULL;
97 newCookie->lpCookieData = NULL;
101 newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1)*sizeof(WCHAR));
102 lstrcpyW(newCookie->lpCookieName, name);
106 newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, (strlenW(data) + 1)*sizeof(WCHAR));
107 lstrcpyW(newCookie->lpCookieData, data);
110 TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
112 list_add_tail(&domain->cookie_list, &newCookie->entry);
113 newCookie->parent = domain;
118 /* finds a cookie in the domain matching the cookie name */
119 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
121 struct list * cursor;
122 TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
124 LIST_FOR_EACH(cursor, &domain->cookie_list)
126 cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
127 BOOL candidate = TRUE;
128 if (candidate && lpszCookieName)
130 if (candidate && !searchCookie->lpCookieName)
132 if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
141 /* removes a cookie from the list, if its the last cookie we also remove the domain */
142 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
144 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
145 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
146 list_remove(&deadCookie->entry);
148 /* special case: last cookie, lets remove the domain to save memory */
149 if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
150 COOKIE_deleteDomain(deadCookie->parent);
151 HeapFree(GetProcessHeap(), 0, deadCookie);
154 /* allocates a domain and adds it to the end */
155 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
157 cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
159 list_init(&newDomain->entry);
160 list_init(&newDomain->cookie_list);
161 newDomain->lpCookieDomain = NULL;
162 newDomain->lpCookiePath = NULL;
166 newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
167 strcpyW(newDomain->lpCookieDomain, domain);
171 newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
172 lstrcpyW(newDomain->lpCookiePath, path);
175 list_add_tail(&domain_list, &newDomain->entry);
177 TRACE("Adding domain: %p\n", newDomain);
181 static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
183 URL_COMPONENTSW UrlComponents;
185 UrlComponents.lpszExtraInfo = NULL;
186 UrlComponents.lpszPassword = NULL;
187 UrlComponents.lpszScheme = NULL;
188 UrlComponents.lpszUrlPath = path;
189 UrlComponents.lpszUserName = NULL;
190 UrlComponents.lpszHostName = hostName;
191 UrlComponents.dwExtraInfoLength = 0;
192 UrlComponents.dwPasswordLength = 0;
193 UrlComponents.dwSchemeLength = 0;
194 UrlComponents.dwUserNameLength = 0;
195 UrlComponents.dwHostNameLength = hostNameLen;
196 UrlComponents.dwUrlPathLength = pathLen;
198 return InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
201 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
202 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
203 cookie_domain *searchDomain, BOOL allow_partial)
205 TRACE("searching on domain %p\n", searchDomain);
206 if (lpszCookieDomain)
208 if (!searchDomain->lpCookieDomain)
211 TRACE("comparing domain %s with %s\n",
212 debugstr_w(lpszCookieDomain),
213 debugstr_w(searchDomain->lpCookieDomain));
215 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
217 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
222 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
223 if (!searchDomain->lpCookiePath)
225 if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
231 /* remove a domain from the list and delete it */
232 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
234 struct list * cursor;
235 while ((cursor = list_tail(&deadDomain->cookie_list)))
237 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
241 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
242 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
244 list_remove(&deadDomain->entry);
246 HeapFree(GetProcessHeap(), 0, deadDomain);
249 /***********************************************************************
250 * InternetGetCookieW (WININET.@)
252 * Retrieve cookie from the specified url
254 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
255 * So it won't be implemented here.
262 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
263 LPWSTR lpCookieData, LPDWORD lpdwSize)
266 struct list * cursor;
267 unsigned int cnt = 0, domain_count = 0, cookie_count = 0;
268 WCHAR hostName[2048], path[2048];
270 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
271 lpCookieData, lpdwSize);
275 SetLastError(ERROR_INVALID_PARAMETER);
280 ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
281 if (!ret || !hostName[0]) return FALSE;
283 LIST_FOR_EACH(cursor, &domain_list)
285 cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
286 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, cookiesDomain, TRUE))
288 struct list * cursor;
290 TRACE("found domain %p\n", cookiesDomain);
292 LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
294 cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
295 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
299 if (cookie_count) cnt += 2; /* '; ' */
300 cnt += strlenW(thisCookie->lpCookieName);
301 if ((len = strlenW(thisCookie->lpCookieData)))
309 static const WCHAR szsc[] = { ';',' ',0 };
310 static const WCHAR szname[] = { '%','s',0 };
311 static const WCHAR szdata[] = { '=','%','s',0 };
313 if (cookie_count) cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
314 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szname, thisCookie->lpCookieName);
316 if (thisCookie->lpCookieData[0])
317 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szdata, thisCookie->lpCookieData);
319 TRACE("Cookie: %s\n", debugstr_w(lpCookieData));
328 TRACE("no cookies found for %s\n", debugstr_w(hostName));
329 SetLastError(ERROR_NO_MORE_ITEMS);
333 if (lpCookieData == NULL)
335 *lpdwSize = (cnt + 1) * sizeof(WCHAR);
336 TRACE("returning %u\n", *lpdwSize);
342 TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count,
343 debugstr_w(lpCookieData));
345 return (cnt ? TRUE : FALSE);
349 /***********************************************************************
350 * InternetGetCookieA (WININET.@)
352 * Retrieve cookie from the specified url
359 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
360 LPSTR lpCookieData, LPDWORD lpdwSize)
363 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
366 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
371 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
372 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
373 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
378 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
379 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
380 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
383 r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
386 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
393 r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
395 *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
396 lpCookieData, *lpdwSize, NULL, NULL );
400 HeapFree( GetProcessHeap(), 0, szCookieData );
401 HeapFree( GetProcessHeap(), 0, szCookieName );
402 HeapFree( GetProcessHeap(), 0, szUrl );
407 static BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cookie_data)
409 cookie_domain *thisCookieDomain = NULL;
413 LIST_FOR_EACH(cursor, &domain_list)
415 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
416 if (COOKIE_matchDomain(domain, NULL /* FIXME: path */, thisCookieDomain, FALSE))
418 thisCookieDomain = NULL;
421 if (!thisCookieDomain)
422 thisCookieDomain = COOKIE_addDomain(domain, path);
424 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, cookie_name)))
425 COOKIE_deleteCookie(thisCookie, FALSE);
427 TRACE("setting cookie %s=%s for domain %s\n", debugstr_w(cookie_name),
428 debugstr_w(cookie_data), debugstr_w(thisCookieDomain->lpCookieDomain));
430 if (!COOKIE_addCookie(thisCookieDomain, cookie_name, cookie_data))
436 /***********************************************************************
437 * InternetSetCookieW (WININET.@)
439 * Sets cookie for the specified url
446 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
447 LPCWSTR lpCookieData)
450 WCHAR hostName[2048], path[2048];
452 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
453 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
455 if (!lpszUrl || !lpCookieData)
457 SetLastError(ERROR_INVALID_PARAMETER);
461 hostName[0] = path[0] = 0;
462 ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
463 if (!ret || !hostName[0]) return FALSE;
468 WCHAR *cookie, *data;
470 len = strlenW(lpCookieData);
471 if (!(cookie = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR))))
473 SetLastError(ERROR_OUTOFMEMORY);
476 strcpyW(cookie, lpCookieData);
478 /* some apps (or is it us??) try to add a cookie with no cookie name, but
479 * the cookie data in the form of name[=data].
481 if (!(data = strchrW(cookie, '='))) data = cookie + len;
484 ret = set_cookie(hostName, path, cookie, data);
486 HeapFree(GetProcessHeap(), 0, cookie);
489 return set_cookie(hostName, path, lpszCookieName, lpCookieData);
493 /***********************************************************************
494 * InternetSetCookieA (WININET.@)
496 * Sets cookie for the specified url
503 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
507 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
510 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
511 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
515 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
516 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
517 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
522 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
523 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
524 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
529 len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
530 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
531 MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
534 r = InternetSetCookieW( szUrl, szCookieName, szCookieData );
536 HeapFree( GetProcessHeap(), 0, szCookieData );
537 HeapFree( GetProcessHeap(), 0, szCookieName );
538 HeapFree( GetProcessHeap(), 0, szUrl );
543 /***********************************************************************
544 * InternetSetCookieExA (WININET.@)
546 * See InternetSetCookieExW.
548 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
549 DWORD dwFlags, DWORD_PTR dwReserved)
551 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
552 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
553 dwFlags, dwReserved);
555 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
556 return InternetSetCookieA(lpszURL, lpszCookieName, lpszCookieData);
559 /***********************************************************************
560 * InternetSetCookieExW (WININET.@)
562 * Sets a cookie for the specified URL.
569 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
570 DWORD dwFlags, DWORD_PTR dwReserved)
572 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
573 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
574 dwFlags, dwReserved);
576 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
577 return InternetSetCookieW(lpszURL, lpszCookieName, lpszCookieData);
580 /***********************************************************************
581 * InternetGetCookieExA (WININET.@)
583 * See InternetGetCookieExW.
585 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
586 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
588 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
589 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
590 pcchCookieData, dwFlags, lpReserved);
592 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
593 return InternetGetCookieA(pchURL, pchCookieName, pchCookieData, pcchCookieData);
596 /***********************************************************************
597 * InternetGetCookieExW (WININET.@)
599 * Retrieve cookie for the specified URL.
606 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
607 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
609 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
610 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
611 pcchCookieData, dwFlags, lpReserved);
613 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
614 return InternetGetCookieW(pchURL, pchCookieName, pchCookieData, pcchCookieData);
617 /***********************************************************************
618 * InternetClearAllPerSiteCookieDecisions (WININET.@)
620 * Clears all per-site decisions about cookies.
627 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
633 /***********************************************************************
634 * InternetEnumPerSiteCookieDecisionA (WININET.@)
636 * See InternetEnumPerSiteCookieDecisionW.
638 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, ULONG *pcSiteNameSize,
639 ULONG *pdwDecision, ULONG dwIndex )
641 FIXME("(%s, %p, %p, 0x%08x) stub\n",
642 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
646 /***********************************************************************
647 * InternetEnumPerSiteCookieDecisionW (WININET.@)
649 * Enumerates all per-site decisions about cookies.
656 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, ULONG *pcSiteNameSize,
657 ULONG *pdwDecision, ULONG dwIndex )
659 FIXME("(%s, %p, %p, 0x%08x) stub\n",
660 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
664 /***********************************************************************
665 * InternetGetPerSiteCookieDecisionA (WININET.@)
667 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, ULONG *pResult )
669 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
673 /***********************************************************************
674 * InternetGetPerSiteCookieDecisionW (WININET.@)
676 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, ULONG *pResult )
678 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
682 /***********************************************************************
683 * InternetSetPerSiteCookieDecisionA (WININET.@)
685 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
687 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
691 /***********************************************************************
692 * InternetSetPerSiteCookieDecisionW (WININET.@)
694 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
696 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);