Parse a Content-type like header. Return the main content-type and a dictionary of options.
(line)
| 236 | s = s[end:] |
| 237 | |
| 238 | def parse_header(line): |
| 239 | """Parse a Content-type like header. |
| 240 | |
| 241 | Return the main content-type and a dictionary of options. |
| 242 | |
| 243 | """ |
| 244 | parts = _parseparam(';' + line) |
| 245 | key = parts.__next__() |
| 246 | pdict = {} |
| 247 | for p in parts: |
| 248 | i = p.find('=') |
| 249 | if i >= 0: |
| 250 | name = p[:i].strip().lower() |
| 251 | value = p[i+1:].strip() |
| 252 | if len(value) >= 2 and value[0] == value[-1] == '"': |
| 253 | value = value[1:-1] |
| 254 | value = value.replace('\\\\', '\\').replace('\\"', '"') |
| 255 | pdict[name] = value |
| 256 | return key, pdict |
| 257 | |
| 258 | |
| 259 | # Classes for field storage |