Ejemplo n.º 1
0
    def test_uri_encode_value(self):
        assert uri.encode_value('abcd') == 'abcd'
        assert uri.encode_value('abcd') == 'abcd'
        assert uri.encode_value('ab cd') == 'ab%20cd'
        assert uri.encode_value('\u00e7') == '%C3%A7'
        assert uri.encode_value('\u00e7\u20ac') == '%C3%A7%E2%82%AC'
        assert uri.encode_value('ab/cd') == 'ab%2Fcd'
        assert uri.encode_value('ab+cd=42,9') == 'ab%2Bcd%3D42%2C9'

        # NOTE(minesja): Addresses #1872
        assert uri.encode_value('%26') == '%2526'
        assert uri.decode(uri.encode_value('%26')) == '%26'
Ejemplo n.º 2
0
def format_content_disposition(value, disposition_type='attachment'):
    """Format a Content-Disposition header given a filename."""

    # NOTE(vytas): RFC 6266, Appendix D.
    #   Include a "filename" parameter when US-ASCII ([US-ASCII]) is
    #   sufficiently expressive.
    if isascii(value):
        return '%s; filename="%s"' % (disposition_type, value)

    # NOTE(vytas): RFC 6266, Appendix D.
    #   * Include a "filename*" parameter where the desired filename cannot be
    #     expressed faithfully using the "filename" form.  Note that legacy
    #     user agents will not process this, and will fall back to using the
    #     "filename" parameter's content.
    #   * When a "filename*" parameter is sent, to also generate a "filename"
    #     parameter as a fallback for user agents that do not support the
    #     "filename*" form, if possible.
    #   * When a "filename" parameter is included as a fallback (as per above),
    #     "filename" should occur first, due to parsing problems in some
    #     existing implementations.
    return "%s; filename=%s; filename*=UTF-8''%s" % (
        disposition_type,
        secure_filename(value),
        uri.encode_value(value),
    )
Ejemplo n.º 3
0
    def test_prop_uri_decode_models_stdlib_unquote_plus(self):
        stdlib_unquote = six.moves.urllib.parse.unquote_plus
        for case in self.uris:
            case = uri.encode_value(case)

            expect = stdlib_unquote(case)
            actual = uri.decode(case)
            self.assertEqual(expect, actual)
Ejemplo n.º 4
0
 def test_prop_uri_encode_value_models_stdlib_quote_safe_tilde(self):
     equiv_quote = functools.partial(
         six.moves.urllib.parse.quote, safe="~"
     )
     for case in self.uris:
         expect = equiv_quote(case)
         actual = uri.encode_value(case)
         self.assertEqual(expect, actual)
Ejemplo n.º 5
0
    def test_prop_uri_decode_models_stdlib_unquote_plus(self):
        stdlib_unquote = unquote_plus
        for case in self.uris:
            case = uri.encode_value(case)

            expect = stdlib_unquote(case)
            actual = uri.decode(case)
            assert expect == actual
Ejemplo n.º 6
0
 def test_prop_uri_encode_value_models_stdlib_quote_safe_tilde(self):
     equiv_quote = functools.partial(
         quote, safe='~'
     )
     for case in self.uris:
         expect = equiv_quote(case)
         actual = uri.encode_value(case)
         assert expect == actual
Ejemplo n.º 7
0
 def test_uri_encode_value(self):
     assert uri.encode_value('abcd') == 'abcd'
     assert uri.encode_value(u'abcd') == u'abcd'
     assert uri.encode_value(u'ab cd') == u'ab%20cd'
     assert uri.encode_value(u'\u00e7') == '%C3%A7'
     assert uri.encode_value(u'\u00e7\u20ac') == '%C3%A7%E2%82%AC'
     assert uri.encode_value('ab/cd') == 'ab%2Fcd'
     assert uri.encode_value('ab+cd=42,9') == 'ab%2Bcd%3D42%2C9'
Ejemplo n.º 8
0
 def test_uri_encode_value(self):
     self.assertEqual(uri.encode_value('abcd'), 'abcd')
     self.assertEqual(uri.encode_value(u'abcd'), u'abcd')
     self.assertEqual(uri.encode_value(u'ab cd'), u'ab%20cd')
     self.assertEqual(uri.encode_value(u'\u00e7'), '%C3%A7')
     self.assertEqual(uri.encode_value(u'\u00e7\u20ac'), '%C3%A7%E2%82%AC')
     self.assertEqual(uri.encode_value('ab/cd'), 'ab%2Fcd')
     self.assertEqual(uri.encode_value('ab+cd=42,9'), 'ab%2Bcd%3D42%2C9')
Ejemplo n.º 9
0
 def test_uri_encode_value(self):
     assert uri.encode_value('abcd') == 'abcd'
     assert uri.encode_value('abcd') == 'abcd'
     assert uri.encode_value('ab cd') == 'ab%20cd'
     assert uri.encode_value('\u00e7') == '%C3%A7'
     assert uri.encode_value('\u00e7\u20ac') == '%C3%A7%E2%82%AC'
     assert uri.encode_value('ab/cd') == 'ab%2Fcd'
     assert uri.encode_value('ab+cd=42,9') == 'ab%2Bcd%3D42%2C9'
Ejemplo n.º 10
0
 def test_uri_encode_value(self):
     self.assertEqual(uri.encode_value("abcd"), "abcd")
     self.assertEqual(uri.encode_value(u"abcd"), u"abcd")
     self.assertEqual(uri.encode_value(u"ab cd"), u"ab%20cd")
     self.assertEqual(uri.encode_value(u"\u00e7"), "%C3%A7")
     self.assertEqual(uri.encode_value(u"\u00e7\u20ac"), "%C3%A7%E2%82%AC")
     self.assertEqual(uri.encode_value("ab/cd"), "ab%2Fcd")
     self.assertEqual(uri.encode_value("ab+cd=42,9"), "ab%2Bcd%3D42%2C9")
Ejemplo n.º 11
0
 def test_uri_encode_value(self):
     self.assertEqual(uri.encode_value('abcd'), 'abcd')
     self.assertEqual(uri.encode_value(u'abcd'), u'abcd')
     self.assertEqual(uri.encode_value(u'ab cd'), u'ab%20cd')
     self.assertEqual(uri.encode_value(u'\u00e7'), '%C3%A7')
     self.assertEqual(uri.encode_value(u'\u00e7\u20ac'),
                      '%C3%A7%E2%82%AC')
     self.assertEqual(uri.encode_value('ab/cd'), 'ab%2Fcd')
     self.assertEqual(uri.encode_value('ab+cd=42,9'),
                      'ab%2Bcd%3D42%2C9')
Ejemplo n.º 12
0
def login_url(req):
    if req.path and req.path != '/login' and req.path != '/logout' and req.path != '/':
        return '/login/?next=%s' % uri.encode_value(req.path)
    else:
        return '/login/'
Ejemplo n.º 13
0
    def add_link(self,
                 target,
                 rel,
                 title=None,
                 title_star=None,
                 anchor=None,
                 hreflang=None,
                 type_hint=None):
        """
        Add a link header to the response.

        See also: https://tools.ietf.org/html/rfc5988

        Note:
            Calling this method repeatedly will cause each link to be
            appended to the Link header value, separated by commas.

        Note:
            So-called "link-extension" elements, as defined by RFC 5988,
            are not yet supported. See also Issue #288.

        Args:
            target (str): Target IRI for the resource identified by the
                link. Will be converted to a URI, if necessary, per
                RFC 3987, Section 3.1.
            rel (str): Relation type of the link, such as "next" or
                "bookmark". See also http://goo.gl/618GHr for a list
                of registered link relation types.

        Kwargs:
            title (str): Human-readable label for the destination of
                the link (default ``None``). If the title includes non-ASCII
                characters, you will need to use `title_star` instead, or
                provide both a US-ASCII version using `title` and a
                Unicode version using `title_star`.
            title_star (tuple of str): Localized title describing the
                destination of the link (default ``None``). The value must be a
                two-member tuple in the form of (*language-tag*, *text*),
                where *language-tag* is a standard language identifier as
                defined in RFC 5646, Section 2.1, and *text* is a Unicode
                string.

                Note:
                    *language-tag* may be an empty string, in which case the
                    client will assume the language from the general context
                    of the current request.

                Note:
                    *text* will always be encoded as UTF-8. If the string
                    contains non-ASCII characters, it should be passed as
                    a ``unicode`` type string (requires the 'u' prefix in
                    Python 2).

            anchor (str): Override the context IRI with a different URI
                (default None). By default, the context IRI for the link is
                simply the IRI of the requested resource. The value
                provided may be a relative URI.
            hreflang (str or iterable): Either a single *language-tag*, or
                a ``list`` or ``tuple`` of such tags to provide a hint to the
                client as to the language of the result of following the link.
                A list of tags may be given in order to indicate to the
                client that the target resource is available in multiple
                languages.
            type_hint(str): Provides a hint as to the media type of the
                result of dereferencing the link (default ``None``). As noted
                in RFC 5988, this is only a hint and does not override the
                Content-Type header returned when the link is followed.

        """

        # PERF(kgriffs): Heuristic to detect possiblity of an extension
        # relation type, in which case it will be a URL that may contain
        # reserved characters. Otherwise, don't waste time running the
        # string through uri.encode
        #
        # Example values for rel:
        #
        #     "next"
        #     "http://example.com/ext-type"
        #     "https://example.com/ext-type"
        #     "alternate http://example.com/ext-type"
        #     "http://example.com/ext-type alternate"
        #
        if '//' in rel:
            if ' ' in rel:
                rel = ('"' + ' '.join([uri.encode(r)
                                       for r in rel.split()]) + '"')
            else:
                rel = '"' + uri.encode(rel) + '"'

        value = '<' + uri.encode(target) + '>; rel=' + rel

        if title is not None:
            value += '; title="' + title + '"'

        if title_star is not None:
            value += ("; title*=UTF-8'" + title_star[0] + "'" +
                      uri.encode_value(title_star[1]))

        if type_hint is not None:
            value += '; type="' + type_hint + '"'

        if hreflang is not None:
            if isinstance(hreflang, six.string_types):
                value += '; hreflang=' + hreflang
            else:
                value += '; '
                value += '; '.join(['hreflang=' + lang for lang in hreflang])

        if anchor is not None:
            value += '; anchor="' + uri.encode(anchor) + '"'

        _headers = self._headers
        if 'link' in _headers:
            _headers['link'] += ', ' + value
        else:
            _headers['link'] = value
Ejemplo n.º 14
0
    def add_link(self, target, rel, title=None, title_star=None,
                 anchor=None, hreflang=None, type_hint=None):
        """
        Add a link header to the response.

        See also: https://tools.ietf.org/html/rfc5988

        Note:
            Calling this method repeatedly will cause each link to be
            appended to the Link header value, separated by commas.

        Note:
            So-called "link-extension" elements, as defined by RFC 5988,
            are not yet supported. See also Issue #288.

        Args:
            target (str): Target IRI for the resource identified by the
                link. Will be converted to a URI, if necessary, per
                RFC 3987, Section 3.1.
            rel (str): Relation type of the link, such as "next" or
                "bookmark". See also http://goo.gl/618GHr for a list
                of registered link relation types.

        Kwargs:
            title (str): Human-readable label for the destination of
                the link (default None). If the title includes non-ASCII
                characters, you will need to use `title_star` instead, or
                provide both a US-ASCII version using `title` and a
                Unicode version using `title_star`.
            title_star (tuple of str): Localized title describing the
                destination of the link (default None). The value must be a
                two-member tuple in the form of (*language-tag*, *text*),
                where *language-tag* is a standard language identifier as
                defined in RFC 5646, Section 2.1, and *text* is a Unicode
                string.

                Note:
                    *language-tag* may be an empty string, in which case the
                    client will assume the language from the general context
                    of the current request.

                Note:
                    *text* will always be encoded as UTF-8. If the string
                    contains non-ASCII characters, it should be passed as
                    a "unicode" type string (requires the 'u' prefix in
                    Python 2).

            anchor (str): Override the context IRI with a different URI
                (default None). By default, the context IRI for the link is
                simply the IRI of the requested resource. The value
                provided may be a relative URI.
            hreflang (str or iterable): Either a single *language-tag*, or
                a list or tuple of such tags to provide a hint to the client
                as to the language of the result of following the link. A
                list of tags may be given in order to indicate to the
                client that the target resource is available in multiple
                languages.
            type_hint(str): Provides a hint as to the media type of the
                result of dereferencing the link (default None). As noted
                in RFC 5988, this is only a hint and does not override the
                Content-Type header returned when the link is followed.

        """

        # PERF(kgriffs): Heuristic to detect possiblity of an extension
        # relation type, in which case it will be a URL that may contain
        # reserved characters. Otherwise, don't waste time running the
        # string through uri.encode
        #
        # Example values for rel:
        #
        #     "next"
        #     "http://example.com/ext-type"
        #     "https://example.com/ext-type"
        #     "alternate http://example.com/ext-type"
        #     "http://example.com/ext-type alternate"
        #
        if '//' in rel:
            if ' ' in rel:
                rel = ('"' +
                       ' '.join([uri.encode(r) for r in rel.split()]) +
                       '"')
            else:
                rel = '"' + uri.encode(rel) + '"'

        value = '<' + uri.encode(target) + '>; rel=' + rel

        if title is not None:
            value += '; title="' + title + '"'

        if title_star is not None:
            value += ("; title*=UTF-8'" + title_star[0] + "'" +
                      uri.encode_value(title_star[1]))

        if type_hint is not None:
            value += '; type="' + type_hint + '"'

        if hreflang is not None:
            if isinstance(hreflang, six.string_types):
                value += '; hreflang=' + hreflang
            else:
                value += '; '
                value += '; '.join(['hreflang=' + lang for lang in hreflang])

        if anchor is not None:
            value += '; anchor="' + uri.encode(anchor) + '"'

        _headers = self._headers
        if 'link' in _headers:
            _headers['link'] += ', ' + value
        else:
            _headers['link'] = value