From 675bef2967121a03989a35047bd9fdc2aea96072 Mon Sep 17 00:00:00 2001 From: Akihiro Harai Date: Thu, 7 Dec 2023 15:16:56 +0900 Subject: [PATCH] Fix incorrect path conversion #20 --- src/origin/request/index.py | 55 +++++++++++++++++++------------- src/origin/request/test_index.py | 22 ++++++++++--- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/origin/request/index.py b/src/origin/request/index.py index d6cb0e1..c517e49 100644 --- a/src/origin/request/index.py +++ b/src/origin/request/index.py @@ -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( @@ -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]: diff --git a/src/origin/request/test_index.py b/src/origin/request/test_index.py index 49d9a8b..f79145f 100644 --- a/src/origin/request/test_index.py +++ b/src/origin/request/test_index.py @@ -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, @@ -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()