@@ -34,22 +34,28 @@ def __init__(self, requestDict=None):
34
34
self ._environ = requestDict ['environ' ]
35
35
self ._input = requestDict ['input' ]
36
36
self ._requestID = requestDict ['requestID' ]
37
+ # Protect the loading of fields with an exception handler,
38
+ # because bad headers sometimes can break the field storage
39
+ # (see also https://bugs.python.org/issue27777).
37
40
try :
38
41
self ._fields = FieldStorage .FieldStorage (
39
42
self ._input , environ = self ._environ ,
40
43
keep_blank_values = True , strict_parsing = False )
41
44
except Exception :
42
- # Protect the loading of fields with an exception handler,
43
- # because bad headers sometimes can break the field storage
44
- # (see also https://bugs.python.org/issue27777).
45
45
self ._fields = cgi .FieldStorage (keep_blank_values = True )
46
46
traceback .print_exc (file = sys .stderr )
47
47
self ._cookies = Cookie ()
48
48
if 'HTTP_COOKIE' in self ._environ :
49
+ # If there are duplicate cookies, always use the first one
50
+ # because it is the most relevant one according to RFC 2965
51
+ # (workaround for https://bugs.python.org/issue1375011).
52
+ # noinspection PyTypeChecker
53
+ cookies = dict (cookie .split ('=' , 1 ) for cookie in reversed (
54
+ self ._environ ['HTTP_COOKIE' ].split ('; ' )))
49
55
# Protect the loading of cookies with an exception handler,
50
56
# because MSIE cookies sometimes can break the cookie module.
51
57
try :
52
- self ._cookies .load (self . _environ [ 'HTTP_COOKIE' ] )
58
+ self ._cookies .load (cookies )
53
59
except Exception :
54
60
traceback .print_exc (file = sys .stderr )
55
61
else :
0 commit comments