Beispiel #1
0
 def on_message(self, message):
     prefix, message = message.split(",", 1)
     id = prefix.split("/", 1)[0]
     message = jsonapi.loads(message)
     logger.debug("SockJSHandler.on_message: %s", message)
     msg_type = message["header"]["msg_type"]
     app = self.session.handler.application
     if id == "complete":
         if msg_type in ("complete_request", "object_info_request"):
             app.completer.registerRequest(self, message)
         return
     try:
         kernel = app.kernel_dealer.kernel(id)
     except KeyError:
         # Ignore messages to nonexistent or killed kernels.
         logger.warning("%s sent to nonexistent kernel %s", msg_type, id)
         return
     if id not in self.channels:
         self.channels[id] = SockJSChannelsHandler(self.send)
         self.channels[id].connect(kernel)
     if msg_type == "execute_request":
         stats_logger.info(
             StatsMessage(kernel_id=id,
                          remote_ip=kernel.remote_ip,
                          referer=kernel.referer,
                          code=message["content"]["code"],
                          execute_type="request"))
     self.channels[id].send(message)
Beispiel #2
0
 def _log_stats(self, kernel, msg):
     msg=json.loads(msg)
     if msg["header"]["msg_type"] == "execute_request":
         statslogger.info(StatsMessage(kernel_id = kernel,
                                       remote_ip = self.kernel_info['remote_ip'],
                                       referer = self.kernel_info['referer'],
                                       code = msg["content"]["code"],
                                       execute_type='request'))
Beispiel #3
0
 def on_message(self, message):
     prefix, json_message = message.split(",", 1)
     kernel, channel = prefix.split("/", 1)
     if channel == "stdin":
         # TODO: Support the stdin channel
         # See http://ipython.org/ipython-doc/dev/development/messaging.html
         return
     application = self.session.handler.application
     message = jsonapi.loads(json_message)
     if kernel == "complete":
         if message["header"]["msg_type"] in ("complete_request",
                                              "object_info_request"):
             application.completer.registerRequest(self, message)
         return
     try:
         if kernel not in self.channels:
             # handler may be None in certain circumstances (it seems to only be set
             # in GET requests, not POST requests, so even using it here may
             # only work with JSONP because of a race condition)
             kernel_info = application.km.kernel_info(kernel)
             self.kernel_info = {
                 'remote_ip': kernel_info['remote_ip'],
                 'referer': kernel_info['referer'],
                 'timeout': kernel_info['timeout']
             }
         if message["header"]["msg_type"] == "execute_request":
             stats_logger.info(
                 StatsMessage(kernel_id=kernel,
                              remote_ip=self.kernel_info['remote_ip'],
                              referer=self.kernel_info['referer'],
                              code=message["content"]["code"],
                              execute_type='request'))
         if kernel not in self.channels:
             self.channels[kernel] = \
                 {"iopub": IOPubSockJSHandler(kernel, self.send, application),
                  "shell": ShellSockJSHandler(kernel, self.send, application)}
             self.channels[kernel]["iopub"].open(kernel)
             self.channels[kernel]["shell"].open(kernel)
         self.channels[kernel][channel].on_message(json_message)
     except KeyError:
         # Ignore messages to nonexistent or killed kernels.
         logger.info("%s message sent to nonexistent kernel: %s" %
                     (message["header"]["msg_type"], kernel))
Beispiel #4
0
 def on_message(self, message):
     prefix, json_message = message.split(",", 1)
     kernel_id = prefix.split("/", 1)[0]
     message = jsonapi.loads(json_message)
     logger.debug("KernelConnection.on_message: %s", message)
     application = self.session.handler.application
     if kernel_id == "complete":
         if message["header"]["msg_type"] in ("complete_request",
                                              "object_info_request"):
             application.completer.registerRequest(self, message)
         return
     try:
         if kernel_id not in self.channels:
             # handler may be None in certain circumstances (it seems to only be set
             # in GET requests, not POST requests, so even using it here may
             # only work with JSONP because of a race condition)
             kernel_info = application.km.kernel_info(kernel_id)
             self.kernel_info = {
                 'remote_ip': kernel_info['remote_ip'],
                 'referer': kernel_info['referer'],
                 'timeout': kernel_info['timeout']
             }
         if message["header"]["msg_type"] == "execute_request":
             stats_logger.info(
                 StatsMessage(kernel_id=kernel_id,
                              remote_ip=self.kernel_info['remote_ip'],
                              referer=self.kernel_info['referer'],
                              code=message["content"]["code"],
                              execute_type='request'))
         if kernel_id not in self.channels:
             self.channels[kernel_id] = SockJSChannelsHandler(self.send)
             self.channels[kernel_id].open(application, kernel_id)
         self.channels[kernel_id].on_message(json_message)
     except KeyError:
         # Ignore messages to nonexistent or killed kernels.
         import traceback
         logger.info("%s message sent to nonexistent kernel: %s\n%s" %
                     (message["header"]["msg_type"], kernel_id,
                      traceback.format_exc()))
Beispiel #5
0
    def post(self):
        if 'Origin' in self.request.headers:
            self.set_header(
                'Access-Control-Allow-Origin', self.request.headers['Origin'])
            self.set_header('Access-Control-Allow-Credentials', 'true')
        if (config.get('requires_tos')
                and self.get_argument('accepted_tos', 'false') != 'true'):
            self.set_status(403)
            self.finish(
                'When evaluating code, you must acknowledge your acceptance '
                'of the terms of service at /static/tos.html by passing the '
                'parameter accepted_tos=true\n')
            return
        code = ''.join(self.get_arguments('code', strip=False))
        if len(code) > 65000:
            self.set_status(413)
            self.finish('Max code size is 65000 characters')
            return
        remote_ip = self.request.remote_ip
        referer = self.request.headers.get('Referer', '')
        self.kernel_id = yield tornado.gen.Task(
            self.application.km.new_session_async,
            referer=referer,
            remote_ip=remote_ip,
            timeout=0)
        sm = StatsMessage(
            kernel_id=self.kernel_id,
            remote_ip=remote_ip,
            referer=referer,
            code=code,
            execute_type='service')
        if remote_ip == '127.0.0.1' and self.kernel_id:
            stats_logger.debug(sm)
        else:
            stats_logger.info(sm)
        if not self.kernel_id:
            logger.error('could not obtain a valid kernel_id')
            self.set_status(503)
            self.finish()
            return
        self.zmq_handler = ZMQServiceHandler()
        self.zmq_handler.open(self.application, self.kernel_id)
        loop = tornado.ioloop.IOLoop.instance()
        
        def kernel_callback(msg):
            if msg['msg_type'] == 'execute_reply':
                loop.remove_timeout(self.timeout_handle)
                logger.debug('service request finished for %s', self.kernel_id)
                streams = self.zmq_handler.streams
                streams['success'] = msg['content']['status'] == 'ok'
                streams['execute_reply'] = msg['content']
                loop.add_callback(self.finish_request)
                
        self.zmq_handler.msg_from_kernel_callbacks.append(kernel_callback)
        
        def timeout_callback():
            logger.debug('service request timed out for %s', self.kernel_id)
            loop.add_callback(self.finish_request)

        self.timeout_handle = loop.call_later(30, timeout_callback)
        exec_message = {
            'channel': 'shell',
            'parent_header': {},
            'header': {
                'msg_id': str(uuid.uuid4()),
                'username': '',
                'session': self.kernel_id,
                'msg_type': 'execute_request',
               },
            'content': {
                'code': code,
                'silent': False,
                'user_expressions':
                    jsonapi.loads(self.get_argument('user_expressions', '{}')),
                'allow_stdin': False,
                },
            'metadata': {},
            }
        self.zmq_handler.on_message(jsonapi.dumps(exec_message))
Beispiel #6
0
    async def post(self):
        if 'Origin' in self.request.headers:
            self.set_header('Access-Control-Allow-Origin',
                            self.request.headers['Origin'])
            self.set_header('Access-Control-Allow-Credentials', 'true')
        if (config.get('requires_tos')
                and self.get_argument('accepted_tos', 'false') != 'true'):
            self.set_status(403)
            self.finish(
                'When evaluating code, you must acknowledge your acceptance '
                'of the terms of service at /static/tos.html by passing the '
                'parameter accepted_tos=true\n')
            return
        code = ''.join(self.get_arguments('code', strip=False))
        if len(code) > 65000:
            self.set_status(413)
            self.finish('Max code size is 65000 characters')
            return
        remote_ip = self.request.remote_ip
        referer = self.request.headers.get('Referer', '')
        self.kernel = await self.application.kernel_dealer.get_kernel(
            rlimits=config.get("provider_settings")["preforked_rlimits"],
            lifespan=config.get("max_lifespan"),
            timeout=0)
        sm = StatsMessage(kernel_id=self.kernel.id,
                          remote_ip=remote_ip,
                          referer=referer,
                          code=code,
                          execute_type='service')
        if remote_ip == '127.0.0.1':
            stats_logger.debug(sm)
        else:
            stats_logger.info(sm)
        self.zmq_handler = ZMQServiceHandler()
        streams = self.zmq_handler.streams
        self.zmq_handler.connect(self.kernel)
        loop = tornado.ioloop.IOLoop.current()

        def kernel_callback(msg):
            if msg['msg_type'] == 'execute_reply':
                loop.remove_timeout(self.timeout_handle)
                streams['success'] = msg['content']['status'] == 'ok'
                streams['execute_reply'] = msg['content']
            if self.kernel.status == "idle" and 'success' in streams:
                logger.debug('service request finished for %s', self.kernel.id)
                loop.add_callback(self.finish_request)

        self.zmq_handler.msg_from_kernel_callbacks.append(kernel_callback)

        def timeout_callback():
            logger.debug('service request timed out for %s', self.kernel.id)
            self.kernel.stop()
            self.zmq_handler.streams['success'] = False
            loop.add_callback(self.finish_request)

        self.timeout_handle = loop.call_later(30, timeout_callback)
        exec_message = {
            'channel': 'shell',
            'parent_header': {},
            'header': {
                'msg_id': str(uuid.uuid4()),
                'username': '',
                'session': self.kernel.id,
                'msg_type': 'execute_request',
            },
            'content': {
                'code':
                code,
                'silent':
                False,
                'user_expressions':
                jsonapi.loads(self.get_argument('user_expressions', '{}')),
                'allow_stdin':
                False,
            },
            'metadata': {},
        }
        self.zmq_handler.send(exec_message)
        self._auto_finish = False
Beispiel #7
0
    def post(self):
        if config.get_config("requires_tos") and self.get_cookie("accepted_tos") != "true" and \
            self.get_argument("accepted_tos", "false") != "true":
            self.write("""When evaluating code, you must acknowledge your acceptance
of the terms of service at /static/tos.html by passing the parameter or cookie
accepted_tos=true\n""")
            self.set_status(403)
            self.finish()
            return
        default_timeout = 30 # seconds
        code = "".join(self.get_arguments('code', strip=False))
        if len(code)>65000:
            self.set_status(413)
            self.write("Max code size is 65000 characters")
            self.finish()
            return
        if code:
            km = self.application.km
            remote_ip = self.request.remote_ip
            referer = self.request.headers.get('Referer','')
            self.kernel_id = yield gen.Task(km.new_session_async,
                                            referer = referer,
                                            remote_ip = remote_ip,
                                            timeout=0)
            if not (remote_ip=="::1" and referer==""
                    and cron.match(code) is not None):
                sm = StatsMessage(kernel_id=self.kernel_id,
                                  remote_ip=remote_ip,
                                  referer=referer,
                                  code=code,
                                  execute_type='service')
                if remote_ip == "127.0.0.1" and self.kernel_id:
                    stats_logger.debug(sm)
                else:
                    stats_logger.info(sm)

            self.shell_handler = ShellServiceHandler(self.application)
            self.iopub_handler = IOPubServiceHandler(self.application)
            self.iopub_handler.open(self.kernel_id)
            self.shell_handler.open(self.kernel_id)

            loop = ioloop.IOLoop.instance()

            self.success = False
            def done(msg):
                if msg["msg_type"] == "execute_reply":
                    self.success = msg["content"]["status"] == "ok"
                    self.user_variables = msg["content"].get("user_variables", [])
                    self.execute_reply = msg['content']
                    loop.remove_timeout(self.timeout_request)
                    loop.add_callback(self.finish_request)
            self.shell_handler.msg_from_kernel_callbacks.append(done)
            self.timeout_request = loop.add_timeout(time.time()+default_timeout, self.timeout_request)
            exec_message = {"parent_header": {},
                            "header": {"msg_id": str(uuid.uuid4()),
                                       "username": "",
                                       "session": self.kernel_id,
                                       "msg_type": "execute_request",
                                       },
                            "content": {"code": code,
                                        "silent": False,
                                        "user_variables": self.get_arguments('user_variables'),
                                        "user_expressions": {},
                                        "allow_stdin": False,
                                        },
                            "metadata": {}
                            }
            self.shell_handler.on_message(jsonapi.dumps(exec_message))