| 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) |
|---|
| 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 |
|---|