Hans Leidekker : wininet: Fix a number of problems with InternetSetCookie.

Alexandre Julliard julliard at winehq.org
Thu Nov 15 07:45:02 CST 2007


Module: wine
Branch: master
Commit: b6ee3c1d6bbecb5d8fda0da6eb45500ae1285293
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=b6ee3c1d6bbecb5d8fda0da6eb45500ae1285293

Author: Hans Leidekker <hans at it.vu.nl>
Date:   Wed Nov 14 13:15:16 2007 +0100

wininet: Fix a number of problems with InternetSetCookie.

---

 dlls/wininet/cookie.c         |  103 ++++++++++++++++++++++------------------
 dlls/wininet/tests/internet.c |    6 ++-
 2 files changed, 61 insertions(+), 48 deletions(-)

diff --git a/dlls/wininet/cookie.c b/dlls/wininet/cookie.c
index f5f3975..a7445b8 100644
--- a/dlls/wininet/cookie.c
+++ b/dlls/wininet/cookie.c
@@ -174,7 +174,7 @@ static cookie_domain *COOKIE_addDomain(LPCWSTR domain, LPCWSTR path)
     return newDomain;
 }
 
-static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
+static BOOL COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostNameLen, LPWSTR path, int pathLen)
 {
     URL_COMPONENTSW UrlComponents;
 
@@ -191,7 +191,7 @@ static void COOKIE_crackUrlSimple(LPCWSTR lpszUrl, LPWSTR hostName, int hostName
     UrlComponents.dwHostNameLength = hostNameLen;
     UrlComponents.dwUrlPathLength = pathLen;
 
-    InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
+    return InternetCrackUrlW(lpszUrl, 0, 0, &UrlComponents);
 }
 
 /* match a domain. domain must match if the domain is not NULL. path must match if the path is not NULL */
@@ -392,6 +392,34 @@ BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
     return r;
 }
 
+static BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cookie_data)
+{
+    cookie_domain *thisCookieDomain = NULL;
+    cookie *thisCookie;
+    struct list *cursor;
+
+    LIST_FOR_EACH(cursor, &domain_list)
+    {
+        thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
+        if (COOKIE_matchDomain(domain, NULL /* FIXME: path */, thisCookieDomain, FALSE))
+            break;
+        thisCookieDomain = NULL;
+    }
+
+    if (!thisCookieDomain)
+        thisCookieDomain = COOKIE_addDomain(domain, path);
+
+    if ((thisCookie = COOKIE_findCookie(thisCookieDomain, cookie_name)))
+        COOKIE_deleteCookie(thisCookie, FALSE);
+
+    TRACE("setting cookie %s=%s for domain %s\n", debugstr_w(cookie_name),
+          debugstr_w(cookie_data), debugstr_w(thisCookieDomain->lpCookieDomain));
+
+    if (!COOKIE_addCookie(thisCookieDomain, cookie_name, cookie_data))
+        return FALSE;
+
+    return TRUE;
+}
 
 /***********************************************************************
  *           InternetSetCookieW (WININET.@)
@@ -406,64 +434,47 @@ BOOL WINAPI InternetGetCookieA(LPCSTR lpszUrl, LPCSTR lpszCookieName,
 BOOL WINAPI InternetSetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
     LPCWSTR lpCookieData)
 {
-    cookie_domain *thisCookieDomain = NULL;
-    cookie *thisCookie;
+    BOOL ret;
     WCHAR hostName[2048], path[2048];
-    struct list * cursor;
 
     TRACE("(%s,%s,%s)\n", debugstr_w(lpszUrl),
         debugstr_w(lpszCookieName), debugstr_w(lpCookieData));
 
-    if (!lpCookieData)
+    if (!lpszUrl || !lpCookieData)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
-	return FALSE;
+        return FALSE;
     }
+
+    hostName[0] = 0;
+    ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
+    if (!ret || !hostName[0]) return FALSE;
+
     if (!lpszCookieName)
     {
-	/* some apps (or is it us??) try to add a cookie with no cookie name, but
-         * the cookie data in the form of name=data. */
-	/* FIXME, probably a bug here, for now I don't care */
-	WCHAR *ourCookieName, *ourCookieData;
-	int ourCookieNameSize;
-        BOOL ret;
-
-	if (!(ourCookieData = strchrW(lpCookieData, '=')))
-	{
-            TRACE("something terribly wrong with cookie data %s\n", 
-                   debugstr_w(ourCookieData));
-	    return FALSE;
-	}
-	ourCookieNameSize = ourCookieData - lpCookieData;
-	ourCookieData += 1;
-	ourCookieName = HeapAlloc(GetProcessHeap(), 0, 
-                              (ourCookieNameSize + 1)*sizeof(WCHAR));
-	memcpy(ourCookieName, lpCookieData, ourCookieNameSize * sizeof(WCHAR));
-	ourCookieName[ourCookieNameSize] = '\0';
-	TRACE("setting (hacked) cookie of %s, %s\n",
-               debugstr_w(ourCookieName), debugstr_w(ourCookieData));
-        ret = InternetSetCookieW(lpszUrl, ourCookieName, ourCookieData);
-	HeapFree(GetProcessHeap(), 0, ourCookieName);
-        return ret;
-    }
+        unsigned int len;
+        WCHAR *cookie, *data;
 
-    COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
+        len = strlenW(lpCookieData);
+        if (!(cookie = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR))))
+        {
+            SetLastError(ERROR_OUTOFMEMORY);
+            return FALSE;
+        }
+        strcpyW(cookie, lpCookieData);
 
-    LIST_FOR_EACH(cursor, &domain_list)
-    {
-        thisCookieDomain = LIST_ENTRY(cursor, cookie_domain, entry);
-        if (COOKIE_matchDomain(hostName, NULL /* FIXME: path */, thisCookieDomain, FALSE))
-            break;
-        thisCookieDomain = NULL;
-    }
-    if (!thisCookieDomain)
-        thisCookieDomain = COOKIE_addDomain(hostName, path);
+        /* some apps (or is it us??) try to add a cookie with no cookie name, but
+         * the cookie data in the form of name[=data].
+         */
+        if (!(data = strchrW(cookie, '='))) data = cookie + len;
+        else data++;
 
-    if ((thisCookie = COOKIE_findCookie(thisCookieDomain, lpszCookieName)))
-	COOKIE_deleteCookie(thisCookie, FALSE);
+        ret = set_cookie(hostName, path, cookie, data);
 
-    thisCookie = COOKIE_addCookie(thisCookieDomain, lpszCookieName, lpCookieData);
-    return TRUE;
+        HeapFree(GetProcessHeap(), 0, cookie);
+        return ret;
+    }
+    return set_cookie(hostName, path, lpszCookieName, lpCookieData);
 }
 
 
diff --git a/dlls/wininet/tests/internet.c b/dlls/wininet/tests/internet.c
index c441bf2..8a83132 100644
--- a/dlls/wininet/tests/internet.c
+++ b/dlls/wininet/tests/internet.c
@@ -218,6 +218,7 @@ static void test_null(void)
   static const WCHAR szServer[] = { 's','e','r','v','e','r',0 };
   static const WCHAR szEmpty[] = { 0 };
   static const WCHAR szUrl[] = { 'h','t','t','p',':','/','/','a','.','b','.','c',0 };
+  static const WCHAR szUrlEmpty[] = { 'h','t','t','p',':','/','/',0 };
   static const WCHAR szExpect[] = { 's','e','r','v','e','r',';',' ','s','e','r','v','e','r',0 };
   WCHAR buffer[0x20];
   BOOL r;
@@ -267,14 +268,15 @@ static void test_null(void)
   r = InternetSetCookieW(szUrl, szServer, szServer);
   ok(r == TRUE, "return wrong\n");
 
-  todo_wine {
   r = InternetSetCookieW(szUrl, NULL, szServer);
   ok(r == TRUE, "return wrong\n");
-  }
 
   r = InternetSetCookieW(szUrl, szServer, szEmpty);
   ok(r == TRUE, "return wrong\n");
 
+  r = InternetSetCookieW(szUrlEmpty, szServer, szServer);
+  ok(r == FALSE, "return wrong\n");
+
   r = InternetSetCookieW(szServer, NULL, szServer);
   todo_wine {
   ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n");




More information about the wine-cvs mailing list