crypt32: Use set encoding function for encoding PKCS signed info.
[wine/multimedia.git] / dlls / wininet / cookie.c
blobb479aa7372c96b7bca8cfc68650789a7cc66a333
1 /*
2 * Wininet - cookie handling stuff
4 * Copyright 2002 TransGaming Technologies Inc.
6 * David Hammerton
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wininet.h"
37 #include "winerror.h"
39 #include "wine/debug.h"
40 #include "internet.h"
42 #include "wine/list.h"
44 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
47 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
49 /* FIXME
50 * Cookies are currently memory only.
51 * Cookies are NOT THREAD SAFE
52 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
53 * Cookies should care about the expiry time
56 typedef struct _cookie_domain cookie_domain;
57 typedef struct _cookie cookie;
59 struct _cookie
61 struct list entry;
63 struct _cookie_domain *parent;
65 LPWSTR lpCookieName;
66 LPWSTR lpCookieData;
67 time_t expiry; /* FIXME: not used */
70 struct _cookie_domain
72 struct list entry;
74 LPWSTR lpCookieDomain;
75 LPWSTR lpCookiePath;
76 struct list cookie_list;
79 static struct list domain_list = LIST_INIT(domain_list);
81 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data);
82 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName);
83 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain);
84 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path);
85 static void COOKIE_deleteDomain(cookie_domain *deadDomain);
88 /* adds a cookie to the domain */
89 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data)
91 cookie *newCookie = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie));
93 list_init(&newCookie->entry);
94 newCookie->lpCookieName = NULL;
95 newCookie->lpCookieData = NULL;
97 if (name)
99 newCookie->lpCookieName = HeapAlloc(GetProcessHeap(), 0, (strlenW(name) + 1)*sizeof(WCHAR));
100 lstrcpyW(newCookie->lpCookieName, name);
102 if (data)
104 newCookie->lpCookieData = HeapAlloc(GetProcessHeap(), 0, (strlenW(data) + 1)*sizeof(WCHAR));
105 lstrcpyW(newCookie->lpCookieData, data);
108 TRACE("added cookie %p (data is %s)\n", newCookie, debugstr_w(data) );
110 list_add_tail(&domain->cookie_list, &newCookie->entry);
111 newCookie->parent = domain;
112 return newCookie;
116 /* finds a cookie in the domain matching the cookie name */
117 static cookie *COOKIE_findCookie(cookie_domain *domain, LPCWSTR lpszCookieName)
119 struct list * cursor;
120 TRACE("(%p, %s)\n", domain, debugstr_w(lpszCookieName));
122 LIST_FOR_EACH(cursor, &domain->cookie_list)
124 cookie *searchCookie = LIST_ENTRY(cursor, cookie, entry);
125 BOOL candidate = TRUE;
126 if (candidate && lpszCookieName)
128 if (candidate && !searchCookie->lpCookieName)
129 candidate = FALSE;
130 if (candidate && strcmpW(lpszCookieName, searchCookie->lpCookieName) != 0)
131 candidate = FALSE;
133 if (candidate)
134 return searchCookie;
136 return NULL;
139 /* removes a cookie from the list, if its the last cookie we also remove the domain */
140 static void COOKIE_deleteCookie(cookie *deadCookie, BOOL deleteDomain)
142 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
143 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
144 list_remove(&deadCookie->entry);
146 /* special case: last cookie, lets remove the domain to save memory */
147 if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
148 COOKIE_deleteDomain(deadCookie->parent);
149 HeapFree(GetProcessHeap(), 0, deadCookie);
152 /* allocates a domain and adds it to the end */
153 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
155 cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
157 list_init(&newDomain->entry);
158 list_init(&newDomain->cookie_list);
159 newDomain->lpCookieDomain = NULL;
160 newDomain->lpCookiePath = NULL;
162 if (domain)
164 newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
165 strcpyW(newDomain->lpCookieDomain, domain);
167 if (path)
169 newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
170 lstrcpyW(newDomain->lpCookiePath, path);
173 list_add_tail(&domain_list, &newDomain->entry);
175 TRACE("Adding domain: %p\n", newDomain);
176 return newDomain;
179 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
181 URL_COMPONENTSW UrlComponents;
183 UrlComponents.lpszExtraInfo = NULL;
184 UrlComponents.lpszPassword = NULL;
185 UrlComponents.lpszScheme = NULL;
186 UrlComponents.lpszUrlPath = path;
187 UrlComponents.lpszUserName = NULL;
188 UrlComponents.lpszHostName = hostName;
189 UrlComponents.dwHostNameLength = hostNameLen;
190 UrlComponents.dwUrlPathLength = pathLen;
192 InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
195 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
196 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
197 cookie_domain *searchDomain, BOOL allow_partial)
199 TRACE("searching on domain %p\n", searchDomain);
200 if (lpszCookieDomain)
202 if (!searchDomain->lpCookieDomain)
203 return FALSE;
205 TRACE("comparing domain %s with %s\n",
206 debugstr_w(lpszCookieDomain),
207 debugstr_w(searchDomain->lpCookieDomain));
209 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
210 return FALSE;
211 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
212 return FALSE;
214 if (lpszCookiePath)
216 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
217 if (!searchDomain->lpCookiePath)
218 return FALSE;
219 if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
220 return FALSE;
222 return TRUE;
225 /* remove a domain from the list and delete it */
226 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
228 struct list * cursor;
229 while ((cursor = list_tail(&deadDomain->cookie_list)))
231 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
232 list_remove(cursor);
235 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
236 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
238 list_remove(&deadDomain->entry);
240 HeapFree(GetProcessHeap(), 0, deadDomain);
243 /***********************************************************************
244 * InternetGetCookieW (WININET.@)
246 * Retrieve cookie from the specified url
248 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
249 * So it won't be implemented here.
251 * RETURNS
252 * TRUE on success
253 * FALSE on failure
256 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
257 LPWSTR lpCookieData, LPDWORD lpdwSize)
259 struct list * cursor;
260 int cnt = 0, domain_count = 0;
261 int cookie_count = 0;
262 WCHAR hostName[2048], path[2048];
264 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
265 lpCookieData, lpdwSize);
267 if (!lpszUrl)
269 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
270 return FALSE;
273 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
275 LIST_FOR_EACH(cursor, &domain_list)
277 cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
278 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, cookiesDomain, TRUE))
280 struct list * cursor;
281 domain_count++;
282 TRACE("found domain %p\n", cookiesDomain);
284 LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
286 cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
287 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
289 if (cookie_count != 0)
290 cnt += 2; /* '; ' */
291 cnt += strlenW(thisCookie->lpCookieName);
292 cnt += 1; /* = */
293 cnt += strlenW(thisCookie->lpCookieData);
295 else
297 static const WCHAR szsc[] = { ';',' ',0 };
298 static const WCHAR szpseq[] = { '%','s','=','%','s',0 };
299 if (cookie_count != 0)
300 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
301 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szpseq,
302 thisCookie->lpCookieName,
303 thisCookie->lpCookieData);
304 TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie->lpCookieName), debugstr_w(thisCookie->lpCookieData));
306 cookie_count++;
311 if (!domain_count)
313 TRACE("no cookies found for %s\n", debugstr_w(hostName));
314 SetLastError(ERROR_NO_MORE_ITEMS);
315 return FALSE;
318 if (lpCookieData == NULL)
320 cnt += 1; /* NULL */
321 *lpdwSize = cnt*sizeof(WCHAR);
322 TRACE("returning\n");
323 return TRUE;
326 *lpdwSize = (cnt + 1)*sizeof(WCHAR);
328 TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count,
329 debugstr_w(lpCookieData));
331 return (cnt ? TRUE : FALSE);
335 /***********************************************************************
336 * InternetGetCookieA (WININET.@)
338 * Retrieve cookie from the specified url
340 * RETURNS
341 * TRUE on success
342 * FALSE on failure
345 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
346 LPSTR lpCookieData, LPDWORD lpdwSize)
348 DWORD len;
349 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
350 BOOL r;
352 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
353 lpCookieData);
355 if( lpszUrl )
357 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
358 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
359 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
362 if( lpszCookieName )
364 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
365 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
366 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
369 r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
370 if( r )
372 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
373 if( !szCookieData )
374 return FALSE;
376 r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
378 *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
379 lpCookieData, *lpdwSize, NULL, NULL );
382 HeapFree( GetProcessHeap(), 0, szCookieData );
383 HeapFree( GetProcessHeap(), 0, szCookieName );
384 HeapFree( GetProcessHeap(), 0, szUrl );
386 return r;
390 /***********************************************************************
391 * InternetSetCookieW (WININET.@)
393 * Sets cookie for the specified url
395 * RETURNS
396 * TRUE on success
397 * FALSE on failure
400 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
401 LPCWSTR lpCookieData)
403 cookie_domain *thisCookieDomain = NULL;
404 cookie *thisCookie;
405 WCHAR hostName[2048], path[2048];
406 struct list * cursor;
408 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
409 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
411 if (!lpCookieData)
413 SetLastError(ERROR_INVALID_PARAMETER);
414 return FALSE;
416 if (!lpszCookieName)
418 /* some apps (or is it us??) try to add a cookie with no cookie name, but
419 * the cookie data in the form of name=data. */
420 /* FIXME, probably a bug here, for now I don't care */
421 WCHAR *ourCookieName, *ourCookieData;
422 int ourCookieNameSize;
423 BOOL ret;
425 if (!(ourCookieData = strchrW(lpCookieData, '=')))
427 TRACE("something terribly wrong with cookie data %s\n",
428 debugstr_w(ourCookieData));
429 return FALSE;
431 ourCookieNameSize = ourCookieData - lpCookieData;
432 ourCookieData += 1;
433 ourCookieName = HeapAlloc(GetProcessHeap(), 0,
434 (ourCookieNameSize + 1)*sizeof(WCHAR));
435 memcpy(ourCookieName, lpCookieData, ourCookieNameSize * sizeof(WCHAR));
436 ourCookieName[ourCookieNameSize] = '\0';
437 TRACE("setting (hacked) cookie of %s, %s\n",
438 debugstr_w(ourCookieName), debugstr_w(ourCookieData));
439 ret = InternetSetCookieW(lpszUrl, ourCookieName, ourCookieData);
440 HeapFree(GetProcessHeap(), 0, ourCookieName);
441 return ret;
444 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
446 LIST_FOR_EACH(cursor, &domain_list)
448 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
449 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, thisCookieDomain, FALSE))
450 break;
451 thisCookieDomain = NULL;
453 if (!thisCookieDomain)
454 thisCookieDomain = COOKIE_addDomain(hostName, path);
456 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
457 COOKIE_deleteCookie(thisCookie, FALSE);
459 thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
460 return TRUE;
464 /***********************************************************************
465 * InternetSetCookieA (WININET.@)
467 * Sets cookie for the specified url
469 * RETURNS
470 * TRUE on success
471 * FALSE on failure
474 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
475 LPCSTR lpCookieData)
477 DWORD len;
478 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
479 BOOL r;
481 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
482 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
484 if( lpszUrl )
486 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
487 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
488 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
491 if( lpszCookieName )
493 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
494 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
495 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
498 if( lpCookieData )
500 len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
501 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
502 MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
505 r = InternetSetCookieW( szUrl, szCookieName, szCookieData );
507 HeapFree( GetProcessHeap(), 0, szCookieData );
508 HeapFree( GetProcessHeap(), 0, szCookieName );
509 HeapFree( GetProcessHeap(), 0, szUrl );
511 return r;
514 /***********************************************************************
515 * InternetSetCookieExA (WININET.@)
517 * See InternetSetCookieExW.
519 DWORD WINAPI InternetSetCookieExA( LPCSTR lpszURL, LPCSTR lpszCookieName, LPCSTR lpszCookieData,
520 DWORD dwFlags, DWORD_PTR dwReserved)
522 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
523 debugstr_a(lpszURL), debugstr_a(lpszCookieName), debugstr_a(lpszCookieData),
524 dwFlags, dwReserved);
525 return TRUE;
528 /***********************************************************************
529 * InternetSetCookieExW (WININET.@)
531 * Sets a cookie for the specified URL.
533 * RETURNS
534 * TRUE on success
535 * FALSE on failure
538 DWORD WINAPI InternetSetCookieExW( LPCWSTR lpszURL, LPCWSTR lpszCookieName, LPCWSTR lpszCookieData,
539 DWORD dwFlags, DWORD_PTR dwReserved)
541 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
542 debugstr_w(lpszURL), debugstr_w(lpszCookieName), debugstr_w(lpszCookieData),
543 dwFlags, dwReserved);
544 return TRUE;
547 /***********************************************************************
548 * InternetGetCookieExA (WININET.@)
550 * See InternetGetCookieExW.
552 BOOL WINAPI InternetGetCookieExA( LPCSTR pchURL, LPCSTR pchCookieName, LPSTR pchCookieData,
553 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
555 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
556 debugstr_a(pchURL), debugstr_a(pchCookieName), debugstr_a(pchCookieData),
557 pcchCookieData, dwFlags, lpReserved);
558 return FALSE;
561 /***********************************************************************
562 * InternetGetCookieExW (WININET.@)
564 * Retrieve cookie for the specified URL.
566 * RETURNS
567 * TRUE on success
568 * FALSE on failure
571 BOOL WINAPI InternetGetCookieExW( LPCWSTR pchURL, LPCWSTR pchCookieName, LPWSTR pchCookieData,
572 LPDWORD pcchCookieData, DWORD dwFlags, LPVOID lpReserved)
574 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
575 debugstr_w(pchURL), debugstr_w(pchCookieName), debugstr_w(pchCookieData),
576 pcchCookieData, dwFlags, lpReserved);
577 return FALSE;
580 /***********************************************************************
581 * InternetClearAllPerSiteCookieDecisions (WININET.@)
583 * Clears all per-site decisions about cookies.
585 * RETURNS
586 * TRUE on success
587 * FALSE on failure
590 BOOL WINAPI InternetClearAllPerSiteCookieDecisions( VOID )
592 FIXME("stub\n");
593 return TRUE;
596 /***********************************************************************
597 * InternetEnumPerSiteCookieDecisionA (WININET.@)
599 * See InternetEnumPerSiteCookieDecisionW.
601 BOOL WINAPI InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName, unsigned long *pcSiteNameSize,
602 unsigned long *pdwDecision, unsigned long dwIndex )
604 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
605 debugstr_a(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
606 return FALSE;
609 /***********************************************************************
610 * InternetEnumPerSiteCookieDecisionW (WININET.@)
612 * Enumerates all per-site decisions about cookies.
614 * RETURNS
615 * TRUE on success
616 * FALSE on failure
619 BOOL WINAPI InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName, unsigned long *pcSiteNameSize,
620 unsigned long *pdwDecision, unsigned long dwIndex )
622 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
623 debugstr_w(pszSiteName), pcSiteNameSize, pdwDecision, dwIndex);
624 return FALSE;
627 /***********************************************************************
628 * InternetGetPerSiteCookieDecisionA (WININET.@)
630 BOOL WINAPI InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName, unsigned long *pResult )
632 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName), pResult);
633 return FALSE;
636 /***********************************************************************
637 * InternetGetPerSiteCookieDecisionW (WININET.@)
639 BOOL WINAPI InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName, unsigned long *pResult )
641 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName), pResult);
642 return FALSE;
645 /***********************************************************************
646 * InternetSetPerSiteCookieDecisionA (WININET.@)
648 BOOL WINAPI InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName, DWORD dwDecision )
650 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName), dwDecision);
651 return FALSE;
654 /***********************************************************************
655 * InternetSetPerSiteCookieDecisionW (WININET.@)
657 BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDecision )
659 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
660 return FALSE;