def cookie_decode(data, key): ''' Verify and decode an encoded string. Return an object or None.''' data = to_bytes(data) if cookie_is_encoded(data): sig, msg = data.split(to_bytes('?'), 1) if _lscmp(sig[1:], base64.b64encode(hmac.new(key, msg).digest())): return pickle.loads(base64.b64decode(msg)) return None
def http_response(body, code, status, headers): """Renders arguments into an HTTP response. """ payload = {"code": code, "status": status, "body": body} content_length = 0 if body is not None: content_length = len(to_bytes(body)) headers["Content-Length"] = content_length payload["headers"] = "\r\n".join("%s: %s" % (k, v) for k, v in headers.items()) return HTTP_FORMAT % payload
def cookies(self): """Lazy generation of cookies from request headers.""" if not hasattr(self, "_cookies"): self._cookies = Cookie.SimpleCookie() if "cookie" in self.headers: try: cookies = self.headers["cookie"] self._cookies.load(to_bytes(cookies)) except Exception, e: logging.error("Failed to load cookies") self.clear_all_cookies()
def http_response(body, code, status, headers): """Renders arguments into an HTTP response. """ payload = {'code': code, 'status': status, 'body': body} content_length = 0 if body is not None: content_length = len(to_bytes(body)) headers['Content-Length'] = content_length payload['headers'] = "\r\n".join('%s: %s' % (k, v) for k, v in headers.items()) return HTTP_FORMAT % payload
def cookie_is_encoded(data): ''' Return True if the argument looks like a encoded cookie.''' return bool(data.startswith(to_bytes('!')) and to_bytes('?') in data)
def cookie_encode(data, key): """Encode and sign a pickle-able object. Return a (byte) string """ msg = base64.b64encode(pickle.dumps(data, -1)) sig = base64.b64encode(hmac.new(key, msg).digest()) return to_bytes('!') + sig + to_bytes('?') + msg
def cookie_is_encoded(data): """ Return True if the argument looks like a encoded cookie.""" return bool(data.startswith(to_bytes("!")) and to_bytes("?") in data)