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