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
) );
374 r
= InternetGetCookieW( szUrl
, szCookieName
, szCookieData
, &len
);
376 *lpdwSize
= WideCharToMultiByte( CP_ACP
, 0, szCookieData
, len
,
377 lpCookieData
, *lpdwSize
, NULL
, NULL
);
380 HeapFree( GetProcessHeap(), 0, szCookieData
);
381 HeapFree( GetProcessHeap(), 0, szCookieName
);
382 HeapFree( GetProcessHeap(), 0, szUrl
);
388 /***********************************************************************
389 * InternetSetCookieW (WININET.@)
391 * Sets cookie for the specified url
398 BOOL WINAPI
InternetSetCookieW(LPCWSTR lpszUrl
, LPCWSTR lpszCookieName
,
399 LPCWSTR lpCookieData
)
401 cookie_domain
*thisCookieDomain
= NULL
;
403 WCHAR hostName
[2048], path
[2048];
404 struct list
* cursor
;
406 TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl
),
407 debugstr_w(lpszCookieName
), debugstr_w(lpCookieData
));
411 SetLastError(ERROR_INVALID_PARAMETER
);
416 /* some apps (or is it us??) try to add a cookie with no cookie name, but
417 * the cookie data in the form of name=data. */
418 /* FIXME, probably a bug here, for now I don't care */
419 WCHAR
*ourCookieName
, *ourCookieData
;
420 int ourCookieNameSize
;
423 if (!(ourCookieData
= strchrW(lpCookieData
, '=')))
425 TRACE("something terribly wrong with cookie data %s\n",
426 debugstr_w(ourCookieData
));
429 ourCookieNameSize
= ourCookieData
- lpCookieData
;
431 ourCookieName
= HeapAlloc(GetProcessHeap(), 0,
432 (ourCookieNameSize
+ 1)*sizeof(WCHAR
));
433 memcpy(ourCookieName
, lpCookieData
, ourCookieNameSize
* sizeof(WCHAR
));
434 ourCookieName
[ourCookieNameSize
] = '\0';
435 TRACE("setting (hacked) cookie of %s, %s\n",
436 debugstr_w(ourCookieName
), debugstr_w(ourCookieData
));
437 ret
= InternetSetCookieW(lpszUrl
, ourCookieName
, ourCookieData
);
438 HeapFree(GetProcessHeap(), 0, ourCookieName
);
442 COOKIE_crackUrlSimple(lpszUrl
, hostName
, sizeof(hostName
)/sizeof(hostName
[0]), path
, sizeof(path
)/sizeof(path
[0]));
444 LIST_FOR_EACH(cursor
, &domain_list
)
446 thisCookieDomain
= LIST_ENTRY(cursor
, cookie_domain
, entry
);
447 if (COOKIE_matchDomain(hostName
, NULL
/* FIXME: path */, thisCookieDomain
, FALSE
))
449 thisCookieDomain
= NULL
;
451 if (!thisCookieDomain
)
452 thisCookieDomain
= COOKIE_addDomain(hostName
, path
);
454 if ((thisCookie
= COOKIE_findCookie(thisCookieDomain
, lpszCookieName
)))
455 COOKIE_deleteCookie(thisCookie
, FALSE
);
457 thisCookie
= COOKIE_addCookie(thisCookieDomain
, lpszCookieName
, lpCookieData
);
462 /***********************************************************************
463 * InternetSetCookieA (WININET.@)
465 * Sets cookie for the specified url
472 BOOL WINAPI
InternetSetCookieA(LPCSTR lpszUrl
, LPCSTR lpszCookieName
,
476 LPWSTR szCookieData
= NULL
, szUrl
= NULL
, szCookieName
= NULL
;
479 TRACE("(%s,%s,%s)\n", debugstr_a(lpszUrl
),
480 debugstr_a(lpszCookieName
), debugstr_a(lpCookieData
));
484 len
= MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, NULL
, 0 );
485 szUrl
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
486 MultiByteToWideChar( CP_ACP
, 0, lpszUrl
, -1, szUrl
, len
);
491 len
= MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, NULL
, 0 );
492 szCookieName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
493 MultiByteToWideChar( CP_ACP
, 0, lpszCookieName
, -1, szCookieName
, len
);
498 len
= MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, NULL
, 0 );
499 szCookieData
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
500 MultiByteToWideChar( CP_ACP
, 0, lpCookieData
, -1, szCookieData
, len
);
503 r
= InternetSetCookieW( szUrl
, szCookieName
, szCookieData
);
505 HeapFree( GetProcessHeap(), 0, szCookieData
);
506 HeapFree( GetProcessHeap(), 0, szCookieName
);
507 HeapFree( GetProcessHeap(), 0, szUrl
);
512 /***********************************************************************
513 * InternetSetCookieExA (WININET.@)
515 * See InternetSetCookieExW.
517 DWORD WINAPI
InternetSetCookieExA( LPCSTR lpszURL
, LPCSTR lpszCookieName
, LPCSTR lpszCookieData
,
518 DWORD dwFlags
, DWORD_PTR dwReserved
)
520 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
521 debugstr_a(lpszURL
), debugstr_a(lpszCookieName
), debugstr_a(lpszCookieData
),
522 dwFlags
, dwReserved
);
526 /***********************************************************************
527 * InternetSetCookieExW (WININET.@)
529 * Sets a cookie for the specified URL.
536 DWORD WINAPI
InternetSetCookieExW( LPCWSTR lpszURL
, LPCWSTR lpszCookieName
, LPCWSTR lpszCookieData
,
537 DWORD dwFlags
, DWORD_PTR dwReserved
)
539 FIXME("(%s, %s, %s, 0x%08x, 0x%08lx) stub\n",
540 debugstr_w(lpszURL
), debugstr_w(lpszCookieName
), debugstr_w(lpszCookieData
),
541 dwFlags
, dwReserved
);
545 /***********************************************************************
546 * InternetGetCookieExA (WININET.@)
548 * See InternetGetCookieExW.
550 BOOL WINAPI
InternetGetCookieExA( LPCSTR pchURL
, LPCSTR pchCookieName
, LPSTR pchCookieData
,
551 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
553 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
554 debugstr_a(pchURL
), debugstr_a(pchCookieName
), debugstr_a(pchCookieData
),
555 pcchCookieData
, dwFlags
, lpReserved
);
559 /***********************************************************************
560 * InternetGetCookieExW (WININET.@)
562 * Retrieve cookie for the specified URL.
569 BOOL WINAPI
InternetGetCookieExW( LPCWSTR pchURL
, LPCWSTR pchCookieName
, LPWSTR pchCookieData
,
570 LPDWORD pcchCookieData
, DWORD dwFlags
, LPVOID lpReserved
)
572 FIXME("(%s, %s, %s, %p, 0x%08x, %p) stub\n",
573 debugstr_w(pchURL
), debugstr_w(pchCookieName
), debugstr_w(pchCookieData
),
574 pcchCookieData
, dwFlags
, lpReserved
);
578 /***********************************************************************
579 * InternetClearAllPerSiteCookieDecisions (WININET.@)
581 * Clears all per-site decisions about cookies.
588 BOOL WINAPI
InternetClearAllPerSiteCookieDecisions( VOID
)
594 /***********************************************************************
595 * InternetEnumPerSiteCookieDecisionA (WININET.@)
597 * See InternetEnumPerSiteCookieDecisionW.
599 BOOL WINAPI
InternetEnumPerSiteCookieDecisionA( LPSTR pszSiteName
, unsigned long *pcSiteNameSize
,
600 unsigned long *pdwDecision
, unsigned long dwIndex
)
602 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
603 debugstr_a(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
607 /***********************************************************************
608 * InternetEnumPerSiteCookieDecisionW (WININET.@)
610 * Enumerates all per-site decisions about cookies.
617 BOOL WINAPI
InternetEnumPerSiteCookieDecisionW( LPWSTR pszSiteName
, unsigned long *pcSiteNameSize
,
618 unsigned long *pdwDecision
, unsigned long dwIndex
)
620 FIXME("(%s, %p, %p, 0x%08lx) stub\n",
621 debugstr_w(pszSiteName
), pcSiteNameSize
, pdwDecision
, dwIndex
);
625 /***********************************************************************
626 * InternetGetPerSiteCookieDecisionA (WININET.@)
628 BOOL WINAPI
InternetGetPerSiteCookieDecisionA( LPCSTR pwchHostName
, unsigned long *pResult
)
630 FIXME("(%s, %p) stub\n", debugstr_a(pwchHostName
), pResult
);
634 /***********************************************************************
635 * InternetGetPerSiteCookieDecisionW (WININET.@)
637 BOOL WINAPI
InternetGetPerSiteCookieDecisionW( LPCWSTR pwchHostName
, unsigned long *pResult
)
639 FIXME("(%s, %p) stub\n", debugstr_w(pwchHostName
), pResult
);
643 /***********************************************************************
644 * InternetSetPerSiteCookieDecisionA (WININET.@)
646 BOOL WINAPI
InternetSetPerSiteCookieDecisionA( LPCSTR pchHostName
, DWORD dwDecision
)
648 FIXME("(%s, 0x%08x) stub\n", debugstr_a(pchHostName
), dwDecision
);
652 /***********************************************************************
653 * InternetSetPerSiteCookieDecisionW (WININET.@)
655 BOOL WINAPI
InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName
, DWORD dwDecision
)
657 FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName
), dwDecision
);