[mod_python] Cookie problems

Wouter van Marle wouter at squirrel-systems.com
Fri Jan 27 01:38:50 EST 2006


Hi All,

I'm fiddling around with cookies, and after wondering why I always lost
my settings information I did some testing, and ran into what seems a
very very strict limit in cookie length. The code here is based on the
examples of the web pages. From the documentation I find that there is
no strict limit on the size of the value in the cookies, the only limit
I found mentioned in the archives is a 4k http header limit from Apache.
I'd assume I'm way below that.

Test 1 works correctly.

Test 2, with a slightly larger data for the cookie, fails. It is for
some reason not recognised as a valid MarshalCookie! The value used in
this case (the rest of the code being identical):
value = {'egg': 32,
         'color': 'white',
         'foo': 'bar',
         'foobar': 5}

Here the code of Test 1 (maybe with some extra line breaks due to e-mail
formatting):

from mod_python import Cookie, apache
import time

def handler(req):

    req.content_type = "text/html; charset=utf-8"

    cookies = Cookie.get_cookies(req, Cookie.MarshalCookie,
                                    secret='secret007')
    
    if cookies.has_key('spam'):
        spamcookie = cookies['spam']

        req.write('Great, a spam cookie was found: %s\n' \
                                      % str(spamcookie))
        if type(spamcookie) is Cookie.MarshalCookie:
            req.write('Here is what it looks like decoded: %s=%s\n'
                      % (spamcookie.name, spamcookie.value))
        else:
            req.write('WARNING: The cookie found is not a \
                       MarshalCookie, it may have been tapered with!')

    else:

        value = {'egg': 32,
                 'color': 'white',
                 'foo': 'bar'}
        
        Cookie.add_cookie(req, Cookie.MarshalCookie('spam', value, 'secret007'))
        req.write('Spam cookie not found, but we just set one!\n')

    return





More information about the Mod_python mailing list