d2d1: Implement d2d_d3d_render_target_CreateBitmap().
[wine/multimedia.git] / dlls / wininet / cookie.c
blob57e5424672334287c20f458ae81f50895ef98ee5
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 #include <assert.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wininet.h"
42 #include "winerror.h"
44 #include "wine/debug.h"
45 #include "internet.h"
47 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
50 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
52 /* FIXME
53 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
56 struct _cookie_domain_t;
57 struct _cookie_container_t;
59 typedef struct _cookie_t {
60 struct list entry;
62 struct _cookie_container_t *container;
64 WCHAR *name;
65 WCHAR *data;
66 DWORD flags;
67 FILETIME expiry;
68 FILETIME create;
69 } cookie_t;
71 typedef struct _cookie_container_t {
72 struct list entry;
74 WCHAR *path;
75 struct _cookie_domain_t *domain;
77 struct list cookie_list;
78 } cookie_container_t;
80 typedef struct _cookie_domain_t {
81 struct list entry;
83 WCHAR *domain;
84 unsigned subdomain_len;
86 struct _cookie_domain_t *parent;
87 struct list subdomain_list;
89 /* List of stored paths sorted by length of the path. */
90 struct list path_list;
91 } cookie_domain_t;
93 static CRITICAL_SECTION cookie_cs;
94 static CRITICAL_SECTION_DEBUG cookie_cs_debug =
96 0, 0, &cookie_cs,
97 { &cookie_cs_debug.ProcessLocksList, &cookie_cs_debug.ProcessLocksList },
98 0, 0, { (DWORD_PTR)(__FILE__ ": cookie_cs") }
100 static CRITICAL_SECTION cookie_cs = { &cookie_cs_debug, -1, 0, 0, 0, 0 };
101 static struct list domain_list = LIST_INIT(domain_list);
103 static cookie_domain_t *get_cookie_domain(const WCHAR *domain, BOOL create)
105 const WCHAR *ptr = domain + strlenW(domain), *ptr_end, *subdomain_ptr;
106 cookie_domain_t *iter, *current_domain, *prev_domain = NULL;
107 struct list *current_list = &domain_list;
109 while(1) {
110 for(ptr_end = ptr--; ptr > domain && *ptr != '.'; ptr--);
111 subdomain_ptr = *ptr == '.' ? ptr+1 : ptr;
113 current_domain = NULL;
114 LIST_FOR_EACH_ENTRY(iter, current_list, cookie_domain_t, entry) {
115 if(ptr_end-subdomain_ptr == iter->subdomain_len && !memcmp(subdomain_ptr, iter->domain, iter->subdomain_len)) {
116 current_domain = iter;
117 break;
121 if(!current_domain) {
122 if(!create)
123 return prev_domain;
125 current_domain = heap_alloc(sizeof(*current_domain));
126 if(!current_domain)
127 return NULL;
129 current_domain->domain = heap_strdupW(subdomain_ptr);
130 if(!current_domain->domain) {
131 heap_free(current_domain);
132 return NULL;
135 current_domain->subdomain_len = ptr_end-subdomain_ptr;
137 current_domain->parent = prev_domain;
138 list_init(&current_domain->path_list);
139 list_init(&current_domain->subdomain_list);
141 list_add_tail(current_list, &current_domain->entry);
144 if(ptr == domain)
145 return current_domain;
147 prev_domain = current_domain;
148 current_list = &current_domain->subdomain_list;
152 static cookie_container_t *get_cookie_container(const WCHAR *domain, const WCHAR *path, BOOL create)
154 cookie_domain_t *cookie_domain;
155 cookie_container_t *cookie_container, *iter;
156 size_t path_len, len;
158 cookie_domain = get_cookie_domain(domain, create);
159 if(!cookie_domain)
160 return NULL;
162 path_len = strlenW(path);
164 LIST_FOR_EACH_ENTRY(cookie_container, &cookie_domain->path_list, cookie_container_t, entry) {
165 len = strlenW(cookie_container->path);
166 if(len < path_len)
167 break;
169 if(!strcmpiW(cookie_container->path, path))
170 return cookie_container;
173 if(!create)
174 return NULL;
176 cookie_container = heap_alloc(sizeof(*cookie_container));
177 if(!cookie_container)
178 return NULL;
180 cookie_container->path = heap_strdupW(path);
181 if(!cookie_container->path) {
182 heap_free(cookie_container);
183 return NULL;
186 cookie_container->domain = cookie_domain;
187 list_init(&cookie_container->cookie_list);
190 LIST_FOR_EACH_ENTRY(iter, &cookie_domain->path_list, cookie_container_t, entry) {
191 if(strlenW(iter->path) <= path_len) {
192 list_add_before(&iter->entry, &cookie_container->entry);
193 return cookie_container;
197 list_add_tail(&cookie_domain->path_list, &cookie_container->entry);
198 return cookie_container;
201 static void delete_cookie(cookie_t *cookie)
203 list_remove(&cookie->entry);
205 heap_free(cookie->name);
206 heap_free(cookie->data);
207 heap_free(cookie);
210 static cookie_t *alloc_cookie(const WCHAR *name, const WCHAR *data, FILETIME expiry, FILETIME create_time, DWORD flags)
212 cookie_t *new_cookie;
214 new_cookie = heap_alloc(sizeof(*new_cookie));
215 if(!new_cookie)
216 return NULL;
218 new_cookie->expiry = expiry;
219 new_cookie->create = create_time;
220 new_cookie->flags = flags;
221 list_init(&new_cookie->entry);
223 new_cookie->name = heap_strdupW(name);
224 new_cookie->data = heap_strdupW(data);
225 if((name && !new_cookie->name) || (data && !new_cookie->data)) {
226 delete_cookie(new_cookie);
227 return NULL;
230 return new_cookie;
233 static cookie_t *find_cookie(cookie_container_t *container, const WCHAR *name)
235 cookie_t *iter;
237 LIST_FOR_EACH_ENTRY(iter, &container->cookie_list, cookie_t, entry) {
238 if(!strcmpiW(iter->name, name))
239 return iter;
242 return NULL;
245 static void add_cookie(cookie_container_t *container, cookie_t *new_cookie)
247 TRACE("Adding %s=%s to %s %s\n", debugstr_w(new_cookie->name), debugstr_w(new_cookie->data),
248 debugstr_w(container->domain->domain), debugstr_w(container->path));
250 list_add_tail(&container->cookie_list, &new_cookie->entry);
251 new_cookie->container = container;
254 static void replace_cookie(cookie_container_t *container, cookie_t *new_cookie)
256 cookie_t *old_cookie;
258 old_cookie = find_cookie(container, new_cookie->name);
259 if(old_cookie)
260 delete_cookie(old_cookie);
262 add_cookie(container, new_cookie);
265 static BOOL cookie_match_path(cookie_container_t *container, const WCHAR *path)
267 return !strncmpiW(container->path, path, strlenW(container->path));
270 static BOOL create_cookie_url(LPCWSTR domain, LPCWSTR path, WCHAR *buf, DWORD buf_len)
272 static const WCHAR cookie_prefix[] = {'C','o','o','k','i','e',':'};
274 WCHAR *p;
275 DWORD len;
277 if(buf_len < sizeof(cookie_prefix)/sizeof(WCHAR))
278 return FALSE;
279 memcpy(buf, cookie_prefix, sizeof(cookie_prefix));
280 buf += sizeof(cookie_prefix)/sizeof(WCHAR);
281 buf_len -= sizeof(cookie_prefix)/sizeof(WCHAR);
282 p = buf;
284 len = buf_len;
285 if(!GetUserNameW(buf, &len))
286 return FALSE;
287 buf += len-1;
288 buf_len -= len-1;
290 if(!buf_len)
291 return FALSE;
292 *(buf++) = '@';
293 buf_len--;
295 len = strlenW(domain);
296 if(len >= buf_len)
297 return FALSE;
298 memcpy(buf, domain, len*sizeof(WCHAR));
299 buf += len;
300 buf_len -= len;
302 len = strlenW(path);
303 if(len >= buf_len)
304 return FALSE;
305 memcpy(buf, path, len*sizeof(WCHAR));
306 buf += len;
308 *buf = 0;
310 for(; *p; p++)
311 *p = tolowerW(*p);
312 return TRUE;
315 static BOOL load_persistent_cookie(LPCWSTR domain, LPCWSTR path)
317 INTERNET_CACHE_ENTRY_INFOW *info;
318 cookie_container_t *cookie_container;
319 cookie_t *new_cookie;
320 WCHAR cookie_url[MAX_PATH];
321 HANDLE cookie;
322 char *str = NULL, *pbeg, *pend;
323 DWORD size, flags;
324 WCHAR *name, *data;
325 FILETIME expiry, create, time;
327 if (!create_cookie_url(domain, path, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
328 return FALSE;
330 size = 0;
331 RetrieveUrlCacheEntryStreamW(cookie_url, NULL, &size, FALSE, 0);
332 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
333 return TRUE;
334 info = heap_alloc(size);
335 if(!info)
336 return FALSE;
337 cookie = RetrieveUrlCacheEntryStreamW(cookie_url, info, &size, FALSE, 0);
338 size = info->dwSizeLow;
339 heap_free(info);
340 if(!cookie)
341 return FALSE;
343 if(!(str = heap_alloc(size+1)) || !ReadUrlCacheEntryStream(cookie, 0, str, &size, 0)) {
344 UnlockUrlCacheEntryStream(cookie, 0);
345 heap_free(str);
346 return FALSE;
348 str[size] = 0;
349 UnlockUrlCacheEntryStream(cookie, 0);
351 cookie_container = get_cookie_container(domain, path, TRUE);
352 if(!cookie_container)
353 return FALSE;
355 GetSystemTimeAsFileTime(&time);
356 for(pbeg=str; pbeg && *pbeg; name=data=NULL) {
357 pend = strchr(pbeg, '\n');
358 if(!pend)
359 break;
360 *pend = 0;
361 name = heap_strdupAtoW(pbeg);
363 pbeg = pend+1;
364 pend = strchr(pbeg, '\n');
365 if(!pend)
366 break;
367 *pend = 0;
368 data = heap_strdupAtoW(pbeg);
370 pbeg = pend+1;
371 pbeg = strchr(pend+1, '\n');
372 if(!pbeg)
373 break;
374 sscanf(pbeg, "%u %u %u %u %u", &flags, &expiry.dwLowDateTime, &expiry.dwHighDateTime,
375 &create.dwLowDateTime, &create.dwHighDateTime);
377 /* skip "*\n" */
378 pbeg = strchr(pbeg, '*');
379 if(pbeg) {
380 pbeg++;
381 if(*pbeg)
382 pbeg++;
385 if(!name || !data)
386 break;
388 if(CompareFileTime(&time, &expiry) <= 0) {
389 new_cookie = alloc_cookie(NULL, NULL, expiry, create, flags);
390 if(!new_cookie)
391 break;
393 new_cookie->name = name;
394 new_cookie->data = data;
396 replace_cookie(cookie_container, new_cookie);
397 }else {
398 heap_free(name);
399 heap_free(data);
402 heap_free(str);
403 heap_free(name);
404 heap_free(data);
406 return TRUE;
409 static BOOL save_persistent_cookie(cookie_container_t *container)
411 static const WCHAR txtW[] = {'t','x','t',0};
413 WCHAR cookie_url[MAX_PATH], cookie_file[MAX_PATH];
414 HANDLE cookie_handle;
415 cookie_t *cookie_container = NULL, *cookie_iter;
416 BOOL do_save = FALSE;
417 char buf[64], *dyn_buf;
418 FILETIME time;
419 DWORD bytes_written;
421 if (!create_cookie_url(container->domain->domain, container->path, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
422 return FALSE;
424 /* check if there's anything to save */
425 GetSystemTimeAsFileTime(&time);
426 LIST_FOR_EACH_ENTRY_SAFE(cookie_container, cookie_iter, &container->cookie_list, cookie_t, entry)
428 if((cookie_container->expiry.dwLowDateTime || cookie_container->expiry.dwHighDateTime)
429 && CompareFileTime(&time, &cookie_container->expiry) > 0) {
430 delete_cookie(cookie_container);
431 continue;
434 if(!(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)) {
435 do_save = TRUE;
436 break;
439 if(!do_save) {
440 DeleteUrlCacheEntryW(cookie_url);
441 return TRUE;
444 if(!CreateUrlCacheEntryW(cookie_url, 0, txtW, cookie_file, 0))
445 return FALSE;
446 cookie_handle = CreateFileW(cookie_file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
447 if(cookie_handle == INVALID_HANDLE_VALUE) {
448 DeleteFileW(cookie_file);
449 return FALSE;
452 LIST_FOR_EACH_ENTRY(cookie_container, &container->cookie_list, cookie_t, entry)
454 if(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)
455 continue;
457 dyn_buf = heap_strdupWtoA(cookie_container->name);
458 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
459 heap_free(dyn_buf);
460 do_save = FALSE;
461 break;
463 heap_free(dyn_buf);
464 if(!WriteFile(cookie_handle, "\n", 1, &bytes_written, NULL)) {
465 do_save = FALSE;
466 break;
469 dyn_buf = heap_strdupWtoA(cookie_container->data);
470 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
471 heap_free(dyn_buf);
472 do_save = FALSE;
473 break;
475 heap_free(dyn_buf);
476 if(!WriteFile(cookie_handle, "\n", 1, &bytes_written, NULL)) {
477 do_save = FALSE;
478 break;
481 dyn_buf = heap_strdupWtoA(container->domain->domain);
482 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
483 heap_free(dyn_buf);
484 do_save = FALSE;
485 break;
487 heap_free(dyn_buf);
489 dyn_buf = heap_strdupWtoA(container->path);
490 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
491 heap_free(dyn_buf);
492 do_save = FALSE;
493 break;
495 heap_free(dyn_buf);
497 sprintf(buf, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container->flags,
498 cookie_container->expiry.dwLowDateTime, cookie_container->expiry.dwHighDateTime,
499 cookie_container->create.dwLowDateTime, cookie_container->create.dwHighDateTime);
500 if(!WriteFile(cookie_handle, buf, strlen(buf), &bytes_written, NULL)) {
501 do_save = FALSE;
502 break;
506 CloseHandle(cookie_handle);
507 if(!do_save) {
508 ERR("error saving cookie file\n");
509 DeleteFileW(cookie_file);
510 return FALSE;
513 memset(&time, 0, sizeof(time));
514 return CommitUrlCacheEntryW(cookie_url, cookie_file, time, time, 0, NULL, 0, txtW, 0);
517 static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
519 URL_COMPONENTSW UrlComponents;
521 UrlComponents.lpszExtraInfo = NULL;
522 UrlComponents.lpszPassword = NULL;
523 UrlComponents.lpszScheme = NULL;
524 UrlComponents.lpszUrlPath = path;
525 UrlComponents.lpszUserName = NULL;
526 UrlComponents.lpszHostName = hostName;
527 UrlComponents.dwExtraInfoLength = 0;
528 UrlComponents.dwPasswordLength = 0;
529 UrlComponents.dwSchemeLength = 0;
530 UrlComponents.dwUserNameLength = 0;
531 UrlComponents.dwHostNameLength = hostNameLen;
532 UrlComponents.dwUrlPathLength = pathLen;
534 if (!InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents)) return FALSE;
536 /* discard the webpage off the end of the path */
537 if (UrlComponents.dwUrlPathLength)
539 if (path[UrlComponents.dwUrlPathLength - 1] != '/')
541 WCHAR *ptr;
542 if ((ptr = strrchrW(path, '/'))) *(++ptr) = 0;
543 else
545 path[0] = '/';
546 path[1] = 0;
550 else if (pathLen >= 2)
552 path[0] = '/';
553 path[1] = 0;
555 return TRUE;
558 typedef struct {
559 cookie_t **cookies;
560 unsigned cnt;
561 unsigned size;
563 unsigned string_len;
564 } cookie_set_t;
566 static DWORD get_cookie(const WCHAR *host, const WCHAR *path, DWORD flags, cookie_set_t *res)
568 static const WCHAR empty_path[] = { '/',0 };
570 WCHAR *ptr, subpath[INTERNET_MAX_PATH_LENGTH];
571 const WCHAR *p;
572 cookie_domain_t *domain;
573 cookie_container_t *container;
574 unsigned len;
575 FILETIME tm;
577 GetSystemTimeAsFileTime(&tm);
579 EnterCriticalSection(&cookie_cs);
581 len = strlenW(host);
582 p = host+len;
583 while(p>host && p[-1]!='.') p--;
584 while(p != host) {
585 p--;
586 while(p>host && p[-1]!='.') p--;
587 if(p == host) break;
589 load_persistent_cookie(p, empty_path);
592 len = strlenW(path);
593 assert(len+1 < INTERNET_MAX_PATH_LENGTH);
594 memcpy(subpath, path, (len+1)*sizeof(WCHAR));
595 ptr = subpath+len;
596 do {
597 *ptr = 0;
598 load_persistent_cookie(host, subpath);
600 ptr--;
601 while(ptr>subpath && ptr[-1]!='/') ptr--;
602 }while(ptr != subpath);
604 domain = get_cookie_domain(host, FALSE);
605 if(!domain) {
606 TRACE("Unknown host %s\n", debugstr_w(host));
607 LeaveCriticalSection(&cookie_cs);
608 return ERROR_NO_MORE_ITEMS;
611 for(domain = get_cookie_domain(host, FALSE); domain; domain = domain->parent) {
612 TRACE("Trying %s domain...\n", debugstr_w(domain->domain));
614 LIST_FOR_EACH_ENTRY(container, &domain->path_list, cookie_container_t, entry) {
615 struct list *cursor, *cursor2;
617 TRACE("path %s\n", debugstr_w(container->path));
619 if(!cookie_match_path(container, path))
620 continue;
622 TRACE("found domain %p\n", domain->domain);
624 LIST_FOR_EACH_SAFE(cursor, cursor2, &container->cookie_list) {
625 cookie_t *cookie_iter = LIST_ENTRY(cursor, cookie_t, entry);
627 /* check for expiry */
628 if((cookie_iter->expiry.dwLowDateTime != 0 || cookie_iter->expiry.dwHighDateTime != 0)
629 && CompareFileTime(&tm, &cookie_iter->expiry) > 0) {
630 TRACE("Found expired cookie. deleting\n");
631 delete_cookie(cookie_iter);
632 continue;
635 if((cookie_iter->flags & INTERNET_COOKIE_HTTPONLY) && !(flags & INTERNET_COOKIE_HTTPONLY))
636 continue;
639 if(!res->size) {
640 res->cookies = heap_alloc(4*sizeof(*res->cookies));
641 if(!res->cookies)
642 continue;
643 res->size = 4;
644 }else if(res->cnt == res->size) {
645 cookie_t **new_cookies = heap_realloc(res->cookies, res->size*2*sizeof(*res->cookies));
646 if(!new_cookies)
647 continue;
648 res->cookies = new_cookies;
649 res->size *= 2;
652 if(res->cnt)
653 res->string_len += 2; /* '; ' */
654 res->cookies[res->cnt++] = cookie_iter;
656 res->string_len += strlenW(cookie_iter->name);
657 if(*cookie_iter->data)
658 res->string_len += 1 /* = */ + strlenW(cookie_iter->data);
663 LeaveCriticalSection(&cookie_cs);
664 return ERROR_SUCCESS;
667 static void cookie_set_to_string(const cookie_set_t *cookie_set, WCHAR *str)
669 WCHAR *ptr = str;
670 unsigned i, len;
672 for(i=0; i<cookie_set->cnt; i++) {
673 if(i) {
674 *ptr++ = ';';
675 *ptr++ = ' ';
678 len = strlenW(cookie_set->cookies[i]->name);
679 memcpy(ptr, cookie_set->cookies[i]->name, len*sizeof(WCHAR));
680 ptr += len;
682 if(*cookie_set->cookies[i]->data) {
683 *ptr++ = '=';
684 len = strlenW(cookie_set->cookies[i]->data);
685 memcpy(ptr, cookie_set->cookies[i]->data, len*sizeof(WCHAR));
686 ptr += len;
690 assert(ptr-str == cookie_set->string_len);
691 TRACE("%s\n", debugstr_wn(str, ptr-str));
694 DWORD get_cookie_header(const WCHAR *host, const WCHAR *path, WCHAR **ret)
696 cookie_set_t cookie_set = {0};
697 WCHAR *header, *ptr;
698 DWORD res;
700 static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' '};
702 res = get_cookie(host, path, INTERNET_COOKIE_HTTPONLY, &cookie_set);
703 if(res != ERROR_SUCCESS)
704 return res;
705 if(!cookie_set.cnt) {
706 *ret = NULL;
707 return ERROR_SUCCESS;
710 ptr = header = heap_alloc(sizeof(cookieW) + (cookie_set.string_len + 3 /* crlf0 */) * sizeof(WCHAR));
711 if(!header)
712 return ERROR_NOT_ENOUGH_MEMORY;
714 memcpy(ptr, cookieW, sizeof(cookieW));
715 ptr += sizeof(cookieW)/sizeof(*cookieW);
717 cookie_set_to_string(&cookie_set, ptr);
718 heap_free(cookie_set.cookies);
719 ptr += cookie_set.string_len;
721 *ptr++ = '\r';
722 *ptr++ = '\n';
723 *ptr++ = 0;
725 *ret = header;
726 return ERROR_SUCCESS;
729 /***********************************************************************
730 * InternetGetCookieExW (WININET.@)
732 * Retrieve cookie from the specified url
734 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
735 * So it won't be implemented here.
737 * RETURNS
738 * TRUE on success
739 * FALSE on failure
742 BOOL WINAPI InternetGetCookieExW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
743 LPWSTR lpCookieData, LPDWORD lpdwSize, DWORD flags, void *reserved)
745 WCHAR host[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
746 cookie_set_t cookie_set = {0};
747 DWORD res;
748 BOOL ret;
750 TRACE("(%s, %s, %p, %p, %x, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), lpCookieData, lpdwSize, flags, reserved);
752 if (flags)
753 FIXME("flags 0x%08x not supported\n", flags);
755 if (!lpszUrl)
757 SetLastError(ERROR_INVALID_PARAMETER);
758 return FALSE;
761 host[0] = 0;
762 ret = COOKIE_crackUrlSimple(lpszUrl, host, sizeof(host)/sizeof(host[0]), path, sizeof(path)/sizeof(path[0]));
763 if (!ret || !host[0]) {
764 SetLastError(ERROR_INVALID_PARAMETER);
765 return FALSE;
768 res = get_cookie(host, path, flags, &cookie_set);
769 if(res != ERROR_SUCCESS) {
770 SetLastError(res);
771 return FALSE;
774 if(!cookie_set.cnt) {
775 TRACE("no cookies found for %s\n", debugstr_w(host));
776 SetLastError(ERROR_NO_MORE_ITEMS);
777 return FALSE;
780 if(!lpCookieData || cookie_set.string_len+1 > *lpdwSize) {
781 *lpdwSize = (cookie_set.string_len + 1) * sizeof(WCHAR);
782 TRACE("returning %u\n", *lpdwSize);
783 if(lpCookieData) {
784 SetLastError(ERROR_INSUFFICIENT_BUFFER);
785 ret = FALSE;
787 }else {
788 *lpdwSize = cookie_set.string_len + 1;
789 cookie_set_to_string(&cookie_set, lpCookieData);
790 lpCookieData[cookie_set.string_len] = 0;
793 heap_free(cookie_set.cookies);
794 return ret;
797 /***********************************************************************
798 * InternetGetCookieW (WININET.@)
800 * Retrieve cookie for the specified URL.
802 BOOL WINAPI InternetGetCookieW(const WCHAR *url, const WCHAR *name, WCHAR *data, DWORD *size)
804 TRACE("(%s, %s, %s, %p)\n", debugstr_w(url), debugstr_w(name), debugstr_w(data), size);
806 return InternetGetCookieExW(url, name, data, size, 0, NULL);
809 /***********************************************************************
810 * InternetGetCookieExA (WININET.@)
812 * Retrieve cookie from the specified url
814 * RETURNS
815 * TRUE on success
816 * FALSE on failure
819 BOOL WINAPI InternetGetCookieExA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
820 LPSTR lpCookieData, LPDWORD lpdwSize, DWORD flags, void *reserved)
822 WCHAR *url, *name;
823 DWORD len, size;
824 BOOL r;
826 TRACE("(%s %s %p %p(%u) %x %p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
827 lpCookieData, lpdwSize, lpdwSize ? *lpdwSize : 0, flags, reserved);
829 url = heap_strdupAtoW(lpszUrl);
830 name = heap_strdupAtoW(lpszCookieName);
832 r = InternetGetCookieExW( url, name, NULL, &len, flags, reserved );
833 if( r )
835 WCHAR *szCookieData;
837 szCookieData = heap_alloc(len * sizeof(WCHAR));
838 if( !szCookieData )
840 r = FALSE;
842 else
844 r = InternetGetCookieExW( url, name, szCookieData, &len, flags, reserved );
846 if(r) {
847 size = WideCharToMultiByte( CP_ACP, 0, szCookieData, len, NULL, 0, NULL, NULL);
848 if(lpCookieData) {
849 if(*lpdwSize >= size) {
850 WideCharToMultiByte( CP_ACP, 0, szCookieData, len, lpCookieData, *lpdwSize, NULL, NULL);
851 }else {
852 SetLastError(ERROR_INSUFFICIENT_BUFFER);
853 r = FALSE;
856 *lpdwSize = size;
859 heap_free( szCookieData );
862 heap_free( name );
863 heap_free( url );
864 return r;
867 /***********************************************************************
868 * InternetGetCookieA (WININET.@)
870 * See InternetGetCookieW.
872 BOOL WINAPI InternetGetCookieA(const char *url, const char *name, char *data, DWORD *size)
874 TRACE("(%s, %s, %s, %p)\n", debugstr_a(url), debugstr_a(name), debugstr_a(data), size);
876 return InternetGetCookieExA(url, name, data, size, 0, NULL);
879 /***********************************************************************
880 * IsDomainLegalCookieDomainW (WININET.@)
882 BOOL WINAPI IsDomainLegalCookieDomainW( LPCWSTR s1, LPCWSTR s2 )
884 DWORD s1_len, s2_len;
886 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1), debugstr_w(s2));
888 if (!s1 || !s2)
890 SetLastError(ERROR_INVALID_PARAMETER);
891 return FALSE;
893 if (s1[0] == '.' || !s1[0] || s2[0] == '.' || !s2[0])
895 SetLastError(ERROR_INVALID_NAME);
896 return FALSE;
898 if(!strchrW(s1, '.') || !strchrW(s2, '.'))
899 return FALSE;
901 s1_len = strlenW(s1);
902 s2_len = strlenW(s2);
903 if (s1_len > s2_len)
904 return FALSE;
906 if (strncmpiW(s1, s2+s2_len-s1_len, s1_len) || (s2_len>s1_len && s2[s2_len-s1_len-1]!='.'))
908 SetLastError(ERROR_INVALID_PARAMETER);
909 return FALSE;
912 return TRUE;
915 DWORD set_cookie(const WCHAR *domain, const WCHAR *path, const WCHAR *cookie_name, const WCHAR *cookie_data, DWORD flags)
917 cookie_container_t *container;
918 cookie_t *thisCookie;
919 LPWSTR data, value;
920 WCHAR *ptr;
921 FILETIME expiry, create;
922 BOOL expired = FALSE, update_persistent = FALSE;
923 DWORD cookie_flags = 0;
925 TRACE("%s %s %s=%s %x\n", debugstr_w(domain), debugstr_w(path), debugstr_w(cookie_name), debugstr_w(cookie_data), flags);
927 value = data = heap_strdupW(cookie_data);
928 if (!data)
930 ERR("could not allocate the cookie data buffer\n");
931 return COOKIE_STATE_UNKNOWN;
934 memset(&expiry,0,sizeof(expiry));
935 GetSystemTimeAsFileTime(&create);
937 /* lots of information can be parsed out of the cookie value */
939 ptr = data;
940 for (;;)
942 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
943 static const WCHAR szPath[] = {'p','a','t','h','=',0};
944 static const WCHAR szExpires[] = {'e','x','p','i','r','e','s','=',0};
945 static const WCHAR szSecure[] = {'s','e','c','u','r','e',0};
946 static const WCHAR szHttpOnly[] = {'h','t','t','p','o','n','l','y',0};
948 if (!(ptr = strchrW(ptr,';'))) break;
949 *ptr++ = 0;
951 if (value != data) heap_free(value);
952 value = heap_alloc((ptr - data) * sizeof(WCHAR));
953 if (value == NULL)
955 heap_free(data);
956 ERR("could not allocate the cookie value buffer\n");
957 return COOKIE_STATE_UNKNOWN;
959 strcpyW(value, data);
961 while (*ptr == ' ') ptr++; /* whitespace */
963 if (strncmpiW(ptr, szDomain, 7) == 0)
965 WCHAR *end_ptr;
967 ptr += sizeof(szDomain)/sizeof(szDomain[0])-1;
968 if(*ptr == '.')
969 ptr++;
970 end_ptr = strchrW(ptr, ';');
971 if(end_ptr)
972 *end_ptr = 0;
974 if(!IsDomainLegalCookieDomainW(ptr, domain))
976 if(value != data)
977 heap_free(value);
978 heap_free(data);
979 return COOKIE_STATE_UNKNOWN;
982 if(end_ptr)
983 *end_ptr = ';';
985 domain = ptr;
986 TRACE("Parsing new domain %s\n",debugstr_w(domain));
988 else if (strncmpiW(ptr, szPath, 5) == 0)
990 ptr+=strlenW(szPath);
991 path = ptr;
992 TRACE("Parsing new path %s\n",debugstr_w(path));
994 else if (strncmpiW(ptr, szExpires, 8) == 0)
996 SYSTEMTIME st;
997 ptr+=strlenW(szExpires);
998 if (InternetTimeToSystemTimeW(ptr, &st, 0))
1000 SystemTimeToFileTime(&st, &expiry);
1002 if (CompareFileTime(&create,&expiry) > 0)
1004 TRACE("Cookie already expired.\n");
1005 expired = TRUE;
1009 else if (strncmpiW(ptr, szSecure, 6) == 0)
1011 FIXME("secure not handled (%s)\n",debugstr_w(ptr));
1012 ptr += strlenW(szSecure);
1014 else if (strncmpiW(ptr, szHttpOnly, 8) == 0)
1016 if(!(flags & INTERNET_COOKIE_HTTPONLY)) {
1017 WARN("HTTP only cookie added without INTERNET_COOKIE_HTTPONLY flag\n");
1018 heap_free(data);
1019 if (value != data) heap_free(value);
1020 SetLastError(ERROR_INVALID_OPERATION);
1021 return COOKIE_STATE_REJECT;
1024 cookie_flags |= INTERNET_COOKIE_HTTPONLY;
1025 ptr += strlenW(szHttpOnly);
1027 else if (*ptr)
1029 FIXME("Unknown additional option %s\n",debugstr_w(ptr));
1030 break;
1034 EnterCriticalSection(&cookie_cs);
1036 load_persistent_cookie(domain, path);
1038 container = get_cookie_container(domain, path, !expired);
1039 if(!container) {
1040 heap_free(data);
1041 if (value != data) heap_free(value);
1042 LeaveCriticalSection(&cookie_cs);
1043 return COOKIE_STATE_ACCEPT;
1046 if(!expiry.dwLowDateTime && !expiry.dwHighDateTime)
1047 cookie_flags |= INTERNET_COOKIE_IS_SESSION;
1048 else
1049 update_persistent = TRUE;
1051 if ((thisCookie = find_cookie(container, cookie_name)))
1053 if ((thisCookie->flags & INTERNET_COOKIE_HTTPONLY) && !(flags & INTERNET_COOKIE_HTTPONLY)) {
1054 WARN("An attempt to override httponly cookie\n");
1055 SetLastError(ERROR_INVALID_OPERATION);
1056 heap_free(data);
1057 if (value != data) heap_free(value);
1058 return COOKIE_STATE_REJECT;
1061 if (!(thisCookie->flags & INTERNET_COOKIE_IS_SESSION))
1062 update_persistent = TRUE;
1063 delete_cookie(thisCookie);
1066 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name),
1067 debugstr_w(value), debugstr_w(container->domain->domain),debugstr_w(container->path));
1069 if (!expired) {
1070 cookie_t *new_cookie;
1072 new_cookie = alloc_cookie(cookie_name, value, expiry, create, cookie_flags);
1073 if(!new_cookie) {
1074 heap_free(data);
1075 if (value != data) heap_free(value);
1076 LeaveCriticalSection(&cookie_cs);
1077 return COOKIE_STATE_UNKNOWN;
1080 add_cookie(container, new_cookie);
1082 heap_free(data);
1083 if (value != data) heap_free(value);
1085 if (!update_persistent || save_persistent_cookie(container))
1087 LeaveCriticalSection(&cookie_cs);
1088 return COOKIE_STATE_ACCEPT;
1090 LeaveCriticalSection(&cookie_cs);
1091 return COOKIE_STATE_UNKNOWN;
1094 /***********************************************************************
1095 * InternetSetCookieExW (WININET.@)
1097 * Sets cookie for the specified url
1099 DWORD WINAPI InternetSetCookieExW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
1100 LPCWSTR lpCookieData, DWORD flags, DWORD_PTR reserved)
1102 BOOL ret;
1103 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
1105 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_w(lpszUrl), debugstr_w(lpszCookieName),
1106 debugstr_w(lpCookieData), flags, reserved);
1108 if (flags & ~INTERNET_COOKIE_HTTPONLY)
1109 FIXME("flags %x not supported\n", flags);
1111 if (!lpszUrl || !lpCookieData)
1113 SetLastError(ERROR_INVALID_PARAMETER);
1114 return COOKIE_STATE_UNKNOWN;
1117 hostName[0] = 0;
1118 ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
1119 if (!ret || !hostName[0]) return COOKIE_STATE_UNKNOWN;
1121 if (!lpszCookieName)
1123 WCHAR *cookie, *data;
1124 DWORD res;
1126 cookie = heap_strdupW(lpCookieData);
1127 if (!cookie)
1129 SetLastError(ERROR_OUTOFMEMORY);
1130 return COOKIE_STATE_UNKNOWN;
1133 /* some apps (or is it us??) try to add a cookie with no cookie name, but
1134 * the cookie data in the form of name[=data].
1136 if (!(data = strchrW(cookie, '='))) data = cookie + strlenW(cookie);
1137 else *data++ = 0;
1139 res = set_cookie(hostName, path, cookie, data, flags);
1141 heap_free(cookie);
1142 return res;
1144 return set_cookie(hostName, path, lpszCookieName, lpCookieData, flags);
1147 /***********************************************************************
1148 * InternetSetCookieW (WININET.@)
1150 * Sets a cookie for the specified URL.
1152 BOOL WINAPI InternetSetCookieW(const WCHAR *url, const WCHAR *name, const WCHAR *data)
1154 TRACE("(%s, %s, %s)\n", debugstr_w(url), debugstr_w(name), debugstr_w(data));
1156 return InternetSetCookieExW(url, name, data, 0, 0) == COOKIE_STATE_ACCEPT;
1159 /***********************************************************************
1160 * InternetSetCookieA (WININET.@)
1162 * Sets cookie for the specified url
1164 * RETURNS
1165 * TRUE on success
1166 * FALSE on failure
1169 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
1170 LPCSTR lpCookieData)
1172 LPWSTR data, url, name;
1173 BOOL r;
1175 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
1176 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
1178 url = heap_strdupAtoW(lpszUrl);
1179 name = heap_strdupAtoW(lpszCookieName);
1180 data = heap_strdupAtoW(lpCookieData);
1182 r = InternetSetCookieW( url, name, data );
1184 heap_free( data );
1185 heap_free( name );
1186 heap_free( url );
1187 return r;
1190 /***********************************************************************
1191 * InternetSetCookieExA (WININET.@)
1193 * See InternetSetCookieExW.
1195 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
1196 DWORD dwFlags, DWORD_PTR dwReserved)
1198 WCHAR *data, *url, *name;
1199 DWORD r;
1201 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_a(lpszURL), debugstr_a(lpszCookieName),
1202 debugstr_a(lpszCookieData), dwFlags, dwReserved);
1204 url = heap_strdupAtoW(lpszURL);
1205 name = heap_strdupAtoW(lpszCookieName);
1206 data = heap_strdupAtoW(lpszCookieData);
1208 r = InternetSetCookieExW(url, name, data, dwFlags, dwReserved);
1210 heap_free( data );
1211 heap_free( name );
1212 heap_free( url );
1213 return r;
1216 /***********************************************************************
1217 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1219 * Clears all per-site decisions about cookies.
1221 * RETURNS
1222 * TRUE on success
1223 * FALSE on failure
1226 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
1228 FIXME("stub\n");
1229 return TRUE;
1232 /***********************************************************************
1233 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1235 * See InternetEnumPerSiteCookieDecisionW.
1237 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, ULONG *pcSiteNameSize,
1238 ULONG *pdwDecision, ULONG dwIndex )
1240 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1241 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1242 return FALSE;
1245 /***********************************************************************
1246 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1248 * Enumerates all per-site decisions about cookies.
1250 * RETURNS
1251 * TRUE on success
1252 * FALSE on failure
1255 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, ULONG *pcSiteNameSize,
1256 ULONG *pdwDecision, ULONG dwIndex )
1258 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1259 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1260 return FALSE;
1263 /***********************************************************************
1264 * InternetGetPerSiteCookieDecisionA (WININET.@)
1266 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, ULONG *pResult )
1268 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
1269 return FALSE;
1272 /***********************************************************************
1273 * InternetGetPerSiteCookieDecisionW (WININET.@)
1275 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, ULONG *pResult )
1277 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
1278 return FALSE;
1281 /***********************************************************************
1282 * InternetSetPerSiteCookieDecisionA (WININET.@)
1284 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
1286 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
1287 return FALSE;
1290 /***********************************************************************
1291 * InternetSetPerSiteCookieDecisionW (WININET.@)
1293 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
1295 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
1296 return FALSE;
1299 void free_cookie(void)
1301 DeleteCriticalSection(&cookie_cs);