Produce a header value from a list of items or ``key=value`` pairs, separated by commas ``,``. This is the reverse of :func:`parse_list_header`, :func:`parse_dict_header`, and :func:`parse_set_header`. If a value contains non-token characters, it will be quoted. If a value is
(iterable: dict[str, t.Any] | t.Iterable[t.Any])
| 240 | |
| 241 | |
| 242 | def dump_header(iterable: dict[str, t.Any] | t.Iterable[t.Any]) -> str: |
| 243 | """Produce a header value from a list of items or ``key=value`` pairs, separated by |
| 244 | commas ``,``. |
| 245 | |
| 246 | This is the reverse of :func:`parse_list_header`, :func:`parse_dict_header`, and |
| 247 | :func:`parse_set_header`. |
| 248 | |
| 249 | If a value contains non-token characters, it will be quoted. |
| 250 | |
| 251 | If a value is ``None``, the key is output alone. |
| 252 | |
| 253 | In some keys for some headers, a UTF-8 value can be encoded using a special |
| 254 | ``key*=UTF-8''value`` form, where ``value`` is percent encoded. This function will |
| 255 | not produce that format automatically, but if a given key ends with an asterisk |
| 256 | ``*``, the value is assumed to have that form and will not be quoted further. |
| 257 | |
| 258 | .. code-block:: python |
| 259 | |
| 260 | dump_header(["foo", "bar baz"]) |
| 261 | 'foo, "bar baz"' |
| 262 | |
| 263 | dump_header({"foo": "bar baz"}) |
| 264 | 'foo="bar baz"' |
| 265 | |
| 266 | :param iterable: The items to create a header from. |
| 267 | |
| 268 | .. versionchanged:: 3.0 |
| 269 | The ``allow_token`` parameter is removed. |
| 270 | |
| 271 | .. versionchanged:: 2.2.3 |
| 272 | If a key ends with ``*``, its value will not be quoted. |
| 273 | """ |
| 274 | if isinstance(iterable, dict): |
| 275 | items = [] |
| 276 | |
| 277 | for key, value in iterable.items(): |
| 278 | if value is None: |
| 279 | items.append(key) |
| 280 | elif key[-1] == "*": |
| 281 | items.append(f"{key}={value}") |
| 282 | else: |
| 283 | items.append(f"{key}={quote_header_value(value)}") |
| 284 | else: |
| 285 | items = [quote_header_value(x) for x in iterable] |
| 286 | |
| 287 | return ", ".join(items) |
| 288 | |
| 289 | |
| 290 | def dump_csp_header(header: ds.ContentSecurityPolicy) -> str: |