wininet: Accept Version in cookies.
[wine.git] / dlls / wininet / cookie.c
blobaf9dcdf9a9c1d751df460ff767d63740c564aec9
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 "ws2tcpip.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wininet.h"
34 #include "winerror.h"
36 #include "wine/debug.h"
37 #include "internet.h"
39 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
42 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
44 /* FIXME
45 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
48 struct _cookie_domain_t;
49 struct _cookie_container_t;
51 typedef struct _cookie_t {
52 struct list entry;
54 struct _cookie_container_t *container;
56 WCHAR *name;
57 WCHAR *data;
58 DWORD flags;
59 FILETIME expiry;
60 FILETIME create;
61 } cookie_t;
63 typedef struct _cookie_container_t {
64 struct list entry;
66 WCHAR *path;
67 struct _cookie_domain_t *domain;
69 struct list cookie_list;
70 } cookie_container_t;
72 typedef struct _cookie_domain_t {
73 struct list entry;
75 WCHAR *domain;
76 unsigned subdomain_len;
78 struct _cookie_domain_t *parent;
79 struct list subdomain_list;
81 /* List of stored paths sorted by length of the path. */
82 struct list path_list;
83 } cookie_domain_t;
85 static CRITICAL_SECTION cookie_cs;
86 static CRITICAL_SECTION_DEBUG cookie_cs_debug =
88 0, 0, &cookie_cs,
89 { &cookie_cs_debug.ProcessLocksList, &cookie_cs_debug.ProcessLocksList },
90 0, 0, { (DWORD_PTR)(__FILE__ ": cookie_cs") }
92 static CRITICAL_SECTION cookie_cs = { &cookie_cs_debug, -1, 0, 0, 0, 0 };
93 static struct list domain_list = LIST_INIT(domain_list);
95 static cookie_domain_t *get_cookie_domain(const WCHAR *domain, BOOL create)
97 const WCHAR *ptr = domain + strlenW(domain), *ptr_end, *subdomain_ptr;
98 cookie_domain_t *iter, *current_domain, *prev_domain = NULL;
99 struct list *current_list = &domain_list;
101 while(1) {
102 for(ptr_end = ptr--; ptr > domain && *ptr != '.'; ptr--);
103 subdomain_ptr = *ptr == '.' ? ptr+1 : ptr;
105 current_domain = NULL;
106 LIST_FOR_EACH_ENTRY(iter, current_list, cookie_domain_t, entry) {
107 if(ptr_end-subdomain_ptr == iter->subdomain_len && !memcmp(subdomain_ptr, iter->domain, iter->subdomain_len)) {
108 current_domain = iter;
109 break;
113 if(!current_domain) {
114 if(!create)
115 return prev_domain;
117 current_domain = heap_alloc(sizeof(*current_domain));
118 if(!current_domain)
119 return NULL;
121 current_domain->domain = heap_strdupW(subdomain_ptr);
122 if(!current_domain->domain) {
123 heap_free(current_domain);
124 return NULL;
127 current_domain->subdomain_len = ptr_end-subdomain_ptr;
129 current_domain->parent = prev_domain;
130 list_init(&current_domain->path_list);
131 list_init(&current_domain->subdomain_list);
133 list_add_tail(current_list, &current_domain->entry);
136 if(ptr == domain)
137 return current_domain;
139 prev_domain = current_domain;
140 current_list = &current_domain->subdomain_list;
144 static cookie_container_t *get_cookie_container(const WCHAR *domain, const WCHAR *path, BOOL create)
146 cookie_domain_t *cookie_domain;
147 cookie_container_t *cookie_container, *iter;
148 size_t path_len, len;
150 cookie_domain = get_cookie_domain(domain, create);
151 if(!cookie_domain)
152 return NULL;
154 path_len = strlenW(path);
156 LIST_FOR_EACH_ENTRY(cookie_container, &cookie_domain->path_list, cookie_container_t, entry) {
157 len = strlenW(cookie_container->path);
158 if(len < path_len)
159 break;
161 if(!strcmpiW(cookie_container->path, path))
162 return cookie_container;
165 if(!create)
166 return NULL;
168 cookie_container = heap_alloc(sizeof(*cookie_container));
169 if(!cookie_container)
170 return NULL;
172 cookie_container->path = heap_strdupW(path);
173 if(!cookie_container->path) {
174 heap_free(cookie_container);
175 return NULL;
178 cookie_container->domain = cookie_domain;
179 list_init(&cookie_container->cookie_list);
182 LIST_FOR_EACH_ENTRY(iter, &cookie_domain->path_list, cookie_container_t, entry) {
183 if(strlenW(iter->path) <= path_len) {
184 list_add_before(&iter->entry, &cookie_container->entry);
185 return cookie_container;
189 list_add_tail(&cookie_domain->path_list, &cookie_container->entry);
190 return cookie_container;
193 static void delete_cookie(cookie_t *cookie)
195 list_remove(&cookie->entry);
197 heap_free(cookie->name);
198 heap_free(cookie->data);
199 heap_free(cookie);
202 static cookie_t *alloc_cookie(const WCHAR *name, const WCHAR *data, FILETIME expiry, FILETIME create_time, DWORD flags)
204 cookie_t *new_cookie;
206 new_cookie = heap_alloc(sizeof(*new_cookie));
207 if(!new_cookie)
208 return NULL;
210 new_cookie->expiry = expiry;
211 new_cookie->create = create_time;
212 new_cookie->flags = flags;
213 list_init(&new_cookie->entry);
215 new_cookie->name = heap_strdupW(name);
216 new_cookie->data = heap_strdupW(data);
217 if((name && !new_cookie->name) || (data && !new_cookie->data)) {
218 delete_cookie(new_cookie);
219 return NULL;
222 return new_cookie;
225 static cookie_t *find_cookie(cookie_container_t *container, const WCHAR *name)
227 cookie_t *iter;
229 LIST_FOR_EACH_ENTRY(iter, &container->cookie_list, cookie_t, entry) {
230 if(!strcmpiW(iter->name, name))
231 return iter;
234 return NULL;
237 static void add_cookie(cookie_container_t *container, cookie_t *new_cookie)
239 TRACE("Adding %s=%s to %s %s\n", debugstr_w(new_cookie->name), debugstr_w(new_cookie->data),
240 debugstr_w(container->domain->domain), debugstr_w(container->path));
242 list_add_tail(&container->cookie_list, &new_cookie->entry);
243 new_cookie->container = container;
246 static void replace_cookie(cookie_container_t *container, cookie_t *new_cookie)
248 cookie_t *old_cookie;
250 old_cookie = find_cookie(container, new_cookie->name);
251 if(old_cookie)
252 delete_cookie(old_cookie);
254 add_cookie(container, new_cookie);
257 static BOOL cookie_match_path(cookie_container_t *container, const WCHAR *path)
259 return !strncmpiW(container->path, path, strlenW(container->path));
262 static BOOL create_cookie_url(LPCWSTR domain, LPCWSTR path, WCHAR *buf, DWORD buf_len)
264 static const WCHAR cookie_prefix[] = {'C','o','o','k','i','e',':'};
266 WCHAR *p;
267 DWORD len;
269 if(buf_len < sizeof(cookie_prefix)/sizeof(WCHAR))
270 return FALSE;
271 memcpy(buf, cookie_prefix, sizeof(cookie_prefix));
272 buf += sizeof(cookie_prefix)/sizeof(WCHAR);
273 buf_len -= sizeof(cookie_prefix)/sizeof(WCHAR);
274 p = buf;
276 len = buf_len;
277 if(!GetUserNameW(buf, &len))
278 return FALSE;
279 buf += len-1;
280 buf_len -= len-1;
282 if(!buf_len)
283 return FALSE;
284 *(buf++) = '@';
285 buf_len--;
287 len = strlenW(domain);
288 if(len >= buf_len)
289 return FALSE;
290 memcpy(buf, domain, len*sizeof(WCHAR));
291 buf += len;
292 buf_len -= len;
294 len = strlenW(path);
295 if(len >= buf_len)
296 return FALSE;
297 memcpy(buf, path, len*sizeof(WCHAR));
298 buf += len;
300 *buf = 0;
302 for(; *p; p++)
303 *p = tolowerW(*p);
304 return TRUE;
307 static BOOL load_persistent_cookie(LPCWSTR domain, LPCWSTR path)
309 INTERNET_CACHE_ENTRY_INFOW *info;
310 cookie_container_t *cookie_container;
311 cookie_t *new_cookie;
312 WCHAR cookie_url[MAX_PATH];
313 HANDLE cookie;
314 char *str = NULL, *pbeg, *pend;
315 DWORD size, flags;
316 WCHAR *name, *data;
317 FILETIME expiry, create, time;
319 if (!create_cookie_url(domain, path, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
320 return FALSE;
322 size = 0;
323 RetrieveUrlCacheEntryStreamW(cookie_url, NULL, &size, FALSE, 0);
324 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
325 return TRUE;
326 info = heap_alloc(size);
327 if(!info)
328 return FALSE;
329 cookie = RetrieveUrlCacheEntryStreamW(cookie_url, info, &size, FALSE, 0);
330 size = info->dwSizeLow;
331 heap_free(info);
332 if(!cookie)
333 return FALSE;
335 if(!(str = heap_alloc(size+1)) || !ReadUrlCacheEntryStream(cookie, 0, str, &size, 0)) {
336 UnlockUrlCacheEntryStream(cookie, 0);
337 heap_free(str);
338 return FALSE;
340 str[size] = 0;
341 UnlockUrlCacheEntryStream(cookie, 0);
343 cookie_container = get_cookie_container(domain, path, TRUE);
344 if(!cookie_container)
345 return FALSE;
347 GetSystemTimeAsFileTime(&time);
348 for(pbeg=str; pbeg && *pbeg; name=data=NULL) {
349 pend = strchr(pbeg, '\n');
350 if(!pend)
351 break;
352 *pend = 0;
353 name = heap_strdupAtoW(pbeg);
355 pbeg = pend+1;
356 pend = strchr(pbeg, '\n');
357 if(!pend)
358 break;
359 *pend = 0;
360 data = heap_strdupAtoW(pbeg);
362 pbeg = strchr(pend+1, '\n');
363 if(!pbeg)
364 break;
365 sscanf(pbeg, "%u %u %u %u %u", &flags, &expiry.dwLowDateTime, &expiry.dwHighDateTime,
366 &create.dwLowDateTime, &create.dwHighDateTime);
368 /* skip "*\n" */
369 pbeg = strchr(pbeg, '*');
370 if(pbeg) {
371 pbeg++;
372 if(*pbeg)
373 pbeg++;
376 if(!name || !data)
377 break;
379 if(CompareFileTime(&time, &expiry) <= 0) {
380 new_cookie = alloc_cookie(NULL, NULL, expiry, create, flags);
381 if(!new_cookie)
382 break;
384 new_cookie->name = name;
385 new_cookie->data = data;
387 replace_cookie(cookie_container, new_cookie);
388 }else {
389 heap_free(name);
390 heap_free(data);
393 heap_free(str);
394 heap_free(name);
395 heap_free(data);
397 return TRUE;
400 static BOOL save_persistent_cookie(cookie_container_t *container)
402 static const WCHAR txtW[] = {'t','x','t',0};
404 WCHAR cookie_url[MAX_PATH], cookie_file[MAX_PATH];
405 HANDLE cookie_handle;
406 cookie_t *cookie_container = NULL, *cookie_iter;
407 BOOL do_save = FALSE;
408 char buf[64], *dyn_buf;
409 FILETIME time;
410 DWORD bytes_written;
412 if (!create_cookie_url(container->domain->domain, container->path, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
413 return FALSE;
415 /* check if there's anything to save */
416 GetSystemTimeAsFileTime(&time);
417 LIST_FOR_EACH_ENTRY_SAFE(cookie_container, cookie_iter, &container->cookie_list, cookie_t, entry)
419 if((cookie_container->expiry.dwLowDateTime || cookie_container->expiry.dwHighDateTime)
420 && CompareFileTime(&time, &cookie_container->expiry) > 0) {
421 delete_cookie(cookie_container);
422 continue;
425 if(!(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)) {
426 do_save = TRUE;
427 break;
430 if(!do_save) {
431 DeleteUrlCacheEntryW(cookie_url);
432 return TRUE;
435 if(!CreateUrlCacheEntryW(cookie_url, 0, txtW, cookie_file, 0))
436 return FALSE;
437 cookie_handle = CreateFileW(cookie_file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
438 if(cookie_handle == INVALID_HANDLE_VALUE) {
439 DeleteFileW(cookie_file);
440 return FALSE;
443 LIST_FOR_EACH_ENTRY(cookie_container, &container->cookie_list, cookie_t, entry)
445 if(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)
446 continue;
448 dyn_buf = heap_strdupWtoA(cookie_container->name);
449 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
450 heap_free(dyn_buf);
451 do_save = FALSE;
452 break;
454 heap_free(dyn_buf);
455 if(!WriteFile(cookie_handle, "\n", 1, &bytes_written, NULL)) {
456 do_save = FALSE;
457 break;
460 dyn_buf = heap_strdupWtoA(cookie_container->data);
461 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
462 heap_free(dyn_buf);
463 do_save = FALSE;
464 break;
466 heap_free(dyn_buf);
467 if(!WriteFile(cookie_handle, "\n", 1, &bytes_written, NULL)) {
468 do_save = FALSE;
469 break;
472 dyn_buf = heap_strdupWtoA(container->domain->domain);
473 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
474 heap_free(dyn_buf);
475 do_save = FALSE;
476 break;
478 heap_free(dyn_buf);
480 dyn_buf = heap_strdupWtoA(container->path);
481 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &bytes_written, NULL)) {
482 heap_free(dyn_buf);
483 do_save = FALSE;
484 break;
486 heap_free(dyn_buf);
488 sprintf(buf, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container->flags,
489 cookie_container->expiry.dwLowDateTime, cookie_container->expiry.dwHighDateTime,
490 cookie_container->create.dwLowDateTime, cookie_container->create.dwHighDateTime);
491 if(!WriteFile(cookie_handle, buf, strlen(buf), &bytes_written, NULL)) {
492 do_save = FALSE;
493 break;
497 CloseHandle(cookie_handle);
498 if(!do_save) {
499 ERR("error saving cookie file\n");
500 DeleteFileW(cookie_file);
501 return FALSE;
504 memset(&time, 0, sizeof(time));
505 return CommitUrlCacheEntryW(cookie_url, cookie_file, time, time, 0, NULL, 0, txtW, 0);
508 static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
510 URL_COMPONENTSW UrlComponents;
512 UrlComponents.lpszExtraInfo = NULL;
513 UrlComponents.lpszPassword = NULL;
514 UrlComponents.lpszScheme = NULL;
515 UrlComponents.lpszUrlPath = path;
516 UrlComponents.lpszUserName = NULL;
517 UrlComponents.lpszHostName = hostName;
518 UrlComponents.dwExtraInfoLength = 0;
519 UrlComponents.dwPasswordLength = 0;
520 UrlComponents.dwSchemeLength = 0;
521 UrlComponents.dwUserNameLength = 0;
522 UrlComponents.dwHostNameLength = hostNameLen;
523 UrlComponents.dwUrlPathLength = pathLen;
525 if (!InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents)) return FALSE;
527 /* discard the webpage off the end of the path */
528 if (UrlComponents.dwUrlPathLength)
530 if (path[UrlComponents.dwUrlPathLength - 1] != '/')
532 WCHAR *ptr;
533 if ((ptr = strrchrW(path, '/'))) *(++ptr) = 0;
534 else
536 path[0] = '/';
537 path[1] = 0;
541 else if (pathLen >= 2)
543 path[0] = '/';
544 path[1] = 0;
546 return TRUE;
549 typedef struct {
550 cookie_t **cookies;
551 unsigned cnt;
552 unsigned size;
554 unsigned string_len;
555 } cookie_set_t;
557 static DWORD get_cookie(const WCHAR *host, const WCHAR *path, DWORD flags, cookie_set_t *res)
559 static const WCHAR empty_path[] = { '/',0 };
561 WCHAR *ptr, subpath[INTERNET_MAX_PATH_LENGTH];
562 const WCHAR *p;
563 cookie_domain_t *domain;
564 cookie_container_t *container;
565 unsigned len;
566 FILETIME tm;
568 GetSystemTimeAsFileTime(&tm);
570 len = strlenW(host);
571 p = host+len;
572 while(p>host && p[-1]!='.') p--;
573 while(p != host) {
574 p--;
575 while(p>host && p[-1]!='.') p--;
576 if(p == host) break;
578 load_persistent_cookie(p, empty_path);
581 len = strlenW(path);
582 assert(len+1 < INTERNET_MAX_PATH_LENGTH);
583 memcpy(subpath, path, (len+1)*sizeof(WCHAR));
584 ptr = subpath+len;
585 do {
586 *ptr = 0;
587 load_persistent_cookie(host, subpath);
589 ptr--;
590 while(ptr>subpath && ptr[-1]!='/') ptr--;
591 }while(ptr != subpath);
593 domain = get_cookie_domain(host, FALSE);
594 if(!domain) {
595 TRACE("Unknown host %s\n", debugstr_w(host));
596 return ERROR_NO_MORE_ITEMS;
599 for(domain = get_cookie_domain(host, FALSE); domain; domain = domain->parent) {
600 TRACE("Trying %s domain...\n", debugstr_w(domain->domain));
602 LIST_FOR_EACH_ENTRY(container, &domain->path_list, cookie_container_t, entry) {
603 struct list *cursor, *cursor2;
605 TRACE("path %s\n", debugstr_w(container->path));
607 if(!cookie_match_path(container, path))
608 continue;
610 TRACE("found domain %p\n", domain->domain);
612 LIST_FOR_EACH_SAFE(cursor, cursor2, &container->cookie_list) {
613 cookie_t *cookie_iter = LIST_ENTRY(cursor, cookie_t, entry);
615 /* check for expiry */
616 if((cookie_iter->expiry.dwLowDateTime != 0 || cookie_iter->expiry.dwHighDateTime != 0)
617 && CompareFileTime(&tm, &cookie_iter->expiry) > 0) {
618 TRACE("Found expired cookie. deleting\n");
619 delete_cookie(cookie_iter);
620 continue;
623 if((cookie_iter->flags & INTERNET_COOKIE_HTTPONLY) && !(flags & INTERNET_COOKIE_HTTPONLY))
624 continue;
627 if(!res->size) {
628 res->cookies = heap_alloc(4*sizeof(*res->cookies));
629 if(!res->cookies)
630 continue;
631 res->size = 4;
632 }else if(res->cnt == res->size) {
633 cookie_t **new_cookies = heap_realloc(res->cookies, res->size*2*sizeof(*res->cookies));
634 if(!new_cookies)
635 continue;
636 res->cookies = new_cookies;
637 res->size *= 2;
640 if(res->cnt)
641 res->string_len += 2; /* '; ' */
642 res->cookies[res->cnt++] = cookie_iter;
644 res->string_len += strlenW(cookie_iter->name);
645 if(*cookie_iter->data)
646 res->string_len += 1 /* = */ + strlenW(cookie_iter->data);
651 return ERROR_SUCCESS;
654 static void cookie_set_to_string(const cookie_set_t *cookie_set, WCHAR *str)
656 WCHAR *ptr = str;
657 unsigned i, len;
659 for(i=0; i<cookie_set->cnt; i++) {
660 if(i) {
661 *ptr++ = ';';
662 *ptr++ = ' ';
665 len = strlenW(cookie_set->cookies[i]->name);
666 memcpy(ptr, cookie_set->cookies[i]->name, len*sizeof(WCHAR));
667 ptr += len;
669 if(*cookie_set->cookies[i]->data) {
670 *ptr++ = '=';
671 len = strlenW(cookie_set->cookies[i]->data);
672 memcpy(ptr, cookie_set->cookies[i]->data, len*sizeof(WCHAR));
673 ptr += len;
677 assert(ptr-str == cookie_set->string_len);
678 TRACE("%s\n", debugstr_wn(str, ptr-str));
681 DWORD get_cookie_header(const WCHAR *host, const WCHAR *path, WCHAR **ret)
683 cookie_set_t cookie_set = {0};
684 DWORD res;
686 static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' '};
688 EnterCriticalSection(&cookie_cs);
690 res = get_cookie(host, path, INTERNET_COOKIE_HTTPONLY, &cookie_set);
691 if(res != ERROR_SUCCESS) {
692 LeaveCriticalSection(&cookie_cs);
693 return res;
696 if(cookie_set.cnt) {
697 WCHAR *header, *ptr;
699 ptr = header = heap_alloc(sizeof(cookieW) + (cookie_set.string_len + 3 /* crlf0 */) * sizeof(WCHAR));
700 if(header) {
701 memcpy(ptr, cookieW, sizeof(cookieW));
702 ptr += sizeof(cookieW)/sizeof(*cookieW);
704 cookie_set_to_string(&cookie_set, ptr);
705 heap_free(cookie_set.cookies);
706 ptr += cookie_set.string_len;
708 *ptr++ = '\r';
709 *ptr++ = '\n';
710 *ptr++ = 0;
712 *ret = header;
713 }else {
714 res = ERROR_NOT_ENOUGH_MEMORY;
716 }else {
717 *ret = NULL;
720 LeaveCriticalSection(&cookie_cs);
721 return ERROR_SUCCESS;
724 /***********************************************************************
725 * InternetGetCookieExW (WININET.@)
727 * Retrieve cookie from the specified url
729 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
730 * So it won't be implemented here.
732 * RETURNS
733 * TRUE on success
734 * FALSE on failure
737 BOOL WINAPI InternetGetCookieExW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
738 LPWSTR lpCookieData, LPDWORD lpdwSize, DWORD flags, void *reserved)
740 WCHAR host[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
741 cookie_set_t cookie_set = {0};
742 DWORD res;
743 BOOL ret;
745 TRACE("(%s, %s, %p, %p, %x, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), lpCookieData, lpdwSize, flags, reserved);
747 if (flags)
748 FIXME("flags 0x%08x not supported\n", flags);
750 if (!lpszUrl)
752 SetLastError(ERROR_INVALID_PARAMETER);
753 return FALSE;
756 host[0] = 0;
757 ret = COOKIE_crackUrlSimple(lpszUrl, host, sizeof(host)/sizeof(host[0]), path, sizeof(path)/sizeof(path[0]));
758 if (!ret || !host[0]) {
759 SetLastError(ERROR_INVALID_PARAMETER);
760 return FALSE;
763 EnterCriticalSection(&cookie_cs);
765 res = get_cookie(host, path, flags, &cookie_set);
766 if(res != ERROR_SUCCESS) {
767 LeaveCriticalSection(&cookie_cs);
768 SetLastError(res);
769 return FALSE;
772 if(cookie_set.cnt) {
773 if(!lpCookieData || cookie_set.string_len+1 > *lpdwSize) {
774 *lpdwSize = (cookie_set.string_len + 1) * sizeof(WCHAR);
775 TRACE("returning %u\n", *lpdwSize);
776 if(lpCookieData) {
777 SetLastError(ERROR_INSUFFICIENT_BUFFER);
778 ret = FALSE;
780 }else {
781 *lpdwSize = cookie_set.string_len + 1;
782 cookie_set_to_string(&cookie_set, lpCookieData);
783 lpCookieData[cookie_set.string_len] = 0;
785 }else {
786 TRACE("no cookies found for %s\n", debugstr_w(host));
787 SetLastError(ERROR_NO_MORE_ITEMS);
788 ret = FALSE;
791 heap_free(cookie_set.cookies);
792 LeaveCriticalSection(&cookie_cs);
793 return ret;
796 /***********************************************************************
797 * InternetGetCookieW (WININET.@)
799 * Retrieve cookie for the specified URL.
801 BOOL WINAPI InternetGetCookieW(const WCHAR *url, const WCHAR *name, WCHAR *data, DWORD *size)
803 TRACE("(%s, %s, %s, %p)\n", debugstr_w(url), debugstr_w(name), debugstr_w(data), size);
805 return InternetGetCookieExW(url, name, data, size, 0, NULL);
808 /***********************************************************************
809 * InternetGetCookieExA (WININET.@)
811 * Retrieve cookie from the specified url
813 * RETURNS
814 * TRUE on success
815 * FALSE on failure
818 BOOL WINAPI InternetGetCookieExA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
819 LPSTR lpCookieData, LPDWORD lpdwSize, DWORD flags, void *reserved)
821 WCHAR *url, *name;
822 DWORD len, size;
823 BOOL r;
825 TRACE("(%s %s %p %p(%u) %x %p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
826 lpCookieData, lpdwSize, lpdwSize ? *lpdwSize : 0, flags, reserved);
828 url = heap_strdupAtoW(lpszUrl);
829 name = heap_strdupAtoW(lpszCookieName);
831 r = InternetGetCookieExW( url, name, NULL, &len, flags, reserved );
832 if( r )
834 WCHAR *szCookieData;
836 szCookieData = heap_alloc(len * sizeof(WCHAR));
837 if( !szCookieData )
839 r = FALSE;
841 else
843 r = InternetGetCookieExW( url, name, szCookieData, &len, flags, reserved );
845 if(r) {
846 size = WideCharToMultiByte( CP_ACP, 0, szCookieData, len, NULL, 0, NULL, NULL);
847 if(lpCookieData) {
848 if(*lpdwSize >= size) {
849 WideCharToMultiByte( CP_ACP, 0, szCookieData, len, lpCookieData, *lpdwSize, NULL, NULL);
850 }else {
851 SetLastError(ERROR_INSUFFICIENT_BUFFER);
852 r = FALSE;
855 *lpdwSize = size;
858 heap_free( szCookieData );
861 heap_free( name );
862 heap_free( url );
863 return r;
866 /***********************************************************************
867 * InternetGetCookieA (WININET.@)
869 * See InternetGetCookieW.
871 BOOL WINAPI InternetGetCookieA(const char *url, const char *name, char *data, DWORD *size)
873 TRACE("(%s, %s, %s, %p)\n", debugstr_a(url), debugstr_a(name), debugstr_a(data), size);
875 return InternetGetCookieExA(url, name, data, size, 0, NULL);
878 /***********************************************************************
879 * IsDomainLegalCookieDomainW (WININET.@)
881 BOOL WINAPI IsDomainLegalCookieDomainW( LPCWSTR s1, LPCWSTR s2 )
883 DWORD s1_len, s2_len;
885 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1), debugstr_w(s2));
887 if (!s1 || !s2)
889 SetLastError(ERROR_INVALID_PARAMETER);
890 return FALSE;
892 if (s1[0] == '.' || !s1[0] || s2[0] == '.' || !s2[0])
894 SetLastError(ERROR_INVALID_NAME);
895 return FALSE;
897 if(!strchrW(s1, '.') || !strchrW(s2, '.'))
898 return FALSE;
900 s1_len = strlenW(s1);
901 s2_len = strlenW(s2);
902 if (s1_len > s2_len)
903 return FALSE;
905 if (strncmpiW(s1, s2+s2_len-s1_len, s1_len) || (s2_len>s1_len && s2[s2_len-s1_len-1]!='.'))
907 SetLastError(ERROR_INVALID_PARAMETER);
908 return FALSE;
911 return TRUE;
914 DWORD set_cookie(const WCHAR *domain, const WCHAR *path, const WCHAR *cookie_name, const WCHAR *cookie_data, DWORD flags)
916 cookie_container_t *container;
917 cookie_t *thisCookie;
918 LPWSTR data, value;
919 WCHAR *ptr;
920 FILETIME expiry, create;
921 BOOL expired = FALSE, update_persistent = FALSE;
922 DWORD cookie_flags = 0;
924 TRACE("%s %s %s=%s %x\n", debugstr_w(domain), debugstr_w(path), debugstr_w(cookie_name), debugstr_w(cookie_data), flags);
926 value = data = heap_strdupW(cookie_data);
927 if (!data)
929 ERR("could not allocate the cookie data buffer\n");
930 return COOKIE_STATE_UNKNOWN;
933 memset(&expiry,0,sizeof(expiry));
934 GetSystemTimeAsFileTime(&create);
936 /* lots of information can be parsed out of the cookie value */
938 ptr = data;
939 for (;;)
941 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
942 static const WCHAR szPath[] = {'p','a','t','h','=',0};
943 static const WCHAR szExpires[] = {'e','x','p','i','r','e','s','=',0};
944 static const WCHAR szSecure[] = {'s','e','c','u','r','e',0};
945 static const WCHAR szHttpOnly[] = {'h','t','t','p','o','n','l','y',0};
946 static const WCHAR szVersion[] = {'v','e','r','s','i','o','n','=',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 (strncmpiW(ptr, szVersion, 8) == 0)
1029 FIXME("version not handled (%s)\n",debugstr_w(ptr));
1030 ptr += strlenW(szVersion);
1032 else if (*ptr)
1034 FIXME("Unknown additional option %s\n",debugstr_w(ptr));
1035 break;
1039 EnterCriticalSection(&cookie_cs);
1041 load_persistent_cookie(domain, path);
1043 container = get_cookie_container(domain, path, !expired);
1044 if(!container) {
1045 heap_free(data);
1046 if (value != data) heap_free(value);
1047 LeaveCriticalSection(&cookie_cs);
1048 return COOKIE_STATE_ACCEPT;
1051 if(!expiry.dwLowDateTime && !expiry.dwHighDateTime)
1052 cookie_flags |= INTERNET_COOKIE_IS_SESSION;
1053 else
1054 update_persistent = TRUE;
1056 if ((thisCookie = find_cookie(container, cookie_name)))
1058 if ((thisCookie->flags & INTERNET_COOKIE_HTTPONLY) && !(flags & INTERNET_COOKIE_HTTPONLY)) {
1059 WARN("An attempt to override httponly cookie\n");
1060 SetLastError(ERROR_INVALID_OPERATION);
1061 heap_free(data);
1062 if (value != data) heap_free(value);
1063 return COOKIE_STATE_REJECT;
1066 if (!(thisCookie->flags & INTERNET_COOKIE_IS_SESSION))
1067 update_persistent = TRUE;
1068 delete_cookie(thisCookie);
1071 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name),
1072 debugstr_w(value), debugstr_w(container->domain->domain),debugstr_w(container->path));
1074 if (!expired) {
1075 cookie_t *new_cookie;
1077 new_cookie = alloc_cookie(cookie_name, value, expiry, create, cookie_flags);
1078 if(!new_cookie) {
1079 heap_free(data);
1080 if (value != data) heap_free(value);
1081 LeaveCriticalSection(&cookie_cs);
1082 return COOKIE_STATE_UNKNOWN;
1085 add_cookie(container, new_cookie);
1087 heap_free(data);
1088 if (value != data) heap_free(value);
1090 if (!update_persistent || save_persistent_cookie(container))
1092 LeaveCriticalSection(&cookie_cs);
1093 return COOKIE_STATE_ACCEPT;
1095 LeaveCriticalSection(&cookie_cs);
1096 return COOKIE_STATE_UNKNOWN;
1099 /***********************************************************************
1100 * InternetSetCookieExW (WININET.@)
1102 * Sets cookie for the specified url
1104 DWORD WINAPI InternetSetCookieExW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
1105 LPCWSTR lpCookieData, DWORD flags, DWORD_PTR reserved)
1107 BOOL ret;
1108 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
1110 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_w(lpszUrl), debugstr_w(lpszCookieName),
1111 debugstr_w(lpCookieData), flags, reserved);
1113 if (flags & ~INTERNET_COOKIE_HTTPONLY)
1114 FIXME("flags %x not supported\n", flags);
1116 if (!lpszUrl || !lpCookieData)
1118 SetLastError(ERROR_INVALID_PARAMETER);
1119 return COOKIE_STATE_UNKNOWN;
1122 hostName[0] = 0;
1123 ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
1124 if (!ret || !hostName[0]) return COOKIE_STATE_UNKNOWN;
1126 if (!lpszCookieName)
1128 WCHAR *cookie, *data;
1129 DWORD res;
1131 cookie = heap_strdupW(lpCookieData);
1132 if (!cookie)
1134 SetLastError(ERROR_OUTOFMEMORY);
1135 return COOKIE_STATE_UNKNOWN;
1138 /* some apps (or is it us??) try to add a cookie with no cookie name, but
1139 * the cookie data in the form of name[=data].
1141 if (!(data = strchrW(cookie, '='))) data = cookie + strlenW(cookie);
1142 else *data++ = 0;
1144 res = set_cookie(hostName, path, cookie, data, flags);
1146 heap_free(cookie);
1147 return res;
1149 return set_cookie(hostName, path, lpszCookieName, lpCookieData, flags);
1152 /***********************************************************************
1153 * InternetSetCookieW (WININET.@)
1155 * Sets a cookie for the specified URL.
1157 BOOL WINAPI InternetSetCookieW(const WCHAR *url, const WCHAR *name, const WCHAR *data)
1159 TRACE("(%s, %s, %s)\n", debugstr_w(url), debugstr_w(name), debugstr_w(data));
1161 return InternetSetCookieExW(url, name, data, 0, 0) == COOKIE_STATE_ACCEPT;
1164 /***********************************************************************
1165 * InternetSetCookieA (WININET.@)
1167 * Sets cookie for the specified url
1169 * RETURNS
1170 * TRUE on success
1171 * FALSE on failure
1174 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
1175 LPCSTR lpCookieData)
1177 LPWSTR data, url, name;
1178 BOOL r;
1180 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
1181 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
1183 url = heap_strdupAtoW(lpszUrl);
1184 name = heap_strdupAtoW(lpszCookieName);
1185 data = heap_strdupAtoW(lpCookieData);
1187 r = InternetSetCookieW( url, name, data );
1189 heap_free( data );
1190 heap_free( name );
1191 heap_free( url );
1192 return r;
1195 /***********************************************************************
1196 * InternetSetCookieExA (WININET.@)
1198 * See InternetSetCookieExW.
1200 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
1201 DWORD dwFlags, DWORD_PTR dwReserved)
1203 WCHAR *data, *url, *name;
1204 DWORD r;
1206 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_a(lpszURL), debugstr_a(lpszCookieName),
1207 debugstr_a(lpszCookieData), dwFlags, dwReserved);
1209 url = heap_strdupAtoW(lpszURL);
1210 name = heap_strdupAtoW(lpszCookieName);
1211 data = heap_strdupAtoW(lpszCookieData);
1213 r = InternetSetCookieExW(url, name, data, dwFlags, dwReserved);
1215 heap_free( data );
1216 heap_free( name );
1217 heap_free( url );
1218 return r;
1221 /***********************************************************************
1222 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1224 * Clears all per-site decisions about cookies.
1226 * RETURNS
1227 * TRUE on success
1228 * FALSE on failure
1231 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
1233 FIXME("stub\n");
1234 return TRUE;
1237 /***********************************************************************
1238 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1240 * See InternetEnumPerSiteCookieDecisionW.
1242 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, ULONG *pcSiteNameSize,
1243 ULONG *pdwDecision, ULONG dwIndex )
1245 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1246 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1247 return FALSE;
1250 /***********************************************************************
1251 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1253 * Enumerates all per-site decisions about cookies.
1255 * RETURNS
1256 * TRUE on success
1257 * FALSE on failure
1260 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, ULONG *pcSiteNameSize,
1261 ULONG *pdwDecision, ULONG dwIndex )
1263 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1264 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1265 return FALSE;
1268 /***********************************************************************
1269 * InternetGetPerSiteCookieDecisionA (WININET.@)
1271 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, ULONG *pResult )
1273 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
1274 return FALSE;
1277 /***********************************************************************
1278 * InternetGetPerSiteCookieDecisionW (WININET.@)
1280 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, ULONG *pResult )
1282 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
1283 return FALSE;
1286 /***********************************************************************
1287 * InternetSetPerSiteCookieDecisionA (WININET.@)
1289 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
1291 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
1292 return FALSE;
1295 /***********************************************************************
1296 * InternetSetPerSiteCookieDecisionW (WININET.@)
1298 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
1300 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
1301 return FALSE;
1304 void free_cookie(void)
1306 DeleteCriticalSection(&cookie_cs);