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
)
330 GetSystemTimeAsFileTime(&time
);
331 for(pbeg
=str
; pbeg
&& *pbeg
; name
=data
=NULL
) {
332 pend
= strchr(pbeg
, '\n');
336 name
= heap_strdupAtoW(pbeg
);
339 pend
= strchr(pbeg
, '\n');
343 data
= heap_strdupAtoW(pbeg
);
346 pbeg
= strchr(pend
+1, '\n');
349 sscanf(pbeg
, "%u %u %u %u %u", &flags
, &expiry
.dwLowDateTime
, &expiry
.dwHighDateTime
,
350 &create
.dwLowDateTime
, &create
.dwHighDateTime
);
353 pbeg
= strchr(pbeg
, '*');
363 if(CompareFileTime(&time
, &expiry
) <= 0) {
364 new_cookie
= alloc_cookie(NULL
, NULL
, expiry
, create
, flags
);
368 new_cookie
->name
= name
;
369 new_cookie
->data
= data
;
371 replace_cookie(cookie_container
, new_cookie
);
384 static BOOL
save_persistent_cookie(cookie_container_t
*container
)
386 static const WCHAR txtW
[] = {'t','x','t',0};
388 WCHAR cookie_url
[MAX_PATH
], cookie_file
[MAX_PATH
];
389 HANDLE cookie_handle
;
390 cookie_t
*cookie_container
= NULL
, *cookie_iter
;
391 BOOL do_save
= FALSE
;
392 char buf
[64], *dyn_buf
;
396 if (!create_cookie_url(container
->domain
->domain
, container
->path
, cookie_url
, sizeof(cookie_url
)/sizeof(cookie_url
[0])))
399 /* check if there's anything to save */
400 GetSystemTimeAsFileTime(&time
);
401 LIST_FOR_EACH_ENTRY_SAFE(cookie_container
, cookie_iter
, &container
->cookie_list
, cookie_t
, entry
)
403 if((cookie_container
->expiry
.dwLowDateTime
|| cookie_container
->expiry
.dwHighDateTime
)
404 && CompareFileTime(&time
, &cookie_container
->expiry
) > 0) {
405 delete_cookie(cookie_container
);
409 if(!(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)) {
415 DeleteUrlCacheEntryW(cookie_url
);
419 if(!CreateUrlCacheEntryW(cookie_url
, 0, txtW
, cookie_file
, 0))
421 cookie_handle
= CreateFileW(cookie_file
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
422 if(cookie_handle
== INVALID_HANDLE_VALUE
) {
423 DeleteFileW(cookie_file
);
427 LIST_FOR_EACH_ENTRY(cookie_container
, &container
->cookie_list
, cookie_t
, entry
)
429 if(cookie_container
->flags
& INTERNET_COOKIE_IS_SESSION
)
432 dyn_buf
= heap_strdupWtoA(cookie_container
->name
);
433 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
439 if(!WriteFile(cookie_handle
, "\n", 1, &bytes_written
, NULL
)) {
444 dyn_buf
= heap_strdupWtoA(cookie_container
->data
);
445 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
451 if(!WriteFile(cookie_handle
, "\n", 1, &bytes_written
, NULL
)) {
456 dyn_buf
= heap_strdupWtoA(container
->domain
->domain
);
457 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
464 dyn_buf
= heap_strdupWtoA(container
->path
);
465 if(!dyn_buf
|| !WriteFile(cookie_handle
, dyn_buf
, strlen(dyn_buf
), &bytes_written
, NULL
)) {
472 sprintf(buf
, "\n%u\n%u\n%u\n%u\n%u\n*\n", cookie_container
->flags
,
473 cookie_container
->expiry
.dwLowDateTime
, cookie_container
->expiry
.dwHighDateTime
,
474 cookie_container
->create
.dwLowDateTime
, cookie_container
->create
.dwHighDateTime
);
475 if(!WriteFile(cookie_handle
, buf
, strlen(buf
), &bytes_written
, NULL
)) {
481 CloseHandle(cookie_handle
);
483 ERR("error saving cookie file\n");
484 DeleteFileW(cookie_file
);
488 memset(&time
, 0, sizeof(time
));
489 return CommitUrlCacheEntryW(cookie_url
, cookie_file
, time
, time
, 0, NULL
, 0, txtW
, 0);
492 static BOOL
COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
494 URL_COMPONENTSW UrlComponents
;
496 UrlComponents
.lpszExtraInfo
= NULL
;
497 UrlComponents
.lpszPassword
= NULL
;
498 UrlComponents
.lpszScheme
= NULL
;
499 UrlComponents
.lpszUrlPath
= path
;
500 UrlComponents
.lpszUserName
= NULL
;
501 UrlComponents
.lpszHostName
= hostName
;
502 UrlComponents
.dwExtraInfoLength
= 0;
503 UrlComponents
.dwPasswordLength
= 0;
504 UrlComponents
.dwSchemeLength
= 0;
505 UrlComponents
.dwUserNameLength
= 0;
506 UrlComponents
.dwHostNameLength
= hostNameLen
;
507 UrlComponents
.dwUrlPathLength
= pathLen
;
509 if (!InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
)) return FALSE
;
511 /* discard the webpage off the end of the path */
512 if (UrlComponents
.dwUrlPathLength
)
514 if (path
[UrlComponents
.dwUrlPathLength
- 1] != '/')
517 if ((ptr
= strrchrW(path
, '/'))) *(++ptr
) = 0;
525 else if (pathLen
>= 2)
541 static DWORD
get_cookie(const WCHAR
*host
, const WCHAR
*path
, DWORD flags
, cookie_set_t
*res
)
543 static const WCHAR empty_path
[] = { '/',0 };
545 WCHAR
*ptr
, subpath
[INTERNET_MAX_PATH_LENGTH
];
547 cookie_domain_t
*domain
;
548 cookie_container_t
*container
;
552 GetSystemTimeAsFileTime(&tm
);
556 while(p
>host
&& p
[-1]!='.') p
--;
559 while(p
>host
&& p
[-1]!='.') p
--;
562 load_persistent_cookie(p
, empty_path
);
566 assert(len
+1 < INTERNET_MAX_PATH_LENGTH
);
567 memcpy(subpath
, path
, (len
+1)*sizeof(WCHAR
));
571 load_persistent_cookie(host
, subpath
);
574 while(ptr
>subpath
&& ptr
[-1]!='/') ptr
--;
575 }while(ptr
!= subpath
);
577 domain
= get_cookie_domain(host
, FALSE
);
579 TRACE("Unknown host %s\n", debugstr_w(host
));
580 return ERROR_NO_MORE_ITEMS
;
583 for(domain
= get_cookie_domain(host
, FALSE
); domain
; domain
= domain
->parent
) {
584 TRACE("Trying %s domain...\n", debugstr_w(domain
->domain
));
586 LIST_FOR_EACH_ENTRY(container
, &domain
->path_list
, cookie_container_t
, entry
) {
587 struct list
*cursor
, *cursor2
;
589 TRACE("path %s\n", debugstr_w(container
->path
));
591 if(!cookie_match_path(container
, path
))
594 TRACE("found domain %p\n", domain
->domain
);
596 LIST_FOR_EACH_SAFE(cursor
, cursor2
, &container
->cookie_list
) {
597 cookie_t
*cookie_iter
= LIST_ENTRY(cursor
, cookie_t
, entry
);
599 /* check for expiry */
600 if((cookie_iter
->expiry
.dwLowDateTime
!= 0 || cookie_iter
->expiry
.dwHighDateTime
!= 0)
601 && CompareFileTime(&tm
, &cookie_iter
->expiry
) > 0) {
602 TRACE("Found expired cookie. deleting\n");
603 delete_cookie(cookie_iter
);
607 if((cookie_iter
->flags
& INTERNET_COOKIE_HTTPONLY
) && !(flags
& INTERNET_COOKIE_HTTPONLY
))
612 res
->cookies
= heap_alloc(4*sizeof(*res
->cookies
));
616 }else if(res
->cnt
== res
->size
) {
617 cookie_t
**new_cookies
= heap_realloc(res
->cookies
, res
->size
*2*sizeof(*res
->cookies
));
620 res
->cookies
= new_cookies
;
625 res
->string_len
+= 2; /* '; ' */
626 res
->cookies
[res
->cnt
++] = cookie_iter
;
628 res
->string_len
+= strlenW(cookie_iter
->name
);
629 if(*cookie_iter
->data
)
630 res
->string_len
+= 1 /* = */ + strlenW(cookie_iter
->data
);
635 return ERROR_SUCCESS
;
638 static void cookie_set_to_string(const cookie_set_t
*cookie_set
, WCHAR
*str
)
643 for(i
=0; i
<cookie_set
->cnt
; i
++) {
649 len
= strlenW(cookie_set
->cookies
[i
]->name
);
650 memcpy(ptr
, cookie_set
->cookies
[i
]->name
, len
*sizeof(WCHAR
));
653 if(*cookie_set
->cookies
[i
]->data
) {
655 len
= strlenW(cookie_set
->cookies
[i
]->data
);
656 memcpy(ptr
, cookie_set
->cookies
[i
]->data
, len
*sizeof(WCHAR
));
661 assert(ptr
-str
== cookie_set
->string_len
);
662 TRACE("%s\n", debugstr_wn(str
, ptr
-str
));
665 DWORD
get_cookie_header(const WCHAR
*host
, const WCHAR
*path
, WCHAR
**ret
)
667 cookie_set_t cookie_set
= {0};
670 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' '};
672 EnterCriticalSection(&cookie_cs
);
674 res
= get_cookie(host
, path
, INTERNET_COOKIE_HTTPONLY
, &cookie_set
);
675 if(res
!= ERROR_SUCCESS
) {
676 LeaveCriticalSection(&cookie_cs
);
683 ptr
= header
= heap_alloc(sizeof(cookieW
) + (cookie_set
.string_len
+ 3 /* crlf0 */) * sizeof(WCHAR
));
685 memcpy(ptr
, cookieW
, sizeof(cookieW
));
686 ptr
+= sizeof(cookieW
)/sizeof(*cookieW
);
688 cookie_set_to_string(&cookie_set
, ptr
);
689 heap_free(cookie_set
.cookies
);
690 ptr
+= cookie_set
.string_len
;
698 res
= ERROR_NOT_ENOUGH_MEMORY
;
704 LeaveCriticalSection(&cookie_cs
);
705 return ERROR_SUCCESS
;
708 /***********************************************************************
709 * InternetGetCookieExW (WININET.@)
711 * Retrieve cookie from the specified url
713 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
714 * So it won't be implemented here.
721 BOOL WINAPI
InternetGetCookieExW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
722 LPWSTR lpCookieData
, LPDWORD lpdwSize
, DWORD flags
, void *reserved
)
724 WCHAR host
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
725 cookie_set_t cookie_set
= {0};
729 TRACE("(%s, %s, %p, %p, %x, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
), lpCookieData
, lpdwSize
, flags
, reserved
);
732 FIXME("flags 0x%08x not supported\n", flags
);
736 SetLastError(ERROR_INVALID_PARAMETER
);
741 ret
= COOKIE_crackUrlSimple(lpszUrl
, host
, sizeof(host
)/sizeof(host
[0]), path
, sizeof(path
)/sizeof(path
[0]));
742 if (!ret
|| !host
[0]) {
743 SetLastError(ERROR_INVALID_PARAMETER
);
747 EnterCriticalSection(&cookie_cs
);
749 res
= get_cookie(host
, path
, flags
, &cookie_set
);
750 if(res
!= ERROR_SUCCESS
) {
751 LeaveCriticalSection(&cookie_cs
);
757 if(!lpCookieData
|| cookie_set
.string_len
+1 > *lpdwSize
) {
758 *lpdwSize
= (cookie_set
.string_len
+ 1) * sizeof(WCHAR
);
759 TRACE("returning %u\n", *lpdwSize
);
761 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
765 *lpdwSize
= cookie_set
.string_len
+ 1;
766 cookie_set_to_string(&cookie_set
, lpCookieData
);
767 lpCookieData
[cookie_set
.string_len
] = 0;
770 TRACE("no cookies found for %s\n", debugstr_w(host
));
771 SetLastError(ERROR_NO_MORE_ITEMS
);
775 heap_free(cookie_set
.cookies
);
776 LeaveCriticalSection(&cookie_cs
);
780 /***********************************************************************
781 * InternetGetCookieW (WININET.@)
783 * Retrieve cookie for the specified URL.
785 BOOL WINAPI
InternetGetCookieW(const WCHAR
*url
, const WCHAR
*name
, WCHAR
*data
, DWORD
*size
)
787 TRACE("(%s, %s, %s, %p)\n", debugstr_w(url
), debugstr_w(name
), debugstr_w(data
), size
);
789 return InternetGetCookieExW(url
, name
, data
, size
, 0, NULL
);
792 /***********************************************************************
793 * InternetGetCookieExA (WININET.@)
795 * Retrieve cookie from the specified url
802 BOOL WINAPI
InternetGetCookieExA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
803 LPSTR lpCookieData
, LPDWORD lpdwSize
, DWORD flags
, void *reserved
)
809 TRACE("(%s %s %p %p(%u) %x %p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
810 lpCookieData
, lpdwSize
, lpdwSize
? *lpdwSize
: 0, flags
, reserved
);
812 url
= heap_strdupAtoW(lpszUrl
);
813 name
= heap_strdupAtoW(lpszCookieName
);
815 r
= InternetGetCookieExW( url
, name
, NULL
, &len
, flags
, reserved
);
820 szCookieData
= heap_alloc(len
* sizeof(WCHAR
));
827 r
= InternetGetCookieExW( url
, name
, szCookieData
, &len
, flags
, reserved
);
830 size
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, NULL
, 0, NULL
, NULL
);
832 if(*lpdwSize
>= size
) {
833 WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
, lpCookieData
, *lpdwSize
, NULL
, NULL
);
835 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
842 heap_free( szCookieData
);
850 /***********************************************************************
851 * InternetGetCookieA (WININET.@)
853 * See InternetGetCookieW.
855 BOOL WINAPI
InternetGetCookieA(const char *url
, const char *name
, char *data
, DWORD
*size
)
857 TRACE("(%s, %s, %s, %p)\n", debugstr_a(url
), debugstr_a(name
), debugstr_a(data
), size
);
859 return InternetGetCookieExA(url
, name
, data
, size
, 0, NULL
);
862 /***********************************************************************
863 * IsDomainLegalCookieDomainW (WININET.@)
865 BOOL WINAPI
IsDomainLegalCookieDomainW( LPCWSTR s1
, LPCWSTR s2
)
867 DWORD s1_len
, s2_len
;
869 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1
), debugstr_w(s2
));
873 SetLastError(ERROR_INVALID_PARAMETER
);
876 if (s1
[0] == '.' || !s1
[0] || s2
[0] == '.' || !s2
[0])
878 SetLastError(ERROR_INVALID_NAME
);
881 if(!strchrW(s1
, '.') || !strchrW(s2
, '.'))
884 s1_len
= strlenW(s1
);
885 s2_len
= strlenW(s2
);
889 if (strncmpiW(s1
, s2
+s2_len
-s1_len
, s1_len
) || (s2_len
>s1_len
&& s2
[s2_len
-s1_len
-1]!='.'))
891 SetLastError(ERROR_INVALID_PARAMETER
);
898 DWORD
set_cookie(const WCHAR
*domain
, const WCHAR
*path
, const WCHAR
*cookie_name
, const WCHAR
*cookie_data
, DWORD flags
)
900 cookie_container_t
*container
;
901 cookie_t
*thisCookie
;
904 FILETIME expiry
, create
;
905 BOOL expired
= FALSE
, update_persistent
= FALSE
;
906 DWORD cookie_flags
= 0;
908 TRACE("%s %s %s=%s %x\n", debugstr_w(domain
), debugstr_w(path
), debugstr_w(cookie_name
), debugstr_w(cookie_data
), flags
);
910 value
= data
= heap_strdupW(cookie_data
);
913 ERR("could not allocate the cookie data buffer\n");
914 return COOKIE_STATE_UNKNOWN
;
917 memset(&expiry
,0,sizeof(expiry
));
918 GetSystemTimeAsFileTime(&create
);
920 /* lots of information can be parsed out of the cookie value */
925 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
926 static const WCHAR szPath
[] = {'p','a','t','h','=',0};
927 static const WCHAR szExpires
[] = {'e','x','p','i','r','e','s','=',0};
928 static const WCHAR szSecure
[] = {'s','e','c','u','r','e',0};
929 static const WCHAR szHttpOnly
[] = {'h','t','t','p','o','n','l','y',0};
931 if (!(ptr
= strchrW(ptr
,';'))) break;
934 if (value
!= data
) heap_free(value
);
935 value
= heap_alloc((ptr
- data
) * sizeof(WCHAR
));
939 ERR("could not allocate the cookie value buffer\n");
940 return COOKIE_STATE_UNKNOWN
;
942 strcpyW(value
, data
);
944 while (*ptr
== ' ') ptr
++; /* whitespace */
946 if (strncmpiW(ptr
, szDomain
, 7) == 0)
950 ptr
+= sizeof(szDomain
)/sizeof(szDomain
[0])-1;
953 end_ptr
= strchrW(ptr
, ';');
957 if(!IsDomainLegalCookieDomainW(ptr
, domain
))
962 return COOKIE_STATE_UNKNOWN
;
969 TRACE("Parsing new domain %s\n",debugstr_w(domain
));
971 else if (strncmpiW(ptr
, szPath
, 5) == 0)
973 ptr
+=strlenW(szPath
);
975 TRACE("Parsing new path %s\n",debugstr_w(path
));
977 else if (strncmpiW(ptr
, szExpires
, 8) == 0)
980 ptr
+=strlenW(szExpires
);
981 if (InternetTimeToSystemTimeW(ptr
, &st
, 0))
983 SystemTimeToFileTime(&st
, &expiry
);
985 if (CompareFileTime(&create
,&expiry
) > 0)
987 TRACE("Cookie already expired.\n");
992 else if (strncmpiW(ptr
, szSecure
, 6) == 0)
994 FIXME("secure not handled (%s)\n",debugstr_w(ptr
));
995 ptr
+= strlenW(szSecure
);
997 else if (strncmpiW(ptr
, szHttpOnly
, 8) == 0)
999 if(!(flags
& INTERNET_COOKIE_HTTPONLY
)) {
1000 WARN("HTTP only cookie added without INTERNET_COOKIE_HTTPONLY flag\n");
1002 if (value
!= data
) heap_free(value
);
1003 SetLastError(ERROR_INVALID_OPERATION
);
1004 return COOKIE_STATE_REJECT
;
1007 cookie_flags
|= INTERNET_COOKIE_HTTPONLY
;
1008 ptr
+= strlenW(szHttpOnly
);
1012 FIXME("Unknown additional option %s\n",debugstr_w(ptr
));
1017 EnterCriticalSection(&cookie_cs
);
1019 load_persistent_cookie(domain
, path
);
1021 container
= get_cookie_container(domain
, path
, !expired
);
1024 if (value
!= data
) heap_free(value
);
1025 LeaveCriticalSection(&cookie_cs
);
1026 return COOKIE_STATE_ACCEPT
;
1029 if(!expiry
.dwLowDateTime
&& !expiry
.dwHighDateTime
)
1030 cookie_flags
|= INTERNET_COOKIE_IS_SESSION
;
1032 update_persistent
= TRUE
;
1034 if ((thisCookie
= find_cookie(container
, cookie_name
)))
1036 if ((thisCookie
->flags
& INTERNET_COOKIE_HTTPONLY
) && !(flags
& INTERNET_COOKIE_HTTPONLY
)) {
1037 WARN("An attempt to override httponly cookie\n");
1038 SetLastError(ERROR_INVALID_OPERATION
);
1040 if (value
!= data
) heap_free(value
);
1041 return COOKIE_STATE_REJECT
;
1044 if (!(thisCookie
->flags
& INTERNET_COOKIE_IS_SESSION
))
1045 update_persistent
= TRUE
;
1046 delete_cookie(thisCookie
);
1049 TRACE("setting cookie %s=%s for domain %s path %s\n", debugstr_w(cookie_name
),
1050 debugstr_w(value
), debugstr_w(container
->domain
->domain
),debugstr_w(container
->path
));
1053 cookie_t
*new_cookie
;
1055 new_cookie
= alloc_cookie(cookie_name
, value
, expiry
, create
, cookie_flags
);
1058 if (value
!= data
) heap_free(value
);
1059 LeaveCriticalSection(&cookie_cs
);
1060 return COOKIE_STATE_UNKNOWN
;
1063 add_cookie(container
, new_cookie
);
1066 if (value
!= data
) heap_free(value
);
1068 if (!update_persistent
|| save_persistent_cookie(container
))
1070 LeaveCriticalSection(&cookie_cs
);
1071 return COOKIE_STATE_ACCEPT
;
1073 LeaveCriticalSection(&cookie_cs
);
1074 return COOKIE_STATE_UNKNOWN
;
1077 /***********************************************************************
1078 * InternetSetCookieExW (WININET.@)
1080 * Sets cookie for the specified url
1082 DWORD WINAPI
InternetSetCookieExW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
1083 LPCWSTR lpCookieData
, DWORD flags
, DWORD_PTR reserved
)
1086 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
], path
[INTERNET_MAX_PATH_LENGTH
];
1088 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_w(lpszUrl
), debugstr_w(lpszCookieName
),
1089 debugstr_w(lpCookieData
), flags
, reserved
);
1091 if (flags
& ~INTERNET_COOKIE_HTTPONLY
)
1092 FIXME("flags %x not supported\n", flags
);
1094 if (!lpszUrl
|| !lpCookieData
)
1096 SetLastError(ERROR_INVALID_PARAMETER
);
1097 return COOKIE_STATE_UNKNOWN
;
1101 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
1102 if (!ret
|| !hostName
[0]) return COOKIE_STATE_UNKNOWN
;
1104 if (!lpszCookieName
)
1106 WCHAR
*cookie
, *data
;
1109 cookie
= heap_strdupW(lpCookieData
);
1112 SetLastError(ERROR_OUTOFMEMORY
);
1113 return COOKIE_STATE_UNKNOWN
;
1116 /* some apps (or is it us??) try to add a cookie with no cookie name, but
1117 * the cookie data in the form of name[=data].
1119 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ strlenW(cookie
);
1122 res
= set_cookie(hostName
, path
, cookie
, data
, flags
);
1127 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
, flags
);
1130 /***********************************************************************
1131 * InternetSetCookieW (WININET.@)
1133 * Sets a cookie for the specified URL.
1135 BOOL WINAPI
InternetSetCookieW(const WCHAR
*url
, const WCHAR
*name
, const WCHAR
*data
)
1137 TRACE("(%s, %s, %s)\n", debugstr_w(url
), debugstr_w(name
), debugstr_w(data
));
1139 return InternetSetCookieExW(url
, name
, data
, 0, 0) == COOKIE_STATE_ACCEPT
;
1142 /***********************************************************************
1143 * InternetSetCookieA (WININET.@)
1145 * Sets cookie for the specified url
1152 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
1153 LPCSTR lpCookieData
)
1155 LPWSTR data
, url
, name
;
1158 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
1159 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
1161 url
= heap_strdupAtoW(lpszUrl
);
1162 name
= heap_strdupAtoW(lpszCookieName
);
1163 data
= heap_strdupAtoW(lpCookieData
);
1165 r
= InternetSetCookieW( url
, name
, data
);
1173 /***********************************************************************
1174 * InternetSetCookieExA (WININET.@)
1176 * See InternetSetCookieExW.
1178 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
1179 DWORD dwFlags
, DWORD_PTR dwReserved
)
1181 WCHAR
*data
, *url
, *name
;
1184 TRACE("(%s, %s, %s, %x, %lx)\n", debugstr_a(lpszURL
), debugstr_a(lpszCookieName
),
1185 debugstr_a(lpszCookieData
), dwFlags
, dwReserved
);
1187 url
= heap_strdupAtoW(lpszURL
);
1188 name
= heap_strdupAtoW(lpszCookieName
);
1189 data
= heap_strdupAtoW(lpszCookieData
);
1191 r
= InternetSetCookieExW(url
, name
, data
, dwFlags
, dwReserved
);
1199 /***********************************************************************
1200 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1202 * Clears all per-site decisions about cookies.
1209 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
1215 /***********************************************************************
1216 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1218 * See InternetEnumPerSiteCookieDecisionW.
1220 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1221 ULONG
*pdwDecision
, ULONG dwIndex
)
1223 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1224 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1228 /***********************************************************************
1229 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1231 * Enumerates all per-site decisions about cookies.
1238 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, ULONG
*pcSiteNameSize
,
1239 ULONG
*pdwDecision
, ULONG dwIndex
)
1241 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1242 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
1246 /***********************************************************************
1247 * InternetGetPerSiteCookieDecisionA (WININET.@)
1249 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, ULONG
*pResult
)
1251 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
1255 /***********************************************************************
1256 * InternetGetPerSiteCookieDecisionW (WININET.@)
1258 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, ULONG
*pResult
)
1260 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
1264 /***********************************************************************
1265 * InternetSetPerSiteCookieDecisionA (WININET.@)
1267 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
1269 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
1273 /***********************************************************************
1274 * InternetSetPerSiteCookieDecisionW (WININET.@)
1276 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
1278 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);
1282 void free_cookie(void)
1284 DeleteCriticalSection(&cookie_cs
);