diff -r dec10a3eb95f Doc/library/cookie.rst --- a/Doc/library/cookie.rst Sat Feb 23 08:19:00 2013 +0200 +++ b/Doc/library/cookie.rst Sat Feb 23 17:08:03 2013 +0100 @@ -145,7 +145,8 @@ .. method:: BaseCookie.load(rawdata) If *rawdata* is a string, parse it as an ``HTTP_COOKIE`` and add the values - found there as :class:`Morsel`\ s. If it is a dictionary, it is equivalent to:: + found there as :class:`Morsel`\ s. In case the string contains duplicate + keys, the first value is used. If it is a dictionary, it is equivalent to:: for k, v in rawdata.items(): cookie[k] = v diff -r dec10a3eb95f Lib/Cookie.py --- a/Lib/Cookie.py Sat Feb 23 08:19:00 2013 +0200 +++ b/Lib/Cookie.py Sat Feb 23 17:08:03 2013 +0100 @@ -226,6 +226,7 @@ _semispacejoin = '; '.join _spacejoin = ' '.join + # # Define an exception visible to External modules # @@ -641,6 +642,7 @@ i = 0 # Our starting point n = len(str) # Length of string M = None # current morsel + keys = set() # Keys found in string while 0 <= i < n: # Start looking for a cookie @@ -650,6 +652,13 @@ K,V = match.group("key"), match.group("val") i = match.end(0) + # If a key is duplicated in the string, + # only use the first value and ignore duplicates + if K in keys: + continue + else: + keys.add(K) + # Parse the key, value in case it's metainfo if K[0] == "$": # We ignore attributes which pertain to the cookie diff -r dec10a3eb95f Lib/test/test_cookie.py --- a/Lib/test/test_cookie.py Sat Feb 23 08:19:00 2013 +0200 +++ b/Lib/test/test_cookie.py Sat Feb 23 17:08:03 2013 +0100 @@ -21,6 +21,13 @@ 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', }, + # Check that first value for a duplicate key is used + { 'data': 'duplicate=first; duplicate=second', + 'dict': {'duplicate':'first'}, + 'repr': "", + 'output': 'Set-Cookie: duplicate=first', + }, + # Check illegal cookies that have an '=' char in an unquoted value { 'data': 'keebler=E=mc2', 'dict': {'keebler' : 'E=mc2'},