push bc3e5bf5ba806943a97979264a1f2e4a4dde5c94
[wine/hacks.git] / dlls / wininet / cookie.c
blobabf897a4bf4733e2b610bda19d79ae066fa8b25e
1 /*
2 * Wininet - cookie handling stuff
4 * Copyright 2002 TransGaming Technologies Inc.
6 * David Hammerton
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wininet.h"
37 #include "winerror.h"
39 #include "wine/debug.h"
40 #include "internet.h"
42 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
45 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
47 /* FIXME
48 * Cookies are currently memory only.
49 * Cookies are NOT THREAD SAFE
50 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
51 * Cookies should care about the expiry time
54 typedef struct _cookie_domain cookie_domain;
55 typedef struct _cookie cookie;
57 struct _cookie
59 struct list entry;
61 struct _cookie_domain *parent;
63 LPWSTR lpCookieName;
64 LPWSTR lpCookieData;
65 time_t expiry; /* FIXME: not used */
68 struct _cookie_domain
70 struct list entry;
72 LPWSTR lpCookieDomain;
73 LPWSTR lpCookiePath;
74 struct list cookie_list;
77 static struct list domain_list = LIST_INIT(domain_list);
79 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data);
80 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
81 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
82 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
83 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
86 /* adds a cookie to the domain */
87 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data)
89 cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));
91 list_init(&newCookie->entry);
92 newCookie->lpCookieName = NULL;
93 newCookie->lpCookieData = NULL;
95 if (name)
97 newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1)*sizeof(WCHAR));
98 lstrcpyW(newCookie->lpCookieName, name);
100 if (data)
102 newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, (strlenW(data) + 1)*sizeof(WCHAR));
103 lstrcpyW(newCookie->lpCookieData, data);
106 TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
108 list_add_tail(&domain->cookie_list, &newCookie->entry);
109 newCookie->parent = domain;
110 return newCookie;
114 /* finds a cookie in the domain matching the cookie name */
115 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
117 struct list * cursor;
118 TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
120 LIST_FOR_EACH(cursor, &domain->cookie_list)
122 cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
123 BOOL candidate = TRUE;
124 if (candidate && lpszCookieName)
126 if (candidate && !searchCookie->lpCookieName)
127 candidate = FALSE;
128 if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
129 candidate = FALSE;
131 if (candidate)
132 return searchCookie;
134 return NULL;
137 /* removes a cookie from the list, if its the last cookie we also remove the domain */
138 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
140 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
141 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
142 list_remove(&deadCookie->entry);
144 /* special case: last cookie, lets remove the domain to save memory */
145 if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
146 COOKIE_deleteDomain(deadCookie->parent);
147 HeapFree(GetProcessHeap(), 0, deadCookie);
150 /* allocates a domain and adds it to the end */
151 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
153 cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
155 list_init(&newDomain->entry);
156 list_init(&newDomain->cookie_list);
157 newDomain->lpCookieDomain = NULL;
158 newDomain->lpCookiePath = NULL;
160 if (domain)
162 newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
163 strcpyW(newDomain->lpCookieDomain, domain);
165 if (path)
167 newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
168 lstrcpyW(newDomain->lpCookiePath, path);
171 list_add_tail(&domain_list, &newDomain->entry);
173 TRACE("Adding domain: %p\n", newDomain);
174 return newDomain;
177 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
179 URL_COMPONENTSW UrlComponents;
181 UrlComponents.lpszExtraInfo = NULL;
182 UrlComponents.lpszPassword = NULL;
183 UrlComponents.lpszScheme = NULL;
184 UrlComponents.lpszUrlPath = path;
185 UrlComponents.lpszUserName = NULL;
186 UrlComponents.lpszHostName = hostName;
187 UrlComponents.dwHostNameLength = hostNameLen;
188 UrlComponents.dwUrlPathLength = pathLen;
190 InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
193 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
194 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
195 cookie_domain *searchDomain, BOOL allow_partial)
197 TRACE("searching on domain %p\n", searchDomain);
198 if (lpszCookieDomain)
200 if (!searchDomain->lpCookieDomain)
201 return FALSE;
203 TRACE("comparing domain %s with %s\n",
204 debugstr_w(lpszCookieDomain),
205 debugstr_w(searchDomain->lpCookieDomain));
207 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
208 return FALSE;
209 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
210 return FALSE;
212 if (lpszCookiePath)
214 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
215 if (!searchDomain->lpCookiePath)
216 return FALSE;
217 if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
218 return FALSE;
220 return TRUE;
223 /* remove a domain from the list and delete it */
224 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
226 struct list * cursor;
227 while ((cursor = list_tail(&deadDomain->cookie_list)))
229 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
230 list_remove(cursor);
233 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
234 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
236 list_remove(&deadDomain->entry);
238 HeapFree(GetProcessHeap(), 0, deadDomain);
241 /***********************************************************************
242 * InternetGetCookieW (WININET.@)
244 * Retrieve cookie from the specified url
246 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
247 * So it won't be implemented here.
249 * RETURNS
250 * TRUE on success
251 * FALSE on failure
254 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
255 LPWSTR lpCookieData, LPDWORD lpdwSize)
257 struct list * cursor;
258 int cnt = 0, domain_count = 0;
259 int cookie_count = 0;
260 WCHAR hostName[2048], path[2048];
262 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
263 lpCookieData, lpdwSize);
265 if (!lpszUrl)
267 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
268 return FALSE;
271 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
273 LIST_FOR_EACH(cursor, &domain_list)
275 cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
276 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, cookiesDomain, TRUE))
278 struct list * cursor;
279 domain_count++;
280 TRACE("found domain %p\n", cookiesDomain);
282 LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
284 cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
285 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
287 if (cookie_count != 0)
288 cnt += 2; /* '; ' */
289 cnt += strlenW(thisCookie->lpCookieName);
290 cnt += 1; /* = */
291 cnt += strlenW(thisCookie->lpCookieData);
293 else
295 static const WCHAR szsc[] = { ';',' ',0 };
296 static const WCHAR szpseq[] = { '%','s','=','%','s',0 };
297 if (cookie_count != 0)
298 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
299 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szpseq,
300 thisCookie->lpCookieName,
301 thisCookie->lpCookieData);
302 TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie->lpCookieName), debugstr_w(thisCookie->lpCookieData));
304 cookie_count++;
309 if (!domain_count)
311 TRACE("no cookies found for %s\n", debugstr_w(hostName));
312 SetLastError(ERROR_NO_MORE_ITEMS);
313 return FALSE;
316 if (lpCookieData == NULL)
318 cnt += 1; /* NULL */
319 *lpdwSize = cnt*sizeof(WCHAR);
320 TRACE("returning\n");
321 return TRUE;
324 *lpdwSize = (cnt + 1)*sizeof(WCHAR);
326 TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count,
327 debugstr_w(lpCookieData));
329 return (cnt ? TRUE : FALSE);
333 /***********************************************************************
334 * InternetGetCookieA (WININET.@)
336 * Retrieve cookie from the specified url
338 * RETURNS
339 * TRUE on success
340 * FALSE on failure
343 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
344 LPSTR lpCookieData, LPDWORD lpdwSize)
346 DWORD len;
347 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
348 BOOL r;
350 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
351 lpCookieData);
353 if( lpszUrl )
355 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
356 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
357 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
360 if( lpszCookieName )
362 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
363 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
364 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
367 r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
368 if( r )
370 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
371 if( !szCookieData )
372 return FALSE;
374 r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
376 *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
377 lpCookieData, *lpdwSize, NULL, NULL );
380 HeapFree( GetProcessHeap(), 0, szCookieData );
381 HeapFree( GetProcessHeap(), 0, szCookieName );
382 HeapFree( GetProcessHeap(), 0, szUrl );
384 return r;
388 /***********************************************************************
389 * InternetSetCookieW (WININET.@)
391 * Sets cookie for the specified url
393 * RETURNS
394 * TRUE on success
395 * FALSE on failure
398 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
399 LPCWSTR lpCookieData)
401 cookie_domain *thisCookieDomain = NULL;
402 cookie *thisCookie;
403 WCHAR hostName[2048], path[2048];
404 struct list * cursor;
406 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
407 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
409 if (!lpCookieData)
411 SetLastError(ERROR_INVALID_PARAMETER);
412 return FALSE;
414 if (!lpszCookieName)
416 /* some apps (or is it us??) try to add a cookie with no cookie name, but
417 * the cookie data in the form of name=data. */
418 /* FIXME, probably a bug here, for now I don't care */
419 WCHAR *ourCookieName, *ourCookieData;
420 int ourCookieNameSize;
421 BOOL ret;
423 if (!(ourCookieData = strchrW(lpCookieData, '=')))
425 TRACE("something terribly wrong with cookie data %s\n",
426 debugstr_w(ourCookieData));
427 return FALSE;
429 ourCookieNameSize = ourCookieData - lpCookieData;
430 ourCookieData += 1;
431 ourCookieName = HeapAlloc(GetProcessHeap(), 0,
432 (ourCookieNameSize + 1)*sizeof(WCHAR));
433 memcpy(ourCookieName, lpCookieData, ourCookieNameSize * sizeof(WCHAR));
434 ourCookieName[ourCookieNameSize] = '\0';
435 TRACE("setting (hacked) cookie of %s, %s\n",
436 debugstr_w(ourCookieName), debugstr_w(ourCookieData));
437 ret = InternetSetCookieW(lpszUrl, ourCookieName, ourCookieData);
438 HeapFree(GetProcessHeap(), 0, ourCookieName);
439 return ret;
442 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
444 LIST_FOR_EACH(cursor, &domain_list)
446 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
447 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, thisCookieDomain, FALSE))
448 break;
449 thisCookieDomain = NULL;
451 if (!thisCookieDomain)
452 thisCookieDomain = COOKIE_addDomain(hostName, path);
454 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
455 COOKIE_deleteCookie(thisCookie, FALSE);
457 thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
458 return TRUE;
462 /***********************************************************************
463 * InternetSetCookieA (WININET.@)
465 * Sets cookie for the specified url
467 * RETURNS
468 * TRUE on success
469 * FALSE on failure
472 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
473 LPCSTR lpCookieData)
475 DWORD len;
476 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
477 BOOL r;
479 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
480 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
482 if( lpszUrl )
484 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
485 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
486 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
489 if( lpszCookieName )
491 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
492 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
493 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
496 if( lpCookieData )
498 len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
499 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
500 MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
503 r = InternetSetCookieW( szUrl, szCookieName, szCookieData );
505 HeapFree( GetProcessHeap(), 0, szCookieData );
506 HeapFree( GetProcessHeap(), 0, szCookieName );
507 HeapFree( GetProcessHeap(), 0, szUrl );
509 return r;
512 /***********************************************************************
513 * InternetSetCookieExA (WININET.@)
515 * See InternetSetCookieExW.
517 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
518 DWORD dwFlags, DWORD_PTR dwReserved)
520 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
521 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
522 dwFlags, dwReserved);
523 return TRUE;
526 /***********************************************************************
527 * InternetSetCookieExW (WININET.@)
529 * Sets a cookie for the specified URL.
531 * RETURNS
532 * TRUE on success
533 * FALSE on failure
536 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
537 DWORD dwFlags, DWORD_PTR dwReserved)
539 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
540 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
541 dwFlags, dwReserved);
542 return TRUE;
545 /***********************************************************************
546 * InternetGetCookieExA (WININET.@)
548 * See InternetGetCookieExW.
550 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
551 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
553 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
554 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
555 pcchCookieData, dwFlags, lpReserved);
556 return FALSE;
559 /***********************************************************************
560 * InternetGetCookieExW (WININET.@)
562 * Retrieve cookie for the specified URL.
564 * RETURNS
565 * TRUE on success
566 * FALSE on failure
569 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
570 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
572 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
573 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
574 pcchCookieData, dwFlags, lpReserved);
575 return FALSE;
578 /***********************************************************************
579 * InternetClearAllPerSiteCookieDecisions (WININET.@)
581 * Clears all per-site decisions about cookies.
583 * RETURNS
584 * TRUE on success
585 * FALSE on failure
588 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
590 FIXME("stub\n");
591 return TRUE;
594 /***********************************************************************
595 * InternetEnumPerSiteCookieDecisionA (WININET.@)
597 * See InternetEnumPerSiteCookieDecisionW.
599 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, unsigned long *pcSiteNameSize,
600 unsigned long *pdwDecision, unsigned long dwIndex )
602 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
603 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
604 return FALSE;
607 /***********************************************************************
608 * InternetEnumPerSiteCookieDecisionW (WININET.@)
610 * Enumerates all per-site decisions about cookies.
612 * RETURNS
613 * TRUE on success
614 * FALSE on failure
617 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, unsigned long *pcSiteNameSize,
618 unsigned long *pdwDecision, unsigned long dwIndex )
620 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
621 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
622 return FALSE;
625 /***********************************************************************
626 * InternetGetPerSiteCookieDecisionA (WININET.@)
628 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, unsigned long *pResult )
630 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
631 return FALSE;
634 /***********************************************************************
635 * InternetGetPerSiteCookieDecisionW (WININET.@)
637 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, unsigned long *pResult )
639 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
640 return FALSE;
643 /***********************************************************************
644 * InternetSetPerSiteCookieDecisionA (WININET.@)
646 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
648 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
649 return FALSE;
652 /***********************************************************************
653 * InternetSetPerSiteCookieDecisionW (WININET.@)
655 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
657 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
658 return FALSE;