def setcookie(self, key, value, max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False): newcookie = Morsel() newcookie.key = key newcookie.value = value newcookie.coded_value = value if max_age is not None: newcookie['max-age'] = max_age if expires is not None: newcookie['expires'] = expires if path is not None: newcookie['path'] = path if domain is not None: newcookie['domain'] = domain if secure: newcookie['secure'] = secure if httponly: newcookie['httponly'] = httponly self.sent_cookies = [c for c in self.sent_cookies if c.key != key] self.sent_cookies.append(newcookie)
def start(self, cookies, cookieopts=None): c = SimpleCookie(cookies) sid = c.get(self.cookiename) create = True if sid is not None: for m in self.get(sid.value): yield m if self.apiroutine.retvalue is not None: self.apiroutine.retvalue = (self.SessionHandle(self.apiroutine.retvalue, self.apiroutine), []) create = False if create: for m in self.create(): yield m sh = self.apiroutine.retvalue m = Morsel() m.key = self.cookiename m.value = sh.id m.coded_value = sh.id opts = {"path": "/", "httponly": True} if cookieopts: opts.update(cookieopts) if not cookieopts["httponly"]: del cookieopts["httponly"] m.update(opts) self.apiroutine.retvalue = (sh, [m])
async def start(self, cookies, cookieopts=None): """ Session start operation. First check among the cookies to find existed sessions; if there is not an existed session, create a new one. :param cookies: cookie header from the client :param cookieopts: extra options used when creating a new cookie :return: ``(session_handle, cookies)`` where session_handle is a SessionHandle object, and cookies is a list of created Set-Cookie headers (may be empty) """ c = SimpleCookie(cookies) sid = c.get(self.cookiename) create = True if sid is not None: sh = await self.get(sid.value) if sh is not None: return (self.SessionHandle(sh, self.apiroutine), []) if create: sh = await self.create() m = Morsel() m.key = self.cookiename m.value = sh.id m.coded_value = sh.id opts = {'path': '/', 'httponly': True} if cookieopts: opts.update(cookieopts) if not cookieopts['httponly']: del cookieopts['httponly'] m.update(opts) return (sh, [m])
def start(self, cookies, cookieopts=None): c = SimpleCookie(cookies) sid = c.get(self.cookiename) create = True if sid is not None: for m in self.get(sid.value): yield m if self.apiroutine.retvalue is not None: self.apiroutine.retvalue = (self.SessionHandle( self.apiroutine.retvalue, self.apiroutine), []) create = False if create: for m in self.create(): yield m sh = self.apiroutine.retvalue m = Morsel() m.key = self.cookiename m.value = sh.id m.coded_value = sh.id opts = {'path': '/', 'httponly': True} if cookieopts: opts.update(cookieopts) if not cookieopts['httponly']: del cookieopts['httponly'] m.update(opts) self.apiroutine.retvalue = (sh, [m])
def destroy(self, sessionid): for m in callAPI(self.apiroutine, "memorystorage", "delete", {"key": __name__ + "." + sessionid}): yield m m = Morsel() m.key = self.cookiename m.value = "deleted" m.coded_value = "deleted" opts = {"path": "/", "httponly": True, "max-age": 0} m.update(opts) self.apiroutine.retvalue = [m]
def destroy(self, sessionid): for m in callAPI(self.apiroutine, 'memorystorage', 'delete', {'key': __name__ + '.' + sessionid}): yield m m = Morsel() m.key = self.cookiename m.value = 'deleted' m.coded_value = 'deleted' opts = {'path': '/', 'httponly': True, 'max-age': 0} m.update(opts) self.apiroutine.retvalue = [m]
def set_cookie(self, **kwargs): key = kwargs['key'] m = Morsel() m.key = key m.value = kwargs['value'] m.coded_value = kwargs['value'] for k, v in kwargs.items(): try: m[k] = v except: pass self.cookies[key] = m self.headers['Set-Cookie'] = str(m)
def set_cookie(self, key, value=None, path='/', domain=None, secure=False, httponly=False, expires=None): morsel = Morsel() morsel.key = key morsel.coded_value = value morsel['path'] = path if expires: morsel['expires'] = expires if domain: morsel['domain'] = domain if secure: morsel['secure'] = secure if httponly: morsel['httponly'] = httponly self.append_response_header('Set-Cookie', morsel.OutputString())
def injecting_start_response(status, headers, exc_info=None): if session.should_save: morsel = Morsel() morsel.key = self.cookie_name morsel.coded_value = session.sid if self.cookie_age is not None: morsel['max-age'] = self.cookie_age morsel['expires'] = cookie_date(time() + self.cookie_age) if self.cookie_domain is not None: morsel['domain'] = self.cookie_domain if self.cookie_path is not None: morsel['path'] = self.cookie_path if self.cookie_secure is not None: morsel['secure'] = self.cookie_secure headers.append(tuple(str(morsel).split(':', 1))) return start_response(status, headers, exc_info)
async def destroy(self, sessionid): """ Destroy a session :param sessionid: session ID :return: a list of Set-Cookie headers to be sent to the client """ await call_api(self.apiroutine, 'memorystorage', 'delete', {'key': __name__ + '.' + sessionid}) m = Morsel() m.key = self.cookiename m.value = 'deleted' m.coded_value = 'deleted' opts = {'path': '/', 'httponly': True, 'max-age': 0} m.update(opts) return [m]