Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
 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()
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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)