MCPcopy
hub / github.com/aio-libs/aiohttp / parse_content_disposition

Function parse_content_disposition

aiohttp/multipart.py:50–129  ·  aiohttp/multipart.py::parse_content_disposition
(header)

Source from the content-addressed store, hash-verified

48
49
50def 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):

Callers 15

test_parse_emptyMethod · 0.90
test_inlonlyMethod · 0.90
test_inlonlyquotedMethod · 0.90
test_inlwithfnattachMethod · 0.90
test_attonlyMethod · 0.90
test_attonlyquotedMethod · 0.90
test_attonlyucaseMethod · 0.90

Calls 8

is_tokenFunction · 0.85
is_continuous_paramFunction · 0.85
is_quotedFunction · 0.85
unescapeFunction · 0.85
is_extended_paramFunction · 0.85
is_rfc5987Function · 0.85

Tested by 15

test_parse_emptyMethod · 0.72
test_inlonlyMethod · 0.72
test_inlonlyquotedMethod · 0.72
test_inlwithfnattachMethod · 0.72
test_attonlyMethod · 0.72
test_attonlyquotedMethod · 0.72
test_attonlyucaseMethod · 0.72