Make sure we include config.h before including twain_i.h.
[wine/hacks.git] / dlls / wininet / cookie.c
blob217ef1b4c0bfa9ad384eb231165ff120cdf74fe9
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 ALOT 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 if (deadCookie->lpCookieName)
143 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieName);
144 if (deadCookie->lpCookieData)
145 HeapFree(GetProcessHeap(), 0, deadCookie->lpCookieData);
146 list_remove(&deadCookie->entry);
148 /* special case: last cookie, lets remove the domain to save memory */
149 if (list_empty(&deadCookie->parent->cookie_list) && deleteDomain)
150 COOKIE_deleteDomain(deadCookie->parent);
151 HeapFree(GetProcessHeap(), 0, deadCookie);
154 /* allocates a domain and adds it to the end */
155 static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
157 cookie_domain *newDomain = HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain));
159 list_init(&newDomain->entry);
160 list_init(&newDomain->cookie_list);
161 newDomain->lpCookieDomain = NULL;
162 newDomain->lpCookiePath = NULL;
164 if (domain)
166 newDomain->lpCookieDomain = HeapAlloc(GetProcessHeap(), 0, (strlenW(domain) + 1)*sizeof(WCHAR));
167 strcpyW(newDomain->lpCookieDomain, domain);
169 if (path)
171 newDomain->lpCookiePath = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1)*sizeof(WCHAR));
172 lstrcpyW(newDomain->lpCookiePath, path);
175 list_add_tail(&domain_list, &newDomain->entry);
177 TRACE("Adding domain: %p\n", newDomain);
178 return newDomain;
181 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
183 URL_COMPONENTSW UrlComponents;
185 UrlComponents.lpszExtraInfo = NULL;
186 UrlComponents.lpszPassword = NULL;
187 UrlComponents.lpszScheme = NULL;
188 UrlComponents.lpszUrlPath = path;
189 UrlComponents.lpszUserName = NULL;
190 UrlComponents.lpszHostName = hostName;
191 UrlComponents.dwHostNameLength = hostNameLen;
192 UrlComponents.dwUrlPathLength = pathLen;
194 InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
197 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
198 static BOOL COOKIE_matchDomain(LPCWSTR lpszCookieDomain, LPCWSTR lpszCookiePath,
199 cookie_domain *searchDomain, BOOL allow_partial)
201 TRACE("searching on domain %p\n", searchDomain);
202 if (lpszCookieDomain)
204 if (!searchDomain->lpCookieDomain)
205 return FALSE;
207 TRACE("comparing domain %s with %s\n",
208 debugstr_w(lpszCookieDomain),
209 debugstr_w(searchDomain->lpCookieDomain));
211 if (allow_partial && !strstrW(lpszCookieDomain, searchDomain->lpCookieDomain))
212 return FALSE;
213 else if (!allow_partial && lstrcmpW(lpszCookieDomain, searchDomain->lpCookieDomain) != 0)
214 return FALSE;
216 if (lpszCookiePath)
218 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath), debugstr_w(searchDomain->lpCookiePath));
219 if (!searchDomain->lpCookiePath)
220 return FALSE;
221 if (strcmpW(lpszCookiePath, searchDomain->lpCookiePath))
222 return FALSE;
224 return TRUE;
227 /* remove a domain from the list and delete it */
228 static void COOKIE_deleteDomain(cookie_domain *deadDomain)
230 struct list * cursor;
231 while ((cursor = list_tail(&deadDomain->cookie_list)))
233 COOKIE_deleteCookie(LIST_ENTRY(cursor, cookie, entry), FALSE);
234 list_remove(cursor);
237 if (deadDomain->lpCookieDomain)
238 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookieDomain);
239 if (deadDomain->lpCookiePath)
240 HeapFree(GetProcessHeap(), 0, deadDomain->lpCookiePath);
242 list_remove(&deadDomain->entry);
244 HeapFree(GetProcessHeap(), 0, deadDomain);
247 /***********************************************************************
248 * InternetGetCookieW (WININET.@)
250 * Retrieve cookie from the specified url
252 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
253 * So it won't be implemented here.
255 * RETURNS
256 * TRUE on success
257 * FALSE on failure
260 BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
261 LPWSTR lpCookieData, LPDWORD lpdwSize)
263 struct list * cursor;
264 int cnt = 0, domain_count = 0;
265 int cookie_count = 0;
266 WCHAR hostName[2048], path[2048];
268 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
269 lpCookieData, lpdwSize);
271 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
273 LIST_FOR_EACH(cursor, &domain_list)
275 cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
276 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, cookiesDomain, TRUE))
278 struct list * cursor;
279 domain_count++;
280 TRACE("found domain %p\n", cookiesDomain);
282 LIST_FOR_EACH(cursor, &cookiesDomain->cookie_list)
284 cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
285 if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
287 if (cookie_count != 0)
288 cnt += 2; /* '; ' */
289 cnt += strlenW(thisCookie->lpCookieName);
290 cnt += 1; /* = */
291 cnt += strlenW(thisCookie->lpCookieData);
293 else
295 static const WCHAR szsc[] = { ';',' ',0 };
296 static const WCHAR szpseq[] = { '%','s','=','%','s',0 };
297 if (cookie_count != 0)
298 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
299 cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szpseq,
300 thisCookie->lpCookieName,
301 thisCookie->lpCookieData);
302 TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie->lpCookieName), debugstr_w(thisCookie->lpCookieData));
304 cookie_count++;
308 if (lpCookieData == NULL)
310 cnt += 1; /* NULL */
311 *lpdwSize = cnt*sizeof(WCHAR);
312 TRACE("returning\n");
313 return TRUE;
316 if (!domain_count)
317 return FALSE;
319 *lpdwSize = (cnt + 1)*sizeof(WCHAR);
321 TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count,
322 debugstr_w(lpCookieData));
324 return (cnt ? TRUE : FALSE);
328 /***********************************************************************
329 * InternetGetCookieA (WININET.@)
331 * Retrieve cookie from the specified url
333 * RETURNS
334 * TRUE on success
335 * FALSE on failure
338 BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
339 LPSTR lpCookieData, LPDWORD lpdwSize)
341 DWORD len;
342 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
343 BOOL r;
345 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl), debugstr_a(lpszCookieName),
346 lpCookieData);
348 if( lpszUrl )
350 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
351 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
352 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
355 if( lpszCookieName )
357 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
358 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
359 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
362 r = InternetGetCookieW( szUrl, szCookieName, NULL, &len );
363 if( r )
365 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
366 if( !szCookieData )
367 return FALSE;
369 r = InternetGetCookieW( szUrl, szCookieName, szCookieData, &len );
371 *lpdwSize = WideCharToMultiByte( CP_ACP, 0, szCookieData, len,
372 lpCookieData, *lpdwSize, NULL, NULL );
375 if( szCookieData )
376 HeapFree( GetProcessHeap(), 0, szCookieData );
377 if( szCookieName )
378 HeapFree( GetProcessHeap(), 0, szCookieName );
379 if( szUrl )
380 HeapFree( GetProcessHeap(), 0, szUrl );
382 return r;
386 /***********************************************************************
387 * InternetSetCookieW (WININET.@)
389 * Sets cookie for the specified url
391 * RETURNS
392 * TRUE on success
393 * FALSE on failure
396 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
397 LPCWSTR lpCookieData)
399 cookie_domain *thisCookieDomain = NULL;
400 cookie *thisCookie;
401 WCHAR hostName[2048], path[2048];
402 struct list * cursor;
404 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
405 debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
407 if (!lpCookieData || !strlenW(lpCookieData))
409 TRACE("no cookie data, not adding\n");
410 return FALSE;
412 if (!lpszCookieName)
414 /* some apps (or is it us??) try to add a cookie with no cookie name, but
415 * the cookie data in the form of name=data. */
416 /* FIXME, probably a bug here, for now I don't care */
417 WCHAR *ourCookieName, *ourCookieData;
418 int ourCookieNameSize;
419 BOOL ret;
420 if (!(ourCookieData = strchrW(lpCookieData, '=')))
422 TRACE("something terribly wrong with cookie data %s\n",
423 debugstr_w(ourCookieData));
424 return FALSE;
426 ourCookieNameSize = ourCookieData - lpCookieData;
427 ourCookieData += 1;
428 ourCookieName = HeapAlloc(GetProcessHeap(), 0,
429 (ourCookieNameSize + 1)*sizeof(WCHAR));
430 strncpyW(ourCookieName, ourCookieData, ourCookieNameSize);
431 ourCookieName[ourCookieNameSize] = '\0';
432 TRACE("setting (hacked) cookie of %s, %s\n",
433 debugstr_w(ourCookieName), debugstr_w(ourCookieData));
434 ret = InternetSetCookieW(lpszUrl, ourCookieName, ourCookieData);
435 HeapFree(GetProcessHeap(), 0, ourCookieName);
436 return ret;
439 COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
441 LIST_FOR_EACH(cursor, &domain_list)
443 thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
444 if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, thisCookieDomain, FALSE))
445 break;
446 thisCookieDomain = NULL;
448 if (!thisCookieDomain)
449 thisCookieDomain = COOKIE_addDomain(hostName, path);
451 if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
452 COOKIE_deleteCookie(thisCookie, FALSE);
454 thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
455 return TRUE;
459 /***********************************************************************
460 * InternetSetCookieA (WININET.@)
462 * Sets cookie for the specified url
464 * RETURNS
465 * TRUE on success
466 * FALSE on failure
469 BOOL WINAPI InternetSetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
470 LPCSTR lpCookieData)
472 DWORD len;
473 LPWSTR szCookieData = NULL, szUrl = NULL, szCookieName = NULL;
474 BOOL r;
476 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl),
477 debugstr_a(lpszCookieName), debugstr_a(lpCookieData));
479 if( lpszUrl )
481 len = MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, NULL, 0 );
482 szUrl = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
483 MultiByteToWideChar( CP_ACP, 0, lpszUrl, -1, szUrl, len );
486 if( lpszCookieName )
488 len = MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, NULL, 0 );
489 szCookieName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
490 MultiByteToWideChar( CP_ACP, 0, lpszCookieName, -1, szCookieName, len );
493 if( lpCookieData )
495 len = MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, NULL, 0 );
496 szCookieData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
497 MultiByteToWideChar( CP_ACP, 0, lpCookieData, -1, szCookieData, len );
500 r = InternetSetCookieW( szUrl, szCookieName, szCookieData );
502 if( szCookieData )
503 HeapFree( GetProcessHeap(), 0, szCookieData );
504 if( szCookieName )
505 HeapFree( GetProcessHeap(), 0, szCookieName );
506 if( szUrl )
507 HeapFree( GetProcessHeap(), 0, szUrl );
509 return r;