Decode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value).
(params)
| 258 | re.ASCII) |
| 259 | |
| 260 | def decode_params(params): |
| 261 | """Decode parameters list according to RFC 2231. |
| 262 | |
| 263 | params is a sequence of 2-tuples containing (param name, string value). |
| 264 | """ |
| 265 | new_params = [params[0]] |
| 266 | # Map parameter's name to a list of continuations. The values are a |
| 267 | # 3-tuple of the continuation number, the string value, and a flag |
| 268 | # specifying whether a particular segment is %-encoded. |
| 269 | rfc2231_params = {} |
| 270 | for name, value in params[1:]: |
| 271 | encoded = name.endswith('*') |
| 272 | value = unquote(value) |
| 273 | mo = rfc2231_continuation.match(name) |
| 274 | if mo: |
| 275 | name, num = mo.group('name', 'num') |
| 276 | if num is not None: |
| 277 | num = int(num) |
| 278 | rfc2231_params.setdefault(name, []).append((num, value, encoded)) |
| 279 | else: |
| 280 | new_params.append((name, '"%s"' % quote(value))) |
| 281 | if rfc2231_params: |
| 282 | for name, continuations in rfc2231_params.items(): |
| 283 | value = [] |
| 284 | extended = False |
| 285 | # Sort by number |
| 286 | continuations.sort() |
| 287 | # And now append all values in numerical order, converting |
| 288 | # %-encodings for the encoded segments. If any of the |
| 289 | # continuation names ends in a *, then the entire string, after |
| 290 | # decoding segments and concatenating, must have the charset and |
| 291 | # language specifiers at the beginning of the string. |
| 292 | for num, s, encoded in continuations: |
| 293 | if encoded: |
| 294 | # Decode as "latin-1", so the characters in s directly |
| 295 | # represent the percent-encoded octet values. |
| 296 | # collapse_rfc2231_value treats this as an octet sequence. |
| 297 | s = urllib.parse.unquote(s, encoding="latin-1") |
| 298 | extended = True |
| 299 | value.append(s) |
| 300 | value = quote(EMPTYSTRING.join(value)) |
| 301 | if extended: |
| 302 | charset, language, value = decode_rfc2231(value) |
| 303 | new_params.append((name, (charset, language, '"%s"' % value))) |
| 304 | else: |
| 305 | new_params.append((name, '"%s"' % value)) |
| 306 | return new_params |
| 307 | |
| 308 | def collapse_rfc2231_value(value, errors='replace', |
| 309 | fallback_charset='us-ascii'): |
nothing calls this directly
no test coverage detected