[REACTOS]
[reactos.git] / reactos / dll / win32 / wininet / cookie.c
1 /*
2 * Wininet - cookie handling stuff
3 *
4 * Copyright 2002 TransGaming Technologies Inc.
5 *
6 * David Hammerton
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "internet.h"
24
25 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
26
27 /* FIXME
28 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
29 */
30
31 typedef struct _cookie_domain cookie_domain;
32 typedef struct _cookie cookie;
33
34 struct _cookie
35 {
36 struct list entry;
37
38 struct _cookie_domain *parent;
39
40 LPWSTR lpCookieName;
41 LPWSTR lpCookieData;
42 DWORD flags;
43 FILETIME expiry;
44 FILETIME create;
45 };
46
47 struct _cookie_domain
48 {
49 struct list entry;
50
51 LPWSTR lpCookieDomain;
52 LPWSTR lpCookiePath;
53 struct list cookie_list;
54 };
55
56 static CRITICAL_SECTION cookie_cs;
57 static CRITICAL_SECTION_DEBUG cookie_cs_debug =
58 {
59 0, 0, &cookie_cs,
60 { &cookie_cs_debug.ProcessLocksList, &cookie_cs_debug.ProcessLocksList },
61 0, 0, { (DWORD_PTR)(__FILE__ ": cookie_cs") }
62 };
63 static CRITICAL_SECTION cookie_cs = { &cookie_cs_debug, -1, 0, 0, 0, 0 };
64 static struct list domain_list = LIST_INIT(domain_list);
65
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);
74
75 static BOOL create_cookie_url(LPCWSTR domain, LPCWSTR path, WCHAR *buf, DWORD buf_len)
76 {
77 static const WCHAR cookie_prefix[] = {'C','o','o','k','i','e',':'};
78
79 WCHAR *p;
80 DWORD len;
81
82 if(buf_len < sizeof(cookie_prefix)/sizeof(WCHAR))
83 return FALSE;
84 memcpy(buf, cookie_prefix, sizeof(cookie_prefix));
85 buf += sizeof(cookie_prefix)/sizeof(WCHAR);
86 buf_len -= sizeof(cookie_prefix)/sizeof(WCHAR);
87 p = buf;
88
89 len = buf_len;
90 if(!GetUserNameW(buf, &len))
91 return FALSE;
92 buf += len-1;
93 buf_len -= len-1;
94
95 if(!buf_len)
96 return FALSE;
97 *(buf++) = '@';
98 buf_len--;
99
100 len = strlenW(domain);
101 if(len >= buf_len)
102 return FALSE;
103 memcpy(buf, domain, len*sizeof(WCHAR));
104 buf += len;
105 buf_len -= len;
106
107 len = strlenW(path);
108 if(len >= buf_len)
109 return FALSE;
110 memcpy(buf, path, len*sizeof(WCHAR));
111 buf += len;
112
113 *buf = 0;
114
115 for(; *p; p++)
116 *p = tolowerW(*p);
117 return TRUE;
118 }
119
120 static BOOL load_persistent_cookie(LPCWSTR domain, LPCWSTR path)
121 {
122 INTERNET_CACHE_ENTRY_INFOW *info;
123 cookie_domain *domain_container = NULL;
124 cookie *old_cookie;
125 struct list *iter;
126 WCHAR cookie_url[MAX_PATH];
127 HANDLE cookie;
128 char *str = NULL, *pbeg, *pend;
129 DWORD size, flags;
130 WCHAR *name, *data;
131 FILETIME expiry, create, time;
132
133 if (!create_cookie_url(domain, path, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
134 return FALSE;
135
136 size = 0;
137 RetrieveUrlCacheEntryStreamW(cookie_url, NULL, &size, FALSE, 0);
138 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
139 return TRUE;
140 info = heap_alloc(size);
141 if(!info)
142 return FALSE;
143 cookie = RetrieveUrlCacheEntryStreamW(cookie_url, info, &size, FALSE, 0);
144 size = info->dwSizeLow;
145 heap_free(info);
146 if(!cookie)
147 return FALSE;
148
149 if(!(str = heap_alloc(size+1)) || !ReadUrlCacheEntryStream(cookie, 0, str, &size, 0)) {
150 UnlockUrlCacheEntryStream(cookie, 0);
151 heap_free(str);
152 return FALSE;
153 }
154 str[size] = 0;
155 UnlockUrlCacheEntryStream(cookie, 0);
156
157 LIST_FOR_EACH(iter, &domain_list)
158 {
159 domain_container = LIST_ENTRY(iter, cookie_domain, entry);
160 if(COOKIE_matchDomain(domain, path, domain_container, FALSE))
161 break;
162 domain_container = NULL;
163 }
164 if(!domain_container)
165 domain_container = COOKIE_addDomain(domain, path);
166 if(!domain_container) {
167 heap_free(str);
168 return FALSE;
169 }
170
171 GetSystemTimeAsFileTime(&time);
172 for(pbeg=str; pbeg && *pbeg; name=data=NULL) {
173 pend = strchr(pbeg, '\n');
174 if(!pend)
175 break;
176 *pend = 0;
177 name = heap_strdupAtoW(pbeg);
178
179 pbeg = pend+1;
180 pend = strchr(pbeg, '\n');
181 if(!pend)
182 break;
183 *pend = 0;
184 data = heap_strdupAtoW(pbeg);
185
186 pbeg = pend+1;
187 pbeg = strchr(pend+1, '\n');
188 if(!pbeg)
189 break;
190 sscanf(pbeg, "%u %u %u %u %u", &flags, &expiry.dwLowDateTime, &expiry.dwHighDateTime,
191 &create.dwLowDateTime, &create.dwHighDateTime);
192
193 /* skip "*\n" */
194 pbeg = strchr(pbeg, '*');
195 if(pbeg) {
196 pbeg++;
197 if(*pbeg)
198 pbeg++;
199 }
200
201 if(!name || !data)
202 break;
203
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);
208 }
209 heap_free(name);
210 heap_free(data);
211 }
212 heap_free(str);
213 heap_free(name);
214 heap_free(data);
215
216 return TRUE;
217 }
218
219 static BOOL save_persistent_cookie(cookie_domain *domain)
220 {
221 static const WCHAR txtW[] = {'t','x','t',0};
222
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;
228 FILETIME time;
229 DWORD dwBytesWritten;
230
231 if (!create_cookie_url(domain->lpCookieDomain, domain->lpCookiePath, cookie_url, sizeof(cookie_url)/sizeof(cookie_url[0])))
232 return FALSE;
233
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)
237 {
238 if((cookie_container->expiry.dwLowDateTime || cookie_container->expiry.dwHighDateTime)
239 && CompareFileTime(&time, &cookie_container->expiry) > 0) {
240 COOKIE_deleteCookie(cookie_container, FALSE);
241 continue;
242 }
243
244 if(!(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)) {
245 do_save = TRUE;
246 break;
247 }
248 }
249 if(!do_save) {
250 DeleteUrlCacheEntryW(cookie_url);
251 return TRUE;
252 }
253
254 if(!CreateUrlCacheEntryW(cookie_url, 0, txtW, cookie_file, 0))
255 return FALSE;
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);
259 return FALSE;
260 }
261
262 LIST_FOR_EACH_ENTRY(cookie_container, &domain->cookie_list, cookie, entry)
263 {
264 if(cookie_container->flags & INTERNET_COOKIE_IS_SESSION)
265 continue;
266
267 dyn_buf = heap_strdupWtoA(cookie_container->lpCookieName);
268 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &dwBytesWritten, NULL)) {
269 heap_free(dyn_buf);
270 do_save = FALSE;
271 break;
272 }
273 heap_free(dyn_buf);
274 if(!WriteFile(cookie_handle, "\n", 1, &dwBytesWritten, NULL)) {
275 do_save = FALSE;
276 break;
277 }
278
279 dyn_buf = heap_strdupWtoA(cookie_container->lpCookieData);
280 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &dwBytesWritten, NULL)) {
281 heap_free(dyn_buf);
282 do_save = FALSE;
283 break;
284 }
285 heap_free(dyn_buf);
286 if(!WriteFile(cookie_handle, "\n", 1, &dwBytesWritten, NULL)) {
287 do_save = FALSE;
288 break;
289 }
290
291 dyn_buf = heap_strdupWtoA(domain->lpCookieDomain);
292 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &dwBytesWritten, NULL)) {
293 heap_free(dyn_buf);
294 do_save = FALSE;
295 break;
296 }
297 heap_free(dyn_buf);
298
299 dyn_buf = heap_strdupWtoA(domain->lpCookiePath);
300 if(!dyn_buf || !WriteFile(cookie_handle, dyn_buf, strlen(dyn_buf), &dwBytesWritten, NULL)) {
301 heap_free(dyn_buf);
302 do_save = FALSE;
303 break;
304 }
305 heap_free(dyn_buf);
306
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)) {
311 do_save = FALSE;
312 break;
313 }
314 }
315
316 CloseHandle(cookie_handle);
317 if(!do_save) {
318 ERR("error saving cookie file\n");
319 DeleteFileW(cookie_file);
320 return FALSE;
321 }
322
323 memset(&time, 0, sizeof(time));
324 return CommitUrlCacheEntryW(cookie_url, cookie_file, time, time, 0, NULL, 0, txtW, 0);
325 }
326
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)
330 {
331 cookie *newCookie = heap_alloc(sizeof(cookie));
332 if (!newCookie)
333 return NULL;
334
335 newCookie->lpCookieName = heap_strdupW(name);
336 newCookie->lpCookieData = heap_strdupW(data);
337
338 if (!newCookie->lpCookieName || !newCookie->lpCookieData)
339 {
340 heap_free(newCookie->lpCookieName);
341 heap_free(newCookie->lpCookieData);
342 heap_free(newCookie);
343
344 return NULL;
345 }
346
347 newCookie->flags = flags;
348 newCookie->expiry = expiry;
349 newCookie->create = create;
350
351 TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
352
353 list_add_tail(&domain->cookie_list, &newCookie->entry);
354 newCookie->parent = domain;
355 return newCookie;
356 }
357
358
359 /* finds a cookie in the domain matching the cookie name */
360 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
361 {
362 struct list * cursor;
363 TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
364
365 LIST_FOR_EACH(cursor, &domain->cookie_list)
366 {
367 cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
368 BOOL candidate = TRUE;
369 if (candidate && lpszCookieName)
370 {
371 if (candidate && !searchCookie->lpCookieName)
372 candidate = FALSE;
373 if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
374 candidate = FALSE;
375 }
376 if (candidate)
377 return searchCookie;
378 }
379 return NULL;
380 }
381
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)
384 {
385 heap_free(deadCookie->lpCookieName);
386 heap_free(deadCookie->lpCookieData);
387 list_remove(&deadCookie->entry);
388
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);
393 }
394
395 /* allocates a domain and adds it to the end */
396 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
397 {
398 cookie_domain *newDomain = heap_alloc(sizeof(cookie_domain));
399
400 list_init(&newDomain->entry);
401 list_init(&newDomain->cookie_list);
402 newDomain->lpCookieDomain = heap_strdupW(domain);
403 newDomain->lpCookiePath = heap_strdupW(path);
404
405 list_add_tail(&domain_list, &newDomain->entry);
406
407 TRACE("Adding domain: %p\n", newDomain);
408 return newDomain;
409 }
410
411 static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
412 {
413 URL_COMPONENTSW UrlComponents;
414
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;
427
428 if (!InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents)) return FALSE;
429
430 /* discard the webpage off the end of the path */
431 if (UrlComponents.dwUrlPathLength)
432 {
433 if (path[UrlComponents.dwUrlPathLength - 1] != '/')
434 {
435 WCHAR *ptr;
436 if ((ptr = strrchrW(path, '/'))) *(++ptr) = 0;
437 else
438 {
439 path[0] = '/';
440 path[1] = 0;
441 }
442 }
443 }
444 else if (pathLen >= 2)
445 {
446 path[0] = '/';
447 path[1] = 0;
448 }
449 return TRUE;
450 }
451
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)
455 {
456 TRACE("searching on domain %p\n", searchDomain);
457 if (lpszCookieDomain)
458 {
459 if (!searchDomain->lpCookieDomain)
460 return FALSE;
461
462 TRACE("comparing domain %s with %s\n",
463 debugstr_w(lpszCookieDomain),
464 debugstr_w(searchDomain->lpCookieDomain));
465
466 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
467 return FALSE;
468 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
469 return FALSE;
470 }
471 if (lpszCookiePath)
472 {
473 INT len;
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
477 */
478 if (!searchDomain->lpCookiePath)
479 return FALSE;
480 if (allow_partial)
481 {
482 len = lstrlenW(searchDomain->lpCookiePath);
483 if (strncmpiW(searchDomain->lpCookiePath, lpszCookiePath, len)!=0)
484 return FALSE;
485 }
486 else if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
487 return FALSE;
488
489 }
490 return TRUE;
491 }
492
493 /* remove a domain from the list and delete it */
494 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
495 {
496 struct list * cursor;
497 while ((cursor = list_tail(&deadDomain->cookie_list)))
498 {
499 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
500 list_remove(cursor);
501 }
502 heap_free(deadDomain->lpCookieDomain);
503 heap_free(deadDomain->lpCookiePath);
504
505 list_remove(&deadDomain->entry);
506
507 heap_free(deadDomain);
508 }
509
510 DWORD get_cookie(const WCHAR *host, const WCHAR *path, WCHAR *cookie_data, DWORD *size)
511 {
512 static const WCHAR empty_path[] = { '/',0 };
513
514 unsigned cnt = 0, len, name_len, domain_count = 0, cookie_count = 0;
515 WCHAR *ptr, subpath[INTERNET_MAX_PATH_LENGTH];
516 const WCHAR *p;
517 cookie_domain *domain;
518 FILETIME tm;
519
520 GetSystemTimeAsFileTime(&tm);
521
522 EnterCriticalSection(&cookie_cs);
523
524 len = strlenW(host);
525 p = host+len;
526 while(p>host && p[-1]!='.') p--;
527 while(p != host) {
528 p--;
529 while(p>host && p[-1]!='.') p--;
530 if(p == host) break;
531
532 load_persistent_cookie(p, empty_path);
533 }
534
535 len = strlenW(path);
536 assert(len+1 < INTERNET_MAX_PATH_LENGTH);
537 memcpy(subpath, path, (len+1)*sizeof(WCHAR));
538 ptr = subpath+len;
539 do {
540 *ptr = 0;
541 load_persistent_cookie(host, subpath);
542
543 ptr--;
544 while(ptr>subpath && ptr[-1]!='/') ptr--;
545 }while(ptr != subpath);
546
547 ptr = cookie_data;
548 LIST_FOR_EACH_ENTRY(domain, &domain_list, cookie_domain, entry) {
549 struct list *cursor, *cursor2;
550
551 if(!COOKIE_matchDomain(host, path, domain, TRUE))
552 continue;
553
554 domain_count++;
555 TRACE("found domain %p\n", domain);
556
557 LIST_FOR_EACH_SAFE(cursor, cursor2, &domain->cookie_list) {
558 cookie *cookie_iter = LIST_ENTRY(cursor, cookie, entry);
559
560 /* check for expiry */
561 if((cookie_iter->expiry.dwLowDateTime != 0 || cookie_iter->expiry.dwHighDateTime != 0)
562 && CompareFileTime(&tm, &cookie_iter->expiry) > 0)
563 {
564 TRACE("Found expired cookie. deleting\n");
565 COOKIE_deleteCookie(cookie_iter, FALSE);
566 continue;
567 }
568
569 if (cookie_count)
570 cnt += 2; /* '; ' */
571 cnt += name_len = strlenW(cookie_iter->lpCookieName);
572 if ((len = strlenW(cookie_iter->lpCookieData))) {
573 cnt += 1; /* = */
574 cnt += len;
575 }
576
577 if(ptr) {
578 if(*size > cnt) {
579 if(cookie_count) {
580 *ptr++ = ';';
581 *ptr++ = ' ';
582 }
583
584 memcpy(ptr, cookie_iter->lpCookieName, name_len*sizeof(WCHAR));
585 ptr += name_len;
586
587 if(len) {
588 *ptr++ = '=';
589 memcpy(ptr, cookie_iter->lpCookieData, len*sizeof(WCHAR));
590 ptr += len;
591 }
592
593 assert(cookie_data+cnt == ptr);
594 TRACE("Cookie: %s\n", debugstr_wn(cookie_data, cnt));
595 }else {
596 /* Stop writing data, just compute the size */
597 ptr = NULL;
598 }
599 }
600
601 cookie_count++;
602 }
603 }
604
605 LeaveCriticalSection(&cookie_cs);
606
607 if(ptr)
608 *ptr = 0;
609
610 if (!cnt) {
611 TRACE("no cookies found for %s\n", debugstr_w(host));
612 return ERROR_NO_MORE_ITEMS;
613 }
614
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;
619 }
620
621 *size = cnt + 1;
622
623 TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count, debugstr_w(cookie_data));
624 return ERROR_SUCCESS;
625 }
626
627 /***********************************************************************
628 * InternetGetCookieW (WININET.@)
629 *
630 * Retrieve cookie from the specified url
631 *
632 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
633 * So it won't be implemented here.
634 *
635 * RETURNS
636 * TRUE on success
637 * FALSE on failure
638 *
639 */
640 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
641 LPWSTR lpCookieData, LPDWORD lpdwSize)
642 {
643 WCHAR host[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
644 DWORD res;
645 BOOL ret;
646
647 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), lpCookieData, lpdwSize);
648
649 if (!lpszUrl)
650 {
651 SetLastError(ERROR_INVALID_PARAMETER);
652 return FALSE;
653 }
654
655 host[0] = 0;
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);
659 return FALSE;
660 }
661
662 res = get_cookie(host, path, lpCookieData, lpdwSize);
663 if(res != ERROR_SUCCESS)
664 SetLastError(res);
665 return res == ERROR_SUCCESS;
666 }
667
668
669 /***********************************************************************
670 * InternetGetCookieA (WININET.@)
671 *
672 * Retrieve cookie from the specified url
673 *
674 * RETURNS
675 * TRUE on success
676 * FALSE on failure
677 *
678 */
679 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
680 LPSTR lpCookieData, LPDWORD lpdwSize)
681 {
682 WCHAR *url, *name;
683 DWORD len, size;
684 BOOL r;
685
686 TRACE("(%s %s %p %p(%u))\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
687 lpCookieData, lpdwSize, lpdwSize ? *lpdwSize : 0);
688
689 url = heap_strdupAtoW(lpszUrl);
690 name = heap_strdupAtoW(lpszCookieName);
691
692 r = InternetGetCookieW( url, name, NULL, &len );
693 if( r )
694 {
695 WCHAR *szCookieData;
696
697 szCookieData = heap_alloc(len * sizeof(WCHAR));
698 if( !szCookieData )
699 {
700 r = FALSE;
701 }
702 else
703 {
704 r = InternetGetCookieW( url, name, szCookieData, &len );
705
706 if(r) {
707 size = WideCharToMultiByte( CP_ACP, 0, szCookieData, len, NULL, 0, NULL, NULL);
708 if(lpCookieData) {
709 if(*lpdwSize >= size) {
710 WideCharToMultiByte( CP_ACP, 0, szCookieData, len, lpCookieData, *lpdwSize, NULL, NULL);
711 }else {
712 SetLastError(ERROR_INSUFFICIENT_BUFFER);
713 r = FALSE;
714 }
715 }
716 *lpdwSize = size;
717 }
718
719 heap_free( szCookieData );
720 }
721 }
722 heap_free( name );
723 heap_free( url );
724 return r;
725 }
726
727
728 /***********************************************************************
729 * IsDomainLegalCookieDomainW (WININET.@)
730 */
731 BOOL WINAPI IsDomainLegalCookieDomainW( LPCWSTR s1, LPCWSTR s2 )
732 {
733 DWORD s1_len, s2_len;
734
735 FIXME("(%s, %s) semi-stub\n", debugstr_w(s1), debugstr_w(s2));
736
737 if (!s1 || !s2)
738 {
739 SetLastError(ERROR_INVALID_PARAMETER);
740 return FALSE;
741 }
742 if (s1[0] == '.' || !s1[0] || s2[0] == '.' || !s2[0])
743 {
744 SetLastError(ERROR_INVALID_NAME);
745 return FALSE;
746 }
747 if(!strchrW(s1, '.') || !strchrW(s2, '.'))
748 return FALSE;
749
750 s1_len = strlenW(s1);
751 s2_len = strlenW(s2);
752 if (s1_len > s2_len)
753 return FALSE;
754
755 if (strncmpiW(s1, s2+s2_len-s1_len, s1_len) || (s2_len>s1_len && s2[s2_len-s1_len-1]!='.'))
756 {
757 SetLastError(ERROR_INVALID_PARAMETER);
758 return FALSE;
759 }
760
761 return TRUE;
762 }
763
764 BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cookie_data)
765 {
766 cookie_domain *thisCookieDomain = NULL;
767 cookie *thisCookie;
768 struct list *cursor;
769 LPWSTR data, value;
770 WCHAR *ptr;
771 FILETIME expiry, create;
772 BOOL expired = FALSE, update_persistent = FALSE;
773 DWORD flags = 0;
774
775 value = data = heap_strdupW(cookie_data);
776 if (!data)
777 {
778 ERR("could not allocate the cookie data buffer\n");
779 return FALSE;
780 }
781
782 memset(&expiry,0,sizeof(expiry));
783 GetSystemTimeAsFileTime(&create);
784
785 /* lots of information can be parsed out of the cookie value */
786
787 ptr = data;
788 for (;;)
789 {
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};
795
796 if (!(ptr = strchrW(ptr,';'))) break;
797 *ptr++ = 0;
798
799 if (value != data) heap_free(value);
800 value = heap_alloc((ptr - data) * sizeof(WCHAR));
801 if (value == NULL)
802 {
803 heap_free(data);
804 ERR("could not allocate the cookie value buffer\n");
805 return FALSE;
806 }
807 strcpyW(value, data);
808
809 while (*ptr == ' ') ptr++; /* whitespace */
810
811 if (strncmpiW(ptr, szDomain, 7) == 0)
812 {
813 WCHAR *end_ptr;
814
815 ptr += sizeof(szDomain)/sizeof(szDomain[0])-1;
816 if(*ptr == '.')
817 ptr++;
818 end_ptr = strchrW(ptr, ';');
819 if(end_ptr)
820 *end_ptr = 0;
821
822 if(!IsDomainLegalCookieDomainW(ptr, domain))
823 {
824 if(value != data)
825 heap_free(value);
826 heap_free(data);
827 return FALSE;
828 }
829
830 if(end_ptr)
831 *end_ptr = ';';
832
833 domain = ptr;
834 TRACE("Parsing new domain %s\n",debugstr_w(domain));
835 }
836 else if (strncmpiW(ptr, szPath, 5) == 0)
837 {
838 ptr+=strlenW(szPath);
839 path = ptr;
840 TRACE("Parsing new path %s\n",debugstr_w(path));
841 }
842 else if (strncmpiW(ptr, szExpires, 8) == 0)
843 {
844 SYSTEMTIME st;
845 ptr+=strlenW(szExpires);
846 if (InternetTimeToSystemTimeW(ptr, &st, 0))
847 {
848 SystemTimeToFileTime(&st, &expiry);
849
850 if (CompareFileTime(&create,&expiry) > 0)
851 {
852 TRACE("Cookie already expired.\n");
853 expired = TRUE;
854 }
855 }
856 }
857 else if (strncmpiW(ptr, szSecure, 6) == 0)
858 {
859 FIXME("secure not handled (%s)\n",debugstr_w(ptr));
860 ptr += strlenW(szSecure);
861 }
862 else if (strncmpiW(ptr, szHttpOnly, 8) == 0)
863 {
864 FIXME("httponly not handled (%s)\n",debugstr_w(ptr));
865 ptr += strlenW(szHttpOnly);
866 }
867 else if (*ptr)
868 {
869 FIXME("Unknown additional option %s\n",debugstr_w(ptr));
870 break;
871 }
872 }
873
874 EnterCriticalSection(&cookie_cs);
875
876 load_persistent_cookie(domain, path);
877
878 LIST_FOR_EACH(cursor, &domain_list)
879 {
880 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
881 if (COOKIE_matchDomain(domain, path, thisCookieDomain, FALSE))
882 break;
883 thisCookieDomain = NULL;
884 }
885
886 if (!thisCookieDomain)
887 {
888 if (!expired)
889 thisCookieDomain = COOKIE_addDomain(domain, path);
890 else
891 {
892 heap_free(data);
893 if (value != data) heap_free(value);
894 LeaveCriticalSection(&cookie_cs);
895 return TRUE;
896 }
897 }
898
899 if(!expiry.dwLowDateTime && !expiry.dwHighDateTime)
900 flags |= INTERNET_COOKIE_IS_SESSION;
901 else
902 update_persistent = TRUE;
903
904 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, cookie_name)))
905 {
906 if (!(thisCookie->flags & INTERNET_COOKIE_IS_SESSION))
907 update_persistent = TRUE;
908 COOKIE_deleteCookie(thisCookie, FALSE);
909 }
910
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));
913
914 if (!expired && !COOKIE_addCookie(thisCookieDomain, cookie_name, value, expiry, create, flags))
915 {
916 heap_free(data);
917 if (value != data) heap_free(value);
918 LeaveCriticalSection(&cookie_cs);
919 return FALSE;
920 }
921 heap_free(data);
922 if (value != data) heap_free(value);
923
924 if (!update_persistent || save_persistent_cookie(thisCookieDomain))
925 {
926 LeaveCriticalSection(&cookie_cs);
927 return TRUE;
928 }
929 LeaveCriticalSection(&cookie_cs);
930 return FALSE;
931 }
932
933 /***********************************************************************
934 * InternetSetCookieW (WININET.@)
935 *
936 * Sets cookie for the specified url
937 *
938 * RETURNS
939 * TRUE on success
940 * FALSE on failure
941 *
942 */
943 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
944 LPCWSTR lpCookieData)
945 {
946 BOOL ret;
947 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
948
949 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
950 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
951
952 if (!lpszUrl || !lpCookieData)
953 {
954 SetLastError(ERROR_INVALID_PARAMETER);
955 return FALSE;
956 }
957
958 hostName[0] = 0;
959 ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
960 if (!ret || !hostName[0]) return FALSE;
961
962 if (!lpszCookieName)
963 {
964 WCHAR *cookie, *data;
965
966 cookie = heap_strdupW(lpCookieData);
967 if (!cookie)
968 {
969 SetLastError(ERROR_OUTOFMEMORY);
970 return FALSE;
971 }
972
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].
975 */
976 if (!(data = strchrW(cookie, '='))) data = cookie + strlenW(cookie);
977 else *data++ = 0;
978
979 ret = set_cookie(hostName, path, cookie, data);
980
981 heap_free(cookie);
982 return ret;
983 }
984 return set_cookie(hostName, path, lpszCookieName, lpCookieData);
985 }
986
987
988 /***********************************************************************
989 * InternetSetCookieA (WININET.@)
990 *
991 * Sets cookie for the specified url
992 *
993 * RETURNS
994 * TRUE on success
995 * FALSE on failure
996 *
997 */
998 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
999 LPCSTR lpCookieData)
1000 {
1001 LPWSTR data, url, name;
1002 BOOL r;
1003
1004 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
1005 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
1006
1007 url = heap_strdupAtoW(lpszUrl);
1008 name = heap_strdupAtoW(lpszCookieName);
1009 data = heap_strdupAtoW(lpCookieData);
1010
1011 r = InternetSetCookieW( url, name, data );
1012
1013 heap_free( data );
1014 heap_free( name );
1015 heap_free( url );
1016 return r;
1017 }
1018
1019 /***********************************************************************
1020 * InternetSetCookieExA (WININET.@)
1021 *
1022 * See InternetSetCookieExW.
1023 */
1024 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
1025 DWORD dwFlags, DWORD_PTR dwReserved)
1026 {
1027 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1028 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
1029 dwFlags, dwReserved);
1030
1031 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1032 return InternetSetCookieA(lpszURL, lpszCookieName, lpszCookieData);
1033 }
1034
1035 /***********************************************************************
1036 * InternetSetCookieExW (WININET.@)
1037 *
1038 * Sets a cookie for the specified URL.
1039 *
1040 * RETURNS
1041 * TRUE on success
1042 * FALSE on failure
1043 *
1044 */
1045 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
1046 DWORD dwFlags, DWORD_PTR dwReserved)
1047 {
1048 TRACE("(%s, %s, %s, 0x%08x, 0x%08lx)\n",
1049 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
1050 dwFlags, dwReserved);
1051
1052 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1053 return InternetSetCookieW(lpszURL, lpszCookieName, lpszCookieData);
1054 }
1055
1056 /***********************************************************************
1057 * InternetGetCookieExA (WININET.@)
1058 *
1059 * See InternetGetCookieExW.
1060 */
1061 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
1062 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
1063 {
1064 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1065 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
1066 pcchCookieData, dwFlags, lpReserved);
1067
1068 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1069 return InternetGetCookieA(pchURL, pchCookieName, pchCookieData, pcchCookieData);
1070 }
1071
1072 /***********************************************************************
1073 * InternetGetCookieExW (WININET.@)
1074 *
1075 * Retrieve cookie for the specified URL.
1076 *
1077 * RETURNS
1078 * TRUE on success
1079 * FALSE on failure
1080 *
1081 */
1082 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
1083 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
1084 {
1085 TRACE("(%s, %s, %s, %p, 0x%08x, %p)\n",
1086 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
1087 pcchCookieData, dwFlags, lpReserved);
1088
1089 if (dwFlags) FIXME("flags 0x%08x not supported\n", dwFlags);
1090 return InternetGetCookieW(pchURL, pchCookieName, pchCookieData, pcchCookieData);
1091 }
1092
1093 /***********************************************************************
1094 * InternetClearAllPerSiteCookieDecisions (WININET.@)
1095 *
1096 * Clears all per-site decisions about cookies.
1097 *
1098 * RETURNS
1099 * TRUE on success
1100 * FALSE on failure
1101 *
1102 */
1103 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
1104 {
1105 FIXME("stub\n");
1106 return TRUE;
1107 }
1108
1109 /***********************************************************************
1110 * InternetEnumPerSiteCookieDecisionA (WININET.@)
1111 *
1112 * See InternetEnumPerSiteCookieDecisionW.
1113 */
1114 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, ULONG *pcSiteNameSize,
1115 ULONG *pdwDecision, ULONG dwIndex )
1116 {
1117 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1118 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1119 return FALSE;
1120 }
1121
1122 /***********************************************************************
1123 * InternetEnumPerSiteCookieDecisionW (WININET.@)
1124 *
1125 * Enumerates all per-site decisions about cookies.
1126 *
1127 * RETURNS
1128 * TRUE on success
1129 * FALSE on failure
1130 *
1131 */
1132 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, ULONG *pcSiteNameSize,
1133 ULONG *pdwDecision, ULONG dwIndex )
1134 {
1135 FIXME("(%s, %p, %p, 0x%08x) stub\n",
1136 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
1137 return FALSE;
1138 }
1139
1140 /***********************************************************************
1141 * InternetGetPerSiteCookieDecisionA (WININET.@)
1142 */
1143 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, ULONG *pResult )
1144 {
1145 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
1146 return FALSE;
1147 }
1148
1149 /***********************************************************************
1150 * InternetGetPerSiteCookieDecisionW (WININET.@)
1151 */
1152 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, ULONG *pResult )
1153 {
1154 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
1155 return FALSE;
1156 }
1157
1158 /***********************************************************************
1159 * InternetSetPerSiteCookieDecisionA (WININET.@)
1160 */
1161 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
1162 {
1163 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
1164 return FALSE;
1165 }
1166
1167 /***********************************************************************
1168 * InternetSetPerSiteCookieDecisionW (WININET.@)
1169 */
1170 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
1171 {
1172 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
1173 return FALSE;
1174 }
1175
1176 void free_cookie(void)
1177 {
1178 DeleteCriticalSection(&cookie_cs);
1179 }