Skip to content

Commit

Permalink
better immutable_file_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Sep 9, 2024
1 parent 54793d1 commit 19c6da9
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/servestatic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,23 @@ def __init__(
self.add_headers_function = add_headers_function
self._immutable_file_test = immutable_file_test
self._immutable_file_test_regex: re.Pattern | None = None
self.media_types = MediaTypes(extra_types=mimetypes)
self.application = application
self.files = {}
self.directories = []

if index_file is True:
self.index_file: str | None = "index.html"
elif isinstance(index_file, str):
self.index_file = index_file
else:
self.index_file = None

self.media_types = MediaTypes(extra_types=mimetypes)
self.application = application
self.files = {}
self.directories = []
if isinstance(immutable_file_test, str):
self.user_immutable_file_test = re.compile(immutable_file_test)
else:
self.user_immutable_file_test = immutable_file_test

if root is not None:
self.add_files(root, prefix)

Expand Down Expand Up @@ -202,15 +208,10 @@ def immutable_file_test(self, path, url):
This should be implemented by sub-classes (see e.g. ServeStaticMiddleware)
or by setting the `immutable_file_test` config option
"""
if self._immutable_file_test is not None:
if callable(self._immutable_file_test):
return self._immutable_file_test(path, url)
if isinstance(self._immutable_file_test, str):
if self._immutable_file_test_regex is None:
self._immutable_file_test_regex = re.compile(
self._immutable_file_test
)
return bool(self._immutable_file_test_regex.search(url))
if self.user_immutable_file_test is not None:
if callable(self.user_immutable_file_test):
return self.user_immutable_file_test(path, url)
return bool(self.user_immutable_file_test.search(url))
return False

def redirect(self, from_url, to_url):
Expand Down

0 comments on commit 19c6da9

Please sign in to comment.