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)
43 #include "wine/debug.h"
46 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
49 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
52 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
55 typedef struct _cookie_domain cookie_domain
;
56 typedef struct _cookie cookie
;
62 struct _cookie_domain
*parent
;
75 LPWSTR lpCookieDomain
;
77 struct list cookie_list
;
80 static CRITICAL_SECTION cookie_cs
;
81 static CRITICAL_SECTION_DEBUG cookie_cs_debug
=
84 { &cookie_cs_debug
.ProcessLocksList
, &cookie_cs_debug
.ProcessLocksList
},
85 0, 0, { (DWORD_PTR
)(__FILE__
": cookie_cs") }
87 static CRITICAL_SECTION cookie_cs
= { &cookie_cs_debug
, -1, 0, 0, 0, 0 };
88 static struct list domain_list
= LIST_INIT(domain_list
);
90 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
91 FILETIME expiry
, FILETIME create
, DWORD flags
);
92 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
);
93 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
);
94 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
);
95 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
);
96 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
97 cookie_domain
*searchDomain
, BOOL allow_partial
);
99 static BOOL
create_cookie_url(LPCWSTR domain
, LPCWSTR path
, WCHAR
*buf
, DWORD buf_len
)
101 static const WCHAR cookie_prefix
[] = {'C','o','o','k','i','e',':'};
106 if(buf_len
< sizeof(cookie_prefix
)/sizeof(WCHAR
))
108 memcpy(buf
, cookie_prefix
, sizeof(cookie_prefix
));
109 buf
+= sizeof(cookie_prefix
)/sizeof(WCHAR
);
110 buf_len
-= sizeof(cookie_prefix
)/sizeof(WCHAR
);
114 if(!GetUserNameW(buf
, &len
))
124 len
= strlenW(domain
);
127 memcpy(buf
, domain
, len
*sizeof(WCHAR
));
134 memcpy(buf
, path
, len
*sizeof(WCHAR
));
144 static BOOL
load_persistent_cookie(LPCWSTR domain
, LPCWSTR path
)
146 INTERNET_CACHE_ENTRY_INFOW
*info
;
147 cookie_domain
*domain_container
= NULL
;
150 WCHAR cookie_url
[MAX_PATH
];
152 char *str
= NULL
, *pbeg
, *pend
;
155 FILETIME expiry
, create
, time
;
157 if (!create_cookie_url(domain
, path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
161 RetrieveUrlCacheEntryStreamW(cookie_url
, NULL
, &size
, FALSE
, 0);
162 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
164 info
= heap_alloc(size
);
167 cookie
= RetrieveUrlCacheEntryStreamW(cookie_url
, info
, &size
, FALSE
, 0);
168 size
= info
->dwSizeLow
;
173 if(!(str
= heap_alloc(size
)) || !ReadUrlCacheEntryStream(cookie
, 0, str
, &size
, 0)) {
174 UnlockUrlCacheEntryStream(cookie
, 0);
178 UnlockUrlCacheEntryStream(cookie
, 0);
180 LIST_FOR_EACH(iter
, &domain_list
)
182 domain_container
= LIST_ENTRY(iter
, cookie_domain
, entry
);
183 if(COOKIE_matchDomain(domain
, path
, domain_container
, FALSE
))
185 domain_container
= NULL
;
187 if(!domain_container
)
188 domain_container
= COOKIE_addDomain(domain
, path
);
189 if(!domain_container
) {
194 GetSystemTimeAsFileTime(&time
);
195 for(pbeg
=str
; pbeg
&& *pbeg
; name
=data
=NULL
) {
196 pend
= strchr(pbeg
, '\n');
200 name
= heap_strdupAtoW(pbeg
);
203 pend
= strchr(pbeg
, '\n');
207 data
= heap_strdupAtoW(pbeg
);
210 pbeg
= strchr(pend
+1, '\n');
213 sscanf(pbeg
, "%u %u %u %u %u", &flags
, &expiry
.dwLowDateTime
, &expiry
.dwHighDateTime
,
214 &create
.dwLowDateTime
, &create
.dwHighDateTime
);
217 pbeg
= strchr(pbeg
, '*');
227 if(CompareFileTime(&time
, &expiry
) <= 0) {
228 if((old_cookie
= COOKIE_findCookie(domain_container
, name
)))
229 COOKIE_deleteCookie(old_cookie
, FALSE
);
230 COOKIE_addCookie(domain_container
, name
, data
, expiry
, create
, flags
);
241 static BOOL
save_persistent_cookie(cookie_domain
*domain
)
243 static const WCHAR txtW
[] = {'t','x','t',0};
245 WCHAR cookie_url
[MAX_PATH
], cookie_file
[MAX_PATH
];
246 HANDLE cookie_handle
;
247 cookie
*cookie_container
= NULL
, *cookie_iter
;
248 BOOL do_save
= FALSE
;
249 char buf
[64], *dyn_buf
;
252 if (!create_cookie_url(domain
->lpCookieDomain
, domain
->lpCookiePath
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
255 /* check if there's anything to save */
256 GetSystemTimeAsFileTime(&time
);
257 LIST_FOR_EACH_ENTRY_SAFE(cookie_container
, cookie_iter
, &domain
->cookie_list
, cookie
, entry
)
259 if((cookie_container
->expiry
.dwLowDateTime
|| cookie_container
->expiry
.dwHighDateTime
)
260 && CompareFileTime(&time
, &cookie_container
->expiry
) > 0) {
261 COOKIE_deleteCookie(cookie_container
, FALSE
);
265 if(!(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)) {
271 DeleteUrlCacheEntryW(cookie_url
);
275 if(!CreateUrlCacheEntryW(cookie_url
, 0, txtW
, cookie_file
, 0))
277 cookie_handle
= CreateFileW(cookie_file
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
278 if(cookie_handle
== INVALID_HANDLE_VALUE
) {
279 DeleteFileW(cookie_file
);
283 LIST_FOR_EACH_ENTRY(cookie_container
, &domain
->cookie_list
, cookie
, entry
)
285 if(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)
288 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieName
);
289 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
295 if(!WriteFile(cookie_handle
, "\n", 1, NULL
, NULL
)) {
300 dyn_buf
= heap_strdupWtoA(cookie_container
->lpCookieData
);
301 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
307 if(!WriteFile(cookie_handle
, "\n", 1, NULL
, NULL
)) {
312 dyn_buf
= heap_strdupWtoA(domain
->lpCookieDomain
);
313 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
320 dyn_buf
= heap_strdupWtoA(domain
->lpCookiePath
);
321 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), NULL
, NULL
)) {
328 sprintf(buf
, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container
->flags
,
329 cookie_container
->expiry
.dwLowDateTime
, cookie_container
->expiry
.dwHighDateTime
,
330 cookie_container
->create
.dwLowDateTime
, cookie_container
->create
.dwHighDateTime
);
331 if(!WriteFile(cookie_handle
, buf
, strlen(buf
), NULL
, NULL
)) {
337 CloseHandle(cookie_handle
);
339 ERR("error saving cookie file\n");
340 DeleteFileW(cookie_file
);
344 memset(&time
, 0, sizeof(time
));
345 return CommitUrlCacheEntryW(cookie_url
, cookie_file
, time
, time
, 0, NULL
, 0, txtW
, 0);
348 /* adds a cookie to the domain */
349 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
,
350 FILETIME expiry
, FILETIME create
, DWORD flags
)
352 cookie
*newCookie
= heap_alloc(sizeof(cookie
));
356 newCookie
->lpCookieName
= heap_strdupW(name
);
357 newCookie
->lpCookieData
= heap_strdupW(data
);
359 if (!newCookie
->lpCookieName
|| !newCookie
->lpCookieData
)
361 heap_free(newCookie
->lpCookieName
);
362 heap_free(newCookie
->lpCookieData
);
363 heap_free(newCookie
);
368 newCookie
->flags
= flags
;
369 newCookie
->expiry
= expiry
;
370 newCookie
->create
= create
;
372 TRACE("added cookie %p (data is %s)\n", newCookie
, debugstr_w(data
) );
374 list_add_tail(&domain
->cookie_list
, &newCookie
->entry
);
375 newCookie
->parent
= domain
;
380 /* finds a cookie in the domain matching the cookie name */
381 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
)
383 struct list
* cursor
;
384 TRACE("(%p, %s)\n", domain
, debugstr_w(lpszCookieName
));
386 LIST_FOR_EACH(cursor
, &domain
->cookie_list
)
388 cookie
*searchCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
389 BOOL candidate
= TRUE
;
390 if (candidate
&& lpszCookieName
)
392 if (candidate
&& !searchCookie
->lpCookieName
)
394 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
403 /* removes a cookie from the list, if its the last cookie we also remove the domain */
404 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
)
406 heap_free(deadCookie
->lpCookieName
);
407 heap_free(deadCookie
->lpCookieData
);
408 list_remove(&deadCookie
->entry
);
410 /* special case: last cookie, lets remove the domain to save memory */
411 if (list_empty(&deadCookie
->parent
->cookie_list
) && deleteDomain
)
412 COOKIE_deleteDomain(deadCookie
->parent
);
413 heap_free(deadCookie
);
416 /* allocates a domain and adds it to the end */
417 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
)
419 cookie_domain
*newDomain
= heap_alloc(sizeof(cookie_domain
));
421 list_init(&newDomain
->entry
);
422 list_init(&newDomain
->cookie_list
);
423 newDomain
->lpCookieDomain
= heap_strdupW(domain
);
424 newDomain
->lpCookiePath
= heap_strdupW(path
);
426 list_add_tail(&domain_list
, &newDomain
->entry
);
428 TRACE("Adding domain: %p\n", newDomain
);
432 static BOOL
COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
434 URL_COMPONENTSW UrlComponents
;
436 UrlComponents
.lpszExtraInfo
= NULL
;
437 UrlComponents
.lpszPassword
= NULL
;
438 UrlComponents
.lpszScheme
= NULL
;
439 UrlComponents
.lpszUrlPath
= path
;
440 UrlComponents
.lpszUserName
= NULL
;
441 UrlComponents
.lpszHostName
= hostName
;
442 UrlComponents
.dwExtraInfoLength
= 0;
443 UrlComponents
.dwPasswordLength
= 0;
444 UrlComponents
.dwSchemeLength
= 0;
445 UrlComponents
.dwUserNameLength
= 0;
446 UrlComponents
.dwHostNameLength
= hostNameLen
;
447 UrlComponents
.dwUrlPathLength
= pathLen
;
449 if (!InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
)) return FALSE
;
451 /* discard the webpage off the end of the path */
452 if (UrlComponents
.dwUrlPathLength
)
454 if (path
[UrlComponents
.dwUrlPathLength
- 1] != '/')
457 if ((ptr
= strrchrW(path
, '/'))) *(++ptr
) = 0;
465 else if (pathLen
>= 2)
473 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
474 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
475 cookie_domain
*searchDomain
, BOOL allow_partial
)
477 TRACE("searching on domain %p\n", searchDomain
);
478 if (lpszCookieDomain
)
480 if (!searchDomain
->lpCookieDomain
)
483 TRACE("comparing domain %s with %s\n",
484 debugstr_w(lpszCookieDomain
),
485 debugstr_w(searchDomain
->lpCookieDomain
));
487 if (allow_partial
&& !strstrW(lpszCookieDomain
, searchDomain
->lpCookieDomain
))
489 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
495 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
496 /* paths match at the beginning. so a path of /foo would match
497 * /foobar and /foo/bar
499 if (!searchDomain
->lpCookiePath
)
503 len
= lstrlenW(searchDomain
->lpCookiePath
);
504 if (strncmpiW(searchDomain
->lpCookiePath
, lpszCookiePath
, len
)!=0)
507 else if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
514 /* remove a domain from the list and delete it */
515 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
)
517 struct list
* cursor
;
518 while ((cursor
= list_tail(&deadDomain
->cookie_list
)))
520 COOKIE_deleteCookie(LIST_ENTRY(cursor
, cookie
, entry
), FALSE
);
523 heap_free(deadDomain
->lpCookieDomain
);
524 heap_free(deadDomain
->lpCookiePath
);
526 list_remove(&deadDomain
->entry
);
528 heap_free(deadDomain
);
531 BOOL
get_cookie(const WCHAR
*host
, const WCHAR
*path
, WCHAR
*cookie_data
, DWORD
*size
)
533 unsigned cnt
= 0, len
, domain_count
= 0, cookie_count
= 0;
534 cookie_domain
*domain
;
537 GetSystemTimeAsFileTime(&tm
);
539 EnterCriticalSection(&cookie_cs
);
541 load_persistent_cookie(host
, path
);
543 LIST_FOR_EACH_ENTRY(domain
, &domain_list
, cookie_domain
, entry
) {
544 struct list
*cursor
, *cursor2
;
546 if(!COOKIE_matchDomain(host
, path
, domain
, TRUE
))
550 TRACE("found domain %p\n", domain
);
552 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &domain
->cookie_list
) {
553 cookie
*cookie_iter
= LIST_ENTRY(cursor
, cookie
, entry
);
555 /* check for expiry */
556 if((cookie_iter
->expiry
.dwLowDateTime
!= 0 || cookie_iter
->expiry
.dwHighDateTime
!= 0)
557 && CompareFileTime(&tm
, &cookie_iter
->expiry
) > 0)
559 TRACE("Found expired cookie. deleting\n");
560 COOKIE_deleteCookie(cookie_iter
, FALSE
);
564 if(!cookie_data
) { /* return the size of the buffer required to lpdwSize */
567 cnt
+= strlenW(cookie_iter
->lpCookieName
);
568 if ((len
= strlenW(cookie_iter
->lpCookieData
))) {
573 static const WCHAR szsc
[] = { ';',' ',0 };
574 static const WCHAR szname
[] = { '%','s',0 };
575 static const WCHAR szdata
[] = { '=','%','s',0 };
577 if (cookie_count
) cnt
+= snprintfW(cookie_data
+ cnt
, *size
- cnt
, szsc
);
578 cnt
+= snprintfW(cookie_data
+ cnt
, *size
- cnt
, szname
, cookie_iter
->lpCookieName
);
580 if (cookie_iter
->lpCookieData
[0])
581 cnt
+= snprintfW(cookie_data
+ cnt
, *size
- cnt
, szdata
, cookie_iter
->lpCookieData
);
583 TRACE("Cookie: %s\n", debugstr_w(cookie_data
));
589 LeaveCriticalSection(&cookie_cs
);
592 TRACE("no cookies found for %s\n", debugstr_w(host
));
593 SetLastError(ERROR_NO_MORE_ITEMS
);
598 *size
= (cnt
+ 1) * sizeof(WCHAR
);
599 TRACE("returning %u\n", *size
);
605 TRACE("Returning %u (from %u domains): %s\n", cnt
, domain_count
, debugstr_w(cookie_data
));
609 /***********************************************************************
610 * InternetGetCookieW (WININET.@)
612 * Retrieve cookie from the specified url
614 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
615 * So it won't be implemented here.
622 BOOL WINAPI
InternetGetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
623 LPWSTR lpCookieData
, LPDWORD lpdwSize
)
625 WCHAR host
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
628 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
), lpCookieData
, lpdwSize
);
632 SetLastError(ERROR_INVALID_PARAMETER
);
637 ret
= COOKIE_crackUrlSimple(lpszUrl
, host
, sizeof(host
)/sizeof(host
[0]), path
, sizeof(path
)/sizeof(path
[0]));
638 if (!ret
|| !host
[0]) {
639 SetLastError(ERROR_INVALID_PARAMETER
);
643 return get_cookie(host
, path
, lpCookieData
, lpdwSize
);
647 /***********************************************************************
648 * InternetGetCookieA (WININET.@)
650 * Retrieve cookie from the specified url
657 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
658 LPSTR lpCookieData
, LPDWORD lpdwSize
)
661 LPWSTR szCookieData
= NULL
, url
, name
;
664 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
667 url
= heap_strdupAtoW(lpszUrl
);
668 name
= heap_strdupAtoW(lpszCookieName
);
670 r
= InternetGetCookieW( url
, name
, NULL
, &len
);
673 szCookieData
= heap_alloc(len
* sizeof(WCHAR
));
680 r
= InternetGetCookieW( url
, name
, szCookieData
, &len
);
682 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
683 lpCookieData
, *lpdwSize
, NULL
, NULL
);
686 heap_free( szCookieData
);
693 /***********************************************************************
694 * IsDomainLegalCookieDomainW (WININET.@)
696 BOOL WINAPI
IsDomainLegalCookieDomainW( LPCWSTR s1
, LPCWSTR s2
)
698 DWORD s1_len
, s2_len
;
700 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1
), debugstr_w(s2
));
704 SetLastError(ERROR_INVALID_PARAMETER
);
707 if (s1
[0] == '.' || !s1
[0] || s2
[0] == '.' || !s2
[0])
709 SetLastError(ERROR_INVALID_NAME
);
712 if(!strchrW(s1
, '.') || !strchrW(s2
, '.'))
715 s1_len
= strlenW(s1
);
716 s2_len
= strlenW(s2
);
720 if (strncmpiW(s1
, s2
+s2_len
-s1_len
, s1_len
) || (s2_len
>s1_len
&& s2
[s2_len
-s1_len
-1]!='.'))
722 SetLastError(ERROR_INVALID_PARAMETER
);
729 BOOL
set_cookie(LPCWSTR domain
, LPCWSTR path
, LPCWSTR cookie_name
, LPCWSTR cookie_data
)
731 cookie_domain
*thisCookieDomain
= NULL
;
736 FILETIME expiry
, create
;
737 BOOL expired
= FALSE
, update_persistent
= FALSE
;
740 value
= data
= heap_strdupW(cookie_data
);
743 ERR("could not allocate the cookie data buffer\n");
747 memset(&expiry
,0,sizeof(expiry
));
748 GetSystemTimeAsFileTime(&create
);
750 /* lots of information can be parsed out of the cookie value */
755 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
756 static const WCHAR szPath
[] = {'p','a','t','h','=',0};
757 static const WCHAR szExpires
[] = {'e','x','p','i','r','e','s','=',0};
758 static const WCHAR szSecure
[] = {'s','e','c','u','r','e',0};
759 static const WCHAR szHttpOnly
[] = {'h','t','t','p','o','n','l','y',0};
761 if (!(ptr
= strchrW(ptr
,';'))) break;
764 if (value
!= data
) heap_free(value
);
765 value
= heap_alloc((ptr
- data
) * sizeof(WCHAR
));
769 ERR("could not allocate the cookie value buffer\n");
772 strcpyW(value
, data
);
774 while (*ptr
== ' ') ptr
++; /* whitespace */
776 if (strncmpiW(ptr
, szDomain
, 7) == 0)
780 ptr
+= sizeof(szDomain
)/sizeof(szDomain
[0])-1;
783 end_ptr
= strchrW(ptr
, ';');
787 if(!IsDomainLegalCookieDomainW(ptr
, domain
))
799 TRACE("Parsing new domain %s\n",debugstr_w(domain
));
801 else if (strncmpiW(ptr
, szPath
, 5) == 0)
803 ptr
+=strlenW(szPath
);
805 TRACE("Parsing new path %s\n",debugstr_w(path
));
807 else if (strncmpiW(ptr
, szExpires
, 8) == 0)
810 ptr
+=strlenW(szExpires
);
811 if (InternetTimeToSystemTimeW(ptr
, &st
, 0))
813 SystemTimeToFileTime(&st
, &expiry
);
815 if (CompareFileTime(&create
,&expiry
) > 0)
817 TRACE("Cookie already expired.\n");
822 else if (strncmpiW(ptr
, szSecure
, 6) == 0)
824 FIXME("secure not handled (%s)\n",debugstr_w(ptr
));
825 ptr
+= strlenW(szSecure
);
827 else if (strncmpiW(ptr
, szHttpOnly
, 8) == 0)
829 FIXME("httponly not handled (%s)\n",debugstr_w(ptr
));
830 ptr
+= strlenW(szHttpOnly
);
834 FIXME("Unknown additional option %s\n",debugstr_w(ptr
));
839 EnterCriticalSection(&cookie_cs
);
841 load_persistent_cookie(domain
, path
);
843 LIST_FOR_EACH(cursor
, &domain_list
)
845 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
846 if (COOKIE_matchDomain(domain
, path
, thisCookieDomain
, FALSE
))
848 thisCookieDomain
= NULL
;
851 if (!thisCookieDomain
)
854 thisCookieDomain
= COOKIE_addDomain(domain
, path
);
858 if (value
!= data
) heap_free(value
);
859 LeaveCriticalSection(&cookie_cs
);
864 if(!expiry
.dwLowDateTime
&& !expiry
.dwHighDateTime
)
865 flags
|= INTERNET_COOKIE_IS_SESSION
;
867 update_persistent
= TRUE
;
869 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, cookie_name
)))
871 if (!(thisCookie
->flags
& INTERNET_COOKIE_IS_SESSION
))
872 update_persistent
= TRUE
;
873 COOKIE_deleteCookie(thisCookie
, FALSE
);
876 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name
),
877 debugstr_w(value
), debugstr_w(thisCookieDomain
->lpCookieDomain
),debugstr_w(thisCookieDomain
->lpCookiePath
));
879 if (!expired
&& !COOKIE_addCookie(thisCookieDomain
, cookie_name
, value
, expiry
, create
, flags
))
882 if (value
!= data
) heap_free(value
);
883 LeaveCriticalSection(&cookie_cs
);
887 if (value
!= data
) heap_free(value
);
889 if (!update_persistent
|| save_persistent_cookie(thisCookieDomain
))
891 LeaveCriticalSection(&cookie_cs
);
894 LeaveCriticalSection(&cookie_cs
);
898 /***********************************************************************
899 * InternetSetCookieW (WININET.@)
901 * Sets cookie for the specified url
908 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
909 LPCWSTR lpCookieData
)
912 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
914 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
915 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
917 if (!lpszUrl
|| !lpCookieData
)
919 SetLastError(ERROR_INVALID_PARAMETER
);
924 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
925 if (!ret
|| !hostName
[0]) return FALSE
;
929 WCHAR
*cookie
, *data
;
931 cookie
= heap_strdupW(lpCookieData
);
934 SetLastError(ERROR_OUTOFMEMORY
);
938 /* some apps (or is it us??) try to add a cookie with no cookie name, but
939 * the cookie data in the form of name[=data].
941 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ strlenW(cookie
);
944 ret
= set_cookie(hostName
, path
, cookie
, data
);
949 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
);
953 /***********************************************************************
954 * InternetSetCookieA (WININET.@)
956 * Sets cookie for the specified url
963 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
966 LPWSTR data
, url
, name
;
969 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
970 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
972 url
= heap_strdupAtoW(lpszUrl
);
973 name
= heap_strdupAtoW(lpszCookieName
);
974 data
= heap_strdupAtoW(lpCookieData
);
976 r
= InternetSetCookieW( url
, name
, data
);
984 /***********************************************************************
985 * InternetSetCookieExA (WININET.@)
987 * See InternetSetCookieExW.
989 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
990 DWORD dwFlags
, DWORD_PTR dwReserved
)
992 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
993 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
994 dwFlags
, dwReserved
);
996 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
997 return InternetSetCookieA(lpszURL
, lpszCookieName
, lpszCookieData
);
1000 /***********************************************************************
1001 * InternetSetCookieExW (WININET.@)
1003 * Sets a cookie for the specified URL.
1010 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
1011 DWORD dwFlags
, DWORD_PTR dwReserved
)
1013 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1014 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
1015 dwFlags
, dwReserved
);
1017 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1018 return InternetSetCookieW(lpszURL
, lpszCookieName
, lpszCookieData
);
1021 /***********************************************************************
1022 * InternetGetCookieExA (WININET.@)
1024 * See InternetGetCookieExW.
1026 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
1027 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
1029 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1030 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
1031 pcchCookieData
, dwFlags
, lpReserved
);
1033 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1034 return InternetGetCookieA(pchURL
, pchCookieName
, pchCookieData
, pcchCookieData
);
1037 /***********************************************************************
1038 * InternetGetCookieExW (WININET.@)
1040 * Retrieve cookie for the specified URL.
1047 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
1048 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
1050 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1051 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
1052 pcchCookieData
, dwFlags
, lpReserved
);
1054 if (dwFlags
) FIXME("flags 0x%08x not supported\n", dwFlags
);
1055 return InternetGetCookieW(pchURL
, pchCookieName
, pchCookieData
, pcchCookieData
);
1058 /***********************************************************************
1059 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1061 * Clears all per-site decisions about cookies.
1068 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
1074 /***********************************************************************
1075 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1077 * See InternetEnumPerSiteCookieDecisionW.
1079 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1080 ULONG
*pdwDecision
, ULONG dwIndex
)
1082 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1083 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1087 /***********************************************************************
1088 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1090 * Enumerates all per-site decisions about cookies.
1097 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1098 ULONG
*pdwDecision
, ULONG dwIndex
)
1100 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1101 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1105 /***********************************************************************
1106 * InternetGetPerSiteCookieDecisionA (WININET.@)
1108 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, ULONG
*pResult
)
1110 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
1114 /***********************************************************************
1115 * InternetGetPerSiteCookieDecisionW (WININET.@)
1117 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, ULONG
*pResult
)
1119 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
1123 /***********************************************************************
1124 * InternetSetPerSiteCookieDecisionA (WININET.@)
1126 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
1128 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
1132 /***********************************************************************
1133 * InternetSetPerSiteCookieDecisionW (WININET.@)
1135 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
1137 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);
1141 void free_cookie(void)
1143 DeleteCriticalSection(&cookie_cs
);