Piotr Caban : wininet: Make cookies functions thread-safe.

Alexandre Julliard julliard at winehq.org
Mon Oct 1 13:35:07 CDT 2012


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

Author: Piotr Caban <piotr at codeweavers.com>
Date:   Mon Oct  1 14:23:10 2012 +0200

wininet: Make cookies functions thread-safe.

---

 dlls/wininet/cookie.c   |   31 +++++++++++++++++++++++++++++--
 dlls/wininet/internet.c |    1 +
 dlls/wininet/internet.h |    1 +
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/dlls/wininet/cookie.c b/dlls/wininet/cookie.c
index 5253b2b..0a65e4c 100644
--- a/dlls/wininet/cookie.c
+++ b/dlls/wininet/cookie.c
@@ -49,7 +49,6 @@
 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
 
 /* FIXME
- *     Cookies are NOT THREAD SAFE
  *     Cookies could use A LOT OF MEMORY. We need some kind of memory management here!
  */
 
@@ -78,6 +77,14 @@ struct _cookie_domain
     struct list cookie_list;
 };
 
+static CRITICAL_SECTION cookie_cs;
+static CRITICAL_SECTION_DEBUG cookie_cs_debug =
+{
+    0, 0, &cookie_cs,
+    { &cookie_cs_debug.ProcessLocksList, &cookie_cs_debug.ProcessLocksList },
+    0, 0, { (DWORD_PTR)(__FILE__ ": cookie_cs") }
+};
+static CRITICAL_SECTION cookie_cs = { &cookie_cs_debug, -1, 0, 0, 0, 0 };
 static struct list domain_list = LIST_INIT(domain_list);
 
 static cookie *COOKIE_addCookie(cookie_domain *domain, LPCWSTR name, LPCWSTR data,
@@ -529,6 +536,8 @@ BOOL get_cookie(const WCHAR *host, const WCHAR *path, WCHAR *cookie_data, DWORD
 
     GetSystemTimeAsFileTime(&tm);
 
+    EnterCriticalSection(&cookie_cs);
+
     load_persistent_cookie(host, path);
 
     LIST_FOR_EACH_ENTRY(domain, &domain_list, cookie_domain, entry) {
@@ -577,6 +586,8 @@ BOOL get_cookie(const WCHAR *host, const WCHAR *path, WCHAR *cookie_data, DWORD
         }
     }
 
+    LeaveCriticalSection(&cookie_cs);
+
     if (!domain_count) {
         TRACE("no cookies found for %s\n", debugstr_w(host));
         SetLastError(ERROR_NO_MORE_ITEMS);
@@ -825,6 +836,8 @@ BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cooki
         }
     }
 
+    EnterCriticalSection(&cookie_cs);
+
     load_persistent_cookie(domain, path);
 
     LIST_FOR_EACH(cursor, &domain_list)
@@ -843,6 +856,7 @@ BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cooki
         {
             heap_free(data);
             if (value != data) heap_free(value);
+            LeaveCriticalSection(&cookie_cs);
             return TRUE;
         }
     }
@@ -866,11 +880,19 @@ BOOL set_cookie(LPCWSTR domain, LPCWSTR path, LPCWSTR cookie_name, LPCWSTR cooki
     {
         heap_free(data);
         if (value != data) heap_free(value);
+        LeaveCriticalSection(&cookie_cs);
         return FALSE;
     }
     heap_free(data);
     if (value != data) heap_free(value);
-    return !update_persistent || save_persistent_cookie(thisCookieDomain);
+
+    if (!update_persistent || save_persistent_cookie(thisCookieDomain))
+    {
+        LeaveCriticalSection(&cookie_cs);
+        return TRUE;
+    }
+    LeaveCriticalSection(&cookie_cs);
+    return FALSE;
 }
 
 /***********************************************************************
@@ -1115,3 +1137,8 @@ BOOL WINAPI InternetSetPerSiteCookieDecisionW( LPCWSTR pchHostName, DWORD dwDeci
     FIXME("(%s, 0x%08x) stub\n", debugstr_w(pchHostName), dwDecision);
     return FALSE;
 }
+
+void free_cookie(void)
+{
+    DeleteCriticalSection(&cookie_cs);
+}
diff --git a/dlls/wininet/internet.c b/dlls/wininet/internet.c
index a6dbaa1..0e83389 100644
--- a/dlls/wininet/internet.c
+++ b/dlls/wininet/internet.c
@@ -315,6 +315,7 @@ BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
             collect_connections(COLLECT_CLEANUP);
             NETCON_unload();
             free_urlcache();
+            free_cookie();
 
             if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
             {
diff --git a/dlls/wininet/internet.h b/dlls/wininet/internet.h
index a4fcdaa..181ef4c 100644
--- a/dlls/wininet/internet.h
+++ b/dlls/wininet/internet.h
@@ -553,6 +553,7 @@ server_t *get_server(const WCHAR*,INTERNET_PORT,BOOL);
 
 BOOL init_urlcache(void) DECLSPEC_HIDDEN;
 void free_urlcache(void) DECLSPEC_HIDDEN;
+void free_cookie(void) DECLSPEC_HIDDEN;
 
 #define MAX_REPLY_LEN	 	0x5B4
 




More information about the wine-cvs mailing list