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 BOOL
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
.dwExtraInfoLength
= 0;
188 UrlComponents
.dwPasswordLength
= 0;
189 UrlComponents
.dwSchemeLength
= 0;
190 UrlComponents
.dwUserNameLength
= 0;
191 UrlComponents
.dwHostNameLength
= hostNameLen
;
192 UrlComponents
.dwUrlPathLength
= pathLen
;
194 return 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 HeapFree(GetProcessHeap(), 0, deadDomain
->lpCookieDomain
);
238 HeapFree(GetProcessHeap(), 0, deadDomain
->lpCookiePath
);
240 list_remove(&deadDomain
->entry
);
242 HeapFree(GetProcessHeap(), 0, deadDomain
);
245 /***********************************************************************
246 * InternetGetCookieW (WININET.@)
248 * Retrieve cookie from the specified url
250 * It should be noted that on windows the lpszCookieName parameter is "not implemented".
251 * So it won't be implemented here.
258 BOOL WINAPI
InternetGetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
259 LPWSTR lpCookieData
, LPDWORD lpdwSize
)
262 struct list
* cursor
;
263 unsigned int cnt
= 0, domain_count
= 0, cookie_count
= 0;
264 WCHAR hostName
[2048], path
[2048];
266 TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl
),debugstr_w(lpszCookieName
),
267 lpCookieData
, lpdwSize
);
271 SetLastError(ERROR_INVALID_PARAMETER
);
276 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
277 if (!ret
|| !hostName
[0]) return FALSE
;
279 LIST_FOR_EACH(cursor
, &domain_list
)
281 cookie_domain
*cookiesDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
282 if (COOKIE_matchDomain(hostName
, NULL
/* FIXME: path */, cookiesDomain
, TRUE
))
284 struct list
* cursor
;
286 TRACE("found domain %p\n", cookiesDomain
);
288 LIST_FOR_EACH(cursor
, &cookiesDomain
->cookie_list
)
290 cookie
*thisCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
291 if (lpCookieData
== NULL
) /* return the size of the buffer required to lpdwSize */
295 if (cookie_count
) cnt
+= 2; /* '; ' */
296 cnt
+= strlenW(thisCookie
->lpCookieName
);
297 if ((len
= strlenW(thisCookie
->lpCookieData
)))
305 static const WCHAR szsc
[] = { ';',' ',0 };
306 static const WCHAR szname
[] = { '%','s',0 };
307 static const WCHAR szdata
[] = { '=','%','s',0 };
309 if (cookie_count
) cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szsc
);
310 cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szname
, thisCookie
->lpCookieName
);
312 if (thisCookie
->lpCookieData
[0])
313 cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szdata
, thisCookie
->lpCookieData
);
315 TRACE("Cookie: %s\n", debugstr_w(lpCookieData
));
324 TRACE("no cookies found for %s\n", debugstr_w(hostName
));
325 SetLastError(ERROR_NO_MORE_ITEMS
);
329 if (lpCookieData
== NULL
)
331 *lpdwSize
= (cnt
+ 1) * sizeof(WCHAR
);
332 TRACE("returning %u\n", *lpdwSize
);
338 TRACE("Returning %u (from %u domains): %s\n", cnt
, domain_count
,
339 debugstr_w(lpCookieData
));
341 return (cnt
? TRUE
: FALSE
);
345 /***********************************************************************
346 * InternetGetCookieA (WININET.@)
348 * Retrieve cookie from the specified url
355 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
356 LPSTR lpCookieData
, LPDWORD lpdwSize
)
359 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
362 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
367 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
368 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
369 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
374 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
375 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
376 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
379 r
= InternetGetCookieW( szUrl
, szCookieName
, NULL
, &len
);
382 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
389 r
= InternetGetCookieW( szUrl
, szCookieName
, szCookieData
, &len
);
391 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
392 lpCookieData
, *lpdwSize
, NULL
, NULL
);
396 HeapFree( GetProcessHeap(), 0, szCookieData
);
397 HeapFree( GetProcessHeap(), 0, szCookieName
);
398 HeapFree( GetProcessHeap(), 0, szUrl
);
403 static BOOL
set_cookie(LPCWSTR domain
, LPCWSTR path
, LPCWSTR cookie_name
, LPCWSTR cookie_data
)
405 cookie_domain
*thisCookieDomain
= NULL
;
409 LIST_FOR_EACH(cursor
, &domain_list
)
411 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
412 if (COOKIE_matchDomain(domain
, NULL
/* FIXME: path */, thisCookieDomain
, FALSE
))
414 thisCookieDomain
= NULL
;
417 if (!thisCookieDomain
)
418 thisCookieDomain
= COOKIE_addDomain(domain
, path
);
420 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, cookie_name
)))
421 COOKIE_deleteCookie(thisCookie
, FALSE
);
423 TRACE("setting cookie %s=%s for domain %s\n", debugstr_w(cookie_name
),
424 debugstr_w(cookie_data
), debugstr_w(thisCookieDomain
->lpCookieDomain
));
426 if (!COOKIE_addCookie(thisCookieDomain
, cookie_name
, cookie_data
))
432 /***********************************************************************
433 * InternetSetCookieW (WININET.@)
435 * Sets cookie for the specified url
442 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
443 LPCWSTR lpCookieData
)
446 WCHAR hostName
[2048], path
[2048];
448 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
449 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
451 if (!lpszUrl
|| !lpCookieData
)
453 SetLastError(ERROR_INVALID_PARAMETER
);
457 hostName
[0] = path
[0] = 0;
458 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
459 if (!ret
|| !hostName
[0]) return FALSE
;
464 WCHAR
*cookie
, *data
;
466 len
= strlenW(lpCookieData
);
467 if (!(cookie
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
))))
469 SetLastError(ERROR_OUTOFMEMORY
);
472 strcpyW(cookie
, lpCookieData
);
474 /* some apps (or is it us??) try to add a cookie with no cookie name, but
475 * the cookie data in the form of name[=data].
477 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ len
;
480 ret
= set_cookie(hostName
, path
, cookie
, data
);
482 HeapFree(GetProcessHeap(), 0, cookie
);
485 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
);
489 /***********************************************************************
490 * InternetSetCookieA (WININET.@)
492 * Sets cookie for the specified url
499 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
503 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
506 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
507 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
511 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
512 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
513 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
518 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
519 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
520 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
525 len
= MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, NULL
, 0 );
526 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
527 MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, szCookieData
, len
);
530 r
= InternetSetCookieW( szUrl
, szCookieName
, szCookieData
);
532 HeapFree( GetProcessHeap(), 0, szCookieData
);
533 HeapFree( GetProcessHeap(), 0, szCookieName
);
534 HeapFree( GetProcessHeap(), 0, szUrl
);
539 /***********************************************************************
540 * InternetSetCookieExA (WININET.@)
542 * See InternetSetCookieExW.
544 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
545 DWORD dwFlags
, DWORD_PTR dwReserved
)
547 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
548 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
549 dwFlags
, dwReserved
);
553 /***********************************************************************
554 * InternetSetCookieExW (WININET.@)
556 * Sets a cookie for the specified URL.
563 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
564 DWORD dwFlags
, DWORD_PTR dwReserved
)
566 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
567 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
568 dwFlags
, dwReserved
);
572 /***********************************************************************
573 * InternetGetCookieExA (WININET.@)
575 * See InternetGetCookieExW.
577 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
578 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
580 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
581 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
582 pcchCookieData
, dwFlags
, lpReserved
);
586 /***********************************************************************
587 * InternetGetCookieExW (WININET.@)
589 * Retrieve cookie for the specified URL.
596 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
597 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
599 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
600 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
601 pcchCookieData
, dwFlags
, lpReserved
);
605 /***********************************************************************
606 * InternetClearAllPerSiteCookieDecisions (WININET.@)
608 * Clears all per-site decisions about cookies.
615 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
621 /***********************************************************************
622 * InternetEnumPerSiteCookieDecisionA (WININET.@)
624 * See InternetEnumPerSiteCookieDecisionW.
626 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, unsigned long *pcSiteNameSize
,
627 unsigned long *pdwDecision
, unsigned long dwIndex
)
629 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
630 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
634 /***********************************************************************
635 * InternetEnumPerSiteCookieDecisionW (WININET.@)
637 * Enumerates all per-site decisions about cookies.
644 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, unsigned long *pcSiteNameSize
,
645 unsigned long *pdwDecision
, unsigned long dwIndex
)
647 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
648 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
652 /***********************************************************************
653 * InternetGetPerSiteCookieDecisionA (WININET.@)
655 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, unsigned long *pResult
)
657 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
661 /***********************************************************************
662 * InternetGetPerSiteCookieDecisionW (WININET.@)
664 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, unsigned long *pResult
)
666 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
670 /***********************************************************************
671 * InternetSetPerSiteCookieDecisionA (WININET.@)
673 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
675 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
679 /***********************************************************************
680 * InternetSetPerSiteCookieDecisionW (WININET.@)
682 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
684 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);