Пример #1
0
    def handle(self, http_context):
        start_time = time.time()

        self.vacuum()

        session = self.obtain_session(http_context.env)
        gate = None

        if not session and aj.dev_autologin:
            username = pwd.getpwuid(os.geteuid()).pw_name
            logging.warn('Opening an autologin session for user %s', username)
            session = self.open_session(http_context.env,
                                        initial_identity=username)
            session.set_cookie(http_context)

        if session:
            session.touch()
            gate = session.gate
        else:
            gate = self.restricted_gate

        if http_context.path.startswith('/socket'):
            http_context.fallthrough(SocketIORouteHandler.get(self.context))
            http_context.respond_ok()
            return 'Socket closed'

        request_object = {
            'type': 'http',
            'context': http_context.serialize(),
        }

        # await response

        try:
            timeout = 600
            with Timeout(timeout) as t:
                q = gate.q_http_replies.register()
                rq = gate.stream.send(request_object)
                while True:
                    resp = q.get(t)
                    if resp.id == rq.id:
                        break
        # pylint: disable=E0712
        except Timeout:
            http_context.respond('504 Gateway Timeout')
            return [b'Worker timeout']

        # ---

        if 'error' in resp.object:
            raise WorkerError(resp.object)

        for header in resp.object['headers']:
            http_context.add_header(*header)

        headers = dict(resp.object['headers'])
        if 'X-Session-Redirect' in headers:
            # new authenticated session
            username = headers['X-Session-Redirect']
            logging.info('Opening a session for user %s', username)
            session = self.open_session(
                http_context.env,
                initial_identity=username,
                auth_info=headers['X-Auth-Info'],
            )
            session.set_cookie(http_context)

        http_context.respond(resp.object['status'])
        content = resp.object['content']

        end_time = time.time()
        logging.debug('%.03fs %12s   %s %s %s', end_time - start_time,
                      str_fsize(len(content[0] if content else [])),
                      str(http_context.status).split()[0],
                      http_context.env['REQUEST_METHOD'], http_context.path)

        for index, item in enumerate(content):
            if isinstance(item, six.text_type):
                content[index] = item.encode('utf-8')

        # Touch the session in case system time has dramatically
        # changed during request
        if session:
            session.touch()

        return content
Пример #2
0
    def handle(self, http_context):
        start_time = time.time()

        self.vacuum()

        session = self.obtain_session(http_context.env)
        gate = None

        if not session and aj.dev_autologin:
            username = pwd.getpwuid(os.geteuid()).pw_name
            logging.warn('Opening an autologin session for user %s', username)
            session = self.open_session(
                http_context.env,
                initial_identity=username
            )
            session.set_cookie(http_context)

        if session:
            session.touch()
            gate = session.gate
        else:
            gate = self.restricted_gate

        if http_context.path.startswith('/socket'):
            http_context.fallthrough(SocketIORouteHandler.get(self.context))
            http_context.respond_ok()
            return 'Socket closed'

        request_object = {
            'type': 'http',
            'context': http_context.serialize(),
        }

        # await response

        try:
            timeout = 60
            with Timeout(timeout) as t:
                q = gate.q_http_replies.register()
                rq = gate.stream.send(request_object)
                while True:
                    resp = q.get(t)
                    if resp.id == rq.id:
                        break
        # pylint: disable=E0712
        except Timeout:
            http_context.respond('504 Gateway Timeout')
            return 'Worker timeout'

        # ---

        if 'error' in resp.object:
            raise WorkerError(resp.object)

        for header in resp.object['headers']:
            http_context.add_header(*header)
            if header[0] == 'X-Session-Redirect':
                # new authenticated session
                username = header[1]
                logging.info('Opening a session for user %s', username)
                session = self.open_session(
                    http_context.env,
                    initial_identity=username
                )
                session.set_cookie(http_context)

        http_context.respond(resp.object['status'])
        content = resp.object['content']

        end_time = time.time()
        logging.debug(
            '%.03fs %12s   %s %s %s',
            end_time - start_time,
            str_fsize(len(content[0] if content else [])),
            str(http_context.status).split()[0],
            http_context.env['REQUEST_METHOD'],
            http_context.path
        )

        for index, item in enumerate(content):
            if isinstance(item, six.text_type):
                content[index] = item.encode('utf-8')

        return content