2 * Wininet - cookie handling stuff
4 * Copyright 2002 TransGaming Technologies Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #if defined(__MINGW32__) || defined (_MSC_VER)
44 #include "wine/debug.h"
47 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
50 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
53 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
56 typedef struct _cookie_domain cookie_domain
;
57 typedef struct _cookie cookie
;
63 struct _cookie_domain
*parent
;
76 LPWSTR lpCookieDomain
;
78 struct list cookie_list
;
81 static CRITICAL_SECTION cookie_cs
;
82 static CRITICAL_SECTION_DEBUG cookie_cs_debug
=
85 { &cookie_cs_debug
.ProcessLocksList
, &cookie_cs_debug
.ProcessLocksList
},
86 0, 0, { (DWORD_PTR
)(__FILE__
": cookie_cs") }
88 static CRITICAL_SECTION cookie_cs
= { &cookie_cs_debug
, -1, 0, 0, 0, 0 };
89 static struct list domain_list
= LIST_INIT(domain_list
);
91 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
92 FILETIME expiry
, FILETIME create
, DWORD flags
);
93 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
);
94 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
);
95 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
);
96 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
);
97 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
98 cookie_domain
*searchDomain
, BOOL allow_partial
);
100 static BOOL
create_cookie_url(LPCWSTR domain
, LPCWSTR path
, WCHAR
*buf
, DWORD buf_len
)
102 static const WCHAR cookie_prefix
[] = {'C','o','o','k','i','e',':'};
107 if(buf_len
< sizeof(cookie_prefix
)/sizeof(WCHAR
))
109 memcpy(buf
, cookie_prefix
, sizeof(cookie_prefix
));
110 buf
+= sizeof(cookie_prefix
)/sizeof(WCHAR
);
111 buf_len
-= sizeof(cookie_prefix
)/sizeof(WCHAR
);
115 if(!GetUserNameW(buf
, &len
))
125 len
= strlenW(domain
);
128 memcpy(buf
, domain
, len
*sizeof(WCHAR
));
135 memcpy(buf
, path
, len
*sizeof(WCHAR
));
145 static BOOL
load_persistent_cookie(LPCWSTR domain
, LPCWSTR path
)
147 INTERNET_CACHE_ENTRY_INFOW
*info
;
148 cookie_domain
*domain_container
= NULL
;
151 WCHAR cookie_url
[MAX_PATH
];
153 char *str
= NULL
, *pbeg
, *pend
;
156 FILETIME expiry
, create
, time
;
158 if (!create_cookie_url(domain
, path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
162 RetrieveUrlCacheEntryStreamW(cookie_url
, NULL
, &size
, FALSE
, 0);
163 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
165 info
= heap_alloc(size
);
168 cookie
= RetrieveUrlCacheEntryStreamW(cookie_url
, info
, &size
, FALSE
, 0);
169 size
= info
->dwSizeLow
;
174 if(!(str
= heap_alloc(size
+1)) || !ReadUrlCacheEntryStream(cookie
, 0, str
, &size
, 0)) {
175 UnlockUrlCacheEntryStream(cookie
, 0);
180 UnlockUrlCacheEntryStream(cookie
, 0);
182 LIST_FOR_EACH(iter
, &domain_list
)
184 domain_container
= LIST_ENTRY(iter
, cookie_domain
, entry
);
185 if(COOKIE_matchDomain(domain
, path
, domain_container
, FALSE
))
187 domain_container
= NULL
;
189 if(!domain_container
)
190 domain_container
= COOKIE_addDomain(domain
, path
);
191 if(!domain_container
) {
196 GetSystemTimeAsFileTime(&time
);
197 for(pbeg
=str
; pbeg
&& *pbeg
; name
=data
=NULL
) {
198 pend
= strchr(pbeg
, '\n');
202 name
= heap_strdupAtoW(pbeg
);
205 pend
= strchr(pbeg
, '\n');
209 data
= heap_strdupAtoW(pbeg
);
212 pbeg
= strchr(pend
+1, '\n');
215 sscanf(pbeg
, "%u %u %u %u %u", &flags
, &expiry
.dwLowDateTime
, &expiry
.dwHighDateTime
,
216 &create
.dwLowDateTime
, &create
.dwHighDateTime
);
219 pbeg
= strchr(pbeg
, '*');
229 if(CompareFileTime(&time
, &expiry
) <= 0) {
230 if((old_cookie
= COOKIE_findCookie(domain_container
, name
)))
231 COOKIE_deleteCookie(old_cookie
, FALSE
);
232 COOKIE_addCookie(domain_container
, name
, data
, expiry
, create
, flags
);
244 static BOOL
save_persistent_cookie(cookie_domain
*domain
)
246 static const WCHAR txtW
[] = {'t','x','t',0};
248 WCHAR cookie_url
[MAX_PATH
], cookie_file
[MAX_PATH
];
249 HANDLE cookie_handle
;
250 cookie
*cookie_container
= NULL
, *cookie_iter
;
251 BOOL do_save
= FALSE
;
252 char buf
[64], *dyn_buf
;
255 if (!create_cookie_url(domain
->lpCookieDomain
, domain
->lpCookiePath
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
258 /* check if there's anything to save */
259 GetSystemTimeAsFileTime(&time
);
260 LIST_FOR_EACH_ENTRY_SAFE(cookie_container
, cookie_iter
, &domain
->cookie_list
, cookie
, entry
)
262 if((cookie_container
->expiry
.dwLowDateTime
|| cookie_container
->expiry
.dwHighDateTime
)
263 && CompareFileTime(&time
, &cookie_container
->expiry
) > 0) {
264 COOKIE_deleteCookie(cookie_container
, FALSE
);
268 if(!(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)) {
274 DeleteUrlCacheEntryW(cookie_url
);
278 if(!CreateUrlCacheEntryW(cookie_url
, 0, txtW
, cookie_file
, 0))
280 cookie_handle
= CreateFileW(cookie_file
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
281 if(cookie_handle
== INVALID_HANDLE_VALUE
) {
282 DeleteFileW(cookie_file
);
286 LIST_FOR_EACH_ENTRY(cookie_container
, &domain
->cookie_list
, cookie
, entry
)
288 if(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)
291 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieName
);
292 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
298 if(!WriteFile(cookie_handle
, "\n", 1, NULL
, NULL
)) {
303 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieData
);
304 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
310 if(!WriteFile(cookie_handle
, "\n", 1, NULL
, NULL
)) {
315 dyn_buf
= heap_strdupWtoA(domain
->lpCookieDomain
);
316 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
323 dyn_buf
= heap_strdupWtoA(domain
->lpCookiePath
);
324 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
331 sprintf(buf
, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container
->flags
,
332 cookie_container
->expiry
.dwLowDateTime
, cookie_container
->expiry
.dwHighDateTime
,
333 cookie_container
->create
.dwLowDateTime
, cookie_container
->create
.dwHighDateTime
);
334 if(!WriteFile(cookie_handle
, buf
, strlen(buf
), NULL
, NULL
)) {
340 CloseHandle(cookie_handle
);
342 ERR("error saving cookie file\n");
343 DeleteFileW(cookie_file
);
347 memset(&time
, 0, sizeof(time
));
348 return CommitUrlCacheEntryW(cookie_url
, cookie_file
, time
, time
, 0, NULL
, 0, txtW
, 0);
351 /* adds a cookie to the domain */
352 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
353 FILETIME expiry
, FILETIME create
, DWORD flags
)
355 cookie
*newCookie
= heap_alloc(sizeof(cookie
));
359 newCookie
->lpCookieName
= heap_strdupW(name
);
360 newCookie
->lpCookieData
= heap_strdupW(data
);
362 if (!newCookie
->lpCookieName
|| !newCookie
->lpCookieData
)
364 heap_free(newCookie
->lpCookieName
);
365 heap_free(newCookie
->lpCookieData
);
366 heap_free(newCookie
);
371 newCookie
->flags
= flags
;
372 newCookie
->expiry
= expiry
;
373 newCookie
->create
= create
;
375 TRACE("added cookie %p (data is %s)\n", newCookie
, debugstr_w(data
) );
377 list_add_tail(&domain
->cookie_list
, &newCookie
->entry
);
378 newCookie
->parent
= domain
;
383 /* finds a cookie in the domain matching the cookie name */
384 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
)
386 struct list
* cursor
;
387 TRACE("(%p, %s)\n", domain
, debugstr_w(lpszCookieName
));
389 LIST_FOR_EACH(cursor
, &domain
->cookie_list
)
391 cookie
*searchCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
392 BOOL candidate
= TRUE
;
393 if (candidate
&& lpszCookieName
)
395 if (candidate
&& !searchCookie
->lpCookieName
)
397 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
406 /* removes a cookie from the list, if its the last cookie we also remove the domain */
407 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
)
409 heap_free(deadCookie
->lpCookieName
);
410 heap_free(deadCookie
->lpCookieData
);
411 list_remove(&deadCookie
->entry
);
413 /* special case: last cookie, lets remove the domain to save memory */
414 if (list_empty(&deadCookie
->parent
->cookie_list
) && deleteDomain
)
415 COOKIE_deleteDomain(deadCookie
->parent
);
416 heap_free(deadCookie
);
419 /* allocates a domain and adds it to the end */
420 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
)
422 cookie_domain
*newDomain
= heap_alloc(sizeof(cookie_domain
));
424 list_init(&newDomain
->entry
);
425 list_init(&newDomain
->cookie_list
);
426 newDomain
->lpCookieDomain
= heap_strdupW(domain
);
427 newDomain
->lpCookiePath
= heap_strdupW(path
);
429 list_add_tail(&domain_list
, &newDomain
->entry
);
431 TRACE("Adding domain: %p\n", newDomain
);
435 static BOOL
COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
437 URL_COMPONENTSW UrlComponents
;
439 UrlComponents
.lpszExtraInfo
= NULL
;
440 UrlComponents
.lpszPassword
= NULL
;
441 UrlComponents
.lpszScheme
= NULL
;
442 UrlComponents
.lpszUrlPath
= path
;
443 UrlComponents
.lpszUserName
= NULL
;
444 UrlComponents
.lpszHostName
= hostName
;
445 UrlComponents
.dwExtraInfoLength
= 0;
446 UrlComponents
.dwPasswordLength
= 0;
447 UrlComponents
.dwSchemeLength
= 0;
448 UrlComponents
.dwUserNameLength
= 0;
449 UrlComponents
.dwHostNameLength
= hostNameLen
;
450 UrlComponents
.dwUrlPathLength
= pathLen
;
452 if (!InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
)) return FALSE
;
454 /* discard the webpage off the end of the path */
455 if (UrlComponents
.dwUrlPathLength
)
457 if (path
[UrlComponents
.dwUrlPathLength
- 1] != '/')
460 if ((ptr
= strrchrW(path
, '/'))) *(++ptr
) = 0;
468 else if (pathLen
>= 2)
476 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
477 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
478 cookie_domain
*searchDomain
, BOOL allow_partial
)
480 TRACE("searching on domain %p\n", searchDomain
);
481 if (lpszCookieDomain
)
483 if (!searchDomain
->lpCookieDomain
)
486 TRACE("comparing domain %s with %s\n",
487 debugstr_w(lpszCookieDomain
),
488 debugstr_w(searchDomain
->lpCookieDomain
));
490 if (allow_partial
&& !strstrW(lpszCookieDomain
, searchDomain
->lpCookieDomain
))
492 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
498 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
499 /* paths match at the beginning. so a path of /foo would match
500 * /foobar and /foo/bar
502 if (!searchDomain
->lpCookiePath
)
506 len
= lstrlenW(searchDomain
->lpCookiePath
);
507 if (strncmpiW(searchDomain
->lpCookiePath
, lpszCookiePath
, len
)!=0)
510 else if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
517 /* remove a domain from the list and delete it */
518 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
)
520 struct list
* cursor
;
521 while ((cursor
= list_tail(&deadDomain
->cookie_list
)))
523 COOKIE_deleteCookie(LIST_ENTRY(cursor
, cookie
, entry
), FALSE
);
526 heap_free(deadDomain
->lpCookieDomain
);
527 heap_free(deadDomain
->lpCookiePath
);
529 list_remove(&deadDomain
->entry
);
531 heap_free(deadDomain
);
534 DWORD
get_cookie(const WCHAR
*host
, const WCHAR
*path
, WCHAR
*cookie_data
, DWORD
*size
)
536 static const WCHAR empty_path
[] = { '/',0 };
538 unsigned cnt
= 0, len
, name_len
, domain_count
= 0, cookie_count
= 0;
539 WCHAR
*ptr
, subpath
[INTERNET_MAX_PATH_LENGTH
];
541 cookie_domain
*domain
;
544 GetSystemTimeAsFileTime(&tm
);
546 EnterCriticalSection(&cookie_cs
);
550 while(p
>host
&& p
[-1]!='.') p
--;
553 while(p
>host
&& p
[-1]!='.') p
--;
556 load_persistent_cookie(p
, empty_path
);
560 assert(len
+1 < INTERNET_MAX_PATH_LENGTH
);
561 memcpy(subpath
, path
, (len
+1)*sizeof(WCHAR
));
565 load_persistent_cookie(host
, subpath
);
568 while(ptr
>subpath
&& ptr
[-1]!='/') ptr
--;
569 }while(ptr
!= subpath
);
572 LIST_FOR_EACH_ENTRY(domain
, &domain_list
, cookie_domain
, entry
) {
573 struct list
*cursor
, *cursor2
;
575 if(!COOKIE_matchDomain(host
, path
, domain
, TRUE
))
579 TRACE("found domain %p\n", domain
);
581 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &domain
->cookie_list
) {
582 cookie
*cookie_iter
= LIST_ENTRY(cursor
, cookie
, entry
);
584 /* check for expiry */
585 if((cookie_iter
->expiry
.dwLowDateTime
!= 0 || cookie_iter
->expiry
.dwHighDateTime
!= 0)
586 && CompareFileTime(&tm
, &cookie_iter
->expiry
) > 0)
588 TRACE("Found expired cookie. deleting\n");
589 COOKIE_deleteCookie(cookie_iter
, FALSE
);
595 cnt
+= name_len
= strlenW(cookie_iter
->lpCookieName
);
596 if ((len
= strlenW(cookie_iter
->lpCookieData
))) {
608 memcpy(ptr
, cookie_iter
->lpCookieName
, name_len
*sizeof(WCHAR
));
613 memcpy(ptr
, cookie_iter
->lpCookieData
, len
*sizeof(WCHAR
));
617 assert(cookie_data
+cnt
== ptr
);
618 TRACE("Cookie: %s\n", debugstr_wn(cookie_data
, cnt
));
620 /* Stop writing data, just compute the size */
629 LeaveCriticalSection(&cookie_cs
);
635 TRACE("no cookies found for %s\n", debugstr_w(host
));
636 return ERROR_NO_MORE_ITEMS
;
639 if(!cookie_data
|| !ptr
) {
640 *size
= (cnt
+ 1) * sizeof(WCHAR
);
641 TRACE("returning %u\n", *size
);
642 return cookie_data
? ERROR_INSUFFICIENT_BUFFER
: ERROR_SUCCESS
;
647 TRACE("Returning %u (from %u domains): %s\n", cnt
, domain_count
, debugstr_w(cookie_data
));
648 return ERROR_SUCCESS
;
651 /***********************************************************************
652 * InternetGetCookieExW (WININET.@)
654 * Retrieve cookie from the specified url
656 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
657 * So it won't be implemented here.
664 BOOL WINAPI
InternetGetCookieExW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
665 LPWSTR lpCookieData
, LPDWORD lpdwSize
, DWORD flags
, void *reserved
)
667 WCHAR host
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
671 TRACE("(%s, %s, %p, %p, %x, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
), lpCookieData
, lpdwSize
, flags
, reserved
);
674 FIXME("flags 0x%08x not supported\n", flags
);
678 SetLastError(ERROR_INVALID_PARAMETER
);
683 ret
= COOKIE_crackUrlSimple(lpszUrl
, host
, sizeof(host
)/sizeof(host
[0]), path
, sizeof(path
)/sizeof(path
[0]));
684 if (!ret
|| !host
[0]) {
685 SetLastError(ERROR_INVALID_PARAMETER
);
689 res
= get_cookie(host
, path
, lpCookieData
, lpdwSize
);
690 if(res
!= ERROR_SUCCESS
)
692 return res
== ERROR_SUCCESS
;
695 /***********************************************************************
696 * InternetGetCookieW (WININET.@)
698 * Retrieve cookie for the specified URL.
700 BOOL WINAPI
InternetGetCookieW(const WCHAR
*url
, const WCHAR
*name
, WCHAR
*data
, DWORD
*size
)
702 TRACE("(%s, %s, %s, %p)\n", debugstr_w(url
), debugstr_w(name
), debugstr_w(data
), size
);
704 return InternetGetCookieExW(url
, name
, data
, size
, 0, NULL
);
707 /***********************************************************************
708 * InternetGetCookieExA (WININET.@)
710 * Retrieve cookie from the specified url
717 BOOL WINAPI
InternetGetCookieExA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
718 LPSTR lpCookieData
, LPDWORD lpdwSize
, DWORD flags
, void *reserved
)
724 TRACE("(%s %s %p %p(%u) %x %p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
725 lpCookieData
, lpdwSize
, lpdwSize
? *lpdwSize
: 0, flags
, reserved
);
727 url
= heap_strdupAtoW(lpszUrl
);
728 name
= heap_strdupAtoW(lpszCookieName
);
730 r
= InternetGetCookieExW( url
, name
, NULL
, &len
, flags
, reserved
);
735 szCookieData
= heap_alloc(len
* sizeof(WCHAR
));
742 r
= InternetGetCookieExW( url
, name
, szCookieData
, &len
, flags
, reserved
);
745 size
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, NULL
, 0, NULL
, NULL
);
747 if(*lpdwSize
>= size
) {
748 WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, lpCookieData
, *lpdwSize
, NULL
, NULL
);
750 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
757 heap_free( szCookieData
);
765 /***********************************************************************
766 * InternetGetCookieA (WININET.@)
768 * See InternetGetCookieW.
770 BOOL WINAPI
InternetGetCookieA(const char *url
, const char *name
, char *data
, DWORD
*size
)
772 TRACE("(%s, %s, %s, %p)\n", debugstr_a(url
), debugstr_a(name
), debugstr_a(data
), size
);
774 return InternetGetCookieExA(url
, name
, data
, size
, 0, NULL
);
777 /***********************************************************************
778 * IsDomainLegalCookieDomainW (WININET.@)
780 BOOL WINAPI
IsDomainLegalCookieDomainW( LPCWSTR s1
, LPCWSTR s2
)
782 DWORD s1_len
, s2_len
;
784 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1
), debugstr_w(s2
));
788 SetLastError(ERROR_INVALID_PARAMETER
);
791 if (s1
[0] == '.' || !s1
[0] || s2
[0] == '.' || !s2
[0])
793 SetLastError(ERROR_INVALID_NAME
);
796 if(!strchrW(s1
, '.') || !strchrW(s2
, '.'))
799 s1_len
= strlenW(s1
);
800 s2_len
= strlenW(s2
);
804 if (strncmpiW(s1
, s2
+s2_len
-s1_len
, s1_len
) || (s2_len
>s1_len
&& s2
[s2_len
-s1_len
-1]!='.'))
806 SetLastError(ERROR_INVALID_PARAMETER
);
813 DWORD
set_cookie(LPCWSTR domain
, LPCWSTR path
, LPCWSTR cookie_name
, LPCWSTR cookie_data
)
815 cookie_domain
*thisCookieDomain
= NULL
;
820 FILETIME expiry
, create
;
821 BOOL expired
= FALSE
, update_persistent
= FALSE
;
824 value
= data
= heap_strdupW(cookie_data
);
827 ERR("could not allocate the cookie data buffer\n");
828 return COOKIE_STATE_UNKNOWN
;
831 memset(&expiry
,0,sizeof(expiry
));
832 GetSystemTimeAsFileTime(&create
);
834 /* lots of information can be parsed out of the cookie value */
839 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
840 static const WCHAR szPath
[] = {'p','a','t','h','=',0};
841 static const WCHAR szExpires
[] = {'e','x','p','i','r','e','s','=',0};
842 static const WCHAR szSecure
[] = {'s','e','c','u','r','e',0};
843 static const WCHAR szHttpOnly
[] = {'h','t','t','p','o','n','l','y',0};
845 if (!(ptr
= strchrW(ptr
,';'))) break;
848 if (value
!= data
) heap_free(value
);
849 value
= heap_alloc((ptr
- data
) * sizeof(WCHAR
));
853 ERR("could not allocate the cookie value buffer\n");
854 return COOKIE_STATE_UNKNOWN
;
856 strcpyW(value
, data
);
858 while (*ptr
== ' ') ptr
++; /* whitespace */
860 if (strncmpiW(ptr
, szDomain
, 7) == 0)
864 ptr
+= sizeof(szDomain
)/sizeof(szDomain
[0])-1;
867 end_ptr
= strchrW(ptr
, ';');
871 if(!IsDomainLegalCookieDomainW(ptr
, domain
))
876 return COOKIE_STATE_UNKNOWN
;
883 TRACE("Parsing new domain %s\n",debugstr_w(domain
));
885 else if (strncmpiW(ptr
, szPath
, 5) == 0)
887 ptr
+=strlenW(szPath
);
889 TRACE("Parsing new path %s\n",debugstr_w(path
));
891 else if (strncmpiW(ptr
, szExpires
, 8) == 0)
894 ptr
+=strlenW(szExpires
);
895 if (InternetTimeToSystemTimeW(ptr
, &st
, 0))
897 SystemTimeToFileTime(&st
, &expiry
);
899 if (CompareFileTime(&create
,&expiry
) > 0)
901 TRACE("Cookie already expired.\n");
906 else if (strncmpiW(ptr
, szSecure
, 6) == 0)
908 FIXME("secure not handled (%s)\n",debugstr_w(ptr
));
909 ptr
+= strlenW(szSecure
);
911 else if (strncmpiW(ptr
, szHttpOnly
, 8) == 0)
913 FIXME("httponly not handled (%s)\n",debugstr_w(ptr
));
914 ptr
+= strlenW(szHttpOnly
);
918 FIXME("Unknown additional option %s\n",debugstr_w(ptr
));
923 EnterCriticalSection(&cookie_cs
);
925 load_persistent_cookie(domain
, path
);
927 LIST_FOR_EACH(cursor
, &domain_list
)
929 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
930 if (COOKIE_matchDomain(domain
, path
, thisCookieDomain
, FALSE
))
932 thisCookieDomain
= NULL
;
935 if (!thisCookieDomain
)
938 thisCookieDomain
= COOKIE_addDomain(domain
, path
);
942 if (value
!= data
) heap_free(value
);
943 LeaveCriticalSection(&cookie_cs
);
944 return COOKIE_STATE_ACCEPT
;
948 if(!expiry
.dwLowDateTime
&& !expiry
.dwHighDateTime
)
949 flags
|= INTERNET_COOKIE_IS_SESSION
;
951 update_persistent
= TRUE
;
953 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, cookie_name
)))
955 if (!(thisCookie
->flags
& INTERNET_COOKIE_IS_SESSION
))
956 update_persistent
= TRUE
;
957 COOKIE_deleteCookie(thisCookie
, FALSE
);
960 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name
),
961 debugstr_w(value
), debugstr_w(thisCookieDomain
->lpCookieDomain
),debugstr_w(thisCookieDomain
->lpCookiePath
));
963 if (!expired
&& !COOKIE_addCookie(thisCookieDomain
, cookie_name
, value
, expiry
, create
, flags
))
966 if (value
!= data
) heap_free(value
);
967 LeaveCriticalSection(&cookie_cs
);
968 return COOKIE_STATE_UNKNOWN
;
971 if (value
!= data
) heap_free(value
);
973 if (!update_persistent
|| save_persistent_cookie(thisCookieDomain
))
975 LeaveCriticalSection(&cookie_cs
);
976 return COOKIE_STATE_ACCEPT
;
978 LeaveCriticalSection(&cookie_cs
);
979 return COOKIE_STATE_UNKNOWN
;
982 /***********************************************************************
983 * InternetSetCookieExW (WININET.@)
985 * Sets cookie for the specified url
987 DWORD WINAPI
InternetSetCookieExW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
988 LPCWSTR lpCookieData
, DWORD flags
, DWORD_PTR reserved
)
991 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
993 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_w(lpszUrl
), debugstr_w(lpszCookieName
),
994 debugstr_w(lpCookieData
), flags
, reserved
);
997 FIXME("flags %x not supported\n", flags
);
999 if (!lpszUrl
|| !lpCookieData
)
1001 SetLastError(ERROR_INVALID_PARAMETER
);
1002 return COOKIE_STATE_UNKNOWN
;
1006 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
1007 if (!ret
|| !hostName
[0]) return COOKIE_STATE_UNKNOWN
;
1009 if (!lpszCookieName
)
1011 WCHAR
*cookie
, *data
;
1014 cookie
= heap_strdupW(lpCookieData
);
1017 SetLastError(ERROR_OUTOFMEMORY
);
1018 return COOKIE_STATE_UNKNOWN
;
1021 /* some apps (or is it us??) try to add a cookie with no cookie name, but
1022 * the cookie data in the form of name[=data].
1024 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ strlenW(cookie
);
1027 res
= set_cookie(hostName
, path
, cookie
, data
);
1032 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
);
1035 /***********************************************************************
1036 * InternetSetCookieW (WININET.@)
1038 * Sets a cookie for the specified URL.
1040 BOOL WINAPI
InternetSetCookieW(const WCHAR
*url
, const WCHAR
*name
, const WCHAR
*data
)
1042 TRACE("(%s, %s, %s)\n", debugstr_w(url
), debugstr_w(name
), debugstr_w(data
));
1044 return InternetSetCookieExW(url
, name
, data
, 0, 0) == COOKIE_STATE_ACCEPT
;
1047 /***********************************************************************
1048 * InternetSetCookieA (WININET.@)
1050 * Sets cookie for the specified url
1057 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
1058 LPCSTR lpCookieData
)
1060 LPWSTR data
, url
, name
;
1063 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
1064 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
1066 url
= heap_strdupAtoW(lpszUrl
);
1067 name
= heap_strdupAtoW(lpszCookieName
);
1068 data
= heap_strdupAtoW(lpCookieData
);
1070 r
= InternetSetCookieW( url
, name
, data
);
1078 /***********************************************************************
1079 * InternetSetCookieExA (WININET.@)
1081 * See InternetSetCookieExW.
1083 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
1084 DWORD dwFlags
, DWORD_PTR dwReserved
)
1086 WCHAR
*data
, *url
, *name
;
1089 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_a(lpszURL
), debugstr_a(lpszCookieName
),
1090 debugstr_a(lpszCookieData
), dwFlags
, dwReserved
);
1092 url
= heap_strdupAtoW(lpszURL
);
1093 name
= heap_strdupAtoW(lpszCookieName
);
1094 data
= heap_strdupAtoW(lpszCookieData
);
1096 r
= InternetSetCookieExW(url
, name
, data
, dwFlags
, dwReserved
);
1104 /***********************************************************************
1105 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1107 * Clears all per-site decisions about cookies.
1114 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
1120 /***********************************************************************
1121 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1123 * See InternetEnumPerSiteCookieDecisionW.
1125 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1126 ULONG
*pdwDecision
, ULONG dwIndex
)
1128 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1129 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1133 /***********************************************************************
1134 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1136 * Enumerates all per-site decisions about cookies.
1143 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1144 ULONG
*pdwDecision
, ULONG dwIndex
)
1146 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1147 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1151 /***********************************************************************
1152 * InternetGetPerSiteCookieDecisionA (WININET.@)
1154 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, ULONG
*pResult
)
1156 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
1160 /***********************************************************************
1161 * InternetGetPerSiteCookieDecisionW (WININET.@)
1163 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, ULONG
*pResult
)
1165 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
1169 /***********************************************************************
1170 * InternetSetPerSiteCookieDecisionA (WININET.@)
1172 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
1174 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
1178 /***********************************************************************
1179 * InternetSetPerSiteCookieDecisionW (WININET.@)
1181 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
1183 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);
1187 void free_cookie(void)
1189 DeleteCriticalSection(&cookie_cs
);