[mod_python] Can i have both Marshal and Signed CookieswithPublisher?

Graham Dumpleton grahamd at dscpl.com.au
Thu Nov 2 04:23:56 EST 2006


On 01/11/2006, at 12:49 AM, Clodoaldo Pinto Neto wrote:

> 2006/10/31, Clodoaldo Pinto Neto <clodoaldo.pinto at gmail.com>:
>> >     def parse(Class, s, secret, downgrade=False, strict=False):
>>
>> Sorry, should be downgrade=True to not break existing applications.
>>
>> Regards, Clodoaldo Pinto Neto
>>
>
> And test not downgrade:
>
>    def parse(Class, s, secret, downgrade=False, strict=False):
>
>       dict = _parse_cookie(s, Class)
>
>       del_list = list()
>       for k in dict:
>           c = dict[k]
>           try:
>               c.unmarshal(secret)
>           except (CookieError, ValueError):
>              if not downgrade:
>                  del_list.append(k)
>              else:
>                  if strict:
>                      raise
>                  else:
>                      # downgrade to Cookie
>                      dict[k] = Cookie.parse(Cookie.__str__(c))[k]
>
>       for k in del_list:
>           del dict(k)
>       return dict

Except that we are now using two options to represent what should be
three exclusive possibilities. Possibly better off using a single  
argument
which can be assigned three values representing the possibilities of
what to do when a mismatch occurs.

1. Downgrade cookie if mismatch.
2. Ignore cookie if mismatch.
3. Raise exception if mismatch.

In some respects, it is actually wrong that the downgrading is being  
done
within the parse() method of the derived classes. The derived class  
should
really have let the exception be passed back. Instead I would have  
had the
get_cookies() method catch the exception and downgrade the cookie at
that point. Anyway, that is the way I would have designed it if  
starting from
scratch, but how it works is how it is documented and can't going  
changing
that now.

Back to the options, I'll have to think about what names make sense, but
the intent is something like:

   MISMATCH_DOWNGRADE = 0
   MISMATCH_IGNORE = 1
   MISMATCH_EXCEPTION = 2

   class MarshalCookie(Cookie):

      def parse(Class, s, secret, mismatch=MISMATCH_DOWNGRADE):

       dict = _parse_cookie(s, Class)

       del_list = list()
       for k in dict:
           c = dict[k]
           try:
               c.unmarshal(secret)
           except (CookieError, ValueError):
               if mismatch == MISMATCH_EXCEPTION:
                      raise
               elif mismatch == MISMATCH_IGNORE:
                      del_list.append(k)
               else:
                      # downgrade to Cookie
                      dict[k] = Cookie.parse(Cookie.__str__(c))[k]

       for k in del_list:
           del dict(k)
       return dict

I don't like the name 'mismatch' though. Maybe make them non namespaced
class attributes of Cookie instead. Thus you would ultimately write:

   cookies = Cookies.get_cookies(req, mismatch=Cookies.Cookie.IGNORE)

Any other suggestions?

Graham


More information about the Mod_python mailing list