wininet: Moved getting cookie from known host to separated function.

This commit is contained in:
Jacek Caban 2011-05-19 16:11:10 +02:00 committed by Alexandre Julliard
parent aca2385d6c
commit 9a741bf3d7
1 changed files with 78 additions and 82 deletions

View File

@ -260,6 +260,78 @@ static void COOKIE_deleteDomain(cookie_domain *deadDomain)
HeapFree(GetProcessHeap(), 0, deadDomain); HeapFree(GetProcessHeap(), 0, deadDomain);
} }
static BOOL get_cookie(const WCHAR *host, const WCHAR *path, WCHAR *cookie_data, DWORD *size)
{
unsigned cnt = 0, len, domain_count = 0, cookie_count = 0;
cookie_domain *domain;
FILETIME tm;
GetSystemTimeAsFileTime(&tm);
LIST_FOR_EACH_ENTRY(domain, &domain_list, cookie_domain, entry) {
struct list *cursor, *cursor2;
if(!COOKIE_matchDomain(host, path, domain, TRUE))
continue;
domain_count++;
TRACE("found domain %p\n", domain);
LIST_FOR_EACH_SAFE(cursor, cursor2, &domain->cookie_list) {
cookie *cookie_iter = LIST_ENTRY(cursor, cookie, entry);
/* check for expiry */
if((cookie_iter->expiry.dwLowDateTime != 0 || cookie_iter->expiry.dwHighDateTime != 0)
&& CompareFileTime(&tm, &cookie_iter->expiry) > 0)
{
TRACE("Found expired cookie. deleting\n");
COOKIE_deleteCookie(cookie_iter, FALSE);
continue;
}
if(!cookie_data) { /* return the size of the buffer required to lpdwSize */
if (cookie_count)
cnt += 2; /* '; ' */
cnt += strlenW(cookie_iter->lpCookieName);
if ((len = strlenW(cookie_iter->lpCookieData))) {
cnt += 1; /* = */
cnt += len;
}
}else {
static const WCHAR szsc[] = { ';',' ',0 };
static const WCHAR szname[] = { '%','s',0 };
static const WCHAR szdata[] = { '=','%','s',0 };
if (cookie_count) cnt += snprintfW(cookie_data + cnt, *size - cnt, szsc);
cnt += snprintfW(cookie_data + cnt, *size - cnt, szname, cookie_iter->lpCookieName);
if (cookie_iter->lpCookieData[0])
cnt += snprintfW(cookie_data + cnt, *size - cnt, szdata, cookie_iter->lpCookieData);
TRACE("Cookie: %s\n", debugstr_w(cookie_data));
}
cookie_count++;
}
}
if (!domain_count) {
TRACE("no cookies found for %s\n", debugstr_w(host));
SetLastError(ERROR_NO_MORE_ITEMS);
return FALSE;
}
if(!cookie_data) {
*size = (cnt + 1) * sizeof(WCHAR);
TRACE("returning %u\n", *size);
return TRUE;
}
*size = cnt + 1;
TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count, debugstr_w(cookie_data));
return cnt != 0;
}
/*********************************************************************** /***********************************************************************
* InternetGetCookieW (WININET.@) * InternetGetCookieW (WININET.@)
* *
@ -276,14 +348,10 @@ static void COOKIE_deleteDomain(cookie_domain *deadDomain)
BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName, BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
LPWSTR lpCookieData, LPDWORD lpdwSize) LPWSTR lpCookieData, LPDWORD lpdwSize)
{ {
WCHAR host[INTERNET_MAX_HOST_NAME_LENGTH], path[INTERNET_MAX_PATH_LENGTH];
BOOL ret; BOOL ret;
struct list * cursor;
unsigned int cnt = 0, domain_count = 0, cookie_count = 0;
WCHAR hostName[2048], path[2048];
FILETIME tm;
TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), lpCookieData, lpdwSize);
lpCookieData, lpdwSize);
if (!lpszUrl) if (!lpszUrl)
{ {
@ -291,83 +359,11 @@ BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
return FALSE; return FALSE;
} }
hostName[0] = 0; host[0] = 0;
ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0])); ret = COOKIE_crackUrlSimple(lpszUrl, host, sizeof(host)/sizeof(host[0]), path, sizeof(path)/sizeof(path[0]));
if (!ret || !hostName[0]) return FALSE; if (!ret || !host[0]) return FALSE;
GetSystemTimeAsFileTime(&tm); return get_cookie(host, path, lpCookieData, lpdwSize);
LIST_FOR_EACH(cursor, &domain_list)
{
cookie_domain *cookiesDomain = LIST_ENTRY(cursor, cookie_domain, entry);
if (COOKIE_matchDomain(hostName, path, cookiesDomain, TRUE))
{
struct list * cursor, * cursor2;
domain_count++;
TRACE("found domain %p\n", cookiesDomain);
LIST_FOR_EACH_SAFE(cursor, cursor2, &cookiesDomain->cookie_list)
{
cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
/* check for expiry */
if ((thisCookie->expiry.dwLowDateTime != 0 || thisCookie->expiry.dwHighDateTime != 0) && CompareFileTime(&tm,&thisCookie->expiry) > 0)
{
TRACE("Found expired cookie. deleting\n");
COOKIE_deleteCookie(thisCookie, FALSE);
continue;
}
if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
{
unsigned int len;
if (cookie_count) cnt += 2; /* '; ' */
cnt += strlenW(thisCookie->lpCookieName);
if ((len = strlenW(thisCookie->lpCookieData)))
{
cnt += 1; /* = */
cnt += len;
}
}
else
{
static const WCHAR szsc[] = { ';',' ',0 };
static const WCHAR szname[] = { '%','s',0 };
static const WCHAR szdata[] = { '=','%','s',0 };
if (cookie_count) cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szname, thisCookie->lpCookieName);
if (thisCookie->lpCookieData[0])
cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szdata, thisCookie->lpCookieData);
TRACE("Cookie: %s\n", debugstr_w(lpCookieData));
}
cookie_count++;
}
}
}
if (!domain_count)
{
TRACE("no cookies found for %s\n", debugstr_w(hostName));
SetLastError(ERROR_NO_MORE_ITEMS);
return FALSE;
}
if (lpCookieData == NULL)
{
*lpdwSize = (cnt + 1) * sizeof(WCHAR);
TRACE("returning %u\n", *lpdwSize);
return TRUE;
}
*lpdwSize = cnt + 1;
TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count,
debugstr_w(lpCookieData));
return (cnt ? TRUE : FALSE);
} }