Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1384

Show
Ignore:
Timestamp:
09/27/06 18:21:39
Author:
fumanchu
Message:

Better use of tools.encode.encoding.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/encoding.py

    r1382 r1384  
    5252 
    5353def encode(encoding=None, errors='strict'): 
     54    # Guard against running twice 
     55    if getattr(cherrypy.request, "_encoding_attempted", False): 
     56        return 
     57    cherrypy.request._encoding_attempted = True 
     58     
    5459    ct = cherrypy.response.headers.elements("Content-Type") 
    5560    if ct: 
     
    9196    response = cherrypy.response 
    9297     
    93     attempted_charsets = [] 
    94      
    9598    if cherrypy.response.stream: 
    9699        encoder = encode_stream 
     
    98101        response.collapse_body() 
    99102        encoder = encode_string 
    100      
    101     failmsg = "The response could not be encoded with %s" 
    102      
    103     if encoding is not None: 
    104         # If specified, force this encoding to be used, or fail. 
    105         if encoder(encoding, errors): 
    106             return encoding 
    107         else: 
    108             raise cherrypy.HTTPError(500, failmsg % encoding) 
    109103     
    110104    # Parse the Accept-Charset request header, and try to provide one 
    111105    # of the requested charsets (in order of user preference). 
    112106    encs = cherrypy.request.headers.elements('Accept-Charset') 
    113     if not encs: 
    114         # Any character-set is acceptable. 
    115         charsets = [] 
    116         if encoder(default_encoding, errors): 
    117             return default_encoding 
     107    charsets = [enc.value.lower() for enc in encs] 
     108    attempted_charsets = [] 
     109     
     110    if encoding is not None: 
     111        # If specified, force this encoding to be used, or fail. 
     112        encoding = encoding.lower() 
     113        if (not charsets) or "*" in charsets or encoding in charsets: 
     114            if encoder(encoding, errors): 
     115                return encoding 
     116    else: 
     117        if not encs: 
     118            # Any character-set is acceptable. 
     119            if encoder(default_encoding, errors): 
     120                return default_encoding 
     121            else: 
     122                raise cherrypy.HTTPError(500, failmsg % default_encoding) 
    118123        else: 
    119             raise cherrypy.HTTPError(500, failmsg % default_encoding) 
    120     else: 
    121         charsets = [enc.value.lower() for enc in encs] 
    122         if "*" not in charsets: 
    123             # If no "*" is present in an Accept-Charset field, then all 
    124             # character sets not explicitly mentioned get a quality 
    125             # value of 0, except for ISO-8859-1, which gets a quality 
    126             # value of 1 if not explicitly mentioned. 
    127             iso = 'iso-8859-1' 
    128             if iso not in charsets: 
    129                 attempted_charsets.append(iso) 
    130                 if encoder(iso, errors): 
    131                     return iso 
    132          
    133         for element in encs: 
    134             if element.qvalue > 0: 
    135                 if element.value == "*": 
    136                     # Matches any charset. Try our default. 
    137                     if default_encoding not in attempted_charsets: 
    138                         attempted_charsets.append(default_encoding) 
    139                         if encoder(default_encoding, errors): 
    140                             return default_encoding 
    141                 else: 
    142                     encoding = element.value 
    143                     if encoding not in attempted_charsets: 
    144                         attempted_charsets.append(encoding) 
    145                         if encoder(encoding, errors): 
    146                             return encoding 
     124            if "*" not in charsets: 
     125                # If no "*" is present in an Accept-Charset field, then all 
     126                # character sets not explicitly mentioned get a quality 
     127                # value of 0, except for ISO-8859-1, which gets a quality 
     128                # value of 1 if not explicitly mentioned. 
     129                iso = 'iso-8859-1' 
     130                if iso not in charsets: 
     131                    attempted_charsets.append(iso) 
     132                    if encoder(iso, errors): 
     133                        return iso 
     134             
     135            for element in encs: 
     136                if element.qvalue > 0: 
     137                    if element.value == "*": 
     138                        # Matches any charset. Try our default. 
     139                        if default_encoding not in attempted_charsets: 
     140                            attempted_charsets.append(default_encoding) 
     141                            if encoder(default_encoding, errors): 
     142                                return default_encoding 
     143                    else: 
     144                        encoding = element.value 
     145                        if encoding not in attempted_charsets: 
     146                            attempted_charsets.append(encoding) 
     147                            if encoder(encoding, errors): 
     148                                return encoding 
    147149     
    148150    # No suitable encoding found. 
  • trunk/cherrypy/test/test_encoding.py

    r1377 r1384  
    2121            return sing 
    2222        mao_zedong.exposed = True 
     23         
     24        def utf8(self): 
     25            return sing8 
     26        utf8.exposed = True 
     27        utf8._cp_config = {'tools.encode.encoding': 'utf-8'} 
    2328     
    2429    class GZIP: 
     
    97102                          "us-ascii, ISO-8859-1, x-mac-ce. We tried these " 
    98103                          "charsets: x-mac-ce, us-ascii, ISO-8859-1.") 
     104         
     105        # Test the 'encoding' arg to encode. 
     106        self.getPage('/utf8') 
     107        self.assertBody(sing8) 
     108        self.getPage('/utf8', [('Accept-Charset', 'us-ascii, ISO-8859-1')]) 
     109        self.assertStatus("406 Not Acceptable") 
    99110     
    100111    def testGzip(self): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets