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
23 #define WIN32_NO_STATUS
25 #define COM_NO_WINDOWS_H
28 //#include "wine/port.h"
42 //#include "winerror.h"
44 #if defined(__MINGW32__) || defined (_MSC_VER)
48 #include <wine/debug.h>
51 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
54 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
57 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
60 typedef struct _cookie_domain cookie_domain
;
61 typedef struct _cookie cookie
;
67 struct _cookie_domain
*parent
;
80 LPWSTR lpCookieDomain
;
82 struct list cookie_list
;
85 static CRITICAL_SECTION cookie_cs
;
86 static CRITICAL_SECTION_DEBUG cookie_cs_debug
=
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
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
96 FILETIME expiry
, FILETIME create
, DWORD flags
);
97 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
);
98 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
);
99 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
);
100 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
);
101 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
102 cookie_domain
*searchDomain
, BOOL allow_partial
);
104 static BOOL
create_cookie_url(LPCWSTR domain
, LPCWSTR path
, WCHAR
*buf
, DWORD buf_len
)
106 static const WCHAR cookie_prefix
[] = {'C','o','o','k','i','e',':'};
111 if(buf_len
< sizeof(cookie_prefix
)/sizeof(WCHAR
))
113 memcpy(buf
, cookie_prefix
, sizeof(cookie_prefix
));
114 buf
+= sizeof(cookie_prefix
)/sizeof(WCHAR
);
115 buf_len
-= sizeof(cookie_prefix
)/sizeof(WCHAR
);
119 if(!GetUserNameW(buf
, &len
))
129 len
= strlenW(domain
);
132 memcpy(buf
, domain
, len
*sizeof(WCHAR
));
139 memcpy(buf
, path
, len
*sizeof(WCHAR
));
149 static BOOL
load_persistent_cookie(LPCWSTR domain
, LPCWSTR path
)
151 INTERNET_CACHE_ENTRY_INFOW
*info
;
152 cookie_domain
*domain_container
= NULL
;
155 WCHAR cookie_url
[MAX_PATH
];
157 char *str
= NULL
, *pbeg
, *pend
;
160 FILETIME expiry
, create
, time
;
162 if (!create_cookie_url(domain
, path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
166 RetrieveUrlCacheEntryStreamW(cookie_url
, NULL
, &size
, FALSE
, 0);
167 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
169 info
= heap_alloc(size
);
172 cookie
= RetrieveUrlCacheEntryStreamW(cookie_url
, info
, &size
, FALSE
, 0);
173 size
= info
->dwSizeLow
;
178 if(!(str
= heap_alloc(size
+1)) || !ReadUrlCacheEntryStream(cookie
, 0, str
, &size
, 0)) {
179 UnlockUrlCacheEntryStream(cookie
, 0);
184 UnlockUrlCacheEntryStream(cookie
, 0);
186 LIST_FOR_EACH(iter
, &domain_list
)
188 domain_container
= LIST_ENTRY(iter
, cookie_domain
, entry
);
189 if(COOKIE_matchDomain(domain
, path
, domain_container
, FALSE
))
191 domain_container
= NULL
;
193 if(!domain_container
)
194 domain_container
= COOKIE_addDomain(domain
, path
);
195 if(!domain_container
) {
200 GetSystemTimeAsFileTime(&time
);
201 for(pbeg
=str
; pbeg
&& *pbeg
; name
=data
=NULL
) {
202 pend
= strchr(pbeg
, '\n');
206 name
= heap_strdupAtoW(pbeg
);
209 pend
= strchr(pbeg
, '\n');
213 data
= heap_strdupAtoW(pbeg
);
216 pbeg
= strchr(pend
+1, '\n');
219 sscanf(pbeg
, "%u %u %u %u %u", &flags
, &expiry
.dwLowDateTime
, &expiry
.dwHighDateTime
,
220 &create
.dwLowDateTime
, &create
.dwHighDateTime
);
223 pbeg
= strchr(pbeg
, '*');
233 if(CompareFileTime(&time
, &expiry
) <= 0) {
234 if((old_cookie
= COOKIE_findCookie(domain_container
, name
)))
235 COOKIE_deleteCookie(old_cookie
, FALSE
);
236 COOKIE_addCookie(domain_container
, name
, data
, expiry
, create
, flags
);
247 static BOOL
save_persistent_cookie(cookie_domain
*domain
)
249 static const WCHAR txtW
[] = {'t','x','t',0};
251 WCHAR cookie_url
[MAX_PATH
], cookie_file
[MAX_PATH
];
252 HANDLE cookie_handle
;
253 cookie
*cookie_container
= NULL
, *cookie_iter
;
254 BOOL do_save
= FALSE
;
255 char buf
[64], *dyn_buf
;
257 DWORD dwBytesWritten
;
259 if (!create_cookie_url(domain
->lpCookieDomain
, domain
->lpCookiePath
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
262 /* check if there's anything to save */
263 GetSystemTimeAsFileTime(&time
);
264 LIST_FOR_EACH_ENTRY_SAFE(cookie_container
, cookie_iter
, &domain
->cookie_list
, cookie
, entry
)
266 if((cookie_container
->expiry
.dwLowDateTime
|| cookie_container
->expiry
.dwHighDateTime
)
267 && CompareFileTime(&time
, &cookie_container
->expiry
) > 0) {
268 COOKIE_deleteCookie(cookie_container
, FALSE
);
272 if(!(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)) {
278 DeleteUrlCacheEntryW(cookie_url
);
282 if(!CreateUrlCacheEntryW(cookie_url
, 0, txtW
, cookie_file
, 0))
284 cookie_handle
= CreateFileW(cookie_file
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
285 if(cookie_handle
== INVALID_HANDLE_VALUE
) {
286 DeleteFileW(cookie_file
);
290 LIST_FOR_EACH_ENTRY(cookie_container
, &domain
->cookie_list
, cookie
, entry
)
292 if(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)
295 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieName
);
296 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
302 if(!WriteFile(cookie_handle
, "\n", 1, &dwBytesWritten
, NULL
)) {
307 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieData
);
308 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
314 if(!WriteFile(cookie_handle
, "\n", 1, &dwBytesWritten
, NULL
)) {
319 dyn_buf
= heap_strdupWtoA(domain
->lpCookieDomain
);
320 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
327 dyn_buf
= heap_strdupWtoA(domain
->lpCookiePath
);
328 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
335 sprintf(buf
, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container
->flags
,
336 cookie_container
->expiry
.dwLowDateTime
, cookie_container
->expiry
.dwHighDateTime
,
337 cookie_container
->create
.dwLowDateTime
, cookie_container
->create
.dwHighDateTime
);
338 if(!WriteFile(cookie_handle
, buf
, strlen(buf
), &dwBytesWritten
, NULL
)) {
344 CloseHandle(cookie_handle
);
346 ERR("error saving cookie file\n");
347 DeleteFileW(cookie_file
);
351 memset(&time
, 0, sizeof(time
));
352 return CommitUrlCacheEntryW(cookie_url
, cookie_file
, time
, time
, 0, NULL
, 0, txtW
, 0);
355 /* adds a cookie to the domain */
356 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
357 FILETIME expiry
, FILETIME create
, DWORD flags
)
359 cookie
*newCookie
= heap_alloc(sizeof(cookie
));
363 newCookie
->lpCookieName
= heap_strdupW(name
);
364 newCookie
->lpCookieData
= heap_strdupW(data
);
366 if (!newCookie
->lpCookieName
|| !newCookie
->lpCookieData
)
368 heap_free(newCookie
->lpCookieName
);
369 heap_free(newCookie
->lpCookieData
);
370 heap_free(newCookie
);
375 newCookie
->flags
= flags
;
376 newCookie
->expiry
= expiry
;
377 newCookie
->create
= create
;
379 TRACE("added cookie %p (data is %s)\n", newCookie
, debugstr_w(data
) );
381 list_add_tail(&domain
->cookie_list
, &newCookie
->entry
);
382 newCookie
->parent
= domain
;
387 /* finds a cookie in the domain matching the cookie name */
388 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
)
390 struct list
* cursor
;
391 TRACE("(%p, %s)\n", domain
, debugstr_w(lpszCookieName
));
393 LIST_FOR_EACH(cursor
, &domain
->cookie_list
)
395 cookie
*searchCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
396 BOOL candidate
= TRUE
;
397 if (candidate
&& lpszCookieName
)
399 if (candidate
&& !searchCookie
->lpCookieName
)
401 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
410 /* removes a cookie from the list, if its the last cookie we also remove the domain */
411 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
)
413 heap_free(deadCookie
->lpCookieName
);
414 heap_free(deadCookie
->lpCookieData
);
415 list_remove(&deadCookie
->entry
);
417 /* special case: last cookie, lets remove the domain to save memory */
418 if (list_empty(&deadCookie
->parent
->cookie_list
) && deleteDomain
)
419 COOKIE_deleteDomain(deadCookie
->parent
);
420 heap_free(deadCookie
);
423 /* allocates a domain and adds it to the end */
424 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
)
426 cookie_domain
*newDomain
= heap_alloc(sizeof(cookie_domain
));
428 list_init(&newDomain
->entry
);
429 list_init(&newDomain
->cookie_list
);
430 newDomain
->lpCookieDomain
= heap_strdupW(domain
);
431 newDomain
->lpCookiePath
= heap_strdupW(path
);
433 list_add_tail(&domain_list
, &newDomain
->entry
);
435 TRACE("Adding domain: %p\n", newDomain
);
439 static BOOL
COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
441 URL_COMPONENTSW UrlComponents
;
443 UrlComponents
.lpszExtraInfo
= NULL
;
444 UrlComponents
.lpszPassword
= NULL
;
445 UrlComponents
.lpszScheme
= NULL
;
446 UrlComponents
.lpszUrlPath
= path
;
447 UrlComponents
.lpszUserName
= NULL
;
448 UrlComponents
.lpszHostName
= hostName
;
449 UrlComponents
.dwExtraInfoLength
= 0;
450 UrlComponents
.dwPasswordLength
= 0;
451 UrlComponents
.dwSchemeLength
= 0;
452 UrlComponents
.dwUserNameLength
= 0;
453 UrlComponents
.dwHostNameLength
= hostNameLen
;
454 UrlComponents
.dwUrlPathLength
= pathLen
;
456 if (!InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
)) return FALSE
;
458 /* discard the webpage off the end of the path */
459 if (UrlComponents
.dwUrlPathLength
)
461 if (path
[UrlComponents
.dwUrlPathLength
- 1] != '/')
464 if ((ptr
= strrchrW(path
, '/'))) *(++ptr
) = 0;
472 else if (pathLen
>= 2)
480 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
481 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
482 cookie_domain
*searchDomain
, BOOL allow_partial
)
484 TRACE("searching on domain %p\n", searchDomain
);
485 if (lpszCookieDomain
)
487 if (!searchDomain
->lpCookieDomain
)
490 TRACE("comparing domain %s with %s\n",
491 debugstr_w(lpszCookieDomain
),
492 debugstr_w(searchDomain
->lpCookieDomain
));
494 if (allow_partial
&& !strstrW(lpszCookieDomain
, searchDomain
->lpCookieDomain
))
496 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
502 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
503 /* paths match at the beginning. so a path of /foo would match
504 * /foobar and /foo/bar
506 if (!searchDomain
->lpCookiePath
)
510 len
= lstrlenW(searchDomain
->lpCookiePath
);
511 if (strncmpiW(searchDomain
->lpCookiePath
, lpszCookiePath
, len
)!=0)
514 else if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
521 /* remove a domain from the list and delete it */
522 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
)
524 struct list
* cursor
;
525 while ((cursor
= list_tail(&deadDomain
->cookie_list
)))
527 COOKIE_deleteCookie(LIST_ENTRY(cursor
, cookie
, entry
), FALSE
);
530 heap_free(deadDomain
->lpCookieDomain
);
531 heap_free(deadDomain
->lpCookiePath
);
533 list_remove(&deadDomain
->entry
);
535 heap_free(deadDomain
);
538 DWORD
get_cookie(const WCHAR
*host
, const WCHAR
*path
, WCHAR
*cookie_data
, DWORD
*size
)
540 unsigned cnt
= 0, len
, name_len
, domain_count
= 0, cookie_count
= 0;
541 WCHAR
*ptr
= cookie_data
;
542 cookie_domain
*domain
;
545 GetSystemTimeAsFileTime(&tm
);
547 EnterCriticalSection(&cookie_cs
);
549 load_persistent_cookie(host
, path
);
551 LIST_FOR_EACH_ENTRY(domain
, &domain_list
, cookie_domain
, entry
) {
552 struct list
*cursor
, *cursor2
;
554 if(!COOKIE_matchDomain(host
, path
, domain
, TRUE
))
558 TRACE("found domain %p\n", domain
);
560 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &domain
->cookie_list
) {
561 cookie
*cookie_iter
= LIST_ENTRY(cursor
, cookie
, entry
);
563 /* check for expiry */
564 if((cookie_iter
->expiry
.dwLowDateTime
!= 0 || cookie_iter
->expiry
.dwHighDateTime
!= 0)
565 && CompareFileTime(&tm
, &cookie_iter
->expiry
) > 0)
567 TRACE("Found expired cookie. deleting\n");
568 COOKIE_deleteCookie(cookie_iter
, FALSE
);
574 cnt
+= name_len
= strlenW(cookie_iter
->lpCookieName
);
575 if ((len
= strlenW(cookie_iter
->lpCookieData
))) {
587 memcpy(ptr
, cookie_iter
->lpCookieName
, name_len
*sizeof(WCHAR
));
592 memcpy(ptr
, cookie_iter
->lpCookieData
, len
*sizeof(WCHAR
));
596 assert(cookie_data
+cnt
== ptr
);
597 TRACE("Cookie: %s\n", debugstr_wn(cookie_data
, cnt
));
599 /* Stop writing data, just compute the size */
608 LeaveCriticalSection(&cookie_cs
);
614 TRACE("no cookies found for %s\n", debugstr_w(host
));
615 return ERROR_NO_MORE_ITEMS
;
618 if(!cookie_data
|| !ptr
) {
619 *size
= (cnt
+ 1) * sizeof(WCHAR
);
620 TRACE("returning %u\n", *size
);
621 return cookie_data
? ERROR_INSUFFICIENT_BUFFER
: ERROR_SUCCESS
;
626 TRACE("Returning %u (from %u domains): %s\n", cnt
, domain_count
, debugstr_w(cookie_data
));
627 return ERROR_SUCCESS
;
630 /***********************************************************************
631 * InternetGetCookieW (WININET.@)
633 * Retrieve cookie from the specified url
635 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
636 * So it won't be implemented here.
643 BOOL WINAPI
InternetGetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
644 LPWSTR lpCookieData
, LPDWORD lpdwSize
)
646 WCHAR host
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
650 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
), lpCookieData
, lpdwSize
);
654 SetLastError(ERROR_INVALID_PARAMETER
);
659 ret
= COOKIE_crackUrlSimple(lpszUrl
, host
, sizeof(host
)/sizeof(host
[0]), path
, sizeof(path
)/sizeof(path
[0]));
660 if (!ret
|| !host
[0]) {
661 SetLastError(ERROR_INVALID_PARAMETER
);
665 res
= get_cookie(host
, path
, lpCookieData
, lpdwSize
);
666 if(res
!= ERROR_SUCCESS
)
668 return res
== ERROR_SUCCESS
;
672 /***********************************************************************
673 * InternetGetCookieA (WININET.@)
675 * Retrieve cookie from the specified url
682 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
683 LPSTR lpCookieData
, LPDWORD lpdwSize
)
689 TRACE("(%s %s %p %p(%u))\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
690 lpCookieData
, lpdwSize
, lpdwSize
? *lpdwSize
: 0);
692 url
= heap_strdupAtoW(lpszUrl
);
693 name
= heap_strdupAtoW(lpszCookieName
);
695 r
= InternetGetCookieW( url
, name
, NULL
, &len
);
700 szCookieData
= heap_alloc(len
* sizeof(WCHAR
));
707 r
= InternetGetCookieW( url
, name
, szCookieData
, &len
);
710 size
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, NULL
, 0, NULL
, NULL
);
712 if(*lpdwSize
>= size
) {
713 WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, lpCookieData
, *lpdwSize
, NULL
, NULL
);
715 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
722 heap_free( szCookieData
);
731 /***********************************************************************
732 * IsDomainLegalCookieDomainW (WININET.@)
734 BOOL WINAPI
IsDomainLegalCookieDomainW( LPCWSTR s1
, LPCWSTR s2
)
736 DWORD s1_len
, s2_len
;
738 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1
), debugstr_w(s2
));
742 SetLastError(ERROR_INVALID_PARAMETER
);
745 if (s1
[0] == '.' || !s1
[0] || s2
[0] == '.' || !s2
[0])
747 SetLastError(ERROR_INVALID_NAME
);
750 if(!strchrW(s1
, '.') || !strchrW(s2
, '.'))
753 s1_len
= strlenW(s1
);
754 s2_len
= strlenW(s2
);
758 if (strncmpiW(s1
, s2
+s2_len
-s1_len
, s1_len
) || (s2_len
>s1_len
&& s2
[s2_len
-s1_len
-1]!='.'))
760 SetLastError(ERROR_INVALID_PARAMETER
);
767 BOOL
set_cookie(LPCWSTR domain
, LPCWSTR path
, LPCWSTR cookie_name
, LPCWSTR cookie_data
)
769 cookie_domain
*thisCookieDomain
= NULL
;
774 FILETIME expiry
, create
;
775 BOOL expired
= FALSE
, update_persistent
= FALSE
;
778 value
= data
= heap_strdupW(cookie_data
);
781 ERR("could not allocate the cookie data buffer\n");
785 memset(&expiry
,0,sizeof(expiry
));
786 GetSystemTimeAsFileTime(&create
);
788 /* lots of information can be parsed out of the cookie value */
793 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
794 static const WCHAR szPath
[] = {'p','a','t','h','=',0};
795 static const WCHAR szExpires
[] = {'e','x','p','i','r','e','s','=',0};
796 static const WCHAR szSecure
[] = {'s','e','c','u','r','e',0};
797 static const WCHAR szHttpOnly
[] = {'h','t','t','p','o','n','l','y',0};
799 if (!(ptr
= strchrW(ptr
,';'))) break;
802 if (value
!= data
) heap_free(value
);
803 value
= heap_alloc((ptr
- data
) * sizeof(WCHAR
));
807 ERR("could not allocate the cookie value buffer\n");
810 strcpyW(value
, data
);
812 while (*ptr
== ' ') ptr
++; /* whitespace */
814 if (strncmpiW(ptr
, szDomain
, 7) == 0)
818 ptr
+= sizeof(szDomain
)/sizeof(szDomain
[0])-1;
821 end_ptr
= strchrW(ptr
, ';');
825 if(!IsDomainLegalCookieDomainW(ptr
, domain
))
837 TRACE("Parsing new domain %s\n",debugstr_w(domain
));
839 else if (strncmpiW(ptr
, szPath
, 5) == 0)
841 ptr
+=strlenW(szPath
);
843 TRACE("Parsing new path %s\n",debugstr_w(path
));
845 else if (strncmpiW(ptr
, szExpires
, 8) == 0)
848 ptr
+=strlenW(szExpires
);
849 if (InternetTimeToSystemTimeW(ptr
, &st
, 0))
851 SystemTimeToFileTime(&st
, &expiry
);
853 if (CompareFileTime(&create
,&expiry
) > 0)
855 TRACE("Cookie already expired.\n");
860 else if (strncmpiW(ptr
, szSecure
, 6) == 0)
862 FIXME("secure not handled (%s)\n",debugstr_w(ptr
));
863 ptr
+= strlenW(szSecure
);
865 else if (strncmpiW(ptr
, szHttpOnly
, 8) == 0)
867 FIXME("httponly not handled (%s)\n",debugstr_w(ptr
));
868 ptr
+= strlenW(szHttpOnly
);
872 FIXME("Unknown additional option %s\n",debugstr_w(ptr
));
877 EnterCriticalSection(&cookie_cs
);
879 load_persistent_cookie(domain
, path
);
881 LIST_FOR_EACH(cursor
, &domain_list
)
883 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
884 if (COOKIE_matchDomain(domain
, path
, thisCookieDomain
, FALSE
))
886 thisCookieDomain
= NULL
;
889 if (!thisCookieDomain
)
892 thisCookieDomain
= COOKIE_addDomain(domain
, path
);
896 if (value
!= data
) heap_free(value
);
897 LeaveCriticalSection(&cookie_cs
);
902 if(!expiry
.dwLowDateTime
&& !expiry
.dwHighDateTime
)
903 flags
|= INTERNET_COOKIE_IS_SESSION
;
905 update_persistent
= TRUE
;
907 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, cookie_name
)))
909 if (!(thisCookie
->flags
& INTERNET_COOKIE_IS_SESSION
))
910 update_persistent
= TRUE
;
911 COOKIE_deleteCookie(thisCookie
, FALSE
);
914 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name
),
915 debugstr_w(value
), debugstr_w(thisCookieDomain
->lpCookieDomain
),debugstr_w(thisCookieDomain
->lpCookiePath
));
917 if (!expired
&& !COOKIE_addCookie(thisCookieDomain
, cookie_name
, value
, expiry
, create
, flags
))
920 if (value
!= data
) heap_free(value
);
921 LeaveCriticalSection(&cookie_cs
);
925 if (value
!= data
) heap_free(value
);
927 if (!update_persistent
|| save_persistent_cookie(thisCookieDomain
))
929 LeaveCriticalSection(&cookie_cs
);
932 LeaveCriticalSection(&cookie_cs
);
936 /***********************************************************************
937 * InternetSetCookieW (WININET.@)
939 * Sets cookie for the specified url
946 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
947 LPCWSTR lpCookieData
)
950 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
952 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
953 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
955 if (!lpszUrl
|| !lpCookieData
)
957 SetLastError(ERROR_INVALID_PARAMETER
);
962 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
963 if (!ret
|| !hostName
[0]) return FALSE
;
967 WCHAR
*cookie
, *data
;
969 cookie
= heap_strdupW(lpCookieData
);
972 SetLastError(ERROR_OUTOFMEMORY
);
976 /* some apps (or is it us??) try to add a cookie with no cookie name, but
977 * the cookie data in the form of name[=data].
979 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ strlenW(cookie
);
982 ret
= set_cookie(hostName
, path
, cookie
, data
);
987 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
);
991 /***********************************************************************
992 * InternetSetCookieA (WININET.@)
994 * Sets cookie for the specified url
1001 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
1002 LPCSTR lpCookieData
)
1004 LPWSTR data
, url
, name
;
1007 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
1008 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
1010 url
= heap_strdupAtoW(lpszUrl
);
1011 name
= heap_strdupAtoW(lpszCookieName
);
1012 data
= heap_strdupAtoW(lpCookieData
);
1014 r
= InternetSetCookieW( url
, name
, data
);
1022 /***********************************************************************
1023 * InternetSetCookieExA (WININET.@)
1025 * See InternetSetCookieExW.
1027 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
1028 DWORD dwFlags
, DWORD_PTR dwReserved
)
1030 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1031 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
1032 dwFlags
, dwReserved
);
1034 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1035 return InternetSetCookieA(lpszURL
, lpszCookieName
, lpszCookieData
);
1038 /***********************************************************************
1039 * InternetSetCookieExW (WININET.@)
1041 * Sets a cookie for the specified URL.
1048 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
1049 DWORD dwFlags
, DWORD_PTR dwReserved
)
1051 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1052 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
1053 dwFlags
, dwReserved
);
1055 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1056 return InternetSetCookieW(lpszURL
, lpszCookieName
, lpszCookieData
);
1059 /***********************************************************************
1060 * InternetGetCookieExA (WININET.@)
1062 * See InternetGetCookieExW.
1064 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
1065 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
1067 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1068 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
1069 pcchCookieData
, dwFlags
, lpReserved
);
1071 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1072 return InternetGetCookieA(pchURL
, pchCookieName
, pchCookieData
, pcchCookieData
);
1075 /***********************************************************************
1076 * InternetGetCookieExW (WININET.@)
1078 * Retrieve cookie for the specified URL.
1085 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
1086 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
1088 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1089 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
1090 pcchCookieData
, dwFlags
, lpReserved
);
1092 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1093 return InternetGetCookieW(pchURL
, pchCookieName
, pchCookieData
, pcchCookieData
);
1096 /***********************************************************************
1097 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1099 * Clears all per-site decisions about cookies.
1106 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
1112 /***********************************************************************
1113 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1115 * See InternetEnumPerSiteCookieDecisionW.
1117 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1118 ULONG
*pdwDecision
, ULONG dwIndex
)
1120 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1121 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1125 /***********************************************************************
1126 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1128 * Enumerates all per-site decisions about cookies.
1135 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1136 ULONG
*pdwDecision
, ULONG dwIndex
)
1138 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1139 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1143 /***********************************************************************
1144 * InternetGetPerSiteCookieDecisionA (WININET.@)
1146 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, ULONG
*pResult
)
1148 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
1152 /***********************************************************************
1153 * InternetGetPerSiteCookieDecisionW (WININET.@)
1155 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, ULONG
*pResult
)
1157 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
1161 /***********************************************************************
1162 * InternetSetPerSiteCookieDecisionA (WININET.@)
1164 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
1166 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
1170 /***********************************************************************
1171 * InternetSetPerSiteCookieDecisionW (WININET.@)
1173 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
1175 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);
1179 void free_cookie(void)
1181 DeleteCriticalSection(&cookie_cs
);