def decode_params(params): """Decode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (content type, string value). """ new_params = [] # maps parameter's name to a list of continuations rfc2231_params = {} # params is a sequence of 2-tuples containing (content_type, string value) name, value = params[0] new_params.append((name, value)) # Cycle through each of the rest of the parameters. for name, value in params[1:]: value = unquote(value) mo = rfc2231_continuation.match(name) if mo: name, num = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_param1 = rfc2231_params.setdefault(name, []) rfc2231_param1.append((num, value)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for name, continuations in rfc2231_params.items(): value = [] # Sort by number continuations.sort() # And now append all values in num order for num, continuation in continuations: value.append(continuation) charset, language, value = decode_rfc2231(EMPTYSTRING.join(value)) new_params.append( (name, (charset, language, '"%s"' % quote(value)))) return new_params
def decode_params(params): new_params = [] rfc2231_params = {} (name, value) = params[0] new_params.append((name, value)) for (name, value) in params[1:]: value = unquote(value) mo = rfc2231_continuation.match(name) if mo: (name, num) = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_param1 = rfc2231_params.setdefault(name, []) rfc2231_param1.append((num, value)) continue new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for (name, continuations) in rfc2231_params.items(): value = [] continuations.sort() for (num, continuation) in continuations: value.append(continuation) (charset, language, value) = decode_rfc2231(EMPTYSTRING.join(value)) new_params.append( (name, (charset, language, '"%s"' % quote(value)))) return new_params
def decode_params(params): new_params = [] rfc2231_params = {} name, value = params[0] new_params.append((name, value)) for name, value in params[1:]: value = unquote(value) mo = rfc2231_continuation.match(name) if mo: name, num = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_param1 = rfc2231_params.setdefault(name, []) rfc2231_param1.append((num, value)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for name, continuations in rfc2231_params.items(): value = [] continuations.sort() for num, continuation in continuations: value.append(continuation) charset, language, value = decode_rfc2231(EMPTYSTRING.join(value)) new_params.append((name, (charset, language, '"%s"' % quote(value)))) return new_params
def decode_params(params): """Decode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). """ # Copy params so we don't mess with the original params = params[:] new_params = [] # Map parameter's name to a list of continuations. The values are a # 3-tuple of the continuation number, the string value, and a flag # specifying whether a particular segment is %-encoded. rfc2231_params = {} name, value = params.pop(0) new_params.append((name, value)) while params: name, value = params.pop(0) if name.endswith('*'): encoded = True else: encoded = False value = unquote(value) mo = rfc2231_continuation.match(name) if mo: name, num = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_params.setdefault(name, []).append((num, value, encoded)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for name, continuations in rfc2231_params.items(): value = [] extended = False # Sort by number continuations.sort() # And now append all values in numerical order, converting # %-encodings for the encoded segments. If any of the # continuation names ends in a *, then the entire string, after # decoding segments and concatenating, must have the charset and # language specifiers at the beginning of the string. for num, s, encoded in continuations: if encoded: # Decode as "latin-1", so the characters in s directly # represent the percent-encoded octet values. # collapse_rfc2231_value treats this as an octet sequence. s = urllib.parse.unquote(s, encoding="latin-1") extended = True value.append(s) value = quote(EMPTYSTRING.join(value)) if extended: charset, language, value = decode_rfc2231(value) new_params.append((name, (charset, language, '"%s"' % value))) else: new_params.append((name, '"%s"' % value)) return new_params
def decode_params(params): """Decode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). """ params = params[:] new_params = [] rfc2231_params = {} name, value = params.pop(0) new_params.append((name, value)) while 1: if params: name, value = params.pop(0) if name.endswith('*'): encoded = True else: encoded = False value = unquote(value) mo = rfc2231_continuation.match(name) if mo: name, num = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_params.setdefault(name, []).append( (num, value, encoded)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for name, continuations in rfc2231_params.items(): value = [] extended = False continuations.sort() for num, s, encoded in continuations: if encoded: s = urllib.parse.unquote(s, encoding='latin-1') extended = True value.append(s) value = quote(EMPTYSTRING.join(value)) if extended: charset, language, value = decode_rfc2231(value) new_params.append((name, (charset, language, '"%s"' % value))) else: new_params.append((name, '"%s"' % value)) return new_params
def decode_params(params): """Decode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). """ params = params[:] new_params = [] rfc2231_params = {} name, value = params.pop(0) new_params.append((name, value)) while params: name, value = params.pop(0) if name.endswith('*'): encoded = True else: encoded = False value = unquote(value) mo = rfc2231_continuation.match(name) if mo: name, num = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_params.setdefault(name, []).append((num, value, encoded)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for name, continuations in rfc2231_params.items(): value = [] extended = False continuations.sort() for num, s, encoded in continuations: if encoded: s = urllib.unquote(s) extended = True value.append(s) value = quote(EMPTYSTRING.join(value)) if extended: charset, language, value = decode_rfc2231(value) new_params.append((name, (charset, language, '"%s"' % value))) else: new_params.append((name, '"%s"' % value)) return new_params
def decode_params(params): params = params[:] new_params = [] rfc2231_params = {} name, value = params.pop(0) new_params.append((name, value)) while params: name, value = params.pop(0) if name.endswith('*'): encoded = True else: encoded = False value = unquote(value) mo = rfc2231_continuation.match(name) if mo: name, num = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_params.setdefault(name, []).append((num, value, encoded)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for name, continuations in rfc2231_params.items(): value = [] extended = False continuations.sort() for num, s, encoded in continuations: if encoded: s = urllib.unquote(s) extended = True value.append(s) value = quote(EMPTYSTRING.join(value)) if extended: charset, language, value = decode_rfc2231(value) new_params.append((name, (charset, language, '"%s"' % value))) else: new_params.append((name, '"%s"' % value)) return new_params
def decode_params(params): params = params[:] new_params = [] rfc2231_params = {} (name, value) = params.pop(0) new_params.append((name, value)) while params: (name, value) = params.pop(0) if name.endswith('*'): encoded = True else: encoded = False value = unquote(value) mo = rfc2231_continuation.match(name) if mo: (name, num) = mo.group('name', 'num') if num is not None: num = int(num) rfc2231_params.setdefault(name, []).append((num, value, encoded)) else: new_params.append((name, '"%s"' % quote(value))) if rfc2231_params: for (name, continuations) in rfc2231_params.items(): value = [] extended = False continuations.sort() for (num, s, encoded) in continuations: if encoded: s = urllib.parse.unquote(s, encoding='latin-1') extended = True value.append(s) value = quote(EMPTYSTRING.join(value)) if extended: (charset, language, value) = decode_rfc2231(value) new_params.append((name, (charset, language, '"%s"' % value))) else: new_params.append((name, '"%s"' % value)) return new_params