msvcr100: Added _aligned_msize implementation.
[wine.git] / dlls / wininet / cookie.c
blob42c7525405e3af3ee8b602dd2f92d9b010f218f8
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 #if defined(__MINGW32__) || defined (_MSC_VER)
27 #include <ws2tcpip.h>
28 #endif
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wininet.h"
41 #include "winerror.h"
43 #include "wine/debug.h"
44 #include "internet.h"
46 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
49 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
51 /* FIXME
52 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
55 typedef struct _cookie_domain cookie_domain;
56 typedef struct _cookie cookie;
58 struct _cookie
60 struct list entry;
62 struct _cookie_domain *parent;
64 LPWSTR lpCookieName;
65 LPWSTR lpCookieData;
66 DWORD flags;
67 FILETIME expiry;
68 FILETIME create;
71 struct _cookie_domain
73 struct list entry;
75 LPWSTR lpCookieDomain;
76 LPWSTR lpCookiePath;
77 struct list cookie_list;
80 static CRITICAL_SECTION cookie_cs;
81 static CRITICAL_SECTION_DEBUG cookie_cs_debug =
83 0, 0, &cookie_cs,
84 { &cookie_cs_debug.ProcessLocksList, &cookie_cs_debug.ProcessLocksList },
85 0, 0, { (DWORD_PTR)(__FILE__ ": cookie_cs") }
87 static CRITICAL_SECTION cookie_cs = { &cookie_cs_debug, -1, 0, 0, 0, 0 };
88 static struct list domain_list = LIST_INIT(domain_list);
90 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data,
91 FILETIME expiry, FILETIME create, DWORD flags);
92 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
93 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
94 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
95 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
96 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
97 cookie_domain *searchDomain, BOOL allow_partial);
99 static BOOL create_cookie_url(LPCWSTR domain, LPCWSTR path, WCHAR *buf, DWORD buf_len)
101 static const WCHAR cookie_prefix[] = {'C','o','o','k','i','e',':'};
103 WCHAR *p;
104 DWORD len;
106 if(buf_len < sizeof(cookie_prefix)/sizeof(WCHAR))
107 return FALSE;
108 memcpy(buf, cookie_prefix, sizeof(cookie_prefix));
109 buf += sizeof(cookie_prefix)/sizeof(WCHAR);
110 buf_len -= sizeof(cookie_prefix)/sizeof(WCHAR);
111 p = buf;
113 len = buf_len;
114 if(!GetUserNameW(buf, &len))
115 return FALSE;
116 buf += len-1;
117 buf_len -= len-1;
119 if(!buf_len)
120 return FALSE;
121 *(buf++) = '@';
122 buf_len--;
124 len = strlenW(domain);
125 if(len >= buf_len)
126 return FALSE;
127 memcpy(buf, domain, len*sizeof(WCHAR));
128 buf += len;
129 buf_len -= len;
131 len = strlenW(path);
132 if(len >= buf_len)
133 return FALSE;
134 memcpy(buf, path, len*sizeof(WCHAR));
135 buf += len;
137 *buf = 0;
139 for(; *p; p++)
140 *p = tolowerW(*p);
141 return TRUE;
144 static BOOL load_persistent_cookie(LPCWSTR domain, LPCWSTR path)
146 INTERNET_CACHE_ENTRY_INFOW *info;
147 cookie_domain *domain_container = NULL;
148 cookie *old_cookie;
149 struct list *iter;
150 WCHAR cookie_url[MAX_PATH];
151 HANDLE cookie;
152 char *str = NULL, *pbeg, *pend;
153 DWORD size, flags;
154 WCHAR *name, *data;
155 FILETIME expiry, create, time;
157 if (!create_cookie_url(domain, path, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
158 return FALSE;
160 size = 0;
161 RetrieveUrlCacheEntryStreamW(cookie_url, NULL, &size, FALSE, 0);
162 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
163 return TRUE;
164 info = heap_alloc(size);
165 if(!info)
166 return FALSE;
167 cookie = RetrieveUrlCacheEntryStreamW(cookie_url, info, &size, FALSE, 0);
168 size = info->dwSizeLow;
169 heap_free(info);
170 if(!cookie)
171 return FALSE;
173 if(!(str = heap_alloc(size)) || !ReadUrlCacheEntryStream(cookie, 0, str, &size, 0)) {
174 UnlockUrlCacheEntryStream(cookie, 0);
175 heap_free(str);
176 return FALSE;
178 UnlockUrlCacheEntryStream(cookie, 0);
180 LIST_FOR_EACH(iter, &domain_list)
182 domain_container = LIST_ENTRY(iter, cookie_domain, entry);
183 if(COOKIE_matchDomain(domain, path, domain_container, FALSE))
184 break;
185 domain_container = NULL;
187 if(!domain_container)
188 domain_container = COOKIE_addDomain(domain, path);
189 if(!domain_container) {
190 heap_free(str);
191 return FALSE;
194 GetSystemTimeAsFileTime(&time);
195 for(pbeg=str; pbeg && *pbeg; name=data=NULL) {
196 pend = strchr(pbeg, '\n');
197 if(!pend)
198 break;
199 *pend = 0;
200 name = heap_strdupAtoW(pbeg);
202 pbeg = pend+1;
203 pend = strchr(pbeg, '\n');
204 if(!pend)
205 break;
206 *pend = 0;
207 data = heap_strdupAtoW(pbeg);
209 pbeg = pend+1;
210 pbeg = strchr(pend+1, '\n');
211 if(!pbeg)
212 break;
213 sscanf(pbeg, "%u %u %u %u %u", &flags, &expiry.dwLowDateTime, &expiry.dwHighDateTime,
214 &create.dwLowDateTime, &create.dwHighDateTime);
216 /* skip "*\n" */
217 pbeg = strchr(pbeg, '*');
218 if(pbeg) {
219 pbeg++;
220 if(*pbeg)
221 pbeg++;
224 if(!name || !data)
225 break;
227 if(CompareFileTime(&time, &expiry) <= 0) {
228 if((old_cookie = COOKIE_findCookie(domain_container, name)))
229 COOKIE_deleteCookie(old_cookie, FALSE);
230 COOKIE_addCookie(domain_container, name, data, expiry, create, flags);
232 heap_free(name);
233 heap_free(data);
235 heap_free(name);
236 heap_free(data);
238 return TRUE;
241 static BOOL save_persistent_cookie(cookie_domain *domain)
243 static const WCHAR txtW[] = {'t','x','t',0};
245 WCHAR cookie_url[MAX_PATH], cookie_file[MAX_PATH];
246 HANDLE cookie_handle;
247 cookie *cookie_container = NULL, *cookie_iter;
248 BOOL do_save = FALSE;
249 char buf[64], *dyn_buf;
250 FILETIME time;
252 if (!create_cookie_url(domain->lpCookieDomain, domain->lpCookiePath, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
253 return FALSE;
255 /* check if there's anything to save */
256 GetSystemTimeAsFileTime(&time);
257 LIST_FOR_EACH_ENTRY_SAFE(cookie_container, cookie_iter, &domain->cookie_list, cookie, entry)
259 if((cookie_container->expiry.dwLowDateTime || cookie_container->expiry.dwHighDateTime)
260 && CompareFileTime(&time, &cookie_container->expiry) > 0) {
261 COOKIE_deleteCookie(cookie_container, FALSE);
262 continue;
265 if(!(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)) {
266 do_save = TRUE;
267 break;
270 if(!do_save) {
271 DeleteUrlCacheEntryW(cookie_url);
272 return TRUE;
275 if(!CreateUrlCacheEntryW(cookie_url, 0, txtW, cookie_file, 0))
276 return FALSE;
277 cookie_handle = CreateFileW(cookie_file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
278 if(cookie_handle == INVALID_HANDLE_VALUE) {
279 DeleteFileW(cookie_file);
280 return FALSE;
283 LIST_FOR_EACH_ENTRY(cookie_container, &domain->cookie_list, cookie, entry)
285 if(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)
286 continue;
288 dyn_buf = heap_strdupWtoA(cookie_container->lpCookieName);
289 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), NULL, NULL)) {
290 heap_free(dyn_buf);
291 do_save = FALSE;
292 break;
294 heap_free(dyn_buf);
295 if(!WriteFile(cookie_handle, "\n", 1, NULL, NULL)) {
296 do_save = FALSE;
297 break;
300 dyn_buf = heap_strdupWtoA(cookie_container->lpCookieData);
301 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), NULL, NULL)) {
302 heap_free(dyn_buf);
303 do_save = FALSE;
304 break;
306 heap_free(dyn_buf);
307 if(!WriteFile(cookie_handle, "\n", 1, NULL, NULL)) {
308 do_save = FALSE;
309 break;
312 dyn_buf = heap_strdupWtoA(domain->lpCookieDomain);
313 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), NULL, NULL)) {
314 heap_free(dyn_buf);
315 do_save = FALSE;
316 break;
318 heap_free(dyn_buf);
320 dyn_buf = heap_strdupWtoA(domain->lpCookiePath);
321 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), NULL, NULL)) {
322 heap_free(dyn_buf);
323 do_save = FALSE;
324 break;
326 heap_free(dyn_buf);
328 sprintf(buf, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container->flags,
329 cookie_container->expiry.dwLowDateTime, cookie_container->expiry.dwHighDateTime,
330 cookie_container->create.dwLowDateTime, cookie_container->create.dwHighDateTime);
331 if(!WriteFile(cookie_handle, buf, strlen(buf), NULL, NULL)) {
332 do_save = FALSE;
333 break;
337 CloseHandle(cookie_handle);
338 if(!do_save) {
339 ERR("error saving cookie file\n");
340 DeleteFileW(cookie_file);
341 return FALSE;
344 memset(&time, 0, sizeof(time));
345 return CommitUrlCacheEntryW(cookie_url, cookie_file, time, time, 0, NULL, 0, txtW, 0);
348 /* adds a cookie to the domain */
349 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data,
350 FILETIME expiry, FILETIME create, DWORD flags)
352 cookie *newCookie = heap_alloc(sizeof(cookie));
353 if (!newCookie)
354 return NULL;
356 newCookie->lpCookieName = heap_strdupW(name);
357 newCookie->lpCookieData = heap_strdupW(data);
359 if (!newCookie->lpCookieName || !newCookie->lpCookieData)
361 heap_free(newCookie->lpCookieName);
362 heap_free(newCookie->lpCookieData);
363 heap_free(newCookie);
365 return NULL;
368 newCookie->flags = flags;
369 newCookie->expiry = expiry;
370 newCookie->create = create;
372 TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
374 list_add_tail(&domain->cookie_list, &newCookie->entry);
375 newCookie->parent = domain;
376 return newCookie;
380 /* finds a cookie in the domain matching the cookie name */
381 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
383 struct list * cursor;
384 TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
386 LIST_FOR_EACH(cursor, &domain->cookie_list)
388 cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
389 BOOL candidate = TRUE;
390 if (candidate && lpszCookieName)
392 if (candidate && !searchCookie->lpCookieName)
393 candidate = FALSE;
394 if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
395 candidate = FALSE;
397 if (candidate)
398 return searchCookie;
400 return NULL;
403 /* removes a cookie from the list, if its the last cookie we also remove the domain */
404 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
406 heap_free(deadCookie->lpCookieName);
407 heap_free(deadCookie->lpCookieData);
408 list_remove(&deadCookie->entry);
410 /* special case: last cookie, lets remove the domain to save memory */
411 if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
412 COOKIE_deleteDomain(deadCookie->parent);
413 heap_free(deadCookie);
416 /* allocates a domain and adds it to the end */
417 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
419 cookie_domain *newDomain = heap_alloc(sizeof(cookie_domain));
421 list_init(&newDomain->entry);
422 list_init(&newDomain->cookie_list);
423 newDomain->lpCookieDomain = heap_strdupW(domain);
424 newDomain->lpCookiePath = heap_strdupW(path);
426 list_add_tail(&domain_list, &newDomain->entry);
428 TRACE("Adding domain: %p\n", newDomain);
429 return newDomain;
432 static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
434 URL_COMPONENTSW UrlComponents;
436 UrlComponents.lpszExtraInfo = NULL;
437 UrlComponents.lpszPassword = NULL;
438 UrlComponents.lpszScheme = NULL;
439 UrlComponents.lpszUrlPath = path;
440 UrlComponents.lpszUserName = NULL;
441 UrlComponents.lpszHostName = hostName;
442 UrlComponents.dwExtraInfoLength = 0;
443 UrlComponents.dwPasswordLength = 0;
444 UrlComponents.dwSchemeLength = 0;
445 UrlComponents.dwUserNameLength = 0;
446 UrlComponents.dwHostNameLength = hostNameLen;
447 UrlComponents.dwUrlPathLength = pathLen;
449 if (!InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents)) return FALSE;
451 /* discard the webpage off the end of the path */
452 if (UrlComponents.dwUrlPathLength)
454 if (path[UrlComponents.dwUrlPathLength - 1] != '/')
456 WCHAR *ptr;
457 if ((ptr = strrchrW(path, '/'))) *(++ptr) = 0;
458 else
460 path[0] = '/';
461 path[1] = 0;
465 else if (pathLen >= 2)
467 path[0] = '/';
468 path[1] = 0;
470 return TRUE;
473 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
474 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
475 cookie_domain *searchDomain, BOOL allow_partial)
477 TRACE("searching on domain %p\n", searchDomain);
478 if (lpszCookieDomain)
480 if (!searchDomain->lpCookieDomain)
481 return FALSE;
483 TRACE("comparing domain %s with %s\n",
484 debugstr_w(lpszCookieDomain),
485 debugstr_w(searchDomain->lpCookieDomain));
487 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
488 return FALSE;
489 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
490 return FALSE;
492 if (lpszCookiePath)
494 INT len;
495 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
496 /* paths match at the beginning. so a path of /foo would match
497 * /foobar and /foo/bar
499 if (!searchDomain->lpCookiePath)
500 return FALSE;
501 if (allow_partial)
503 len = lstrlenW(searchDomain->lpCookiePath);
504 if (strncmpiW(searchDomain->lpCookiePath, lpszCookiePath, len)!=0)
505 return FALSE;
507 else if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
508 return FALSE;
511 return TRUE;
514 /* remove a domain from the list and delete it */
515 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
517 struct list * cursor;
518 while ((cursor = list_tail(&deadDomain->cookie_list)))
520 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
521 list_remove(cursor);
523 heap_free(deadDomain->lpCookieDomain);
524 heap_free(deadDomain->lpCookiePath);
526 list_remove(&deadDomain->entry);
528 heap_free(deadDomain);
531 BOOL get_cookie(const WCHAR *host, const WCHAR *path, WCHAR *cookie_data, DWORD *size)
533 unsigned cnt = 0, len, domain_count = 0, cookie_count = 0;
534 cookie_domain *domain;
535 FILETIME tm;
537 GetSystemTimeAsFileTime(&tm);
539 EnterCriticalSection(&cookie_cs);
541 load_persistent_cookie(host, path);
543 LIST_FOR_EACH_ENTRY(domain, &domain_list, cookie_domain, entry) {
544 struct list *cursor, *cursor2;
546 if(!COOKIE_matchDomain(host, path, domain, TRUE))
547 continue;
549 domain_count++;
550 TRACE("found domain %p\n", domain);
552 LIST_FOR_EACH_SAFE(cursor, cursor2, &domain->cookie_list) {
553 cookie *cookie_iter = LIST_ENTRY(cursor, cookie, entry);
555 /* check for expiry */
556 if((cookie_iter->expiry.dwLowDateTime != 0 || cookie_iter->expiry.dwHighDateTime != 0)
557 && CompareFileTime(&tm, &cookie_iter->expiry) > 0)
559 TRACE("Found expired cookie. deleting\n");
560 COOKIE_deleteCookie(cookie_iter, FALSE);
561 continue;
564 if(!cookie_data) { /* return the size of the buffer required to lpdwSize */
565 if (cookie_count)
566 cnt += 2; /* '; ' */
567 cnt += strlenW(cookie_iter->lpCookieName);
568 if ((len = strlenW(cookie_iter->lpCookieData))) {
569 cnt += 1; /* = */
570 cnt += len;
572 }else {
573 static const WCHAR szsc[] = { ';',' ',0 };
574 static const WCHAR szname[] = { '%','s',0 };
575 static const WCHAR szdata[] = { '=','%','s',0 };
577 if (cookie_count) cnt += snprintfW(cookie_data + cnt, *size - cnt, szsc);
578 cnt += snprintfW(cookie_data + cnt, *size - cnt, szname, cookie_iter->lpCookieName);
580 if (cookie_iter->lpCookieData[0])
581 cnt += snprintfW(cookie_data + cnt, *size - cnt, szdata, cookie_iter->lpCookieData);
583 TRACE("Cookie: %s\n", debugstr_w(cookie_data));
585 cookie_count++;
589 LeaveCriticalSection(&cookie_cs);
591 if (!domain_count) {
592 TRACE("no cookies found for %s\n", debugstr_w(host));
593 SetLastError(ERROR_NO_MORE_ITEMS);
594 return FALSE;
597 if(!cookie_data) {
598 *size = (cnt + 1) * sizeof(WCHAR);
599 TRACE("returning %u\n", *size);
600 return TRUE;
603 *size = cnt + 1;
605 TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count, debugstr_w(cookie_data));
606 return cnt != 0;
609 /***********************************************************************
610 * InternetGetCookieW (WININET.@)
612 * Retrieve cookie from the specified url
614 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
615 * So it won't be implemented here.
617 * RETURNS
618 * TRUE on success
619 * FALSE on failure
622 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
623 LPWSTR lpCookieData, LPDWORD lpdwSize)
625 WCHAR host[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
626 BOOL ret;
628 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), lpCookieData, lpdwSize);
630 if (!lpszUrl)
632 SetLastError(ERROR_INVALID_PARAMETER);
633 return FALSE;
636 host[0] = 0;
637 ret = COOKIE_crackUrlSimple(lpszUrl, host, sizeof(host)/sizeof(host[0]), path, sizeof(path)/sizeof(path[0]));
638 if (!ret || !host[0]) {
639 SetLastError(ERROR_INVALID_PARAMETER);
640 return FALSE;
643 return get_cookie(host, path, lpCookieData, lpdwSize);
647 /***********************************************************************
648 * InternetGetCookieA (WININET.@)
650 * Retrieve cookie from the specified url
652 * RETURNS
653 * TRUE on success
654 * FALSE on failure
657 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
658 LPSTR lpCookieData, LPDWORD lpdwSize)
660 WCHAR *url, *name;
661 DWORD len;
662 BOOL r;
664 TRACE("(%s %s %p %p(%u))\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
665 lpCookieData, lpdwSize, lpdwSize ? *lpdwSize : 0);
667 url = heap_strdupAtoW(lpszUrl);
668 name = heap_strdupAtoW(lpszCookieName);
670 r = InternetGetCookieW( url, name, NULL, &len );
671 if( r )
673 WCHAR *szCookieData;
675 szCookieData = heap_alloc(len * sizeof(WCHAR));
676 if( !szCookieData )
678 r = FALSE;
680 else
682 r = InternetGetCookieW( url, name, szCookieData, &len );
684 *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
685 lpCookieData, lpCookieData ? *lpdwSize : 0, NULL, NULL );
687 heap_free( szCookieData );
690 heap_free( name );
691 heap_free( url );
692 return r;
696 /***********************************************************************
697 * IsDomainLegalCookieDomainW (WININET.@)
699 BOOL WINAPI IsDomainLegalCookieDomainW( LPCWSTR s1, LPCWSTR s2 )
701 DWORD s1_len, s2_len;
703 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1), debugstr_w(s2));
705 if (!s1 || !s2)
707 SetLastError(ERROR_INVALID_PARAMETER);
708 return FALSE;
710 if (s1[0] == '.' || !s1[0] || s2[0] == '.' || !s2[0])
712 SetLastError(ERROR_INVALID_NAME);
713 return FALSE;
715 if(!strchrW(s1, '.') || !strchrW(s2, '.'))
716 return FALSE;
718 s1_len = strlenW(s1);
719 s2_len = strlenW(s2);
720 if (s1_len > s2_len)
721 return FALSE;
723 if (strncmpiW(s1, s2+s2_len-s1_len, s1_len) || (s2_len>s1_len && s2[s2_len-s1_len-1]!='.'))
725 SetLastError(ERROR_INVALID_PARAMETER);
726 return FALSE;
729 return TRUE;
732 BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cookie_data)
734 cookie_domain *thisCookieDomain = NULL;
735 cookie *thisCookie;
736 struct list *cursor;
737 LPWSTR data, value;
738 WCHAR *ptr;
739 FILETIME expiry, create;
740 BOOL expired = FALSE, update_persistent = FALSE;
741 DWORD flags = 0;
743 value = data = heap_strdupW(cookie_data);
744 if (!data)
746 ERR("could not allocate the cookie data buffer\n");
747 return FALSE;
750 memset(&expiry,0,sizeof(expiry));
751 GetSystemTimeAsFileTime(&create);
753 /* lots of information can be parsed out of the cookie value */
755 ptr = data;
756 for (;;)
758 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
759 static const WCHAR szPath[] = {'p','a','t','h','=',0};
760 static const WCHAR szExpires[] = {'e','x','p','i','r','e','s','=',0};
761 static const WCHAR szSecure[] = {'s','e','c','u','r','e',0};
762 static const WCHAR szHttpOnly[] = {'h','t','t','p','o','n','l','y',0};
764 if (!(ptr = strchrW(ptr,';'))) break;
765 *ptr++ = 0;
767 if (value != data) heap_free(value);
768 value = heap_alloc((ptr - data) * sizeof(WCHAR));
769 if (value == NULL)
771 heap_free(data);
772 ERR("could not allocate the cookie value buffer\n");
773 return FALSE;
775 strcpyW(value, data);
777 while (*ptr == ' ') ptr++; /* whitespace */
779 if (strncmpiW(ptr, szDomain, 7) == 0)
781 WCHAR *end_ptr;
783 ptr += sizeof(szDomain)/sizeof(szDomain[0])-1;
784 if(*ptr == '.')
785 ptr++;
786 end_ptr = strchrW(ptr, ';');
787 if(end_ptr)
788 *end_ptr = 0;
790 if(!IsDomainLegalCookieDomainW(ptr, domain))
792 if(value != data)
793 heap_free(value);
794 heap_free(data);
795 return FALSE;
798 if(end_ptr)
799 *end_ptr = ';';
801 domain = ptr;
802 TRACE("Parsing new domain %s\n",debugstr_w(domain));
804 else if (strncmpiW(ptr, szPath, 5) == 0)
806 ptr+=strlenW(szPath);
807 path = ptr;
808 TRACE("Parsing new path %s\n",debugstr_w(path));
810 else if (strncmpiW(ptr, szExpires, 8) == 0)
812 SYSTEMTIME st;
813 ptr+=strlenW(szExpires);
814 if (InternetTimeToSystemTimeW(ptr, &st, 0))
816 SystemTimeToFileTime(&st, &expiry);
818 if (CompareFileTime(&create,&expiry) > 0)
820 TRACE("Cookie already expired.\n");
821 expired = TRUE;
825 else if (strncmpiW(ptr, szSecure, 6) == 0)
827 FIXME("secure not handled (%s)\n",debugstr_w(ptr));
828 ptr += strlenW(szSecure);
830 else if (strncmpiW(ptr, szHttpOnly, 8) == 0)
832 FIXME("httponly not handled (%s)\n",debugstr_w(ptr));
833 ptr += strlenW(szHttpOnly);
835 else if (*ptr)
837 FIXME("Unknown additional option %s\n",debugstr_w(ptr));
838 break;
842 EnterCriticalSection(&cookie_cs);
844 load_persistent_cookie(domain, path);
846 LIST_FOR_EACH(cursor, &domain_list)
848 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
849 if (COOKIE_matchDomain(domain, path, thisCookieDomain, FALSE))
850 break;
851 thisCookieDomain = NULL;
854 if (!thisCookieDomain)
856 if (!expired)
857 thisCookieDomain = COOKIE_addDomain(domain, path);
858 else
860 heap_free(data);
861 if (value != data) heap_free(value);
862 LeaveCriticalSection(&cookie_cs);
863 return TRUE;
867 if(!expiry.dwLowDateTime && !expiry.dwHighDateTime)
868 flags |= INTERNET_COOKIE_IS_SESSION;
869 else
870 update_persistent = TRUE;
872 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, cookie_name)))
874 if (!(thisCookie->flags & INTERNET_COOKIE_IS_SESSION))
875 update_persistent = TRUE;
876 COOKIE_deleteCookie(thisCookie, FALSE);
879 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name),
880 debugstr_w(value), debugstr_w(thisCookieDomain->lpCookieDomain),debugstr_w(thisCookieDomain->lpCookiePath));
882 if (!expired && !COOKIE_addCookie(thisCookieDomain, cookie_name, value, expiry, create, flags))
884 heap_free(data);
885 if (value != data) heap_free(value);
886 LeaveCriticalSection(&cookie_cs);
887 return FALSE;
889 heap_free(data);
890 if (value != data) heap_free(value);
892 if (!update_persistent || save_persistent_cookie(thisCookieDomain))
894 LeaveCriticalSection(&cookie_cs);
895 return TRUE;
897 LeaveCriticalSection(&cookie_cs);
898 return FALSE;
901 /***********************************************************************
902 * InternetSetCookieW (WININET.@)
904 * Sets cookie for the specified url
906 * RETURNS
907 * TRUE on success
908 * FALSE on failure
911 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
912 LPCWSTR lpCookieData)
914 BOOL ret;
915 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
917 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
918 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
920 if (!lpszUrl || !lpCookieData)
922 SetLastError(ERROR_INVALID_PARAMETER);
923 return FALSE;
926 hostName[0] = 0;
927 ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
928 if (!ret || !hostName[0]) return FALSE;
930 if (!lpszCookieName)
932 WCHAR *cookie, *data;
934 cookie = heap_strdupW(lpCookieData);
935 if (!cookie)
937 SetLastError(ERROR_OUTOFMEMORY);
938 return FALSE;
941 /* some apps (or is it us??) try to add a cookie with no cookie name, but
942 * the cookie data in the form of name[=data].
944 if (!(data = strchrW(cookie, '='))) data = cookie + strlenW(cookie);
945 else *data++ = 0;
947 ret = set_cookie(hostName, path, cookie, data);
949 heap_free(cookie);
950 return ret;
952 return set_cookie(hostName, path, lpszCookieName, lpCookieData);
956 /***********************************************************************
957 * InternetSetCookieA (WININET.@)
959 * Sets cookie for the specified url
961 * RETURNS
962 * TRUE on success
963 * FALSE on failure
966 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
967 LPCSTR lpCookieData)
969 LPWSTR data, url, name;
970 BOOL r;
972 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
973 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
975 url = heap_strdupAtoW(lpszUrl);
976 name = heap_strdupAtoW(lpszCookieName);
977 data = heap_strdupAtoW(lpCookieData);
979 r = InternetSetCookieW( url, name, data );
981 heap_free( data );
982 heap_free( name );
983 heap_free( url );
984 return r;
987 /***********************************************************************
988 * InternetSetCookieExA (WININET.@)
990 * See InternetSetCookieExW.
992 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
993 DWORD dwFlags, DWORD_PTR dwReserved)
995 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
996 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
997 dwFlags, dwReserved);
999 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1000 return InternetSetCookieA(lpszURL, lpszCookieName, lpszCookieData);
1003 /***********************************************************************
1004 * InternetSetCookieExW (WININET.@)
1006 * Sets a cookie for the specified URL.
1008 * RETURNS
1009 * TRUE on success
1010 * FALSE on failure
1013 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
1014 DWORD dwFlags, DWORD_PTR dwReserved)
1016 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1017 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
1018 dwFlags, dwReserved);
1020 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1021 return InternetSetCookieW(lpszURL, lpszCookieName, lpszCookieData);
1024 /***********************************************************************
1025 * InternetGetCookieExA (WININET.@)
1027 * See InternetGetCookieExW.
1029 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
1030 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
1032 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1033 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
1034 pcchCookieData, dwFlags, lpReserved);
1036 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1037 return InternetGetCookieA(pchURL, pchCookieName, pchCookieData, pcchCookieData);
1040 /***********************************************************************
1041 * InternetGetCookieExW (WININET.@)
1043 * Retrieve cookie for the specified URL.
1045 * RETURNS
1046 * TRUE on success
1047 * FALSE on failure
1050 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
1051 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
1053 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1054 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
1055 pcchCookieData, dwFlags, lpReserved);
1057 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1058 return InternetGetCookieW(pchURL, pchCookieName, pchCookieData, pcchCookieData);
1061 /***********************************************************************
1062 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1064 * Clears all per-site decisions about cookies.
1066 * RETURNS
1067 * TRUE on success
1068 * FALSE on failure
1071 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
1073 FIXME("stub\n");
1074 return TRUE;
1077 /***********************************************************************
1078 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1080 * See InternetEnumPerSiteCookieDecisionW.
1082 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, ULONG *pcSiteNameSize,
1083 ULONG *pdwDecision, ULONG dwIndex )
1085 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1086 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1087 return FALSE;
1090 /***********************************************************************
1091 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1093 * Enumerates all per-site decisions about cookies.
1095 * RETURNS
1096 * TRUE on success
1097 * FALSE on failure
1100 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, ULONG *pcSiteNameSize,
1101 ULONG *pdwDecision, ULONG dwIndex )
1103 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1104 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1105 return FALSE;
1108 /***********************************************************************
1109 * InternetGetPerSiteCookieDecisionA (WININET.@)
1111 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, ULONG *pResult )
1113 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
1114 return FALSE;
1117 /***********************************************************************
1118 * InternetGetPerSiteCookieDecisionW (WININET.@)
1120 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, ULONG *pResult )
1122 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
1123 return FALSE;
1126 /***********************************************************************
1127 * InternetSetPerSiteCookieDecisionA (WININET.@)
1129 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
1131 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
1132 return FALSE;
1135 /***********************************************************************
1136 * InternetSetPerSiteCookieDecisionW (WININET.@)
1138 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
1140 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
1141 return FALSE;
1144 void free_cookie(void)
1146 DeleteCriticalSection(&cookie_cs);