| 48 | |
| 49 | |
| 50 | def parse_content_disposition(header): |
| 51 | def is_token(string): |
| 52 | return string and TOKEN >= set(string) |
| 53 | |
| 54 | def is_quoted(string): |
| 55 | return string[0] == string[-1] == &class="cm">#x27;"' |
| 56 | |
| 57 | def is_rfc5987(string): |
| 58 | return is_token(string) and string.count(class="st">"&class="cm">#x27;") == 2 |
| 59 | |
| 60 | def is_extended_param(string): |
| 61 | return string.endswith(&class="cm">#x27;*') |
| 62 | |
| 63 | def is_continuous_param(string): |
| 64 | pos = string.find(&class="cm">#x27;*') + 1 |
| 65 | if not pos: |
| 66 | return False |
| 67 | substring = string[pos:-1] if string.endswith(&class="cm">#x27;*') else string[pos:] |
| 68 | return substring.isdigit() |
| 69 | |
| 70 | def unescape(text, *, chars=&class="cm">#x27;'.join(map(re.escape, CHAR))): |
| 71 | return re.sub(&class="cm">#x27;\\\\([{}])class="st">'.format(chars), '\\1', text) |
| 72 | |
| 73 | if not header: |
| 74 | return None, {} |
| 75 | |
| 76 | disptype, *parts = header.split(&class="cm">#x27;;') |
| 77 | if not is_token(disptype): |
| 78 | warnings.warn(BadContentDispositionHeader(header)) |
| 79 | return None, {} |
| 80 | |
| 81 | params = {} |
| 82 | for item in parts: |
| 83 | if &class="cm">#x27;=' not in item: |
| 84 | warnings.warn(BadContentDispositionHeader(header)) |
| 85 | return None, {} |
| 86 | |
| 87 | key, value = item.split(&class="cm">#x27;=', 1) |
| 88 | key = key.lower().strip() |
| 89 | value = value.lstrip() |
| 90 | |
| 91 | if key in params: |
| 92 | warnings.warn(BadContentDispositionHeader(header)) |
| 93 | return None, {} |
| 94 | |
| 95 | if not is_token(key): |
| 96 | warnings.warn(BadContentDispositionParam(item)) |
| 97 | continue |
| 98 | |
| 99 | elif is_continuous_param(key): |
| 100 | if is_quoted(value): |
| 101 | value = unescape(value[1:-1]) |
| 102 | elif not is_token(value): |
| 103 | warnings.warn(BadContentDispositionParam(item)) |
| 104 | continue |
| 105 | |
| 106 | elif is_extended_param(key): |
| 107 | if is_rfc5987(value): |