Skip to content

Commit

Permalink
Merge pull request #21 from tarosky/issue/20
Browse files Browse the repository at this point in the history
Fix incorrect path conversion
Close #20.
  • Loading branch information
harai authored Dec 7, 2023
2 parents 21fbabd + 675bef2 commit 6b9e5dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
55 changes: 32 additions & 23 deletions src/origin/request/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,32 @@ def respond_with_original(self) -> FieldUpdate:
return self.as_overridable(self.as_permanent(FieldUpdate()))

def process(self, path: str, accept_header: str) -> FieldUpdate:

def run(path: str):
if self.bypass_minifier_path_spec is not None and (
self.bypass_minifier_path_spec.match_file(path)):
return self.respond_with_original()

ext = get_normalized_extension(path)

if ext in image_exts:
if self.supports_webp(accept_header):
return self.res_with_generated_or_generate_and_res_with_original(
Location.from_path(path, self.generated_key_prefix, '.webp'))
else:
return self.generate_and_respond_with_original(
Location.from_path(path, self.generated_key_prefix, '.webp'))
elif ext in minifiable_exts:
return self.res_with_generated_or_generate_and_res_with_original(
Location.from_path(path, self.generated_key_prefix, ''))
elif ext == '.js.map':
return self.res_with_original_or_generated(
Location.from_path(path, self.generated_key_prefix, ''))
else:
# This condition includes:
# '.min.js' and '.min.css'
return self.respond_with_original()

if self.basedir != '':
if not path.startswith(self.basedir):
self.log.error(
Expand All @@ -461,31 +487,14 @@ def process(self, path: str, accept_header: str) -> FieldUpdate:
'path': path,
'basedir': self.basedir,
})
path = path[len(self.basedir):]

if self.bypass_minifier_path_spec is not None and (
self.bypass_minifier_path_spec.match_file(path)):
return self.respond_with_original()
else:
path = path[len(self.basedir):]

ext = get_normalized_extension(path)
fu = run(path)
if self.basedir != '' and fu.uri is None:
fu.uri = path

if ext in image_exts:
if self.supports_webp(accept_header):
return self.res_with_generated_or_generate_and_res_with_original(
Location.from_path(path, self.generated_key_prefix, '.webp'))
else:
return self.generate_and_respond_with_original(
Location.from_path(path, self.generated_key_prefix, '.webp'))
elif ext in minifiable_exts:
return self.res_with_generated_or_generate_and_res_with_original(
Location.from_path(path, self.generated_key_prefix, ''))
elif ext == '.js.map':
return self.res_with_original_or_generated(
Location.from_path(path, self.generated_key_prefix, ''))
else:
# This condition includes:
# '.min.js' and '.min.css'
return self.respond_with_original()
return fu


def lambda_main(event: Dict[str, Any]) -> Dict[str, Any]:
Expand Down
22 changes: 18 additions & 4 deletions src/origin/request/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,12 @@ class ImgserverBasedirTestCase(BaseTestCase):
def get_basedir(self) -> str:
return '/blog'

def test_normal(self) -> None:
def test_generated(self) -> None:
ts = self.put_original(JPG_NAME, JPEG_MIME)
self.put_generated(JPG_WEBP_NAME, JPEG_MIME, ts)

update = self._img_server.process(
'/blog' + self.to_path(JPG_NAME), CHROME_ACCEPT_HEADER)
f'/blog{self.to_path(JPG_NAME)}', CHROME_ACCEPT_HEADER)
self.assertEqual(
FieldUpdate(
res_cache_control=CACHE_CONTROL_PERM,
Expand All @@ -284,13 +284,27 @@ def test_normal(self) -> None:
), update)
self.assert_no_sqs_message()

def test_no_basedir(self) -> None:
def test_bypass(self) -> None:
update = self._img_server.process(
f'/blog{self.to_path(JS_NOMINIFY_NAME)}', CHROME_ACCEPT_HEADER)
self.assertEqual(
FieldUpdate(
res_cache_control=CACHE_CONTROL_PERM,
res_cache_control_overridable='true',
uri=self.to_path(JS_NOMINIFY_NAME)), update)
self.assert_no_sqs_message()

def test_no_basedir_generated(self) -> None:
ts = self.put_original(JPG_NAME, JPEG_MIME)
self.put_generated(JPG_WEBP_NAME, JPEG_MIME, ts)

update = self._img_server.process(
self.to_path(JPG_NAME), CHROME_ACCEPT_HEADER)
self.assertEqual(FieldUpdate(res_cache_control=CACHE_CONTROL_TEMP), update)
self.assertEqual(
FieldUpdate(
res_cache_control=CACHE_CONTROL_PERM,
origin_domain=self._img_server.generated_domain,
uri=self.to_uri(f'{JPG_NAME}.webp')), update)
self.assert_no_sqs_message()


Expand Down

0 comments on commit 6b9e5dd

Please sign in to comment.