Ejemplo n.º 1
0
    def getCookies(self, cookieClass=None, secret=None):
        """Return a reference to the request cookies dictionary.
        Wrap in this method because the access is method is different
        depending on whether this is a CGI or PSP call.
        """
        cookieDict = None
        if self.req is not None:
            # PSP call
            import mod_python.Cookie
            if cookieClass == "signed" and secret is not None:
                #Signed Cookie
                cookieDict = mod_python.Cookie.get_cookies(
                    self.req, mod_python.Cookie.SignedCookie, secret=secret)
            else:
                #Non-signed, regular, less secure, user modifiable cookies
                cookieDict = mod_python.Cookie.get_cookies(self.req)
        else:
            # Assume is a CGI call
            import Cookie
            cookieDict = Cookie.SimpleCookie()
            if "HTTP_COOKIE" in os.environ:
                cookieDict.load(os.environ["HTTP_COOKIE"])

                #Set all default attributes, because they were all lost
                #Actually this does not have the desired affect, all cookies should be set to root path, but they are not.
                #Leaving it in here, perhaps some servers/clients support this
                for cookieName in cookieDict:
                    cookie = cookieDict[cookieName]
                    cookie.path = '/'
        return cookieDict
Ejemplo n.º 2
0
    def addCookie(self,
                  key,
                  value,
                  cookieClass=None,
                  secret=None,
                  cookieAttributes=[]):
        """Simplest case of adding a name-value pair cookie for session persistence.
        """
        if self.req is not None:
            # PSP call
            import mod_python.Cookie
            # Check to see if this should be a signed cookie, or a regular cookie
            cookie = mod_python.Cookie.Cookie(key, value)

            #Go through and assign all cookie attributes, I know this is cool right?  Variable vaiables!
            for attribute in cookieAttributes:
                if isinstance(attribute['value'], int):
                    exec "cookie.%s = %s" % (attribute['name'],
                                             attribute['value'])
                else:
                    exec "cookie.%s = '%s'" % (attribute['name'],
                                               attribute['value'])

            #Add cookie to header
            mod_python.Cookie.add_cookie(self.req, cookie)
        else:
            # Assume is a CGI call
            import Cookie
            if os.environ.has_key("HTTP_COOKIE"):
                cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
            else:
                cookie = Cookie.SimpleCookie()

            #Define cookie name and value
            cookie[key] = value

            #Go through and assign all cookie attributes
            for attribute in cookieAttributes:
                cookie[key][attribute['name']] = attribute['value']

            #This is the actual header output and will eventually happen in printHeaders response method
            os.environ["HTTP_COOKIE"] = cookie.output(header='')