Esempio n. 1
0
 def test_quote_url(self):
     self.assertEqual('this%20is%20some%20path',
                      quote_url('this is some path'))
     self.assertEqual('this%20is%20some%20path',
                      quote_url(b'this is some path'))
     self.assertEqual(
         'R%C3%A9pertoire%20%28%40vec%29%20%7Bc%C3%A0ra%C3%A7t%23%C3%A8r%C3%AB%7D%20%24%C3%A9p%C3%AAcial',
         quote_url(
             b'R\xc3\xa9pertoire (@vec) {c\xc3\xa0ra\xc3\xa7t#\xc3\xa8r\xc3\xab} $\xc3\xa9p\xc3\xaacial'
         ))
Esempio n. 2
0
def url_for(endpoint, *args, **kwargs):
    """
    Generate a url for the given endpoint, path (*args) with parameters (**kwargs)
    """
    url = "/" + endpoint.strip("/")
    for chunk in args:
        if not chunk:
            continue
        if hasattr(chunk, 'owner') and hasattr(chunk, 'path'):
            # This is a RepoObject
            url += "/"
            url += chunk.owner
            url += "/"
            url += rdw_helpers.quote_url(chunk.path.strip(b"/"))
        elif hasattr(chunk, 'path'):
            # This is a DirEntry
            if chunk.path:
                url += "/"
                url += rdw_helpers.quote_url(chunk.path.strip(b"/"))
        elif chunk and isinstance(chunk, bytes):
            url += "/"
            url += rdw_helpers.quote_url(chunk.strip(b"/"))
        elif chunk and isinstance(chunk, str):
            url += "/"
            url += chunk.rstrip("/")
        else:
            raise ValueError(
                'invalid positional arguments, url_for accept str, bytes or RepoPath: %r'
                % chunk)
    first_args = True
    # Sort the arguments to have predictable results.
    for key, value in sorted(kwargs.items()):
        url += "?" if first_args else "&"
        first_args = False
        if hasattr(value, 'epoch'):
            value = value.epoch()
        url += "%s=%s" % (key, value)
    return url
Esempio n. 3
0
    def index(self, redirect=b'/', username='', error_msg='', **kwargs):
        # Re-encode the redirect for display in HTML
        redirect = quote_url(redirect, safe=";/?:@&=+$,%")

        params = {
            'redirect': redirect,
            'login': username,
            'warning': error_msg
        }

        # Add welcome message to params. Try to load translated message.
        params["welcome_msg"] = self._welcome_msg
        if hasattr(cherrypy.response, 'i18n'):
            lang = cherrypy.response.i18n.locale.language
            params["welcome_msg"] = Option(
                "WelcomeMsg[%s]" % (lang),
                default=params["welcome_msg"]).get()

        return self._compile_template("login.html", **params).encode("utf-8")
Esempio n. 4
0
def _content_disposition(filename):
    """
    Try to generate the best content-disposition value to support most browser.
    """
    assert isinstance(filename, str)
    # Provide hint filename. Try to follow recommendation at
    # http://greenbytes.de/tech/tc2231/
    # I choose to only provide filename if the filename is a simple ascii
    # file without special character. Otherwise, we provide filename*

    # 1. Used quoted filename for ascii filename.
    try:
        filename.encode('ascii')
        # Some char are not decoded properly by user agent.
        if not any(c in filename for c in [';', '%', '\\']):
            return 'attachment; filename="%s"' % filename
    except:
        pass
    # 3. Define filename* as encoded UTF8 (replace invalid char)
    filename_utf8 = filename.encode('utf-8', 'replace')
    return 'attachment; filename*=UTF-8\'\'%s' % quote_url(filename_utf8,
                                                           safe='?')