Returns a dictionary from HTTP header text. >>> h = HTTPHeaders.parse("Content-Type: text/html\\r\\nContent-Length: 42\\r\\n") >>> sorted(h.items()) [('Content-Length', '42'), ('Content-Type', 'text/html')] .. versionchanged:: 5.1 Raises `HTTPInputError`
(cls, headers: str)
| 183 | |
| 184 | @classmethod |
| 185 | def parse(cls, headers: str) -> "HTTPHeaders": |
| 186 | """Returns a dictionary from HTTP header text. |
| 187 | |
| 188 | >>> h = HTTPHeaders.parse("Content-Type: text/html\\r\\nContent-Length: 42\\r\\n") |
| 189 | >>> sorted(h.items()) |
| 190 | [('Content-Length', '42'), ('Content-Type', 'text/html')] |
| 191 | |
| 192 | .. versionchanged:: 5.1 |
| 193 | |
| 194 | Raises `HTTPInputError` on malformed headers instead of a |
| 195 | mix of `KeyError`, and `ValueError`. |
| 196 | |
| 197 | """ |
| 198 | h = cls() |
| 199 | # RFC 7230 section 3.5: a recipient MAY recognize a single LF as a line |
| 200 | # terminator and ignore any preceding CR. |
| 201 | for line in headers.split("\n"): |
| 202 | if line.endswith("\r"): |
| 203 | line = line[:-1] |
| 204 | if line: |
| 205 | h.parse_line(line) |
| 206 | return h |
| 207 | |
| 208 | # MutableMapping abstract method implementations. |
| 209 |
no test coverage detected