Ticket #11522: ticket11522_puny_code.patch

File ticket11522_puny_code.patch, 2.5 KB (added by yoan@…, 15 years ago)

Patch to support IRI in redirections.

  • django/http/__init__.py

     
    317317            yield value
    318318
    319319    def __setitem__(self, header, value):
    320         header, value = self._convert_to_ascii(header, value)
     320        # Conversion to ASCII is done later on using iri_to_uri for a proper
     321        # puny-code friendly encoding.
     322        #
     323        # See: HttpRequest.build_absolute_uri
     324        #      fix_location_header
     325        if header != 'Location' and isinstance(value, unicode):
     326            header, value = self._convert_to_ascii(header, value)
    321327        self._headers[header.lower()] = (header, value)
    322328
    323329    def __delitem__(self, header):
  • django/utils/encoding.py

     
    11import types
    22import urllib
     3import urlparse
    34import locale
    45import datetime
    56import codecs
     
    136137    # section 3.1 of RFC 3987.
    137138    if iri is None:
    138139        return iri
    139     return urllib.quote(smart_str(iri), safe='/#%[]=:;$&()+,!?*')
    140140
     141    if isinstance(iri, unicode):
     142        parts = urlparse.urlsplit(iri)
    141143
     144        if parts.hostname:
     145            hostname = parts.hostname.encode('idna')
     146        else:
     147            hostname = None
     148       
     149        # Fix auth part
     150        if parts.username or parts.password:
     151            username = urllib.quote(parts.username.encode("utf-8"))
     152            if parts.password:
     153                password = urllib.quote(parts.password.encode("utf-8"))
     154                username = username + ":" + password
     155            hostname = username + "@" + hostname
     156
     157        # add the port
     158        if parts.port:
     159            hostname += ":" + str(parts.port)
     160       
     161        path = urllib.quote(parts.path.encode("utf-8"), safe="/:~+")
     162        query = urllib.quote(parts.query.encode("utf-8"), safe="=%&[]:;$()+,!?*/")
     163
     164        return urlparse.urlunsplit([parts.scheme,
     165                                      hostname,
     166                                      path,
     167                                      query,
     168                                      parts.fragment])
     169    else:
     170        return urllib.quote(smart_str(iri), safe='/#%[]=:;$&()+,!?*')
     171
     172
    142173# The encoding of the default system locale but falls back to the
    143174# given fallback encoding if the encoding is unsupported by python or could
    144175# not be determined.  See tickets #10335 and #5846
Back to Top