Skip to content

Commit

Permalink
Revert improved multisite support #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Akihiro Harai committed Dec 4, 2023
1 parent 1c5ef4b commit 7c56b4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 70 deletions.
16 changes: 2 additions & 14 deletions src/origin/request/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
])

expiration_re = re.compile(r'\s*([\w-]+)="([^"]*)"(:?,|$)')
ms_subdir_re = re.compile(r'^/[_0-9a-zA-Z-]+(/wp-.*)$')

API_VERSION = 2

Expand Down Expand Up @@ -130,7 +129,6 @@ class XParams:
bypass_minifier_patterns: str
expiration_margin: int
basedir: str
is_multisite_subdir: bool


@dataclasses.dataclass
Expand Down Expand Up @@ -208,7 +206,6 @@ def __init__(
bypass_minifier_path_spec: Optional[PathSpec],
expiration_margin: int,
basedir: str,
is_multisite_subdir: bool,
):
self.log = log
self.region = region
Expand All @@ -224,7 +221,6 @@ def __init__(
self.bypass_minifier_path_spec = bypass_minifier_path_spec
self.expiration_margin = datetime.timedelta(seconds=expiration_margin)
self.basedir = basedir
self.is_multisite_subdir = is_multisite_subdir

@classmethod
def from_lambda(
Expand All @@ -245,8 +241,6 @@ def from_lambda(
bypass_minifier_patterns: str = get_header_or(
req, 'x-env-bypass-minifier-patterns')
basedir: str = get_header_or(req, 'x-env-basedir')
is_multisite_subdir: bool = bool(
get_header_or(req, 'x-env-is-multisite-subdir'))
except KeyError as e:
log.warning({
MESSAGE: 'environment variable not found',
Expand All @@ -264,8 +258,7 @@ def from_lambda(
temp_resp_max_age=temp_resp_max_age,
bypass_minifier_patterns=bypass_minifier_patterns,
expiration_margin=expiration_margin,
basedir=basedir,
is_multisite_subdir=is_multisite_subdir)
basedir=basedir)

if server_key not in cls.instances:
sqs = boto3.client('sqs', region_name=region)
Expand All @@ -286,8 +279,7 @@ def from_lambda(
temp_resp_max_age=temp_resp_max_age,
bypass_minifier_path_spec=path_spec,
expiration_margin=expiration_margin,
basedir=basedir,
is_multisite_subdir=is_multisite_subdir)
basedir=basedir)

return cls.instances[server_key]

Expand Down Expand Up @@ -471,10 +463,6 @@ def process(self, path: str, accept_header: str) -> FieldUpdate:
})
path = path[len(self.basedir):]

if self.is_multisite_subdir:
if m := ms_subdir_re.match(path):
path = m.group(1)

if self.bypass_minifier_path_spec is not None and (
self.bypass_minifier_path_spec.match_file(path)):
return self.respond_with_original()
Expand Down
69 changes: 13 additions & 56 deletions src/origin/request/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def generate_safe_random_string() -> str:

def create_img_server(
log: Logger, name: str, expiration_margin: int, key_prefix: str,
basedir: str, is_multisite_subdir: bool) -> ImgServer:
basedir: str) -> ImgServer:
account_id = read_test_config('aws-account-id')
sqs_name = f'test-{name}-{generate_safe_random_string()}'

Expand Down Expand Up @@ -109,8 +109,7 @@ def create_img_server(
bypass_minifier_path_spec=PathSpec.from_lines(
GitWildMatchPattern, get_bypass_minifier_patterns(key_prefix)),
expiration_margin=expiration_margin,
basedir=basedir,
is_multisite_subdir=is_multisite_subdir)
basedir=basedir)


def get_test_sqs_queue_name_from_url(sqs_queue_url: str) -> str:
Expand All @@ -123,10 +122,9 @@ def create_test_environment(
expiration_margin: int,
key_prefix: str,
basedir: str,
is_multisite_subdir: bool,
) -> ImgServer:
img_server = create_img_server(
log, name, expiration_margin, key_prefix, basedir, is_multisite_subdir)
log, name, expiration_margin, key_prefix, basedir)
img_server.sqs.create_queue(
QueueName=get_test_sqs_queue_name_from_url(img_server.sqs_queue_url))
return img_server
Expand Down Expand Up @@ -217,29 +215,22 @@ def setUp(self) -> None:

self._img_server = create_test_environment(
self._log, 'imglambda', self.get_expiration_margin(), self._key_prefix,
self.get_basedir(), self.is_multisite_subdir())
self.get_basedir())

def get_expiration_margin(self) -> int:
return 10

def get_basedir(self) -> str:
return ''

def is_multisite_subdir(self) -> bool:
return False

def put_original(
self, name: str, mime: str, dir: str = '') -> datetime.datetime:
def put_original(self, name: str, mime: str) -> datetime.datetime:
return put_original(
self._img_server, f'{dir}{self._key_prefix}{name}', name, mime)
self._img_server, f'{self._key_prefix}{name}', name, mime)

def put_generated(
self,
name: str,
mime: str,
timestamp: Optional[datetime.datetime],
dir: str = '') -> None:
key = f'{self._img_server.generated_key_prefix}{dir}{self._key_prefix}{name}'
self, name: str, mime: str,
timestamp: Optional[datetime.datetime]) -> None:
key = f'{self._img_server.generated_key_prefix}{self._key_prefix}{name}'

put_generated(self._img_server, key, name, mime, timestamp)

Expand All @@ -264,11 +255,11 @@ def assert_sqs_message(self, key: str) -> None:
},
}, self.receive_sqs_message())

def to_path(self, name: str, dir: str = '') -> str:
return f'/{dir}{self._key_prefix}{name}'
def to_path(self, name: str) -> str:
return f'/{self._key_prefix}{name}'

def to_uri(self, name: str, dir: str = '') -> str:
return f'/{self._img_server.generated_key_prefix}{dir}{self._key_prefix}{name}'
def to_uri(self, name: str) -> str:
return f'/{self._img_server.generated_key_prefix}{self._key_prefix}{name}'

def tearDown(self) -> None:
clean_test_environment(self._img_server)
Expand Down Expand Up @@ -303,40 +294,6 @@ def test_no_basedir(self) -> None:
self.assert_no_sqs_message()


class ImgserverMultisiteSubdirTestCase(BaseTestCase):

def is_multisite_subdir(self) -> bool:
return True

def test_subdir_wp(self) -> None:
ts = self.put_original(JPG_NAME, JPEG_MIME, 'wp-content/uploads/')
self.put_generated(JPG_WEBP_NAME, JPEG_MIME, ts, 'wp-content/uploads/')

update = self._img_server.process(
self.to_path(JPG_NAME, 'blog1/wp-content/uploads/'),
CHROME_ACCEPT_HEADER)
self.assertEqual(
FieldUpdate(
res_cache_control=CACHE_CONTROL_PERM,
origin_domain=self._img_server.generated_domain,
uri=self.to_uri(f'{JPG_NAME}.webp', 'wp-content/uploads/')), update)
self.assert_no_sqs_message()

def test_subdir(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_PERM,
origin_domain=self._img_server.generated_domain,
uri=self.to_uri(f'{JPG_NAME}.webp'),
), update)
self.assert_no_sqs_message()


class ImgserverExpiredTestCase(BaseTestCase):

def get_expiration_margin(self) -> int:
Expand Down

0 comments on commit 7c56b4e

Please sign in to comment.