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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
39 #include "wine/debug.h"
42 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
45 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
48 * Cookies are currently memory only.
49 * Cookies are NOT THREAD SAFE
50 * Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
51 * Cookies should care about the expiry time
54 typedef struct _cookie_domain cookie_domain
;
55 typedef struct _cookie cookie
;
61 struct _cookie_domain
*parent
;
65 time_t expiry
; /* FIXME: not used */
72 LPWSTR lpCookieDomain
;
74 struct list cookie_list
;
77 static struct list domain_list
= LIST_INIT(domain_list
);
79 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
);
80 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
);
81 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
);
82 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
);
83 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
);
86 /* adds a cookie to the domain */
87 static cookie
*COOKIE_addCookie(cookie_domain
*domain
, LPCWSTR name
, LPCWSTR data
)
89 cookie
*newCookie
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookie
));
91 list_init(&newCookie
->entry
);
92 newCookie
->lpCookieName
= NULL
;
93 newCookie
->lpCookieData
= NULL
;
97 newCookie
->lpCookieName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(name
) + 1)*sizeof(WCHAR
));
98 lstrcpyW(newCookie
->lpCookieName
, name
);
102 newCookie
->lpCookieData
= HeapAlloc(GetProcessHeap(), 0, (strlenW(data
) + 1)*sizeof(WCHAR
));
103 lstrcpyW(newCookie
->lpCookieData
, data
);
106 TRACE("added cookie %p (data is %s)\n", newCookie
, debugstr_w(data
) );
108 list_add_tail(&domain
->cookie_list
, &newCookie
->entry
);
109 newCookie
->parent
= domain
;
114 /* finds a cookie in the domain matching the cookie name */
115 static cookie
*COOKIE_findCookie(cookie_domain
*domain
, LPCWSTR lpszCookieName
)
117 struct list
* cursor
;
118 TRACE("(%p, %s)\n", domain
, debugstr_w(lpszCookieName
));
120 LIST_FOR_EACH(cursor
, &domain
->cookie_list
)
122 cookie
*searchCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
123 BOOL candidate
= TRUE
;
124 if (candidate
&& lpszCookieName
)
126 if (candidate
&& !searchCookie
->lpCookieName
)
128 if (candidate
&& strcmpW(lpszCookieName
, searchCookie
->lpCookieName
) != 0)
137 /* removes a cookie from the list, if its the last cookie we also remove the domain */
138 static void COOKIE_deleteCookie(cookie
*deadCookie
, BOOL deleteDomain
)
140 HeapFree(GetProcessHeap(), 0, deadCookie
->lpCookieName
);
141 HeapFree(GetProcessHeap(), 0, deadCookie
->lpCookieData
);
142 list_remove(&deadCookie
->entry
);
144 /* special case: last cookie, lets remove the domain to save memory */
145 if (list_empty(&deadCookie
->parent
->cookie_list
) && deleteDomain
)
146 COOKIE_deleteDomain(deadCookie
->parent
);
147 HeapFree(GetProcessHeap(), 0, deadCookie
);
150 /* allocates a domain and adds it to the end */
151 static cookie_domain
*COOKIE_addDomain(LPCWSTR domain
, LPCWSTR path
)
153 cookie_domain
*newDomain
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookie_domain
));
155 list_init(&newDomain
->entry
);
156 list_init(&newDomain
->cookie_list
);
157 newDomain
->lpCookieDomain
= NULL
;
158 newDomain
->lpCookiePath
= NULL
;
162 newDomain
->lpCookieDomain
= HeapAlloc(GetProcessHeap(), 0, (strlenW(domain
) + 1)*sizeof(WCHAR
));
163 strcpyW(newDomain
->lpCookieDomain
, domain
);
167 newDomain
->lpCookiePath
= HeapAlloc(GetProcessHeap(), 0, (strlenW(path
) + 1)*sizeof(WCHAR
));
168 lstrcpyW(newDomain
->lpCookiePath
, path
);
171 list_add_tail(&domain_list
, &newDomain
->entry
);
173 TRACE("Adding domain: %p\n", newDomain
);
177 static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl
, LPWSTR hostName
, int hostNameLen
, LPWSTR path
, int pathLen
)
179 URL_COMPONENTSW UrlComponents
;
181 UrlComponents
.lpszExtraInfo
= NULL
;
182 UrlComponents
.lpszPassword
= NULL
;
183 UrlComponents
.lpszScheme
= NULL
;
184 UrlComponents
.lpszUrlPath
= path
;
185 UrlComponents
.lpszUserName
= NULL
;
186 UrlComponents
.lpszHostName
= hostName
;
187 UrlComponents
.dwHostNameLength
= hostNameLen
;
188 UrlComponents
.dwUrlPathLength
= pathLen
;
190 InternetCrackUrlW(lpszUrl
, 0, 0, &UrlComponents
);
193 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
194 static BOOL
COOKIE_matchDomain(LPCWSTR lpszCookieDomain
, LPCWSTR lpszCookiePath
,
195 cookie_domain
*searchDomain
, BOOL allow_partial
)
197 TRACE("searching on domain %p\n", searchDomain
);
198 if (lpszCookieDomain
)
200 if (!searchDomain
->lpCookieDomain
)
203 TRACE("comparing domain %s with %s\n",
204 debugstr_w(lpszCookieDomain
),
205 debugstr_w(searchDomain
->lpCookieDomain
));
207 if (allow_partial
&& !strstrW(lpszCookieDomain
, searchDomain
->lpCookieDomain
))
209 else if (!allow_partial
&& lstrcmpW(lpszCookieDomain
, searchDomain
->lpCookieDomain
) != 0)
214 TRACE("comparing paths: %s with %s\n", debugstr_w(lpszCookiePath
), debugstr_w(searchDomain
->lpCookiePath
));
215 if (!searchDomain
->lpCookiePath
)
217 if (strcmpW(lpszCookiePath
, searchDomain
->lpCookiePath
))
223 /* remove a domain from the list and delete it */
224 static void COOKIE_deleteDomain(cookie_domain
*deadDomain
)
226 struct list
* cursor
;
227 while ((cursor
= list_tail(&deadDomain
->cookie_list
)))
229 COOKIE_deleteCookie(LIST_ENTRY(cursor
, cookie
, entry
), FALSE
);
233 HeapFree(GetProcessHeap(), 0, deadDomain
->lpCookieDomain
);
234 HeapFree(GetProcessHeap(), 0, deadDomain
->lpCookiePath
);
236 list_remove(&deadDomain
->entry
);
238 HeapFree(GetProcessHeap(), 0, deadDomain
);
241 /***********************************************************************
242 * InternetGetCookieW (WININET.@)
244 * Retrieve cookie from the specified url
246 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
247 * So it won't be implemented here.
254 BOOL WINAPI
InternetGetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
255 LPWSTR lpCookieData
, LPDWORD lpdwSize
)
257 struct list
* cursor
;
258 int cnt
= 0, domain_count
= 0;
259 int cookie_count
= 0;
260 WCHAR hostName
[2048], path
[2048];
262 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
),
263 lpCookieData
, lpdwSize
);
267 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME
);
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
));
311 TRACE("no cookies found for %s\n", debugstr_w(hostName
));
312 SetLastError(ERROR_NO_MORE_ITEMS
);
316 if (lpCookieData
== NULL
)
319 *lpdwSize
= cnt
*sizeof(WCHAR
);
320 TRACE("returning\n");
324 *lpdwSize
= (cnt
+ 1)*sizeof(WCHAR
);
326 TRACE("Returning %i (from %i domains): %s\n", cnt
, domain_count
,
327 debugstr_w(lpCookieData
));
329 return (cnt
? TRUE
: FALSE
);
333 /***********************************************************************
334 * InternetGetCookieA (WININET.@)
336 * Retrieve cookie from the specified url
343 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
344 LPSTR lpCookieData
, LPDWORD lpdwSize
)
347 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
350 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
355 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
356 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
357 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
362 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
363 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
364 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
367 r
= InternetGetCookieW( szUrl
, szCookieName
, NULL
, &len
);
370 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
377 r
= InternetGetCookieW( szUrl
, szCookieName
, szCookieData
, &len
);
379 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
380 lpCookieData
, *lpdwSize
, NULL
, NULL
);
384 HeapFree( GetProcessHeap(), 0, szCookieData
);
385 HeapFree( GetProcessHeap(), 0, szCookieName
);
386 HeapFree( GetProcessHeap(), 0, szUrl
);
392 /***********************************************************************
393 * InternetSetCookieW (WININET.@)
395 * Sets cookie for the specified url
402 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
403 LPCWSTR lpCookieData
)
405 cookie_domain
*thisCookieDomain
= NULL
;
407 WCHAR hostName
[2048], path
[2048];
408 struct list
* cursor
;
410 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
411 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
415 SetLastError(ERROR_INVALID_PARAMETER
);
420 /* some apps (or is it us??) try to add a cookie with no cookie name, but
421 * the cookie data in the form of name=data. */
422 /* FIXME, probably a bug here, for now I don't care */
423 WCHAR
*ourCookieName
, *ourCookieData
;
424 int ourCookieNameSize
;
427 if (!(ourCookieData
= strchrW(lpCookieData
, '=')))
429 TRACE("something terribly wrong with cookie data %s\n",
430 debugstr_w(ourCookieData
));
433 ourCookieNameSize
= ourCookieData
- lpCookieData
;
435 ourCookieName
= HeapAlloc(GetProcessHeap(), 0,
436 (ourCookieNameSize
+ 1)*sizeof(WCHAR
));
437 memcpy(ourCookieName
, lpCookieData
, ourCookieNameSize
* sizeof(WCHAR
));
438 ourCookieName
[ourCookieNameSize
] = '\0';
439 TRACE("setting (hacked) cookie of %s, %s\n",
440 debugstr_w(ourCookieName
), debugstr_w(ourCookieData
));
441 ret
= InternetSetCookieW(lpszUrl
, ourCookieName
, ourCookieData
);
442 HeapFree(GetProcessHeap(), 0, ourCookieName
);
446 COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
448 LIST_FOR_EACH(cursor
, &domain_list
)
450 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
451 if (COOKIE_matchDomain(hostName
, NULL
/* FIXME: path */, thisCookieDomain
, FALSE
))
453 thisCookieDomain
= NULL
;
455 if (!thisCookieDomain
)
456 thisCookieDomain
= COOKIE_addDomain(hostName
, path
);
458 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, lpszCookieName
)))
459 COOKIE_deleteCookie(thisCookie
, FALSE
);
461 thisCookie
= COOKIE_addCookie(thisCookieDomain
, lpszCookieName
, lpCookieData
);
466 /***********************************************************************
467 * InternetSetCookieA (WININET.@)
469 * Sets cookie for the specified url
476 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
480 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
483 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
484 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
488 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
489 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
490 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
495 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
496 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
497 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
502 len
= MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, NULL
, 0 );
503 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
504 MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, szCookieData
, len
);
507 r
= InternetSetCookieW( szUrl
, szCookieName
, szCookieData
);
509 HeapFree( GetProcessHeap(), 0, szCookieData
);
510 HeapFree( GetProcessHeap(), 0, szCookieName
);
511 HeapFree( GetProcessHeap(), 0, szUrl
);
516 /***********************************************************************
517 * InternetSetCookieExA (WININET.@)
519 * See InternetSetCookieExW.
521 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
522 DWORD dwFlags
, DWORD_PTR dwReserved
)
524 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
525 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
526 dwFlags
, dwReserved
);
530 /***********************************************************************
531 * InternetSetCookieExW (WININET.@)
533 * Sets a cookie for the specified URL.
540 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
541 DWORD dwFlags
, DWORD_PTR dwReserved
)
543 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
544 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
545 dwFlags
, dwReserved
);
549 /***********************************************************************
550 * InternetGetCookieExA (WININET.@)
552 * See InternetGetCookieExW.
554 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
555 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
557 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
558 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
559 pcchCookieData
, dwFlags
, lpReserved
);
563 /***********************************************************************
564 * InternetGetCookieExW (WININET.@)
566 * Retrieve cookie for the specified URL.
573 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
574 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
576 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
577 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
578 pcchCookieData
, dwFlags
, lpReserved
);
582 /***********************************************************************
583 * InternetClearAllPerSiteCookieDecisions (WININET.@)
585 * Clears all per-site decisions about cookies.
592 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
598 /***********************************************************************
599 * InternetEnumPerSiteCookieDecisionA (WININET.@)
601 * See InternetEnumPerSiteCookieDecisionW.
603 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, unsigned long *pcSiteNameSize
,
604 unsigned long *pdwDecision
, unsigned long dwIndex
)
606 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
607 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
611 /***********************************************************************
612 * InternetEnumPerSiteCookieDecisionW (WININET.@)
614 * Enumerates all per-site decisions about cookies.
621 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, unsigned long *pcSiteNameSize
,
622 unsigned long *pdwDecision
, unsigned long dwIndex
)
624 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
625 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
629 /***********************************************************************
630 * InternetGetPerSiteCookieDecisionA (WININET.@)
632 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, unsigned long *pResult
)
634 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
638 /***********************************************************************
639 * InternetGetPerSiteCookieDecisionW (WININET.@)
641 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, unsigned long *pResult
)
643 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
647 /***********************************************************************
648 * InternetSetPerSiteCookieDecisionA (WININET.@)
650 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
652 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
656 /***********************************************************************
657 * InternetSetPerSiteCookieDecisionW (WININET.@)
659 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
661 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);