Beispiel #1
0
    def start(self, lua_source, sandboxed, lua_package_path, lua_sandbox_allowed_modules):
        self.exceptions = StoredExceptions()
        self.log(lua_source)
        self.sandboxed = sandboxed
        self.lua = SplashLuaRuntime(
            sandboxed=sandboxed,
            lua_package_path=lua_package_path,
            lua_sandbox_allowed_modules=lua_sandbox_allowed_modules,
        )
        self.splash = Splash(self.lua, self.exceptions, self.tab, self.render_options, log=self.log)
        self.extras = Extras(self.lua, self.exceptions)
        self.extras.inject_to_globals()

        self.runner = MainCoroutineRunner(lua=self.lua, splash=self.splash, log=self.log, sandboxed=sandboxed)

        try:
            main_coro = self.get_main_coro(lua_source)
        except lupa.LuaSyntaxError as e:
            # XXX: is this code active?
            # It looks like we're always getting LuaError
            # because of sandbox and coroutine handling code.
            raise ScriptError({"type": ScriptError.SYNTAX_ERROR, "message": to_unicode(e.args[0])})
        except lupa.LuaError as e:
            # Error happened before starting coroutine
            info = parse_error_message(e.args[0])
            info.update({"type": ScriptError.LUA_INIT_ERROR, "message": to_unicode(e.args[0])})
            raise ScriptError(info)
        # except ValueError as e:
        #     # XXX: when does it happen?
        #     raise ScriptError({
        #         "type": ScriptError.UNKNOWN_ERROR,
        #         "message": repr(e),
        #     })

        self.runner.start(main_coro=main_coro, return_result=self.return_result, return_error=self.return_error)
Beispiel #2
0
    def delete(self, name=None, url=None):
        """
        Remove all cookies with a passed name for the passed url.
        Return a number of cookies deleted.
        """
        all_cookies = self.allCookies()
        if url is None:
            new_cookies = [c for c in all_cookies if
                           to_unicode(bytes(c.name())) != name]
        else:
            remove_cookies = self.cookiesForUrl(to_qurl(url))
            if name is not None:
                remove_cookies = [c for c in remove_cookies if
                                  to_unicode(bytes(c.name())) == name]
            to_remove = {self._cookie_fp(c) for c in remove_cookies}
            new_cookies = [
                c for c in all_cookies if self._cookie_fp(c) not in to_remove
            ]

        self.setAllCookies(new_cookies)
        return len(all_cookies) - len(new_cookies)
 def test_b64_encode(self):
     for txt in ["hello", u"привет", ""]:
         resp = self.request_lua("""
         b64 = require('base64')
         function main(splash)
             return {res=b64.encode(splash.args.txt)}
         end
         """, {'txt': txt})
         self.assertStatusCode(resp, 200)
         txt = to_bytes(txt)
         self.assertEqual(resp.json(), {
             'res': to_unicode(base64.b64encode(txt))
         })
 def test_b64_encode(self):
     for txt in ["hello", u"привет", ""]:
         resp = self.request_lua(
             """
         b64 = require('base64')
         function main(splash)
             return {res=b64.encode(splash.args.txt)}
         end
         """, {'txt': txt})
         self.assertStatusCode(resp, 200)
         txt = to_bytes(txt)
         self.assertEqual(resp.json(),
                          {'res': to_unicode(base64.b64encode(txt))})
Beispiel #5
0
    def _attr_getter(self, obj, attr_name):
        try:
            attr_name = to_unicode(attr_name)
        except TypeError:
            raise AttributeError("Non-string lookups are not allowed (requested: %r)" % attr_name)

        if obj not in self._allowed_object_attrs:
            raise AttributeError("Access to object %r is not allowed" % obj)

        if attr_name not in self._allowed_object_attrs[obj]:
            raise AttributeError("Access to private attribute %r is not allowed" % attr_name)

        value = getattr(obj, attr_name)
        return value
Beispiel #6
0
    def _attr_getter(self, obj, attr_name):
        try:
            attr_name = to_unicode(attr_name)
        except TypeError:
            raise AttributeError("Non-string lookups are not allowed (requested: %r)" % attr_name)

        if obj not in self._allowed_object_attrs:
            raise AttributeError("Access to object %r is not allowed" % obj)

        if attr_name not in self._allowed_object_attrs[obj]:
            raise AttributeError("Access to private attribute %r is not allowed" % attr_name)

        value = getattr(obj, attr_name)
        return value
Beispiel #7
0
    def delete(self, name=None, url=None):
        """
        Remove all cookies with a passed name for the passed url.
        Return a number of cookies deleted.
        """
        all_cookies = self.allCookies()
        if url is None:
            new_cookies = [
                c for c in all_cookies if to_unicode(bytes(c.name())) != name
            ]
        else:
            remove_cookies = self.cookiesForUrl(to_qurl(url))
            if name is not None:
                remove_cookies = [
                    c for c in remove_cookies
                    if to_unicode(bytes(c.name())) == name
                ]
            to_remove = {self._cookie_fp(c) for c in remove_cookies}
            new_cookies = [
                c for c in all_cookies if self._cookie_fp(c) not in to_remove
            ]

        self.setAllCookies(new_cookies)
        return len(all_cookies) - len(new_cookies)
Beispiel #8
0
def parse_error_message(error_text):
    r"""
    Split Lua error message into 'source', 'line_number' and 'error'.
    If error message can't be parsed, an empty dict is returned.

    This function is not reliable because error text is ambiguous.

    Parse runtime error messages::

        >>> info = parse_error_message('[string "function main(splash)\r..."]:2: /app/splash.lua:81: ValueError(\'could not convert string to float: sdf\'')
        >>> print(info['line_number'])
        2
        >>> print(repr(info['source']))
        '[string "function main(splash)\r..."]'
        >>> print(info['error'])
        /app/splash.lua:81: ValueError('could not convert string to float: sdf'

        >>> parse_error_message('dfsadf')
        {}

    Parse syntax errors::

        >>> info = parse_error_message("error loading code: [string \"<python>\"]:1: syntax error near 'ction'")
        >>> info['line_number']
        1
        >>> print(info['error'])
        syntax error near 'ction'

    """
    error_text = to_unicode(error_text)
    m = _LUA_ERROR_RE.match(error_text)
    if not m:
        m = _SYNTAX_ERROR_RE.match(error_text)

    if not m:
        return {}

    return {
        'source': m.group(1),
        'line_number': int(m.group(2)),
        'error': m.group(3)
    }
Beispiel #9
0
 def test_converting_a_latin_1_encoded_string_to_unicode(self):
     self.assertEqual(to_unicode(b'lel\xf1e', 'latin-1'), u'lel\xf1e')
Beispiel #10
0
def to_unicode_all(it):
    return [to_unicode(el) for el in it]
Beispiel #11
0
def to_unicode_all(it):
    return [to_unicode(el) for el in it]
Beispiel #12
0
 def test_converting_an_utf8_encoded_string_to_unicode(self):
     self.assertEqual(to_unicode(b'lel\xc3\xb1e'), u'lel\xf1e')
Beispiel #13
0
 def test_errors_argument(self):
     self.assertEqual(to_unicode(b'a\xedb', 'utf-8', errors='replace'),
                      u'a\ufffdb')
Beispiel #14
0
 def test_converting_a_unicode_to_unicode_should_return_the_same_object(
         self):
     self.assertEqual(to_unicode(u'\xf1e\xf1e\xf1e'), u'\xf1e\xf1e\xf1e')
Beispiel #15
0
 def test_converting_a_latin_1_encoded_string_to_unicode(self):
     self.assertEqual(to_unicode(b'lel\xf1e', 'latin-1'), u'lel\xf1e')
Beispiel #16
0
 def test_converting_an_utf8_encoded_string_to_unicode(self):
     self.assertEqual(to_unicode(b'lel\xc3\xb1e'), u'lel\xf1e')
Beispiel #17
0
 def test_errors_argument(self):
     self.assertEqual(
         to_unicode(b'a\xedb', 'utf-8', errors='replace'),
         u'a\ufffdb'
     )
Beispiel #18
0
 def test_converting_a_unicode_to_unicode_should_return_the_same_object(self):
     self.assertEqual(to_unicode(u'\xf1e\xf1e\xf1e'), u'\xf1e\xf1e\xf1e')