Exemple #1
0
 def test_utf8(self):
     la = b'La Pe\xc3\xb1a'
     encoded = url_quote(la)
     decoded = text_(la, 'utf-8')
     path = '/'.join([encoded, encoded])
     result = self._callFUT(path)
     self.assertEqual(result, (decoded, decoded))
 def cookie_value(self):
     v = '%s%08x%s!' % (self.digest(), int(self.time),
                        url_quote(self.userid))
     if self.tokens:
         v += self.tokens + '!'
     v += self.user_data
     return v
Exemple #3
0
 def test_highchars_in_filename(self):
     url = url_quote('/static/héhé.html')
     res = self.testapp.get(url, status=200)
     _assertBody(
         res.body,
         os.path.join(here, text_('fixtures/static/héhé.html',
                                  'utf-8')))
Exemple #4
0
    def test_utf16(self):
        from pyramid.exceptions import URLDecodeError

        la = text_(b'La Pe\xc3\xb1a', 'utf-8').encode('utf-16')
        encoded = url_quote(la)
        path = '/'.join([encoded, encoded])
        self.assertRaises(URLDecodeError, self._callFUT, path)
 def test_utf8(self):
     la = b'La Pe\xc3\xb1a'
     encoded = url_quote(la)
     decoded = text_(la, 'utf-8')
     path = '/'.join([encoded, encoded])
     result = self._callFUT(path)
     self.assertEqual(result, (decoded, decoded))
Exemple #6
0
 def cookie_value(self):
     v = '%s%08x%s!' % (self.digest(), int(self.time), url_quote(
         self.userid))
     if self.tokens:
         v += self.tokens + '!'
     v += self.user_data
     return v
 def test_highchars_in_filename(self):
     url = url_quote('/static/héhé.html')
     res = self.testapp.get(url, status=200)
     _assertBody(
         res.body,
         os.path.join(here,
                      text_('fixtures/static/héhé.html', 'utf-8'))
         )
 def test_highchars_in_filename(self):
     path = os.path.join(here,
                         text_('fixtures/static/héhé.html', 'utf-8'))
     body = b'<html>hehe file</html>\n'
     with open(path, 'wb') as fp:
         fp.write(body)
     try:
         url = url_quote('/static/héhé.html')
         res = self.testapp.get(url, status=200)
         self.assertEqual(res.body, body)
     finally:
         os.unlink(path)
Exemple #9
0
 def generator(dict):
     newdict = {}
     for k, v in dict.items():
         if v.__class__ is text_type:
             v = native_(v, 'utf-8')
         if k == star and is_nonstr_iter(v):
             v = '/'.join([quote_path_segment(x) for x in v])
         elif k != star:
             if v.__class__ not in string_types:
                 v = str(v)
             v = url_quote(v, safe='')
         newdict[k] = v
     return gen % newdict
Exemple #10
0
 def generator(dict):
     newdict = {}
     for k, v in dict.items():
         if v.__class__ is text_type:
             v = native_(v, "utf-8")
         if k == star and is_nonstr_iter(v):
             v = "/".join([quote_path_segment(x) for x in v])
         elif k != star:
             if v.__class__ not in string_types:
                 v = str(v)
             v = url_quote(v, safe="")
         newdict[k] = v
     return gen % newdict
 def test_highchars_in_filename(self):
     path = os.path.join(
         here,
         text_('fixtures/static/héhé.html', 'utf-8'))
     body = b'<html>hehe file</html>\n'
     with open(path, 'wb') as fp:
         fp.write(body)
     try:
         url = url_quote('/static/héhé.html')
         res = self.testapp.get(url, status=200)
         self.assertEqual(res.body, body)
     finally:
         os.unlink(path)
Exemple #12
0
    def _partial_application_url(self, scheme=None, host=None, port=None):
        """
        Construct the URL defined by request.application_url, replacing any
        of the default scheme, host, or port portions with user-supplied
        variants.

        If ``scheme`` is passed as ``https``, and the ``port`` is *not*
        passed, the ``port`` value is assumed to ``443``.  Likewise, if
        ``scheme`` is passed as ``http`` and ``port`` is not passed, the
        ``port`` value is assumed to be ``80``.
        """
        e = self.environ
        if scheme is None:
            scheme = e['wsgi.url_scheme']
        else:
            if scheme == 'https':
                if port is None:
                    port = '443'
            if scheme == 'http':
                if port is None:
                    port = '80'
        url = scheme + '://'
        if port is not None:
            port = str(port)
        if host is None:
            host = e.get('HTTP_HOST')
        if host is None:
            host = e['SERVER_NAME']
        if port is None:
            if ':' in host:
                host, port = host.split(':', 1)
            else:
                port = e['SERVER_PORT']
        else:
            if ':' in host:
                host, _ = host.split(':', 1)
        if scheme == 'https':
            if port == '443':
                port = None
        elif scheme == 'http':
            if port == '80':
                port = None
        url += host
        if port:
            url += ':%s' % port

        url_encoding = getattr(self, 'url_encoding', 'utf-8') # webob 1.2b3+
        bscript_name = bytes_(self.script_name, url_encoding)
        return url + url_quote(bscript_name, PATH_SAFE)
Exemple #13
0
    def _partial_application_url(self, scheme=None, host=None, port=None):
        """
        Construct the URL defined by request.application_url, replacing any
        of the default scheme, host, or port portions with user-supplied
        variants.

        If ``scheme`` is passed as ``https``, and the ``port`` is *not*
        passed, the ``port`` value is assumed to ``443``.  Likewise, if
        ``scheme`` is passed as ``http`` and ``port`` is not passed, the
        ``port`` value is assumed to be ``80``.
        """
        e = self.environ
        if scheme is None:
            scheme = e['wsgi.url_scheme']
        else:
            if scheme == 'https':
                if port is None:
                    port = '443'
            if scheme == 'http':
                if port is None:
                    port = '80'
        url = scheme + '://'
        if port is not None:
            port = str(port)
        if host is None:
            host = e.get('HTTP_HOST')
        if host is None:
            host = e['SERVER_NAME']
        if port is None:
            if ':' in host:
                host, port = host.split(':', 1)
            else:
                port = e['SERVER_PORT']
        else:
            if ':' in host:
                host, _ = host.split(':', 1)
        if scheme == 'https':
            if port == '443':
                port = None
        elif scheme == 'http':
            if port == '80':
                port = None
        url += host
        if port:
            url += ':%s' % port

        url_encoding = getattr(self, 'url_encoding', 'utf-8') # webob 1.2b3+
        bscript_name = bytes_(self.script_name, url_encoding)
        return url + url_quote(bscript_name, PATH_SAFE)
Exemple #14
0
    def partial_application_url(self, scheme=None, host=None, port=None):
        """
        Construct the URL defined by request.application_url, replacing any
        of the default scheme, host, or port portions with user-supplied
        variants.

        If ``scheme`` is passed as ``https``, and the ``port`` is *not*
        passed, the ``port`` value is assumed to ``443``.  Likewise, if
        ``scheme`` is passed as ``http`` and ``port`` is not passed, the
        ``port`` value is assumed to be ``80``.
        """
        e = self.environ
        if scheme is None:
            scheme = e["wsgi.url_scheme"]
        else:
            if scheme == "https":
                if port is None:
                    port = "443"
            if scheme == "http":
                if port is None:
                    port = "80"
        url = scheme + "://"
        if port is not None:
            port = str(port)
        if host is None:
            host = e.get("HTTP_HOST")
        if host is None:
            host = e["SERVER_NAME"]
        if port is None:
            if ":" in host:
                host, port = host.split(":", 1)
            else:
                port = e["SERVER_PORT"]
        else:
            if ":" in host:
                host, _ = host.split(":", 1)
        if scheme == "https":
            if port == "443":
                port = None
        elif scheme == "http":
            if port == "80":
                port = None
        url += host
        if port:
            url += ":%s" % port

        url_encoding = getattr(self, "url_encoding", "utf-8")  # webob 1.2b3+
        bscript_name = bytes_(self.script_name, url_encoding)
        return url + url_quote(bscript_name, PATH_SAFE)
 def test_highchars_in_pathelement(self):
     path = os.path.join(
         here, text_('fixtures/static/héhé/index.html', 'utf-8'))
     pathdir = os.path.dirname(path)
     body = b'<html>hehe</html>\n'
     try:
         os.makedirs(pathdir)
         with open(path, 'wb') as fp:
             fp.write(body)
         url = url_quote('/static/héhé/index.html')
         res = self.testapp.get(url, status=200)
         self.assertEqual(res.body, body)
     finally:
         os.unlink(path)
         os.rmdir(pathdir)
 def test_highchars_in_pathelement(self):
     path = os.path.join(
         here,
         text_('fixtures/static/héhé/index.html', 'utf-8'))
     pathdir = os.path.dirname(path)
     body = b'<html>hehe</html>\n'
     try:
         os.makedirs(pathdir)
         with open(path, 'wb') as fp:
             fp.write(body)
         url = url_quote('/static/héhé/index.html')
         res = self.testapp.get(url, status=200)
         self.assertEqual(res.body, body)
     finally:
         os.unlink(path)
         os.rmdir(pathdir)
Exemple #17
0
    def static_url(self, path, **kw):
        if not isabs(path) and ':' not in path:
            package = caller_package()
            package_name = package.__name__
            path = '%s:%s' % (package_name, path)
        else:
            package_name = path.split(':', 1)[0]

        if package_name == self.package_name:
            registry = self.registry
        else:
            for application_name, config in APPLICATIONS.items():
                if config.package_name == package_name:
                    registry = config.registry
                    break
            else:
                registry = get_current_registry() # b/c

        info = registry.queryUtility(IStaticURLInfo)
        if info is None:
            raise ValueError('No static URL definition matching %s' % path)

        registrations = info._get_registrations(registry)
        api_route_url = getattr(self.applications, registry.application_name).route_url

        for (url, spec, route_name, cachebust) in registrations:
            if path.startswith(spec):
                subpath = path[len(spec):]
                if WIN: # pragma: no cover
                    subpath = subpath.replace('\\', '/') # windows
                if cachebust:
                    subpath, kw = cachebust(subpath, kw)
                if url is None:
                    kw['subpath'] = subpath
                    return api_route_url(route_name, **kw)
                else:
                    app_url, scheme, host, port, qs, anchor = parse_url_overrides(kw)
                    parsed = urlparse(url)
                    if not parsed.scheme:
                        url = urlunparse(parsed._replace(scheme=self.environ['wsgi.url_scheme']))
                    subpath = url_quote(subpath)
                    result = urljoin(url, subpath)
                    return result + qs + anchor

        raise ValueError('No static URL definition matching %s' % path)
Exemple #18
0
def get_file(request):
    # TODO: Add a route that enables the call to have the filename
    # appended to the end. This is so that gmail can read the services
    # Read more here:
    # http://stackoverflow.com/questions/20903967/gmails-new-image-caching-is-breaking-image-links-in-newsletter
    ctx = request.context
    document = ctx._instance
    f = File.get(document.id)
    if f.infected:
        raise HTTPNotAcceptable("Infected with a virus")
    escaped_double_quotes_filename = (f.title
        .replace(u'"', u'\\"')
        .encode('iso-8859-1', 'replace'))
    url_quoted_utf8_filename = url_quote(f.title.encode('utf-8'))
    handoff_to_nginx = asbool(config.get('handoff_to_nginx', False))
    if handoff_to_nginx:
        kwargs = dict(body='')
    else:
        if 'Range' in request.headers:
            raise HTTPRequestRangeNotSatisfiable()
        fs = open(f.path, 'rb')
        app_iter = None
        environ = request.environ
        if 'wsgi.file_wrapper' in environ:
            app_iter = environ['wsgi.file_wrapper'](fs, _BLOCK_SIZE)
        if app_iter is None:
            app_iter = FileIter(fs, _BLOCK_SIZE)
        kwargs=dict(app_iter=app_iter)

    r = Response(
        content_length=f.size,
        content_type=str(f.mime_type),
        last_modified=f.creation_date,
        expires=datetime.now()+timedelta(days=365),
        accept_ranges="bytes" if handoff_to_nginx else "none",
        content_disposition=
            'attachment; filename="%s"; filename*=utf-8\'\'%s' # RFC 6266
            % (escaped_double_quotes_filename, url_quoted_utf8_filename),
        **kwargs
    )
    if handoff_to_nginx:
        r.headers[b'X-Accel-Redirect'] = f.handoff_url
    return r
Exemple #19
0
def disposition(title):
    escaped_double_quotes_filename = title.replace(u'"', u'\\"').encode(
        'iso-8859-1', 'replace').decode('iso-8859-1')
    url_quoted_utf8_filename = url_quote(title.encode('utf-8'))
    return 'attachment; filename="%s"; filename*=utf-8\'\'%s' % (
        escaped_double_quotes_filename, url_quoted_utf8_filename)
Exemple #20
0
	def _(self):
		q = 'foo bar'
		res = self.testapp.post('/search?query=foo bar', status=302)
		ok(res.location) == 'http://localhost/find/'+url_quote(q)
 def test_utf16(self):
     from pyramid.exceptions import URLDecodeError
     la = text_(b'La Pe\xc3\xb1a', 'utf-8').encode('utf-16')
     encoded = url_quote(la)
     path = '/'.join([encoded, encoded])
     self.assertRaises(URLDecodeError, self._callFUT, path)