From 98b850b2850b1b6685b760019fff256bbb424c96 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= <danmichaelo@gmail.com>
Date: Fri, 2 May 2014 11:09:02 +0200
Subject: [PATCH] [#38] Quickfix for cookies not being removed from CookieJar

---
 RELEASE-NOTES.md |  2 ++
 mwclient/http.py | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 408ac6e..718e1c0 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -50,6 +50,8 @@
   [provided by Semantic MediaWiki](http://semantic-mediawiki.org/wiki/Ask_API).
   (by [@kyv](https://github.com/kyv))
   [0a16afc](https://github.com/kyv/mwclient/commit/0a16afc)
+* [2014-05-02] Quickfix for [https://github.com/mwclient/mwclient/issues/38 #38]
+  (by [@danmichaelo](https://github.com/danmichaelo))
 
 ## Changes in version 0.6.5
 Mwclient 0.6.5 was released on 6 May 2011
diff --git a/mwclient/http.py b/mwclient/http.py
index cb2b2a8..1d30964 100644
--- a/mwclient/http.py
+++ b/mwclient/http.py
@@ -27,9 +27,19 @@ class CookieJar(dict):
             return
         value, attrs = cookie.split(': ', 1)[1].split(';', 1)
         i = value.strip().split('=')
-        if len(i) == 1 and i[0] in self:
+
+        # This CookieJar class doesn't care about the `expires` attribute,
+        # but remove cookies when they are emptied. At some point, the
+        # mediawiki API started setting the value of cookies to-be-removed
+        # to 'deleted' instead of emptying them. As a quick fix for
+        # <https://github.com/mwclient/mwclient/issues/38>, we will remove
+        # cookies with the value 'deleted' as well, but we should eventually
+        # replace this class, and probably the whole http class, by something
+        # more robust. The `requests` library looks like a promising candidate.
+
+        if (len(i) == 1 and i[0] in self) or (len(i) == 2 and i[0] in self and i[1] == 'deleted'):
             del self[i[0]]
-        else:
+        elif len(i) == 2 and i[1] != 'deleted':
             self[i[0]] = i[1]
 
     def get_cookie_header(self):
-- 
GitLab