Parse a header value that consists of a list of comma separated items according to `RFC 9110 <https://httpwg.org/specs/rfc9110.html#abnf.extension>`__. Surrounding quotes are removed from items, but internal quotes are left for future parsing. Empty values are discarded. .. code-bl
(value: str)
| 301 | |
| 302 | |
| 303 | def parse_list_header(value: str) -> list[str]: |
| 304 | """Parse a header value that consists of a list of comma separated items according |
| 305 | to `RFC 9110 <https://httpwg.org/specs/rfc9110.html#abnf.extension>`__. |
| 306 | |
| 307 | Surrounding quotes are removed from items, but internal quotes are left for |
| 308 | future parsing. Empty values are discarded. |
| 309 | |
| 310 | .. code-block:: python |
| 311 | |
| 312 | parse_list_header('token, "quoted value"') |
| 313 | ['token', 'quoted value'] |
| 314 | |
| 315 | This is the reverse of :func:`dump_header`. |
| 316 | |
| 317 | :param value: The header value to parse. |
| 318 | |
| 319 | .. versionchanged:: 3.2 |
| 320 | Quotes and escapes are kept if only part of an item is quoted. Empty |
| 321 | values are omitted. An empty list is returned if the value contains an |
| 322 | unclosed quoted string. |
| 323 | """ |
| 324 | items = [] |
| 325 | item = "" |
| 326 | escape = False |
| 327 | quote = False |
| 328 | |
| 329 | for char in value: |
| 330 | if escape: |
| 331 | escape = False |
| 332 | item += char |
| 333 | continue |
| 334 | |
| 335 | if quote: |
| 336 | if char == "\\": |
| 337 | escape = True |
| 338 | elif char == '"': |
| 339 | quote = False |
| 340 | |
| 341 | item += char |
| 342 | continue |
| 343 | |
| 344 | if char == ",": |
| 345 | items.append(item) |
| 346 | item = "" |
| 347 | continue |
| 348 | |
| 349 | if char == '"': |
| 350 | quote = True |
| 351 | |
| 352 | item += char |
| 353 | |
| 354 | if quote: |
| 355 | # invalid, unclosed quoted string |
| 356 | return [] |
| 357 | |
| 358 | items.append(item) |
| 359 | return [ |
| 360 | unquote_header_value(item) for item in (item.strip() for item in items) if item |
no test coverage detected