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 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'])]
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 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 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))
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)