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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "wine/debug.h"
41 #include "wine/list.h"
43 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
46 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
49 * Cookies are currently memory only.
50 * Cookies are NOT THREAD SAFE
51 * Cookies could use ALOT OF MEMORY. We need some kind of memory management here!
52 * Cookies should care about the expiry time
55 typedef struct _cookie_domain cookie_domain
;
56 typedef struct _cookie cookie
;
62 struct _cookie_domain
*parent
;
66 time_t expiry
; /* FIXME: not used */
73 LPWSTR lpCookieDomain
;
75 struct list cookie_list
;
78 static struct list domain_list
= LIST_INIT(domain_list
);
80 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
);
81 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
);
82 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
);
83 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
);
84 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
);
87 /* adds a cookie to the domain */
88 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
)
90 cookie
*newCookie
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookie
));
92 list_init(&newCookie
->entry
);
93 newCookie
->lpCookieName
= NULL
;
94 newCookie
->lpCookieData
= NULL
;
98 newCookie
->lpCookieName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(name
) + 1)*sizeof(WCHAR
));
99 lstrcpyW(newCookie
->lpCookieName
, name
);
103 newCookie
->lpCookieData
= HeapAlloc(GetProcessHeap(), 0, (strlenW(data
) + 1)*sizeof(WCHAR
));
104 lstrcpyW(newCookie
->lpCookieData
, data
);
107 TRACE("added cookie %p (data is %s)\n", newCookie
, debugstr_w(data
) );
109 list_add_tail(&domain
->cookie_list
, &newCookie
->entry
);
110 newCookie
->parent
= domain
;
115 /* finds a cookie in the domain matching the cookie name */
116 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
)
118 struct list
* cursor
;
119 TRACE("(%p, %s)\n", domain
, debugstr_w(lpszCookieName
));
121 LIST_FOR_EACH(cursor
, &domain
->cookie_list
)
123 cookie
*searchCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
124 BOOL candidate
= TRUE
;
125 if (candidate
&& lpszCookieName
)
127 if (candidate
&& !searchCookie
->lpCookieName
)
129 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
138 /* removes a cookie from the list, if its the last cookie we also remove the domain */
139 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
)
141 if (deadCookie
->lpCookieName
)
142 HeapFree(GetProcessHeap(), 0, deadCookie
->lpCookieName
);
143 if (deadCookie
->lpCookieData
)
144 HeapFree(GetProcessHeap(), 0, deadCookie
->lpCookieData
);
145 list_remove(&deadCookie
->entry
);
147 /* special case: last cookie, lets remove the domain to save memory */
148 if (list_empty(&deadCookie
->parent
->cookie_list
) && deleteDomain
)
149 COOKIE_deleteDomain(deadCookie
->parent
);
150 HeapFree(GetProcessHeap(), 0, deadCookie
);
153 /* allocates a domain and adds it to the end */
154 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
)
156 cookie_domain
*newDomain
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain
));
158 list_init(&newDomain
->entry
);
159 list_init(&newDomain
->cookie_list
);
160 newDomain
->lpCookieDomain
= NULL
;
161 newDomain
->lpCookiePath
= NULL
;
165 newDomain
->lpCookieDomain
= HeapAlloc(GetProcessHeap(), 0, (strlenW(domain
) + 1)*sizeof(WCHAR
));
166 strcpyW(newDomain
->lpCookieDomain
, domain
);
170 newDomain
->lpCookiePath
= HeapAlloc(GetProcessHeap(), 0, (strlenW(path
) + 1)*sizeof(WCHAR
));
171 lstrcpyW(newDomain
->lpCookiePath
, path
);
174 list_add_tail(&domain_list
, &newDomain
->entry
);
176 TRACE("Adding domain: %p\n", newDomain
);
180 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
182 URL_COMPONENTSW UrlComponents
;
184 UrlComponents
.lpszExtraInfo
= NULL
;
185 UrlComponents
.lpszPassword
= NULL
;
186 UrlComponents
.lpszScheme
= NULL
;
187 UrlComponents
.lpszUrlPath
= path
;
188 UrlComponents
.lpszUserName
= NULL
;
189 UrlComponents
.lpszHostName
= hostName
;
190 UrlComponents
.dwHostNameLength
= hostNameLen
;
191 UrlComponents
.dwUrlPathLength
= pathLen
;
193 InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
);
196 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
197 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
198 cookie_domain
*searchDomain
, BOOL allow_partial
)
200 TRACE("searching on domain %p\n", searchDomain
);
201 if (lpszCookieDomain
)
203 if (!searchDomain
->lpCookieDomain
)
206 TRACE("comparing domain %s with %s\n",
207 debugstr_w(lpszCookieDomain
),
208 debugstr_w(searchDomain
->lpCookieDomain
));
210 if (allow_partial
&& !strstrW(lpszCookieDomain
, searchDomain
->lpCookieDomain
))
212 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
217 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
218 if (!searchDomain
->lpCookiePath
)
220 if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
226 /* remove a domain from the list and delete it */
227 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
)
229 struct list
* cursor
;
230 while ((cursor
= list_tail(&deadDomain
->cookie_list
)))
232 COOKIE_deleteCookie(LIST_ENTRY(cursor
, cookie
, entry
), FALSE
);
236 if (deadDomain
->lpCookieDomain
)
237 HeapFree(GetProcessHeap(), 0, deadDomain
->lpCookieDomain
);
238 if (deadDomain
->lpCookiePath
)
239 HeapFree(GetProcessHeap(), 0, deadDomain
->lpCookiePath
);
241 list_remove(&deadDomain
->entry
);
243 HeapFree(GetProcessHeap(), 0, deadDomain
);
246 /***********************************************************************
247 * InternetGetCookieW (WININET.@)
249 * Retrieve cookie from the specified url
251 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
252 * So it won't be implemented here.
259 BOOL WINAPI
InternetGetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
260 LPWSTR lpCookieData
, LPDWORD lpdwSize
)
262 struct list
* cursor
;
263 int cnt
= 0, domain_count
= 0;
264 int cookie_count
= 0;
265 WCHAR hostName
[2048], path
[2048];
267 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
),
268 lpCookieData
, lpdwSize
);
270 COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
272 LIST_FOR_EACH(cursor
, &domain_list
)
274 cookie_domain
*cookiesDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
275 if (COOKIE_matchDomain(hostName
, NULL
/* FIXME: path */, cookiesDomain
, TRUE
))
277 struct list
* cursor
;
279 TRACE("found domain %p\n", cookiesDomain
);
281 LIST_FOR_EACH(cursor
, &cookiesDomain
->cookie_list
)
283 cookie
*thisCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
284 if (lpCookieData
== NULL
) /* return the size of the buffer required to lpdwSize */
286 if (cookie_count
!= 0)
288 cnt
+= strlenW(thisCookie
->lpCookieName
);
290 cnt
+= strlenW(thisCookie
->lpCookieData
);
294 static const WCHAR szsc
[] = { ';',' ',0 };
295 static const WCHAR szpseq
[] = { '%','s','=','%','s',0 };
296 if (cookie_count
!= 0)
297 cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szsc
);
298 cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szpseq
,
299 thisCookie
->lpCookieName
,
300 thisCookie
->lpCookieData
);
301 TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie
->lpCookieName
), debugstr_w(thisCookie
->lpCookieData
));
307 if (lpCookieData
== NULL
)
310 *lpdwSize
= cnt
*sizeof(WCHAR
);
311 TRACE("returning\n");
318 *lpdwSize
= (cnt
+ 1)*sizeof(WCHAR
);
320 TRACE("Returning %i (from %i domains): %s\n", cnt
, domain_count
,
321 debugstr_w(lpCookieData
));
323 return (cnt
? TRUE
: FALSE
);
327 /***********************************************************************
328 * InternetGetCookieA (WININET.@)
330 * Retrieve cookie from the specified url
337 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
338 LPSTR lpCookieData
, LPDWORD lpdwSize
)
341 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
344 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
349 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
350 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
351 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
356 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
357 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
358 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
361 r
= InternetGetCookieW( szUrl
, szCookieName
, NULL
, &len
);
364 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
368 r
= InternetGetCookieW( szUrl
, szCookieName
, szCookieData
, &len
);
370 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
371 lpCookieData
, *lpdwSize
, NULL
, NULL
);
375 HeapFree( GetProcessHeap(), 0, szCookieData
);
377 HeapFree( GetProcessHeap(), 0, szCookieName
);
379 HeapFree( GetProcessHeap(), 0, szUrl
);
385 /***********************************************************************
386 * InternetSetCookieW (WININET.@)
388 * Sets cookie for the specified url
395 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
396 LPCWSTR lpCookieData
)
398 cookie_domain
*thisCookieDomain
= NULL
;
400 WCHAR hostName
[2048], path
[2048];
401 struct list
* cursor
;
403 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
404 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
406 if (!lpCookieData
|| !strlenW(lpCookieData
))
408 TRACE("no cookie data, not adding\n");
413 /* some apps (or is it us??) try to add a cookie with no cookie name, but
414 * the cookie data in the form of name=data. */
415 /* FIXME, probably a bug here, for now I don't care */
416 WCHAR
*ourCookieName
, *ourCookieData
;
417 int ourCookieNameSize
;
419 if (!(ourCookieData
= strchrW(lpCookieData
, '=')))
421 TRACE("something terribly wrong with cookie data %s\n",
422 debugstr_w(ourCookieData
));
425 ourCookieNameSize
= ourCookieData
- lpCookieData
;
427 ourCookieName
= HeapAlloc(GetProcessHeap(), 0,
428 (ourCookieNameSize
+ 1)*sizeof(WCHAR
));
429 strncpyW(ourCookieName
, ourCookieData
, ourCookieNameSize
);
430 ourCookieName
[ourCookieNameSize
] = '\0';
431 TRACE("setting (hacked) cookie of %s, %s\n",
432 debugstr_w(ourCookieName
), debugstr_w(ourCookieData
));
433 ret
= InternetSetCookieW(lpszUrl
, ourCookieName
, ourCookieData
);
434 HeapFree(GetProcessHeap(), 0, ourCookieName
);
438 COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
440 LIST_FOR_EACH(cursor
, &domain_list
)
442 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
443 if (COOKIE_matchDomain(hostName
, NULL
/* FIXME: path */, thisCookieDomain
, FALSE
))
445 thisCookieDomain
= NULL
;
447 if (!thisCookieDomain
)
448 thisCookieDomain
= COOKIE_addDomain(hostName
, path
);
450 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, lpszCookieName
)))
451 COOKIE_deleteCookie(thisCookie
, FALSE
);
453 thisCookie
= COOKIE_addCookie(thisCookieDomain
, lpszCookieName
, lpCookieData
);
458 /***********************************************************************
459 * InternetSetCookieA (WININET.@)
461 * Sets cookie for the specified url
468 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
472 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
475 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
476 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
480 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
481 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
482 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
487 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
488 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
489 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
494 len
= MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, NULL
, 0 );
495 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
496 MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, szCookieData
, len
);
499 r
= InternetSetCookieW( szUrl
, szCookieName
, szCookieData
);
502 HeapFree( GetProcessHeap(), 0, szCookieData
);
504 HeapFree( GetProcessHeap(), 0, szCookieName
);
506 HeapFree( GetProcessHeap(), 0, szUrl
);