1

I have been working towards converting one of our applications to be threadsafe. When testing on the local dev app server everything is working as expected. However, upon deployment of the application it seems that Cookies are not being written correctly?

Within the logs there is an error with no stack trace:

2012-11-27 16:14:16.879 Set-Cookie: idd_SRP=Uyd7InRpbnlJZCI6ICJXNFdYQ1ZITSJ9JwpwMAou.Q6vNs9vGR-rmg0FkAa_P1PGBD94; expires=Wed, 28-Nov-2012 23:59:59 GMT; Path=/ 

Here is the block of code in question:

# area of the code the emits the cookie
cookie = Cookie.SimpleCookie()
if not domain:
    domain = self.__domain
self.__updateCookie(cookie, expires=expires, domain=domain)
self.__updateSessionCookie(cookie, domain=domain)
print cookie.output()

Cookie helper methods:

def __updateCookie(self, cookie, expires=None, domain=None):
    """
    Takes a Cookie.SessionCookie instance an updates it with all of the
    private persistent cookie data, expiry and domain.

    @param cookie: a Cookie.SimpleCookie instance
    @param expires: a datetime.datetime instance to use for expiry
    @param domain: a string to use for the cookie domain
    """

    cookieValue = AccountCookieManager.CookieHelper.toString(self.cookie)
    cookieName = str(AccountCookieManager.COOKIE_KEY % self.partner.pid)

    cookie[cookieName] = cookieValue
    cookie[cookieName]['path'] = '/'
    cookie[cookieName]['domain'] = domain

    if not expires:
        # set the expiry date to 1 day from now
        expires = datetime.date.today() + datetime.timedelta(days = 1)

    expiryDate = expires.strftime("%a, %d-%b-%Y 23:59:59 GMT")
    cookie[cookieName]['expires'] = expiryDate

def __updateSessionCookie(self, cookie, domain=None):
    """
    Takes a Cookie.SessionCookie instance an updates it with all of the 
    private session cookie data and domain.

    @param cookie: a Cookie.SimpleCookie instance
    @param expires: a datetime.datetime instance to use for expiry
    @param domain: a string to use for the cookie domain
    """

    cookieValue = AccountCookieManager.CookieHelper.toString(self.sessionCookie)
    cookieName = str(AccountCookieManager.SESSION_COOKIE_KEY % self.partner.pid)

    cookie[cookieName] = cookieValue
    cookie[cookieName]['path'] = '/'
    cookie[cookieName]['domain'] = domain

Again, the libraries in use are:

  • Python 2.7
  • Django 1.2

Any suggestion on what I can try?

Jesse
  • 8,223
  • 6
  • 49
  • 81
  • How do you know this is an error? – Sam Mussmann Nov 27 '12 at 23:06
  • It is being logged as an error within the google app engine logs and the cookie does not exist on subsequent requests. Also, when deploying an old version of the application these Set-Cookie errors do not appear within the logs. – Jesse Nov 27 '12 at 23:14

1 Answers1

3

I see the code for creating the cookie, but do you actually attach it to the response anywhere?

You should have a response.set_cookie() somewhere.

dragonx
  • 14,963
  • 27
  • 44
  • Thanks, dragon. I came to the same conclusion last night. The old "non threadsafe code" was routing standard out to the response stream. The class needed to be modified to use the django response set_cookie() – Jesse Nov 28 '12 at 14:56