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
24 #include "wine/port.h"
39 #include "wine/debug.h"
42 #include "wine/list.h"
44 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
47 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
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
;
63 struct _cookie_domain
*parent
;
67 time_t expiry
; /* FIXME: not used */
74 LPWSTR lpCookieDomain
;
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
;
99 newCookie
->lpCookieName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(name
) + 1)*sizeof(WCHAR
));
100 lstrcpyW(newCookie
->lpCookieName
, name
);
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
;
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
)
130 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
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
;
166 newDomain
->lpCookieDomain
= HeapAlloc(GetProcessHeap(), 0, (strlenW(domain
) + 1)*sizeof(WCHAR
));
167 strcpyW(newDomain
->lpCookieDomain
, domain
);
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
);
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
)
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
))
213 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
218 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
219 if (!searchDomain
->lpCookiePath
)
221 if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
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
);
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.
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
;
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)
289 cnt
+= strlenW(thisCookie
->lpCookieName
);
291 cnt
+= strlenW(thisCookie
->lpCookieData
);
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
));
308 if (lpCookieData
== NULL
)
311 *lpdwSize
= cnt
*sizeof(WCHAR
);
312 TRACE("returning\n");
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
338 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
339 LPSTR lpCookieData
, LPDWORD lpdwSize
)
342 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
345 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
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
);
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
);
365 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
369 r
= InternetGetCookieW( szUrl
, szCookieName
, szCookieData
, &len
);
371 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
372 lpCookieData
, *lpdwSize
, NULL
, NULL
);
376 HeapFree( GetProcessHeap(), 0, szCookieData
);
378 HeapFree( GetProcessHeap(), 0, szCookieName
);
380 HeapFree( GetProcessHeap(), 0, szUrl
);
386 /***********************************************************************
387 * InternetSetCookieW (WININET.@)
389 * Sets cookie for the specified url
396 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
397 LPCWSTR lpCookieData
)
399 cookie_domain
*thisCookieDomain
= NULL
;
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");
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
;
420 if (!(ourCookieData
= strchrW(lpCookieData
, '=')))
422 TRACE("something terribly wrong with cookie data %s\n",
423 debugstr_w(ourCookieData
));
426 ourCookieNameSize
= ourCookieData
- lpCookieData
;
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
);
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
))
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
);
459 /***********************************************************************
460 * InternetSetCookieA (WININET.@)
462 * Sets cookie for the specified url
469 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
473 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
476 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
477 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
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
);
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
);
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
);
503 HeapFree( GetProcessHeap(), 0, szCookieData
);
505 HeapFree( GetProcessHeap(), 0, szCookieName
);
507 HeapFree( GetProcessHeap(), 0, szUrl
);