def _rfc2231_and_continuation_params(cls, params): # TODO: complexity count = set() continuations = dict() for key, value, quoted in params: if key in count: raise InvalidHeader(_(u'Parameter given twice: %r'), key.decode('ISO8859-1')) count.add(key) if '*' in key: if key.endswith('*') and not quoted: charset, language, value_ = decode_rfc2231( value.encode('ISO8859-1')) if not charset: yield key, value continue encoding = sanitize_encoding(charset) if encoding is None: raise InvalidHeader(_(u'Unknown encoding: %r'), charset) try: key, value = key[:-1], Percent.unquote(value_).decode( encoding) except UnicodeDecodeError as exc: raise InvalidHeader(_(u'%s') % (exc, )) key_, asterisk, num = key.rpartition('*') if asterisk: try: if num != '0' and num.startswith('0'): raise ValueError num = int(num) except ValueError: yield key, value continue continuations.setdefault(key_, {})[num] = value continue yield key, value for key, lines in iteritems(continuations): value = b'' for i in xrange(len(lines)): try: value += lines.pop(i) except KeyError: break if not key: raise InvalidHeader(_(u'...')) if value: yield key, value for k, v in iteritems(lines): yield '%s*%d' % (key, k), v
def _rfc2231_and_continuation_params(cls, params): # TODO: complexity count = set() continuations = dict() for key, value, quoted in params: if key in count: raise InvalidHeader(_(u'Parameter given twice: %r'), key.decode('ISO8859-1')) count.add(key) if '*' in key: if key.endswith('*') and not quoted: charset, language, value_ = decode_rfc2231(value.encode('ISO8859-1')) if not charset: yield key, value continue encoding = sanitize_encoding(charset) if encoding is None: raise InvalidHeader(_(u'Unknown encoding: %r'), charset) try: key, value = key[:-1], Percent.unquote(value_).decode(encoding) except UnicodeDecodeError as exc: raise InvalidHeader(_(u'%s') % (exc,)) key_, asterisk, num = key.rpartition('*') if asterisk: try: if num != '0' and num.startswith('0'): raise ValueError num = int(num) except ValueError: yield key, value continue continuations.setdefault(key_, {})[num] = value continue yield key, value for key, lines in iteritems(continuations): value = b'' for i in xrange(len(lines)): try: value += lines.pop(i) except KeyError: break if not key: raise InvalidHeader(_(u'...')) if value: yield key, value for k, v in iteritems(lines): yield '%s*%d' % (key, k), v
def unquote(self, data): return Percent.unquote(bytes(data)).decode(self.encoding)
def unquote(cls, data, charset=None): return Percent.unquote(data).decode(charset or 'ISO8859-1')