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
)
261 struct list
* cursor
;
262 int cnt
= 0, domain_count
= 0;
263 int 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_INTERNET_UNRECOGNIZED_SCHEME
);
275 COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
277 LIST_FOR_EACH(cursor
, &domain_list
)
279 cookie_domain
*cookiesDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
280 if (COOKIE_matchDomain(hostName
, NULL
/* FIXME: path */, cookiesDomain
, TRUE
))
282 struct list
* cursor
;
284 TRACE("found domain %p\n", cookiesDomain
);
286 LIST_FOR_EACH(cursor
, &cookiesDomain
->cookie_list
)
288 cookie
*thisCookie
= LIST_ENTRY(cursor
, cookie
, entry
);
289 if (lpCookieData
== NULL
) /* return the size of the buffer required to lpdwSize */
291 if (cookie_count
!= 0)
293 cnt
+= strlenW(thisCookie
->lpCookieName
);
295 cnt
+= strlenW(thisCookie
->lpCookieData
);
299 static const WCHAR szsc
[] = { ';',' ',0 };
300 static const WCHAR szpseq
[] = { '%','s','=','%','s',0 };
301 if (cookie_count
!= 0)
302 cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szsc
);
303 cnt
+= snprintfW(lpCookieData
+ cnt
, *lpdwSize
- cnt
, szpseq
,
304 thisCookie
->lpCookieName
,
305 thisCookie
->lpCookieData
);
306 TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie
->lpCookieName
), debugstr_w(thisCookie
->lpCookieData
));
315 TRACE("no cookies found for %s\n", debugstr_w(hostName
));
316 SetLastError(ERROR_NO_MORE_ITEMS
);
320 if (lpCookieData
== NULL
)
323 *lpdwSize
= cnt
*sizeof(WCHAR
);
324 TRACE("returning\n");
328 *lpdwSize
= (cnt
+ 1)*sizeof(WCHAR
);
330 TRACE("Returning %i (from %i domains): %s\n", cnt
, domain_count
,
331 debugstr_w(lpCookieData
));
333 return (cnt
? TRUE
: FALSE
);
337 /***********************************************************************
338 * InternetGetCookieA (WININET.@)
340 * Retrieve cookie from the specified url
347 BOOL WINAPI
InternetGetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
348 LPSTR lpCookieData
, LPDWORD lpdwSize
)
351 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
354 TRACE("(%s,%s,%p)\n", debugstr_a(lpszUrl
), debugstr_a(lpszCookieName
),
359 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
360 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
361 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
366 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
367 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
368 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
371 r
= InternetGetCookieW( szUrl
, szCookieName
, NULL
, &len
);
374 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
381 r
= InternetGetCookieW( szUrl
, szCookieName
, szCookieData
, &len
);
383 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
384 lpCookieData
, *lpdwSize
, NULL
, NULL
);
388 HeapFree( GetProcessHeap(), 0, szCookieData
);
389 HeapFree( GetProcessHeap(), 0, szCookieName
);
390 HeapFree( GetProcessHeap(), 0, szUrl
);
395 static BOOL
set_cookie(LPCWSTR domain
, LPCWSTR path
, LPCWSTR cookie_name
, LPCWSTR cookie_data
)
397 cookie_domain
*thisCookieDomain
= NULL
;
401 LIST_FOR_EACH(cursor
, &domain_list
)
403 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
404 if (COOKIE_matchDomain(domain
, NULL
/* FIXME: path */, thisCookieDomain
, FALSE
))
406 thisCookieDomain
= NULL
;
409 if (!thisCookieDomain
)
410 thisCookieDomain
= COOKIE_addDomain(domain
, path
);
412 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, cookie_name
)))
413 COOKIE_deleteCookie(thisCookie
, FALSE
);
415 TRACE("setting cookie %s=%s for domain %s\n", debugstr_w(cookie_name
),
416 debugstr_w(cookie_data
), debugstr_w(thisCookieDomain
->lpCookieDomain
));
418 if (!COOKIE_addCookie(thisCookieDomain
, cookie_name
, cookie_data
))
424 /***********************************************************************
425 * InternetSetCookieW (WININET.@)
427 * Sets cookie for the specified url
434 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
435 LPCWSTR lpCookieData
)
438 WCHAR hostName
[2048], path
[2048];
440 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
441 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
443 if (!lpszUrl
|| !lpCookieData
)
445 SetLastError(ERROR_INVALID_PARAMETER
);
450 ret
= COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
451 if (!ret
|| !hostName
[0]) return FALSE
;
456 WCHAR
*cookie
, *data
;
458 len
= strlenW(lpCookieData
);
459 if (!(cookie
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
))))
461 SetLastError(ERROR_OUTOFMEMORY
);
464 strcpyW(cookie
, lpCookieData
);
466 /* some apps (or is it us??) try to add a cookie with no cookie name, but
467 * the cookie data in the form of name[=data].
469 if (!(data
= strchrW(cookie
, '='))) data
= cookie
+ len
;
472 ret
= set_cookie(hostName
, path
, cookie
, data
);
474 HeapFree(GetProcessHeap(), 0, cookie
);
477 return set_cookie(hostName
, path
, lpszCookieName
, lpCookieData
);
481 /***********************************************************************
482 * InternetSetCookieA (WININET.@)
484 * Sets cookie for the specified url
491 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
495 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
498 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
499 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
503 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
504 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
505 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
510 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
511 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
512 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
517 len
= MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, NULL
, 0 );
518 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
519 MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, szCookieData
, len
);
522 r
= InternetSetCookieW( szUrl
, szCookieName
, szCookieData
);
524 HeapFree( GetProcessHeap(), 0, szCookieData
);
525 HeapFree( GetProcessHeap(), 0, szCookieName
);
526 HeapFree( GetProcessHeap(), 0, szUrl
);
531 /***********************************************************************
532 * InternetSetCookieExA (WININET.@)
534 * See InternetSetCookieExW.
536 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
537 DWORD dwFlags
, DWORD_PTR dwReserved
)
539 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
540 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
541 dwFlags
, dwReserved
);
545 /***********************************************************************
546 * InternetSetCookieExW (WININET.@)
548 * Sets a cookie for the specified URL.
555 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
556 DWORD dwFlags
, DWORD_PTR dwReserved
)
558 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
559 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
560 dwFlags
, dwReserved
);
564 /***********************************************************************
565 * InternetGetCookieExA (WININET.@)
567 * See InternetGetCookieExW.
569 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
570 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
572 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
573 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
574 pcchCookieData
, dwFlags
, lpReserved
);
578 /***********************************************************************
579 * InternetGetCookieExW (WININET.@)
581 * Retrieve cookie for the specified URL.
588 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
589 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
591 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
592 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
593 pcchCookieData
, dwFlags
, lpReserved
);
597 /***********************************************************************
598 * InternetClearAllPerSiteCookieDecisions (WININET.@)
600 * Clears all per-site decisions about cookies.
607 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
613 /***********************************************************************
614 * InternetEnumPerSiteCookieDecisionA (WININET.@)
616 * See InternetEnumPerSiteCookieDecisionW.
618 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, unsigned long *pcSiteNameSize
,
619 unsigned long *pdwDecision
, unsigned long dwIndex
)
621 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
622 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
626 /***********************************************************************
627 * InternetEnumPerSiteCookieDecisionW (WININET.@)
629 * Enumerates all per-site decisions about cookies.
636 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, unsigned long *pcSiteNameSize
,
637 unsigned long *pdwDecision
, unsigned long dwIndex
)
639 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
640 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
644 /***********************************************************************
645 * InternetGetPerSiteCookieDecisionA (WININET.@)
647 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, unsigned long *pResult
)
649 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
653 /***********************************************************************
654 * InternetGetPerSiteCookieDecisionW (WININET.@)
656 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, unsigned long *pResult
)
658 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
662 /***********************************************************************
663 * InternetSetPerSiteCookieDecisionA (WININET.@)
665 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
667 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
671 /***********************************************************************
672 * InternetSetPerSiteCookieDecisionW (WININET.@)
674 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
676 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);