Exemplo 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
Exemplo 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
Exemplo n.º 3
0
    def process_message(self, application, environ, callback):
        request = Request.parse_wsgi_request(environ)
        handler = application.route_message(request)
        result = handler()
        
        wsgi_status = ' '.join([str(result['status_code']), result['status_msg']])
        headers = [(k, v) for k,v in result['headers'].items()]
        callback(str(wsgi_status), headers)

        return [to_bytes(result['body'])]
Exemplo n.º 4
0
    def process_message(self, application, environ, callback):
        request = Request.parse_wsgi_request(environ)
        handler = application.route_message(request)
        result = handler()

        wsgi_status = ' '.join([str(result['status_code']), result['status_msg']])
        headers = [(k, v) for k,v in result['headers'].items()]
        callback(str(wsgi_status), headers)

        return [to_bytes(result['body'])]
Exemplo 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
Exemplo 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
Exemplo n.º 7
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
Exemplo n.º 8
0
 def send(self, uuid, conn_id, msg):
     """Raw send to the given connection ID at the given uuid, mostly used
     internally.
     """
     header = "%s %d:%s," % (uuid, len(str(conn_id)), str(conn_id))
     self.out_sock.send(header + ' ' + to_bytes(msg))
Exemplo 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)
Exemplo 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
Exemplo 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)
Exemplo n.º 12
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
Exemplo n.º 13
0
 def send(self, uuid, conn_id, msg):
     """Raw send to the given connection ID at the given uuid, mostly used
     internally.
     """
     header = "%s %d:%s," % (uuid, len(str(conn_id)), str(conn_id))
     self.out_sock.send(header + ' ' + to_bytes(msg))
Exemplo n.º 14
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)