Python tornado.escape.url_escape() Examples

The following are 30 code examples of tornado.escape.url_escape(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.escape , or try the search function .
Example #1
Source File: handlers.py    From nativeauthenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _render(self, login_error=None, username=None):
        self._register_template_path()
        return self.render_template(
            'native-login.html',
            next=url_escape(self.get_argument('next', default='')),
            username=username,
            login_error=login_error,
            custom_html=self.authenticator.custom_html,
            login_url=self.settings['login_url'],
            enable_signup=self.authenticator.enable_signup,
            two_factor_auth=self.authenticator.allow_2fa,
            authenticator_login_url=url_concat(
                self.authenticator.login_url(self.hub.base_url),
                {'next': self.get_argument('next', '')},
            ),
        ) 
Example #2
Source File: request_handler.py    From zoe with Apache License 2.0 6 votes vote down vote up
def init_app(cls, application, jinja_options=None):
        """Init the application."""
        app_settings = application.settings

        _loader = FileSystemLoader(
            app_settings.get('template_path', 'templates')
        )

        _jinja_config = {
            'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'],
            'auto_reload': app_settings.get('autoreload', False),
            'loader': _loader,
            'cache_size': 50 if app_settings.get('compiled_template_cache', True) else 0,
            'autoescape': app_settings.get('autoescape', 'xhtml_escape') == "xhtml_escape"
        }

        _jinja_config.update(**(jinja_options or {}))
        environment = Environment(**_jinja_config)

        application.jinja_environment = environment
        app_settings['jinja_environment'] = environment
        environment.filters.update(tojson=tojson_filter, xhtml_escape=xhtml_escape, url_escape=url_escape, squeeze=squeeze, linkify=linkify)

        return environment 
Example #3
Source File: gitlab.py    From oauthenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _check_group_whitelist(self, user_id, access_token):
        http_client = AsyncHTTPClient()
        headers = _api_headers(access_token)
        # Check if user is a member of any group in the whitelist
        for group in map(url_escape, self.gitlab_group_whitelist):
            url = "%s/groups/%s/members/%s%d" % (
                self.gitlab_api,
                quote(group, safe=''),
                self.member_api_variant,
                user_id,
            )
            req = HTTPRequest(url, method="GET", headers=headers)
            resp = await http_client.fetch(req, raise_error=False)
            if resp.code == 200:
                return True  # user _is_ in group
        return False 
Example #4
Source File: conftest.py    From elyra with Apache License 2.0 6 votes vote down vote up
def fetch(request, *parts, **kwargs):
    # Handle URL strings

    # Since base_url is already escaped, unescape it.
    path = url_escape(url_path_join(*parts), plus=False)

    # Make request.
    method = 'GET'
    if 'method' in kwargs and kwargs['method']:
        method = kwargs['method']

    body = None
    if 'body' in kwargs and kwargs['body']:
        body = kwargs['body']

    return request(method, path, data=body)
# END - Remove once transition to jupyter_server occurs 
Example #5
Source File: sourcefile.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def make_oembed_url(url):
        url_parsed = None
        try:
            url_parsed = urlparse(url)
        except:
            return None

        if url_parsed.hostname.lower() not in ['youtube.com', 'www.youtube.com', 'vimeo.com', 'www.vimeo.com', 'youtu.be', 'flic.kr', 'flickr.com', 'www.flickr.com']:
            return None

        oembed_url = None
        if url_parsed.hostname.lower() in ['youtube.com', 'www.youtube.com', 'youtu.be']:
            to_url = 'https://%s%s?%s' % (url_parsed.hostname, url_parsed.path, url_parsed.query)
            oembed_url = 'https://www.youtube.com/oembed?url=%s&maxwidth=550&format=json' % (url_escape(to_url))
        elif url_parsed.hostname.lower() in ['vimeo.com', 'www.vimeo.com']:
            to_url = 'https://%s%s' % (url_parsed.hostname, url_parsed.path)
            oembed_url = 'https://vimeo.com/api/oembed.json?url=%s&maxwidth=550' % (url_escape(to_url))
        elif url_parsed.hostname.lower() in ['flic.kr', 'flickr.com', 'www.flickr.com']:
            to_url = 'https://%s%s' % (url_parsed.hostname, url_parsed.path)
            oembed_url = 'https://www.flickr.com/services/oembed/?url=%s&maxwidth=550&format=json' % (url_escape(to_url))
        return oembed_url 
Example #6
Source File: conversations_tests.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def test_another_user_commenting_will_update_the_files_activity_at(self):
        request = HTTPRequest(self.get_url('/p/%s/comment' % self.shf.share_key), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "body=%s&_xsrf=%s" % (url_escape("a comment"), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()

        time.sleep(1)

        
        sf = Sharedfile.get('id=%s', self.shf.id)
        activity_one = sf.activity_at

        request = HTTPRequest(self.get_url('/p/%s/comment' % self.shf.share_key), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "body=%s&_xsrf=%s" % (url_escape("a second comment"), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()

        
        sf = Sharedfile.get('id=%s', self.shf.id)
        activity_two = sf.activity_at

        self.assertTrue(activity_two > activity_one) 
Example #7
Source File: web.py    From temboard with PostgreSQL License 5 votes vote down vote up
def serialize_querystring(query):
    return "&".join([
        "%s=%s" % (url_escape(name), url_escape(value))
        for name, value in sorted(query.items())
    ]) 
Example #8
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_quick_edit_title(self):
        self.upload_file(self.test_file1_path, self.test_file1_sha1, self.test_file1_content_type, 1, self.sid, self.xsrf)
        request = HTTPRequest(self.get_url('/p/1/quick-edit-title'), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "_xsrf=%s&title=%s" % (self.xsrf, url_escape("Monkey Business")))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        j = json_decode(response.body)
        self.assertEqual(j['title'], 'Monkey Business') 
Example #9
Source File: routing.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def reverse(self, *args: Any) -> Optional[str]:
        if self._path is None:
            raise ValueError("Cannot reverse url regex " + self.regex.pattern)
        assert len(args) == self._group_count, (
            "required number of arguments " "not found"
        )
        if not len(args):
            return self._path
        converted_args = []
        for a in args:
            if not isinstance(a, (unicode_type, bytes)):
                a = str(a)
            converted_args.append(url_escape(utf8(a), plus=False))
        return self._path % tuple(converted_args) 
Example #10
Source File: template.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def generate(self, **kwargs: Any) -> bytes:
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_tt_utf8": escape.utf8,  # for internal use
            "_tt_string_types": (unicode_type, bytes),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace(".", "_"),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec_in(self.compiled, namespace)
        execute = typing.cast(Callable[[], bytes], namespace["_tt_execute"])
        # Clear the traceback module's cache of source data now that
        # we've generated a new template (mainly for this module's
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        return execute() 
Example #11
Source File: escape_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_url_escape_quote_plus(self):
        unescaped = '+ #%'
        plus_escaped = '%2B+%23%25'
        escaped = '%2B%20%23%25'
        self.assertEqual(url_escape(unescaped), plus_escaped)
        self.assertEqual(url_escape(unescaped, plus=False), escaped)
        self.assertEqual(url_unescape(plus_escaped), unescaped)
        self.assertEqual(url_unescape(escaped, plus=False), unescaped)
        self.assertEqual(url_unescape(plus_escaped, encoding=None),
                         utf8(unescaped))
        self.assertEqual(url_unescape(escaped, encoding=None, plus=False),
                         utf8(unescaped)) 
Example #12
Source File: escape_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_url_escape_unicode(self):
        tests = [
            # byte strings are passed through as-is
            (u('\u00e9').encode('utf8'), '%C3%A9'),
            (u('\u00e9').encode('latin1'), '%E9'),

            # unicode strings become utf8
            (u('\u00e9'), '%C3%A9'),
        ]
        for unescaped, escaped in tests:
            self.assertEqual(url_escape(unescaped), escaped) 
Example #13
Source File: gen_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_task_handler(self):
        response = self.fetch('/task?url=%s' % url_escape(self.get_url('/sequence')))
        self.assertEqual(response.body, b"got response: 123") 
Example #14
Source File: template.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def generate(self, **kwargs):
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_tt_utf8": escape.utf8,  # for internal use
            "_tt_string_types": (unicode_type, bytes),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace('.', '_'),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec_in(self.compiled, namespace)
        execute = namespace["_tt_execute"]
        # Clear the traceback module's cache of source data now that
        # we've generated a new template (mainly for this module's
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        return execute() 
Example #15
Source File: web.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def reverse(self, *args):
        assert self._path is not None, \
            "Cannot reverse url regex " + self.regex.pattern
        assert len(args) == self._group_count, "required number of arguments "\
            "not found"
        if not len(args):
            return self._path
        converted_args = []
        for a in args:
            if not isinstance(a, (unicode_type, bytes)):
                a = str(a)
            converted_args.append(escape.url_escape(utf8(a), plus=False))
        return self._path % tuple(converted_args) 
Example #16
Source File: web.py    From temboard with PostgreSQL License 5 votes vote down vote up
def generic_proxy(self, url, methods=None):
        # Pass-through implementation for /proxy/address/port/…
        url = r'(%s)' % url

        @self.instance_proxy(url, methods)
        def generic_instance_proxy(request, path):
            if request.blueprint and request.blueprint.plugin_name:
                request.instance.check_active_plugin(
                    request.blueprint.plugin_name)
            body = request.instance.http(
                path=url_escape(path, plus=False),
                method=request.method,
                body=request.json,
            )
            return jsonify(body) 
Example #17
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_picker_stores_description(self):
        request = HTTPRequest(self.get_url('/tools/p'), 'POST', {"Cookie":"_xsrf=%s;sid=%s" % (self.xsrf,self.sid)}, "_xsrf=%s&url=%s&title=boatmoatgoat&description=%s" % (self.xsrf, url_escape(self.url), url_escape(self.description)))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        self.assertTrue(response.body.find("ERROR") == -1)
        sf = Sharedfile.get("id=1")
        self.assertEqual(sf.description, self.description) 
Example #18
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_picker_strips_google_img_url(self):
        """
        https://www.google.com/imgres?imgurl=http://cragganmorefarm.com/user/gimage/Baby-Ground-hogs_480_320.jpg&imgrefurl=http://cragganmorefarm.com/&usg=__kpRJbm_WBlbEnqDvfi3A2JuJ9Wg=&h=320&w=480&sz=33&hl=en&start=24&sig2=SyR_NSDovcsOYu5tJYtlig&zoom=1&tbnid=TT5jIOrb76kqbM:&tbnh=130&tbnw=173&ei=f5lJTdjbHoL6lweT2cU3&prev=/images%3Fq%3Dbaby%2Bgroundhogs%26um%3D1%26hl%3Den%26client%3Dfirefox-a%26sa%3DX%26rls%3Dorg.mozilla:en-US:official%26biw%3D1152%26bih%3D709%26tbs%3Disch:10%2C540&um=1&itbs=1&iact=rc&dur=326&oei=YZlJTYuYJsH78AaQh6msDg&esq=2&page=2&ndsp=24&ved=1t:429,r:8,s:24&tx=103&ty=85&biw=1152&bih=709
        """
        request = HTTPRequest(self.get_url('/tools/p?url=%s&source_url=%s' % (self.url, url_escape("https://www.google.com/imgres?imgurl=http://cragganmorefarm.com/user/gimage/Baby-Ground-hogs_480_320.jpg&imgrefurl=http://cragganmorefarm.com/&usg=__kpRJbm_WBlbEnqDvfi3A2JuJ9Wg=&h=320&w=480&sz=33&hl=en&start=24&sig2=SyR_NSDovcsOYu5tJYtlig&zoom=1&tbnid=TT5jIOrb76kqbM:&tbnh=130&tbnw=173&ei=f5lJTdjbHoL6lweT2cU3&prev=/images%3Fq%3Dbaby%2Bgroundhogs%26um%3D1%26hl%3Den%26client%3Dfirefox-a%26sa%3DX%26rls%3Dorg.mozilla:en-US:official%26biw%3D1152%26bih%3D709%26tbs%3Disch:10%2C540&um=1&itbs=1&iact=rc&dur=326&oei=YZlJTYuYJsH78AaQh6msDg&esq=2&page=2&ndsp=24&ved=1t:429,r:8,s:24&tx=103&ty=85&biw=1152&bih=709"))), 'GET', {"Cookie":"sid=%s" % (self.sid)})
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        self.assertTrue(response.body.find("source: http://cragganmorefarm.com/</textarea>") > 0) 
Example #19
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_save_video_correctly_processes_various_youtube_urls(self):
        urls = ['https://www.youtube.com/watch?v=EmcMG4uxiHk&recommended=0', 'https://youtu.be/EmcMG4uxiHk', 'https://www.youtube.com/watch?v=EmcMG4uxiHk&feature=rec-LGOUT-real_rev-rn-1r-11-HM']
        for url in urls:
            request = HTTPRequest(self.get_url('/tools/save-video?url=%s' % (url_escape(url))), 'GET', {"Cookie":"sid=%s" % (self.sid)})
            self.http_client.fetch(request, self.stop)
            response = self.wait()
            self.assertTrue(response.body.find('value="https://www.youtube.com/watch?v=EmcMG4uxiHk">') > -1) 
Example #20
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_quick_edit_source_url(self):
        self.upload_file(self.test_file1_path, self.test_file1_sha1, self.test_file1_content_type, 1, self.sid, self.xsrf)
        request = HTTPRequest(self.get_url('/p/1/quick-edit-source-url'), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "_xsrf=%s&source_url=%s" % (self.xsrf, url_escape('http://www.example.com/')))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        j = json_decode(response.body)
        self.assertEqual(j['source_url'], 'http://www.example.com/') 
Example #21
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_quick_edit_description(self):
        self.upload_file(self.test_file1_path, self.test_file1_sha1, self.test_file1_content_type, 1, self.sid, self.xsrf)
        request = HTTPRequest(self.get_url('/p/1/quick-edit-description'), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "_xsrf=%s&description=%s" % (self.xsrf, url_escape('Bilbo\nbaggins')))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        j = json_decode(response.body)
        self.assertEqual(j['description_raw'], 'Bilbo\nbaggins') 
Example #22
Source File: FileTests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_adding_video_makes_it_show_up_in_friends_shake(self):
        user2 = User(name='user2', email='user2@mltshp.com', email_confirmed=1, is_paid=1)
        user2.set_password('asdfasdf')
        user2.save()
        user2.subscribe(self.user.shake())

        url = 'https://vimeo.com/20379529'
        request = HTTPRequest(self.get_url('/tools/save-video'), 'POST', {"Cookie":"sid=%s;_xsrf=%s" % (self.sid, self.xsrf)}, "url=%s&_xsrf=%s" % (url_escape(url), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        sfs = Sharedfile.from_subscriptions(user2.id)
        self.assertTrue(len(sfs) > 0)
        self.assertEqual(sfs[0].name , url) 
Example #23
Source File: api_tests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_authorize_code_request_redirects_to_sign_in(self):

        authorization_url = '/api/authorize?response_type=code&client_id=%s' % (self.app.key())

        response = api_request(self, self.get_url(authorization_url), unsigned=True)
        self.assertEqual(response.effective_url, self.get_url('/sign-in?next=%s' % url_escape(authorization_url)))
        self.assertEqual(response.code, 200) 
Example #24
Source File: conversations_tests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_creating_a_new_comment_does_not_create_a_duplicate_conversation(self):
        request = HTTPRequest(self.get_url('/p/%s/comment' % self.shf.share_key), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "body=%s&_xsrf=%s" % (url_escape("a comment"), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()

        request = HTTPRequest(self.get_url('/p/%s/comment' % self.shf.share_key), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "body=%s&_xsrf=%s" % (url_escape("a second comment"), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        
        conversations = Conversation.all()
        self.assertEqual(len(conversations), 2) 
Example #25
Source File: conversations_tests.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def test_creating_a_new_comment_creates_a_conversation(self):
        request = HTTPRequest(self.get_url('/p/%s/comment' % self.shf.share_key), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "body=%s&_xsrf=%s" % (url_escape("a comment"), self.xsrf))
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        
        conversations = Conversation.all()
        self.assertEqual(len(conversations), 2) 
Example #26
Source File: tools.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def post(self):
        """
        TODO: better determination of correct file name, if it is indeed a file, plus type.
        """
        self.url = self.get_argument('url', None)
        self.content_type = None

        if not self.url:
            raise tornado.web.HTTPError(404)

        #TODO : check if it is a valid URL
        #       copy from above

        http = tornado.httpclient.AsyncHTTPClient()

        #this sends a header value for cookie to d/l protected FP files
        fp_cookie = None
        b = re.compile(r"^http(s?)://(.*?)(.?)filepile\.org")
        m = b.match(self.url)
        if m:
            for char in [' ', '[', ']']:
                self.url = self.url.replace(char, url_escape(char))

            fp_cookie = {'Cookie':'_filepile_session=4c2eff30dd27e679d38fbc030b204488'}
        request = HTTPRequest(self.url, headers=fp_cookie, header_callback=self.on_header)
        http.fetch(request, self.on_response) 
Example #27
Source File: viewer.py    From nyroglancer with Apache License 2.0 5 votes vote down vote up
def register_volume(self, volume):

        # globally register volume
        global volumes
        volumes[volume.token] = volume

        # globally register kernel client for this volume in the Jupyter server
        cf = url_escape(find_connection_file())
        http_client= HTTPClient()
        try:
            response = http_client.fetch(self.get_server_url() + '/register_token/' + volume.token.decode('utf8') + '/' + cf)
        except Exception as e:
            raise RuntimeError("could not register token: " + str(e))
        http_client.close() 
Example #28
Source File: test_handlers.py    From dokomoforms with GNU General Public License v3.0 5 votes vote down vote up
def test_get_enumerator_only_survey_by_title_not_logged_in(self):
        survey_id = 'c0816b52-204f-41d4-aaf0-ac6ae2970925'
        with self.session.begin():
            survey = self.session.query(models.Survey).get(survey_id)
            survey.url_slug = 'url_slug'
        url = '/enumerate/url_slug'
        response = self.fetch(
            url, method='GET', follow_redirects=False, _logged_in_user=None
        )
        self.assertEqual(response.code, 302)
        self.assertEqual(
            response.headers['Location'], '/?next=' + url_escape(url)
        ) 
Example #29
Source File: test_handlers.py    From dokomoforms with GNU General Public License v3.0 5 votes vote down vote up
def test_get_enumerator_only_survey_not_logged_in(self):
        survey_id = 'c0816b52-204f-41d4-aaf0-ac6ae2970925'
        url = '/enumerate/' + survey_id
        response = self.fetch(
            url, method='GET', follow_redirects=False, _logged_in_user=None
        )
        self.assertEqual(response.code, 302)
        self.assertEqual(
            response.headers['Location'], '/?next=' + url_escape(url)
        ) 
Example #30
Source File: escape_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def test_url_escape(self):
        tests = [
            # byte strings are passed through as-is
            (u'\u00e9'.encode('utf8'), '%C3%A9'),
            (u'\u00e9'.encode('latin1'), '%E9'),

            # unicode strings become utf8
            (u'\u00e9', '%C3%A9'),
            ]
        for unescaped, escaped in tests:
            self.assertEqual(url_escape(unescaped), escaped)