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 struct _cookie_domain_t
;
32 struct _cookie_container_t
;
34 typedef struct _cookie_t
{
37 struct _cookie_container_t
*container
;
46 typedef struct _cookie_container_t
{
50 struct _cookie_domain_t
*domain
;
52 struct list cookie_list
;
55 typedef struct _cookie_domain_t
{
59 unsigned subdomain_len
;
61 struct _cookie_domain_t
*parent
;
62 struct list subdomain_list
;
64 /* List of stored paths sorted by length of the path. */
65 struct list path_list
;
68 static CRITICAL_SECTION cookie_cs
;
69 static CRITICAL_SECTION_DEBUG cookie_cs_debug
=
72 { &cookie_cs_debug
.ProcessLocksList
, &cookie_cs_debug
.ProcessLocksList
},
73 0, 0, { (DWORD_PTR
)(__FILE__
": cookie_cs") }
75 static CRITICAL_SECTION cookie_cs
= { &cookie_cs_debug
, -1, 0, 0, 0, 0 };
76 static struct list domain_list
= LIST_INIT(domain_list
);
78 static cookie_domain_t
*get_cookie_domain(const WCHAR
*domain
, BOOL create
)
80 const WCHAR
*ptr
= domain
+ strlenW(domain
), *ptr_end
, *subdomain_ptr
;
81 cookie_domain_t
*iter
, *current_domain
, *prev_domain
= NULL
;
82 struct list
*current_list
= &domain_list
;
85 for(ptr_end
= ptr
--; ptr
> domain
&& *ptr
!= '.'; ptr
--);
86 subdomain_ptr
= *ptr
== '.' ? ptr
+1 : ptr
;
88 current_domain
= NULL
;
89 LIST_FOR_EACH_ENTRY(iter
, current_list
, cookie_domain_t
, entry
) {
90 if(ptr_end
-subdomain_ptr
== iter
->subdomain_len
&& !memcmp(subdomain_ptr
, iter
->domain
, iter
->subdomain_len
)) {
91 current_domain
= iter
;
100 current_domain
= heap_alloc(sizeof(*current_domain
));
104 current_domain
->domain
= heap_strdupW(subdomain_ptr
);
105 if(!current_domain
->domain
) {
106 heap_free(current_domain
);
110 current_domain
->subdomain_len
= ptr_end
-subdomain_ptr
;
112 current_domain
->parent
= prev_domain
;
113 list_init(¤t_domain
->path_list
);
114 list_init(¤t_domain
->subdomain_list
);
116 list_add_tail(current_list
, ¤t_domain
->entry
);
120 return current_domain
;
122 prev_domain
= current_domain
;
123 current_list
= ¤t_domain
->subdomain_list
;
127 static cookie_container_t
*get_cookie_container(const WCHAR
*domain
, const WCHAR
*path
, BOOL create
)
129 cookie_domain_t
*cookie_domain
;
130 cookie_container_t
*cookie_container
, *iter
;
131 size_t path_len
, len
;
133 cookie_domain
= get_cookie_domain(domain
, create
);
137 path_len
= strlenW(path
);
139 LIST_FOR_EACH_ENTRY(cookie_container
, &cookie_domain
->path_list
, cookie_container_t
, entry
) {
140 len
= strlenW(cookie_container
->path
);
144 if(!strcmpiW(cookie_container
->path
, path
))
145 return cookie_container
;
151 cookie_container
= heap_alloc(sizeof(*cookie_container
));
152 if(!cookie_container
)
155 cookie_container
->path
= heap_strdupW(path
);
156 if(!cookie_container
->path
) {
157 heap_free(cookie_container
);
161 cookie_container
->domain
= cookie_domain
;
162 list_init(&cookie_container
->cookie_list
);
165 LIST_FOR_EACH_ENTRY(iter
, &cookie_domain
->path_list
, cookie_container_t
, entry
) {
166 if(strlenW(iter
->path
) <= path_len
) {
167 list_add_before(&iter
->entry
, &cookie_container
->entry
);
168 return cookie_container
;
172 list_add_tail(&cookie_domain
->path_list
, &cookie_container
->entry
);
173 return cookie_container
;
176 static void delete_cookie(cookie_t
*cookie
)
178 list_remove(&cookie
->entry
);
180 heap_free(cookie
->name
);
181 heap_free(cookie
->data
);
185 static cookie_t
*alloc_cookie(const WCHAR
*name
, const WCHAR
*data
, FILETIME expiry
, FILETIME create_time
, DWORD flags
)
187 cookie_t
*new_cookie
;
189 new_cookie
= heap_alloc(sizeof(*new_cookie
));
193 new_cookie
->expiry
= expiry
;
194 new_cookie
->create
= create_time
;
195 new_cookie
->flags
= flags
;
196 list_init(&new_cookie
->entry
);
198 new_cookie
->name
= heap_strdupW(name
);
199 new_cookie
->data
= heap_strdupW(data
);
200 if((name
&& !new_cookie
->name
) || (data
&& !new_cookie
->data
)) {
201 delete_cookie(new_cookie
);
208 static cookie_t
*find_cookie(cookie_container_t
*container
, const WCHAR
*name
)
212 LIST_FOR_EACH_ENTRY(iter
, &container
->cookie_list
, cookie_t
, entry
) {
213 if(!strcmpiW(iter
->name
, name
))
220 static void add_cookie(cookie_container_t
*container
, cookie_t
*new_cookie
)
222 TRACE("Adding %s=%s to %s %s\n", debugstr_w(new_cookie
->name
), debugstr_w(new_cookie
->data
),
223 debugstr_w(container
->domain
->domain
), debugstr_w(container
->path
));
225 list_add_tail(&container
->cookie_list
, &new_cookie
->entry
);
226 new_cookie
->container
= container
;
229 static void replace_cookie(cookie_container_t
*container
, cookie_t
*new_cookie
)
231 cookie_t
*old_cookie
;
233 old_cookie
= find_cookie(container
, new_cookie
->name
);
235 delete_cookie(old_cookie
);
237 add_cookie(container
, new_cookie
);
240 static BOOL
cookie_match_path(cookie_container_t
*container
, const WCHAR
*path
)
242 return !strncmpiW(container
->path
, path
, strlenW(container
->path
));
245 static BOOL
create_cookie_url(LPCWSTR domain
, LPCWSTR path
, WCHAR
*buf
, DWORD buf_len
)
247 static const WCHAR cookie_prefix
[] = {'C','o','o','k','i','e',':'};
252 if(buf_len
< sizeof(cookie_prefix
)/sizeof(WCHAR
))
254 memcpy(buf
, cookie_prefix
, sizeof(cookie_prefix
));
255 buf
+= sizeof(cookie_prefix
)/sizeof(WCHAR
);
256 buf_len
-= sizeof(cookie_prefix
)/sizeof(WCHAR
);
260 if(!GetUserNameW(buf
, &len
))
270 len
= strlenW(domain
);
273 memcpy(buf
, domain
, len
*sizeof(WCHAR
));
280 memcpy(buf
, path
, len
*sizeof(WCHAR
));
290 static BOOL
load_persistent_cookie(LPCWSTR domain
, LPCWSTR path
)
292 INTERNET_CACHE_ENTRY_INFOW
*info
;
293 cookie_container_t
*cookie_container
;
294 cookie_t
*new_cookie
;
295 WCHAR cookie_url
[MAX_PATH
];
297 char *str
= NULL
, *pbeg
, *pend
;
300 FILETIME expiry
, create
, time
;
302 if (!create_cookie_url(domain
, path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
306 RetrieveUrlCacheEntryStreamW(cookie_url
, NULL
, &size
, FALSE
, 0);
307 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
309 info
= heap_alloc(size
);
312 cookie
= RetrieveUrlCacheEntryStreamW(cookie_url
, info
, &size
, FALSE
, 0);
313 size
= info
->dwSizeLow
;
318 if(!(str
= heap_alloc(size
+1)) || !ReadUrlCacheEntryStream(cookie
, 0, str
, &size
, 0)) {
319 UnlockUrlCacheEntryStream(cookie
, 0);
324 UnlockUrlCacheEntryStream(cookie
, 0);
326 cookie_container
= get_cookie_container(domain
, path
, TRUE
);
327 if(!cookie_container
) {
332 GetSystemTimeAsFileTime(&time
);
333 for(pbeg
=str
; pbeg
&& *pbeg
; name
=data
=NULL
) {
334 pend
= strchr(pbeg
, '\n');
338 name
= heap_strdupAtoW(pbeg
);
341 pend
= strchr(pbeg
, '\n');
345 data
= heap_strdupAtoW(pbeg
);
347 pbeg
= strchr(pend
+1, '\n');
350 sscanf(pbeg
, "%u %u %u %u %u", &flags
, &expiry
.dwLowDateTime
, &expiry
.dwHighDateTime
,
351 &create
.dwLowDateTime
, &create
.dwHighDateTime
);
354 pbeg
= strchr(pbeg
, '*');
364 if(CompareFileTime(&time
, &expiry
) <= 0) {
365 new_cookie
= alloc_cookie(NULL
, NULL
, expiry
, create
, flags
);
369 new_cookie
->name
= name
;
370 new_cookie
->data
= data
;
372 replace_cookie(cookie_container
, new_cookie
);
385 static BOOL
save_persistent_cookie(cookie_container_t
*container
)
387 static const WCHAR txtW
[] = {'t','x','t',0};
389 WCHAR cookie_url
[MAX_PATH
], cookie_file
[MAX_PATH
];
390 HANDLE cookie_handle
;
391 cookie_t
*cookie_container
= NULL
, *cookie_iter
;
392 BOOL do_save
= FALSE
;
393 char buf
[64], *dyn_buf
;
397 if (!create_cookie_url(container
->domain
->domain
, container
->path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
400 /* check if there's anything to save */
401 GetSystemTimeAsFileTime(&time
);
402 LIST_FOR_EACH_ENTRY_SAFE(cookie_container
, cookie_iter
, &container
->cookie_list
, cookie_t
, entry
)
404 if((cookie_container
->expiry
.dwLowDateTime
|| cookie_container
->expiry
.dwHighDateTime
)
405 && CompareFileTime(&time
, &cookie_container
->expiry
) > 0) {
406 delete_cookie(cookie_container
);
410 if(!(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)) {
416 DeleteUrlCacheEntryW(cookie_url
);
420 if(!CreateUrlCacheEntryW(cookie_url
, 0, txtW
, cookie_file
, 0))
422 cookie_handle
= CreateFileW(cookie_file
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
423 if(cookie_handle
== INVALID_HANDLE_VALUE
) {
424 DeleteFileW(cookie_file
);
428 LIST_FOR_EACH_ENTRY(cookie_container
, &container
->cookie_list
, cookie_t
, entry
)
430 if(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)
433 dyn_buf
= heap_strdupWtoA(cookie_container
->name
);
434 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
440 if(!WriteFile(cookie_handle
, "\n", 1, &bytes_written
, NULL
)) {
445 dyn_buf
= heap_strdupWtoA(cookie_container
->data
);
446 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
452 if(!WriteFile(cookie_handle
, "\n", 1, &bytes_written
, NULL
)) {
457 dyn_buf
= heap_strdupWtoA(container
->domain
->domain
);
458 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
465 dyn_buf
= heap_strdupWtoA(container
->path
);
466 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
473 sprintf(buf
, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container
->flags
,
474 cookie_container
->expiry
.dwLowDateTime
, cookie_container
->expiry
.dwHighDateTime
,
475 cookie_container
->create
.dwLowDateTime
, cookie_container
->create
.dwHighDateTime
);
476 if(!WriteFile(cookie_handle
, buf
, strlen(buf
), &bytes_written
, NULL
)) {
482 CloseHandle(cookie_handle
);
484 ERR("error saving cookie file\n");
485 DeleteFileW(cookie_file
);
489 memset(&time
, 0, sizeof(time
));
490 return CommitUrlCacheEntryW(cookie_url
, cookie_file
, time
, time
, 0, NULL
, 0, txtW
, 0);
493 static BOOL
COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
495 URL_COMPONENTSW UrlComponents
;
497 UrlComponents
.lpszExtraInfo
= NULL
;
498 UrlComponents
.lpszPassword
= NULL
;
499 UrlComponents
.lpszScheme
= NULL
;
500 UrlComponents
.lpszUrlPath
= path
;
501 UrlComponents
.lpszUserName
= NULL
;
502 UrlComponents
.lpszHostName
= hostName
;
503 UrlComponents
.dwExtraInfoLength
= 0;
504 UrlComponents
.dwPasswordLength
= 0;
505 UrlComponents
.dwSchemeLength
= 0;
506 UrlComponents
.dwUserNameLength
= 0;
507 UrlComponents
.dwHostNameLength
= hostNameLen
;
508 UrlComponents
.dwUrlPathLength
= pathLen
;
510 if (!InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
)) return FALSE
;
512 /* discard the webpage off the end of the path */
513 if (UrlComponents
.dwUrlPathLength
)
515 if (path
[UrlComponents
.dwUrlPathLength
- 1] != '/')
518 if ((ptr
= strrchrW(path
, '/'))) *(++ptr
) = 0;
526 else if (pathLen
>= 2)
542 static DWORD
get_cookie(const WCHAR
*host
, const WCHAR
*path
, DWORD flags
, cookie_set_t
*res
)
544 static const WCHAR empty_path
[] = { '/',0 };
546 WCHAR
*ptr
, subpath
[INTERNET_MAX_PATH_LENGTH
];
548 cookie_domain_t
*domain
;
549 cookie_container_t
*container
;
553 GetSystemTimeAsFileTime(&tm
);
557 while(p
>host
&& p
[-1]!='.') p
--;
560 while(p
>host
&& p
[-1]!='.') p
--;
563 load_persistent_cookie(p
, empty_path
);
567 assert(len
+1 < INTERNET_MAX_PATH_LENGTH
);
568 memcpy(subpath
, path
, (len
+1)*sizeof(WCHAR
));
572 load_persistent_cookie(host
, subpath
);
575 while(ptr
>subpath
&& ptr
[-1]!='/') ptr
--;
576 }while(ptr
!= subpath
);
578 domain
= get_cookie_domain(host
, FALSE
);
580 TRACE("Unknown host %s\n", debugstr_w(host
));
581 return ERROR_NO_MORE_ITEMS
;
584 for(domain
= get_cookie_domain(host
, FALSE
); domain
; domain
= domain
->parent
) {
585 TRACE("Trying %s domain...\n", debugstr_w(domain
->domain
));
587 LIST_FOR_EACH_ENTRY(container
, &domain
->path_list
, cookie_container_t
, entry
) {
588 struct list
*cursor
, *cursor2
;
590 TRACE("path %s\n", debugstr_w(container
->path
));
592 if(!cookie_match_path(container
, path
))
595 TRACE("found domain %p\n", domain
->domain
);
597 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &container
->cookie_list
) {
598 cookie_t
*cookie_iter
= LIST_ENTRY(cursor
, cookie_t
, entry
);
600 /* check for expiry */
601 if((cookie_iter
->expiry
.dwLowDateTime
!= 0 || cookie_iter
->expiry
.dwHighDateTime
!= 0)
602 && CompareFileTime(&tm
, &cookie_iter
->expiry
) > 0) {
603 TRACE("Found expired cookie. deleting\n");
604 delete_cookie(cookie_iter
);
608 if((cookie_iter
->flags
& INTERNET_COOKIE_HTTPONLY
) && !(flags
& INTERNET_COOKIE_HTTPONLY
))
613 res
->cookies
= heap_alloc(4*sizeof(*res
->cookies
));
617 }else if(res
->cnt
== res
->size
) {
618 cookie_t
**new_cookies
= heap_realloc(res
->cookies
, res
->size
*2*sizeof(*res
->cookies
));
621 res
->cookies
= new_cookies
;
626 res
->string_len
+= 2; /* '; ' */
627 res
->cookies
[res
->cnt
++] = cookie_iter
;
629 res
->string_len
+= strlenW(cookie_iter
->name
);
630 if(*cookie_iter
->data
)
631 res
->string_len
+= 1 /* = */ + strlenW(cookie_iter
->data
);
636 return ERROR_SUCCESS
;
639 static void cookie_set_to_string(const cookie_set_t
*cookie_set
, WCHAR
*str
)
644 for(i
=0; i
<cookie_set
->cnt
; i
++) {
650 len
= strlenW(cookie_set
->cookies
[i
]->name
);
651 memcpy(ptr
, cookie_set
->cookies
[i
]->name
, len
*sizeof(WCHAR
));
654 if(*cookie_set
->cookies
[i
]->data
) {
656 len
= strlenW(cookie_set
->cookies
[i
]->data
);
657 memcpy(ptr
, cookie_set
->cookies
[i
]->data
, len
*sizeof(WCHAR
));
662 assert(ptr
-str
== cookie_set
->string_len
);
663 TRACE("%s\n", debugstr_wn(str
, ptr
-str
));
666 DWORD
get_cookie_header(const WCHAR
*host
, const WCHAR
*path
, WCHAR
**ret
)
668 cookie_set_t cookie_set
= {0};
671 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' '};
673 EnterCriticalSection(&cookie_cs
);
675 res
= get_cookie(host
, path
, INTERNET_COOKIE_HTTPONLY
, &cookie_set
);
676 if(res
!= ERROR_SUCCESS
) {
677 LeaveCriticalSection(&cookie_cs
);
684 ptr
= header
= heap_alloc(sizeof(cookieW
) + (cookie_set
.string_len
+ 3 /* crlf0 */) * sizeof(WCHAR
));
686 memcpy(ptr
, cookieW
, sizeof(cookieW
));
687 ptr
+= sizeof(cookieW
)/sizeof(*cookieW
);
689 cookie_set_to_string(&cookie_set
, ptr
);
690 heap_free(cookie_set
.cookies
);
691 ptr
+= cookie_set
.string_len
;
699 res
= ERROR_NOT_ENOUGH_MEMORY
;
705 LeaveCriticalSection(&cookie_cs
);
709 /***********************************************************************
710 * InternetGetCookieExW (WININET.@)
712 * Retrieve cookie from the specified url
714 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
715 * So it won't be implemented here.
722 BOOL WINAPI
InternetGetCookieExW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
723 LPWSTR lpCookieData
, LPDWORD lpdwSize
, DWORD flags
, void *reserved
)
725 WCHAR host
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
726 cookie_set_t cookie_set
= {0};
730 TRACE("(%s, %s, %p, %p, %x, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
), lpCookieData
, lpdwSize
, flags
, reserved
);
733 FIXME("flags 0x%08x not supported\n", flags
);
737 SetLastError(ERROR_INVALID_PARAMETER
);
742 ret
= COOKIE_crackUrlSimple(lpszUrl
, host
, sizeof(host
)/sizeof(host
[0]), path
, sizeof(path
)/sizeof(path
[0]));
743 if (!ret
|| !host
[0]) {
744 SetLastError(ERROR_INVALID_PARAMETER
);
748 EnterCriticalSection(&cookie_cs
);
750 res
= get_cookie(host
, path
, flags
, &cookie_set
);
751 if(res
!= ERROR_SUCCESS
) {
752 LeaveCriticalSection(&cookie_cs
);
758 if(!lpCookieData
|| cookie_set
.string_len
+1 > *lpdwSize
) {
759 *lpdwSize
= (cookie_set
.string_len
+ 1) * sizeof(WCHAR
);
760 TRACE("returning %u\n", *lpdwSize
);
762 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
766 *lpdwSize
= cookie_set
.string_len
+ 1;
767 cookie_set_to_string(&cookie_set
, lpCookieData
);
768 lpCookieData
[cookie_set
.string_len
] = 0;
771 TRACE("no cookies found for %s\n", debugstr_w(host
));
772 SetLastError(ERROR_NO_MORE_ITEMS
);
776 heap_free(cookie_set
.cookies
);
777 LeaveCriticalSection(&cookie_cs
);
781 /***********************************************************************
782 * InternetGetCookieW (WININET.@)
784 * Retrieve cookie for the specified URL.
786 BOOL WINAPI
InternetGetCookieW(const WCHAR
*url
, const WCHAR
*name
, WCHAR
*data
, DWORD
*size
)
788 TRACE("(%s, %s, %s, %p)\n", debugstr_w(url
), debugstr_w(name
), debugstr_w(data
), size
);
790 return InternetGetCookieExW(url
, name
, data
, size
, 0, NULL
);
793 /***********************************************************************
794 * InternetGetCookieExA (WININET.@)
796 * Retrieve cookie from the specified url
803 BOOL WINAPI
InternetGetCookieExA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
804 LPSTR lpCookieData
, LPDWORD lpdwSize
, DWORD flags
, void *reserved
)
810 TRACE("(%s %s %p %p(%u) %x %p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
811 lpCookieData
, lpdwSize
, lpdwSize
? *lpdwSize
: 0, flags
, reserved
);
813 url
= heap_strdupAtoW(lpszUrl
);
814 name
= heap_strdupAtoW(lpszCookieName
);
816 r
= InternetGetCookieExW( url
, name
, NULL
, &len
, flags
, reserved
);
821 szCookieData
= heap_alloc(len
* sizeof(WCHAR
));
828 r
= InternetGetCookieExW( url
, name
, szCookieData
, &len
, flags
, reserved
);
831 size
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, NULL
, 0, NULL
, NULL
);
833 if(*lpdwSize
>= size
) {
834 WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, lpCookieData
, *lpdwSize
, NULL
, NULL
);
836 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
843 heap_free( szCookieData
);
851 /***********************************************************************
852 * InternetGetCookieA (WININET.@)
854 * See InternetGetCookieW.
856 BOOL WINAPI
InternetGetCookieA(const char *url
, const char *name
, char *data
, DWORD
*size
)
858 TRACE("(%s, %s, %s, %p)\n", debugstr_a(url
), debugstr_a(name
), debugstr_a(data
), size
);
860 return InternetGetCookieExA(url
, name
, data
, size
, 0, NULL
);
863 /***********************************************************************
864 * IsDomainLegalCookieDomainW (WININET.@)
866 BOOL WINAPI
IsDomainLegalCookieDomainW( LPCWSTR s1
, LPCWSTR s2
)
868 DWORD s1_len
, s2_len
;
870 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1
), debugstr_w(s2
));
874 SetLastError(ERROR_INVALID_PARAMETER
);
877 if (s1
[0] == '.' || !s1
[0] || s2
[0] == '.' || !s2
[0])
879 SetLastError(ERROR_INVALID_NAME
);
882 if(!strchrW(s1
, '.') || !strchrW(s2
, '.'))
885 s1_len
= strlenW(s1
);
886 s2_len
= strlenW(s2
);
890 if (strncmpiW(s1
, s2
+s2_len
-s1_len
, s1_len
) || (s2_len
>s1_len
&& s2
[s2_len
-s1_len
-1]!='.'))
892 SetLastError(ERROR_INVALID_PARAMETER
);
899 DWORD
set_cookie(const WCHAR
*domain
, const WCHAR
*path
, const WCHAR
*cookie_name
, const WCHAR
*cookie_data
, DWORD flags
)
901 cookie_container_t
*container
;
902 cookie_t
*thisCookie
;
905 FILETIME expiry
, create
;
906 BOOL expired
= FALSE
, update_persistent
= FALSE
;
907 DWORD cookie_flags
= 0;
909 TRACE("%s %s %s=%s %x\n", debugstr_w(domain
), debugstr_w(path
), debugstr_w(cookie_name
), debugstr_w(cookie_data
), flags
);
911 value
= data
= heap_strdupW(cookie_data
);
914 ERR("could not allocate the cookie data buffer\n");
915 return COOKIE_STATE_UNKNOWN
;
918 memset(&expiry
,0,sizeof(expiry
));
919 GetSystemTimeAsFileTime(&create
);
921 /* lots of information can be parsed out of the cookie value */
926 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
927 static const WCHAR szPath
[] = {'p','a','t','h','=',0};
928 static const WCHAR szExpires
[] = {'e','x','p','i','r','e','s','=',0};
929 static const WCHAR szSecure
[] = {'s','e','c','u','r','e',0};
930 static const WCHAR szHttpOnly
[] = {'h','t','t','p','o','n','l','y',0};
931 static const WCHAR szVersion
[] = {'v','e','r','s','i','o','n','=',0};
933 if (!(ptr
= strchrW(ptr
,';'))) break;
936 if (value
!= data
) heap_free(value
);
937 value
= heap_alloc((ptr
- data
) * sizeof(WCHAR
));
941 ERR("could not allocate the cookie value buffer\n");
942 return COOKIE_STATE_UNKNOWN
;
944 strcpyW(value
, data
);
946 while (*ptr
== ' ') ptr
++; /* whitespace */
948 if (strncmpiW(ptr
, szDomain
, 7) == 0)
952 ptr
+= sizeof(szDomain
)/sizeof(szDomain
[0])-1;
955 end_ptr
= strchrW(ptr
, ';');
959 if(!IsDomainLegalCookieDomainW(ptr
, domain
))
964 return COOKIE_STATE_UNKNOWN
;
971 TRACE("Parsing new domain %s\n",debugstr_w(domain
));
973 else if (strncmpiW(ptr
, szPath
, 5) == 0)
975 ptr
+=strlenW(szPath
);
977 TRACE("Parsing new path %s\n",debugstr_w(path
));
979 else if (strncmpiW(ptr
, szExpires
, 8) == 0)
982 ptr
+=strlenW(szExpires
);
983 if (InternetTimeToSystemTimeW(ptr
, &st
, 0))
985 SystemTimeToFileTime(&st
, &expiry
);
987 if (CompareFileTime(&create
,&expiry
) > 0)
989 TRACE("Cookie already expired.\n");
994 else if (strncmpiW(ptr
, szSecure
, 6) == 0)
996 FIXME("secure not handled (%s)\n",debugstr_w(ptr
));
997 ptr
+= strlenW(szSecure
);
999 else if (strncmpiW(ptr
, szHttpOnly
, 8) == 0)
1001 if(!(flags
& INTERNET_COOKIE_HTTPONLY
)) {
1002 WARN("HTTP only cookie added without INTERNET_COOKIE_HTTPONLY flag\n");
1004 if (value
!= data
) heap_free(value
);
1005 SetLastError(ERROR_INVALID_OPERATION
);
1006 return COOKIE_STATE_REJECT
;
1009 cookie_flags
|= INTERNET_COOKIE_HTTPONLY
;
1010 ptr
+= strlenW(szHttpOnly
);
1012 else if (strncmpiW(ptr
, szVersion
, 8) == 0)
1014 FIXME("version not handled (%s)\n",debugstr_w(ptr
));
1015 ptr
+= strlenW(szVersion
);
1019 FIXME("Unknown additional option %s\n",debugstr_w(ptr
));
1024 EnterCriticalSection(&cookie_cs
);
1026 load_persistent_cookie(domain
, path
);
1028 container
= get_cookie_container(domain
, path
, !expired
);
1031 if (value
!= data
) heap_free(value
);
1032 LeaveCriticalSection(&cookie_cs
);
1033 return COOKIE_STATE_ACCEPT
;
1036 if(!expiry
.dwLowDateTime
&& !expiry
.dwHighDateTime
)
1037 cookie_flags
|= INTERNET_COOKIE_IS_SESSION
;
1039 update_persistent
= TRUE
;
1041 if ((thisCookie
= find_cookie(container
, cookie_name
)))
1043 if ((thisCookie
->flags
& INTERNET_COOKIE_HTTPONLY
) && !(flags
& INTERNET_COOKIE_HTTPONLY
)) {
1044 WARN("An attempt to override httponly cookie\n");
1045 SetLastError(ERROR_INVALID_OPERATION
);
1047 if (value
!= data
) heap_free(value
);
1048 return COOKIE_STATE_REJECT
;
1051 if (!(thisCookie
->flags
& INTERNET_COOKIE_IS_SESSION
))
1052 update_persistent
= TRUE
;
1053 delete_cookie(thisCookie
);
1056 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name
),
1057 debugstr_w(value
), debugstr_w(container
->domain
->domain
),debugstr_w(container
->path
));
1060 cookie_t
*new_cookie
;
1062 new_cookie
= alloc_cookie(cookie_name
, value
, expiry
, create
, cookie_flags
);
1065 if (value
!= data
) heap_free(value
);
1066 LeaveCriticalSection(&cookie_cs
);
1067 return COOKIE_STATE_UNKNOWN
;
1070 add_cookie(container
, new_cookie
);
1073 if (value
!= data
) heap_free(value
);
1075 if (!update_persistent
|| save_persistent_cookie(container
))
1077 LeaveCriticalSection(&cookie_cs
);
1078 return COOKIE_STATE_ACCEPT
;
1080 LeaveCriticalSection(&cookie_cs
);
1081 return COOKIE_STATE_UNKNOWN
;
1084 /***********************************************************************
1085 * InternetSetCookieExW (WININET.@)
1087 * Sets cookie for the specified url
1089 DWORD WINAPI
InternetSetCookieExW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
1090 LPCWSTR lpCookieData
, DWORD flags
, DWORD_PTR reserved
)
1093 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
1095 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_w(lpszUrl
), debugstr_w(lpszCookieName
),
1096 debugstr_w(lpCookieData
), flags
, reserved
);
1098 if (flags
& ~INTERNET_COOKIE_HTTPONLY
)
1099 FIXME("flags %x not supported\n", flags
);
1101 if (!lpszUrl
|| !lpCookieData
)
1103 SetLastError(ERROR_INVALID_PARAMETER
);
1104 return COOKIE_STATE_UNKNOWN
;
1108 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
1109 if (!ret
|| !hostName
[0]) return COOKIE_STATE_UNKNOWN
;
1111 if (!lpszCookieName
)
1113 WCHAR
*cookie
, *data
;
1116 cookie
= heap_strdupW(lpCookieData
);
1119 SetLastError(ERROR_OUTOFMEMORY
);
1120 return COOKIE_STATE_UNKNOWN
;
1123 /* some apps (or is it us??) try to add a cookie with no cookie name, but
1124 * the cookie data in the form of name[=data].
1126 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ strlenW(cookie
);
1129 res
= set_cookie(hostName
, path
, cookie
, data
, flags
);
1134 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
, flags
);
1137 /***********************************************************************
1138 * InternetSetCookieW (WININET.@)
1140 * Sets a cookie for the specified URL.
1142 BOOL WINAPI
InternetSetCookieW(const WCHAR
*url
, const WCHAR
*name
, const WCHAR
*data
)
1144 TRACE("(%s, %s, %s)\n", debugstr_w(url
), debugstr_w(name
), debugstr_w(data
));
1146 return InternetSetCookieExW(url
, name
, data
, 0, 0) == COOKIE_STATE_ACCEPT
;
1149 /***********************************************************************
1150 * InternetSetCookieA (WININET.@)
1152 * Sets cookie for the specified url
1159 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
1160 LPCSTR lpCookieData
)
1162 LPWSTR data
, url
, name
;
1165 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
1166 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
1168 url
= heap_strdupAtoW(lpszUrl
);
1169 name
= heap_strdupAtoW(lpszCookieName
);
1170 data
= heap_strdupAtoW(lpCookieData
);
1172 r
= InternetSetCookieW( url
, name
, data
);
1180 /***********************************************************************
1181 * InternetSetCookieExA (WININET.@)
1183 * See InternetSetCookieExW.
1185 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
1186 DWORD dwFlags
, DWORD_PTR dwReserved
)
1188 WCHAR
*data
, *url
, *name
;
1191 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_a(lpszURL
), debugstr_a(lpszCookieName
),
1192 debugstr_a(lpszCookieData
), dwFlags
, dwReserved
);
1194 url
= heap_strdupAtoW(lpszURL
);
1195 name
= heap_strdupAtoW(lpszCookieName
);
1196 data
= heap_strdupAtoW(lpszCookieData
);
1198 r
= InternetSetCookieExW(url
, name
, data
, dwFlags
, dwReserved
);
1206 /***********************************************************************
1207 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1209 * Clears all per-site decisions about cookies.
1216 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
1222 /***********************************************************************
1223 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1225 * See InternetEnumPerSiteCookieDecisionW.
1227 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1228 ULONG
*pdwDecision
, ULONG dwIndex
)
1230 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1231 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1235 /***********************************************************************
1236 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1238 * Enumerates all per-site decisions about cookies.
1245 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1246 ULONG
*pdwDecision
, ULONG dwIndex
)
1248 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1249 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1253 /***********************************************************************
1254 * InternetGetPerSiteCookieDecisionA (WININET.@)
1256 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, ULONG
*pResult
)
1258 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
1262 /***********************************************************************
1263 * InternetGetPerSiteCookieDecisionW (WININET.@)
1265 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, ULONG
*pResult
)
1267 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
1271 /***********************************************************************
1272 * InternetSetPerSiteCookieDecisionA (WININET.@)
1274 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
1276 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
1280 /***********************************************************************
1281 * InternetSetPerSiteCookieDecisionW (WININET.@)
1283 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
1285 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);
1289 void free_cookie(void)
1291 DeleteCriticalSection(&cookie_cs
);