Try to guess the appropriate response based on the body content. This method is a bit magic and could be improved in the future, but it's not meant to be used except for special cases where response types cannot be guess using more straightforward methods.
(self, body: bytes)
| 104 | return Response |
| 105 | |
| 106 | def from_body(self, body: bytes) -> type[Response]: |
| 107 | """Try to guess the appropriate response based on the body content. |
| 108 | This method is a bit magic and could be improved in the future, but |
| 109 | it's not meant to be used except for special cases where response types |
| 110 | cannot be guess using more straightforward methods.""" |
| 111 | chunk = body[:5000] |
| 112 | chunk = to_bytes(chunk) |
| 113 | if not binary_is_text(chunk): |
| 114 | return self.from_mimetype("application/octet-stream") |
| 115 | lowercase_chunk = chunk.lower() |
| 116 | if b"<html>" in lowercase_chunk: |
| 117 | return self.from_mimetype("text/html") |
| 118 | if b"<?xml" in lowercase_chunk: |
| 119 | return self.from_mimetype("text/xml") |
| 120 | if b"<!doctype html>" in lowercase_chunk: |
| 121 | return self.from_mimetype("text/html") |
| 122 | return self.from_mimetype("text") |
| 123 | |
| 124 | def from_args( |
| 125 | self, |