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
25 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
28 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
31 typedef struct _cookie_domain cookie_domain
;
32 typedef struct _cookie cookie
;
38 struct _cookie_domain
*parent
;
51 LPWSTR lpCookieDomain
;
53 struct list cookie_list
;
56 static CRITICAL_SECTION cookie_cs
;
57 static CRITICAL_SECTION_DEBUG cookie_cs_debug
=
60 { &cookie_cs_debug
.ProcessLocksList
, &cookie_cs_debug
.ProcessLocksList
},
61 0, 0, { (DWORD_PTR
)(__FILE__
": cookie_cs") }
63 static CRITICAL_SECTION cookie_cs
= { &cookie_cs_debug
, -1, 0, 0, 0, 0 };
64 static struct list domain_list
= LIST_INIT(domain_list
);
66 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
67 FILETIME expiry
, FILETIME create
, DWORD flags
);
68 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
);
69 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
);
70 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
);
71 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
);
72 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
73 cookie_domain
*searchDomain
, BOOL allow_partial
);
75 static BOOL
create_cookie_url(LPCWSTR domain
, LPCWSTR path
, WCHAR
*buf
, DWORD buf_len
)
77 static const WCHAR cookie_prefix
[] = {'C','o','o','k','i','e',':'};
82 if(buf_len
< sizeof(cookie_prefix
)/sizeof(WCHAR
))
84 memcpy(buf
, cookie_prefix
, sizeof(cookie_prefix
));
85 buf
+= sizeof(cookie_prefix
)/sizeof(WCHAR
);
86 buf_len
-= sizeof(cookie_prefix
)/sizeof(WCHAR
);
90 if(!GetUserNameW(buf
, &len
))
100 len
= strlenW(domain
);
103 memcpy(buf
, domain
, len
*sizeof(WCHAR
));
110 memcpy(buf
, path
, len
*sizeof(WCHAR
));
120 static BOOL
load_persistent_cookie(LPCWSTR domain
, LPCWSTR path
)
122 INTERNET_CACHE_ENTRY_INFOW
*info
;
123 cookie_domain
*domain_container
= NULL
;
126 WCHAR cookie_url
[MAX_PATH
];
128 char *str
= NULL
, *pbeg
, *pend
;
131 FILETIME expiry
, create
, time
;
133 if (!create_cookie_url(domain
, path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
137 RetrieveUrlCacheEntryStreamW(cookie_url
, NULL
, &size
, FALSE
, 0);
138 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
140 info
= heap_alloc(size
);
143 cookie
= RetrieveUrlCacheEntryStreamW(cookie_url
, info
, &size
, FALSE
, 0);
144 size
= info
->dwSizeLow
;
149 if(!(str
= heap_alloc(size
+1)) || !ReadUrlCacheEntryStream(cookie
, 0, str
, &size
, 0)) {
150 UnlockUrlCacheEntryStream(cookie
, 0);
155 UnlockUrlCacheEntryStream(cookie
, 0);
157 LIST_FOR_EACH(iter
, &domain_list
)
159 domain_container
= LIST_ENTRY(iter
, cookie_domain
, entry
);
160 if(COOKIE_matchDomain(domain
, path
, domain_container
, FALSE
))
162 domain_container
= NULL
;
164 if(!domain_container
)
165 domain_container
= COOKIE_addDomain(domain
, path
);
166 if(!domain_container
) {
171 GetSystemTimeAsFileTime(&time
);
172 for(pbeg
=str
; pbeg
&& *pbeg
; name
=data
=NULL
) {
173 pend
= strchr(pbeg
, '\n');
177 name
= heap_strdupAtoW(pbeg
);
180 pend
= strchr(pbeg
, '\n');
184 data
= heap_strdupAtoW(pbeg
);
187 pbeg
= strchr(pend
+1, '\n');
190 sscanf(pbeg
, "%u %u %u %u %u", &flags
, &expiry
.dwLowDateTime
, &expiry
.dwHighDateTime
,
191 &create
.dwLowDateTime
, &create
.dwHighDateTime
);
194 pbeg
= strchr(pbeg
, '*');
204 if(CompareFileTime(&time
, &expiry
) <= 0) {
205 if((old_cookie
= COOKIE_findCookie(domain_container
, name
)))
206 COOKIE_deleteCookie(old_cookie
, FALSE
);
207 COOKIE_addCookie(domain_container
, name
, data
, expiry
, create
, flags
);
219 static BOOL
save_persistent_cookie(cookie_domain
*domain
)
221 static const WCHAR txtW
[] = {'t','x','t',0};
223 WCHAR cookie_url
[MAX_PATH
], cookie_file
[MAX_PATH
];
224 HANDLE cookie_handle
;
225 cookie
*cookie_container
= NULL
, *cookie_iter
;
226 BOOL do_save
= FALSE
;
227 char buf
[64], *dyn_buf
;
229 DWORD dwBytesWritten
;
231 if (!create_cookie_url(domain
->lpCookieDomain
, domain
->lpCookiePath
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
234 /* check if there's anything to save */
235 GetSystemTimeAsFileTime(&time
);
236 LIST_FOR_EACH_ENTRY_SAFE(cookie_container
, cookie_iter
, &domain
->cookie_list
, cookie
, entry
)
238 if((cookie_container
->expiry
.dwLowDateTime
|| cookie_container
->expiry
.dwHighDateTime
)
239 && CompareFileTime(&time
, &cookie_container
->expiry
) > 0) {
240 COOKIE_deleteCookie(cookie_container
, FALSE
);
244 if(!(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)) {
250 DeleteUrlCacheEntryW(cookie_url
);
254 if(!CreateUrlCacheEntryW(cookie_url
, 0, txtW
, cookie_file
, 0))
256 cookie_handle
= CreateFileW(cookie_file
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
257 if(cookie_handle
== INVALID_HANDLE_VALUE
) {
258 DeleteFileW(cookie_file
);
262 LIST_FOR_EACH_ENTRY(cookie_container
, &domain
->cookie_list
, cookie
, entry
)
264 if(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)
267 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieName
);
268 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
274 if(!WriteFile(cookie_handle
, "\n", 1, &dwBytesWritten
, NULL
)) {
279 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieData
);
280 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
286 if(!WriteFile(cookie_handle
, "\n", 1, &dwBytesWritten
, NULL
)) {
291 dyn_buf
= heap_strdupWtoA(domain
->lpCookieDomain
);
292 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
299 dyn_buf
= heap_strdupWtoA(domain
->lpCookiePath
);
300 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &dwBytesWritten
, NULL
)) {
307 sprintf(buf
, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container
->flags
,
308 cookie_container
->expiry
.dwLowDateTime
, cookie_container
->expiry
.dwHighDateTime
,
309 cookie_container
->create
.dwLowDateTime
, cookie_container
->create
.dwHighDateTime
);
310 if(!WriteFile(cookie_handle
, buf
, strlen(buf
), &dwBytesWritten
, NULL
)) {
316 CloseHandle(cookie_handle
);
318 ERR("error saving cookie file\n");
319 DeleteFileW(cookie_file
);
323 memset(&time
, 0, sizeof(time
));
324 return CommitUrlCacheEntryW(cookie_url
, cookie_file
, time
, time
, 0, NULL
, 0, txtW
, 0);
327 /* adds a cookie to the domain */
328 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
329 FILETIME expiry
, FILETIME create
, DWORD flags
)
331 cookie
*newCookie
= heap_alloc(sizeof(cookie
));
335 newCookie
->lpCookieName
= heap_strdupW(name
);
336 newCookie
->lpCookieData
= heap_strdupW(data
);
338 if (!newCookie
->lpCookieName
|| !newCookie
->lpCookieData
)
340 heap_free(newCookie
->lpCookieName
);
341 heap_free(newCookie
->lpCookieData
);
342 heap_free(newCookie
);
347 newCookie
->flags
= flags
;
348 newCookie
->expiry
= expiry
;
349 newCookie
->create
= create
;
351 TRACE("added cookie %p (data is %s)\n", newCookie
, debugstr_w(data
) );
353 list_add_tail(&domain
->cookie_list
, &newCookie
->entry
);
354 newCookie
->parent
= domain
;
359 /* finds a cookie in the domain matching the cookie name */
360 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
)
362 struct list
* cursor
;
363 TRACE("(%p, %s)\n", domain
, debugstr_w(lpszCookieName
));
365 LIST_FOR_EACH(cursor
, &domain
->cookie_list
)
367 cookie
*searchCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
368 BOOL candidate
= TRUE
;
369 if (candidate
&& lpszCookieName
)
371 if (candidate
&& !searchCookie
->lpCookieName
)
373 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
382 /* removes a cookie from the list, if its the last cookie we also remove the domain */
383 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
)
385 heap_free(deadCookie
->lpCookieName
);
386 heap_free(deadCookie
->lpCookieData
);
387 list_remove(&deadCookie
->entry
);
389 /* special case: last cookie, lets remove the domain to save memory */
390 if (list_empty(&deadCookie
->parent
->cookie_list
) && deleteDomain
)
391 COOKIE_deleteDomain(deadCookie
->parent
);
392 heap_free(deadCookie
);
395 /* allocates a domain and adds it to the end */
396 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
)
398 cookie_domain
*newDomain
= heap_alloc(sizeof(cookie_domain
));
400 list_init(&newDomain
->entry
);
401 list_init(&newDomain
->cookie_list
);
402 newDomain
->lpCookieDomain
= heap_strdupW(domain
);
403 newDomain
->lpCookiePath
= heap_strdupW(path
);
405 list_add_tail(&domain_list
, &newDomain
->entry
);
407 TRACE("Adding domain: %p\n", newDomain
);
411 static BOOL
COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
413 URL_COMPONENTSW UrlComponents
;
415 UrlComponents
.lpszExtraInfo
= NULL
;
416 UrlComponents
.lpszPassword
= NULL
;
417 UrlComponents
.lpszScheme
= NULL
;
418 UrlComponents
.lpszUrlPath
= path
;
419 UrlComponents
.lpszUserName
= NULL
;
420 UrlComponents
.lpszHostName
= hostName
;
421 UrlComponents
.dwExtraInfoLength
= 0;
422 UrlComponents
.dwPasswordLength
= 0;
423 UrlComponents
.dwSchemeLength
= 0;
424 UrlComponents
.dwUserNameLength
= 0;
425 UrlComponents
.dwHostNameLength
= hostNameLen
;
426 UrlComponents
.dwUrlPathLength
= pathLen
;
428 if (!InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
)) return FALSE
;
430 /* discard the webpage off the end of the path */
431 if (UrlComponents
.dwUrlPathLength
)
433 if (path
[UrlComponents
.dwUrlPathLength
- 1] != '/')
436 if ((ptr
= strrchrW(path
, '/'))) *(++ptr
) = 0;
444 else if (pathLen
>= 2)
452 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
453 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
454 cookie_domain
*searchDomain
, BOOL allow_partial
)
456 TRACE("searching on domain %p\n", searchDomain
);
457 if (lpszCookieDomain
)
459 if (!searchDomain
->lpCookieDomain
)
462 TRACE("comparing domain %s with %s\n",
463 debugstr_w(lpszCookieDomain
),
464 debugstr_w(searchDomain
->lpCookieDomain
));
466 if (allow_partial
&& !strstrW(lpszCookieDomain
, searchDomain
->lpCookieDomain
))
468 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
474 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
475 /* paths match at the beginning. so a path of /foo would match
476 * /foobar and /foo/bar
478 if (!searchDomain
->lpCookiePath
)
482 len
= lstrlenW(searchDomain
->lpCookiePath
);
483 if (strncmpiW(searchDomain
->lpCookiePath
, lpszCookiePath
, len
)!=0)
486 else if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
493 /* remove a domain from the list and delete it */
494 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
)
496 struct list
* cursor
;
497 while ((cursor
= list_tail(&deadDomain
->cookie_list
)))
499 COOKIE_deleteCookie(LIST_ENTRY(cursor
, cookie
, entry
), FALSE
);
502 heap_free(deadDomain
->lpCookieDomain
);
503 heap_free(deadDomain
->lpCookiePath
);
505 list_remove(&deadDomain
->entry
);
507 heap_free(deadDomain
);
510 DWORD
get_cookie(const WCHAR
*host
, const WCHAR
*path
, WCHAR
*cookie_data
, DWORD
*size
)
512 static const WCHAR empty_path
[] = { '/',0 };
514 unsigned cnt
= 0, len
, name_len
, domain_count
= 0, cookie_count
= 0;
515 WCHAR
*ptr
, subpath
[INTERNET_MAX_PATH_LENGTH
];
517 cookie_domain
*domain
;
520 GetSystemTimeAsFileTime(&tm
);
522 EnterCriticalSection(&cookie_cs
);
526 while(p
>host
&& p
[-1]!='.') p
--;
529 while(p
>host
&& p
[-1]!='.') p
--;
532 load_persistent_cookie(p
, empty_path
);
536 assert(len
+1 < INTERNET_MAX_PATH_LENGTH
);
537 memcpy(subpath
, path
, (len
+1)*sizeof(WCHAR
));
541 load_persistent_cookie(host
, subpath
);
544 while(ptr
>subpath
&& ptr
[-1]!='/') ptr
--;
545 }while(ptr
!= subpath
);
548 LIST_FOR_EACH_ENTRY(domain
, &domain_list
, cookie_domain
, entry
) {
549 struct list
*cursor
, *cursor2
;
551 if(!COOKIE_matchDomain(host
, path
, domain
, TRUE
))
555 TRACE("found domain %p\n", domain
);
557 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &domain
->cookie_list
) {
558 cookie
*cookie_iter
= LIST_ENTRY(cursor
, cookie
, entry
);
560 /* check for expiry */
561 if((cookie_iter
->expiry
.dwLowDateTime
!= 0 || cookie_iter
->expiry
.dwHighDateTime
!= 0)
562 && CompareFileTime(&tm
, &cookie_iter
->expiry
) > 0)
564 TRACE("Found expired cookie. deleting\n");
565 COOKIE_deleteCookie(cookie_iter
, FALSE
);
571 cnt
+= name_len
= strlenW(cookie_iter
->lpCookieName
);
572 if ((len
= strlenW(cookie_iter
->lpCookieData
))) {
584 memcpy(ptr
, cookie_iter
->lpCookieName
, name_len
*sizeof(WCHAR
));
589 memcpy(ptr
, cookie_iter
->lpCookieData
, len
*sizeof(WCHAR
));
593 assert(cookie_data
+cnt
== ptr
);
594 TRACE("Cookie: %s\n", debugstr_wn(cookie_data
, cnt
));
596 /* Stop writing data, just compute the size */
605 LeaveCriticalSection(&cookie_cs
);
611 TRACE("no cookies found for %s\n", debugstr_w(host
));
612 return ERROR_NO_MORE_ITEMS
;
615 if(!cookie_data
|| !ptr
) {
616 *size
= (cnt
+ 1) * sizeof(WCHAR
);
617 TRACE("returning %u\n", *size
);
618 return cookie_data
? ERROR_INSUFFICIENT_BUFFER
: ERROR_SUCCESS
;
623 TRACE("Returning %u (from %u domains): %s\n", cnt
, domain_count
, debugstr_w(cookie_data
));
624 return ERROR_SUCCESS
;
627 /***********************************************************************
628 * InternetGetCookieW (WININET.@)
630 * Retrieve cookie from the specified url
632 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
633 * So it won't be implemented here.
640 BOOL WINAPI
InternetGetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
641 LPWSTR lpCookieData
, LPDWORD lpdwSize
)
643 WCHAR host
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
647 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
), lpCookieData
, lpdwSize
);
651 SetLastError(ERROR_INVALID_PARAMETER
);
656 ret
= COOKIE_crackUrlSimple(lpszUrl
, host
, sizeof(host
)/sizeof(host
[0]), path
, sizeof(path
)/sizeof(path
[0]));
657 if (!ret
|| !host
[0]) {
658 SetLastError(ERROR_INVALID_PARAMETER
);
662 res
= get_cookie(host
, path
, lpCookieData
, lpdwSize
);
663 if(res
!= ERROR_SUCCESS
)
665 return res
== ERROR_SUCCESS
;
669 /***********************************************************************
670 * InternetGetCookieA (WININET.@)
672 * Retrieve cookie from the specified url
679 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
680 LPSTR lpCookieData
, LPDWORD lpdwSize
)
686 TRACE("(%s %s %p %p(%u))\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
687 lpCookieData
, lpdwSize
, lpdwSize
? *lpdwSize
: 0);
689 url
= heap_strdupAtoW(lpszUrl
);
690 name
= heap_strdupAtoW(lpszCookieName
);
692 r
= InternetGetCookieW( url
, name
, NULL
, &len
);
697 szCookieData
= heap_alloc(len
* sizeof(WCHAR
));
704 r
= InternetGetCookieW( url
, name
, szCookieData
, &len
);
707 size
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, NULL
, 0, NULL
, NULL
);
709 if(*lpdwSize
>= size
) {
710 WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, lpCookieData
, *lpdwSize
, NULL
, NULL
);
712 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
719 heap_free( szCookieData
);
728 /***********************************************************************
729 * IsDomainLegalCookieDomainW (WININET.@)
731 BOOL WINAPI
IsDomainLegalCookieDomainW( LPCWSTR s1
, LPCWSTR s2
)
733 DWORD s1_len
, s2_len
;
735 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1
), debugstr_w(s2
));
739 SetLastError(ERROR_INVALID_PARAMETER
);
742 if (s1
[0] == '.' || !s1
[0] || s2
[0] == '.' || !s2
[0])
744 SetLastError(ERROR_INVALID_NAME
);
747 if(!strchrW(s1
, '.') || !strchrW(s2
, '.'))
750 s1_len
= strlenW(s1
);
751 s2_len
= strlenW(s2
);
755 if (strncmpiW(s1
, s2
+s2_len
-s1_len
, s1_len
) || (s2_len
>s1_len
&& s2
[s2_len
-s1_len
-1]!='.'))
757 SetLastError(ERROR_INVALID_PARAMETER
);
764 BOOL
set_cookie(LPCWSTR domain
, LPCWSTR path
, LPCWSTR cookie_name
, LPCWSTR cookie_data
)
766 cookie_domain
*thisCookieDomain
= NULL
;
771 FILETIME expiry
, create
;
772 BOOL expired
= FALSE
, update_persistent
= FALSE
;
775 value
= data
= heap_strdupW(cookie_data
);
778 ERR("could not allocate the cookie data buffer\n");
782 memset(&expiry
,0,sizeof(expiry
));
783 GetSystemTimeAsFileTime(&create
);
785 /* lots of information can be parsed out of the cookie value */
790 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
791 static const WCHAR szPath
[] = {'p','a','t','h','=',0};
792 static const WCHAR szExpires
[] = {'e','x','p','i','r','e','s','=',0};
793 static const WCHAR szSecure
[] = {'s','e','c','u','r','e',0};
794 static const WCHAR szHttpOnly
[] = {'h','t','t','p','o','n','l','y',0};
796 if (!(ptr
= strchrW(ptr
,';'))) break;
799 if (value
!= data
) heap_free(value
);
800 value
= heap_alloc((ptr
- data
) * sizeof(WCHAR
));
804 ERR("could not allocate the cookie value buffer\n");
807 strcpyW(value
, data
);
809 while (*ptr
== ' ') ptr
++; /* whitespace */
811 if (strncmpiW(ptr
, szDomain
, 7) == 0)
815 ptr
+= sizeof(szDomain
)/sizeof(szDomain
[0])-1;
818 end_ptr
= strchrW(ptr
, ';');
822 if(!IsDomainLegalCookieDomainW(ptr
, domain
))
834 TRACE("Parsing new domain %s\n",debugstr_w(domain
));
836 else if (strncmpiW(ptr
, szPath
, 5) == 0)
838 ptr
+=strlenW(szPath
);
840 TRACE("Parsing new path %s\n",debugstr_w(path
));
842 else if (strncmpiW(ptr
, szExpires
, 8) == 0)
845 ptr
+=strlenW(szExpires
);
846 if (InternetTimeToSystemTimeW(ptr
, &st
, 0))
848 SystemTimeToFileTime(&st
, &expiry
);
850 if (CompareFileTime(&create
,&expiry
) > 0)
852 TRACE("Cookie already expired.\n");
857 else if (strncmpiW(ptr
, szSecure
, 6) == 0)
859 FIXME("secure not handled (%s)\n",debugstr_w(ptr
));
860 ptr
+= strlenW(szSecure
);
862 else if (strncmpiW(ptr
, szHttpOnly
, 8) == 0)
864 FIXME("httponly not handled (%s)\n",debugstr_w(ptr
));
865 ptr
+= strlenW(szHttpOnly
);
869 FIXME("Unknown additional option %s\n",debugstr_w(ptr
));
874 EnterCriticalSection(&cookie_cs
);
876 load_persistent_cookie(domain
, path
);
878 LIST_FOR_EACH(cursor
, &domain_list
)
880 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
881 if (COOKIE_matchDomain(domain
, path
, thisCookieDomain
, FALSE
))
883 thisCookieDomain
= NULL
;
886 if (!thisCookieDomain
)
889 thisCookieDomain
= COOKIE_addDomain(domain
, path
);
893 if (value
!= data
) heap_free(value
);
894 LeaveCriticalSection(&cookie_cs
);
899 if(!expiry
.dwLowDateTime
&& !expiry
.dwHighDateTime
)
900 flags
|= INTERNET_COOKIE_IS_SESSION
;
902 update_persistent
= TRUE
;
904 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, cookie_name
)))
906 if (!(thisCookie
->flags
& INTERNET_COOKIE_IS_SESSION
))
907 update_persistent
= TRUE
;
908 COOKIE_deleteCookie(thisCookie
, FALSE
);
911 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name
),
912 debugstr_w(value
), debugstr_w(thisCookieDomain
->lpCookieDomain
),debugstr_w(thisCookieDomain
->lpCookiePath
));
914 if (!expired
&& !COOKIE_addCookie(thisCookieDomain
, cookie_name
, value
, expiry
, create
, flags
))
917 if (value
!= data
) heap_free(value
);
918 LeaveCriticalSection(&cookie_cs
);
922 if (value
!= data
) heap_free(value
);
924 if (!update_persistent
|| save_persistent_cookie(thisCookieDomain
))
926 LeaveCriticalSection(&cookie_cs
);
929 LeaveCriticalSection(&cookie_cs
);
933 /***********************************************************************
934 * InternetSetCookieW (WININET.@)
936 * Sets cookie for the specified url
943 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
944 LPCWSTR lpCookieData
)
947 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
949 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
950 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
952 if (!lpszUrl
|| !lpCookieData
)
954 SetLastError(ERROR_INVALID_PARAMETER
);
959 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
960 if (!ret
|| !hostName
[0]) return FALSE
;
964 WCHAR
*cookie
, *data
;
966 cookie
= heap_strdupW(lpCookieData
);
969 SetLastError(ERROR_OUTOFMEMORY
);
973 /* some apps (or is it us??) try to add a cookie with no cookie name, but
974 * the cookie data in the form of name[=data].
976 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ strlenW(cookie
);
979 ret
= set_cookie(hostName
, path
, cookie
, data
);
984 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
);
988 /***********************************************************************
989 * InternetSetCookieA (WININET.@)
991 * Sets cookie for the specified url
998 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
1001 LPWSTR data
, url
, name
;
1004 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
1005 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
1007 url
= heap_strdupAtoW(lpszUrl
);
1008 name
= heap_strdupAtoW(lpszCookieName
);
1009 data
= heap_strdupAtoW(lpCookieData
);
1011 r
= InternetSetCookieW( url
, name
, data
);
1019 /***********************************************************************
1020 * InternetSetCookieExA (WININET.@)
1022 * See InternetSetCookieExW.
1024 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
1025 DWORD dwFlags
, DWORD_PTR dwReserved
)
1027 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1028 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
1029 dwFlags
, dwReserved
);
1031 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1032 return InternetSetCookieA(lpszURL
, lpszCookieName
, lpszCookieData
);
1035 /***********************************************************************
1036 * InternetSetCookieExW (WININET.@)
1038 * Sets a cookie for the specified URL.
1045 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
1046 DWORD dwFlags
, DWORD_PTR dwReserved
)
1048 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1049 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
1050 dwFlags
, dwReserved
);
1052 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1053 return InternetSetCookieW(lpszURL
, lpszCookieName
, lpszCookieData
);
1056 /***********************************************************************
1057 * InternetGetCookieExA (WININET.@)
1059 * See InternetGetCookieExW.
1061 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
1062 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
1064 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1065 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
1066 pcchCookieData
, dwFlags
, lpReserved
);
1068 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1069 return InternetGetCookieA(pchURL
, pchCookieName
, pchCookieData
, pcchCookieData
);
1072 /***********************************************************************
1073 * InternetGetCookieExW (WININET.@)
1075 * Retrieve cookie for the specified URL.
1082 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
1083 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
1085 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1086 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
1087 pcchCookieData
, dwFlags
, lpReserved
);
1089 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1090 return InternetGetCookieW(pchURL
, pchCookieName
, pchCookieData
, pcchCookieData
);
1093 /***********************************************************************
1094 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1096 * Clears all per-site decisions about cookies.
1103 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
1109 /***********************************************************************
1110 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1112 * See InternetEnumPerSiteCookieDecisionW.
1114 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1115 ULONG
*pdwDecision
, ULONG dwIndex
)
1117 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1118 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1122 /***********************************************************************
1123 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1125 * Enumerates all per-site decisions about cookies.
1132 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1133 ULONG
*pdwDecision
, ULONG dwIndex
)
1135 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1136 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1140 /***********************************************************************
1141 * InternetGetPerSiteCookieDecisionA (WININET.@)
1143 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, ULONG
*pResult
)
1145 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
1149 /***********************************************************************
1150 * InternetGetPerSiteCookieDecisionW (WININET.@)
1152 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, ULONG
*pResult
)
1154 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
1158 /***********************************************************************
1159 * InternetSetPerSiteCookieDecisionA (WININET.@)
1161 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
1163 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
1167 /***********************************************************************
1168 * InternetSetPerSiteCookieDecisionW (WININET.@)
1170 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
1172 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);
1176 void free_cookie(void)
1178 DeleteCriticalSection(&cookie_cs
);