Given the request and response headers, return `True` if an HTTP "Not Modified" response could be returned instead.
(self, response_headers: Headers, request_headers: Headers)
| 209 | raise RuntimeError(f"StaticFiles path '{self.directory}' is not a directory.") |
| 210 | |
| 211 | def is_not_modified(self, response_headers: Headers, request_headers: Headers) -> bool: |
| 212 | """ |
| 213 | Given the request and response headers, return `True` if an HTTP |
| 214 | "Not Modified" response could be returned instead. |
| 215 | """ |
| 216 | if if_none_match := request_headers.get("if-none-match"): |
| 217 | if if_none_match.strip() == "*": |
| 218 | return True |
| 219 | # The "etag" header is added by FileResponse, so it's always present. |
| 220 | etag = response_headers["etag"] |
| 221 | return etag in [tag.strip().removeprefix("W/") for tag in if_none_match.split(",")] |
| 222 | |
| 223 | try: |
| 224 | if_modified_since = parsedate(request_headers["if-modified-since"]) |
| 225 | last_modified = parsedate(response_headers["last-modified"]) |
| 226 | if if_modified_since is not None and last_modified is not None and if_modified_since >= last_modified: |
| 227 | return True |
| 228 | except KeyError: |
| 229 | pass |
| 230 | |
| 231 | return False |